Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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")]
Expand Down
6 changes: 1 addition & 5 deletions wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::Mailbox,
Expand Down
6 changes: 1 addition & 5 deletions wgpu/examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 1 addition & 6 deletions wgpu/examples/hello-windows/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
Expand Down
7 changes: 3 additions & 4 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down