-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathWebGPUDevice.cpp
More file actions
218 lines (193 loc) · 5.65 KB
/
Copy pathWebGPUDevice.cpp
File metadata and controls
218 lines (193 loc) · 5.65 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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/webgpu/runtime/WebGPUCompat.h>
#include <executorch/backends/webgpu/runtime/WebGPUDevice.h>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <stdexcept>
namespace executorch {
namespace backends {
namespace webgpu {
namespace {
struct AdapterResult {
WGPUAdapter adapter = nullptr;
bool done = false;
};
struct DeviceResult {
WGPUDevice device = nullptr;
bool done = false;
};
void on_adapter_request(
WGPURequestAdapterStatus status,
WGPUAdapter adapter,
WGPUStringView message,
void* userdata1,
void* /*userdata2*/) {
auto* result = static_cast<AdapterResult*>(userdata1);
if (status == WGPURequestAdapterStatus_Success) {
result->adapter = adapter;
} else {
fprintf(
stderr,
"WebGPU adapter request failed (status %d): %.*s\n",
static_cast<int>(status),
static_cast<int>(message.length),
message.data);
}
result->done = true;
}
void on_device_request(
WGPURequestDeviceStatus status,
WGPUDevice device,
WGPUStringView message,
void* userdata1,
void* /*userdata2*/) {
auto* result = static_cast<DeviceResult*>(userdata1);
if (status == WGPURequestDeviceStatus_Success) {
result->device = device;
} else {
fprintf(
stderr,
"WebGPU device request failed (status %d): %.*s\n",
static_cast<int>(status),
static_cast<int>(message.length),
message.data);
}
result->done = true;
}
void on_device_error(
WGPUDevice const* /*device*/,
WGPUErrorType type,
WGPUStringView message,
void* /*userdata1*/,
void* /*userdata2*/) {
fprintf(
stderr,
"WebGPU device error (type %d): %.*s\n",
static_cast<int>(type),
static_cast<int>(message.length),
message.data);
}
} // namespace
WebGPUContext create_webgpu_context() {
WebGPUContext ctx;
ctx.instance = wgpuCreateInstance(nullptr);
if (!ctx.instance) {
throw std::runtime_error("Failed to create WebGPU instance");
}
// Request adapter using AllowSpontaneous mode (fires during
// wgpuInstanceProcessEvents or any other API call).
AdapterResult adapter_result;
WGPURequestAdapterCallbackInfo adapter_cb = {};
adapter_cb.mode = WGPUCallbackMode_AllowSpontaneous;
adapter_cb.callback = on_adapter_request;
adapter_cb.userdata1 = &adapter_result;
// Release Dawn has no bundled fallback adapter; pick the platform backend
// (Metal on Apple, Vulkan elsewhere -- SwiftShader via VK_ICD_FILENAMES).
WGPURequestAdapterOptions adapter_opts = {};
#if defined(__APPLE__)
adapter_opts.backendType = WGPUBackendType_Metal;
#else
adapter_opts.backendType = WGPUBackendType_Vulkan;
#endif
adapter_opts.forceFallbackAdapter = false;
wgpuInstanceRequestAdapter(ctx.instance, &adapter_opts, adapter_cb);
while (!adapter_result.done) {
webgpu_poll(ctx.instance);
}
if (!adapter_result.adapter) {
wgpuInstanceRelease(ctx.instance);
ctx.instance = nullptr;
throw std::runtime_error(
"Failed to get WebGPU adapter. "
"Ensure a GPU with Vulkan (Linux) or Metal (macOS) is available.");
}
ctx.adapter = adapter_result.adapter;
// Request device
DeviceResult device_result;
WGPURequestDeviceCallbackInfo device_cb = {};
device_cb.mode = WGPUCallbackMode_AllowSpontaneous;
device_cb.callback = on_device_request;
device_cb.userdata1 = &device_result;
// Request the adapter's full limits; software adapters default many to 0.
WGPULimits supported_limits = {};
WGPUDeviceDescriptor device_desc = {};
if (wgpuAdapterGetLimits(ctx.adapter, &supported_limits) ==
WGPUStatus_Success) {
device_desc.requiredLimits = &supported_limits;
}
device_desc.uncapturedErrorCallbackInfo.callback = on_device_error;
wgpuAdapterRequestDevice(ctx.adapter, &device_desc, device_cb);
while (!device_result.done) {
webgpu_poll(ctx.instance);
}
if (!device_result.device) {
wgpuAdapterRelease(ctx.adapter);
wgpuInstanceRelease(ctx.instance);
ctx.adapter = nullptr;
ctx.instance = nullptr;
throw std::runtime_error("Failed to get WebGPU device");
}
ctx.device = device_result.device;
ctx.queue = wgpuDeviceGetQueue(ctx.device);
return ctx;
}
namespace {
WebGPUContext* g_default_context = nullptr;
} // namespace
void set_default_webgpu_context(WebGPUContext* ctx) {
g_default_context = ctx;
}
WebGPUContext* get_default_webgpu_context() {
if (g_default_context) {
return g_default_context;
}
#if !defined(__EMSCRIPTEN__)
// Native-only lazy process-wide context, mirroring Vulkan api::context().
static const std::unique_ptr<WebGPUContext, void (*)(WebGPUContext*)>
lazy_context(
[]() -> WebGPUContext* {
try {
return new WebGPUContext(create_webgpu_context());
} catch (...) {
return nullptr;
}
}(),
[](WebGPUContext* c) {
if (c) {
destroy_webgpu_context(*c);
delete c;
}
});
return lazy_context.get();
#else
return nullptr;
#endif
}
void destroy_webgpu_context(WebGPUContext& ctx) {
if (ctx.queue) {
wgpuQueueRelease(ctx.queue);
ctx.queue = nullptr;
}
if (ctx.device) {
wgpuDeviceRelease(ctx.device);
ctx.device = nullptr;
}
if (ctx.adapter) {
wgpuAdapterRelease(ctx.adapter);
ctx.adapter = nullptr;
}
if (ctx.instance) {
wgpuInstanceRelease(ctx.instance);
ctx.instance = nullptr;
}
}
} // namespace webgpu
} // namespace backends
} // namespace executorch