summaryrefslogtreecommitdiff
path: root/src/gpu
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-07 17:04:56 +0100
committerskal <pascal.massimino@gmail.com>2026-02-07 17:04:56 +0100
commitbd939acdf750181ef0e1a612b445da4c15077c85 (patch)
tree028401c762b0436d9a5de1aa656ab35ba6445674 /src/gpu
parentf2963ac821a3af1c54002ba13944552166956d04 (diff)
refactor: Bundle GPU context into GpuContext struct
- Created GpuContext struct {device, queue, format} - Updated Effect/PostProcessEffect to take const GpuContext& - Updated all 19 effect implementations - Updated MainSequence.init() and LoadTimeline() signatures - Updated generated timeline files - Updated all test files - Added gpu_get_context() accessor and fixture.ctx() helper Fixes test_mesh.cc compilation error from g_device/g_queue/g_format conflicts. All targets build successfully.
Diffstat (limited to 'src/gpu')
-rw-r--r--src/gpu/demo_effects.h37
-rw-r--r--src/gpu/effect.cc35
-rw-r--r--src/gpu/effect.h16
-rw-r--r--src/gpu/effects/chroma_aberration_effect.cc8
-rw-r--r--src/gpu/effects/distort_effect.cc7
-rw-r--r--src/gpu/effects/fade_effect.cc9
-rw-r--r--src/gpu/effects/fade_effect.h2
-rw-r--r--src/gpu/effects/flash_cube_effect.cc10
-rw-r--r--src/gpu/effects/flash_cube_effect.h2
-rw-r--r--src/gpu/effects/flash_effect.cc9
-rw-r--r--src/gpu/effects/flash_effect.h2
-rw-r--r--src/gpu/effects/gaussian_blur_effect.cc7
-rw-r--r--src/gpu/effects/heptagon_effect.cc7
-rw-r--r--src/gpu/effects/hybrid_3d_effect.cc12
-rw-r--r--src/gpu/effects/hybrid_3d_effect.h2
-rw-r--r--src/gpu/effects/moving_ellipse_effect.cc7
-rw-r--r--src/gpu/effects/particle_spray_effect.cc7
-rw-r--r--src/gpu/effects/particles_effect.cc7
-rw-r--r--src/gpu/effects/passthrough_effect.cc7
-rw-r--r--src/gpu/effects/solarize_effect.cc7
-rw-r--r--src/gpu/effects/theme_modulation_effect.cc9
-rw-r--r--src/gpu/effects/theme_modulation_effect.h3
-rw-r--r--src/gpu/gpu.cc23
-rw-r--r--src/gpu/gpu.h13
24 files changed, 115 insertions, 133 deletions
diff --git a/src/gpu/demo_effects.h b/src/gpu/demo_effects.h
index 4b96ba7..cddd04b 100644
--- a/src/gpu/demo_effects.h
+++ b/src/gpu/demo_effects.h
@@ -23,7 +23,7 @@ struct Particle {
class HeptagonEffect : public Effect {
public:
- HeptagonEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ HeptagonEffect(const GpuContext& ctx);
void render(WGPURenderPassEncoder pass, float time, float beat,
float intensity, float aspect_ratio) override;
@@ -33,7 +33,7 @@ class HeptagonEffect : public Effect {
class ParticlesEffect : public Effect {
public:
- ParticlesEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ ParticlesEffect(const GpuContext& ctx);
void compute(WGPUCommandEncoder encoder, float time, float beat,
float intensity, float aspect_ratio) override;
void render(WGPURenderPassEncoder pass, float time, float beat,
@@ -47,15 +47,13 @@ class ParticlesEffect : public Effect {
class PassthroughEffect : public PostProcessEffect {
public:
- PassthroughEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+ PassthroughEffect(const GpuContext& ctx);
void update_bind_group(WGPUTextureView input_view) override;
};
class MovingEllipseEffect : public Effect {
public:
- MovingEllipseEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+ MovingEllipseEffect(const GpuContext& ctx);
void render(WGPURenderPassEncoder pass, float time, float beat,
float intensity, float aspect_ratio) override;
@@ -65,8 +63,7 @@ class MovingEllipseEffect : public Effect {
class ParticleSprayEffect : public Effect {
public:
- ParticleSprayEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+ ParticleSprayEffect(const GpuContext& ctx);
void compute(WGPUCommandEncoder encoder, float time, float beat,
float intensity, float aspect_ratio) override;
void render(WGPURenderPassEncoder pass, float time, float beat,
@@ -80,8 +77,7 @@ class ParticleSprayEffect : public Effect {
class GaussianBlurEffect : public PostProcessEffect {
public:
- GaussianBlurEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+ GaussianBlurEffect(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;
@@ -89,7 +85,7 @@ class GaussianBlurEffect : public PostProcessEffect {
class SolarizeEffect : public PostProcessEffect {
public:
- SolarizeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ SolarizeEffect(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;
@@ -97,7 +93,7 @@ class SolarizeEffect : public PostProcessEffect {
class DistortEffect : public PostProcessEffect {
public:
- DistortEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ DistortEffect(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;
@@ -105,8 +101,7 @@ class DistortEffect : public PostProcessEffect {
class ChromaAberrationEffect : public PostProcessEffect {
public:
- ChromaAberrationEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+ ChromaAberrationEffect(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;
@@ -114,7 +109,7 @@ class ChromaAberrationEffect : public PostProcessEffect {
class Hybrid3DEffect : public Effect {
public:
- Hybrid3DEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ Hybrid3DEffect(const GpuContext& ctx);
void init(MainSequence* demo) override;
void render(WGPURenderPassEncoder pass, float time, float beat,
float intensity, float aspect_ratio) override;
@@ -130,7 +125,7 @@ class Hybrid3DEffect : public Effect {
class FlashCubeEffect : public Effect {
public:
- FlashCubeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ FlashCubeEffect(const GpuContext& ctx);
void init(MainSequence* demo) override;
void resize(int width, int height) override;
void render(WGPURenderPassEncoder pass, float time, float beat,
@@ -149,8 +144,7 @@ class FlashCubeEffect : public Effect {
class ThemeModulationEffect : public PostProcessEffect {
public:
- ThemeModulationEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+ ThemeModulationEffect(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;
@@ -158,7 +152,7 @@ class ThemeModulationEffect : public PostProcessEffect {
class FadeEffect : public PostProcessEffect {
public:
- FadeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ FadeEffect(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;
@@ -166,7 +160,7 @@ class FadeEffect : public PostProcessEffect {
class FlashEffect : public PostProcessEffect {
public:
- FlashEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ 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;
@@ -176,7 +170,6 @@ class FlashEffect : public PostProcessEffect {
};
// Auto-generated functions
-void LoadTimeline(MainSequence& main_seq, WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+void LoadTimeline(MainSequence& main_seq, const GpuContext& ctx);
float GetDemoDuration(); // Returns demo end time in seconds, or -1 if not
// specified \ No newline at end of file
diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc
index b95c4ce..ea5230d 100644
--- a/src/gpu/effect.cc
+++ b/src/gpu/effect.cc
@@ -117,16 +117,16 @@ void MainSequence::create_framebuffers(int width, int height) {
WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding;
desc.dimension = WGPUTextureDimension_2D;
desc.size = {(uint32_t)width, (uint32_t)height, 1};
- desc.format = format;
+ desc.format = gpu_ctx.format;
desc.mipLevelCount = 1;
desc.sampleCount = 1;
- framebuffer_a_ = wgpuDeviceCreateTexture(device, &desc);
- framebuffer_b_ = wgpuDeviceCreateTexture(device, &desc);
+ framebuffer_a_ = wgpuDeviceCreateTexture(gpu_ctx.device, &desc);
+ framebuffer_b_ = wgpuDeviceCreateTexture(gpu_ctx.device, &desc);
WGPUTextureViewDescriptor view_desc = {};
view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.format = format;
+ view_desc.format = gpu_ctx.format;
view_desc.mipLevelCount = 1;
view_desc.arrayLayerCount = 1;
@@ -141,7 +141,7 @@ void MainSequence::create_framebuffers(int width, int height) {
depth_desc.format = WGPUTextureFormat_Depth24Plus;
depth_desc.mipLevelCount = 1;
depth_desc.sampleCount = 1;
- depth_texture_ = wgpuDeviceCreateTexture(device, &depth_desc);
+ depth_texture_ = wgpuDeviceCreateTexture(gpu_ctx.device, &depth_desc);
WGPUTextureViewDescriptor depth_view_desc = {};
depth_view_desc.format = WGPUTextureFormat_Depth24Plus;
@@ -152,25 +152,20 @@ void MainSequence::create_framebuffers(int width, int height) {
depth_view_ = wgpuTextureCreateView(depth_texture_, &depth_view_desc);
}
-void MainSequence::init_test(WGPUDevice d, WGPUQueue q, WGPUTextureFormat f) {
- device = d;
- queue = q;
- format = f;
+void MainSequence::init_test(const GpuContext& ctx) {
+ gpu_ctx = ctx;
// No framebuffers or passthrough effect created in test mode.
// Test effects should not rely on these being real.
}
-void MainSequence::init(WGPUDevice d, WGPUQueue q, WGPUTextureFormat f,
- int width, int height) {
- device = d;
- queue = q;
- format = f;
+void MainSequence::init(const GpuContext& ctx, int width, int height) {
+ gpu_ctx = ctx;
width_ = width;
height_ = height;
create_framebuffers(width, height);
passthrough_effect_ =
- std::make_unique<PassthroughEffect>(device, queue, format);
+ std::make_unique<PassthroughEffect>(gpu_ctx);
passthrough_effect_->resize(width, height);
for (ActiveSequence& entry : sequences_) {
@@ -183,7 +178,7 @@ void MainSequence::add_sequence(std::shared_ptr<Sequence> seq, float start_time,
int priority) {
sequences_.push_back({seq, start_time, priority});
// If MainSequence is already initialized, init the new sequence immediately
- if (device) {
+ if (gpu_ctx.device) {
seq->init(this);
seq->resize(width_, height_);
}
@@ -225,7 +220,7 @@ void MainSequence::resize(int width, int height) {
void MainSequence::render_frame(float global_time, float beat, float peak,
float aspect_ratio, WGPUSurface surface) {
- WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, nullptr);
+ WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(gpu_ctx.device, nullptr);
std::vector<SequenceItem*> scene_effects;
std::vector<SequenceItem*> post_effects;
@@ -360,7 +355,7 @@ void MainSequence::render_frame(float global_time, float beat, float peak,
}
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr);
- wgpuQueueSubmit(queue, 1, &commands);
+ wgpuQueueSubmit(gpu_ctx.queue, 1, &commands);
if (st.texture) {
wgpuTextureViewRelease(final_view);
@@ -393,7 +388,7 @@ void MainSequence::simulate_until(float target_time, float step_rate) {
const float aspect_ratio = 16.0f / 9.0f;
for (float t = 0.0f; t < target_time; t += step_rate) {
WGPUCommandEncoder encoder =
- wgpuDeviceCreateCommandEncoder(device, nullptr);
+ wgpuDeviceCreateCommandEncoder(gpu_ctx.device, nullptr);
float beat = fmodf(t * bpm / 60.0f, 1.0f);
std::vector<SequenceItem*> scene_effects, post_effects;
for (ActiveSequence& entry : sequences_) {
@@ -407,7 +402,7 @@ void MainSequence::simulate_until(float target_time, float step_rate) {
aspect_ratio);
}
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr);
- wgpuQueueSubmit(queue, 1, &commands);
+ wgpuQueueSubmit(gpu_ctx.queue, 1, &commands);
}
}
#endif /* !defined(STRIP_ALL) */
diff --git a/src/gpu/effect.h b/src/gpu/effect.h
index 0cc9de5..006ee6f 100644
--- a/src/gpu/effect.h
+++ b/src/gpu/effect.h
@@ -9,7 +9,7 @@ class PostProcessEffect;
class Effect {
public:
- Effect(WGPUDevice device, WGPUQueue queue) : device_(device), queue_(queue) {
+ Effect(const GpuContext& ctx) : device_(ctx.device), queue_(ctx.queue), format_(ctx.format) {
}
virtual ~Effect() = default;
virtual void init(MainSequence* demo) {
@@ -42,6 +42,7 @@ class Effect {
protected:
WGPUDevice device_;
WGPUQueue queue_;
+ WGPUTextureFormat format_;
GpuBuffer uniforms_;
int width_ = 1280;
int height_ = 720;
@@ -49,8 +50,8 @@ class Effect {
class PostProcessEffect : public Effect {
public:
- PostProcessEffect(WGPUDevice device, WGPUQueue queue)
- : Effect(device, queue) {
+ PostProcessEffect(const GpuContext& ctx)
+ : Effect(ctx) {
}
bool is_post_process() const override {
return true;
@@ -103,13 +104,10 @@ class MainSequence {
public:
MainSequence();
~MainSequence();
- WGPUDevice device;
- WGPUQueue queue;
- WGPUTextureFormat format;
+ GpuContext gpu_ctx;
- void init(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format,
- int width, int height);
- void init_test(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ void init(const GpuContext& ctx, int width, int height);
+ void init_test(const GpuContext& ctx);
void add_sequence(std::shared_ptr<Sequence> seq, float start_time,
int priority = 0);
void render_frame(float global_time, float beat, float peak,
diff --git a/src/gpu/effects/chroma_aberration_effect.cc b/src/gpu/effects/chroma_aberration_effect.cc
index 6e64988..83c5f2a 100644
--- a/src/gpu/effects/chroma_aberration_effect.cc
+++ b/src/gpu/effects/chroma_aberration_effect.cc
@@ -5,14 +5,12 @@
#include "gpu/gpu.h"
// --- ChromaAberrationEffect ---
-ChromaAberrationEffect::ChromaAberrationEffect(WGPUDevice device,
- WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+ChromaAberrationEffect::ChromaAberrationEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 6,
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 0d4bb36..abaa2e7 100644
--- a/src/gpu/effects/distort_effect.cc
+++ b/src/gpu/effects/distort_effect.cc
@@ -5,14 +5,13 @@
#include "gpu/gpu.h"
// --- DistortEffect ---
-DistortEffect::DistortEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+DistortEffect::DistortEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 6,
WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
pipeline_ =
- create_post_process_pipeline(device_, format, distort_shader_wgsl);
+ 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/fade_effect.cc b/src/gpu/effects/fade_effect.cc
index 4d7633c..dce2360 100644
--- a/src/gpu/effects/fade_effect.cc
+++ b/src/gpu/effects/fade_effect.cc
@@ -5,9 +5,8 @@
#include "gpu/effects/post_process_helper.h"
#include <cmath>
-FadeEffect::FadeEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+FadeEffect::FadeEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
const char* shader_code = R"(
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@@ -46,9 +45,9 @@ FadeEffect::FadeEffect(WGPUDevice device, WGPUQueue queue,
}
)";
- pipeline_ = create_post_process_pipeline(device, format, shader_code);
+ pipeline_ = create_post_process_pipeline(device_, format_, shader_code);
uniforms_ = gpu_create_buffer(
- device, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
+ device_, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
}
void FadeEffect::update_bind_group(WGPUTextureView input_view) {
diff --git a/src/gpu/effects/fade_effect.h b/src/gpu/effects/fade_effect.h
index fc91646..2048c2a 100644
--- a/src/gpu/effects/fade_effect.h
+++ b/src/gpu/effects/fade_effect.h
@@ -8,7 +8,7 @@
class FadeEffect : public PostProcessEffect {
public:
- FadeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ FadeEffect(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;
diff --git a/src/gpu/effects/flash_cube_effect.cc b/src/gpu/effects/flash_cube_effect.cc
index 75c71e1..4f58562 100644
--- a/src/gpu/effects/flash_cube_effect.cc
+++ b/src/gpu/effects/flash_cube_effect.cc
@@ -8,10 +8,8 @@
#include <cmath>
#include <iostream>
-FlashCubeEffect::FlashCubeEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : Effect(device, queue) {
- (void)format;
+FlashCubeEffect::FlashCubeEffect(const GpuContext& ctx)
+ : Effect(ctx) {
}
void FlashCubeEffect::resize(int width, int height) {
@@ -22,9 +20,9 @@ void FlashCubeEffect::resize(int width, int height) {
void FlashCubeEffect::init(MainSequence* demo) {
(void)demo;
- WGPUTextureFormat format = demo->format;
+ WGPUTextureFormat format = demo->gpu_ctx.format;
- renderer_.init(device_, queue_, format);
+ renderer_.init(device_, queue_, format_);
renderer_.resize(width_, height_);
// Texture Manager
diff --git a/src/gpu/effects/flash_cube_effect.h b/src/gpu/effects/flash_cube_effect.h
index 3af13eb..7089af2 100644
--- a/src/gpu/effects/flash_cube_effect.h
+++ b/src/gpu/effects/flash_cube_effect.h
@@ -11,7 +11,7 @@
class FlashCubeEffect : public Effect {
public:
- FlashCubeEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ FlashCubeEffect(const GpuContext& ctx);
void init(MainSequence* demo) override;
void resize(int width, int height) override;
void render(WGPURenderPassEncoder pass, float time, float beat,
diff --git a/src/gpu/effects/flash_effect.cc b/src/gpu/effects/flash_effect.cc
index 176c60c..3dcb48a 100644
--- a/src/gpu/effects/flash_effect.cc
+++ b/src/gpu/effects/flash_effect.cc
@@ -5,9 +5,8 @@
#include "gpu/effects/post_process_helper.h"
#include <cmath>
-FlashEffect::FlashEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+FlashEffect::FlashEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
const char* shader_code = R"(
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@@ -48,9 +47,9 @@ FlashEffect::FlashEffect(WGPUDevice device, WGPUQueue queue,
}
)";
- pipeline_ = create_post_process_pipeline(device, format, shader_code);
+ pipeline_ = create_post_process_pipeline(device_, format_, shader_code);
uniforms_ = gpu_create_buffer(
- device, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
+ device_, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
}
void FlashEffect::update_bind_group(WGPUTextureView input_view) {
diff --git a/src/gpu/effects/flash_effect.h b/src/gpu/effects/flash_effect.h
index 9aa2c67..6be375d 100644
--- a/src/gpu/effects/flash_effect.h
+++ b/src/gpu/effects/flash_effect.h
@@ -8,7 +8,7 @@
class FlashEffect : public PostProcessEffect {
public:
- FlashEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ 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;
diff --git a/src/gpu/effects/gaussian_blur_effect.cc b/src/gpu/effects/gaussian_blur_effect.cc
index ad9bf4b..72a93b8 100644
--- a/src/gpu/effects/gaussian_blur_effect.cc
+++ b/src/gpu/effects/gaussian_blur_effect.cc
@@ -5,14 +5,13 @@
#include "gpu/gpu.h"
// --- GaussianBlurEffect ---
-GaussianBlurEffect::GaussianBlurEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+GaussianBlurEffect::GaussianBlurEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 6,
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 c982912..024f076 100644
--- a/src/gpu/effects/heptagon_effect.cc
+++ b/src/gpu/effects/heptagon_effect.cc
@@ -5,15 +5,14 @@
#include "gpu/gpu.h"
// --- HeptagonEffect ---
-HeptagonEffect::HeptagonEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : Effect(device, queue) {
+HeptagonEffect::HeptagonEffect(const GpuContext& ctx)
+ : Effect(ctx) {
uniforms_ =
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);
+ 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/hybrid_3d_effect.cc b/src/gpu/effects/hybrid_3d_effect.cc
index 8c70c07..6f89bf3 100644
--- a/src/gpu/effects/hybrid_3d_effect.cc
+++ b/src/gpu/effects/hybrid_3d_effect.cc
@@ -8,10 +8,8 @@
#include <cmath>
#include <iostream>
-Hybrid3DEffect::Hybrid3DEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : Effect(device, queue) {
- (void)format; // Passed to base, not directly used here.
+Hybrid3DEffect::Hybrid3DEffect(const GpuContext& ctx)
+ : Effect(ctx) {
}
void Hybrid3DEffect::resize(int width, int height) {
@@ -23,10 +21,10 @@ void Hybrid3DEffect::resize(int width, int height) {
void Hybrid3DEffect::init(MainSequence* demo) {
(void)demo;
WGPUTextureFormat format =
- demo->format; // Get current format from MainSequence (might be different
- // than constructor if resized)
+ demo->gpu_ctx.format; // Get current format from MainSequence (might be different
+ // than constructor if resized)
- renderer_.init(device_, queue_, format);
+ renderer_.init(device_, queue_, format_);
renderer_.resize(width_, height_);
// Texture Manager
diff --git a/src/gpu/effects/hybrid_3d_effect.h b/src/gpu/effects/hybrid_3d_effect.h
index 8e2fef9..3a4e87c 100644
--- a/src/gpu/effects/hybrid_3d_effect.h
+++ b/src/gpu/effects/hybrid_3d_effect.h
@@ -12,7 +12,7 @@
class Hybrid3DEffect : public Effect {
public:
- Hybrid3DEffect(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
+ Hybrid3DEffect(const GpuContext& ctx);
virtual ~Hybrid3DEffect() override = default;
void init(MainSequence* demo) override;
diff --git a/src/gpu/effects/moving_ellipse_effect.cc b/src/gpu/effects/moving_ellipse_effect.cc
index 3b73697..1163215 100644
--- a/src/gpu/effects/moving_ellipse_effect.cc
+++ b/src/gpu/effects/moving_ellipse_effect.cc
@@ -5,15 +5,14 @@
#include "gpu/gpu.h"
// --- MovingEllipseEffect ---
-MovingEllipseEffect::MovingEllipseEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : Effect(device, queue) {
+MovingEllipseEffect::MovingEllipseEffect(const GpuContext& ctx)
+ : Effect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 6,
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 e8ead0a..452017e 100644
--- a/src/gpu/effects/particle_spray_effect.cc
+++ b/src/gpu/effects/particle_spray_effect.cc
@@ -6,9 +6,8 @@
#include <vector>
// --- ParticleSprayEffect ---
-ParticleSprayEffect::ParticleSprayEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : Effect(device, queue) {
+ParticleSprayEffect::ParticleSprayEffect(const GpuContext& ctx)
+ : Effect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 6,
WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
@@ -27,7 +26,7 @@ ParticleSprayEffect::ParticleSprayEffect(WGPUDevice device, WGPUQueue queue,
{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 6218af9..137150a 100644
--- a/src/gpu/effects/particles_effect.cc
+++ b/src/gpu/effects/particles_effect.cc
@@ -6,9 +6,8 @@
#include <vector>
// --- ParticlesEffect ---
-ParticlesEffect::ParticlesEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : Effect(device, queue) {
+ParticlesEffect::ParticlesEffect(const GpuContext& ctx)
+ : Effect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 4,
WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
@@ -25,7 +24,7 @@ ParticlesEffect::ParticlesEffect(WGPUDevice device, WGPUQueue queue,
{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 7825c0a..203f912 100644
--- a/src/gpu/effects/passthrough_effect.cc
+++ b/src/gpu/effects/passthrough_effect.cc
@@ -5,14 +5,13 @@
#include "gpu/gpu.h"
// --- PassthroughEffect ---
-PassthroughEffect::PassthroughEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+PassthroughEffect::PassthroughEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 6,
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) {
struct {
diff --git a/src/gpu/effects/solarize_effect.cc b/src/gpu/effects/solarize_effect.cc
index a0bc971..b6cf6f9 100644
--- a/src/gpu/effects/solarize_effect.cc
+++ b/src/gpu/effects/solarize_effect.cc
@@ -5,14 +5,13 @@
#include "gpu/gpu.h"
// --- SolarizeEffect ---
-SolarizeEffect::SolarizeEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+SolarizeEffect::SolarizeEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
uniforms_ =
gpu_create_buffer(device_, sizeof(float) * 6,
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/gpu/effects/theme_modulation_effect.cc b/src/gpu/effects/theme_modulation_effect.cc
index d2705d5..d0d71cb 100644
--- a/src/gpu/effects/theme_modulation_effect.cc
+++ b/src/gpu/effects/theme_modulation_effect.cc
@@ -6,9 +6,8 @@
#include "gpu/effects/shaders.h"
#include <cmath>
-ThemeModulationEffect::ThemeModulationEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format)
- : PostProcessEffect(device, queue) {
+ThemeModulationEffect::ThemeModulationEffect(const GpuContext& ctx)
+ : PostProcessEffect(ctx) {
const char* shader_code = R"(
struct VertexOutput {
@builtin(position) position: vec4<f32>,
@@ -48,11 +47,11 @@ ThemeModulationEffect::ThemeModulationEffect(WGPUDevice device, WGPUQueue queue,
}
)";
- pipeline_ = create_post_process_pipeline(device, format, shader_code);
+ pipeline_ = create_post_process_pipeline(device_, format_, shader_code);
// Create uniform buffer (4 floats: brightness + padding)
uniforms_ = gpu_create_buffer(
- device, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
+ device_, 16, WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst);
}
void ThemeModulationEffect::update_bind_group(WGPUTextureView input_view) {
diff --git a/src/gpu/effects/theme_modulation_effect.h b/src/gpu/effects/theme_modulation_effect.h
index ac584e2..ad7322e 100644
--- a/src/gpu/effects/theme_modulation_effect.h
+++ b/src/gpu/effects/theme_modulation_effect.h
@@ -8,8 +8,7 @@
class ThemeModulationEffect : public PostProcessEffect {
public:
- ThemeModulationEffect(WGPUDevice device, WGPUQueue queue,
- WGPUTextureFormat format);
+ ThemeModulationEffect(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;
diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc
index 63a30ff..fe71d3e 100644
--- a/src/gpu/gpu.cc
+++ b/src/gpu/gpu.cc
@@ -21,12 +21,13 @@
static WGPUInstance g_instance = nullptr;
static WGPUAdapter g_adapter = nullptr;
-WGPUDevice g_device = nullptr; // Non-static for external access (debug builds)
-WGPUQueue g_queue = nullptr; // Non-static for external access (debug builds)
+static WGPUDevice g_device = nullptr;
+static WGPUQueue g_queue = nullptr;
static WGPUSurface g_surface = nullptr;
static WGPUSurfaceConfiguration g_config = {};
-WGPUTextureFormat g_format = WGPUTextureFormat_BGRA8Unorm; // Exposed for custom effects
+static WGPUTextureFormat g_format = WGPUTextureFormat_BGRA8Unorm;
+static GpuContext g_gpu_context = {};
static MainSequence g_main_sequence;
// --- Helper Functions ---
@@ -355,7 +356,12 @@ void gpu_init(PlatformState* platform_state) {
g_config.device = g_device;
g_config.format = swap_chain_format;
- g_format = swap_chain_format; // Update global format for external access
+ g_format = swap_chain_format;
+
+ // Update GPU context for accessor
+ g_gpu_context.device = g_device;
+ g_gpu_context.queue = g_queue;
+ g_gpu_context.format = g_format;
g_config.usage = WGPUTextureUsage_RenderAttachment;
g_config.width = platform_state->width;
g_config.height = platform_state->height;
@@ -365,10 +371,9 @@ void gpu_init(PlatformState* platform_state) {
InitShaderComposer();
- g_main_sequence.init(g_device, g_queue, g_config.format,
- platform_state->width, platform_state->height);
+ g_main_sequence.init(g_gpu_context, platform_state->width, platform_state->height);
- LoadTimeline(g_main_sequence, g_device, g_queue, g_config.format);
+ LoadTimeline(g_main_sequence, g_gpu_context);
}
void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat) {
@@ -395,6 +400,10 @@ void gpu_add_custom_effect(Effect* effect, float start_time, float end_time, int
seq->init(&g_main_sequence);
g_main_sequence.add_sequence(seq, 0.0f, priority);
}
+
+const GpuContext* gpu_get_context() {
+ return &g_gpu_context;
+}
#endif /* !defined(STRIP_ALL) */
void gpu_shutdown() {
diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h
index b8f58b2..c4d1993 100644
--- a/src/gpu/gpu.h
+++ b/src/gpu/gpu.h
@@ -9,6 +9,13 @@
struct PlatformState; // Forward declaration
class Effect; // Forward declaration
+// GPU context bundling device, queue, and surface format
+struct GpuContext {
+ WGPUDevice device;
+ WGPUQueue queue;
+ WGPUTextureFormat format;
+};
+
// Basic wrapper for WebGPU buffers
struct GpuBuffer {
WGPUBuffer buffer;
@@ -39,10 +46,8 @@ void gpu_resize(int width, int height);
void gpu_simulate_until(float time);
void gpu_add_custom_effect(Effect* effect, float start_time, float end_time, int priority);
-// Expose WebGPU globals for custom effects (debug builds only)
-extern WGPUDevice g_device;
-extern WGPUQueue g_queue;
-extern WGPUTextureFormat g_format;
+// Get GPU context for custom effects (debug builds only)
+const GpuContext* gpu_get_context();
#endif
void gpu_shutdown();