| 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 update(); |
| 65 | |
| 66 | void processXrEvents(); |
| 67 | |
| 68 | void doRenderFrame(); |
| 69 | |
| 70 | bool finalizeGraphics(QRhi *rhi); |
| 71 | |
| 72 | void setSamples(int samples); |
| 73 | |
| 74 | QStringList enabledExtensions() const; |
| 75 | QString runtimeName() const; |
| 76 | QVersionNumber runtimeVersion() const; |
| 77 | |
| 78 | void setMultiViewRenderingEnabled(bool enable); |
| 79 | bool isMultiViewRenderingEnabled() const { return m_multiviewRendering; } |
| 80 | |
| 81 | void setPassthroughEnabled(bool enable); |
| 82 | bool isPassthroughEnabled() const { return m_enablePassthrough; } |
| 83 | |
| 84 | void setDepthSubmissionEnabled(bool enable); |
| 85 | bool isDepthSubmissionEnabled() const { return m_compositionLayerDepthSupported && m_submitLayerDepth; } |
| 86 | |
| 87 | void setReferenceSpace(QtQuick3DXr::ReferenceSpace newReferenceSpace); |
| 88 | QtQuick3DXr::ReferenceSpace getReferenceSpace() const; |
| 89 | |
| 90 | void getDefaultClipDistances(float &nearClip, float &farClip) const; |
| 91 | |
| 92 | QString errorString() const { return m_errorString; } |
| 93 | |
| 94 | private: |
| 95 | friend class QQuick3DXrRuntimeInfo; |
| 96 | |
| 97 | bool initialize(); |
| 98 | void teardown(); |
| 99 | |
| 100 | void destroySwapchain(); |
| 101 | void setErrorString(XrResult result, const char *callName); |
| 102 | void checkXrExtensions(const char* layerName, int indent = 0); |
| 103 | void checkXrLayers(); |
| 104 | |
| 105 | XrResult createXrInstance(); |
| 106 | void checkXrInstance(); |
| 107 | |
| 108 | void setupDebugMessenger(); |
| 109 | |
| 110 | XrResult initializeSystem(); |
| 111 | |
| 112 | void checkViewConfiguration(); |
| 113 | [[nodiscard]] bool checkXrResult(const XrResult &result); |
| 114 | bool resolveXrFunction(const char *name, PFN_xrVoidFunction *function); |
| 115 | void checkEnvironmentBlendMode(XrViewConfigurationType type); |
| 116 | |
| 117 | void pollEvents(bool *exitRenderLoop, bool *requestRestart); |
| 118 | void pollActions(); |
| 119 | |
| 120 | void checkReferenceSpaces(); |
| 121 | bool isReferenceSpaceAvailable(XrReferenceSpaceType type); |
| 122 | |
| 123 | bool setupAppSpace(); |
| 124 | void updateAppSpace(XrTime predictedDisplayTime); |
| 125 | bool setupViewSpace(); |
| 126 | bool resetEmulatedFloorHeight(XrTime predictedDisplayTime); |
| 127 | |
| 128 | bool createSwapchains(); |
| 129 | |
| 130 | bool renderLayer(XrTime predictedDisplayTime, |
| 131 | XrDuration predictedDisplayPeriod, |
| 132 | XrCompositionLayerProjection &layer); |
| 133 | void (const XrSwapchainSubImage &subImage, |
| 134 | const XrSwapchainImageBaseHeader *swapchainImage, |
| 135 | const XrSwapchainImageBaseHeader *depthSwapchainImage = nullptr); |
| 136 | |
| 137 | void updateCameraHelper(QQuick3DXrEyeCamera *camera, const XrCompositionLayerProjectionView &layerView); |
| 138 | void updateCameraNonMultiview(int eye, const XrCompositionLayerProjectionView &layerView); |
| 139 | void updateCameraMultiview(int projectionLayerViewStartIndex, int count); |
| 140 | |
| 141 | void setupMetaQuestColorSpaces(); |
| 142 | void setupMetaQuestRefreshRates(); |
| 143 | void setupMetaQuestFoveation(); |
| 144 | |
| 145 | // Passthrough |
| 146 | XrPassthroughFB m_passthroughFeature{XR_NULL_HANDLE}; |
| 147 | void createMetaQuestPassthrough(); |
| 148 | void destroyMetaQuestPassthrough(); |
| 149 | void startMetaQuestPassthrough(); |
| 150 | void pauseMetaQuestPassthrough(); |
| 151 | |
| 152 | XrPassthroughLayerFB m_passthroughLayer{XR_NULL_HANDLE}; |
| 153 | void createMetaQuestPassthroughLayer(); |
| 154 | void destroyMetaQuestPassthroughLayer(); |
| 155 | void pauseMetaQuestPassthroughLayer(); |
| 156 | void resumeMetaQuestPassthroughLayer(); |
| 157 | |
| 158 | XrTime m_previousTime = 0; |
| 159 | |
| 160 | struct Swapchain { |
| 161 | XrSwapchain handle; |
| 162 | quint32 width; |
| 163 | quint32 height; |
| 164 | quint32 arraySize; |
| 165 | }; |
| 166 | |
| 167 | QVector<XrViewConfigurationView> m_configViews; |
| 168 | QVector<XrCompositionLayerProjectionView> m_projectionLayerViews; |
| 169 | QVector<XrCompositionLayerDepthInfoKHR> m_layerDepthInfos; |
| 170 | QVector<Swapchain> m_swapchains; |
| 171 | QVector<Swapchain> m_depthSwapchains; |
| 172 | QMap<XrSwapchain, QVector<XrSwapchainImageBaseHeader*>> m_swapchainImages; |
| 173 | QMap<XrSwapchain, QVector<XrSwapchainImageBaseHeader*>> m_depthSwapchainImages; |
| 174 | QVector<XrView> m_views; |
| 175 | |
| 176 | QString m_errorString; |
| 177 | QString m_runtimeName; |
| 178 | QVersionNumber m_runtimeVersion; |
| 179 | QStringList m_enabledApiLayers; |
| 180 | QStringList m_enabledExtensions; |
| 181 | |
| 182 | XrEventDataBuffer m_eventDataBuffer; |
| 183 | |
| 184 | XrInstance m_instance{XR_NULL_HANDLE}; |
| 185 | XrSession m_session{XR_NULL_HANDLE}; |
| 186 | XrSpace m_appSpace{XR_NULL_HANDLE}; |
| 187 | XrReferenceSpaceType m_requestedReferenceSpace = XR_REFERENCE_SPACE_TYPE_STAGE; |
| 188 | XrReferenceSpaceType m_referenceSpace = XR_REFERENCE_SPACE_TYPE_LOCAL; |
| 189 | bool m_isEmulatingLocalFloor = false; |
| 190 | bool m_isFloorResetPending = false; |
| 191 | XrSpace m_viewSpace{XR_NULL_HANDLE}; |
| 192 | XrFormFactor m_formFactor{XR_FORM_FACTOR_HEAD_MOUNTED_DISPLAY}; |
| 193 | XrViewConfigurationType m_viewConfigType{XR_VIEW_CONFIGURATION_TYPE_PRIMARY_STEREO}; |
| 194 | XrEnvironmentBlendMode m_environmentBlendMode{XR_ENVIRONMENT_BLEND_MODE_OPAQUE}; |
| 195 | XrSystemId m_systemId{XR_NULL_SYSTEM_ID}; |
| 196 | |
| 197 | // Application's current lifecycle state according to the runtime |
| 198 | XrSessionState m_sessionState{XR_SESSION_STATE_UNKNOWN}; |
| 199 | |
| 200 | QVector<XrReferenceSpaceType> m_availableReferenceSpace; |
| 201 | |
| 202 | QPointer<QQuick3DXrInputManager> m_inputManager; |
| 203 | |
| 204 | int64_t m_colorSwapchainFormat = -1; |
| 205 | int64_t m_depthSwapchainFormat = -1; |
| 206 | int m_samples = 1; |
| 207 | |
| 208 | bool m_passThroughEnabled = false; |
| 209 | bool m_passthroughSupported = false; |
| 210 | bool m_enablePassthrough = false; |
| 211 | bool m_multiviewRendering = true; |
| 212 | bool m_spaceExtensionSupported = false; |
| 213 | QQuick3DXrAnchorManager *m_spaceExtension = nullptr; |
| 214 | bool m_colorspaceExtensionSupported = false; |
| 215 | bool m_displayRefreshRateExtensionSupported = false; |
| 216 | bool m_foveationExtensionSupported = false; |
| 217 | #ifdef Q_OS_ANDROID |
| 218 | bool m_androidCreateInstanceExtensionSupported = false; |
| 219 | #endif |
| 220 | XrFoveationLevelFB m_foveationLevel = XR_FOVEATION_LEVEL_HIGH_FB; |
| 221 | bool m_compositionLayerDepthSupported = false; |
| 222 | bool m_submitLayerDepth = false; |
| 223 | bool m_handtrackingExtensionSupported = false; |
| 224 | bool m_handtrackingAimExtensionSupported = false; |
| 225 | bool m_isGraphicsInitialized = false; |
| 226 | |
| 227 | bool m_sessionRunning{false}; |
| 228 | |
| 229 | QQuick3DXrManager *q_ptr = nullptr; |
| 230 | #ifdef XR_USE_PLATFORM_ANDROID |
| 231 | QJniObject m_androidActivity; |
| 232 | JavaVM* m_javaVM = nullptr; |
| 233 | #endif // XR_USE_PLATFORM_ANDROID |
| 234 | |
| 235 | #ifdef XR_EXT_debug_utils |
| 236 | XrDebugUtilsMessengerEXT m_debugMessenger = XR_NULL_HANDLE; |
| 237 | PFN_xrDestroyDebugUtilsMessengerEXT m_xrDestroyDebugUtilsMessengerEXT = nullptr; |
| 238 | #endif |
| 239 | |
| 240 | #if QT_CONFIG(graphicsframecapture) |
| 241 | std::unique_ptr<QGraphicsFrameCapture> m_frameCapture; |
| 242 | #endif |
| 243 | |
| 244 | QAbstractOpenXRGraphics *m_graphics = nullptr; |
| 245 | |
| 246 | }; |
| 247 | |
| 248 | QT_END_NAMESPACE |
| 249 | |
| 250 | #endif // QQUICK3DXRMANAGER_OPENXR_P_H |
| 251 | |