Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

Commit

Permalink
Get capture mostly working
Browse files Browse the repository at this point in the history
`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)
  • Loading branch information
grovesNL committed Apr 6, 2020
1 parent c4f2e9b commit 34b9101
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
21 changes: 19 additions & 2 deletions examples/capture/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
14 changes: 14 additions & 0 deletions src/backend/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,22 +963,22 @@ impl CommandEncoder {
);
}

/*
/// Copy data from a texture to a buffer.
pub fn copy_texture_to_buffer(
&mut self,
source: TextureCopyView,
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,
Expand Down

0 comments on commit 34b9101

Please sign in to comment.