MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ComputeProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "ShaderProcessor.hpp"
4
5namespace MayaFlux::Buffers {
6
7/**
8 * @struct ShaderDispatchConfig
9 * @brief Configuration for compute shader dispatch
10 */
12 uint32_t workgroup_x = 256; ///< Workgroup size X (should match shader)
13 uint32_t workgroup_y = 1;
14 uint32_t workgroup_z = 1;
15
16 enum class DispatchMode : uint8_t {
17 ELEMENT_COUNT, ///< Calculate from buffer element count
18 MANUAL, ///< Use explicit group counts
19 BUFFER_SIZE, ///< Calculate from buffer byte size
20 CUSTOM ///< User-provided calculation function
22
23 // Manual dispatch (MANUAL mode)
24 uint32_t group_count_x = 1;
25 uint32_t group_count_y = 1;
26 uint32_t group_count_z = 1;
27
28 std::function<std::array<uint32_t, 3>(const std::shared_ptr<VKBuffer>&)> custom_calculator;
29
31};
32
33/**
34 * @class ComputeProcessor
35 * @brief Specialized ShaderProcessor for Compute Pipelines
36 *
37 * ComputeProcessor extends ShaderProcessor to handle the specifics of compute shader execution:
38 * - **Pipeline Creation:** Creates and manages `VKComputePipeline`.
39 * - **Dispatch Logic:** Calculates workgroup counts based on buffer size or manual configuration.
40 * - **Execution:** Records `vkCmdDispatch` commands.
41 *
42 * It inherits all shader resource management (descriptors, push constants, bindings) from
43 * ShaderProcessor, adding only what is necessary for compute dispatch.
44 *
45 * Dispatch Modes:
46 * - **ELEMENT_COUNT:** (Default) Calculates groups based on buffer element count / workgroup size.
47 * - **BUFFER_SIZE:** Calculates groups based on total buffer bytes / workgroup size.
48 * - **MANUAL:** Uses fixed group counts (x, y, z).
49 * - **CUSTOM:** Uses a user-provided lambda to calculate dispatch dimensions.
50 *
51 * Usage:
52 * // Simple usage - single buffer processor
53 * auto processor = std::make_shared<ComputeProcessor>("shaders/kernel.comp");
54 * processor->bind_buffer("input_buffer", my_buffer);
55 * my_buffer->set_default_processor(processor);
56 *
57 * // Advanced - multi-buffer with explicit bindings
58 * ComputeProcessorConfig config("shaders/complex.comp");
59 * config.bindings["input"] = ShaderBinding(0, 0);
60 * config.bindings["output"] = ShaderBinding(0, 1);
61 * config.dispatch.workgroup_x = 512;
62 *
63 * auto processor = std::make_shared<ComputeProcessor>(config);
64 * processor->bind_buffer("input", input_buffer);
65 * processor->bind_buffer("output", output_buffer);
66 *
67 * chain->add_processor(processor, input_buffer);
68 * chain->add_processor(processor, output_buffer);
69 *
70 * // With push constants
71 * struct Params { float scale; uint32_t iterations; };
72 * processor->set_push_constant_size<Params>();
73 * processor->set_push_constant_data(Params{2.0f, 100});
74 *
75 * Specialized Processors:
76 * class FFTProcessor : public ComputeProcessor {
77 * FFTProcessor() : ComputeProcessor("shaders/fft.comp") {
78 * configure_fft_bindings();
79 * }
80 *
81 * void on_attach(std::shared_ptr<Buffer> buffer) override {
82 * ComputeProcessor::on_attach(buffer);
83 * // FFT-specific setup
84 * }
85 * };
86 */
87class MAYAFLUX_API ComputeProcessor : public ShaderProcessor {
88public:
89 /**
90 * @brief Construct processor with shader path
91 * @param shader_path Path to compute shader (.comp or .spv)
92 * @param workgroup_x Workgroup size X (default 256)
93 */
94 explicit ComputeProcessor(const std::string& shader_path, uint32_t workgroup_x = 256);
95
96 //==========================================================================
97 // Dispatch Configuration
98 //==========================================================================
99
100 /**
101 * @brief Set workgroup size (should match shader local_size)
102 * @param x Workgroup size X
103 * @param y Workgroup size Y (default 1)
104 * @param z Workgroup size Z (default 1)
105 */
106 void set_workgroup_size(uint32_t x, uint32_t y = 1, uint32_t z = 1);
107
108 /**
109 * @brief Set dispatch mode
110 * @param mode Dispatch calculation mode
111 */
112 void set_dispatch_mode(ShaderDispatchConfig::DispatchMode mode);
113
114 /**
115 * @brief Set manual dispatch group counts
116 * @param x Group count X
117 * @param y Group count Y (default 1)
118 * @param z Group count Z (default 1)
119 */
120 void set_manual_dispatch(uint32_t x, uint32_t y = 1, uint32_t z = 1);
121
122 /**
123 * @brief Set custom dispatch calculator
124 * @param calculator Function that calculates dispatch from buffer
125 */
126 void set_custom_dispatch(std::function<std::array<uint32_t, 3>(const std::shared_ptr<VKBuffer>&)> calculator);
127
128 /**
129 * @brief Get current dispatch configuration
130 */
131 [[nodiscard]] const ShaderDispatchConfig& get_dispatch_config() const { return m_dispatch_config; }
132
133 /**
134 * @brief Check if pipeline is created
135 */
136 bool is_pipeline_ready() const { return m_pipeline_id != Portal::Graphics::INVALID_COMPUTE_PIPELINE; }
137
138protected:
139 /**
140 * @brief Calculate dispatch size from buffer
141 * @param buffer Buffer to process
142 * @return {group_count_x, group_count_y, group_count_z}
143 *
144 * Override for custom dispatch calculation logic.
145 * Default implementation uses m_config.dispatch settings.
146 */
147 virtual std::array<uint32_t, 3> calculate_dispatch_size(const std::shared_ptr<VKBuffer>& buffer);
148
149 void initialize_pipeline(const std::shared_ptr<VKBuffer>& buffer) override;
150
151 void initialize_descriptors(const std::shared_ptr<VKBuffer>& buffer) override;
152
153 void cleanup() override;
154
155private:
157
158 void execute_shader(const std::shared_ptr<VKBuffer>& buffer) override;
159
160 Portal::Graphics::ComputePipelineID m_pipeline_id = Portal::Graphics::INVALID_COMPUTE_PIPELINE;
161};
162
163} // namespace MayaFlux::Buffers
const ShaderDispatchConfig & get_dispatch_config() const
Get current dispatch configuration.
bool is_pipeline_ready() const
Check if pipeline is created.
Specialized ShaderProcessor for Compute Pipelines.
Abstract base class for shader-based buffer processing.
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.
@ BUFFER_SIZE
Calculate from buffer byte size.
@ ELEMENT_COUNT
Calculate from buffer element count.
uint32_t workgroup_x
Workgroup size X (should match shader)
Configuration for compute shader dispatch.