MayaFlux 1.0.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
13
18
20{
21 return m_callbacks.add_unique(callback, callback.target_type());
22}
23
25{
26 return m_conditional_callbacks.add_conditional_unique(callback, condition, callback.target_type());
27}
28
30{
31 return m_callbacks.remove_by_key(callback.target_type());
32}
33
38
40{
41 for (auto* list : m_hook_lists) {
42 list->clear();
43 }
44}
45
47{
48 return std::ranges::any_of(m_hook_lists, [id](HookList* list) { return list->remove(id); });
49}
50
51void Node::register_channel_usage(uint32_t channel_id)
52{
53 if (channel_id >= 32)
54 return;
55
56 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
57
58 m_active_channels_mask.fetch_or(channel_bit, std::memory_order_acq_rel);
59}
60
61void Node::unregister_channel_usage(uint32_t channel_id)
62{
63 if (channel_id >= 32)
64 return;
65 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
66
67 m_active_channels_mask.fetch_and(~channel_bit, std::memory_order_acq_rel);
68 m_pending_reset_mask.fetch_and(~channel_bit, std::memory_order_acq_rel);
69}
70
71bool Node::is_used_by_channel(uint32_t channel_id) const
72{
73 if (channel_id >= 32)
74 return false;
75
76 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
77 uint32_t active_mask = m_active_channels_mask.load(std::memory_order_acquire);
78 return (active_mask & channel_bit) != 0;
79}
80
81void Node::request_reset_from_channel(uint32_t channel_id)
82{
83 if (channel_id >= 32)
84 return;
85 auto channel_bit = static_cast<uint32_t>((0x0ffffffff) & (1ULL << (uint64_t)channel_id));
86 uint32_t old_pending = m_pending_reset_mask.fetch_or(channel_bit, std::memory_order_acq_rel);
87 uint32_t new_pending = old_pending | channel_bit;
88 uint32_t active_channels = m_active_channels_mask.load(std::memory_order_acquire);
89
90 if ((new_pending & active_channels) == active_channels && active_channels != 0) {
91 uint32_t expected = new_pending;
92 if (m_pending_reset_mask.compare_exchange_strong(expected, 0, std::memory_order_acq_rel)) {
94 }
95 }
96}
97
98[[nodiscard]] std::span<const float> Node::get_gpu_data_buffer() const
99{
100 return { m_gpu_data_buffer.data(), m_gpu_data_buffer.size() };
101}
102
104{
105 uint32_t active_mask = m_active_channels_mask.load(std::memory_order_acquire);
106 if (active_mask == 0) {
108 }
109}
110
115
116bool Node::try_claim_snapshot_context(uint64_t context_id)
117{
118 uint64_t expected = 0;
119 return m_snapshot_context_id.compare_exchange_strong(
120 expected, context_id,
121 std::memory_order_acq_rel,
122 std::memory_order_acquire);
123}
124
125bool Node::is_in_snapshot_context(uint64_t context_id) const
126{
127 return m_snapshot_context_id.load(std::memory_order_acquire) == context_id;
128}
129
130void Node::release_snapshot_context(uint64_t context_id)
131{
132 uint64_t expected = context_id;
133 m_snapshot_context_id.compare_exchange_strong(
134 expected, 0,
135 std::memory_order_release,
136 std::memory_order_relaxed);
137}
138
140{
141 return m_snapshot_context_id.load(std::memory_order_acquire) != 0;
142}
143
145{
146 m_buffer_count.fetch_add(1, std::memory_order_release);
147}
148
150{
151 m_buffer_count.fetch_sub(1, std::memory_order_release);
152}
153
155{
156 uint32_t count = m_buffer_count.load(std::memory_order_acquire);
157 auto state = m_state.load(std::memory_order_acquire);
158
159 if (count >= 1 && state == NodeState::INACTIVE) {
160 if (count == 1) {
161 return true;
162 }
163 bool expected = false;
164 if (m_buffer_processed.compare_exchange_strong(expected, true,
165 std::memory_order_acq_rel)) {
166 m_buffer_reset_count.fetch_add(1, std::memory_order_release);
167 return true;
168 }
169 }
170 return false;
171}
172
174{
175 uint32_t reset_count = m_buffer_reset_count.fetch_add(1, std::memory_order_acq_rel);
176 uint32_t buffer_count = m_buffer_count.load(std::memory_order_acquire);
177
178 if (reset_count + 1 == buffer_count || buffer_count <= 1) {
179 m_buffer_processed.store(false, std::memory_order_release);
180 m_buffer_reset_count.store(0, std::memory_order_release);
181 }
182}
183
184}
PickCallback callback
Definition Picker.cpp:22
size_t count
bool remove_by_key(const std::type_info &key)
Removes every entry registered with this callable type.
bool remove(HookId id)
Removes the registration with this id.
HookId add_conditional_unique(NodeHook hook, NodeCondition condition, const std::type_info &key, uint8_t tag=0)
As add_conditional, returning the existing id when the same hook and condition types exist.
bool remove_by_condition_key(const std::type_info &key)
Removes every entry whose condition has this callable type.
HookId add_unique(NodeHook hook, const std::type_info &key, uint8_t tag=0, std::array< double, 2 > params={})
Appends unless an entry with the same key and params exists, in which case that entry's id is returne...
Stores, fires and clears the hooks of one node channel.
Definition NodeUtils.hpp:93
bool try_claim_snapshot_context(uint64_t context_id)
Attempt to claim snapshot context for this processing cycle.
Definition Node.cpp:116
std::atomic< uint32_t > m_active_channels_mask
Bitmask tracking which channels are currently using this node.
Definition Node.hpp:753
virtual HookId on_tick(const NodeHook &callback)
Registers a callback to be called on each tick.
Definition Node.cpp:19
bool has_active_snapshot() const
Check if node is currently being snapshotted by any context.
Definition Node.cpp:139
void add_buffer_reference()
Increments the buffer reference count This method is called when a new buffer starts using this node ...
Definition Node.cpp:144
HookList m_conditional_callbacks
Hooks fired on a tick only while their condition holds.
Definition Node.hpp:485
bool is_in_snapshot_context(uint64_t context_id) const
Check if currently in a snapshot context.
Definition Node.cpp:125
void request_buffer_reset()
Requests a reset of the buffer state.
Definition Node.cpp:173
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:149
std::atomic< uint32_t > m_pending_reset_mask
Bitmask tracking which channels have requested a reset.
Definition Node.hpp:762
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:788
std::atomic< uint64_t > m_snapshot_context_id
Unique identifier for the current snapshot context.
Definition Node.hpp:773
virtual void reset_processed_state()
Resets the processed state of the node and any attached input nodes.
Definition Node.cpp:103
std::atomic< NodeState > m_state
Atomic state flag tracking the node's processing status.
Definition Node.hpp:543
virtual bool unhook(HookId id)
Removes the one registration with this id, whichever hook list holds it.
Definition Node.cpp:46
virtual bool is_output_excluded_from_graph() const
True if the node's output is excluded from the graph.
Definition Node.cpp:14
std::atomic< uint32_t > m_buffer_reset_count
Counter tracking how many buffers have requested a reset.
Definition Node.hpp:797
HookList m_callbacks
Hooks fired on every tick.
Definition Node.hpp:478
bool is_used_by_channel(uint32_t channel_id) const
Checks if the node is currently used by a specific channel.
Definition Node.cpp:71
virtual void exclude_output_from_graph(bool excluded=true)
Exclude this node's output from the graph it is registered in.
Definition Node.cpp:5
void unregister_channel_usage(uint32_t channel_id)
Removes the specified channel from the usage tracking.
Definition Node.cpp:61
virtual void reset_processed_state_internal()
Resets the processed state of the node directly.
Definition Node.cpp:111
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:781
virtual void remove_all_hooks()
Removes all registered callbacks.
Definition Node.cpp:39
virtual HookId on_tick_if(const NodeCondition &condition, const NodeHook &callback)
Registers a conditional callback.
Definition Node.cpp:24
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:51
void request_reset_from_channel(uint32_t channel_id)
Requests a reset of the processed state from a specific channel.
Definition Node.cpp:81
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:460
std::span< const float > get_gpu_data_buffer() const
Provides access to the GPU data buffer.
Definition Node.cpp:98
std::vector< HookList * > m_hook_lists
Definition Node.hpp:469
bool mark_buffer_processed()
Marks the node as having been processed by a buffer.
Definition Node.cpp:154
void release_snapshot_context(uint64_t context_id)
Release snapshot context.
Definition Node.cpp:130
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
@ OUTPUT_EXCLUDED
Node is processed but its output is left out of the graph.
Definition NodeSpec.hpp:48
uint64_t HookId
Identifies one registration inside a HookList.
Definition NodeUtils.hpp:60
void atomic_remove_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically removes a flag from a node state.
std::function< bool(NodeContext &)> NodeCondition
Predicate function type for conditional callbacks.
Definition NodeUtils.hpp:54
void atomic_add_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically adds a flag to a node state.
Contains the node-based computational processing system components.
Definition Chronie.hpp:14