make Meshes cheaply cloneable by using Arc<[T]> in iced_graphics::mesh::Indexed

This commit is contained in:
edwloef 2025-10-04 23:47:55 +02:00 • committed by Héctor Ramón Jiménez
parent 39ca9e0233
commit 42bc4af972
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 14 additions and 10 deletions

View file

@ -100,7 +100,7 @@ mod rainbow {
let mesh = Mesh::Solid { let mesh = Mesh::Solid {
buffers: mesh::Indexed { buffers: mesh::Indexed {
vertices: vec![ vertices: [
SolidVertex2D { SolidVertex2D {
position: posn_center, position: posn_center,
color: color::pack([1.0, 1.0, 1.0, 1.0]), color: color::pack([1.0, 1.0, 1.0, 1.0]),
@ -137,8 +137,9 @@ mod rainbow {
position: posn_l, position: posn_l,
color: color::pack(color_v), color: color::pack(color_v),
}, },
], ]
indices: vec![ .into(),
indices: [
0, 1, 2, // TL 0, 1, 2, // TL
0, 2, 3, // T 0, 2, 3, // T
0, 3, 4, // TR 0, 3, 4, // TR
@ -147,7 +148,8 @@ mod rainbow {
0, 6, 7, // B 0, 6, 7, // B
0, 7, 8, // BL 0, 7, 8, // BL
0, 8, 1, // L 0, 8, 1, // L
], ]
.into(),
}, },
transformation: Transformation::IDENTITY, transformation: Transformation::IDENTITY,
clip_bounds: Rectangle::INFINITE, clip_bounds: Rectangle::INFINITE,

View file

@ -5,6 +5,8 @@ use crate::gradient;
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use std::sync::Arc;
/// A low-level primitive to render a mesh of triangles. /// A low-level primitive to render a mesh of triangles.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Mesh { pub enum Mesh {
@ -70,12 +72,12 @@ impl Mesh {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct Indexed<T> { pub struct Indexed<T> {
/// The vertices of the mesh /// The vertices of the mesh
pub vertices: Vec<T>, pub vertices: Arc<[T]>,
/// The list of vertex indices that defines the triangles of the mesh. /// The list of vertex indices that defines the triangles of the mesh.
/// ///
/// Therefore, this list should always have a length that is a multiple of 3. /// Therefore, this list should always have a length that is a multiple of 3.
pub indices: Vec<u32>, pub indices: Arc<[u32]>,
} }
/// A two-dimensional vertex with a color. /// A two-dimensional vertex with a color.

View file

@ -547,8 +547,8 @@ impl BufferStack {
Buffer::Solid(buffer) if !buffer.indices.is_empty() => { Buffer::Solid(buffer) if !buffer.indices.is_empty() => {
Some(Mesh::Solid { Some(Mesh::Solid {
buffers: mesh::Indexed { buffers: mesh::Indexed {
vertices: buffer.vertices, vertices: buffer.vertices.into(),
indices: buffer.indices, indices: buffer.indices.into(),
}, },
clip_bounds, clip_bounds,
transformation: Transformation::IDENTITY, transformation: Transformation::IDENTITY,
@ -557,8 +557,8 @@ impl BufferStack {
Buffer::Gradient(buffer) if !buffer.indices.is_empty() => { Buffer::Gradient(buffer) if !buffer.indices.is_empty() => {
Some(Mesh::Gradient { Some(Mesh::Gradient {
buffers: mesh::Indexed { buffers: mesh::Indexed {
vertices: buffer.vertices, vertices: buffer.vertices.into(),
indices: buffer.indices, indices: buffer.indices.into(),
}, },
clip_bounds, clip_bounds,
transformation: Transformation::IDENTITY, transformation: Transformation::IDENTITY,