22 "Cannot create pipeline without shader");
30 const auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
31 for (
const auto& binding : descriptor_bindings) {
32 unified_bindings[{ binding.set, binding.binding }] = binding;
36 auto key = std::make_pair(binding.set, binding.binding);
37 if (unified_bindings.find(key) == unified_bindings.end()) {
40 .binding = binding.binding,
48 std::map<uint32_t, std::vector<Portal::Graphics::DescriptorBindingInfo>> bindings_by_set;
49 for (
const auto& [key, binding] : unified_bindings) {
50 bindings_by_set[binding.set].push_back(binding);
53 std::vector<std::vector<Portal::Graphics::DescriptorBindingInfo>> descriptor_sets;
55 descriptor_sets.reserve(bindings_by_set.size());
56 for (
const auto& [set_index, set_bindings] : bindings_by_set) {
57 descriptor_sets.push_back(set_bindings);
60 const auto& staging = buffer->get_pipeline_context().push_constant_staging;
61 size_t push_constant_size = 0;
63 if (!staging.empty()) {
64 push_constant_size = staging.size();
76 "Failed to create compute pipeline");
83 "Compute pipeline created (ID: {}, {} descriptor sets, {} bytes push constants)",
91 "Cannot allocate descriptor sets without pipeline");
103 "Failed to allocate descriptor sets");
139 std::function<std::array<uint32_t, 3>(
const std::shared_ptr<VKBuffer>&)> calculator)
150 case DispatchMode::MANUAL:
153 case DispatchMode::ELEMENT_COUNT: {
154 uint64_t element_count = 0;
155 const auto& dimensions = buffer->get_dimensions();
157 if (!dimensions.empty()) {
158 element_count = dimensions[0].size;
160 element_count = buffer->get_size_bytes() /
sizeof(float);
163 auto groups_x =
static_cast<uint32_t
>(
165 return { groups_x, 1, 1 };
168 case DispatchMode::BUFFER_SIZE: {
169 uint64_t size_bytes = buffer->get_size_bytes();
170 auto groups_x =
static_cast<uint32_t
>(
172 return { groups_x, 1, 1 };
175 case DispatchMode::CUSTOM:
190 "Cannot dispatch without pipeline and descriptors");
196 "Descriptor sets not initialized");
210 auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
211 if (!descriptor_bindings.empty()) {
212 for (
const auto& binding : descriptor_bindings) {
215 "Descriptor set index {} out of range", binding.set);
219 foundry.update_descriptor_buffer(
223 binding.buffer_info.buffer,
224 binding.buffer_info.offset,
225 binding.buffer_info.range);
233 const auto& staging = buffer->get_pipeline_context();
234 if (!staging.push_constant_staging.empty()) {
235 compute_press.push_constants(
238 staging.push_constant_staging.data(),
239 staging.push_constant_staging.size());
241 compute_press.push_constants(
251 compute_press.dispatch(cmd_id, dispatch_size[0], dispatch_size[1], dispatch_size[2]);
255 foundry.buffer_barrier(
257 buffer->get_buffer(),
258 vk::AccessFlagBits::eShaderWrite,
259 vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eTransferRead,
260 vk::PipelineStageFlagBits::eComputeShader,
261 vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer);
263 foundry.submit_and_wait(cmd_id);
#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)
ShaderDispatchConfig m_dispatch_config
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
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 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
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
size_t push_constant_size
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.
@ MANUAL
Use explicit group counts.
uint32_t workgroup_x
Workgroup size X (should match shader)