Skip to content

Commit 4339fbe

Browse files
committed
Single source of truth for preferred canvas format
1 parent 067d92d commit 4339fbe

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

crates/wasi-webgpu-wasmtime/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ pub mod reexports {
2929
pub use wgpu_types;
3030
}
3131

32+
// https://searchfox.org/mozilla-central/source/dom/webgpu/Instance.h#68
33+
#[cfg(target_os = "android")]
34+
const PREFERRED_CANVAS_FORMAT: wasi::webgpu::webgpu::GpuTextureFormat =
35+
wasi::webgpu::webgpu::GpuTextureFormat::Rgba8unorm;
36+
#[cfg(not(target_os = "android"))]
37+
const PREFERRED_CANVAS_FORMAT: wasi::webgpu::webgpu::GpuTextureFormat =
38+
wasi::webgpu::webgpu::GpuTextureFormat::Bgra8unorm;
39+
3240
#[cfg(all(
3341
not(target_os = "linux"),
3442
not(target_os = "android"),
@@ -184,10 +192,7 @@ where
184192
fn display_api_ready(&mut self, display: &Arc<dyn DisplayApi + Send + Sync>) {
185193
let surface_id = (self.create_surface)(display);
186194
// TODO: fix this once user can pass in configuration options. For now just taking from `gpu.get-preferred-canvas-format()`.
187-
#[cfg(target_os = "android")]
188-
let swapchain_format = wgpu_types::TextureFormat::Rgba8Unorm;
189-
#[cfg(not(target_os = "android"))]
190-
let swapchain_format = wgpu_types::TextureFormat::Bgra8Unorm;
195+
let swapchain_format = PREFERRED_CANVAS_FORMAT.into();
191196

192197
// https://www.w3.org/TR/webgpu/#dictdef-gpucanvasconfiguration
193198
let config = wgpu_types::SurfaceConfiguration {

crates/wasi-webgpu-wasmtime/src/trait_impls.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
wasi::{io::poll, webgpu::webgpu},
1717
wrapper_types::{Buffer, ComputePassEncoder, Device, RenderBundleEncoder, RenderPassEncoder},
1818
AbstractBuffer, MainThreadSpawner, WasiWebGpuImpl, WasiWebGpuView, WebGpuSurface,
19+
PREFERRED_CANVAS_FORMAT,
1920
};
2021

2122
impl<T: WasiWebGpuView> webgpu::Host for WasiWebGpuImpl<T> {
@@ -2375,11 +2376,7 @@ impl<T: WasiWebGpuView> webgpu::HostGpu for WasiWebGpuImpl<T> {
23752376
&mut self,
23762377
_gpu: Resource<webgpu::Gpu>,
23772378
) -> webgpu::GpuTextureFormat {
2378-
// https://searchfox.org/mozilla-central/source/dom/webgpu/Instance.h#68
2379-
#[cfg(target_os = "android")]
2380-
return webgpu::GpuTextureFormat::Rgba8unorm;
2381-
#[cfg(not(target_os = "android"))]
2382-
return webgpu::GpuTextureFormat::Bgra8unorm;
2379+
PREFERRED_CANVAS_FORMAT
23832380
}
23842381

23852382
fn wgsl_language_features(

0 commit comments

Comments
 (0)