Skip to content

Commit 4035bba

Browse files
committed
fix(webgpu): reading a mapped buffer throws on a detached ArrayBuffer
Cloning a buffer duplicated its cached mapped ArrayBuffer rather than sharing it, so unmapping through one clone left every other clone holding an ArrayBuffer that Javascript had already detached. The next read built a Uint8Array view over that buffer and threw "Cannot perform Construct on a detached ArrayBuffer". The same applies to the mapped range recorded by map_async, which was invisible to any other clone. Upstream carries this same change from 29.0.1 onward; 27.0.4 is still affected.
1 parent 1a272dc commit 4035bba

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

‎wgpu/src/backend/webgpu.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,9 @@ struct WebBufferMapState {
12151215
pub struct WebBuffer {
12161216
/// The associated GPU buffer.
12171217
inner: webgpu_sys::GpuBuffer,
1218-
/// The mapped array buffer and mapped range.
1219-
mapping: RefCell<WebBufferMapState>,
1218+
/// The mapped array buffer and mapped range. Shared across clones, because an unmap through
1219+
/// any one clone detaches the `ArrayBuffer` for all of them.
1220+
mapping: Rc<RefCell<WebBufferMapState>>,
12201221
/// Unique identifier for this Buffer.
12211222
ident: crate::cmp::Identifier,
12221223
}
@@ -1226,10 +1227,10 @@ impl WebBuffer {
12261227
fn new(inner: webgpu_sys::GpuBuffer, desc: &crate::BufferDescriptor<'_>) -> Self {
12271228
Self {
12281229
inner,
1229-
mapping: RefCell::new(WebBufferMapState {
1230+
mapping: Rc::new(RefCell::new(WebBufferMapState {
12301231
mapped_buffer: None,
12311232
range: 0..desc.size,
1232-
}),
1233+
})),
12331234
ident: crate::cmp::Identifier::create(),
12341235
}
12351236
}

0 commit comments

Comments
 (0)