From d87bea9cce6a5b23a229d4429df3fee0dc478da9 Mon Sep 17 00:00:00 2001 From: Alan Race Date: Mon, 21 Nov 2022 08:46:33 +0100 Subject: [PATCH] Fixed generation of screenshot for widths not a multiple of 256 --- src/camera.rs | 112 +++++++++++++++++++++++++++------------------- src/image_copy.rs | 29 +++++++++++- 2 files changed, 94 insertions(+), 47 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index 66b2f0e..d1fdace 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -97,7 +97,7 @@ fn handle_camera_event( mut q_text: Query<&mut Text>, mut windows: ResMut, mut camera_setup: ResMut, - images: Res>, + mut images: ResMut>, render_device: Res, ) { let window = windows.primary_mut(); @@ -148,37 +148,57 @@ fn handle_camera_event( } } CameraEvent::CopyToClipboard => { - if let Some(view_texture) = images.get(&camera_setup.cpu_target.as_ref().unwrap()) { - let size = view_texture.size().as_ivec2(); - let size = Extent3d { - width: size.x as u32, - height: size.y as u32, - depth_or_array_layers: 4, - }; - - println!("Setting up screenshot: {:?} | {:?}", size, view_texture.size()); - println!("Setting up screenshot: {:?} | {:?}", size, images.get(&camera_setup.target.as_ref().unwrap()).size()); - - // TODO: update the size of cpu_target here to re - - commands.spawn(ImageCopier::new( - camera_setup.target.as_ref().unwrap().clone(), - camera_setup.cpu_target.as_ref().unwrap().clone(), - size, - &render_device, - )); - // image_copier.disable();) - - // let bytes = [ - // 255, 100, 100, 255, 100, 255, 100, 100, 100, 100, 255, 100, 0, 0, 0, 255, - // ]; - // let img_data = arboard::ImageData { - // width: 2, - // height: 2, - // bytes: bytes.as_ref().into(), - // }; - // ctx.set_image(img_data).unwrap(); - } + let size = images + .get(&camera_setup.target.as_ref().unwrap()) + .unwrap() + .size() + .as_ivec2(); + + let size = Extent3d { + width: size.x as u32, + height: size.y as u32, + ..default() + }; + + let view_texture = images + .get_mut(&camera_setup.cpu_target.as_ref().unwrap()) + .unwrap(); + + view_texture.resize(size); + + println!( + "Setting up screenshot: {:?} | {:?}", + size, + view_texture.size() + ); + println!( + "Setting up screenshot: {:?} | {:?}", + size, + images + .get(&camera_setup.target.as_ref().unwrap()) + .unwrap() + .size() + ); + + // TODO: update the size of cpu_target here to re + + commands.spawn(ImageCopier::new( + camera_setup.target.as_ref().unwrap().clone(), + camera_setup.cpu_target.as_ref().unwrap().clone(), + size, + &render_device, + )); + // image_copier.disable();) + + // let bytes = [ + // 255, 100, 100, 255, 100, 255, 100, 100, 100, 100, 255, 100, 0, 0, 0, 255, + // ]; + // let img_data = arboard::ImageData { + // width: 2, + // height: 2, + // bytes: bytes.as_ref().into(), + // }; + // ctx.set_image(img_data).unwrap(); } } } @@ -191,6 +211,15 @@ fn copy_to_clipboard( mut images: ResMut>, ) { for (entity, copier) in q_copier.iter() { + if copier.copy_count() == 0 && copier.render_count() == 0 { + continue; + } + info!( + "Copier: copied: {} rendered: {}", + copier.copy_count(), + copier.render_count() + ); + let mut ctx = Clipboard::new().unwrap(); let view_texture = images @@ -586,8 +615,8 @@ fn update_camera( info!("Window resized - resizing all textures"); // TODO: Do we really want to resize these images every time? - let image_width = physical_view_width as f32;// / window.scale_factor() as f32; - let image_height = (window.physical_height() - top_panel_height as u32) as f32;// / window.scale_factor() as f32; + let image_width = physical_view_width as f32; // / window.scale_factor() as f32; + let image_height = (window.physical_height() - top_panel_height as u32) as f32; // / window.scale_factor() as f32; images .get_mut(&camera_setup.target.as_ref().unwrap()) @@ -597,21 +626,14 @@ fn update_camera( height: image_height as u32, ..default() }); - images - .get_mut(&camera_setup.cpu_target.as_ref().unwrap()) - .unwrap() - .resize(Extent3d { - width: image_width as u32, - height: image_height as u32, - ..default() - }); if let Ok(mut view_texture_transform) = view_texture.get_single_mut() { view_texture_transform.translation.x = -panel_width / 2.0 / window.scale_factor() as f32; - view_texture_transform.translation.y = top_panel_height / 2.0 / window.scale_factor() as f32; + view_texture_transform.translation.y = + top_panel_height / 2.0 / window.scale_factor() as f32; - view_texture_transform.scale.x = 1.0 / window.scale_factor() as f32; - view_texture_transform.scale.y = 1.0 / window.scale_factor() as f32; + view_texture_transform.scale.x = 1.0 / window.scale_factor() as f32; + view_texture_transform.scale.y = 1.0 / window.scale_factor() as f32; } // println!("{:?}", window); diff --git a/src/image_copy.rs b/src/image_copy.rs index 560afd6..639c160 100644 --- a/src/image_copy.rs +++ b/src/image_copy.rs @@ -14,7 +14,7 @@ use bevy::render::render_resource::{ }; use pollster::FutureExt; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicI32, Ordering}; pub fn receive_images( image_copiers: Query<&ImageCopier>, @@ -46,6 +46,8 @@ pub fn receive_images( image_copier.buffer.unmap(); } .block_on(); + + image_copier.copied(); } } @@ -79,6 +81,9 @@ pub struct ImageCopier { dst_image: Handle, padded_bytes_per_row: usize, + + copied: Arc, + rendered: Arc, } impl ImageCopier { @@ -89,7 +94,7 @@ impl ImageCopier { render_device: &RenderDevice, ) -> ImageCopier { let padded_bytes_per_row = - RenderDevice::align_copy_bytes_per_row((size.width) as usize) * 4; + RenderDevice::align_copy_bytes_per_row((size.width) as usize * 4); let cpu_buffer = render_device.create_buffer(&BufferDescriptor { label: None, @@ -104,6 +109,9 @@ impl ImageCopier { dst_image, enabled: Arc::new(AtomicBool::new(true)), padded_bytes_per_row, + + copied: Arc::new(AtomicI32::new(0)), + rendered: Arc::new(AtomicI32::new(0)), } } @@ -122,6 +130,21 @@ impl ImageCopier { pub fn padded_bytes_per_row(&self) -> usize { self.padded_bytes_per_row } + + pub fn copy_count(&self) -> i32 { + self.copied.load(Ordering::Relaxed) + } + pub fn render_count(&self) -> i32 { + self.rendered.load(Ordering::Relaxed) + } + + pub fn copied(&self) { + self.copied.fetch_add(1, Ordering::Relaxed); + } + + pub fn rendered(&self) { + self.rendered.fetch_add(1, Ordering::Relaxed); + } } pub fn image_copy_extract(mut commands: Commands, image_copiers: Extract>) { @@ -184,6 +207,8 @@ impl render_graph::Node for ImageCopyDriver { let render_queue = world.get_resource::().unwrap(); render_queue.submit(std::iter::once(encoder.finish())); + + image_copier.rendered(); } Ok(())