| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 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 QQUICKOPENGLSHADEREFFECT_P_H |
| 41 | #define QQUICKOPENGLSHADEREFFECT_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/qtquickglobal_p.h> |
| 55 | |
| 56 | QT_REQUIRE_CONFIG(quick_shadereffect); |
| 57 | |
| 58 | #include <QtQuick/qquickitem.h> |
| 59 | |
| 60 | #include <QtQuick/qsgmaterial.h> |
| 61 | #include <private/qtquickglobal_p.h> |
| 62 | #include <private/qsgadaptationlayer_p.h> |
| 63 | #include <private/qquickopenglshadereffectnode_p.h> |
| 64 | #include "qquickshadereffect_p.h" |
| 65 | #include "qquickshadereffectmesh_p.h" |
| 66 | |
| 67 | #include <QtCore/qpointer.h> |
| 68 | #include <functional> |
| 69 | |
| 70 | QT_BEGIN_NAMESPACE |
| 71 | |
| 72 | class QSGContext; |
| 73 | class QFileSelector; |
| 74 | class QQuickOpenGLCustomMaterialShader; |
| 75 | |
| 76 | namespace QtPrivate { |
| 77 | class MappedSlotObject; |
| 78 | } |
| 79 | |
| 80 | // Common class for QQuickOpenGLShaderEffect and QQuickCustomParticle. |
| 81 | struct Q_QUICK_PRIVATE_EXPORT QQuickOpenGLShaderEffectCommon |
| 82 | { |
| 83 | typedef QQuickOpenGLShaderEffectMaterialKey Key; |
| 84 | typedef QQuickOpenGLShaderEffectMaterial::UniformData UniformData; |
| 85 | |
| 86 | QQuickOpenGLShaderEffectCommon(QObject *host, std::function<void(int)> mappedPropertyChanged) |
| 87 | : host(host), mappedPropertyChanged(mappedPropertyChanged), fileSelector(nullptr) |
| 88 | { } |
| 89 | |
| 90 | ~QQuickOpenGLShaderEffectCommon(); |
| 91 | |
| 92 | void disconnectPropertySignals(QQuickItem *item, Key::ShaderType shaderType); |
| 93 | void connectPropertySignals(QQuickItem *item, const QMetaObject *itemMetaObject, Key::ShaderType shaderType); |
| 94 | void updateParseLog(bool ignoreAttributes); |
| 95 | void lookThroughShaderCode(QQuickItem *item, const QMetaObject *itemMetaObject, Key::ShaderType shaderType, const QByteArray &code); |
| 96 | void updateShader(QQuickItem *item, const QMetaObject *itemMetaObject, Key::ShaderType shaderType); |
| 97 | void updateMaterial(QQuickOpenGLShaderEffectNode *node, QQuickOpenGLShaderEffectMaterial *material, |
| 98 | bool updateUniforms, bool updateUniformValues, bool updateTextureProviders); |
| 99 | void updateWindow(QQuickWindow *window); |
| 100 | |
| 101 | // Called by slots in QQuickOpenGLShaderEffect: |
| 102 | void sourceDestroyed(QObject *object); |
| 103 | void propertyChanged(QQuickItem *item, const QMetaObject *itemMetaObject, int mappedId, bool *textureProviderChanged); |
| 104 | |
| 105 | void clearSignalMappers(int shader); |
| 106 | |
| 107 | QObject *host; |
| 108 | std::function<void(int)> mappedPropertyChanged; |
| 109 | Key source; |
| 110 | QVector<QByteArray> attributes; |
| 111 | QVector<UniformData> uniformData[Key::ShaderTypeCount]; |
| 112 | QVector<QtPrivate::MappedSlotObject *> signalMappers[Key::ShaderTypeCount]; |
| 113 | QString parseLog; |
| 114 | QFileSelector *fileSelector; |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | class Q_QUICK_PRIVATE_EXPORT QQuickOpenGLShaderEffect : public QObject |
| 119 | { |
| 120 | Q_OBJECT |
| 121 | |
| 122 | public: |
| 123 | QQuickOpenGLShaderEffect(QQuickShaderEffect *item, QObject *parent = nullptr); |
| 124 | ~QQuickOpenGLShaderEffect() override; |
| 125 | |
| 126 | QByteArray fragmentShader() const { return m_common.source.sourceCode[Key::FragmentShader]; } |
| 127 | void setFragmentShader(const QByteArray &code); |
| 128 | |
| 129 | QByteArray vertexShader() const { return m_common.source.sourceCode[Key::VertexShader]; } |
| 130 | void setVertexShader(const QByteArray &code); |
| 131 | |
| 132 | bool blending() const { return m_blending; } |
| 133 | void setBlending(bool enable); |
| 134 | |
| 135 | QVariant mesh() const; |
| 136 | void setMesh(const QVariant &mesh); |
| 137 | |
| 138 | QQuickShaderEffect::CullMode cullMode() const { return m_cullMode; } |
| 139 | void setCullMode(QQuickShaderEffect::CullMode face); |
| 140 | |
| 141 | QString log() const { return m_log; } |
| 142 | QQuickShaderEffect::Status status() const { return m_status; } |
| 143 | |
| 144 | bool supportsAtlasTextures() const { return m_supportsAtlasTextures; } |
| 145 | void setSupportsAtlasTextures(bool supports); |
| 146 | |
| 147 | QString parseLog(); |
| 148 | |
| 149 | void handleEvent(QEvent *); |
| 150 | void handleGeometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); |
| 151 | QSGNode *handleUpdatePaintNode(QSGNode *, QQuickItem::UpdatePaintNodeData *); |
| 152 | void handleItemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value); |
| 153 | void maybeUpdateShaders(bool force = false); |
| 154 | |
| 155 | private Q_SLOTS: |
| 156 | void updateGeometry(); |
| 157 | void updateGeometryIfAtlased(); |
| 158 | void updateLogAndStatus(const QString &log, int status); |
| 159 | void sourceDestroyed(QObject *object); |
| 160 | |
| 161 | private: |
| 162 | void propertyChanged(int mappedId); |
| 163 | |
| 164 | friend class QQuickCustomMaterialShader; |
| 165 | friend class QQuickOpenGLShaderEffectNode; |
| 166 | |
| 167 | typedef QQuickOpenGLShaderEffectMaterialKey Key; |
| 168 | typedef QQuickOpenGLShaderEffectMaterial::UniformData UniformData; |
| 169 | |
| 170 | QQuickShaderEffect *m_item; |
| 171 | const QMetaObject *m_itemMetaObject; |
| 172 | QSize m_meshResolution; |
| 173 | QQuickShaderEffectMesh *m_mesh; |
| 174 | QQuickGridMesh m_defaultMesh; |
| 175 | QQuickShaderEffect::CullMode m_cullMode; |
| 176 | QString m_log; |
| 177 | QQuickShaderEffect::Status m_status; |
| 178 | |
| 179 | QQuickOpenGLShaderEffectCommon m_common; |
| 180 | |
| 181 | uint m_blending : 1; |
| 182 | uint m_dirtyUniforms : 1; |
| 183 | uint m_dirtyUniformValues : 1; |
| 184 | uint m_dirtyTextureProviders : 1; |
| 185 | uint m_dirtyProgram : 1; |
| 186 | uint m_dirtyParseLog : 1; |
| 187 | uint m_dirtyMesh : 1; |
| 188 | uint m_dirtyGeometry : 1; |
| 189 | uint m_customVertexShader : 1; |
| 190 | uint m_supportsAtlasTextures : 1; |
| 191 | uint m_vertNeedsUpdate : 1; |
| 192 | uint m_fragNeedsUpdate : 1; |
| 193 | }; |
| 194 | |
| 195 | QT_END_NAMESPACE |
| 196 | |
| 197 | #endif // QQUICKOPENGLSHADEREFFECT_P_H |
| 198 | |