@@ -8,8 +8,7 @@ use crate::graphics::mesh::{self, Mesh};
88
99use rustc_hash:: FxHashMap ;
1010use std:: collections:: hash_map;
11- use std:: sync:: atomic:: { self , AtomicU64 } ;
12- use std:: sync:: { self , Arc } ;
11+ use std:: sync:: Weak ;
1312
1413const INITIAL_INDEX_COUNT : usize = 1_000 ;
1514const INITIAL_VERTEX_COUNT : usize = 1_000 ;
@@ -24,65 +23,34 @@ pub enum Item {
2423 } ,
2524 Cached {
2625 transformation : Transformation ,
27- cache : Cache ,
26+ cache : mesh :: Cache ,
2827 } ,
2928}
3029
31- #[ derive( Debug , Clone ) ]
32- pub struct Cache {
33- id : Id ,
34- batch : Arc < [ Mesh ] > ,
35- version : usize ,
36- }
37-
38- #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
39- pub struct Id ( u64 ) ;
40-
41- impl Cache {
42- pub fn new ( meshes : Vec < Mesh > ) -> Option < Self > {
43- static NEXT_ID : AtomicU64 = AtomicU64 :: new ( 0 ) ;
44-
45- if meshes. is_empty ( ) {
46- return None ;
47- }
48-
49- Some ( Self {
50- id : Id ( NEXT_ID . fetch_add ( 1 , atomic:: Ordering :: Relaxed ) ) ,
51- batch : Arc :: from ( meshes) ,
52- version : 0 ,
53- } )
54- }
55-
56- pub fn update ( & mut self , meshes : Vec < Mesh > ) {
57- self . batch = Arc :: from ( meshes) ;
58- self . version += 1 ;
59- }
60- }
61-
6230#[ derive( Debug ) ]
6331struct Upload {
6432 layer : Layer ,
6533 transformation : Transformation ,
6634 version : usize ,
67- batch : sync :: Weak < [ Mesh ] > ,
35+ batch : Weak < [ Mesh ] > ,
6836}
6937
7038#[ derive( Debug , Default ) ]
7139pub struct Storage {
72- uploads : FxHashMap < Id , Upload > ,
40+ uploads : FxHashMap < mesh :: Id , Upload > ,
7341}
7442
7543impl Storage {
7644 pub fn new ( ) -> Self {
7745 Self :: default ( )
7846 }
7947
80- fn get ( & self , cache : & Cache ) -> Option < & Upload > {
81- if cache. batch . is_empty ( ) {
48+ fn get ( & self , cache : & mesh :: Cache ) -> Option < & Upload > {
49+ if cache. is_empty ( ) {
8250 return None ;
8351 }
8452
85- self . uploads . get ( & cache. id )
53+ self . uploads . get ( & cache. id ( ) )
8654 }
8755
8856 fn prepare (
@@ -92,15 +60,15 @@ impl Storage {
9260 belt : & mut wgpu:: util:: StagingBelt ,
9361 solid : & solid:: Pipeline ,
9462 gradient : & gradient:: Pipeline ,
95- cache : & Cache ,
63+ cache : & mesh :: Cache ,
9664 new_transformation : Transformation ,
9765 ) {
98- match self . uploads . entry ( cache. id ) {
66+ match self . uploads . entry ( cache. id ( ) ) {
9967 hash_map:: Entry :: Occupied ( entry) => {
10068 let upload = entry. into_mut ( ) ;
10169
102- if !cache. batch . is_empty ( )
103- && ( upload. version != cache. version
70+ if !cache. is_empty ( )
71+ && ( upload. version != cache. version ( )
10472 || upload. transformation != new_transformation)
10573 {
10674 upload. layer . prepare (
@@ -109,12 +77,12 @@ impl Storage {
10977 belt,
11078 solid,
11179 gradient,
112- & cache. batch ,
80+ cache. batch ( ) ,
11381 new_transformation,
11482 ) ;
11583
116- upload. batch = Arc :: downgrade ( & cache. batch ) ;
117- upload. version = cache. version ;
84+ upload. batch = cache. downgrade ( ) ;
85+ upload. version = cache. version ( ) ;
11886 upload. transformation = new_transformation;
11987 }
12088 }
@@ -127,20 +95,20 @@ impl Storage {
12795 belt,
12896 solid,
12997 gradient,
130- & cache. batch ,
98+ cache. batch ( ) ,
13199 new_transformation,
132100 ) ;
133101
134102 let _ = entry. insert ( Upload {
135103 layer,
136104 transformation : new_transformation,
137105 version : 0 ,
138- batch : Arc :: downgrade ( & cache. batch ) ,
106+ batch : cache. downgrade ( ) ,
139107 } ) ;
140108
141109 log:: debug!(
142- "New mesh upload: {} (total: {})" ,
143- cache. id. 0 ,
110+ "New mesh upload: {:? } (total: {})" ,
111+ cache. id( ) ,
144112 self . uploads. len( )
145113 ) ;
146114 }
@@ -278,7 +246,7 @@ impl State {
278246
279247 Some ( (
280248 & upload. layer ,
281- & cache. batch ,
249+ cache. batch ( ) ,
282250 screen_transformation * * transformation,
283251 ) )
284252 }
0 commit comments