Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions graphics/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ impl From<Linear> 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;
Expand Down Expand Up @@ -85,4 +94,28 @@ impl Linear {

self
}

/// Packs the [`Gradient`] for use in shader code.
pub fn pack(&self) -> [f32; 44] {
Comment thread
hecrj marked this conversation as resolved.
Outdated
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
}
}
43 changes: 2 additions & 41 deletions wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl BufferStack {
Box::new(tessellation::BuffersBuilder::new(
buffer,
GradientVertex2DBuilder {
gradient: pack_gradient(gradient),
gradient: gradient.pack(),
},
))
}
Expand All @@ -97,7 +97,7 @@ impl BufferStack {
Box::new(tessellation::BuffersBuilder::new(
buffer,
GradientVertex2DBuilder {
gradient: pack_gradient(gradient),
gradient: gradient.pack(),
},
))
}
Expand Down Expand Up @@ -623,42 +623,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
}
}
}