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 committed Jan 27, 2025
1 parent fcbadc9 commit d3113af
Showing 1 changed file with 12 additions and 0 deletions.
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 d3113af

Please sign in to comment.