1
- //! Slab storage and ops used for storing on CPU and extracting on GPU .
1
+ //! Slab storage and operations .
2
2
use core:: marker:: PhantomData ;
3
3
4
4
pub use renderling_derive:: Slabbed ;
5
5
6
- use crate :: id:: Id ;
6
+ use crate :: { array :: Array , id:: Id } ;
7
7
8
8
/// Determines the "size" of a type when stored in a slab of `&[u32]`,
9
9
/// and how to read/write it from/to the slab.
@@ -341,6 +341,7 @@ impl<T: core::any::Any> Slabbed for PhantomData<T> {
341
341
}
342
342
}
343
343
344
+ /// Trait for slabs of `u32`s that can store many types.
344
345
pub trait Slab {
345
346
/// Return the number of u32 elements in the slab.
346
347
fn len ( & self ) -> usize ;
@@ -366,12 +367,32 @@ pub trait Slab {
366
367
/// Write the type into the slab at the index.
367
368
///
368
369
/// Return the next index, or the same index if writing would overlap the slab.
369
- fn write < T : Slabbed + Default > ( & mut self , t : & T , index : usize ) -> usize ;
370
+ fn write_indexed < T : Slabbed > ( & mut self , t : & T , index : usize ) -> usize ;
370
371
371
372
/// Write a slice of the type into the slab at the index.
372
373
///
373
374
/// Return the next index, or the same index if writing would overlap the slab.
374
- fn write_slice < T : Slabbed + Default > ( & mut self , t : & [ T ] , index : usize ) -> usize ;
375
+ fn write_indexed_slice < T : Slabbed > ( & mut self , t : & [ T ] , index : usize ) -> usize ;
376
+
377
+ /// Write the type into the slab at the position of the given `Id`.
378
+ ///
379
+ /// This likely performs a partial write if the given `Id` is out of bounds.
380
+ fn write < T : Slabbed > ( & mut self , id : Id < T > , t : & T ) {
381
+ let _ = self . write_indexed ( t, id. index ( ) ) ;
382
+ }
383
+
384
+ /// Write contiguous elements into the slab at the position of the given `Array`.
385
+ ///
386
+ /// ## NOTE
387
+ /// This does nothing if the length of `Array` is greater than the length of `data`.
388
+ fn write_array < T : Slabbed > ( & mut self , array : Array < T > , data : & [ T ] ) {
389
+ if array. len ( ) > data. len ( ) {
390
+ return ;
391
+ }
392
+ for i in 0 ..array. len ( ) {
393
+ let _ = self . write ( array. at ( i) , & data[ i] ) ;
394
+ }
395
+ }
375
396
}
376
397
377
398
impl Slab for [ u32 ] {
@@ -385,11 +406,11 @@ impl Slab for [u32] {
385
406
t
386
407
}
387
408
388
- fn write < T : Slabbed + Default > ( & mut self , t : & T , index : usize ) -> usize {
409
+ fn write_indexed < T : Slabbed > ( & mut self , t : & T , index : usize ) -> usize {
389
410
t. write_slab ( index, self )
390
411
}
391
412
392
- fn write_slice < T : Slabbed + Default > ( & mut self , t : & [ T ] , index : usize ) -> usize {
413
+ fn write_indexed_slice < T : Slabbed > ( & mut self , t : & [ T ] , index : usize ) -> usize {
393
414
let mut index = index;
394
415
for item in t {
395
416
index = item. write_slab ( index, self ) ;
@@ -408,12 +429,12 @@ impl Slab for Vec<u32> {
408
429
self . as_slice ( ) . read ( id)
409
430
}
410
431
411
- fn write < T : Slabbed + Default > ( & mut self , t : & T , index : usize ) -> usize {
412
- self . as_mut_slice ( ) . write ( t, index)
432
+ fn write_indexed < T : Slabbed > ( & mut self , t : & T , index : usize ) -> usize {
433
+ self . as_mut_slice ( ) . write_indexed ( t, index)
413
434
}
414
435
415
- fn write_slice < T : Slabbed + Default > ( & mut self , t : & [ T ] , index : usize ) -> usize {
416
- self . as_mut_slice ( ) . write_slice ( t, index)
436
+ fn write_indexed_slice < T : Slabbed > ( & mut self , t : & [ T ] , index : usize ) -> usize {
437
+ self . as_mut_slice ( ) . write_indexed_slice ( t, index)
417
438
}
418
439
}
419
440
@@ -428,16 +449,16 @@ mod test {
428
449
#[ test]
429
450
fn slab_array_readwrite ( ) {
430
451
let mut slab = [ 0u32 ; 16 ] ;
431
- slab. write ( & 42 , 0 ) ;
432
- slab. write ( & 666 , 1 ) ;
452
+ slab. write_indexed ( & 42 , 0 ) ;
453
+ slab. write_indexed ( & 666 , 1 ) ;
433
454
let t = slab. read ( Id :: < [ u32 ; 2 ] > :: new ( 0 ) ) ;
434
455
assert_eq ! ( [ 42 , 666 ] , t) ;
435
456
let t: Vec < u32 > = slab. read_vec ( Array :: new ( 0 , 2 ) ) ;
436
457
assert_eq ! ( [ 42 , 666 ] , t[ ..] ) ;
437
- slab. write_slice ( & [ 1 , 2 , 3 , 4 ] , 2 ) ;
458
+ slab. write_indexed_slice ( & [ 1 , 2 , 3 , 4 ] , 2 ) ;
438
459
let t: Vec < u32 > = slab. read_vec ( Array :: new ( 2 , 4 ) ) ;
439
460
assert_eq ! ( [ 1 , 2 , 3 , 4 ] , t[ ..] ) ;
440
- slab. write_slice ( & [ [ 1.0 , 2.0 , 3.0 , 4.0 ] , [ 5.5 , 6.5 , 7.5 , 8.5 ] ] , 0 ) ;
461
+ slab. write_indexed_slice ( & [ [ 1.0 , 2.0 , 3.0 , 4.0 ] , [ 5.5 , 6.5 , 7.5 , 8.5 ] ] , 0 ) ;
441
462
442
463
let arr = Array :: < [ f32 ; 4 ] > :: new ( 0 , 2 ) ;
443
464
assert_eq ! ( Id :: new( 0 ) , arr. at( 0 ) ) ;
@@ -481,10 +502,10 @@ mod test {
481
502
let mut slab = vec ! [ 0u32 ; geometry_slab_size + Array :: <Vertex >:: slab_size( ) ] ;
482
503
let index = 0usize ;
483
504
let vertices = Array :: < Vertex > :: new ( index as u32 , geometry. len ( ) as u32 ) ;
484
- let index = slab. write_slice ( & geometry, index) ;
505
+ let index = slab. write_indexed_slice ( & geometry, index) ;
485
506
assert_eq ! ( geometry_slab_size, index) ;
486
507
let vertices_id = Id :: < Array < Vertex > > :: from ( index) ;
487
- let index = slab. write ( & vertices, index) ;
508
+ let index = slab. write_indexed ( & vertices, index) ;
488
509
assert_eq ! ( geometry_slab_size + Array :: <Vertex >:: slab_size( ) , index) ;
489
510
assert_eq ! ( Vertex :: slab_size( ) * 6 , vertices_id. index( ) ) ;
490
511
assert ! ( slab. contains( vertices_id) , ) ;
0 commit comments