MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
NodeCombine.cpp
Go to the documentation of this file.
1#include "NodeCombine.hpp"
2
4
6
7namespace MayaFlux::Nodes {
8
9BinaryOpContext::BinaryOpContext(double value, double lhs_value, double rhs_value)
10 : NodeContext(value, typeid(BinaryOpContext).name())
11 , lhs_value(lhs_value)
12 , rhs_value(rhs_value)
13{
14}
15
16BinaryOpContextGpu::BinaryOpContextGpu(double value, double lhs_value, double rhs_value, std::span<const float> gpu_data)
17 : BinaryOpContext(value, lhs_value, rhs_value)
18 , GpuVectorData(gpu_data)
19{
20 type_id = typeid(BinaryOpContextGpu).name();
21}
22
23CompositeOpContext::CompositeOpContext(double value, std::vector<double> input_values)
24 : NodeContext(value, typeid(CompositeOpContext).name())
25 , input_values(std::move(input_values))
26{
27}
28
29CompositeOpContextGpu::CompositeOpContextGpu(double value, std::vector<double> input_values, std::span<const float> gpu_data)
30 : CompositeOpContext(value, std::move(input_values))
31 , GpuVectorData(gpu_data)
32{
33 type_id = typeid(CompositeOpContextGpu).name();
34}
35
36BinaryOpNode::BinaryOpNode(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Node>& rhs, CombineFunc func)
37 : m_lhs(lhs)
38 , m_rhs(rhs)
39 , m_func(std::move(func))
40 , m_context(0.0, 0.0, 0.0)
41 , m_context_gpu(0.0, 0.0, 0.0, get_gpu_data_buffer())
42{
43 if (!m_lhs || !m_rhs) {
44 error<std::invalid_argument>(
46 std::source_location::current(),
47 "BinaryOpNode requires both lhs and rhs nodes to be non-null");
48 }
49}
50
52 const std::shared_ptr<Node>& lhs,
53 const std::shared_ptr<Node>& rhs,
54 CombineFunc func,
55 NodeGraphManager& manager,
56 ProcessingToken token)
57 : BinaryOpNode(lhs, rhs, std::move(func))
58{
59 m_manager = &manager;
60 m_token = token;
61}
62
64{
65 if (!m_is_initialized) {
66
67 if (m_manager) {
68 auto self = shared_from_this();
69 uint32_t lhs_mask = m_lhs ? m_lhs->get_channel_mask().load() : 0;
70 uint32_t rhs_mask = m_rhs ? m_rhs->get_channel_mask().load() : 0;
71 uint32_t combined_mask = lhs_mask | rhs_mask;
72
73 if (combined_mask != 0) {
74 for (auto& channel : get_active_channels(combined_mask, 0)) {
75 m_manager->add_to_root(self, m_token, channel);
76 }
77 } else {
78 m_manager->add_to_root(self, m_token, 0);
79 }
80 m_is_initialized = true;
81 }
82 }
83
84 if (m_manager) {
86 switch (semantics) {
88 if (m_lhs) {
89 for (auto& channel : get_active_channels(m_lhs, 0)) {
91 }
92 }
93 if (m_rhs) {
94 for (auto& channel : get_active_channels(m_rhs, 0)) {
96 }
97 }
98 break;
100 default:
101 break;
102 }
103 }
104}
105
107{
108 if (!m_lhs || !m_rhs) {
109 return input;
110 }
111
112 if (!is_initialized())
113 initialize();
114
115 atomic_inc_modulator_count(m_lhs->m_modulator_count, 1);
116 atomic_inc_modulator_count(m_rhs->m_modulator_count, 1);
117
118 uint32_t lstate = m_lhs->m_state.load();
119 if (lstate & NodeState::PROCESSED) {
120 m_last_lhs_value = input + m_lhs->get_last_output();
121 } else {
122 m_last_lhs_value = m_lhs->process_sample(input);
124 }
125
126 uint32_t rstate = m_rhs->m_state.load();
127 if (rstate & NodeState::PROCESSED) {
128 m_last_rhs_value = input + m_rhs->get_last_output();
129 } else {
130 m_last_rhs_value = m_rhs->process_sample(input);
132 }
133
135
138
139 atomic_dec_modulator_count(m_lhs->m_modulator_count, 1);
140 atomic_dec_modulator_count(m_rhs->m_modulator_count, 1);
141
144
145 return m_last_output;
146}
147
148std::vector<double> BinaryOpNode::process_batch(unsigned int num_samples)
149{
150 std::vector<double> output(num_samples);
151 for (unsigned int i = 0; i < num_samples; ++i) {
152 output[i] = process_sample(0.0);
153 }
154 return output;
155}
156
158{
159 update_context(value);
160 auto& ctx = get_last_context();
161
162 for (const auto& callback : m_callbacks) {
163 callback(ctx);
164 }
165
166 for (const auto& [callback, condition] : m_conditional_callbacks) {
167 if (condition(ctx)) {
168 callback(ctx);
169 }
170 }
171}
172
174{
176 if (m_lhs)
177 m_lhs->reset_processed_state();
178 if (m_rhs)
179 m_rhs->reset_processed_state();
180}
181
183{
186
187 if (m_lhs)
188 m_lhs->save_state();
189 if (m_rhs)
190 m_rhs->save_state();
191
192 m_state_saved = true;
193}
194
196{
199
200 if (m_lhs)
201 m_lhs->restore_state();
202 if (m_rhs)
203 m_rhs->restore_state();
204
205 m_state_saved = false;
206}
207
220
222{
223 if (m_gpu_compatible) {
224 return m_context_gpu;
225 }
226 return m_context;
227}
228
230{
231 auto lstate = m_lhs->m_state.load();
232 auto rstate = m_rhs->m_state.load();
233 bool is_lhs_registered = m_lhs ? (lstate & NodeState::ACTIVE) : false;
234 bool is_rhs_registered = m_rhs ? (rstate & NodeState::ACTIVE) : false;
235 return !is_lhs_registered && !is_rhs_registered;
236}
237
238namespace detail {
239
241 const std::vector<std::shared_ptr<Node>>& inputs,
242 NodeGraphManager& manager,
243 ProcessingToken token,
244 const std::shared_ptr<Node>& self)
245 {
246 uint32_t combined_mask = 0;
247 for (const auto& input : inputs)
248 combined_mask |= input->get_channel_mask().load();
249
250 if (combined_mask != 0) {
251 for (auto ch : get_active_channels(combined_mask, 0))
252 manager.add_to_root(self, token, ch);
253 } else {
254 manager.add_to_root(self, token, 0);
255 }
256 }
257
259 const std::vector<std::shared_ptr<Node>>& inputs,
260 NodeGraphManager& manager,
261 ProcessingToken token)
262 {
264 return;
265
266 for (const auto& input : inputs) {
267 for (auto ch : get_active_channels(input, 0))
268 manager.remove_from_root(input, token, ch);
269 }
270 }
271
272 void composite_validate(const std::vector<std::shared_ptr<Node>>& inputs, size_t N)
273 {
274 if (N > 0 && inputs.size() != N) {
275 error<std::invalid_argument>(
277 std::source_location::current(),
278 "CompositeOpNode<{}> requires exactly {} inputs, got {}",
279 N, N, inputs.size());
280 }
281
282 if (inputs.size() < 2) {
283 error<std::invalid_argument>(
285 std::source_location::current(),
286 "CompositeOpNode requires at least 2 inputs, got {}",
287 inputs.size());
288 }
289
290 for (size_t i = 0; i < inputs.size(); ++i) {
291 if (!inputs[i]) {
292 error<std::invalid_argument>(
294 std::source_location::current(),
295 "CompositeOpNode input at index {} is null", i);
296 }
297 }
298 }
299}
300
301} // namespace MayaFlux::Nodes
#define N(method_name, full_type_name)
Definition Creator.hpp:183
BinaryOpContextGpu(double value, double lhs_value, double rhs_value, std::span< const float > gpu_data)
GPU-compatible context for binary operation callbacks.
double rhs_value
The value from the right-hand side node.
BinaryOpContext(double value, double lhs_value, double rhs_value)
Constructs a BinaryOpContext with the current operation state.
double lhs_value
The value from the left-hand side node.
Specialized context for binary operation callbacks.
bool m_is_initialized
Flag indicating whether the binary operator has been properly initialized.
void restore_state() override
Restores the node's state from the last save Recursively cascades through all connected modulator nod...
ProcessingToken m_token
The processing token indicating the domain in which this node operates.
void update_context(double value) override
updates context object for callbacks
NodeGraphManager * m_manager
Reference to the node graph manager for registration and callback management.
void initialize()
Initializes the binary operation node.
NodeContext & get_last_context() override
Retrieves the last created context object.
void notify_tick(double value) override
Notifies all registered callbacks about a new output value.
std::shared_ptr< Node > m_lhs
The left-hand side node.
std::function< double(double, double)> CombineFunc
Function type for combining two node outputs.
void reset_processed_state() override
Resets the processed state of the node and any attached input nodes.
CombineFunc m_func
The function used to combine the outputs of both nodes.
double process_sample(double input=0.) override
Processes a single sample through both nodes and combines the results.
void save_state() override
Saves the node's current state for later restoration Recursively cascades through all connected modul...
std::vector< double > process_batch(unsigned int num_samples) override
Processes multiple samples through both nodes and combines the results.
double m_last_rhs_value
The last output value from the right-hand side node.
double m_last_lhs_value
The last output value from the left-hand side node.
BinaryOpNode(const std::shared_ptr< Node > &lhs, const std::shared_ptr< Node > &rhs, CombineFunc func)
Creates a new binary operation node.
std::shared_ptr< Node > m_rhs
The right-hand side node.
BinaryOpContextGpu m_context_gpu
Combines the outputs of two nodes using a binary operation.
CompositeOpContextGpu(double value, std::vector< double > input_values, std::span< const float > gpu_data)
GPU-compatible context for composite operation callbacks.
CompositeOpContext(double value, std::vector< double > input_values)
Context for N-ary composite operation callbacks.
GPU-uploadable 1D array data interface.
std::string type_id
Type identifier for runtime type checking.
Definition Node.hpp:48
double value
Current sample value.
Definition Node.hpp:40
Base context class for node callbacks.
Definition Node.hpp:30
void add_to_root(const std::shared_ptr< Node > &node, ProcessingToken token, unsigned int channel=0)
Add node to specific processing token and channel.
void remove_from_root(const std::shared_ptr< Node > &node, ProcessingToken token, unsigned int channel=0)
Remove node from a specific processing token and channel.
NodeConfig & get_node_config()
Gets the current node configuration.
Central manager for the computational processing node graph.
std::vector< NodeHook > m_callbacks
Collection of standard callback functions.
Definition Node.hpp:406
double m_last_output
The most recent sample value generated by this oscillator.
Definition Node.hpp:377
std::atomic< NodeState > m_state
Atomic state flag tracking the node's processing status.
Definition Node.hpp:468
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
Definition Node.hpp:453
std::vector< std::pair< NodeHook, NodeCondition > > m_conditional_callbacks
Collection of conditional callback functions with their predicates.
Definition Node.hpp:416
bool m_gpu_compatible
Flag indicating if the node supports GPU processing This flag is set by derived classes to indicate w...
Definition Node.hpp:386
@ Init
Engine/subsystem initialization.
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
void composite_validate(const std::vector< std::shared_ptr< Node > > &inputs, size_t N)
void composite_initialize(const std::vector< std::shared_ptr< Node > > &inputs, NodeGraphManager &manager, ProcessingToken token, const std::shared_ptr< Node > &self)
void composite_apply_semantics(const std::vector< std::shared_ptr< Node > > &inputs, NodeGraphManager &manager, ProcessingToken token)
@ PROCESSED
Node has been processed this cycle.
Definition NodeSpec.hpp:49
@ ACTIVE
Engine is processing this node.
Definition NodeSpec.hpp:45
ProcessingToken
Enumerates the different processing domains for nodes.
void atomic_add_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically adds a flag to a node state.
Definition NodeUtils.cpp:94
std::vector< uint32_t > get_active_channels(const std::shared_ptr< Nodes::Node > &node, uint32_t fallback_channel)
Extracts active channel list from a node's channel mask.
@ KEEP
Preserve both nodes in the binary op, add new binary op node to root, i.e doubling the signal.
Definition NodeSpec.hpp:23
@ REPLACE
Unregister both nodes and register with the new binary op node.
Definition NodeSpec.hpp:22
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
void atomic_remove_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically removes a flag from a node state.
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
Contains the node-based computational processing system components.
Definition Chronie.hpp:11
NodeBinaryOpSemantics binary_op_semantics
Definition NodeSpec.hpp:36