Skip to content

Commit

Permalink
Changed gradient::Packed to be repr(C) for direct gpu upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
bungoboingo committed May 26, 2023
1 parent 413526a commit 902e333
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
14 changes: 9 additions & 5 deletions graphics/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
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
4 changes: 2 additions & 2 deletions wgpu/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ impl tessellation::FillVertexConstructor<primitive::GradientVertex2D>

primitive::GradientVertex2D {
position: [position.x, position.y],
gradient: self.gradient.data,
gradient: self.gradient,
}
}
}
Expand All @@ -521,7 +521,7 @@ impl tessellation::StrokeVertexConstructor<primitive::GradientVertex2D>

primitive::GradientVertex2D {
position: [position.x, position.y],
gradient: self.gradient.data,
gradient: self.gradient,
}
}
}
Expand Down
28 changes: 16 additions & 12 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 @@ -312,30 +313,33 @@ impl<'a> Layer<'a> {
}

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

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

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

pack
data.into()
}
}
}
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

0 comments on commit 902e333

Please sign in to comment.