-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathGPUDevice.h
More file actions
172 lines (153 loc) · 6.85 KB
/
GPUDevice.h
File metadata and controls
172 lines (153 loc) · 6.85 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once
#include <memory>
#include <optional>
#include <string>
#include <unordered_set>
#include <utility>
#include <variant>
#include "Unions.h"
#include "RNFHybridObject.h"
#include "rnwgpu/async/AsyncRunner.h"
#include "rnwgpu/async/AsyncTaskHandle.h"
#include "webgpu/webgpu_cpp.h"
#include "GPUBindGroup.h"
#include "GPUBindGroupDescriptor.h"
#include "GPUBindGroupLayout.h"
#include "GPUBindGroupLayoutDescriptor.h"
#include "GPUBuffer.h"
#include "GPUBufferDescriptor.h"
#include "GPUCommandEncoder.h"
#include "GPUCommandEncoderDescriptor.h"
#include "GPUComputePipeline.h"
#include "GPUComputePipelineDescriptor.h"
#include "GPUDeviceLostInfo.h"
#include "GPUError.h"
#include "GPUExternalTexture.h"
#include "GPUExternalTextureDescriptor.h"
#include "GPUPipelineLayout.h"
#include "GPUPipelineLayoutDescriptor.h"
#include "GPUQuerySet.h"
#include "GPUQuerySetDescriptor.h"
#include "GPUQueue.h"
#include "GPURenderBundleEncoder.h"
#include "GPURenderBundleEncoderDescriptor.h"
#include "GPURenderPipeline.h"
#include "GPURenderPipelineDescriptor.h"
#include "GPUSampler.h"
#include "GPUSamplerDescriptor.h"
#include "GPUShaderModule.h"
#include "GPUShaderModuleDescriptor.h"
#include "GPUSupportedLimits.h"
#include "GPUTexture.h"
#include "GPUTextureDescriptor.h"
namespace rnwgpu {
namespace m = margelo;
class GPUDevice : public m::HybridObject {
public:
explicit GPUDevice(wgpu::Device instance,
std::shared_ptr<async::AsyncRunner> async,
std::string label)
: HybridObject("GPUDevice"), _instance(instance), _async(async),
_label(label) {}
public:
std::string getBrand() { return _name; }
void destroy();
std::shared_ptr<GPUBuffer>
createBuffer(std::shared_ptr<GPUBufferDescriptor> descriptor);
std::shared_ptr<GPUTexture>
createTexture(std::shared_ptr<GPUTextureDescriptor> descriptor);
std::shared_ptr<GPUSampler> createSampler(
std::optional<std::shared_ptr<GPUSamplerDescriptor>> descriptor);
std::shared_ptr<GPUExternalTexture> importExternalTexture(
std::shared_ptr<GPUExternalTextureDescriptor> descriptor);
std::shared_ptr<GPUBindGroupLayout> createBindGroupLayout(
std::shared_ptr<GPUBindGroupLayoutDescriptor> descriptor);
std::shared_ptr<GPUPipelineLayout>
createPipelineLayout(std::shared_ptr<GPUPipelineLayoutDescriptor> descriptor);
std::shared_ptr<GPUBindGroup>
createBindGroup(std::shared_ptr<GPUBindGroupDescriptor> descriptor);
std::shared_ptr<GPUShaderModule>
createShaderModule(std::shared_ptr<GPUShaderModuleDescriptor> descriptor);
std::shared_ptr<GPUComputePipeline> createComputePipeline(
std::shared_ptr<GPUComputePipelineDescriptor> descriptor);
std::shared_ptr<GPURenderPipeline>
createRenderPipeline(std::shared_ptr<GPURenderPipelineDescriptor> descriptor);
async::AsyncTaskHandle createComputePipelineAsync(
std::shared_ptr<GPUComputePipelineDescriptor> descriptor);
async::AsyncTaskHandle createRenderPipelineAsync(
std::shared_ptr<GPURenderPipelineDescriptor> descriptor);
std::shared_ptr<GPUCommandEncoder> createCommandEncoder(
std::optional<std::shared_ptr<GPUCommandEncoderDescriptor>> descriptor);
std::shared_ptr<GPURenderBundleEncoder> createRenderBundleEncoder(
std::shared_ptr<GPURenderBundleEncoderDescriptor> descriptor);
std::shared_ptr<GPUQuerySet>
createQuerySet(std::shared_ptr<GPUQuerySetDescriptor> descriptor);
void pushErrorScope(wgpu::ErrorFilter filter);
async::AsyncTaskHandle popErrorScope();
std::unordered_set<std::string> getFeatures();
std::shared_ptr<GPUSupportedLimits> getLimits();
std::shared_ptr<GPUQueue> getQueue();
async::AsyncTaskHandle getLost();
void notifyDeviceLost(wgpu::DeviceLostReason reason, std::string message);
void forceLossForTesting();
std::string getLabel() { return _label; }
void setLabel(const std::string &label) {
_label = label;
_instance.SetLabel(_label.c_str());
}
void loadHybridMethods() override {
registerHybridGetter("__brand", &GPUDevice::getBrand, this);
registerHybridMethod("destroy", &GPUDevice::destroy, this);
registerHybridMethod("createBuffer", &GPUDevice::createBuffer, this);
registerHybridMethod("createTexture", &GPUDevice::createTexture, this);
registerHybridMethod("createSampler", &GPUDevice::createSampler, this);
registerHybridMethod("importExternalTexture",
&GPUDevice::importExternalTexture, this);
registerHybridMethod("createBindGroupLayout",
&GPUDevice::createBindGroupLayout, this);
registerHybridMethod("createPipelineLayout",
&GPUDevice::createPipelineLayout, this);
registerHybridMethod("createBindGroup", &GPUDevice::createBindGroup, this);
registerHybridMethod("createShaderModule", &GPUDevice::createShaderModule,
this);
registerHybridMethod("createComputePipeline",
&GPUDevice::createComputePipeline, this);
registerHybridMethod("createRenderPipeline",
&GPUDevice::createRenderPipeline, this);
registerHybridMethod("createComputePipelineAsync",
&GPUDevice::createComputePipelineAsync, this);
registerHybridMethod("createRenderPipelineAsync",
&GPUDevice::createRenderPipelineAsync, this);
registerHybridMethod("createCommandEncoder",
&GPUDevice::createCommandEncoder, this);
registerHybridMethod("createRenderBundleEncoder",
&GPUDevice::createRenderBundleEncoder, this);
registerHybridMethod("createQuerySet", &GPUDevice::createQuerySet, this);
registerHybridMethod("pushErrorScope", &GPUDevice::pushErrorScope, this);
registerHybridMethod("popErrorScope", &GPUDevice::popErrorScope, this);
registerHybridGetter("features", &GPUDevice::getFeatures, this);
registerHybridGetter("limits", &GPUDevice::getLimits, this);
registerHybridGetter("queue", &GPUDevice::getQueue, this);
registerHybridGetter("lost", &GPUDevice::getLost, this);
registerHybridGetter("label", &GPUDevice::getLabel, this);
registerHybridSetter("label", &GPUDevice::setLabel, this);
registerHybridMethod("forceLossForTesting", &GPUDevice::forceLossForTesting,
this);
}
inline const wgpu::Device get() { return _instance; }
size_t getMemoryPressure() override {
// Devices keep large driver heaps, pipeline caches and residency tracking.
constexpr size_t kDeviceFloor = 8 * 1024 * 1024; // 8MB baseline
return kDeviceFloor + _label.capacity();
}
private:
friend class GPUAdapter;
wgpu::Device _instance;
std::shared_ptr<async::AsyncRunner> _async;
std::string _label;
std::optional<async::AsyncTaskHandle> _lostHandle;
std::shared_ptr<GPUDeviceLostInfo> _lostInfo;
bool _lostSettled = false;
std::optional<async::AsyncTaskHandle::ResolveFunction> _lostResolve;
};
} // namespace rnwgpu