diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index da2866e9ff..63c87b9331 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -190,17 +190,17 @@ fn map_buffer( // // If this is a write mapping zeroing out the memory here is the only reasonable way as all data is pushed to GPU anyways. let zero_init_needs_flush_now = mapping.is_coherent && buffer.sync_mapped_writes.is_none(); // No need to flush if it is flushed later anyways. - for uninitialized_range in buffer.initialization_status.drain(offset..(size + offset)) { - let num_bytes = uninitialized_range.end - uninitialized_range.start; + let mapped = unsafe { std::slice::from_raw_parts_mut(mapping.ptr.as_ptr(), size as usize) }; + + for uninitialized in buffer.initialization_status.drain(offset..(size + offset)) { // The mapping's pointer is already offset, however we track the uninitialized range relative to the buffer's start. - let start = (uninitialized_range.start - offset) as isize; - unsafe { ptr::write_bytes(mapping.ptr.as_ptr().offset(start), 0, num_bytes as usize) }; + let fill_range = + (uninitialized.start - offset) as usize..(uninitialized.end - offset) as usize; + mapped[fill_range].fill(0); + if zero_init_needs_flush_now { unsafe { - raw.flush_mapped_ranges( - buffer.raw.as_ref().unwrap(), - iter::once(uninitialized_range), - ) + raw.flush_mapped_ranges(buffer.raw.as_ref().unwrap(), iter::once(uninitialized)) }; } }