@@ -80,16 +80,13 @@ fn map_and_send_buffer_async(query: Query<&ExtractedGpuPickingCamera, With<Extra
80
80
Err ( err) => bevy_log:: error!( "Failed to map entity buffer: {err}" ) ,
81
81
} ) ;
82
82
83
- // Buffer is mapped and ready to be sent
83
+ // This waits until the Buffer is mapped
84
84
rx. recv ( ) . await . unwrap ( ) ;
85
85
86
86
// Send the buffer to the main world
87
- let entity_buffer = & buffers. entity_buffer ;
88
87
let buffer_view = buffer_slice. get_mapped_range ( ) ;
89
- // Immediately copy the data to CPU to avoid holding the mapped view for long
88
+ // Copy the data to the CPU
90
89
let entity_data = Vec :: from ( & * buffer_view) ;
91
- drop ( buffer_view) ;
92
- entity_buffer. unmap ( ) ;
93
90
94
91
// Because the channel is bounded to 1 entry, the channel will sometimes be full.
95
92
// This isn't ideal but not blocking makes it faster which is preferred.
@@ -245,6 +242,7 @@ pub fn entity_as_uvec2(entity: Entity) -> UVec2 {
245
242
UVec2 :: new ( bits as u32 , ( bits >> 32 ) as u32 )
246
243
}
247
244
245
+ /// The textures used to draw the entity for each rendered mesh
248
246
#[ derive( Component , Clone ) ]
249
247
pub struct EntityTextures {
250
248
pub main : CachedTexture ,
@@ -282,6 +280,7 @@ impl EntityTextures {
282
280
}
283
281
}
284
282
283
+ /// This creates the required buffers for each camera
285
284
fn prepare_gpu_picking_buffers (
286
285
render_device : Res < RenderDevice > ,
287
286
mut cameras : Query <
@@ -294,6 +293,9 @@ fn prepare_gpu_picking_buffers(
294
293
295
294
let buffer_dimensions =
296
295
BufferDimensions :: new ( size. x as usize , size. y as usize , ENTITY_TEXTURE_FORMAT ) ;
296
+ // TODO We currently create one buffer per frame. This isn't ideal.
297
+ // We should have a way to only create a buffer when required. Unfortunately, the async nature of
298
+ // the buffer map api makes it hard to know how many buffer we'll need since we need at least one per frame.
297
299
let entity_buffer = render_device. create_buffer ( & BufferDescriptor {
298
300
label : Some ( "Entity Buffer" ) ,
299
301
size : ( buffer_dimensions. padded_bytes_per_row * buffer_dimensions. height ) as u64 ,
0 commit comments