11use crate :: { DeviceHandle , WgpuContextError , util:: create_texture} ;
22use wgpu:: {
3- CommandEncoderDescriptor , CompositeAlphaMode , Device , PresentMode , Queue , Surface ,
4- SurfaceConfiguration , SurfaceError , SurfaceTexture , TextureFormat , TextureUsages , TextureView ,
5- TextureViewDescriptor , util:: TextureBlitter ,
3+ CommandEncoderDescriptor , CompositeAlphaMode , CurrentSurfaceTexture , Device , PresentMode ,
4+ Queue , Surface , SurfaceConfiguration , SurfaceTexture , TextureFormat , TextureUsages ,
5+ TextureView , TextureViewDescriptor , util:: TextureBlitter ,
66} ;
77
8+ /// Error getting the current surface texture
9+ #[ derive( Clone , Debug ) ]
10+ pub struct GetCurrentSurfaceTextureErr ;
11+
812#[ derive( Clone ) ]
913pub struct TextureConfiguration {
1014 pub usage : TextureUsages ,
@@ -85,7 +89,7 @@ pub struct SurfaceRenderer<'s> {
8589 pub surface : Surface < ' s > ,
8690 pub config : SurfaceConfiguration ,
8791
88- current_surface_texture : Option < Result < SurfaceTexture , SurfaceError > > ,
92+ current_surface_texture : Option < CurrentSurfaceTexture > ,
8993 intermediate_texture : Option < Box < IntermediateTextureStuff > > ,
9094}
9195
@@ -189,43 +193,41 @@ impl<'s> SurfaceRenderer<'s> {
189193 self . current_surface_texture = None ;
190194 }
191195
192- pub fn ensure_current_surface_texture ( & mut self ) -> Result < ( ) , SurfaceError > {
196+ pub fn ensure_current_surface_texture (
197+ & mut self ,
198+ ) -> Result < & SurfaceTexture , GetCurrentSurfaceTextureErr > {
193199 if self . current_surface_texture . is_none ( ) {
194200 let tex = self . surface . get_current_texture ( ) ;
195- if let Err ( SurfaceError :: Lost | SurfaceError :: Outdated ) = & tex {
196- self . surface
197- . configure ( & self . device_handle . device , & self . config ) ;
201+ match & tex {
202+ CurrentSurfaceTexture :: Lost
203+ | CurrentSurfaceTexture :: Outdated
204+ | CurrentSurfaceTexture :: Suboptimal ( _) => {
205+ self . surface
206+ . configure ( & self . device_handle . device , & self . config ) ;
207+ }
208+ _ => { }
198209 }
199210
200211 self . current_surface_texture = Some ( tex) ;
201212 }
202213
203- self . current_surface_texture
204- . as_ref ( )
205- . unwrap ( )
206- . as_ref ( )
207- . map ( |_| ( ) )
208- . map_err ( |err| err. clone ( ) )
214+ match self . current_surface_texture . as_ref ( ) . unwrap ( ) {
215+ CurrentSurfaceTexture :: Success ( surface_texture) => Ok ( surface_texture) ,
216+ _ => Err ( GetCurrentSurfaceTextureErr ) ,
217+ }
209218 }
210219
211220 /// Get a target texture view to render to.
212221 ///
213222 /// If there is an intermediate texture, this is a view of that intermediate texture, otherwise
214223 /// it is a view of the surface texture.
215- pub fn target_texture_view ( & mut self ) -> Result < TextureView , SurfaceError > {
224+ pub fn target_texture_view ( & mut self ) -> Result < TextureView , GetCurrentSurfaceTextureErr > {
216225 match & self . intermediate_texture {
217226 Some ( intermediate_texture) => Ok ( intermediate_texture. texture_view . clone ( ) ) ,
218- None => {
219- self . ensure_current_surface_texture ( ) ?;
220- Ok ( self
221- . current_surface_texture
222- . as_ref ( )
223- . unwrap ( )
224- . as_ref ( )
225- . unwrap ( )
226- . texture
227- . create_view ( & TextureViewDescriptor :: default ( ) ) )
228- }
227+ None => Ok ( self
228+ . ensure_current_surface_texture ( ) ?
229+ . texture
230+ . create_view ( & TextureViewDescriptor :: default ( ) ) ) ,
229231 }
230232 }
231233
@@ -234,9 +236,13 @@ impl<'s> SurfaceRenderer<'s> {
234236 ///
235237 /// Prior to calling this, [`Self::target_texture_view`] must have been called and some
236238 /// rendering work must have been scheduled to the resulting view.
237- pub fn maybe_blit_and_present ( & mut self ) -> Result < ( ) , SurfaceError > {
239+ pub fn maybe_blit_and_present ( & mut self ) -> Result < ( ) , GetCurrentSurfaceTextureErr > {
238240 self . ensure_current_surface_texture ( ) ?;
239- let surface_texture = self . current_surface_texture . take ( ) . unwrap ( ) . unwrap ( ) ;
241+ let CurrentSurfaceTexture :: Success ( surface_texture) =
242+ self . current_surface_texture . take ( ) . unwrap ( )
243+ else {
244+ unreachable ! ( "Surface texture was set in ensure_current_surface_texture above" ) ;
245+ } ;
240246
241247 if let Some ( its) = & self . intermediate_texture {
242248 self . blit_from_intermediate_texture_to_surface ( & surface_texture, its) ;
0 commit comments