@@ -5,7 +5,8 @@ use crate::gradient;
55
66use 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 ) ]
7374pub 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`].
147204pub 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}
0 commit comments