Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Make gradient pack fn public for iced_graphics::Gradient #1871

Merged
merged 4 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion graphics/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -23,6 +23,15 @@ impl From<Linear> for Gradient {
}
}

impl Gradient {
/// Packs the [`Gradient`] for use in shader code.
pub fn pack(&self) -> Packed {
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;
Expand Down Expand Up @@ -85,4 +94,62 @@ impl Linear {

self
}

/// Packs the [`Gradient`] for use in shader code.
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();

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);
}

data[40] = self.start.x;
data[41] = self.start.y;
data[42] = self.end.x;
data[43] = self.end.y;

Packed(data)
}
}

/// Packed [`Gradient`] data for use in shader code.
#[derive(Debug, Copy, Clone, PartialEq)]
#[repr(C)]
pub struct Packed([f32; 44]);

/// 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)
}
}
}
3 changes: 2 additions & 1 deletion graphics/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::core::image;
use crate::core::svg;
use crate::core::text;
use crate::core::{Background, Color, Font, Rectangle, Size, Vector};
use crate::gradient;

use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
Expand Down Expand Up @@ -258,7 +259,7 @@ pub struct GradientVertex2D {
pub position: [f32; 2],

/// The packed vertex data of the gradient.
pub gradient: [f32; 44],
pub gradient: gradient::Packed,
}

#[allow(unsafe_code)]
Expand Down
46 changes: 4 additions & 42 deletions wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,7 +75,7 @@ impl BufferStack {
Box::new(tessellation::BuffersBuilder::new(
buffer,
GradientVertex2DBuilder {
gradient: pack_gradient(gradient),
gradient: gradient.pack(),
},
))
}
Expand All @@ -97,7 +98,7 @@ impl BufferStack {
Box::new(tessellation::BuffersBuilder::new(
buffer,
GradientVertex2DBuilder {
gradient: pack_gradient(gradient),
gradient: gradient.pack(),
},
))
}
Expand Down Expand Up @@ -490,7 +491,7 @@ impl Frame {
}

struct GradientVertex2DBuilder {
gradient: [f32; 44],
gradient: gradient::Packed,
}

impl tessellation::FillVertexConstructor<primitive::GradientVertex2D>
Expand Down Expand Up @@ -623,42 +624,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
}
}
}
32 changes: 2 additions & 30 deletions wgpu/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -182,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(),
Expand Down Expand Up @@ -310,32 +311,3 @@ impl<'a> Layer<'a> {
}
}
}

/// Packs the [`Gradient`] for use in shader code.
fn pack_gradient(gradient: &core::Gradient, bounds: Rectangle) -> [f32; 44] {
match gradient {
core::Gradient::Linear(linear) => {
let mut pack: [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);
}

let (start, end) = linear.angle.to_distance(&bounds);

pack[40] = start.x;
pack[41] = start.y;
pack[42] = end.x;
pack[43] = end.y;

pack
}
}
}
4 changes: 2 additions & 2 deletions wgpu/src/layer/quad.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! A rectangle with certain styled properties.

use crate::graphics::gradient;
use bytemuck::{Pod, Zeroable};

/// The properties of a quad.
Expand Down Expand Up @@ -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,
Expand Down