VTK  9.6.20260201
vtkOpenXRManager.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
12
13#ifndef vtkOpenXRManager_h
14#define vtkOpenXRManager_h
15
16#include "vtkRenderingOpenXRModule.h" // needed for exports
17
18#include "vtkNew.h"
19#include "vtkOpenXR.h"
22#include "vtkSmartPointer.h"
23
24#include <array>
25#include <cstdint>
26#include <memory>
27#include <string>
28#include <vector>
29
30VTK_ABI_NAMESPACE_BEGIN
34
35class VTKRENDERINGOPENXR_EXPORT vtkOpenXRManager
36{
37public:
39
43 {
44 static vtkOpenXRManager UniqueInstance;
45 return UniqueInstance;
46 }
47
48
55
57
61 bool XrCheckOutput(OutputLevel level, const XrResult&, const std::string& message);
63
67 struct VTKRENDERINGOPENXR_EXPORT InstanceVersion
68 {
69 std::uint16_t Major{};
70 std::uint16_t Minor{};
71 std::uint32_t Patch{};
72 };
73
79
81
85 void PrintSystemProperties(XrSystemProperties* system_properties);
87 void PrintViewConfigViewInfo(const std::vector<XrViewConfigurationView>&);
90
92
101
103
108 void Finalize();
110
112
115 std::tuple<uint32_t, uint32_t> GetRecommendedImageRectSize();
117
119
124
128 uint32_t GetViewCount()
129 {
130 return static_cast<uint32_t>(this->RenderResources->ConfigViews.size());
131 }
132
134
140
142
148 const XrPosef* GetViewPose(uint32_t eye)
149 {
150 if (eye >= this->GetViewCount())
151 {
152 return nullptr;
153 }
154 return &(this->RenderResources->Views[eye].pose);
155 }
156
157
159
164 const XrFovf* GetProjectionFov(uint32_t eye)
165 {
166 if (eye >= this->GetViewCount())
167 {
168 return nullptr;
169 }
170 return &(this->RenderResources->Views[eye].fov);
171 }
172
173
175
180 bool IsDepthExtensionSupported() { return this->OptionalExtensions.DepthExtensionSupported; }
182
189 {
190 return this->OptionalExtensions.SceneUnderstandingSupported;
191 }
192
194
201
203
209
211
214 const XrSession& GetSession() { return this->Session; }
216
218
221 const XrInstance& GetXrRuntimeInstance() { return this->Instance; }
223
225
229 bool IsSessionRunning() { return this->SessionRunning; }
231
233
240
242
248 bool PrepareRendering(vtkOpenXRRenderWindow* win, void* colorTextureId, void* depthTextureId);
250
252
256 void ReleaseSwapchainImage(uint32_t eye);
258
260
264 bool EndFrame();
266
268
271 bool PollEvent(XrEventDataBuffer& eventData);
273
275
278 XrPath GetXrPath(const std::string& path);
280
281 const std::array<XrPath, 2>& GetSubactionPaths() { return this->SubactionPaths; }
282
284
287 bool CreateActionSet(const std::string& actionSetName, const std::string& localizedActionSetName);
289
291
295 bool SelectActiveActionSet(unsigned int index);
297
299
304
306
311
312 struct Action_t;
313
315
321 Action_t& actionT, const std::string& name, const std::string& localizedName);
323
325
329 const std::string& profile, std::vector<XrActionSuggestedBinding>& actionSuggestedBindings);
331
333
339
341
348 bool UpdateActionData(Action_t& action_t, int hand);
350
356 bool ApplyVibration(const Action_t& actionT, int hand, float amplitude = 0.5,
357 float duration = 25000000.0, float frequency = XR_FREQUENCY_UNSPECIFIED);
358
368
369 struct Action_t
370 {
371 XrAction Action;
372 XrActionType ActionType;
373
374 union
375 {
376 XrActionStateFloat _float;
377 XrActionStateBoolean _boolean;
378 XrActionStatePose _pose;
379 XrActionStateVector2f _vec2f;
381
385 };
386
388
394
396
402
406 XrSystemId GetSystemID() const { return this->SystemId; }
407
411 XrSpace GetReferenceSpace() const { return this->ReferenceSpace; }
412
418 XrTime GetPredictedDisplayTime() const { return this->PredictedDisplayTime; }
419
420protected:
422 ~vtkOpenXRManager() = default;
423
425
431 std::vector<const char*> SelectExtensions(vtkOpenXRRenderWindow* window);
433
435
440
442
447
449
455
457
464
466
472
474
480
482
488 std::tuple<int64_t, int64_t> SelectSwapchainPixelFormats();
490
491 struct Swapchain_t;
492
494
498 Swapchain_t CreateSwapchain(int64_t format, uint32_t width, uint32_t height, uint32_t sampleCount,
499 XrSwapchainCreateFlags createFlags, XrSwapchainUsageFlags usageFlags);
501
503
508
510
512
515 bool CreateOneActionSpace(const XrAction& action, const XrPath& subactionPath,
516 const XrPosef& poseInActionSpace, XrSpace& space);
518
520
525
527
531 uint32_t WaitAndAcquireSwapchainImage(const XrSwapchain& swapchainHandle);
533
534 // Currently VTK only supports HeadMountedDisplay (HMD)
535 constexpr static XrFormFactor FormFactor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
536
537 // Pick the view type to be stereo rather than mono or anything else
538 constexpr static XrViewConfigurationType ViewType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
539
540 // PRIMARY_STEREO view configuration always has 2 views
541 constexpr static uint32_t StereoViewCount = 2;
542
543 // Three available types: VIEW, LOCAL and STAGE. We use LOCAL space which
544 // establishes a world-locked origin, rather than VIEW space, which tracks the
545 // view origin.
546 XrReferenceSpaceType ReferenceSpaceType = XR_REFERENCE_SPACE_TYPE_STAGE;
547
548 // Communication with the runtime happens through this instance
549 XrInstance Instance;
550
551 // A system is defined by an id and is used to create a session
552 XrSystemId SystemId;
553
554 XrSession Session;
555 XrSessionState SessionState;
557
558 // At the end of a frame, we must select en environment blend mode
559 // to tell the runtime how we want to blend the image with the user's
560 // view of the physical world. For example, in VR, we will generally
561 // choose XR_ENVIRONMENT_BLEND_MODE_OPAQUE while AR will generally
562 // choose XR_ENVIRONMENT_BLEND_MODE_ADDITIVE or XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND
563 XrEnvironmentBlendMode EnvironmentBlendMode;
564
565 // Non optional extension
567
569
573 struct
574 {
581 bool RemotingSupported{ false };
583 bool SceneMarkerSupported{ false };
584 } OptionalExtensions;
586
592 {
593 XrSwapchain Swapchain;
594 int64_t Format{ 0 };
595 uint32_t Width{ 0 };
596 uint32_t Height{ 0 };
597 };
598
600
607 {
608 XrViewState ViewState{ XR_TYPE_VIEW_STATE };
609 // Each physical Display/Eye is described by a view
610 std::vector<XrView> Views;
611 // One configuration view per view : this store
612 std::vector<XrViewConfigurationView> ConfigViews;
613
614 std::vector<Swapchain_t> ColorSwapchains;
615 std::vector<Swapchain_t> DepthSwapchains;
616
617 std::vector<XrCompositionLayerProjectionView> ProjectionLayerViews;
618 std::vector<XrCompositionLayerDepthInfoKHR> DepthInfoViews;
619 };
620 std::unique_ptr<RenderResources_t> RenderResources;
622
623 // There is one subaction path for each hand.
624 std::array<XrPath, 2> SubactionPaths;
625
626 std::vector<XrActionSet> ActionSets;
627 XrActionSet* ActiveActionSet = nullptr;
628
634
635 bool SessionRunning = false;
636 // Following each WaitAndBeginFrame operation, the OpenXR runtime may indicate
637 // whether the current frame should be rendered using the `XrFrameState.shouldRender`
638 // property. We store this information to optimize rendering and prevent unnecessary
639 // render calls. For further details, refer to:
640 // https://registry.khronos.org/OpenXR/specs/1.0/man/html/XrFrameState.html
642 // If true, the function UpdateActionData will store
643 // pose velocities for pose actions
645
647
649
650private:
651 vtkOpenXRManager(const vtkOpenXRManager&) = delete;
652 void operator=(const vtkOpenXRManager&) = delete;
653};
654
655VTK_ABI_NAMESPACE_END
656#endif
657// VTK-HeaderTest-Exclude: vtkOpenXRManager.h
OpenGL rendering window.
OpenXR manager connection no-op implementation.
OpenXR manager graphics implementation.
XrSpace GetReferenceSpace() const
Return XrSpace associated with the XrSession.
const XrInstance & GetXrRuntimeInstance()
Return the instance used to communicate with the runtime.
void PrintViewConfigViewInfo(const std::vector< XrViewConfigurationView > &)
Utility functions to print information about OpenXR manager internal structures.
bool CreateOneActionSpace(const XrAction &action, const XrPath &subactionPath, const XrPosef &poseInActionSpace, XrSpace &space)
For pose actions, we must create an action space to locate it.
XrTime PredictedDisplayTime
Store the frame predicted display time in WaitAndBeginFrame To get the action data at this time and t...
bool BeginSession()
Start the OpenXR session.
void PrintOptionalExtensions()
Print the optional extensions which were found and enabled.
const XrPosef * GetViewPose(uint32_t eye)
Returns a pointer to the view pose that contains the view orientation and position for the specified ...
bool GetShouldRenderCurrentFrame()
Return true if the current frame should be rendered.
vtkOpenXRManagerGraphics * GetGraphicsStrategy()
Set/Get the rendering backend strategy.
void PrintInstanceProperties()
Utility functions to print information about OpenXR manager internal structures.
static InstanceVersion QueryInstanceVersion(vtkOpenXRManagerConnection *cs)
Utility function to get XrInstance runtime version for given ConnectionStrategy This function creates...
void PrintSystemProperties(XrSystemProperties *system_properties)
Utility functions to print information about OpenXR manager internal structures.
XrReferenceSpaceType ReferenceSpaceType
bool WaitAndBeginFrame()
This function is used to start a frame.
std::tuple< int64_t, int64_t > SelectSwapchainPixelFormats()
During the creation of the swapchains, we need to check the runtime available pixels formats,...
bool AttachSessionActionSets()
Attach all action sets in the ActionSets vector to the session.
bool CreateInstance(vtkOpenXRRenderWindow *window)
OpenXR Instance creation.
~vtkOpenXRManager()=default
struct vtkOpenXRManager::@342253300264113136100372334253042337262164244010 OptionalExtensions
Structure to hold optional extensions loaded with SelectExtensions.
XrSystemId GetSystemID() const
Return OpenXR System ID associated with the XrSession.
bool IsSessionRunning()
Return true if the OpenXR session is currently running, ie.
bool ApplyVibration(const Action_t &actionT, int hand, float amplitude=0.5, float duration=25000000.0, float frequency=XR_FREQUENCY_UNSPECIFIED)
Apply haptic vibration action to emit vibration on hand to emit on amplitude 0.0 to 1....
const XrFovf * GetProjectionFov(uint32_t eye)
Returns a pointer to the projection field of view for the specified eye, or nullptr if eye exceeds or...
bool CreateSystem()
OpenXR System creation.
bool CreateReferenceSpace()
Creates the reference space of type ReferenceSpaceType that will be used to locate views.
void SetGraphicsStrategy(vtkOpenXRManagerGraphics *gs)
Set/Get the rendering backend strategy.
bool IsDepthExtensionSupported()
Return true if the runtime supports the depth extension.
std::tuple< uint32_t, uint32_t > GetRecommendedImageRectSize()
Return as a tuple the OpenXR recommended texture size to be sent to the device.
uint32_t GetViewCount()
Return the number of OpenXR views (typically one per physical display / eye)
XrPath GetXrPath(const std::string &path)
Get the XrPath from the well-formed string path.
uint32_t WaitAndAcquireSwapchainImage(const XrSwapchain &swapchainHandle)
When preparing the rendering for an eye, we must ask the runtime for a texture to draw in it.
vtkSmartPointer< vtkOpenXRManagerConnection > ConnectionStrategy
XrTime GetPredictedDisplayTime() const
Return runtime predicted display time for next frame.
vtkOpenXRManagerConnection * GetConnectionStrategy()
Set/Get the connection strategy.
bool EndFrame()
Submit the composition layers for the predicted display time of the current frame.
bool PollEvent(XrEventDataBuffer &eventData)
Store in eventData the result of xrPollEvent.
static vtkOpenXRManager & GetInstance()
Return the singleton instance.
bool CreateOneAction(Action_t &actionT, const std::string &name, const std::string &localizedName)
Creates one action with name name and localizedName localizedName and store the action handle inside ...
XrActionSet * ActiveActionSet
uint32_t GetRecommendedSampleCount()
Return the recommended swapchain sample count.
std::array< XrPath, 2 > SubactionPaths
bool SyncActions()
Update the action states using the active action set.
bool CreateActionSet(const std::string &actionSetName, const std::string &localizedActionSetName)
Creates an action set and add it to the vector of action sets.
Swapchain_t CreateSwapchain(int64_t format, uint32_t width, uint32_t height, uint32_t sampleCount, XrSwapchainCreateFlags createFlags, XrSwapchainUsageFlags usageFlags)
Create an XrSwapchain handle used to present rendered image to the user with the given parameters for...
bool ControllerModelExtensionSupported
bool RenderingBackendExtensionSupported
bool CreateSystemProperties()
Enable system properties such as hand tracking, and choose environment blend modes.
std::string GetOpenXRPropertiesAsString()
Return the OpenXR properties as a string, with format "RuntimeName MAJOR.MINOR.PATCH".
void Finalize()
Internal API.
bool XrCheckOutput(OutputLevel level, const XrResult &, const std::string &message)
Utility function to check the XrResult, print the result message as a debug, warning or error message...
void SetConnectionStrategy(vtkOpenXRManagerConnection *cs)
Set/Get the connection strategy.
void ReleaseSwapchainImage(uint32_t eye)
When the rendering in a swapchain image is done, it must be released with this function.
bool SelectActiveActionSet(unsigned int index)
Selects the current active action set from the ActionSets vector using its index.
bool CreateConfigViews()
There is one configuration view per view, and it contains the recommended texture resolution in pixel...
XrSessionState SessionState
bool IsSceneUnderstandingSupported()
Return true if the runtime supports the scene understanding extension.
bool PrepareRendering(vtkOpenXRRenderWindow *win, void *colorTextureId, void *depthTextureId)
Prepare the rendering resources for the specified eye and store in colorTextureId and in depthTexture...
bool Initialize(vtkOpenXRRenderWindow *xrWindow)
Internal API.
bool CreateSwapchains()
Swapchaines creation : there is one swapchain per view / display.
void DestroyActionSets()
Iterate over and destroy all action sets that have been created.
bool CreateSession()
Create the session and pass the GraphicsBinding to the next pointer of the XrSessionCreateInfo.
std::vector< XrActionSet > ActionSets
const XrSession & GetSession()
Return the OpenXR Session.
void PrintSupportedViewConfigs()
Utility functions to print information about OpenXR manager internal structures.
const std::array< XrPath, 2 > & GetSubactionPaths()
XrEnvironmentBlendMode EnvironmentBlendMode
bool LoadControllerModels()
bool UpdateActionData(Action_t &action_t, int hand)
Update the action data and store it in action_t.States for one hand.
bool SuggestActions(const std::string &profile, std::vector< XrActionSuggestedBinding > &actionSuggestedBindings)
Suggest actions stored in actionSuggestedBindings for the interaction profile profile.
bool CreateSubactionPaths()
Creates one subaction path for each hand.
static constexpr XrFormFactor FormFactor
bool PrintReferenceSpaces()
Utility functions to print information about OpenXR manager internal structures.
std::unique_ptr< RenderResources_t > RenderResources
static constexpr uint32_t StereoViewCount
std::vector< const char * > SelectExtensions(vtkOpenXRRenderWindow *window)
OpenXR Instance creation.
static constexpr XrViewConfigurationType ViewType
vtkSmartPointer< vtkOpenXRManagerGraphics > GraphicsStrategy
OpenXR rendering window.
vtkOpenXRSceneObserver is a wrapper around OpenXR scene understanding extensions
Hold a reference to a vtkObjectBase instance.
XrSpace PoseSpaces[ControllerIndex::NumberOfControllers]
XrSpaceVelocity PoseVelocities[ControllerIndex::NumberOfControllers]
XrActionStateVector2f _vec2f
XrActionStateBoolean _boolean
XrSpaceLocation PoseLocations[ControllerIndex::NumberOfControllers]
Structure representing OpenXR instance version.
This struct stores all needed information to render the images and send it to the user We can't make ...
std::vector< Swapchain_t > ColorSwapchains
std::vector< XrViewConfigurationView > ConfigViews
std::vector< Swapchain_t > DepthSwapchains
std::vector< XrCompositionLayerProjectionView > ProjectionLayerViews
std::vector< XrCompositionLayerDepthInfoKHR > DepthInfoViews
Swapchain structure storing information common to all rendering backend.
Defines the OpenXR types and extensions common to all platforms.