summaryrefslogtreecommitdiffstats
path: root/src/declarative/items/shadereffectnode.cpp
blob: 7a40c7f79021f806dd34c75d725c9f27375f180c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "shadereffectnode.h"

#include "shadereffectitem.h" // XXX todo
#include "qsgtextureprovider.h"

QT_BEGIN_NAMESPACE

class CustomMaterialShader : public AbstractMaterialShader
{
public:
    CustomMaterialShader(const ShaderEffectMaterialKey &key, const QVector<const char *> &attributes);
    virtual void updateState(Renderer *renderer, AbstractMaterial *newEffect, AbstractMaterial *oldEffect, Renderer::Updates updates);
    virtual char const *const *attributeNames() const;

protected:
    friend class ShaderEffectNode;

    virtual void initialize();
    virtual const char *vertexShader() const;
    virtual const char *fragmentShader() const;

    ShaderEffectMaterialKey m_key;
    QVector<const char *> m_attributes;

    QVector<int> m_uniformLocs;
    int m_opacityLoc;
    int m_matrixLoc;
    uint m_textureIndicesSet;
};

CustomMaterialShader::CustomMaterialShader(const ShaderEffectMaterialKey &key, const QVector<const char *> &attributes)
    : m_key(key)
    , m_attributes(attributes)
    , m_textureIndicesSet(false)
{
}

void CustomMaterialShader::updateState(Renderer *r, AbstractMaterial *newEffect, AbstractMaterial *oldEffect, Renderer::Updates updates)
{
    Q_ASSERT(newEffect != 0);
    Q_UNUSED(oldEffect);
    Q_UNUSED(newEffect);

    const ShaderEffectMaterial *material = static_cast<const ShaderEffectMaterial *>(newEffect);

    if (!m_textureIndicesSet) {
        for (int i = 0; i < material->m_textures.size(); ++i)
            m_program.setUniformValue(material->m_textures.at(i).first.constData(), i);
        m_textureIndicesSet = true;
    }

    if (m_uniformLocs.size() != material->m_uniformValues.size()) {
        m_uniformLocs.reserve(material->m_uniformValues.size());
        for (int i = 0; i < material->m_uniformValues.size(); ++i) {
            const QByteArray &name = material->m_uniformValues.at(i).first;
            m_uniformLocs.append(m_program.uniformLocation(name.constData()));
        }
    }

    for (int i = material->m_textures.size() - 1; i >= 0; --i) {
        QPointer<QSGTextureProvider> source = material->m_textures.at(i).second;
        if (!source || !source->texture().texture())
            continue;

        r->glActiveTexture(GL_TEXTURE0 + i);
        source->bind();
    }

    if (material->m_source.respectsOpacity)
        m_program.setUniformValue(m_opacityLoc, (float) r->renderOpacity());

    for (int i = 0; i < material->m_uniformValues.count(); ++i) {
        const QVariant &v = material->m_uniformValues.at(i).second;

        switch (v.type()) {
        case QVariant::Color:
            m_program.setUniformValue(m_uniformLocs.at(i), qvariant_cast<QColor>(v));
            break;
        case QVariant::Double:
            m_program.setUniformValue(m_uniformLocs.at(i), (float) qvariant_cast<double>(v));
            break;
        case QVariant::Transform:
            m_program.setUniformValue(m_uniformLocs.at(i), qvariant_cast<QTransform>(v));
            break;
        case QVariant::Int:
            m_program.setUniformValue(m_uniformLocs.at(i), v.toInt());
            break;
        case QVariant::Bool:
            m_program.setUniformValue(m_uniformLocs.at(i), GLint(v.toBool()));
            break;
        case QVariant::Size:
        case QVariant::SizeF:
            m_program.setUniformValue(m_uniformLocs.at(i), v.toSizeF());
            break;
        case QVariant::Point:
        case QVariant::PointF:
            m_program.setUniformValue(m_uniformLocs.at(i), v.toPointF());
            break;
        case QVariant::Rect:
        case QVariant::RectF:
            {
                QRectF r = v.toRectF();
                m_program.setUniformValue(m_uniformLocs.at(i), r.x(), r.y(), r.width(), r.height());
            }
            break;
        case QVariant::Vector3D:
            m_program.setUniformValue(m_uniformLocs.at(i), qvariant_cast<QVector3D>(v));
            break;
        default:
            break;
        }
    }

    if ((updates & Renderer::UpdateMatrices) && material->m_source.respectsMatrix)
        m_program.setUniformValue(m_matrixLoc, r->combinedMatrix());
}

char const *const *CustomMaterialShader::attributeNames() const
{
    return m_attributes.constData();
}

void CustomMaterialShader::initialize()
{
    m_opacityLoc = m_program.uniformLocation("qt_Opacity");
    m_matrixLoc = m_program.uniformLocation("qt_ModelViewProjectionMatrix");
}

const char *CustomMaterialShader::vertexShader() const
{
    return m_key.vertexCode.constData();
}

const char *CustomMaterialShader::fragmentShader() const
{
    return m_key.fragmentCode.constData();
}


