-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgpu-utils.cpp
More file actions
161 lines (137 loc) · 5.45 KB
/
webgpu-utils.cpp
File metadata and controls
161 lines (137 loc) · 5.45 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
#include "webgpu-utils.h"
#ifdef __EMSCRIPTEN__
# include <emscripten.h>
#endif // __EMSCRIPTEN__
#include <iostream>
#include <vector>
#include <cassert>
/**
* Utility function to get a WebGPU adapter, so that
* WGPUAdapter adapter = requestAdapterSync(options);
* is roughly equivalent to
* const adapter = await navigator.gpu.requestAdapter(options);
*/
WGPUAdapter requestAdapterSync(WGPUInstance instance, WGPURequestAdapterOptions const* options) {
// A simple structure holding the local information shared with the
// onAdapterRequestEnded callback.
struct UserData {
WGPUAdapter adapter = nullptr;
bool requestEnded = false;
};
UserData userData;
// Callback called by wgpuInstanceRequestAdapter when the request returns
// This is a C++ lambda function, but could be any function defined in the
// global scope. It must be non-capturing (the brackets [] are empty) so
// that it behaves like a regular C function pointer, which is what
// wgpuInstanceRequestAdapter expects (WebGPU being a C API). The workaround
// is to convey what we want to capture through the pUserData pointer,
// provided as the last argument of wgpuInstanceRequestAdapter and received
// by the callback as its last argument.
auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const* message, void* pUserData) {
UserData& userData = *reinterpret_cast<UserData*>(pUserData);
if (status == WGPURequestAdapterStatus_Success) {
userData.adapter = adapter;
}
else {
std::cout << "Could not get WebGPU adapter: " << message << std::endl;
}
userData.requestEnded = true;
};
// Call to the WebGPU request adapter procedure
wgpuInstanceRequestAdapter(
instance /* equivalent of navigator.gpu */,
options,
onAdapterRequestEnded,
(void*)&userData
);
// We wait until userData.requestEnded gets true
#ifdef __EMSCRIPTEN__
while (!userData.requestEnded) {
emscripten_sleep(100);
}
#endif // __EMSCRIPTEN__
assert(userData.requestEnded);
return userData.adapter;
}
void inspectAdapter(WGPUAdapter adapter) {
#ifndef __EMSCRIPTEN__
WGPUSupportedLimits supportedLimits = {};
supportedLimits.nextInChain = nullptr;
#ifdef WEBGPU_BACKEND_DAWN
bool success = wgpuAdapterGetLimits(adapter, &supportedLimits) == WGPUStatus_Success;
#else
bool success = wgpuAdapterGetLimits(adapter, &supportedLimits);
#endif
if (success) {
std::cout << "Adapter limits:" << std::endl;
std::cout << " - maxTextureDimension1D: " << supportedLimits.limits.maxTextureDimension1D << std::endl;
std::cout << " - maxTextureDimension2D: " << supportedLimits.limits.maxTextureDimension2D << std::endl;
std::cout << " - maxTextureDimension3D: " << supportedLimits.limits.maxTextureDimension3D << std::endl;
std::cout << " - maxTextureArrayLayers: " << supportedLimits.limits.maxTextureArrayLayers << std::endl;
}
#endif // NOT __EMSCRIPTEN__
}
// DEVICE
/**
* Utility function to get a WebGPU device, so that
* WGPUDevice device = requestDeviceSync(adapter, options);
* is roughly equivalent to
* const device = await adapter.requestDevice(descriptor);
* It is very similar to requestAdapter
*/
WGPUDevice requestDeviceSync(WGPUAdapter adapter, WGPUDeviceDescriptor const* descriptor) {
struct UserData {
WGPUDevice device = nullptr;
bool requestEnded = false;
};
UserData userData;
auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, char const* message, void* pUserData) {
UserData& userData = *reinterpret_cast<UserData*>(pUserData);
if (status == WGPURequestDeviceStatus_Success) {
userData.device = device;
}
else {
std::cout << "Could not get WebGPU device: " << message << std::endl;
}
userData.requestEnded = true;
};
wgpuAdapterRequestDevice(
adapter,
descriptor,
onDeviceRequestEnded,
(void*)&userData
);
#ifdef __EMSCRIPTEN__
while (!userData.requestEnded) {
emscripten_sleep(100);
}
#endif // __EMSCRIPTEN__
assert(userData.requestEnded);
return userData.device;
}
void inspectDevice(WGPUDevice device) {
std::vector<WGPUFeatureName> features;
size_t featureCount = wgpuDeviceEnumerateFeatures(device, nullptr);
features.resize(featureCount);
wgpuDeviceEnumerateFeatures(device, features.data());
std::cout << "Device features:" << std::endl;
std::cout << std::hex;
for (auto f : features) {
std::cout << " - 0x" << f << std::endl;
}
std::cout << std::dec;
WGPUSupportedLimits limits = {};
limits.nextInChain = nullptr;
#ifdef WEBGPU_BACKEND_DAWN
bool success = wgpuDeviceGetLimits(device, &limits) == WGPUStatus_Success;
#else
bool success = wgpuDeviceGetLimits(device, &limits);
#endif
if (success) {
std::cout << "Device limits:" << std::endl;
std::cout << " - maxTextureDimension1D: " << limits.limits.maxTextureDimension1D << std::endl;
std::cout << " - maxTextureDimension2D: " << limits.limits.maxTextureDimension2D << std::endl;
std::cout << " - maxTextureDimension3D: " << limits.limits.maxTextureDimension3D << std::endl;
std::cout << " - maxTextureArrayLayers: " << limits.limits.maxTextureArrayLayers << std::endl;
}
}