From 4ad1ea8523c2e59ac4d26aeda1c95550c2cf41f0 Mon Sep 17 00:00:00 2001 From: PancakeTAS Date: Sun, 6 Jul 2025 14:52:45 +0200 Subject: [PATCH] use same device for subcontext fixes #13 --- include/layer.hpp | 24 ++++++++------- include/utils/utils.hpp | 8 +++++ lsfg-vk-gen/src/core/device.cpp | 18 ++++++++--- src/context.cpp | 4 +++ src/layer.cpp | 53 ++++++++++++++++++++------------- src/utils/utils.cpp | 8 ++++- 6 files changed, 80 insertions(+), 35 deletions(-) diff --git a/include/layer.hpp b/include/layer.hpp index d06bbb4..87c6857 100644 --- a/include/layer.hpp +++ b/include/layer.hpp @@ -34,6 +34,20 @@ namespace Layer { VkDevice device, const char* pName); + /// Call to the original vkGetPhysicalDeviceQueueFamilyProperties function. + void ovkGetPhysicalDeviceQueueFamilyProperties( + VkPhysicalDevice physicalDevice, + uint32_t* pQueueFamilyPropertyCount, + VkQueueFamilyProperties* pQueueFamilyProperties); + /// Call to the original vkGetPhysicalDeviceMemoryProperties function. + void ovkGetPhysicalDeviceMemoryProperties( + VkPhysicalDevice physicalDevice, + VkPhysicalDeviceMemoryProperties* pMemoryProperties); + /// Call to the original vkGetPhysicalDeviceProperties function. + void ovkGetPhysicalDeviceProperties( + VkPhysicalDevice physicalDevice, + VkPhysicalDeviceProperties* pProperties); + /// Call to the original vkCreateSwapchainKHR function. VkResult ovkCreateSwapchainKHR( VkDevice device, @@ -147,16 +161,6 @@ namespace Layer { const VkSemaphoreGetFdInfoKHR* pGetFdInfo, int* pFd); - /// Call to the original vkGetPhysicalDeviceQueueFamilyProperties function. - void ovkGetPhysicalDeviceQueueFamilyProperties( - VkPhysicalDevice physicalDevice, - uint32_t* pQueueFamilyPropertyCount, - VkQueueFamilyProperties* pQueueFamilyProperties); - /// Call to the original vkGetPhysicalDeviceMemoryProperties function. - void ovkGetPhysicalDeviceMemoryProperties( - VkPhysicalDevice physicalDevice, - VkPhysicalDeviceMemoryProperties* pMemoryProperties); - /// Call to the original vkGetDeviceQueue function. void ovkGetDeviceQueue( VkDevice device, diff --git a/include/utils/utils.hpp b/include/utils/utils.hpp index fb6d8b1..147bfe3 100644 --- a/include/utils/utils.hpp +++ b/include/utils/utils.hpp @@ -22,6 +22,14 @@ namespace Utils { std::pair findQueue(VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceCreateInfo* desc, VkQueueFlags flags); + /// + /// Get the UUID of the physical device. + /// + /// @param physicalDevice The physical device to get the UUID from. + /// @return The UUID of the physical device. + /// + uint64_t getDeviceUUID(VkPhysicalDevice physicalDevice); + /// /// Ensure a list of extensions is present in the given array. /// diff --git a/lsfg-vk-gen/src/core/device.cpp b/lsfg-vk-gen/src/core/device.cpp index ebfbd3f..098ebbf 100644 --- a/lsfg-vk-gen/src/core/device.cpp +++ b/lsfg-vk-gen/src/core/device.cpp @@ -24,18 +24,28 @@ Device::Device(const Instance& instance) { if (res != VK_SUCCESS) throw LSFG::vulkan_error(res, "Failed to get physical devices"); + // get uuid env vars + const char* deviceUUIDEnv = std::getenv("LSFG_DEVICE_UUID"); + if (!deviceUUIDEnv) + throw LSFG::vulkan_error(VK_ERROR_INITIALIZATION_FAILED, + "LSFG_DEVICE_UUID environment variable not set"); + const uint64_t deviceUUID = std::stoull(deviceUUIDEnv); + // find first discrete GPU std::optional physicalDevice; for (const auto& device : devices) { VkPhysicalDeviceProperties properties; vkGetPhysicalDeviceProperties(device, &properties); - physicalDevice = device; - if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) - break; // dedicated will always work + const uint64_t uuid = static_cast(properties.vendorID) << 32 | properties.deviceID; + if (deviceUUID == uuid) { + physicalDevice = device; + break; + } } if (!physicalDevice) - throw LSFG::vulkan_error(VK_ERROR_INITIALIZATION_FAILED, "No discrete GPU found"); + throw LSFG::vulkan_error(VK_ERROR_INITIALIZATION_FAILED, + "Could not find physical device with UUID"); // find queue family indices uint32_t familyCount{}; diff --git a/src/context.cpp b/src/context.cpp index 2816f41..76f8ae4 100644 --- a/src/context.cpp +++ b/src/context.cpp @@ -3,6 +3,7 @@ #include "utils/log.hpp" #include "utils/utils.hpp" +#include #include #include @@ -46,6 +47,9 @@ LsContext::LsContext(const Hooks::DeviceInfo& info, VkSwapchainKHR swapchain, } // initialize lsfg + const uint64_t deviceUUID = Utils::getDeviceUUID(info.physicalDevice); + setenv("LSFG_DEVICE_UUID", std::to_string(deviceUUID).c_str(), 1); + Log::debug("context", "(entering LSFG initialization)"); Utils::storeLayerEnv(); LSFG::initialize(); diff --git a/src/layer.cpp b/src/layer.cpp index d5ec26f..af0bf5c 100644 --- a/src/layer.cpp +++ b/src/layer.cpp @@ -19,6 +19,10 @@ namespace { PFN_vkGetInstanceProcAddr next_vkGetInstanceProcAddr{}; PFN_vkGetDeviceProcAddr next_vkGetDeviceProcAddr{}; + PFN_vkGetPhysicalDeviceQueueFamilyProperties next_vkGetPhysicalDeviceQueueFamilyProperties{}; + PFN_vkGetPhysicalDeviceMemoryProperties next_vkGetPhysicalDeviceMemoryProperties{}; + PFN_vkGetPhysicalDeviceProperties next_vkGetPhysicalDeviceProperties{}; + PFN_vkCreateSwapchainKHR next_vkCreateSwapchainKHR{}; PFN_vkQueuePresentKHR next_vkQueuePresentKHR{}; PFN_vkDestroySwapchainKHR next_vkDestroySwapchainKHR{}; @@ -39,8 +43,6 @@ namespace { PFN_vkDestroySemaphore next_vkDestroySemaphore{}; PFN_vkGetMemoryFdKHR next_vkGetMemoryFdKHR{}; PFN_vkGetSemaphoreFdKHR next_vkGetSemaphoreFdKHR{}; - PFN_vkGetPhysicalDeviceQueueFamilyProperties next_vkGetPhysicalDeviceQueueFamilyProperties{}; - PFN_vkGetPhysicalDeviceMemoryProperties next_vkGetPhysicalDeviceMemoryProperties{}; PFN_vkGetDeviceQueue next_vkGetDeviceQueue{}; PFN_vkQueueSubmit next_vkQueueSubmit{}; PFN_vkCmdPipelineBarrier next_vkCmdPipelineBarrier{}; @@ -112,8 +114,12 @@ namespace { // get relevant function pointers from the next layer success = true; success &= initInstanceFunc(*pInstance, "vkDestroyInstance", &next_vkDestroyInstance); - success &= initInstanceFunc(*pInstance, "vkGetPhysicalDeviceQueueFamilyProperties", &next_vkGetPhysicalDeviceQueueFamilyProperties); - success &= initInstanceFunc(*pInstance, "vkGetPhysicalDeviceMemoryProperties", &next_vkGetPhysicalDeviceMemoryProperties); + success &= initInstanceFunc(*pInstance, + "vkGetPhysicalDeviceQueueFamilyProperties", &next_vkGetPhysicalDeviceQueueFamilyProperties); + success &= initInstanceFunc(*pInstance, + "vkGetPhysicalDeviceMemoryProperties", &next_vkGetPhysicalDeviceMemoryProperties); + success &= initInstanceFunc(*pInstance, + "vkGetPhysicalDeviceProperties", &next_vkGetPhysicalDeviceProperties); if (!success) { Log::error("layer", "Failed to get instance function pointers"); return VK_ERROR_INITIALIZATION_FAILED; @@ -319,6 +325,29 @@ PFN_vkVoidFunction Layer::ovkGetDeviceProcAddr( return next_vkGetDeviceProcAddr(device, pName); } +void Layer::ovkGetPhysicalDeviceQueueFamilyProperties( + VkPhysicalDevice physicalDevice, + uint32_t* pQueueFamilyPropertyCount, + VkQueueFamilyProperties* pQueueFamilyProperties) { + Log::debug("vulkan", "vkGetPhysicalDeviceQueueFamilyProperties called for physical device {:x}", + reinterpret_cast(physicalDevice)); + next_vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties); +} +void Layer::ovkGetPhysicalDeviceMemoryProperties( + VkPhysicalDevice physicalDevice, + VkPhysicalDeviceMemoryProperties* pMemoryProperties) { + Log::debug("vulkan", "vkGetPhysicalDeviceMemoryProperties called for physical device {:x}", + reinterpret_cast(physicalDevice)); + next_vkGetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties); +} +void Layer::ovkGetPhysicalDeviceProperties( + VkPhysicalDevice physicalDevice, + VkPhysicalDeviceProperties* pProperties) { + Log::debug("vulkan", "vkGetPhysicalDeviceProperties called for physical device {:x}", + reinterpret_cast(physicalDevice)); + next_vkGetPhysicalDeviceProperties(physicalDevice, pProperties); +} + VkResult Layer::ovkCreateSwapchainKHR( VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, @@ -547,22 +576,6 @@ VkResult Layer::ovkGetSemaphoreFdKHR( return res; } -void Layer::ovkGetPhysicalDeviceQueueFamilyProperties( - VkPhysicalDevice physicalDevice, - uint32_t* pQueueFamilyPropertyCount, - VkQueueFamilyProperties* pQueueFamilyProperties) { - Log::debug("vulkan", "vkGetPhysicalDeviceQueueFamilyProperties called for physical device {:x}", - reinterpret_cast(physicalDevice)); - next_vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties); -} -void Layer::ovkGetPhysicalDeviceMemoryProperties( - VkPhysicalDevice physicalDevice, - VkPhysicalDeviceMemoryProperties* pMemoryProperties) { - Log::debug("vulkan", "vkGetPhysicalDeviceMemoryProperties called for physical device {:x}", - reinterpret_cast(physicalDevice)); - next_vkGetPhysicalDeviceMemoryProperties(physicalDevice, pMemoryProperties); -} - void Layer::ovkGetDeviceQueue( VkDevice device, uint32_t queueFamilyIndex, diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 8621f76..a50c067 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -6,7 +6,6 @@ #include #include -#include using namespace Utils; @@ -38,6 +37,13 @@ std::pair Utils::findQueue(VkDevice device, VkPhysicalDevice return { *idx, queue }; } +uint64_t Utils::getDeviceUUID(VkPhysicalDevice physicalDevice) { + VkPhysicalDeviceProperties properties{}; + Layer::ovkGetPhysicalDeviceProperties(physicalDevice, &properties); + + return static_cast(properties.vendorID) << 32 | properties.deviceID; +} + std::vector Utils::addExtensions(const char* const* extensions, size_t count, const std::vector& requiredExtensions) { std::vector ext(count);