diff options
author | 2023-03-25 10:45:39 -0700 | |
---|---|---|
committer | 2023-06-06 15:37:30 +0200 | |
commit | 233196eb14b40f8bd5201ea0262571f82136ad53 (patch) | |
tree | 410cf71cda98bdcb55ecc384f7fbc2f4ef669edd /wgpu/src/backend.rs | |
parent | c15f1b5f6575792cc89bb5fba2e613428397e46a (diff) | |
download | iced-233196eb14b40f8bd5201ea0262571f82136ad53.tar.gz iced-233196eb14b40f8bd5201ea0262571f82136ad53.tar.bz2 iced-233196eb14b40f8bd5201ea0262571f82136ad53.zip |
Added offscreen rendering support for wgpu & tiny-skia exposed with the window::screenshot command.
Diffstat (limited to '')
-rw-r--r-- | wgpu/src/backend.rs | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index b524c615..8f37f285 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -1,4 +1,3 @@ -use crate::core; use crate::core::{Color, Font, Point, Size}; use crate::graphics::backend; use crate::graphics::color; @@ -6,6 +5,7 @@ use crate::graphics::{Primitive, Transformation, Viewport}; use crate::quad; use crate::text; use crate::triangle; +use crate::{core, offscreen}; use crate::{Layer, Settings}; #[cfg(feature = "tracing")] @@ -123,6 +123,65 @@ impl Backend { self.image_pipeline.end_frame(); } + /// Performs an offscreen render pass. If the `format` selected by WGPU is not + /// `wgpu::TextureFormat::Rgba8UnormSrgb`, a conversion compute pipeline will run. + /// + /// Returns `None` if the `frame` is `Rgba8UnormSrgb`, else returns the newly + /// converted texture view in `Rgba8UnormSrgb`. + pub fn offscreen<T: AsRef<str>>( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + clear_color: Option<Color>, + frame: &wgpu::TextureView, + format: wgpu::TextureFormat, + primitives: &[Primitive], + viewport: &Viewport, + overlay_text: &[T], + texture_extent: wgpu::Extent3d, + ) -> Option<wgpu::Texture> { + #[cfg(feature = "tracing")] + let _ = info_span!("iced_wgpu::offscreen", "DRAW").entered(); + + self.present( + device, + queue, + encoder, + clear_color, + frame, + primitives, + viewport, + overlay_text, + ); + + if format != wgpu::TextureFormat::Rgba8UnormSrgb { + log::info!("Texture format is {format:?}; performing conversion to rgba8.."); + let pipeline = offscreen::Pipeline::new(device); + + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("iced_wgpu.offscreen.conversion.source_texture"), + size: texture_extent, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::STORAGE_BINDING + | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }); + + let view = + texture.create_view(&wgpu::TextureViewDescriptor::default()); + + pipeline.convert(device, texture_extent, frame, &view, encoder); + + return Some(texture); + } + + None + } + fn prepare_text( &mut self, device: &wgpu::Device, |