MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
DescriptorBindingsProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ShaderProcessor.hpp"
4
5namespace MayaFlux::Nodes {
6class Node;
7}
8
9namespace MayaFlux::Buffers {
10
11/**
12 * @class DescriptorBindingsProcessor
13 * @brief ShaderProcessor that uploads node outputs to descriptor sets
14 *
15 * Binds nodes to UBO/SSBO descriptors. Supports:
16 * - Scalar nodes (single value)
17 * - Vector nodes (contiguous arrays via VectorContext)
18 * - Matrix nodes (2D grids via MatrixContext)
19 * - Structured nodes (arrays of POD structs)
20 *
21 * Usage:
22 * auto processor = std::make_shared<DescriptorBindingsProcessor>(shader_config);
23 *
24 * // Bind scalar node to UBO
25 * processor->bind_scalar_node("frequency", freq_node,
26 * "params", 0, vk::DescriptorType::eUniformBuffer);
27 *
28 * // Bind vector node to SSBO
29 * processor->bind_vector_node("spectrum", spectrum_node,
30 * "spectrum_data", 0, vk::DescriptorType::eStorageBuffer);
31 */
32class MAYAFLUX_API DescriptorBindingsProcessor : public ShaderProcessor {
33public:
34 enum class BindingType : uint8_t {
35 SCALAR, ///< Single value from node output
36 VECTOR, ///< Array from VectorContext
37 MATRIX, ///< 2D grid from MatrixContext
38 STRUCTURED ///< Array of structs from StructuredContext
39 };
40
42 std::shared_ptr<Nodes::Node> node;
43 std::string descriptor_name; ///< Matches ShaderProcessor binding name
44 uint32_t set_index;
45 uint32_t binding_index;
46 vk::DescriptorType type; ///< UBO or SSBO
48 std::shared_ptr<VKBuffer> gpu_buffer; ///< UBO/SSBO backing storage
49 size_t buffer_offset {}; ///< Offset within buffer (for packed UBOs)
50 size_t buffer_size; ///< Size to write
51 };
52
53 /**
54 * @brief Create DescriptorBindingsProcessor with shader path
55 * @param shader_path Path to compute shader
56 */
57 DescriptorBindingsProcessor(const std::string& shader_path);
58
59 /**
60 * @brief Create DescriptorBindingsProcessor with shader config
61 * @param config Shader processor configuration
62 */
64
65 /**
66 * @brief Bind scalar node output to descriptor
67 * @param name Logical binding name
68 * @param node Node to read from
69 * @param descriptor_name Name in shader config bindings
70 * @param set Descriptor set index
71 * @param type UBO or SSBO
72 */
73 void bind_scalar_node(
74 const std::string& name,
75 const std::shared_ptr<Nodes::Node>& node,
76 const std::string& descriptor_name,
77 uint32_t set,
78 vk::DescriptorType type = vk::DescriptorType::eUniformBuffer);
79
80 /**
81 * @brief Bind vector node (VectorContext) to descriptor
82 * @param name Logical binding name
83 * @param node Node that creates VectorContext
84 * @param descriptor_name Name in shader config bindings
85 * @param set Descriptor set index
86 * @param type Typically eStorageBuffer for arrays
87 */
88 void bind_vector_node(
89 const std::string& name,
90 const std::shared_ptr<Nodes::Node>& node,
91 const std::string& descriptor_name,
92 uint32_t set,
93 vk::DescriptorType type = vk::DescriptorType::eStorageBuffer);
94
95 /**
96 * @brief Bind matrix node (MatrixContext) to descriptor
97 */
98 void bind_matrix_node(
99 const std::string& name,
100 const std::shared_ptr<Nodes::Node>& node,
101 const std::string& descriptor_name,
102 uint32_t set,
103 vk::DescriptorType type = vk::DescriptorType::eStorageBuffer);
104
105 /**
106 * @brief Bind structured node (arrays of POD structs) to descriptor
107 * @param name Logical binding name
108 * @param node Node that creates context with GpuStructuredData
109 * @param descriptor_name Name in shader config bindings
110 * @param set Descriptor set index
111 * @param type Typically eStorageBuffer for structured arrays
112 */
113 void bind_structured_node(
114 const std::string& name,
115 const std::shared_ptr<Nodes::Node>& node,
116 const std::string& descriptor_name,
117 uint32_t set,
118 vk::DescriptorType type = vk::DescriptorType::eStorageBuffer);
119
120 /**
121 * @brief Remove a binding
122 */
123 void unbind_node(const std::string& name);
124
125 /**
126 * @brief Check if binding exists
127 */
128 bool has_binding(const std::string& name) const;
129
130 /**
131 * @brief Get all binding names
132 */
133 std::vector<std::string> get_binding_names() const;
134
135protected:
136 /**
137 * @brief Called after pipeline creation - allocates GPU buffers for descriptors
138 */
139 void on_pipeline_created(Portal::Graphics::ComputePipelineID pipeline_id) override;
140
141 void execute_shader(const std::shared_ptr<VKBuffer>& buffer) override;
142
143 void initialize_pipeline(const std::shared_ptr<VKBuffer>& buffer) override { }
144
145 void initialize_descriptors(const std::shared_ptr<VKBuffer>& buffer) override { }
146
147private:
148 std::unordered_map<std::string, DescriptorBinding> m_bindings;
149
150 /**
151 * @brief Ensure descriptor buffer has sufficient capacity
152 * @param binding Descriptor binding to check
153 * @param required_size Minimum required size in bytes
154 *
155 * If buffer is too small, resizes with 50% over-allocation.
156 * Sets m_needs_descriptor_rebuild flag if resize occurs.
157 */
158 void ensure_buffer_capacity(DescriptorBinding& binding, size_t required_size);
159
160 /**
161 * @brief Update descriptor from node context
162 */
163 void update_descriptor_from_node(DescriptorBinding& binding);
164
165 /**
166 * @brief Create GPU buffer for a descriptor binding
167 */
168 std::shared_ptr<VKBuffer> create_descriptor_buffer(
169 size_t size,
170 vk::DescriptorType type);
171};
172
173} // namespace MayaFlux::Buffers
void initialize_descriptors(const std::shared_ptr< VKBuffer > &buffer) override
std::unordered_map< std::string, DescriptorBinding > m_bindings
void initialize_pipeline(const std::shared_ptr< VKBuffer > &buffer) override
ShaderProcessor that uploads node outputs to descriptor sets.
Abstract base class for shader-based buffer processing.
Contains the node-based computational processing system components.
Definition Chronie.hpp:5
std::shared_ptr< VKBuffer > gpu_buffer
UBO/SSBO backing storage.