Skip to content

Commit 75110b9

Browse files
authored
Merge pull request #1871 from bungoboingo/fix/bg-gradient
[Fix] Make gradient pack fn public for iced_graphics::Gradient
2 parents fcb1b45 + 8ca7b88 commit 75110b9

5 files changed

Lines changed: 78 additions & 76 deletions

File tree

graphics/src/gradient.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! [`Gradient`]: crate::core::Gradient;
66
use crate::core::gradient::ColorStop;
7-
use crate::core::{Color, Point};
7+
use crate::core::{self, Color, Point, Rectangle};
88
use std::cmp::Ordering;
99

1010
#[derive(Debug, Clone, PartialEq)]
@@ -23,6 +23,15 @@ impl From<Linear> for Gradient {
2323
}
2424
}
2525

26+
impl Gradient {
27+
/// Packs the [`Gradient`] for use in shader code.
28+
pub fn pack(&self) -> Packed {
29+
match self {
30+
Gradient::Linear(linear) => linear.pack(),
31+
}
32+
}
33+
}
34+
2635
/// A linear gradient that can be used in the style of [`Fill`] or [`Stroke`].
2736
///
2837
/// [`Fill`]: crate::geometry::Fill;
@@ -85,4 +94,62 @@ impl Linear {
8594

8695
self
8796
}
97+
98+
/// Packs the [`Gradient`] for use in shader code.
99+
pub fn pack(&self) -> Packed {
100+
let mut data: [f32; 44] = [0.0; 44];
101+
102+
for (index, stop) in self.stops.iter().enumerate() {
103+
let [r, g, b, a] =
104+
stop.map_or(Color::default(), |s| s.color).into_linear();
105+
106+
data[index * 4] = r;
107+
data[(index * 4) + 1] = g;
108+
data[(index * 4) + 2] = b;
109+
data[(index * 4) + 3] = a;
110+
111+
data[32 + index] = stop.map_or(2.0, |s| s.offset);
112+
}
113+
114+
data[40] = self.start.x;
115+
data[41] = self.start.y;
116+
data[42] = self.end.x;
117+
data[43] = self.end.y;
118+
119+
Packed(data)
120+
}
121+
}
122+
123+
/// Packed [`Gradient`] data for use in shader code.
124+
#[derive(Debug, Copy, Clone, PartialEq)]
125+
#[repr(C)]
126+
pub struct Packed([f32; 44]);
127+
128+
/// Creates a new [`Packed`] gradient for use in shader code.
129+
pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed {
130+
match gradient {
131+
core::Gradient::Linear(linear) => {
132+
let mut data: [f32; 44] = [0.0; 44];
133+
134+
for (index, stop) in linear.stops.iter().enumerate() {
135+
let [r, g, b, a] =
136+
stop.map_or(Color::default(), |s| s.color).into_linear();
137+
138+
data[index * 4] = r;
139+
data[(index * 4) + 1] = g;
140+
data[(index * 4) + 2] = b;
141+
data[(index * 4) + 3] = a;
142+
data[32 + index] = stop.map_or(2.0, |s| s.offset);
143+
}
144+
145+
let (start, end) = linear.angle.to_distance(&bounds);
146+
147+
data[40] = start.x;
148+
data[41] = start.y;
149+
data[42] = end.x;
150+
data[43] = end.y;
151+
152+
Packed(data)
153+
}
154+
}
88155
}

graphics/src/primitive.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::core::image;
44
use crate::core::svg;
55
use crate::core::text;
66
use crate::core::{Background, Color, Font, Rectangle, Size, Vector};
7+
use crate::gradient;
78

89
use bytemuck::{Pod, Zeroable};
910
use std::sync::Arc;
@@ -258,7 +259,7 @@ pub struct GradientVertex2D {
258259
pub position: [f32; 2],
259260

260261
/// The packed vertex data of the gradient.
261-
pub gradient: [f32; 44],
262+
pub gradient: gradient::Packed,
262263
}
263264

264265
#[allow(unsafe_code)]

