1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2016 Jolla Ltd, author: <gunnar.sletta@jollamobile.com>
5 ** Copyright (C) 2016 Robin Burchell <robin.burchell@viroteck.net>
6 ** Contact: https://www.qt.io/licensing/
7 **
8 ** This file is part of the QtQuick module of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Commercial License Usage
12 ** Licensees holding valid commercial Qt licenses may use this file in
13 ** accordance with the commercial license agreement provided with the
14 ** Software or, alternatively, in accordance with the terms contained in
15 ** a written agreement between you and The Qt Company. For licensing terms
16 ** and conditions see https://www.qt.io/terms-conditions. For further
17 ** information use the contact form at https://www.qt.io/contact-us.
18 **
19 ** GNU Lesser General Public License Usage
20 ** Alternatively, this file may be used under the terms of the GNU Lesser
21 ** General Public License version 3 as published by the Free Software
22 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
23 ** packaging of this file. Please review the following information to
24 ** ensure the GNU Lesser General Public License version 3 requirements
25 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
26 **
27 ** GNU General Public License Usage
28 ** Alternatively, this file may be used under the terms of the GNU
29 ** General Public License version 2.0 or (at your option) the GNU General
30 ** Public license version 3 or any later version approved by the KDE Free
31 ** Qt Foundation. The licenses are as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
33 ** included in the packaging of this file. Please review the following
34 ** information to ensure the GNU General Public License requirements will
35 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
36 ** https://www.gnu.org/licenses/gpl-3.0.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QSGRHIVISUALIZER_P_H
43 #define QSGRHIVISUALIZER_P_H
44 
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55 
56 #include "qsgbatchrenderer_p.h"
57 
58 QT_BEGIN_NAMESPACE
59 
60 namespace QSGBatchRenderer
61 {
62 
63 class RhiVisualizer : public Visualizer
64 {
65 public:
66     RhiVisualizer(Renderer *renderer);
67     ~RhiVisualizer();
68 
69     void prepareVisualize() override;
70     void visualize() override;
71 
72     void releaseResources() override;
73 
74     struct DrawCall
75     {
76         static const int UBUF_SIZE = 152; // visualization.vert/frag
77         struct {
78             char data[UBUF_SIZE]; // matrix, rotation, color, pattern, projection
79         } uniforms;
80         struct {
81             QRhiGraphicsPipeline::Topology topology;
82             QRhiVertexInputAttribute::Format format;
83             int count;
84             int stride;
85             const void *data; // only when using own vbuf
86         } vertex;
87         struct {
88             QRhiCommandBuffer::IndexFormat format;
89             int count;
90             int stride;
91             const void *data; // only when using own ibuf
92         } index;
93         struct {
94             QRhiBuffer *vbuf; // either same for all draw calls and owned by the *Vis, or points to a Batch.Buffer.vbo.buf
95             int vbufOffset;
96             QRhiBuffer *ibuf; // same, but for index
97             int ibufOffset;
98             int ubufOffset;
99         } buf;
100     };
101 
102 private:
103     QShader m_vs;
104     QShader m_fs;
105 
106     void recordDrawCalls(const QVector<DrawCall> &drawCalls,
107                          QRhiCommandBuffer *cb,
108                          QRhiShaderResourceBindings *srb,
109                          bool blendOneOne = false);
110 
111     class PipelineCache {
112     public:
113         QRhiGraphicsPipeline *pipeline(RhiVisualizer *visualizer,
114                                        QRhi *rhi,
115                                        QRhiShaderResourceBindings *srb,
116                                        QRhiRenderPassDescriptor *rpDesc,
117                                        QRhiGraphicsPipeline::Topology topology,
118                                        QRhiVertexInputAttribute::Format vertexFormat,
119                                        quint32 vertexStride,
120                                        bool blendOneOne);
121         void releaseResources();
122     private:
123         struct Pipeline {
124             QRhiGraphicsPipeline::Topology topology;
125             QRhiVertexInputAttribute::Format format;
126             quint32 stride;
127             QRhiGraphicsPipeline *ps;
128         };
129         QVarLengthArray<Pipeline, 16> pipelines;
130     };
131 
132     PipelineCache m_pipelines;
133 
134     class Fade {
135     public:
136         void prepare(RhiVisualizer *visualizer,
137                      QRhi *rhi, QRhiResourceUpdateBatch *u, QRhiRenderPassDescriptor *rpDesc);
138         void releaseResources();
139         void render(QRhiCommandBuffer *cb);
140     private:
141         RhiVisualizer *visualizer;
142         QRhiBuffer *vbuf = nullptr;
143         QRhiBuffer *ubuf = nullptr;
144         QRhiGraphicsPipeline *ps = nullptr;
145         QRhiShaderResourceBindings *srb = nullptr;
146     } m_fade;
147 
148     class ChangeVis {
149     public:
150         void prepare(Node *n, RhiVisualizer *visualizer,
151                      QRhi *rhi, QRhiResourceUpdateBatch *u);
152         void releaseResources();
153         void render(QRhiCommandBuffer *cb);
154     private:
155         void gather(Node *n);
156         RhiVisualizer *visualizer;
157         QVector<DrawCall> drawCalls;
158         QRhiBuffer *vbuf = nullptr;
159         QRhiBuffer *ibuf = nullptr;
160         QRhiBuffer *ubuf = nullptr;
161         QRhiShaderResourceBindings *srb = nullptr;
162     } m_changeVis;
163 
164     class BatchVis {
165     public:
166         void prepare(const QDataBuffer<Batch *> &opaqueBatches,
167                      const QDataBuffer<Batch *> &alphaBatches,
168                      RhiVisualizer *visualizer,
169                      QRhi *rhi, QRhiResourceUpdateBatch *u,
170                      bool forceUintIndex);
171         void releaseResources();
172         void render(QRhiCommandBuffer *cb);
173     private:
174         void gather(Batch *b);
175         RhiVisualizer *visualizer;
176         bool forceUintIndex;
177         QVector<DrawCall> drawCalls;
178         QRhiBuffer *ubuf = nullptr;
179         QRhiShaderResourceBindings *srb = nullptr;
180     } m_batchVis;
181 
182     class ClipVis {
183     public:
184         void prepare(QSGNode *node, RhiVisualizer *visualizer,
185                      QRhi *rhi, QRhiResourceUpdateBatch *u);
186         void releaseResources();
187         void render(QRhiCommandBuffer *cb);
188     private:
189         void gather(QSGNode *node);
190         RhiVisualizer *visualizer;
191         QVector<DrawCall> drawCalls;
192         QRhiBuffer *vbuf = nullptr;
193         QRhiBuffer *ibuf = nullptr;
194         QRhiBuffer *ubuf = nullptr;
195         QRhiShaderResourceBindings *srb = nullptr;
196     } m_clipVis;
197 
198     class OverdrawVis {
199     public:
200         void prepare(Node *n, RhiVisualizer *visualizer,
201                      QRhi *rhi, QRhiResourceUpdateBatch *u);
202         void releaseResources();
203         void render(QRhiCommandBuffer *cb);
204     private:
205         void gather(Node *n);
206         RhiVisualizer *visualizer;
207         QVector<DrawCall> drawCalls;
208         QRhiBuffer *vbuf = nullptr;
209         QRhiBuffer *ibuf = nullptr;
210         QRhiBuffer *ubuf = nullptr;
211         QRhiShaderResourceBindings *srb = nullptr;
212         float step = 0.0f;
213         QMatrix4x4 rotation;
214         struct {
215             QRhiBuffer *vbuf = nullptr;
216             QRhiBuffer *ubuf = nullptr;
217             QRhiShaderResourceBindings *srb = nullptr;
218             QRhiGraphicsPipeline *ps = nullptr;
219         } box;
220     } m_overdrawVis;
221 
222     friend class Fade;
223     friend class PipelineCache;
224     friend class ChangeVis;
225     friend class ClipVis;
226     friend class OverdrawVis;
227 };
228 
229 } // namespace QSGBatchRenderer
230 
231 QT_END_NAMESPACE
232 
233 #endif // QSGRHIVISUALIZER_P_H
234