From b8d4a815453acac752c6fb3c56d047e39a76fd05 Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 14 Feb 2026 19:05:34 +0100 Subject: feat(gpu): add SDF camera infrastructure and effect base class Add unified camera system for SDF raymarching effects: - CameraParams struct (80 bytes): inv_view matrix + FOV/near/far/aspect - SDFEffect base class: manages camera uniform, provides update_camera() helpers - camera_common.wgsl: getCameraRay(), position/forward/up/right extractors - SDFTestEffect: working example with orbiting camera + animated sphere Refactor effect headers: - Extract class definitions from demo_effects.h to individual .h files - Update includes in .cc files to use specific headers - Cleaner compilation dependencies, faster incremental builds Documentation: - Add SDF_EFFECT_GUIDE.md with complete workflow - Update ARCHITECTURE.md, UNIFORM_BUFFER_GUIDELINES.md - Update EFFECT_WORKFLOW.md, CONTRIBUTING.md Tests: 34/34 passing, SDFTestEffect validated Co-Authored-By: Claude Sonnet 4.5 --- src/effects/chroma_aberration_effect.cc | 3 +- src/effects/chroma_aberration_effect.h | 29 +++++ src/effects/distort_effect.cc | 3 +- src/effects/distort_effect.h | 28 +++++ src/effects/gaussian_blur_effect.cc | 3 +- src/effects/gaussian_blur_effect.h | 32 +++++ src/effects/heptagon_effect.cc | 3 +- src/effects/heptagon_effect.h | 16 +++ src/effects/moving_ellipse_effect.cc | 3 +- src/effects/moving_ellipse_effect.h | 16 +++ src/effects/particle_defs.h | 13 ++ src/effects/particle_spray_effect.cc | 3 +- src/effects/particle_spray_effect.h | 21 ++++ src/effects/particles_effect.cc | 3 +- src/effects/particles_effect.h | 21 ++++ src/effects/passthrough_effect.cc | 3 +- src/effects/passthrough_effect.h | 14 +++ src/effects/sdf_test_effect.cc | 36 ++++++ src/effects/sdf_test_effect.h | 16 +++ src/effects/solarize_effect.cc | 3 +- src/effects/solarize_effect.h | 16 +++ src/effects/vignette_effect.cc | 3 +- src/effects/vignette_effect.h | 26 ++++ src/gpu/camera_params.h | 18 +++ src/gpu/demo_effects.h | 203 +++++--------------------------- src/gpu/sdf_effect.h | 53 +++++++++ src/gpu/shaders.cc | 5 + src/gpu/shaders.h | 1 + src/tests/gpu/test_demo_effects.cc | 1 + 29 files changed, 414 insertions(+), 181 deletions(-) create mode 100644 src/effects/chroma_aberration_effect.h create mode 100644 src/effects/distort_effect.h create mode 100644 src/effects/gaussian_blur_effect.h create mode 100644 src/effects/heptagon_effect.h create mode 100644 src/effects/moving_ellipse_effect.h create mode 100644 src/effects/particle_defs.h create mode 100644 src/effects/particle_spray_effect.h create mode 100644 src/effects/particles_effect.h create mode 100644 src/effects/passthrough_effect.h create mode 100644 src/effects/sdf_test_effect.cc create mode 100644 src/effects/sdf_test_effect.h create mode 100644 src/effects/solarize_effect.h create mode 100644 src/effects/vignette_effect.h create mode 100644 src/gpu/camera_params.h create mode 100644 src/gpu/sdf_effect.h (limited to 'src') diff --git a/src/effects/chroma_aberration_effect.cc b/src/effects/chroma_aberration_effect.cc index 2a92225..4038696 100644 --- a/src/effects/chroma_aberration_effect.cc +++ b/src/effects/chroma_aberration_effect.cc @@ -1,9 +1,10 @@ // This file is part of the 64k demo project. // It implements the ChromaAberrationEffect with parameterization. -#include "gpu/demo_effects.h" +#include "effects/chroma_aberration_effect.h" #include "gpu/gpu.h" #include "gpu/post_process_helper.h" +#include "gpu/shaders.h" // --- ChromaAberrationEffect --- diff --git a/src/effects/chroma_aberration_effect.h b/src/effects/chroma_aberration_effect.h new file mode 100644 index 0000000..1790952 --- /dev/null +++ b/src/effects/chroma_aberration_effect.h @@ -0,0 +1,29 @@ +// This file is part of the 64k demo project. +// It declares the ChromaAberrationEffect. + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +// Parameters for ChromaAberrationEffect (set at construction time) +struct ChromaAberrationParams { + float offset_scale = 0.02f; // Default: 2% screen offset + float angle = 0.0f; // Default: horizontal (0 radians) +}; + +class ChromaAberrationEffect : public PostProcessEffect { + public: + // Backward compatibility constructor (uses default params) + ChromaAberrationEffect(const GpuContext& ctx); + // New parameterized constructor + ChromaAberrationEffect(const GpuContext& ctx, + const ChromaAberrationParams& params); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + void update_bind_group(WGPUTextureView input_view) override; + + private: + ChromaAberrationParams params_; + UniformBuffer params_buffer_; +}; diff --git a/src/effects/distort_effect.cc b/src/effects/distort_effect.cc index f4e68d2..aa72386 100644 --- a/src/effects/distort_effect.cc +++ b/src/effects/distort_effect.cc @@ -1,8 +1,9 @@ // This file is part of the 64k demo project. // It implements the DistortEffect. -#include "gpu/demo_effects.h" +#include "effects/distort_effect.h" #include "gpu/gpu.h" +#include "gpu/shaders.h" // --- DistortEffect --- DistortEffect::DistortEffect(const GpuContext& ctx) diff --git a/src/effects/distort_effect.h b/src/effects/distort_effect.h new file mode 100644 index 0000000..548cf91 --- /dev/null +++ b/src/effects/distort_effect.h @@ -0,0 +1,28 @@ +// This file is part of the 64k demo project. +// It declares the DistortEffect. + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +// Parameters for DistortEffect +struct DistortParams { + float strength = 0.01f; // Default distortion strength + float speed = 1.0f; // Default distortion speed +}; +static_assert(sizeof(DistortParams) == 8, + "DistortParams must be 8 bytes for WGSL alignment"); + +class DistortEffect : public PostProcessEffect { + public: + DistortEffect(const GpuContext& ctx); + DistortEffect(const GpuContext& ctx, const DistortParams& params); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + void update_bind_group(WGPUTextureView input_view) override; + + private: + DistortParams params_; + UniformBuffer params_buffer_; +}; diff --git a/src/effects/gaussian_blur_effect.cc b/src/effects/gaussian_blur_effect.cc index 6a0675d..b5961fa 100644 --- a/src/effects/gaussian_blur_effect.cc +++ b/src/effects/gaussian_blur_effect.cc @@ -1,9 +1,10 @@ // This file is part of the 64k demo project. // It implements the GaussianBlurEffect with parameterization. -#include "gpu/demo_effects.h" +#include "effects/gaussian_blur_effect.h" #include "gpu/gpu.h" #include "gpu/post_process_helper.h" +#include "gpu/shaders.h" // --- GaussianBlurEffect --- diff --git a/src/effects/gaussian_blur_effect.h b/src/effects/gaussian_blur_effect.h new file mode 100644 index 0000000..651c5c3 --- /dev/null +++ b/src/effects/gaussian_blur_effect.h @@ -0,0 +1,32 @@ +// This file is part of the 64k demo project. +// It declares the GaussianBlurEffect. + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +// Parameters for GaussianBlurEffect (set at construction time) +struct GaussianBlurParams { + float strength = 1.0f; // Default + float strength_audio = 0.5f; // how much to pulse with audio + float stretch = 1.f; // y/x axis ratio + float _pad = 0.; +}; +static_assert(sizeof(GaussianBlurParams) == 16, + "GaussianBlurParams must be 16 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, + const CommonPostProcessUniforms& uniforms) override; + void update_bind_group(WGPUTextureView input_view) override; + + private: + GaussianBlurParams params_; + UniformBuffer params_buffer_; +}; diff --git a/src/effects/heptagon_effect.cc b/src/effects/heptagon_effect.cc index 273adc2..d15882d 100644 --- a/src/effects/heptagon_effect.cc +++ b/src/effects/heptagon_effect.cc @@ -1,8 +1,9 @@ // This file is part of the 64k demo project. // It implements the HeptagonEffect. -#include "gpu/demo_effects.h" +#include "effects/heptagon_effect.h" #include "gpu/gpu.h" +#include "gpu/shaders.h" #include "util/mini_math.h" // --- HeptagonEffect --- diff --git a/src/effects/heptagon_effect.h b/src/effects/heptagon_effect.h new file mode 100644 index 0000000..fe19839 --- /dev/null +++ b/src/effects/heptagon_effect.h @@ -0,0 +1,16 @@ +// This file is part of the 64k demo project. +// It declares the HeptagonEffect. + +#pragma once + +#include "gpu/effect.h" + +class HeptagonEffect : public Effect { + public: + HeptagonEffect(const GpuContext& ctx); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + + private: + RenderPass pass_; +}; diff --git a/src/effects/moving_ellipse_effect.cc b/src/effects/moving_ellipse_effect.cc index e641927..6fb0195 100644 --- a/src/effects/moving_ellipse_effect.cc +++ b/src/effects/moving_ellipse_effect.cc @@ -1,9 +1,10 @@ // This file is part of the 64k demo project. // It implements the MovingEllipseEffect. -#include "gpu/demo_effects.h" +#include "effects/moving_ellipse_effect.h" #include "gpu/gpu.h" #include "gpu/post_process_helper.h" +#include "gpu/shaders.h" // --- MovingEllipseEffect --- MovingEllipseEffect::MovingEllipseEffect(const GpuContext& ctx) : Effect(ctx) { diff --git a/src/effects/moving_ellipse_effect.h b/src/effects/moving_ellipse_effect.h new file mode 100644 index 0000000..46c1f0e --- /dev/null +++ b/src/effects/moving_ellipse_effect.h @@ -0,0 +1,16 @@ +// This file is part of the 64k demo project. +// It declares the MovingEllipseEffect. + +#pragma once + +#include "gpu/effect.h" + +class MovingEllipseEffect : public Effect { + public: + MovingEllipseEffect(const GpuContext& ctx); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + + private: + RenderPass pass_; +}; diff --git a/src/effects/particle_defs.h b/src/effects/particle_defs.h new file mode 100644 index 0000000..dcbb830 --- /dev/null +++ b/src/effects/particle_defs.h @@ -0,0 +1,13 @@ +// This file is part of the 64k demo project. +// It defines common structures for particle-based effects. + +#pragma once + +static const int NUM_PARTICLES = 10000; + +struct Particle { + float pos[4]; + float vel[4]; + float rot[4]; + float color[4]; +}; diff --git a/src/effects/particle_spray_effect.cc b/src/effects/particle_spray_effect.cc index 0b0dba1..1acf67d 100644 --- a/src/effects/particle_spray_effect.cc +++ b/src/effects/particle_spray_effect.cc @@ -1,9 +1,10 @@ // This file is part of the 64k demo project. // It implements the ParticleSprayEffect. -#include "gpu/demo_effects.h" +#include "effects/particle_spray_effect.h" #include "gpu/gpu.h" #include "gpu/post_process_helper.h" +#include "gpu/shaders.h" #include // --- ParticleSprayEffect --- diff --git a/src/effects/particle_spray_effect.h b/src/effects/particle_spray_effect.h new file mode 100644 index 0000000..c83d691 --- /dev/null +++ b/src/effects/particle_spray_effect.h @@ -0,0 +1,21 @@ +// This file is part of the 64k demo project. +// It declares the ParticleSprayEffect. + +#pragma once + +#include "gpu/effect.h" +#include "effects/particle_defs.h" + +class ParticleSprayEffect : public Effect { + public: + ParticleSprayEffect(const GpuContext& ctx); + void compute(WGPUCommandEncoder encoder, + const CommonPostProcessUniforms& uniforms) override; + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + + private: + ComputePass compute_pass_; + RenderPass render_pass_; + GpuBuffer particles_buffer_; +}; diff --git a/src/effects/particles_effect.cc b/src/effects/particles_effect.cc index b05aecd..25589fd 100644 --- a/src/effects/particles_effect.cc +++ b/src/effects/particles_effect.cc @@ -1,9 +1,10 @@ // This file is part of the 64k demo project. // It implements the ParticlesEffect. -#include "gpu/demo_effects.h" +#include "effects/particles_effect.h" #include "gpu/gpu.h" #include "gpu/post_process_helper.h" +#include "gpu/shaders.h" #include // --- ParticlesEffect --- diff --git a/src/effects/particles_effect.h b/src/effects/particles_effect.h new file mode 100644 index 0000000..6d46ea2 --- /dev/null +++ b/src/effects/particles_effect.h @@ -0,0 +1,21 @@ +// This file is part of the 64k demo project. +// It declares the ParticlesEffect. + +#pragma once + +#include "gpu/effect.h" +#include "effects/particle_defs.h" + +class ParticlesEffect : public Effect { + public: + ParticlesEffect(const GpuContext& ctx); + void compute(WGPUCommandEncoder encoder, + const CommonPostProcessUniforms& uniforms) override; + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + + private: + ComputePass compute_pass_; + RenderPass render_pass_; + GpuBuffer particles_buffer_; +}; diff --git a/src/effects/passthrough_effect.cc b/src/effects/passthrough_effect.cc index aedb387..50f5a5c 100644 --- a/src/effects/passthrough_effect.cc +++ b/src/effects/passthrough_effect.cc @@ -1,8 +1,9 @@ // This file is part of the 64k demo project. // It implements the PassthroughEffect. -#include "gpu/demo_effects.h" +#include "effects/passthrough_effect.h" #include "gpu/gpu.h" +#include "gpu/shaders.h" // --- PassthroughEffect --- PassthroughEffect::PassthroughEffect(const GpuContext& ctx) diff --git a/src/effects/passthrough_effect.h b/src/effects/passthrough_effect.h new file mode 100644 index 0000000..36f93f2 --- /dev/null +++ b/src/effects/passthrough_effect.h @@ -0,0 +1,14 @@ +// This file is part of the 64k demo project. +// It declares the PassthroughEffect. + +#pragma once + +#include "gpu/effect.h" + +class PassthroughEffect : public PostProcessEffect { + public: + PassthroughEffect(const GpuContext& ctx); + void update_bind_group(WGPUTextureView input_view) override; + + private: +}; diff --git a/src/effects/sdf_test_effect.cc b/src/effects/sdf_test_effect.cc new file mode 100644 index 0000000..28b3513 --- /dev/null +++ b/src/effects/sdf_test_effect.cc @@ -0,0 +1,36 @@ +// This file is part of the 64k demo project. +// It implements the SDFTestEffect. + +#include "effects/sdf_test_effect.h" +#include "gpu/gpu.h" +#include "gpu/shaders.h" + +SDFTestEffect::SDFTestEffect(const GpuContext& ctx) : SDFEffect(ctx) { + ResourceBinding bindings[] = { + {uniforms_.get(), WGPUBufferBindingType_Uniform}, + {camera_params_.get(), WGPUBufferBindingType_Uniform}}; + pass_ = gpu_create_render_pass(ctx_.device, ctx_.format, + sdf_test_shader_wgsl, bindings, 2); + pass_.vertex_count = 3; +} + +void SDFTestEffect::render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) { + // Update common uniforms + uniforms_.update(ctx_.queue, uniforms); + + // Update camera (simple orbiting camera) + const float radius = 5.0f; + const float speed = 0.3f; + vec3 cam_pos(std::cos(uniforms.time * speed) * radius, 2.0f, + std::sin(uniforms.time * speed) * radius); + vec3 cam_target(0.0f, 0.0f, 0.0f); + vec3 cam_up(0.0f, 1.0f, 0.0f); + update_camera(cam_pos, cam_target, cam_up, 0.785398f, 0.1f, 100.0f, + uniforms.aspect_ratio); + + // Render + wgpuRenderPassEncoderSetPipeline(pass, pass_.pipeline); + wgpuRenderPassEncoderSetBindGroup(pass, 0, pass_.bind_group, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, pass_.vertex_count, 1, 0, 0); +} diff --git a/src/effects/sdf_test_effect.h b/src/effects/sdf_test_effect.h new file mode 100644 index 0000000..41baf83 --- /dev/null +++ b/src/effects/sdf_test_effect.h @@ -0,0 +1,16 @@ +// This file is part of the 64k demo project. +// It demonstrates SDFEffect base class usage. + +#pragma once + +#include "gpu/sdf_effect.h" + +class SDFTestEffect : public SDFEffect { + public: + SDFTestEffect(const GpuContext& ctx); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + + private: + RenderPass pass_; +}; diff --git a/src/effects/solarize_effect.cc b/src/effects/solarize_effect.cc index cdb9354..a367e51 100644 --- a/src/effects/solarize_effect.cc +++ b/src/effects/solarize_effect.cc @@ -1,8 +1,9 @@ // This file is part of the 64k demo project. // It implements the SolarizeEffect. -#include "gpu/demo_effects.h" +#include "effects/solarize_effect.h" #include "gpu/gpu.h" +#include "gpu/shaders.h" // --- SolarizeEffect --- SolarizeEffect::SolarizeEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { diff --git a/src/effects/solarize_effect.h b/src/effects/solarize_effect.h new file mode 100644 index 0000000..6132f58 --- /dev/null +++ b/src/effects/solarize_effect.h @@ -0,0 +1,16 @@ +// This file is part of the 64k demo project. +// It declares the SolarizeEffect. + +#pragma once + +#include "gpu/effect.h" + +class SolarizeEffect : public PostProcessEffect { + public: + SolarizeEffect(const GpuContext& ctx); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + void update_bind_group(WGPUTextureView input_view) override; + + private: +}; diff --git a/src/effects/vignette_effect.cc b/src/effects/vignette_effect.cc index f5c3f05..3ddbee3 100644 --- a/src/effects/vignette_effect.cc +++ b/src/effects/vignette_effect.cc @@ -1,9 +1,10 @@ // This file is part of the 64k demo project. // It implements the VignetteEffect. -#include "gpu/demo_effects.h" +#include "effects/vignette_effect.h" #include "gpu/gpu.h" #include "gpu/post_process_helper.h" +#include "gpu/shaders.h" VignetteEffect::VignetteEffect(const GpuContext& ctx) : VignetteEffect(ctx, VignetteParams()) { diff --git a/src/effects/vignette_effect.h b/src/effects/vignette_effect.h new file mode 100644 index 0000000..f891d14 --- /dev/null +++ b/src/effects/vignette_effect.h @@ -0,0 +1,26 @@ +// This file is part of the 64k demo project. +// It declares the VignetteEffect. + +#pragma once + +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +// Parameters for VignetteEffect +struct VignetteParams { + float radius = 0.5f; // Radius of the clear center + float softness = 0.5f; // Softness of the vignette edge +}; + +class VignetteEffect : public PostProcessEffect { + public: + VignetteEffect(const GpuContext& ctx); + VignetteEffect(const GpuContext& ctx, const VignetteParams& params); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + void update_bind_group(WGPUTextureView input_view) override; + + private: + VignetteParams params_; + UniformBuffer params_buffer_; +}; diff --git a/src/gpu/camera_params.h b/src/gpu/camera_params.h new file mode 100644 index 0000000..361f65f --- /dev/null +++ b/src/gpu/camera_params.h @@ -0,0 +1,18 @@ +// This file is part of the 64k demo project. +// It defines CameraParams for raymarching effects. + +#pragma once + +#include "util/mini_math.h" + +// Camera parameters for SDF raymarching effects +// Binding convention: @group(0) @binding(3) +struct CameraParams { + mat4 inv_view; // Inverse view matrix (screen→world transform) + float fov; // Vertical field of view in radians + float near_plane; // Near clipping plane + float far_plane; // Far clipping plane + float aspect_ratio; // Width/height ratio +}; +static_assert(sizeof(CameraParams) == 80, + "CameraParams must be 80 bytes for WGSL alignment"); diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index 72c8e6e..85498ad 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -1,192 +1,53 @@ // This file is part of the 64k demo project. -// It declares the concrete effects used in the demo. +// It includes all concrete effects used in the demo. #pragma once + +// Core 3D #include "3d/camera.h" #include "3d/renderer.h" #include "3d/scene.h" + +// Base effect classes #include "effect.h" -#include "effects/circle_mask_effect.h" -#include "effects/fade_effect.h" // FadeEffect with full definition -#include "effects/flash_cube_effect.h" -#include "effects/flash_effect.h" // FlashEffect with params support -#include "effects/hybrid_3d_effect.h" -#include "effects/rotating_cube_effect.h" -#include "effects/scene1_effect.h" -#include "effects/theme_modulation_effect.h" // ThemeModulationEffect with full definition -#include "gpu/gpu.h" #include "gpu/post_process_helper.h" #include "gpu/shaders.h" #include "gpu/texture_manager.h" #include "gpu/uniform_helper.h" -#include - -static const int NUM_PARTICLES = 10000; - -struct Particle { - float pos[4]; - float vel[4]; - float rot[4]; - float color[4]; -}; - -class HeptagonEffect : public Effect { - public: - HeptagonEffect(const GpuContext& ctx); - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - - private: - RenderPass pass_; -}; - -class ParticlesEffect : public Effect { - public: - ParticlesEffect(const GpuContext& ctx); - void compute(WGPUCommandEncoder encoder, - const CommonPostProcessUniforms& uniforms) override; - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - - private: - ComputePass compute_pass_; - RenderPass render_pass_; - GpuBuffer particles_buffer_; -}; - -class PassthroughEffect : public PostProcessEffect { - public: - PassthroughEffect(const GpuContext& ctx); - void update_bind_group(WGPUTextureView input_view) override; - - private: -}; - -class MovingEllipseEffect : public Effect { - public: - MovingEllipseEffect(const GpuContext& ctx); - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - - private: - RenderPass pass_; -}; - -class ParticleSprayEffect : public Effect { - public: - ParticleSprayEffect(const GpuContext& ctx); - void compute(WGPUCommandEncoder encoder, - const CommonPostProcessUniforms& uniforms) override; - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - private: - ComputePass compute_pass_; - RenderPass render_pass_; - GpuBuffer particles_buffer_; -}; - -// Parameters for GaussianBlurEffect (set at construction time) -struct GaussianBlurParams { - float strength = 1.0f; // Default - float strength_audio = 0.5f; // how much to pulse with audio - float stretch = 1.f; // y/x axis ratio - float _pad = 0.; -}; -static_assert(sizeof(GaussianBlurParams) == 16, - "GaussianBlurParams must be 16 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, - const CommonPostProcessUniforms& uniforms) override; - void update_bind_group(WGPUTextureView input_view) override; - - private: - GaussianBlurParams params_; - UniformBuffer params_buffer_; -}; - -class SolarizeEffect : public PostProcessEffect { - public: - SolarizeEffect(const GpuContext& ctx); - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - void update_bind_group(WGPUTextureView input_view) override; - - private: -}; - -// Parameters for VignetteEffect -struct VignetteParams { - float radius = 0.5f; // Radius of the clear center - float softness = 0.5f; // Softness of the vignette edge -}; - -class VignetteEffect : public PostProcessEffect { - public: - VignetteEffect(const GpuContext& ctx); - VignetteEffect(const GpuContext& ctx, const VignetteParams& params); - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - void update_bind_group(WGPUTextureView input_view) override; - - private: - VignetteParams params_; - UniformBuffer params_buffer_; -}; +// Individual Effect Headers +#include "effects/chroma_aberration_effect.h" +#include "effects/circle_mask_effect.h" +#include "effects/cnn_effect.h" +#include "effects/cnn_v2_effect.h" +#include "effects/distort_effect.h" +#include "effects/fade_effect.h" +#include "effects/flash_cube_effect.h" +#include "effects/flash_effect.h" +#include "effects/gaussian_blur_effect.h" +#include "effects/heptagon_effect.h" +#include "effects/hybrid_3d_effect.h" +#include "effects/moving_ellipse_effect.h" +#include "effects/particle_spray_effect.h" +#include "effects/particles_effect.h" +#include "effects/passthrough_effect.h" +#include "effects/rotating_cube_effect.h" +#include "effects/scene1_effect.h" +#include "effects/sdf_test_effect.h" +#include "effects/solarize_effect.h" +#include "effects/theme_modulation_effect.h" +#include "effects/vignette_effect.h" -// Parameters for ChromaAberrationEffect (set at construction time) -struct ChromaAberrationParams { - float offset_scale = 0.02f; // Default: 2% screen offset - float angle = 0.0f; // Default: horizontal (0 radians) -}; +#include -class ChromaAberrationEffect : public PostProcessEffect { - public: - // Backward compatibility constructor (uses default params) - ChromaAberrationEffect(const GpuContext& ctx); - // New parameterized constructor - ChromaAberrationEffect(const GpuContext& ctx, - const ChromaAberrationParams& params); - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - void update_bind_group(WGPUTextureView input_view) override; - private: - ChromaAberrationParams params_; - UniformBuffer params_buffer_; -}; -// Parameters for DistortEffect -struct DistortParams { - float strength = 0.01f; // Default distortion strength - float speed = 1.0f; // Default distortion speed -}; -static_assert(sizeof(DistortParams) == 8, - "DistortParams must be 8 bytes for WGSL alignment"); +// Common particle definition is now in effects/particle_defs.h -class DistortEffect : public PostProcessEffect { - public: - DistortEffect(const GpuContext& ctx); - DistortEffect(const GpuContext& ctx, const DistortParams& params); - void render(WGPURenderPassEncoder pass, - const CommonPostProcessUniforms& uniforms) override; - void update_bind_group(WGPUTextureView input_view) override; - private: - DistortParams params_; - UniformBuffer params_buffer_; -}; -#include "effects/cnn_effect.h" -#include "effects/cnn_v2_effect.h" +// Auto-generated functions from sequence compiler -// Auto-generated functions void LoadTimeline(MainSequence& main_seq, const GpuContext& ctx); -float GetDemoDuration(); // Returns demo end time in seconds, or -1 if not - // specified \ No newline at end of file + +float GetDemoDuration(); diff --git a/src/gpu/sdf_effect.h b/src/gpu/sdf_effect.h new file mode 100644 index 0000000..4f23604 --- /dev/null +++ b/src/gpu/sdf_effect.h @@ -0,0 +1,53 @@ +// This file is part of the 64k demo project. +// It defines SDFEffect base class for raymarching effects. + +#pragma once + +#include "3d/camera.h" +#include "gpu/camera_params.h" +#include "gpu/effect.h" +#include "gpu/uniform_helper.h" + +// Base class for SDF raymarching effects +// Provides CameraParams uniform buffer and helper methods +// +// Binding convention: +// @group(0) @binding(2): CommonPostProcessUniforms (from Effect base) +// @group(0) @binding(3): CameraParams +// @group(0) @binding(4+): Per-effect custom parameters +class SDFEffect : public Effect { + public: + SDFEffect(const GpuContext& ctx) : Effect(ctx) { + camera_params_.init(ctx.device); + } + + virtual ~SDFEffect() = default; + + // Populate camera parameters from Camera object + void update_camera(const Camera& camera, float aspect_ratio) { + CameraParams params; + params.inv_view = camera.get_view_matrix().inverse(); + params.fov = camera.fov_y_rad; + params.near_plane = camera.near_plane; + params.far_plane = camera.far_plane; + params.aspect_ratio = aspect_ratio; + camera_params_.update(ctx_.queue, params); + } + + // Populate camera parameters with custom values + void update_camera(const vec3& position, const vec3& target, const vec3& up, + float fov, float near_plane, float far_plane, + float aspect_ratio) { + mat4 view = mat4::look_at(position, target, up); + CameraParams params; + params.inv_view = view.inverse(); + params.fov = fov; + params.near_plane = near_plane; + params.far_plane = far_plane; + params.aspect_ratio = aspect_ratio; + camera_params_.update(ctx_.queue, params); + } + + protected: + UniformBuffer camera_params_; +}; diff --git a/src/gpu/shaders.cc b/src/gpu/shaders.cc index 1bf5604..30bbb0c 100644 --- a/src/gpu/shaders.cc +++ b/src/gpu/shaders.cc @@ -31,6 +31,7 @@ void InitShaderComposer() { }; register_if_exists("common_uniforms", AssetId::ASSET_SHADER_COMMON_UNIFORMS); + register_if_exists("camera_common", AssetId::ASSET_SHADER_CAMERA_COMMON); register_if_exists("math/sdf_shapes", AssetId::ASSET_SHADER_MATH_SDF_SHAPES); register_if_exists("math/sdf_utils", AssetId::ASSET_SHADER_MATH_SDF_UTILS); register_if_exists("math/common_utils", @@ -114,6 +115,10 @@ const char* scene1_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_SCENE1); +const char* sdf_test_shader_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_SDF_TEST); + const char* distort_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_DISTORT); diff --git a/src/gpu/shaders.h b/src/gpu/shaders.h index 03fa48c..03263db 100644 --- a/src/gpu/shaders.h +++ b/src/gpu/shaders.h @@ -16,6 +16,7 @@ extern const char* particle_spray_compute_wgsl; extern const char* gaussian_blur_shader_wgsl; extern const char* solarize_shader_wgsl; extern const char* scene1_shader_wgsl; +extern const char* sdf_test_shader_wgsl; extern const char* distort_shader_wgsl; extern const char* chroma_aberration_shader_wgsl; extern const char* vignette_shader_wgsl; diff --git a/src/tests/gpu/test_demo_effects.cc b/src/tests/gpu/test_demo_effects.cc index 4234901..ec78c10 100644 --- a/src/tests/gpu/test_demo_effects.cc +++ b/src/tests/gpu/test_demo_effects.cc @@ -136,6 +136,7 @@ static void test_scene_effects() { {"RotatingCubeEffect", std::make_shared(fixture.ctx())}, {"Scene1Effect", std::make_shared(fixture.ctx())}, + {"SDFTestEffect", std::make_shared(fixture.ctx())}, }; int passed = 0; -- cgit v1.2.3