28 "Cannot create pipeline without shader");
36 const auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
37 for (
const auto& binding : descriptor_bindings) {
38 unified_bindings[{ binding.set, binding.binding }] = binding;
42 auto key = std::make_pair(binding.set, binding.binding);
43 if (unified_bindings.find(key) == unified_bindings.end()) {
46 .binding = binding.binding,
54 std::map<uint32_t, std::vector<Portal::Graphics::DescriptorBindingInfo>> bindings_by_set;
55 for (
const auto& [key, binding] : unified_bindings) {
56 bindings_by_set[binding.set].push_back(binding);
59 std::vector<std::vector<Portal::Graphics::DescriptorBindingInfo>> descriptor_sets;
61 descriptor_sets.reserve(bindings_by_set.size());
62 for (
const auto& [set_index, set_bindings] : bindings_by_set) {
63 descriptor_sets.push_back(set_bindings);
73 "Failed to create compute pipeline");
80 "Compute pipeline created (ID: {}, {} descriptor sets, {} bytes push constants)",
88 "Cannot allocate descriptor sets without pipeline");
100 "Failed to allocate descriptor sets");
136 std::function<std::array<uint32_t, 3>(
const std::shared_ptr<VKBuffer>&)> calculator)
147 case DispatchMode::MANUAL:
150 case DispatchMode::ELEMENT_COUNT: {
151 uint64_t element_count = 0;
152 const auto& dimensions = buffer->get_dimensions();
154 if (!dimensions.empty()) {
155 element_count = dimensions[0].size;
157 element_count = buffer->get_size_bytes() /
sizeof(float);
160 auto groups_x =
static_cast<uint32_t
>(
162 return { groups_x, 1, 1 };
165 case DispatchMode::BUFFER_SIZE: {
166 uint64_t size_bytes = buffer->get_size_bytes();
167 auto groups_x =
static_cast<uint32_t
>(
169 return { groups_x, 1, 1 };
172 case DispatchMode::CUSTOM:
190 const std::shared_ptr<VKBuffer>&,
198 const std::shared_ptr<VKBuffer>& buffer,
203 buffer->get_buffer(),
204 vk::AccessFlagBits::eShaderWrite,
205 vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eShaderWrite,
206 vk::PipelineStageFlagBits::eComputeShader,
207 vk::PipelineStageFlagBits::eComputeShader);
214 "Cannot dispatch without pipeline and descriptors");
220 "Descriptor sets not initialized");
234 auto& descriptor_bindings = buffer->get_pipeline_context().descriptor_buffer_bindings;
235 if (!descriptor_bindings.empty()) {
236 for (
const auto& binding : descriptor_bindings) {
239 "Descriptor set index {} out of range", binding.set);
243 foundry.update_descriptor_buffer(
247 binding.buffer_info.buffer,
248 binding.buffer_info.offset,
249 binding.buffer_info.range);
262 for (uint32_t i = 0; i < iterations; ++i) {
267 const auto& pc_bindings = buffer->get_pipeline_context().push_constant_bindings;
269 if (!pc_bindings.empty()) {
271 for (
const auto& pc : pc_bindings) {
272 required = std::max(required,
static_cast<size_t>(pc.offset) + pc.data.size());
277 for (
const auto& pc : pc_bindings) {
281 compute_press.push_constants(
286 compute_press.push_constants(
292 compute_press.dispatch(cmd_id, dispatch_size[0], dispatch_size[1], dispatch_size[2]);
294 if (i + 1 < iterations) {
301 foundry.buffer_barrier(
303 buffer->get_buffer(),
304 vk::AccessFlagBits::eShaderWrite,
305 vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eTransferRead,
306 vk::PipelineStageFlagBits::eComputeShader,
307 vk::PipelineStageFlagBits::eComputeShader | vk::PipelineStageFlagBits::eTransfer);
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
virtual void on_iteration_barrier(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer, uint32_t index)
Called after each iteration except the last.
std::vector< uint8_t > m_push_constant_scratch
Coalesced push constant bindings, reused across iterations.
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_iteration_count(uint32_t count)
Set how many dispatches are recorded per execute cycle.
void set_dispatch_mode(ShaderDispatchConfig::DispatchMode mode)
Set dispatch mode.
virtual bool on_iteration(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer, uint32_t index)
Called before each iteration's push constants and dispatch.
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.
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
void submit_recorded(Portal::Graphics::CommandBufferID cmd_id, const std::shared_ptr< VKBuffer > &buffer)
Submit a recorded command buffer honoring the submission mode.
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.
void buffer_barrier(CommandBufferID cmd_id, vk::Buffer buffer, vk::AccessFlags src_access, vk::AccessFlags dst_access, vk::PipelineStageFlags src_stage, vk::PipelineStageFlags dst_stage)
Insert buffer memory barrier.
@ 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)
uint32_t iteration_count
Dispatches recorded per execute cycle.
std::array< uint32_t, 3 > workgroup_size
Complete declarative description of a generated compute shader.