MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioComputeProcessor.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::Buffers {
6
7class AudioBuffer;
8
9/**
10 * @class AudioComputeProcessor
11 * @brief Runs a compute shader over one audio block on the GPU
12 *
13 * Install as the default processor of a child AudioBuffer under AUDIO_PARALLEL.
14 * Each cycle the child's samples are written to an owned host-visible storage
15 * buffer, bound as "block" at set 0, binding 0. The shader is dispatched
16 * synchronously and the result is read back into the child.
17 *
18 * A dispatch that does not run leaves the child cleared.
19 *
20 * Usage:
21 * auto proc = std::make_shared<AudioComputeProcessor>("shaders/gain.comp");
22 * child->set_default_processor(proc);
23 */
24class MAYAFLUX_API AudioComputeProcessor : public ComputeProcessor {
25public:
26 /**
27 * @param shader_path Path to a compute shader (.comp or .spv)
28 * @param native_f64 True if the shader reads samples as double. Requires
29 * shaderFloat64 on the selected device. False reads them as uvec2.
30 * @param workgroup_x Workgroup size X, must match the shader
31 */
32 explicit AudioComputeProcessor(
33 const std::string& shader_path,
34 bool native_f64,
35 uint32_t workgroup_x = 256);
36
37 /**
38 * @param spec Generated shader specification
39 * @param native_f64 True if the shader reads samples as double. Requires
40 * shaderFloat64 on the selected device. False reads them as uvec2.
41 */
42 explicit AudioComputeProcessor(
44 bool native_f64);
45
46 void processing_function(const std::shared_ptr<Buffer>& buffer) override;
47 void on_attach(const std::shared_ptr<Buffer>& buffer) override;
48 void on_detach(const std::shared_ptr<Buffer>& buffer) override;
49 [[nodiscard]] bool is_compatible_with(const std::shared_ptr<Buffer>& buffer) const override;
50
51 [[nodiscard]] bool is_native_f64() const { return m_native_f64; }
52
53protected:
54 /**
55 * @brief Dispatch size from the block's frame count, not its byte size
56 */
57 std::array<uint32_t, 3> calculate_dispatch_size(const std::shared_ptr<VKBuffer>& buffer) override;
58
59private:
60 bool m_native_f64 {};
61 std::atomic<bool> m_attached { false }; ///< Set on attach, cleared on detach.
62 std::atomic<bool> m_in_flight { false }; ///< Set while processing_function runs on the processing thread.
63 std::atomic<bool> m_holds_block { false }; ///< True while m_block is allocated and bound.
64 std::shared_ptr<VKBuffer> m_block;
65 uint32_t m_frames {};
66
67 void configure();
68 void validate_shader();
69 void resize_block(uint32_t frames);
70 void process_block(const std::shared_ptr<Buffer>& buffer);
71 void release(const std::shared_ptr<Buffer>& buffer);
72};
73
74} // namespace MayaFlux::Buffers
Concrete audio implementation of the Buffer interface for double-precision audio data.
void on_attach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is attached to a buffer.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
std::atomic< bool > m_attached
Set on attach, cleared on detach.
bool is_compatible_with(const std::shared_ptr< Buffer > &buffer) const override
Checks if this processor can handle the specified buffer type.
std::atomic< bool > m_holds_block
True while m_block is allocated and bound.
AudioComputeProcessor(const std::string &shader_path, bool native_f64, uint32_t workgroup_x=256)
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
std::atomic< bool > m_in_flight
Set while processing_function runs on the processing thread.
ComputeProcessor(const std::string &shader_path, uint32_t workgroup_x=256)
Construct processor with shader path.
Complete declarative description of a generated compute shader.