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 QSGMATERIALSHADER_H
41 #define QSGMATERIALSHADER_H
42 
43 #include <QtQuick/qtquickglobal.h>
44 #if QT_CONFIG(opengl)
45 # include <QtGui/qopenglshaderprogram.h>
46 #endif
47 #include <QtGui/QMatrix4x4>
48 #include <QtCore/QRect>
49 #include <QtQuick/qsgmaterialtype.h> // for source compat
50 
51 QT_BEGIN_NAMESPACE
52 
53 class QSGMaterial;
54 class QSGMaterialShaderPrivate;
55 
56 namespace QSGBatchRenderer {
57     class ShaderManager;
58 }
59 
60 class Q_QUICK_EXPORT QSGMaterialShader
61 {
62 public:
63     class Q_QUICK_EXPORT RenderState {
64     public:
65         enum DirtyState
66         {
67             DirtyMatrix             = 0x0001,
68             DirtyOpacity            = 0x0002,
69             DirtyCachedMaterialData = 0x0004,
70             DirtyAll                = 0xFFFF
71         };
Q_DECLARE_FLAGS(DirtyStates,DirtyState)72         Q_DECLARE_FLAGS(DirtyStates, DirtyState)
73 
74         inline DirtyStates dirtyStates() const { return m_dirty; }
75 
isMatrixDirty()76         inline bool isMatrixDirty() const { return m_dirty & DirtyMatrix; }
isOpacityDirty()77         inline bool isOpacityDirty() const { return m_dirty & DirtyOpacity; }
isCachedMaterialDataDirty()78         bool isCachedMaterialDataDirty() const { return m_dirty & DirtyCachedMaterialData; }
79 
80         float opacity() const;
81         QMatrix4x4 combinedMatrix() const;
82         QMatrix4x4 modelViewMatrix() const;
83         QMatrix4x4 projectionMatrix() const;
84         QRect viewportRect() const;
85         QRect deviceRect() const;
86         float determinant() const;
87         float devicePixelRatio() const;
88 #if QT_CONFIG(opengl)
89         QOpenGLContext *context() const;
90 #endif
91     private:
92         friend class QSGRenderer;
93         DirtyStates m_dirty;
94         const void *m_data;
95     };
96 
97     QSGMaterialShader();
98     virtual ~QSGMaterialShader();
99 
100     virtual void activate();
101     virtual void deactivate();
102     // First time a material is used, oldMaterial is null.
103     virtual void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
104     virtual char const *const *attributeNames() const = 0; // Array must end with null.
105 #if QT_CONFIG(opengl)
program()106     inline QOpenGLShaderProgram *program() { return &m_program; }
107 #endif
108 protected:
109     Q_DECLARE_PRIVATE(QSGMaterialShader)
110     QSGMaterialShader(QSGMaterialShaderPrivate &dd);
111 
112     friend class QSGDefaultRenderContext;
113     friend class QSGBatchRenderer::ShaderManager;
114 #if QT_CONFIG(opengl)
115     void setShaderSourceFile(QOpenGLShader::ShaderType type, const QString &sourceFile);
116     void setShaderSourceFiles(QOpenGLShader::ShaderType type, const QStringList &sourceFiles);
117 
118     virtual void compile();
119 #endif
initialize()120     virtual void initialize() { }
121 #if QT_CONFIG(opengl)
122     virtual const char *vertexShader() const;
123     virtual const char *fragmentShader() const;
124 #endif
125 private:
126 #if QT_CONFIG(opengl)
127     QOpenGLShaderProgram m_program;
128 #endif
129     QScopedPointer<QSGMaterialShaderPrivate> d_ptr;
130 };
131 
132 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGMaterialShader::RenderState::DirtyStates)
133 
134 QT_END_NAMESPACE
135 
136 #endif
137