Skyscraper 2.0
XrEnumerate.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
6#include <openxr/openxr.h>
7#include <vector>
8#include <set>
9#include "XrError.h"
10
11namespace xr {
12 template <typename TArray, typename TValue>
13 inline bool Contains(const TArray& array, const TValue& value) {
14 return std::end(array) != std::find(std::begin(array), std::end(array), value);
15 }
16
17 inline std::vector<XrExtensionProperties> EnumerateInstanceExtensionProperties(const char* layerName = nullptr) {
18 uint32_t extensionCount;
19 CHECK_XRCMD(xrEnumerateInstanceExtensionProperties(layerName, 0, &extensionCount, nullptr));
20 std::vector<XrExtensionProperties> extensionProperties(extensionCount, {XR_TYPE_EXTENSION_PROPERTIES});
21 CHECK_XRCMD(xrEnumerateInstanceExtensionProperties(layerName, extensionCount, &extensionCount, extensionProperties.data()));
22 extensionProperties.resize(extensionCount);
23 return extensionProperties;
24 }
25
26 inline std::vector<XrViewConfigurationType> EnumerateViewConfigurations(XrInstance instance, XrSystemId systemId) {
27 uint32_t viewConfigurationCount = 0;
28 CHECK_XRCMD(xrEnumerateViewConfigurations(instance, systemId, 0, &viewConfigurationCount, nullptr));
29
30 std::vector<XrViewConfigurationType> viewConfigs(viewConfigurationCount);
31 CHECK_XRCMD(xrEnumerateViewConfigurations(instance, systemId, viewConfigurationCount, &viewConfigurationCount, viewConfigs.data()));
32 return viewConfigs;
33 }
34
35 inline std::vector<XrViewConfigurationView> EnumerateViewConfigurationViews(XrInstance instance,
36 XrSystemId systemId,
37 XrViewConfigurationType viewConfigurationType) {
38 uint32_t viewCount;
39 CHECK_XRCMD(xrEnumerateViewConfigurationViews(instance, systemId, viewConfigurationType, 0, &viewCount, nullptr));
40
41 std::vector<XrViewConfigurationView> viewConfigViews(viewCount, {XR_TYPE_VIEW_CONFIGURATION_VIEW});
42 CHECK_XRCMD(xrEnumerateViewConfigurationViews(
43 instance, systemId, viewConfigurationType, (uint32_t)viewConfigViews.size(), &viewCount, viewConfigViews.data()));
44
45 return viewConfigViews;
46 }
47
48 inline std::vector<XrEnvironmentBlendMode> EnumerateEnvironmentBlendModes(XrInstance instance,
49 XrSystemId systemId,
50 XrViewConfigurationType viewConfigType) {
51 uint32_t blendModeCount;
52 CHECK_XRCMD(xrEnumerateEnvironmentBlendModes(instance, systemId, viewConfigType, 0, &blendModeCount, nullptr));
53
54 std::vector<XrEnvironmentBlendMode> blendModes(blendModeCount);
56 xrEnumerateEnvironmentBlendModes(instance, systemId, viewConfigType, blendModeCount, &blendModeCount, blendModes.data()));
57
58 return blendModes;
59 }
60
61 // Pick the first supported EnvironmentBlendMode from runtime's supported list.
62 inline XrEnvironmentBlendMode PickEnvironmentBlendMode(const std::vector<XrEnvironmentBlendMode>& systemSupportedBlendModes,
63 const std::vector<XrEnvironmentBlendMode>& appSupportedBlendModes) {
64 auto blendModeIt = std::find_first_of(systemSupportedBlendModes.begin(),
65 systemSupportedBlendModes.end(),
66 appSupportedBlendModes.begin(),
67 appSupportedBlendModes.end());
68 if (blendModeIt == std::end(systemSupportedBlendModes)) {
69 throw std::runtime_error("No blend modes supported");
70 }
71
72 return *blendModeIt;
73 }
74
75 inline std::vector<int64_t> EnumerateSwapchainFormats(XrSession session) {
76 uint32_t swapchainFormatsCount;
77 CHECK_XRCMD(xrEnumerateSwapchainFormats(session, 0, &swapchainFormatsCount, nullptr));
78
79 std::vector<int64_t> runtimeFormats(swapchainFormatsCount);
80 CHECK_XRCMD(xrEnumerateSwapchainFormats(session, (uint32_t)runtimeFormats.size(), &swapchainFormatsCount, runtimeFormats.data()));
81 return runtimeFormats;
82 }
83
84 // Pick the first supported swapchain format from runtime's supported format list.
85 template <typename TSystemPixelFormat, typename TAppPixelFormat>
86 inline TAppPixelFormat PickSwapchainFormat(const std::vector<TSystemPixelFormat>& systemSupportedFormats,
87 const std::vector<TAppPixelFormat>& appSupportedFormats) {
88 // Here we prioritize Runtime format preference over App format preference
89 auto swapchainFormatIt = std::find_first_of(
90 systemSupportedFormats.begin(), systemSupportedFormats.end(), appSupportedFormats.begin(), appSupportedFormats.end());
91
92 if (swapchainFormatIt == std::end(systemSupportedFormats)) {
93 throw std::runtime_error("No swapchain format supported");
94 }
95
96 return (TAppPixelFormat)*swapchainFormatIt;
97 }
98
99 inline std::vector<XrReferenceSpaceType> EnumerateReferenceSpaceTypes(XrSession session) {
100 uint32_t spaceCountOutput;
101 CHECK_XRCMD(xrEnumerateReferenceSpaces(session, 0, &spaceCountOutput, nullptr));
102
103 std::vector<XrReferenceSpaceType> runtimeSupportedReferenceSpaces(spaceCountOutput);
104 CHECK_XRCMD(xrEnumerateReferenceSpaces(
105 session, (uint32_t)runtimeSupportedReferenceSpaces.size(), &spaceCountOutput, runtimeSupportedReferenceSpaces.data()));
106 return runtimeSupportedReferenceSpaces;
107 }
108
109 inline std::set<std::string> QueryActionLocalizedName(XrSession session, XrAction action, XrInputSourceLocalizedNameFlags flags) {
110 std::set<std::string> names;
111 uint32_t nameCapacityOutput = 0;
112 uint32_t sourcesCount;
113
114 XrBoundSourcesForActionEnumerateInfo enumerateBoundSourcesInfo{XR_TYPE_BOUND_SOURCES_FOR_ACTION_ENUMERATE_INFO};
115 enumerateBoundSourcesInfo.action = action;
116 CHECK_XRCMD(xrEnumerateBoundSourcesForAction(session, &enumerateBoundSourcesInfo, 0, &sourcesCount, nullptr));
117
118 std::vector<XrPath> sourcesBuffer;
119 sourcesBuffer.resize(sourcesCount);
121 xrEnumerateBoundSourcesForAction(session, &enumerateBoundSourcesInfo, sourcesCount, &sourcesCount, sourcesBuffer.data()));
122
123 std::string nameBuffer;
124 for (uint32_t i = 0; i < sourcesCount; i++) {
125 XrInputSourceLocalizedNameGetInfo getLocalizedNameInfo{XR_TYPE_INPUT_SOURCE_LOCALIZED_NAME_GET_INFO};
126 getLocalizedNameInfo.sourcePath = sourcesBuffer[i];
127 getLocalizedNameInfo.whichComponents = flags;
128
129 CHECK_XRCMD(xrGetInputSourceLocalizedName(session, &getLocalizedNameInfo, 0, &nameCapacityOutput, nullptr));
130
131 nameBuffer.resize(nameCapacityOutput);
133 xrGetInputSourceLocalizedName(session, &getLocalizedNameInfo, nameCapacityOutput, &nameCapacityOutput, nameBuffer.data()));
134
135 // Some names are duplicated due to binding to the same input component and the left and right controllers
136 names.emplace(nameBuffer.data());
137 }
138
139 return names;
140 };
141
142} // namespace xr
#define CHECK_XRCMD(cmd)
Definition XrError.h:14
The xr::DispatchTable struct contains all available PFN pointers to xr functions including those in a...
std::set< std::string > QueryActionLocalizedName(XrSession session, XrAction action, XrInputSourceLocalizedNameFlags flags)
bool Contains(const TArray &array, const TValue &value)
Definition XrEnumerate.h:13
std::vector< int64_t > EnumerateSwapchainFormats(XrSession session)
Definition XrEnumerate.h:75
std::vector< XrEnvironmentBlendMode > EnumerateEnvironmentBlendModes(XrInstance instance, XrSystemId systemId, XrViewConfigurationType viewConfigType)
Definition XrEnumerate.h:48
std::vector< XrExtensionProperties > EnumerateInstanceExtensionProperties(const char *layerName=nullptr)
Definition XrEnumerate.h:17
std::vector< XrViewConfigurationView > EnumerateViewConfigurationViews(XrInstance instance, XrSystemId systemId, XrViewConfigurationType viewConfigurationType)
Definition XrEnumerate.h:35
XrEnvironmentBlendMode PickEnvironmentBlendMode(const std::vector< XrEnvironmentBlendMode > &systemSupportedBlendModes, const std::vector< XrEnvironmentBlendMode > &appSupportedBlendModes)
Definition XrEnumerate.h:62
std::vector< XrViewConfigurationType > EnumerateViewConfigurations(XrInstance instance, XrSystemId systemId)
Definition XrEnumerate.h:26
std::vector< XrReferenceSpaceType > EnumerateReferenceSpaceTypes(XrSession session)
Definition XrEnumerate.h:99
TAppPixelFormat PickSwapchainFormat(const std::vector< TSystemPixelFormat > &systemSupportedFormats, const std::vector< TAppPixelFormat > &appSupportedFormats)
Definition XrEnumerate.h:86