From 233196eb14b40f8bd5201ea0262571f82136ad53 Mon Sep 17 00:00:00 2001 From: Bingus Date: Sat, 25 Mar 2023 10:45:39 -0700 Subject: Added offscreen rendering support for wgpu & tiny-skia exposed with the window::screenshot command. --- 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 +++++++++++++++++++++++++++++++++++- 6 files changed, 329 insertions(+), 11 deletions(-) create mode 100644 wgpu/src/offscreen.rs create mode 100644 wgpu/src/shader/offscreen_blit.wgsl (limited to 'wgpu/src') 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>( + &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 0a5726b5..827acb89 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 00000000..29913d02 --- /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 00000000..9c764c36 --- /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 4afbdb32..320b5b12 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 2eaafde0..43c3dce5 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, + } + } } -- cgit From 05e238e9ed5f0c6cade87228f8f3044ee26df756 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 8 Jun 2023 10:10:26 -0700 Subject: Adjusted offscreen pass to be a render pass vs compute for compat with web-colors flag. --- wgpu/src/backend.rs | 21 +---- wgpu/src/offscreen.rs | 164 +++++++++++++++++++++++++++++++----- wgpu/src/shader/offscreen_blit.wgsl | 37 ++++---- 3 files changed, 165 insertions(+), 57 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 8f37f285..0735f81f 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 29913d02..d0758b66 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 9c764c36..08952d62 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 -- cgit From af099fa6d72d9c3e23c85a36aee14904526d02f0 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 8 Jun 2023 10:17:53 -0700 Subject: Added in check for web-colors. --- wgpu/src/offscreen.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/offscreen.rs b/wgpu/src/offscreen.rs index d0758b66..baa069e8 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: &[], -- cgit From d955b3444da19e24bf0de6d6f432f06623ed5db2 Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 14 Jun 2023 11:10:09 -0700 Subject: 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 (limited to 'wgpu/src') diff --git a/wgpu/src/offscreen.rs b/wgpu/src/offscreen.rs index baa069e8..b25602e4 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 08952d62..00000000 --- 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 -- cgit From 93673836cd2932351b25dea6ca46ae50d0e35ace Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 14 Jun 2023 11:45:29 -0700 Subject: Fixed documentation --- wgpu/src/backend.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src') diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 0735f81f..7c962308 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`. -- cgit From 5b6e205e998cbb20b3c8aaff8b515d78315d6703 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 27 Jun 2023 20:26:13 +0200 Subject: 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 (limited to 'wgpu/src') diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 7c962308..b524c615 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 00000000..a1025601 --- /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 827acb89..86a962a5 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 b25602e4..00000000 --- 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 43c3dce5..1cfd7b67 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 { -- cgit