diff options
Diffstat (limited to 'src/gpu')
| -rw-r--r-- | src/gpu/demo_effects.h | 67 | ||||
| -rw-r--r-- | src/gpu/effect.cc | 5 | ||||
| -rw-r--r-- | src/gpu/effect.h | 2 | ||||
| -rw-r--r-- | src/gpu/effects/chroma_aberration_effect.cc | 48 | ||||
| -rw-r--r-- | src/gpu/effects/flash_effect.cc | 55 | ||||
| -rw-r--r-- | src/gpu/effects/flash_effect.h | 26 | ||||
| -rw-r--r-- | src/gpu/effects/gaussian_blur_effect.cc | 48 | ||||
| -rw-r--r-- | src/gpu/effects/shaders.cc | 4 | ||||
| -rw-r--r-- | src/gpu/gpu.cc | 4 | ||||
| -rw-r--r-- | src/gpu/gpu.h | 2 | ||||
| -rw-r--r-- | src/gpu/uniform_helper.h | 41 |
11 files changed, 237 insertions, 65 deletions
diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index cddd04b..6c8729d 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -6,10 +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/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; @@ -75,12 +77,38 @@ class ParticleSprayEffect : public Effect { GpuBuffer particles_buffer_; }; +// Parameters for GaussianBlurEffect (set at construction time) +struct GaussianBlurParams { + float strength = 2.0f; // Default: 2.0 pixel blur radius +}; + +// Uniform data sent to GPU shader +struct GaussianBlurUniforms { + float time; // offset 0 + float beat; // offset 4 + float intensity; // offset 8 + float aspect_ratio; // offset 12 + float width; // offset 16 + float height; // offset 20 + float strength; // offset 24 + float _pad; // offset 28 +}; +static_assert(sizeof(GaussianBlurUniforms) == 32, + "GaussianBlurUniforms must be 32 bytes for WGSL alignment"); + class GaussianBlurEffect : public PostProcessEffect { public: + // Backward compatibility constructor (uses default params) GaussianBlurEffect(const GpuContext& ctx); + // New parameterized constructor + GaussianBlurEffect(const GpuContext& ctx, const GaussianBlurParams& params); void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; + + private: + GaussianBlurParams params_; + UniformBuffer<GaussianBlurUniforms> uniforms_; }; class SolarizeEffect : public PostProcessEffect { @@ -99,12 +127,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 { @@ -158,16 +214,7 @@ class FadeEffect : public PostProcessEffect { void update_bind_group(WGPUTextureView input_view) override; }; -class FlashEffect : public PostProcessEffect { - public: - FlashEffect(const GpuContext& ctx); - void render(WGPURenderPassEncoder pass, float time, float beat, - float intensity, float aspect_ratio) override; - void update_bind_group(WGPUTextureView input_view) override; - - private: - float flash_intensity_ = 0.0f; -}; +// FlashEffect now defined in gpu/effects/flash_effect.h (included above) // Auto-generated functions void LoadTimeline(MainSequence& main_seq, const GpuContext& ctx); diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc index a1b45f1..df8ef2d 100644 --- a/src/gpu/effect.cc +++ b/src/gpu/effect.cc @@ -2,6 +2,7 @@ // It implements the Sequence management logic. #include "effect.h" +#include "audio/tracker.h" #include "gpu/demo_effects.h" #include "gpu/gpu.h" #include <algorithm> @@ -383,8 +384,8 @@ void MainSequence::shutdown() { } #if !defined(STRIP_ALL) -void MainSequence::simulate_until(float target_time, float step_rate) { - const float bpm = 128.0f; +void MainSequence::simulate_until(float target_time, float step_rate, + float bpm) { const float aspect_ratio = 16.0f / 9.0f; for (float t = 0.0f; t < target_time; t += step_rate) { WGPUCommandEncoder encoder = diff --git a/src/gpu/effect.h b/src/gpu/effect.h index 19b8118..6ed2c55 100644 --- a/src/gpu/effect.h +++ b/src/gpu/effect.h @@ -113,7 +113,7 @@ class MainSequence { void shutdown(); #if !defined(STRIP_ALL) - void simulate_until(float target_time, float step_rate); + void simulate_until(float target_time, float step_rate, float bpm = 120.0f); #endif /* !defined(STRIP_ALL) */ private: 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 diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index 9776eac..025ea99 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -391,8 +391,8 @@ void gpu_resize(int width, int height) { } #if !defined(STRIP_ALL) -void gpu_simulate_until(float time) { - g_main_sequence.simulate_until(time, 1.0f / 60.0f); +void gpu_simulate_until(float time, float bpm) { + g_main_sequence.simulate_until(time, 1.0f / 60.0f, bpm); } void gpu_add_custom_effect(Effect* effect, float start_time, float end_time, diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h index 36ee662..7e2ff47 100644 --- a/src/gpu/gpu.h +++ b/src/gpu/gpu.h @@ -43,7 +43,7 @@ void gpu_init(PlatformState* platform_state); void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat); void gpu_resize(int width, int height); #if !defined(STRIP_ALL) -void gpu_simulate_until(float time); +void gpu_simulate_until(float time, float bpm = 120.0f); void gpu_add_custom_effect(Effect* effect, float start_time, float end_time, int priority); diff --git a/src/gpu/uniform_helper.h b/src/gpu/uniform_helper.h new file mode 100644 index 0000000..151153f --- /dev/null +++ b/src/gpu/uniform_helper.h @@ -0,0 +1,41 @@ +// This file is part of the 64k demo project. +// It provides a generic uniform buffer helper to reduce boilerplate. +// Templated on uniform struct type for type safety and automatic sizing. + +#pragma once + +#include "gpu/gpu.h" +#include <cstring> + +// Generic uniform buffer helper +// Usage: +// UniformBuffer<MyUniforms> uniforms_; +// uniforms_.init(device); +// uniforms_.update(queue, my_data); +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); + } + + // Update the uniform buffer with new data + void update(WGPUQueue queue, const T& data) { + wgpuQueueWriteBuffer(queue, buffer_.buffer, 0, &data, sizeof(T)); + } + + // Get the underlying GpuBuffer (for bind group creation) + GpuBuffer& get() { + return buffer_; + } + + const GpuBuffer& get() const { + return buffer_; + } + + private: + GpuBuffer buffer_; +}; |
