diff options
Diffstat (limited to 'src/gpu')
| -rw-r--r-- | src/gpu/demo_effects.h | 31 | ||||
| -rw-r--r-- | src/gpu/effects/chroma_aberration_effect.cc | 48 | ||||
| -rw-r--r-- | src/gpu/effects/flash_effect.cc | 4 | ||||
| -rw-r--r-- | src/gpu/effects/flash_effect.h | 19 | ||||
| -rw-r--r-- | src/gpu/effects/shaders.cc | 3 | ||||
| -rw-r--r-- | src/gpu/uniform_helper.h | 7 |
6 files changed, 81 insertions, 31 deletions
diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index d9487fa..36be118 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -6,11 +6,12 @@ #include "3d/renderer.h" #include "3d/scene.h" #include "effect.h" -#include "gpu/effects/flash_effect.h" // FlashEffect with params support +#include "gpu/effects/flash_effect.h" // FlashEffect with params support #include "gpu/effects/post_process_helper.h" #include "gpu/effects/shaders.h" #include "gpu/gpu.h" #include "gpu/texture_manager.h" +#include "gpu/uniform_helper.h" #include <memory> static const int NUM_PARTICLES = 10000; @@ -100,12 +101,40 @@ class DistortEffect : public PostProcessEffect { void update_bind_group(WGPUTextureView input_view) override; }; +// 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) +}; + +// Uniform data sent to GPU shader +struct ChromaUniforms { + 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 offset_scale; // offset 24 + float angle; // offset 28 +}; +static_assert(sizeof(ChromaUniforms) == 32, + "ChromaUniforms must be 32 bytes for WGSL alignment"); + 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, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; + + private: + ChromaAberrationParams params_; + UniformBuffer<ChromaUniforms> uniforms_; }; class Hybrid3DEffect : public Effect { diff --git a/src/gpu/effects/chroma_aberration_effect.cc b/src/gpu/effects/chroma_aberration_effect.cc index dc28ee5..3e953e3 100644 --- a/src/gpu/effects/chroma_aberration_effect.cc +++ b/src/gpu/effects/chroma_aberration_effect.cc @@ -1,26 +1,46 @@ // This file is part of the 64k demo project. -// It implements the ChromaAberrationEffect. +// It implements the ChromaAberrationEffect with parameterization. #include "gpu/demo_effects.h" +#include "gpu/effects/post_process_helper.h" #include "gpu/gpu.h" // --- ChromaAberrationEffect --- + +// Backward compatibility constructor (delegates to parameterized constructor) ChromaAberrationEffect::ChromaAberrationEffect(const GpuContext& ctx) - : PostProcessEffect(ctx) { - uniforms_ = - gpu_create_buffer(ctx_.device, sizeof(float) * 6, - WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); + : ChromaAberrationEffect(ctx, ChromaAberrationParams{}) { +} + +// Parameterized constructor +ChromaAberrationEffect::ChromaAberrationEffect( + const GpuContext& ctx, const ChromaAberrationParams& params) + : PostProcessEffect(ctx), params_(params) { pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, chroma_aberration_shader_wgsl); + uniforms_.init(ctx_.device); } -void ChromaAberrationEffect::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 ChromaAberrationEffect::render(WGPURenderPassEncoder pass, float time, + float beat, float intensity, + float aspect_ratio) { + // Update uniforms with current state and parameters + const ChromaUniforms u = {.time = time, + .beat = beat, + .intensity = intensity, + .aspect_ratio = aspect_ratio, + .width = (float)width_, + .height = (float)height_, + .offset_scale = params_.offset_scale, + .angle = params_.angle}; + uniforms_.update(ctx_.queue, u); + + wgpuRenderPassEncoderSetPipeline(pass, pipeline_); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); } -void ChromaAberrationEffect::update_bind_group(WGPUTextureView v) { - pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, uniforms_); + +void ChromaAberrationEffect::update_bind_group(WGPUTextureView input_view) { + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, + uniforms_.get()); } diff --git a/src/gpu/effects/flash_effect.cc b/src/gpu/effects/flash_effect.cc index 1bb4d93..fdd1e1c 100644 --- a/src/gpu/effects/flash_effect.cc +++ b/src/gpu/effects/flash_effect.cc @@ -85,8 +85,8 @@ void FlashEffect::render(WGPURenderPassEncoder pass, float time, float beat, const FlashUniforms u = { .flash_intensity = flash_intensity_, .intensity = intensity, - ._pad1 = {0.0f, 0.0f}, // Padding for vec3 alignment - .color = {r, g, b}, // Time-dependent, computed every frame + ._pad1 = {0.0f, 0.0f}, // Padding for vec3 alignment + .color = {r, g, b}, // Time-dependent, computed every frame ._pad2 = 0.0f}; uniforms_.update(ctx_.queue, u); diff --git a/src/gpu/effects/flash_effect.h b/src/gpu/effects/flash_effect.h index 373b48b..71815d5 100644 --- a/src/gpu/effects/flash_effect.h +++ b/src/gpu/effects/flash_effect.h @@ -9,22 +9,23 @@ // Parameters for FlashEffect (set at construction time) struct FlashEffectParams { - float color[3] = {1.0f, 1.0f, 1.0f}; // Default: white - float decay_rate = 0.98f; // Default: fast decay - float trigger_threshold = 0.7f; // Default: trigger on strong beats + float color[3] = {1.0f, 1.0f, 1.0f}; // Default: white + float decay_rate = 0.98f; // Default: fast decay + float trigger_threshold = 0.7f; // Default: trigger on strong beats }; // Uniform data sent to GPU shader // IMPORTANT: Must match WGSL struct layout with proper alignment // vec3<f32> in WGSL has 16-byte alignment, not 12-byte! struct FlashUniforms { - float flash_intensity; // offset 0 - float intensity; // offset 4 - float _pad1[2]; // offset 8-15 (padding for vec3 alignment) - float color[3]; // offset 16-27 (vec3 aligned to 16 bytes) - float _pad2; // offset 28-31 + float flash_intensity; // offset 0 + float intensity; // offset 4 + float _pad1[2]; // offset 8-15 (padding for vec3 alignment) + float color[3]; // offset 16-27 (vec3 aligned to 16 bytes) + float _pad2; // offset 28-31 }; -static_assert(sizeof(FlashUniforms) == 32, "FlashUniforms must be 32 bytes for WGSL alignment"); +static_assert(sizeof(FlashUniforms) == 32, + "FlashUniforms must be 32 bytes for WGSL alignment"); class FlashEffect : public PostProcessEffect { public: diff --git a/src/gpu/effects/shaders.cc b/src/gpu/effects/shaders.cc index 51e6e41..380b5b4 100644 --- a/src/gpu/effects/shaders.cc +++ b/src/gpu/effects/shaders.cc @@ -33,7 +33,8 @@ void InitShaderComposer() { register_if_exists("common_uniforms", AssetId::ASSET_SHADER_COMMON_UNIFORMS); 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", AssetId::ASSET_SHADER_MATH_COMMON_UTILS); + register_if_exists("math/common_utils", + AssetId::ASSET_SHADER_MATH_COMMON_UTILS); register_if_exists("render/shadows", AssetId::ASSET_SHADER_RENDER_SHADOWS); register_if_exists("render/scene_query_bvh", AssetId::ASSET_SHADER_RENDER_SCENE_QUERY_BVH); diff --git a/src/gpu/uniform_helper.h b/src/gpu/uniform_helper.h index afc4a4b..151153f 100644 --- a/src/gpu/uniform_helper.h +++ b/src/gpu/uniform_helper.h @@ -12,15 +12,14 @@ // UniformBuffer<MyUniforms> uniforms_; // uniforms_.init(device); // uniforms_.update(queue, my_data); -template <typename T> -class UniformBuffer { +template <typename T> class UniformBuffer { public: UniformBuffer() = default; // Initialize the uniform buffer with the device void init(WGPUDevice device) { - buffer_ = gpu_create_buffer(device, sizeof(T), - WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); + buffer_ = gpu_create_buffer( + device, sizeof(T), WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); } // Update the uniform buffer with new data |
