diff --git a/wgpu/examples/capture/main.rs b/wgpu/examples/capture/main.rs index a4fefe1feb..15296b6018 100644 --- a/wgpu/examples/capture/main.rs +++ b/wgpu/examples/capture/main.rs @@ -96,7 +96,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); @@ -109,8 +114,19 @@ 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() {} + +#[cfg(not(target_arch = "wasm32"))] fn main() { env_logger::init(); - futures::executor::block_on(run()); } diff --git a/wgpu/src/backend/native.rs b/wgpu/src/backend/native.rs index 7c64ff738d..c0f757eaae 100644 --- a/wgpu/src/backend/native.rs +++ b/wgpu/src/backend/native.rs @@ -431,6 +431,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/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 290463e2f0..f067ac8da9 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -746,6 +746,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/wgpu/src/lib.rs b/wgpu/src/lib.rs index 00c89dcbb3..1938735214 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -923,7 +923,6 @@ impl CommandEncoder { backend::command_encoder_copy_buffer_to_texture(&self.id, source, destination, copy_size); } - /* /// Copy data from a texture to a buffer. pub fn copy_texture_to_buffer( &mut self, @@ -931,14 +930,10 @@ impl CommandEncoder { destination: BufferCopyView, copy_size: Extent3d, ) { - wgn::wgpu_command_encoder_copy_texture_to_buffer( - self.id, - &source.into_native(), - &destination.into_native(), - copy_size, - ); + 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,