Skip to content

Commit a05c8dc

Browse files
nicalcwfitzgerald
andauthored
Fix buffer zeroing with offset in map_buffer (#2916)
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
1 parent b784eee commit a05c8dc

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ the same every time it is rendered, we now warn if it is missing.
9393
- Fix panics that occur when using `as_hal` functions when the hal generic type does not match the hub being looked up in by @i509VCB [#2871](https://github.com/gfx-rs/wgpu/pull/2871)
9494
- Add some validation in map_async by @nical in [#2876](https://github.com/gfx-rs/wgpu/pull/2876)
9595
- Fix bugs when mapping/unmapping zero-sized buffers and ranges by @nical in [#2877](https://github.com/gfx-rs/wgpu/pull/2877)
96+
- Fix out-of-bound write in `map_buffer` with non-zero offset by @nical in [#2916](https://github.com/gfx-rs/wgpu/pull/2916)
9697
- Validate the number of color attachments in `create_render_pipeline` by @nical in [#2913](https://github.com/gfx-rs/wgpu/pull/2913)
9798

9899
#### DX12

wgpu-core/src/device/mod.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,24 +190,17 @@ fn map_buffer<A: hal::Api>(
190190
//
191191
// If this is a write mapping zeroing out the memory here is the only reasonable way as all data is pushed to GPU anyways.
192192
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.
193-
for uninitialized_range in buffer.initialization_status.drain(offset..(size + offset)) {
194-
let num_bytes = uninitialized_range.end - uninitialized_range.start;
195-
unsafe {
196-
ptr::write_bytes(
197-
mapping
198-
.ptr
199-
.as_ptr()
200-
.offset(uninitialized_range.start as isize),
201-
0,
202-
num_bytes as usize,
203-
)
204-
};
193+
let mapped = unsafe { std::slice::from_raw_parts_mut(mapping.ptr.as_ptr(), size as usize) };
194+
195+
for uninitialized in buffer.initialization_status.drain(offset..(size + offset)) {
196+
// The mapping's pointer is already offset, however we track the uninitialized range relative to the buffer's start.
197+
let fill_range =
198+
(uninitialized.start - offset) as usize..(uninitialized.end - offset) as usize;
199+
mapped[fill_range].fill(0);
200+
205201
if zero_init_needs_flush_now {
206202
unsafe {
207-
raw.flush_mapped_ranges(
208-
buffer.raw.as_ref().unwrap(),
209-
iter::once(uninitialized_range.start..uninitialized_range.start + num_bytes),
210-
)
203+
raw.flush_mapped_ranges(buffer.raw.as_ref().unwrap(), iter::once(uninitialized))
211204
};
212205
}
213206
}

0 commit comments

Comments
 (0)