[rs] 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)
This commit is contained in:
Joshua Groves
2020-04-06 00:33:57 -02:30
committed by Josh Groves
parent 8f7d8cf27e
commit 654833ea56
4 changed files with 47 additions and 9 deletions

View File

@@ -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());
}

View File

@@ -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) }
}

View File

@@ -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)

View File

@@ -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,