prototype of async/await for buffer mapping

This commit is contained in:
Tristam MacDonald authored and Tristam MacDonald committed 2019-11-21 21:42:22 -08:00
1 parent e82ac7a7f9
commit cc32408fa2
6 files changed
+206 -73

No files matched your search

+15 -15
View File
@@ -4,7 +4,7 @@
use std::fs::File;
use std::mem::size_of;
fn main() {
async fn run() {
env_logger::init();
let adapter = wgpu::Adapter::request(
@@ -86,21 +86,21 @@ fn main() {
queue.submit(&[command_buffer]);
// Write the buffer as a PNG
output_buffer.map_read_async(
0,
(size * size) as usize * size_of::<u32>(),
move |result: wgpu::BufferMapAsyncResult<&[u8]>| {
let mut png_encoder = png::Encoder::new(File::create("red.png").unwrap(), size, size);
png_encoder.set_depth(png::BitDepth::Eight);
png_encoder.set_color(png::ColorType::RGBA);
png_encoder
.write_header()
.unwrap()
.write_image_data(result.unwrap().data)
.unwrap();
},
);
if let Ok(mapping) = output_buffer.map_read(0u64, (size * size) as u64 * size_of::<u32>() as u64).await {
let mut png_encoder = png::Encoder::new(File::create("red.png").unwrap(), size, size);
png_encoder.set_depth(png::BitDepth::Eight);
png_encoder.set_color(png::ColorType::RGBA);
png_encoder
.write_header()
.unwrap()
.write_image_data(mapping.as_slice())
.unwrap();
}
// The device will be polled when it is dropped but we can also poll it explicitly
device.poll(true);
}
fn main() {
futures::executor::block_on(run());
}
+14 -12
View File
@@ -1,7 +1,7 @@
use std::{convert::TryInto as _, str::FromStr};
use zerocopy::AsBytes as _;
fn main() {
async fn run() {
env_logger::init();
// For now this just panics if you didn't pass numbers. Could add proper error handling.
@@ -93,15 +93,17 @@ fn main() {
queue.submit(&[encoder.finish()]);
// FIXME: Align and use `LayoutVerified`
staging_buffer.map_read_async(0, slice_size, |result| {
if let Ok(mapping) = result {
let times: Box<[u32]> = mapping
.data
.chunks_exact(4)
.map(|b| u32::from_ne_bytes(b.try_into().unwrap()))
.collect();
println!("Times: {:?}", times);
}
});
if let Ok(mapping) = staging_buffer.map_read(0u64, size).await {
let times : Box<[u32]> = mapping
.as_slice()
.chunks_exact(4)
.map(|b| u32::from_ne_bytes(b.try_into().unwrap()))
.collect();
println!("Times: {:?}", times);
}
}
fn main() {
futures::executor::block_on(run());
}