From e6e34e551a73e65301685071445aaec9aaf60fd2 Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 1 Feb 2026 01:41:01 +0100 Subject: Refactor: Move common GPU fields to base Effect classes Moved WGPUDevice, WGPUQueue, and GpuBuffer uniforms_ into the base Effect and PostProcessEffect classes. Updated all derived effect classes to use these inherited members and refactored their constructors. Also fixed associated compilation errors in test files and adjusted a printf statement. --- src/gpu/demo_effects.h | 34 +---------------------------- src/gpu/effect.cc | 2 +- src/gpu/effect.h | 10 +++++++++ src/gpu/effects/chroma_aberration_effect.cc | 6 ++--- src/gpu/effects/distort_effect.cc | 6 ++--- src/gpu/effects/gaussian_blur_effect.cc | 6 ++--- src/gpu/effects/heptagon_effect.cc | 6 ++--- src/gpu/effects/moving_ellipse_effect.cc | 6 ++--- src/gpu/effects/particle_spray_effect.cc | 10 ++++----- src/gpu/effects/particles_effect.cc | 10 ++++----- src/gpu/effects/passthrough_effect.cc | 8 +++---- src/gpu/effects/solarize_effect.cc | 6 ++--- src/tests/test_sequence.cc | 17 ++++++++------- 13 files changed, 53 insertions(+), 74 deletions(-) (limited to 'src') diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h index e94f015..2d495f3 100644 --- a/src/gpu/demo_effects.h +++ b/src/gpu/demo_effects.h @@ -24,9 +24,7 @@ class HeptagonEffect : public Effect { float intensity, float aspect_ratio) override; private: - WGPUQueue queue_; RenderPass pass_; - GpuBuffer uniforms_; }; class ParticlesEffect : public Effect { @@ -38,21 +36,15 @@ class ParticlesEffect : public Effect { float intensity, float aspect_ratio) override; private: - WGPUQueue queue_; ComputePass compute_pass_; RenderPass render_pass_; GpuBuffer particles_buffer_; - GpuBuffer uniforms_; }; class PassthroughEffect : public PostProcessEffect { public: - PassthroughEffect(WGPUDevice device, WGPUTextureFormat format); + PassthroughEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format); void update_bind_group(WGPUTextureView input_view) override; - - private: - WGPUDevice device_; - GpuBuffer uniforms_; }; class MovingEllipseEffect : public Effect { @@ -63,9 +55,7 @@ class MovingEllipseEffect : public Effect { float intensity, float aspect_ratio) override; private: - WGPUQueue queue_; RenderPass pass_; - GpuBuffer uniforms_; }; class ParticleSprayEffect : public Effect { @@ -78,11 +68,9 @@ class ParticleSprayEffect : public Effect { float intensity, float aspect_ratio) override; private: - WGPUQueue queue_; ComputePass compute_pass_; RenderPass render_pass_; GpuBuffer particles_buffer_; - GpuBuffer uniforms_; }; class GaussianBlurEffect : public PostProcessEffect { @@ -92,11 +80,6 @@ class GaussianBlurEffect : public PostProcessEffect { void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; - - private: - WGPUDevice device_; - WGPUQueue queue_; - GpuBuffer uniforms_; }; class SolarizeEffect : public PostProcessEffect { @@ -105,11 +88,6 @@ class SolarizeEffect : public PostProcessEffect { void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; - - private: - WGPUDevice device_; - WGPUQueue queue_; - GpuBuffer uniforms_; }; class DistortEffect : public PostProcessEffect { @@ -118,11 +96,6 @@ class DistortEffect : public PostProcessEffect { void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; - - private: - WGPUDevice device_; - WGPUQueue queue_; - GpuBuffer uniforms_; }; class ChromaAberrationEffect : public PostProcessEffect { @@ -132,11 +105,6 @@ class ChromaAberrationEffect : public PostProcessEffect { void render(WGPURenderPassEncoder pass, float time, float beat, float intensity, float aspect_ratio) override; void update_bind_group(WGPUTextureView input_view) override; - - private: - WGPUDevice device_; - WGPUQueue queue_; - GpuBuffer uniforms_; }; // Auto-generated function to populate the timeline diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc index 64bd053..997946c 100644 --- a/src/gpu/effect.cc +++ b/src/gpu/effect.cc @@ -128,7 +128,7 @@ void MainSequence::init(WGPUDevice d, WGPUQueue q, WGPUTextureFormat f, format = f; create_framebuffers(width, height); - passthrough_effect_ = std::make_unique(device, format); + passthrough_effect_ = std::make_unique(device, queue, format); for (ActiveSequence& entry : sequences_) { entry.seq->init(this); diff --git a/src/gpu/effect.h b/src/gpu/effect.h index ee90fa4..488e92e 100644 --- a/src/gpu/effect.h +++ b/src/gpu/effect.h @@ -1,4 +1,5 @@ #pragma once +#include "gpu/gpu.h" #include #include #include @@ -14,6 +15,8 @@ class PostProcessEffect; class Effect { public: + Effect(WGPUDevice device, WGPUQueue queue) + : device_(device), queue_(queue) {} virtual ~Effect() = default; virtual void init(MainSequence* demo) { (void)demo; @@ -36,10 +39,17 @@ class Effect { virtual bool is_post_process() const { return false; } + + protected: + WGPUDevice device_; + WGPUQueue queue_; + GpuBuffer uniforms_; }; class PostProcessEffect : public Effect { public: + PostProcessEffect(WGPUDevice device, WGPUQueue queue) + : Effect(device, queue) {} bool is_post_process() const override { return true; } diff --git a/src/gpu/effects/chroma_aberration_effect.cc b/src/gpu/effects/chroma_aberration_effect.cc index 5acc1f4..ef9e963 100644 --- a/src/gpu/effects/chroma_aberration_effect.cc +++ b/src/gpu/effects/chroma_aberration_effect.cc @@ -8,11 +8,11 @@ ChromaAberrationEffect::ChromaAberrationEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : device_(device), queue_(queue) { + : PostProcessEffect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); - pipeline_ = create_post_process_pipeline(device, format, + pipeline_ = create_post_process_pipeline(device_, format, chroma_aberration_shader_wgsl); } void ChromaAberrationEffect::render(WGPURenderPassEncoder pass, float t, diff --git a/src/gpu/effects/distort_effect.cc b/src/gpu/effects/distort_effect.cc index 994ee34..1f2b8fc 100644 --- a/src/gpu/effects/distort_effect.cc +++ b/src/gpu/effects/distort_effect.cc @@ -7,11 +7,11 @@ // --- DistortEffect --- DistortEffect::DistortEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : device_(device), queue_(queue) { + : PostProcessEffect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); - pipeline_ = create_post_process_pipeline(device, format, distort_shader_wgsl); + pipeline_ = create_post_process_pipeline(device_, format, distort_shader_wgsl); } void DistortEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { diff --git a/src/gpu/effects/gaussian_blur_effect.cc b/src/gpu/effects/gaussian_blur_effect.cc index 0b55bb0..28f5b97 100644 --- a/src/gpu/effects/gaussian_blur_effect.cc +++ b/src/gpu/effects/gaussian_blur_effect.cc @@ -7,12 +7,12 @@ // --- GaussianBlurEffect --- GaussianBlurEffect::GaussianBlurEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : device_(device), queue_(queue) { + : PostProcessEffect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = - create_post_process_pipeline(device, format, gaussian_blur_shader_wgsl); + create_post_process_pipeline(device_, format, gaussian_blur_shader_wgsl); } void GaussianBlurEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { diff --git a/src/gpu/effects/heptagon_effect.cc b/src/gpu/effects/heptagon_effect.cc index 321b9d1..64e4b47 100644 --- a/src/gpu/effects/heptagon_effect.cc +++ b/src/gpu/effects/heptagon_effect.cc @@ -7,12 +7,12 @@ // --- HeptagonEffect --- HeptagonEffect::HeptagonEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : queue_(queue) { + : Effect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); ResourceBinding bindings[] = {{uniforms_, WGPUBufferBindingType_Uniform}}; - pass_ = gpu_create_render_pass(device, format, main_shader_wgsl, bindings, 1); + pass_ = gpu_create_render_pass(device_, format, main_shader_wgsl, bindings, 1); pass_.vertex_count = 21; } void HeptagonEffect::render(WGPURenderPassEncoder pass, float t, float b, diff --git a/src/gpu/effects/moving_ellipse_effect.cc b/src/gpu/effects/moving_ellipse_effect.cc index 60fc825..b46eecd 100644 --- a/src/gpu/effects/moving_ellipse_effect.cc +++ b/src/gpu/effects/moving_ellipse_effect.cc @@ -7,13 +7,13 @@ // --- MovingEllipseEffect --- MovingEllipseEffect::MovingEllipseEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : queue_(queue) { + : Effect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); ResourceBinding bindings[] = {{uniforms_, WGPUBufferBindingType_Uniform}}; pass_ = - gpu_create_render_pass(device, format, ellipse_shader_wgsl, bindings, 1); + gpu_create_render_pass(device_, format, ellipse_shader_wgsl, bindings, 1); pass_.vertex_count = 3; } void MovingEllipseEffect::render(WGPURenderPassEncoder pass, float t, float b, diff --git a/src/gpu/effects/particle_spray_effect.cc b/src/gpu/effects/particle_spray_effect.cc index a5e4292..b5c5f42 100644 --- a/src/gpu/effects/particle_spray_effect.cc +++ b/src/gpu/effects/particle_spray_effect.cc @@ -8,26 +8,26 @@ // --- ParticleSprayEffect --- ParticleSprayEffect::ParticleSprayEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : queue_(queue) { + : Effect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); std::vector init_p(NUM_PARTICLES); for (Particle& p : init_p) p.pos[3] = 0.0f; particles_buffer_ = gpu_create_buffer( - device, sizeof(Particle) * NUM_PARTICLES, + device_, sizeof(Particle) * NUM_PARTICLES, WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data()); ResourceBinding cb[] = {{particles_buffer_, WGPUBufferBindingType_Storage}, {uniforms_, WGPUBufferBindingType_Uniform}}; compute_pass_ = - gpu_create_compute_pass(device, particle_spray_compute_wgsl, cb, 2); + gpu_create_compute_pass(device_, particle_spray_compute_wgsl, cb, 2); compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64; ResourceBinding rb[] = { {particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage}, {uniforms_, WGPUBufferBindingType_Uniform}}; render_pass_ = - gpu_create_render_pass(device, format, particle_render_wgsl, rb, 2); + gpu_create_render_pass(device_, format, particle_render_wgsl, rb, 2); render_pass_.vertex_count = 6; render_pass_.instance_count = NUM_PARTICLES; } diff --git a/src/gpu/effects/particles_effect.cc b/src/gpu/effects/particles_effect.cc index 545fdfb..f2ef96b 100644 --- a/src/gpu/effects/particles_effect.cc +++ b/src/gpu/effects/particles_effect.cc @@ -8,23 +8,23 @@ // --- ParticlesEffect --- ParticlesEffect::ParticlesEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : queue_(queue) { + : Effect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); std::vector init_p(NUM_PARTICLES); particles_buffer_ = gpu_create_buffer( - device, sizeof(Particle) * NUM_PARTICLES, + device_, sizeof(Particle) * NUM_PARTICLES, WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex, init_p.data()); ResourceBinding cb[] = {{particles_buffer_, WGPUBufferBindingType_Storage}, {uniforms_, WGPUBufferBindingType_Uniform}}; - compute_pass_ = gpu_create_compute_pass(device, particle_compute_wgsl, cb, 2); + compute_pass_ = gpu_create_compute_pass(device_, particle_compute_wgsl, cb, 2); compute_pass_.workgroup_size_x = (NUM_PARTICLES + 63) / 64; ResourceBinding rb[] = { {particles_buffer_, WGPUBufferBindingType_ReadOnlyStorage}, {uniforms_, WGPUBufferBindingType_Uniform}}; render_pass_ = - gpu_create_render_pass(device, format, particle_render_wgsl, rb, 2); + gpu_create_render_pass(device_, format, particle_render_wgsl, rb, 2); render_pass_.vertex_count = 6; render_pass_.instance_count = NUM_PARTICLES; } diff --git a/src/gpu/effects/passthrough_effect.cc b/src/gpu/effects/passthrough_effect.cc index a00f661..cb92ba1 100644 --- a/src/gpu/effects/passthrough_effect.cc +++ b/src/gpu/effects/passthrough_effect.cc @@ -5,14 +5,14 @@ #include "gpu/gpu.h" // --- PassthroughEffect --- -PassthroughEffect::PassthroughEffect(WGPUDevice device, +PassthroughEffect::PassthroughEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : device_(device) { + : PostProcessEffect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = - create_post_process_pipeline(device, format, passthrough_shader_wgsl); + create_post_process_pipeline(device_, format, passthrough_shader_wgsl); } void PassthroughEffect::update_bind_group(WGPUTextureView input_view) { pp_update_bind_group(device_, pipeline_, &bind_group_, input_view, uniforms_); diff --git a/src/gpu/effects/solarize_effect.cc b/src/gpu/effects/solarize_effect.cc index f7c09de..f8a7f33 100644 --- a/src/gpu/effects/solarize_effect.cc +++ b/src/gpu/effects/solarize_effect.cc @@ -7,12 +7,12 @@ // --- SolarizeEffect --- SolarizeEffect::SolarizeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format) - : device_(device), queue_(queue) { + : PostProcessEffect(device, queue) { uniforms_ = - gpu_create_buffer(device, sizeof(float) * 4, + gpu_create_buffer(device_, sizeof(float) * 4, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst); pipeline_ = - create_post_process_pipeline(device, format, solarize_shader_wgsl); + create_post_process_pipeline(device_, format, solarize_shader_wgsl); } void SolarizeEffect::render(WGPURenderPassEncoder pass, float t, float b, float i, float a) { diff --git a/src/tests/test_sequence.cc b/src/tests/test_sequence.cc index 6b04302..d944371 100644 --- a/src/tests/test_sequence.cc +++ b/src/tests/test_sequence.cc @@ -26,8 +26,8 @@ class DummyEffect : public Effect { int end_calls = 0; bool is_pp = false; - DummyEffect(bool post_process = false) : is_pp(post_process) { - } + DummyEffect(WGPUDevice device, WGPUQueue queue, bool post_process = false) + : Effect(device, queue), is_pp(post_process) {} void init(MainSequence* demo) override { ++init_calls; @@ -68,8 +68,9 @@ class DummyPostProcessEffect : public PostProcessEffect { int render_calls = 0; int update_bind_group_calls = 0; - DummyPostProcessEffect(WGPUDevice device, WGPUTextureFormat format) { - (void)device; + DummyPostProcessEffect(WGPUDevice device, WGPUQueue queue, + WGPUTextureFormat format) + : PostProcessEffect(device, queue) { (void)format; } @@ -99,7 +100,7 @@ void test_effect_lifecycle() { MainSequence main_seq; main_seq.init_test(dummy_device, dummy_queue, dummy_format); - auto effect1 = std::make_shared(); + auto effect1 = std::make_shared(dummy_device, dummy_queue); auto seq1 = std::make_shared(); seq1->add_effect(effect1, 1.0f, 3.0f); main_seq.add_sequence(seq1, 0.0f, 0); @@ -145,7 +146,7 @@ void test_simulate_until() { MainSequence main_seq; main_seq.init_test(dummy_device, dummy_queue, dummy_format); - auto effect1 = std::make_shared(); + auto effect1 = std::make_shared(dummy_device, dummy_queue); auto seq1 = std::make_shared(); seq1->add_effect(effect1, 1.0f, 3.0f); main_seq.add_sequence(seq1, 0.0f, 0); @@ -164,7 +165,7 @@ void test_simulate_until() { assert(effect1->render_calls == 0); assert(effect1->end_calls == 1); // Should end #else - printf(" test_simulate_until (skipped in STRIP_ALL build)...\n"); + printf(" test_simulate_until (skipped in STRIP_ALL build)...\\n"); #endif /* !defined(STRIP_ALL) */ } @@ -184,4 +185,4 @@ int main() { printf("Sequence/Effect System tests PASSED\n"); return 0; -} +} \ No newline at end of file -- cgit v1.2.3