-
Notifications
You must be signed in to change notification settings - Fork 946
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
Built-in polling thread #1871
Comments
Hi! 👋 we talked about some options to do this in gfx-rs/wgpu-rs#727 which used a background thread directly in the implementation of the It's a bit complicated because some applications don't want us wgpu to automatically create a background thread. Instead we're hoping to integrate directly into the event loop somehow, which would hopefully avoid the extra background thread. We haven't figured out how the event loop integration would work yet though. |
There's an important difference between the approach in gfx-rs/wgpu-rs#727 and my approach: In my approach, the background thread is completely idle while no buffer mapping is needed, and yet there's no delay once a mapping is requested. In your approach, there are either long delays before the future completes (up to 100 ms in your code), or the background thread has to be woken up very frequently.
It's easy to make the creation of the background thread optional:
I don't see how this could be done in a sensible way. There are basically three options for this:
In contrast, there's basically no drawback to my proposed solution: It's almost zero-cost when enabled, and zero-cost when not enabled. |
Right, the approach in gfx-rs/wgpu-rs#727 was more of a proof of concept -- I think we would've wanted more control over how the device is polled (basically the same way you described, and directly integrated with pending futures). |
I'll try to implement my idea, then we'll see how well it integrates with wgpu :) |
Hi Josh, I've created PR #1891. Let me know what you think! |
An update on this department. The webgpu.h group has been talking about adding a couple apis that would make this possible to implement in user-space. There is more that goes into this than what is here, but the proposal isn't final, so I haven't transposed the whole api, but we definitely want to make this possible. struct WgpuFuture(...);
struct InstanceDescriptor {
...
// Called once every time a WgpuFuture is created
future_callback: Box<dyn Fn(WgpuFuture)>,
}
impl Device {
fn waitAnyFutures(futures: &[WgpuFuture], timeout: Duration);
} These two primitives will allow you to create a runtime which constantly polls in runtime. There is also discussions about allowing export to Fds so integration with runtimes like tokio on certain platforms. More details to come. |
Is your feature request related to a problem? Please describe.
The current interface for mapping a buffer is a little inconvenient: Calling
BufferSlice::map_async()
followed bydevice.poll(Maintain::Wait)
blocks the thread, defeating the purpose of that future. Integratingdevice.poll()
into an event loop, as suggested by the docs, is also suboptimal: Either you block the entire event loop waiting for the GPU to become idle, or you're back to wasting CPU time by polling continuously.Describe the solution you'd like
In my application, I start a dedicated thread for polling the device. This thread is usually idle (parked) and is woken up only when a buffer is mapped.
This
PollThread
is roughly used as follows:This works really well, especially with multiple futures which might map independent buffers concurrently, and has very low overhead. But it would be even better if it was integrated into wgpu itself: Each
Device
starts its ownPollThread
instance, andBufferSlice::map_async()
callsPollThread::request_poll()
for the device it belongs to. You could even make the starting of aPollThread
optional at device creation.Would this approach be acceptable for you? If yes, I could prepare a pull request.
The text was updated successfully, but these errors were encountered: