12 constexpr const char* k_block_name =
"block";
13 constexpr size_t k_word_bytes =
sizeof(uint64_t);
21 const std::string& shader_path,
44 for (
auto it = bindings.begin(); it != bindings.end();) {
45 if (it->second.set == 0 && it->second.binding == 0) {
46 it = bindings.erase(it);
61 auto audio = std::dynamic_pointer_cast<AudioBuffer>(buffer);
64 "AudioComputeProcessor can only attach to an AudioBuffer");
72 if (!features.shaderFloat64) {
73 error<std::runtime_error>(
76 std::source_location::current(),
77 "NATIVE_F64 requires shaderFloat64 on the selected device");
82 resize_block(
static_cast<uint32_t
>(audio->get_data().size()));
88 m_attached.store(
true, std::memory_order_seq_cst);
99 const auto block_binding = std::ranges::find_if(
100 reflection.descriptor_bindings,
101 [](
const auto& binding) { return binding.set == 0 && binding.binding == 0; });
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");
111 if (reflection.workgroup_size) {
112 const auto& size = *reflection.workgroup_size;
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);
124 m_attached.store(
false, std::memory_order_seq_cst);
125 if (!
m_in_flight.load(std::memory_order_seq_cst)) {
132 if (!
m_holds_block.exchange(
false, std::memory_order_seq_cst)) {
144 return std::dynamic_pointer_cast<AudioBuffer>(buffer) !=
nullptr;
157 const size_t bytes =
static_cast<size_t>(frames) * k_word_bytes;
160 m_block = std::make_shared<VKBuffer>(
164 }
else if (
m_block->get_size_bytes() != bytes) {
177 m_in_flight.store(
true, std::memory_order_seq_cst);
179 m_in_flight.store(
false, std::memory_order_seq_cst);
181 if (!
m_attached.load(std::memory_order_seq_cst)) {
188 auto audio = std::dynamic_pointer_cast<AudioBuffer>(buffer);
191 "AudioComputeProcessor received a non-audio buffer");
195 auto& samples = audio->get_data();
196 const auto frames =
static_cast<uint32_t
>(samples.size());
202 if (!
m_attached.load(std::memory_order_seq_cst)) {
211 const size_t bytes =
static_cast<size_t>(frames) * k_word_bytes;
213 auto* words =
static_cast<unsigned char*
>(
m_block->get_buffer_resources().mapped_ptr);
219 for (
size_t i = 0; i < frames; ++i) {
230 "AudioComputeProcessor dispatch did not run, child cleared");
234 const auto& resources =
m_block->get_buffer_resources();
237 if (service && service->invalidate_range) {
238 service->invalidate_range(resources.memory, 0, bytes);
241 const auto* out =
static_cast<const unsigned char*
>(resources.mapped_ptr);
242 for (
size_t i = 0; i < frames; ++i) {
250 return { (
m_frames + workgroup - 1) / workgroup, 1, 1 };
#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.
std::shared_ptr< VKBuffer > m_block
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.
void resize_block(uint32_t frames)
ProcessingToken m_processing_token
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.
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.