MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ComputeOutNode.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "GpuSync.hpp"
4
5namespace MayaFlux::Buffers {
6class VKBuffer;
7} // namespace MayaFlux::Buffers
8
10
11/**
12 * @class ComputeOutContext
13 * @brief Context for ComputeOutNode - provides readback buffer access
14 */
15class MAYAFLUX_API ComputeOutContext : public NodeContext, public GpuVectorData {
16public:
17 ComputeOutContext(double value, std::span<const float> readback_data, size_t element_count)
18 : NodeContext(value, typeid(ComputeOutContext).name())
19 , GpuVectorData(readback_data)
20 , element_count(element_count)
21 {
22 }
23
25
26protected:
27 friend class ComputeOutNode;
28};
29
30/**
31 * @class ComputeOutNode
32 * @brief Node that reads back data from GPU buffer to CPU
33 *
34 * Facilitates GPU → CPU data transfer by downloading compute shader results
35 * into a CPU-accessible vector. Useful for feedback loops where GPU computation
36 * results need to influence CPU-side node processing or decision-making.
37 *
38 * Usage:
39 * // GPU computes particle collisions
40 * auto collision_buffer = std::make_shared<VKBuffer>(...);
41 * auto collision_node = std::make_shared<ComputeOutNode>(collision_buffer, 1);
42 *
43 * // Node reads collision count from GPU
44 * double collision_count = collision_node->process_sample();
45 */
46class MAYAFLUX_API ComputeOutNode : public GpuSync {
47public:
48 /**
49 * @brief Construct with GPU buffer and element count
50 * @param buffer GPU buffer to read from
51 * @param element_count Number of double elements to read
52 */
53 ComputeOutNode(const std::shared_ptr<Buffers::VKBuffer>& buffer, size_t element_count);
54
55 void compute_frame() override;
56
57 /**
58 * @brief Get full readback array
59 * @return Reference to downloaded data
60 */
61 [[nodiscard]] const std::vector<double>& get_readback_data() const { return m_readback_data; }
62
63 /**
64 * @brief Get specific element from readback data
65 * @param index Element index
66 * @return Element value
67 */
68 [[nodiscard]] double get_element(size_t index) const;
69
70 /**
71 * @brief Get number of elements
72 * @return Element count
73 */
74 [[nodiscard]] size_t get_element_count() const { return m_element_count; }
75
76 /**
77 * @brief Get GPU buffer
78 * @return GPU buffer reference
79 */
80 [[nodiscard]] std::shared_ptr<Buffers::VKBuffer> get_gpu_buffer() const { return m_gpu_buffer; }
81
82 [[nodiscard]] bool needs_gpu_update() const override
83 {
84 return false; // Readback nodes don't trigger GPU updates
85 }
86
87 void clear_gpu_update_flag() override { }
88
89 /**
90 * @brief Update context with current readback data
91 * @param value Current sample value
92 */
93 void update_context(double value) override;
94
95 /**
96 * @brief Get last created context object
97 * @return Reference to last ComputeOutContext
98 */
99 NodeContext& get_last_context() override { return m_context; }
100
101protected:
102 mutable std::vector<float> m_readback_float_buffer;
103 ComputeOutContext m_context { 0.0, {}, 0 };
104
105private:
106 std::shared_ptr<Buffers::VKBuffer> m_gpu_buffer;
107 std::vector<double> m_readback_data;
109};
110
111} // namespace MayaFlux::Nodes
ComputeOutContext(double value, std::span< const float > readback_data, size_t element_count)
Context for ComputeOutNode - provides readback buffer access.
void clear_gpu_update_flag() override
Clear the "needs update" flag after GPU binding.
bool needs_gpu_update() const override
Mark if this node needs GPU binding update.
NodeContext & get_last_context() override
Get last created context object.
const std::vector< double > & get_readback_data() const
Get full readback array.
std::shared_ptr< Buffers::VKBuffer > m_gpu_buffer
std::shared_ptr< Buffers::VKBuffer > get_gpu_buffer() const
Get GPU buffer.
size_t get_element_count() const
Get number of elements.
Node that reads back data from GPU buffer to CPU.
GPU-uploadable 1D array data interface.
Base context class for node callbacks.
Definition Node.hpp:30