blob: e12aa4528229b1acb471f042ea00ba489bd8aca6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// 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 <typename T, void (*Release)(T)> 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<WGPUBindGroup, wgpuBindGroupRelease>;
using RenderPipeline =
WGPUResource<WGPURenderPipeline, wgpuRenderPipelineRelease>;
using ComputePipeline =
WGPUResource<WGPUComputePipeline, wgpuComputePipelineRelease>;
using Sampler = WGPUResource<WGPUSampler, wgpuSamplerRelease>;
using Texture = WGPUResource<WGPUTexture, wgpuTextureRelease>;
using TextureView = WGPUResource<WGPUTextureView, wgpuTextureViewRelease>;
using Buffer = WGPUResource<WGPUBuffer, wgpuBufferRelease>;
|