diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-16 17:12:11 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-16 17:12:11 +0100 |
| commit | 18a3ccb2a1335d3747657e0b764af31fd723856e (patch) | |
| tree | 3d310d9f0fb915a8092d83c22c224300f7cd86ed /src/effects | |
| parent | c79ebff06ae74135c5f67ecc01d5bb55aeb5eda9 (diff) | |
feat: Add PeakMeterEffect v2 for test_demo audio visualization
Ports PeakMeterEffect to v2 Effect system with proper DAG routing.
Red horizontal bar overlay displays audio_intensity for visual debugging
of audio-visual synchronization.
Changes:
- New: src/effects/peak_meter_effect.{h,cc} - v2 implementation
- Timeline: FlashEffect -> flash_out -> PeakMeterEffect -> sink
- Build: Added to COMMON_GPU_EFFECTS and demo_effects.h
- Test: Added to test_demo_effects.cc (9/9 effects pass)
- Cleanup: Removed old disabled PeakMeterEffect code from test_demo.cc
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/effects')
| -rw-r--r-- | src/effects/peak_meter_effect.cc | 98 | ||||
| -rw-r--r-- | src/effects/peak_meter_effect.h | 23 |
2 files changed, 121 insertions, 0 deletions
diff --git a/src/effects/peak_meter_effect.cc b/src/effects/peak_meter_effect.cc new file mode 100644 index 0000000..692d851 --- /dev/null +++ b/src/effects/peak_meter_effect.cc @@ -0,0 +1,98 @@ +// Peak meter overlay for audio debugging + +#include "effects/peak_meter_effect.h" +#include "gpu/post_process_helper.h" +#include "gpu/shader_composer.h" +#include "util/fatal_error.h" + +PeakMeterEffect::PeakMeterEffect(const GpuContext& ctx, + const std::vector<std::string>& inputs, + const std::vector<std::string>& outputs) + : Effect(ctx, inputs, outputs), pipeline_(nullptr), bind_group_(nullptr) { + HEADLESS_RETURN_IF_NULL(ctx_.device); + + uniforms_buffer_.init(ctx_.device); + + const char* shader_main = R"( + struct VertexOutput { + @builtin(position) position: vec4<f32>, + @location(0) uv: vec2<f32>, + }; + + @group(0) @binding(0) var inputSampler: sampler; + @group(0) @binding(1) var inputTexture: texture_2d<f32>; + @group(0) @binding(2) var<uniform> uniforms: CommonUniforms; + + @vertex + fn vs_main(@builtin(vertex_index) vertexIndex: u32) -> VertexOutput { + var output: VertexOutput; + var pos = array<vec2<f32>, 3>( + vec2<f32>(-1.0, -1.0), + vec2<f32>(3.0, -1.0), + vec2<f32>(-1.0, 3.0) + ); + output.position = vec4<f32>(pos[vertexIndex], 0.0, 1.0); + output.uv = pos[vertexIndex] * 0.5 + 0.5; + return output; + } + + @fragment + fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> { + let bar_y_min = 0.005; + let bar_y_max = 0.015; + let bar_x_min = 0.015; + let bar_x_max = 0.250; + let in_bar_y = input.uv.y >= bar_y_min && input.uv.y <= bar_y_max; + let in_bar_x = input.uv.x >= bar_x_min && input.uv.x <= bar_x_max; + + if (in_bar_y && in_bar_x) { + let uv_x = (input.uv.x - bar_x_min) / (bar_x_max - bar_x_min); + let factor = step(uv_x, uniforms.audio_intensity); + return mix(vec4<f32>(0.0, 0.0, 0.0, 1.0), vec4<f32>(1.0, 0.0, 0.0, 1.0), factor); + } + + return textureSample(inputTexture, inputSampler, input.uv); + } + )"; + + std::string shader_code = + ShaderComposer::Get().Compose({"common_uniforms"}, shader_main); + + pipeline_ = create_post_process_pipeline(ctx_.device, + WGPUTextureFormat_RGBA8Unorm, + shader_code.c_str()); +} + +PeakMeterEffect::~PeakMeterEffect() { + if (bind_group_) wgpuBindGroupRelease(bind_group_); + if (pipeline_) wgpuRenderPipelineRelease(pipeline_); +} + +void PeakMeterEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + WGPUTextureView input_view = nodes.get_view(input_nodes_[0]); + WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); + + uniforms_buffer_.update(ctx_.queue, params); + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, + uniforms_buffer_.get(), {nullptr, 0}); + + WGPURenderPassColorAttachment color_attachment = {}; + color_attachment.view = output_view; + color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; + color_attachment.loadOp = WGPULoadOp_Load; + color_attachment.storeOp = WGPUStoreOp_Store; + color_attachment.clearValue = {0.0, 0.0, 0.0, 1.0}; + + WGPURenderPassDescriptor pass_desc = {}; + pass_desc.colorAttachmentCount = 1; + pass_desc.colorAttachments = &color_attachment; + + WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); + wgpuRenderPassEncoderEnd(pass); + wgpuRenderPassEncoderRelease(pass); +} diff --git a/src/effects/peak_meter_effect.h b/src/effects/peak_meter_effect.h new file mode 100644 index 0000000..905bcda --- /dev/null +++ b/src/effects/peak_meter_effect.h @@ -0,0 +1,23 @@ +// Peak meter overlay for audio debugging +// Renders red horizontal bar showing audio_intensity + +#pragma once +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +class PeakMeterEffect : public Effect { + public: + PeakMeterEffect(const GpuContext& ctx, + const std::vector<std::string>& inputs, + const std::vector<std::string>& outputs); + ~PeakMeterEffect() override; + + void render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) override; + + private: + WGPURenderPipeline pipeline_; + WGPUBindGroup bind_group_; + UniformBuffer<UniformsSequenceParams> uniforms_buffer_; +}; |
