From 6551a0b2ab6c831dd1d3646ecf55180339275e22 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 11 May 2023 09:12:06 -0700 Subject: Added support for gradients as background variants + other optimizations. --- wgpu/src/image.rs | 46 +++++++--------------------------------------- 1 file changed, 7 insertions(+), 39 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 263bcfa2..6fe02b91 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -8,10 +8,10 @@ mod vector; use atlas::Atlas; +use crate::buffer::Buffer; use crate::core::{Rectangle, Size}; use crate::graphics::Transformation; -use crate::layer; -use crate::Buffer; +use crate::{layer, quad}; use std::cell::RefCell; use std::mem; @@ -121,7 +121,7 @@ impl Layer { ); let _ = self.instances.resize(device, instances.len()); - self.instances.write(queue, 0, instances); + let _ = self.instances.write(queue, 0, instances); self.instance_count = instances.len(); } @@ -131,7 +131,7 @@ impl Layer { render_pass.set_vertex_buffer(1, self.instances.slice(..)); render_pass.draw_indexed( - 0..QUAD_INDICES.len() as u32, + 0..quad::INDICES.len() as u32, 0, 0..self.instance_count as u32, ); @@ -244,22 +244,7 @@ impl Pipeline { 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, - })], + targets: &quad::color_target_state(format), }), primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, @@ -278,14 +263,14 @@ impl Pipeline { let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("iced_wgpu::image vertex buffer"), - contents: bytemuck::cast_slice(&QUAD_VERTS), + contents: bytemuck::cast_slice(&quad::VERTICES), usage: wgpu::BufferUsages::VERTEX, }); let indices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("iced_wgpu::image index buffer"), - contents: bytemuck::cast_slice(&QUAD_INDICES), + contents: bytemuck::cast_slice(&quad::INDICES), usage: wgpu::BufferUsages::INDEX, }); @@ -498,23 +483,6 @@ pub struct Vertex { _position: [f32; 2], } -const QUAD_INDICES: [u16; 6] = [0, 1, 2, 0, 2, 3]; - -const QUAD_VERTS: [Vertex; 4] = [ - Vertex { - _position: [0.0, 0.0], - }, - Vertex { - _position: [1.0, 0.0], - }, - Vertex { - _position: [1.0, 1.0], - }, - Vertex { - _position: [0.0, 1.0], - }, -]; - #[repr(C)] #[derive(Debug, Clone, Copy, Zeroable, Pod)] struct Instance { -- cgit From f557b810f5931e69a9a35353b20fff1b07480715 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 May 2023 03:58:25 +0200 Subject: Keep `image` pipeline decoupled from `quad` in `iced_wgpu` --- wgpu/src/image.rs | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index 6fe02b91..c3479652 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -11,7 +11,7 @@ use atlas::Atlas; use crate::buffer::Buffer; use crate::core::{Rectangle, Size}; use crate::graphics::Transformation; -use crate::{layer, quad}; +use crate::layer; use std::cell::RefCell; use std::mem; @@ -131,7 +131,7 @@ impl Layer { render_pass.set_vertex_buffer(1, self.instances.slice(..)); render_pass.draw_indexed( - 0..quad::INDICES.len() as u32, + 0..QUAD_INDICES.len() as u32, 0, 0..self.instance_count as u32, ); @@ -244,7 +244,22 @@ impl Pipeline { fragment: Some(wgpu::FragmentState { module: &shader, entry_point: "fs_main", - targets: &quad::color_target_state(format), + 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, @@ -263,14 +278,14 @@ impl Pipeline { let vertices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("iced_wgpu::image vertex buffer"), - contents: bytemuck::cast_slice(&quad::VERTICES), + contents: bytemuck::cast_slice(&QUAD_VERTICES), usage: wgpu::BufferUsages::VERTEX, }); let indices = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("iced_wgpu::image index buffer"), - contents: bytemuck::cast_slice(&quad::INDICES), + contents: bytemuck::cast_slice(&QUAD_INDICES), usage: wgpu::BufferUsages::INDEX, }); @@ -483,6 +498,23 @@ pub struct Vertex { _position: [f32; 2], } +const QUAD_INDICES: [u16; 6] = [0, 1, 2, 0, 2, 3]; + +const QUAD_VERTICES: [Vertex; 4] = [ + Vertex { + _position: [0.0, 0.0], + }, + Vertex { + _position: [1.0, 0.0], + }, + Vertex { + _position: [1.0, 1.0], + }, + Vertex { + _position: [0.0, 1.0], + }, +]; + #[repr(C)] #[derive(Debug, Clone, Copy, Zeroable, Pod)] struct Instance { -- cgit From e267e075ccd35ae3ca357ae4143796e68c1ad392 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 May 2023 04:02:18 +0200 Subject: Avoid redundant `buffer::Buffer` import --- wgpu/src/image.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/image.rs') diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs index c3479652..3407aa92 100644 --- a/wgpu/src/image.rs +++ b/wgpu/src/image.rs @@ -8,10 +8,10 @@ mod vector; use atlas::Atlas; -use crate::buffer::Buffer; use crate::core::{Rectangle, Size}; use crate::graphics::Transformation; use crate::layer; +use crate::Buffer; use std::cell::RefCell; use std::mem; -- cgit