From d384c90104d9a86a5ab977c05c4677afb2e26cb6 Mon Sep 17 00:00:00 2001 From: Christoph Haag Date: Fri, 7 May 2021 00:34:47 +0200 Subject: [PATCH] st/oxr: Check that VkPhysicalDevice in graphics binding matches suggested device XR_KHR_vulkan_enable2: physicalDevice VkPhysicalDevice must match the device specified by xrGetVulkanGraphicsDevice2KHR XR_KHR_vulkan_enable: physicalDevice VkPhysicalDevice must match the device specified by xrGetVulkanGraphicsDeviceKHR XR_KHR_vulkan_enable: Add a trivial check that xrGetVulkanGraphicsDeviceKHR is called before xrCreateSession. (our cached suggested device will be XR_NULL_HANDLE if it has not been called). The XR_KHR_vulkan_enable2 code path already contains this check. --- src/xrt/state_trackers/oxr/oxr_api_system.c | 4 ++-- src/xrt/state_trackers/oxr/oxr_objects.h | 4 +++- src/xrt/state_trackers/oxr/oxr_session.c | 15 +++++++++++++++ src/xrt/state_trackers/oxr/oxr_system.c | 2 +- src/xrt/state_trackers/oxr/oxr_vulkan.c | 5 ++++- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/xrt/state_trackers/oxr/oxr_api_system.c b/src/xrt/state_trackers/oxr/oxr_api_system.c index 96a7a800a..ef3f24e5f 100644 --- a/src/xrt/state_trackers/oxr/oxr_api_system.c +++ b/src/xrt/state_trackers/oxr/oxr_api_system.c @@ -403,10 +403,10 @@ oxr_xrCreateVulkanDeviceKHR(XrInstance instance, // VK_NULL_HANDLE is 0 OXR_VERIFY_ARG_NOT_NULL(&log, createInfo->vulkanPhysicalDevice); - OXR_VERIFY_ARG_NOT_NULL(&log, sys->vulkan_enable2_physical_device); + OXR_VERIFY_ARG_NOT_NULL(&log, sys->suggested_vulkan_physical_device); OXR_VERIFY_ARG_NOT_NULL(&log, sys->vulkan_enable2_instance); - if (sys->vulkan_enable2_physical_device != createInfo->vulkanPhysicalDevice) { + if (sys->suggested_vulkan_physical_device != createInfo->vulkanPhysicalDevice) { return oxr_error(&log, XR_ERROR_HANDLE_INVALID, "createInfo->vulkanPhysicalDevice must be the device " "returned by xrGetVulkanGraphicsDeviceKHR"); diff --git a/src/xrt/state_trackers/oxr/oxr_objects.h b/src/xrt/state_trackers/oxr/oxr_objects.h index 036887ece..914fad391 100644 --- a/src/xrt/state_trackers/oxr/oxr_objects.h +++ b/src/xrt/state_trackers/oxr/oxr_objects.h @@ -1095,7 +1095,9 @@ struct oxr_system #ifdef XR_USE_GRAPHICS_API_VULKAN //! The instance/device we create when vulkan_enable2 is used VkInstance vulkan_enable2_instance; - VkPhysicalDevice vulkan_enable2_physical_device; + //! The device returned with the last xrGetVulkanGraphicsDeviceKHR or xrGetVulkanGraphicsDevice2KHR call. + //! XR_NULL_HANDLE if neither has been called. + VkPhysicalDevice suggested_vulkan_physical_device; #endif }; diff --git a/src/xrt/state_trackers/oxr/oxr_session.c b/src/xrt/state_trackers/oxr/oxr_session.c index aafdb67c5..53f4eeaa9 100644 --- a/src/xrt/state_trackers/oxr/oxr_session.c +++ b/src/xrt/state_trackers/oxr/oxr_session.c @@ -2048,6 +2048,21 @@ oxr_session_create_impl(struct oxr_logger *log, "xrGetVulkanGraphicsRequirementsKHR"); } + if (sys->suggested_vulkan_physical_device == VK_NULL_HANDLE) { + char *fn = sys->inst->extensions.KHR_vulkan_enable ? "xrGetVulkanGraphicsDeviceKHR" + : "xrGetVulkanGraphicsDevice2KHR"; + return oxr_error(log, XR_ERROR_VALIDATION_FAILURE, "Has not called %s", fn); + } + + if (sys->suggested_vulkan_physical_device != vulkan->physicalDevice) { + char *fn = sys->inst->extensions.KHR_vulkan_enable ? "xrGetVulkanGraphicsDeviceKHR" + : "xrGetVulkanGraphicsDevice2KHR"; + return oxr_error( + log, XR_ERROR_VALIDATION_FAILURE, + "XrGraphicsBindingVulkanKHR::physicalDevice %p must match device %p specified by %s", + (void *)vulkan->physicalDevice, (void *)sys->suggested_vulkan_physical_device, fn); + } + OXR_SESSION_ALLOCATE(log, sys, *out_session); OXR_ALLOCATE_NATIVE_COMPOSITOR(log, xsi, *out_session); return oxr_session_populate_vk(log, sys, vulkan, *out_session); diff --git a/src/xrt/state_trackers/oxr/oxr_system.c b/src/xrt/state_trackers/oxr/oxr_system.c index 1b14a1525..79f314dde 100644 --- a/src/xrt/state_trackers/oxr/oxr_system.c +++ b/src/xrt/state_trackers/oxr/oxr_system.c @@ -101,7 +101,7 @@ oxr_system_fill_in(struct oxr_logger *log, struct oxr_instance *inst, XrSystemId #ifdef XR_USE_GRAPHICS_API_VULKAN sys->vulkan_enable2_instance = VK_NULL_HANDLE; - sys->vulkan_enable2_physical_device = VK_NULL_HANDLE; + sys->suggested_vulkan_physical_device = VK_NULL_HANDLE; #endif // Headless. diff --git a/src/xrt/state_trackers/oxr/oxr_vulkan.c b/src/xrt/state_trackers/oxr/oxr_vulkan.c index 6cc93d0f7..4d6498543 100644 --- a/src/xrt/state_trackers/oxr/oxr_vulkan.c +++ b/src/xrt/state_trackers/oxr/oxr_vulkan.c @@ -369,7 +369,10 @@ oxr_vk_get_physical_device(struct oxr_logger *log, // vulkan_enable2 needs the physical device in xrCreateVulkanDeviceKHR if (inst->extensions.KHR_vulkan_enable2) { sys->vulkan_enable2_instance = vkInstance; - sys->vulkan_enable2_physical_device = *vkPhysicalDevice; + } + sys->suggested_vulkan_physical_device = *vkPhysicalDevice; + if (ll <= U_LOGGING_DEBUG) { + oxr_log(log, "Suggesting vulkan physical device %p", (void *)*vkPhysicalDevice); } free(phys);