@@ -57,7 +57,7 @@ struct State {
57
57
queue : wgpu:: Queue ,
58
58
59
59
sc_desc : wgpu:: SwapChainDescriptor ,
60
- swap_chain : wgpu:: SwapChain ,
60
+ swap_chain : Option < wgpu:: SwapChain > ,
61
61
size : winit:: dpi:: PhysicalSize < u32 > ,
62
62
63
63
sampler : wgpu:: Sampler ,
@@ -127,7 +127,7 @@ impl State {
127
127
device,
128
128
queue,
129
129
sc_desc,
130
- swap_chain,
130
+ swap_chain : Some ( swap_chain ) ,
131
131
size,
132
132
sampler,
133
133
compute_bind_group : compute_group,
@@ -137,15 +137,19 @@ impl State {
137
137
}
138
138
139
139
fn resize ( & mut self , new_size : PhysicalSize < u32 > ) {
140
- self . sc_desc . width = new_size. width ;
141
- self . sc_desc . height = new_size. height ;
142
- self . size = new_size;
143
- let new_swap_chain = self . device . create_swap_chain ( & self . surface , & self . sc_desc ) ;
144
- self . swap_chain = new_swap_chain;
145
- let ( compute_bind_group, copy_bind_group) =
146
- State :: bind_for_size ( & self . device , & self . sampler , new_size, & self . layouts ) ;
147
- self . compute_bind_group = compute_bind_group;
148
- self . copy_bind_group = copy_bind_group;
140
+ if new_size. width != 0 && new_size. height != 0 {
141
+ self . sc_desc . width = new_size. width ;
142
+ self . sc_desc . height = new_size. height ;
143
+ self . size = new_size;
144
+ let new_swap_chain = self . device . create_swap_chain ( & self . surface , & self . sc_desc ) ;
145
+ self . swap_chain = Some ( new_swap_chain) ;
146
+ let ( compute_bind_group, copy_bind_group) =
147
+ State :: bind_for_size ( & self . device , & self . sampler , new_size, & self . layouts ) ;
148
+ self . compute_bind_group = compute_bind_group;
149
+ self . copy_bind_group = copy_bind_group;
150
+ } else {
151
+ self . swap_chain = None ;
152
+ }
149
153
}
150
154
151
155
fn bind_for_size (
@@ -310,46 +314,47 @@ impl State {
310
314
}
311
315
312
316
fn render_frame ( & self ) {
313
- let frame = self
314
- . swap_chain
315
- . get_current_frame ( )
316
- . expect ( "error getting frame from swap chain" )
317
- . output ;
317
+ if let Some ( chain ) = & self . swap_chain {
318
+ let frame = chain
319
+ . get_current_frame ( )
320
+ . expect ( "error getting frame from swap chain" )
321
+ . output ;
318
322
319
- let i_time: f32 = 0.5 + self . start_time . elapsed ( ) . as_micros ( ) as f32 * 1e-6 ;
320
- let size = self . size ;
321
- let config = Config {
322
- width : size. width ,
323
- height : size. height ,
324
- time : i_time,
325
- } ;
323
+ let i_time: f32 = 0.5 + self . start_time . elapsed ( ) . as_micros ( ) as f32 * 1e-6 ;
324
+ let size = self . size ;
325
+ let config = Config {
326
+ width : size. width ,
327
+ height : size. height ,
328
+ time : i_time,
329
+ } ;
326
330
327
- let mut encoder = self . device . create_command_encoder ( & Default :: default ( ) ) ;
328
- {
329
- let mut cpass = encoder. begin_compute_pass ( & Default :: default ( ) ) ;
330
- cpass. set_pipeline ( & self . pipelines . compute_pipeline ) ;
331
- cpass. set_push_constants ( 0 , bytemuck:: bytes_of ( & config) ) ;
332
- cpass. set_bind_group ( 0 , & self . compute_bind_group , & [ ] ) ;
333
- cpass. dispatch ( size. width / 16 , size. height / 16 , 1 ) ;
334
- }
335
- {
336
- let mut rpass = encoder. begin_render_pass ( & wgpu:: RenderPassDescriptor {
337
- label : None ,
338
- color_attachments : & [ wgpu:: RenderPassColorAttachment {
339
- view : & frame. view ,
340
- resolve_target : None ,
341
- ops : wgpu:: Operations {
342
- load : wgpu:: LoadOp :: Clear ( wgpu:: Color :: GREEN ) ,
343
- store : true ,
344
- } ,
345
- } ] ,
346
- depth_stencil_attachment : None ,
347
- } ) ;
348
- rpass. set_pipeline ( & self . pipelines . copy_pipeline ) ;
349
- rpass. set_bind_group ( 0 , & self . copy_bind_group , & [ ] ) ;
350
- rpass. draw ( 0 ..3 , 0 ..2 ) ;
331
+ let mut encoder = self . device . create_command_encoder ( & Default :: default ( ) ) ;
332
+ {
333
+ let mut cpass = encoder. begin_compute_pass ( & Default :: default ( ) ) ;
334
+ cpass. set_pipeline ( & self . pipelines . compute_pipeline ) ;
335
+ cpass. set_push_constants ( 0 , bytemuck:: bytes_of ( & config) ) ;
336
+ cpass. set_bind_group ( 0 , & self . compute_bind_group , & [ ] ) ;
337
+ cpass. dispatch ( size. width / 16 , size. height / 16 , 1 ) ;
338
+ }
339
+ {
340
+ let mut rpass = encoder. begin_render_pass ( & wgpu:: RenderPassDescriptor {
341
+ label : None ,
342
+ color_attachments : & [ wgpu:: RenderPassColorAttachment {
343
+ view : & frame. view ,
344
+ resolve_target : None ,
345
+ ops : wgpu:: Operations {
346
+ load : wgpu:: LoadOp :: Clear ( wgpu:: Color :: GREEN ) ,
347
+ store : true ,
348
+ } ,
349
+ } ] ,
350
+ depth_stencil_attachment : None ,
351
+ } ) ;
352
+ rpass. set_pipeline ( & self . pipelines . copy_pipeline ) ;
353
+ rpass. set_bind_group ( 0 , & self . copy_bind_group , & [ ] ) ;
354
+ rpass. draw ( 0 ..3 , 0 ..2 ) ;
355
+ }
356
+ self . queue . submit ( Some ( encoder. finish ( ) ) ) ;
351
357
}
352
- self . queue . submit ( Some ( encoder. finish ( ) ) ) ;
353
358
}
354
359
}
355
360
0 commit comments