33//! Meshes abstract over vertex and index data on the GPU as well, and
44//! provide utilities for constructing data from pieces.
55
6- use std:: { cell :: RefCell , rc :: Rc } ;
6+ use std:: sync :: { Arc , Mutex } ;
77
88use common:: { vec2, Size , Vec2 , Vec3 } ;
99pub use macros:: Vertex ;
@@ -124,7 +124,7 @@ impl Vertex3 {
124124/// for rendering at any time, provided a valid [`Material`] is available.
125125#[ derive( Clone ) ]
126126pub struct Mesh < V > {
127- state : Rc < RefCell < MeshState < V > > > ,
127+ state : Arc < Mutex < MeshState < V > > > ,
128128}
129129
130130/// The internal state for a mesh.
@@ -148,7 +148,7 @@ impl<V: Vertex> Mesh<V> {
148148 let indices = Buffer :: new ( BufferKind :: Index , usage) . map_err ( |_| MeshError :: FailedToCreate ) ?;
149149
150150 Ok ( Self {
151- state : Rc :: new ( RefCell :: new ( MeshState {
151+ state : Arc :: new ( Mutex :: new ( MeshState {
152152 id : graphics ( ) . mesh_create ( vertices. id ( ) , indices. id ( ) , V :: DESCRIPTORS ) ?,
153153 vertices,
154154 indices,
@@ -181,22 +181,22 @@ impl<V: Vertex> Mesh<V> {
181181
182182 /// Returns the identifier of this mesh.
183183 pub fn id ( & self ) -> MeshId {
184- self . state . borrow ( ) . id
184+ self . state . lock ( ) . unwrap ( ) . id
185185 }
186186
187187 /// Returns the number of vertices in the mesh.
188188 pub fn vertices ( & self ) -> usize {
189- self . state . borrow ( ) . vertices . len ( )
189+ self . state . lock ( ) . unwrap ( ) . vertices . len ( )
190190 }
191191
192192 /// Returns the number of indices in the mesh.
193193 pub fn indices ( & self ) -> usize {
194- self . state . borrow ( ) . indices . len ( )
194+ self . state . lock ( ) . unwrap ( ) . indices . len ( )
195195 }
196196
197197 /// Draws this mesh with the given material and topology.
198198 pub fn draw ( & self , material : & Material , topology : PrimitiveTopology ) {
199- let state = self . state . borrow ( ) ;
199+ let state = self . state . lock ( ) . unwrap ( ) ;
200200
201201 self . draw_sub ( material, topology, state. vertices . len ( ) , state. indices . len ( ) ) ;
202202 }
@@ -205,7 +205,7 @@ impl<V: Vertex> Mesh<V> {
205205 pub fn draw_sub ( & self , material : & Material , topology : PrimitiveTopology , vertex_count : usize , index_count : usize ) {
206206 material. bind ( ) ;
207207
208- let state = self . state . borrow ( ) ;
208+ let state = self . state . lock ( ) . unwrap ( ) ;
209209
210210 graphics ( )
211211 . mesh_draw ( state. id , topology, vertex_count, index_count)
@@ -216,7 +216,7 @@ impl<V: Vertex> Mesh<V> {
216216
217217 /// Acquires mutable write access the mesh buffers.
218218 pub fn with_buffers ( & mut self , body : impl FnOnce ( & mut Buffer < V > , & mut Buffer < Index > ) ) {
219- let state = & mut self . state . borrow_mut ( ) ;
219+ let state = & mut self . state . lock ( ) . unwrap ( ) ;
220220 let ( vertices, indices) = state. borrow_buffers_mut ( ) ;
221221
222222 body ( vertices, indices) ;
0 commit comments