Created "Packed" data structure for gradient data.

This commit is contained in:
Bingus 2023-05-25 10:49:26 -07:00
parent a395e78596
commit 413526ad09
No known key found for this signature in database
GPG key ID: 5F84D2AA40A9F170
2 changed files with 24 additions and 16 deletions

View file

@ -25,7 +25,7 @@ impl From<Linear> 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],
}

View file

@ -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<primitive::GradientVertex2D>
@ -504,7 +505,7 @@ impl tessellation::FillVertexConstructor<primitive::GradientVertex2D>
primitive::GradientVertex2D {
position: [position.x, position.y],
gradient: self.gradient,
gradient: self.gradient.data,
}
}
}
@ -520,7 +521,7 @@ impl tessellation::StrokeVertexConstructor<primitive::GradientVertex2D>
primitive::GradientVertex2D {
position: [position.x, position.y],
gradient: self.gradient,
gradient: self.gradient.data,
}
}
}