Skip to content

Commit

Permalink
OXR infra
Browse files Browse the repository at this point in the history
  • Loading branch information
shg8 committed Apr 2, 2024
1 parent 00f4257 commit 620c2d4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
51 changes: 45 additions & 6 deletions apps/vr_viewer/src/OXRContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -22,8 +18,8 @@ void OXRContext::setupOXR() {
createInstance();
createSystem();
setupViews();
getRequiredVulkanInstanceExtensions();
getRequiredVulkanDeviceExtensions();
std::ignore = getRequiredVulkanInstanceExtensions();
std::ignore = getRequiredVulkanDeviceExtensions();
}

void OXRContext::createInstance() {
Expand Down Expand Up @@ -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>(vkInstance);
graphicsBinding.physicalDevice = static_cast<VkPhysicalDevice>(vkPhysicalDevice);
graphicsBinding.device = static_cast<VkDevice>(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");
}
}
16 changes: 12 additions & 4 deletions apps/vr_viewer/src/OXRContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
9 changes: 9 additions & 0 deletions apps/vr_viewer/src/VRViewer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "VRViewer.h"

#include <memory>
#include <openxr/openxr_platform.h>

#include "OXRContext.h"
#include "3dgs.h"
#include "spdlog/spdlog.h"

void VRViewer::run() {
context = std::make_shared<OXRContext>();
Expand All @@ -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();
}

3 changes: 3 additions & 0 deletions apps/vr_viewer/src/VRViewer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OPENXRVIEWER_H
#define OPENXRVIEWER_H
#include <args.hxx>
#include <barrier>
#include <memory>
#include <utility>

Expand All @@ -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<OXRContext> context = nullptr;
Expand Down

0 comments on commit 620c2d4

Please sign in to comment.