You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -48,8 +49,10 @@ For now we place this in the `main.cpp`, but you could also create a dedicated `
48
49
49
50
We now incrementally see the niceties that this wrapper introduces. Keep in mind that the C++ wrapper types are (almost) always **compatible with the C API**, so you can migrate your code **progressively**.
50
51
51
-
Namespace
52
-
---------
52
+
Core features
53
+
-------------
54
+
55
+
### Namespace
53
56
54
57
The C interface could not make use of **namespaces**, since they only exist in C++, so you may have noticed that every single function starts with `wgpu` and every single structure starts with `WGPU`. A more C++ idiomatic way of doing this is to enclose all these functions into a **namespace**.
55
58
@@ -78,8 +81,7 @@ using namespace wgpu;
78
81
Instance instance = createInstance();
79
82
```
80
83
81
-
Objects
82
-
-------
84
+
### Objects
83
85
84
86
Beyond namespace, most functions are also **prefixed by the type of their first argument**, for instance:
As you can see, descriptors can also be **initialized** using the generic `wgpu::Default` object instead of the long struct-specific INIT macro.
140
142
```
141
143
142
-
Scoped enumerations
143
-
-------------------
144
+
### Scoped enumerations
144
145
145
146
Because enums are *unscoped* by default, the C API is forced to **prefix all values** that an enumeration can take with the name of the enum, leading to quite long names:
The actual implementation use a little trickery so that enum names are scoped, but implicitly converted to and from the original WebGPU enum values.
177
178
```
178
179
179
-
Capturing closures
180
-
------------------
180
+
### String View
181
+
182
+
Instead of writing conversion functions like `toStdStringView` and `toWgpuStringView`, the wrapper defines a `wgpu::StringView` type that is able to automatically convert:
Many asynchronous operations use callbacks. In order to provide some context to the callback's body, there are always two `void *userdata` arguments passed around. This can be simplified in C++ by using capturing closures.
A little difference between the C and C++ versions above is that the C version allocates the `Context` statically on the stack, while rooms for the C++ lambda gets allocated dynamically in the heap. If this bothers you, the wrapper provides an in-between that enables using the object notation but still expects you to create the callback info structure yourself.
224
244
```
225
245
246
+
Extensions
247
+
----------
248
+
249
+
Besides the core zero-overhead features described above, the wrapper provides some utility features. When these are used, they add a bit of runtime code, but that likely corresponds to what you would manually write without using them.
250
+
251
+
### Synchronous adapter and device requests
252
+
253
+
As we have seen in the early chapters, it can be convenient to get a version of `Instance::requestAdapter` and `Adapter::requestDevice` that are blocking instead of asynchronous. The wrapper provides a `Instance::requestAdapterSync` and a `Adapter::requestDeviceSync` that are equivalent to the utility functions that we previously introduced.
254
+
255
+
### Object pretty printing
256
+
257
+
Object wrappers (Instance, Adapter, Device, etc.) provide an overload of `operator<<` so that printing them with `std::cout` does not just give a pointer address but also prefixes it with the type name, like `<wgpu::Device 0x1234567>` instead of just `0x1234567`.
258
+
259
+
If you want to avoid this, just cast the object back to the raw type before printing it: `std::cout << (WGPUDevice)device < std::endl`.
260
+
226
261
Conclusion
227
262
----------
228
263
@@ -231,3 +266,297 @@ From now on, I will be providing two versions of each code snippet: one that use
231
266
Congratulations, you've made it to **the end of the "Getting Started" section**! It was not a small thing, you are now well equipped to explore and understand the various documentation about WebGPU. From here, you can decide to either move on to the [Graphics](../basic-3d-rendering/index.md) section and draw your first triangle, or go to the [Compute](../basic-compute/index.md) section and start playing with tensors.
Copy file name to clipboardExpand all lines: next/getting-started/first-color.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -202,7 +202,7 @@ private:
202
202
To get the texture to draw onto, we use `wgpuSurfaceGetCurrentTexture`. The "surface texture" is not really an object but rather a **container** for the **multiple things that this function returns**. It is thus up to us to create the `WGPUSurfaceTexture` container, which we pass to the function to write into it:
0 commit comments