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

webgpu: Use safe callbacks & try_recv_timeout #32008

Merged
merged 6 commits into from Apr 30, 2024

Conversation

sagudev
Copy link
Member

@sagudev sagudev commented Apr 6, 2024

It also removes the need for HashMaps and BufferMaps struct that keep data alive until it was used by C callback.

Callbacks now do not send request to self anymore so https://sagudev.github.io/briefcase/fastgame.html actually works now.

We now do try_recv_timeout instead of running loop continuously.


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • There are tests for these changes in WebGPU CTS

@sagudev sagudev added the A-content/webgpu The WebGPU implementation. label Apr 6, 2024
@sagudev

This comment was marked as resolved.

@sagudev

This comment was marked as resolved.

@sagudev

This comment was marked as resolved.

@sagudev
Copy link
Member Author

sagudev commented Apr 8, 2024

NOTE-TO-SELF: Ipc-channel created from response_async should only send one message back (wgpu runs callback also on failures at buffer map async).

@sagudev

This comment was marked as outdated.

@sagudev sagudev mentioned this pull request Apr 22, 2024
3 tasks
@sagudev sagudev changed the title webgpu: Use safe callbacks webgpu: Use safe callbacks & try_recv_timeout Apr 26, 2024
@sagudev sagudev marked this pull request as ready for review April 28, 2024 08:49
@sagudev sagudev requested a review from gterzian April 29, 2024 04:49
@sagudev sagudev mentioned this pull request Apr 29, 2024
3 tasks
Copy link
Member

@gterzian gterzian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I've looked a bit into https://docs.rs/wgpu-core/0.20.0/src/wgpu_core/device/global.rs.html#2391, and I'm wondering if we really need the callbacks for the map asyncs. It seems to just makes things more complicated, see for example the lock around webrender.

This is different from the closures of submitted work done, because those are actually called at a later stage from when they are submitted. But the map asyncs callbacks are just called right then and there.

I think https://docs.rs/wgpu/latest/wgpu/struct.BufferSlice.html#method.map_async only applies to when you are using the higher-level wgpu API. the wgpu-core buffer_map_async calls the callback synchronously.

If that is correct(I think it is, but let me know if you think otherwise), I would say we should remove the callbacks, and the locks.

let wgpu_image_map = Arc::clone(&self.wgpu_image_map);
let webrender_api = Arc::clone(&self.webrender_api);
let webrender_document = self.webrender_document;
let callback = BufferMapCallback::from_rust(Box::from(move |result| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just call global.buffer_map_async synchronously, and then handle the result and do the webrender stuff?

global.buffer_get_mapped_range(info.buffer_id, 0, None))
let glob = Arc::clone(&self.global);
let resp_sender = sender.clone();
let callback = BufferMapCallback::from_rust(Box::from(move |result| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here: do we need the callback?

@sagudev
Copy link
Member Author

sagudev commented Apr 29, 2024

Callback is called synchronously only when result if there is immediate failure (mostly validation): https://docs.rs/wgpu-core/0.20.0/src/wgpu_core/device/global.rs.html#2388 (we currently handle this twice, see #32172)

I think I can see why you get confused by callbacks (I got too for few moments), but op (BufferMapOperation that contains callback) is passed to https://docs.rs/wgpu-core/0.20.0/src/wgpu_core/device/global.rs.html#2498 as described in https://github.com/gfx-rs/wgpu/blob/08efa72a83d0acdf25177408150e4c87933c1cd7/wgpu-core/src/device/life.rs#L222 and triggered on maintain (it's called by device_poll) here: https://github.com/gfx-rs/wgpu/blob/08efa72a83d0acdf25177408150e4c87933c1cd7/wgpu-core/src/device/resource.rs#L455 (later this function returns both submission and mapping closures, that are actually fired in poll functions).

So yeah I think callbacks are really needed (if they were not, they would not be in wgpu). You can always ask in wgpu matrix channel if my explanation is to messy.

Copy link
Member

@gterzian gterzian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So yeah I think callbacks are really needed

Ok, I now see the callback is stored over at https://github.com/gfx-rs/wgpu/blob/bdf30fa45aa86b827749f7ee45ed342ce27b0fbb/wgpu-core/src/device/global.rs#L2498

LGTM!

@sagudev sagudev added this pull request to the merge queue Apr 30, 2024
Merged via the queue into servo:main with commit b6748db Apr 30, 2024
12 checks passed
@sagudev sagudev deleted the safe-callbacks branch April 30, 2024 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-content/webgpu The WebGPU implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants