From 5be253dec204fb88c300aa4fc9c433926da2af3e Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Sat, 8 Mar 2025 19:51:54 +0100 Subject: [PATCH] vulkan: implement Context::reset_status --- src/gfx_apis/vulkan.rs | 2 +- src/gfx_apis/vulkan/bo_allocator.rs | 12 ++++++++++-- src/gfx_apis/vulkan/device.rs | 14 +++++++++++++- src/gfx_apis/vulkan/renderer.rs | 2 ++ src/gfx_apis/vulkan/shm_image.rs | 1 + src/gfx_apis/vulkan/transfer.rs | 1 + 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/gfx_apis/vulkan.rs b/src/gfx_apis/vulkan.rs index 4a177b79..7124c311 100644 --- a/src/gfx_apis/vulkan.rs +++ b/src/gfx_apis/vulkan.rs @@ -243,7 +243,7 @@ struct Context(Rc); impl GfxContext for Context { fn reset_status(&self) -> Option { - None + self.0.device.lost.get().then_some(ResetStatus::Unknown) } fn render_node(&self) -> Option> { diff --git a/src/gfx_apis/vulkan/bo_allocator.rs b/src/gfx_apis/vulkan/bo_allocator.rs index 4afa8af4..81eddce8 100644 --- a/src/gfx_apis/vulkan/bo_allocator.rs +++ b/src/gfx_apis/vulkan/bo_allocator.rs @@ -260,8 +260,12 @@ impl VulkanBoAllocator { slice::from_ref(&submit_info), Fence::null(), ) + .inspect_err(self.data.device.idl()) .map_err(VulkanError::Submit)?; - device.device_wait_idle().map_err(VulkanError::WaitIdle)?; + device + .device_wait_idle() + .inspect_err(self.data.device.idl()) + .map_err(VulkanError::WaitIdle)?; } destroy_image.forget(); destroy_memory.forget(); @@ -565,8 +569,12 @@ impl VulkanBo { slice::from_ref(&submit_info), Fence::null(), ) + .inspect_err(self.allocator.device.idl()) .map_err(VulkanError::Submit)?; - device.device_wait_idle().map_err(VulkanError::WaitIdle)?; + device + .device_wait_idle() + .inspect_err(self.allocator.device.idl()) + .map_err(VulkanError::WaitIdle)?; } Ok(()) } diff --git a/src/gfx_apis/vulkan/device.rs b/src/gfx_apis/vulkan/device.rs index 976af41b..5734b4d9 100644 --- a/src/gfx_apis/vulkan/device.rs +++ b/src/gfx_apis/vulkan/device.rs @@ -28,7 +28,7 @@ use { push_descriptor, }, vk::{ - DeviceCreateInfo, DeviceQueueCreateInfo, DeviceSize, DriverId, + self, DeviceCreateInfo, DeviceQueueCreateInfo, DeviceSize, DriverId, ExternalSemaphoreFeatureFlags, ExternalSemaphoreHandleTypeFlags, ExternalSemaphoreProperties, MAX_MEMORY_TYPES, MemoryPropertyFlags, MemoryType, PhysicalDevice, PhysicalDeviceBufferDeviceAddressFeatures, @@ -44,6 +44,7 @@ use { }, isnt::std_1::collections::IsntHashMap2Ext, std::{ + cell::Cell, ffi::{CStr, CString}, rc::Rc, sync::Arc, @@ -78,6 +79,7 @@ pub struct VulkanDevice { pub(super) is_anv: bool, pub(super) uniform_buffer_offset_mask: DeviceSize, pub(super) uniform_buffer_descriptor_size: usize, + pub(super) lost: Cell, } impl Drop for VulkanDevice { @@ -103,6 +105,15 @@ impl VulkanDevice { } None } + + #[inline(always)] + pub(super) fn idl(&self) -> impl Fn(&vk::Result) + use<'_> { + |res| { + if *res == vk::Result::ERROR_DEVICE_LOST { + self.lost.set(true); + } + } + } } impl VulkanInstance { @@ -450,6 +461,7 @@ impl VulkanInstance { == DriverId::INTEL_OPEN_SOURCE_MESA, uniform_buffer_offset_mask, uniform_buffer_descriptor_size, + lost: Cell::new(false), })) } } diff --git a/src/gfx_apis/vulkan/renderer.rs b/src/gfx_apis/vulkan/renderer.rs index bdc873be..efb31306 100644 --- a/src/gfx_apis/vulkan/renderer.rs +++ b/src/gfx_apis/vulkan/renderer.rs @@ -1610,6 +1610,7 @@ impl VulkanRenderer { slice::from_ref(&submit_info), release_fence.fence, ) + .inspect_err(self.device.idl()) .map_err(VulkanError::Submit)?; } zone!("export_sync_file"); @@ -1919,6 +1920,7 @@ impl VulkanRenderer { log::warn!("Blocking."); unsafe { if let Err(e) = self.device.device.device_wait_idle() { + self.device.idl()(&e); log::error!("Could not wait for device idle: {}", ErrorFmt(e)); } } diff --git a/src/gfx_apis/vulkan/shm_image.rs b/src/gfx_apis/vulkan/shm_image.rs index b7cd130a..76e96184 100644 --- a/src/gfx_apis/vulkan/shm_image.rs +++ b/src/gfx_apis/vulkan/shm_image.rs @@ -296,6 +296,7 @@ impl VulkanShmImage { slice::from_ref(&submit_info), release_fence.fence, ) + .inspect_err(img.renderer.device.idl()) .map_err(VulkanError::Submit)?; } if tt == TransferType::Upload { diff --git a/src/gfx_apis/vulkan/transfer.rs b/src/gfx_apis/vulkan/transfer.rs index 4fd49d13..7ceb15d0 100644 --- a/src/gfx_apis/vulkan/transfer.rs +++ b/src/gfx_apis/vulkan/transfer.rs @@ -298,6 +298,7 @@ impl VulkanShmImage { slice::from_ref(&submit_info), release_fence.fence, ) + .inspect_err(img.renderer.device.idl()) .map_err(VulkanError::Submit)?; } let sync_file = release_fence.export_sync_file()?;