MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextureNode.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "GpuSync.hpp"
4
6
7/**
8 * @class TextureContext
9 * @brief Context for TextureNode - provides pixel buffer access
10 */
11class MAYAFLUX_API TextureContext : public NodeContext, public GpuVectorData {
12public:
13 TextureContext(double value, uint32_t width, uint32_t height, std::span<const float> pixel_data)
14 : NodeContext(value, typeid(TextureContext).name())
15 , GpuVectorData(pixel_data)
16 , width(width)
17 , height(height)
18 {
19 }
20
21 uint32_t width;
22 uint32_t height;
23
24protected:
25 friend class TextureNode;
26};
27
28/**
29 * @class TextureNode
30 * @brief Base class for texture-generating nodes
31 *
32 * Provides common functionality for managing texture dimensions and pixel data
33 * in RGBA float format. Derived classes implement compute_frame() to define
34 * specific texture generation or processing algorithms.
35 *
36 * Texture data is stored as a flat array in row-major order:
37 * [R0,G0,B0,A0, R1,G1,B1,A1, ..., Rn,Gn,Bn,An]
38 */
39class MAYAFLUX_API TextureNode : public GpuSync {
40protected:
41 uint32_t m_width;
42 uint32_t m_height;
43 std::vector<float> m_pixel_buffer; // RGBA format
44
45 /**
46 * @brief Flag: pixel data changed since last GPU upload
47 *
48 * Set to true whenever compute_frame() modifies m_pixel_buffer.
49 * Kakshya binding system checks this before staging texture upload.
50 * Cleared by Kakshya after successful GPU transfer.
51 */
52 bool m_pixel_data_dirty { true };
53
54public:
55 TextureNode(uint32_t width, uint32_t height);
56
57 /**
58 * @brief Get pixel buffer
59 * @return Span of pixel data in RGBA format
60 */
61 [[nodiscard]] std::span<const float> get_pixel_buffer() const { return m_pixel_buffer; }
62
63 [[nodiscard]] uint32_t get_width() const { return m_width; }
64 [[nodiscard]] uint32_t get_height() const { return m_height; }
65
66 /**
67 * @brief Get total number of pixels
68 * @return width * height
69 */
70 [[nodiscard]] size_t get_pixel_count() const
71 {
72 return static_cast<size_t>(m_width) * static_cast<size_t>(m_height);
73 }
74
75 /**
76 * @brief Get buffer size in bytes
77 * @return Total size of pixel buffer
78 */
79 [[nodiscard]] size_t get_buffer_size() const
80 {
81 return m_pixel_buffer.size() * sizeof(float);
82 }
83
84 /**
85 * @brief Check if pixel data changed since last GPU sync
86 * @return True if m_pixel_buffer has been modified
87 *
88 * For textures, this is simple: did compute_frame() modify pixels?
89 * The binding processor checks this to decide whether to upload to GPU.
90 */
91 [[nodiscard]] bool needs_gpu_update() const override
92 {
93 return m_pixel_data_dirty;
94 }
95
96 /**
97 * @brief Clear the dirty flag after GPU upload completes
98 *
99 * Called by Kakshya's TextureBindingProcessor after it stages the
100 * pixel data into a GPU transfer buffer and submits the command.
101 */
102 void clear_gpu_update_flag() override
103 {
104 m_pixel_data_dirty = false;
105 }
106
107 /**
108 * @brief Save current pixel state
109 */
110 void save_state() override;
111
112 /**
113 * @brief Restore saved pixel state
114 */
115 void restore_state() override;
116
117 /**
118 * @brief Get the last created context object
119 * @return Reference to the last TextureContext object
120 *
121 * This method provides access to the most recent TextureContext object
122 * created by the texture node. This context contains information about
123 * the node's state at the time of the last output generation.
124 */
125 NodeContext& get_last_context() override;
126
127 /**
128 * @brief Updates the context object with the current node state
129 * @param value The current sample value
130 */
131 void update_context(double value) override;
132
133protected:
134 /**
135 * @brief Set pixel color at (x, y)
136 * @param x X coordinate
137 * @param y Y coordinate
138 * @param r Red channel [0, 1]
139 * @param g Green channel [0, 1]
140 * @param b Blue channel [0, 1]
141 * @param a Alpha channel [0, 1] (default: 1.0)
142 */
143 void set_pixel(uint32_t x, uint32_t y, float r, float g, float b, float a = 1.0F);
144
145 /**
146 * @brief Get pixel color at (x, y)
147 * @param x X coordinate
148 * @param y Y coordinate
149 * @return RGBA values as array [r, g, b, a]
150 */
151 [[nodiscard]] std::array<float, 4> get_pixel(uint32_t x, uint32_t y) const;
152
153 /**
154 * @brief Fill entire texture with solid color
155 * @param r Red channel
156 * @param g Green channel
157 * @param b Blue channel
158 * @param a Alpha channel (default: 1.0)
159 */
160 void fill(float r, float g, float b, float a = 1.0F);
161
162 /**
163 * @brief Clear texture to black
164 */
165 void clear();
166
167 std::vector<float> m_saved_pixel_buffer;
168 bool m_saved_dirty_flag {};
169 TextureContext m_context { 0.0, 0, 0, {} };
170
171 /**
172 * @brief Get mutable pixel buffer for direct write access
173 * @return Span of pixel data (RGBA32F format)
174 *
175 * For performance-critical pixel generation.
176 * Remember to call mark_dirty() after writing!
177 */
178 std::span<float> get_pixel_buffer_mutable()
179 {
180 return m_pixel_buffer;
181 }
182};
183
184} // namespace MayaFlux::Nodes
TextureContext(double value, uint32_t width, uint32_t height, std::span< const float > pixel_data)
Context for TextureNode - provides pixel buffer access.
void clear_gpu_update_flag() override
Clear the dirty flag after GPU upload completes.
std::span< float > get_pixel_buffer_mutable()
Get mutable pixel buffer for direct write access.
std::vector< float > m_saved_pixel_buffer
std::span< const float > get_pixel_buffer() const
Get pixel buffer.
bool needs_gpu_update() const override
Check if pixel data changed since last GPU sync.
size_t get_pixel_count() const
Get total number of pixels.
size_t get_buffer_size() const
Get buffer size in bytes.
Base class for texture-generating nodes.
GPU-uploadable 1D array data interface.
Base context class for node callbacks.
Definition Node.hpp:30