@@ -31,12 +31,15 @@ impl State {
3131
3232 // The instance is a handle to our GPU
3333 // BackendBit::PRIMARY => Vulkan + Metal + DX12 + Browser WebGPU
34- let instance = wgpu:: Instance :: new ( & wgpu:: InstanceDescriptor {
34+ let instance = wgpu:: Instance :: new ( wgpu:: InstanceDescriptor {
3535 #[ cfg( not( target_arch = "wasm32" ) ) ]
3636 backends : wgpu:: Backends :: PRIMARY ,
3737 #[ cfg( target_arch = "wasm32" ) ]
3838 backends : wgpu:: Backends :: GL ,
39- ..Default :: default ( )
39+ flags : Default :: default ( ) ,
40+ memory_budget_thresholds : Default :: default ( ) ,
41+ backend_options : Default :: default ( ) ,
42+ display : None ,
4043 } ) ;
4144
4245 let surface = instance. create_surface ( window. clone ( ) ) . unwrap ( ) ;
@@ -232,15 +235,36 @@ impl State {
232235 #[ allow( dead_code) ]
233236 fn update ( & mut self ) { }
234237
235- fn render ( & mut self ) -> Result < ( ) , wgpu :: SurfaceError > {
238+ fn render ( & mut self ) -> anyhow :: Result < ( ) > {
236239 self . window . request_redraw ( ) ;
237240
238241 // We can't render unless the surface is configured
239242 if !self . is_surface_configured {
240243 return Ok ( ( ) ) ;
241244 }
242245
243- let output = self . surface . get_current_texture ( ) ?;
246+ let output = match self . surface . get_current_texture ( ) {
247+ wgpu:: CurrentSurfaceTexture :: Success ( surface_texture) => surface_texture,
248+ wgpu:: CurrentSurfaceTexture :: Suboptimal ( surface_texture) => {
249+ self . surface . configure ( & self . device , & self . config ) ;
250+ surface_texture
251+ }
252+ wgpu:: CurrentSurfaceTexture :: Timeout
253+ | wgpu:: CurrentSurfaceTexture :: Occluded
254+ | wgpu:: CurrentSurfaceTexture :: Validation => {
255+ // Skip this frame
256+ return Ok ( ( ) ) ;
257+ }
258+ wgpu:: CurrentSurfaceTexture :: Outdated => {
259+ self . surface . configure ( & self . device , & self . config ) ;
260+ return Ok ( ( ) ) ;
261+ }
262+ wgpu:: CurrentSurfaceTexture :: Lost => {
263+ // You could recreate the devices and all resources
264+ // created with it here, but we'll just bail
265+ anyhow:: bail!( "Lost device" ) ;
266+ }
267+ } ;
244268 let view = output
245269 . texture
246270 . create_view ( & wgpu:: TextureViewDescriptor :: default ( ) ) ;
@@ -385,13 +409,10 @@ impl ApplicationHandler<State> for App {
385409 WindowEvent :: RedrawRequested => {
386410 match state. render ( ) {
387411 Ok ( _) => { }
388- // Reconfigure the surface if it's lost or outdated
389- Err ( wgpu:: SurfaceError :: Lost | wgpu:: SurfaceError :: Outdated ) => {
390- let size = state. window . inner_size ( ) ;
391- state. resize ( size. width , size. height ) ;
392- }
393412 Err ( e) => {
394- log:: error!( "Unable to render {}" , e) ;
413+ // Log the error and exit gracefully
414+ log:: error!( "{e}" ) ;
415+ event_loop. exit ( ) ;
395416 }
396417 }
397418 }
0 commit comments