summaryrefslogtreecommitdiff
path: root/src/gpu/effects
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/effects')
-rw-r--r--src/gpu/effects/chroma_aberration_effect.cc48
-rw-r--r--src/gpu/effects/flash_effect.cc55
-rw-r--r--src/gpu/effects/flash_effect.h26
-rw-r--r--src/gpu/effects/gaussian_blur_effect.cc48
-rw-r--r--src/gpu/effects/shaders.cc4
5 files changed, 132 insertions, 49 deletions
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 d0226e5..fdd1e1c 100644
--- a/src/gpu/effects/flash_effect.cc
+++ b/src/gpu/effects/flash_effect.cc
@@ -1,11 +1,19 @@
// This file is part of the 64k demo project.
-// It implements the FlashEffect - brief white flash on beat hits.
+// It implements the FlashEffect - brief flash on beat hits.
+// Now supports parameterized color with per-frame animation.
#include "gpu/effects/flash_effect.h"
#include "gpu/effects/post_process_helper.h"
#include <cmath>
-FlashEffect::FlashEffect(const GpuContext& ctx) : PostProcessEffect(ctx) {
+// Backward compatibility constructor (delegates to parameterized constructor)
+FlashEffect::FlashEffect(const GpuContext& ctx)
+ : FlashEffect(ctx, FlashEffectParams{}) {
+}
+
+// Parameterized constructor
+FlashEffect::FlashEffect(const GpuContext& ctx, const FlashEffectParams& params)
+ : PostProcessEffect(ctx), params_(params) {
const char* shader_code = R"(
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@@ -15,8 +23,8 @@ FlashEffect::FlashEffect(const GpuContext& ctx) : PostProcessEffect(ctx) {
struct Uniforms {
flash_intensity: f32,
intensity: f32,
- _pad1: f32,
- _pad2: f32,
+ flash_color: vec3<f32>, // Parameterized color
+ _pad: f32,
};
@group(0) @binding(0) var inputSampler: sampler;
@@ -39,43 +47,48 @@ FlashEffect::FlashEffect(const GpuContext& ctx) : PostProcessEffect(ctx) {
@fragment
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
let color = textureSample(inputTexture, inputSampler, input.uv);
- // Add white flash: blend towards white based on flash intensity
- let white = vec3<f32>(1.0, 1.0, 1.0);
- let green = vec3<f32>(0.0, 1.0, 0.0);
- var flashed = mix(color.rgb, green, uniforms.intensity);
- if (input.uv.y > .5) { flashed = mix(color.rgb, white, uniforms.flash_intensity); }
+ // Use parameterized flash color instead of hardcoded white
+ var flashed = mix(color.rgb, uniforms.flash_color, uniforms.flash_intensity);
return vec4<f32>(flashed, color.a);
}
)";
pipeline_ =
create_post_process_pipeline(ctx_.device, ctx_.format, shader_code);
- uniforms_ = gpu_create_buffer(
- ctx_.device, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
+ uniforms_.init(ctx_.device);
}
void FlashEffect::update_bind_group(WGPUTextureView input_view) {
pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view,
- uniforms_);
+ uniforms_.get());
}
void FlashEffect::render(WGPURenderPassEncoder pass, float time, float beat,
float intensity, float aspect_ratio) {
- (void)time;
- (void)beat;
(void)aspect_ratio;
- // Trigger flash on strong beat hits
- if (intensity > 0.7f && flash_intensity_ < 0.2f) {
+ // Trigger flash based on configured threshold
+ if (intensity > params_.trigger_threshold && flash_intensity_ < 0.2f) {
flash_intensity_ = 0.8f; // Trigger flash
}
- // Exponential decay
- flash_intensity_ *= 0.98f;
+ // Decay based on configured rate
+ flash_intensity_ *= params_.decay_rate;
+
+ // *** PER-FRAME PARAMETER COMPUTATION ***
+ // Animate color based on time and beat
+ const float r = params_.color[0] * (0.5f + 0.5f * sinf(time * 0.5f));
+ const float g = params_.color[1] * (0.5f + 0.5f * cosf(time * 0.7f));
+ const float b = params_.color[2] * (1.0f + 0.3f * beat);
- float uniforms[4] = {flash_intensity_, intensity, 0.0f, 0.0f};
- wgpuQueueWriteBuffer(ctx_.queue, uniforms_.buffer, 0, uniforms,
- sizeof(uniforms));
+ // Update uniforms with computed (animated) values
+ 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
+ ._pad2 = 0.0f};
+ uniforms_.update(ctx_.queue, u);
wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
diff --git a/src/gpu/effects/flash_effect.h b/src/gpu/effects/flash_effect.h
index 6be375d..71815d5 100644
--- a/src/gpu/effects/flash_effect.h
+++ b/src/gpu/effects/flash_effect.h
@@ -5,14 +5,40 @@
#include "gpu/effect.h"
#include "gpu/gpu.h"
+#include "gpu/uniform_helper.h"
+
+// 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
+};
+
+// 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
+};
+static_assert(sizeof(FlashUniforms) == 32,
+ "FlashUniforms must be 32 bytes for WGSL alignment");
class FlashEffect : public PostProcessEffect {
public:
+ // Backward compatibility constructor (uses default params)
FlashEffect(const GpuContext& ctx);
+ // New parameterized constructor
+ FlashEffect(const GpuContext& ctx, const FlashEffectParams& params);
void render(WGPURenderPassEncoder pass, float time, float beat,
float intensity, float aspect_ratio) override;
void update_bind_group(WGPUTextureView input_view) override;
private:
+ FlashEffectParams params_;
+ UniformBuffer<FlashUniforms> uniforms_;
float flash_intensity_ = 0.0f;
};
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/src/gpu/effects/shaders.cc b/src/gpu/effects/shaders.cc
index 0646f92..ce60a74 100644
--- a/src/gpu/effects/shaders.cc
+++ b/src/gpu/effects/shaders.cc
@@ -33,6 +33,9 @@ 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/noise", AssetId::ASSET_SHADER_MATH_NOISE);
register_if_exists("render/shadows", AssetId::ASSET_SHADER_RENDER_SHADOWS);
register_if_exists("render/scene_query_bvh",
AssetId::ASSET_SHADER_RENDER_SCENE_QUERY_BVH);
@@ -47,6 +50,7 @@ void InitShaderComposer() {
register_if_exists("lighting", AssetId::ASSET_SHADER_LIGHTING);
register_if_exists("ray_box", AssetId::ASSET_SHADER_RAY_BOX);
+ register_if_exists("ray_triangle", AssetId::ASSET_SHADER_RAY_TRIANGLE);
}
// Helper to get asset string or empty string