From ef79a923cfea50f86a4241ef523f8fd6091cf9a0 Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Fri, 17 Jul 2026 12:41:57 -0700 Subject: [PATCH] hal/vulkan: With `internal_error_panic`, panic on Vulkan validation errors (#9879) --- Cargo.lock | 1 + cts_runner/Cargo.toml | 1 + wgpu-hal/Cargo.toml | 9 +++-- wgpu-hal/src/vulkan/instance.rs | 71 +++++++++++++++++++++++++++++++++ xtask/src/cts.rs | 4 ++ 5 files changed, 82 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 885b457e0..ec3d78604 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -815,6 +815,7 @@ dependencies = [ "tempfile", "termcolor", "tokio", + "wgpu-hal", ] [[package]] diff --git a/cts_runner/Cargo.toml b/cts_runner/Cargo.toml index 8e64bf9df..2271269e7 100644 --- a/cts_runner/Cargo.toml +++ b/cts_runner/Cargo.toml @@ -24,6 +24,7 @@ log.workspace = true pico-args.workspace = true tokio = { workspace = true, features = ["full"] } termcolor.workspace = true +wgpu-hal = { workspace = true, features = ["internal_error_panic"] } [dev-dependencies] tempfile.workspace = true diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index e538b12cb..3831adb8b 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -186,10 +186,11 @@ portable-atomic = ["dep:portable-atomic", "dep:portable-atomic-util"] # Panic when running into a device lost error (for debugging purposes). # Only affects the d3d12 and vulkan backends. device_lost_panic = [] -# Panic when running into an internal error other than out-of-memory and device lost -# (for debugging purposes). -# -# Only affects the d3d12 and vulkan backends. +# Panic on unexpected errors (does _not_ include out-of-memory errors or device +# loss) from platform APIs, for debugging purposes. Such errors likely indicate +# bug(s) in wgpu, platform components, or drivers. Despite the name, this is not +# related to the spec's `GPUInternalError`. Only affects the d3d12 and vulkan +# backends. internal_error_panic = [] # Tracks validation errors in a `VALIDATION_CANARY` static. validation_canary = ["dep:parking_lot"] diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index 2b12813df..1e3944b2c 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -158,9 +158,80 @@ unsafe extern "system" fn debug_utils_messenger_callback( crate::VALIDATION_CANARY.add(message.to_string()); } + #[cfg(all(debug_assertions, feature = "internal_error_panic"))] + if level == log::Level::Error + && message_type.contains(vk::DebugUtilsMessageTypeFlagsEXT::VALIDATION) + && !error_is_waived(cd.message_id_number) + && !cts_error_is_waived(cd.message_id_number) + { + use alloc::string::ToString as _; + panic!("{}", message.to_string()); + } + vk::FALSE } +/// Validation errors known to fire, not just in the CTS. +/// +/// These never panic. +#[cfg(feature = "internal_error_panic")] +fn error_is_waived(message_id_number: i32) -> bool { + const WAIVED_MESSAGE_IDS: &[i32] = &[ + // SYNC-HAZARD-WRITE-AFTER-WRITE + // e.g. webgpu:api,operation,memory_sync,texture,readonly_depth_stencil:sampling_while_testing:* + // https://github.com/gfx-rs/wgpu/issues/5231 + // https://github.com/gfx-rs/wgpu/issues/8705 + 0x5c0ec5d6_u32 as i32, + ]; + + WAIVED_MESSAGE_IDS.contains(&message_id_number) +} + +/// Validation errors known to fire when running the CTS. +/// +/// These waivers are keyed off the `WGPU_CTS_XTASK` environment variable, which +/// is set in `xtask/src/cts.rs`. +#[cfg(feature = "internal_error_panic")] +fn cts_error_is_waived(message_id_number: i32) -> bool { + use std::sync::LazyLock; + + static WGPU_CTS_XTASK: LazyLock = + LazyLock::new(|| std::env::var_os("WGPU_CTS_XTASK").is_some()); + + if !*WGPU_CTS_XTASK { + return false; + } + + const WAIVED_MESSAGE_IDS: &[i32] = &[ + // VUID-vkCmdPushConstants-offset-01795 + // e.g. webgpu:api,operation,command_buffer,programmable,immediate:* + 0x27bc88c6_u32 as i32, + // VUID-SampleMask-SampleMask-04359 + // e.g. webgpu:api,validation,render_pipeline,inter_stage:max_variables_count,* + 0x34d444b2_u32 as i32, + // VUID-vkCmdCopyImage-srcImage-01728 + // e.g. webgpu:api,validation,encoding,cmds,copyTextureToTexture:* + 0x6b654496_u32 as i32, + // VUID-StandaloneSpirv-OpImageQuerySizeLod-04659 + // e.g. webgpu:shader,execution,expression,call,builtin,textureNumLayers:* + 0x82396078_u32 as i32, + // VUID-RuntimeSpirv-Location-06272 + // e.g. webgpu:api,validation,render_pipeline,inter_stage:max_variables_count,* + 0xa3614f8b_u32 as i32, + // VUID-VkViewport-width-01770 + // e.g. webgpu:api,validation,encoding,cmds,render,dynamic_state:* + 0xa4164ba5_u32 as i32, + // VUID-VkImageViewCreateInfo-image-04441 + // e.g. webgpu:api,validation,createView:texture_view_usage:* + 0xb75da543_u32 as i32, + // VUID-VkBufferCreateInfo-None-09500 + // e.g. webgpu:api,validation,buffer,create:usage,* + 0xf6d454db_u32 as i32, + ]; + + WAIVED_MESSAGE_IDS.contains(&message_id_number) +} + impl super::DebugUtilsCreateInfo { fn to_vk_create_info(&self) -> vk::DebugUtilsMessengerCreateInfoEXT<'_> { let user_data_ptr: *const super::DebugUtilsMessengerUserData = &*self.callback_data; diff --git a/xtask/src/cts.rs b/xtask/src/cts.rs index 7fc5bf139..78bc3e980 100644 --- a/xtask/src/cts.rs +++ b/xtask/src/cts.rs @@ -72,6 +72,10 @@ pub fn run_cts( let llvm_cov = args.contains("--llvm-cov"); let release = args.contains("--release"); + // This is used in the Vulkan hal to waive pre-existing validation + // errors in the CTS, until they can be fixed. + shell.set_var("WGPU_CTS_XTASK", "1"); + let output_filter = args .opt_value_from_str::<_, String>("--print-output-when")? .map(|f| {