MayaFlux 1.0.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)
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}
21
22CompositeOpContext::CompositeOpContext(double value, std::vector<double> input_values)
24 , input_values(std::move(input_values))
25{
26}
27
28CompositeOpContextGpu::CompositeOpContextGpu(double value, std::vector<double> input_values, std::span<const float> gpu_data)
29 : CompositeOpContext(value, std::move(input_values))
30 , GpuVectorData(gpu_data)
31{
32}
33
34BinaryOpNode::BinaryOpNode(const std::shared_ptr<Node>& lhs, const std::shared_ptr<Node>& rhs, CombineFunc func)
35 : m_lhs(lhs)
36 , m_rhs(rhs)
37 , m_func(std::move(func))
38 , m_context(0.0, 0.0, 0.0)
39 , m_context_gpu(0.0, 0.0, 0.0, get_gpu_data_buffer())
40{
41 if (!m_lhs || !m_rhs) {
42 error<std::invalid_argument>(
44 std::source_location::current(),
45 "BinaryOpNode requires both lhs and rhs nodes to be non-null");
46 }
47}
48
50 const std::shared_ptr<Node>& lhs,
51 const std::shared_ptr<Node>& rhs,
52 CombineFunc func,
53 NodeGraphManager& manager,
54 ProcessingToken token)
55 : BinaryOpNode(lhs, rhs, std::move(func))
56{
57 m_manager = &manager;
58 m_token = token;
59}
60
62{
63 if (!m_is_initialized) {
64
65 if (m_manager) {
66 auto self = shared_from_this();
67 uint32_t lhs_mask = m_lhs ? m_lhs->get_channel_mask().load() : 0;
68 uint32_t rhs_mask = m_rhs ? m_rhs->get_channel_mask().load() : 0;
69 uint32_t combined_mask = lhs_mask | rhs_mask;
70
71 if (combined_mask != 0) {
72 for (auto& channel : get_active_channels(combined_mask, 0)) {
73 m_manager->add_to_root(self, m_token, channel);
74 }
75 } else {
76 m_manager->add_to_root(self, m_token, 0);
77 }
78 m_is_initialized = true;
79 }
80 }
81
82 if (m_manager) {
84 switch (semantics) {
86 if (m_lhs) {
87 for (auto& channel : get_active_channels(m_lhs, 0)) {
89 }
90 }
91 if (m_rhs) {
92 for (auto& channel : get_active_channels(m_rhs, 0)) {
94 }
95 }
96 break;
98 default:
99 break;
100 }
101 }
102}
103
105{
106 if (!m_lhs || !m_rhs) {
107 return input;
108 }
109
110 if (!is_initialized())
111 initialize();
112
113 atomic_inc_modulator_count(m_lhs->m_modulator_count, 1);
114 atomic_inc_modulator_count(m_rhs->m_modulator_count, 1);
115
116 uint32_t lstate = m_lhs->m_state.load();
117 if (lstate & NodeState::PROCESSED) {
118 m_last_lhs_value = input + m_lhs->get_last_output();
119 } else {
120 m_last_lhs_value = m_lhs->process_sample(input);
122 }
123
124 uint32_t rstate = m_rhs->m_state.load();
125 if (rstate & NodeState::PROCESSED) {
126 m_last_rhs_value = input + m_rhs->get_last_output();
127 } else {
128 m_last_rhs_value = m_rhs->process_sample(input);
130 }
131
133
136
137 atomic_dec_modulator_count(m_lhs->m_modulator_count, 1);
138 atomic_dec_modulator_count(m_rhs->m_modulator_count, 1);
139
142
143 return m_last_output;
144}
145
146std::vector<double> BinaryOpNode::process_batch(unsigned int num_samples)
147{
148 std::vector<double> output(num_samples);
149 for (unsigned int i = 0; i < num_samples; ++i) {
150 output[i] = process_sample(0.0);
151 }
152 return output;
153}
154
156{
158 auto& ctx = get_last_context();
159
160 m_callbacks.fire(ctx);
162}
163
165{
167 if (m_lhs)
168 m_lhs->reset_processed_state();
169 if (m_rhs)
170 m_rhs->reset_processed_state();
171}
172
174{
177
178 if (m_lhs)
179 m_lhs->save_state();
180 if (m_rhs)
181 m_rhs->save_state();
182
183 m_state_saved = true;
184}
185
187{
190
191 if (m_lhs)
192 m_lhs->restore_state();
193 if (m_rhs)
194 m_rhs->restore_state();
195
196 m_state_saved = false;
197}
198
211
213{
214 if (m_gpu_compatible) {
215 return m_context_gpu;
216 }
217 return m_context;
218}
219
221{
222 auto lstate = m_lhs->m_state.load();
223 auto rstate = m_rhs->m_state.load();
224 bool is_lhs_registered = m_lhs ? (lstate & NodeState::ACTIVE) : false;
225 bool is_rhs_registered = m_rhs ? (rstate & NodeState::ACTIVE) : false;
226 return !is_lhs_registered && !is_rhs_registered;
227}
228
229std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>>
231{
232 std::vector<std::pair<ModulatorRole, std::shared_ptr<Node>>> result;
233 if (m_lhs)
234 result.emplace_back(ModulatorRole::Lhs, m_lhs);
235 if (m_rhs)
236 result.emplace_back(ModulatorRole::Rhs, m_rhs);
237 return result;
238}
239
240namespace detail {
241
243 const std::vector<std::shared_ptr<Node>>& inputs,
244 NodeGraphManager& manager,
245 ProcessingToken token,
246 const std::shared_ptr<Node>& self)
247 {
248 uint32_t combined_mask = 0;
249 for (const auto& input : inputs)
250 combined_mask |= input->get_channel_mask().load();
251
252 if (combined_mask != 0) {
253 for (auto ch : get_active_channels(combined_mask, 0))
254 manager.add_to_root(self, token, ch);
255 } else {
256 manager.add_to_root(self, token, 0);
257 }
258 }
259
261 const std::vector<std::shared_ptr<Node>>& inputs,
262 NodeGraphManager& manager,
263 ProcessingToken token)
264 {
266 return;
267
268 for (const auto& input : inputs) {
269 for (auto ch : get_active_channels(input, 0))
270 manager.remove_from_root(input, token, ch);
271 }
272 }
273
274 void composite_validate(const std::vector<std::shared_ptr<Node>>& inputs, size_t N)
275 {
276 if (N > 0 && inputs.size() != N) {
277 error<std::invalid_argument>(
279 std::source_location::current(),
280 "CompositeOpNode<{}> requires exactly {} inputs, got {}",
281 N, N, inputs.size());
282 }
283
284 if (inputs.size() < 2) {
285 error<std::invalid_argument>(
287 std::source_location::current(),
288 "CompositeOpNode requires at least 2 inputs, got {}",
289 inputs.size());
290 }
291
292 for (size_t i = 0; i < inputs.size(); ++i) {
293 if (!inputs[i]) {
294 error<std::invalid_argument>(
296 std::source_location::current(),
297 "CompositeOpNode input at index {} is null", i);
298 }
299 }
300 }
301}
302
303} // namespace MayaFlux::Nodes
Core::GlobalInputConfig input
Definition Config.cpp:42
glm::vec3 value
BinaryOpContextGpu(double value, double lhs_value, double rhs_value, std::span< const float > gpu_data)
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< std::pair< ModulatorRole, std::shared_ptr< Node > > > get_modulators() const override
Retrieves the current modulators connected to this node.
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)
CompositeOpContext(double value, std::vector< double > input_values)
Context for N-ary composite operation callbacks.
GPU-uploadable 1D array data interface.
void fire(NodeContext &ctx) const
Calls every entry whose condition, if any, holds.
Definition NodeUtils.cpp:68
double value
Current sample value.
Definition Node.hpp:63
Base context class for node callbacks.
Definition Node.hpp:53
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.
HookList m_conditional_callbacks
Hooks fired on a tick only while their condition holds.
Definition Node.hpp:485
double m_last_output
The most recent sample value generated by this oscillator.
Definition Node.hpp:441
std::atomic< NodeState > m_state
Atomic state flag tracking the node's processing status.
Definition Node.hpp:543
HookList m_callbacks
Hooks fired on every tick.
Definition Node.hpp:478
bool m_fire_events_during_snapshot
Internal flag controlling whether notify_tick fires during state snapshots Default: false (events don...
Definition Node.hpp:528
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:450
@ Init
Engine/subsystem initialization.
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
void composite_apply_semantics(const std::vector< std::shared_ptr< Node > > &inputs, NodeGraphManager &manager, ProcessingToken token)
void composite_initialize(const std::vector< std::shared_ptr< Node > > &inputs, NodeGraphManager &manager, ProcessingToken token, const std::shared_ptr< Node > &self)
void composite_validate(const std::vector< std::shared_ptr< Node > > &inputs, size_t N)
@ PROCESSED
Node has been processed this cycle.
Definition NodeSpec.hpp:49
@ ACTIVE
Engine is processing this node.
Definition NodeSpec.hpp:45
void try_reset_processed_state(std::shared_ptr< Node > node)
Attempts to reset the processed state of a node.
ProcessingToken
Enumerates the different processing domains for nodes.
void atomic_inc_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically increments the modulator count by a specified amount.
void atomic_dec_modulator_count(std::atomic< uint32_t > &count, int amount)
Atomically decrements the modulator count by a specified amount.
@ 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 atomic_remove_flag(std::atomic< NodeState > &state, NodeState flag)
Atomically removes a flag from a node state.
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.
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
NodeBinaryOpSemantics binary_op_semantics
Definition NodeSpec.hpp:36