From 7a034a130a934da798a6e9566e2641a4e6dd3870 Mon Sep 17 00:00:00 2001 From: Victor van den Elzen Date: Sat, 18 Jun 2022 13:17:38 +0200 Subject: [PATCH] Add back get_preferred_format and sRGB preference --- wgpu/examples/framework.rs | 6 +----- wgpu/examples/hello-triangle/main.rs | 6 +----- wgpu/examples/hello-windows/main.rs | 7 +------ wgpu/src/lib.rs | 21 ++++++++++++++++++++- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 802760364c..08d4fa9a78 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -269,11 +269,7 @@ fn start( let spawner = Spawner::new(); let mut config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: *surface - .get_supported_formats(&adapter) - .unwrap() - .first() - .unwrap(), + format: surface.get_preferred_format(&adapter).unwrap(), width: size.width, height: size.height, present_mode: wgpu::PresentMode::Mailbox, diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index c1d292d41c..fe6f0c2132 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -46,11 +46,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) { push_constant_ranges: &[], }); - let swapchain_format = *surface - .get_supported_formats(&adapter) - .unwrap() - .first() - .unwrap(); + let swapchain_format = surface.get_preferred_format(&adapter).unwrap(); let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: None, diff --git a/wgpu/examples/hello-windows/main.rs b/wgpu/examples/hello-windows/main.rs index 7ead1cbc8e..ca725f44b3 100644 --- a/wgpu/examples/hello-windows/main.rs +++ b/wgpu/examples/hello-windows/main.rs @@ -33,12 +33,7 @@ impl ViewportDesc { let config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: *self - .surface - .get_supported_formats(adapter) - .unwrap() - .first() - .unwrap(), + format: self.surface.get_preferred_format(adapter).unwrap(), width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 187ca59d8e..74e3aa2699 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -3447,13 +3447,32 @@ impl Drop for SurfaceTexture { impl Surface { /// Returns a vec of supported texture formats to use for the [`Surface`] with this adapter. - /// Note: The first format in the vector is preferred /// /// Returns None if the surface is incompatible with the adapter. pub fn get_supported_formats(&self, adapter: &Adapter) -> Option> { Context::surface_get_supported_formats(&*self.context, &self.id, &adapter.id) } + /// Returns an optimal texture format to use for the [`Surface`] with this adapter. + pub fn get_preferred_format(&self, adapter: &Adapter) -> Option { + // Check the four formats mentioned in the WebGPU spec. + // Also, prefer sRGB over linear as it is better in + // representing perceived colors. + let preferred_formats = [ + wgt::TextureFormat::Bgra8UnormSrgb, + wgt::TextureFormat::Rgba8UnormSrgb, + wgt::TextureFormat::Bgra8Unorm, + wgt::TextureFormat::Rgba8Unorm, + wgt::TextureFormat::Rgba16Float, + ]; + + let formats = self.get_supported_formats(adapter)?; + preferred_formats + .iter() + .cloned() + .find(|preferred| formats.contains(preferred)) + } + /// Initializes [`Surface`] for presentation. /// /// # Panics