Skip to content

Commit

Permalink
Make the unsafe part of zeroing buffers in map_buffer simpler.
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed Jul 27, 2022
1 parent 656ab1f commit d4dc856
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,17 @@ fn map_buffer<A: hal::Api>(
//
// 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))
};
}
}
Expand Down

0 comments on commit d4dc856

Please sign in to comment.