get_supported_formats: sort like the old get_preferred_format and simplify return type (#2786)

* get_supported_formats: sort srgb first like the old get_preferred_format

* get_supported_formats: simplify return type
This commit is contained in:
victorvde 2022-06-22 23:04:18 +02:00 committed by GitHub
parent 5dcd19c167
commit 7375acb230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 15 additions and 32 deletions

View File

@ -157,7 +157,7 @@ impl Surface {
adapter: &Adapter<A>,
) -> Result<Vec<wgt::TextureFormat>, GetSurfacePreferredFormatError> {
let suf = A::get_surface(self);
let caps = unsafe {
let mut caps = unsafe {
profiling::scope!("surface_capabilities");
adapter
.raw
@ -166,9 +166,8 @@ impl Surface {
.ok_or(GetSurfacePreferredFormatError::UnsupportedQueueFamily)?
};
if caps.formats.is_empty() {
return Err(GetSurfacePreferredFormatError::NotFound);
}
// TODO: maybe remove once we support texture view changing srgb-ness
caps.formats.sort_by_key(|f| !f.describe().srgb);
Ok(caps.formats)
}
@ -343,8 +342,6 @@ pub enum IsSurfaceSupportedError {
#[derive(Clone, Debug, Error)]
pub enum GetSurfacePreferredFormatError {
#[error("no suitable format found")]
NotFound,
#[error("invalid adapter")]
InvalidAdapter,
#[error("invalid surface")]

View File

@ -269,11 +269,7 @@ fn start<E: Example>(
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_supported_formats(&adapter)[0],
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,

View File

@ -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_supported_formats(&adapter)[0];
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,

View File

@ -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_supported_formats(adapter)[0],
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,

View File

@ -962,12 +962,12 @@ impl crate::Context for Context {
&self,
surface: &Self::SurfaceId,
adapter: &Self::AdapterId,
) -> Option<Vec<TextureFormat>> {
) -> Vec<TextureFormat> {
let global = &self.0;
match wgc::gfx_select!(adapter => global.surface_get_supported_formats(surface.id, *adapter))
{
Ok(formats) => Some(formats),
Err(wgc::instance::GetSurfacePreferredFormatError::UnsupportedQueueFamily) => None,
Ok(formats) => formats,
Err(wgc::instance::GetSurfacePreferredFormatError::UnsupportedQueueFamily) => vec![],
Err(err) => self.handle_error_fatal(err, "Surface::get_supported_formats"),
}
}

View File

@ -1142,14 +1142,13 @@ impl crate::Context for Context {
&self,
_surface: &Self::SurfaceId,
_adapter: &Self::AdapterId,
) -> Option<Vec<wgt::TextureFormat>> {
) -> Vec<wgt::TextureFormat> {
// https://gpuweb.github.io/gpuweb/#supported-context-formats
let formats = vec![
vec![
wgt::TextureFormat::Bgra8Unorm,
wgt::TextureFormat::Rgba8Unorm,
wgt::TextureFormat::Rgba16Float,
];
Some(formats)
]
}
fn surface_configure(

View File

@ -229,7 +229,7 @@ trait Context: Debug + Send + Sized + Sync {
&self,
surface: &Self::SurfaceId,
adapter: &Self::AdapterId,
) -> Option<Vec<TextureFormat>>;
) -> Vec<TextureFormat>;
fn surface_configure(
&self,
surface: &Self::SurfaceId,
@ -3449,8 +3449,8 @@ 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<Vec<TextureFormat>> {
/// Returns an empty vector if the surface is incompatible with the adapter.
pub fn get_supported_formats(&self, adapter: &Adapter) -> Vec<TextureFormat> {
Context::surface_get_supported_formats(&*self.context, &self.id, &adapter.id)
}