@@ -13,7 +13,9 @@ use crate::core::{Rectangle, Size, Transformation};
1313use crate :: Buffer ;
1414
1515use bytemuck:: { Pod , Zeroable } ;
16+
1617use std:: mem;
18+ use std:: sync:: Arc ;
1719
1820pub use crate :: graphics:: Image ;
1921
@@ -22,13 +24,11 @@ pub type Batch = Vec<Image>;
2224#[ derive( Debug ) ]
2325pub struct Pipeline {
2426 pipeline : wgpu:: RenderPipeline ,
27+ backend : wgpu:: Backend ,
2528 nearest_sampler : wgpu:: Sampler ,
2629 linear_sampler : wgpu:: Sampler ,
27- texture : wgpu:: BindGroup ,
28- texture_version : usize ,
29- texture_layout : wgpu:: BindGroupLayout ,
30+ texture_layout : Arc < wgpu:: BindGroupLayout > ,
3031 constant_layout : wgpu:: BindGroupLayout ,
31- cache : cache:: Shared ,
3232 layers : Vec < Layer > ,
3333 prepare_layer : usize ,
3434}
@@ -186,32 +186,28 @@ impl Pipeline {
186186 multiview : None ,
187187 } ) ;
188188
189- let cache = Cache :: new ( device, backend) ;
190- let texture = cache. create_bind_group ( device, & texture_layout) ;
191-
192189 Pipeline {
193190 pipeline,
191+ backend,
194192 nearest_sampler,
195193 linear_sampler,
196- texture,
197- texture_version : cache. layer_count ( ) ,
198- texture_layout,
194+ texture_layout : Arc :: new ( texture_layout) ,
199195 constant_layout,
200- cache : cache:: Shared :: new ( cache) ,
201196 layers : Vec :: new ( ) ,
202197 prepare_layer : 0 ,
203198 }
204199 }
205200
206- pub fn cache ( & self ) -> & cache :: Shared {
207- & self . cache
201+ pub fn create_cache ( & self , device : & wgpu :: Device ) -> Cache {
202+ Cache :: new ( device , self . backend , self . texture_layout . clone ( ) )
208203 }
209204
210205 pub fn prepare (
211206 & mut self ,
212207 device : & wgpu:: Device ,
213208 encoder : & mut wgpu:: CommandEncoder ,
214209 belt : & mut wgpu:: util:: StagingBelt ,
210+ cache : & mut Cache ,
215211 images : & Batch ,
216212 transformation : Transformation ,
217213 scale : f32 ,
@@ -221,8 +217,6 @@ impl Pipeline {
221217 let nearest_instances: & mut Vec < Instance > = & mut Vec :: new ( ) ;
222218 let linear_instances: & mut Vec < Instance > = & mut Vec :: new ( ) ;
223219
224- let mut cache = self . cache . lock ( ) ;
225-
226220 for image in images {
227221 match & image {
228222 #[ cfg( feature = "image" ) ]
@@ -288,16 +282,6 @@ impl Pipeline {
288282 return ;
289283 }
290284
291- let texture_version = cache. layer_count ( ) ;
292-
293- if self . texture_version != texture_version {
294- log:: debug!( "Atlas has grown. Recreating bind group..." ) ;
295-
296- self . texture =
297- cache. create_bind_group ( device, & self . texture_layout ) ;
298- self . texture_version = texture_version;
299- }
300-
301285 if self . layers . len ( ) <= self . prepare_layer {
302286 self . layers . push ( Layer :: new (
303287 device,
@@ -323,6 +307,7 @@ impl Pipeline {
323307
324308 pub fn render < ' a > (
325309 & ' a self ,
310+ cache : & ' a Cache ,
326311 layer : usize ,
327312 bounds : Rectangle < u32 > ,
328313 render_pass : & mut wgpu:: RenderPass < ' a > ,
@@ -337,14 +322,13 @@ impl Pipeline {
337322 bounds. height ,
338323 ) ;
339324
340- render_pass. set_bind_group ( 1 , & self . texture , & [ ] ) ;
325+ render_pass. set_bind_group ( 1 , cache . bind_group ( ) , & [ ] ) ;
341326
342327 layer. render ( render_pass) ;
343328 }
344329 }
345330
346331 pub fn end_frame ( & mut self ) {
347- self . cache . lock ( ) . trim ( ) ;
348332 self . prepare_layer = 0 ;
349333 }
350334}
0 commit comments