Skip to content

Commit

Permalink
Add back get_preferred_format and sRGB preference
Browse files Browse the repository at this point in the history
  • Loading branch information
victorvde committed Jun 18, 2022
1 parent 0d4d3f4 commit 7a034a1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
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_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> {
// 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

0 comments on commit 7a034a1

Please sign in to comment.