Add BufferView and BufferViewMut and fix safety issues with mapped range.

This commit is contained in:
Lachlan Sneff
2020-06-09 01:33:00 -04:00
parent 092d5bb6fd
commit 35dd34b4ec
3 changed files with 167 additions and 35 deletions

View File

@@ -117,7 +117,8 @@ async fn run() {
queue.submit(Some(command_buffer));
// Note that we're not calling `.await` here.
let buffer_future = output_buffer.map_async(wgpu::MapMode::Read, 0, wgt::BufferSize::WHOLE);
let buffer_slice = output_buffer.slice(..);
let buffer_future = buffer_slice.map_async(wgpu::MapMode::Read);
// Poll the device in a blocking manner so that our future resolves.
// In an actual application, `device.poll(...)` should
@@ -131,7 +132,7 @@ async fn run() {
}
if let Ok(()) = buffer_future.await {
let padded_buffer = output_buffer.get_mapped_range(0, wgt::BufferSize::WHOLE);
let padded_buffer = buffer_slice.get_mapped_range();
let mut png_encoder = png::Encoder::new(
File::create("red.png").unwrap(),

View File

@@ -108,7 +108,8 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
queue.submit(Some(encoder.finish()));
// Note that we're not calling `.await` here.
let buffer_future = staging_buffer.map_async(wgpu::MapMode::Read, 0, wgt::BufferSize::WHOLE);
let buffer_slice = staging_buffer.slice(..);
let buffer_future = buffer_slice.map_async(wgpu::MapMode::Read);
// Poll the device in a blocking manner so that our future resolves.
// In an actual application, `device.poll(...)` should
@@ -116,7 +117,7 @@ async fn execute_gpu(numbers: Vec<u32>) -> Vec<u32> {
device.poll(wgpu::Maintain::Wait);
if let Ok(()) = buffer_future.await {
let data = staging_buffer.get_mapped_range(0, wgt::BufferSize::WHOLE);
let data = buffer_slice.get_mapped_range();
let result = data
.chunks_exact(4)
.map(|b| u32::from_ne_bytes(b.try_into().unwrap()))