wgpu/src/geometry.rs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::graphics::geometry::{
77
use crate::graphics::primitive::{self, Primitive};
88
use crate::graphics::Gradient;
99

10+
use iced_graphics::gradient;
1011
use lyon::geom::euclid;
1112
use lyon::tessellation;
1213
use std::borrow::Cow;
@@ -74,7 +75,7 @@ impl BufferStack {
7475
Box::new(tessellation::BuffersBuilder::new(
7576
buffer,
7677
GradientVertex2DBuilder {
77-
gradient: pack_gradient(gradient),
78+
gradient: gradient.pack(),
7879
},
7980
))
8081
}
@@ -97,7 +98,7 @@ impl BufferStack {
9798
Box::new(tessellation::BuffersBuilder::new(
9899
buffer,
99100
GradientVertex2DBuilder {
100-
gradient: pack_gradient(gradient),
101+
gradient: gradient.pack(),
101102
},
102103
))
103104
}
@@ -490,7 +491,7 @@ impl Frame {
490491
}
491492

492493
struct GradientVertex2DBuilder {
493-
gradient: [f32; 44],
494+
gradient: gradient::Packed,
494495
}
495496

496497
impl tessellation::FillVertexConstructor<primitive::GradientVertex2D>
@@ -623,42 +624,3 @@ pub(super) fn dashed(path: &Path, line_dash: LineDash<'_>) -> Path {
623624
);
624625
})
625626
}
626-
627-
/// Packs the [`Gradient`] for use in shader code.
628-
fn pack_gradient(gradient: &Gradient) -> [f32; 44] {
629-
match gradient {
630-
Gradient::Linear(linear) => {
631-
let mut pack: [f32; 44] = [0.0; 44];
632-
let mut offsets: [f32; 8] = [2.0; 8];
633-
634-
for (index, stop) in linear.stops.iter().enumerate() {
635-
let [r, g, b, a] = stop
636-
.map_or(crate::core::Color::default(), |s| s.color)
637-
.into_linear();
638-
639-
pack[index * 4] = r;
640-
pack[(index * 4) + 1] = g;
641-
pack[(index * 4) + 2] = b;
642-
pack[(index * 4) + 3] = a;
643-
644-
offsets[index] = stop.map_or(2.0, |s| s.offset);
645-
}
646-
647-
pack[32] = offsets[0];
648-
pack[33] = offsets[1];
649-
pack[34] = offsets[2];
650-
pack[35] = offsets[3];
651-
pack[36] = offsets[4];
652-
pack[37] = offsets[5];
653-
pack[38] = offsets[6];
654-
pack[39] = offsets[7];
655-
656-
pack[40] = linear.start.x;
657-
pack[41] = linear.start.y;
658-
pack[42] = linear.end.x;
659-
pack[43] = linear.end.y;
660-
661-
pack
662-
}
663-
}
664-
}

wgpu/src/layer.rs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub use text::Text;
1313
use crate::core;
1414
use crate::core::alignment;
1515
use crate::core::{Background, Color, Font, Point, Rectangle, Size, Vector};
16+
use crate::graphics::gradient;
1617
use crate::graphics::{Primitive, Viewport};
1718

1819
/// A group of primitives that should be clipped together.
@@ -182,7 +183,7 @@ impl<'a> Layer<'a> {
182183
}
183184
Background::Gradient(gradient) => {
184185
let quad = quad::Gradient {
185-
gradient: pack_gradient(
186+
gradient: gradient::pack(
186187
gradient,
187188
Rectangle::new(
188189
quad.position.into(),
@@ -310,32 +311,3 @@ impl<'a> Layer<'a> {
310311
}
311312
}
312313
}
313-
314-
/// Packs the [`Gradient`] for use in shader code.
315-
fn pack_gradient(gradient: &core::Gradient, bounds: Rectangle) -> [f32; 44] {
316-
match gradient {
317-
core::Gradient::Linear(linear) => {
318-
let mut pack: [f32; 44] = [0.0; 44];
319-
320-
for (index, stop) in linear.stops.iter().enumerate() {
321-
let [r, g, b, a] =
322-
stop.map_or(Color::default(), |s| s.color).into_linear();
323-
324-
pack[index * 4] = r;
325-
pack[(index * 4) + 1] = g;
326-
pack[(index * 4) + 2] = b;
327-
pack[(index * 4) + 3] = a;
328-
pack[32 + index] = stop.map_or(2.0, |s| s.offset);
329-
}
330-
331-
let (start, end) = linear.angle.to_distance(&bounds);
332-
333-
pack[40] = start.x;
334-
pack[41] = start.y;
335-
pack[42] = end.x;
336-
pack[43] = end.y;
337-
338-
pack
339-
}
340-
}
341-
}

wgpu/src/layer/quad.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! A rectangle with certain styled properties.
2-
2+
use crate::graphics::gradient;
33
use bytemuck::{Pod, Zeroable};
44

55
/// The properties of a quad.
@@ -38,7 +38,7 @@ pub struct Solid {
3838
#[repr(C)]
3939
pub struct Gradient {
4040
/// The background gradient data of the quad.
41-
pub gradient: [f32; 44],
41+
pub gradient: gradient::Packed,
4242

4343
/// The [`Quad`] data of the [`Gradient`].
4444
pub quad: Quad,

0 commit comments

Comments
 (0)