Skip to content

Commit

Permalink
Put Shape::Mesh in an Arc to reduce the size of Shape
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 2, 2025
1 parent ebb5136 commit 741022b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
14 changes: 8 additions & 6 deletions crates/epaint/src/shape_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn adjust_colors(
}

if !galley.is_empty() {
let galley = std::sync::Arc::make_mut(galley);
let galley = Arc::make_mut(galley);
for row in &mut galley.rows {
for vertex in &mut row.visuals.mesh.vertices {
adjust_color(&mut vertex.color);
Expand All @@ -95,11 +95,13 @@ pub fn adjust_colors(
}
}

Shape::Mesh(Mesh {
indices: _,
vertices,
texture_id: _,
}) => {
Shape::Mesh(mesh) => {
let Mesh {
indices: _,
vertices,
texture_id: _,
} = Arc::make_mut(mesh);

for v in vertices {
adjust_color(&mut v.color);
}
Expand Down
22 changes: 18 additions & 4 deletions crates/epaint/src/shapes/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ pub enum Shape {
/// A general triangle mesh.
///
/// Can be used to display images.
Mesh(Mesh),
///
/// Wrapped in an [`Arc`] to minimize the size of [`Shape`].
Mesh(Arc<Mesh>),

/// A quadratic [Bézier Curve](https://en.wikipedia.org/wiki/B%C3%A9zier_curve).
QuadraticBezier(QuadraticBezierShape),
Expand All @@ -71,9 +73,13 @@ pub enum Shape {
#[test]
fn shape_size() {
assert_eq!(
std::mem::size_of::<Shape>(), 72,
std::mem::size_of::<Shape>(), 64,
"Shape changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
);
assert!(
std::mem::size_of::<Shape>() <= 64,
"Shape is getting way too big!"
);
}

#[test]
Expand All @@ -92,6 +98,13 @@ impl From<Vec<Self>> for Shape {
impl From<Mesh> for Shape {
#[inline(always)]
fn from(mesh: Mesh) -> Self {
Self::Mesh(mesh.into())
}
}

impl From<Arc<Mesh>> for Shape {
#[inline(always)]
fn from(mesh: Arc<Mesh>) -> Self {
Self::Mesh(mesh)
}
}
Expand Down Expand Up @@ -322,7 +335,8 @@ impl Shape {
}

#[inline]
pub fn mesh(mesh: Mesh) -> Self {
pub fn mesh(mesh: impl Into<Arc<Mesh>>) -> Self {
let mesh = mesh.into();
debug_assert!(mesh.is_valid());
Self::Mesh(mesh)
}
Expand Down Expand Up @@ -454,7 +468,7 @@ impl Shape {
galley.rect = transform.scaling * galley.rect;
}
Self::Mesh(mesh) => {
mesh.transform(transform);
Arc::make_mut(mesh).transform(transform);
}
Self::QuadraticBezier(bezier_shape) => {
bezier_shape.points[0] = transform * bezier_shape.points[0];
Expand Down
4 changes: 2 additions & 2 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ impl Tessellator {
return;
}

out.append(mesh);
out.append_ref(&mesh);
}
Shape::LineSegment { points, stroke } => {
self.tessellate_line_segment(points, stroke, out);
Expand Down Expand Up @@ -2177,7 +2177,7 @@ impl Tessellator {

profiling::scope!("distribute results", tessellated.len().to_string());
for (index, mesh) in tessellated {
shapes[index].shape = Shape::Mesh(mesh);
shapes[index].shape = Shape::Mesh(mesh.into());
}
}

Expand Down

0 comments on commit 741022b

Please sign in to comment.