1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSGRENDERER_P_H
41#define QSGRENDERER_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include "qsgabstractrenderer.h"
55#include "qsgabstractrenderer_p.h"
56#include "qsgnode.h"
57#include "qsgmaterial.h"
58
59#include <QtQuick/private/qsgcontext_p.h>
60
61QT_BEGIN_NAMESPACE
62
63class QSGBindable;
64class QSGNodeUpdater;
65class QRhiRenderTarget;
66class QRhiCommandBuffer;
67class QRhiRenderPassDescriptor;
68class QRhiResourceUpdateBatch;
69
70Q_QUICK_PRIVATE_EXPORT bool qsg_test_and_clear_fatal_render_error();
71Q_QUICK_PRIVATE_EXPORT void qsg_set_fatal_renderer_error();
72
73class Q_QUICK_PRIVATE_EXPORT QSGRenderer : public QSGAbstractRenderer
74{
75public:
76 QSGRenderer(QSGRenderContext *context);
77 virtual ~QSGRenderer();
78
79 // Accessed by QSGMaterial[Rhi]Shader::RenderState.
80 QMatrix4x4 currentProjectionMatrix() const { return m_current_projection_matrix; }
81 QMatrix4x4 currentModelViewMatrix() const { return m_current_model_view_matrix; }
82 QMatrix4x4 currentCombinedMatrix() const { return m_current_projection_matrix * m_current_model_view_matrix; }
83 qreal currentOpacity() const { return m_current_opacity; }
84 qreal determinant() const { return m_current_determinant; }
85
86 void setDevicePixelRatio(qreal ratio) { m_device_pixel_ratio = ratio; }
87 qreal devicePixelRatio() const { return m_device_pixel_ratio; }
88 QSGRenderContext *context() const { return m_context; }
89
90 bool isMirrored() const;
91 void renderScene(const QSGBindable &bindable);
92 void renderScene(uint fboId = 0) override;
93 void nodeChanged(QSGNode *node, QSGNode::DirtyState state) override;
94
95 QSGNodeUpdater *nodeUpdater() const;
96 void setNodeUpdater(QSGNodeUpdater *updater);
97 inline QSGMaterialShader::RenderState state(QSGMaterialShader::RenderState::DirtyStates dirty) const;
98 inline QSGMaterialRhiShader::RenderState rhiState(QSGMaterialRhiShader::RenderState::DirtyStates dirty) const;
99 virtual void setCustomRenderMode(const QByteArray &) { }
100 virtual void releaseCachedResources() { }
101
102 void clearChangedFlag() { m_changed_emitted = false; }
103
104 // Accessed by QSGMaterialRhiShader::RenderState.
105 QByteArray *currentUniformData() const { return m_current_uniform_data; }
106 QRhiResourceUpdateBatch *currentResourceUpdateBatch() const { return m_current_resource_update_batch; }
107 QRhi *currentRhi() const { return m_rhi; }
108
109 void setRenderTarget(QRhiRenderTarget *rt) { m_rt = rt; }
110 QRhiRenderTarget *renderTarget() const { return m_rt; }
111
112 void setCommandBuffer(QRhiCommandBuffer *cb) { m_cb = cb; }
113 QRhiCommandBuffer *commandBuffer() const { return m_cb; }
114
115 void setRenderPassDescriptor(QRhiRenderPassDescriptor *rpDesc) { m_rp_desc = rpDesc; }
116 QRhiRenderPassDescriptor *renderPassDescriptor() const { return m_rp_desc; }
117
118 void setRenderPassRecordingCallbacks(QSGRenderContext::RenderPassCallback start,
119 QSGRenderContext::RenderPassCallback end,
120 void *userData)
121 {
122 m_renderPassRecordingCallbacks.start = start;
123 m_renderPassRecordingCallbacks.end = end;
124 m_renderPassRecordingCallbacks.userData = userData;
125 }
126
127protected:
128 virtual void render() = 0;
129
130 const QSGBindable *bindable() const { return m_bindable; }
131
132 virtual void preprocess();
133
134 void addNodesToPreprocess(QSGNode *node);
135 void removeNodesToPreprocess(QSGNode *node);
136
137 QMatrix4x4 m_current_projection_matrix; // includes adjustment, where applicable, so can be treated as Y up in NDC always
138 QMatrix4x4 m_current_projection_matrix_native_ndc; // Vulkan has Y down in normalized device coordinates, others Y up...
139 QMatrix4x4 m_current_model_view_matrix;
140 qreal m_current_opacity;
141 qreal m_current_determinant;
142 qreal m_device_pixel_ratio;
143
144 QSGRenderContext *m_context;
145
146 QByteArray *m_current_uniform_data;
147 QRhiResourceUpdateBatch *m_current_resource_update_batch;
148 QRhi *m_rhi;
149 QRhiRenderTarget *m_rt;
150 QRhiCommandBuffer *m_cb;
151 QRhiRenderPassDescriptor *m_rp_desc;
152 struct {
153 QSGRenderContext::RenderPassCallback start = nullptr;
154 QSGRenderContext::RenderPassCallback end = nullptr;
155 void *userData = nullptr;
156 } m_renderPassRecordingCallbacks;
157
158private:
159 QSGNodeUpdater *m_node_updater;
160
161 QSet<QSGNode *> m_nodes_to_preprocess;
162 QSet<QSGNode *> m_nodes_dont_preprocess;
163
164 const QSGBindable *m_bindable;
165
166 uint m_changed_emitted : 1;
167 uint m_is_rendering : 1;
168 uint m_is_preprocessing : 1;
169};
170
171class Q_QUICK_PRIVATE_EXPORT QSGBindable
172{
173public:
174 virtual ~QSGBindable() { }
175 virtual void bind() const = 0;
176 virtual void clear(QSGAbstractRenderer::ClearMode mode) const;
177 virtual void reactivate() const;
178};
179#if QT_CONFIG(opengl)
180class QSGBindableFboId : public QSGBindable
181{
182public:
183 QSGBindableFboId(GLuint);
184 void bind() const override;
185private:
186 GLuint m_id;
187};
188#endif
189
190
191QSGMaterialShader::RenderState QSGRenderer::state(QSGMaterialShader::RenderState::DirtyStates dirty) const
192{
193 QSGMaterialShader::RenderState s;
194 s.m_dirty = dirty;
195 s.m_data = this;
196 return s;
197}
198
199QSGMaterialRhiShader::RenderState QSGRenderer::rhiState(QSGMaterialRhiShader::RenderState::DirtyStates dirty) const
200{
201 QSGMaterialRhiShader::RenderState s;
202 s.m_dirty = dirty;
203 s.m_data = this;
204 return s;
205}
206
207
208class Q_QUICK_PRIVATE_EXPORT QSGNodeDumper : public QSGNodeVisitor {
209
210public:
211 static void dump(QSGNode *n);
212
213 QSGNodeDumper() {}
214 void visitNode(QSGNode *n) override;
215 void visitChildren(QSGNode *n) override;
216
217private:
218 int m_indent = 0;
219};
220
221
222
223QT_END_NAMESPACE
224
225#endif
226