From 233196eb14b40f8bd5201ea0262571f82136ad53 Mon Sep 17 00:00:00 2001 From: Bingus Date: Sat, 25 Mar 2023 10:45:39 -0700 Subject: [PATCH 01/16] Added offscreen rendering support for wgpu & tiny-skia exposed with the window::screenshot command. --- examples/screenshot/Cargo.toml | 11 + examples/screenshot/src/main.rs | 305 ++++++++++++++++++++++++++++ graphics/src/compositor.rs | 15 +- renderer/src/compositor.rs | 30 +++ runtime/src/lib.rs | 2 + runtime/src/screenshot.rs | 80 ++++++++ runtime/src/window.rs | 8 + runtime/src/window/action.rs | 9 + tiny_skia/src/window/compositor.rs | 76 ++++++- wgpu/src/backend.rs | 61 +++++- wgpu/src/lib.rs | 1 + wgpu/src/offscreen.rs | 102 ++++++++++ wgpu/src/shader/offscreen_blit.wgsl | 22 ++ wgpu/src/triangle/msaa.rs | 11 +- wgpu/src/window/compositor.rs | 143 ++++++++++++- winit/src/application.rs | 41 +++- 16 files changed, 893 insertions(+), 24 deletions(-) create mode 100644 examples/screenshot/Cargo.toml create mode 100644 examples/screenshot/src/main.rs create mode 100644 runtime/src/screenshot.rs create mode 100644 wgpu/src/offscreen.rs create mode 100644 wgpu/src/shader/offscreen_blit.wgsl diff --git a/examples/screenshot/Cargo.toml b/examples/screenshot/Cargo.toml new file mode 100644 index 0000000000..b79300b7e4 --- /dev/null +++ b/examples/screenshot/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "screenshot" +version = "0.1.0" +authors = ["Bingus "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../..", features = ["debug", "image", "advanced"] } +image = { version = "0.24.6", features = ["png"]} +env_logger = "0.10.0" diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs new file mode 100644 index 0000000000..29d961d986 --- /dev/null +++ b/examples/screenshot/src/main.rs @@ -0,0 +1,305 @@ +use iced::alignment::{Horizontal, Vertical}; +use iced::keyboard::KeyCode; +use iced::theme::{Button, Container}; +use iced::widget::runtime::{CropError, Screenshot}; +use iced::widget::{ + button, column as col, container, image as iced_image, row, text, + text_input, +}; +use iced::{ + event, executor, keyboard, subscription, Alignment, Application, Command, + ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, + Theme, +}; +use image as img; +use image::ColorType; + +fn main() -> iced::Result { + env_logger::builder().format_timestamp(None).init(); + + Example::run(iced::Settings::default()) +} + +struct Example { + screenshot: Option, + saved_png_path: Option>, + png_saving: bool, + crop_error: Option, + x_input_value: u32, + y_input_value: u32, + width_input_value: u32, + height_input_value: u32, +} + +#[derive(Clone, Debug)] +enum Message { + Crop, + Screenshot, + ScreenshotData(Screenshot), + Png, + PngSaved(Result), + XInputChanged(String), + YInputChanged(String), + WidthInputChanged(String), + HeightInputChanged(String), +} + +impl Application for Example { + type Executor = executor::Default; + type Message = Message; + type Theme = Theme; + type Flags = (); + + fn new(_flags: Self::Flags) -> (Self, Command) { + ( + Example { + screenshot: None, + saved_png_path: None, + png_saving: false, + crop_error: None, + x_input_value: 0, + y_input_value: 0, + width_input_value: 0, + height_input_value: 0, + }, + Command::none(), + ) + } + + fn title(&self) -> String { + "Screenshot".to_string() + } + + fn update(&mut self, message: Self::Message) -> Command { + match message { + Message::Screenshot => { + return iced::window::screenshot(Message::ScreenshotData); + } + Message::ScreenshotData(screenshot) => { + self.screenshot = Some(screenshot); + } + Message::Png => { + if let Some(screenshot) = &self.screenshot { + return Command::perform( + save_to_png(screenshot.clone()), + Message::PngSaved, + ); + } + self.png_saving = true; + } + Message::PngSaved(res) => { + self.png_saving = false; + self.saved_png_path = Some(res); + } + Message::XInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.x_input_value = value; + } + } + Message::YInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.y_input_value = value; + } + } + Message::WidthInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.width_input_value = value; + } + } + Message::HeightInputChanged(new) => { + if let Ok(value) = new.parse::() { + self.height_input_value = value; + } + } + Message::Crop => { + if let Some(screenshot) = &self.screenshot { + let cropped = screenshot.crop(Rectangle:: { + x: self.x_input_value, + y: self.y_input_value, + width: self.width_input_value, + height: self.height_input_value, + }); + + match cropped { + Ok(screenshot) => { + self.screenshot = Some(screenshot); + self.crop_error = None; + } + Err(crop_error) => { + self.crop_error = Some(crop_error); + } + } + } + } + } + + Command::none() + } + + fn view(&self) -> Element<'_, Self::Message, Renderer> { + let image: Element = if let Some(screenshot) = &self.screenshot + { + iced_image(iced_image::Handle::from_pixels( + screenshot.size.width, + screenshot.size.height, + screenshot.bytes.clone(), + )) + .content_fit(ContentFit::ScaleDown) + .width(Length::Fill) + .height(Length::Fill) + .into() + } else { + text("Press the button to take a screenshot!").into() + }; + + let image = container(image) + .padding(10) + .style(Container::Custom(Box::new(ScreenshotDisplayContainer))) + .width(Length::FillPortion(2)) + .height(Length::Fill) + .center_x() + .center_y(); + + let crop_origin_controls = row![ + text("X:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.x_input_value),) + .on_input(Message::XInputChanged) + .width(40), + text("Y:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.y_input_value),) + .on_input(Message::YInputChanged) + .width(40), + ] + .spacing(10) + .align_items(Alignment::Center); + + let crop_dimension_controls = row![ + text("W:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.width_input_value),) + .on_input(Message::WidthInputChanged) + .width(40), + text("H:").vertical_alignment(Vertical::Center).width(14), + text_input("0", &format!("{}", self.height_input_value),) + .on_input(Message::HeightInputChanged) + .width(40), + ] + .spacing(10) + .align_items(Alignment::Center); + + let mut crop_controls = + col![crop_origin_controls, crop_dimension_controls] + .spacing(10) + .align_items(Alignment::Center); + + if let Some(crop_error) = &self.crop_error { + crop_controls = crop_controls + .push(text(format!("Crop error! \n{}", crop_error))); + } + + let png_button = if !self.png_saving { + button("Save to png.") + .style(Button::Secondary) + .padding([10, 20, 10, 20]) + .on_press(Message::Png) + } else { + button("Saving..") + .style(Button::Secondary) + .padding([10, 20, 10, 20]) + }; + + let mut controls = col![ + button("Screenshot!") + .padding([10, 20, 10, 20]) + .on_press(Message::Screenshot), + button("Crop") + .style(Button::Destructive) + .padding([10, 20, 10, 20]) + .on_press(Message::Crop), + crop_controls, + png_button, + ] + .spacing(40) + .align_items(Alignment::Center); + + if let Some(png_result) = &self.saved_png_path { + let msg = match png_result { + Ok(path) => format!("Png saved as: {:?}!", path), + Err(msg) => { + format!("Png could not be saved due to:\n{:?}", msg) + } + }; + + controls = controls.push(text(msg)); + } + + let side_content = container(controls) + .align_x(Horizontal::Center) + .width(Length::FillPortion(1)) + .height(Length::Fill) + .center_y() + .center_x(); + + let content = row![side_content, image] + .width(Length::Fill) + .height(Length::Fill) + .align_items(Alignment::Center); + + container(content) + .padding(10) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } + + fn subscription(&self) -> Subscription { + subscription::events_with(|event, status| { + if let event::Status::Captured = status { + return None; + } + + if let Event::Keyboard(keyboard::Event::KeyPressed { + key_code: KeyCode::F5, + .. + }) = event + { + Some(Message::Screenshot) + } else { + None + } + }) + } +} + +struct ScreenshotDisplayContainer; + +impl container::StyleSheet for ScreenshotDisplayContainer { + type Style = Theme; + + fn appearance(&self, style: &Self::Style) -> container::Appearance { + container::Appearance { + text_color: None, + background: None, + border_radius: 5.0, + border_width: 4.0, + border_color: style.palette().primary, + } + } +} + +async fn save_to_png(screenshot: Screenshot) -> Result { + let path = "screenshot.png".to_string(); + img::save_buffer( + &path, + &screenshot.bytes, + screenshot.size.width, + screenshot.size.height, + ColorType::Rgba8, + ) + .map(|_| path) + .map_err(|err| PngError(format!("{:?}", err))) +} + +#[derive(Clone, Debug)] +struct PngError(String); diff --git a/graphics/src/compositor.rs b/graphics/src/compositor.rs index d55e801a54..f7b8604581 100644 --- a/graphics/src/compositor.rs +++ b/graphics/src/compositor.rs @@ -59,6 +59,19 @@ pub trait Compositor: Sized { background_color: Color, overlay: &[T], ) -> Result<(), SurfaceError>; + + /// Screenshots the current [`Renderer`] primitives to an offscreen texture, and returns the bytes of + /// the texture ordered as `RGBA` in the sRGB color space. + /// + /// [`Renderer`]: Self::Renderer; + fn screenshot>( + &mut self, + renderer: &mut Self::Renderer, + surface: &mut Self::Surface, + viewport: &Viewport, + background_color: Color, + overlay: &[T], + ) -> Vec; } /// Result of an unsuccessful call to [`Compositor::present`]. @@ -82,7 +95,7 @@ pub enum SurfaceError { OutOfMemory, } -/// Contains informations about the graphics (e.g. graphics adapter, graphics backend). +/// Contains information about the graphics (e.g. graphics adapter, graphics backend). #[derive(Debug)] pub struct Information { /// Contains the graphics adapter. diff --git a/renderer/src/compositor.rs b/renderer/src/compositor.rs index a353b8e4f2..57317b28f3 100644 --- a/renderer/src/compositor.rs +++ b/renderer/src/compositor.rs @@ -136,6 +136,36 @@ impl crate::graphics::Compositor for Compositor { } }) } + + fn screenshot>( + &mut self, + renderer: &mut Self::Renderer, + surface: &mut Self::Surface, + viewport: &Viewport, + background_color: Color, + overlay: &[T], + ) -> Vec { + renderer.with_primitives(|backend, primitives| match (self, backend, surface) { + (Self::TinySkia(_compositor), crate::Backend::TinySkia(backend), Surface::TinySkia(surface)) => { + iced_tiny_skia::window::compositor::screenshot(surface, backend, primitives, viewport, background_color, overlay) + }, + #[cfg(feature = "wgpu")] + (Self::Wgpu(compositor), crate::Backend::Wgpu(backend), Surface::Wgpu(_)) => { + iced_wgpu::window::compositor::screenshot( + compositor, + backend, + primitives, + viewport, + background_color, + overlay, + ) + }, + #[allow(unreachable_patterns)] + _ => panic!( + "The provided renderer or backend are not compatible with the compositor." + ), + }) + } } enum Candidate { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 50abf7b27e..32ed14d87c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -60,6 +60,7 @@ mod debug; #[cfg(not(feature = "debug"))] #[path = "debug/null.rs"] mod debug; +mod screenshot; pub use iced_core as core; pub use iced_futures as futures; @@ -68,4 +69,5 @@ pub use command::Command; pub use debug::Debug; pub use font::Font; pub use program::Program; +pub use screenshot::{CropError, Screenshot}; pub use user_interface::UserInterface; diff --git a/runtime/src/screenshot.rs b/runtime/src/screenshot.rs new file mode 100644 index 0000000000..527e400f95 --- /dev/null +++ b/runtime/src/screenshot.rs @@ -0,0 +1,80 @@ +use iced_core::{Rectangle, Size}; +use std::fmt::{Debug, Formatter}; + +/// Data of a screenshot, captured with `window::screenshot()`. +/// +/// The `bytes` of this screenshot will always be ordered as `RGBA` in the sRGB color space. +#[derive(Clone)] +pub struct Screenshot { + /// The bytes of the [`Screenshot`]. + pub bytes: Vec, + /// The size of the [`Screenshot`]. + pub size: Size, +} + +impl Debug for Screenshot { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!( + f, + "Screenshot: {{ \n bytes: {}\n size: {:?} }}", + self.bytes.len(), + self.size + ) + } +} + +impl Screenshot { + /// Creates a new [`Screenshot`]. + pub fn new(bytes: Vec, size: Size) -> Self { + Self { bytes, size } + } + + /// Crops a [`Screenshot`] to the provided `region`. This will always be relative to the + /// top-left corner of the [`Screenshot`]. + pub fn crop(&self, region: Rectangle) -> Result { + if region.width == 0 || region.height == 0 { + return Err(CropError::Zero); + } + + if region.x + region.width > self.size.width + || region.y + region.height > self.size.height + { + return Err(CropError::OutOfBounds); + } + + // Image is always RGBA8 = 4 bytes per pixel + const PIXEL_SIZE: usize = 4; + + let bytes_per_row = self.size.width as usize * PIXEL_SIZE; + let row_range = region.y as usize..(region.y + region.height) as usize; + let column_range = region.x as usize * PIXEL_SIZE + ..(region.x + region.width) as usize * PIXEL_SIZE; + + let chopped = self.bytes.chunks(bytes_per_row).enumerate().fold( + vec![], + |mut acc, (row, bytes)| { + if row_range.contains(&row) { + acc.extend(&bytes[column_range.clone()]); + } + + acc + }, + ); + + Ok(Self { + bytes: chopped, + size: Size::new(region.width, region.height), + }) + } +} + +#[derive(Debug, thiserror::Error)] +/// Errors that can occur when cropping a [`Screenshot`]. +pub enum CropError { + #[error("The cropped region is out of bounds.")] + /// The cropped region's size is out of bounds. + OutOfBounds, + #[error("The cropped region is not visible.")] + /// The cropped region's size is zero. + Zero, +} diff --git a/runtime/src/window.rs b/runtime/src/window.rs index d411129380..9b66cb0e65 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -7,6 +7,7 @@ use crate::command::{self, Command}; use crate::core::time::Instant; use crate::core::window::{Event, Icon, Level, Mode, UserAttention}; use crate::futures::subscription::{self, Subscription}; +use crate::screenshot::Screenshot; /// Subscribes to the frames of the window of the running application. /// @@ -115,3 +116,10 @@ pub fn fetch_id( pub fn change_icon(icon: Icon) -> Command { Command::single(command::Action::Window(Action::ChangeIcon(icon))) } + +/// Captures a [`Screenshot`] from the window. +pub fn screenshot( + f: impl FnOnce(Screenshot) -> Message + Send + 'static, +) -> Command { + Command::single(command::Action::Window(Action::Screenshot(Box::new(f)))) +} diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index a9d2a3d0d8..cb43068190 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -1,6 +1,7 @@ use crate::core::window::{Icon, Level, Mode, UserAttention}; use crate::futures::MaybeSend; +use crate::screenshot::Screenshot; use std::fmt; /// An operation to be performed on some window. @@ -89,6 +90,8 @@ pub enum Action { /// - **X11:** Has no universal guidelines for icon sizes, so you're at the whims of the WM. That /// said, it's usually in the same ballpark as on Windows. ChangeIcon(Icon), + /// Screenshot the viewport of the window. + Screenshot(Box T + 'static>), } impl Action { @@ -118,6 +121,11 @@ impl Action { Self::ChangeLevel(level) => Action::ChangeLevel(level), Self::FetchId(o) => Action::FetchId(Box::new(move |s| f(o(s)))), Self::ChangeIcon(icon) => Action::ChangeIcon(icon), + Self::Screenshot(tag) => { + Action::Screenshot(Box::new(move |screenshot| { + f(tag(screenshot)) + })) + } } } } @@ -155,6 +163,7 @@ impl fmt::Debug for Action { Self::ChangeIcon(_icon) => { write!(f, "Action::ChangeIcon(icon)") } + Self::Screenshot(_) => write!(f, "Action::Screenshot"), } } } diff --git a/tiny_skia/src/window/compositor.rs b/tiny_skia/src/window/compositor.rs index 9999a18888..f3be3f16b3 100644 --- a/tiny_skia/src/window/compositor.rs +++ b/tiny_skia/src/window/compositor.rs @@ -1,5 +1,5 @@ -use crate::core::{Color, Rectangle}; -use crate::graphics::compositor::{self, Information, SurfaceError}; +use crate::core::{Color, Rectangle, Size}; +use crate::graphics::compositor::{self, Information}; use crate::graphics::damage; use crate::graphics::{Error, Primitive, Viewport}; use crate::{Backend, Renderer, Settings}; @@ -79,7 +79,7 @@ impl crate::graphics::Compositor for Compositor { viewport: &Viewport, background_color: Color, overlay: &[T], - ) -> Result<(), SurfaceError> { + ) -> Result<(), compositor::SurfaceError> { renderer.with_primitives(|backend, primitives| { present( backend, @@ -91,6 +91,26 @@ impl crate::graphics::Compositor for Compositor { ) }) } + + fn screenshot>( + &mut self, + renderer: &mut Self::Renderer, + surface: &mut Self::Surface, + viewport: &Viewport, + background_color: Color, + overlay: &[T], + ) -> Vec { + renderer.with_primitives(|backend, primitives| { + screenshot( + surface, + backend, + primitives, + viewport, + background_color, + overlay, + ) + }) + } } pub fn new(settings: Settings) -> (Compositor, Backend) { @@ -156,3 +176,53 @@ pub fn present>( Ok(()) } + +pub fn screenshot>( + surface: &mut Surface, + backend: &mut Backend, + primitives: &[Primitive], + viewport: &Viewport, + background_color: Color, + overlay: &[T], +) -> Vec { + let size = viewport.physical_size(); + + let mut offscreen_buffer: Vec = + vec![0; size.width as usize * size.height as usize]; + + backend.draw( + &mut tiny_skia::PixmapMut::from_bytes( + bytemuck::cast_slice_mut(&mut offscreen_buffer), + size.width, + size.height, + ) + .expect("Create offscreen pixel map"), + &mut surface.clip_mask, + primitives, + viewport, + &[Rectangle::with_size(Size::new( + size.width as f32, + size.height as f32, + ))], + background_color, + overlay, + ); + + offscreen_buffer.iter().fold( + Vec::with_capacity(offscreen_buffer.len() * 4), + |mut acc, pixel| { + const A_MASK: u32 = 0xFF_00_00_00; + const R_MASK: u32 = 0x00_FF_00_00; + const G_MASK: u32 = 0x00_00_FF_00; + const B_MASK: u32 = 0x00_00_00_FF; + + let a = ((A_MASK & pixel) >> 24) as u8; + let r = ((R_MASK & pixel) >> 16) as u8; + let g = ((G_MASK & pixel) >> 8) as u8; + let b = (B_MASK & pixel) as u8; + + acc.extend([r, g, b, a]); + acc + }, + ) +} diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index b524c6152a..8f37f28510 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>( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + clear_color: Option, + frame: &wgpu::TextureView, + format: wgpu::TextureFormat, + primitives: &[Primitive], + viewport: &Viewport, + overlay_text: &[T], + texture_extent: wgpu::Extent3d, + ) -> Option { + #[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, diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 0a5726b558..827acb892a 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -46,6 +46,7 @@ pub mod geometry; mod backend; mod buffer; +mod offscreen; mod quad; mod text; mod triangle; diff --git a/wgpu/src/offscreen.rs b/wgpu/src/offscreen.rs new file mode 100644 index 0000000000..29913d0244 --- /dev/null +++ b/wgpu/src/offscreen.rs @@ -0,0 +1,102 @@ +use std::borrow::Cow; + +/// A simple compute pipeline to convert any texture to Rgba8UnormSrgb. +#[derive(Debug)] +pub struct Pipeline { + pipeline: wgpu::ComputePipeline, + layout: wgpu::BindGroupLayout, +} + +impl Pipeline { + pub fn new(device: &wgpu::Device) -> Self { + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("iced_wgpu.offscreen.blit.shader"), + source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!( + "shader/offscreen_blit.wgsl" + ))), + }); + + let bind_group_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu.offscreen.blit.bind_group_layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::COMPUTE, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: false, + }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::COMPUTE, + ty: wgpu::BindingType::StorageTexture { + access: wgpu::StorageTextureAccess::WriteOnly, + format: wgpu::TextureFormat::Rgba8Unorm, + view_dimension: wgpu::TextureViewDimension::D2, + }, + count: None, + }, + ], + }); + + let pipeline_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("iced_wgpu.offscreen.blit.pipeline_layout"), + bind_group_layouts: &[&bind_group_layout], + push_constant_ranges: &[], + }); + + let pipeline = + device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + label: Some("iced_wgpu.offscreen.blit.pipeline"), + layout: Some(&pipeline_layout), + module: &shader, + entry_point: "main", + }); + + Self { + pipeline, + layout: bind_group_layout, + } + } + + pub fn convert( + &self, + device: &wgpu::Device, + extent: wgpu::Extent3d, + frame: &wgpu::TextureView, + view: &wgpu::TextureView, + encoder: &mut wgpu::CommandEncoder, + ) { + let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu.offscreen.blit.bind_group"), + layout: &self.layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(frame), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::TextureView(view), + }, + ], + }); + + let mut compute_pass = + encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { + label: Some("iced_wgpu.offscreen.blit.compute_pass"), + }); + + compute_pass.set_pipeline(&self.pipeline); + compute_pass.set_bind_group(0, &bind, &[]); + compute_pass.dispatch_workgroups(extent.width, extent.height, 1); + } +} diff --git a/wgpu/src/shader/offscreen_blit.wgsl b/wgpu/src/shader/offscreen_blit.wgsl new file mode 100644 index 0000000000..9c764c36dc --- /dev/null +++ b/wgpu/src/shader/offscreen_blit.wgsl @@ -0,0 +1,22 @@ +@group(0) @binding(0) var u_texture: texture_2d; +@group(0) @binding(1) var out_texture: texture_storage_2d; + +fn srgb(color: f32) -> f32 { + if (color <= 0.0031308) { + return 12.92 * color; + } else { + return (1.055 * (pow(color, (1.0/2.4)))) - 0.055; + } +} + +@compute @workgroup_size(1) +fn main(@builtin(global_invocation_id) id: vec3) { + // texture coord must be i32 due to a naga bug: + // https://github.com/gfx-rs/naga/issues/1997 + let coords = vec2(i32(id.x), i32(id.y)); + + let src: vec4 = textureLoad(u_texture, coords, 0); + let srgb_color: vec4 = vec4(srgb(src.x), srgb(src.y), srgb(src.z), src.w); + + textureStore(out_texture, coords, srgb_color); +} diff --git a/wgpu/src/triangle/msaa.rs b/wgpu/src/triangle/msaa.rs index 4afbdb32b9..320b5b124d 100644 --- a/wgpu/src/triangle/msaa.rs +++ b/wgpu/src/triangle/msaa.rs @@ -16,15 +16,8 @@ impl Blit { format: wgpu::TextureFormat, antialiasing: graphics::Antialiasing, ) -> Blit { - let sampler = device.create_sampler(&wgpu::SamplerDescriptor { - address_mode_u: wgpu::AddressMode::ClampToEdge, - address_mode_v: wgpu::AddressMode::ClampToEdge, - address_mode_w: wgpu::AddressMode::ClampToEdge, - mag_filter: wgpu::FilterMode::Nearest, - min_filter: wgpu::FilterMode::Nearest, - mipmap_filter: wgpu::FilterMode::Nearest, - ..Default::default() - }); + let sampler = + device.create_sampler(&wgpu::SamplerDescriptor::default()); let constant_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 2eaafde046..43c3dce58f 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -1,5 +1,5 @@ //! Connect a window with a renderer. -use crate::core::Color; +use crate::core::{Color, Size}; use crate::graphics; use crate::graphics::color; use crate::graphics::compositor; @@ -283,4 +283,145 @@ impl graphics::Compositor for Compositor { ) }) } + + fn screenshot>( + &mut self, + renderer: &mut Self::Renderer, + _surface: &mut Self::Surface, + viewport: &Viewport, + background_color: Color, + overlay: &[T], + ) -> Vec { + renderer.with_primitives(|backend, primitives| { + screenshot( + self, + backend, + primitives, + viewport, + background_color, + overlay, + ) + }) + } +} + +/// Renders the current surface to an offscreen buffer. +/// +/// Returns RGBA bytes of the texture data. +pub fn screenshot>( + compositor: &Compositor, + backend: &mut Backend, + primitives: &[Primitive], + viewport: &Viewport, + background_color: Color, + overlay: &[T], +) -> Vec { + let mut encoder = compositor.device.create_command_encoder( + &wgpu::CommandEncoderDescriptor { + label: Some("iced_wgpu.offscreen.encoder"), + }, + ); + + let dimensions = BufferDimensions::new(viewport.physical_size()); + + let texture_extent = wgpu::Extent3d { + width: dimensions.width, + height: dimensions.height, + depth_or_array_layers: 1, + }; + + let texture = compositor.device.create_texture(&wgpu::TextureDescriptor { + label: Some("iced_wgpu.offscreen.source_texture"), + size: texture_extent, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: compositor.format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + + let rgba_texture = backend.offscreen( + &compositor.device, + &compositor.queue, + &mut encoder, + Some(background_color), + &view, + compositor.format, + primitives, + viewport, + overlay, + texture_extent, + ); + + let output_buffer = + compositor.device.create_buffer(&wgpu::BufferDescriptor { + label: Some("iced_wgpu.offscreen.output_texture_buffer"), + size: (dimensions.padded_bytes_per_row * dimensions.height as usize) + as u64, + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + + encoder.copy_texture_to_buffer( + rgba_texture.unwrap_or(texture).as_image_copy(), + wgpu::ImageCopyBuffer { + buffer: &output_buffer, + layout: wgpu::ImageDataLayout { + offset: 0, + bytes_per_row: Some(dimensions.padded_bytes_per_row as u32), + rows_per_image: None, + }, + }, + texture_extent, + ); + + let index = compositor.queue.submit(Some(encoder.finish())); + + let slice = output_buffer.slice(..); + slice.map_async(wgpu::MapMode::Read, |_| {}); + + let _ = compositor + .device + .poll(wgpu::Maintain::WaitForSubmissionIndex(index)); + + let mapped_buffer = slice.get_mapped_range(); + + mapped_buffer.chunks(dimensions.padded_bytes_per_row).fold( + vec![], + |mut acc, row| { + acc.extend(&row[..dimensions.unpadded_bytes_per_row]); + acc + }, + ) +} + +#[derive(Clone, Copy, Debug)] +struct BufferDimensions { + width: u32, + height: u32, + unpadded_bytes_per_row: usize, + padded_bytes_per_row: usize, +} + +impl BufferDimensions { + fn new(size: Size) -> Self { + let unpadded_bytes_per_row = size.width as usize * 4; //slice of buffer per row; always RGBA + let alignment = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize; //256 + let padded_bytes_per_row_padding = + (alignment - unpadded_bytes_per_row % alignment) % alignment; + let padded_bytes_per_row = + unpadded_bytes_per_row + padded_bytes_per_row_padding; + + Self { + width: size.width, + height: size.height, + unpadded_bytes_per_row, + padded_bytes_per_row, + } + } } diff --git a/winit/src/application.rs b/winit/src/application.rs index 4147be1727..5176bec608 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -19,7 +19,7 @@ use crate::graphics::compositor::{self, Compositor}; use crate::runtime::clipboard; use crate::runtime::program::Program; use crate::runtime::user_interface::{self, UserInterface}; -use crate::runtime::{Command, Debug}; +use crate::runtime::{Command, Debug, Screenshot}; use crate::style::application::{Appearance, StyleSheet}; use crate::{Clipboard, Error, Proxy, Settings}; @@ -308,6 +308,8 @@ async fn run_instance( run_command( &application, + &mut compositor, + &mut surface, &mut cache, &state, &mut renderer, @@ -318,7 +320,6 @@ async fn run_instance( &mut proxy, &mut debug, &window, - || compositor.fetch_information(), ); runtime.track(application.subscription().into_recipes()); @@ -382,6 +383,8 @@ async fn run_instance( // Update application update( &mut application, + &mut compositor, + &mut surface, &mut cache, &state, &mut renderer, @@ -392,7 +395,6 @@ async fn run_instance( &mut debug, &mut messages, &window, - || compositor.fetch_information(), ); // Update window @@ -645,8 +647,10 @@ where /// Updates an [`Application`] by feeding it the provided messages, spawning any /// resulting [`Command`], and tracking its [`Subscription`]. -pub fn update( +pub fn update( application: &mut A, + compositor: &mut C, + surface: &mut C::Surface, cache: &mut user_interface::Cache, state: &State, renderer: &mut A::Renderer, @@ -657,8 +661,8 @@ pub fn update( debug: &mut Debug, messages: &mut Vec, window: &winit::window::Window, - graphics_info: impl FnOnce() -> compositor::Information + Copy, ) where + C: Compositor + 'static, ::Theme: StyleSheet, { for message in messages.drain(..) { @@ -676,6 +680,8 @@ pub fn update( run_command( application, + compositor, + surface, cache, state, renderer, @@ -686,7 +692,6 @@ pub fn update( proxy, debug, window, - graphics_info, ); } @@ -695,8 +700,10 @@ pub fn update( } /// Runs the actions of a [`Command`]. -pub fn run_command( +pub fn run_command( application: &A, + compositor: &mut C, + surface: &mut C::Surface, cache: &mut user_interface::Cache, state: &State, renderer: &mut A::Renderer, @@ -707,10 +714,10 @@ pub fn run_command( proxy: &mut winit::event_loop::EventLoopProxy, debug: &mut Debug, window: &winit::window::Window, - _graphics_info: impl FnOnce() -> compositor::Information + Copy, ) where A: Application, E: Executor, + C: Compositor + 'static, ::Theme: StyleSheet, { use crate::runtime::command; @@ -802,12 +809,28 @@ pub fn run_command( .send_event(tag(window.id().into())) .expect("Send message to event loop"); } + window::Action::Screenshot(tag) => { + let bytes = compositor.screenshot( + renderer, + surface, + state.viewport(), + state.background_color(), + &debug.overlay(), + ); + + proxy + .send_event(tag(Screenshot::new( + bytes, + state.physical_size(), + ))) + .expect("Send message to event loop.") + } }, command::Action::System(action) => match action { system::Action::QueryInformation(_tag) => { #[cfg(feature = "system")] { - let graphics_info = _graphics_info(); + let graphics_info = compositor.fetch_information(); let proxy = proxy.clone(); let _ = std::thread::spawn(move || { From 5ed945287745f5c40ca5cc8013458eee89e76f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 15:39:29 +0200 Subject: [PATCH 02/16] Use `Container::Box` in `screenshot` example --- examples/screenshot/src/main.rs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 29d961d986..cfdddd7267 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -154,7 +154,7 @@ impl Application for Example { let image = container(image) .padding(10) - .style(Container::Custom(Box::new(ScreenshotDisplayContainer))) + .style(Container::Box) .width(Length::FillPortion(2)) .height(Length::Fill) .center_x() @@ -272,22 +272,6 @@ impl Application for Example { } } -struct ScreenshotDisplayContainer; - -impl container::StyleSheet for ScreenshotDisplayContainer { - type Style = Theme; - - fn appearance(&self, style: &Self::Style) -> container::Appearance { - container::Appearance { - text_color: None, - background: None, - border_radius: 5.0, - border_width: 4.0, - border_color: style.palette().primary, - } - } -} - async fn save_to_png(screenshot: Screenshot) -> Result { let path = "screenshot.png".to_string(); img::save_buffer( From 8820583cc04f468b403e0660118760389f9e4370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 15:51:32 +0200 Subject: [PATCH 03/16] Create `numeric_input` helper in `screenshot` example --- examples/screenshot/src/main.rs | 98 ++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index cfdddd7267..8e3bcaecb4 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -25,10 +25,10 @@ struct Example { saved_png_path: Option>, png_saving: bool, crop_error: Option, - x_input_value: u32, - y_input_value: u32, - width_input_value: u32, - height_input_value: u32, + x_input_value: Option, + y_input_value: Option, + width_input_value: Option, + height_input_value: Option, } #[derive(Clone, Debug)] @@ -38,10 +38,10 @@ enum Message { ScreenshotData(Screenshot), Png, PngSaved(Result), - XInputChanged(String), - YInputChanged(String), - WidthInputChanged(String), - HeightInputChanged(String), + XInputChanged(Option), + YInputChanged(Option), + WidthInputChanged(Option), + HeightInputChanged(Option), } impl Application for Example { @@ -57,10 +57,10 @@ impl Application for Example { saved_png_path: None, png_saving: false, crop_error: None, - x_input_value: 0, - y_input_value: 0, - width_input_value: 0, - height_input_value: 0, + x_input_value: None, + y_input_value: None, + width_input_value: None, + height_input_value: None, }, Command::none(), ) @@ -91,33 +91,25 @@ impl Application for Example { self.png_saving = false; self.saved_png_path = Some(res); } - Message::XInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.x_input_value = value; - } + Message::XInputChanged(new_value) => { + self.x_input_value = new_value; } - Message::YInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.y_input_value = value; - } + Message::YInputChanged(new_value) => { + self.y_input_value = new_value; } - Message::WidthInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.width_input_value = value; - } + Message::WidthInputChanged(new_value) => { + self.width_input_value = new_value; } - Message::HeightInputChanged(new) => { - if let Ok(value) = new.parse::() { - self.height_input_value = value; - } + Message::HeightInputChanged(new_value) => { + self.height_input_value = new_value; } Message::Crop => { if let Some(screenshot) = &self.screenshot { let cropped = screenshot.crop(Rectangle:: { - x: self.x_input_value, - y: self.y_input_value, - width: self.width_input_value, - height: self.height_input_value, + x: self.x_input_value.unwrap_or(0), + y: self.y_input_value.unwrap_or(0), + width: self.width_input_value.unwrap_or(0), + height: self.height_input_value.unwrap_or(0), }); match cropped { @@ -162,26 +154,20 @@ impl Application for Example { let crop_origin_controls = row![ text("X:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.x_input_value),) - .on_input(Message::XInputChanged) - .width(40), + numeric_input("0", self.x_input_value).map(Message::XInputChanged), text("Y:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.y_input_value),) - .on_input(Message::YInputChanged) - .width(40), + numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) .align_items(Alignment::Center); let crop_dimension_controls = row![ text("W:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.width_input_value),) - .on_input(Message::WidthInputChanged) - .width(40), + numeric_input("0", self.width_input_value) + .map(Message::WidthInputChanged), text("H:").vertical_alignment(Vertical::Center).width(14), - text_input("0", &format!("{}", self.height_input_value),) - .on_input(Message::HeightInputChanged) - .width(40), + numeric_input("0", self.height_input_value) + .map(Message::HeightInputChanged) ] .spacing(10) .align_items(Alignment::Center); @@ -287,3 +273,27 @@ async fn save_to_png(screenshot: Screenshot) -> Result { #[derive(Clone, Debug)] struct PngError(String); + +fn numeric_input( + placeholder: &str, + value: Option, +) -> Element<'_, Option> { + text_input( + placeholder, + &value + .as_ref() + .map(ToString::to_string) + .unwrap_or_else(String::new), + ) + .on_input(move |text| { + if text.is_empty() { + None + } else if let Ok(new_value) = text.parse() { + Some(new_value) + } else { + value + } + }) + .width(40) + .into() +} From cd15f8305ab1094ff9887f8d8822e385a381ab1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 15:59:36 +0200 Subject: [PATCH 04/16] Fix width of crop labels in `screenshot` example --- examples/screenshot/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 8e3bcaecb4..0d77d316cd 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -153,19 +153,19 @@ impl Application for Example { .center_y(); let crop_origin_controls = row![ - text("X:").vertical_alignment(Vertical::Center).width(14), + text("X:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.x_input_value).map(Message::XInputChanged), - text("Y:").vertical_alignment(Vertical::Center).width(14), + text("Y:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) .align_items(Alignment::Center); let crop_dimension_controls = row![ - text("W:").vertical_alignment(Vertical::Center).width(14), + text("W:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.width_input_value) .map(Message::WidthInputChanged), - text("H:").vertical_alignment(Vertical::Center).width(14), + text("H:").vertical_alignment(Vertical::Center).width(20), numeric_input("0", self.height_input_value) .map(Message::HeightInputChanged) ] From c1021c71758d0c850c7b3fea26075bb83830cb7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 15:59:56 +0200 Subject: [PATCH 05/16] Fix punctuation in `screenshot` example --- examples/screenshot/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 0d77d316cd..0f1d8dce8d 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -183,12 +183,12 @@ impl Application for Example { } let png_button = if !self.png_saving { - button("Save to png.") + button("Save to png") .style(Button::Secondary) .padding([10, 20, 10, 20]) .on_press(Message::Png) } else { - button("Saving..") + button("Saving...") .style(Button::Secondary) .padding([10, 20, 10, 20]) }; From 7adfaa88a68e1accfaddf13e82b8bd7a11ee8786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 16:05:46 +0200 Subject: [PATCH 06/16] Avoid `iced_image` import in `screenshot` example --- examples/screenshot/src/main.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 0f1d8dce8d..66cbcd8cd5 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -1,18 +1,17 @@ +use ::image as img; +use ::image::ColorType; use iced::alignment::{Horizontal, Vertical}; use iced::keyboard::KeyCode; use iced::theme::{Button, Container}; use iced::widget::runtime::{CropError, Screenshot}; use iced::widget::{ - button, column as col, container, image as iced_image, row, text, - text_input, + button, column as col, container, image, row, text, text_input, }; use iced::{ event, executor, keyboard, subscription, Alignment, Application, Command, ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, Theme, }; -use image as img; -use image::ColorType; fn main() -> iced::Result { env_logger::builder().format_timestamp(None).init(); @@ -131,7 +130,7 @@ impl Application for Example { fn view(&self) -> Element<'_, Self::Message, Renderer> { let image: Element = if let Some(screenshot) = &self.screenshot { - iced_image(iced_image::Handle::from_pixels( + image(image::Handle::from_pixels( screenshot.size.width, screenshot.size.height, screenshot.bytes.clone(), From 5324928044cba800454b1861eb9999038bc28c2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 16:14:42 +0200 Subject: [PATCH 07/16] Wrap `Screenshot::bytes` in an `Arc` and implement `AsRef<[u8]>` --- examples/screenshot/src/main.rs | 2 +- runtime/src/screenshot.rs | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 66cbcd8cd5..7d53b3b213 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -133,7 +133,7 @@ impl Application for Example { image(image::Handle::from_pixels( screenshot.size.width, screenshot.size.height, - screenshot.bytes.clone(), + screenshot.clone(), )) .content_fit(ContentFit::ScaleDown) .width(Length::Fill) diff --git a/runtime/src/screenshot.rs b/runtime/src/screenshot.rs index 527e400f95..b88f2c2010 100644 --- a/runtime/src/screenshot.rs +++ b/runtime/src/screenshot.rs @@ -1,5 +1,7 @@ -use iced_core::{Rectangle, Size}; +use crate::core::{Rectangle, Size}; + use std::fmt::{Debug, Formatter}; +use std::sync::Arc; /// Data of a screenshot, captured with `window::screenshot()`. /// @@ -7,7 +9,7 @@ use std::fmt::{Debug, Formatter}; #[derive(Clone)] pub struct Screenshot { /// The bytes of the [`Screenshot`]. - pub bytes: Vec, + pub bytes: Arc>, /// The size of the [`Screenshot`]. pub size: Size, } @@ -26,7 +28,10 @@ impl Debug for Screenshot { impl Screenshot { /// Creates a new [`Screenshot`]. pub fn new(bytes: Vec, size: Size) -> Self { - Self { bytes, size } + Self { + bytes: Arc::new(bytes), + size, + } } /// Crops a [`Screenshot`] to the provided `region`. This will always be relative to the @@ -62,12 +67,18 @@ impl Screenshot { ); Ok(Self { - bytes: chopped, + bytes: Arc::new(chopped), size: Size::new(region.width, region.height), }) } } +impl AsRef<[u8]> for Screenshot { + fn as_ref(&self) -> &[u8] { + &self.bytes + } +} + #[derive(Debug, thiserror::Error)] /// Errors that can occur when cropping a [`Screenshot`]. pub enum CropError { From 5b5000e3ae9789cf1d83cecd08f4d0cedc16d788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 16:18:20 +0200 Subject: [PATCH 08/16] Introduce `on_press_maybe` helper for `Button` --- examples/screenshot/src/main.rs | 5 +++-- widget/src/button.rs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 7d53b3b213..be39f829aa 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -79,12 +79,13 @@ impl Application for Example { } Message::Png => { if let Some(screenshot) = &self.screenshot { + self.png_saving = true; + return Command::perform( save_to_png(screenshot.clone()), Message::PngSaved, ); } - self.png_saving = true; } Message::PngSaved(res) => { self.png_saving = false; @@ -185,7 +186,7 @@ impl Application for Example { button("Save to png") .style(Button::Secondary) .padding([10, 20, 10, 20]) - .on_press(Message::Png) + .on_press_maybe(self.screenshot.is_some().then(|| Message::Png)) } else { button("Saving...") .style(Button::Secondary) diff --git a/widget/src/button.rs b/widget/src/button.rs index 70fed1d531..c656142c3b 100644 --- a/widget/src/button.rs +++ b/widget/src/button.rs @@ -102,8 +102,17 @@ where /// Sets the message that will be produced when the [`Button`] is pressed. /// /// Unless `on_press` is called, the [`Button`] will be disabled. - pub fn on_press(mut self, msg: Message) -> Self { - self.on_press = Some(msg); + pub fn on_press(mut self, on_press: Message) -> Self { + self.on_press = Some(on_press); + self + } + + /// Sets the message that will be produced when the [`Button`] is pressed, + /// if `Some`. + /// + /// If `None`, the [`Button`] will be disabled. + pub fn on_press_maybe(mut self, on_press: Option) -> Self { + self.on_press = on_press; self } From 38582873b7d64174f11a7c4d74ed8f654320f7f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 16:32:14 +0200 Subject: [PATCH 09/16] Rearrange controls of the `screenshot` example --- examples/screenshot/src/main.rs | 84 ++++++++++++++++++++------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index be39f829aa..7b9ac4e038 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -1,12 +1,10 @@ use ::image as img; use ::image::ColorType; -use iced::alignment::{Horizontal, Vertical}; +use iced::alignment; use iced::keyboard::KeyCode; use iced::theme::{Button, Container}; use iced::widget::runtime::{CropError, Screenshot}; -use iced::widget::{ - button, column as col, container, image, row, text, text_input, -}; +use iced::widget::{button, column, container, image, row, text, text_input}; use iced::{ event, executor, keyboard, subscription, Alignment, Application, Command, ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, @@ -153,19 +151,27 @@ impl Application for Example { .center_y(); let crop_origin_controls = row![ - text("X:").vertical_alignment(Vertical::Center).width(20), + text("X:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.x_input_value).map(Message::XInputChanged), - text("Y:").vertical_alignment(Vertical::Center).width(20), + text("Y:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) .align_items(Alignment::Center); let crop_dimension_controls = row![ - text("W:").vertical_alignment(Vertical::Center).width(20), + text("W:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.width_input_value) .map(Message::WidthInputChanged), - text("H:").vertical_alignment(Vertical::Center).width(20), + text("H:") + .vertical_alignment(alignment::Vertical::Center) + .width(20), numeric_input("0", self.height_input_value) .map(Message::HeightInputChanged) ] @@ -173,7 +179,7 @@ impl Application for Example { .align_items(Alignment::Center); let mut crop_controls = - col![crop_origin_controls, crop_dimension_controls] + column![crop_origin_controls, crop_dimension_controls] .spacing(10) .align_items(Alignment::Center); @@ -182,30 +188,36 @@ impl Application for Example { .push(text(format!("Crop error! \n{}", crop_error))); } - let png_button = if !self.png_saving { - button("Save to png") - .style(Button::Secondary) - .padding([10, 20, 10, 20]) - .on_press_maybe(self.screenshot.is_some().then(|| Message::Png)) - } else { - button("Saving...") + let mut controls = column![ + column![ + button(centered_text("Screenshot!")) + .padding([10, 20, 10, 20]) + .width(Length::Fill) + .on_press(Message::Screenshot), + if !self.png_saving { + button(centered_text("Save as png")).on_press_maybe( + self.screenshot.is_some().then(|| Message::Png), + ) + } else { + button(centered_text("Saving...")).style(Button::Secondary) + } .style(Button::Secondary) .padding([10, 20, 10, 20]) - }; - - let mut controls = col![ - button("Screenshot!") - .padding([10, 20, 10, 20]) - .on_press(Message::Screenshot), - button("Crop") - .style(Button::Destructive) - .padding([10, 20, 10, 20]) - .on_press(Message::Crop), - crop_controls, - png_button, + .width(Length::Fill) + ] + .spacing(10), + column![ + crop_controls, + button(centered_text("Crop")) + .on_press(Message::Crop) + .style(Button::Destructive) + .padding([10, 20, 10, 20]) + .width(Length::Fill), + ] + .spacing(10) + .align_items(Alignment::Center), ] - .spacing(40) - .align_items(Alignment::Center); + .spacing(40); if let Some(png_result) = &self.saved_png_path { let msg = match png_result { @@ -219,21 +231,22 @@ impl Application for Example { } let side_content = container(controls) - .align_x(Horizontal::Center) + .align_x(alignment::Horizontal::Center) .width(Length::FillPortion(1)) .height(Length::Fill) .center_y() .center_x(); let content = row![side_content, image] + .spacing(10) .width(Length::Fill) .height(Length::Fill) .align_items(Alignment::Center); container(content) - .padding(10) .width(Length::Fill) .height(Length::Fill) + .padding(10) .center_x() .center_y() .into() @@ -297,3 +310,10 @@ fn numeric_input( .width(40) .into() } + +fn centered_text(content: &str) -> Element<'_, Message> { + text(content) + .width(Length::Fill) + .horizontal_alignment(alignment::Horizontal::Center) + .into() +} From 78c0189824bbae2ba679c8f8b5ae9552debcb0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 16:36:20 +0200 Subject: [PATCH 10/16] Fix width of crop labels in `screenshot` example (again) --- examples/screenshot/src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 7b9ac4e038..59dfdf1485 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -153,11 +153,11 @@ impl Application for Example { let crop_origin_controls = row![ text("X:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.x_input_value).map(Message::XInputChanged), text("Y:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.y_input_value).map(Message::YInputChanged) ] .spacing(10) @@ -166,12 +166,12 @@ impl Application for Example { let crop_dimension_controls = row![ text("W:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.width_input_value) .map(Message::WidthInputChanged), text("H:") .vertical_alignment(alignment::Vertical::Center) - .width(20), + .width(30), numeric_input("0", self.height_input_value) .map(Message::HeightInputChanged) ] From 05e238e9ed5f0c6cade87228f8f3044ee26df756 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 8 Jun 2023 10:10:26 -0700 Subject: [PATCH 11/16] Adjusted offscreen pass to be a render pass vs compute for compat with web-colors flag. --- examples/screenshot/src/main.rs | 2 +- wgpu/src/backend.rs | 21 +--- wgpu/src/offscreen.rs | 164 ++++++++++++++++++++++++---- wgpu/src/shader/offscreen_blit.wgsl | 37 ++++--- 4 files changed, 166 insertions(+), 58 deletions(-) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index 59dfdf1485..ef2be4fe7f 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -134,7 +134,7 @@ impl Application for Example { screenshot.size.height, screenshot.clone(), )) - .content_fit(ContentFit::ScaleDown) + .content_fit(ContentFit::Contain) .width(Length::Fill) .height(Length::Fill) .into() diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 8f37f28510..0735f81f6e 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -139,7 +139,7 @@ impl Backend { primitives: &[Primitive], viewport: &Viewport, overlay_text: &[T], - texture_extent: wgpu::Extent3d, + size: wgpu::Extent3d, ) -> Option { #[cfg(feature = "tracing")] let _ = info_span!("iced_wgpu::offscreen", "DRAW").entered(); @@ -159,24 +159,7 @@ impl Backend { 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); + return Some(pipeline.convert(device, frame, size, encoder)); } None diff --git a/wgpu/src/offscreen.rs b/wgpu/src/offscreen.rs index 29913d0244..d0758b667a 100644 --- a/wgpu/src/offscreen.rs +++ b/wgpu/src/offscreen.rs @@ -1,12 +1,24 @@ use std::borrow::Cow; +use wgpu::util::DeviceExt; +use wgpu::vertex_attr_array; /// A simple compute pipeline to convert any texture to Rgba8UnormSrgb. #[derive(Debug)] pub struct Pipeline { - pipeline: wgpu::ComputePipeline, + pipeline: wgpu::RenderPipeline, + vertices: wgpu::Buffer, + indices: wgpu::Buffer, + sampler: wgpu::Sampler, layout: wgpu::BindGroupLayout, } +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] +#[repr(C)] +struct Vertex { + ndc: [f32; 2], + texel: [f32; 2], +} + impl Pipeline { pub fn new(device: &wgpu::Device) -> Self { let shader = @@ -17,13 +29,53 @@ impl Pipeline { ))), }); + let vertices = + device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("iced_wgpu.offscreen.vertex_buffer"), + contents: bytemuck::cast_slice(&[ + //bottom left + Vertex { + ndc: [-1.0, -1.0], + texel: [0.0, 1.0], + }, + //bottom right + Vertex { + ndc: [1.0, -1.0], + texel: [1.0, 1.0], + }, + //top right + Vertex { + ndc: [1.0, 1.0], + texel: [1.0, 0.0], + }, + //top left + Vertex { + ndc: [-1.0, 1.0], + texel: [0.0, 0.0], + }, + ]), + usage: wgpu::BufferUsages::VERTEX, + }); + + let indices = + device.create_buffer_init(&wgpu::util::BufferInitDescriptor { + label: Some("iced_wgpu.offscreen.index_buffer"), + contents: bytemuck::cast_slice(&[0u16, 1, 2, 2, 3, 0]), + usage: wgpu::BufferUsages::INDEX, + }); + + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("iced_wgpu.offscreen.sampler"), + ..Default::default() + }); + let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("iced_wgpu.offscreen.blit.bind_group_layout"), entries: &[ wgpu::BindGroupLayoutEntry { binding: 0, - visibility: wgpu::ShaderStages::COMPUTE, + visibility: wgpu::ShaderStages::FRAGMENT, ty: wgpu::BindingType::Texture { sample_type: wgpu::TextureSampleType::Float { filterable: false, @@ -35,12 +87,10 @@ impl Pipeline { }, wgpu::BindGroupLayoutEntry { binding: 1, - visibility: wgpu::ShaderStages::COMPUTE, - ty: wgpu::BindingType::StorageTexture { - access: wgpu::StorageTextureAccess::WriteOnly, - format: wgpu::TextureFormat::Rgba8Unorm, - view_dimension: wgpu::TextureViewDimension::D2, - }, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler( + wgpu::SamplerBindingType::NonFiltering, + ), count: None, }, ], @@ -54,15 +104,56 @@ impl Pipeline { }); let pipeline = - device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("iced_wgpu.offscreen.blit.pipeline"), layout: Some(&pipeline_layout), - module: &shader, - entry_point: "main", + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[wgpu::VertexBufferLayout { + array_stride: std::mem::size_of::() as u64, + step_mode: wgpu::VertexStepMode::Vertex, + attributes: &vertex_attr_array![ + 0 => Float32x2, // quad ndc pos + 1 => Float32x2, // texture uv + ], + }], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[Some(wgpu::ColorTargetState { + format: wgpu::TextureFormat::Rgba8UnormSrgb, + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::SrcAlpha, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::One, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + }), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Cw, + ..Default::default() + }, + depth_stencil: None, + multisample: Default::default(), + multiview: None, }); Self { pipeline, + vertices, + indices, + sampler, layout: bind_group_layout, } } @@ -70,11 +161,25 @@ impl Pipeline { pub fn convert( &self, device: &wgpu::Device, - extent: wgpu::Extent3d, frame: &wgpu::TextureView, - view: &wgpu::TextureView, + size: wgpu::Extent3d, encoder: &mut wgpu::CommandEncoder, - ) { + ) -> wgpu::Texture { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("iced_wgpu.offscreen.conversion.source_texture"), + size, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8UnormSrgb, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }); + + let view = + &texture.create_view(&wgpu::TextureViewDescriptor::default()); + let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("iced_wgpu.offscreen.blit.bind_group"), layout: &self.layout, @@ -85,18 +190,33 @@ impl Pipeline { }, wgpu::BindGroupEntry { binding: 1, - resource: wgpu::BindingResource::TextureView(view), + resource: wgpu::BindingResource::Sampler(&self.sampler), }, ], }); - let mut compute_pass = - encoder.begin_compute_pass(&wgpu::ComputePassDescriptor { - label: Some("iced_wgpu.offscreen.blit.compute_pass"), - }); + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("iced_wgpu.offscreen.blit.render_pass"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: true, + }, + })], + depth_stencil_attachment: None, + }); + + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, &bind, &[]); + pass.set_vertex_buffer(0, self.vertices.slice(..)); + pass.set_index_buffer( + self.indices.slice(..), + wgpu::IndexFormat::Uint16, + ); + pass.draw_indexed(0..6u32, 0, 0..1); - compute_pass.set_pipeline(&self.pipeline); - compute_pass.set_bind_group(0, &bind, &[]); - compute_pass.dispatch_workgroups(extent.width, extent.height, 1); + texture } } diff --git a/wgpu/src/shader/offscreen_blit.wgsl b/wgpu/src/shader/offscreen_blit.wgsl index 9c764c36dc..08952d62f4 100644 --- a/wgpu/src/shader/offscreen_blit.wgsl +++ b/wgpu/src/shader/offscreen_blit.wgsl @@ -1,22 +1,27 @@ -@group(0) @binding(0) var u_texture: texture_2d; -@group(0) @binding(1) var out_texture: texture_storage_2d; +@group(0) @binding(0) var frame_texture: texture_2d; +@group(0) @binding(1) var frame_sampler: sampler; -fn srgb(color: f32) -> f32 { - if (color <= 0.0031308) { - return 12.92 * color; - } else { - return (1.055 * (pow(color, (1.0/2.4)))) - 0.055; - } +struct VertexInput { + @location(0) v_pos: vec2, + @location(1) texel_coord: vec2, } -@compute @workgroup_size(1) -fn main(@builtin(global_invocation_id) id: vec3) { - // texture coord must be i32 due to a naga bug: - // https://github.com/gfx-rs/naga/issues/1997 - let coords = vec2(i32(id.x), i32(id.y)); +struct VertexOutput { + @builtin(position) clip_pos: vec4, + @location(0) uv: vec2, +} + +@vertex +fn vs_main(input: VertexInput) -> VertexOutput { + var output: VertexOutput; - let src: vec4 = textureLoad(u_texture, coords, 0); - let srgb_color: vec4 = vec4(srgb(src.x), srgb(src.y), srgb(src.z), src.w); + output.clip_pos = vec4(input.v_pos, 0.0, 1.0); + output.uv = input.texel_coord; - textureStore(out_texture, coords, srgb_color); + return output; } + +@fragment +fn fs_main(input: VertexOutput) -> @location(0) vec4 { + return textureSample(frame_texture, frame_sampler, input.uv); +} \ No newline at end of file From af099fa6d72d9c3e23c85a36aee14904526d02f0 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 8 Jun 2023 10:17:53 -0700 Subject: [PATCH 12/16] Added in check for web-colors. --- wgpu/src/offscreen.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/wgpu/src/offscreen.rs b/wgpu/src/offscreen.rs index d0758b667a..baa069e8b9 100644 --- a/wgpu/src/offscreen.rs +++ b/wgpu/src/offscreen.rs @@ -1,3 +1,4 @@ +use crate::graphics::color; use std::borrow::Cow; use wgpu::util::DeviceExt; use wgpu::vertex_attr_array; @@ -16,7 +17,7 @@ pub struct Pipeline { #[repr(C)] struct Vertex { ndc: [f32; 2], - texel: [f32; 2], + uv: [f32; 2], } impl Pipeline { @@ -36,22 +37,22 @@ impl Pipeline { //bottom left Vertex { ndc: [-1.0, -1.0], - texel: [0.0, 1.0], + uv: [0.0, 1.0], }, //bottom right Vertex { ndc: [1.0, -1.0], - texel: [1.0, 1.0], + uv: [1.0, 1.0], }, //top right Vertex { ndc: [1.0, 1.0], - texel: [1.0, 0.0], + uv: [1.0, 0.0], }, //top left Vertex { ndc: [-1.0, 1.0], - texel: [0.0, 0.0], + uv: [0.0, 0.0], }, ]), usage: wgpu::BufferUsages::VERTEX, @@ -123,7 +124,11 @@ impl Pipeline { module: &shader, entry_point: "fs_main", targets: &[Some(wgpu::ColorTargetState { - format: wgpu::TextureFormat::Rgba8UnormSrgb, + format: if color::GAMMA_CORRECTION { + wgpu::TextureFormat::Rgba8UnormSrgb + } else { + wgpu::TextureFormat::Rgba8Unorm + }, blend: Some(wgpu::BlendState { color: wgpu::BlendComponent { src_factor: wgpu::BlendFactor::SrcAlpha, @@ -171,7 +176,11 @@ impl Pipeline { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8UnormSrgb, + format: if color::GAMMA_CORRECTION { + wgpu::TextureFormat::Rgba8UnormSrgb + } else { + wgpu::TextureFormat::Rgba8Unorm + }, usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, view_formats: &[], From d955b3444da19e24bf0de6d6f432f06623ed5db2 Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 14 Jun 2023 11:10:09 -0700 Subject: [PATCH 13/16] Replaced offscreen_blit.wgsl with existing blit.wgsl. --- wgpu/src/offscreen.rs | 163 ++++++++++------------------ wgpu/src/shader/offscreen_blit.wgsl | 27 ----- 2 files changed, 60 insertions(+), 130 deletions(-) delete mode 100644 wgpu/src/shader/offscreen_blit.wgsl diff --git a/wgpu/src/offscreen.rs b/wgpu/src/offscreen.rs index baa069e8b9..b25602e407 100644 --- a/wgpu/src/offscreen.rs +++ b/wgpu/src/offscreen.rs @@ -1,16 +1,12 @@ use crate::graphics::color; use std::borrow::Cow; -use wgpu::util::DeviceExt; -use wgpu::vertex_attr_array; /// A simple compute pipeline to convert any texture to Rgba8UnormSrgb. #[derive(Debug)] pub struct Pipeline { pipeline: wgpu::RenderPipeline, - vertices: wgpu::Buffer, - indices: wgpu::Buffer, - sampler: wgpu::Sampler, - layout: wgpu::BindGroupLayout, + sampler_bind_group: wgpu::BindGroup, + texture_layout: wgpu::BindGroupLayout, } #[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] @@ -22,88 +18,67 @@ struct Vertex { impl Pipeline { pub fn new(device: &wgpu::Device) -> Self { - let shader = - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("iced_wgpu.offscreen.blit.shader"), - source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!( - "shader/offscreen_blit.wgsl" - ))), - }); - - let vertices = - device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("iced_wgpu.offscreen.vertex_buffer"), - contents: bytemuck::cast_slice(&[ - //bottom left - Vertex { - ndc: [-1.0, -1.0], - uv: [0.0, 1.0], - }, - //bottom right - Vertex { - ndc: [1.0, -1.0], - uv: [1.0, 1.0], - }, - //top right - Vertex { - ndc: [1.0, 1.0], - uv: [1.0, 0.0], - }, - //top left - Vertex { - ndc: [-1.0, 1.0], - uv: [0.0, 0.0], - }, - ]), - usage: wgpu::BufferUsages::VERTEX, - }); - - let indices = - device.create_buffer_init(&wgpu::util::BufferInitDescriptor { - label: Some("iced_wgpu.offscreen.index_buffer"), - contents: bytemuck::cast_slice(&[0u16, 1, 2, 2, 3, 0]), - usage: wgpu::BufferUsages::INDEX, - }); - let sampler = device.create_sampler(&wgpu::SamplerDescriptor { label: Some("iced_wgpu.offscreen.sampler"), ..Default::default() }); - let bind_group_layout = + //sampler in 0 + let sampler_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("iced_wgpu.offscreen.blit.bind_group_layout"), - entries: &[ - wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Texture { - sample_type: wgpu::TextureSampleType::Float { - filterable: false, - }, - view_dimension: wgpu::TextureViewDimension::D2, - multisampled: false, + label: Some("iced_wgpu.offscreen.blit.sampler_layout"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler( + wgpu::SamplerBindingType::NonFiltering, + ), + count: None, + }], + }); + + let sampler_bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu.offscreen.sampler.bind_group"), + layout: &sampler_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Sampler(&sampler), + }], + }); + + let texture_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu.offscreen.blit.texture_layout"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: false, }, - count: None, - }, - wgpu::BindGroupLayoutEntry { - binding: 1, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Sampler( - wgpu::SamplerBindingType::NonFiltering, - ), - count: None, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, }, - ], + count: None, + }], }); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("iced_wgpu.offscreen.blit.pipeline_layout"), - bind_group_layouts: &[&bind_group_layout], + bind_group_layouts: &[&sampler_layout, &texture_layout], push_constant_ranges: &[], }); + let shader = + device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("iced_wgpu.offscreen.blit.shader"), + source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!( + "shader/blit.wgsl" + ))), + }); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some("iced_wgpu.offscreen.blit.pipeline"), @@ -111,14 +86,7 @@ impl Pipeline { vertex: wgpu::VertexState { module: &shader, entry_point: "vs_main", - buffers: &[wgpu::VertexBufferLayout { - array_stride: std::mem::size_of::() as u64, - step_mode: wgpu::VertexStepMode::Vertex, - attributes: &vertex_attr_array![ - 0 => Float32x2, // quad ndc pos - 1 => Float32x2, // texture uv - ], - }], + buffers: &[], }, fragment: Some(wgpu::FragmentState { module: &shader, @@ -156,10 +124,8 @@ impl Pipeline { Self { pipeline, - vertices, - indices, - sampler, - layout: bind_group_layout, + sampler_bind_group, + texture_layout, } } @@ -189,20 +155,15 @@ impl Pipeline { let view = &texture.create_view(&wgpu::TextureViewDescriptor::default()); - let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu.offscreen.blit.bind_group"), - layout: &self.layout, - entries: &[ - wgpu::BindGroupEntry { + let texture_bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu.offscreen.blit.texture_bind_group"), + layout: &self.texture_layout, + entries: &[wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(frame), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::Sampler(&self.sampler), - }, - ], - }); + }], + }); let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("iced_wgpu.offscreen.blit.render_pass"), @@ -218,13 +179,9 @@ impl Pipeline { }); pass.set_pipeline(&self.pipeline); - pass.set_bind_group(0, &bind, &[]); - pass.set_vertex_buffer(0, self.vertices.slice(..)); - pass.set_index_buffer( - self.indices.slice(..), - wgpu::IndexFormat::Uint16, - ); - pass.draw_indexed(0..6u32, 0, 0..1); + pass.set_bind_group(0, &self.sampler_bind_group, &[]); + pass.set_bind_group(1, &texture_bind_group, &[]); + pass.draw(0..6, 0..1); texture } diff --git a/wgpu/src/shader/offscreen_blit.wgsl b/wgpu/src/shader/offscreen_blit.wgsl deleted file mode 100644 index 08952d62f4..0000000000 --- a/wgpu/src/shader/offscreen_blit.wgsl +++ /dev/null @@ -1,27 +0,0 @@ -@group(0) @binding(0) var frame_texture: texture_2d; -@group(0) @binding(1) var frame_sampler: sampler; - -struct VertexInput { - @location(0) v_pos: vec2, - @location(1) texel_coord: vec2, -} - -struct VertexOutput { - @builtin(position) clip_pos: vec4, - @location(0) uv: vec2, -} - -@vertex -fn vs_main(input: VertexInput) -> VertexOutput { - var output: VertexOutput; - - output.clip_pos = vec4(input.v_pos, 0.0, 1.0); - output.uv = input.texel_coord; - - return output; -} - -@fragment -fn fs_main(input: VertexOutput) -> @location(0) vec4 { - return textureSample(frame_texture, frame_sampler, input.uv); -} \ No newline at end of file From 93673836cd2932351b25dea6ca46ae50d0e35ace Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 14 Jun 2023 11:45:29 -0700 Subject: [PATCH 14/16] Fixed documentation --- wgpu/src/backend.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 0735f81f6e..7c962308b0 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -124,7 +124,7 @@ impl Backend { } /// Performs an offscreen render pass. If the `format` selected by WGPU is not - /// `wgpu::TextureFormat::Rgba8UnormSrgb`, a conversion compute pipeline will run. + /// `wgpu::TextureFormat::Rgba8UnormSrgb`, it will be run through a blit. /// /// Returns `None` if the `frame` is `Rgba8UnormSrgb`, else returns the newly /// converted texture view in `Rgba8UnormSrgb`. From 5ae726e02c4d6c9889ef7335d9bc80ef1992e34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 27 Jun 2023 19:41:03 +0200 Subject: [PATCH 15/16] Move `Screenshot` inside `window` module --- examples/screenshot/src/main.rs | 9 +++++---- runtime/src/lib.rs | 2 -- runtime/src/window.rs | 4 +++- runtime/src/window/action.rs | 2 +- runtime/src/{ => window}/screenshot.rs | 1 + src/lib.rs | 1 + winit/src/application.rs | 4 ++-- 7 files changed, 13 insertions(+), 10 deletions(-) rename runtime/src/{ => window}/screenshot.rs (98%) diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index ef2be4fe7f..838245350e 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -1,16 +1,17 @@ -use ::image as img; -use ::image::ColorType; use iced::alignment; use iced::keyboard::KeyCode; use iced::theme::{Button, Container}; -use iced::widget::runtime::{CropError, Screenshot}; use iced::widget::{button, column, container, image, row, text, text_input}; +use iced::window::screenshot::{self, Screenshot}; use iced::{ event, executor, keyboard, subscription, Alignment, Application, Command, ContentFit, Element, Event, Length, Rectangle, Renderer, Subscription, Theme, }; +use ::image as img; +use ::image::ColorType; + fn main() -> iced::Result { env_logger::builder().format_timestamp(None).init(); @@ -21,7 +22,7 @@ struct Example { screenshot: Option, saved_png_path: Option>, png_saving: bool, - crop_error: Option, + crop_error: Option, x_input_value: Option, y_input_value: Option, width_input_value: Option, diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 32ed14d87c..50abf7b27e 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -60,7 +60,6 @@ mod debug; #[cfg(not(feature = "debug"))] #[path = "debug/null.rs"] mod debug; -mod screenshot; pub use iced_core as core; pub use iced_futures as futures; @@ -69,5 +68,4 @@ pub use command::Command; pub use debug::Debug; pub use font::Font; pub use program::Program; -pub use screenshot::{CropError, Screenshot}; pub use user_interface::UserInterface; diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 9b66cb0e65..e448edef56 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -1,13 +1,15 @@ //! Build window-based GUI applications. mod action; +pub mod screenshot; + pub use action::Action; +pub use screenshot::Screenshot; use crate::command::{self, Command}; use crate::core::time::Instant; use crate::core::window::{Event, Icon, Level, Mode, UserAttention}; use crate::futures::subscription::{self, Subscription}; -use crate::screenshot::Screenshot; /// Subscribes to the frames of the window of the running application. /// diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index cb43068190..09be18100f 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -1,7 +1,7 @@ use crate::core::window::{Icon, Level, Mode, UserAttention}; use crate::futures::MaybeSend; +use crate::window::Screenshot; -use crate::screenshot::Screenshot; use std::fmt; /// An operation to be performed on some window. diff --git a/runtime/src/screenshot.rs b/runtime/src/window/screenshot.rs similarity index 98% rename from runtime/src/screenshot.rs rename to runtime/src/window/screenshot.rs index b88f2c2010..c84286b68a 100644 --- a/runtime/src/screenshot.rs +++ b/runtime/src/window/screenshot.rs @@ -1,3 +1,4 @@ +//! Take screenshots of a window. use crate::core::{Rectangle, Size}; use std::fmt::{Debug, Formatter}; diff --git a/src/lib.rs b/src/lib.rs index a0b8a95610..513fb43282 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -276,6 +276,7 @@ pub mod widget { mod native {} mod renderer {} mod style {} + mod runtime {} } pub use application::Application; diff --git a/winit/src/application.rs b/winit/src/application.rs index 5176bec608..a4687375d5 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -19,7 +19,7 @@ use crate::graphics::compositor::{self, Compositor}; use crate::runtime::clipboard; use crate::runtime::program::Program; use crate::runtime::user_interface::{self, UserInterface}; -use crate::runtime::{Command, Debug, Screenshot}; +use crate::runtime::{Command, Debug}; use crate::style::application::{Appearance, StyleSheet}; use crate::{Clipboard, Error, Proxy, Settings}; @@ -819,7 +819,7 @@ pub fn run_command( ); proxy - .send_event(tag(Screenshot::new( + .send_event(tag(window::Screenshot::new( bytes, state.physical_size(), ))) From 5b6e205e998cbb20b3c8aaff8b515d78315d6703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 27 Jun 2023 20:26:13 +0200 Subject: [PATCH 16/16] Simplify `offscreen` API as `color` module in `iced_wgpu` --- wgpu/src/backend.rs | 44 +------- wgpu/src/color.rs | 165 +++++++++++++++++++++++++++++ wgpu/src/lib.rs | 2 +- wgpu/src/offscreen.rs | 188 ---------------------------------- wgpu/src/window/compositor.rs | 17 ++- 5 files changed, 180 insertions(+), 236 deletions(-) create mode 100644 wgpu/src/color.rs delete mode 100644 wgpu/src/offscreen.rs diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 7c962308b0..b524c6152a 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -1,3 +1,4 @@ +use crate::core; use crate::core::{Color, Font, Point, Size}; use crate::graphics::backend; use crate::graphics::color; @@ -5,7 +6,6 @@ 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,48 +123,6 @@ impl Backend { self.image_pipeline.end_frame(); } - /// Performs an offscreen render pass. If the `format` selected by WGPU is not - /// `wgpu::TextureFormat::Rgba8UnormSrgb`, it will be run through a blit. - /// - /// Returns `None` if the `frame` is `Rgba8UnormSrgb`, else returns the newly - /// converted texture view in `Rgba8UnormSrgb`. - pub fn offscreen>( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - clear_color: Option, - frame: &wgpu::TextureView, - format: wgpu::TextureFormat, - primitives: &[Primitive], - viewport: &Viewport, - overlay_text: &[T], - size: wgpu::Extent3d, - ) -> Option { - #[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); - - return Some(pipeline.convert(device, frame, size, encoder)); - } - - None - } - fn prepare_text( &mut self, device: &wgpu::Device, diff --git a/wgpu/src/color.rs b/wgpu/src/color.rs new file mode 100644 index 0000000000..a1025601a2 --- /dev/null +++ b/wgpu/src/color.rs @@ -0,0 +1,165 @@ +use std::borrow::Cow; + +pub fn convert( + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + source: wgpu::Texture, + format: wgpu::TextureFormat, +) -> wgpu::Texture { + if source.format() == format { + return source; + } + + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("iced_wgpu.offscreen.sampler"), + ..Default::default() + }); + + //sampler in 0 + let sampler_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu.offscreen.blit.sampler_layout"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler( + wgpu::SamplerBindingType::NonFiltering, + ), + count: None, + }], + }); + + let sampler_bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu.offscreen.sampler.bind_group"), + layout: &sampler_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Sampler(&sampler), + }], + }); + + let texture_layout = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("iced_wgpu.offscreen.blit.texture_layout"), + entries: &[wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { + filterable: false, + }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }], + }); + + let pipeline_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("iced_wgpu.offscreen.blit.pipeline_layout"), + bind_group_layouts: &[&sampler_layout, &texture_layout], + push_constant_ranges: &[], + }); + + let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("iced_wgpu.offscreen.blit.shader"), + source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!( + "shader/blit.wgsl" + ))), + }); + + let pipeline = + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("iced_wgpu.offscreen.blit.pipeline"), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: "vs_main", + buffers: &[], + }, + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: "fs_main", + targets: &[Some(wgpu::ColorTargetState { + format, + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::SrcAlpha, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::One, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + }), + write_mask: wgpu::ColorWrites::ALL, + })], + }), + primitive: wgpu::PrimitiveState { + topology: wgpu::PrimitiveTopology::TriangleList, + front_face: wgpu::FrontFace::Cw, + ..Default::default() + }, + depth_stencil: None, + multisample: Default::default(), + multiview: None, + }); + + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("iced_wgpu.offscreen.conversion.source_texture"), + size: source.size(), + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }); + + let view = &texture.create_view(&wgpu::TextureViewDescriptor::default()); + + let texture_bind_group = + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("iced_wgpu.offscreen.blit.texture_bind_group"), + layout: &texture_layout, + entries: &[wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView( + &source + .create_view(&wgpu::TextureViewDescriptor::default()), + ), + }], + }); + + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("iced_wgpu.offscreen.blit.render_pass"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, + store: true, + }, + })], + depth_stencil_attachment: None, + }); + + pass.set_pipeline(&pipeline); + pass.set_bind_group(0, &sampler_bind_group, &[]); + pass.set_bind_group(1, &texture_bind_group, &[]); + pass.draw(0..6, 0..1); + + texture +} + +#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] +#[repr(C)] +struct Vertex { + ndc: [f32; 2], + uv: [f32; 2], +} diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 827acb892a..86a962a544 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -46,7 +46,7 @@ pub mod geometry; mod backend; mod buffer; -mod offscreen; +mod color; mod quad; mod text; mod triangle; diff --git a/wgpu/src/offscreen.rs b/wgpu/src/offscreen.rs deleted file mode 100644 index b25602e407..0000000000 --- a/wgpu/src/offscreen.rs +++ /dev/null @@ -1,188 +0,0 @@ -use crate::graphics::color; -use std::borrow::Cow; - -/// A simple compute pipeline to convert any texture to Rgba8UnormSrgb. -#[derive(Debug)] -pub struct Pipeline { - pipeline: wgpu::RenderPipeline, - sampler_bind_group: wgpu::BindGroup, - texture_layout: wgpu::BindGroupLayout, -} - -#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] -#[repr(C)] -struct Vertex { - ndc: [f32; 2], - uv: [f32; 2], -} - -impl Pipeline { - pub fn new(device: &wgpu::Device) -> Self { - let sampler = device.create_sampler(&wgpu::SamplerDescriptor { - label: Some("iced_wgpu.offscreen.sampler"), - ..Default::default() - }); - - //sampler in 0 - let sampler_layout = - device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("iced_wgpu.offscreen.blit.sampler_layout"), - entries: &[wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Sampler( - wgpu::SamplerBindingType::NonFiltering, - ), - count: None, - }], - }); - - let sampler_bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu.offscreen.sampler.bind_group"), - layout: &sampler_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Sampler(&sampler), - }], - }); - - let texture_layout = - device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("iced_wgpu.offscreen.blit.texture_layout"), - entries: &[wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Texture { - sample_type: wgpu::TextureSampleType::Float { - filterable: false, - }, - view_dimension: wgpu::TextureViewDimension::D2, - multisampled: false, - }, - count: None, - }], - }); - - let pipeline_layout = - device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("iced_wgpu.offscreen.blit.pipeline_layout"), - bind_group_layouts: &[&sampler_layout, &texture_layout], - push_constant_ranges: &[], - }); - - let shader = - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: Some("iced_wgpu.offscreen.blit.shader"), - source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!( - "shader/blit.wgsl" - ))), - }); - - let pipeline = - device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("iced_wgpu.offscreen.blit.pipeline"), - layout: Some(&pipeline_layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "vs_main", - buffers: &[], - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "fs_main", - targets: &[Some(wgpu::ColorTargetState { - format: if color::GAMMA_CORRECTION { - wgpu::TextureFormat::Rgba8UnormSrgb - } else { - wgpu::TextureFormat::Rgba8Unorm - }, - blend: Some(wgpu::BlendState { - color: wgpu::BlendComponent { - src_factor: wgpu::BlendFactor::SrcAlpha, - dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, - operation: wgpu::BlendOperation::Add, - }, - alpha: wgpu::BlendComponent { - src_factor: wgpu::BlendFactor::One, - dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, - operation: wgpu::BlendOperation::Add, - }, - }), - write_mask: wgpu::ColorWrites::ALL, - })], - }), - primitive: wgpu::PrimitiveState { - topology: wgpu::PrimitiveTopology::TriangleList, - front_face: wgpu::FrontFace::Cw, - ..Default::default() - }, - depth_stencil: None, - multisample: Default::default(), - multiview: None, - }); - - Self { - pipeline, - sampler_bind_group, - texture_layout, - } - } - - pub fn convert( - &self, - device: &wgpu::Device, - frame: &wgpu::TextureView, - size: wgpu::Extent3d, - encoder: &mut wgpu::CommandEncoder, - ) -> wgpu::Texture { - let texture = device.create_texture(&wgpu::TextureDescriptor { - label: Some("iced_wgpu.offscreen.conversion.source_texture"), - size, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: if color::GAMMA_CORRECTION { - wgpu::TextureFormat::Rgba8UnormSrgb - } else { - wgpu::TextureFormat::Rgba8Unorm - }, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::COPY_SRC, - view_formats: &[], - }); - - let view = - &texture.create_view(&wgpu::TextureViewDescriptor::default()); - - let texture_bind_group = - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("iced_wgpu.offscreen.blit.texture_bind_group"), - layout: &self.texture_layout, - entries: &[wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::TextureView(frame), - }], - }); - - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("iced_wgpu.offscreen.blit.render_pass"), - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Load, - store: true, - }, - })], - depth_stencil_attachment: None, - }); - - pass.set_pipeline(&self.pipeline); - pass.set_bind_group(0, &self.sampler_bind_group, &[]); - pass.set_bind_group(1, &texture_bind_group, &[]); - pass.draw(0..6, 0..1); - - texture - } -} diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 43c3dce58f..1cfd7b677f 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -345,17 +345,26 @@ pub fn screenshot>( let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); - let rgba_texture = backend.offscreen( + backend.present( &compositor.device, &compositor.queue, &mut encoder, Some(background_color), &view, - compositor.format, primitives, viewport, overlay, - texture_extent, + ); + + let texture = crate::color::convert( + &compositor.device, + &mut encoder, + texture, + if color::GAMMA_CORRECTION { + wgpu::TextureFormat::Rgba8UnormSrgb + } else { + wgpu::TextureFormat::Rgba8Unorm + }, ); let output_buffer = @@ -368,7 +377,7 @@ pub fn screenshot>( }); encoder.copy_texture_to_buffer( - rgba_texture.unwrap_or(texture).as_image_copy(), + texture.as_image_copy(), wgpu::ImageCopyBuffer { buffer: &output_buffer, layout: wgpu::ImageDataLayout {