From 5fd5d1cdf8e5354788dc40729c4565ef377d3bba Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Mar 2023 21:34:26 +0100 Subject: Implement `Canvas` support for `iced_tiny_skia` --- graphics/src/gradient.rs | 117 ----------------------------------------------- 1 file changed, 117 deletions(-) delete mode 100644 graphics/src/gradient.rs (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs deleted file mode 100644 index 61e919d6..00000000 --- a/graphics/src/gradient.rs +++ /dev/null @@ -1,117 +0,0 @@ -//! For creating a Gradient. -pub mod linear; - -pub use linear::Linear; - -use crate::{Color, Point, Size}; - -#[derive(Debug, Clone, PartialEq)] -/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), -/// or conically (TBD). -pub enum Gradient { - /// A linear gradient interpolates colors along a direction from its `start` to its `end` - /// point. - Linear(Linear), -} - -impl Gradient { - /// Creates a new linear [`linear::Builder`]. - pub fn linear(position: impl Into) -> linear::Builder { - linear::Builder::new(position.into()) - } -} - -#[derive(Debug, Clone, Copy, PartialEq)] -/// A point along the gradient vector where the specified [`color`] is unmixed. -/// -/// [`color`]: Self::color -pub struct ColorStop { - /// Offset along the gradient vector. - pub offset: f32, - - /// The color of the gradient at the specified [`offset`]. - /// - /// [`offset`]: Self::offset - pub color: Color, -} - -#[derive(Debug)] -/// The position of the gradient within its bounds. -pub enum Position { - /// The gradient will be positioned with respect to two points. - Absolute { - /// The starting point of the gradient. - start: Point, - /// The ending point of the gradient. - end: Point, - }, - /// The gradient will be positioned relative to the provided bounds. - Relative { - /// The top left position of the bounds. - top_left: Point, - /// The width & height of the bounds. - size: Size, - /// The start [Location] of the gradient. - start: Location, - /// The end [Location] of the gradient. - end: Location, - }, -} - -impl From<(Point, Point)> for Position { - fn from((start, end): (Point, Point)) -> Self { - Self::Absolute { start, end } - } -} - -#[derive(Debug, Clone, Copy)] -/// The location of a relatively-positioned gradient. -pub enum Location { - /// Top left. - TopLeft, - /// Top. - Top, - /// Top right. - TopRight, - /// Right. - Right, - /// Bottom right. - BottomRight, - /// Bottom. - Bottom, - /// Bottom left. - BottomLeft, - /// Left. - Left, -} - -impl Location { - fn to_absolute(self, top_left: Point, size: Size) -> Point { - match self { - Location::TopLeft => top_left, - Location::Top => { - Point::new(top_left.x + size.width / 2.0, top_left.y) - } - Location::TopRight => { - Point::new(top_left.x + size.width, top_left.y) - } - Location::Right => Point::new( - top_left.x + size.width, - top_left.y + size.height / 2.0, - ), - Location::BottomRight => { - Point::new(top_left.x + size.width, top_left.y + size.height) - } - Location::Bottom => Point::new( - top_left.x + size.width / 2.0, - top_left.y + size.height, - ), - Location::BottomLeft => { - Point::new(top_left.x, top_left.y + size.height) - } - Location::Left => { - Point::new(top_left.x, top_left.y + size.height / 2.0) - } - } - } -} -- cgit 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. --- graphics/src/gradient.rs | 119 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 graphics/src/gradient.rs (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs new file mode 100644 index 00000000..21bcd2c6 --- /dev/null +++ b/graphics/src/gradient.rs @@ -0,0 +1,119 @@ +//! A gradient that can be used as a [`Fill`] for a mesh. +//! +//! For a gradient that you can use as a background variant for a widget, see [`Gradient`]. +//! +//! [`Gradient`]: crate::core::Gradient; +use crate::core::Point; +pub use linear::Linear; + +#[derive(Debug, Clone, PartialEq)] +/// A fill which linearly interpolates colors along a direction. +/// +/// For a gradient which can be used as a fill for a background of a widget, see [`crate::core::Gradient`]. +pub enum Gradient { + /// A linear gradient interpolates colors along a direction from its `start` to its `end` + /// point. + Linear(Linear), +} + +impl Gradient { + /// Creates a new linear [`linear::Builder`]. + /// + /// The `start` and `end` [`Point`]s define the absolute position of the [`Gradient`]. + pub fn linear(start: Point, end: Point) -> linear::Builder { + linear::Builder::new(start, end) + } +} + +pub mod linear { + //! Linear gradient builder & definition. + use crate::Gradient; + use iced_core::gradient::ColorStop; + use iced_core::{Color, Point}; + use std::cmp::Ordering; + + /// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`]. + /// + /// [`Fill`]: crate::geometry::Fill; + /// [`Stroke`]: crate::geometry::Stroke; + #[derive(Debug, Clone, Copy, PartialEq)] + pub struct Linear { + /// The absolute starting position of the gradient. + pub start: Point, + + /// The absolute ending position of the gradient. + pub end: Point, + + /// [`ColorStop`]s along the linear gradient direction. + pub color_stops: [Option; 8], + } + + /// A [`Linear`] builder. + #[derive(Debug)] + pub struct Builder { + start: Point, + end: Point, + stops: [Option; 8], + } + + impl Builder { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: [None; 8], + } + } + + /// Adds a new [`ColorStop`], defined by an offset and a color, to the gradient. + /// + /// Any `offset` that is not within `0.0..=1.0` will be silently ignored. + /// + /// Any stop added after the 8th will be silently ignored. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if offset.is_finite() && (0.0..=1.0).contains(&offset) { + let (Ok(index) | Err(index)) = + self.stops.binary_search_by(|stop| match stop { + None => Ordering::Greater, + Some(stop) => stop.offset.partial_cmp(&offset).unwrap(), + }); + + if index < 8 { + self.stops[index] = Some(ColorStop { offset, color }); + } + } else { + log::warn!( + "Gradient: ColorStop must be within 0.0..=1.0 range." + ); + }; + + self + } + + /// Adds multiple [`ColorStop`]s to the gradient. + /// + /// Any stop added after the 8th will be silently ignored. + pub fn add_stops( + mut self, + stops: impl IntoIterator, + ) -> Self { + for stop in stops.into_iter() { + self = self.add_stop(stop.offset, stop.color) + } + + self + } + + /// Builds the linear [`Gradient`] of this [`Builder`]. + /// + /// Returns `BuilderError` if gradient in invalid. + pub fn build(self) -> Gradient { + Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: self.stops, + }) + } + } +} -- cgit From 4c1a082f0468a59099bbf8aa8991420a41234948 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 May 2023 03:32:21 +0200 Subject: Remove `Builder` abstractions for gradients --- graphics/src/gradient.rs | 145 +++++++++++++++++++---------------------------- 1 file changed, 57 insertions(+), 88 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 21bcd2c6..3e88d9de 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,10 +1,11 @@ -//! A gradient that can be used as a [`Fill`] for a mesh. +//! A gradient that can be used as a [`Fill`] for some geometry. //! //! For a gradient that you can use as a background variant for a widget, see [`Gradient`]. //! //! [`Gradient`]: crate::core::Gradient; -use crate::core::Point; -pub use linear::Linear; +use crate::core::gradient::ColorStop; +use crate::core::{Color, Point}; +use std::cmp::Ordering; #[derive(Debug, Clone, PartialEq)] /// A fill which linearly interpolates colors along a direction. @@ -16,104 +17,72 @@ pub enum Gradient { Linear(Linear), } -impl Gradient { - /// Creates a new linear [`linear::Builder`]. - /// - /// The `start` and `end` [`Point`]s define the absolute position of the [`Gradient`]. - pub fn linear(start: Point, end: Point) -> linear::Builder { - linear::Builder::new(start, end) +impl From for Gradient { + fn from(gradient: Linear) -> Self { + Self::Linear(gradient) } } -pub mod linear { - //! Linear gradient builder & definition. - use crate::Gradient; - use iced_core::gradient::ColorStop; - use iced_core::{Color, Point}; - use std::cmp::Ordering; +/// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`]. +/// +/// [`Fill`]: crate::geometry::Fill; +/// [`Stroke`]: crate::geometry::Stroke; +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Linear { + /// The absolute starting position of the gradient. + pub start: Point, - /// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`]. - /// - /// [`Fill`]: crate::geometry::Fill; - /// [`Stroke`]: crate::geometry::Stroke; - #[derive(Debug, Clone, Copy, PartialEq)] - pub struct Linear { - /// The absolute starting position of the gradient. - pub start: Point, + /// The absolute ending position of the gradient. + pub end: Point, - /// The absolute ending position of the gradient. - pub end: Point, + /// [`ColorStop`]s along the linear gradient direction. + pub stops: [Option; 8], +} - /// [`ColorStop`]s along the linear gradient direction. - pub color_stops: [Option; 8], +impl Linear { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: [None; 8], + } } - /// A [`Linear`] builder. - #[derive(Debug)] - pub struct Builder { - start: Point, - end: Point, - stops: [Option; 8], - } + /// Adds a new [`ColorStop`], defined by an offset and a color, to the gradient. + /// + /// Any `offset` that is not within `0.0..=1.0` will be silently ignored. + /// + /// Any stop added after the 8th will be silently ignored. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if offset.is_finite() && (0.0..=1.0).contains(&offset) { + let (Ok(index) | Err(index)) = + self.stops.binary_search_by(|stop| match stop { + None => Ordering::Greater, + Some(stop) => stop.offset.partial_cmp(&offset).unwrap(), + }); - impl Builder { - /// Creates a new [`Builder`]. - pub fn new(start: Point, end: Point) -> Self { - Self { - start, - end, - stops: [None; 8], + if index < 8 { + self.stops[index] = Some(ColorStop { offset, color }); } - } + } else { + log::warn!("Gradient: ColorStop must be within 0.0..=1.0 range."); + }; - /// Adds a new [`ColorStop`], defined by an offset and a color, to the gradient. - /// - /// Any `offset` that is not within `0.0..=1.0` will be silently ignored. - /// - /// Any stop added after the 8th will be silently ignored. - pub fn add_stop(mut self, offset: f32, color: Color) -> Self { - if offset.is_finite() && (0.0..=1.0).contains(&offset) { - let (Ok(index) | Err(index)) = - self.stops.binary_search_by(|stop| match stop { - None => Ordering::Greater, - Some(stop) => stop.offset.partial_cmp(&offset).unwrap(), - }); - - if index < 8 { - self.stops[index] = Some(ColorStop { offset, color }); - } - } else { - log::warn!( - "Gradient: ColorStop must be within 0.0..=1.0 range." - ); - }; - - self - } - - /// Adds multiple [`ColorStop`]s to the gradient. - /// - /// Any stop added after the 8th will be silently ignored. - pub fn add_stops( - mut self, - stops: impl IntoIterator, - ) -> Self { - for stop in stops.into_iter() { - self = self.add_stop(stop.offset, stop.color) - } + self + } - self + /// Adds multiple [`ColorStop`]s to the gradient. + /// + /// Any stop added after the 8th will be silently ignored. + pub fn add_stops( + mut self, + stops: impl IntoIterator, + ) -> Self { + for stop in stops.into_iter() { + self = self.add_stop(stop.offset, stop.color) } - /// Builds the linear [`Gradient`] of this [`Builder`]. - /// - /// Returns `BuilderError` if gradient in invalid. - pub fn build(self) -> Gradient { - Gradient::Linear(Linear { - start: self.start, - end: self.end, - color_stops: self.stops, - }) - } + self } } -- cgit 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. --- graphics/src/gradient.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 3e88d9de..86d14b6c 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -23,6 +23,15 @@ impl From for Gradient { } } +impl Gradient { + /// Packs the [`Gradient`] for use in shader code. + pub fn pack(&self) -> [f32; 44] { + match self { + Gradient::Linear(linear) => linear.pack(), + } + } +} + /// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`]. /// /// [`Fill`]: crate::geometry::Fill; @@ -85,4 +94,28 @@ impl Linear { self } + + /// Packs the [`Gradient`] for use in shader code. + pub fn pack(&self) -> [f32; 44] { + let mut pack: [f32; 44] = [0.0; 44]; + + for (index, stop) in self.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); + } + + pack[40] = self.start.x; + pack[41] = self.start.y; + pack[42] = self.end.x; + pack[43] = self.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. --- graphics/src/gradient.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 86d14b6c..2b6aba44 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -25,7 +25,7 @@ impl From for Gradient { impl Gradient { /// Packs the [`Gradient`] for use in shader code. - pub fn pack(&self) -> [f32; 44] { + pub fn pack(&self) -> Packed { match self { Gradient::Linear(linear) => linear.pack(), } @@ -96,26 +96,33 @@ impl Linear { } /// Packs the [`Gradient`] for use in shader code. - pub fn pack(&self) -> [f32; 44] { - let mut pack: [f32; 44] = [0.0; 44]; + pub fn pack(&self) -> Packed { + let mut data: [f32; 44] = [0.0; 44]; for (index, stop) in self.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; + data[index * 4] = r; + data[(index * 4) + 1] = g; + data[(index * 4) + 2] = b; + data[(index * 4) + 3] = a; - pack[32 + index] = stop.map_or(2.0, |s| s.offset); + data[32 + index] = stop.map_or(2.0, |s| s.offset); } - pack[40] = self.start.x; - pack[41] = self.start.y; - pack[42] = self.end.x; - pack[43] = self.end.y; + data[40] = self.start.x; + data[41] = self.start.y; + data[42] = self.end.x; + data[43] = self.end.y; - pack + Packed { data } } } + +/// Packed [`Gradient`] data for use in shader code. +#[derive(Debug)] +pub struct Packed { + /// The packed [`Gradient`] data. + pub data: [f32; 44], +} -- 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. --- graphics/src/gradient.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 2b6aba44..9f1e7bbc 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -116,13 +116,17 @@ impl Linear { data[42] = self.end.x; data[43] = self.end.y; - Packed { data } + Packed(data) } } /// Packed [`Gradient`] data for use in shader code. -#[derive(Debug)] -pub struct Packed { - /// The packed [`Gradient`] data. - pub data: [f32; 44], +#[derive(Debug, Copy, Clone, PartialEq)] +#[repr(C)] +pub struct Packed([f32; 44]); + +impl From<[f32; 44]> for Packed { + fn from(value: [f32; 44]) -> Self { + Self(value) + } } -- 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 --- graphics/src/gradient.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 9f1e7bbc..d3eabb6f 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -4,7 +4,7 @@ //! //! [`Gradient`]: crate::core::Gradient; use crate::core::gradient::ColorStop; -use crate::core::{Color, Point}; +use crate::core::{self, Color, Point, Rectangle}; use std::cmp::Ordering; #[derive(Debug, Clone, PartialEq)] @@ -125,8 +125,31 @@ impl Linear { #[repr(C)] pub struct Packed([f32; 44]); -impl From<[f32; 44]> for Packed { - fn from(value: [f32; 44]) -> Self { - Self(value) +/// Creates a new [`Packed`] gradient for use in shader code. +pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> 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; + + Packed(data) + } } } -- cgit From faa7627ea41b1ce372bae7f0d2ae36e9b15a97a3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 31 May 2023 21:31:58 +0200 Subject: Introduce `web-colors` feature flag to enable sRGB linear blending This is how browsers perform color management. They treat gamma-corrected sRGB colors as if they were linear RGB. Correctness aside, this mode is introduced for legacy reasons. Most UI/UX tooling uses this color management as well, and many have created an intuition about how color should behave from interacting with a browser. This feature flag should facilitate application development with `iced` in those cases. More details: https://webcolorisstillbroken.com/ --- graphics/src/gradient.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index d3eabb6f..d26b5665 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -3,8 +3,10 @@ //! For a gradient that you can use as a background variant for a widget, see [`Gradient`]. //! //! [`Gradient`]: crate::core::Gradient; +use crate::color; use crate::core::gradient::ColorStop; use crate::core::{self, Color, Point, Rectangle}; + use std::cmp::Ordering; #[derive(Debug, Clone, PartialEq)] @@ -101,7 +103,8 @@ impl Linear { for (index, stop) in self.stops.iter().enumerate() { let [r, g, b, a] = - stop.map_or(Color::default(), |s| s.color).into_linear(); + color::pack(stop.map_or(Color::default(), |s| s.color)) + .components(); data[index * 4] = r; data[(index * 4) + 1] = g; @@ -133,7 +136,8 @@ pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed { for (index, stop) in linear.stops.iter().enumerate() { let [r, g, b, a] = - stop.map_or(Color::default(), |s| s.color).into_linear(); + color::pack(stop.map_or(Color::default(), |s| s.color)) + .components(); data[index * 4] = r; data[(index * 4) + 1] = g; -- cgit From ea7f2626b11af249510b27001fb6addd7f9210a9 Mon Sep 17 00:00:00 2001 From: Bingus Date: Mon, 29 May 2023 16:44:56 -0700 Subject: Optimized gradient data packing. --- graphics/src/gradient.rs | 71 ++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 32 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index d26b5665..57cc007f 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -99,61 +99,68 @@ impl Linear { /// Packs the [`Gradient`] for use in shader code. pub fn pack(&self) -> Packed { - let mut data: [f32; 44] = [0.0; 44]; + let mut colors = [0u32; 8]; + let mut offsets = [0.0f32; 8]; for (index, stop) in self.stops.iter().enumerate() { - let [r, g, b, a] = - color::pack(stop.map_or(Color::default(), |s| s.color)) - .components(); - - 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 (color, offset) = stop + .map_or((Color::default().into_u32(), 2.0), |s| { + (s.color.into_u32(), s.offset) + }); + colors[index] = color; + offsets[index] = offset; } - data[40] = self.start.x; - data[41] = self.start.y; - data[42] = self.end.x; - data[43] = self.end.y; + let direction = [self.start.x, self.start.y, self.end.x, self.end.y]; - Packed(data) + Packed { + colors, + offsets, + direction, + } } } /// Packed [`Gradient`] data for use in shader code. #[derive(Debug, Copy, Clone, PartialEq)] #[repr(C)] -pub struct Packed([f32; 44]); +pub struct Packed { + // 8 colors, each packed into a u32 + colors: [u32; 8], + offsets: [f32; 8], + direction: [f32; 4], +} /// Creates a new [`Packed`] gradient for use in shader code. pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed { match gradient { core::Gradient::Linear(linear) => { - let mut data: [f32; 44] = [0.0; 44]; + let mut colors = [0u32; 8]; + let mut offsets = [0.0f32; 8]; for (index, stop) in linear.stops.iter().enumerate() { - let [r, g, b, a] = - color::pack(stop.map_or(Color::default(), |s| s.color)) - .components(); - - 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 [r, g, b, a] = + // color::pack(stop.map_or(Color::default(), |s| s.color)) + // .components(); + + let (color, offset) = stop + .map_or((Color::default().into_u32(), 2.0), |s| { + (s.color.into_u32(), s.offset) + }); + + colors[index] = color; + offsets[index] = 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; + let direction = [start.x, start.y, end.x, end.y]; - Packed(data) + Packed { + colors, + offsets, + direction, + } } } } -- cgit From 9554c78f3adc9846b76e9d3b96af06e98fb69aa0 Mon Sep 17 00:00:00 2001 From: Bingus Date: Tue, 6 Jun 2023 17:06:40 -0700 Subject: Updated color packing into u32 to consider incorrect web-colors. --- graphics/src/gradient.rs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 57cc007f..97b0a6d7 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -103,11 +103,17 @@ impl Linear { let mut offsets = [0.0f32; 8]; for (index, stop) in self.stops.iter().enumerate() { - let (color, offset) = stop - .map_or((Color::default().into_u32(), 2.0), |s| { - (s.color.into_u32(), s.offset) - }); - colors[index] = color; + let (color, offset) = + stop.map_or((Color::default(), 2.0), |s| (s.color, s.offset)); + + if color::GAMMA_CORRECTION { + //correct colors, convert to linear before uploading to GPU + colors[index] = color.into_linear_u32(); + } else { + //web colors, don't convert to linear before uploading to GPU + colors[index] = color.into_u32(); + } + offsets[index] = offset; } @@ -139,16 +145,17 @@ pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed { let mut offsets = [0.0f32; 8]; for (index, stop) in linear.stops.iter().enumerate() { - // let [r, g, b, a] = - // color::pack(stop.map_or(Color::default(), |s| s.color)) - // .components(); - let (color, offset) = stop - .map_or((Color::default().into_u32(), 2.0), |s| { - (s.color.into_u32(), s.offset) - }); + .map_or((Color::default(), 2.0), |s| (s.color, s.offset)); + + if color::GAMMA_CORRECTION { + //correct colors, convert to linear before uploading to GPU + colors[index] = color.into_linear_u32(); + } else { + //web colors, don't convert to linear before uploading to GPU + colors[index] = color.into_u32(); + } - colors[index] = color; offsets[index] = offset; } -- cgit From 677f564f087b009842207e6df74aed343454ea17 Mon Sep 17 00:00:00 2001 From: Bingus Date: Wed, 7 Jun 2023 10:47:57 -0700 Subject: Switched to packing using f16s to maintain acceptable precision. --- graphics/src/gradient.rs | 82 ++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 30 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 97b0a6d7..3f5d0509 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -7,6 +7,7 @@ use crate::color; use crate::core::gradient::ColorStop; use crate::core::{self, Color, Point, Rectangle}; +use half::f16; use std::cmp::Ordering; #[derive(Debug, Clone, PartialEq)] @@ -99,24 +100,30 @@ impl Linear { /// Packs the [`Gradient`] for use in shader code. pub fn pack(&self) -> Packed { - let mut colors = [0u32; 8]; - let mut offsets = [0.0f32; 8]; + let mut colors = [[0u32; 2]; 8]; + let mut offsets = [f16::from(0u8); 8]; for (index, stop) in self.stops.iter().enumerate() { - let (color, offset) = - stop.map_or((Color::default(), 2.0), |s| (s.color, s.offset)); - - if color::GAMMA_CORRECTION { - //correct colors, convert to linear before uploading to GPU - colors[index] = color.into_linear_u32(); - } else { - //web colors, don't convert to linear before uploading to GPU - colors[index] = color.into_u32(); - } + let [r, g, b, a] = + color::pack(stop.map_or(Color::default(), |s| s.color)) + .components(); + + colors[index] = [ + pack_f16s([f16::from_f32(r), f16::from_f32(g)]), + pack_f16s([f16::from_f32(b), f16::from_f32(a)]), + ]; - offsets[index] = offset; + offsets[index] = + stop.map_or(f16::from_f32(2.0), |s| f16::from_f32(s.offset)); } + let offsets = [ + pack_f16s([offsets[0], offsets[1]]), + pack_f16s([offsets[2], offsets[3]]), + pack_f16s([offsets[4], offsets[5]]), + pack_f16s([offsets[6], offsets[7]]), + ]; + let direction = [self.start.x, self.start.y, self.end.x, self.end.y]; Packed { @@ -131,9 +138,10 @@ impl Linear { #[derive(Debug, Copy, Clone, PartialEq)] #[repr(C)] pub struct Packed { - // 8 colors, each packed into a u32 - colors: [u32; 8], - offsets: [f32; 8], + // 8 colors, each channel = 16 bit float, 2 colors packed into 1 u32 + colors: [[u32; 2]; 8], + // 8 offsets, 8x 16 bit floats packed into 4 u32s + offsets: [u32; 4], direction: [f32; 4], } @@ -141,24 +149,30 @@ pub struct Packed { pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed { match gradient { core::Gradient::Linear(linear) => { - let mut colors = [0u32; 8]; - let mut offsets = [0.0f32; 8]; + let mut colors = [[0u32; 2]; 8]; + let mut offsets = [f16::from(0u8); 8]; for (index, stop) in linear.stops.iter().enumerate() { - let (color, offset) = stop - .map_or((Color::default(), 2.0), |s| (s.color, s.offset)); - - if color::GAMMA_CORRECTION { - //correct colors, convert to linear before uploading to GPU - colors[index] = color.into_linear_u32(); - } else { - //web colors, don't convert to linear before uploading to GPU - colors[index] = color.into_u32(); - } - - offsets[index] = offset; + let [r, g, b, a] = + color::pack(stop.map_or(Color::default(), |s| s.color)) + .components(); + + colors[index] = [ + pack_f16s([f16::from_f32(r), f16::from_f32(g)]), + pack_f16s([f16::from_f32(b), f16::from_f32(a)]), + ]; + + offsets[index] = stop + .map_or(f16::from_f32(2.0), |s| f16::from_f32(s.offset)); } + let offsets = [ + pack_f16s([offsets[0], offsets[1]]), + pack_f16s([offsets[2], offsets[3]]), + pack_f16s([offsets[4], offsets[5]]), + pack_f16s([offsets[6], offsets[7]]), + ]; + let (start, end) = linear.angle.to_distance(&bounds); let direction = [start.x, start.y, end.x, end.y]; @@ -171,3 +185,11 @@ pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed { } } } + +/// Packs two f16s into one u32. +fn pack_f16s(f: [f16; 2]) -> u32 { + let one = (f[0].to_bits() as u32) << 16; + let two = f[1].to_bits() as u32; + + one | two +} -- cgit From fa5650cfd1115e6ccec2ad795cf58fd970d5b43c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 29 Jun 2023 07:48:03 +0200 Subject: Decouple `Mesh` primitives from main `Primitive` type --- graphics/src/gradient.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 3f5d0509..4db565d8 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -7,6 +7,7 @@ use crate::color; use crate::core::gradient::ColorStop; use crate::core::{self, Color, Point, Rectangle}; +use bytemuck::{Pod, Zeroable}; use half::f16; use std::cmp::Ordering; @@ -135,7 +136,7 @@ impl Linear { } /// Packed [`Gradient`] data for use in shader code. -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Zeroable, Pod)] #[repr(C)] pub struct Packed { // 8 colors, each channel = 16 bit float, 2 colors packed into 1 u32 -- cgit