From 620c2d40d9efb4595924cf9bda53ddd77fd33c5a Mon Sep 17 00:00:00 2001 From: shg8 Date: Mon, 1 Apr 2024 21:37:22 -0500 Subject: [PATCH] OXR infra --- apps/vr_viewer/src/OXRContext.cpp | 51 +++++++++++++++++++++++++++---- apps/vr_viewer/src/OXRContext.h | 16 +++++++--- apps/vr_viewer/src/VRViewer.cpp | 9 ++++++ apps/vr_viewer/src/VRViewer.h | 3 ++ 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/apps/vr_viewer/src/OXRContext.cpp b/apps/vr_viewer/src/OXRContext.cpp index ceba80a..73a6978 100644 --- a/apps/vr_viewer/src/OXRContext.cpp +++ b/apps/vr_viewer/src/OXRContext.cpp @@ -10,10 +10,6 @@ #include "spdlog/spdlog.h" void OXRContext::setup() { - setupOXR(); -} - -void OXRContext::setupOXR() { bool vulkanSupported = OXR::isExtensionSupported(XR_KHR_VULKAN_ENABLE_EXTENSION_NAME); if (!vulkanSupported) { throw std::runtime_error("Vulkan not supported"); @@ -22,8 +18,8 @@ void OXRContext::setupOXR() { createInstance(); createSystem(); setupViews(); - getRequiredVulkanInstanceExtensions(); - getRequiredVulkanDeviceExtensions(); + std::ignore = getRequiredVulkanInstanceExtensions(); + std::ignore = getRequiredVulkanDeviceExtensions(); } void OXRContext::createInstance() { @@ -180,3 +176,46 @@ void * OXRContext::getPhysicalDevice(void *instance) const { return physicalDevice; } + +void OXRContext::createSession(void *vkInstance, void *vkPhysicalDevice, void *vkDevice, uint32_t vkQueueFamilyIndex, + uint32_t vkQueueIndex) { + XrGraphicsBindingVulkanKHR graphicsBinding = {XR_TYPE_GRAPHICS_BINDING_VULKAN_KHR}; + graphicsBinding.instance = static_cast(vkInstance); + graphicsBinding.physicalDevice = static_cast(vkPhysicalDevice); + graphicsBinding.device = static_cast(vkDevice); + graphicsBinding.queueFamilyIndex = vkQueueFamilyIndex; + graphicsBinding.queueIndex = vkQueueIndex; + + XrSessionCreateInfo createInfo = {XR_TYPE_SESSION_CREATE_INFO}; + createInfo.next = &graphicsBinding; + createInfo.systemId = systemId; + + auto result = xrCreateSession(oxrInstance, &createInfo, &oxrSession); + if (XR_FAILED(result)) { + throw std::runtime_error("Failed to create OpenXR session"); + } + + spdlog::debug("Created OpenXR session"); +} + +void OXRContext::createReferenceSpace() { + XrPosef identityPose = {{0, 0, 0, 1}, {0, 0, 0}}; + XrReferenceSpaceCreateInfo createInfo = {XR_TYPE_REFERENCE_SPACE_CREATE_INFO}; + createInfo.poseInReferenceSpace = identityPose; + createInfo.referenceSpaceType = XR_REFERENCE_SPACE_TYPE_LOCAL; + + auto result = xrCreateReferenceSpace(oxrSession, &createInfo, &localSpace); + if (XR_FAILED(result)) { + throw std::runtime_error("Failed to create OpenXR reference space"); + } +} + +void OXRContext::beginSession() { + XrSessionBeginInfo beginInfo = {XR_TYPE_SESSION_BEGIN_INFO}; + beginInfo.primaryViewConfigurationType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO; + + auto result = xrBeginSession(oxrSession, &beginInfo); + if (XR_FAILED(result)) { + throw std::runtime_error("Failed to begin OpenXR session"); + } +} diff --git a/apps/vr_viewer/src/OXRContext.h b/apps/vr_viewer/src/OXRContext.h index 2cf1771..e54a686 100644 --- a/apps/vr_viewer/src/OXRContext.h +++ b/apps/vr_viewer/src/OXRContext.h @@ -11,12 +11,20 @@ class OXRContext { public: void setup(); void* getPhysicalDevice(void *instance) const; + + void createSession(void *vkInstance, void *vkPhysicalDevice, void *vkDevice, uint32_t vkQueueFamilyIndex, + uint32_t vkQueueIndex); + + void createReferenceSpace(); + + void beginSession(); + private: - XrInstance oxrInstance; - XrSystemId systemId; + XrInstance oxrInstance = XR_NULL_HANDLE; + XrSystemId systemId = XR_NULL_SYSTEM_ID; XrViewConfigurationView views[2] = {{XR_TYPE_VIEW_CONFIGURATION_VIEW}, {XR_TYPE_VIEW_CONFIGURATION_VIEW}}; - - void setupOXR(); + XrSession oxrSession = XR_NULL_HANDLE; + XrSpace localSpace = XR_NULL_HANDLE; void createInstance(); diff --git a/apps/vr_viewer/src/VRViewer.cpp b/apps/vr_viewer/src/VRViewer.cpp index 87b20fe..07b6532 100644 --- a/apps/vr_viewer/src/VRViewer.cpp +++ b/apps/vr_viewer/src/VRViewer.cpp @@ -1,9 +1,11 @@ #include "VRViewer.h" #include +#include #include "OXRContext.h" #include "3dgs.h" +#include "spdlog/spdlog.h" void VRViewer::run() { context = std::make_shared(); @@ -13,3 +15,10 @@ void VRViewer::run() { configuration.getPhysicalDevice = std::bind(&OXRContext::getPhysicalDevice, context.get(), std::placeholders::_1); } +void VRViewer::finishSetup(void *vkInstance, void *vkPhysicalDevice, void *vkDevice, uint32_t vkQueueFamilyIndex, + uint32_t vkQueueIndex) { + context->createSession(vkInstance, vkPhysicalDevice, vkDevice, vkQueueFamilyIndex, vkQueueIndex); + context->createReferenceSpace(); + context->beginSession(); +} + diff --git a/apps/vr_viewer/src/VRViewer.h b/apps/vr_viewer/src/VRViewer.h index ea2bac9..e388b2e 100644 --- a/apps/vr_viewer/src/VRViewer.h +++ b/apps/vr_viewer/src/VRViewer.h @@ -1,6 +1,7 @@ #ifndef OPENXRVIEWER_H #define OPENXRVIEWER_H #include +#include #include #include @@ -26,6 +27,8 @@ class VRViewer { void run(); + void finishSetup(void *vkInstance, void *vkPhysicalDevice, void *vkDevice, uint32_t vkQueueFamilyIndex, uint32_t vkQueueIndex); + private: Configuration config; std::shared_ptr context = nullptr;