diff options
Diffstat (limited to 'src/gpu/effects')
21 files changed, 111 insertions, 86 deletions
diff --git a/src/gpu/effects/chroma_aberration_effect.cc b/src/gpu/effects/chroma_aberration_effect.cc index 7f41153..af3acc5 100644 --- a/src/gpu/effects/chroma_aberration_effect.cc +++ b/src/gpu/effects/chroma_aberration_effect.cc @@ -18,7 +18,6 @@ ChromaAberrationEffect::ChromaAberrationEffect( : PostProcessEffect(ctx), params_(params) { pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, chroma_aberration_shader_wgsl); - uniforms_.init(ctx_.device); params_buffer_.init(ctx_.device); } diff --git a/src/gpu/effects/circle_mask_effect.cc b/src/gpu/effects/circle_mask_effect.cc index 5b71086..ca80cf9 100644 --- a/src/gpu/effects/circle_mask_effect.cc +++ b/src/gpu/effects/circle_mask_effect.cc @@ -3,6 +3,7 @@ // Generates circular mask and renders green background outside circle. #include "gpu/effects/circle_mask_effect.h" +#include "gpu/effects/shader_composer.h" #include "generated/assets.h" CircleMaskEffect::CircleMaskEffect(const GpuContext& ctx, float radius) @@ -30,9 +31,7 @@ void CircleMaskEffect::init(MainSequence* demo) { demo_->register_auxiliary_texture("circle_mask", width, height); - compute_uniforms_.init(ctx_.device); compute_params_.init(ctx_.device); - render_uniforms_.init(ctx_.device); WGPUSamplerDescriptor sampler_desc = {}; sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; @@ -49,9 +48,12 @@ void CircleMaskEffect::init(MainSequence* demo) { const char* render_shader = (const char*)GetAsset( AssetId::ASSET_CIRCLE_MASK_RENDER_SHADER, &render_size); + // Compose shaders to resolve #include directives + std::string composed_compute = ShaderComposer::Get().Compose({}, compute_shader); + WGPUShaderSourceWGSL compute_wgsl = {}; compute_wgsl.chain.sType = WGPUSType_ShaderSourceWGSL; - compute_wgsl.code = str_view(compute_shader); + compute_wgsl.code = str_view(composed_compute.c_str()); WGPUShaderModuleDescriptor compute_desc = {}; compute_desc.nextInChain = &compute_wgsl.chain; @@ -82,11 +84,11 @@ void CircleMaskEffect::init(MainSequence* demo) { const WGPUBindGroupEntry compute_entries[] = { {.binding = 0, - .buffer = compute_uniforms_.get().buffer, + .buffer = uniforms_.get().buffer, .size = sizeof(CommonPostProcessUniforms)}, {.binding = 1, .buffer = compute_params_.get().buffer, - .size = sizeof(EffectParams)}, + .size = sizeof(CircleMaskParams)}, }; const WGPUBindGroupDescriptor compute_bg_desc = { .layout = wgpuRenderPipelineGetBindGroupLayout(compute_pipeline_, 0), @@ -96,9 +98,11 @@ void CircleMaskEffect::init(MainSequence* demo) { compute_bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &compute_bg_desc); + std::string composed_render = ShaderComposer::Get().Compose({}, render_shader); + WGPUShaderSourceWGSL render_wgsl = {}; render_wgsl.chain.sType = WGPUSType_ShaderSourceWGSL; - render_wgsl.code = str_view(render_shader); + render_wgsl.code = str_view(composed_render.c_str()); WGPUShaderModuleDescriptor render_desc = {}; render_desc.nextInChain = &render_wgsl.chain; @@ -139,7 +143,7 @@ void CircleMaskEffect::init(MainSequence* demo) { {.binding = 0, .textureView = mask_view}, {.binding = 1, .sampler = mask_sampler_}, {.binding = 2, - .buffer = render_uniforms_.get().buffer, + .buffer = uniforms_.get().buffer, .size = sizeof(CommonPostProcessUniforms)}, }; const WGPUBindGroupDescriptor render_bg_desc = { @@ -160,9 +164,9 @@ void CircleMaskEffect::compute(WGPUCommandEncoder encoder, float time, .beat = beat, .audio_intensity = intensity, }; - compute_uniforms_.update(ctx_.queue, uniforms); + uniforms_.update(ctx_.queue, uniforms); - const EffectParams params = { + const CircleMaskParams params = { .radius = radius_, }; compute_params_.update(ctx_.queue, params); @@ -199,7 +203,7 @@ void CircleMaskEffect::render(WGPURenderPassEncoder pass, float time, .beat = beat, .audio_intensity = intensity, }; - render_uniforms_.update(ctx_.queue, uniforms); + uniforms_.update(ctx_.queue, uniforms); wgpuRenderPassEncoderSetPipeline(pass, render_pipeline_); wgpuRenderPassEncoderSetBindGroup(pass, 0, render_bind_group_, 0, nullptr); diff --git a/src/gpu/effects/circle_mask_effect.h b/src/gpu/effects/circle_mask_effect.h index ac44210..2ddbb11 100644 --- a/src/gpu/effects/circle_mask_effect.h +++ b/src/gpu/effects/circle_mask_effect.h @@ -21,23 +21,23 @@ class CircleMaskEffect : public Effect { float intensity, float aspect_ratio) override; private: - struct EffectParams { + struct CircleMaskParams { float radius; float _pad[3]; }; + static_assert(sizeof(CircleMaskParams) == 16, + "CircleMaskParams must be 16 bytes for WGSL alignment"); MainSequence* demo_ = nullptr; float radius_; WGPURenderPipeline compute_pipeline_ = nullptr; WGPUBindGroup compute_bind_group_ = nullptr; - UniformBuffer<CommonPostProcessUniforms> compute_uniforms_; - UniformBuffer<EffectParams> compute_params_; + UniformBuffer<CircleMaskParams> compute_params_; WGPURenderPipeline render_pipeline_ = nullptr; WGPUBindGroup render_bind_group_ = nullptr; WGPUSampler mask_sampler_ = nullptr; - UniformBuffer<CommonPostProcessUniforms> render_uniforms_; }; #endif /* CIRCLE_MASK_EFFECT_H_ */ diff --git a/src/gpu/effects/distort_effect.cc b/src/gpu/effects/distort_effect.cc index d11dfd7..52a8ec7 100644 --- a/src/gpu/effects/distort_effect.cc +++ b/src/gpu/effects/distort_effect.cc @@ -9,31 +9,35 @@ DistortEffect::DistortEffect(const GpuContext& ctx) : DistortEffect(ctx, DistortParams()) { } -DistortEffect::DistEffect(const GpuContext& ctx, const DistortParams& params) +DistortEffect::DistortEffect(const GpuContext& ctx, const DistortParams& params) : PostProcessEffect(ctx), params_(params) { - uniforms_ = - gpu_create_buffer(ctx_.device, sizeof(DistortUniforms), - WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); + params_buffer_.init(ctx_.device); pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, distort_shader_wgsl); } void DistortEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { - DistortUniforms u = { + // Populate CommonPostProcessUniforms + const CommonPostProcessUniforms common_u = { + .resolution = {(float)width_, (float)height_}, + .aspect_ratio = a, .time = t, .beat = b, - .intensity = i, - .aspect_ratio = a, - .width = (float)width_, - .height = (float)height_, + .audio_intensity = i, + }; + uniforms_.update(ctx_.queue, common_u); + + // Populate DistortParams + const DistortParams distort_p = { .strength = params_.strength, .speed = params_.speed, }; - wgpuQueueWriteBuffer(ctx_.queue, uniforms_.buffer, 0, &u, sizeof(u)); + params_buffer_.update(ctx_.queue, distort_p); + PostProcessEffect::render(pass, t, b, i, a); } void DistortEffect::update_bind_group(WGPUTextureView v) { - pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, {}, uniforms_); + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, uniforms_.get(), params_buffer_); }
\ No newline at end of file diff --git a/src/gpu/effects/fade_effect.cc b/src/gpu/effects/fade_effect.cc index 3efc583..39b54e0 100644 --- a/src/gpu/effects/fade_effect.cc +++ b/src/gpu/effects/fade_effect.cc @@ -5,6 +5,12 @@ #include "gpu/effects/post_process_helper.h" #include <cmath> +struct FadeParams { + float fade_amount; + float _pad[3]; +}; +static_assert(sizeof(FadeParams) == 16, "FadeParams must be 16 bytes for WGSL alignment"); + FadeEffect::FadeEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { const char* shader_code = R"( struct VertexOutput { @@ -22,7 +28,7 @@ FadeEffect::FadeEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { audio_intensity: f32, }; - struct EffectParams { + struct FadeParams { fade_amount: f32, _pad0: f32, _pad1: f32, @@ -32,7 +38,7 @@ FadeEffect::FadeEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { @group(0) @binding(0) var inputSampler: sampler; @group(0) @binding(1) var inputTexture: texture_2d<f32>; @group(0) @binding(2) var<uniform> uniforms: CommonUniforms; - @group(0) @binding(3) var<uniform> params: EffectParams; + @group(0) @binding(3) var<uniform> params: FadeParams; @vertex fn vs_main(@builtin(vertex_index) vertexIndex: u32) -> VertexOutput { @@ -57,14 +63,13 @@ FadeEffect::FadeEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, shader_code); - common_uniforms_.init(ctx_.device); params_buffer_ = gpu_create_buffer( ctx_.device, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); } void FadeEffect::update_bind_group(WGPUTextureView input_view) { pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, - common_uniforms_.get(), params_buffer_); + uniforms_.get(), params_buffer_); } void FadeEffect::render(WGPURenderPassEncoder pass, float time, float beat, @@ -76,7 +81,7 @@ void FadeEffect::render(WGPURenderPassEncoder pass, float time, float beat, .beat = beat, .audio_intensity = intensity, }; - common_uniforms_.update(ctx_.queue, u); + uniforms_.update(ctx_.queue, u); // Example fade pattern: fade in at start, fade out at end // Customize this based on your needs @@ -90,8 +95,8 @@ void FadeEffect::render(WGPURenderPassEncoder pass, float time, float beat, fade_amount = fmaxf(fade_amount, 0.0f); } - float params[4] = {fade_amount, 0.0f, 0.0f, 0.0f}; - wgpuQueueWriteBuffer(ctx_.queue, params_buffer_.buffer, 0, params, + FadeParams params = {fade_amount, {0.0f, 0.0f, 0.0f}}; + wgpuQueueWriteBuffer(ctx_.queue, params_buffer_.buffer, 0, ¶ms, sizeof(params)); wgpuRenderPassEncoderSetPipeline(pass, pipeline_); diff --git a/src/gpu/effects/fade_effect.h b/src/gpu/effects/fade_effect.h index 22b8f76..178c360 100644 --- a/src/gpu/effects/fade_effect.h +++ b/src/gpu/effects/fade_effect.h @@ -4,9 +4,9 @@ #pragma once #include "gpu/effect.h" +#include "gpu/effects/post_process_helper.h" #include "gpu/gpu.h" #include "gpu/uniform_helper.h" -#include "gpu/effects/post_process_helper.h" class FadeEffect : public PostProcessEffect { public: @@ -16,6 +16,5 @@ class FadeEffect : public PostProcessEffect { void update_bind_group(WGPUTextureView input_view) override; private: - UniformBuffer<CommonPostProcessUniforms> common_uniforms_; GpuBuffer params_buffer_; }; diff --git a/src/gpu/effects/flash_cube_effect.h b/src/gpu/effects/flash_cube_effect.h index 7089af2..5faeb00 100644 --- a/src/gpu/effects/flash_cube_effect.h +++ b/src/gpu/effects/flash_cube_effect.h @@ -22,8 +22,6 @@ class FlashCubeEffect : public Effect { TextureManager texture_manager_; Scene scene_; Camera camera_; - int width_ = 1280; - int height_ = 720; float last_beat_ = 0.0f; float flash_intensity_ = 0.0f; }; diff --git a/src/gpu/effects/gaussian_blur_effect.cc b/src/gpu/effects/gaussian_blur_effect.cc index 0cc4821..697be88 100644 --- a/src/gpu/effects/gaussian_blur_effect.cc +++ b/src/gpu/effects/gaussian_blur_effect.cc @@ -18,7 +18,6 @@ GaussianBlurEffect::GaussianBlurEffect(const GpuContext& ctx, : PostProcessEffect(ctx), params_(params) { pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, gaussian_blur_shader_wgsl); - uniforms_.init(ctx_.device); params_buffer_.init(ctx_.device); } diff --git a/src/gpu/effects/heptagon_effect.cc b/src/gpu/effects/heptagon_effect.cc index b77ec53..7b0702d 100644 --- a/src/gpu/effects/heptagon_effect.cc +++ b/src/gpu/effects/heptagon_effect.cc @@ -5,39 +5,25 @@ #include "gpu/gpu.h" #include "util/mini_math.h" -// Match CommonUniforms struct from main_shader.wgsl. -// Padded to 32 bytes for WGSL alignment rules. -struct HeptagonUniforms { - vec2 resolution; // 8 bytes - float _pad0[2]; // 8 bytes padding to align next float - float aspect_ratio; // 4 bytes - float time; // 4 bytes - float beat; // 4 bytes - float audio_intensity; // 4 bytes -}; -static_assert(sizeof(HeptagonUniforms) == 32, - "HeptagonUniforms must be 32 bytes for WGSL alignment"); - // --- HeptagonEffect --- HeptagonEffect::HeptagonEffect(const GpuContext& ctx) : Effect(ctx) { - uniforms_ = - gpu_create_buffer(ctx_.device, sizeof(HeptagonUniforms), - WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); - ResourceBinding bindings[] = {{uniforms_, WGPUBufferBindingType_Uniform}}; + // uniforms_ is initialized by Effect base class + ResourceBinding bindings[] = {{uniforms_.get(), WGPUBufferBindingType_Uniform}}; pass_ = gpu_create_render_pass(ctx_.device, ctx_.format, main_shader_wgsl, bindings, 1); pass_.vertex_count = 21; } void HeptagonEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { - HeptagonUniforms u = { + CommonPostProcessUniforms u = { .resolution = {(float)width_, (float)height_}, + ._pad = {0.0f, 0.0f}, .aspect_ratio = a, .time = t, .beat = b, .audio_intensity = i, }; - wgpuQueueWriteBuffer(ctx_.queue, uniforms_.buffer, 0, &u, sizeof(u)); + uniforms_.update(ctx_.queue, u); wgpuRenderPassEncoderSetPipeline(pass, pass_.pipeline); wgpuRenderPassEncoderSetBindGroup(pass, 0, pass_.bind_group, 0, nullptr); wgpuRenderPassEncoderDraw(pass, pass_.vertex_count, 1, 0, 0); diff --git a/src/gpu/effects/moving_ellipse_effect.cc b/src/gpu/effects/moving_ellipse_effect.cc index 945f807..9866f20 100644 --- a/src/gpu/effects/moving_ellipse_effect.cc +++ b/src/gpu/effects/moving_ellipse_effect.cc @@ -7,10 +7,8 @@ // --- MovingEllipseEffect --- MovingEllipseEffect::MovingEllipseEffect(const GpuContext& ctx) : Effect(ctx) { - uniforms_ = - gpu_create_buffer(ctx_.device, sizeof(CommonPostProcessUniforms), - WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); - ResourceBinding bindings[] = {{uniforms_, WGPUBufferBindingType_Uniform}}; + // uniforms_ is initialized by Effect base class + ResourceBinding bindings[] = {{uniforms_.get(), WGPUBufferBindingType_Uniform}}; pass_ = gpu_create_render_pass(ctx_.device, ctx_.format, ellipse_shader_wgsl, bindings, 1); pass_.vertex_count = 3; @@ -19,12 +17,13 @@ void MovingEllipseEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { const CommonPostProcessUniforms u = { .resolution = {(float)width_, (float)height_}, + ._pad = {0.0f, 0.0f}, .aspect_ratio = a, .time = t, .beat = b, .audio_intensity = i, }; - wgpuQueueWriteBuffer(ctx_.queue, uniforms_.buffer, 0, &u, sizeof(u)); + uniforms_.update(ctx_.queue, u); wgpuRenderPassEncoderSetPipeline(pass, pass_.pipeline); wgpuRenderPassEncoderSetBindGroup(pass, 0, pass_.bind_group, 0, nullptr); wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); diff --git a/src/gpu/effects/particle_spray_effect.cc b/src/gpu/effects/particle_spray_effect.cc index 3fd2590..a435884 100644 --- a/src/gpu/effects/particle_spray_effect.cc +++ b/src/gpu/effects/particle_spray_effect.cc @@ -8,7 +8,6 @@ // --- ParticleSprayEffect --- ParticleSprayEffect::ParticleSprayEffect(const GpuContext& ctx) : Effect(ctx) { - uniforms_.init(ctx_.device); std::vector<Particle> init_p(NUM_PARTICLES); for (Particle& p : init_p) p.pos[3] = 0.0f; diff --git a/src/gpu/effects/particles_effect.cc b/src/gpu/effects/particles_effect.cc index 01f90a5..cd0df74 100644 --- a/src/gpu/effects/particles_effect.cc +++ b/src/gpu/effects/particles_effect.cc @@ -8,7 +8,6 @@ // --- ParticlesEffect --- ParticlesEffect::ParticlesEffect(const GpuContext& ctx) : Effect(ctx) { - uniforms_.init(ctx_.device); std::vector<Particle> init_p(NUM_PARTICLES); particles_buffer_ = gpu_create_buffer( ctx_.device, sizeof(Particle) * NUM_PARTICLES, diff --git a/src/gpu/effects/passthrough_effect.cc b/src/gpu/effects/passthrough_effect.cc index 93cf948..01d557a 100644 --- a/src/gpu/effects/passthrough_effect.cc +++ b/src/gpu/effects/passthrough_effect.cc @@ -7,7 +7,6 @@ // --- PassthroughEffect --- PassthroughEffect::PassthroughEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { - uniforms_.init(ctx_.device); pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, passthrough_shader_wgsl); } diff --git a/src/gpu/effects/post_process_helper.cc b/src/gpu/effects/post_process_helper.cc index 74e052d..e99467f 100644 --- a/src/gpu/effects/post_process_helper.cc +++ b/src/gpu/effects/post_process_helper.cc @@ -4,16 +4,19 @@ #include "post_process_helper.h" #include "../demo_effects.h" #include "gpu/gpu.h" +#include "gpu/effects/shader_composer.h" #include <cstring> // Helper to create a standard post-processing pipeline WGPURenderPipeline create_post_process_pipeline(WGPUDevice device, WGPUTextureFormat format, const char* shader_code) { + std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code); + WGPUShaderModuleDescriptor shader_desc = {}; WGPUShaderSourceWGSL wgsl_src = {}; wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(shader_code); + wgsl_src.code = str_view(composed_shader.c_str()); shader_desc.nextInChain = &wgsl_src.chain; WGPUShaderModule shader_module = wgpuDeviceCreateShaderModule(device, &shader_desc); @@ -94,7 +97,8 @@ void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline, bge[2].buffer = uniforms.buffer; bge[2].size = uniforms.size; bge[3].binding = PP_BINDING_EFFECT_PARAMS; - bge[3].buffer = effect_params.buffer ? effect_params.buffer : g_dummy_buffer.buffer; + bge[3].buffer = + effect_params.buffer ? effect_params.buffer : g_dummy_buffer.buffer; bge[3].size = effect_params.buffer ? effect_params.size : g_dummy_buffer.size; WGPUBindGroupDescriptor bgd = { .layout = bgl, .entryCount = 4, .entries = bge}; diff --git a/src/gpu/effects/post_process_helper.h b/src/gpu/effects/post_process_helper.h index 77b184f..23cde0e 100644 --- a/src/gpu/effects/post_process_helper.h +++ b/src/gpu/effects/post_process_helper.h @@ -19,10 +19,10 @@ static_assert(sizeof(CommonPostProcessUniforms) == 32, "CommonPostProcessUniforms must be 32 bytes for WGSL alignment"); // Standard post-process bind group layout (group 0): -#define PP_BINDING_SAMPLER 0 // Sampler for input texture -#define PP_BINDING_TEXTURE 1 // Input texture (previous render pass) -#define PP_BINDING_UNIFORMS 2 // Custom uniforms buffer -#define PP_BINDING_EFFECT_PARAMS 3 // Effect-specific parameters +#define PP_BINDING_SAMPLER 0 // Sampler for input texture +#define PP_BINDING_TEXTURE 1 // Input texture (previous render pass) +#define PP_BINDING_UNIFORMS 2 // Custom uniforms buffer +#define PP_BINDING_EFFECT_PARAMS 3 // Effect-specific parameters // Helper to create a standard post-processing pipeline // Uniforms are accessible to both vertex and fragment shaders diff --git a/src/gpu/effects/shaders.cc b/src/gpu/effects/shaders.cc index 2e1cfe5..625c5b6 100644 --- a/src/gpu/effects/shaders.cc +++ b/src/gpu/effects/shaders.cc @@ -99,6 +99,28 @@ const char* chroma_aberration_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_CHROMA_ABERRATION); +const char* gen_noise_compute_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_COMPUTE_GEN_NOISE); + +const char* gen_perlin_compute_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_COMPUTE_GEN_PERLIN); + +const char* gen_grid_compute_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_COMPUTE_GEN_GRID); + +#if !defined(STRIP_GPU_COMPOSITE) +const char* gen_blend_compute_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_COMPUTE_GEN_BLEND); + +const char* gen_mask_compute_wgsl = + + SafeGetAsset(AssetId::ASSET_SHADER_COMPUTE_GEN_MASK); +#endif + const char* vignette_shader_wgsl = SafeGetAsset(AssetId::ASSET_SHADER_VIGNETTE); diff --git a/src/gpu/effects/shaders.h b/src/gpu/effects/shaders.h index 50b4f32..68b8834 100644 --- a/src/gpu/effects/shaders.h +++ b/src/gpu/effects/shaders.h @@ -18,3 +18,10 @@ extern const char* solarize_shader_wgsl; extern const char* distort_shader_wgsl; extern const char* chroma_aberration_shader_wgsl; extern const char* vignette_shader_wgsl; +extern const char* gen_noise_compute_wgsl; +extern const char* gen_perlin_compute_wgsl; +extern const char* gen_grid_compute_wgsl; +#if !defined(STRIP_GPU_COMPOSITE) +extern const char* gen_blend_compute_wgsl; +extern const char* gen_mask_compute_wgsl; +#endif diff --git a/src/gpu/effects/solarize_effect.cc b/src/gpu/effects/solarize_effect.cc index d74d708..4f47218 100644 --- a/src/gpu/effects/solarize_effect.cc +++ b/src/gpu/effects/solarize_effect.cc @@ -6,7 +6,6 @@ // --- SolarizeEffect --- SolarizeEffect::SolarizeEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { - uniforms_.init(ctx.device); pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, solarize_shader_wgsl); } @@ -23,6 +22,6 @@ void SolarizeEffect::render(WGPURenderPassEncoder pass, float t, float b, PostProcessEffect::render(pass, t, b, i, a); } void SolarizeEffect::update_bind_group(WGPUTextureView v) { - pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, - uniforms_.get(), {}); + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, uniforms_.get(), + {}); } diff --git a/src/gpu/effects/theme_modulation_effect.cc b/src/gpu/effects/theme_modulation_effect.cc index f9ae636..b1eff90 100644 --- a/src/gpu/effects/theme_modulation_effect.cc +++ b/src/gpu/effects/theme_modulation_effect.cc @@ -6,6 +6,12 @@ #include "gpu/effects/shaders.h" #include <cmath> +struct ThemeModulationParams { + float theme_brightness; + float _pad[3]; +}; +static_assert(sizeof(ThemeModulationParams) == 16, "ThemeModulationParams must be 16 bytes for WGSL alignment"); + ThemeModulationEffect::ThemeModulationEffect(const GpuContext& ctx) : PostProcessEffect(ctx) { const char* shader_code = R"( @@ -24,7 +30,7 @@ ThemeModulationEffect::ThemeModulationEffect(const GpuContext& ctx) audio_intensity: f32, }; - struct EffectParams { + struct ThemeModulationParams { theme_brightness: f32, _pad0: f32, _pad1: f32, @@ -34,7 +40,7 @@ ThemeModulationEffect::ThemeModulationEffect(const GpuContext& ctx) @group(0) @binding(0) var inputSampler: sampler; @group(0) @binding(1) var inputTexture: texture_2d<f32>; @group(0) @binding(2) var<uniform> uniforms: CommonUniforms; - @group(0) @binding(3) var<uniform> params: EffectParams; + @group(0) @binding(3) var<uniform> params: ThemeModulationParams; @vertex fn vs_main(@builtin(vertex_index) vertexIndex: u32) -> VertexOutput { @@ -61,14 +67,13 @@ ThemeModulationEffect::ThemeModulationEffect(const GpuContext& ctx) pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, shader_code); - common_uniforms_.init(ctx_.device); params_buffer_ = gpu_create_buffer( ctx_.device, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); } void ThemeModulationEffect::update_bind_group(WGPUTextureView input_view) { pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, input_view, - common_uniforms_.get(), params_buffer_); + uniforms_.get(), params_buffer_); } void ThemeModulationEffect::render(WGPURenderPassEncoder pass, float time, @@ -81,7 +86,7 @@ void ThemeModulationEffect::render(WGPURenderPassEncoder pass, float time, .beat = beat, .audio_intensity = intensity, }; - common_uniforms_.update(ctx_.queue, u); + uniforms_.update(ctx_.queue, u); // Alternate between bright and dark every 4 seconds (2 pattern changes) // Music patterns change every 2 seconds at 120 BPM @@ -97,8 +102,8 @@ void ThemeModulationEffect::render(WGPURenderPassEncoder pass, float time, bright_value + (dark_value - bright_value) * transition; // Update params buffer - float params[4] = {theme_brightness, 0.0f, 0.0f, 0.0f}; - wgpuQueueWriteBuffer(ctx_.queue, params_buffer_.buffer, 0, params, + ThemeModulationParams params = {theme_brightness, {0.0f, 0.0f, 0.0f}}; + wgpuQueueWriteBuffer(ctx_.queue, params_buffer_.buffer, 0, ¶ms, sizeof(params)); // Render diff --git a/src/gpu/effects/theme_modulation_effect.h b/src/gpu/effects/theme_modulation_effect.h index 107529b..713347b 100644 --- a/src/gpu/effects/theme_modulation_effect.h +++ b/src/gpu/effects/theme_modulation_effect.h @@ -5,8 +5,8 @@ #pragma once #include "gpu/effect.h" -#include "gpu/uniform_helper.h" #include "gpu/effects/post_process_helper.h" +#include "gpu/uniform_helper.h" class ThemeModulationEffect : public PostProcessEffect { public: @@ -16,6 +16,5 @@ class ThemeModulationEffect : public PostProcessEffect { void update_bind_group(WGPUTextureView input_view) override; private: - UniformBuffer<CommonPostProcessUniforms> common_uniforms_; GpuBuffer params_buffer_; }; diff --git a/src/gpu/effects/vignette_effect.cc b/src/gpu/effects/vignette_effect.cc index a4967dd..bba0372 100644 --- a/src/gpu/effects/vignette_effect.cc +++ b/src/gpu/effects/vignette_effect.cc @@ -12,7 +12,6 @@ VignetteEffect::VignetteEffect(const GpuContext& ctx) VignetteEffect::VignetteEffect(const GpuContext& ctx, const VignetteParams& params) : PostProcessEffect(ctx), params_(params) { - uniforms_.init(ctx_.device); params_buffer_.init(ctx_.device); pipeline_ = create_post_process_pipeline(ctx_.device, ctx_.format, vignette_shader_wgsl); @@ -33,6 +32,6 @@ void VignetteEffect::render(WGPURenderPassEncoder pass, float t, float b, } void VignetteEffect::update_bind_group(WGPUTextureView v) { - pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, - uniforms_.get(), params_buffer_.get()); + pp_update_bind_group(ctx_.device, pipeline_, &bind_group_, v, uniforms_.get(), + params_buffer_.get()); } |
