3
3
//! Meshes abstract over vertex and index data on the GPU as well, and
4
4
//! provide utilities for constructing data from pieces.
5
5
6
- use std:: { cell :: RefCell , rc :: Rc } ;
6
+ use std:: sync :: { Arc , Mutex } ;
7
7
8
8
use common:: { vec2, Size , Vec2 , Vec3 } ;
9
9
pub use macros:: Vertex ;
@@ -124,7 +124,7 @@ impl Vertex3 {
124
124
/// for rendering at any time, provided a valid [`Material`] is available.
125
125
#[ derive( Clone ) ]
126
126
pub struct Mesh < V > {
127
- state : Rc < RefCell < MeshState < V > > > ,
127
+ state : Arc < Mutex < MeshState < V > > > ,
128
128
}
129
129
130
130
/// The internal state for a mesh.
@@ -148,7 +148,7 @@ impl<V: Vertex> Mesh<V> {
148
148
let indices = Buffer :: new ( BufferKind :: Index , usage) . map_err ( |_| MeshError :: FailedToCreate ) ?;
149
149
150
150
Ok ( Self {
151
- state : Rc :: new ( RefCell :: new ( MeshState {
151
+ state : Arc :: new ( Mutex :: new ( MeshState {
152
152
id : graphics ( ) . mesh_create ( vertices. id ( ) , indices. id ( ) , V :: DESCRIPTORS ) ?,
153
153
vertices,
154
154
indices,
@@ -181,22 +181,22 @@ impl<V: Vertex> Mesh<V> {
181
181
182
182
/// Returns the identifier of this mesh.
183
183
pub fn id ( & self ) -> MeshId {
184
- self . state . borrow ( ) . id
184
+ self . state . lock ( ) . unwrap ( ) . id
185
185
}
186
186
187
187
/// Returns the number of vertices in the mesh.
188
188
pub fn vertices ( & self ) -> usize {
189
- self . state . borrow ( ) . vertices . len ( )
189
+ self . state . lock ( ) . unwrap ( ) . vertices . len ( )
190
190
}
191
191
192
192
/// Returns the number of indices in the mesh.
193
193
pub fn indices ( & self ) -> usize {
194
- self . state . borrow ( ) . indices . len ( )
194
+ self . state . lock ( ) . unwrap ( ) . indices . len ( )
195
195
}
196
196
197
197
/// Draws this mesh with the given material and topology.
198
198
pub fn draw ( & self , material : & Material , topology : PrimitiveTopology ) {
199
- let state = self . state . borrow ( ) ;
199
+ let state = self . state . lock ( ) . unwrap ( ) ;
200
200
201
201
self . draw_sub ( material, topology, state. vertices . len ( ) , state. indices . len ( ) ) ;
202
202
}
@@ -205,7 +205,7 @@ impl<V: Vertex> Mesh<V> {
205
205
pub fn draw_sub ( & self , material : & Material , topology : PrimitiveTopology , vertex_count : usize , index_count : usize ) {
206
206
material. bind ( ) ;
207
207
208
- let state = self . state . borrow ( ) ;
208
+ let state = self . state . lock ( ) . unwrap ( ) ;
209
209
210
210
graphics ( )
211
211
. mesh_draw ( state. id , topology, vertex_count, index_count)
@@ -216,7 +216,7 @@ impl<V: Vertex> Mesh<V> {
216
216
217
217
/// Acquires mutable write access the mesh buffers.
218
218
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 ( ) ;
220
220
let ( vertices, indices) = state. borrow_buffers_mut ( ) ;
221
221
222
222
body ( vertices, indices) ;
0 commit comments