eliemichel / WebGPU-Cpp

A single-file zero-overhead C++ idiomatic wrapper for WebGPU native
https://eliemichel.github.io/WebGPU-Cpp/
MIT License
294 stars 19 forks source link

Question: Why no automatic reference counting? #18

Open trbabb opened 3 months ago

trbabb commented 3 months ago

Apologies if this is a simple question, I'm just getting started with wgpu—

I'm quickly finding that it's a necessary pattern for every class that retains a Handle type wgpu object to:

and failing to do this (afaict) would result in either leaked resources, or prematurely-freed ones.

From this it seems like it would be very ergonomic and reduce errors to have the HANDLE() template overload the copy/move ctors/dtors/assignment ops to do this implicitly.

Is there a Chesterton's Fence here I'm missing? Why is this not already done? Do I misunderstand how .reference() and .release() are intended to be used?

thanks!

eliemichel commented 3 months ago

I did not want to have this wrapper do anything automagic on top of the raw C API, so that it is only about a nicer C++ style syntax but not about extra features.

What you are looking for is commonly refered to as a RAII idiom and there is the beginning of something in the WebGPU-RAII repo.

I've been using my WebGPU-RAII wrapper for some time, it is nice although what it is still missing is support for Reference: it behaves more like a std::unique_ptr that you cannot copy around. Typically, the owner of a WebGPU entity stores it as ag a wgpu::raii::Buffer and functions that borrow it (i.e., that do not need to care about the lifetime) operate on a regular wgpu::Buffer. It is not fully error-safe but it's been a nice compromise for me so far.

petar-andrejic commented 2 months ago

Dawn's own cpp header also does RAII, so you can use that instead if that's desirable

trbabb commented 2 weeks ago

@petar-andrejic How do I obtain that header? I found this but it's basically empty, and I don't see what it's pointing to.

petar-andrejic commented 2 weeks ago

@petar-andrejic How do I obtain that header? I found this but it's basically empty, and I don't see what it's pointing to.

They use code generation to make the headers from a json description of the API, so it's mostly empty until you actually build the library

eliemichel commented 2 weeks ago

They use code generation to make the headers from a json description of the API, so it's mostly empty until you actually build the library

Correct, once you've built Dawn once, it is found in gen/include/dawn/webgpu.h. If you're looking for emscripten's header, it is in emsdk/upstream/emscripten/system/include/webgpu/webgpu.h.