Debian Codegraph @

third_party/rust/wgpu-hal/src/auxil/dxgi/exception.rs

raw
1 use alloc::{
2 borrow::Cow,
3 string::{String, ToString as _},
4 };
5
6 use parking_lot::Mutex;
7 use windows::Win32::{Foundation, System::Diagnostics::Debug};
8
9 // This is a mutex as opposed to an atomic as we need to completely
10 // lock everyone out until we have registered or unregistered the
11 // exception handler, otherwise really nasty races could happen.
12 //
13 // By routing all the registration through these functions we can guarantee
14 // there is either 1 or 0 exception handlers registered, not multiple.
15 static EXCEPTION_HANDLER_COUNT: Mutex<usize> = Mutex::new(0);
16
17 pub fn register_exception_handler() {
18 let mut count_guard = EXCEPTION_HANDLER_COUNT.lock();
19 if *count_guard == 0 {
20 unsafe { Debug::AddVectoredExceptionHandler(0, Some(output_debug_string_handler)) };
21 }
22 *count_guard += 1;
23 }
24
25 pub fn unregister_exception_handler() {
26 let mut count_guard = EXCEPTION_HANDLER_COUNT.lock();
27 if *count_guard == 1 {
28 unsafe { Debug::RemoveVectoredExceptionHandler(output_debug_string_handler as *mut _) };
29 }
30 *count_guard -= 1;
31 }
32
33 const MESSAGE_PREFIXES: &[(&str, log::Level)] = &[
34 ("CORRUPTION", log::Level::Error),
35 ("ERROR", log::Level::Error),
36 ("WARNING", log::Level::Warn),
37 ("INFO", log::Level::Info),
38 ("MESSAGE", log::Level::Debug),
39 ];
40
41 unsafe extern "system" fn output_debug_string_handler(
42 exception_info: *mut Debug::EXCEPTION_POINTERS,
43 ) -> i32 {
44 // See https://stackoverflow.com/a/41480827
45 let record = unsafe { &*(*exception_info).ExceptionRecord };
46 if record.NumberParameters != 2 {
47 return Debug::EXCEPTION_CONTINUE_SEARCH;
48 }
49 let message = match record.ExceptionCode {
50 Foundation::DBG_PRINTEXCEPTION_C => {
51 String::from_utf8_lossy(bytemuck::cast_slice(&record.ExceptionInformation))
52 }
53 Foundation::DBG_PRINTEXCEPTION_WIDE_C => Cow::Owned(String::from_utf16_lossy(
54 bytemuck::cast_slice(&record.ExceptionInformation),
55 )),
56 _ => return Debug::EXCEPTION_CONTINUE_SEARCH,
57 };
58
59 let message = match message.strip_prefix("D3D12 ") {
60 Some(msg) => msg
61 .trim_end_matches("\n\0")
62 .trim_end_matches("[ STATE_CREATION WARNING #0: UNKNOWN]"),
63 None => return Debug::EXCEPTION_CONTINUE_SEARCH,
64 };
65
66 let (message, level) = match MESSAGE_PREFIXES
67 .iter()
68 .find(|&&(prefix, _)| message.starts_with(prefix))
69 {
70 Some(&(prefix, level)) => (&message[prefix.len() + 2..], level),
71 None => (message, log::Level::Debug),
72 };
73
74 if level == log::Level::Warn && message.contains("#82") {
75 // This is are useless spammy warnings (#820, #821):
76 // "The application did not pass any clear value to resource creation"
77 return Debug::EXCEPTION_CONTINUE_SEARCH;
78 }
79
80 if level == log::Level::Warn && message.contains("DRAW_EMPTY_SCISSOR_RECTANGLE") {
81 // This is normal, WebGPU allows passing empty scissor rectangles.
82 return Debug::EXCEPTION_CONTINUE_SEARCH;
83 }
84
85 let _ = std::panic::catch_unwind(|| {
86 log::log!(level, "{}", message);
87 });
88
89 if cfg!(debug_assertions) && level == log::Level::Error {
90 // Set canary and continue
91 crate::VALIDATION_CANARY.add(message.to_string());
92 }
93
94 Debug::EXCEPTION_CONTINUE_EXECUTION
95 }