From 4b23fdc63d422b31b6ad86d34218e7b66b462514 Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 8 Feb 2026 17:43:44 +0100 Subject: feat(gpu): Add parameter-driven GaussianBlurEffect Extends shader parametrization system to GaussianBlurEffect with strength parameter (Task #73 continued). Changes: - Added GaussianBlurParams struct (strength, default: 2.0f) - Added GaussianBlurUniforms with proper WGSL alignment (32 bytes) - Updated shader to use parameterized strength instead of hardcoded 2.0 - Extended seq_compiler to parse strength parameter - Updated demo.seq with 2 parameterized instances: * Line 38: strength=3.0 (stronger blur for particles) * Line 48: strength=1.5 (subtle blur) Technical details: - Backward-compatible default constructor maintained - Migrated from raw buffer to UniformBuffer - Shader replaces 'let base_size = 2.0' with 'uniforms.strength' - Generated code creates GaussianBlurParams initialization Testing: - All 32/32 tests pass - Demo runs without errors - Generated code verified correct Co-Authored-By: Claude Sonnet 4.5 --- assets/demo.seq | 4 +-- assets/final/shaders/gaussian_blur.wgsl | 14 +++++----- src/gpu/demo_effects.h | 26 ++++++++++++++++++ src/gpu/effects/gaussian_blur_effect.cc | 48 +++++++++++++++++++++++---------- tools/seq_compiler.cc | 16 +++++++++++ 5 files changed, 86 insertions(+), 22 deletions(-) diff --git a/assets/demo.seq b/assets/demo.seq index 8f84c30..cada95e 100644 --- a/assets/demo.seq +++ b/assets/demo.seq @@ -35,7 +35,7 @@ SEQUENCE 4b 0 SEQUENCE 6b 1 EFFECT + ParticleSprayEffect 0 4 # Priority 0 (spray particles) EFFECT + ParticlesEffect 0 4 # Priority 1 - EFFECT = GaussianBlurEffect 0 8 # Priority 1 (same layer) + EFFECT = GaussianBlurEffect 0 8 strength=3.0 # Priority 1 (stronger blur) SEQUENCE 7b 0 EFFECT + HeptagonEffect 0.0 .2 # Priority 0 @@ -46,7 +46,7 @@ SEQUENCE 7b 0 SEQUENCE 8b 3 EFFECT + ThemeModulationEffect 0 4 # Priority 0 EFFECT = HeptagonEffect 0.0 4.0 # Priority 0 (same layer) - EFFECT + GaussianBlurEffect 0 8 # Priority 1 + EFFECT + GaussianBlurEffect 0 8 strength=1.5 # Priority 1 (subtle blur) EFFECT + ChromaAberrationEffect 0 6 offset=0.03 angle=0.785 # Priority 2 (diagonal, stronger) EFFECT + SolarizeEffect 0 10 # Priority 3 diff --git a/assets/final/shaders/gaussian_blur.wgsl b/assets/final/shaders/gaussian_blur.wgsl index 7d39ac4..e848c6b 100644 --- a/assets/final/shaders/gaussian_blur.wgsl +++ b/assets/final/shaders/gaussian_blur.wgsl @@ -6,7 +6,10 @@ struct Uniforms { beat: f32, intensity: f32, aspect_ratio: f32, - resolution: vec2, + width: f32, + height: f32, + strength: f32, + _pad: f32, }; @group(0) @binding(2) var uniforms: Uniforms; @@ -21,17 +24,16 @@ struct Uniforms { } @fragment fn fs_main(@builtin(position) p: vec4) -> @location(0) vec4 { - let uv = p.xy / uniforms.resolution; + let uv = p.xy / vec2(uniforms.width, uniforms.height); var res = vec4(0.0); - // Reduced base size + dramatic beat pulsation - let base_size = 2.0; + // Parameterized strength + dramatic beat pulsation let pulse = 0.5 + uniforms.intensity * 2.0; // Pulsate between 0.5x and 2.5x with beat - let size = base_size * pulse; + let size = uniforms.strength * pulse; for (var x: f32 = -2.0; x <= 2.0; x += 1.0) { for (var y: f32 = -2.0; y <= 2.0; y += 1.0) { - res += textureSample(txt, smplr, uv + vec2(x, y) * size / uniforms.resolution.x); + res += textureSample(txt, smplr, uv + vec2(x, y) * size / uniforms.width); } } return res / 25.0; diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index 36be118..6c8729d 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -77,12 +77,38 @@ class ParticleSprayEffect : public Effect { GpuBuffer particles_buffer_; }; +// Parameters for GaussianBlurEffect (set at construction time) +struct GaussianBlurParams { + float strength = 2.0f; // Default: 2.0 pixel blur radius +}; + +// Uniform data sent to GPU shader +struct GaussianBlurUniforms { + float time; // offset 0 + float beat; // offset 4 + float intensity; // offset 8 + float aspect_ratio; // offset 12 + float width; // offset 16 + float height; // offset 20 + float strength; // offset 24 + float _pad; // offset 28 +}; +static_assert(sizeof(GaussianBlurUniforms) == 32, + "GaussianBlurUniforms must be 32 bytes for WGSL alignment"); + class GaussianBlurEffect : public PostProcessEffect { public: + // Backward compatibility constructor (uses default params) GaussianBlurEffect(const GpuContext& ctx); + // New parameterized constructor + GaussianBlurEffect(const GpuContext& ctx, const GaussianBlurParams& params); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; + + private: + GaussianBlurParams params_; + UniformBuffer uniforms_; }; class SolarizeEffect : public PostProcessEffect { diff --git a/src/gpu/effects/gaussian_blur_effect.cc b/src/gpu/effects/gaussian_blur_effect.cc index f1736bf..3975aff 100644 --- a/src/gpu/effects/gaussian_blur_effect.cc +++ b/src/gpu/effects/gaussian_blur_effect.cc @@ -1,26 +1,46 @@ // This file is part of the 64k demo project. -// It implements the GaussianBlurEffect. +// It implements the GaussianBlurEffect with parameterization. #include "gpu/demo_effects.h" +#include "gpu/effects/post_process_helper.h" #include "gpu/gpu.h" // --- GaussianBlurEffect --- + +// Backward compatibility constructor (delegates to parameterized constructor) GaussianBlurEffect::GaussianBlurEffect(const GpuContext& ctx) - : PostProcessEffect(ctx) { - uniforms_ = - gpu_create_buffer(ctx_.device, sizeof(float) * 6, - WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); + : GaussianBlurEffect(ctx, GaussianBlurParams{}) { +} + +// Parameterized constructor +GaussianBlurEffect::GaussianBlurEffect(const GpuContext& ctx, + const GaussianBlurParams& params) + : PostProcessEffect(ctx), params_(params) { pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, gaussian_blur_shader_wgsl); + uniforms_.init(ctx_.device); } -void GaussianBlurEffect::render(WGPURenderPassEncoder pass, float t, float b, - float i, float a) { - struct { - float t, b, i, a, w, h; - } u = {t, b, i, a, (float)width_, (float)height_}; - wgpuQueueWriteBuffer(ctx_.queue, uniforms_.buffer, 0, &u, sizeof(u)); - PostProcessEffect::render(pass, t, b, i, a); + +void GaussianBlurEffect::render(WGPURenderPassEncoder pass, float time, + float beat, float intensity, + float aspect_ratio) { + // Update uniforms with current state and parameters + const GaussianBlurUniforms u = {.time = time, + .beat = beat, + .intensity = intensity, + .aspect_ratio = aspect_ratio, + .width = (float)width_, + .height = (float)height_, + .strength = params_.strength, + ._pad = 0.0f}; + uniforms_.update(ctx_.queue, u); + + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); } -void GaussianBlurEffect::update_bind_group(WGPUTextureView v) { - pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, uniforms_); + +void GaussianBlurEffect::update_bind_group(WGPUTextureView input_view) { + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, + uniforms_.get()); } diff --git a/tools/seq_compiler.cc b/tools/seq_compiler.cc index d89ab3d..218ef93 100644 --- a/tools/seq_compiler.cc +++ b/tools/seq_compiler.cc @@ -958,6 +958,22 @@ int main(int argc, char* argv[]) { } } + out_file << " seq->add_effect(std::make_shared<" + << eff.class_name << ">(ctx, p), " << eff.start << "f, " + << eff.end << "f, " << eff.priority << ");\n"; + out_file << " }\n"; + } else if (!eff.params.empty() && + eff.class_name == "GaussianBlurEffect") { + // Generate parameter struct initialization for GaussianBlurEffect + out_file << " {\n"; + out_file << " GaussianBlurParams p;\n"; + + for (const auto& [key, value] : eff.params) { + if (key == "strength") { + out_file << " p.strength = " << value << "f;\n"; + } + } + out_file << " seq->add_effect(std::make_shared<" << eff.class_name << ">(ctx, p), " << eff.start << "f, " << eff.end << "f, " << eff.priority << ");\n"; -- cgit v1.2.3