Skyscraper 2.0
OgreOpenXRInstance.cpp
Go to the documentation of this file.
1/*
2 Skyscraper 2.0 - OpenXR Instance
3 Portions Copyright (C)2024 Ryan Thoryk
4 MIT license - see LICENSE file
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8*/
9
10/*
11 Original work produced by Glastonbridge Software Limited. This code is provided under the MIT license.
12 https://github.com/glastonbridge/OgreOpenXRRenderWindow
13*/
14
15#include "OgreOpenXRInstance.h"
16
17#include <vector>
18#include "XrUtility/XrError.h"
20
24
28
29
30std::vector<const char*> SelectExtensions();
31
32void Ogre::OpenXRInstance::Initialize(const std::string& applicationName)
33{
34 CHECK(m_instanceHandle.Get() == XR_NULL_HANDLE);
35
36 std::vector<const char*> enabledExtensions = SelectExtensions();
37
38 XrInstanceCreateInfo createInfo{ XR_TYPE_INSTANCE_CREATE_INFO };
39 createInfo.enabledExtensionCount = (uint32_t)enabledExtensions.size();
40 createInfo.enabledExtensionNames = enabledExtensions.data();
41 createInfo.applicationInfo.applicationVersion = 1;
42 createInfo.applicationInfo.engineName[0] = 0;
43 createInfo.applicationInfo.engineVersion = 1;
44 createInfo.applicationInfo.apiVersion = XR_API_VERSION_1_0;
45
46 strcpy_s(createInfo.applicationInfo.applicationName, applicationName.c_str());
47
48 CHECK_XRCMD(xrCreateInstance(&createInfo, m_instanceHandle.Put(xrDestroyInstance)));
49
50 xr::g_dispatchTable.Initialize(m_instanceHandle.Get(), xrGetInstanceProcAddr);
51}
52
54{
55 return m_instanceHandle;
56}
57
59{
60 return m_instanceHandle.Get() != XR_NULL_HANDLE;
61}
62
63std::vector<const char*> SelectExtensions()
64{
65 // Fetch the list of extensions supported by the runtime.
66 uint32_t extensionCount;
67 CHECK_XRCMD(xrEnumerateInstanceExtensionProperties(nullptr, 0, &extensionCount, nullptr));
68 std::vector<XrExtensionProperties> extensionProperties(extensionCount, { XR_TYPE_EXTENSION_PROPERTIES });
69 CHECK_XRCMD(xrEnumerateInstanceExtensionProperties(nullptr, extensionCount, &extensionCount, extensionProperties.data()));
70
71 std::vector<const char*> enabledExtensions;
72
73 // Add a specific extension to the list of extensions to be enabled, if it is supported.
74 auto EnableExtensionIfSupported = [&](const char* extensionName) {
75 for (uint32_t i = 0; i < extensionCount; i++) {
76 if (strcmp(extensionProperties[i].extensionName, extensionName) == 0) {
77 enabledExtensions.push_back(extensionName);
78 return true;
79 }
80 }
81 return false;
82 };
83
84 // D3D11 extension is required for this sample, so check if it's supported.
85 CHECK(EnableExtensionIfSupported(XR_KHR_D3D11_ENABLE_EXTENSION_NAME));
86
87 return enabledExtensions;
88}
std::vector< const char * > SelectExtensions()
#define CHECK(exp)
Definition XrError.h:59
#define CHECK_XRCMD(cmd)
Definition XrError.h:14
void Initialize(const std::string &applicationName)
const xr::InstanceHandle & getHandle()
HandleType Get() const noexcept
Definition XrHandle.h:51