VTK  9.4.20250509
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
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 }
48
50 {
51 DebugOutput = 0,
52 WarningOutput = 1,
53 ErrorOutput = 2
54 };
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 }
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 }
173
175
180 bool IsDepthExtensionSupported() { return this->OptionalExtensions.DepthExtensionSupported; }
182
189 {
190 return this->OptionalExtensions.SceneUnderstandingSupported;
191 }
192
194
199 bool GetShouldRenderCurrentFrame() { return this->ShouldRenderCurrentFrame; }
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
360 {
361 Inactive = -1,
362 Left = 0,
363 Right = 1,
364 Head = 2,
365 Generic = 3,
366 NumberOfControllers = 4
367 };
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;
380 } States[ControllerIndex::NumberOfControllers];
381
382 XrSpace PoseSpaces[ControllerIndex::NumberOfControllers];
383 XrSpaceLocation PoseLocations[ControllerIndex::NumberOfControllers];
384 XrSpaceVelocity PoseVelocities[ControllerIndex::NumberOfControllers];
385 };
386
388
391 void SetGraphicsStrategy(vtkOpenXRManagerGraphics* gs) { this->GraphicsStrategy = gs; }
392 vtkOpenXRManagerGraphics* GetGraphicsStrategy() { return this->GraphicsStrategy; }
394
396
399 void SetConnectionStrategy(vtkOpenXRManagerConnection* cs) { this->ConnectionStrategy = cs; }
400 vtkOpenXRManagerConnection* GetConnectionStrategy() { return this->ConnectionStrategy; }
402
404 "Use vtkOpenXRRenderWindow::SetUseDepthExtension instead. This has no effect!")
405 void SetUseDepthExtension(bool) {}
407 "Use vtkOpenXRRenderWindow::GetUseDepthExtension instead. This returns false!")
408 bool GetUseDepthExtension() const { return false; }
409
413 XrSystemId GetSystemID() const { return this->SystemId; }
414
418 XrSpace GetReferenceSpace() const { return this->ReferenceSpace; }
419
425 XrTime GetPredictedDisplayTime() const { return this->PredictedDisplayTime; }
426
427protected:
429 ~vtkOpenXRManager() = default;
430
432
438 std::vector<const char*> SelectExtensions(vtkOpenXRRenderWindow* window);
440
442
447
449
454
456
462
464
471
473
479
481
487
489
495 std::tuple<int64_t, int64_t> SelectSwapchainPixelFormats();
497
498 struct Swapchain_t;
499
501
505 Swapchain_t CreateSwapchain(int64_t format, uint32_t width, uint32_t height, uint32_t sampleCount,
506 XrSwapchainCreateFlags createFlags, XrSwapchainUsageFlags usageFlags);
508
510
515
517
519
522 bool CreateOneActionSpace(const XrAction& action, const XrPath& subactionPath,
523 const XrPosef& poseInActionSpace, XrSpace& space);
525
527
532
534
538 uint32_t WaitAndAcquireSwapchainImage(const XrSwapchain& swapchainHandle);
540
541 // Currently VTK only supports HeadMountedDisplay (HMD)
542 constexpr static XrFormFactor FormFactor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
543
544 // Pick the view type to be stereo rather than mono or anything else
545 constexpr static XrViewConfigurationType ViewType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
546
547 // PRIMARY_STEREO view configuration always has 2 views
548 constexpr static uint32_t StereoViewCount = 2;
549
550 // Three available types: VIEW, LOCAL and STAGE. We use LOCAL space which
551 // establishes a world-locked origin, rather than VIEW space, which tracks the
552 // view origin.
553 XrReferenceSpaceType ReferenceSpaceType = XR_REFERENCE_SPACE_TYPE_STAGE;
554
555 // Communication with the runtime happens through this instance
556 XrInstance Instance;
557
558 // A system is defined by an id and is used to create a session
559 XrSystemId SystemId;
560
561 XrSession Session;
562 XrSessionState SessionState;
564
565 // At the end of a frame, we must select en environment blend mode
566 // to tell the runtime how we want to blend the image with the user's
567 // view of the physical world. For example, in VR, we will generally
568 // choose XR_ENVIRONMENT_BLEND_MODE_OPAQUE while AR will generally
569 // choose XR_ENVIRONMENT_BLEND_MODE_ADDITIVE or XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND
570 XrEnvironmentBlendMode EnvironmentBlendMode;
571
572 // Non optional extension
573 bool RenderingBackendExtensionSupported = false;
574
576
580 struct
581 {
582 bool DepthExtensionSupported{ false };
583 bool ControllerModelExtensionSupported{ false };
584 bool UnboundedRefSpaceSupported{ false };
585 bool SpatialAnchorSupported{ false };
586 bool HandInteractionSupported{ false };
587 bool HandTrackingSupported{ false };
588 bool RemotingSupported{ false };
589 bool SceneUnderstandingSupported{ false };
590 bool SceneMarkerSupported{ false };
591 } OptionalExtensions;
593
599 {
600 XrSwapchain Swapchain;
601 int64_t Format{ 0 };
602 uint32_t Width{ 0 };
603 uint32_t Height{ 0 };
604 };
605
607
614 {
615 XrViewState ViewState{ XR_TYPE_VIEW_STATE };
616 // Each physical Display/Eye is described by a view
617 std::vector<XrView> Views;
618 // One configuration view per view : this store
619 std::vector<XrViewConfigurationView> ConfigViews;
620
621 std::vector<Swapchain_t> ColorSwapchains;
622 std::vector<Swapchain_t> DepthSwapchains;
623
624 std::vector<XrCompositionLayerProjectionView> ProjectionLayerViews;
625 std::vector<XrCompositionLayerDepthInfoKHR> DepthInfoViews;
626 };
627 std::unique_ptr<RenderResources_t> RenderResources;
629
630 // There is one subaction path for each hand.
631 std::array<XrPath, 2> SubactionPaths;
632
633 std::vector<XrActionSet> ActionSets;
634 XrActionSet* ActiveActionSet = nullptr;
635
641
642 bool SessionRunning = false;
643 // Following each WaitAndBeginFrame operation, the OpenXR runtime may indicate
644 // whether the current frame should be rendered using the `XrFrameState.shouldRender`
645 // property. We store this information to optimize rendering and prevent unnecessary
646 // render calls. For further details, refer to:
647 // https://registry.khronos.org/OpenXR/specs/1.0/man/html/XrFrameState.html
648 bool ShouldRenderCurrentFrame = false;
649 // If true, the function UpdateActionData will store
650 // pose velocities for pose actions
651 bool StorePoseVelocities = false;
652
654
656
657private:
658 vtkOpenXRManager(const vtkOpenXRManager&) = delete;
659 void operator=(const vtkOpenXRManager&) = delete;
660};
661
662VTK_ABI_NAMESPACE_END
663#endif
664// VTK-HeaderTest-Exclude: vtkOpenXRManager.h
OpenGL rendering window.
OpenXR manager connection no-op implementation.
OpenXR manager graphics implementation.
Singleton class that holds a collection of utility functions and member variables to communicate with...
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.
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
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 ...
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 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.
bool PrintReferenceSpaces()
Utility functions to print information about OpenXR manager internal structures.
std::unique_ptr< RenderResources_t > RenderResources
std::vector< const char * > SelectExtensions(vtkOpenXRRenderWindow *window)
OpenXR Instance creation.
vtkSmartPointer< vtkOpenXRManagerGraphics > GraphicsStrategy
OpenXR rendering window.
Hold a reference to a vtkObjectBase instance.
XrActionStateVector2f _vec2f
XrActionStateBoolean _boolean
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.
#define VTK_DEPRECATED_IN_9_5_0(reason)
Defines the OpenXR types and extensions common to all platforms.