// 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;