From a395e78596d0711a50108cb6654121541337ceb5 Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 24 May 2023 13:08:59 -0700 Subject: Made gradient pack public for iced_graphics::gradient mod for use with GradientVertex2D. --- wgpu/src/geometry.rs | 43 ++----------------------------------------- 1 file changed, 2 insertions(+), 41 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index d1d4fd3c..13ce2359 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -74,7 +74,7 @@ impl BufferStack { Box::new(tessellation::BuffersBuilder::new( buffer, GradientVertex2DBuilder { - gradient: pack_gradient(gradient), + gradient: gradient.pack(), }, )) } @@ -97,7 +97,7 @@ impl BufferStack { Box::new(tessellation::BuffersBuilder::new( buffer, GradientVertex2DBuilder { - gradient: pack_gradient(gradient), + gradient: gradient.pack(), }, )) } @@ -623,42 +623,3 @@ pub(super) fn dashed(path: &Path, line_dash: LineDash<'_>) -> Path { ); }) } - -/// Packs the [`Gradient`] for use in shader code. -fn pack_gradient(gradient: &Gradient) -> [f32; 44] { - match gradient { - Gradient::Linear(linear) => { - let mut pack: [f32; 44] = [0.0; 44]; - let mut offsets: [f32; 8] = [2.0; 8]; - - for (index, stop) in linear.stops.iter().enumerate() { - let [r, g, b, a] = stop - .map_or(crate::core::Color::default(), |s| s.color) - .into_linear(); - - pack[index * 4] = r; - pack[(index * 4) + 1] = g; - pack[(index * 4) + 2] = b; - pack[(index * 4) + 3] = a; - - offsets[index] = stop.map_or(2.0, |s| s.offset); - } - - pack[32] = offsets[0]; - pack[33] = offsets[1]; - pack[34] = offsets[2]; - pack[35] = offsets[3]; - pack[36] = offsets[4]; - pack[37] = offsets[5]; - pack[38] = offsets[6]; - pack[39] = offsets[7]; - - pack[40] = linear.start.x; - pack[41] = linear.start.y; - pack[42] = linear.end.x; - pack[43] = linear.end.y; - - pack - } - } -} -- cgit From 413526ad09d006853eb9659efabee168f4a0e0a4 Mon Sep 17 00:00:00 2001 From: Bingus Date: Thu, 25 May 2023 10:49:26 -0700 Subject: Created "Packed" data structure for gradient data. --- wgpu/src/geometry.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index 13ce2359..f3fed7ae 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -7,6 +7,7 @@ use crate::graphics::geometry::{ use crate::graphics::primitive::{self, Primitive}; use crate::graphics::Gradient; +use iced_graphics::gradient; use lyon::geom::euclid; use lyon::tessellation; use std::borrow::Cow; @@ -490,7 +491,7 @@ impl Frame { } struct GradientVertex2DBuilder { - gradient: [f32; 44], + gradient: gradient::Packed, } impl tessellation::FillVertexConstructor @@ -504,7 +505,7 @@ impl tessellation::FillVertexConstructor primitive::GradientVertex2D { position: [position.x, position.y], - gradient: self.gradient, + gradient: self.gradient.data, } } } @@ -520,7 +521,7 @@ impl tessellation::StrokeVertexConstructor primitive::GradientVertex2D { position: [position.x, position.y], - gradient: self.gradient, + gradient: self.gradient.data, } } } -- cgit From 902e333148a1ceed85aba36262a849aaed8d3ac9 Mon Sep 17 00:00:00 2001 From: Bingus Date: Fri, 26 May 2023 10:07:52 -0700 Subject: Changed gradient::Packed to be `repr(C)` for direct gpu upload. --- wgpu/src/geometry.rs | 4 ++-- wgpu/src/layer.rs | 28 ++++++++++++++++------------ wgpu/src/layer/quad.rs | 4 ++-- 3 files changed, 20 insertions(+), 16 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs index f3fed7ae..740ec20c 100644 --- a/wgpu/src/geometry.rs +++ b/wgpu/src/geometry.rs @@ -505,7 +505,7 @@ impl tessellation::FillVertexConstructor primitive::GradientVertex2D { position: [position.x, position.y], - gradient: self.gradient.data, + gradient: self.gradient, } } } @@ -521,7 +521,7 @@ impl tessellation::StrokeVertexConstructor primitive::GradientVertex2D { position: [position.x, position.y], - gradient: self.gradient.data, + gradient: self.gradient, } } } diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs index 980d807b..9d7f9f2a 100644 --- a/wgpu/src/layer.rs +++ b/wgpu/src/layer.rs @@ -13,6 +13,7 @@ pub use text::Text; use crate::core; use crate::core::alignment; use crate::core::{Background, Color, Font, Point, Rectangle, Size, Vector}; +use crate::graphics::gradient; use crate::graphics::{Primitive, Viewport}; /// A group of primitives that should be clipped together. @@ -312,30 +313,33 @@ impl<'a> Layer<'a> { } /// Packs the [`Gradient`] for use in shader code. -fn pack_gradient(gradient: &core::Gradient, bounds: Rectangle) -> [f32; 44] { +fn pack_gradient( + gradient: &core::Gradient, + bounds: Rectangle, +) -> gradient::Packed { match gradient { core::Gradient::Linear(linear) => { - let mut pack: [f32; 44] = [0.0; 44]; + let mut data: [f32; 44] = [0.0; 44]; for (index, stop) in linear.stops.iter().enumerate() { let [r, g, b, a] = stop.map_or(Color::default(), |s| s.color).into_linear(); - pack[index * 4] = r; - pack[(index * 4) + 1] = g; - pack[(index * 4) + 2] = b; - pack[(index * 4) + 3] = a; - pack[32 + index] = stop.map_or(2.0, |s| s.offset); + data[index * 4] = r; + data[(index * 4) + 1] = g; + data[(index * 4) + 2] = b; + data[(index * 4) + 3] = a; + data[32 + index] = stop.map_or(2.0, |s| s.offset); } let (start, end) = linear.angle.to_distance(&bounds); - pack[40] = start.x; - pack[41] = start.y; - pack[42] = end.x; - pack[43] = end.y; + data[40] = start.x; + data[41] = start.y; + data[42] = end.x; + data[43] = end.y; - pack + data.into() } } } diff --git a/wgpu/src/layer/quad.rs b/wgpu/src/layer/quad.rs index 9913cfe0..0bf7837a 100644 --- a/wgpu/src/layer/quad.rs +++ b/wgpu/src/layer/quad.rs @@ -1,5 +1,5 @@ //! A rectangle with certain styled properties. - +use crate::graphics::gradient; use bytemuck::{Pod, Zeroable}; /// The properties of a quad. @@ -38,7 +38,7 @@ pub struct Solid { #[repr(C)] pub struct Gradient { /// The background gradient data of the quad. - pub gradient: [f32; 44], + pub gradient: gradient::Packed, /// The [`Quad`] data of the [`Gradient`]. pub quad: Quad, -- cgit From 8ca7b884c0695e4e7a031ea1359ee733bdcaa8a4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 29 May 2023 20:56:51 +0200 Subject: Make `Packed` fully opaque ... by only allowing direct conversion from our `Gradient` types --- wgpu/src/layer.rs | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) (limited to 'wgpu/src') diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs index 9d7f9f2a..bf5c4c0a 100644 --- a/wgpu/src/layer.rs +++ b/wgpu/src/layer.rs @@ -183,7 +183,7 @@ impl<'a> Layer<'a> { } Background::Gradient(gradient) => { let quad = quad::Gradient { - gradient: pack_gradient( + gradient: gradient::pack( gradient, Rectangle::new( quad.position.into(), @@ -311,35 +311,3 @@ impl<'a> Layer<'a> { } } } - -/// Packs the [`Gradient`] for use in shader code. -fn pack_gradient( - gradient: &core::Gradient, - bounds: Rectangle, -) -> gradient::Packed { - match gradient { - core::Gradient::Linear(linear) => { - let mut data: [f32; 44] = [0.0; 44]; - - for (index, stop) in linear.stops.iter().enumerate() { - let [r, g, b, a] = - stop.map_or(Color::default(), |s| s.color).into_linear(); - - data[index * 4] = r; - data[(index * 4) + 1] = g; - data[(index * 4) + 2] = b; - data[(index * 4) + 3] = a; - data[32 + index] = stop.map_or(2.0, |s| s.offset); - } - - let (start, end) = linear.angle.to_distance(&bounds); - - data[40] = start.x; - data[41] = start.y; - data[42] = end.x; - data[43] = end.y; - - data.into() - } - } -} -- cgit