| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QSSGSCENEMANAGER_P_H |
| 5 | #define QSSGSCENEMANAGER_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 <QtCore/QObject> |
| 19 | #include <QtCore/QSet> |
| 20 | |
| 21 | #include <QtQuick3D/private/qtquick3dglobal_p.h> |
| 22 | |
| 23 | #include <QtQuick3DRuntimeRender/private/qssgrendergraphobject_p.h> |
| 24 | |
| 25 | #include "qquick3dnode_p.h" |
| 26 | |
| 27 | #include <QtCore/qpointer.h> |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | class QSGDynamicTexture; |
| 32 | class QQuickWindow; |
| 33 | class QSSGBufferManager; |
| 34 | class QSSGRenderContextInterface; |
| 35 | |
| 36 | class Q_QUICK3D_EXPORT QQuick3DWindowAttachment : public QObject |
| 37 | { |
| 38 | Q_OBJECT |
| 39 | public: |
| 40 | enum SyncResultFlag : quint32 |
| 41 | { |
| 42 | None, |
| 43 | SharedResourcesDirty = 0x1, |
| 44 | ExtensionsDiry = 0x2, |
| 45 | }; |
| 46 | |
| 47 | using SyncResult = std::underlying_type_t<SyncResultFlag>; |
| 48 | |
| 49 | explicit QQuick3DWindowAttachment(QQuickWindow *window); |
| 50 | ~QQuick3DWindowAttachment() override; |
| 51 | |
| 52 | Q_INVOKABLE void preSync(); |
| 53 | Q_INVOKABLE void cleanupResources(); |
| 54 | Q_INVOKABLE SyncResult synchronize(QSet<QSSGRenderGraphObject *> &resourceLoaders); |
| 55 | Q_INVOKABLE void requestUpdate(); |
| 56 | Q_INVOKABLE void evaluateEol(); |
| 57 | |
| 58 | QQuickWindow *window() const; |
| 59 | |
| 60 | const std::shared_ptr<QSSGRenderContextInterface> &rci() const { return m_rci; } |
| 61 | void setRci(const std::shared_ptr<QSSGRenderContextInterface> &rciptr); |
| 62 | |
| 63 | void registerSceneManager(QQuick3DSceneManager &manager); |
| 64 | void unregisterSceneManager(QQuick3DSceneManager &manager); |
| 65 | |
| 66 | void queueForCleanup(QSSGRenderGraphObject *obj); |
| 67 | void queueForCleanup(QQuick3DSceneManager *manager); |
| 68 | |
| 69 | Q_SIGNALS: |
| 70 | void releaseCachedResources(); |
| 71 | void renderContextInterfaceChanged(); |
| 72 | |
| 73 | private: |
| 74 | Q_INVOKABLE void onReleaseCachedResources(); |
| 75 | Q_INVOKABLE void onInvalidated(); |
| 76 | |
| 77 | QPointer<QQuickWindow> m_window; |
| 78 | std::shared_ptr<QSSGRenderContextInterface> m_rci; |
| 79 | QList<QQuick3DSceneManager *> sceneManagers; |
| 80 | QList<QQuick3DSceneManager *> sceneManagerCleanupQueue; |
| 81 | QList<QSSGRenderGraphObject *> pendingResourceCleanupQueue; |
| 82 | QSet<QSSGRenderGraphObject *> resourceCleanupQueue; |
| 83 | }; |
| 84 | |
| 85 | class Q_QUICK3D_EXPORT QQuick3DSceneManager : public QObject |
| 86 | { |
| 87 | Q_OBJECT |
| 88 | public: |
| 89 | using SyncResultFlag = QQuick3DWindowAttachment::SyncResultFlag; |
| 90 | using SyncResult = QQuick3DWindowAttachment::SyncResult; |
| 91 | |
| 92 | explicit QQuick3DSceneManager(QObject *parent = nullptr); |
| 93 | ~QQuick3DSceneManager() override; |
| 94 | |
| 95 | void setWindow(QQuickWindow *window); |
| 96 | QQuickWindow *window(); |
| 97 | |
| 98 | void dirtyItem(QQuick3DObject *item); |
| 99 | void requestUpdate(); |
| 100 | void cleanup(QSSGRenderGraphObject *item); |
| 101 | |
| 102 | void polishItems(); |
| 103 | void forcePolish(); |
| 104 | void sync(); |
| 105 | void preSync(); |
| 106 | |
| 107 | SyncResult cleanupNodes(); |
| 108 | SyncResult updateDirtyResourceNodes(); |
| 109 | void updateDirtySpatialNodes(); |
| 110 | SyncResult updateDiryExtensions(); |
| 111 | SyncResult updateDirtyResourceSecondPass(); |
| 112 | |
| 113 | void updateDirtyResource(QQuick3DObject *resourceObject); |
| 114 | void updateDirtySpatialNode(QQuick3DNode *spatialNode); |
| 115 | void updateBoundingBoxes(QSSGBufferManager &mgr); |
| 116 | |
| 117 | QQuick3DObject *lookUpNode(const QSSGRenderGraphObject *node) const; |
| 118 | |
| 119 | // Where the enumerator is placed will decide the priority it gets. |
| 120 | // NOTE: Place new list types before 'Count'. |
| 121 | // NOTE: InstanceNodes are nodes that have an instance root set, we'll process these |
| 122 | // after the other nodes but before light nodes; this implies that lights are not good candidates |
| 123 | // for being instance roots... |
| 124 | enum class NodePriority { Skeleton, Other, ModelWithInstanceRoot, Lights, Count }; |
| 125 | enum class ResourcePriority { TextureData, Texture, Other, Count }; |
| 126 | enum class ExtensionPriority { RenderExtension, Count }; |
| 127 | |
| 128 | static inline size_t resourceListIndex(QSSGRenderGraphObject::Type type) |
| 129 | { |
| 130 | Q_ASSERT(!QSSGRenderGraphObject::isNodeType(type)); |
| 131 | |
| 132 | if (QSSGRenderGraphObject::isTexture(type)) |
| 133 | return size_t(ResourcePriority::Texture); |
| 134 | |
| 135 | if (type == QSSGRenderGraphObject::Type::TextureData) |
| 136 | return size_t(ResourcePriority::TextureData); |
| 137 | |
| 138 | return size_t(ResourcePriority::Other); |
| 139 | } |
| 140 | |
| 141 | static inline size_t nodeListIndex(QSSGRenderGraphObject::Type type) |
| 142 | { |
| 143 | Q_ASSERT(QSSGRenderGraphObject::isNodeType(type)); |
| 144 | |
| 145 | if (QSSGRenderGraphObject::isLight(type)) |
| 146 | return size_t(NodePriority::Lights); |
| 147 | |
| 148 | if (type == QSSGRenderGraphObject::Type::Skeleton) |
| 149 | return size_t(NodePriority::Skeleton); |
| 150 | |
| 151 | return size_t(NodePriority::Other); |
| 152 | } |
| 153 | |
| 154 | static constexpr size_t extensionListIndex(QSSGRenderGraphObject::Type type) |
| 155 | { |
| 156 | Q_ASSERT(QSSGRenderGraphObject::isExtension(type)); |
| 157 | |
| 158 | return size_t(ExtensionPriority::RenderExtension); |
| 159 | } |
| 160 | |
| 161 | static QQuick3DWindowAttachment *getOrSetWindowAttachment(QQuickWindow &window); |
| 162 | |
| 163 | QQuick3DObject *dirtyResources[size_t(ResourcePriority::Count)] {}; |
| 164 | QQuick3DObject *dirtyNodes[size_t(NodePriority::Count)] {}; |
| 165 | QQuick3DObject *dirtyExtensions[size_t(ExtensionPriority::Count)] {}; |
| 166 | // For exceptions to the norm we create a list of resources that |
| 167 | // we get a second update. |
| 168 | // In the case of the render extensions the resources are update first and for the |
| 169 | // first time the extensions have not been run and therefore have no backend node, which |
| 170 | // we'll need to use connect the render result from the extension with the texture. |
| 171 | QSet<QQuick3DObject *> dirtySecondPassResources; |
| 172 | |
| 173 | QList<QQuick3DObject *> dirtyBoundingBoxList; |
| 174 | QSet<QSSGRenderGraphObject *> cleanupNodeList; |
| 175 | QList<QSSGRenderGraphObject *> resourceCleanupQueue; |
| 176 | |
| 177 | QSet<QQuick3DObject *> parentlessItems; |
| 178 | QVector<QSGDynamicTexture *> qsgDynamicTextures; |
| 179 | QHash<QSSGRenderGraphObject *, QQuick3DObject *> m_nodeMap; |
| 180 | QSet<QSSGRenderGraphObject *> resourceLoaders; |
| 181 | QQuickWindow *m_window = nullptr; |
| 182 | QPointer<QQuick3DWindowAttachment> wattached; |
| 183 | int inputHandlingEnabled = 0; // Holds the count of active item2Ds, input disabled if zero. |
| 184 | bool sharedResourceRemoved = false; |
| 185 | friend QQuick3DObject; |
| 186 | |
| 187 | Q_SIGNALS: |
| 188 | void needsUpdate(); |
| 189 | void windowChanged(); |
| 190 | |
| 191 | private Q_SLOTS: |
| 192 | SyncResult updateResources(QQuick3DObject **listHead); |
| 193 | void updateNodes(QQuick3DObject **listHead); |
| 194 | SyncResult updateExtensions(QQuick3DObject **listHead); |
| 195 | }; |
| 196 | |
| 197 | class QSSGCleanupObject : public QObject |
| 198 | { |
| 199 | Q_OBJECT |
| 200 | public: |
| 201 | QSSGCleanupObject(std::shared_ptr<QSSGRenderContextInterface> rci, |
| 202 | QList<QSSGRenderGraphObject *> resourceCleanupQueue, |
| 203 | QQuickWindow *window); |
| 204 | |
| 205 | ~QSSGCleanupObject() override; |
| 206 | |
| 207 | Q_INVOKABLE void cleanupResources(); |
| 208 | |
| 209 | private: |
| 210 | std::shared_ptr<QSSGRenderContextInterface> m_rci; |
| 211 | QPointer<QQuickWindow> m_window; |
| 212 | QList<QSSGRenderGraphObject *> m_resourceCleanupQueue; |
| 213 | }; |
| 214 | |
| 215 | QT_END_NAMESPACE |
| 216 | |
| 217 | QML_DECLARE_TYPE(QQuick3DSceneManager) |
| 218 | |
| 219 | #endif // QSSGSCENEMANAGER_P_H |
| 220 | |