mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-28 00:59:12 +01:00
prototype of async/await for buffer mapping
This commit is contained in:
1 parent
e82ac7a7f9
commit
cc32408fa2
6 files changed
+206
-73
No files matched your search
+15
-15
@@ -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());
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
Reference in new issue
Block a user