diff options
39 files changed, 777 insertions, 191 deletions
@@ -119,6 +119,7 @@ IMPORTANT: - **CNNv2 Training Pipeline:** Fixed critical checkpointing bugs and streamlined the output of the training scripts for faster iteration. - **Effect Render API Refactor:** Simplified the `Effect::render` API and fixed uniform initialization bugs across 19 effects. - **CNN Shader Testing Tool:** Implemented `tools/cnn_test` for offline GPU-accelerated validation of trained CNN models. + - **Effect Source Hierarchy Cleanup**: Refactored the effects system by splitting `src/gpu/demo_effects.h` into individual header files for each effect, creating 10 new headers, moving class definitions, updating `.cc` files with new includes, fixing missing `#include` statements, creating a common `particle_defs.h`, and cleaning up `demo_effects.h`. Verified by passing all 34 tests. handoff(Gemini): </recent_actions> <task_state> diff --git a/cmake/DemoSourceLists.cmake b/cmake/DemoSourceLists.cmake index a54eb4b..d4cdd21 100644 --- a/cmake/DemoSourceLists.cmake +++ b/cmake/DemoSourceLists.cmake @@ -29,11 +29,11 @@ set(UTIL_SOURCES src/util/asset_manager.cc src/util/file_watcher.cc) # GPU sources (conditional: HEADLESS / STRIP_EXTERNAL / NORMAL) demo_set_conditional_sources(GPU_SOURCES # Headless mode: Functional stubs (timeline/audio work) - "src/gpu/headless_gpu.cc;src/gpu/demo_effects.cc;src/gpu/effect.cc;src/effects/heptagon_effect.cc;src/effects/particles_effect.cc;src/effects/passthrough_effect.cc;src/effects/moving_ellipse_effect.cc;src/effects/particle_spray_effect.cc;src/effects/gaussian_blur_effect.cc;src/effects/solarize_effect.cc;src/effects/scene1_effect.cc;src/effects/chroma_aberration_effect.cc;src/effects/vignette_effect.cc;src/effects/cnn_effect.cc;src/effects/cnn_v2_effect.cc;src/gpu/post_process_helper.cc;src/gpu/shaders.cc;src/effects/hybrid_3d_effect.cc;src/effects/flash_cube_effect.cc;src/effects/theme_modulation_effect.cc;src/effects/fade_effect.cc;src/effects/flash_effect.cc;src/gpu/shader_composer.cc;src/effects/circle_mask_effect.cc;src/effects/rotating_cube_effect.cc;src/gpu/texture_manager.cc;src/gpu/texture_readback.cc" + "src/gpu/headless_gpu.cc;src/gpu/demo_effects.cc;src/gpu/effect.cc;src/effects/heptagon_effect.cc;src/effects/particles_effect.cc;src/effects/passthrough_effect.cc;src/effects/moving_ellipse_effect.cc;src/effects/particle_spray_effect.cc;src/effects/gaussian_blur_effect.cc;src/effects/solarize_effect.cc;src/effects/scene1_effect.cc;src/effects/chroma_aberration_effect.cc;src/effects/vignette_effect.cc;src/effects/cnn_effect.cc;src/effects/cnn_v2_effect.cc;src/gpu/post_process_helper.cc;src/gpu/shaders.cc;src/effects/hybrid_3d_effect.cc;src/effects/flash_cube_effect.cc;src/effects/theme_modulation_effect.cc;src/effects/fade_effect.cc;src/effects/flash_effect.cc;src/gpu/shader_composer.cc;src/effects/circle_mask_effect.cc;src/effects/rotating_cube_effect.cc;src/gpu/texture_manager.cc;src/gpu/texture_readback.cc;src/effects/sdf_test_effect.cc" # Strip mode: Minimal GPU stubs only "src/gpu/stub_gpu.cc" # Normal mode: Full GPU implementation - "src/gpu/gpu.cc;src/gpu/effect.cc;src/effects/heptagon_effect.cc;src/effects/particles_effect.cc;src/effects/passthrough_effect.cc;src/effects/moving_ellipse_effect.cc;src/effects/particle_spray_effect.cc;src/effects/gaussian_blur_effect.cc;src/effects/solarize_effect.cc;src/effects/scene1_effect.cc;src/effects/chroma_aberration_effect.cc;src/effects/vignette_effect.cc;src/effects/cnn_effect.cc;src/effects/cnn_v2_effect.cc;src/gpu/post_process_helper.cc;src/gpu/shaders.cc;src/effects/hybrid_3d_effect.cc;src/effects/flash_cube_effect.cc;src/effects/theme_modulation_effect.cc;src/effects/fade_effect.cc;src/effects/flash_effect.cc;src/gpu/shader_composer.cc;src/effects/circle_mask_effect.cc;src/effects/rotating_cube_effect.cc;src/gpu/texture_manager.cc;src/gpu/texture_readback.cc" + "src/gpu/gpu.cc;src/gpu/effect.cc;src/effects/heptagon_effect.cc;src/effects/particles_effect.cc;src/effects/passthrough_effect.cc;src/effects/moving_ellipse_effect.cc;src/effects/particle_spray_effect.cc;src/effects/gaussian_blur_effect.cc;src/effects/solarize_effect.cc;src/effects/scene1_effect.cc;src/effects/chroma_aberration_effect.cc;src/effects/vignette_effect.cc;src/effects/cnn_effect.cc;src/effects/cnn_v2_effect.cc;src/gpu/post_process_helper.cc;src/gpu/shaders.cc;src/effects/hybrid_3d_effect.cc;src/effects/flash_cube_effect.cc;src/effects/theme_modulation_effect.cc;src/effects/fade_effect.cc;src/effects/flash_effect.cc;src/gpu/shader_composer.cc;src/effects/circle_mask_effect.cc;src/effects/rotating_cube_effect.cc;src/gpu/texture_manager.cc;src/gpu/texture_readback.cc;src/effects/sdf_test_effect.cc" ) # 3D sources (conditional: HEADLESS / STRIP_EXTERNAL / NORMAL) diff --git a/common/shaders/camera_common.wgsl b/common/shaders/camera_common.wgsl new file mode 100644 index 0000000..bd29775 --- /dev/null +++ b/common/shaders/camera_common.wgsl @@ -0,0 +1,52 @@ +// Camera parameters and helpers for SDF raymarching effects + +struct CameraParams { + inv_view: mat4x4<f32>, + fov: f32, + near_plane: f32, + far_plane: f32, + aspect_ratio: f32, +} + +struct Ray { + origin: vec3<f32>, + direction: vec3<f32>, +} + +// Generate camera ray for given UV coordinates (-1 to 1) +fn getCameraRay(cam: CameraParams, uv: vec2<f32>) -> Ray { + let cam_pos = vec3<f32>(cam.inv_view[3].x, cam.inv_view[3].y, cam.inv_view[3].z); + + // Compute ray direction from FOV and aspect ratio + let tan_fov = tan(cam.fov * 0.5); + let ndc = vec3<f32>(uv.x * cam.aspect_ratio * tan_fov, uv.y * tan_fov, -1.0); + + // Transform direction by inverse view matrix (rotation only) + let dir = normalize( + cam.inv_view[0].xyz * ndc.x + + cam.inv_view[1].xyz * ndc.y + + cam.inv_view[2].xyz * ndc.z + ); + + return Ray(cam_pos, dir); +} + +// Extract camera position from inverse view matrix +fn getCameraPosition(cam: CameraParams) -> vec3<f32> { + return vec3<f32>(cam.inv_view[3].x, cam.inv_view[3].y, cam.inv_view[3].z); +} + +// Extract camera forward vector (view direction) +fn getCameraForward(cam: CameraParams) -> vec3<f32> { + return -normalize(vec3<f32>(cam.inv_view[2].x, cam.inv_view[2].y, cam.inv_view[2].z)); +} + +// Extract camera up vector +fn getCameraUp(cam: CameraParams) -> vec3<f32> { + return normalize(vec3<f32>(cam.inv_view[1].x, cam.inv_view[1].y, cam.inv_view[1].z)); +} + +// Extract camera right vector +fn getCameraRight(cam: CameraParams) -> vec3<f32> { + return normalize(vec3<f32>(cam.inv_view[0].x, cam.inv_view[0].y, cam.inv_view[0].z)); +} diff --git a/doc/ARCHITECTURE.md b/doc/ARCHITECTURE.md index 4c36ec5..ebb2a59 100644 --- a/doc/ARCHITECTURE.md +++ b/doc/ARCHITECTURE.md @@ -4,6 +4,28 @@ Detailed system architecture for the 64k demo project. --- +## SDF Camera System + +**Purpose**: Unified camera infrastructure for SDF raymarching effects. + +**CameraParams** (80 bytes, `src/gpu/camera_params.h`): +- `inv_view`: mat4 (inverse view matrix for screen→world transform) +- `fov`, `near_plane`, `far_plane`, `aspect_ratio`: f32 (camera parameters) + +**SDFEffect Base Class** (`src/gpu/sdf_effect.h`): +- Manages `UniformBuffer<CameraParams>` +- Provides `update_camera()` helper methods (from Camera object or manual values) +- Standard binding: 0=CommonUniforms, 1=CameraParams + +**WGSL Helpers** (`common/shaders/camera_common.wgsl`): +- `getCameraRay(cam, uv)`: Generate ray from screen UV coordinates +- `getCameraPosition/Forward/Up/Right()`: Extract camera vectors from inv_view +- Integrates with existing `render/raymarching.wgsl` (rayMarch, normal, shadow) + +**Usage**: Effects inherit from SDFEffect, update camera each frame, shader accesses camera uniforms for raymarching. + +--- + ## Hybrid 3D Renderer **Core Idea**: Uses standard rasterization to draw proxy hulls (boxes), then raymarches inside the fragment shader to find the exact SDF surface. @@ -18,6 +40,10 @@ Detailed system architecture for the 64k demo project. **Effect**: Abstract base for visual elements. Supports `compute` and `render` phases. +**PostProcessEffect**: Subclass for full-screen post-processing effects. + +**SDFEffect**: Subclass for SDF raymarching effects with camera management (see SDF Camera System below). + **Sequence**: Timeline of effects with start/end times defined in beats. **MainSequence**: Top-level coordinator and framebuffer manager. diff --git a/doc/CONTRIBUTING.md b/doc/CONTRIBUTING.md index d7ef88a..7fbfd64 100644 --- a/doc/CONTRIBUTING.md +++ b/doc/CONTRIBUTING.md @@ -65,10 +65,14 @@ See `doc/CODING_STYLE.md` for detailed examples. ## Development Protocols ### Adding Visual Effect -1. Create effect class files (use `tools/shadertoy/convert_shadertoy.py` or templates) + +**For SDF/raymarching effects:** Use `SDFEffect` base class (see `doc/SDF_EFFECT_GUIDE.md`). + +**For standard effects:** +1. Create effect class files (each effect should have its own `.h` and `.cc` file, e.g., `src/effects/my_effect.h` and `src/effects/my_effect.cc`). Use `tools/shadertoy/convert_shadertoy.py` or templates. 2. Add shader to `workspaces/main/assets.txt` 3. Add effect `.cc` file to `CMakeLists.txt` GPU_SOURCES (both sections) -4. Include header in `src/gpu/demo_effects.h` +4. Include header in `src/gpu/demo_effects.h` (which now serves as a central include for all individual effect headers) 5. Add to workspace `timeline.seq` (e.g., `workspaces/main/timeline.seq`) 6. **Update `src/tests/gpu/test_demo_effects.cc`**: - Add to `post_process_effects` list (lines 80-93) or `scene_effects` list (lines 125-137) diff --git a/doc/EFFECT_WORKFLOW.md b/doc/EFFECT_WORKFLOW.md index 22b8dc9..57cf904 100644 --- a/doc/EFFECT_WORKFLOW.md +++ b/doc/EFFECT_WORKFLOW.md @@ -10,6 +10,8 @@ Automated checklist for adding new visual effects to the demo. **For ShaderToy conversions:** Use `tools/shadertoy/convert_shadertoy.py` then follow steps 3-8 below. +**For SDF/raymarching effects:** See `doc/SDF_EFFECT_GUIDE.md` for streamlined workflow using SDFEffect base class. + **For custom effects:** Follow all steps 1-8. --- @@ -18,6 +20,8 @@ Automated checklist for adding new visual effects to the demo. ### 1. Create Effect Files +**Description:** Each visual effect must have its own dedicated header (`.h`) and implementation (`.cc`) file pair. + **Location:** - Header: `src/effects/<effect_name>_effect.h` - Implementation: `src/effects/<effect_name>_effect.cc` @@ -84,7 +88,7 @@ SHADER_TUNNEL, NONE, shaders/tunnel.wgsl, "Tunnel effect shader" # In normal section (line ~183): src/effects/solarize_effect.cc - src/effects/tunnel_effect.cc # <-- Add here + src/effects/tunnel.cc # <-- Add here src/effects/chroma_aberration_effect.cc ``` @@ -92,7 +96,7 @@ SHADER_TUNNEL, NONE, shaders/tunnel.wgsl, "Tunnel effect shader" **File:** `src/gpu/demo_effects.h` -**Action:** Add include directive: +**Action:** `src/gpu/demo_effects.h` now acts as a central include file. Add a single include directive for your new effect's header: ```cpp #include "effects/<effect_name>_effect.h" ``` diff --git a/doc/SDF_EFFECT_GUIDE.md b/doc/SDF_EFFECT_GUIDE.md new file mode 100644 index 0000000..fba80e7 --- /dev/null +++ b/doc/SDF_EFFECT_GUIDE.md @@ -0,0 +1,164 @@ +# SDF Effect Guide + +Streamlined workflow for SDF raymarching effects using the `SDFEffect` base class. + +--- + +## Quick Start + +```cpp +// src/effects/my_sdf_effect.h +class MySDFEffect : public SDFEffect { + MySDFEffect(const GpuContext& ctx); + void render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) override; + RenderPass pass_; +}; +``` + +```cpp +// src/effects/my_sdf_effect.cc +#include "effects/my_sdf_effect.h" +#include "gpu/gpu.h" +#include "gpu/shaders.h" + +MySDFEffect::MySDFEffect(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, + my_sdf_shader_wgsl, bindings, 2); + pass_.vertex_count = 3; +} + +void MySDFEffect::render(WGPURenderPassEncoder pass, + const CommonPostProcessUniforms& uniforms) { + uniforms_.update(ctx_.queue, uniforms); + + // Orbiting camera + vec3 cam_pos(std::cos(uniforms.time * 0.5f) * 5.0f, 2.0f, + std::sin(uniforms.time * 0.5f) * 5.0f); + update_camera(cam_pos, vec3(0, 0, 0), vec3(0, 1, 0), 0.785398f, 0.1f, 100.0f, + uniforms.aspect_ratio); + + wgpuRenderPassEncoderSetPipeline(pass, pass_.pipeline); + wgpuRenderPassEncoderSetBindGroup(pass, 0, pass_.bind_group, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, pass_.vertex_count, 1, 0, 0); +} +``` + +```wgsl +// workspaces/main/shaders/my_sdf.wgsl +#include "common_uniforms" +#include "camera_common" +#include "math/sdf_shapes" +#include "render/raymarching" + +@group(0) @binding(0) var<uniform> uniforms: CommonUniforms; +@group(0) @binding(1) var<uniform> camera: CameraParams; + +fn df(p: vec3<f32>) -> f32 { + return sdSphere(p, 1.0); +} + +@vertex +fn vs_main(@builtin(vertex_index) vid: u32) -> @builtin(position) vec4<f32> { + let x = f32((vid & 1u) << 2u) - 1.0; + let y = f32((vid & 2u) << 1u) - 1.0; + return vec4<f32>(x, y, 0.0, 1.0); +} + +@fragment +fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> { + let uv = (pos.xy / uniforms.resolution - 0.5) * 2.0; + let ray = getCameraRay(camera, uv); + let t = rayMarch(ray.origin, ray.direction, 0.0); + + var col = vec3<f32>(0.1); + if (t < MAX_RAY_LENGTH) { + let hit_pos = ray.origin + ray.direction * t; + let n = normal(hit_pos); + col = vec3<f32>(n * 0.5 + 0.5); + } + return vec4<f32>(col, 1.0); +} +``` + +--- + +## Available Uniforms + +### CommonUniforms (binding 0) +- `resolution`: vec2 (screen size) +- `time`: float (physical seconds) +- `beat_time`: float (musical beats) +- `beat_phase`: float (0-1 within beat) +- `audio_intensity`: float (peak) +- `aspect_ratio`: float + +### CameraParams (binding 1) +- `inv_view`: mat4x4 (inverse view matrix) +- `fov`: float (vertical FOV in radians) +- `near_plane`, `far_plane`: float +- `aspect_ratio`: float + +--- + +## WGSL Helpers + +From `camera_common.wgsl`: + +```wgsl +fn getCameraRay(cam: CameraParams, uv: vec2<f32>) -> Ray; +fn getCameraPosition(cam: CameraParams) -> vec3<f32>; +fn getCameraForward(cam: CameraParams) -> vec3<f32>; +fn getCameraUp(cam: CameraParams) -> vec3<f32>; +fn getCameraRight(cam: CameraParams) -> vec3<f32>; +``` + +From `render/raymarching.wgsl`: + +```wgsl +fn rayMarch(ro: vec3<f32>, rd: vec3<f32>, initt: f32) -> f32; +fn normal(pos: vec3<f32>) -> vec3<f32>; +fn shadow(lp: vec3<f32>, ld: vec3<f32>, mint: f32, maxt: f32) -> f32; +``` + +From `math/sdf_shapes.wgsl`: + +```wgsl +fn sdSphere(p: vec3<f32>, r: float) -> f32; +fn sdBox(p: vec3<f32>, b: vec3<f32>) -> f32; +fn sdTorus(p: vec3<f32>, t: vec2<f32>) -> f32; +fn sdPlane(p: vec3<f32>, n: vec3<f32>, h: f32) -> f32; +``` + +--- + +## Camera Control + +```cpp +// Method 1: Manual values +update_camera(position, target, up, fov, near, far, aspect); + +// Method 2: Camera object +Camera cam; +cam.position = vec3(0, 5, 10); +cam.target = vec3(0, 0, 0); +update_camera(cam, uniforms.aspect_ratio); +``` + +--- + +## Registration Checklist + +1. Add shader to `workspaces/main/assets.txt` +2. Add extern declaration to `src/gpu/shaders.h` +3. Add definition to `src/gpu/shaders.cc` +4. Add `.cc` to `cmake/DemoSourceLists.cmake` (both headless & normal) +5. Include header in `src/gpu/demo_effects.h` +6. Add to `src/tests/gpu/test_demo_effects.cc` + +--- + +## Example: workspaces/main/shaders/sdf_test.wgsl diff --git a/doc/UNIFORM_BUFFER_GUIDELINES.md b/doc/UNIFORM_BUFFER_GUIDELINES.md index 93999d8..c6cf9c8 100644 --- a/doc/UNIFORM_BUFFER_GUIDELINES.md +++ b/doc/UNIFORM_BUFFER_GUIDELINES.md @@ -16,11 +16,17 @@ Structs are padded to the alignment of their largest member. Any trailing space ## Standard Uniform Buffer Pattern -To maintain consistency and facilitate efficient rendering, a standard pattern for uniform buffer usage is established: +To maintain consistency and facilitate efficient rendering, standard patterns for uniform buffer usage are established: +### Post-Process Effects - **Binding 0 & 1:** Reserved for Sampler and Texture access (handled by `pp_update_bind_group`). -- **Binding 2:** **Common Uniforms** (`CommonPostProcessUniforms` or similar). This buffer should contain frequently used data like resolution, aspect ratio, physical time, beat time, beat phase, and audio intensity. -- **Binding 3:** **Effect-Specific Parameters**. This buffer holds parameters unique to a particular effect (e.g., `strength`, `speed`, `fade_amount`). +- **Binding 2:** **Common Uniforms** (`CommonPostProcessUniforms`). Contains resolution, aspect ratio, physical time, beat time, beat phase, audio intensity. +- **Binding 3:** **Effect-Specific Parameters**. Unique per-effect data (e.g., `strength`, `speed`, `fade_amount`). + +### SDF/Raymarching Effects +- **Binding 0:** **Common Uniforms** (`CommonPostProcessUniforms`). Same as above. +- **Binding 1:** **Camera Parameters** (`CameraParams`). Camera transform and projection data for raymarching. +- **Binding 2+:** **Effect-Specific Parameters** (optional). This pattern ensures that common data is shared efficiently across effects, while effect-specific data remains isolated. @@ -98,10 +104,36 @@ struct GaussianBlurParams { float strength = 2.0f; float _pad = 0.0f; }; -static_assert(sizeof(GaussianBlurParams) == 8, +static_assert(sizeof(GaussianBlurParams) == 8, "GaussianBlurParams must be 8 bytes for WGSL alignment"); ``` +**Example (C++ CameraParams):** + +```cpp +struct CameraParams { + mat4 inv_view; // 64 bytes - inverse view matrix (screen→world) + float fov; // 4 bytes - vertical field of view (radians) + float near_plane; // 4 bytes - near clipping plane + float far_plane; // 4 bytes - far clipping plane + float aspect_ratio; // 4 bytes - width/height ratio +}; +static_assert(sizeof(CameraParams) == 80, + "CameraParams must be 80 bytes for WGSL alignment"); +``` + +**Corresponding WGSL:** + +```wgsl +struct CameraParams { + inv_view: mat4x4<f32>, // 64 bytes + fov: f32, // 4 bytes + near_plane: f32, // 4 bytes + far_plane: f32, // 4 bytes + aspect_ratio: f32, // 4 bytes +} +``` + ## Handling Common Pitfalls - **`vec3<f32>` Padding:** Avoid using `vec3<f32>` for padding in WGSL, as it has a 16-byte alignment. If padding is needed, use `vec2<f32>` for 8 bytes or individual `f32`s for 4-byte alignment. 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<ChromaAberrationParams> 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<DistortParams> 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<GaussianBlurParams> 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 <vector> // --- 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 <vector> // --- 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<VignetteParams> 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 <memory> - -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<GaussianBlurParams> 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<VignetteParams> 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 <memory> -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<ChromaAberrationParams> 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<DistortParams> 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<CameraParams> 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<RotatingCubeEffect>(fixture.ctx())}, {"Scene1Effect", std::make_shared<Scene1Effect>(fixture.ctx())}, + {"SDFTestEffect", std::make_shared<SDFTestEffect>(fixture.ctx())}, }; int passed = 0; diff --git a/workspaces/main/assets.txt b/workspaces/main/assets.txt index 0b64ba9..972cc8b 100644 --- a/workspaces/main/assets.txt +++ b/workspaces/main/assets.txt @@ -25,6 +25,7 @@ NOISE_TEX, PROC(gen_noise, 1234, 16), _, "Procedural noise texture for bump mapp # --- WGSL Shaders & Snippets --- SHADER_RENDERER_3D, NONE, shaders/renderer_3d.wgsl, "Hybrid 3D Renderer Shader" SHADER_COMMON_UNIFORMS, NONE, ../../common/shaders/common_uniforms.wgsl, "Common Uniforms Snippet" +SHADER_CAMERA_COMMON, NONE, ../../common/shaders/camera_common.wgsl, "Camera parameters and raymarching helpers" SHADER_SDF_SHAPES, NONE, ../../common/shaders/math/sdf_shapes.wgsl, "SDF Shapes (2D/3D primitives)" SHADER_LIGHTING, NONE, ../../common/shaders/lighting.wgsl, "Lighting Snippet" SHADER_RAY_BOX, NONE, ../../common/shaders/ray_box.wgsl, "Ray-Box Intersection Snippet" @@ -50,6 +51,7 @@ SHADER_SOLARIZE, NONE, shaders/solarize.wgsl, "Solarize Shader" SHADER_DISTORT, NONE, shaders/distort.wgsl, "Distort Shader" SHADER_CHROMA_ABERRATION, NONE, shaders/chroma_aberration.wgsl, "Chroma Aberration Shader" SHADER_VISUAL_DEBUG, NONE, shaders/visual_debug.wgsl, "Visual Debug Shader" +SHADER_SDF_TEST, NONE, shaders/sdf_test.wgsl, "SDF test effect demonstrating SDFEffect base class" SHADER_SKYBOX, NONE, ../../common/shaders/skybox.wgsl, "Skybox background shader" SHADER_MATH_SDF_SHAPES, NONE, ../../common/shaders/math/sdf_shapes.wgsl, "SDF Shapes Snippet" SHADER_MATH_SDF_UTILS, NONE, ../../common/shaders/math/sdf_utils.wgsl, "SDF Utils Snippet" diff --git a/workspaces/main/shaders/sdf_test.wgsl b/workspaces/main/shaders/sdf_test.wgsl new file mode 100644 index 0000000..3c97613 --- /dev/null +++ b/workspaces/main/shaders/sdf_test.wgsl @@ -0,0 +1,68 @@ +// SDF Test Effect - demonstrates SDFEffect base class usage +// Simple scene with a sphere and box + +#include "common_uniforms" +#include "camera_common" +#include "math/sdf_shapes" +#include "render/raymarching" + +@group(0) @binding(0) var<uniform> uniforms: CommonUniforms; +@group(0) @binding(1) var<uniform> camera: CameraParams; + +// Scene distance function +fn df(p: vec3<f32>) -> f32 { + // Animated sphere + let sphere_pos = vec3<f32>(sin(uniforms.beat_time * 0.5) * 2.0, 0.0, 0.0); + let d_sphere = sdSphere(p - sphere_pos, 1.0); + + // Static box + let box_pos = vec3<f32>(0.0, -2.0, 0.0); + let d_box = sdBox(p - box_pos, vec3<f32>(3.0, 0.5, 3.0)); + + return min(d_sphere, d_box); +} + +// Simple lighting +fn shade(pos: vec3<f32>, rd: vec3<f32>) -> vec3<f32> { + let n = normal(pos); + let light_dir = normalize(vec3<f32>(1.0, 1.0, -1.0)); + let diff = max(dot(n, light_dir), 0.0); + let amb = 0.2; + + // Color based on position + let col = mix(vec3<f32>(0.2, 0.6, 0.9), vec3<f32>(0.9, 0.3, 0.2), + smoothstep(-2.0, 2.0, pos.x)); + + return col * (diff + amb); +} + +@vertex +fn vs_main(@builtin(vertex_index) vid: u32) -> @builtin(position) vec4<f32> { + // Fullscreen triangle + let x = f32((vid & 1u) << 2u) - 1.0; + let y = f32((vid & 2u) << 1u) - 1.0; + return vec4<f32>(x, y, 0.0, 1.0); +} + +@fragment +fn fs_main(@builtin(position) pos: vec4<f32>) -> @location(0) vec4<f32> { + // UV coordinates (-1 to 1) + let uv = (pos.xy / uniforms.resolution - 0.5) * 2.0; + + // Generate ray + let ray = getCameraRay(camera, uv); + + // Raymarch + let t = rayMarch(ray.origin, ray.direction, 0.0); + + // Background color + var col = vec3<f32>(0.1, 0.1, 0.15); + + // Shade hit point + if (t < MAX_RAY_LENGTH) { + let hit_pos = ray.origin + ray.direction * t; + col = shade(hit_pos, ray.direction); + } + + return vec4<f32>(col, 1.0); +} |
