Skip to content

Commit

Permalink
[rs] Merge gfx-rs#693
Browse files Browse the repository at this point in the history
693: Expose get_swap_chain_preferred_format r=kvark a=manugildev

Fixes gfx-rs#691

This functionality was committed in gfx-rs/wgpu@6f1d614 but never exposed to `wgpu-rs`.

Problem: Examples made use of `wgpu::TextureFormat::Bgra8UnormSrgb` but that's unreliable and according to the [API](https://gpuweb.github.io/gpuweb/#dom-gpucanvascontext-getswapchainpreferredformat) this method is the way of properly obtaining an optimal texture format for the `SwapChain` based on the platform.

- [x]  Expose `get_swap_chain_preferred_format()` on Device
- [x]  Use `TextureFormat::Bgra8Unorm` for [web.rs](https://github.com/gfx-rs/wgpu-rs/blob/e00ef5d5842a1fcfb46bb531789b1c340bdee3ab/src/backend/web.rs#L970)
- [x]  Update [framework.rs](https://github.com/gfx-rs/wgpu-rs/blob/e00ef5d5842a1fcfb46bb531789b1c340bdee3ab/examples/framework.rs#L227) and examples to call this new method

Co-authored-by: Manuel Gil <manugildev@gmail.com>
  • Loading branch information
bors[bot] and manugildev authored Jan 4, 2021
2 parents ee3ca89 + 81bcc72 commit 7f0b8f3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
7 changes: 1 addition & 6 deletions wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,7 @@ fn start<E: Example>(

let mut sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
// TODO: Allow srgb unconditionally
format: if cfg!(target_arch = "wasm32") {
wgpu::TextureFormat::Bgra8Unorm
} else {
wgpu::TextureFormat::Bgra8UnormSrgb
},
format: device.get_swap_chain_preferred_format(),
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Mailbox,
Expand Down
8 changes: 5 additions & 3 deletions wgpu/examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use winit::{
window::Window,
};

async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::TextureFormat) {
async fn run(event_loop: EventLoop<()>, window: Window) {
let size = window.inner_size();
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let surface = unsafe { instance.create_surface(&window) };
Expand Down Expand Up @@ -44,6 +44,8 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
push_constant_ranges: &[],
});

let swapchain_format = device.get_swap_chain_preferred_format();

let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: None,
layout: Some(&pipeline_layout),
Expand Down Expand Up @@ -138,7 +140,7 @@ fn main() {
{
subscriber::initialize_default_subscriber(None);
// Temporarily avoid srgb formats for the swapchain on the web
futures::executor::block_on(run(event_loop, window, wgpu::TextureFormat::Bgra8UnormSrgb));
futures::executor::block_on(run(event_loop, window));
}
#[cfg(target_arch = "wasm32")]
{
Expand All @@ -154,6 +156,6 @@ fn main() {
.ok()
})
.expect("couldn't append canvas to document body");
wasm_bindgen_futures::spawn_local(run(event_loop, window, wgpu::TextureFormat::Bgra8Unorm));
wasm_bindgen_futures::spawn_local(run(event_loop, window));
}
}
18 changes: 5 additions & 13 deletions wgpu/examples/hello-windows/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ impl ViewportDesc {
}
}

fn build(self, device: &wgpu::Device, swapchain_format: wgpu::TextureFormat) -> Viewport {
fn build(self, device: &wgpu::Device) -> Viewport {
let size = self.window.inner_size();

let sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
format: swapchain_format,
format: device.get_swap_chain_preferred_format(),
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
Expand Down Expand Up @@ -62,11 +62,7 @@ impl Viewport {
}
}

async fn run(
event_loop: EventLoop<()>,
viewports: Vec<(Window, wgpu::Color)>,
swapchain_format: wgpu::TextureFormat,
) {
async fn run(event_loop: EventLoop<()>, viewports: Vec<(Window, wgpu::Color)>) {
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let viewports: Vec<_> = viewports
.into_iter()
Expand Down Expand Up @@ -96,7 +92,7 @@ async fn run(

let mut viewports: HashMap<WindowId, Viewport> = viewports
.into_iter()
.map(|desc| (desc.window.id(), desc.build(&device, swapchain_format)))
.map(|desc| (desc.window.id(), desc.build(&device)))
.collect();

event_loop.run(move |event, _, control_flow| {
Expand Down Expand Up @@ -195,11 +191,7 @@ fn main() {

subscriber::initialize_default_subscriber(None);
// Temporarily avoid srgb formats for the swapchain on the web
futures::executor::block_on(run(
event_loop,
viewports,
wgpu::TextureFormat::Bgra8UnormSrgb,
));
futures::executor::block_on(run(event_loop, viewports));
}
#[cfg(target_arch = "wasm32")]
{
Expand Down
11 changes: 10 additions & 1 deletion wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
CommandEncoderDescriptor, ComputePassDescriptor, ComputePipelineDescriptor, Features, Label,
Limits, LoadOp, MapMode, Operations, PipelineLayoutDescriptor, RenderBundleEncoderDescriptor,
RenderPipelineDescriptor, SamplerDescriptor, ShaderModuleDescriptor, ShaderSource,
SwapChainStatus, TextureDescriptor, TextureViewDescriptor,
SwapChainStatus, TextureDescriptor, TextureFormat, TextureViewDescriptor,
};

use arrayvec::ArrayVec;
Expand Down Expand Up @@ -707,6 +707,15 @@ impl crate::Context for Context {
}
}

fn device_get_swap_chain_preferred_format(&self, device: &Self::DeviceId) -> TextureFormat {
let global = &self.0;
match wgc::gfx_select!(device.id => global.device_get_swap_chain_preferred_format(device.id))
{
Ok(swap_chain_preferred_format) => swap_chain_preferred_format,
Err(err) => self.handle_error_fatal(err, "Device::get_swap_chain_preferred_format"),
}
}

fn device_create_swap_chain(
&self,
device: &Self::DeviceId,
Expand Down
8 changes: 8 additions & 0 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,14 @@ impl crate::Context for Context {
wgt::Limits::default()
}

fn device_get_swap_chain_preferred_format(
&self,
device: &Self::DeviceId,
) -> wgt::TextureFormat {
// TODO: web-sys bindings need to be updated to not return a promise
wgt::TextureFormat::Bgra8Unorm
}

fn device_create_swap_chain(
&self,
device: &Self::DeviceId,
Expand Down
6 changes: 6 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ trait Context: Debug + Send + Sized + Sync {

fn device_features(&self, device: &Self::DeviceId) -> Features;
fn device_limits(&self, device: &Self::DeviceId) -> Limits;
fn device_get_swap_chain_preferred_format(&self, device: &Self::DeviceId) -> TextureFormat;
fn device_create_swap_chain(
&self,
device: &Self::DeviceId,
Expand Down Expand Up @@ -1428,6 +1429,11 @@ impl Device {
Context::device_limits(&*self.context, &self.id)
}

/// Returns an optimal texture format to use for the [`SwapChain`] with this device.
pub fn get_swap_chain_preferred_format(&self) -> TextureFormat {
Context::device_get_swap_chain_preferred_format(&*self.context, &self.id)
}

/// Creates a shader module from either SPIR-V or WGSL source code.
pub fn create_shader_module(&self, desc: &ShaderModuleDescriptor) -> ShaderModule {
ShaderModule {
Expand Down

0 comments on commit 7f0b8f3

Please sign in to comment.