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

Add back get_preferred_format and sRGB preference #2785

Closed
wants to merge 1 commit into from
Closed
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sold on needing a separate api here - especially if we remove the outer option.

.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,
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_preferred_format(&adapter).unwrap();

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_preferred_format(adapter).unwrap(),
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
Expand Down
21 changes: 20 additions & 1 deletion wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<TextureFormat>> {
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<wgt::TextureFormat> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this separately, both methods of "preferred" format should return the same value. How about inside of get_supported_formats we just bring the srgb formats to the front - caps.format.sort_by_key(|f| !f.describe().srgb . Once we support texture view changing srgb-ness we can remove the sort.

// 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
Expand Down