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