Skip to content

Commit

Permalink
Don’t copy buffer data back to JS if unnecessary, in WebGPU backend. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid authored Jan 27, 2025
1 parent 33e8df0 commit 321ee42
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ By @brodycj in [#6924](https://github.com/gfx-rs/wgpu/pull/6924).

- Stop naga causing undefined behavior when a ray query misses. By @Vecvec in [#6752](https://github.com/gfx-rs/wgpu/pull/6752).

#### WebGPU

- Improve efficiency of dropping read-only buffer mappings. By @kpreid in [#7007](https://github.com/gfx-rs/wgpu/pull/7007).

### Documentation

- Improved documentation around pipeline caches and `TextureBlitter`. By @DJMcNab in [#6978](https://github.com/gfx-rs/wgpu/pull/6978) and [#7003](https://github.com/gfx-rs/wgpu/pull/7003).
Expand Down
12 changes: 12 additions & 0 deletions wgpu/src/backend/webgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,11 @@ pub struct WebQueueWriteBuffer {
#[derive(Debug)]
pub struct WebBufferMappedRange {
actual_mapping: js_sys::Uint8Array,
/// Copy of the mapped data that lives in the Rust/Wasm heap instead of JS,
/// so Rust code can borrow it.
temporary_mapping: Vec<u8>,
/// Whether `temporary_mapping` has possibly been written to and needs to be written back to JS.
temporary_mapping_modified: bool,
/// Unique identifier for this BufferMappedRange.
ident: crate::cmp::Identifier,
}
Expand Down Expand Up @@ -2672,6 +2676,7 @@ impl dispatch::BufferInterface for WebBuffer {
WebBufferMappedRange {
actual_mapping,
temporary_mapping,
temporary_mapping_modified: false,
ident: crate::cmp::Identifier::create(),
}
.into()
Expand Down Expand Up @@ -3780,11 +3785,18 @@ impl dispatch::BufferMappedRangeInterface for WebBufferMappedRange {

#[inline]
fn slice_mut(&mut self) -> &mut [u8] {
self.temporary_mapping_modified = true;
&mut self.temporary_mapping
}
}
impl Drop for WebBufferMappedRange {
fn drop(&mut self) {
if !self.temporary_mapping_modified {
// For efficiency, skip the copy if it is not needed.
// This is also how we skip copying back on *read-only* mappings.
return;
}

// Copy from the temporary mapping back into the array buffer that was
// originally provided by the browser
let temporary_mapping_slice = self.temporary_mapping.as_slice();
Expand Down

0 comments on commit 321ee42

Please sign in to comment.