VTK  9.6.20260428
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
14
15#ifndef vtkOpenXRManager_h
16#define vtkOpenXRManager_h
17
18#include "vtkABINamespace.h"
19#include "vtkRenderingOpenXRModule.h" // needed for exports
20
21#include "vtkNew.h"
22#include "vtkOpenXR.h"
26#include "vtkSmartPointer.h"
27
28#include <array>
29#include <cstdint>
30#include <memory>
31#include <string>
32#include <vector>
33
34VTK_ABI_NAMESPACE_BEGIN
37VTK_ABI_NAMESPACE_END
38
39namespace vtk::detail
40{
41VTK_ABI_NAMESPACE_BEGIN
42class VTKRENDERINGOPENXR_EXPORT vtkOpenXRManager
43{
44public:
46
50 {
51 static vtkOpenXRManager UniqueInstance;
52 return UniqueInstance;
53 }
54
55
62
64
68 bool XrCheckOutput(OutputLevel level, const XrResult&, const std::string& message);
70
72
76 void PrintSystemProperties(XrSystemProperties* system_properties);
78 void PrintViewConfigViewInfo(const std::vector<XrViewConfigurationView>&);
81
83
92
94
99 void Finalize();
101
103
106 std::tuple<uint32_t, uint32_t> GetRecommendedImageRectSize();
108
110
115
119 uint32_t GetViewCount()
120 {
121 return static_cast<uint32_t>(this->RenderResources->ConfigViews.size());
122 }
123
125
131
133
139 const XrPosef* GetViewPose(uint32_t eye)
140 {
141 if (eye >= this->GetViewCount())
142 {
143 return nullptr;
144 }
145 return &(this->RenderResources->Views[eye].pose);
146 }
147
148
150
155 const XrFovf* GetProjectionFov(uint32_t eye)
156 {
157 if (eye >= this->GetViewCount())
158 {
159 return nullptr;
160 }
161 return &(this->RenderResources->Views[eye].fov);
162 }
163
164
166
171 bool IsDepthExtensionSupported() { return this->OptionalExtensions.DepthExtensionSupported; }
173
180 {
181 return this->OptionalExtensions.SceneUnderstandingSupported;
182 }
183
185
192
194
200
202
205 const XrSession& GetSession() { return this->Session; }
207
209
212 const XrInstance& GetXrRuntimeInstance() { return this->Instance; }
214
216
220 bool IsSessionRunning() { return this->SessionRunning; }
222
224
231
233
239 bool PrepareRendering(vtkOpenXRRenderWindow* win, void* colorTextureId, void* depthTextureId);
241
243
247 void ReleaseSwapchainImage(uint32_t eye);
249
251
255 bool EndFrame();
257
259
262 bool PollEvent(XrEventDataBuffer& eventData);
264
266
269 XrPath GetXrPath(const std::string& path);
271
272 const std::array<XrPath, 2>& GetSubactionPaths() { return this->SubactionPaths; }
273
275
278 bool CreateActionSet(const std::string& actionSetName, const std::string& localizedActionSetName);
280
282
286 bool SelectActiveActionSet(unsigned int index);
288
290
295
297
302
303 struct Action_t;
304
306
312 Action_t& actionT, const std::string& name, const std::string& localizedName);
314
316
320 const std::string& profile, std::vector<XrActionSuggestedBinding>& actionSuggestedBindings);
322
324
330
332
339 bool UpdateActionData(Action_t& action_t, int hand);
341
347 bool ApplyVibration(const Action_t& actionT, int hand, float amplitude = 0.5,
348 float duration = 25000000.0, float frequency = XR_FREQUENCY_UNSPECIFIED);
349
359
360 struct Action_t
361 {
362 XrAction Action;
363 XrActionType ActionType;
364
365 union
366 {
367 XrActionStateFloat _float;
368 XrActionStateBoolean _boolean;
369 XrActionStatePose _pose;
370 XrActionStateVector2f _vec2f;
372
376 };
377
379
385
387
393
397 XrSystemId GetSystemID() const { return this->SystemId; }
398
402 XrSpace GetReferenceSpace() const { return this->ReferenceSpace; }
403
409 XrTime GetPredictedDisplayTime() const { return this->PredictedDisplayTime; }
410
411protected:
413 ~vtkOpenXRManager() = default;
414
416
422 std::vector<const char*> SelectExtensions(vtkOpenXRRenderWindow* window);
424
426
431
433
438
440
446
448
455
457
463
465
471
473
479 std::tuple<int64_t, int64_t> SelectSwapchainPixelFormats();
481
482 struct Swapchain_t;
483
485
489 Swapchain_t CreateSwapchain(int64_t format, uint32_t width, uint32_t height, uint32_t sampleCount,
490 XrSwapchainCreateFlags createFlags, XrSwapchainUsageFlags usageFlags);
492
494
499
501
503
506 bool CreateOneActionSpace(const XrAction& action, const XrPath& subactionPath,
507 const XrPosef& poseInActionSpace, XrSpace& space);
509
511
516
518
522 uint32_t WaitAndAcquireSwapchainImage(const XrSwapchain& swapchainHandle);
524
525 // Currently VTK only supports HeadMountedDisplay (HMD)
526 constexpr static XrFormFactor FormFactor = XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY;
527
528 // Pick the view type to be stereo rather than mono or anything else
529 constexpr static XrViewConfigurationType ViewType = XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO;
530
531 // PRIMARY_STEREO view configuration always has 2 views
532 constexpr static uint32_t StereoViewCount = 2;
533
534 // Three available types: VIEW, LOCAL and STAGE. We use LOCAL space which
535 // establishes a world-locked origin, rather than VIEW space, which tracks the
536 // view origin.
537 XrReferenceSpaceType ReferenceSpaceType = XR_REFERENCE_SPACE_TYPE_STAGE;
538
539 // Communication with the runtime happens through this instance
540 XrInstance Instance;
541
542 // A system is defined by an id and is used to create a session
543 XrSystemId SystemId;
544
545 XrSession Session;
546 XrSessionState SessionState;
548
549 // At the end of a frame, we must select en environment blend mode
550 // to tell the runtime how we want to blend the image with the user's
551 // view of the physical world. For example, in VR, we will generally
552 // choose XR_ENVIRONMENT_BLEND_MODE_OPAQUE while AR will generally
553 // choose XR_ENVIRONMENT_BLEND_MODE_ADDITIVE or XR_ENVIRONMENT_BLEND_MODE_ALPHA_BLEND
554 XrEnvironmentBlendMode EnvironmentBlendMode;
555
556 // Non optional extension
558
560
564 struct
565 {
572 bool RemotingSupported{ false };
574 bool SceneMarkerSupported{ false };
575 } OptionalExtensions;
577
583 {
584 XrSwapchain Swapchain;
585 int64_t Format{ 0 };
586 uint32_t Width{ 0 };
587 uint32_t Height{ 0 };
588 };
589
591
598 {
599 XrViewState ViewState{ XR_TYPE_VIEW_STATE };
600 // Each physical Display/Eye is described by a view
601 std::vector<XrView> Views;
602 // One configuration view per view : this store
603 std::vector<XrViewConfigurationView> ConfigViews;
604
605 std::vector<Swapchain_t> ColorSwapchains;
606 std::vector<Swapchain_t> DepthSwapchains;
607
608 std::vector<XrCompositionLayerProjectionView> ProjectionLayerViews;
609 std::vector<XrCompositionLayerDepthInfoKHR> DepthInfoViews;
610 };
611 std::unique_ptr<RenderResources_t> RenderResources;
613
614 // There is one subaction path for each hand.
615 std::array<XrPath, 2> SubactionPaths;
616
617 std::vector<XrActionSet> ActionSets;
618 XrActionSet* ActiveActionSet = nullptr;
619
625
626 bool SessionRunning = false;
627 // Following each WaitAndBeginFrame operation, the OpenXR runtime may indicate
628 // whether the current frame should be rendered using the `XrFrameState.shouldRender`
629 // property. We store this information to optimize rendering and prevent unnecessary
630 // render calls. For further details, refer to:
631 // https://registry.khronos.org/OpenXR/specs/1.0/man/html/XrFrameState.html
633 // If true, the function UpdateActionData will store
634 // pose velocities for pose actions
636
638
640
641private:
642 vtkOpenXRManager(const vtkOpenXRManager&) = delete;
643 void operator=(const vtkOpenXRManager&) = delete;
644};
645VTK_ABI_NAMESPACE_END
646} // namespace vtk::detail
647
648#endif
649// VTK-HeaderTest-Exclude: vtkOpenXRManager.h
OpenGL rendering window.
OpenXR manager connection no-op implementation.
OpenXR manager graphics implementation.
OpenXR rendering window.
vtkOpenXRSceneObserver is a wrapper around OpenXR scene understanding extensions
Hold a reference to a vtkObjectBase instance.
bool CreateActionSet(const std::string &actionSetName, const std::string &localizedActionSetName)
Creates an action set and add it to the vector of action sets.
vtkSmartPointer< vtkOpenXRManagerConnection > ConnectionStrategy
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....
XrReferenceSpaceType ReferenceSpaceType
void ReleaseSwapchainImage(uint32_t eye)
When the rendering in a swapchain image is done, it must be released with this function.
bool IsDepthExtensionSupported()
Return true if the runtime supports the depth extension.
std::tuple< int64_t, int64_t > SelectSwapchainPixelFormats()
During the creation of the swapchains, we need to check the runtime available pixels formats,...
bool CreateSystem()
OpenXR System creation.
XrPath GetXrPath(const std::string &path)
Get the XrPath from the well-formed string path.
std::vector< const char * > SelectExtensions(vtkOpenXRRenderWindow *window)
OpenXR Instance creation.
XrTime PredictedDisplayTime
Store the frame predicted display time in WaitAndBeginFrame To get the action data at this time and t...
bool CreateSubactionPaths()
Creates one subaction path for each hand.
bool CreateConfigViews()
There is one configuration view per view, and it contains the recommended texture resolution in pixel...
std::vector< XrActionSet > ActionSets
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.
XrSystemId GetSystemID() const
Return OpenXR System ID associated with the XrSession.
bool SyncActions()
Update the action states using the active action set.
std::tuple< uint32_t, uint32_t > GetRecommendedImageRectSize()
Return as a tuple the OpenXR recommended texture size to be sent to the device.
struct vtk::detail::vtkOpenXRManager::@342253300264113136100372334253042337262164244010 OptionalExtensions
Structure to hold optional extensions loaded with SelectExtensions.
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...
void PrintSupportedViewConfigs()
Utility functions to print information about OpenXR manager internal structures.
void Finalize()
Internal API.
bool CreateSystemProperties()
Enable system properties such as hand tracking, and choose environment blend modes.
bool UpdateActionData(Action_t &action_t, int hand)
Update the action data and store it in action_t.States for one hand.
const XrSession & GetSession()
Return the OpenXR Session.
bool Initialize(vtkOpenXRRenderWindow *xrWindow)
Internal API.
void DestroyActionSets()
Iterate over and destroy all action sets that have been created.
bool IsSceneUnderstandingSupported()
Return true if the runtime supports the scene understanding extension.
bool CreateSession()
Create the session and pass the GraphicsBinding to the next pointer of the XrSessionCreateInfo.
static constexpr XrViewConfigurationType ViewType
bool SelectActiveActionSet(unsigned int index)
Selects the current active action set from the ActionSets vector using its index.
bool CreateSwapchains()
Swapchaines creation : there is one swapchain per view / display.
void PrintSystemProperties(XrSystemProperties *system_properties)
Utility functions to print information about OpenXR manager internal structures.
const XrInstance & GetXrRuntimeInstance()
Return the instance used to communicate with the runtime.
static vtkOpenXRManager & GetInstance()
Return the singleton instance.
bool BeginSession()
Start the OpenXR session.
static constexpr XrFormFactor FormFactor
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.
bool WaitAndBeginFrame()
This function is used to start a frame.
bool CreateInstance(vtkOpenXRRenderWindow *window)
OpenXR Instance creation.
bool PollEvent(XrEventDataBuffer &eventData)
Store in eventData the result of xrPollEvent.
void SetConnectionStrategy(vtkOpenXRManagerConnection *cs)
Set/Get the connection strategy.
uint32_t GetViewCount()
Return the number of OpenXR views (typically one per physical display / eye)
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 ...
XrEnvironmentBlendMode EnvironmentBlendMode
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...
bool SuggestActions(const std::string &profile, std::vector< XrActionSuggestedBinding > &actionSuggestedBindings)
Suggest actions stored in actionSuggestedBindings for the interaction profile profile.
bool PrintReferenceSpaces()
Utility functions to print information about OpenXR manager internal structures.
vtkOpenXRManagerGraphics * GetGraphicsStrategy()
Set/Get the rendering backend strategy.
static constexpr uint32_t StereoViewCount
XrSpace GetReferenceSpace() const
Return XrSpace associated with the XrSession.
void PrintOptionalExtensions()
Print the optional extensions which were found and enabled.
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...
uint32_t GetRecommendedSampleCount()
Return the recommended swapchain sample count.
void PrintInstanceProperties()
Utility functions to print information about OpenXR manager internal structures.
bool EndFrame()
Submit the composition layers for the predicted display time of the current frame.
vtkOpenXRManagerConnection * GetConnectionStrategy()
Set/Get the connection strategy.
XrTime GetPredictedDisplayTime() const
Return runtime predicted display time for next frame.
void SetGraphicsStrategy(vtkOpenXRManagerGraphics *gs)
Set/Get the rendering backend strategy.
bool CreateReferenceSpace()
Creates the reference space of type ReferenceSpaceType that will be used to locate views.
vtkSmartPointer< vtkOpenXRManagerGraphics > GraphicsStrategy
bool AttachSessionActionSets()
Attach all action sets in the ActionSets vector to the session.
std::array< XrPath, 2 > SubactionPaths
void PrintViewConfigViewInfo(const std::vector< XrViewConfigurationView > &)
Utility functions to print information about OpenXR manager internal structures.
std::unique_ptr< RenderResources_t > RenderResources
const XrPosef * GetViewPose(uint32_t eye)
Returns a pointer to the view pose that contains the view orientation and position for the specified ...
bool IsSessionRunning()
Return true if the OpenXR session is currently running, ie.
bool GetShouldRenderCurrentFrame()
Return true if the current frame should be rendered.
bool PrepareRendering(vtkOpenXRRenderWindow *win, void *colorTextureId, void *depthTextureId)
Prepare the rendering resources for the specified eye and store in colorTextureId and in depthTexture...
std::string GetOpenXRPropertiesAsString()
Return the OpenXR properties as a string, with format "RuntimeName MAJOR.MINOR.PATCH".
const std::array< XrPath, 2 > & GetSubactionPaths()
XrSpace PoseSpaces[ControllerIndex::NumberOfControllers]
XrSpaceLocation PoseLocations[ControllerIndex::NumberOfControllers]
XrSpaceVelocity PoseVelocities[ControllerIndex::NumberOfControllers]
This struct stores all needed information to render the images and send it to the user We can't make ...
std::vector< XrViewConfigurationView > ConfigViews
std::vector< XrCompositionLayerDepthInfoKHR > DepthInfoViews
std::vector< XrCompositionLayerProjectionView > ProjectionLayerViews
Swapchain structure storing information common to all rendering backend.
Defines the OpenXR types and extensions common to all platforms.