Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qssgrenderer_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
5
6
7#ifndef QSSG_RENDERER_P_H
8#define QSSG_RENDERER_P_H
9
10//
11// W A R N I N G
12// -------------
13//
14// This file is not part of the Qt API. It exists purely as an
15// implementation detail. This header file may change from version to
16// version without notice, or even be removed.
17//
18// We mean it.
19//
20
21#include <private/qssgrenderpickresult_p.h>
22#include <private/qssgrhicontext_p.h>
23#include <private/qssgrhiquadrenderer_p.h>
24
25#include <QtCore/qpointer.h>
26
27#include <vector>
28
29QT_BEGIN_NAMESPACE
30
31class QSSGShaderCache;
32class QSSGProgramGenerator;
33class QSSGShaderLibraryManager;
34class QSSGBufferManager;
37struct QSSGRenderNode;
38struct QSSGRenderItem2D;
39struct QSSGRenderRay;
40class QSSGSubsetRenderable;
42struct QSSGShaderFeatures;
44class QSGRenderContext;
45class QSSGFrustum;
47
48class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRenderer
49{
50 Q_DISABLE_COPY(QSSGRenderer)
51public:
52 QSSGRenderer();
53 ~QSSGRenderer();
54
55 // Returns true if this layer or a sibling was dirty.
56 bool prepareLayerForRender(QSSGRenderLayer &inLayer);
57
58 void rhiPrepare(QSSGRenderLayer &inLayer);
59 void rhiRender(QSSGRenderLayer &inLayer);
60
61 // Clients need to call this every frame in order for various subsystems to release
62 // temporary per-frame allocated objects.
63 void beginFrame(QSSGRenderLayer &layer, bool allowRecursion = true);
64
65 // When allowRecursion is true, the cleanup is only done when all
66 // beginFrames got their corresponding endFrame. This is indicated by the
67 // return value (false if nothing's been done due to pending "frames")
68 bool endFrame(QSSGRenderLayer &layer, bool allowRecursion = true);
69
70 // Get the number of times EndFrame has been called
71 [[nodiscard]] constexpr quint32 frameCount() const { return m_frameCount; }
72
73 void setViewport(QRect inViewport) { m_viewport = inViewport; }
74 QRect viewport() const { return m_viewport; }
75
76 void setDpr(float dpr) { m_dpr = dpr; }
77 float dpr() const { return m_dpr; }
78
79 void setScissorRect(QRect inScissorRect) { m_scissorRect = inScissorRect; }
80 QRect scissorRect() const { return m_scissorRect; }
81
82 quint32 frameDepth() const { return m_activeFrameRef; }
83
86
87 QSSGRenderContextInterface *contextInterface() const { return m_contextInterface; }
88
89 // Before we start rendering a sublayer(s), e.g., Item2D with View3Ds,
90 // we need to inform the renderer about it, so we can restore the state as we
91 // return from the sublayer(s) rendering. The state will be saved in the data set
92 // for the layer.
95
96 void releaseItem2DData(const QSSGRenderItem2D &item2D);
97
98protected:
99 void cleanupResources(QList<QSSGRenderGraphObject*> &resources);
100 void cleanupResources(QSet<QSSGRenderGraphObject*> &resources);
101
102private:
103 friend class QSSGRendererPrivate;
108 friend class QSSGCleanupObject;
110
111 QSSGLayerRenderData *getOrCreateLayerRenderData(QSSGRenderLayer &layer);
112 void beginLayerRender(QSSGLayerRenderData &inLayer);
113 void endLayerRender();
114 void addMaterialDirtyClear(QSSGRenderGraphObject *material);
115 void cleanupUnreferencedBuffers(QSSGRenderLayer *inLayer);
116 void resetResourceCounters(QSSGRenderLayer *inLayer);
117 void releaseCachedResources();
118
119 void registerItem2DData(QSSGRenderItem2DData &data);
120 void unregisterItem2DData(QSSGRenderItem2DData &data);
121
122 QSSGRenderContextInterface *m_contextInterface = nullptr; // We're own by the context interface
123
124 bool m_globalPickingEnabled = false;
125
126 // Temporary information stored only when rendering a particular layer.
127 QSSGLayerRenderData *m_currentLayer = nullptr;
128
129 QSet<QSSGRenderGraphObject *> m_materialClearDirty;
130
131 QPointer<QSGRenderContext> m_qsgRenderContext;
132
133 // Item2D data (per layer).
134 // Note that the data is own by the layer.
135 std::vector<QSSGRenderItem2DData *> m_item2DDatas;
136
137 mutable std::unique_ptr<QSSGRhiQuadRenderer> m_rhiQuadRenderer;
138 mutable std::unique_ptr<QSSGRhiCubeRenderer> m_rhiCubeRenderer;
139
140 quint32 m_activeFrameRef = 0;
141 quint32 m_frameCount = 0;
142
143 // Viewport that this render context should use
144 QRect m_viewport;
145 float m_dpr = 1.0;
146 QRect m_scissorRect;
147};
148
149class Q_QUICK3DRUNTIMERENDER_EXPORT QSSGRendererPrivate
150{
151 QSSGRendererPrivate() = default;
152public:
153 using PickResultList = QVarLengthArray<QSSGRenderPickResult, 20>; // Lets assume most items are filtered out already
154
155 static QSSGRhiShaderPipelinePtr generateRhiShaderPipelineImpl(QSSGSubsetRenderable &renderable,
156 QSSGShaderLibraryManager &shaderLibraryManager,
157 QSSGShaderCache &shaderCache,
158 QSSGProgramGenerator &shaderProgramGenerator,
159 const QSSGShaderDefaultMaterialKeyProperties &shaderKeyProperties,
160 const QSSGShaderFeatures &featureSet,
161 const QSSGUserShaderAugmentation &shaderAugmentation,
162 QByteArray &shaderString);
163 static QSSGRhiShaderPipelinePtr generateRhiShaderPipeline(QSSGRenderer &renderer,
164 QSSGSubsetRenderable &inRenderable,
165 const QSSGShaderFeatures &inFeatureSet,
166 const QSSGUserShaderAugmentation &shaderAugmentation);
167
168 static QSSGRhiShaderPipelinePtr getShaderPipelineForDefaultMaterial(QSSGRenderer &renderer,
169 QSSGSubsetRenderable &inRenderable,
170 const QSSGShaderFeatures &inFeatureSet,
171 const QSSGUserShaderAugmentation &shaderAugmentation);
172
173 static void getLayerHitObjectList(const QSSGRenderLayer &layer,
174 QSSGBufferManager &bufferManager,
175 const QSSGRenderRay &ray,
176 bool inPickEverything,
177 PickResultList &outIntersectionResult);
178 static void intersectRayWithSubsetRenderable(const QSSGRenderLayer &layer, QSSGBufferManager &bufferManager,
179 const QSSGRenderRay &inRay,
180 const QSSGRenderNode &node,
181 PickResultList &outIntersectionResultList);
182 static void intersectRayWithItem2D(const QSSGRenderLayer &layer, const QSSGRenderRay &inRay,
183 const QSSGRenderItem2D &item2D,
184 PickResultList &outIntersectionResultList);
185
186 static std::optional<QSSGRenderPickResult> closestPointOnSubsetRenderable(const QSSGRenderLayer &layer,
187 QSSGBufferManager &bufferManager,
188 const QVector3D &center,
189 float radiusSquared,
190 const QSSGRenderNode &node);
191
192 static PickResultList syncPickAll(const QSSGRenderContextInterface &ctx,
193 const QSSGRenderLayer &layer,
194 const QSSGRenderRay &ray);
195
196 static PickResultList syncPick(const QSSGRenderContextInterface &ctx,
197 const QSSGRenderLayer &layer,
198 const QSSGRenderRay &ray,
199 QSSGRenderNode *target = nullptr);
200
201 static PickResultList syncPickSubset(const QSSGRenderLayer &layer,
202 QSSGBufferManager &bufferManager,
203 const QSSGRenderRay &ray,
204 QVarLengthArray<QSSGRenderNode *> subset);
205
206 static std::optional<QSSGRenderPickResult> syncPickClosestPoint(const QSSGRenderContextInterface &ctx,
207 const QSSGRenderLayer &layer,
208 const QVector3D &center,
209 const float radiusSquared,
210 QSSGRenderNode *target = nullptr);
211
212 static QList<const QSSGRenderNode *> syncPickInFrustum(const QSSGRenderContextInterface &ctx,
213 const QSSGRenderLayer &layer,
214 const QSSGFrustum &frustum);
215
216 // Setting this true enables picking for all the models, regardless of
217 // the models pickable property.
218 static bool isGlobalPickingEnabled(const QSSGRenderer &renderer) { return renderer.m_globalPickingEnabled; }
219 static void setGlobalPickingEnabled(QSSGRenderer &renderer, bool isEnabled);
220
221 static void setRenderContextInterface(QSSGRenderer &renderer, QSSGRenderContextInterface *ctx);
222 static void setSgRenderContext(QSSGRenderer &renderer, QSGRenderContext *sgRenderCtx);
223
224 static QSSGLayerRenderData *getCurrentRenderData(const QSSGRenderer &renderer) { return renderer.m_currentLayer; }
225
226 static QSGRenderContext *getSgRenderContext(const QSSGRenderer &renderer);
227
228 static constexpr float minimumRenderOpacity = .01f;
229};
230
231QT_END_NAMESPACE
232
233#endif // QSSG_RENDERER_P_H
QRect viewport() const
Returns the viewport rectangle.
void drawLine(const QVector3D &startPoint, const QVector3D &endPoint, const QColor &color, bool isPersistent=false)
void recordRenderDebugObjects(QSSGRhiContext *rhiCtx, QSSGRhiGraphicsPipelineState *ps, QRhiShaderResourceBindings *srb, QRhiRenderPassDescriptor *rpDesc)
void drawPoint(const QVector3D &vertex, const QColor &color, bool isPersistent=false)
void prepareGeometry(QSSGRhiContext *rhiCtx, QRhiResourceUpdateBatch *rub)
void drawBounds(const QSSGBounds3 &bounds, const QColor &color, bool isPersistent=false)
QSSGRenderableObjectFlags renderableFlags
friend class QSSGParticleRenderer
void releaseItem2DData(const QSSGRenderItem2D &item2D)
const std::unique_ptr< QSSGRhiQuadRenderer > & rhiQuadRenderer() const
quint32 frameDepth() const
float dpr() const
friend class QSSGRenderContextInterface
QRect scissorRect() const
void cleanupResources(QSet< QSSGRenderGraphObject * > &resources)
void setViewport(QRect inViewport)
void beginSubLayerRender(QSSGLayerRenderData &inLayer)
const std::unique_ptr< QSSGRhiCubeRenderer > & rhiCubeRenderer() const
void setDpr(float dpr)
void setScissorRect(QRect inScissorRect)
void endSubLayerRender(QSSGLayerRenderData &inLayer)
QSSGRenderContextInterface * contextInterface() const
Combined button and popup list for selecting options.
qsizetype rhiPrepareOverrideMaterialUserPass(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, const QSSGRhiGraphicsPipelineState &basePipelineState, QRhiRenderPassDescriptor *rpDesc, QSSGRenderGraphObject *overrideMaterial, QSSGLayerRenderData &inData, QSSGRenderableObjectList &inObjects, QSSGShaderFeatures featureSet)
void rhiRenderUserAugmentedPass(QSSGRhiContext *rhiCtx, QSSGRenderableObjectList &inObjects)
quint32 rhiCalculateABufferSize(int nodeCount)
void rhiRenderAoTexture(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, QSSGRenderer &renderer, QSSGRhiShaderPipeline &shaderPipeline, QSSGRhiGraphicsPipelineState &ps, const QSSGAmbientOcclusionSettings &ao, const QSSGRhiRenderableTexture &rhiAoTexture, const QSSGRhiRenderableTexture &rhiDepthTexture, const QSSGRenderCamera &camera)
bool rhiPrepareNormalPass(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, const QSSGRhiGraphicsPipelineState &basePipelineState, QRhiRenderPassDescriptor *rpDesc, QSSGLayerRenderData &inData, const QSSGRenderableObjectList &sortedOpaqueObjects)
bool rhiPrepareMotionVectorTexture(QSSGRhiContext *rhiCtx, const QSize &size, QSSGRhiRenderableTexture *renderableTex)
void rhiRenderReflectionMap(const QSSGRenderContextInterface &context, QSSGPassKey passKey, const QSSGLayerRenderData &inData, QSSGRhiGraphicsPipelineState *ps, QSSGRenderReflectionMap &reflectionMapManager, const std::vector< QSSGRenderReflectionProbe * > &reflectionProbes, const QSSGRenderableObjectList &reflectionPassObjects, QSSGRenderer &renderer)
void rhiRenderNormalPass(QSSGRhiContext *rhiCtx, const QSSGRhiGraphicsPipelineState &ps, const QSSGRenderableObjectList &sortedOpaqueObjects, bool *needsSetViewport)
Q_QUICK3DRUNTIMERENDER_EXPORT void rhiRenderRenderable(QSSGRhiContext *rhiCtx, const QSSGRhiGraphicsPipelineState &state, QSSGRenderableObject &object, bool *needsSetViewport, QSSGRenderTextureCubeFace cubeFace=QSSGRenderTextureCubeFaceNone, qsizetype userPassIndex=-1)
qsizetype rhiPrepareAugmentedUserPass(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, const QSSGRhiGraphicsPipelineState &basePipelineState, QRhiRenderPassDescriptor *rpDesc, const QSSGUserShaderAugmentation &shaderAugmentation, const QSSGLayerRenderData &inData, QSSGRenderableObjectList &inObjects, QSSGShaderFeatures featureSet)
bool rhiPrepareDepthPass(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, const QSSGRhiGraphicsPipelineState &basePipelineState, QRhiRenderPassDescriptor *rpDesc, QSSGLayerRenderData &inData, const QSSGRenderableObjectList &sortedOpaqueObjects, const QSSGRenderableObjectList &sortedTransparentObjects, int samples, int viewCount)
qsizetype rhiPrepareOriginalMaterialUserPass(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, const QSSGRhiGraphicsPipelineState &basePipelineState, QRhiRenderPassDescriptor *rpDesc, const QSSGLayerRenderData &inData, QSSGRenderableObjectList &inObjects, QSSGShaderFeatures featureSet)
bool rhiPrepareScreenTexture(QSSGRhiContext *rhiCtx, const QSize &size, bool wantsMips, QSSGRhiRenderableTexture *renderableTex, quint8 viewCount)
QRect correctViewportCoordinates(const QRectF &layerViewport, const QRect &deviceRect)
void addAccumulatorImageBindings(QSSGRhiShaderPipeline *shaderPipeline, QSSGRhiShaderResourceBindingList &bindings)
bool rhiPrepareDepthTexture(QSSGRhiContext *rhiCtx, const QSize &size, QSSGRhiRenderableTexture *renderableTex, quint8 viewCount, int samples=1)
void rhiRenderMotionVector(QSSGRhiContext *rhiCtx, const QSSGRhiGraphicsPipelineState &state, const QSSGRenderableObjectList *motionVectorPassObjects, int bucketsCount)
bool rhiPrepareAoTexture(QSSGRhiContext *rhiCtx, const QSize &size, QSSGRhiRenderableTexture *renderableTex, quint8 viewCount)
quint32 rhiCalculateABufferSize(const QSize &size, int levels)
void rhiRenderDepthPass(QSSGRhiContext *rhiCtx, const QSSGRhiGraphicsPipelineState &ps, const QSSGRenderableObjectList &sortedOpaqueObjects, const QSSGRenderableObjectList &sortedTransparentObjects, bool *needsSetViewport)
void addDebugFrustum(const QSSGBoxPoints &frustumPoints, const QColor &color, QSSGDebugDrawSystem *debugDrawSystem)
void addDebugBox(const QSSGBoxPoints &boxUnsorted, const QColor &color, QSSGDebugDrawSystem *debugDrawSystem)
void addDirectionalLightDebugBox(const QSSGBoxPoints &box, QSSGDebugDrawSystem *debugDrawSystem)
#define M_PI_2
Definition qmath.h:205
#define M_PI
Definition qmath.h:201
QVarLengthArray< QSSGShaderLight, 16 > QSSGShaderLightList
static void setupCubeShadowCameras(const QSSGLayerRenderData &inData, const QSSGRenderLight *inLight, float shadowMapFar, QSSGRenderCamera inCameras[6])
static void updateUniformsForDefaultMaterial(QSSGRhiShaderPipeline &shaderPipeline, QSSGRhiContext *rhiCtx, const QSSGLayerRenderData &inData, char *ubufData, QSSGRhiGraphicsPipelineState *ps, QSSGSubsetRenderable &subsetRenderable, const QSSGRenderCameraList &cameras, const QVector2D *depthAdjust, const QMatrix4x4 *alteredModelViewProjection, const QSSGRenderGraphObject *materialOverride=nullptr)
static QSSGRhiShaderPipelinePtr shadersForParticleMaterial(QSSGRhiGraphicsPipelineState *ps, QSSGParticlesRenderable &particleRenderable, const QSSGShaderFeatures &featureSet)
static QSSGRhiShaderPipelinePtr shadersForDefaultMaterial(QSSGRhiGraphicsPipelineState *ps, QSSGSubsetRenderable &subsetRenderable, const QSSGShaderFeatures &featureSet, const QSSGUserShaderAugmentation &shaderAugmentation={})
static std::unique_ptr< QSSGRenderCamera > computeShadowCameraFromFrustum(const QMatrix4x4 &lightMatrix, const QMatrix4x4 &lightMatrixInverted, const QVector3D &lightPivot, const QVector3D &lightForward, const QVector3D &lightUp, const float shadowMapResolution, const float pcfRadius, const QSSGBoxPoints &frustumPoints, float frustumStartT, float frustumEndT, float frustumRadius, bool lockShadowmapTexels, const QSSGBounds3 &castingBox, const QSSGBounds3 &receivingBox, QSSGDebugDrawSystem *debugDrawSystem, const bool drawCascades, const bool drawSceneCascadeIntersection)
static QRhiViewport calculateAtlasViewport(QSize atlasPixelSize, const QSSGShadowMapEntry::AtlasEntry &atlasEntry, bool yIsUp)
static QSSGBoxPoints sliceFrustum(const QSSGBoxPoints &frustumPoints, float t0, float t1)
static void rhiPrepareSkyBox_helper(const QSSGRenderContextInterface &context, QSSGPassKey passKey, QSSGRenderLayer &layer, QSSGRenderCameraList &cameras, QSSGRenderer &renderer, QSSGReflectionMapEntry *entry=nullptr, QSSGRenderTextureCubeFace cubeFace=QSSGRenderTextureCubeFaceNone, uint tonemapMode=0)
static QT_BEGIN_NAMESPACE constexpr float QSSG_PI
static void rhiPrepareResourcesForReflectionMap(const QSSGRenderContextInterface &context, QSSGPassKey passKey, const QSSGLayerRenderData &inData, QSSGReflectionMapEntry *pEntry, QSSGRhiGraphicsPipelineState *ps, const QSSGRenderableObjectList &sortedOpaqueObjects, QSSGRenderCamera &inCamera, QSSGRenderer &renderer, QSSGRenderTextureCubeFace cubeFace)
static void rhiPrepareResourcesForShadowMap(QSSGRhiContext *rhiCtx, const QSSGLayerRenderData &inData, QSSGPassKey passKey, QSSGShadowMapEntry *pEntry, QSSGRhiGraphicsPipelineState *ps, const QVector2D *depthAdjust, const QSSGRenderableObjectList &sortedOpaqueObjects, QSSGRenderCamera &inCamera, bool orthographic, QSSGRenderTextureCubeFace cubeFace, quint32 cascadeIndex)
static void addOpaqueDepthPrePassBindings(QSSGRhiContext *rhiCtx, QSSGRhiShaderPipeline *shaderPipeline, QSSGRenderableImage *renderableImage, QSSGRhiShaderResourceBindingList &bindings, bool isCustomMaterialMeshSubset=false)
static QSSGBoxPoints computeFrustumBounds(const QMatrix4x4 &projection)
static void addNormalTextureBindings(QSSGRhiContext *rhiCtx, QSSGRhiShaderPipeline *shaderPipeline, QSSGRhiShaderResourceBindingList &bindings)
static const QRhiShaderResourceBinding::StageFlags RENDERER_VISIBILITY_ALL
static QRhiShaderResourceBindings * drawCallDataSrb(QSSGRhiContextPrivate *rhiCtxD, QSSGRhiDrawCallData &dcd, const QSSGRhiShaderResourceBindingList &bindings)
static constexpr float QSSG_HALFPI
void rhiPrepareRenderableImp(QSSGRhiContext *rhiCtx, QSSGPassKey passKey, const QSSGLayerRenderData &inData, QSSGRenderableObject &inObject, QRhiRenderPassDescriptor *renderPassDescriptor, QSSGRhiGraphicsPipelineState *ps, QSSGShaderFeatures featureSet, int samples, int viewCount, bool screenMapPass, QSSGRenderCamera *alteredCamera, QMatrix4x4 *alteredModelViewProjection, QSSGRenderTextureCubeFace cubeFace, QSSGReflectionMapEntry *entry, bool oit)
static int setupInstancing(QSSGSubsetRenderable *renderable, QSSGRhiGraphicsPipelineState *ps, QSSGRhiContext *rhiCtx, const QVector3D &cameraDirection, const QVector3D &cameraPosition)
static void setupCubeReflectionCameras(const QSSGLayerRenderData &inData, const QSSGRenderReflectionProbe *inProbe, QSSGRenderCamera inCameras[6])
static void fillTargetBlend(QRhiGraphicsPipeline::TargetBlend *targetBlend, QSSGRenderDefaultMaterial::MaterialBlendMode materialBlend)
static void addDepthTextureBindings(QSSGRhiContext *rhiCtx, QSSGRhiShaderPipeline *shaderPipeline, QSSGRhiShaderResourceBindingList &bindings, QBitArray *specifiedBindings=nullptr)
static QVarLengthArray< std::unique_ptr< QSSGRenderCamera >, 4 > setupCascadingCamerasForShadowMap(const QSSGLayerRenderData &data, const QSSGRenderCamera &inCamera, const QSSGRenderLight *inLight, const int shadowMapResolution, const float pcfRadius, const float clipNear, const float clipFar, const QSSGBounds3 &castingObjectsBox, const QSSGBounds3 &receivingObjectsBox, bool lockShadowmapTexels, QSSGDebugDrawSystem *debugDrawSystem, bool drawCascades, bool drawSceneCascadeIntersection)