mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-19 14:01:08 +01:00
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
176 lines
5.7 KiB
Rust
176 lines
5.7 KiB
Rust
//! Test utilities for the wgpu repository.
|
|
|
|
#![allow(clippy::arc_with_non_send_sync, reason = "False positive on wasm")]
|
|
|
|
mod config;
|
|
mod expectations;
|
|
pub mod image;
|
|
mod init;
|
|
mod isolation;
|
|
pub mod native;
|
|
mod params;
|
|
mod poll;
|
|
mod report;
|
|
mod run;
|
|
pub mod wasm;
|
|
pub mod wasm_manager;
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
pub use init::initialize_html_canvas;
|
|
|
|
pub use self::image::ComparisonType;
|
|
pub use config::{GpuTestConfiguration, GpuTestInitializer};
|
|
pub use expectations::{FailureApplicationReasons, FailureBehavior, FailureCase, FailureReason};
|
|
pub use init::{init_logger, initialize_adapter, initialize_device, initialize_instance};
|
|
pub use macro_rules_attribute::apply;
|
|
pub use params::TestParameters;
|
|
pub use run::{execute_test, TestingContext};
|
|
|
|
/// Run some code in an error scope and assert that validation fails.
|
|
///
|
|
/// Note that errors related to commands for the GPU (i.e. raised by methods on
|
|
/// GPUCommandEncoder, GPURenderPassEncoder, GPUComputePassEncoder,
|
|
/// GPURenderBundleEncoder) are usually not raised immediately. They are raised
|
|
/// only when `finish()` is called on the command encoder. Tests of such error
|
|
/// cases should call `fail` with a closure that calls `finish()`, not with a
|
|
/// closure that encodes the actual command.
|
|
pub fn fail<T>(
|
|
device: &wgpu::Device,
|
|
callback: impl FnOnce() -> T,
|
|
expected_msg_substring: Option<&str>,
|
|
) -> T {
|
|
let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
|
|
let result = callback();
|
|
let validation_error = pollster::block_on(scope.pop())
|
|
.expect("expected validation error in callback, but no validation error was emitted");
|
|
if let Some(expected_msg_substring) = expected_msg_substring {
|
|
let lowered_expected = expected_msg_substring.to_lowercase();
|
|
let lowered_actual = validation_error.to_string().to_lowercase();
|
|
assert!(
|
|
lowered_actual.contains(&lowered_expected),
|
|
concat!(
|
|
"expected validation error case-insensitively containing {}, ",
|
|
"but it was not present in actual error message:\n{}"
|
|
),
|
|
expected_msg_substring,
|
|
validation_error
|
|
);
|
|
}
|
|
|
|
result
|
|
}
|
|
|
|
/// Run some code in an error scope and assert that validation succeeds.
|
|
#[track_caller]
|
|
pub fn valid<T>(device: &wgpu::Device, callback: impl FnOnce() -> T) -> T {
|
|
let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
|
|
let result = callback();
|
|
if let Some(error) = pollster::block_on(scope.pop()) {
|
|
panic!(
|
|
"`valid` block at {} encountered wgpu error:\n{error}",
|
|
std::panic::Location::caller()
|
|
);
|
|
}
|
|
|
|
result
|
|
}
|
|
|
|
/// Run some code in an error scope and assert that validation succeeds or fails depending on the
|
|
/// provided `should_fail` boolean.
|
|
pub fn fail_if<T>(
|
|
device: &wgpu::Device,
|
|
should_fail: bool,
|
|
callback: impl FnOnce() -> T,
|
|
expected_msg_substring: Option<&'static str>,
|
|
) -> T {
|
|
if should_fail {
|
|
fail(device, callback, expected_msg_substring)
|
|
} else {
|
|
valid(device, callback)
|
|
}
|
|
}
|
|
|
|
fn did_fill_error_scope<T>(
|
|
device: &wgpu::Device,
|
|
callback: impl FnOnce() -> T,
|
|
filter: wgpu::ErrorFilter,
|
|
) -> (bool, T) {
|
|
let scope = device.push_error_scope(filter);
|
|
let result = callback();
|
|
let validation_error = pollster::block_on(scope.pop());
|
|
let failed = validation_error.is_some();
|
|
|
|
(failed, result)
|
|
}
|
|
|
|
/// Returns true if the provided callback fails validation.
|
|
pub fn did_fail<T>(device: &wgpu::Device, callback: impl FnOnce() -> T) -> (bool, T) {
|
|
did_fill_error_scope(device, callback, wgpu::ErrorFilter::Validation)
|
|
}
|
|
|
|
/// Returns true if the provided callback encounters an out-of-memory error.
|
|
pub fn did_oom<T>(device: &wgpu::Device, callback: impl FnOnce() -> T) -> (bool, T) {
|
|
did_fill_error_scope(device, callback, wgpu::ErrorFilter::OutOfMemory)
|
|
}
|
|
|
|
/// Adds the necessary main function for our gpu test harness.
|
|
///
|
|
/// Takes a single argument which is an expression that evaluates to `Vec<wgpu_test::GpuTestInitializer>`.
|
|
#[macro_export]
|
|
macro_rules! gpu_test_main {
|
|
($tests: expr) => {
|
|
#[cfg(all(
|
|
target_arch = "wasm32",
|
|
any(target_os = "emscripten", feature = "webgl")
|
|
))]
|
|
#[wasm_bindgen::prelude::wasm_bindgen]
|
|
pub fn run_test(test_name: String) {
|
|
$crate::wasm::main($tests, test_name);
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
fn main() {
|
|
// Ensure that value is used so that warnings don't happen.
|
|
let _ = $tests;
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn main() -> $crate::native::MainResult {
|
|
if cfg!(wasm_test) {
|
|
$crate::wasm_manager::run_wasm_browser_tests($tests)
|
|
} else {
|
|
$crate::native::main($tests)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Creates a test that will run on all gpus on a given system.
|
|
///
|
|
/// Use this macro as [`#[apply(gpu_test)]`][apply] on a static variable with a type that
|
|
/// can be converted to a [`GpuTestConfiguration`].
|
|
#[macro_export]
|
|
macro_rules! gpu_test {
|
|
(
|
|
$( #[ $meta:meta ] )*
|
|
$vis:vis static $static_name:ident: $static_type:ty = $init_expr:expr;
|
|
) => {
|
|
$crate::macro_support::paste! {
|
|
$( #[ $meta ] )*
|
|
#[allow(non_snake_case)]
|
|
$vis fn $static_name() -> $crate::GpuTestConfiguration {
|
|
struct S;
|
|
|
|
// Allow any type that can be converted to a GpuTestConfiguration
|
|
$crate::GpuTestConfiguration::from($init_expr)
|
|
.name_from_init_function_typename::<S>(stringify!([< $static_name:lower >]))
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
#[doc(hidden)]
|
|
pub mod macro_support {
|
|
pub use paste::paste;
|
|
}
|