Skip to content

Commit cc203a6

Browse files
authored
web: use get_preferred_canvas_format() to fill SurfaceCapabilities (#3744)
* web: use `get_preferred_canvas_format()` to fill `formats` of `SurfaceCapabilities` * Only keep preferred format as the first element * Update CHANGELOG * Find preferred format and move it to the first position in the formats. * Add a note to preferred_format. * Remove the unnecessary allocation.
1 parent 45efae3 commit cc203a6

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ Bottom level categories:
8989

9090
- Disable suballocation on Intel Iris(R) Xe. By @xiaopengli89 in [#3668](https://github.com/gfx-rs/wgpu/pull/3668)
9191

92+
#### WebGPU
93+
94+
- Use `get_preferred_canvas_format()` to fill `formats` of `SurfaceCapabilities`. By @jinleili in [#3744](https://github.com/gfx-rs/wgpu/pull/3744)
95+
9296
### Examples
9397

9498
#### General

wgpu/src/backend/web.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,13 +1113,22 @@ impl crate::context::Context for Context {
11131113
_adapter: &Self::AdapterId,
11141114
_adapter_data: &Self::AdapterData,
11151115
) -> wgt::SurfaceCapabilities {
1116+
let mut formats = vec![
1117+
wgt::TextureFormat::Rgba8Unorm,
1118+
wgt::TextureFormat::Bgra8Unorm,
1119+
wgt::TextureFormat::Rgba16Float,
1120+
];
1121+
let mut mapped_formats = formats.iter().map(|format| map_texture_format(*format));
1122+
// Preferred canvas format will only be either "rgba8unorm" or "bgra8unorm".
1123+
// https://www.w3.org/TR/webgpu/#dom-gpu-getpreferredcanvasformat
1124+
let preferred_format = self.0.get_preferred_canvas_format();
1125+
if let Some(index) = mapped_formats.position(|format| format == preferred_format) {
1126+
formats.swap(0, index);
1127+
}
1128+
11161129
wgt::SurfaceCapabilities {
11171130
// https://gpuweb.github.io/gpuweb/#supported-context-formats
1118-
formats: vec![
1119-
wgt::TextureFormat::Bgra8Unorm,
1120-
wgt::TextureFormat::Rgba8Unorm,
1121-
wgt::TextureFormat::Rgba16Float,
1122-
],
1131+
formats,
11231132
// Doesn't really have meaning on the web.
11241133
present_modes: vec![wgt::PresentMode::Fifo],
11251134
alpha_modes: vec![wgt::CompositeAlphaMode::Opaque],

0 commit comments

Comments
 (0)