From 34b9101a0a511e54490aaebe07b7beaaf321bf42 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Mon, 6 Apr 2020 00:33:57 -0230 Subject: [PATCH] Get capture mostly working `File::create` isn't available on wasm, so we can't actually write the output image anywhere unless we target something else as the file system (e.g. local storage) --- examples/capture/main.rs | 21 +++++++++++++++++++-- src/backend/native.rs | 14 ++++++++++++++ src/backend/web.rs | 13 +++++++++++++ src/lib.rs | 10 +++++----- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/examples/capture/main.rs b/examples/capture/main.rs index 613556d6c..e70c904b8 100644 --- a/examples/capture/main.rs +++ b/examples/capture/main.rs @@ -95,7 +95,12 @@ async fn run() { // be called in an event loop or on another thread. device.poll(wgpu::Maintain::Wait); - // Write the buffer as a PNG + // If a file system is available, write the buffer as a PNG + let has_file_system_available = cfg!(not(target_arch = "wasm32")); + if !has_file_system_available { + return; + } + if let Ok(mapping) = buffer_future.await { let mut png_encoder = png::Encoder::new(File::create("red.png").unwrap(), size, size); png_encoder.set_depth(png::BitDepth::Eight); @@ -108,8 +113,20 @@ async fn run() { } } +#[cfg(target_arch = "wasm32")] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen(start))] +pub fn wasm_main() { + console_log::init().expect("could not initialize log"); + std::panic::set_hook(Box::new(console_error_panic_hook::hook)); + wasm_bindgen_futures::spawn_local(run()); +} + +#[cfg(target_arch = "wasm32")] fn main() { - env_logger::init(); +} +#[cfg(not(target_arch = "wasm32"))] +fn main() { + env_logger::init(); futures::executor::block_on(run()); } diff --git a/src/backend/native.rs b/src/backend/native.rs index 2d2669f16..62b7f9b40 100644 --- a/src/backend/native.rs +++ b/src/backend/native.rs @@ -398,6 +398,20 @@ pub(crate) fn command_encoder_copy_buffer_to_texture( ); } +pub(crate) fn command_encoder_copy_texture_to_buffer( + command_encoder: &CommandEncoderId, + source: crate::TextureCopyView, + destination: crate::BufferCopyView, + copy_size: wgt::Extent3d, +) { + wgn::wgpu_command_encoder_copy_texture_to_buffer( + *command_encoder, + &map_texture_copy_view(source), + &map_buffer_copy_view(destination), + copy_size, + ); +} + pub(crate) fn begin_compute_pass(command_encoder: &CommandEncoderId) -> ComputePassId { unsafe { wgn::wgpu_command_encoder_begin_compute_pass(*command_encoder, None) diff --git a/src/backend/web.rs b/src/backend/web.rs index 3364c95fd..94ea5c9f9 100644 --- a/src/backend/web.rs +++ b/src/backend/web.rs @@ -683,6 +683,19 @@ pub(crate) fn command_encoder_copy_buffer_to_texture( ); } +pub(crate) fn command_encoder_copy_texture_to_buffer( + command_encoder: &CommandEncoderId, + source: crate::TextureCopyView, + destination: crate::BufferCopyView, + copy_size: wgt::Extent3d, +) { + command_encoder.copy_texture_to_buffer_with_gpu_extent_3d_dict( + &map_texture_copy_view(source), + &map_buffer_copy_view(destination), + &map_extent_3d(copy_size), + ); +} + pub(crate) fn begin_compute_pass(command_encoder: &CommandEncoderId) -> ComputePassId { let mapped_desc = web_sys::GpuComputePassDescriptor::new(); command_encoder.begin_compute_pass_with_descriptor(&mapped_desc) diff --git a/src/lib.rs b/src/lib.rs index 4a05b92f7..95892af3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -963,7 +963,6 @@ impl CommandEncoder { ); } -/* /// Copy data from a texture to a buffer. pub fn copy_texture_to_buffer( &mut self, @@ -971,14 +970,15 @@ impl CommandEncoder { destination: BufferCopyView, copy_size: Extent3d, ) { - wgn::wgpu_command_encoder_copy_texture_to_buffer( - self.id, - &source.into_native(), - &destination.into_native(), + backend::command_encoder_copy_texture_to_buffer( + &self.id, + source, + destination, copy_size, ); } +/* /// Copy data from one texture to another. pub fn copy_texture_to_texture( &mut self,