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 | const std::unique_ptr<QSSGRhiQuadRenderer> &rhiQuadRenderer() const; |
73 | const std::unique_ptr<QSSGRhiCubeRenderer> &rhiCubeRenderer() const; |
74 | |
75 | QSSGRenderContextInterface *contextInterface() const { return m_contextInterface; } |
76 | |
77 | protected: |
78 | void cleanupResources(QList<QSSGRenderGraphObject*> &resources); |
79 | void cleanupResources(QSet<QSSGRenderGraphObject*> &resources); |
80 | |
81 | private: |
82 | friend class QSSGRendererPrivate; |
83 | friend class QSSGLayerRenderData; |
84 | friend class QSSGRenderContextInterface; |
85 | friend class QQuick3DSceneRenderer; |
86 | friend class QQuick3DWindowAttachment; |
87 | friend class QSSGCleanupObject; |
88 | |
89 | QSSGLayerRenderData *getOrCreateLayerRenderData(QSSGRenderLayer &layer); |
90 | void beginLayerRender(QSSGLayerRenderData &inLayer); |
91 | void endLayerRender(); |
92 | void addMaterialDirtyClear(QSSGRenderGraphObject *material); |
93 | void cleanupUnreferencedBuffers(QSSGRenderLayer *inLayer); |
94 | void resetResourceCounters(QSSGRenderLayer *inLayer); |
95 | void releaseCachedResources(); |
96 | |
97 | QSSGRenderContextInterface *m_contextInterface = nullptr; // We're own by the context interface |
98 | |
99 | bool m_globalPickingEnabled = false; |
100 | |
101 | // Temporary information stored only when rendering a particular layer. |
102 | QSSGLayerRenderData *m_currentLayer = nullptr; |
103 | QByteArray m_generatedShaderString; |
104 | |
105 | QSet<QSSGRenderGraphObject *> m_materialClearDirty; |
106 | |
107 | mutable std::unique_ptr<QSSGRhiQuadRenderer> m_rhiQuadRenderer; |
108 | mutable std::unique_ptr<QSSGRhiCubeRenderer> m_rhiCubeRenderer; |
109 | |
110 | quint32 m_activeFrameRef = 0; |
111 | quint32 m_frameCount = 0; |
112 | |
113 | // Viewport that this render context should use |
114 | QRect m_viewport; |
115 | float m_dpr = 1.0; |
116 | QRect m_scissorRect; |
117 | }; |
118 | |
119 | class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRendererPrivate |
120 | { |
121 | QSSGRendererPrivate() = default; |
122 | public: |
123 | using PickResultList = QVarLengthArray<QSSGRenderPickResult, 20>; // Lets assume most items are filtered out already |
124 | |
125 | static QSSGRhiShaderPipelinePtr generateRhiShaderPipelineImpl(QSSGSubsetRenderable &renderable, |
126 | QSSGShaderLibraryManager &shaderLibraryManager, |
127 | QSSGShaderCache &shaderCache, |
128 | QSSGProgramGenerator &shaderProgramGenerator, |
129 | const QSSGShaderDefaultMaterialKeyProperties &shaderKeyProperties, |
130 | const QSSGShaderFeatures &featureSet, |
131 | QByteArray &shaderString); |
132 | static QSSGRhiShaderPipelinePtr generateRhiShaderPipeline(QSSGRenderer &renderer, |
133 | QSSGSubsetRenderable &inRenderable, |
134 | const QSSGShaderFeatures &inFeatureSet); |
135 | |
136 | static QSSGRhiShaderPipelinePtr getShaderPipelineForDefaultMaterial(QSSGRenderer &renderer, |
137 | QSSGSubsetRenderable &inRenderable, |
138 | const QSSGShaderFeatures &inFeatureSet); |
139 | |
140 | static void getLayerHitObjectList(const QSSGRenderLayer &layer, |
141 | QSSGBufferManager &bufferManager, |
142 | const QSSGRenderRay &ray, |
143 | bool inPickEverything, |
144 | PickResultList &outIntersectionResult); |
145 | static void intersectRayWithSubsetRenderable(QSSGBufferManager &bufferManager, |
146 | const QSSGRenderRay &inRay, |
147 | const QSSGRenderNode &node, |
148 | PickResultList &outIntersectionResultList); |
149 | static void intersectRayWithItem2D(const QSSGRenderRay &inRay, |
150 | const QSSGRenderItem2D &item2D, |
151 | PickResultList &outIntersectionResultList); |
152 | |
153 | static PickResultList syncPickAll(const QSSGRenderContextInterface &ctx, |
154 | const QSSGRenderLayer &layer, |
155 | const QSSGRenderRay &ray); |
156 | |
157 | static PickResultList syncPick(const QSSGRenderContextInterface &ctx, |
158 | const QSSGRenderLayer &layer, |
159 | const QSSGRenderRay &ray, |
160 | QSSGRenderNode *target = nullptr); |
161 | |
162 | static PickResultList syncPickSubset(const QSSGRenderLayer &layer, |
163 | QSSGBufferManager &bufferManager, |
164 | const QSSGRenderRay &ray, |
165 | QVarLengthArray<QSSGRenderNode *> subset); |
166 | |
167 | // Setting this true enables picking for all the models, regardless of |
168 | // the models pickable property. |
169 | static bool isGlobalPickingEnabled(const QSSGRenderer &renderer) { return renderer.m_globalPickingEnabled; } |
170 | static void setGlobalPickingEnabled(QSSGRenderer &renderer, bool isEnabled); |
171 | |
172 | static void setRenderContextInterface(QSSGRenderer &renderer, QSSGRenderContextInterface *ctx); |
173 | }; |
174 | |
175 | QT_END_NAMESPACE |
176 | |
177 | #endif // QSSG_RENDERER_P_H |
178 | |