wgpu_test/
init.rs

1use wgpu::{Adapter, Backends, Device, Features, Instance, Limits, Queue};
2
3use crate::{report::AdapterReport, TestParameters};
4
5/// Initialize the logger for the test runner.
6pub fn init_logger() {
7    // We don't actually care if it fails
8    #[cfg(not(target_arch = "wasm32"))]
9    let _ = env_logger::try_init();
10    #[cfg(target_arch = "wasm32")]
11    let _ = console_log::init_with_level(log::Level::Info);
12}
13
14/// Initialize a wgpu instance with the options from the environment.
15pub fn initialize_instance(backends: wgpu::Backends, params: &TestParameters) -> Instance {
16    // We ignore `WGPU_BACKEND` for now, merely using test filtering to only run a single backend's tests.
17    //
18    // We can potentially work support back into the test runner in the future, but as the adapters are matched up
19    // based on adapter index, removing some backends messes up the indexes in annoying ways.
20    //
21    // WORKAROUND for https://github.com/rust-lang/cargo/issues/7160:
22    // `--no-default-features` is not passed through correctly to the test runner.
23    // We use it whenever we want to explicitly run with webgl instead of webgpu.
24    // To "disable" webgpu regardless, we do this by removing the webgpu backend whenever we see
25    // the webgl feature.
26    let backends = if cfg!(feature = "webgl") {
27        backends - wgpu::Backends::BROWSER_WEBGPU
28    } else {
29        backends
30    };
31    // Some tests need to be able to force demote to FXC, to specifically test workarounds for FXC
32    // behavior.
33    let dx12_shader_compiler = if params.force_fxc {
34        wgpu::Dx12Compiler::Fxc
35    } else {
36        wgpu::Dx12Compiler::from_env().unwrap_or(wgpu::Dx12Compiler::StaticDxc)
37    };
38    // The defaults for debugging, overridden by the environment, overridden by the test parameters.
39    let flags = wgpu::InstanceFlags::debugging()
40        .with_env()
41        .union(params.required_instance_flags);
42
43    Instance::new(&wgpu::InstanceDescriptor {
44        backends,
45        flags,
46        memory_budget_thresholds: wgpu::MemoryBudgetThresholds {
47            for_resource_creation: Some(99),
48            for_device_loss: None,
49        },
50        backend_options: wgpu::BackendOptions {
51            dx12: wgpu::Dx12BackendOptions {
52                shader_compiler: dx12_shader_compiler,
53                ..Default::default()
54            },
55            gl: wgpu::GlBackendOptions {
56                fence_behavior: if cfg!(target_family = "wasm") {
57                    // On WebGL, you cannot call Poll(Wait) with any timeout. This is because the
58                    // browser does not things to block. However all of our tests are written to
59                    // expect this behavior. This is the workaround to allow this to work.
60                    //
61                    // However on native you can wait, so we want to ensure that behavior as well.
62                    wgpu::GlFenceBehavior::AutoFinish
63                } else {
64                    wgpu::GlFenceBehavior::Normal
65                },
66                ..Default::default()
67            }
68            .with_env(),
69            // Allow the noop backend to be used in tests. This will not be used unless
70            // WGPU_GPU_TESTS_USE_NOOP_BACKEND env var is set, because wgpu-info will not
71            // enumerate the noop backend.
72            //
73            // However, we use wasm_bindgen_test to run tests on wasm, and wgpu
74            // will chose the noop on wasm32 for some reason.
75            noop: wgpu::NoopBackendOptions {
76                enable: !cfg!(target_arch = "wasm32"),
77            },
78        },
79    })
80}
81
82/// Initialize a wgpu adapter, using the given adapter report to match the adapter.
83pub async fn initialize_adapter(
84    adapter_report: Option<&AdapterReport>,
85    params: &TestParameters,
86) -> (Instance, Adapter, Option<SurfaceGuard>) {
87    let backends = adapter_report
88        .map(|report| Backends::from(report.info.backend))
89        .unwrap_or_default();
90
91    let instance = initialize_instance(backends, params);
92    #[allow(unused_variables)]
93    let surface: Option<wgpu::Surface>;
94    let surface_guard: Option<SurfaceGuard>;
95
96    #[allow(unused_assignments)]
97    // Create a canvas if we need a WebGL2RenderingContext to have a working device.
98    #[cfg(not(all(
99        target_arch = "wasm32",
100        any(target_os = "emscripten", feature = "webgl")
101    )))]
102    {
103        surface = None;
104        surface_guard = None;
105    }
106    #[cfg(all(
107        target_arch = "wasm32",
108        any(target_os = "emscripten", feature = "webgl")
109    ))]
110    {
111        // On wasm, append a canvas to the document body for initializing the adapter
112        let canvas = initialize_html_canvas();
113
114        surface = Some(
115            instance
116                .create_surface(wgpu::SurfaceTarget::Canvas(canvas.clone()))
117                .expect("could not create surface from canvas"),
118        );
119
120        surface_guard = Some(SurfaceGuard { canvas });
121    }
122
123    cfg_if::cfg_if! {
124        if #[cfg(not(target_arch = "wasm32"))] {
125            let adapter_iter = instance.enumerate_adapters(backends);
126            let adapter = adapter_iter.into_iter()
127                // If we have a report, we only want to match the adapter with the same info.
128                //
129                // If we don't have a report, we just take the first adapter.
130                .find(|adapter| if let Some(adapter_report) = adapter_report {
131                    adapter.get_info() == adapter_report.info
132                } else {
133                    true
134                });
135            let Some(adapter) = adapter else {
136                panic!(
137                    "Could not find adapter with info {:#?} in {:#?}",
138                    adapter_report.map(|r| &r.info),
139                    instance.enumerate_adapters(backends).into_iter().map(|a| a.get_info()).collect::<Vec<_>>(),
140                );
141            };
142        } else {
143            let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions {
144                compatible_surface: surface.as_ref(),
145                ..Default::default()
146            }).await.unwrap();
147        }
148    }
149
150    log::info!("Testing using adapter: {:#?}", adapter.get_info());
151
152    (instance, adapter, surface_guard)
153}
154
155/// Initialize a wgpu device from a given adapter.
156pub async fn initialize_device(
157    adapter: &Adapter,
158    features: Features,
159    limits: Limits,
160) -> (Device, Queue) {
161    let bundle = adapter
162        .request_device(&wgpu::DeviceDescriptor {
163            label: None,
164            required_features: features,
165            required_limits: limits,
166            experimental_features: unsafe { wgpu::ExperimentalFeatures::enabled() },
167            memory_hints: wgpu::MemoryHints::MemoryUsage,
168            trace: wgpu::Trace::Off,
169        })
170        .await;
171
172    match bundle {
173        Ok(b) => b,
174        Err(e) => panic!("Failed to initialize device: {e}"),
175    }
176}
177
178/// Create a canvas for testing.
179#[cfg(target_arch = "wasm32")]
180pub fn initialize_html_canvas() -> web_sys::HtmlCanvasElement {
181    use wasm_bindgen::JsCast;
182
183    web_sys::window()
184        .and_then(|win| win.document())
185        .and_then(|doc| {
186            let canvas = doc.create_element("Canvas").unwrap();
187            canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok()
188        })
189        .expect("couldn't create canvas")
190}
191
192pub struct SurfaceGuard {
193    #[cfg(target_arch = "wasm32")]
194    #[allow(unused)]
195    canvas: web_sys::HtmlCanvasElement,
196}
197
198impl SurfaceGuard {
199    #[cfg(all(
200        target_arch = "wasm32",
201        any(target_os = "emscripten", feature = "webgl")
202    ))]
203    pub(crate) fn check_for_unreported_errors(&self) -> bool {
204        use wasm_bindgen::JsCast;
205
206        self.canvas
207            .get_context("webgl2")
208            .unwrap()
209            .unwrap()
210            .dyn_into::<web_sys::WebGl2RenderingContext>()
211            .unwrap()
212            .get_error()
213            != web_sys::WebGl2RenderingContext::NO_ERROR
214    }
215}