| 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 | QHash<uint, Constant> m_constants; // offset -> Constant |
| 54 | QHash<int, QVariant> m_samplers; // binding -> value (source ref) |
| 55 | QHash<QByteArray, int> m_samplerNameMap; // name -> binding |
| 56 | QSet<int> m_subRectBindings; |
| 57 | }; |
| 58 | |
| 59 | QDebug operator<<(QDebug debug, const QSGRhiShaderLinker::Constant &c); |
| 60 | |
| 61 | class QSGRhiShaderEffectMaterial : public QSGMaterial |
| 62 | { |
| 63 | public: |
| 64 | QSGRhiShaderEffectMaterial(QSGRhiShaderEffectNode *node); |
| 65 | ~QSGRhiShaderEffectMaterial(); |
| 66 | |
| 67 | int compare(const QSGMaterial *other) const override; |
| 68 | QSGMaterialType *type() const override; |
| 69 | QSGMaterialShader *createShader(QSGRendererInterface::RenderMode renderMode) const override; |
| 70 | |
| 71 | void updateTextureProviders(bool layoutChange); |
| 72 | |
| 73 | bool usesSubRectUniform(int binding) const { return m_linker.m_subRectBindings.contains(value: binding); } |
| 74 | |
| 75 | static const int MAX_BINDINGS = 32; |
| 76 | |
| 77 | QSGRhiShaderEffectNode *m_node; |
| 78 | QSGMaterialType *m_materialType = nullptr; |
| 79 | void *m_materialTypeCacheKey = nullptr; |
| 80 | QSGRhiShaderLinker m_linker; |
| 81 | QVector<QSGTextureProvider *> m_textureProviders; // [binding] = QSGTextureProvider |
| 82 | bool m_geometryUsesTextureSubRect = false; |
| 83 | QSGShaderEffectNode::CullMode m_cullMode = QSGShaderEffectNode::NoCulling; |
| 84 | bool m_hasCustomVertexShader = false; |
| 85 | bool m_hasCustomFragmentShader = false; |
| 86 | QShader m_vertexShader; |
| 87 | QShader m_fragmentShader; |
| 88 | QSGPlainTexture *m_dummyTexture = nullptr; |
| 89 | }; |
| 90 | |
| 91 | class QSGRhiShaderEffectNode : public QSGShaderEffectNode |
| 92 | { |
| 93 | Q_OBJECT |
| 94 | |
| 95 | public: |
| 96 | QSGRhiShaderEffectNode(QSGDefaultRenderContext *rc); |
| 97 | |
| 98 | QRectF updateNormalizedTextureSubRect(bool supportsAtlasTextures) override; |
| 99 | void syncMaterial(SyncData *syncData) override; |
| 100 | void preprocess() override; |
| 101 | |
| 102 | static void resetMaterialTypeCache(void *materialTypeCacheKey); |
| 103 | static void garbageCollectMaterialTypeCache(void *materialTypeCacheKey); |
| 104 | |
| 105 | private Q_SLOTS: |
| 106 | void handleTextureChange(); |
| 107 | void handleTextureProviderDestroyed(QObject *object); |
| 108 | |
| 109 | private: |
| 110 | QSGRhiShaderEffectMaterial m_material; |
| 111 | }; |
| 112 | |
| 113 | class QSGRhiGuiThreadShaderEffectManager : public QSGGuiThreadShaderEffectManager |
| 114 | { |
| 115 | public: |
| 116 | bool hasSeparateSamplerAndTextureObjects() const override; |
| 117 | QString log() const override; |
| 118 | Status status() const override; |
| 119 | void prepareShaderCode(ShaderInfo::Type typeHint, const QUrl &src, ShaderInfo *result) override; |
| 120 | |
| 121 | private: |
| 122 | bool reflect(ShaderInfo *result); |
| 123 | Status m_status = Uncompiled; |
| 124 | QFileSelector *m_fileSelector = nullptr; |
| 125 | }; |
| 126 | |
| 127 | QT_END_NAMESPACE |
| 128 | |
| 129 | #endif // QSGRHISHADEREFFECTNODE_P_H |
| 130 | |