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

Change get_preferred_format to get_supported_formats #2783

Merged
merged 10 commits into from
Jun 18, 2022
8 changes: 4 additions & 4 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3013,12 +3013,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.map_err(|_| instance::IsSurfaceSupportedError::InvalidSurface)?;
Ok(adapter.is_surface_supported(surface))
}
pub fn surface_get_preferred_format<A: HalApi>(
pub fn surface_get_supported_formats<A: HalApi>(
&self,
surface_id: id::SurfaceId,
adapter_id: id::AdapterId,
) -> Result<TextureFormat, instance::GetSurfacePreferredFormatError> {
profiling::scope!("surface_get_preferred_format");
) -> Result<Vec<TextureFormat>, instance::GetSurfacePreferredFormatError> {
profiling::scope!("surface_get_supported_formats");
stevenhuyn marked this conversation as resolved.
Show resolved Hide resolved
let hub = A::hub(self);
let mut token = Token::root();

Expand All @@ -3031,7 +3031,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.get(surface_id)
.map_err(|_| instance::GetSurfacePreferredFormatError::InvalidSurface)?;

surface.get_preferred_format(adapter)
surface.get_supported_formats(adapter)
}

pub fn device_features<A: HalApi>(
Expand Down
16 changes: 11 additions & 5 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ impl crate::hub::Resource for Surface {
}

impl Surface {
pub fn get_preferred_format<A: HalApi>(
pub fn get_supported_formats<A: HalApi>(
&self,
adapter: &Adapter<A>,
) -> Result<wgt::TextureFormat, GetSurfacePreferredFormatError> {
) -> Result<Vec<wgt::TextureFormat>, 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,
Expand All @@ -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))
stevenhuyn marked this conversation as resolved.
Show resolved Hide resolved
.collect::<Vec<wgt::TextureFormat>>();

// Error if no formats supported
supported_formats
.is_empty()
stevenhuyn marked this conversation as resolved.
Show resolved Hide resolved
.then(|| supported_formats)
.ok_or(GetSurfacePreferredFormatError::NotFound)
}
}
Expand Down
6 changes: 5 additions & 1 deletion wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ fn start<E: Example>(
let spawner = Spawner::new();
let mut config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: surface.get_preferred_format(&adapter).unwrap(),
format: surface
stevenhuyn marked this conversation as resolved.
Show resolved Hide resolved
.get_supported_formats(&adapter)
.unwrap()
.pop()
.unwrap(),
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Mailbox,
Expand Down
6 changes: 5 additions & 1 deletion wgpu/examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion wgpu/examples/hello-windows/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextureFormat> {
) -> Option<Vec<TextureFormat>> {
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"),
}
}

Expand Down
19 changes: 12 additions & 7 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
stevenhuyn marked this conversation as resolved.
Show resolved Hide resolved
texture_format: web_sys::GpuTextureFormat,
) -> wgt::TextureFormat {
use web_sys::GpuTextureFormat as tf;
Expand Down Expand Up @@ -1188,13 +1188,18 @@ 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<wgt::TextureFormat> {
let format = map_texture_format_from_web_sys(surface.0.get_preferred_format(&adapter.0));
Some(format)
_surface: &Self::SurfaceId,
_adapter: &Self::AdapterId,
) -> Option<Vec<wgt::TextureFormat>> {
// 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(
Expand Down
8 changes: 4 additions & 4 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextureFormat>;
) -> Option<Vec<TextureFormat>>;
fn surface_configure(
&self,
surface: &Self::SurfaceId,
Expand Down Expand Up @@ -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.
stevenhuyn marked this conversation as resolved.
Show resolved Hide resolved
pub fn get_preferred_format(&self, adapter: &Adapter) -> Option<TextureFormat> {
Context::surface_get_preferred_format(&*self.context, &self.id, &adapter.id)
pub fn get_supported_formats(&self, adapter: &Adapter) -> Option<Vec<TextureFormat>> {
Context::surface_get_supported_formats(&*self.context, &self.id, &adapter.id)
}

/// Initializes [`Surface`] for presentation.
Expand Down