Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Why no automatic reference counting? #18

Open
trbabb opened this issue Jul 10, 2024 · 5 comments
Open

Question: Why no automatic reference counting? #18

trbabb opened this issue Jul 10, 2024 · 5 comments

Comments

@trbabb
Copy link

trbabb commented Jul 10, 2024

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:

  • Overload the copy and move constructors, and obj.reference() or obj.release() as appropriate to maintain the lifetime of every wgpu:: Handle member
  • obj.release() all the handles in the destructor

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
Copy link
Owner

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
Copy link

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

@trbabb
Copy link
Author

trbabb commented Oct 11, 2024

@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
Copy link

@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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants