Skip to content

Commit ea05b44

Browse files
cwfitzgeraldbjorn3
andauthored
Improve Presentation Api (#2803)
* Add new presentation modes * Expand presentation formats * Add Surface::get_supported_modes * Update to rust 2021 and improve some metal surface code * Update wgpu-types/src/lib.rs Co-authored-by: bjorn3 <[email protected]> * Fix windows build errors * Fix issues with ALLOW_TEARING Co-authored-by: bjorn3 <[email protected]>
1 parent 98597da commit ea05b44

File tree

26 files changed

+277
-80
lines changed

26 files changed

+277
-80
lines changed

cts_runner/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = [
55
"Luca Casonato <[email protected]>",
66
]
7-
edition = "2018"
7+
edition = "2021"
88
description = "CTS runner for wgpu"
99
license = "MIT OR Apache-2.0"
1010
publish = false

deno_webgpu/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
name = "deno_webgpu"
55
version = "0.54.0"
66
authors = ["the Deno authors"]
7-
edition = "2018"
7+
edition = "2021"
88
license = "MIT"
99
readme = "README.md"
1010
repository = "https://github.com/gfx-rs/wgpu"

dummy/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = [
55
"Dzmitry Malyshau <[email protected]>",
66
]
7-
edition = "2018"
7+
edition = "2021"
88
license = "MIT OR Apache-2.0"
99
publish = false
1010

player/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = [
55
"Dzmitry Malyshau <[email protected]>",
66
]
7-
edition = "2018"
7+
edition = "2021"
88
description = "WebGPU trace player"
99
homepage = "https://github.com/gfx-rs/wgpu"
1010
repository = "https://github.com/gfx-rs/wgpu"

run-wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "run-wasm"
33
version = "0.1.0"
4-
edition = "2018"
4+
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

wgpu-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "wgpu-core"
33
version = "0.12.0"
44
authors = ["wgpu developers"]
5-
edition = "2018"
5+
edition = "2021"
66
description = "WebGPU core logic on wgpu-hal"
77
homepage = "https://github.com/gfx-rs/wgpu"
88
repository = "https://github.com/gfx-rs/wgpu"

wgpu-core/src/device/mod.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3134,7 +3134,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
31343134
&self,
31353135
surface_id: id::SurfaceId,
31363136
adapter_id: id::AdapterId,
3137-
) -> Result<Vec<TextureFormat>, instance::GetSurfacePreferredFormatError> {
3137+
) -> Result<Vec<TextureFormat>, instance::GetSurfaceSupportError> {
31383138
profiling::scope!("Surface::get_supported_formats");
31393139
let hub = A::hub(self);
31403140
let mut token = Token::root();
@@ -3143,13 +3143,33 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
31433143
let (adapter_guard, mut _token) = hub.adapters.read(&mut token);
31443144
let adapter = adapter_guard
31453145
.get(adapter_id)
3146-
.map_err(|_| instance::GetSurfacePreferredFormatError::InvalidAdapter)?;
3146+
.map_err(|_| instance::GetSurfaceSupportError::InvalidAdapter)?;
31473147
let surface = surface_guard
31483148
.get(surface_id)
3149-
.map_err(|_| instance::GetSurfacePreferredFormatError::InvalidSurface)?;
3149+
.map_err(|_| instance::GetSurfaceSupportError::InvalidSurface)?;
31503150

31513151
surface.get_supported_formats(adapter)
31523152
}
3153+
pub fn surface_get_supported_modes<A: HalApi>(
3154+
&self,
3155+
surface_id: id::SurfaceId,
3156+
adapter_id: id::AdapterId,
3157+
) -> Result<Vec<wgt::PresentMode>, instance::GetSurfaceSupportError> {
3158+
profiling::scope!("Surface::get_supported_modes");
3159+
let hub = A::hub(self);
3160+
let mut token = Token::root();
3161+
3162+
let (surface_guard, mut token) = self.surfaces.read(&mut token);
3163+
let (adapter_guard, mut _token) = hub.adapters.read(&mut token);
3164+
let adapter = adapter_guard
3165+
.get(adapter_id)
3166+
.map_err(|_| instance::GetSurfaceSupportError::InvalidAdapter)?;
3167+
let surface = surface_guard
3168+
.get(surface_id)
3169+
.map_err(|_| instance::GetSurfaceSupportError::InvalidSurface)?;
3170+
3171+
surface.get_supported_modes(adapter)
3172+
}
31533173

31543174
pub fn device_features<A: HalApi>(
31553175
&self,
@@ -4955,11 +4975,40 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
49554975
);
49564976
}
49574977
if !caps.present_modes.contains(&config.present_mode) {
4958-
log::warn!(
4959-
"Surface does not support present mode: {:?}, falling back to FIFO",
4960-
config.present_mode,
4978+
let new_mode = loop {
4979+
// Automatic present mode checks.
4980+
//
4981+
// The "Automatic" modes are never supported by the backends.
4982+
match config.present_mode {
4983+
wgt::PresentMode::AutoVsync => {
4984+
if caps.present_modes.contains(&wgt::PresentMode::FifoRelaxed) {
4985+
break wgt::PresentMode::FifoRelaxed;
4986+
}
4987+
if caps.present_modes.contains(&wgt::PresentMode::Fifo) {
4988+
break wgt::PresentMode::Fifo;
4989+
}
4990+
}
4991+
wgt::PresentMode::AutoNoVsync => {
4992+
if caps.present_modes.contains(&wgt::PresentMode::Immediate) {
4993+
break wgt::PresentMode::Immediate;
4994+
}
4995+
if caps.present_modes.contains(&wgt::PresentMode::Mailbox) {
4996+
break wgt::PresentMode::Mailbox;
4997+
}
4998+
}
4999+
_ => {}
5000+
}
5001+
return Err(E::UnsupportedPresentMode {
5002+
requested: config.present_mode,
5003+
available: caps.present_modes.clone(),
5004+
});
5005+
};
5006+
5007+
log::info!(
5008+
"Automatically choosing presentation mode by rule {:?}. Chose {new_mode:?}",
5009+
config.present_mode
49615010
);
4962-
config.present_mode = wgt::PresentMode::Fifo;
5011+
config.present_mode = new_mode;
49635012
}
49645013
if !caps.formats.contains(&config.format) {
49655014
return Err(E::UnsupportedFormat {

wgpu-core/src/instance.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,39 @@ impl Surface {
155155
pub fn get_supported_formats<A: HalApi>(
156156
&self,
157157
adapter: &Adapter<A>,
158-
) -> Result<Vec<wgt::TextureFormat>, GetSurfacePreferredFormatError> {
158+
) -> Result<Vec<wgt::TextureFormat>, GetSurfaceSupportError> {
159159
let suf = A::get_surface(self);
160160
let mut caps = unsafe {
161161
profiling::scope!("surface_capabilities");
162162
adapter
163163
.raw
164164
.adapter
165165
.surface_capabilities(&suf.raw)
166-
.ok_or(GetSurfacePreferredFormatError::UnsupportedQueueFamily)?
166+
.ok_or(GetSurfaceSupportError::UnsupportedQueueFamily)?
167167
};
168168

169169
// TODO: maybe remove once we support texture view changing srgb-ness
170170
caps.formats.sort_by_key(|f| !f.describe().srgb);
171171

172172
Ok(caps.formats)
173173
}
174+
175+
pub fn get_supported_modes<A: HalApi>(
176+
&self,
177+
adapter: &Adapter<A>,
178+
) -> Result<Vec<wgt::PresentMode>, GetSurfaceSupportError> {
179+
let suf = A::get_surface(self);
180+
let caps = unsafe {
181+
profiling::scope!("surface_capabilities");
182+
adapter
183+
.raw
184+
.adapter
185+
.surface_capabilities(&suf.raw)
186+
.ok_or(GetSurfaceSupportError::UnsupportedQueueFamily)?
187+
};
188+
189+
Ok(caps.present_modes)
190+
}
174191
}
175192

176193
pub struct Adapter<A: hal::Api> {
@@ -341,7 +358,7 @@ pub enum IsSurfaceSupportedError {
341358
}
342359

343360
#[derive(Clone, Debug, Error)]
344-
pub enum GetSurfacePreferredFormatError {
361+
pub enum GetSurfaceSupportError {
345362
#[error("invalid adapter")]
346363
InvalidAdapter,
347364
#[error("invalid surface")]

wgpu-core/src/present.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ pub enum ConfigureSurfaceError {
7575
requested: wgt::TextureFormat,
7676
available: Vec<wgt::TextureFormat>,
7777
},
78+
#[error("requested present mode {requested:?} is not in the list of supported present modes: {available:?}")]
79+
UnsupportedPresentMode {
80+
requested: wgt::PresentMode,
81+
available: Vec<wgt::PresentMode>,
82+
},
7883
#[error("requested usage is not supported")]
7984
UnsupportedUsage,
8085
}

wgpu-hal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "wgpu-hal"
33
version = "0.12.0"
44
authors = ["wgpu developers"]
5-
edition = "2018"
5+
edition = "2021"
66
description = "WebGPU hardware abstraction layer"
77
homepage = "https://github.com/gfx-rs/wgpu"
88
repository = "https://github.com/gfx-rs/wgpu"

0 commit comments

Comments
 (0)