-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwebgpu.template.hpp
307 lines (265 loc) · 8.38 KB
/
webgpu.template.hpp
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
/**
* This file is part of the "Learn WebGPU for C++" book.
* https://github.com/eliemichel/LearnWebGPU
*
* MIT License
* Copyright (c) 2022-2024 Elie Michel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Exactly one of your source files must #define WEBGPU_CPP_IMPLEMENTATION
* before including this header.
*
* NB: This file has been generated by the webgpu-cpp generator
* (see https://github.com/eliemichel/webgpu-cpp )
*/
#pragma once
{{webgpu_includes}}
#include <iostream>
#include <vector>
#include <functional>
#include <cassert>
#include <cmath>
#include <memory>
#if __EMSCRIPTEN__
#include <emscripten.h>
#endif
#ifdef _MSVC_LANG
# if _MSVC_LANG >= 202002L
# define NO_DISCARD [[nodiscard("You should keep this handle alive for as long as the callback may get invoked.")]]
# elif _MSVC_LANG >= 201703L
# define NO_DISCARD [[nodiscard]]
# else
# define NO_DISCARD
# endif
#else
# if __cplusplus >= 202002L
# define NO_DISCARD [[nodiscard("You should keep this handle alive for as long as the callback may get invoked.")]]
# elif __cplusplus >= 201703L
# define NO_DISCARD [[nodiscard]]
# else
# define NO_DISCARD
# endif
#endif
/**
* A namespace providing a more C++ idiomatic API to WebGPU.
*/
namespace wgpu {
struct DefaultFlag {};
constexpr DefaultFlag Default;
#define HANDLE(Type) \
class Type { \
public: \
typedef Type S; /* S == Self */ \
typedef WGPU ## Type W; /* W == WGPU Type */ \
Type() : m_raw(nullptr) {} \
Type(const W& w) : m_raw(w) {} \
operator W&() { return m_raw; } \
operator const W&() const { return m_raw; } \
operator bool() const { return m_raw != nullptr; } \
bool operator==(const Type& other) const { return m_raw == other.m_raw; } \
bool operator!=(const Type& other) const { return m_raw != other.m_raw; } \
bool operator==(const W& other) const { return m_raw == other; } \
bool operator!=(const W& other) const { return m_raw != other; } \
friend auto operator<<(std::ostream &stream, const S& self) -> std::ostream & { \
return stream << "<wgpu::" << #Type << " " << self.m_raw << ">"; \
} \
private: \
W m_raw; \
public:
#define DESCRIPTOR(Type) \
struct Type : public WGPU ## Type { \
public: \
typedef Type S; /* S == Self */ \
typedef WGPU ## Type W; /* W == WGPU Type */ \
Type() : W() { nextInChain = nullptr; } \
Type(const W &other) : W(other) { nextInChain = nullptr; } \
Type(const DefaultFlag &) : W() { setDefault(); } \
Type& operator=(const DefaultFlag &) { setDefault(); return *this; } \
friend auto operator<<(std::ostream &stream, const S&) -> std::ostream & { \
return stream << "<wgpu::" << #Type << ">"; \
} \
public:
#define STRUCT_NO_OSTREAM(Type) \
struct Type : public WGPU ## Type { \
public: \
typedef Type S; /* S == Self */ \
typedef WGPU ## Type W; /* W == WGPU Type */ \
Type() : W() {} \
Type(const W &other) : W(other) {} \
Type(const DefaultFlag &) : W() { setDefault(); } \
Type& operator=(const DefaultFlag &) { setDefault(); return *this; } \
public:
#define STRUCT(Type) \
STRUCT_NO_OSTREAM(Type) \
friend auto operator<<(std::ostream &stream, const S&) -> std::ostream & { \
return stream << "<wgpu::" << #Type << ">"; \
} \
public:
#define ENUM(Type) \
class Type { \
public: \
typedef Type S; /* S == Self */ \
typedef WGPU ## Type W; /* W == WGPU Type */ \
constexpr Type() : m_raw(W{}) {} /* Using default value-initialization */ \
constexpr Type(const W& w) : m_raw(w) {} \
constexpr operator W() const { return m_raw; } \
W m_raw; /* Ideally, this would be private, but then types generated with this macro would not be structural. */
#define ENUM_ENTRY(Name, Value) \
static constexpr W Name = (W)(Value);
#define END };
{{begin-inject}}
HANDLE(Instance)
Adapter requestAdapter(const RequestAdapterOptions& options);
END
HANDLE(Adapter)
Device requestDevice(const DeviceDescriptor& descriptor);
END
STRUCT(Color)
Color(double r, double g, double b, double a) : WGPUColor{ r, g, b, a } {}
END
STRUCT(Extent3D)
Extent3D(uint32_t width, uint32_t height, uint32_t depthOrArrayLayers) : WGPUExtent3D{ width, height, depthOrArrayLayers } {}
END
STRUCT(Origin3D)
Origin3D(uint32_t x, uint32_t y, uint32_t z) : WGPUOrigin3D{ x, y, z } {}
END
STRUCT_NO_OSTREAM(StringView)
StringView(const std::string_view& cpp) : WGPUStringView{ cpp.data(), cpp.length() } {}
operator std::string_view() const;
friend auto operator<<(std::ostream& stream, const S& self) -> std::ostream& {
return stream << std::string_view(self);
}
END
{{end-inject}}
{{begin-blacklist}}
wgpuDeviceGetLostFuture
{{end-blacklist}}
// Other type aliases
{{type_aliases}}
// Enumerations
{{enums}}
// Structs
{{structs}}
// Descriptors
{{descriptors}}
// Handles forward declarations
{{handles_decl}}
// Callback types
{{callbacks}}
// Handles detailed declarations
{{handles}}
// Non-member procedures
{{procedures}}
Instance createInstance();
Instance createInstance(const InstanceDescriptor& descriptor);
#ifdef WEBGPU_CPP_IMPLEMENTATION
Instance createInstance() {
return wgpuCreateInstance(nullptr);
}
Instance createInstance(const InstanceDescriptor& descriptor) {
return wgpuCreateInstance(&descriptor);
}
// Handles members implementation
{{handles_impl}}
// Extra implementations
Adapter Instance::requestAdapter(const RequestAdapterOptions& options) {
struct Context {
Adapter adapter = nullptr;
bool requestEnded = false;
};
Context context;
RequestAdapterCallbackInfo{{ext_suffix}} callbackInfo;
callbackInfo.nextInChain = nullptr;
callbackInfo.userdata1 = &context;
callbackInfo.callback = [](
WGPURequestAdapterStatus status,
WGPUAdapter adapter,
WGPUStringView message,
void* userdata1,
[[maybe_unused]] void* userdata2
) {
Context& context = *reinterpret_cast<Context*>(userdata1);
if (status == RequestAdapterStatus::Success) {
context.adapter = adapter;
}
else {
std::cout << "Could not get WebGPU adapter: " << StringView(message) << std::endl;
}
context.requestEnded = true;
};
callbackInfo.mode = CallbackMode::AllowSpontaneous;
requestAdapter{{ext_suffix}}(options, callbackInfo);
#if __EMSCRIPTEN__
while (!context.requestEnded) {
emscripten_sleep(50);
}
#endif
assert(context.requestEnded);
return context.adapter;
}
Device Adapter::requestDevice(const DeviceDescriptor& descriptor) {
struct Context {
Device device = nullptr;
bool requestEnded = false;
};
Context context;
RequestDeviceCallbackInfo{{ext_suffix}} callbackInfo;
callbackInfo.nextInChain = nullptr;
callbackInfo.userdata1 = &context;
callbackInfo.callback = [](
WGPURequestDeviceStatus status,
WGPUDevice device,
WGPUStringView message,
void* userdata1,
[[maybe_unused]] void* userdata2
) {
Context& context = *reinterpret_cast<Context*>(userdata1);
if (status == RequestDeviceStatus::Success) {
context.device = device;
}
else {
std::cout << "Could not get WebGPU device: " << StringView(message) << std::endl;
}
context.requestEnded = true;
};
callbackInfo.mode = CallbackMode::AllowSpontaneous;
requestDevice{{ext_suffix}}(descriptor, callbackInfo);
#if __EMSCRIPTEN__
while (!context.requestEnded) {
emscripten_sleep(50);
}
#endif
assert(context.requestEnded);
return context.device;
}
StringView::operator std::string_view() const {
return
length == WGPU_STRLEN
? std::string_view(data)
: std::string_view(data, length);
}
#endif // WEBGPU_CPP_IMPLEMENTATION
#undef HANDLE
#undef DESCRIPTOR
#undef ENUM
#undef ENUM_ENTRY
#undef END
} // namespace wgpu