gfx-rs / wgpu

A cross-platform, safe, pure-Rust graphics API.
https://wgpu.rs
Apache License 2.0
11.51k stars 858 forks source link

Resolve cycles in wgpu-core using weak references #5120

Open nical opened 5 months ago

nical commented 5 months ago

At the moment the device has string reference to the resources and the resources have strong references to the device. This cycle is manually broken in triage_suspected by looking at the reference count.

We want to remove this manual reference count management and only rely on weak refst to break the cycles.

@cwfitzgerald put together a diagram that illustrates the end result:

Diagram of future strong and weak reference in wgpu-core

nical commented 4 months ago

@cwfitzgerald how does the queue fit in the above diagram? The WebGPU spec says that the device has a strong reference to the queue (but we can make it work in the browser either way by having the JS-facing objects have the strong references in the right order even if wgpu-core internally has it the other way around). Currently the queue has a strong reference in to the device in wgpu-core.

See also #5230.

cwfitzgerald commented 4 months ago

@nical I believe it's "queue owns device", "device has weakref to queue". This doesn't really vibe with the idempotent queue method call on device in WebGPU, but I don't think we need to have that in wgpu-core.

If we were to model the WebGPU way, it would be "device owns InternalQueue", "queue owns InternalQueue". This way the device can hand out the same internal queue over and over to separate Queue objects.