1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QSGRENDERER_P_H |
5 | #define QSGRENDERER_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include "qsgabstractrenderer_p_p.h" |
19 | #include "qsgnode.h" |
20 | #include "qsgmaterial.h" |
21 | |
22 | #include <QtQuick/private/qsgcontext_p.h> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | class QSGNodeUpdater; |
27 | class QRhiRenderTarget; |
28 | class QRhiCommandBuffer; |
29 | class QRhiRenderPassDescriptor; |
30 | class QRhiResourceUpdateBatch; |
31 | |
32 | Q_QUICK_PRIVATE_EXPORT bool qsg_test_and_clear_fatal_render_error(); |
33 | Q_QUICK_PRIVATE_EXPORT void qsg_set_fatal_renderer_error(); |
34 | |
35 | class Q_QUICK_PRIVATE_EXPORT QSGRenderTarget |
36 | { |
37 | public: |
38 | QSGRenderTarget() { } |
39 | |
40 | QSGRenderTarget(QRhiRenderTarget *rt, |
41 | QRhiRenderPassDescriptor *rpDesc, |
42 | QRhiCommandBuffer *cb) |
43 | : rt(rt), rpDesc(rpDesc), cb(cb) { } |
44 | |
45 | explicit QSGRenderTarget(QPaintDevice *paintDevice) |
46 | : paintDevice(paintDevice) { } |
47 | |
48 | QRhiRenderTarget *rt = nullptr; |
49 | // Store the rp descriptor obj separately, it can (even if often it won't) |
50 | // be different from rt->renderPassDescriptor(); e.g. one user is the 2D |
51 | // integration in Quick 3D which will use a different, but compatible rp. |
52 | QRhiRenderPassDescriptor *rpDesc = nullptr; |
53 | QRhiCommandBuffer *cb = nullptr; |
54 | |
55 | QPaintDevice *paintDevice = nullptr; |
56 | }; |
57 | |
58 | class Q_QUICK_PRIVATE_EXPORT QSGRenderer : public QSGAbstractRenderer |
59 | { |
60 | public: |
61 | QSGRenderer(QSGRenderContext *context); |
62 | virtual ~QSGRenderer(); |
63 | |
64 | // Accessed by QSGMaterial[Rhi]Shader::RenderState. |
65 | QMatrix4x4 currentProjectionMatrix() const { return m_current_projection_matrix; } |
66 | QMatrix4x4 currentModelViewMatrix() const { return m_current_model_view_matrix; } |
67 | QMatrix4x4 currentCombinedMatrix() const { return m_current_projection_matrix * m_current_model_view_matrix; } |
68 | qreal currentOpacity() const { return m_current_opacity; } |
69 | qreal determinant() const { return m_current_determinant; } |
70 | |
71 | void setDevicePixelRatio(qreal ratio) { m_device_pixel_ratio = ratio; } |
72 | qreal devicePixelRatio() const { return m_device_pixel_ratio; } |
73 | QSGRenderContext *context() const { return m_context; } |
74 | |
75 | bool isMirrored() const; |
76 | void renderScene() override; |
77 | void prepareSceneInline() override; |
78 | void renderSceneInline() override; |
79 | void nodeChanged(QSGNode *node, QSGNode::DirtyState state) override; |
80 | |
81 | QSGNodeUpdater *nodeUpdater() const; |
82 | void setNodeUpdater(QSGNodeUpdater *updater); |
83 | inline QSGMaterialShader::RenderState state(QSGMaterialShader::RenderState::DirtyStates dirty) const; |
84 | virtual void setVisualizationMode(const QByteArray &) { } |
85 | virtual bool hasVisualizationModeWithContinuousUpdate() const { return false; } |
86 | virtual void releaseCachedResources() { } |
87 | |
88 | void clearChangedFlag() { m_changed_emitted = false; } |
89 | |
90 | // Accessed by QSGMaterialShader::RenderState. |
91 | QByteArray *currentUniformData() const { return m_current_uniform_data; } |
92 | QRhiResourceUpdateBatch *currentResourceUpdateBatch() const { return m_current_resource_update_batch; } |
93 | QRhi *currentRhi() const { return m_rhi; } |
94 | |
95 | void setRenderTarget(const QSGRenderTarget &rt) { m_rt = rt; } |
96 | const QSGRenderTarget &renderTarget() const { return m_rt; } |
97 | |
98 | void setRenderPassRecordingCallbacks(QSGRenderContext::RenderPassCallback start, |
99 | QSGRenderContext::RenderPassCallback end, |
100 | void *userData) |
101 | { |
102 | m_renderPassRecordingCallbacks.start = start; |
103 | m_renderPassRecordingCallbacks.end = end; |
104 | m_renderPassRecordingCallbacks.userData = userData; |
105 | } |
106 | |
107 | protected: |
108 | virtual void render() = 0; |
109 | |
110 | virtual void prepareInline(); |
111 | virtual void renderInline(); |
112 | |
113 | virtual void preprocess(); |
114 | |
115 | void addNodesToPreprocess(QSGNode *node); |
116 | void removeNodesToPreprocess(QSGNode *node); |
117 | |
118 | QMatrix4x4 m_current_projection_matrix; // includes adjustment, where applicable, so can be treated as Y up in NDC always |
119 | QMatrix4x4 m_current_projection_matrix_native_ndc; // Vulkan has Y down in normalized device coordinates, others Y up... |
120 | QMatrix4x4 m_current_model_view_matrix; |
121 | qreal m_current_opacity; |
122 | qreal m_current_determinant; |
123 | qreal m_device_pixel_ratio; |
124 | |
125 | QSGRenderContext *m_context; |
126 | |
127 | QByteArray *m_current_uniform_data; |
128 | QRhiResourceUpdateBatch *m_current_resource_update_batch; |
129 | QRhi *m_rhi; |
130 | QSGRenderTarget m_rt; |
131 | struct { |
132 | QSGRenderContext::RenderPassCallback start = nullptr; |
133 | QSGRenderContext::RenderPassCallback end = nullptr; |
134 | void *userData = nullptr; |
135 | } m_renderPassRecordingCallbacks; |
136 | |
137 | private: |
138 | QSGNodeUpdater *m_node_updater; |
139 | |
140 | QSet<QSGNode *> m_nodes_to_preprocess; |
141 | QSet<QSGNode *> m_nodes_dont_preprocess; |
142 | |
143 | uint m_changed_emitted : 1; |
144 | uint m_is_rendering : 1; |
145 | uint m_is_preprocessing : 1; |
146 | }; |
147 | |
148 | QSGMaterialShader::RenderState QSGRenderer::state(QSGMaterialShader::RenderState::DirtyStates dirty) const |
149 | { |
150 | QSGMaterialShader::RenderState s; |
151 | s.m_dirty = dirty; |
152 | s.m_data = this; |
153 | return s; |
154 | } |
155 | |
156 | |
157 | class Q_QUICK_PRIVATE_EXPORT QSGNodeDumper : public QSGNodeVisitor { |
158 | |
159 | public: |
160 | static void dump(QSGNode *n); |
161 | |
162 | QSGNodeDumper() {} |
163 | void visitNode(QSGNode *n) override; |
164 | void visitChildren(QSGNode *n) override; |
165 | |
166 | private: |
167 | int m_indent = 0; |
168 | }; |
169 | |
170 | #ifndef QT_NO_DEBUG |
171 | extern bool _q_sg_leak_check; |
172 | #endif |
173 | |
174 | QT_END_NAMESPACE |
175 | |
176 | #endif |
177 | |