summaryrefslogtreecommitdiff
path: root/src/gpu
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-17 12:35:59 +0100
committerskal <pascal.massimino@gmail.com>2026-02-17 12:35:59 +0100
commitb3eded8d56219fa19029a1b9bb7e7e7584f093d9 (patch)
tree1bb2b6f5982892f5776e54fdd14a3976fe1d32ec /src/gpu
parent95dd0ff4c000f3752c5c9112d79de3a4bdaa7b25 (diff)
refactor(effects): Factor shared initialization into Effect base class
Eliminate ~100 lines of duplicated code across effect subclasses by moving common resource initialization to the base Effect class. Most effects repeatedly created uniforms buffers, samplers, and dummy textures with identical configurations. Changes: - Add shared members to Effect: uniforms_buffer_, sampler_, dummy_texture_* - Add helpers: init_uniforms_buffer(), create_*_sampler(), create_dummy_scene_texture() - Add gpu_create_*_sampler() and gpu_create_dummy_scene_texture() to gpu.h - Move HEADLESS_RETURN_IF_NULL to Effect constructor - Update 7 effects to use base class helpers (Flash, Heptagon, Passthrough, Placeholder, GaussianBlur, Particles, PeakMeter) Benefits: Improved consistency, easier maintenance, reduced binary size. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/gpu')
-rw-r--r--src/gpu/effect.cc19
-rw-r--r--src/gpu/effect.h19
-rw-r--r--src/gpu/gpu.cc35
-rw-r--r--src/gpu/gpu.h9
4 files changed, 81 insertions, 1 deletions
diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc
index 752dd84..cd4bc16 100644
--- a/src/gpu/effect.cc
+++ b/src/gpu/effect.cc
@@ -14,6 +14,7 @@ Effect::Effect(const GpuContext& ctx, const std::vector<std::string>& inputs,
FATAL_CHECK(!outputs.empty(), "Effect must have at least one output\n");
FATAL_CHECK(start_time <= end_time, "Invalid time range: %f > %f\n",
start_time, end_time);
+ HEADLESS_RETURN_IF_NULL(ctx_.device);
}
void Effect::dispatch_render(WGPUCommandEncoder encoder,
@@ -55,3 +56,21 @@ void Effect::blit_input_to_output(WGPUCommandEncoder encoder,
wgpuCommandEncoderCopyTextureToTexture(encoder, &src_copy, &dst_copy,
&extent);
}
+
+void Effect::init_uniforms_buffer() {
+ uniforms_buffer_.init(ctx_.device);
+}
+
+void Effect::create_linear_sampler() {
+ sampler_ = gpu_create_linear_sampler(ctx_.device);
+}
+
+void Effect::create_nearest_sampler() {
+ sampler_ = gpu_create_nearest_sampler(ctx_.device);
+}
+
+void Effect::create_dummy_scene_texture() {
+ TextureWithView dummy = gpu_create_dummy_scene_texture(ctx_.device);
+ dummy_texture_ = dummy.texture;
+ dummy_texture_view_ = dummy.view;
+}
diff --git a/src/gpu/effect.h b/src/gpu/effect.h
index f5cdf2d..82aa6e1 100644
--- a/src/gpu/effect.h
+++ b/src/gpu/effect.h
@@ -6,6 +6,7 @@
#include "gpu/gpu.h"
#include "gpu/sequence.h"
+#include "gpu/uniform_helper.h"
#include <string>
#include <vector>
@@ -51,6 +52,24 @@ class Effect {
int width_ = 1280;
int height_ = 720;
+ // Common resources for most effects
+ UniformBuffer<UniformsSequenceParams> uniforms_buffer_;
+ WGPUSampler sampler_ = nullptr;
+ WGPUTexture dummy_texture_ = nullptr;
+ WGPUTextureView dummy_texture_view_ = nullptr;
+
+ // Helper: Initialize uniforms buffer (call in subclass constructor)
+ void init_uniforms_buffer();
+
+ // Helper: Create linear sampler (call in subclass constructor)
+ void create_linear_sampler();
+
+ // Helper: Create nearest sampler (call in subclass constructor)
+ void create_nearest_sampler();
+
+ // Helper: Create dummy texture for scene effects (call in subclass constructor)
+ void create_dummy_scene_texture();
+
private:
float start_time_;
float end_time_;
diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc
index 2226889..e743bf7 100644
--- a/src/gpu/gpu.cc
+++ b/src/gpu/gpu.cc
@@ -124,6 +124,41 @@ WGPUTextureView gpu_create_texture_view_2d(WGPUTexture texture,
return wgpuTextureCreateView(texture, &view_desc);
}
+WGPUSampler gpu_create_linear_sampler(WGPUDevice device) {
+ WGPUSamplerDescriptor desc = {};
+ desc.addressModeU = WGPUAddressMode_ClampToEdge;
+ desc.addressModeV = WGPUAddressMode_ClampToEdge;
+ desc.addressModeW = WGPUAddressMode_ClampToEdge;
+ desc.magFilter = WGPUFilterMode_Linear;
+ desc.minFilter = WGPUFilterMode_Linear;
+ desc.mipmapFilter = WGPUMipmapFilterMode_Nearest;
+ desc.maxAnisotropy = 1;
+ return wgpuDeviceCreateSampler(device, &desc);
+}
+
+WGPUSampler gpu_create_nearest_sampler(WGPUDevice device) {
+ WGPUSamplerDescriptor desc = {};
+ desc.addressModeU = WGPUAddressMode_ClampToEdge;
+ desc.addressModeV = WGPUAddressMode_ClampToEdge;
+ desc.magFilter = WGPUFilterMode_Nearest;
+ desc.minFilter = WGPUFilterMode_Nearest;
+ desc.maxAnisotropy = 1;
+ return wgpuDeviceCreateSampler(device, &desc);
+}
+
+TextureWithView gpu_create_dummy_scene_texture(WGPUDevice device) {
+ WGPUTextureDescriptor desc = {};
+ desc.size = {1, 1, 1};
+ desc.format = WGPUTextureFormat_RGBA8Unorm;
+ desc.usage = WGPUTextureUsage_TextureBinding;
+ desc.dimension = WGPUTextureDimension_2D;
+ desc.mipLevelCount = 1;
+ desc.sampleCount = 1;
+ WGPUTexture texture = wgpuDeviceCreateTexture(device, &desc);
+ WGPUTextureView view = wgpuTextureCreateView(texture, nullptr);
+ return {texture, view};
+}
+
RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format,
const char* shader_code,
ResourceBinding* bindings, int num_bindings) {
diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h
index 9a3fd38..d6c0255 100644
--- a/src/gpu/gpu.h
+++ b/src/gpu/gpu.h
@@ -97,4 +97,11 @@ RenderPass
gpu_create_render_pass(WGPUDevice device,
WGPUTextureFormat format, // Needed for render pipeline
const char* shader_code, ResourceBinding* bindings,
- int num_bindings); \ No newline at end of file
+ int num_bindings);
+
+// Common sampler configurations
+WGPUSampler gpu_create_linear_sampler(WGPUDevice device);
+WGPUSampler gpu_create_nearest_sampler(WGPUDevice device);
+
+// Dummy 1x1 texture for scene effects (don't need texture input)
+TextureWithView gpu_create_dummy_scene_texture(WGPUDevice device); \ No newline at end of file