Fix bug where mapped range in WebBuffer is not cloned correctly #8349
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Connections
N/A
Description
Mapping sub-ranges of buffers on web is currently broken and fails with
OperationError: GPUBuffer.getMappedRange: GetMappedRange range extends beyond buffer's mapped range. I have code like the code below in my app that reliably fails on27.0.1but not on26.0.1. It works again after this PR.When you call
create_buffer(..)on web, we create aWebBufferunder the hood. This contains a reference to the underlying GPUBuffer and aWebBufferMapStatewhich is stores the last range passed tomap_async(..).The bug: this range is stored in a
RefCell<..>and not in anRc<RefCell<..>>. So if you clone the buffer and callmap_async(..)on one of the clones, the others will have an out-of-date mapped range.On web, this potentially stale range is used any time someone calls
get_mapped_range(..). That because on web when you callbuffer.get_mapped_range(sub_range)we do not mapsub_range! We map the range used formap_async(..)and then usesub_rangeto get a slice of the appropriate size.This is a pretty common case because the typical way to call
get_mapped_range(..)is inside a callback passed to.map_async(..), which requires cloning the buffer.So this PR wraps WebBufferMapState in an Rc<>, and fixes a validation check that seemed off.
Testing
I didn't add new tests, but my app no longer fails with
OperationError: GPUBuffer.getMappedRange: GetMappedRange range extends beyond buffer's mapped range.Squash or Rebase?
Squash
Checklist
cargo fmt.taplo format.cargo clippy --tests. If applicable, add:--target wasm32-unknown-unknowncargo xtask testto run tests <-- can't find a way to get this to run locallyCHANGELOG.mdentry.