diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 652003b199..1ff351d5f5 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1945,6 +1945,22 @@ if(TARGET Vulkan::Headers) add_test(NAME vk_fence_dispatcher_sync COMMAND vk_fence_dispatcher_sync_test) set_tests_properties(vk_fence_dispatcher_sync PROPERTIES SKIP_RETURN_CODE 77) endif() + # A device loss the driver shows only as a timeline counter of UINT64_MAX (driver functions interposed). + add_executable(vk_fence_lost_by_counter_test test/vk_fence_lost_by_counter_test.cpp) + target_link_libraries(vk_fence_lost_by_counter_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) + if(WIN32) + target_compile_definitions(vk_fence_lost_by_counter_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + else() + find_package(Threads REQUIRED) + target_link_libraries(vk_fence_lost_by_counter_test PRIVATE Threads::Threads) + endif() + if(MSVC) + target_compile_options(vk_fence_lost_by_counter_test PRIVATE /W4 /permissive- /utf-8) + endif() + if(BUILD_TESTING) + add_test(NAME vk_fence_lost_by_counter COMMAND vk_fence_lost_by_counter_test) + set_tests_properties(vk_fence_lost_by_counter PROPERTIES SKIP_RETURN_CODE 77) + endif() # The bindless set under view churn (bounded table, spare generations) and failed allocations (host table, in-place # rewrite, a degraded flush instead of a refused submission). add_executable(vk_bindless_grow_test test/vk_bindless_grow_test.cpp) diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 9652fd51b7..5c3e884ba6 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -2091,6 +2091,22 @@ class VulkanFence { return UINT64_MAX; } if (result != VK_SUCCESS) return 0; + if (value == UINT64_MAX) { + // A lost device can show itself here first: measured on NVIDIA 621.24 and reported on 617.14, the counter + // query succeeds with UINT64_MAX (and vkWaitSemaphores with a zero timeout succeeds too) before any call + // has returned VK_ERROR_DEVICE_LOST. vkDeviceWaitIdle is what then says so, and it is also what a driver + // needs to have returned before vkGetDeviceFaultInfoEXT answers (LEONIDA_DEVICE_FAULT=1). When + // vkDeviceWaitIdle succeeds, nothing is recorded and the value is returned as the driver gave it. + VkResult idle = VK_SUCCESS; + { + std::lock_guard lock(context_.submit_mutex()); + idle = context_.observe_result(context_.vk.vkDeviceWaitIdle(context_.device()), + "vkDeviceWaitIdle (a fence counter of UINT64_MAX)"); + } + if (idle == VK_ERROR_DEVICE_LOST) + context_.mark_device_lost( + "a fence's counter read UINT64_MAX and vkDeviceWaitIdle then returned VK_ERROR_DEVICE_LOST"); + } return value; } diff --git a/runtime/test/vk_fence_lost_by_counter_test.cpp b/runtime/test/vk_fence_lost_by_counter_test.cpp new file mode 100644 index 0000000000..ceb154b3a3 --- /dev/null +++ b/runtime/test/vk_fence_lost_by_counter_test.cpp @@ -0,0 +1,114 @@ +// A device loss the driver shows only through a timeline counter (runtime/src/vulkan_backend.h, VulkanFence::completed): +// vkGetSemaphoreCounterValue succeeds with UINT64_MAX and no call has returned VK_ERROR_DEVICE_LOST yet. +// 1. With vkDeviceWaitIdle then returning VK_ERROR_DEVICE_LOST: the context is marked lost and the loss is said once. +// 2. A second read on the lost device asks the driver nothing and says nothing more. +// 3. With vkDeviceWaitIdle returning VK_SUCCESS, the value alone records no loss and nothing is said. +// 4. An ordinary counter value never reaches vkDeviceWaitIdle. +// The driver functions are interposed on a real device's table. Skips with exit code 77 when no Vulkan 1.3 device is +// available. No game data. +#include "vk_test_device.h" +#include "../src/vulkan_backend.h" + +#include +#include +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); std::_Exit(1); } \ +} while (0) + +namespace { + +namespace driver { +PFN_vkGetDeviceProcAddr real_get_device = nullptr; +PFN_vkGetSemaphoreCounterValue real_get_counter = nullptr; + +std::atomic counter_reads_max{false}; +std::atomic idle_reports_loss{false}; +std::atomic idle_calls{0}; +std::atomic counter_calls{0}; + +VKAPI_ATTR VkResult VKAPI_CALL get_semaphore_counter_value(VkDevice device, VkSemaphore sem, std::uint64_t* value) { + counter_calls.fetch_add(1, std::memory_order_relaxed); + if (counter_reads_max.load(std::memory_order_acquire)) { + *value = UINT64_MAX; + return VK_SUCCESS; + } + return real_get_counter(device, sem, value); +} + +VKAPI_ATTR VkResult VKAPI_CALL device_wait_idle(VkDevice) { + idle_calls.fetch_add(1, std::memory_order_relaxed); + return idle_reports_loss.load(std::memory_order_acquire) ? VK_ERROR_DEVICE_LOST : VK_SUCCESS; +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) { + const std::string_view n = name; + if (n == "vkGetSemaphoreCounterValue") return reinterpret_cast(get_semaphore_counter_value); + if (n == "vkDeviceWaitIdle") return reinterpret_cast(device_wait_idle); + return real_get_device(device, name); +} + +void install(PFN_vkGetDeviceProcAddr device_proc, VkDevice device) { + real_get_device = device_proc; + real_get_counter = + reinterpret_cast(device_proc(device, "vkGetSemaphoreCounterValue")); +} +} // namespace driver + +} // namespace + +int main() { + using namespace leonida; + + vktest::Device d; + if (!d.create(false)) { + std::fprintf(stderr, "vk_fence_lost_by_counter_test: no Vulkan 1.3 device, skipped\n"); + return 77; + } + driver::install(d.device_proc, d.device); + + { // 4, then 3: an ordinary value, then UINT64_MAX on a device that is not lost + VulkanContext context(driver::get_device, d.device, true); + EXPECT(context.loaded()); + VulkanFence fence(context, 7); + const unsigned lost_before = device_lost_reports.load(); + EXPECT(fence.completed() == 7); + EXPECT(driver::idle_calls.load() == 0); + + driver::counter_reads_max.store(true, std::memory_order_release); + EXPECT(fence.completed() == UINT64_MAX); // what the driver said; the layer adds nothing to it + driver::counter_reads_max.store(false, std::memory_order_release); + EXPECT(driver::idle_calls.load() == 1); + EXPECT(!context.is_device_lost()); + EXPECT(device_lost_reports.load() == lost_before); + EXPECT(fence.completed() == 7); + } + { // 1 and 2 + VulkanContext context(driver::get_device, d.device, true); + EXPECT(context.loaded()); + VulkanFence fence(context, 0); + const unsigned lost_before = device_lost_reports.load(); + const unsigned idle_before = driver::idle_calls.load(); + + driver::counter_reads_max.store(true, std::memory_order_release); + driver::idle_reports_loss.store(true, std::memory_order_release); + EXPECT(fence.completed() == UINT64_MAX); + EXPECT(context.is_device_lost()); + EXPECT(driver::idle_calls.load() == idle_before + 1); + EXPECT(device_lost_reports.load() == lost_before + 1); + + const unsigned counter_before = driver::counter_calls.load(); + EXPECT(fence.completed() == UINT64_MAX); // lost: answered without asking the driver again + context.mark_device_lost(); + EXPECT(driver::counter_calls.load() == counter_before); + EXPECT(driver::idle_calls.load() == idle_before + 1); + EXPECT(device_lost_reports.load() == lost_before + 1); + driver::counter_reads_max.store(false, std::memory_order_release); + driver::idle_reports_loss.store(false, std::memory_order_release); + } + std::puts("vk_fence_lost_by_counter: ok"); + return 0; +}