MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Node.cpp
Go to the documentation of this file.
1#include "Node.hpp"
2
3namespace MayaFlux::Nodes {
4
5void Node::enable_mock_process(bool mock_process)
6{
7 if (mock_process) {
9 } else {
11 }
12}
13
15{
16 return m_state.load() & NodeState::MOCK_PROCESS;
17}
18
19void Node::on_tick(const NodeHook& callback)
20{
22}
23
24void Node::on_tick_if(const NodeCondition& condition, const NodeHook& callback)
25{
27}
28
29bool Node::remove_hook(const NodeHook& callback)
30{
31 return safe_remove_callback(m_callbacks, callback);
32}
33
38
40{
41 m_callbacks.clear();
43}
44
45void Node::register_channel_usage(uint32_t channel_id)
46{
47 if (channel_id >= 32)
48 return;
49
50 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
51
52 m_active_channels_mask.fetch_or(channel_bit, std::memory_order_acq_rel);
53}
54
55void Node::unregister_channel_usage(uint32_t channel_id)
56{
57 if (channel_id >= 32)
58 return;
59 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
60
61 m_active_channels_mask.fetch_and(~channel_bit, std::memory_order_acq_rel);
62 m_pending_reset_mask.fetch_and(~channel_bit, std::memory_order_acq_rel);
63}
64
65bool Node::is_used_by_channel(uint32_t channel_id) const
66{
67 if (channel_id >= 32)
68 return false;
69
70 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
71 uint32_t active_mask = m_active_channels_mask.load(std::memory_order_acquire);
72 return (active_mask & channel_bit) != 0;
73}
74
75void Node::request_reset_from_channel(uint32_t channel_id)
76{
77 if (channel_id >= 32)
78 return;
79 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
80 uint32_t old_pending = m_pending_reset_mask.fetch_or(channel_bit, std::memory_order_acq_rel);
81 uint32_t new_pending = old_pending | channel_bit;
82 uint32_t active_channels = m_active_channels_mask.load(std::memory_order_acquire);
83
84 if ((new_pending & active_channels) == active_channels && active_channels != 0) {
85 uint32_t expected = new_pending;
86 if (m_pending_reset_mask.compare_exchange_strong(expected, 0, std::memory_order_acq_rel)) {
88 }
89 }
90}
91
92[[nodiscard]] std::span<const float> Node::get_gpu_data_buffer() const
93{
94 return { m_gpu_data_buffer.data(), m_gpu_data_buffer.size() };
95}
96
98{
99 uint32_t active_mask = m_active_channels_mask.load(std::memory_order_acquire);
100 if (active_mask == 0) {
102 }
103}
104
109
110bool Node::try_claim_snapshot_context(uint64_t context_id)
111{
112 uint64_t expected = 0;
113 return m_snapshot_context_id.compare_exchange_strong(
114 expected, context_id,
115 std::memory_order_acq_rel,
116 std::memory_order_acquire);
117}
118
119bool Node::is_in_snapshot_context(uint64_t context_id) const
120{
121 return m_snapshot_context_id.load(std::memory_order_acquire) == context_id;
122}
123
124void Node::release_snapshot_context(uint64_t context_id)
125{
126 uint64_t expected = context_id;
127 m_snapshot_context_id.compare_exchange_strong(
128 expected, 0,
129 std::memory_order_release,
130 std::memory_order_relaxed);
131}
132
134{
135 return m_snapshot_context_id.load(std::memory_order_acquire) != 0;
136}
137
139{
140 m_buffer_count.fetch_add(1, std::memory_order_release);
141}
142
144{
145 m_buffer_count.fetch_sub(1, std::memory_order_release);
146}
147
149{
150 uint32_t count = m_buffer_count.load(std::memory_order_acquire);
151 auto state = m_state.load(std::memory_order_acquire);
152
153 if (count >= 1 && state == NodeState::INACTIVE) {
154 if (count == 1) {
155 return true;
156 }
157 bool expected = false;
158 if (m_buffer_processed.compare_exchange_strong(expected, true,
159 std::memory_order_acq_rel)) {
160 m_buffer_reset_count.fetch_add(1, std::memory_order_release);
161 return true;
162 }
163 }
164 return false;
165}
166
168{
169 uint32_t reset_count = m_buffer_reset_count.fetch_add(1, std::memory_order_acq_rel);
170 uint32_t buffer_count = m_buffer_count.load(std::memory_order_acquire);
171
172 if (reset_count + 1 == buffer_count || buffer_count <= 1) {
173 m_buffer_processed.store(false, std::memory_order_release);
174 m_buffer_reset_count.store(0, std::memory_order_release);
175 }
176}
177
178}
size_t count
bool try_claim_snapshot_context(uint64_t context_id)
Attempt to claim snapshot context for this processing cycle.
Definition Node.cpp:110
std::atomic< uint32_t > m_active_channels_mask
Bitmask tracking which channels are currently using this node.
Definition Node.hpp:732
bool has_active_snapshot() const
Check if node is currently being snapshotted by any context.
Definition Node.cpp:133
void add_buffer_reference()
Increments the buffer reference count This method is called when a new buffer starts using this node ...
Definition Node.cpp:138
virtual void on_tick_if(const NodeCondition &condition, const NodeHook &callback)
Registers a conditional callback.
Definition Node.cpp:24
bool is_in_snapshot_context(uint64_t context_id) const
Check if currently in a snapshot context.
Definition Node.cpp:119
void request_buffer_reset()
Requests a reset of the buffer state.
Definition Node.cpp:167
void remove_buffer_reference()
Decrements the buffer reference count This method is called when a buffer stops using this node to en...
Definition Node.cpp:143
std::atomic< uint32_t > m_pending_reset_mask
Bitmask tracking which channels have requested a reset.
Definition Node.hpp:741
std::atomic< bool > m_buffer_processed
Flag indicating whether the buffer has been processed This atomic flag is set when the buffer has bee...
Definition Node.hpp:767
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
Definition Node.hpp:454
std::atomic< uint64_t > m_snapshot_context_id
Unique identifier for the current snapshot context.
Definition Node.hpp:752
virtual void reset_processed_state()
Resets the processed state of the node and any attached input nodes.
Definition Node.cpp:97
std::atomic< NodeState > m_state
Atomic state flag tracking the node's processing status.
Definition Node.hpp:522
std::atomic< uint32_t > m_buffer_reset_count
Counter tracking how many buffers have requested a reset.
Definition Node.hpp:776
virtual void enable_mock_process(bool mock_process)
Allows RootNode to process the Generator without using the processed sample.
Definition Node.cpp:5
bool is_used_by_channel(uint32_t channel_id) const
Checks if the node is currently used by a specific channel.
Definition Node.cpp:65
virtual bool should_mock_process() const
Checks if the generator should mock process.
Definition Node.cpp:14
void unregister_channel_usage(uint32_t channel_id)
Removes the specified channel from the usage tracking.
Definition Node.cpp:55
virtual void reset_processed_state_internal()
Resets the processed state of the node directly.
Definition Node.cpp:105
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
Definition Node.hpp:464
std::atomic< uint32_t > m_buffer_count
Counter tracking how many buffers are using this node This counter is incremented when a buffer start...
Definition Node.hpp:760
virtual void remove_all_hooks()
Removes all registered callbacks.
Definition Node.cpp:39
virtual bool remove_hook(const NodeHook &callback)
Removes a previously registered callback.
Definition Node.cpp:29
void register_channel_usage(uint32_t channel_id)
Mark the specificed channel as a processor/user.
Definition Node.cpp:45
void request_reset_from_channel(uint32_t channel_id)
Requests a reset of the processed state from a specific channel.
Definition Node.cpp:75
virtual bool remove_conditional_hook(const NodeCondition &callback)
Removes a previously registered conditional callback.
Definition Node.cpp:34
std::vector< float > m_gpu_data_buffer
GPU data buffer for context objects.
Definition Node.hpp:444
std::span< const float > get_gpu_data_buffer() const
Provides access to the GPU data buffer.
Definition Node.cpp:92
bool mark_buffer_processed()
Marks the node as having been processed by a buffer.
Definition Node.cpp:148
virtual void on_tick(const NodeHook &callback)
Registers a callback to be called on each tick.
Definition Node.cpp:19
void release_snapshot_context(uint64_t context_id)
Release snapshot context.
Definition Node.cpp:124
TypedHook<> NodeHook
Alias for TypedHook<NodeContext>.
Definition NodeUtils.hpp:38
@ PROCESSED
Node has been processed this cycle.
Definition NodeSpec.hpp:49
@ INACTIVE
Engine is not processing this node.
Definition NodeSpec.hpp:44
@ MOCK_PROCESS
Node should be processed but output ignored.
Definition NodeSpec.hpp:48
void atomic_add_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically adds a flag to a node state.
Definition NodeUtils.cpp:60
bool safe_remove_conditional_callback(std::vector< std::pair< NodeHook, NodeCondition > > &callbacks, const NodeCondition &callback)
Removes all conditional callbacks whose condition target_type() matches.
Definition NodeUtils.cpp:32
void atomic_remove_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically removes a flag from a node state.
Definition NodeUtils.cpp:71
bool safe_remove_callback(std::vector< TypedHook< ContextT > > &callbacks, const TypedHook< ContextT > &callback)
Removes all callbacks whose target_type() matches that of the supplied callback.
Definition NodeUtils.hpp:92
std::function< bool(NodeContext &)> NodeCondition
Predicate function type for conditional callbacks.
Definition NodeUtils.hpp:54
bool safe_add_conditional_callback(std::vector< std::pair< NodeHook, NodeCondition > > &callbacks, const NodeHook &callback, const NodeCondition &condition)
Adds a conditional callback if the exact pair is not already present.
Definition NodeUtils.cpp:23
bool safe_add_callback(std::vector< TypedHook< ContextT > > &callbacks, const TypedHook< ContextT > &callback)
Adds a callback to the collection if an equivalent one is not already present.
Definition NodeUtils.hpp:77
Contains the node-based computational processing system components.
Definition Chronie.hpp:14