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/layer.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'wgpu/src/layer.rs') 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() } } } -- 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/layer.rs') 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