1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 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 QSGRHISHADEREFFECTNODE_P_H
41 #define QSGRHISHADEREFFECTNODE_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 <private/qsgadaptationlayer_p.h>
55 #include <qsgmaterial.h>
56 
57 QT_BEGIN_NAMESPACE
58 
59 class QSGDefaultRenderContext;
60 class QSGPlainTexture;
61 class QSGRhiShaderEffectNode;
62 class QSGRhiGuiThreadShaderEffectManager;
63 class QFileSelector;
64 
65 class QSGRhiShaderLinker
66 {
67 public:
68     void reset(const QShader &vs, const QShader &fs);
69 
70     void feedConstants(const QSGShaderEffectNode::ShaderData &shader, const QSet<int> *dirtyIndices = nullptr);
71     void feedSamplers(const QSGShaderEffectNode::ShaderData &shader, const QSet<int> *dirtyIndices = nullptr);
72     void linkTextureSubRects();
73 
74     void dump();
75 
76     struct Constant {
77         uint size;
78         QSGShaderEffectNode::VariableData::SpecialType specialType;
79         QVariant value;
80         bool operator==(const Constant &other) const {
81             return size == other.size && specialType == other.specialType
82                     && (specialType == QSGShaderEffectNode::VariableData::None ? value == other.value : true);
83         }
84     };
85 
86     bool m_error;
87     QShader m_vs;
88     QShader m_fs;
89     uint m_constantBufferSize;
90     QHash<uint, Constant> m_constants; // offset -> Constant
91     QHash<int, QVariant> m_samplers; // binding -> value (source ref)
92     QHash<QByteArray, int> m_samplerNameMap; // name -> binding
93 };
94 
95 QDebug operator<<(QDebug debug, const QSGRhiShaderLinker::Constant &c);
96 
97 class QSGRhiShaderEffectMaterial : public QSGMaterial
98 {
99 public:
100     QSGRhiShaderEffectMaterial(QSGRhiShaderEffectNode *node);
101     ~QSGRhiShaderEffectMaterial();
102 
103     int compare(const QSGMaterial *other) const override;
104     QSGMaterialType *type() const override;
105     QSGMaterialShader *createShader() const override;
106 
107     void updateTextureProviders(bool layoutChange);
108 
109     static const int MAX_BINDINGS = 32;
110 
111     QSGRhiShaderEffectNode *m_node;
112     QSGMaterialType *m_materialType = nullptr;
113     QSGRhiShaderLinker m_linker;
114     QVector<QSGTextureProvider *> m_textureProviders; // [binding] = QSGTextureProvider
115     bool m_geometryUsesTextureSubRect = false;
116     QSGShaderEffectNode::CullMode m_cullMode = QSGShaderEffectNode::NoCulling;
117     bool m_hasCustomVertexShader = false;
118     bool m_hasCustomFragmentShader = false;
119     QShader m_vertexShader;
120     QShader m_fragmentShader;
121     QSGPlainTexture *m_dummyTexture = nullptr;
122 };
123 
124 class QSGRhiShaderEffectNode : public QObject, public QSGShaderEffectNode
125 {
126     Q_OBJECT
127 
128 public:
129     QSGRhiShaderEffectNode(QSGDefaultRenderContext *rc, QSGRhiGuiThreadShaderEffectManager *mgr);
130 
131     QRectF updateNormalizedTextureSubRect(bool supportsAtlasTextures) override;
132     void syncMaterial(SyncData *syncData) override;
133     void preprocess() override;
134 
135     static void cleanupMaterialTypeCache();
136 
137 private Q_SLOTS:
138     void handleTextureChange();
139     void handleTextureProviderDestroyed(QObject *object);
140 
141 private:
142     QSGDefaultRenderContext *m_rc;
143     QSGRhiGuiThreadShaderEffectManager *m_mgr;
144     QSGRhiShaderEffectMaterial m_material;
145 };
146 
147 class QSGRhiGuiThreadShaderEffectManager : public QSGGuiThreadShaderEffectManager
148 {
149 public:
150     bool hasSeparateSamplerAndTextureObjects() const override;
151     QString log() const override;
152     Status status() const override;
153     void prepareShaderCode(ShaderInfo::Type typeHint, const QByteArray &src, ShaderInfo *result) override;
154 
155 private:
156     bool reflect(ShaderInfo *result);
157     Status m_status = Uncompiled;
158     QFileSelector *m_fileSelector = nullptr;
159 };
160 
161 QT_END_NAMESPACE
162 
163 #endif // QSGRHISHADEREFFECTNODE_P_H
164