MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
AudioComputeProcessor.cpp
Go to the documentation of this file.
2
8
9namespace MayaFlux::Buffers {
10
11namespace {
12 constexpr const char* k_block_name = "block";
13 constexpr size_t k_word_bytes = sizeof(uint64_t);
14}
15
16//==============================================================================
17// Construction
18//==============================================================================
19
21 const std::string& shader_path,
22 bool native_f64,
23 uint32_t workgroup_x)
24 : ComputeProcessor(shader_path, workgroup_x)
25 , m_native_f64(native_f64)
26{
27 configure();
28}
29
32 bool native_f64)
33 : ComputeProcessor(spec)
34 , m_native_f64(native_f64)
35{
36 configure();
37}
38
40{
42
43 auto& bindings = m_config.bindings;
44 for (auto it = bindings.begin(); it != bindings.end();) {
45 if (it->second.set == 0 && it->second.binding == 0) {
46 it = bindings.erase(it);
47 } else {
48 ++it;
49 }
50 }
51
52 add_binding(k_block_name, ShaderBinding(0, 0));
53}
54
55//==============================================================================
56// Attachment
57//==============================================================================
58
59void AudioComputeProcessor::on_attach(const std::shared_ptr<Buffer>& buffer)
60{
61 auto audio = std::dynamic_pointer_cast<AudioBuffer>(buffer);
62 if (!audio) {
64 "AudioComputeProcessor can only attach to an AudioBuffer");
65 return;
66 }
67
68 if (m_native_f64) {
69 const auto features = Portal::Graphics::get_shader_foundry()
71 .getFeatures();
72 if (!features.shaderFloat64) {
73 error<std::runtime_error>(
76 std::source_location::current(),
77 "NATIVE_F64 requires shaderFloat64 on the selected device");
78 }
79 }
80
81 if (!m_block) {
82 resize_block(static_cast<uint32_t>(audio->get_data().size()));
83 }
84 if (m_block) {
87 }
88 m_attached.store(true, std::memory_order_seq_cst);
89}
90
92{
93 if (!is_shader_loaded()) {
94 return;
95 }
96
98
99 const auto block_binding = std::ranges::find_if(
100 reflection.descriptor_bindings,
101 [](const auto& binding) { return binding.set == 0 && binding.binding == 0; });
102
103 if (block_binding == reflection.descriptor_bindings.end()) {
105 "AudioComputeProcessor shader declares no binding at set 0, binding 0 for 'block'");
106 } else if (block_binding->type != vk::DescriptorType::eStorageBuffer) {
108 "AudioComputeProcessor shader binding set 0, binding 0 is not a storage buffer");
109 }
110
111 if (reflection.workgroup_size) {
112 const auto& size = *reflection.workgroup_size;
113 const auto& dispatch = get_dispatch_config();
114 if (size[0] != dispatch.workgroup_x || size[1] != dispatch.workgroup_y || size[2] != dispatch.workgroup_z) {
116 "AudioComputeProcessor shader workgroup {}x{}x{} does not match dispatch workgroup {}x{}x{}",
117 size[0], size[1], size[2], dispatch.workgroup_x, dispatch.workgroup_y, dispatch.workgroup_z);
118 }
119 }
120}
121
122void AudioComputeProcessor::on_detach(const std::shared_ptr<Buffer>& buffer)
123{
124 m_attached.store(false, std::memory_order_seq_cst);
125 if (!m_in_flight.load(std::memory_order_seq_cst)) {
126 release(buffer);
127 }
128}
129
130void AudioComputeProcessor::release(const std::shared_ptr<Buffer>& buffer)
131{
132 if (!m_holds_block.exchange(false, std::memory_order_seq_cst)) {
133 return;
134 }
135
136 unbind_buffer(k_block_name);
137 m_block.reset();
138 m_frames = 0;
140}
141
142bool AudioComputeProcessor::is_compatible_with(const std::shared_ptr<Buffer>& buffer) const
143{
144 return std::dynamic_pointer_cast<AudioBuffer>(buffer) != nullptr;
145}
146
147//==============================================================================
148// Block Management
149//==============================================================================
150
152{
153 if (frames == 0) {
154 return;
155 }
156
157 const size_t bytes = static_cast<size_t>(frames) * k_word_bytes;
158
159 if (!m_block) {
160 m_block = std::make_shared<VKBuffer>(
161 bytes, VKBuffer::Usage::HOST_STORAGE, Kakshya::DataModality::UNKNOWN);
162 bind_buffer(k_block_name, m_block);
163 m_holds_block.store(true, std::memory_order_seq_cst);
164 } else if (m_block->get_size_bytes() != bytes) {
165 m_block->resize(bytes, false);
166 }
167
168 m_frames = frames;
169}
170
171//==============================================================================
172// Processing
173//==============================================================================
174
175void AudioComputeProcessor::processing_function(const std::shared_ptr<Buffer>& buffer)
176{
177 m_in_flight.store(true, std::memory_order_seq_cst);
178 process_block(buffer);
179 m_in_flight.store(false, std::memory_order_seq_cst);
180
181 if (!m_attached.load(std::memory_order_seq_cst)) {
182 release(buffer);
183 }
184}
185
186void AudioComputeProcessor::process_block(const std::shared_ptr<Buffer>& buffer)
187{
188 auto audio = std::dynamic_pointer_cast<AudioBuffer>(buffer);
189 if (!audio) {
191 "AudioComputeProcessor received a non-audio buffer");
192 return;
193 }
194
195 auto& samples = audio->get_data();
196 const auto frames = static_cast<uint32_t>(samples.size());
197
198 if (frames == 0) {
199 return;
200 }
201
202 if (!m_attached.load(std::memory_order_seq_cst)) {
203 audio->clear();
204 return;
205 }
206
207 if (frames != m_frames) {
208 resize_block(frames);
209 }
210
211 const size_t bytes = static_cast<size_t>(frames) * k_word_bytes;
212
213 auto* words = static_cast<unsigned char*>(m_block->get_buffer_resources().mapped_ptr);
214 if (!words) {
215 audio->clear();
216 return;
217 }
218
219 for (size_t i = 0; i < frames; ++i) {
220 Transitive::Protocol::store_le_double(words + i * k_word_bytes, samples[i]);
221 }
222 upload_to_gpu(words, bytes, m_block, nullptr, 0, true);
223
224 const auto before = m_last_command_buffer;
226
227 if (!is_shader_loaded() || m_last_command_buffer == before) {
228 audio->clear();
230 "AudioComputeProcessor dispatch did not run, child cleared");
231 return;
232 }
233
234 const auto& resources = m_block->get_buffer_resources();
237 if (service && service->invalidate_range) {
238 service->invalidate_range(resources.memory, 0, bytes);
239 }
240
241 const auto* out = static_cast<const unsigned char*>(resources.mapped_ptr);
242 for (size_t i = 0; i < frames; ++i) {
243 samples[i] = Transitive::Protocol::load_le_double(out + i * k_word_bytes);
244 }
245}
246
247std::array<uint32_t, 3> AudioComputeProcessor::calculate_dispatch_size(const std::shared_ptr<VKBuffer>&)
248{
249 const uint32_t workgroup = get_dispatch_config().workgroup_x;
250 return { (m_frames + workgroup - 1) / workgroup, 1, 1 };
251}
252
253} // namespace MayaFlux::Buffers
#define MF_ERROR(comp, ctx,...)
#define MF_RT_ERROR(comp, ctx,...)
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.
void process_block(const std::shared_ptr< Buffer > &buffer)
std::array< uint32_t, 3 > calculate_dispatch_size(const std::shared_ptr< VKBuffer > &buffer) override
Dispatch size from the block's frame count, not its byte size.
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 release(const std::shared_ptr< Buffer > &buffer)
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.
const ShaderDispatchConfig & get_dispatch_config() const
Get current dispatch configuration.
ComputeProcessor(const std::string &shader_path, uint32_t workgroup_x=256)
Construct processor with shader path.
Portal::Graphics::CommandBufferID m_last_command_buffer
void unbind_buffer(const std::string &descriptor_name)
Unbind a buffer from a descriptor.
Portal::Graphics::ShaderID m_shader_id
void on_detach(const std::shared_ptr< Buffer > &buffer) override
Called when this processor is detached from a buffer.
void processing_function(const std::shared_ptr< Buffer > &buffer) override
The core processing function that must be implemented by derived classes.
void add_binding(const std::string &descriptor_name, const ShaderBinding &binding)
Add descriptor binding configuration.
bool is_shader_loaded() const
Check if shader is loaded.
void bind_buffer(const std::string &descriptor_name, const std::shared_ptr< VKBuffer > &buffer)
Bind a VKBuffer to a named shader descriptor.
void prepare_for(const std::shared_ptr< VKBuffer > &vk_buffer)
Creates or rebuilds the shader, pipeline and descriptors that the next dispatch needs.
ShaderReflectionInfo get_shader_reflection(ShaderID shader_id)
Get reflection info for compiled shader.
vk::PhysicalDevice get_physical_device() const
Get physical device handle.
Interface * get_service()
Query for a backend service.
static BackendRegistry & instance()
Get the global registry instance.
void upload_to_gpu(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging, size_t dst_offset, bool awaited)
Upload raw data to GPU buffer (auto-detects host-visible vs device-local).
@ AUDIO_PARALLEL
High-performance audio processing with GPU acceleration.
@ BufferProcessing
Buffer processing (Buffers::BufferManager, processing chains).
@ Buffers
Buffers, Managers, processors and processing chains.
@ UNKNOWN
Unknown or undefined modality.
Definition NDData.hpp:156
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
void store_le_double(uint8_t *dst, double val) noexcept
Store a double as eight little-endian bytes of its IEEE 754 bits.
double load_le_double(const uint8_t *src) noexcept
Load a double from eight little-endian bytes of its IEEE 754 bits.
Describes how a VKBuffer binds to a shader descriptor.
uint32_t workgroup_x
Workgroup size X (should match shader).
Complete declarative description of a generated compute shader.
Backend buffer management service interface.