-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathGPUDevice.cpp
More file actions
363 lines (330 loc) · 13.1 KB
/
GPUDevice.cpp
File metadata and controls
363 lines (330 loc) · 13.1 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "GPUDevice.h"
#include <memory>
#include <string>
#include <unordered_set>
#include <vector>
#include "Convertors.h"
#include "GPUFeatures.h"
namespace rnwgpu {
std::shared_ptr<GPUBuffer>
GPUDevice::createBuffer(std::shared_ptr<GPUBufferDescriptor> descriptor) {
wgpu::BufferDescriptor desc;
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error(
"GPUDevice::createBuffer(): Error with GPUBufferDescriptor");
}
auto result = _instance.CreateBuffer(&desc);
return std::make_shared<GPUBuffer>(result, _async,
descriptor->label.value_or(""));
}
std::shared_ptr<GPUSupportedLimits> GPUDevice::getLimits() {
wgpu::Limits limits{};
if (!_instance.GetLimits(&limits)) {
throw std::runtime_error("failed to get device limits");
}
return std::make_shared<GPUSupportedLimits>(limits);
}
std::shared_ptr<GPUQueue> GPUDevice::getQueue() {
auto result = _instance.GetQueue();
return std::make_shared<GPUQueue>(result, _async, _label);
}
std::shared_ptr<GPUCommandEncoder> GPUDevice::createCommandEncoder(
std::optional<std::shared_ptr<GPUCommandEncoderDescriptor>> descriptor) {
wgpu::CommandEncoderDescriptor desc;
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error("Error with GPUCommandEncoderDescriptor");
}
auto result = _instance.CreateCommandEncoder(&desc);
return std::make_shared<GPUCommandEncoder>(
result,
descriptor.has_value() ? descriptor.value()->label.value_or("") : "");
}
void GPUDevice::destroy() {
_instance.Destroy();
auto lostInfo = std::make_shared<GPUDeviceLostInfo>(
wgpu::DeviceLostReason::Destroyed, "device was destroyed");
m_lostPromise->set_value(lostInfo);
}
std::shared_ptr<GPUTexture>
GPUDevice::createTexture(std::shared_ptr<GPUTextureDescriptor> descriptor) {
wgpu::TextureDescriptor desc;
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error("Error with GPUTextureDescriptor");
}
auto texture = _instance.CreateTexture(&desc);
return std::make_shared<GPUTexture>(texture, descriptor->label.value_or(""));
}
std::shared_ptr<GPUShaderModule> GPUDevice::createShaderModule(
std::shared_ptr<GPUShaderModuleDescriptor> descriptor) {
wgpu::ShaderSourceWGSL wgsl_desc{};
wgpu::ShaderModuleDescriptor sm_desc{};
Convertor conv;
if (!conv(wgsl_desc.code, descriptor->code) ||
!conv(sm_desc.label, descriptor->label)) {
return {};
}
sm_desc.nextInChain = &wgsl_desc;
if (descriptor->code.find('\0') != std::string::npos) {
auto mod = _instance.CreateErrorShaderModule(
&sm_desc, "The WGSL shader contains an illegal character '\\0'");
return std::make_shared<GPUShaderModule>(mod, _async, sm_desc.label.data);
}
auto module = _instance.CreateShaderModule(&sm_desc);
return std::make_shared<GPUShaderModule>(module, _async,
descriptor->label.value_or(""));
}
std::shared_ptr<GPURenderPipeline> GPUDevice::createRenderPipeline(
std::shared_ptr<GPURenderPipelineDescriptor> descriptor) {
wgpu::RenderPipelineDescriptor desc{};
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error("Error with GPURenderPipelineDescriptor");
}
// assert(desc.fragment != nullptr && "Fragment state must not be null");
auto renderPipeline = _instance.CreateRenderPipeline(&desc);
return std::make_shared<GPURenderPipeline>(renderPipeline,
descriptor->label.value_or(""));
}
std::shared_ptr<GPUBindGroup>
GPUDevice::createBindGroup(std::shared_ptr<GPUBindGroupDescriptor> descriptor) {
Convertor conv;
wgpu::BindGroupDescriptor desc{};
if (!conv(desc.label, descriptor->label) ||
!conv(desc.layout, descriptor->layout) ||
!conv(desc.entries, desc.entryCount, descriptor->entries)) {
throw std::runtime_error(
"GPUBindGroup::createBindGroup(): Error with GPUBindGroupDescriptor");
}
auto bindGroup = _instance.CreateBindGroup(&desc);
return std::make_shared<GPUBindGroup>(bindGroup,
descriptor->label.value_or(""));
}
std::shared_ptr<GPUSampler> GPUDevice::createSampler(
std::optional<std::shared_ptr<GPUSamplerDescriptor>> descriptor) {
wgpu::SamplerDescriptor desc;
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error("GPUDevice::createSampler(): Error with "
"GPUSamplerDescriptor");
}
auto sampler = _instance.CreateSampler(&desc);
return std::make_shared<GPUSampler>(
sampler,
descriptor.has_value() ? descriptor.value()->label.value_or("") : "");
}
std::shared_ptr<GPUComputePipeline> GPUDevice::createComputePipeline(
std::shared_ptr<GPUComputePipelineDescriptor> descriptor) {
wgpu::ComputePipelineDescriptor desc;
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error("GPUDevice::createComputePipeline(): Error with "
"GPUComputePipelineDescriptor");
}
auto computePipeline = _instance.CreateComputePipeline(&desc);
return std::make_shared<GPUComputePipeline>(computePipeline,
descriptor->label.value_or(""));
}
std::shared_ptr<GPUQuerySet>
GPUDevice::createQuerySet(std::shared_ptr<GPUQuerySetDescriptor> descriptor) {
wgpu::QuerySetDescriptor desc;
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error("GPUDevice::createQuerySet(): Error with "
"GPUQuerySetDescriptor");
}
auto querySet = _instance.CreateQuerySet(&desc);
return std::make_shared<GPUQuerySet>(querySet,
descriptor->label.value_or(""));
}
std::shared_ptr<GPURenderBundleEncoder> GPUDevice::createRenderBundleEncoder(
std::shared_ptr<GPURenderBundleEncoderDescriptor> descriptor) {
Convertor conv;
wgpu::RenderBundleEncoderDescriptor desc{};
if (!conv(desc.label, descriptor->label) ||
!conv(desc.colorFormats, desc.colorFormatCount,
descriptor->colorFormats) ||
!conv(desc.depthStencilFormat, descriptor->depthStencilFormat) ||
!conv(desc.sampleCount, descriptor->sampleCount) ||
!conv(desc.depthReadOnly, descriptor->depthReadOnly) ||
!conv(desc.stencilReadOnly, descriptor->stencilReadOnly)) {
return {};
}
return std::make_shared<GPURenderBundleEncoder>(
_instance.CreateRenderBundleEncoder(&desc),
descriptor->label.value_or(""));
}
std::shared_ptr<GPUBindGroupLayout> GPUDevice::createBindGroupLayout(
std::shared_ptr<GPUBindGroupLayoutDescriptor> descriptor) {
Convertor conv;
wgpu::BindGroupLayoutDescriptor desc{};
if (!conv(desc.label, descriptor->label) ||
!conv(desc.entries, desc.entryCount, descriptor->entries)) {
return {};
}
return std::make_shared<GPUBindGroupLayout>(
_instance.CreateBindGroupLayout(&desc), descriptor->label.value_or(""));
}
std::shared_ptr<GPUPipelineLayout> GPUDevice::createPipelineLayout(
std::shared_ptr<GPUPipelineLayoutDescriptor> descriptor) {
Convertor conv;
wgpu::PipelineLayoutDescriptor desc{};
if (!conv(desc.label, descriptor->label) ||
!conv(desc.bindGroupLayouts, desc.bindGroupLayoutCount,
descriptor->bindGroupLayouts)) {
return {};
}
return std::make_shared<GPUPipelineLayout>(
_instance.CreatePipelineLayout(&desc), descriptor->label.value_or(""));
}
std::shared_ptr<GPUExternalTexture> GPUDevice::importExternalTexture(
std::shared_ptr<GPUExternalTextureDescriptor> descriptor) {
throw std::runtime_error(
"GPUDevice::importExternalTexture(): Not implemented");
}
std::future<std::shared_ptr<GPUComputePipeline>>
GPUDevice::createComputePipelineAsync(
std::shared_ptr<GPUComputePipelineDescriptor> descriptor) {
return _async->runAsync([=](wgpu::Instance *instance) {
wgpu::ComputePipelineDescriptor desc{};
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error("GPUDevice::createComputePipeline(): Error with "
"GPUComputePipelineDescriptor");
}
wgpu::ComputePipeline computePipeline = nullptr;
auto label = std::string(
descriptor->label.has_value() ? descriptor->label.value() : "");
auto result = std::make_shared<GPUComputePipeline>(computePipeline, label);
auto future = _instance.CreateComputePipelineAsync(
&desc, wgpu::CallbackMode::WaitAnyOnly,
[&result](wgpu::CreatePipelineAsyncStatus status,
wgpu::ComputePipeline pipeline, char const *msg) {
switch (status) {
case wgpu::CreatePipelineAsyncStatus::Success:
result->_instance = pipeline;
break;
default:
throw std::runtime_error(msg);
break;
}
});
instance->WaitAny(future, UINT64_MAX);
return result;
});
}
std::future<std::shared_ptr<GPURenderPipeline>>
GPUDevice::createRenderPipelineAsync(
std::shared_ptr<GPURenderPipelineDescriptor> descriptor) {
return _async->runAsync([=](wgpu::Instance *instance) {
wgpu::RenderPipelineDescriptor desc{};
Convertor conv;
if (!conv(desc, descriptor)) {
throw std::runtime_error(
"GPUDevice::createRenderPipelineAsync(): Error with "
"GPURenderPipelineDescriptor");
}
wgpu::RenderPipeline renderPipeline = nullptr;
auto label = std::string(
descriptor->label.has_value() ? descriptor->label.value() : "");
auto result = std::make_shared<GPURenderPipeline>(renderPipeline, label);
auto future = _instance.CreateRenderPipelineAsync(
&desc, wgpu::CallbackMode::WaitAnyOnly,
[&result](wgpu::CreatePipelineAsyncStatus status,
wgpu::RenderPipeline pipeline, char const *msg) {
switch (status) {
case wgpu::CreatePipelineAsyncStatus::Success:
result->_instance = pipeline;
break;
default:
throw std::runtime_error(msg);
break;
}
});
instance->WaitAny(future, UINT64_MAX);
return result;
});
}
void GPUDevice::pushErrorScope(wgpu::ErrorFilter filter) {
_instance.PushErrorScope(filter);
}
std::future<std::variant<std::nullptr_t, std::shared_ptr<GPUError>>>
GPUDevice::popErrorScope() {
// Create a promise to return a future, but do the work synchronously on main
// thread
auto promise = std::make_shared<
std::promise<std::variant<std::nullptr_t, std::shared_ptr<GPUError>>>>();
auto future = promise->get_future();
std::variant<std::nullptr_t, std::shared_ptr<GPUError>> result = nullptr;
auto wgpu_future = _instance.PopErrorScope(
wgpu::CallbackMode::WaitAnyOnly,
[&result](wgpu::PopErrorScopeStatus status, wgpu::ErrorType type,
wgpu::StringView message) {
switch (status) {
case wgpu::PopErrorScopeStatus::Error:
// PopErrorScope itself failed, e.g. the error scope stack was empty.
return;
case wgpu::PopErrorScopeStatus::CallbackCancelled:
// The instance has been dropped. Shouldn't happen except maybe during
// shutdown.
return;
case wgpu::PopErrorScopeStatus::Success:
// This is the only case where `type` is set to a meaningful value.
break;
}
switch (type) {
case wgpu::ErrorType::NoError:
break;
case wgpu::ErrorType::OutOfMemory: {
result = std::make_shared<GPUError>(wgpu::ErrorType::OutOfMemory,
std::string(message));
break;
}
case wgpu::ErrorType::Validation: {
result = std::make_shared<GPUError>(wgpu::ErrorType::Validation,
std::string(message));
break;
}
case wgpu::ErrorType::Internal: {
result = std::make_shared<GPUError>(wgpu::ErrorType::Internal,
std::string(message));
break;
}
case wgpu::ErrorType::Unknown:
result = std::make_shared<GPUError>(wgpu::ErrorType::Unknown,
std::string(message));
break;
default:
throw std::runtime_error(
"unhandled error type (" +
std::to_string(
static_cast<std::underlying_type<wgpu::ErrorType>::type>(
type)) +
")");
break;
}
});
// Wait synchronously on main thread - both push and pop now on same thread
_async->instance->WaitAny(wgpu_future, UINT64_MAX);
promise->set_value(result);
return future;
}
std::unordered_set<std::string> GPUDevice::getFeatures() {
wgpu::SupportedFeatures supportedFeatures;
_instance.GetFeatures(&supportedFeatures);
std::unordered_set<std::string> result;
for (size_t i = 0; i < supportedFeatures.featureCount; ++i) {
auto feature = supportedFeatures.features[i];
std::string name;
convertEnumToJSUnion(feature, &name);
result.insert(name);
}
return result;
}
std::future<std::shared_ptr<GPUDeviceLostInfo>> GPUDevice::getLost() {
return std::async(std::launch::async,
[=]() { return m_lostSharedFuture->get(); });
}
} // namespace rnwgpu