1 | // Copyright (C) 2019 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 QSGRHISHADEREFFECTNODE_P_H |
5 | #define QSGRHISHADEREFFECTNODE_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 <private/qsgadaptationlayer_p.h> |
19 | #include <qsgmaterial.h> |
20 | #include <QUrl> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | class QSGDefaultRenderContext; |
25 | class QSGPlainTexture; |
26 | class QSGRhiShaderEffectNode; |
27 | class QFileSelector; |
28 | |
29 | class QSGRhiShaderLinker |
30 | { |
31 | public: |
32 | void reset(const QShader &vs, const QShader &fs); |
33 | |
34 | void feedConstants(const QSGShaderEffectNode::ShaderData &shader, const QSet<int> *dirtyIndices = nullptr); |
35 | void feedSamplers(const QSGShaderEffectNode::ShaderData &shader, const QSet<int> *dirtyIndices = nullptr); |
36 | void linkTextureSubRects(); |
37 | |
38 | void dump(); |
39 | |
40 | struct Constant { |
41 | uint size; |
42 | QSGShaderEffectNode::VariableData::SpecialType specialType; |
43 | QVariant value; |
44 | bool operator==(const Constant &other) const { |
45 | return size == other.size && specialType == other.specialType |
46 | && (specialType == QSGShaderEffectNode::VariableData::None ? value == other.value : true); |
47 | } |
48 | }; |
49 | |
50 | bool m_error; |
51 | QShader m_vs; |
52 | QShader m_fs; |
53 | uint m_constantBufferSize; |
54 | QHash<uint, Constant> m_constants; // offset -> Constant |
55 | QHash<int, QVariant> m_samplers; // binding -> value (source ref) |
56 | QHash<QByteArray, int> m_samplerNameMap; // name -> binding |
57 | QSet<int> m_subRectBindings; |
58 | }; |
59 | |
60 | QDebug operator<<(QDebug debug, const QSGRhiShaderLinker::Constant &c); |
61 | |
62 | class QSGRhiShaderEffectMaterial : public QSGMaterial |
63 | { |
64 | public: |
65 | QSGRhiShaderEffectMaterial(QSGRhiShaderEffectNode *node); |
66 | ~QSGRhiShaderEffectMaterial(); |
67 | |
68 | int compare(const QSGMaterial *other) const override; |
69 | QSGMaterialType *type() const override; |
70 | QSGMaterialShader *createShader(QSGRendererInterface::RenderMode renderMode) const override; |
71 | |
72 | void updateTextureProviders(bool layoutChange); |
73 | |
74 | bool usesSubRectUniform(int binding) const { return m_linker.m_subRectBindings.contains(value: binding); } |
75 | |
76 | static const int MAX_BINDINGS = 32; |
77 | |
78 | QSGRhiShaderEffectNode *m_node; |
79 | QSGMaterialType *m_materialType = nullptr; |
80 | void *m_materialTypeCacheKey = nullptr; |
81 | QSGRhiShaderLinker m_linker; |
82 | QVector<QSGTextureProvider *> m_textureProviders; // [binding] = QSGTextureProvider |
83 | bool m_geometryUsesTextureSubRect = false; |
84 | QSGShaderEffectNode::CullMode m_cullMode = QSGShaderEffectNode::NoCulling; |
85 | bool m_hasCustomVertexShader = false; |
86 | bool m_hasCustomFragmentShader = false; |
87 | QShader m_vertexShader; |
88 | QShader m_fragmentShader; |
89 | QSGPlainTexture *m_dummyTexture = nullptr; |
90 | }; |
91 | |
92 | class QSGRhiShaderEffectNode : public QSGShaderEffectNode |
93 | { |
94 | Q_OBJECT |
95 | |
96 | public: |
97 | QSGRhiShaderEffectNode(QSGDefaultRenderContext *rc); |
98 | |
99 | QRectF updateNormalizedTextureSubRect(bool supportsAtlasTextures) override; |
100 | void syncMaterial(SyncData *syncData) override; |
101 | void preprocess() override; |
102 | |
103 | static void resetMaterialTypeCache(void *materialTypeCacheKey); |
104 | static void garbageCollectMaterialTypeCache(void *materialTypeCacheKey); |
105 | |
106 | private Q_SLOTS: |
107 | void handleTextureChange(); |
108 | void handleTextureProviderDestroyed(QObject *object); |
109 | |
110 | private: |
111 | QSGRhiShaderEffectMaterial m_material; |
112 | }; |
113 | |
114 | class QSGRhiGuiThreadShaderEffectManager : public QSGGuiThreadShaderEffectManager |
115 | { |
116 | public: |
117 | bool hasSeparateSamplerAndTextureObjects() const override; |
118 | QString log() const override; |
119 | Status status() const override; |
120 | void prepareShaderCode(ShaderInfo::Type typeHint, const QUrl &src, ShaderInfo *result) override; |
121 | |
122 | private: |
123 | bool reflect(ShaderInfo *result); |
124 | Status m_status = Uncompiled; |
125 | QFileSelector *m_fileSelector = nullptr; |
126 | }; |
127 | |
128 | QT_END_NAMESPACE |
129 | |
130 | #endif // QSGRHISHADEREFFECTNODE_P_H |
131 | |