MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ComputeProcessor.cpp
Go to the documentation of this file.
2
4
5namespace MayaFlux::Buffers {
6
7//==============================================================================
8// Construction
9//==============================================================================
10
11ComputeProcessor::ComputeProcessor(const std::string& shader_path, uint32_t workgroup_x)
12 : ShaderProcessor(shader_path)
13
14{
15 m_dispatch_config.workgroup_x = workgroup_x;
16}
17
25
26void ComputeProcessor::initialize_pipeline(const std::shared_ptr<VKBuffer>& buffer)
27{
30 "Cannot create pipeline without shader");
31 return;
32 }
33
34 auto& compute_press = Portal::Graphics::get_compute_press();
35
36 std::map<std::pair<uint32_t, uint32_t>, Portal::Graphics::DescriptorBindingInfo> unified_bindings;
37
38 const auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
39 for (const auto& binding : descriptor_bindings) {
40 unified_bindings[{ binding.set, binding.binding }] = binding;
41 }
42
43 for (const auto& [name, binding] : m_config.bindings) {
44 auto key = std::make_pair(binding.set, binding.binding);
45 if (unified_bindings.find(key) == unified_bindings.end()) {
46 unified_bindings[key] = Portal::Graphics::DescriptorBindingInfo {
47 .set = binding.set,
48 .binding = binding.binding,
49 .type = binding.type,
50 .buffer_info = {},
51 .name = name
52 };
53 }
54 }
55
56 std::map<uint32_t, std::vector<Portal::Graphics::DescriptorBindingInfo>> bindings_by_set;
57 for (const auto& [key, binding] : unified_bindings) {
58 bindings_by_set[binding.set].push_back(binding);
59 }
60
61 std::vector<std::vector<Portal::Graphics::DescriptorBindingInfo>> descriptor_sets;
62
63 descriptor_sets.reserve(bindings_by_set.size());
64 for (const auto& [set_index, set_bindings] : bindings_by_set) {
65 descriptor_sets.push_back(set_bindings);
66 }
67
68 m_pipeline_id = compute_press.create_pipeline(
70 descriptor_sets,
72
75 "Failed to create compute pipeline");
76 return;
77 }
78
80
82 "Compute pipeline created (ID: {}, {} descriptor sets, {} bytes push constants)",
83 m_pipeline_id, descriptor_sets.size(), m_config.push_constant_size);
84}
85
86void ComputeProcessor::initialize_descriptors(const std::shared_ptr<VKBuffer>& buffer)
87{
90 "Cannot allocate descriptor sets without pipeline");
91 return;
92 }
93
95
96 auto& compute_press = Portal::Graphics::get_compute_press();
97
98 m_descriptor_set_ids = compute_press.allocate_pipeline_descriptors(m_pipeline_id);
99
100 if (m_descriptor_set_ids.empty()) {
102 "Failed to allocate descriptor sets");
103 return;
104 }
105
106 update_descriptors(buffer);
108
110 "Descriptor sets initialized: {} sets", m_descriptor_set_ids.size());
111}
112
113//==============================================================================
114// Dispatch Configuration
115//==============================================================================
116
117void ComputeProcessor::set_workgroup_size(uint32_t x, uint32_t y, uint32_t z)
118{
122}
123
128
136
138 std::function<std::array<uint32_t, 3>(const std::shared_ptr<VKBuffer>&)> calculator)
139{
141 m_dispatch_config.custom_calculator = std::move(calculator);
142}
143
144std::array<uint32_t, 3> ComputeProcessor::calculate_dispatch_size(const std::shared_ptr<VKBuffer>& buffer)
145{
146 using DispatchMode = ShaderDispatchConfig::DispatchMode;
147
148 switch (m_dispatch_config.mode) {
149 case DispatchMode::MANUAL:
151
152 case DispatchMode::ELEMENT_COUNT: {
153 uint64_t element_count = 0;
154 const auto& dimensions = buffer->get_dimensions();
155
156 if (!dimensions.empty()) {
157 element_count = dimensions[0].size;
158 } else {
159 element_count = buffer->get_size_bytes() / sizeof(float);
160 }
161
162 auto groups_x = static_cast<uint32_t>(
164 return { groups_x, 1, 1 };
165 }
166
167 case DispatchMode::BUFFER_SIZE: {
168 uint64_t size_bytes = buffer->get_size_bytes();
169 auto groups_x = static_cast<uint32_t>(
171 return { groups_x, 1, 1 };
172 }
173
174 case DispatchMode::CUSTOM:
177 }
178 return { 1, 1, 1 };
179
180 default:
181 return { 1, 1, 1 };
182 }
183}
184
185void ComputeProcessor::execute_shader(const std::shared_ptr<VKBuffer>& buffer)
186{
189 "Cannot dispatch without pipeline and descriptors");
190 return;
191 }
192
193 if (m_descriptor_set_ids.empty()) {
195 "Descriptor sets not initialized");
196 return;
197 }
198
199 auto& foundry = Portal::Graphics::get_shader_foundry();
200 auto& compute_press = Portal::Graphics::get_compute_press();
201
202 auto cmd_id = foundry.begin_commands(Portal::Graphics::ShaderFoundry::CommandBufferType::COMPUTE);
203
204 m_last_command_buffer = cmd_id;
206
207 compute_press.bind_pipeline(cmd_id, m_pipeline_id);
208
209 auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
210 if (!descriptor_bindings.empty()) {
211 for (const auto& binding : descriptor_bindings) {
212 if (binding.set >= m_descriptor_set_ids.size()) {
214 "Descriptor set index {} out of range", binding.set);
215 continue;
216 }
217
218 foundry.update_descriptor_buffer(
219 m_descriptor_set_ids[binding.set],
220 binding.binding,
221 binding.type,
222 binding.buffer_info.buffer,
223 binding.buffer_info.offset,
224 binding.buffer_info.range);
225 }
226 }
227
228 if (!m_descriptor_set_ids.empty()) {
229 compute_press.bind_descriptor_sets(cmd_id, m_pipeline_id, m_descriptor_set_ids);
230 }
231
232 on_before_execute(cmd_id, buffer);
233
234 const auto push_data = resolve_push_constants(buffer);
235 if (!push_data.empty()) {
236 compute_press.push_constants(
237 cmd_id, m_pipeline_id, push_data.data(), push_data.size());
238 }
239
240 auto dispatch_size = calculate_dispatch_size(buffer);
241 compute_press.dispatch(cmd_id, dispatch_size[0], dispatch_size[1], dispatch_size[2]);
242
243 on_after_execute(cmd_id, buffer);
244
245 foundry.buffer_barrier(
246 cmd_id,
247 buffer->get_buffer(),
248 vk::AccessFlagBits::eShaderWrite,
249 vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eTransferRead,
250 vk::PipelineStageFlagBits::eComputeShader,
251 vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer);
252
253 foundry.submit_and_wait(cmd_id);
254}
255
257{
258 auto& foundry = Portal::Graphics::get_shader_foundry();
259 auto& compute_press = Portal::Graphics::get_compute_press();
260
262 compute_press.destroy_pipeline(m_pipeline_id);
264 }
265
267 foundry.destroy_shader(m_shader_id);
269 }
270
271 m_descriptor_set_ids.clear();
272 m_bound_buffers.clear();
273 m_initialized = false;
274}
275
276} // namespace MayaFlux::Buffers
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
void set_workgroup_size(uint32_t x, uint32_t y=1, uint32_t z=1)
Set workgroup size (should match shader local_size)
void initialize_descriptors(const std::shared_ptr< VKBuffer > &buffer) override
void execute_shader(const std::shared_ptr< VKBuffer > &buffer) override
ComputeProcessor(const std::string &shader_path, uint32_t workgroup_x=256)
Construct processor with shader path.
void initialize_pipeline(const std::shared_ptr< VKBuffer > &buffer) override
Portal::Graphics::ComputePipelineID m_pipeline_id
void set_dispatch_mode(ShaderDispatchConfig::DispatchMode mode)
Set dispatch mode.
void set_manual_dispatch(uint32_t x, uint32_t y=1, uint32_t z=1)
Set manual dispatch group counts.
virtual std::array< uint32_t, 3 > calculate_dispatch_size(const std::shared_ptr< VKBuffer > &buffer)
Calculate dispatch size from buffer.
void set_custom_dispatch(std::function< std::array< uint32_t, 3 >(const std::shared_ptr< VKBuffer > &)> calculator)
Set custom dispatch calculator.
Portal::Graphics::CommandBufferID m_last_command_buffer
std::unordered_map< std::string, std::shared_ptr< VKBuffer > > m_bound_buffers
Portal::Graphics::ShaderID m_shader_id
virtual void on_after_execute(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer)
Called after each process callback.
virtual void on_descriptors_created()
Called after descriptor sets are created.
size_t resolve_push_constant_size(const std::shared_ptr< VKBuffer > &buffer) const
Byte width of this processor's push constant block, extended to cover any fragment staged on the buff...
virtual void update_descriptors(const std::shared_ptr< VKBuffer > &buffer)
virtual bool on_before_execute(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer)
Called before each process callback.
virtual void on_pipeline_created(Portal::Graphics::ComputePipelineID pipeline_id)
Called after pipeline is created.
std::shared_ptr< VKBuffer > m_last_processed_buffer
virtual void on_before_descriptors_create()
Called before descriptor sets are created.
std::vector< Portal::Graphics::DescriptorSetID > m_descriptor_set_ids
std::vector< uint8_t > resolve_push_constants(const std::shared_ptr< VKBuffer > &buffer) const
This processor's push constant data with buffer-staged fragments overlaid at their declared offsets.
Abstract base class for shader-based buffer processing.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains)
@ Buffers
Buffers, Managers, processors and processing chains.
constexpr ShaderID INVALID_SHADER
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
constexpr ComputePipelineID INVALID_COMPUTE_PIPELINE
MAYAFLUX_API ComputePress & get_compute_press()
std::unordered_map< std::string, ShaderBinding > bindings
enum MayaFlux::Buffers::ShaderDispatchConfig::DispatchMode mode
std::function< std::array< uint32_t, 3 >(const std::shared_ptr< VKBuffer > &)> custom_calculator
@ CUSTOM
User-provided calculation function.
uint32_t workgroup_x
Workgroup size X (should match shader)
std::array< uint32_t, 3 > workgroup_size
Complete declarative description of a generated compute shader.