| 1 | // Copyright (C) 2008-2012 NVIDIA Corporation. |
| 2 | // Copyright (C) 2019 The Qt Company Ltd. |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 4 | |
| 5 | #ifndef QSSG_RENDERER_P_H |
| 6 | #define QSSG_RENDERER_P_H |
| 7 | |
| 8 | // |
| 9 | // W A R N I N G |
| 10 | // ------------- |
| 11 | // |
| 12 | // This file is not part of the Qt API. It exists purely as an |
| 13 | // implementation detail. This header file may change from version to |
| 14 | // version without notice, or even be removed. |
| 15 | // |
| 16 | // We mean it. |
| 17 | // |
| 18 | |
| 19 | #include <private/qssgrenderpickresult_p.h> |
| 20 | #include <private/qssgrhicontext_p.h> |
| 21 | #include <private/qssgrhiquadrenderer_p.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QSSGShaderCache; |
| 26 | class QSSGProgramGenerator; |
| 27 | class QSSGShaderLibraryManager; |
| 28 | class QSSGBufferManager; |
| 29 | class QSSGLayerRenderData; |
| 30 | class QSSGRenderContextInterface; |
| 31 | struct QSSGRenderNode; |
| 32 | struct QSSGRenderItem2D; |
| 33 | struct QSSGRenderRay; |
| 34 | struct QSSGSubsetRenderable; |
| 35 | struct QSSGShaderDefaultMaterialKeyProperties; |
| 36 | struct QSSGShaderFeatures; |
| 37 | |
| 38 | class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRenderer |
| 39 | { |
| 40 | Q_DISABLE_COPY(QSSGRenderer) |
| 41 | public: |
| 42 | QSSGRenderer(); |
| 43 | ~QSSGRenderer(); |
| 44 | |
| 45 | // Returns true if this layer or a sibling was dirty. |
| 46 | bool prepareLayerForRender(QSSGRenderLayer &inLayer); |
| 47 | |
| 48 | void rhiPrepare(QSSGRenderLayer &inLayer); |
| 49 | void rhiRender(QSSGRenderLayer &inLayer); |
| 50 | |
| 51 | // Clients need to call this every frame in order for various subsystems to release |
| 52 | // temporary per-frame allocated objects. |
| 53 | void beginFrame(QSSGRenderLayer &layer, bool allowRecursion = true); |
| 54 | |
| 55 | // When allowRecursion is true, the cleanup is only done when all |
| 56 | // beginFrames got their corresponding endFrame. This is indicated by the |
| 57 | // return value (false if nothing's been done due to pending "frames") |
| 58 | bool endFrame(QSSGRenderLayer &layer, bool allowRecursion = true); |
| 59 | |
| 60 | // Get the number of times EndFrame has been called |
| 61 | [[nodiscard]] constexpr quint32 frameCount() const { return m_frameCount; } |
| 62 | |
| 63 | void setViewport(QRect inViewport) { m_viewport = inViewport; } |
| 64 | QRect viewport() const { return m_viewport; } |
| 65 | |
| 66 | void setDpr(float dpr) { m_dpr = dpr; } |
| 67 | float dpr() const { return m_dpr; } |
| 68 | |
| 69 | void setScissorRect(QRect inScissorRect) { m_scissorRect = inScissorRect; } |
| 70 | QRect scissorRect() const { return m_scissorRect; } |
| 71 | |
| 72 | quint32 frameDepth() const { return m_activeFrameRef; } |
| 73 | |
| 74 | const std::unique_ptr<QSSGRhiQuadRenderer> &rhiQuadRenderer() const; |
| 75 | const std::unique_ptr<QSSGRhiCubeRenderer> &rhiCubeRenderer() const; |
| 76 | |
| 77 | QSSGRenderContextInterface *contextInterface() const { return m_contextInterface; } |
| 78 | |
| 79 | // Before we start rendering a sublayer(s), e.g., Item2D with View3Ds, |
| 80 | // we need to inform the renderer about it, so we can restore the state as we |
| 81 | // return from the sublayer(s) rendering. The state will be saved in the data set |
| 82 | // for the layer. |
| 83 | void beginSubLayerRender(QSSGLayerRenderData &inLayer); |
| 84 | void endSubLayerRender(QSSGLayerRenderData &inLayer); |
| 85 | |
| 86 | protected: |
| 87 | void cleanupResources(QList<QSSGRenderGraphObject*> &resources); |
| 88 | void cleanupResources(QSet<QSSGRenderGraphObject*> &resources); |
| 89 | |
| 90 | private: |
| 91 | friend class QSSGRendererPrivate; |
| 92 | friend class QSSGLayerRenderData; |
| 93 | friend class QSSGRenderContextInterface; |
| 94 | friend class QQuick3DSceneRenderer; |
| 95 | friend class QQuick3DWindowAttachment; |
| 96 | friend class QSSGCleanupObject; |
| 97 | |
| 98 | QSSGLayerRenderData *getOrCreateLayerRenderData(QSSGRenderLayer &layer); |
| 99 | void beginLayerRender(QSSGLayerRenderData &inLayer); |
| 100 | void endLayerRender(); |
| 101 | void addMaterialDirtyClear(QSSGRenderGraphObject *material); |
| 102 | void cleanupUnreferencedBuffers(QSSGRenderLayer *inLayer); |
| 103 | void resetResourceCounters(QSSGRenderLayer *inLayer); |
| 104 | void releaseCachedResources(); |
| 105 | |
| 106 | QSSGRenderContextInterface *m_contextInterface = nullptr; // We're own by the context interface |
| 107 | |
| 108 | bool m_globalPickingEnabled = false; |
| 109 | |
| 110 | // Temporary information stored only when rendering a particular layer. |
| 111 | QSSGLayerRenderData *m_currentLayer = nullptr; |
| 112 | |
| 113 | QSet<QSSGRenderGraphObject *> m_materialClearDirty; |
| 114 | |
| 115 | mutable std::unique_ptr<QSSGRhiQuadRenderer> m_rhiQuadRenderer; |
| 116 | mutable std::unique_ptr<QSSGRhiCubeRenderer> m_rhiCubeRenderer; |
| 117 | |
| 118 | quint32 m_activeFrameRef = 0; |
| 119 | quint32 m_frameCount = 0; |
| 120 | |
| 121 | // Viewport that this render context should use |
| 122 | QRect m_viewport; |
| 123 | float m_dpr = 1.0; |
| 124 | QRect m_scissorRect; |
| 125 | }; |
| 126 | |
| 127 | class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRendererPrivate |
| 128 | { |
| 129 | QSSGRendererPrivate() = default; |
| 130 | public: |
| 131 | using PickResultList = QVarLengthArray<QSSGRenderPickResult, 20>; // Lets assume most items are filtered out already |
| 132 | |
| 133 | static QSSGRhiShaderPipelinePtr generateRhiShaderPipelineImpl(QSSGSubsetRenderable &renderable, |
| 134 | QSSGShaderLibraryManager &shaderLibraryManager, |
| 135 | QSSGShaderCache &shaderCache, |
| 136 | QSSGProgramGenerator &shaderProgramGenerator, |
| 137 | const QSSGShaderDefaultMaterialKeyProperties &shaderKeyProperties, |
| 138 | const QSSGShaderFeatures &featureSet, |
| 139 | QByteArray &shaderString); |
| 140 | static QSSGRhiShaderPipelinePtr generateRhiShaderPipeline(QSSGRenderer &renderer, |
| 141 | QSSGSubsetRenderable &inRenderable, |
| 142 | const QSSGShaderFeatures &inFeatureSet); |
| 143 | |
| 144 | static QSSGRhiShaderPipelinePtr getShaderPipelineForDefaultMaterial(QSSGRenderer &renderer, |
| 145 | QSSGSubsetRenderable &inRenderable, |
| 146 | const QSSGShaderFeatures &inFeatureSet); |
| 147 | |
| 148 | static void getLayerHitObjectList(const QSSGRenderLayer &layer, |
| 149 | QSSGBufferManager &bufferManager, |
| 150 | const QSSGRenderRay &ray, |
| 151 | bool inPickEverything, |
| 152 | PickResultList &outIntersectionResult); |
| 153 | static void intersectRayWithSubsetRenderable(QSSGBufferManager &bufferManager, |
| 154 | const QSSGRenderRay &inRay, |
| 155 | const QSSGRenderNode &node, |
| 156 | PickResultList &outIntersectionResultList); |
| 157 | static void intersectRayWithItem2D(const QSSGRenderRay &inRay, |
| 158 | const QSSGRenderItem2D &item2D, |
| 159 | PickResultList &outIntersectionResultList); |
| 160 | |
| 161 | static PickResultList syncPickAll(const QSSGRenderContextInterface &ctx, |
| 162 | const QSSGRenderLayer &layer, |
| 163 | const QSSGRenderRay &ray); |
| 164 | |
| 165 | static PickResultList syncPick(const QSSGRenderContextInterface &ctx, |
| 166 | const QSSGRenderLayer &layer, |
| 167 | const QSSGRenderRay &ray, |
| 168 | QSSGRenderNode *target = nullptr); |
| 169 | |
| 170 | static PickResultList syncPickSubset(const QSSGRenderLayer &layer, |
| 171 | QSSGBufferManager &bufferManager, |
| 172 | const QSSGRenderRay &ray, |
| 173 | QVarLengthArray<QSSGRenderNode *> subset); |
| 174 | |
| 175 | // Setting this true enables picking for all the models, regardless of |
| 176 | // the models pickable property. |
| 177 | static bool isGlobalPickingEnabled(const QSSGRenderer &renderer) { return renderer.m_globalPickingEnabled; } |
| 178 | static void setGlobalPickingEnabled(QSSGRenderer &renderer, bool isEnabled); |
| 179 | |
| 180 | static void setRenderContextInterface(QSSGRenderer &renderer, QSSGRenderContextInterface *ctx); |
| 181 | }; |
| 182 | |
| 183 | QT_END_NAMESPACE |
| 184 | |
| 185 | #endif // QSSG_RENDERER_P_H |
| 186 | |