summaryrefslogtreecommitdiffstats
path: root/tests/auto/opengl/qglfunctions/tst_qglfunctions.cpp
blob: 2f09d847729c188099afe2e05077bb5834cc49e2 (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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** 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, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include <QtOpenGL/qglfunctions.h>

class tst_QGLFunctions : public QObject
{
    Q_OBJECT
public:
    tst_QGLFunctions() {}
    ~tst_QGLFunctions() {}

private slots:
    void features();
    void multitexture();
    void blendColor();

private:
    static bool hasExtension(const char *name);
};

bool tst_QGLFunctions::hasExtension(const char *name)
{
    QString extensions =
        QString::fromLatin1
            (reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)));
    return extensions.split(QLatin1Char(' ')).contains
        (QString::fromLatin1(name));
}

// Check that the reported features are consistent with the platform.
void tst_QGLFunctions::features()
{
    // Before being associated with a context, there should be
    // no features enabled.
    QGLFunctions funcs;
    QVERIFY(!funcs.openGLFeatures());
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::Multitexture));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::Shaders));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::Buffers));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::Framebuffers));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::BlendColor));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::BlendEquation));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::BlendEquationSeparate));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::BlendFuncSeparate));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::BlendSubtract));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::CompressedTextures));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::Multisample));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::StencilSeparate));
    QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::NPOTTextures));

    // Make a context current.
    QGLWidget glw;
    if (!glw.isValid())
        QSKIP("Could not create a GL context");
    glw.makeCurrent();
    funcs.initializeGLFunctions();

    // Validate the features against what we expect for this platform.
    if (QOpenGLContext::currentContext()->isOpenGLES()) {
#if !defined(QT_OPENGL_ES) || defined(QT_OPENGL_ES_2)
        QGLFunctions::OpenGLFeatures allFeatures =
            (QGLFunctions::Multitexture |
             QGLFunctions::Shaders |
             QGLFunctions::Buffers |
             QGLFunctions::Framebuffers |
             QGLFunctions::BlendColor |
             QGLFunctions::BlendEquation |
             QGLFunctions::BlendEquationSeparate |
             QGLFunctions::BlendFuncSeparate |
             QGLFunctions::BlendSubtract |
             QGLFunctions::CompressedTextures |
             QGLFunctions::Multisample |
             QGLFunctions::StencilSeparate |
             QGLFunctions::NPOTTextures);
        QVERIFY((funcs.openGLFeatures() & allFeatures) == allFeatures);
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Multitexture));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Shaders));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Buffers));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Framebuffers));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::BlendColor));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::BlendEquation));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::BlendEquationSeparate));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::BlendFuncSeparate));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::BlendSubtract));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::CompressedTextures));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Multisample));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::StencilSeparate));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::NPOTTextures));
#elif defined(QT_OPENGL_ES) && !defined(QT_OPENGL_ES_2)
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Multitexture));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Buffers));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::CompressedTextures));
        QVERIFY(funcs.hasOpenGLFeature(QGLFunctions::Multisample));

        QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::Shaders));
        QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::BlendColor));
        QVERIFY(!funcs.hasOpenGLFeature(QGLFunctions::StencilSeparate));

        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::Framebuffers),
                 hasExtension("GL_OES_framebuffer_object"));
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendEquationSeparate),
                 hasExtension("GL_OES_blend_equation_separate"));
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendFuncSeparate),
                 hasExtension("GL_OES_blend_func_separate"));
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendSubtract),
                 hasExtension("GL_OES_blend_subtract"));
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::NPOTTextures),
                 hasExtension("GL_OES_texture_npot"));
#endif
    } else {
        // We check for both the extension name and the minimum OpenGL version
        // for the feature.  This will help us catch situations where a platform
        // doesn't list an extension by name but does have the feature by virtue
        // of its version number.
        QGLFormat::OpenGLVersionFlags versions = QGLFormat::openGLVersionFlags();
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::Multitexture),
                 hasExtension("GL_ARB_multitexture") ||
                 (versions & QGLFormat::OpenGL_Version_1_3) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::Shaders),
                 hasExtension("GL_ARB_shader_objects") ||
                 (versions & QGLFormat::OpenGL_Version_2_0) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::Buffers),
                 (versions & QGLFormat::OpenGL_Version_1_5) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::Framebuffers),
                 hasExtension("GL_EXT_framebuffer_object") ||
                 hasExtension("GL_ARB_framebuffer_object"));
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendColor),
                 hasExtension("GL_EXT_blend_color") ||
                 (versions & QGLFormat::OpenGL_Version_1_2) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendEquation),
                 (versions & QGLFormat::OpenGL_Version_1_2) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendEquationSeparate),
                 hasExtension("GL_EXT_blend_equation_separate") ||
                 (versions & QGLFormat::OpenGL_Version_2_0) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendFuncSeparate),
                 hasExtension("GL_EXT_blend_func_separate") ||
                 (versions & QGLFormat::OpenGL_Version_1_4) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::BlendSubtract),
                 hasExtension("GL_EXT_blend_subtract"));
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::CompressedTextures),
                 hasExtension("GL_ARB_texture_compression") ||
                 (versions & QGLFormat::OpenGL_Version_1_3) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::Multisample),
                 hasExtension("GL_ARB_multisample") ||
                 (versions & QGLFormat::OpenGL_Version_1_3) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::StencilSeparate),
                 (versions & QGLFormat::OpenGL_Version_2_0) != 0);
        QCOMPARE(funcs.hasOpenGLFeature(QGLFunctions::NPOTTextures),
                 hasExtension("GL_ARB_texture_non_power_of_two") ||
                 (versions & QGLFormat::OpenGL_Version_2_0) != 0);
    }
}

// Verify that the multitexture functions appear to resolve and work.
void tst_QGLFunctions::multitexture()
{
    QGLFunctions funcs;
    QGLWidget glw;
    if (!glw.isValid())
        QSKIP("Could not create a GL context");
    glw.makeCurrent();
    funcs.initializeGLFunctions();

    if (!funcs.hasOpenGLFeature(QGLFunctions::Multitexture))
        QSKIP("Multitexture functions are not supported");

    funcs.glActiveTexture(GL_TEXTURE1);

    GLint active = 0;
    glGetIntegerv(GL_ACTIVE_TEXTURE, &active);
    QVERIFY(active == GL_TEXTURE1);

    funcs.glActiveTexture(GL_TEXTURE0);

    active = 0;
    glGetIntegerv(GL_ACTIVE_TEXTURE, &active);
    QVERIFY(active == GL_TEXTURE0);
}

// Verify that the glBlendColor() function appears to resolve and work.
void tst_QGLFunctions::blendColor()
{
    QGLFunctions funcs;
    QGLWidget glw;
    if (!glw.isValid())
        QSKIP("Could not create a GL context");
    glw.makeCurrent();
    funcs.initializeGLFunctions();

    if (!funcs.hasOpenGLFeature(QGLFunctions::BlendColor))
        QSKIP("glBlendColor() is not supported");

    funcs.glBlendColor(0.0f, 1.0f, 0.0f, 1.0f);

    GLfloat colors[4] = {0.5f, 0.5f, 0.5f, 0.5f};
    glGetFloatv(GL_BLEND_COLOR, colors);

    QCOMPARE(colors[0], 0.0f);
    QCOMPARE(colors[1], 1.0f);
    QCOMPARE(colors[2], 0.0f);
    QCOMPARE(colors[3], 1.0f);
}

QTEST_MAIN(tst_QGLFunctions)

#include "tst_qglfunctions.moc"