1 /****************************************************************************
2 **
3 ** Copyright (C) 2008-2012 NVIDIA Corporation.
4 ** Copyright (C) 2019 The Qt Company Ltd.
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of Qt Quick 3D.
8 **
9 ** $QT_BEGIN_LICENSE:GPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU
20 ** General Public License version 3 or (at your option) any later version
21 ** approved by the KDE Free Qt Foundation. The licenses are as published by
22 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
23 ** included in the packaging of this file. Please review the following
24 ** information to ensure the GNU General Public License requirements will
25 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
26 **
27 ** $QT_END_LICENSE$
28 **
29 ****************************************************************************/
30 
31 #ifndef QSSG_RENDERER_IMPL_LAYER_RENDER_DATA_H
32 #define QSSG_RENDERER_IMPL_LAYER_RENDER_DATA_H
33 
34 //
35 //  W A R N I N G
36 //  -------------
37 //
38 // This file is not part of the Qt API.  It exists purely as an
39 // implementation detail.  This header file may change from version to
40 // version without notice, or even be removed.
41 //
42 // We mean it.
43 //
44 
45 #include <QtQuick3DRuntimeRender/private/qssgrendererimpllayerrenderpreparationdata_p.h>
46 #include <QtQuick3DRuntimeRender/private/qssgrenderresourcebufferobjects_p.h>
47 #include <QtQuick3DRuntimeRender/private/qssgrenderresourcetexture2d_p.h>
48 
49 QT_BEGIN_NAMESPACE
50 
51 struct QSSGLayerRenderData : public QSSGLayerRenderPreparationData
52 {
53     QAtomicInt ref;
54 
55     // Layers can be rendered offscreen for many reasons; effects, progressive aa,
56     // or just because a flag forces it.  If they are rendered offscreen we can then
57     // cache the result so we don't render the layer again if it isn't dirty.
58     QSSGResourceTexture2D m_layerTexture;
59     QSSGResourceTexture2D m_temporalAATexture;
60     QSSGResourceTexture2D m_prevTemporalAATexture;
61     // Sometimes we need to render our depth buffer to a depth texture.
62     QSSGResourceTexture2D m_layerDepthTexture;
63     QSSGResourceTexture2D m_layerPrepassDepthTexture;
64     QSSGResourceTexture2D m_layerSsaoTexture;
65     // if we render multisampled we need resolve buffers
66     QSSGResourceTexture2D m_layerMultisampleTexture;
67     QSSGResourceTexture2D m_layerMultisamplePrepassDepthTexture;
68     QSSGResourceTexture2D m_layerMultisampleWidgetTexture;
69 
70     // GPU profiler per layer
71     QScopedPointer<QSSGRenderGPUProfiler> m_layerProfilerGpu;
72 
73     QSSGRenderCamera m_sceneCamera;
74     QVector2D m_sceneDimensions;
75 
76     // ProgressiveAA algorithm details.
77     quint32 m_progressiveAAPassIndex;
78     // Increments every frame regardless to provide appropriate jittering
79     quint32 m_temporalAAPassIndex;
80     // Ensures we don't stop on an in-between frame; we will run two frames after the dirty flag
81     // is clear.
82     quint32 m_nonDirtyTemporalAAPassIndex;
83     float m_textScale;
84 
85     QSSGOption<QVector3D> m_boundingRectColor;
86     QSSGRenderTextureFormat m_depthBufferFormat;
87 
88     QSize m_previousDimensions;
89 
90     QSSGLayerRenderData(QSSGRenderLayer &inLayer, const QSSGRef<QSSGRendererImpl> &inRenderer);
91 
92     virtual ~QSSGLayerRenderData() override;
93 
94     void prepareForRender();
95 
96     // Internal Call
97     void prepareForRender(const QSize &inViewportDimensions) override;
98 
99     QSSGRenderTextureFormat getDepthBufferFormat();
100     QSSGRenderFrameBufferAttachment getFramebufferDepthAttachmentFormat(QSSGRenderTextureFormat depthFormat);
101 
102     // Render this layer assuming viewport and RT are setup.  Just renders exactly this item
103     // no effects.
104     void renderClearPass();
105     void renderDepthPass(bool inEnableTransparentDepthWrite = false);
106     void renderAoPass();
107 #ifdef QT_QUICK3D_DEBUG_SHADOWS
108     void renderDebugDepthMap(QSSGRenderTexture2D *theDepthTex, QSSGRenderTextureCube *theDepthCube);
109 #endif
110     void renderShadowMapPass(QSSGResourceFrameBuffer *theFB);
111     void renderShadowCubeBlurPass(QSSGResourceFrameBuffer *theFB,
112                                   const QSSGRef<QSSGRenderTextureCube> &target0,
113                                   const QSSGRef<QSSGRenderTextureCube> &target1,
114                                   float filterSz,
115                                   float clipFar);
116     void renderShadowMapBlurPass(QSSGResourceFrameBuffer *theFB,
117                                  const QSSGRef<QSSGRenderTexture2D> &target0,
118                                  const QSSGRef<QSSGRenderTexture2D> &target1,
119                                  float filterSz,
120                                  float clipFar);
121 
122     void render(QSSGResourceFrameBuffer *theFB = nullptr);
123     void resetForFrame() override;
124 
125     void createGpuProfiler();
126     void startProfiling(QString &nameID, bool sync);
127     void endProfiling(QString &nameID);
128     void startProfiling(const char *nameID, bool sync);
129     void endProfiling(const char *nameID);
130     void addVertexCount(quint32 count);
131 
132     void applyLayerPostEffects(const QSSGRef<QSSGRenderFrameBuffer> &theFB);
133     void runnableRenderToViewport(const QSSGRef<QSSGRenderFrameBuffer> &theFB);
134 
135     // test method to render this layer to a given view projection without running the entire
136     // layer setup system.  This assumes the client has setup the viewport, scissor, and render
137     // target
138     // the way they want them.
139     void prepareAndRender(const QMatrix4x4 &inViewProjection);
140 
141     bool progressiveAARenderRequest() const;
142 
143 protected:
144     // Used for both the normal passes and the depth pass.
145     // When doing the depth pass, we disable blending completely because it does not really make
146     // sense
147     // to write blend equations into
148     void runRenderPass(TRenderRenderableFunction renderFn,
149                        bool inEnableBlending,
150                        bool inEnableDepthWrite,
151                        bool inEnableTransparentDepthWrite,
152                        bool inSortOpaqueRenderables,
153                        quint32 indexLight,
154                        const QSSGRenderCamera &inCamera,
155                        QSSGResourceFrameBuffer *theFB = nullptr);
156 };
157 QT_END_NAMESPACE
158 #endif
159