Skip to content

Commit 66c7e98

Browse files
authored
Add feature flags in hal to panic when running into some types of errors (#5273)
1 parent 75a98f2 commit 66c7e98

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

wgpu-hal/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ dxc_shader_compiler = ["hassle-rs"]
6363
renderdoc = ["libloading", "renderdoc-sys"]
6464
fragile-send-sync-non-atomic-wasm = ["wgt/fragile-send-sync-non-atomic-wasm"]
6565
link = ["metal/link"]
66+
# Panic when running into an out-of-memory error (for debugging purposes).
67+
#
68+
# Only affects the d3d12 and vulkan backends.
69+
oom_panic = []
70+
# Panic when running into a device lost error (for debugging purposes).
71+
# Only affects the d3d12 and vulkan backends.
72+
device_lost_panic = []
73+
# Panic when running into an internal error other than out-of-memory and device lost
74+
# (for debugging purposes).
75+
#
76+
# Only affects the d3d12 and vulkan backends.
77+
internal_error_panic = []
6678

6779
[[example]]
6880
name = "halmark"

wgpu-hal/src/auxil/dxgi/result.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,26 @@ impl HResult<()> for i32 {
2121
Err(Cow::Borrowed(description))
2222
}
2323
fn into_device_result(self, description: &str) -> Result<(), crate::DeviceError> {
24+
#![allow(unreachable_code)]
25+
2426
self.into_result().map_err(|err| {
2527
log::error!("{} failed: {}", description, err);
28+
29+
match self {
30+
winerror::E_OUTOFMEMORY => {
31+
#[cfg(feature = "oom_panic")]
32+
panic!("{description} failed: Out of memory");
33+
}
34+
winerror::DXGI_ERROR_DEVICE_RESET | winerror::DXGI_ERROR_DEVICE_REMOVED => {
35+
#[cfg(feature = "device_lost_panic")]
36+
panic!("{description} failed: Device lost ({err})");
37+
}
38+
_ => {
39+
#[cfg(feature = "internal_error_panic")]
40+
panic!("{description} failed: {err}");
41+
}
42+
}
43+
2644
if self == winerror::E_OUTOFMEMORY {
2745
crate::DeviceError::OutOfMemory
2846
} else {

wgpu-hal/src/vulkan/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,13 +724,25 @@ impl crate::Queue<Api> for Queue {
724724

725725
impl From<vk::Result> for crate::DeviceError {
726726
fn from(result: vk::Result) -> Self {
727+
#![allow(unreachable_code)]
727728
match result {
728729
vk::Result::ERROR_OUT_OF_HOST_MEMORY | vk::Result::ERROR_OUT_OF_DEVICE_MEMORY => {
730+
#[cfg(feature = "oom_panic")]
731+
panic!("Out of memory ({result:?})");
732+
729733
Self::OutOfMemory
730734
}
731-
vk::Result::ERROR_DEVICE_LOST => Self::Lost,
735+
vk::Result::ERROR_DEVICE_LOST => {
736+
#[cfg(feature = "device_lost_panic")]
737+
panic!("Device lost");
738+
739+
Self::Lost
740+
}
732741
_ => {
733-
log::warn!("Unrecognized device error {:?}", result);
742+
#[cfg(feature = "internal_error_panic")]
743+
panic!("Internal error: {result:?}");
744+
745+
log::warn!("Unrecognized device error {result:?}");
734746
Self::Lost
735747
}
736748
}

0 commit comments

Comments
 (0)