Skip to content

Commit afb3b8f

Browse files
committed
Introduce draw_mesh_cache in mesh::Renderer
1 parent 42bc4af commit afb3b8f

File tree

8 files changed

+119
-69
lines changed

8 files changed

+119
-69
lines changed

examples/geometry/src/main.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ mod rainbow {
100100

101101
let mesh = Mesh::Solid {
102102
buffers: mesh::Indexed {
103-
vertices: [
103+
vertices: vec![
104104
SolidVertex2D {
105105
position: posn_center,
106106
color: color::pack([1.0, 1.0, 1.0, 1.0]),
@@ -137,9 +137,8 @@ mod rainbow {
137137
position: posn_l,
138138
color: color::pack(color_v),
139139
},
140-
]
141-
.into(),
142-
indices: [
140+
],
141+
indices: vec![
143142
0, 1, 2, // TL
144143
0, 2, 3, // T
145144
0, 3, 4, // TR
@@ -148,8 +147,7 @@ mod rainbow {
148147
0, 6, 7, // B
149148
0, 7, 8, // BL
150149
0, 8, 1, // L
151-
]
152-
.into(),
150+
],
153151
},
154152
transformation: Transformation::IDENTITY,
155153
clip_bounds: Rectangle::INFINITE,

graphics/src/mesh.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use crate::gradient;
55

66
use bytemuck::{Pod, Zeroable};
77

8-
use std::sync::Arc;
8+
use std::sync::atomic::{self, AtomicU64};
9+
use std::sync::{Arc, Weak};
910

1011
/// A low-level primitive to render a mesh of triangles.
1112
#[derive(Debug, Clone, PartialEq)]
@@ -72,12 +73,12 @@ impl Mesh {
7273
#[derive(Clone, Debug, PartialEq, Eq)]
7374
pub struct Indexed<T> {
7475
/// The vertices of the mesh
75-
pub vertices: Arc<[T]>,
76+
pub vertices: Vec<T>,
7677

7778
/// The list of vertex indices that defines the triangles of the mesh.
7879
///
7980
/// Therefore, this list should always have a length that is a multiple of 3.
80-
pub indices: Arc<[u32]>,
81+
pub indices: Vec<u32>,
8182
}
8283

8384
/// A two-dimensional vertex with a color.
@@ -143,8 +144,67 @@ pub fn attribute_count_of(meshes: &[Mesh]) -> AttributeCount {
143144
})
144145
}
145146

147+
/// A cache of multiple meshes.
148+
#[derive(Debug, Clone)]
149+
pub struct Cache {
150+
id: Id,
151+
batch: Arc<[Mesh]>,
152+
version: usize,
153+
}
154+
155+
/// The unique id of a [`Cache`].
156+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
157+
pub struct Id(u64);
158+
159+
impl Cache {
160+
/// Creates a new [`Cache`] for the given meshes.
161+
pub fn new(meshes: Arc<[Mesh]>) -> Self {
162+
static NEXT_ID: AtomicU64 = AtomicU64::new(0);
163+
164+
Self {
165+
id: Id(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)),
166+
batch: meshes,
167+
version: 0,
168+
}
169+
}
170+
171+
/// Returns the [`Id`] of the [`Cache`].
172+
pub fn id(&self) -> Id {
173+
self.id
174+
}
175+
176+
/// Returns the current version of the [`Cache`].
177+
pub fn version(&self) -> usize {
178+
self.version
179+
}
180+
181+
/// Returns the batch of meshes in the [`Cache`].
182+
pub fn batch(&self) -> &[Mesh] {
183+
&self.batch
184+
}
185+
186+
/// Returns a [`Weak`] reference to the contents of the [`Cache`].
187+
pub fn downgrade(&self) -> Weak<[Mesh]> {
188+
Arc::downgrade(&self.batch)
189+
}
190+
191+
/// Returns true if the [`Cache`] is empty.
192+
pub fn is_empty(&self) -> bool {
193+
self.batch.is_empty()
194+
}
195+
196+
/// Updates the [`Cache`] with the given meshes.
197+
pub fn update(&mut self, meshes: Arc<[Mesh]>) {
198+
self.batch = meshes;
199+
self.version += 1;
200+
}
201+
}
202+
146203
/// A renderer capable of drawing a [`Mesh`].
147204
pub trait Renderer {
148205
/// Draws the given [`Mesh`].
149206
fn draw_mesh(&mut self, mesh: Mesh);
207+
208+
/// Draws the given [`Cache`].
209+
fn draw_mesh_cache(&mut self, cache: Cache);
150210
}

renderer/src/fallback.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ where
224224
fn draw_mesh(&mut self, mesh: graphics::Mesh) {
225225
delegate!(self, renderer, renderer.draw_mesh(mesh));
226226
}
227+
228+
fn draw_mesh_cache(&mut self, cache: mesh::Cache) {
229+
delegate!(self, renderer, renderer.draw_mesh_cache(cache));
230+
}
227231
}
228232

229233
/// A compositor `A` with a fallback strategy `B`.

tiny_skia/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,10 @@ impl graphics::mesh::Renderer for Renderer {
373373
fn draw_mesh(&mut self, _mesh: graphics::Mesh) {
374374
log::warn!("iced_tiny_skia does not support drawing meshes");
375375
}
376+
377+
fn draw_mesh_cache(&mut self, _cache: iced_graphics::mesh::Cache) {
378+
log::warn!("iced_tiny_skia does not support drawing meshes");
379+
}
376380
}
377381

378382
#[cfg(feature = "image")]

wgpu/src/geometry.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::graphics::gradient::{self, Gradient};
1313
use crate::graphics::mesh::{self, Mesh};
1414
use crate::graphics::{Image, Text};
1515
use crate::text;
16-
use crate::triangle;
1716

1817
use lyon::geom::euclid;
1918
use lyon::tessellation;
@@ -33,7 +32,7 @@ pub enum Geometry {
3332

3433
#[derive(Debug, Clone, Default)]
3534
pub struct Cache {
36-
pub meshes: Option<triangle::Cache>,
35+
pub meshes: Option<mesh::Cache>,
3736
pub images: Option<Arc<[Image]>>,
3837
pub text: Option<text::Cache>,
3938
}
@@ -62,11 +61,17 @@ impl Cached for Geometry {
6261
Some(Arc::from(images))
6362
};
6463

64+
let meshes = Arc::from(meshes);
65+
6566
if let Some(mut previous) = previous {
6667
if let Some(cache) = &mut previous.meshes {
6768
cache.update(meshes);
6869
} else {
69-
previous.meshes = triangle::Cache::new(meshes);
70+
previous.meshes = if meshes.is_empty() {
71+
None
72+
} else {
73+
Some(mesh::Cache::new(meshes))
74+
};
7075
}
7176

7277
if let Some(cache) = &mut previous.text {
@@ -80,7 +85,11 @@ impl Cached for Geometry {
8085
previous
8186
} else {
8287
Cache {
83-
meshes: triangle::Cache::new(meshes),
88+
meshes: if meshes.is_empty() {
89+
None
90+
} else {
91+
Some(mesh::Cache::new(meshes))
92+
},
8493
images,
8594
text: text::Cache::new(group, text),
8695
}
@@ -547,8 +556,8 @@ impl BufferStack {
547556
Buffer::Solid(buffer) if !buffer.indices.is_empty() => {
548557
Some(Mesh::Solid {
549558
buffers: mesh::Indexed {
550-
vertices: buffer.vertices.into(),
551-
indices: buffer.indices.into(),
559+
vertices: buffer.vertices,
560+
indices: buffer.indices,
552561
},
553562
clip_bounds,
554563
transformation: Transformation::IDENTITY,
@@ -557,8 +566,8 @@ impl BufferStack {
557566
Buffer::Gradient(buffer) if !buffer.indices.is_empty() => {
558567
Some(Mesh::Gradient {
559568
buffers: mesh::Indexed {
560-
vertices: buffer.vertices.into(),
561-
indices: buffer.indices.into(),
569+
vertices: buffer.vertices,
570+
indices: buffer.indices,
562571
},
563572
clip_bounds,
564573
transformation: Transformation::IDENTITY,

wgpu/src/layer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::graphics;
55
use crate::graphics::Mesh;
66
use crate::graphics::color;
77
use crate::graphics::layer;
8+
use crate::graphics::mesh;
89
use crate::graphics::text::{Editor, Paragraph};
910
use crate::image::{self, Image};
1011
use crate::primitive::{self, Primitive};
@@ -230,7 +231,7 @@ impl Layer {
230231

231232
pub fn draw_mesh_cache(
232233
&mut self,
233-
cache: triangle::Cache,
234+
cache: mesh::Cache,
234235
transformation: Transformation,
235236
) {
236237
self.flush_meshes();

wgpu/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use crate::core::renderer;
6565
use crate::core::{
6666
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
6767
};
68+
use crate::graphics::mesh;
6869
use crate::graphics::text::{Editor, Paragraph};
6970
use crate::graphics::{Shell, Viewport};
7071

@@ -845,6 +846,11 @@ impl graphics::mesh::Renderer for Renderer {
845846
let (layer, transformation) = self.layers.current_mut();
846847
layer.draw_mesh(mesh, transformation);
847848
}
849+
850+
fn draw_mesh_cache(&mut self, cache: mesh::Cache) {
851+
let (layer, transformation) = self.layers.current_mut();
852+
layer.draw_mesh_cache(cache, transformation);
853+
}
848854
}
849855

850856
#[cfg(feature = "geometry")]

0 commit comments

Comments
 (0)