From 104e37b37626fa5d4c96436c186de78eaad8c1b7 Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 14:59:18 -0700 Subject: [PATCH 01/10] Change all the functions --- wgpu-core/src/device/mod.rs | 8 ++++---- wgpu-core/src/instance.rs | 16 +++++++++++----- wgpu/examples/framework.rs | 6 +++++- wgpu/examples/hello-triangle/main.rs | 6 +++++- wgpu/examples/hello-windows/main.rs | 7 ++++++- wgpu/src/backend/direct.rs | 10 +++++----- wgpu/src/backend/web.rs | 6 +++--- wgpu/src/lib.rs | 8 ++++---- 8 files changed, 43 insertions(+), 24 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 8a9eb0fbbe..e0668a6228 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3013,12 +3013,12 @@ impl Global { .map_err(|_| instance::IsSurfaceSupportedError::InvalidSurface)?; Ok(adapter.is_surface_supported(surface)) } - pub fn surface_get_preferred_format( + pub fn surface_get_supported_formats( &self, surface_id: id::SurfaceId, adapter_id: id::AdapterId, - ) -> Result { - profiling::scope!("surface_get_preferred_format"); + ) -> Result, instance::GetSurfacePreferredFormatError> { + profiling::scope!("surface_get_supported_formats"); let hub = A::hub(self); let mut token = Token::root(); @@ -3031,7 +3031,7 @@ impl Global { .get(surface_id) .map_err(|_| instance::GetSurfacePreferredFormatError::InvalidSurface)?; - surface.get_preferred_format(adapter) + surface.get_supported_formats(adapter) } pub fn device_features( diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 907c65d08c..cd2f636c40 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -152,14 +152,14 @@ impl crate::hub::Resource for Surface { } impl Surface { - pub fn get_preferred_format( + pub fn get_supported_formats( &self, adapter: &Adapter, - ) -> Result { + ) -> Result, GetSurfacePreferredFormatError> { // 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 = [ + let candidate_formats = [ wgt::TextureFormat::Bgra8UnormSrgb, wgt::TextureFormat::Rgba8UnormSrgb, wgt::TextureFormat::Bgra8Unorm, @@ -177,10 +177,16 @@ impl Surface { .ok_or(GetSurfacePreferredFormatError::UnsupportedQueueFamily)? }; - preferred_formats + let supported_formats = candidate_formats .iter() .cloned() - .find(|preferred| caps.formats.contains(preferred)) + .filter(|candidate| caps.formats.contains(candidate)) + .collect::>(); + + // Error if no formats supported + supported_formats + .is_empty() + .then(|| supported_formats) .ok_or(GetSurfacePreferredFormatError::NotFound) } } diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 63c91fd80e..36b6a65689 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -198,7 +198,11 @@ fn start( let spawner = Spawner::new(); let mut config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: surface.get_preferred_format(&adapter).unwrap(), + format: surface + .get_supported_formats(&adapter) + .unwrap() + .pop() + .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 fe6f0c2132..41b6dc6c55 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -46,7 +46,11 @@ async fn run(event_loop: EventLoop<()>, window: Window) { push_constant_ranges: &[], }); - let swapchain_format = surface.get_preferred_format(&adapter).unwrap(); + let swapchain_format = surface + .get_supported_formats(&adapter) + .unwrap() + .pop() + .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 ca725f44b3..692551cd35 100644 --- a/wgpu/examples/hello-windows/main.rs +++ b/wgpu/examples/hello-windows/main.rs @@ -33,7 +33,12 @@ impl ViewportDesc { let config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: self.surface.get_preferred_format(adapter).unwrap(), + format: self + .surface + .get_supported_formats(adapter) + .unwrap() + .pop() + .unwrap(), width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 58acf88c3b..f6bb271126 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -932,17 +932,17 @@ impl crate::Context for Context { } } - fn surface_get_preferred_format( + fn surface_get_supported_formats( &self, surface: &Self::SurfaceId, adapter: &Self::AdapterId, - ) -> Option { + ) -> Option> { let global = &self.0; - match wgc::gfx_select!(adapter => global.surface_get_preferred_format(surface.id, *adapter)) + match wgc::gfx_select!(adapter => global.surface_get_supported_formats(surface.id, *adapter)) { - Ok(format) => Some(format), + Ok(formats) => Some(formats), Err(wgc::instance::GetSurfacePreferredFormatError::UnsupportedQueueFamily) => None, - Err(err) => self.handle_error_fatal(err, "Surface::get_preferred_format"), + Err(err) => self.handle_error_fatal(err, "Surface::get_supported_formats"), } } diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index af496942c8..f066c42246 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1188,12 +1188,12 @@ impl crate::Context for Context { format.describe().guaranteed_format_features } - fn surface_get_preferred_format( + fn surface_get_supported_formats( &self, surface: &Self::SurfaceId, adapter: &Self::AdapterId, - ) -> Option { - let format = map_texture_format_from_web_sys(surface.0.get_preferred_format(&adapter.0)); + ) -> Option> { + let format = map_texture_format_from_web_sys(surface.0.get_supported_formats(&adapter.0)); Some(format) } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 38ae0fab0f..a7d2a9d604 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -225,11 +225,11 @@ trait Context: Debug + Send + Sized + Sync { format: TextureFormat, ) -> TextureFormatFeatures; - fn surface_get_preferred_format( + fn surface_get_supported_formats( &self, surface: &Self::SurfaceId, adapter: &Self::AdapterId, - ) -> Option; + ) -> Option>; fn surface_configure( &self, surface: &Self::SurfaceId, @@ -3438,8 +3438,8 @@ impl Surface { /// Returns an optimal texture format to use for the [`Surface`] with this adapter. /// /// Returns None if the surface is incompatible with the adapter. - pub fn get_preferred_format(&self, adapter: &Adapter) -> Option { - Context::surface_get_preferred_format(&*self.context, &self.id, &adapter.id) + pub fn get_supported_formats(&self, adapter: &Adapter) -> Option> { + Context::surface_get_supported_formats(&*self.context, &self.id, &adapter.id) } /// Initializes [`Surface`] for presentation. From ac9eb3e6d5b3767e325f588c11dd6226544aabbe Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 17:51:52 -0700 Subject: [PATCH 02/10] Return the set of supported TextureFormat specified by WebGPU --- wgpu/src/backend/web.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index f066c42246..c89f63da1f 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -553,7 +553,7 @@ fn map_texture_format(texture_format: wgt::TextureFormat) -> web_sys::GpuTexture } } -fn map_texture_format_from_web_sys( +fn _map_texture_format_from_web_sys( texture_format: web_sys::GpuTextureFormat, ) -> wgt::TextureFormat { use web_sys::GpuTextureFormat as tf; @@ -1190,11 +1190,16 @@ impl crate::Context for Context { fn surface_get_supported_formats( &self, - surface: &Self::SurfaceId, - adapter: &Self::AdapterId, + _surface: &Self::SurfaceId, + _adapter: &Self::AdapterId, ) -> Option> { - let format = map_texture_format_from_web_sys(surface.0.get_supported_formats(&adapter.0)); - Some(format) + // https://gpuweb.github.io/gpuweb/#supported-context-formats + let formats = vec![ + wgt::TextureFormat::Bgra8Unorm, + wgt::TextureFormat::Rgba8Unorm, + wgt::TextureFormat::Rgba16Float, + ]; + Some(formats) } fn surface_configure( From 39fa07e1b170c1121af4867ba681dc4db6418415 Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 20:19:11 -0700 Subject: [PATCH 03/10] Remove redundant filtering and use list directly --- wgpu-core/src/instance.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index cd2f636c40..0344b70d71 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -177,16 +177,9 @@ impl Surface { .ok_or(GetSurfacePreferredFormatError::UnsupportedQueueFamily)? }; - let supported_formats = candidate_formats - .iter() - .cloned() - .filter(|candidate| caps.formats.contains(candidate)) - .collect::>(); - - // Error if no formats supported - supported_formats + caps.formats .is_empty() - .then(|| supported_formats) + .then(|| caps.formats) .ok_or(GetSurfacePreferredFormatError::NotFound) } } From 5ae5489b1075af6f0867e7f3b741cfd7ec386ac4 Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 20:21:48 -0700 Subject: [PATCH 04/10] Replace pops with first --- wgpu/examples/framework.rs | 2 +- wgpu/examples/hello-triangle/main.rs | 2 +- wgpu/examples/hello-windows/main.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 36b6a65689..fd67dfd529 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -201,7 +201,7 @@ fn start( format: surface .get_supported_formats(&adapter) .unwrap() - .pop() + .first() .unwrap(), width: size.width, height: size.height, diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index 41b6dc6c55..52cc4600af 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -49,7 +49,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) { let swapchain_format = surface .get_supported_formats(&adapter) .unwrap() - .pop() + .first() .unwrap(); let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { diff --git a/wgpu/examples/hello-windows/main.rs b/wgpu/examples/hello-windows/main.rs index 692551cd35..6add8b7962 100644 --- a/wgpu/examples/hello-windows/main.rs +++ b/wgpu/examples/hello-windows/main.rs @@ -37,7 +37,7 @@ impl ViewportDesc { .surface .get_supported_formats(adapter) .unwrap() - .pop() + .first() .unwrap(), width: size.width, height: size.height, From 89581a79822e21c127a9132d988cf70b530703f4 Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 20:22:12 -0700 Subject: [PATCH 05/10] Remove now unused function --- wgpu/src/backend/web.rs | 50 ----------------------------------------- 1 file changed, 50 deletions(-) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index c89f63da1f..7a2e99277a 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -553,56 +553,6 @@ fn map_texture_format(texture_format: wgt::TextureFormat) -> web_sys::GpuTexture } } -fn _map_texture_format_from_web_sys( - texture_format: web_sys::GpuTextureFormat, -) -> wgt::TextureFormat { - use web_sys::GpuTextureFormat as tf; - use wgt::TextureFormat; - match texture_format { - tf::R8unorm => TextureFormat::R8Unorm, - tf::R8snorm => TextureFormat::R8Snorm, - tf::R8uint => TextureFormat::R8Uint, - tf::R8sint => TextureFormat::R8Sint, - tf::R16uint => TextureFormat::R16Uint, - tf::R16sint => TextureFormat::R16Sint, - tf::R16float => TextureFormat::R16Float, - tf::Rg8unorm => TextureFormat::Rg8Unorm, - tf::Rg8snorm => TextureFormat::Rg8Snorm, - tf::Rg8uint => TextureFormat::Rg8Uint, - tf::Rg8sint => TextureFormat::Rg8Sint, - tf::R32uint => TextureFormat::R32Uint, - tf::R32sint => TextureFormat::R32Sint, - tf::R32float => TextureFormat::R32Float, - tf::Rg16uint => TextureFormat::Rg16Uint, - tf::Rg16sint => TextureFormat::Rg16Sint, - tf::Rg16float => TextureFormat::Rg16Float, - tf::Rgba8unorm => TextureFormat::Rgba8Unorm, - tf::Rgba8unormSrgb => TextureFormat::Rgba8UnormSrgb, - tf::Rgba8snorm => TextureFormat::Rgba8Snorm, - tf::Rgba8uint => TextureFormat::Rgba8Uint, - tf::Rgba8sint => TextureFormat::Rgba8Sint, - tf::Bgra8unorm => TextureFormat::Bgra8Unorm, - tf::Bgra8unormSrgb => TextureFormat::Bgra8UnormSrgb, - tf::Rgb10a2unorm => TextureFormat::Rgb10a2Unorm, - tf::Rg11b10ufloat => TextureFormat::Rg11b10Float, - tf::Rg32uint => TextureFormat::Rg32Uint, - tf::Rg32sint => TextureFormat::Rg32Sint, - tf::Rg32float => TextureFormat::Rg32Float, - tf::Rgba16uint => TextureFormat::Rgba16Uint, - tf::Rgba16sint => TextureFormat::Rgba16Sint, - tf::Rgba16float => TextureFormat::Rgba16Float, - tf::Rgba32uint => TextureFormat::Rgba32Uint, - tf::Rgba32sint => TextureFormat::Rgba32Sint, - tf::Rgba32float => TextureFormat::Rgba32Float, - tf::Depth32float => TextureFormat::Depth32Float, - tf::Depth32floatStencil8 => TextureFormat::Depth32FloatStencil8, - tf::Depth24plus => TextureFormat::Depth24Plus, - tf::Depth24plusStencil8 => TextureFormat::Depth24PlusStencil8, - tf::Depth24unormStencil8 => TextureFormat::Depth24UnormStencil8, - _ => unimplemented!(), - } -} - fn map_texture_component_type( sample_type: wgt::TextureSampleType, ) -> web_sys::GpuTextureSampleType { From c598346594587c040e913b096b931c43844617e6 Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 20:23:28 -0700 Subject: [PATCH 06/10] Fix doc and clarify preffered format --- wgpu/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index a7d2a9d604..8eb5afe11e 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -3435,7 +3435,8 @@ impl Drop for SurfaceTexture { } impl Surface { - /// Returns an optimal texture format to use for the [`Surface`] with this adapter. + /// 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> { From bb0dce2af5126865bdef5ebc14755762ff91e9b2 Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 20:30:08 -0700 Subject: [PATCH 07/10] Dereference enums --- wgpu/examples/framework.rs | 2 +- wgpu/examples/hello-triangle/main.rs | 2 +- wgpu/examples/hello-windows/main.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index fd67dfd529..35a398594c 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -198,7 +198,7 @@ fn start( let spawner = Spawner::new(); let mut config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: surface + format: *surface .get_supported_formats(&adapter) .unwrap() .first() diff --git a/wgpu/examples/hello-triangle/main.rs b/wgpu/examples/hello-triangle/main.rs index 52cc4600af..c1d292d41c 100644 --- a/wgpu/examples/hello-triangle/main.rs +++ b/wgpu/examples/hello-triangle/main.rs @@ -46,7 +46,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) { push_constant_ranges: &[], }); - let swapchain_format = surface + let swapchain_format = *surface .get_supported_formats(&adapter) .unwrap() .first() diff --git a/wgpu/examples/hello-windows/main.rs b/wgpu/examples/hello-windows/main.rs index 6add8b7962..7ead1cbc8e 100644 --- a/wgpu/examples/hello-windows/main.rs +++ b/wgpu/examples/hello-windows/main.rs @@ -33,7 +33,7 @@ impl ViewportDesc { let config = wgpu::SurfaceConfiguration { usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: self + format: *self .surface .get_supported_formats(adapter) .unwrap() From 33f86cf79dcc68b35370194cc6da03c4403a7b1f Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 20:33:08 -0700 Subject: [PATCH 08/10] Remove unused list --- wgpu-core/src/instance.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 0344b70d71..c3b063c7aa 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -156,17 +156,6 @@ impl Surface { &self, adapter: &Adapter, ) -> Result, GetSurfacePreferredFormatError> { - // Check the four formats mentioned in the WebGPU spec. - // Also, prefer sRGB over linear as it is better in - // representing perceived colors. - let candidate_formats = [ - wgt::TextureFormat::Bgra8UnormSrgb, - wgt::TextureFormat::Rgba8UnormSrgb, - wgt::TextureFormat::Bgra8Unorm, - wgt::TextureFormat::Rgba8Unorm, - wgt::TextureFormat::Rgba16Float, - ]; - let suf = A::get_surface(self); let caps = unsafe { profiling::scope!("surface_capabilities"); From 627a43a34dd6c81374b3b5e39e62cf1fbfd91c41 Mon Sep 17 00:00:00 2001 From: stevenhuyn Date: Fri, 17 Jun 2022 20:58:54 -0700 Subject: [PATCH 09/10] Remove fancy coode --- wgpu-core/src/instance.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index c3b063c7aa..39fc65343d 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -166,10 +166,11 @@ impl Surface { .ok_or(GetSurfacePreferredFormatError::UnsupportedQueueFamily)? }; - caps.formats - .is_empty() - .then(|| caps.formats) - .ok_or(GetSurfacePreferredFormatError::NotFound) + if caps.formats.is_empty() { + return Err(GetSurfacePreferredFormatError::NotFound); + } + + Ok(caps.formats) } } From 517e5fddeb9dac02f27da494d1f6bd7c1375b995 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 17 Jun 2022 21:00:17 -0700 Subject: [PATCH 10/10] Update wgpu-core/src/device/mod.rs Co-authored-by: Connor Fitzgerald --- wgpu-core/src/device/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index e0668a6228..35a26d4a14 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3018,7 +3018,7 @@ impl Global { surface_id: id::SurfaceId, adapter_id: id::AdapterId, ) -> Result, instance::GetSurfacePreferredFormatError> { - profiling::scope!("surface_get_supported_formats"); + profiling::scope!("Surface::get_supported_formats"); let hub = A::hub(self); let mut token = Token::root();