1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QSGSIMPLEMATERIAL_H
43 #define QSGSIMPLEMATERIAL_H
45 #include <QtQuick/qsgmaterial.h>
51 template <typename State>
52 class QSGSimpleMaterialShader : public QSGMaterialShader
56 QSGMaterialShader::initialize();
58 m_id_matrix = program()->uniformLocation(uniformMatrixName());
59 if (m_id_matrix < 0) {
60 qFatal("QSGSimpleMaterialShader does not implement 'uniform highp mat4 %s;' in its vertex shader",
64 const char *opacity = uniformOpacityName();
66 m_id_opacity = program()->uniformLocation(uniformOpacityName());
67 if (m_id_opacity < 0) {
68 qFatal("QSGSimpleMaterialShader does not implement 'uniform lowp float %s' in its fragment shader",
69 uniformOpacityName());
78 const char *uniformMatrixName() const { return "qt_Matrix"; }
79 const char *uniformOpacityName() const { return "qt_Opacity"; }
81 void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
83 virtual void updateState(const State *newState, const State *oldState) = 0;
85 virtual void resolveUniforms() {}
87 virtual QList<QByteArray> attributes() const = 0;
89 char const *const *attributeNames() const
91 if (m_attribute_pointers.size())
92 return m_attribute_pointers.constData();
94 QList<QByteArray> names = attributes();
96 // Calculate the total number of bytes needed, so we don't get rellocs and
97 // bad pointers while copying over the individual names.
98 // Add an extra byte pr entry for the '\0' char.
100 for (int i=0; i<names.size(); ++i)
101 total += names.at(i).size() + 1;
102 m_attribute_name_data.reserve(total);
104 // Copy over the names
105 for (int i=0; i<names.size(); ++i) {
106 m_attribute_pointers << m_attribute_name_data.constData() + m_attribute_name_data.size();
107 m_attribute_name_data.append(names.at(i));
108 m_attribute_name_data.append('\0');
111 // Append the "null" terminator
112 m_attribute_pointers << 0;
114 return m_attribute_pointers.constData();
121 mutable QByteArray m_attribute_name_data;
122 mutable QVector<const char *> m_attribute_pointers;
125 #define QSG_DECLARE_SIMPLE_SHADER(Shader, State) \
126 static QSGMaterialShader *createShader() \
131 static QSGSimpleMaterial<State> *createMaterial() \
133 return new QSGSimpleMaterial<State>(createShader); \
137 typedef QSGMaterialShader *(*PtrShaderCreateFunc)();
140 template <typename State>
141 class QSGSimpleMaterial : public QSGMaterial
145 QSGSimpleMaterial(const State &state, PtrShaderCreateFunc func)
151 QSGSimpleMaterial(PtrShaderCreateFunc func)
156 QSGMaterialShader *createShader() const { return m_func(); }
157 QSGMaterialType *type() const { return &m_type; }
159 State *state() { return &m_state; }
160 const State *state() const { return &m_state; }
163 static QSGMaterialType m_type;
165 PtrShaderCreateFunc m_func;
168 #define QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State) \
169 static QSGMaterialShader *createShader() \
174 static QSGSimpleMaterialComparableMaterial<State> *createMaterial() \
176 return new QSGSimpleMaterialComparableMaterial<State>(createShader); \
179 template <typename State>
180 class QSGSimpleMaterialComparableMaterial : public QSGSimpleMaterial<State>
184 QSGSimpleMaterialComparableMaterial(const State &state, PtrShaderCreateFunc func)
185 : QSGSimpleMaterial<State>(state, func) {}
187 QSGSimpleMaterialComparableMaterial(PtrShaderCreateFunc func)
188 : QSGSimpleMaterial<State>(func) {}
190 int compare(const QSGMaterial *other) const {
191 return QSGSimpleMaterialComparableMaterial<State>::state()->compare(static_cast<const QSGSimpleMaterialComparableMaterial<State> *>(other)->state());
196 template <typename State>
197 QSGMaterialType QSGSimpleMaterial<State>::m_type;
200 template <typename State>
201 Q_INLINE_TEMPLATE void QSGSimpleMaterialShader<State>::updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
203 if (state.isMatrixDirty())
204 program()->setUniformValue(m_id_matrix, state.combinedMatrix());
205 if (state.isOpacityDirty() && m_id_opacity >= 0)
206 program()->setUniformValue(m_id_opacity, state.opacity());
208 State *ns = static_cast<QSGSimpleMaterial<State> *>(newMaterial)->state();
211 old = static_cast<QSGSimpleMaterial<State> *>(oldMaterial)->state();
212 updateState(ns, old);