Plasma Engine  2.0
Loading...
Searching...
No Matches
OpenXRSingleton.h
1#pragma once
2
3#include <Core/Graphics/Camera.h>
4#include <Foundation/Configuration/Singleton.h>
5#include <GameEngine/XR/XRInputDevice.h>
6#include <GameEngine/XR/XRInterface.h>
7#include <OpenXRPlugin/Basics.h>
8#include <OpenXRPlugin/OpenXRIncludes.h>
9#include <RendererCore/Pipeline/Declarations.h>
10#include <RendererCore/Shader/ConstantBufferStorage.h>
11#include <RendererFoundation/Descriptors/Descriptors.h>
12#include <RendererFoundation/Device/SwapChain.h>
13#include <RendererFoundation/Resources/RenderTargetSetup.h>
14
20
21PL_DEFINE_AS_POD_TYPE(XrViewConfigurationView);
22PL_DEFINE_AS_POD_TYPE(XrEnvironmentBlendMode);
23
24class PL_OPENXRPLUGIN_DLL plOpenXR : public plXRInterface
25{
26 PL_DECLARE_SINGLETON_OF_INTERFACE(plOpenXR, plXRInterface);
27
28public:
29 plOpenXR();
30 ~plOpenXR();
31
32 XrInstance GetInstance() const { return m_pInstance; }
33 uint64_t GetSystemId() const { return m_SystemId; }
34 XrSession GetSession() const { return m_pSession; }
35 XrViewConfigurationType GetViewType() const { return m_PrimaryViewConfigurationType; }
36 bool GetDepthComposition() const;
37
38 virtual bool IsHmdPresent() const override;
39
40 virtual plResult Initialize() override;
41 virtual void Deinitialize() override;
42 virtual bool IsInitialized() const override;
43
44 virtual const plHMDInfo& GetHmdInfo() const override;
45 virtual plXRInputDevice& GetXRInput() const override;
46
47 virtual plGALTextureHandle GetCurrentTexture() override;
48
49 void DelayPresent();
50 void Present();
51 void EndFrame();
52
53 virtual plUniquePtr<plActor> CreateActor(plView* pView, plGALMSAASampleCount::Enum msaaCount = plGALMSAASampleCount::None,
54 plUniquePtr<plWindowBase> pCompanionWindow = nullptr, plUniquePtr<plWindowOutputTargetGAL> pCompanionWindowOutput = nullptr) override;
55 virtual void OnActorDestroyed() override;
56 virtual bool SupportsCompanionView() override;
57
58 XrSpace GetBaseSpace() const;
59
60private:
61 XrResult SelectExtensions(plHybridArray<const char*, 6>& extensions);
62 XrResult SelectLayers(plHybridArray<const char*, 6>& layers);
63 XrResult InitSystem();
64 void DeinitSystem();
65 XrResult InitSession();
66 void DeinitSession();
67 XrResult InitGraphicsPlugin();
68 void DeinitGraphicsPlugin();
69 XrResult InitDebugMessenger();
70 void DeinitInitDebugMessenger();
71
72 void GameApplicationEventHandler(const plGameApplicationExecutionEvent& e);
73 void GALDeviceEventHandler(const plGALDeviceEvent& e);
74
75 void BeforeUpdatePlugins();
76 void UpdatePoses();
77 void UpdateCamera();
78 void BeginFrame();
79
80 void SetStageSpace(plXRStageSpace::Enum space);
81 void SetHMDCamera(plCamera* pCamera);
82
83 plWorld* GetWorld();
84
85public:
86 static XrPosef ConvertTransform(const plTransform& tr);
87 static XrQuaternionf ConvertOrientation(const plQuat& q);
88 static XrVector3f ConvertPosition(const plVec3& vPos);
89 static plQuat ConvertOrientation(const XrQuaternionf& q);
90 static plVec3 ConvertPosition(const XrVector3f& pos);
91 static plMat4 ConvertPoseToMatrix(const XrPosef& pose);
92 static plGALResourceFormat::Enum ConvertTextureFormat(int64_t format);
93
94private:
95 friend class plOpenXRInputDevice;
96 friend class plOpenXRSpatialAnchors;
97 friend class plOpenXRHandTracking;
98 friend class plOpenXRRemoting;
99 friend class plGALOpenXRSwapChain;
100
101 struct Extensions
102 {
103 bool m_bValidation = false;
104 bool m_bDebugUtils = false;
105 PFN_xrCreateDebugUtilsMessengerEXT pfn_xrCreateDebugUtilsMessengerEXT;
106 PFN_xrDestroyDebugUtilsMessengerEXT pfn_xrDestroyDebugUtilsMessengerEXT;
107
108 bool m_bD3D11 = false;
109 PFN_xrGetD3D11GraphicsRequirementsKHR pfn_xrGetD3D11GraphicsRequirementsKHR;
110
111 bool m_bDepthComposition = false;
112
113 bool m_bUnboundedReferenceSpace = false;
114
115 bool m_bSpatialAnchor = false;
116 PFN_xrCreateSpatialAnchorMSFT pfn_xrCreateSpatialAnchorMSFT;
117 PFN_xrCreateSpatialAnchorSpaceMSFT pfn_xrCreateSpatialAnchorSpaceMSFT;
118 PFN_xrDestroySpatialAnchorMSFT pfn_xrDestroySpatialAnchorMSFT;
119
120 bool m_bHandInteraction = false;
121
122 bool m_bHandTracking = false;
123 PFN_xrCreateHandTrackerEXT pfn_xrCreateHandTrackerEXT;
124 PFN_xrDestroyHandTrackerEXT pfn_xrDestroyHandTrackerEXT;
125 PFN_xrLocateHandJointsEXT pfn_xrLocateHandJointsEXT;
126
127 bool m_bHandTrackingMesh = false;
128 PFN_xrCreateHandMeshSpaceMSFT pfn_xrCreateHandMeshSpaceMSFT;
129 PFN_xrUpdateHandMeshMSFT pfn_xrUpdateHandMeshMSFT;
130
131 bool m_bHolographicWindowAttachment = false;
132
133 bool m_bRemoting = false;
134#ifdef BUILDSYSTEM_ENABLE_OPENXR_REMOTING_SUPPORT
135 PFN_xrRemotingSetContextPropertiesMSFT pfn_xrRemotingSetContextPropertiesMSFT;
136 PFN_xrRemotingConnectMSFT pfn_xrRemotingConnectMSFT;
137 PFN_xrRemotingDisconnectMSFT pfn_xrRemotingDisconnectMSFT;
138 PFN_xrRemotingGetConnectionStateMSFT pfn_xrRemotingGetConnectionStateMSFT;
139#endif
140 };
141
142 // Instance
143 XrInstance m_pInstance = XR_NULL_HANDLE;
144 Extensions m_Extensions;
145#ifdef BUILDSYSTEM_ENABLE_OPENXR_REMOTING_SUPPORT
147#endif
148
149 // System
150 uint64_t m_SystemId = XR_NULL_SYSTEM_ID;
151
152 // Session
153 XrSession m_pSession = XR_NULL_HANDLE;
154 XrSpace m_pSceneSpace = XR_NULL_HANDLE;
155 XrSpace m_pLocalSpace = XR_NULL_HANDLE;
156 plEventSubscriptionID m_ExecutionEventsId = 0;
157 plEventSubscriptionID m_BeginRenderEventsId = 0;
158 plEventSubscriptionID m_GALdeviceEventsId = 0;
159 XrDebugUtilsMessengerEXT m_pDebugMessenger = XR_NULL_HANDLE;
160
161 // Graphics plugin
162 XrEnvironmentBlendMode m_BlendMode = XR_ENVIRONMENT_BLEND_MODE_OPAQUE;
163 XrGraphicsBindingD3D11KHR m_XrGraphicsBindingD3D11{XR_TYPE_GRAPHICS_BINDING_D3D11_KHR};
164 XrFormFactor m_FormFactor{XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY};
165 XrViewConfigurationType m_PrimaryViewConfigurationType{XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO};
166
167 plGALSwapChainHandle m_hSwapChain;
168
169 // Views
170 XrViewState m_ViewState{XR_TYPE_VIEW_STATE};
171 XrView m_Views[2];
172 bool m_bProjectionChanged = true;
173 XrCompositionLayerProjection m_Layer{XR_TYPE_COMPOSITION_LAYER_PROJECTION};
174 XrCompositionLayerProjectionView m_ProjectionLayerViews[2];
175 XrCompositionLayerDepthInfoKHR m_DepthLayerViews[2];
176
177 // State
178 bool m_bSessionRunning = false;
179 bool m_bExitRenderLoop = false;
180 bool m_bRequestRestart = false;
181 bool m_bRenderInProgress = false;
182 XrSessionState m_SessionState{XR_SESSION_STATE_UNKNOWN};
183
184 XrFrameWaitInfo m_FrameWaitInfo{XR_TYPE_FRAME_WAIT_INFO};
185 XrFrameState m_FrameState{XR_TYPE_FRAME_STATE};
186 XrFrameBeginInfo m_FrameBeginInfo{XR_TYPE_FRAME_BEGIN_INFO};
187
188 // XR interface state
189 plHMDInfo m_Info;
190 mutable plUniquePtr<plOpenXRInputDevice> m_pInput;
192 plUniquePtr<plOpenXRHandTracking> m_pHandTracking;
193
194 plCamera* m_pCameraToSynchronize = nullptr;
195 plEnum<plXRStageSpace> m_StageSpace;
196 plUInt32 m_uiSettingsModificationCounter = 0;
197 plViewHandle m_hView;
198
199 plWindowOutputTargetXR* m_pCompanion = nullptr;
200 bool m_bPresentDelayed = false;
201};
A camera class that stores the orientation and some basic camera settings.
Definition Camera.h:41
Definition OpenXRSwapChain.h:12
Definition RendererFoundationDLL.h:397
Definition RendererFoundationDLL.h:411
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Definition OpenXRHandTracking.h:11
Definition OpenXRSingleton.h:25
Definition OpenXRInputDevice.h:15
Definition OpenXRSpatialAnchors.h:13
A Unique ptr manages an object and destroys that object when it goes out of scope....
Definition UniquePtr.h:10
Definition Declarations.h:64
Encapsulates a view on the given world through the given camera and rendered with the specified Rende...
Definition View.h:21
XR Window output target base implementation. Optionally wraps a companion window output target.
Definition XRWindow.h:42
A world encapsulates a scene graph of game objects and various component managers and their component...
Definition World.h:22
Definition XRInputDevice.h:49
XR singleton interface. Needs to be initialized to be used for VR or AR purposes.
Definition XRInterface.h:29
virtual void OnActorDestroyed()=0
Called when the actor created by 'CreateActor' is destroyed.
virtual const plHMDInfo & GetHmdInfo() const =0
Returns general HMD information.
virtual plXRInputDevice & GetXRInput() const =0
Returns the XR input device.
virtual bool SupportsCompanionView()=0
Returns true if a companion window can be passed into CreateActor.
virtual plGALTextureHandle GetCurrentTexture()=0
Called by plWindowOutputTargetXR::RenderCompanionView Returns the color texture to be used by the com...
virtual bool IsHmdPresent() const =0
Returns whether an HMD is available. Can be used to decide whether it makes sense to call Initialize ...
virtual plUniquePtr< plActor > CreateActor(plView *pView, plGALMSAASampleCount::Enum msaaCount=plGALMSAASampleCount::None, plUniquePtr< plWindowBase > pCompanionWindow=nullptr, plUniquePtr< plWindowOutputTargetGAL > pCompanionWindowOutput=nullptr)=0
Creates a XR actor by trying to startup an XR session.
virtual bool IsInitialized() const =0
Returns whether the XR system is initialized.
virtual void Deinitialize()=0
Shuts down the XR system again.
virtual plResult Initialize()=0
Initializes the XR system. This can be quite time consuming as it will generally start supporting app...
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
Type for important GAL events.
Definition Descriptors.h:253
Definition GameApplicationBase.h:32
Definition Declarations.h:9
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Enum
Definition Declarations.h:23