bool ShaderEffectMaterialKey::operator == (const ShaderEffectMaterialKey &other) const
{
    return vertexCode == other.vertexCode && fragmentCode == other.fragmentCode && className == other.className;
}

uint qHash(const ShaderEffectMaterialKey &key)
{
    return qHash(qMakePair(qMakePair(key.vertexCode, key.fragmentCode), key.className));
}


QHash<ShaderEffectMaterialKey, QSharedPointer<AbstractMaterialType> > ShaderEffectMaterial::materialMap;

ShaderEffectMaterial::ShaderEffectMaterial()
{
    setFlag(Blending, true);
}

AbstractMaterialType *ShaderEffectMaterial::type() const
{
    return m_type.data();
}

AbstractMaterialShader *ShaderEffectMaterial::createShader() const
{
    return new CustomMaterialShader(m_source, m_source.attributeNames);
}

int ShaderEffectMaterial::compare(const AbstractMaterial *other) const
{
    return this - static_cast<const ShaderEffectMaterial *>(other);
}

void ShaderEffectMaterial::setProgramSource(const ShaderEffectProgram &source)
{
    m_source = source;
    m_type = materialMap.value(m_source);
    if (m_type.isNull()) {
        m_type = QSharedPointer<AbstractMaterialType>(new AbstractMaterialType);
        materialMap.insert(m_source, m_type);
    }
}

void ShaderEffectMaterial::setUniforms(const QVector<QPair<QByteArray, QVariant> > &uniformValues)
{
    m_uniformValues = uniformValues;
}

void ShaderEffectMaterial::setTextures(const QVector<QPair<QByteArray, QPointer<QSGTextureProvider> > > &textures)
{
    m_textures = textures;
}

const QVector<QPair<QByteArray, QPointer<QSGTextureProvider> > > &ShaderEffectMaterial::textures() const
{
    return m_textures;
}

void ShaderEffectMaterial::updateTextures() const
{
    for (int i = 0; i < m_textures.size(); ++i) {
        QSGTextureProvider *source = m_textures.at(i).second;
        if (source)
            source->updateTexture();
    }
}


ShaderEffectNode::ShaderEffectNode()
    : m_meshResolution(1, 1)
    , m_geometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4)
{
    Node::setFlag(UsePreprocess, true);
    setGeometry(&m_geometry);
}

ShaderEffectNode::~ShaderEffectNode()
{
}

void ShaderEffectNode::setRect(const QRectF &rect)
{
    m_rect = rect;
    m_dirty_geometry = true;
}

QRectF ShaderEffectNode::rect() const
{
    return m_rect;
}

void ShaderEffectNode::setResolution(const QSize &res)
{
    m_dirty_geometry = true;
    m_meshResolution = res;
}

QSize ShaderEffectNode::resolution() const
{
    return m_meshResolution;
}

void ShaderEffectNode::update()
{
    if (m_dirty_geometry) {
        updateGeometry();
        m_dirty_geometry = false;
    }
}

void ShaderEffectNode::markDirtyTexture()
{
    markDirty(DirtyMaterial);
}

void ShaderEffectNode::updateGeometry()
{
    int vmesh = m_meshResolution.height();
    int hmesh = m_meshResolution.width();

    QSGGeometry *g = geometry();
    if (vmesh == 1 && hmesh == 1) {
        if (g->vertexCount() != 4)
            g->allocate(4);
        QSGGeometry::updateTexturedRectGeometry(g, m_rect, QRectF(0, 0, 1, 1));
        return;
    }

    g->allocate((vmesh + 1) * (hmesh + 1), vmesh * 2 * (hmesh + 2));

    QSGGeometry::TexturedPoint2D *vdata = g->vertexDataAsTexturedPoint2D();

    QRectF dstRect = m_rect;
    QRectF srcRect(0, 0, 1, 1);
    for (int iy = 0; iy <= vmesh; ++iy) {
        float fy = iy / float(vmesh);
        float y = float(dstRect.top()) + fy * float(dstRect.height());
        float ty = float(srcRect.top()) + fy * float(srcRect.height());
        for (int ix = 0; ix <= hmesh; ++ix) {
            float fx = ix / float(hmesh);
            vdata->x = float(dstRect.left()) + fx * float(dstRect.width());
            vdata->y = y;
            vdata->tx = float(srcRect.left()) + fx * float(srcRect.width());
            vdata->ty = ty;
            ++vdata;
        }
    }

    quint16 *indices = (quint16 *)g->indexDataAsUShort();
    int i = 0;
    for (int iy = 0; iy < vmesh; ++iy) {
        *(indices++) = i + hmesh + 1;
        for (int ix = 0; ix <= hmesh; ++ix, ++i) {
            *(indices++) = i + hmesh + 1;
            *(indices++) = i;
        }
        *(indices++) = i - 1;
    }

    markDirty(Node::DirtyGeometry);
}

void ShaderEffectNode::preprocess()
{
    Q_ASSERT(material());
    static_cast<ShaderEffectMaterial *>(material())->updateTextures();
}

QT_END_NAMESPACE