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

Ensure the BufferMapCallback is always called. #2848

Merged
merged 2 commits into from
Jul 8, 2022

Conversation

nical
Copy link
Contributor

@nical nical commented Jul 4, 2022

Checklist

  • Run cargo clippy.
  • Run RUSTFLAGS=--cfg=web_sys_unstable_apis cargo clippy --target wasm32-unknown-unknown if applicable.
  • Add change to CHANGELOG.md. See simple instructions inside file.

Connections

WebGPU specification

Description

This PR enforces that the mapping callback is always called (be it with an error). It does so by calling it manually in more places (with corresponding error codes) and by catching the rest in the callback's destructor.

This solves two issues on the Gecko side:

  • The callback cleans up after itself (the user data is deleted at the end of the callback), so dropping the callback without calling it is a memory leak. I can't think of a better way to implement this on the C++ side since there can be any number of callbacks at any time living for an unspecified amount of time.
  • This makes it easier to implement the error reporting of the WebGPU spec.

Note that when the callback is fired by its Drop impl, there is no guarantee that it happens outside of the locks. If that's a big issue, we could instead panic in the drop impl and be very careful about the control flow.

Also, the WebGPU spec specifies what should happen when destroying a buffer that is mapped or pending. The easiest way to conform to that is to just call unmap. I suspect that it will also avoid issues with wgpu itself?

Testing

None.

@nical
Copy link
Contributor Author

nical commented Jul 4, 2022

I added this to the changelog because the PR template says so, but I wouldn't be offended if this type of plumbing isn't changelog-worthy!

@nical
Copy link
Contributor Author

nical commented Jul 4, 2022

Hm, I just realized that map_async was already firing the callback inside of the locks and I added more of that. Let me know how important that is and in what cases. I'll update the PR so that it does whatever the right thing is.

@nical
Copy link
Contributor Author

nical commented Jul 6, 2022

I updated the PR to be more principled about invoking the callback outside of the lock in buffer_map_async.

@jimblandy jimblandy self-requested a review July 6, 2022 19:57
@jimblandy jimblandy changed the title Ensure the BufferMapAsyncCallback is always called. Ensure the BufferMapCallback is always called. Jul 7, 2022
Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

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

This all makes sense - thank you!

There are a few things that I think need to be addressed.

wgpu-core/src/device/mod.rs Outdated Show resolved Hide resolved
wgpu-core/src/device/mod.rs Outdated Show resolved Hide resolved
wgpu-core/src/resource.rs Outdated Show resolved Hide resolved
wgpu-core/src/resource.rs Outdated Show resolved Hide resolved
@nical nical force-pushed the map-async-callback branch 5 times, most recently from 507fa30 to 6156d97 Compare July 7, 2022 16:13
Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

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

Looks good, thank you! Just a few requests.

wgpu-core/src/device/mod.rs Outdated Show resolved Hide resolved
wgpu-core/src/device/mod.rs Show resolved Hide resolved
wgpu-core/src/resource.rs Show resolved Hide resolved
wgpu-core/src/resource.rs Outdated Show resolved Hide resolved
nical added 2 commits July 7, 2022 18:44
This solves two issues on the Gecko side:
 - The callback cleans up after itself (the user data is deleted at the end of the callback), so dropping the callback without calling it is a memory leak. I can't think of a better way to implement this on the C++ side since there can be any number of callback at any time living for an unspecified amount of time.
 - This makes it easier to implement the error reporting of the WebGPU spec.
Copy link
Member

@jimblandy jimblandy left a comment

Choose a reason for hiding this comment

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

Thanks!

@jimblandy jimblandy enabled auto-merge (squash) July 7, 2022 16:48
@jimblandy jimblandy merged commit 324de1b into gfx-rs:master Jul 8, 2022
@nical nical deleted the map-async-callback branch July 8, 2022 08:09
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

Successfully merging this pull request may close these issues.

2 participants