2bca97cf17f7fa95a7125de5d5b1b1cfbdbe0e2d
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / util / qsgsimplematerial.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QSGSIMPLEMATERIAL_H
43 #define QSGSIMPLEMATERIAL_H
44
45 #include <QtQuick/qsgmaterial.h>
46
47 QT_BEGIN_HEADER
48
49 QT_BEGIN_NAMESPACE
50
51 template <typename State>
52 class QSGSimpleMaterialShader : public QSGMaterialShader
53 {
54 public:
55     void initialize() {
56         QSGMaterialShader::initialize();
57
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",
61                    uniformMatrixName());
62         }
63
64         const char *opacity = uniformOpacityName();
65         if (opacity) {
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());
70             }
71         } else {
72             m_id_opacity = -1;
73         }
74
75         resolveUniforms();
76     }
77
78     const char *uniformMatrixName() const { return "qt_Matrix"; }
79     const char *uniformOpacityName() const { return "qt_Opacity"; }
80
81     void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
82
83     virtual void updateState(const State *newState, const State *oldState) = 0;
84
85     virtual void resolveUniforms() {}
86
87     virtual QList<QByteArray> attributes() const = 0;
88
89     char const *const *attributeNames() const
90     {
91         if (m_attribute_pointers.size())
92             return m_attribute_pointers.constData();
93
94         QList<QByteArray> names = attributes();
95
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.
99         int total = 0;
100         for (int i=0; i<names.size(); ++i)
101             total += names.at(i).size() + 1;
102         m_attribute_name_data.reserve(total);
103
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');
109         }
110
111         // Append the "null" terminator
112         m_attribute_pointers << 0;
113
114         return m_attribute_pointers.constData();
115     }
116
117 private:
118     int m_id_matrix;
119     int m_id_opacity;
120
121     mutable QByteArray m_attribute_name_data;
122     mutable QVector<const char *> m_attribute_pointers;
123 };
124
125 #define QSG_DECLARE_SIMPLE_SHADER(Shader, State)                \
126 static QSGMaterialShader *createShader()                        \
127 {                                                               \
128     return new Shader;                                          \
129 }                                                               \
130 public:                                                         \
131 static QSGSimpleMaterial<State> *createMaterial()               \
132 {                                                               \
133     return new QSGSimpleMaterial<State>(createShader);          \
134 }
135
136
137 typedef QSGMaterialShader *(*PtrShaderCreateFunc)();
138
139
140 template <typename State>
141 class QSGSimpleMaterial : public QSGMaterial
142 {
143
144 public:
145     QSGSimpleMaterial(const State &state, PtrShaderCreateFunc func)
146         : m_state(state)
147         , m_func(func)
148     {
149     }
150
151     QSGSimpleMaterial(PtrShaderCreateFunc func)
152         : m_func(func)
153     {
154     }
155
156     QSGMaterialShader *createShader() const { return m_func(); }
157     QSGMaterialType *type() const { return &m_type; }
158
159     State *state() { return &m_state; }
160     const State *state() const { return &m_state; }
161
162 private:
163     static QSGMaterialType m_type;
164     State m_state;
165     PtrShaderCreateFunc m_func;
166 };
167
168 #define QSG_DECLARE_SIMPLE_COMPARABLE_SHADER(Shader, State)                     \
169 static QSGMaterialShader *createShader()                        \
170 {                                                               \
171     return new Shader;                                          \
172 }                                                               \
173 public:                                                         \
174 static QSGSimpleMaterialComparableMaterial<State> *createMaterial()                            \
175 {                                                               \
176     return new QSGSimpleMaterialComparableMaterial<State>(createShader);    \
177 }
178
179 template <typename State>
180 class QSGSimpleMaterialComparableMaterial : public QSGSimpleMaterial<State>
181 {
182
183 public:
184     QSGSimpleMaterialComparableMaterial(const State &state, PtrShaderCreateFunc func)
185         : QSGSimpleMaterial<State>(state, func) {}
186
187     QSGSimpleMaterialComparableMaterial(PtrShaderCreateFunc func)
188         : QSGSimpleMaterial<State>(func) {}
189
190     int compare(const QSGMaterial *other) const {
191         return QSGSimpleMaterialComparableMaterial<State>::state()->compare(static_cast<const QSGSimpleMaterialComparableMaterial<State> *>(other)->state());
192     }
193 };
194
195
196 template <typename State>
197 QSGMaterialType QSGSimpleMaterial<State>::m_type;
198
199
200 template <typename State>
201 Q_INLINE_TEMPLATE void QSGSimpleMaterialShader<State>::updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
202 {
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());
207
208     State *ns = static_cast<QSGSimpleMaterial<State> *>(newMaterial)->state();
209     State *old = 0;
210     if (oldMaterial)
211         old = static_cast<QSGSimpleMaterial<State> *>(oldMaterial)->state();
212     updateState(ns, old);
213 }
214
215 QT_END_NAMESPACE
216
217 QT_END_HEADER
218
219
220 #endif