MayaFlux 0.1.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 * @param workgroup_x Workgroup size in X dimension
57 */
58 DescriptorBindingsProcessor(const std::string& shader_path, uint32_t workgroup_x = 256);
59
60 /**
61 * @brief Create DescriptorBindingsProcessor with shader config
62 * @param config Shader processor configuration
63 */
65
66 /**
67 * @brief Bind scalar node output to descriptor
68 * @param name Logical binding name
69 * @param node Node to read from
70 * @param descriptor_name Name in shader config bindings
71 * @param set Descriptor set index
72 * @param type UBO or SSBO
73 */
74 void bind_scalar_node(
75 const std::string& name,
76 const std::shared_ptr<Nodes::Node>& node,
77 const std::string& descriptor_name,
78 uint32_t set,
79 vk::DescriptorType type = vk::DescriptorType::eUniformBuffer);
80
81 /**
82 * @brief Bind vector node (VectorContext) to descriptor
83 * @param name Logical binding name
84 * @param node Node that creates VectorContext
85 * @param descriptor_name Name in shader config bindings
86 * @param set Descriptor set index
87 * @param type Typically eStorageBuffer for arrays
88 */
89 void bind_vector_node(
90 const std::string& name,
91 const std::shared_ptr<Nodes::Node>& node,
92 const std::string& descriptor_name,
93 uint32_t set,
94 vk::DescriptorType type = vk::DescriptorType::eStorageBuffer);
95
96 /**
97 * @brief Bind matrix node (MatrixContext) to descriptor
98 */
99 void bind_matrix_node(
100 const std::string& name,
101 const std::shared_ptr<Nodes::Node>& node,
102 const std::string& descriptor_name,
103 uint32_t set,
104 vk::DescriptorType type = vk::DescriptorType::eStorageBuffer);
105
106 /**
107 * @brief Bind structured node (arrays of POD structs) to descriptor
108 * @param name Logical binding name
109 * @param node Node that creates context with GpuStructuredData
110 * @param descriptor_name Name in shader config bindings
111 * @param set Descriptor set index
112 * @param type Typically eStorageBuffer for structured arrays
113 */
114 void bind_structured_node(
115 const std::string& name,
116 const std::shared_ptr<Nodes::Node>& node,
117 const std::string& descriptor_name,
118 uint32_t set,
119 vk::DescriptorType type = vk::DescriptorType::eStorageBuffer);
120
121 /**
122 * @brief Remove a binding
123 */
124 void unbind_node(const std::string& name);
125
126 /**
127 * @brief Check if binding exists
128 */
129 bool has_binding(const std::string& name) const;
130
131 /**
132 * @brief Get all binding names
133 */
134 std::vector<std::string> get_binding_names() const;
135
136protected:
137 /**
138 * @brief Called before shader dispatch - updates all descriptors
139 */
140 void on_before_dispatch(
142 const std::shared_ptr<VKBuffer>& buffer) override;
143
144 /**
145 * @brief Called after pipeline creation - allocates GPU buffers for descriptors
146 */
147 void on_pipeline_created(Portal::Graphics::ComputePipelineID pipeline_id) override;
148
149private:
150 std::unordered_map<std::string, DescriptorBinding> m_bindings;
151
152 /**
153 * @brief Ensure descriptor buffer has sufficient capacity
154 * @param binding Descriptor binding to check
155 * @param required_size Minimum required size in bytes
156 *
157 * If buffer is too small, resizes with 50% over-allocation.
158 * Sets m_needs_descriptor_rebuild flag if resize occurs.
159 */
160 void ensure_buffer_capacity(DescriptorBinding& binding, size_t required_size);
161
162 /**
163 * @brief Update descriptor from node context
164 */
165 void update_descriptor_from_node(DescriptorBinding& binding);
166
167 /**
168 * @brief Create GPU buffer for a descriptor binding
169 */
170 std::shared_ptr<VKBuffer> create_descriptor_buffer(
171 size_t size,
172 vk::DescriptorType type);
173};
174
175} // namespace MayaFlux::Buffers
std::unordered_map< std::string, DescriptorBinding > m_bindings
ShaderProcessor that uploads node outputs to descriptor sets.
Generic compute shader processor for VKBuffers.
Contains the node-based computational processing system components.
Definition Chronie.hpp:5
std::shared_ptr< VKBuffer > gpu_buffer
UBO/SSBO backing storage.
Complete configuration for shader processor.