1 | // Copyright (C) 2024 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #ifndef QQUICK3DXRMANAGER_OPENXR_P_H |
5 | #define QQUICK3DXRMANAGER_OPENXR_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtQuick3DXr/private/qabstractopenxrgraphics_p.h> |
19 | #include <QtQuick3DXr/private/qtquick3dxrglobal_p.h> |
20 | |
21 | #include <QtCore/qmap.h> |
22 | #include <QtCore/qlist.h> |
23 | #include <QtCore/qversionnumber.h> |
24 | #include <QtCore/qpointer.h> |
25 | |
26 | #include <QtGui/private/qtgui-config_p.h> |
27 | #if QT_CONFIG(graphicsframecapture) |
28 | #include <QtGui/private/qgraphicsframecapture_p.h> |
29 | #endif |
30 | |
31 | #ifdef XR_USE_PLATFORM_ANDROID |
32 | # include <QtCore/QJniObject> |
33 | #endif |
34 | |
35 | QT_BEGIN_NAMESPACE |
36 | |
37 | class QQuick3DXrEyeCamera; |
38 | class QQuick3DXrOrigin; |
39 | class QQuick3DViewport; |
40 | class QQuick3DXrAnchorManager; |
41 | class QQuick3DXrManager; |
42 | class QQuick3DXrInputManager; |
43 | |
44 | class QQuick3DXrManagerPrivate |
45 | { |
46 | Q_DECLARE_PUBLIC(QQuick3DXrManager) |
47 | public: |
48 | explicit QQuick3DXrManagerPrivate(QQuick3DXrManager &manager); |
49 | ~QQuick3DXrManagerPrivate(); |
50 | |
51 | static QQuick3DXrManagerPrivate *get(QQuick3DXrManager *manager); |
52 | |
53 | [[nodiscard]] bool supportsPassthrough() const; |
54 | |
55 | bool isValid() const { return m_graphics != nullptr; } |
56 | |
57 | bool isReady() const { return true; } |
58 | |
59 | void setupWindow(QQuickWindow *window); |
60 | |
61 | bool isGraphicsInitialized() const; |
62 | bool setupGraphics(QQuickWindow *window); |
63 | |
64 | void processXrEvents(); |
65 | |
66 | void doRenderFrame(); |
67 | |
68 | bool finalizeGraphics(QRhi *rhi); |
69 | |
70 | void setSamples(int samples); |
71 | |
72 | QStringList enabledExtensions() const; |
73 | QString runtimeName() const; |
74 | QVersionNumber runtimeVersion() const; |
75 | |
76 | void setMultiViewRenderingEnabled(bool enable); |
77 | bool isMultiViewRenderingEnabled() const { return m_multiviewRendering; } |
78 | |
79 | void setPassthroughEnabled(bool enable); |
80 | bool isPassthroughEnabled() const { return m_enablePassthrough; } |
81 | |
82 | void setDepthSubmissionEnabled(bool enable); |
83 | bool isDepthSubmissionEnabled() const { return m_compositionLayerDepthSupported && m_submitLayerDepth; } |
84 | |
85 | void setReferenceSpace(QtQuick3DXr::ReferenceSpace newReferenceSpace); |
86 | QtQuick3DXr::ReferenceSpace getReferenceSpace() const; |
87 | |
88 | void getDefaultClipDistances(float &nearClip, float &farClip) const; |
89 | |
90 | QString errorString() const { return m_errorString; } |
91 | |
92 | private: |
93 | friend class QQuick3DXrRuntimeInfo; |
94 | |
95 | bool initialize(); |
96 | void teardown(); |
97 | |
98 | void destroySwapchain(); |
99 | void setErrorString(XrResult result, const char *callName); |
100 | void checkXrExtensions(const char* layerName, int indent = 0); |
101 | void checkXrLayers(); |
102 | |
103 | XrResult createXrInstance(); |
104 | void checkXrInstance(); |
105 | |
106 | void setupDebugMessenger(); |
107 | |
108 | XrResult initializeSystem(); |
109 | |
110 | void checkViewConfiguration(); |
111 | [[nodiscard]] bool checkXrResult(const XrResult &result); |
112 | bool resolveXrFunction(const char *name, PFN_xrVoidFunction *function); |
113 | void checkEnvironmentBlendMode(XrViewConfigurationType type); |
114 | |
115 | void pollEvents(bool *exitRenderLoop, bool *requestRestart); |
116 | void pollActions(); |
117 | |
118 | void checkReferenceSpaces(); |
119 | bool isReferenceSpaceAvailable(XrReferenceSpaceType type); |
120 | |
121 | bool setupAppSpace(); |
122 | void updateAppSpace(XrTime predictedDisplayTime); |
123 | bool setupViewSpace(); |
124 | bool resetEmulatedFloorHeight(XrTime predictedDisplayTime); |
125 | |
126 | bool createSwapchains(); |
127 | |
128 | bool renderLayer(XrTime predictedDisplayTime, |
129 | XrDuration predictedDisplayPeriod, |
130 | XrCompositionLayerProjection &layer); |
131 | void (const XrSwapchainSubImage &subImage, |
132 | const XrSwapchainImageBaseHeader *swapchainImage, |
133 | const XrSwapchainImageBaseHeader *depthSwapchainImage = nullptr); |
134 | |
135 | void updateCameraHelper(QQuick3DXrEyeCamera *camera, const XrCompositionLayerProjectionView &layerView); |
136 | void updateCameraNonMultiview(int eye, const XrCompositionLayerProjectionView &layerView); |
137 | void updateCameraMultiview(int projectionLayerViewStartIndex, int count); |
138 | |
139 | void setupMetaQuestColorSpaces(); |
140 | void setupMetaQuestRefreshRates(); |
141 | void setupMetaQuestFoveation(); |
142 | |
143 | // Passthrough |
144 | XrPassthroughFB m_passthroughFeature{XR_NULL_HANDLE}; |
145 | void createMetaQuestPassthrough(); |
146 | void destroyMetaQuestPassthrough(); |
147 | void startMetaQuestPassthrough(); |
148 | void pauseMetaQuestPassthrough(); |
149 | |
150 | XrPassthroughLayerFB m_passthroughLayer{XR_NULL_HANDLE}; |
151 | void createMetaQuestPassthroughLayer(); |
152 | void destroyMetaQuestPassthroughLayer(); |
153 | void pauseMetaQuestPassthroughLayer(); |
154 | void resumeMetaQuestPassthroughLayer(); |
155 | |
156 | XrTime m_previousTime = 0; |
157 | |
158 | struct Swapchain { |
159 | XrSwapchain handle; |
160 | quint32 width; |
161 | quint32 height; |
162 | quint32 arraySize; |
163 | }; |
164 | |
165 | QVector<XrViewConfigurationView> m_configViews; |
166 | QVector<XrCompositionLayerProjectionView> m_projectionLayerViews; |
167 | QVector<XrCompositionLayerDepthInfoKHR> m_layerDepthInfos; |
168 | QVector<Swapchain> m_swapchains; |
169 | QVector<Swapchain> m_depthSwapchains; |
170 | QMap<XrSwapchain, QVector<XrSwapchainImageBaseHeader*>> m_swapchainImages; |
171 | QMap<XrSwapchain, QVector<XrSwapchainImageBaseHeader*>> m_depthSwapchainImages; |
172 | QVector<XrView> m_views; |
173 | |
174 | QString m_errorString; |
175 | QString m_runtimeName; |
176 | QVersionNumber m_runtimeVersion; |
177 | QStringList m_enabledApiLayers; |
178 | QStringList m_enabledExtensions; |
179 | |
180 | XrEventDataBuffer m_eventDataBuffer; |
181 | |
182 | XrInstance m_instance{XR_NULL_HANDLE}; |
183 | XrSession m_session{XR_NULL_HANDLE}; |
184 | XrSpace m_appSpace{XR_NULL_HANDLE}; |
185 | XrReferenceSpaceType m_requestedReferenceSpace = XR_REFERENCE_SPACE_TYPE_STAGE; |
186 | XrReferenceSpaceType m_referenceSpace = XR_REFERENCE_SPACE_TYPE_LOCAL; |
187 | bool m_isEmulatingLocalFloor = false; |
188 | bool m_isFloorResetPending = false; |
189 | XrSpace m_viewSpace{XR_NULL_HANDLE}; |
190 | XrFormFactor m_formFactor{XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY}; |
191 | XrViewConfigurationType m_viewConfigType{XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO}; |
192 | XrEnvironmentBlendMode m_environmentBlendMode{XR_ENVIRONMENT_BLEND_MODE_OPAQUE}; |
193 | XrSystemId m_systemId{XR_NULL_SYSTEM_ID}; |
194 | |
195 | // Application's current lifecycle state according to the runtime |
196 | XrSessionState m_sessionState{XR_SESSION_STATE_UNKNOWN}; |
197 | |
198 | QVector<XrReferenceSpaceType> m_availableReferenceSpace; |
199 | |
200 | QPointer<QQuick3DXrInputManager> m_inputManager; |
201 | |
202 | int64_t m_colorSwapchainFormat = -1; |
203 | int64_t m_depthSwapchainFormat = -1; |
204 | int m_samples = 1; |
205 | |
206 | bool m_passThroughEnabled = false; |
207 | bool m_passthroughSupported = false; |
208 | bool m_enablePassthrough = false; |
209 | bool m_multiviewRendering = true; |
210 | bool m_spaceExtensionSupported = false; |
211 | QQuick3DXrAnchorManager *m_spaceExtension = nullptr; |
212 | bool m_colorspaceExtensionSupported = false; |
213 | bool m_displayRefreshRateExtensionSupported = false; |
214 | bool m_foveationExtensionSupported = false; |
215 | #ifdef Q_OS_ANDROID |
216 | bool m_androidCreateInstanceExtensionSupported = false; |
217 | #endif |
218 | XrFoveationLevelFB m_foveationLevel = XR_FOVEATION_LEVEL_HIGH_FB; |
219 | bool m_compositionLayerDepthSupported = false; |
220 | bool m_submitLayerDepth = false; |
221 | bool m_handtrackingExtensionSupported = false; |
222 | bool m_handtrackingAimExtensionSupported = false; |
223 | bool m_isGraphicsInitialized = false; |
224 | |
225 | bool m_sessionRunning{false}; |
226 | |
227 | QQuick3DXrManager *q_ptr = nullptr; |
228 | #ifdef XR_USE_PLATFORM_ANDROID |
229 | QJniObject m_androidActivity; |
230 | JavaVM* m_javaVM = nullptr; |
231 | #endif // XR_USE_PLATFORM_ANDROID |
232 | |
233 | #ifdef XR_EXT_debug_utils |
234 | XrDebugUtilsMessengerEXT m_debugMessenger = XR_NULL_HANDLE; |
235 | PFN_xrDestroyDebugUtilsMessengerEXT m_xrDestroyDebugUtilsMessengerEXT = nullptr; |
236 | #endif |
237 | |
238 | #if QT_CONFIG(graphicsframecapture) |
239 | std::unique_ptr<QGraphicsFrameCapture> m_frameCapture; |
240 | #endif |
241 | |
242 | QAbstractOpenXRGraphics *m_graphics = nullptr; |
243 | |
244 | }; |
245 | |
246 | QT_END_NAMESPACE |
247 | |
248 | #endif // QQUICK3DXRMANAGER_OPENXR_P_H |
249 | |