From 2e2e01048da5c466102bb57d64148aff72f4a558 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 17 Feb 2026 12:51:29 +0100 Subject: refactor(gpu): Add RAII wrapper for WGPU resources to eliminate manual cleanup Introduces WGPUResource template with automatic release on destruction. Reduces boilerplate in effect destructors and prevents resource leaks. - set() for one-time initialization - replace() for per-frame recreation - Field ordering documented for dependency management Converted 3 effects (Heptagon, Flash, Passthrough) and Effect base class. All tests pass (34/34). Co-Authored-By: Claude Sonnet 4.5 --- src/gpu/wgpu_resource.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/gpu/wgpu_resource.h (limited to 'src/gpu/wgpu_resource.h') diff --git a/src/gpu/wgpu_resource.h b/src/gpu/wgpu_resource.h new file mode 100644 index 0000000..e448b18 --- /dev/null +++ b/src/gpu/wgpu_resource.h @@ -0,0 +1,45 @@ +// WGPU Resource RAII Wrapper +// Automatic release on destruction +// +// IMPORTANT: Field ordering matters for destruction order. +// Members are destroyed in reverse declaration order. +// Declare dependencies before dependents: +// RenderPipeline pipeline_; // Destroyed last +// BindGroup bind_group_; // Destroyed first (may reference pipeline) + +#pragma once +#include "platform/platform.h" +#include "util/fatal_error.h" + +template +class WGPUResource { + public: + WGPUResource() : ptr_(nullptr) {} + ~WGPUResource() { if (ptr_) Release(ptr_); } + + void set(T ptr) { + FATAL_ASSERT(ptr_ == nullptr); + ptr_ = ptr; + } + + void replace(T ptr) { + if (ptr_) Release(ptr_); + ptr_ = ptr; + } + + T get() const { return ptr_; } + T* get_address() { return &ptr_; } + + private: + T ptr_; + WGPUResource(const WGPUResource&) = delete; + WGPUResource& operator=(const WGPUResource&) = delete; +}; + +using BindGroup = WGPUResource; +using RenderPipeline = WGPUResource; +using ComputePipeline = WGPUResource; +using Sampler = WGPUResource; +using Texture = WGPUResource; +using TextureView = WGPUResource; +using Buffer = WGPUResource; -- cgit v1.2.3