MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ShaderProcessor.cpp
Go to the documentation of this file.
1#include "ShaderProcessor.hpp"
2
4
5namespace MayaFlux::Buffers {
6
7//==============================================================================
8// Construction
9//==============================================================================
10
11ShaderProcessor::ShaderProcessor(const std::string& shader_path)
12 : m_config({ shader_path })
13{
14 m_processing_token = ProcessingToken::GRAPHICS_BACKEND;
15 initialize_buffer_service();
16 initialize_compute_service();
17}
18
26
31
32//==============================================================================
33// BufferProcessor Interface
34//==============================================================================
35
36void ShaderProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
37{
38 auto vk_buffer = std::dynamic_pointer_cast<VKBuffer>(buffer);
39 if (!vk_buffer) {
41 "ShaderProcessor can only process VKBuffers");
42 return;
43 }
44
47 "on_before_execute() reported failure, skipping shader execution");
48 return;
49 }
50
51 if (!m_initialized) {
53 initialize_pipeline(vk_buffer);
54 m_initialized = true;
55 }
56
58 initialize_pipeline(vk_buffer);
61 }
62
64 initialize_descriptors(vk_buffer);
66 } else {
67 update_descriptors(vk_buffer);
68 }
69
70 execute_shader(vk_buffer);
71}
72
73void ShaderProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
74{
75 auto vk_buffer = std::dynamic_pointer_cast<VKBuffer>(buffer);
76 if (!vk_buffer)
77 return;
78
79 if (m_config.bindings.empty()) {
80 auto_bind_buffer(vk_buffer);
81 }
82
84 "ShaderProcessor attached to VKBuffer (size: {} bytes, modality: {})",
85 vk_buffer->get_size_bytes(),
86 static_cast<int>(vk_buffer->get_modality()));
87}
88
89void ShaderProcessor::on_detach(const std::shared_ptr<Buffer>& buffer)
90{
91 auto vk_buffer = std::dynamic_pointer_cast<VKBuffer>(buffer);
92 if (!vk_buffer)
93 return;
94
95 for (auto it = m_bound_buffers.begin(); it != m_bound_buffers.end();) {
96 if (it->second == vk_buffer) {
97 it = m_bound_buffers.erase(it);
98 } else {
99 ++it;
100 }
101 }
103}
104
105bool ShaderProcessor::is_compatible_with(const std::shared_ptr<Buffer>& buffer) const
106{
107 return std::dynamic_pointer_cast<VKBuffer>(buffer) != nullptr;
108}
109
110//==============================================================================
111// Buffer Binding
112//==============================================================================
113
114void ShaderProcessor::bind_buffer(const std::string& descriptor_name, const std::shared_ptr<VKBuffer>& buffer)
115{
116 if (!buffer) {
118 "Cannot bind null buffer to descriptor '{}'", descriptor_name);
119 return;
120 }
121
122 if (m_config.bindings.find(descriptor_name) == m_config.bindings.end()) {
123 ShaderBinding default_binding;
124 default_binding.set = 0;
125 default_binding.binding = static_cast<uint32_t>(m_config.bindings.size());
126 default_binding.type = vk::DescriptorType::eStorageBuffer;
127 m_config.bindings[descriptor_name] = default_binding;
128
129 // MF_DEBUG(Journal::Component::Buffers, Journal::Context::BufferProcessing,
130 // "Created default binding for '{}': set={}, binding={}",
131 // descriptor_name, default_binding.set, default_binding.binding);
132 }
133
134 m_bound_buffers[descriptor_name] = buffer;
136
137 // MF_DEBUG(Journal::Component::Buffers, Journal::Context::BufferProcessing,
138 // "Bound buffer to descriptor '{}' (size: {} bytes)",
139 // descriptor_name, buffer->get_size_bytes());
140}
141
142void ShaderProcessor::unbind_buffer(const std::string& descriptor_name)
143{
144 auto it = m_bound_buffers.find(descriptor_name);
145 if (it != m_bound_buffers.end()) {
146 m_bound_buffers.erase(it);
148 }
149}
150
151std::shared_ptr<VKBuffer> ShaderProcessor::get_bound_buffer(const std::string& descriptor_name) const
152{
153 auto it = m_bound_buffers.find(descriptor_name);
154 return it != m_bound_buffers.end() ? it->second : nullptr;
155}
156
157void ShaderProcessor::auto_bind_buffer(const std::shared_ptr<VKBuffer>& buffer)
158{
159 std::string descriptor_name;
160 if (m_auto_bind_index == 0) {
161 descriptor_name = "input";
162 } else if (m_auto_bind_index == 1) {
163 descriptor_name = "output";
164 } else {
165 descriptor_name = "buffer_" + std::to_string(m_auto_bind_index);
166 }
167
168 bind_buffer(descriptor_name, buffer);
170}
171
172//==============================================================================
173// Shader Management
174//==============================================================================
175
177{
179 "Hot-reloading shader: {}", m_config.shader_path);
180
181 auto& foundry = Portal::Graphics::get_shader_foundry();
182 auto new_shader_id = foundry.reload_shader(m_config.shader_path);
183
184 if (new_shader_id == Portal::Graphics::INVALID_SHADER) {
186 "Hot-reload failed for shader: {}", m_config.shader_path);
187 return false;
188 }
189
191 foundry.destroy_shader(m_shader_id);
192 }
193
194 m_shader_id = new_shader_id;
197
199 "Shader hot-reloaded successfully (ID: {})", m_shader_id);
200 return true;
201}
202
203void ShaderProcessor::set_shader(const std::string& shader_path)
204{
205 m_config.shader_path = shader_path;
208}
209
210//==============================================================================
211// Push Constants
212//==============================================================================
213
220
221void ShaderProcessor::set_push_constant_data_raw(const void* data, size_t size)
222{
223 if (size > m_config.push_constant_size) {
225 "Push constant data size {} exceeds configured size {}",
227 return;
228 }
229
230 m_push_constant_data.resize(size);
231 std::memcpy(m_push_constant_data.data(), data, size);
232}
233
234//==============================================================================
235// Specialization Constants
236//==============================================================================
237
238void ShaderProcessor::set_specialization_constant(uint32_t constant_id, uint32_t value)
239{
240 m_config.specialization_constants[constant_id] = value;
242}
243
249
250//==============================================================================
251// Configuration
252//==============================================================================
253
255{
256 m_config = config;
260}
261
262void ShaderProcessor::add_binding(const std::string& descriptor_name, const ShaderBinding& binding)
263{
264 m_config.bindings[descriptor_name] = binding;
266}
267
268//==========================================================================
269// Data movement Queries
270//==========================================================================
271
272[[nodiscard]] ShaderProcessor::BufferUsageHint ShaderProcessor::get_buffer_usage_hint(const std::string& descriptor_name) const
273{
274 if (descriptor_name == "input")
276 if (descriptor_name == "output")
279}
280
281bool ShaderProcessor::is_in_place_operation(const std::string& descriptor_name) const
282{
283 auto hint = get_buffer_usage_hint(descriptor_name);
284 return hint == BufferUsageHint::BIDIRECTIONAL;
285}
286
287bool ShaderProcessor::has_binding(const std::string& descriptor_name) const
288{
289 return m_config.bindings.find(descriptor_name) != m_config.bindings.end();
290}
291
292std::vector<std::string> ShaderProcessor::get_binding_names() const
293{
294 std::vector<std::string> names;
295 names.reserve(m_config.bindings.size());
296 for (const auto& [name, _] : m_config.bindings) {
297 names.push_back(name);
298 }
299 return names;
300}
301
303{
304 return std::ranges::all_of(
306 [this](const auto& pair) {
307 return m_bound_buffers.find(pair.first) != m_bound_buffers.end();
308 });
309}
310
311//==============================================================================
312// Protected Hooks
313//==============================================================================
314
315void ShaderProcessor::on_before_compile(const std::string&) { }
320bool ShaderProcessor::on_before_execute(Portal::Graphics::CommandBufferID, const std::shared_ptr<VKBuffer>&) { return true; }
322
323//==============================================================================
324// Private Implementation
325//==============================================================================
326
345
346void ShaderProcessor::update_descriptors(const std::shared_ptr<VKBuffer>& buffer)
347{
348 if (m_descriptor_set_ids.empty()) {
349 return;
350 }
351
352 auto& foundry = Portal::Graphics::get_shader_foundry();
353 auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
354
355 std::set<std::pair<uint32_t, uint32_t>> updated_pairs;
356
357 for (const auto& binding : descriptor_bindings) {
358 if (binding.set >= m_descriptor_set_ids.size()) {
360 "Descriptor set index {} out of range", binding.set);
361 continue;
362 }
363
364 foundry.update_descriptor_buffer(
365 m_descriptor_set_ids[binding.set],
366 binding.binding,
367 binding.type,
368 binding.buffer_info.buffer,
369 binding.buffer_info.offset,
370 binding.buffer_info.range);
371
372 updated_pairs.emplace(binding.set, binding.binding);
373 }
374
375 for (const auto& [descriptor_name, buffer] : m_bound_buffers) {
376 auto binding_it = m_config.bindings.find(descriptor_name);
377 if (binding_it == m_config.bindings.end()) {
378 continue;
379 }
380
381 const auto& binding = binding_it->second;
382 auto key = std::make_pair(binding.set, binding.binding);
383
384 if (updated_pairs.count(key)) {
385 continue;
386 }
387
388 if (binding.set >= m_descriptor_set_ids.size()) {
390 "Invalid descriptor set index {} for binding '{}'",
391 binding.set, descriptor_name);
392 continue;
393 }
394
395 foundry.update_descriptor_buffer(
396 m_descriptor_set_ids[binding.set],
397 binding.binding,
398 binding.type,
399 buffer->get_buffer(),
400 0,
401 buffer->get_size_bytes());
402 }
403}
404
406{
407 auto& foundry = Portal::Graphics::get_shader_foundry();
408 auto& compute_press = Portal::Graphics::get_compute_press();
409
411 foundry.destroy_shader(m_shader_id);
413 }
414
415 m_descriptor_set_ids.clear();
416 m_bound_buffers.clear();
417 m_initialized = false;
418}
419
420} // namespace MayaFlux::Buffers
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
#define MF_RT_DEBUG(comp, ctx,...)
virtual void initialize_pipeline(const std::shared_ptr< VKBuffer > &buffer)=0
ShaderProcessor(const std::string &shader_path)
Construct processor with shader path.
Portal::Graphics::CommandBufferID m_last_command_buffer
bool are_bindings_complete() const
Check if all required bindings are satisfied.
virtual void execute_shader(const std::shared_ptr< VKBuffer > &buffer)=0
std::unordered_map< std::string, std::shared_ptr< VKBuffer > > m_bound_buffers
void unbind_buffer(const std::string &descriptor_name)
Unbind a buffer from a descriptor.
Portal::Graphics::ShaderID m_shader_id
std::vector< uint8_t > m_push_constant_data
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.
virtual void initialize_descriptors(const std::shared_ptr< VKBuffer > &buffer)=0
std::shared_ptr< VKBuffer > get_bound_buffer(const std::string &descriptor_name) const
Get bound buffer for a descriptor name.
virtual void update_descriptors(const std::shared_ptr< VKBuffer > &buffer)
virtual void on_before_compile(const std::string &shader_path)
Called before shader compilation.
virtual bool on_before_execute(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer)
Called before each process callback.
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
void auto_bind_buffer(const std::shared_ptr< VKBuffer > &buffer)
Auto-bind buffer based on attachment order.
bool has_binding(const std::string &descriptor_name) const
Check if a descriptor binding exists.
void set_push_constant_size()
Set push constant size from type.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
virtual void on_pipeline_created(Portal::Graphics::ComputePipelineID pipeline_id)
Called after pipeline is created.
virtual bool is_in_place_operation(const std::string &descriptor_name) const
Check if shader modifies a specific buffer in-place.
virtual void on_shader_loaded(Portal::Graphics::ShaderID shader_id)
Called after shader is loaded.
void add_binding(const std::string &descriptor_name, const ShaderBinding &binding)
Add descriptor binding configuration.
bool hot_reload_shader()
Hot-reload shader from ShaderFoundry.
virtual void on_before_descriptors_create()
Called before descriptor sets are created.
void set_config(const ShaderConfig &config)
Update entire configuration.
void set_shader(const std::string &shader_path)
Update shader path and reload.
bool is_compatible_with(const std::shared_ptr< Buffer > &buffer) const override
Checks if this processor can handle the specified buffer type.
virtual BufferUsageHint get_buffer_usage_hint(const std::string &descriptor_name) const
Get buffer usage hint for a descriptor.
BufferUsageHint
Get buffer usage characteristics needed for safe data flow.
@ OUTPUT_WRITE
Shader writes output (modifies)
std::vector< std::string > get_binding_names() const
Get all configured descriptor names.
void set_specialization_constant(uint32_t constant_id, uint32_t value)
Set specialization constant.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
void bind_buffer(const std::string &descriptor_name, const std::shared_ptr< VKBuffer > &buffer)
Bind a VKBuffer to a named shader descriptor.
std::vector< Portal::Graphics::DescriptorSetID > m_descriptor_set_ids
void clear_specialization_constants()
Clear all specialization constants.
void set_push_constant_data_raw(const void *data, size_t size)
Update push constant data (raw bytes)
@ GRAPHICS_BACKEND
Standard graphics processing backend configuration.
@ 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.
MAYAFLUX_API ComputePress & get_compute_press()
uint32_t binding
Binding point within set.
uint32_t set
Descriptor set index.
Describes how a VKBuffer binds to a shader descriptor.
std::string shader_path
Path to shader file.
std::unordered_map< uint32_t, uint32_t > specialization_constants
std::unordered_map< std::string, ShaderBinding > bindings
Portal::Graphics::ShaderStage stage