-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[wgpu] Improve buffer mapping errors and allow aliasing immutable borrows #8150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
fn mapping_is_zeroed(array: &[u8]) { | ||
for (i, &byte) in array.iter().enumerate() { | ||
assert_eq!(byte, 0, "Byte at index {i} is not zero"); | ||
} | ||
} | ||
|
||
// Ensure that a simple immutable mapping works and it is zeroed. | ||
#[test] | ||
fn full_immutable_binding() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_READ, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
buffer.map_async(wgpu::MapMode::Read, .., |_| {}); | ||
device.poll(wgpu::PollType::Wait).unwrap(); | ||
|
||
let mapping = buffer.slice(..).get_mapped_range(); | ||
|
||
mapping_is_zeroed(&mapping); | ||
|
||
drop(mapping); | ||
|
||
buffer.unmap(); | ||
} | ||
|
||
// Ensure that a simple mutable binding works and it is zeroed. | ||
#[test] | ||
fn full_mut_binding() { | ||
cwfitzgerald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: true, | ||
}); | ||
|
||
let mapping = buffer.slice(..).get_mapped_range_mut(); | ||
|
||
mapping_is_zeroed(&mapping); | ||
|
||
drop(mapping); | ||
|
||
buffer.unmap(); | ||
} | ||
|
||
// Ensure that you can make two non-overlapping immutable ranges, which are both zeroed | ||
#[test] | ||
fn split_immutable_binding() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_READ, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
buffer.map_async(wgpu::MapMode::Read, .., |_| {}); | ||
device.poll(wgpu::PollType::Wait).unwrap(); | ||
|
||
let mapping0 = buffer.slice(0..512).get_mapped_range(); | ||
let mapping1 = buffer.slice(512..1024).get_mapped_range(); | ||
|
||
mapping_is_zeroed(&mapping0); | ||
mapping_is_zeroed(&mapping1); | ||
|
||
drop(mapping0); | ||
drop(mapping1); | ||
|
||
buffer.unmap(); | ||
} | ||
|
||
/// Ensure that you can make two non-overlapping mapped ranges, which are both zeroed | ||
#[test] | ||
fn split_mut_binding() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: true, | ||
}); | ||
|
||
let mapping0 = buffer.slice(0..512).get_mapped_range_mut(); | ||
let mapping1 = buffer.slice(512..1024).get_mapped_range_mut(); | ||
|
||
mapping_is_zeroed(&mapping0); | ||
mapping_is_zeroed(&mapping1); | ||
|
||
drop(mapping0); | ||
drop(mapping1); | ||
|
||
buffer.unmap(); | ||
} | ||
|
||
/// Ensure that you can make two overlapping immutablely mapped ranges. | ||
#[test] | ||
fn overlapping_ref_binding() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: true, | ||
}); | ||
|
||
let _mapping0 = buffer.slice(0..512).get_mapped_range(); | ||
let _mapping1 = buffer.slice(256..768).get_mapped_range(); | ||
} | ||
|
||
/// Ensure that two overlapping mutably mapped ranges panics. | ||
#[test] | ||
#[should_panic(expected = "break Rust memory aliasing rules")] | ||
fn overlapping_mut_binding() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: true, | ||
}); | ||
|
||
let _mapping0 = buffer.slice(0..512).get_mapped_range_mut(); | ||
let _mapping1 = buffer.slice(256..768).get_mapped_range_mut(); | ||
} | ||
|
||
/// Ensure that when you try to get a mapped range from an unmapped buffer, it panics with | ||
/// an error mentioning a completely unmapped buffer. | ||
#[test] | ||
#[should_panic(expected = "an unmapped buffer")] | ||
fn not_mapped() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
let _mapping = buffer.slice(..).get_mapped_range_mut(); | ||
} | ||
|
||
/// Ensure that when you partially map a buffer, then try to read outside of that range, it panics | ||
/// mentioning the mapped indices. | ||
#[test] | ||
#[should_panic( | ||
expected = "Attempted to get range 512..1024 (Mutable), but the mapped range is 0..512" | ||
)] | ||
fn partially_mapped() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: false, | ||
}); | ||
|
||
buffer.map_async(wgpu::MapMode::Write, 0..512, |_| {}); | ||
device.poll(wgpu::PollType::Wait).unwrap(); | ||
|
||
let _mapping0 = buffer.slice(0..512).get_mapped_range_mut(); | ||
let _mapping1 = buffer.slice(512..1024).get_mapped_range_mut(); | ||
} | ||
|
||
/// Ensure that you cannot unmap a buffer while there are still accessible mapped views. | ||
#[test] | ||
#[should_panic(expected = "You cannot unmap a buffer that still has accessible mapped views")] | ||
fn unmap_while_visible() { | ||
let (device, _queue) = wgpu::Device::noop(&wgpu::DeviceDescriptor::default()); | ||
|
||
let buffer = device.create_buffer(&wgpu::BufferDescriptor { | ||
label: None, | ||
size: 1024, | ||
usage: wgpu::BufferUsages::MAP_WRITE | wgpu::BufferUsages::COPY_SRC, | ||
mapped_at_creation: true, | ||
}); | ||
|
||
let _mapping0 = buffer.slice(..).get_mapped_range_mut(); | ||
buffer.unmap(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod binding_arrays; | ||
mod buffer; | ||
mod buffer_mapping; | ||
mod buffer_slice; | ||
mod device; | ||
mod experimental; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's add a highlight for this & removed lifetimes on buffer mappings. If you're used to working around these this thing is a lifechanger!