diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-11 11:46:53 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-11 11:46:53 +0100 |
| commit | c809790015b4ddc354c6b1993a633eddd042930f (patch) | |
| tree | cb89e52e0c3b804b62544941e5cfc9b7d9ffc0c1 | |
| parent | 5fb1bcc25fef7388a2d68be6c1f062bcf996fd85 (diff) | |
refactor: Use lazy initialization for auxiliary textures
Prevents init/resize ordering bug and avoids unnecessary reallocation.
Changes:
- Auxiliary textures created on first use (compute/update_bind_group)
- Added ensure_texture() methods to defer registration until resize()
- Added early return in resize() if dimensions unchanged
- Removed texture registration from init() methods
Benefits:
- No reallocation on window resize if dimensions match
- Texture created with correct dimensions from start
- Memory saved if effect never renders
Tests: All 36 tests passing
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
| -rw-r--r-- | src/gpu/effects/circle_mask_effect.cc | 19 | ||||
| -rw-r--r-- | src/gpu/effects/circle_mask_effect.h | 3 | ||||
| -rw-r--r-- | src/gpu/effects/cnn_effect.cc | 19 | ||||
| -rw-r--r-- | src/gpu/effects/cnn_effect.h | 3 |
4 files changed, 32 insertions, 12 deletions
diff --git a/src/gpu/effects/circle_mask_effect.cc b/src/gpu/effects/circle_mask_effect.cc index aa9f324..08a9e33 100644 --- a/src/gpu/effects/circle_mask_effect.cc +++ b/src/gpu/effects/circle_mask_effect.cc @@ -26,11 +26,6 @@ CircleMaskEffect::~CircleMaskEffect() { void CircleMaskEffect::init(MainSequence* demo) { demo_ = demo; - const uint32_t width = width_; - const uint32_t height = height_; - - demo_->register_auxiliary_texture("circle_mask", width, height); - compute_params_.init(ctx_.device); // Initialize uniforms BEFORE bind group creation @@ -157,10 +152,20 @@ void CircleMaskEffect::init(MainSequence* demo) { render_bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &render_bg_desc); } +void CircleMaskEffect::ensure_texture() { + if (!texture_initialized_ && demo_) { + demo_->register_auxiliary_texture("circle_mask", width_, height_); + texture_initialized_ = true; + } +} + void CircleMaskEffect::resize(int width, int height) { + if (width == width_ && height == height_) + return; + Effect::resize(width, height); - if (!demo_) + if (!demo_ || !texture_initialized_) return; // Resize auxiliary texture @@ -188,6 +193,8 @@ void CircleMaskEffect::resize(int width, int height) { void CircleMaskEffect::compute(WGPUCommandEncoder encoder, const CommonPostProcessUniforms& uniforms) { + ensure_texture(); + uniforms_.update(ctx_.queue, uniforms); const CircleMaskParams params = { diff --git a/src/gpu/effects/circle_mask_effect.h b/src/gpu/effects/circle_mask_effect.h index 6ebaca1..91443d8 100644 --- a/src/gpu/effects/circle_mask_effect.h +++ b/src/gpu/effects/circle_mask_effect.h @@ -29,8 +29,11 @@ class CircleMaskEffect : public Effect { static_assert(sizeof(CircleMaskParams) == 16, "CircleMaskParams must be 16 bytes for WGSL alignment"); + void ensure_texture(); + MainSequence* demo_ = nullptr; float radius_; + bool texture_initialized_ = false; WGPURenderPipeline compute_pipeline_ = nullptr; WGPUBindGroup compute_bind_group_ = nullptr; diff --git a/src/gpu/effects/cnn_effect.cc b/src/gpu/effects/cnn_effect.cc index 84eb530..8673127 100644 --- a/src/gpu/effects/cnn_effect.cc +++ b/src/gpu/effects/cnn_effect.cc @@ -56,20 +56,25 @@ void CNNEffect::init(MainSequence* demo) { // Initialize uniforms BEFORE any bind group creation uniforms_.update(ctx_.queue, get_common_uniforms()); - // Register captured_frame texture (used by all layers for original input) - if (layer_index_ == 0) { - demo_->register_auxiliary_texture("captured_frame", width_, height_); - } - CNNLayerParams params = {layer_index_, blend_amount_, {0.0f, 0.0f}}; params_buffer_.update(ctx_.queue, params); } +void CNNEffect::ensure_texture() { + if (!texture_initialized_ && layer_index_ == 0 && demo_) { + demo_->register_auxiliary_texture("captured_frame", width_, height_); + texture_initialized_ = true; + } +} + void CNNEffect::resize(int width, int height) { + if (width == width_ && height == height_) + return; + PostProcessEffect::resize(width, height); // Only layer 0 owns the captured_frame texture - if (layer_index_ == 0 && demo_) { + if (layer_index_ == 0 && demo_ && texture_initialized_) { demo_->resize_auxiliary_texture("captured_frame", width, height); } } @@ -88,6 +93,8 @@ void CNNEffect::render(WGPURenderPassEncoder pass, } void CNNEffect::update_bind_group(WGPUTextureView input_view) { + ensure_texture(); + input_view_ = input_view; // Update common uniforms (CRITICAL for UV calculation!) diff --git a/src/gpu/effects/cnn_effect.h b/src/gpu/effects/cnn_effect.h index 1c9f0f3..f79fac7 100644 --- a/src/gpu/effects/cnn_effect.h +++ b/src/gpu/effects/cnn_effect.h @@ -35,9 +35,12 @@ class CNNEffect : public PostProcessEffect { } private: + void ensure_texture(); + int layer_index_; int total_layers_; float blend_amount_; + bool texture_initialized_ = false; WGPUTextureView input_view_; WGPUTextureView original_view_; UniformBuffer<CNNLayerParams> params_buffer_; |
