Skip to content

Commit c89031e

Browse files
openxr extensions deferred init (#1258)
Co-authored-by: Gary Hsu <[email protected]>
1 parent bd7a176 commit c89031e

File tree

5 files changed

+149
-67
lines changed

5 files changed

+149
-67
lines changed

Dependencies/xr/Source/OpenXR/SceneUnderstanding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct SceneUnderstanding::SceneUnderstanding::Impl
8080

8181
void Initialize(const InitOptions& options)
8282
{
83-
if (options.Extensions.SceneUnderstandingSupported)
83+
if (options.Extensions.SceneUnderstandingSupported())
8484
{
8585
m_updateInterval = static_cast<XrTime>(NANOSECONDS_IN_SECOND * options.UpdateIntervalInSeconds);
8686

@@ -215,7 +215,7 @@ struct SceneUnderstanding::SceneUnderstanding::Impl
215215

216216
void Enable(const InitOptions& options)
217217
{
218-
if (!options.Extensions.SceneUnderstandingSupported)
218+
if (!options.Extensions.SceneUnderstandingSupported())
219219
{
220220
return;
221221
}

Dependencies/xr/Source/OpenXR/XR.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,21 @@ namespace xr
3535
XrSessionContext::Impl()
3636
: Extensions(std::make_unique<XrSupportedExtensions>()) {}
3737

38+
void InitializeExtensions()
39+
{
40+
if (Instance.Get() == XR_NULL_HANDLE)
41+
{
42+
throw std::runtime_error{ "Attempted to initialize extensions when instance was null" };
43+
}
44+
Extensions->Initialize();
45+
}
46+
3847
void PopulateExtensions()
3948
{
4049
if (Instance.Get() == XR_NULL_HANDLE)
4150
{
4251
throw std::runtime_error{ "Attempted to populate extensions when instance was null" };
4352
}
44-
4553
Extensions->PopulateDispatchTable(Instance.Get());
4654
}
4755

@@ -210,12 +218,13 @@ namespace xr
210218
// Phase one of initialization. Cannot fail without crashing.
211219
void InitializeXrInstanceAndExtensions()
212220
{
221+
Context.ContextImpl->InitializeExtensions();
213222
auto& extensions = Context.ContextImpl->Extensions;
214223
auto& instanceHandle = Context.ContextImpl->Instance;
215224

216225
XrInstanceCreateInfo createInfo{ XR_TYPE_INSTANCE_CREATE_INFO };
217-
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions->Names.size());
218-
createInfo.enabledExtensionNames = extensions->Names.data();
226+
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions->Names().size());
227+
createInfo.enabledExtensionNames = extensions->Names().data();
219228
createInfo.applicationInfo = { "", 1, "BabylonNative", 1, XR_CURRENT_API_VERSION };
220229
strcpy_s(createInfo.applicationInfo.applicationName, ApplicationName.c_str());
221230
XrCheck(xrCreateInstance(&createInfo, instanceHandle.Put()));
@@ -344,7 +353,7 @@ namespace xr
344353
XrCheck(xrCreateSession(instance, &createInfo, sessionHandle.Put()));
345354

346355
// Initialize scene space
347-
if (extensions->UnboundedRefSpaceSupported)
356+
if (extensions->UnboundedRefSpaceSupported())
348357
{
349358
sceneSpaceType = XR_REFERENCE_SPACE_TYPE_UNBOUNDED_MSFT;
350359
}
@@ -441,7 +450,7 @@ namespace xr
441450
const auto& apiExtensions = *HmdImpl.Context.Extensions();
442451
const auto& sceneSpace = HmdImpl.Context.Space();
443452

444-
if (!apiExtensions.SpatialAnchorSupported)
453+
if (!apiExtensions.SpatialAnchorSupported())
445454
{
446455
throw std::runtime_error("Spatial anchors are not supported for this device.");
447456
}
@@ -578,7 +587,7 @@ namespace xr
578587
{
579588
XR_TYPE_SECONDARY_VIEW_CONFIGURATION_SWAPCHAIN_CREATE_INFO_MSFT
580589
};
581-
if (HmdImpl.Context.Extensions()->SecondaryViewConfigurationSupported &&
590+
if (HmdImpl.Context.Extensions()->SecondaryViewConfigurationSupported() &&
582591
viewConfigType == XR_VIEW_CONFIGURATION_TYPE_SECONDARY_MONO_FIRST_PERSON_OBSERVER_MSFT)
583592
{
584593
secondaryViewConfigCreateInfo.viewConfigurationType = viewConfigType;
@@ -654,8 +663,8 @@ namespace xr
654663
XR_TYPE_SECONDARY_VIEW_CONFIGURATION_SESSION_BEGIN_INFO_MSFT
655664
};
656665
const auto& supportedSecondaryViewConfigTypes = HmdImpl.SupportedSecondaryViewConfigurationTypes;
657-
if (HmdImpl.Context.Extensions()->SecondaryViewConfigurationSupported &&
658-
HmdImpl.Context.Extensions()->FirstPersonObserverSupported &&
666+
if (HmdImpl.Context.Extensions()->SecondaryViewConfigurationSupported() &&
667+
HmdImpl.Context.Extensions()->FirstPersonObserverSupported() &&
659668
supportedSecondaryViewConfigTypes.size() > 0)
660669
{
661670
secondaryViewConfigSessionBeginInfo.viewConfigurationCount = static_cast<uint32_t>(supportedSecondaryViewConfigTypes.size());
@@ -755,7 +764,7 @@ namespace xr
755764
assert(viewCountOutput == renderResource.DepthSwapchain.ArraySize);
756765

757766
renderResource.ProjectionLayerViews.resize(viewCountOutput);
758-
if (context.Extensions()->DepthExtensionSupported)
767+
if (context.Extensions()->DepthExtensionSupported())
759768
{
760769
renderResource.DepthInfoViews.resize(viewCountOutput);
761770
}
@@ -983,7 +992,7 @@ namespace xr
983992
{
984993
const auto& context = sessionImpl.HmdImpl.Context;
985994
const auto& sceneSpace = context.Space();
986-
const auto depthSupported = context.Extensions()->DepthExtensionSupported;
995+
const auto depthSupported = context.Extensions()->DepthExtensionSupported();
987996

988997
uint32_t totalViewCount = 0;
989998
uint32_t primaryViewCount;
@@ -1085,7 +1094,7 @@ namespace xr
10851094
const auto& supportedSecondaryViewConfigTypes = m_impl->sessionImpl.HmdImpl.SupportedSecondaryViewConfigurationTypes;
10861095
std::vector<XrSecondaryViewConfigurationLayerInfoMSFT> activeSecondaryViewConfigLayerInfos;
10871096
XrSecondaryViewConfigurationFrameEndInfoMSFT frameEndSecondaryViewConfigInfo{ XR_TYPE_SECONDARY_VIEW_CONFIGURATION_FRAME_END_INFO_MSFT };
1088-
if (extensions->SecondaryViewConfigurationSupported && supportedSecondaryViewConfigTypes.size() > 0)
1097+
if (extensions->SecondaryViewConfigurationSupported() && supportedSecondaryViewConfigTypes.size() > 0)
10891098
{
10901099
activeSecondaryViewConfigLayerInfos.reserve(supportedSecondaryViewConfigTypes.size());
10911100
const auto& resourceMap = m_impl->sessionImpl.RenderResources.ResourceMap;
@@ -1151,7 +1160,7 @@ namespace xr
11511160
frameEndInfo.layerCount = 1;
11521161
frameEndInfo.layers = &layersPtr;
11531162

1154-
if (extensions->SecondaryViewConfigurationSupported &&
1163+
if (extensions->SecondaryViewConfigurationSupported() &&
11551164
activeSecondaryViewConfigLayerInfos.size() > 0)
11561165
{
11571166
for (size_t i = 0; i < activeSecondaryViewConfigLayerInfos.size(); i++)

Dependencies/xr/Source/OpenXR/XrInput.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ namespace xr
228228
SupportsEyeTracking = args.EyeGazeInteractionProps.supportsEyeGazeInteraction;
229229

230230
// Initialize the hand resources
231-
HandData.SupportsArticulatedHandTracking = args.HandTrackingInteractionProps.supportsHandTracking && args.Extensions.HandTrackingSupported;
231+
HandData.SupportsArticulatedHandTracking = args.HandTrackingInteractionProps.supportsHandTracking && args.Extensions.HandTrackingSupported();
232232
InitializeHandResources(args.Session, args.Extensions);
233233

234234
m_destroyHandTrackers = [this, extensions = args.Extensions]() {
@@ -348,7 +348,7 @@ namespace xr
348348
microsoftControllerBindings.push_back({ ActionResources.ControllerGetGripPoseAction });
349349
XrCheck(xrStringToPath(instance, path.data(), &microsoftControllerBindings.back().binding));
350350

351-
if (extensions.HandInteractionSupported)
351+
if (extensions.HandInteractionSupported())
352352
{
353353
microsoftHandBindings.push_back({ ActionResources.ControllerGetGripPoseAction });
354354
XrCheck(xrStringToPath(instance, path.data(), &microsoftHandBindings.back().binding));
@@ -385,7 +385,7 @@ namespace xr
385385
microsoftControllerBindings.push_back({ ActionResources.ControllerGetAimPoseAction });
386386
XrCheck(xrStringToPath(instance, path.data(), &microsoftControllerBindings.back().binding));
387387

388-
if (extensions.HandInteractionSupported)
388+
if (extensions.HandInteractionSupported())
389389
{
390390
microsoftHandBindings.push_back({ ActionResources.ControllerGetAimPoseAction });
391391
XrCheck(xrStringToPath(instance, path.data(), &microsoftHandBindings.back().binding));
@@ -512,7 +512,7 @@ namespace xr
512512
instance,
513513
idx);
514514

515-
if (extensions.HandInteractionSupported)
515+
if (extensions.HandInteractionSupported())
516516
{
517517
// Create action and suggested bindings specific to hands
518518
CreateControllerActionAndBinding(
@@ -551,7 +551,7 @@ namespace xr
551551
microsoftControllerSuggestedBindings.countSuggestedBindings = (uint32_t)microsoftControllerBindings.size();
552552
XrCheck(xrSuggestInteractionProfileBindings(instance, &microsoftControllerSuggestedBindings));
553553

554-
if (extensions.HandInteractionSupported)
554+
if (extensions.HandInteractionSupported())
555555
{
556556
// Provide Microsoft hand suggested binding to instance
557557
XrInteractionProfileSuggestedBinding microsoftHandSuggestedBindings{ XR_TYPE_INTERACTION_PROFILE_SUGGESTED_BINDING };
@@ -689,7 +689,7 @@ namespace xr
689689
else if (interactionProfilePath == actionResources.MicrosoftHandInteractionPath)
690690
{
691691
// Get hand interaction data
692-
if (args.Extensions.HandInteractionSupported)
692+
if (args.Extensions.HandInteractionSupported())
693693
{
694694
const auto& controllerInfo = ControllerInfo;
695695
auto& gamepadObject = inputSource.GamepadObject;

Dependencies/xr/Source/OpenXR/XrRegistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace xr
2525
bool OPENXR_CONTEXT_INTERFACE_API IsSessionRunning() const override;
2626
XrResult OPENXR_CONTEXT_INTERFACE_API GetInstanceProcAddr(const char* name, PFN_xrVoidFunction* function) const override;
2727

28-
const std::unique_ptr<XrSupportedExtensions>& XrSessionContext::Extensions() const;
28+
const std::unique_ptr<XrSupportedExtensions>& Extensions() const;
2929
const SceneUnderstanding& SceneUnderstanding() const;
3030

3131
struct Impl;

Dependencies/xr/Source/OpenXR/XrSupportedExtensions.h

Lines changed: 119 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,13 @@ namespace xr
88
struct XrSupportedExtensions : ExtensionDispatchTable
99
{
1010
XrSupportedExtensions()
11-
: Names{}
11+
: m_names{}
1212
{
13-
uint32_t extensionCount{};
14-
XrResult result{ xrEnumerateInstanceExtensionProperties(nullptr, 0, &extensionCount, nullptr) };
15-
if (result != XR_SUCCESS)
16-
{
17-
// Avoid failing if device doesn't support OpenXR
18-
return;
19-
}
20-
21-
m_extensionProperties.resize(extensionCount, { XR_TYPE_EXTENSION_PROPERTIES });
22-
XrCheck(xrEnumerateInstanceExtensionProperties(nullptr, extensionCount, &extensionCount, m_extensionProperties.data()));
23-
24-
// D3D11 extension is required, so check if it's supported.
25-
for (const char* extensionName : REQUIRED_EXTENSIONS)
26-
{
27-
if (!TryEnableExtension(extensionName))
28-
{
29-
throw std::runtime_error{ "Required extension not supported" };
30-
}
31-
}
32-
33-
// Additional optional extensions for enhanced functionality. Track whether enabled in m_optionalExtensions.
34-
DepthExtensionSupported = TryEnableExtension(XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME);
35-
UnboundedRefSpaceSupported = TryEnableExtension(XR_MSFT_UNBOUNDED_REFERENCE_SPACE_EXTENSION_NAME);
36-
SpatialAnchorSupported = TryEnableExtension(XR_MSFT_SPATIAL_ANCHOR_EXTENSION_NAME);
37-
SpatialAnchorInteropSupported = TryEnableExtension(XR_MSFT_PERCEPTION_ANCHOR_INTEROP_EXTENSION_NAME);
38-
SecondaryViewConfigurationSupported = TryEnableExtension(XR_MSFT_SECONDARY_VIEW_CONFIGURATION_EXTENSION_NAME);
39-
FirstPersonObserverSupported = TryEnableExtension(XR_MSFT_FIRST_PERSON_OBSERVER_EXTENSION_NAME);
40-
HandInteractionSupported = TryEnableExtension(XR_MSFT_HAND_INTERACTION_EXTENSION_NAME);
41-
HandTrackingSupported = TryEnableExtension(XR_EXT_HAND_TRACKING_EXTENSION_NAME);
42-
SceneUnderstandingSupported = TryEnableExtension(XR_MSFT_SCENE_UNDERSTANDING_EXTENSION_NAME);
43-
SceneUnderstandingSerializationSupported = TryEnableExtension(XR_MSFT_SCENE_UNDERSTANDING_SERIALIZATION_EXTENSION_NAME);
44-
EyeTrackingSupported = TryEnableExtension(XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME);
4513
}
4614

4715
bool TryEnableExtension(const char* extensionName)
4816
{
17+
assert(m_initialized);
4918
if (m_supportedExtensionNames.count(extensionName) > 0)
5019
{
5120
return true;
@@ -55,7 +24,7 @@ namespace xr
5524
{
5625
if (strcmp(extensionProperty.extensionName, extensionName) == 0)
5726
{
58-
Names.push_back(extensionName);
27+
m_names.push_back(extensionName);
5928
m_supportedExtensionNames.insert(extensionName);
6029
return true;
6130
}
@@ -65,25 +34,129 @@ namespace xr
6534

6635
bool IsExtensionSupported(const std::string& extensionName) const
6736
{
37+
assert(m_initialized);
6838
return m_supportedExtensionNames.count(extensionName) > 0;
6939
}
7040

71-
std::vector<const char*> Names{};
72-
bool DepthExtensionSupported{ false };
73-
bool UnboundedRefSpaceSupported{ false };
74-
bool SpatialAnchorSupported{ false };
75-
bool SpatialAnchorInteropSupported{ false };
76-
bool SecondaryViewConfigurationSupported{ false };
77-
bool FirstPersonObserverSupported{ false };
78-
bool HandInteractionSupported{ false };
79-
bool HandTrackingSupported{ false };
80-
bool SceneUnderstandingSupported{ false };
81-
bool SceneUnderstandingSerializationSupported{ false };
82-
bool EyeTrackingSupported{ false };
41+
bool DepthExtensionSupported() const
42+
{
43+
assert(m_initialized);
44+
return m_depthExtensionSupported;
45+
}
46+
bool UnboundedRefSpaceSupported() const
47+
{
48+
assert(m_initialized);
49+
return m_unboundedRefSpaceSupported;
50+
}
51+
bool SpatialAnchorSupported() const
52+
{
53+
assert(m_initialized);
54+
return m_spatialAnchorSupported;
55+
}
56+
bool SpatialAnchorInteropSupported() const
57+
{
58+
assert(m_initialized);
59+
return m_spatialAnchorInteropSupported;
60+
}
61+
bool SecondaryViewConfigurationSupported() const
62+
{
63+
assert(m_initialized);
64+
return m_secondaryViewConfigurationSupported;
65+
}
66+
bool FirstPersonObserverSupported() const
67+
{
68+
assert(m_initialized);
69+
return m_firstPersonObserverSupported;
70+
}
71+
bool HandInteractionSupported() const
72+
{
73+
assert(m_initialized);
74+
return m_handInteractionSupported;
75+
}
76+
bool HandTrackingSupported() const
77+
{
78+
assert(m_initialized);
79+
return m_handTrackingSupported;
80+
}
81+
bool SceneUnderstandingSupported() const
82+
{
83+
assert(m_initialized);
84+
return m_sceneUnderstandingSupported;
85+
}
86+
bool SceneUnderstandingSerializationSupported() const
87+
{
88+
assert(m_initialized);
89+
return m_sceneUnderstandingSerializationSupported;
90+
}
91+
bool EyeTrackingSupported() const
92+
{
93+
assert(m_initialized);
94+
return m_eyeTrackingSupported;
95+
}
96+
97+
void Initialize()
98+
{
99+
if (m_initialized)
100+
{
101+
return;
102+
}
103+
m_initialized = true;
104+
uint32_t extensionCount{};
105+
XrResult result{ xrEnumerateInstanceExtensionProperties(nullptr, 0, &extensionCount, nullptr) };
106+
if (result != XR_SUCCESS)
107+
{
108+
// Avoid failing if device doesn't support OpenXR
109+
return;
110+
}
83111

112+
m_extensionProperties.resize(extensionCount, { XR_TYPE_EXTENSION_PROPERTIES });
113+
XrCheck(xrEnumerateInstanceExtensionProperties(nullptr, extensionCount, &extensionCount, m_extensionProperties.data()));
114+
115+
// D3D11 extension is required, so check if it's supported.
116+
for (const char* extensionName : REQUIRED_EXTENSIONS)
117+
{
118+
if (!TryEnableExtension(extensionName))
119+
{
120+
throw std::runtime_error{ "Required extension not supported" };
121+
}
122+
}
123+
124+
// Additional optional extensions for enhanced functionality. Track whether enabled in m_optionalExtensions.
125+
m_depthExtensionSupported = TryEnableExtension(XR_KHR_COMPOSITION_LAYER_DEPTH_EXTENSION_NAME);
126+
m_unboundedRefSpaceSupported = TryEnableExtension(XR_MSFT_UNBOUNDED_REFERENCE_SPACE_EXTENSION_NAME);
127+
m_spatialAnchorSupported = TryEnableExtension(XR_MSFT_SPATIAL_ANCHOR_EXTENSION_NAME);
128+
m_spatialAnchorInteropSupported = TryEnableExtension(XR_MSFT_PERCEPTION_ANCHOR_INTEROP_EXTENSION_NAME);
129+
m_secondaryViewConfigurationSupported = TryEnableExtension(XR_MSFT_SECONDARY_VIEW_CONFIGURATION_EXTENSION_NAME);
130+
m_firstPersonObserverSupported = TryEnableExtension(XR_MSFT_FIRST_PERSON_OBSERVER_EXTENSION_NAME);
131+
m_handInteractionSupported = TryEnableExtension(XR_MSFT_HAND_INTERACTION_EXTENSION_NAME);
132+
m_handTrackingSupported = TryEnableExtension(XR_EXT_HAND_TRACKING_EXTENSION_NAME);
133+
m_sceneUnderstandingSupported = TryEnableExtension(XR_MSFT_SCENE_UNDERSTANDING_EXTENSION_NAME);
134+
m_sceneUnderstandingSerializationSupported = TryEnableExtension(XR_MSFT_SCENE_UNDERSTANDING_SERIALIZATION_EXTENSION_NAME);
135+
m_eyeTrackingSupported = TryEnableExtension(XR_EXT_EYE_GAZE_INTERACTION_EXTENSION_NAME);
136+
}
137+
const std::vector<const char*>& Names() const
138+
{
139+
assert(m_initialized);
140+
return m_names;
141+
}
84142
private:
143+
std::vector<const char*> m_names{};
85144
std::vector<XrExtensionProperties> m_extensionProperties{};
86145
std::unordered_set<std::string> m_supportedExtensionNames{};
146+
147+
bool m_depthExtensionSupported{ false };
148+
bool m_unboundedRefSpaceSupported{ false };
149+
bool m_spatialAnchorSupported{ false };
150+
bool m_spatialAnchorInteropSupported{ false };
151+
bool m_secondaryViewConfigurationSupported{ false };
152+
bool m_firstPersonObserverSupported{ false };
153+
bool m_handInteractionSupported{ false };
154+
bool m_handTrackingSupported{ false };
155+
bool m_sceneUnderstandingSupported{ false };
156+
bool m_sceneUnderstandingSerializationSupported{ false };
157+
bool m_eyeTrackingSupported{ false };
158+
159+
bool m_initialized{ false };
87160
};
88161
}
89162
#endif

0 commit comments

Comments
 (0)