| 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 | class Q_QUICK_EXPORT QSGRenderTarget |
| 33 | { |
| 34 | public: |
| 35 | QSGRenderTarget() { } |
| 36 | |
| 37 | QSGRenderTarget(QRhiRenderTarget *rt, |
| 38 | QRhiRenderPassDescriptor *rpDesc, |
| 39 | QRhiCommandBuffer *cb) |
| 40 | : rt(rt), rpDesc(rpDesc), cb(cb) { } |
| 41 | |
| 42 | explicit QSGRenderTarget(QPaintDevice *paintDevice) |
| 43 | : paintDevice(paintDevice) { } |
| 44 | |
| 45 | QRhiRenderTarget *rt = nullptr; |
| 46 | // Store the rp descriptor obj separately, it can (even if often it won't) |
| 47 | // be different from rt->renderPassDescriptor(); e.g. one user is the 2D |
| 48 | // integration in Quick 3D which will use a different, but compatible rp. |
| 49 | QRhiRenderPassDescriptor *rpDesc = nullptr; |
| 50 | QRhiCommandBuffer *cb = nullptr; |
| 51 | |
| 52 | QPaintDevice *paintDevice = nullptr; |
| 53 | |
| 54 | int multiViewCount = 0; |
| 55 | }; |
| 56 | |
| 57 | class Q_QUICK_EXPORT QSGRenderer : public QSGAbstractRenderer |
| 58 | { |
| 59 | public: |
| 60 | QSGRenderer(QSGRenderContext *context); |
| 61 | virtual ~QSGRenderer(); |
| 62 | |
| 63 | // Accessed by QSGMaterial[Rhi]Shader::RenderState. |
| 64 | QMatrix4x4 currentProjectionMatrix(int index) const { return m_current_projection_matrix[index]; } |
| 65 | QMatrix4x4 currentModelViewMatrix() const { return m_current_model_view_matrix; } |
| 66 | QMatrix4x4 currentCombinedMatrix(int index) const { return m_current_projection_matrix[index] * m_current_model_view_matrix; } |
| 67 | qreal currentOpacity() const { return m_current_opacity; } |
| 68 | qreal determinant() const { return m_current_determinant; } |
| 69 | |
| 70 | void setDevicePixelRatio(qreal ratio) { m_device_pixel_ratio = ratio; } |
| 71 | qreal devicePixelRatio() const { return m_device_pixel_ratio; } |
| 72 | QSGRenderContext *context() const { return m_context; } |
| 73 | |
| 74 | bool isMirrored() const; |
| 75 | void renderScene() override; |
| 76 | void prepareSceneInline() override; |
| 77 | void renderSceneInline() override; |
| 78 | void nodeChanged(QSGNode *node, QSGNode::DirtyState state) override; |
| 79 | |
| 80 | QSGNodeUpdater *nodeUpdater() const; |
| 81 | void setNodeUpdater(QSGNodeUpdater *updater); |
| 82 | inline QSGMaterialShader::RenderState state(QSGMaterialShader::RenderState::DirtyStates dirty) const; |
| 83 | virtual void setVisualizationMode(const QByteArray &) { } |
| 84 | virtual bool hasVisualizationModeWithContinuousUpdate() const { return false; } |
| 85 | virtual void releaseCachedResources() { } |
| 86 | |
| 87 | void clearChangedFlag() { m_changed_emitted = false; } |
| 88 | |
| 89 | // Accessed by QSGMaterialShader::RenderState. |
| 90 | QByteArray *currentUniformData() const { return m_current_uniform_data; } |
| 91 | QRhiResourceUpdateBatch *currentResourceUpdateBatch() const { return m_current_resource_update_batch; } |
| 92 | QRhi *currentRhi() const { return m_rhi; } |
| 93 | |
| 94 | void setRenderTarget(const QSGRenderTarget &rt) { m_rt = rt; } |
| 95 | const QSGRenderTarget &renderTarget() const { return m_rt; } |
| 96 | |
| 97 | void setRenderPassRecordingCallbacks(QSGRenderContext::RenderPassCallback start, |
| 98 | QSGRenderContext::RenderPassCallback end, |
| 99 | void *userData) |
| 100 | { |
| 101 | m_renderPassRecordingCallbacks.start = start; |
| 102 | m_renderPassRecordingCallbacks.end = end; |
| 103 | m_renderPassRecordingCallbacks.userData = userData; |
| 104 | } |
| 105 | |
| 106 | protected: |
| 107 | virtual void render() = 0; |
| 108 | |
| 109 | virtual void prepareInline(); |
| 110 | virtual void renderInline(); |
| 111 | |
| 112 | virtual void preprocess(); |
| 113 | |
| 114 | void addNodesToPreprocess(QSGNode *node); |
| 115 | void removeNodesToPreprocess(QSGNode *node); |
| 116 | |
| 117 | QVarLengthArray<QMatrix4x4, 1> m_current_projection_matrix; // includes adjustment, where applicable, so can be treated as Y up in NDC always |
| 118 | QVarLengthArray<QMatrix4x4, 1> m_current_projection_matrix_native_ndc; // Vulkan has Y down in normalized device coordinates, others Y up... |
| 119 | QMatrix4x4 m_current_model_view_matrix; |
| 120 | qreal m_current_opacity; |
| 121 | qreal m_current_determinant; |
| 122 | qreal m_device_pixel_ratio; |
| 123 | |
| 124 | QSGRenderContext *m_context; |
| 125 | |
| 126 | QByteArray *m_current_uniform_data; |
| 127 | QRhiResourceUpdateBatch *m_current_resource_update_batch; |
| 128 | QRhi *m_rhi; |
| 129 | QSGRenderTarget m_rt; |
| 130 | struct { |
| 131 | QSGRenderContext::RenderPassCallback start = nullptr; |
| 132 | QSGRenderContext::RenderPassCallback end = nullptr; |
| 133 | void *userData = nullptr; |
| 134 | } m_renderPassRecordingCallbacks; |
| 135 | |
| 136 | private: |
| 137 | QSGNodeUpdater *m_node_updater; |
| 138 | |
| 139 | QSet<QSGNode *> m_nodes_to_preprocess; |
| 140 | QSet<QSGNode *> m_nodes_dont_preprocess; |
| 141 | |
| 142 | uint m_changed_emitted : 1; |
| 143 | uint m_is_rendering : 1; |
| 144 | uint m_is_preprocessing : 1; |
| 145 | }; |
| 146 | |
| 147 | QSGMaterialShader::RenderState QSGRenderer::state(QSGMaterialShader::RenderState::DirtyStates dirty) const |
| 148 | { |
| 149 | QSGMaterialShader::RenderState s; |
| 150 | s.m_dirty = dirty; |
| 151 | s.m_data = this; |
| 152 | return s; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | class Q_QUICK_EXPORT QSGNodeDumper : public QSGNodeVisitor { |
| 157 | |
| 158 | public: |
| 159 | static void dump(QSGNode *n); |
| 160 | |
| 161 | QSGNodeDumper() {} |
| 162 | void visitNode(QSGNode *n) override; |
| 163 | void visitChildren(QSGNode *n) override; |
| 164 | |
| 165 | private: |
| 166 | int m_indent = 0; |
| 167 | }; |
| 168 | |
| 169 | QT_END_NAMESPACE |
| 170 | |
| 171 | #endif |
| 172 | |