summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/tests/gl_tests/BuiltinVariableTest.cpp
blob: cbb399dd6e8dcd55c27f007e59aa1c45407e518a (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
//
// Copyright 2016 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// BuiltinVariableTest:
//   Tests the correctness of the builtin GLSL variables.
//

#include "test_utils/ANGLETest.h"

using namespace angle;

class BuiltinVariableVertexIdTest : public ANGLETest
{
  protected:
    BuiltinVariableVertexIdTest()
    {
        setWindowWidth(64);
        setWindowHeight(64);
        setConfigRedBits(8);
        setConfigGreenBits(8);
        setConfigBlueBits(8);
        setConfigAlphaBits(8);
        setConfigDepthBits(24);
    }

    void SetUp() override
    {
        ANGLETest::SetUp();

        const std::string vsSource =
            "#version 300 es\n"
            "precision highp float;\n"
            "in vec4 position;\n"
            "in int expectedID;"
            "out vec4 color;\n"
            "\n"
            "void main()\n"
            "{\n"
            "    gl_Position = position;\n"
            "    color = vec4(gl_VertexID != expectedID, gl_VertexID == expectedID, 0.0, 1.0);"
            "}\n";

        const std::string fsSource =
            "#version 300 es\n"
            "precision highp float;\n"
            "in vec4 color;\n"
            "out vec4 fragColor;\n"
            "void main()\n"
            "{\n"
            "    fragColor = color;\n"
            "}\n";

        mProgram = CompileProgram(vsSource, fsSource);
        ASSERT_NE(0u, mProgram);

        mPositionLocation = glGetAttribLocation(mProgram, "position");
        ASSERT_NE(-1, mPositionLocation);
        mExpectedIdLocation = glGetAttribLocation(mProgram, "expectedID");
        ASSERT_NE(-1, mExpectedIdLocation);

        static const float positions[] =
        {
             0.5,  0.5,
            -0.5,  0.5,
             0.5, -0.5,
            -0.5, -0.5
        };
        glGenBuffers(1, &mPositionBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

        ASSERT_GL_NO_ERROR();
    }

    void TearDown() override
    {
        glDeleteBuffers(1, &mPositionBuffer);
        glDeleteBuffers(1, &mExpectedIdBuffer);
        glDeleteBuffers(1, &mIndexBuffer);
        glDeleteProgram(mProgram);

        ANGLETest::TearDown();
    }

    // Renders a primitive using the specified mode, each vertex color will
    // be green if gl_VertexID is correct, red otherwise.
    void runTest(GLuint drawMode, const std::vector<GLint> &indices, int count)
    {
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);

        glGenBuffers(1, &mIndexBuffer);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * indices.size(), indices.data(),
                     GL_STATIC_DRAW);

        std::vector<GLint> expectedIds = makeRange(count);

        glGenBuffers(1, &mExpectedIdBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, mExpectedIdBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(GLint) * expectedIds.size(), expectedIds.data(),
                     GL_STATIC_DRAW);

        glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
        glVertexAttribPointer(mPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(mPositionLocation);

        glBindBuffer(GL_ARRAY_BUFFER, mExpectedIdBuffer);
        glVertexAttribIPointer(mExpectedIdLocation, 1, GL_INT, 0, 0);
        glEnableVertexAttribArray(mExpectedIdLocation);

        glUseProgram(mProgram);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
        glDrawElements(drawMode, count, GL_UNSIGNED_INT, 0);

        std::vector<GLColor> pixels(getWindowWidth() * getWindowHeight());
        glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RGBA, GL_UNSIGNED_BYTE,
                     pixels.data());

        ASSERT_GL_NO_ERROR();

        const GLColor green(0, 255, 0, 255);
        const GLColor black(0, 0, 0, 255);

        for (const auto &pixel : pixels)
        {
            EXPECT_TRUE(pixel == green || pixel == black);
        }
    }

    std::vector<GLint> makeRange(int n) const
    {
        std::vector<GLint> result;
        for (int i = 0; i < n; i++)
        {
            result.push_back(i);
        }

        return result;
    }

    GLuint mPositionBuffer   = 0;
    GLuint mExpectedIdBuffer = 0;
    GLuint mIndexBuffer      = 0;

    GLuint mProgram           = 0;
    GLint mPositionLocation   = -1;
    GLint mExpectedIdLocation = -1;
};

// Test gl_VertexID when rendering points
TEST_P(BuiltinVariableVertexIdTest, Points)
{
    runTest(GL_POINTS, makeRange(4), 4);
}

// Test gl_VertexID when rendering line strips
TEST_P(BuiltinVariableVertexIdTest, LineStrip)
{
    runTest(GL_LINE_STRIP, makeRange(4), 4);
}

// Test gl_VertexID when rendering line loops
TEST_P(BuiltinVariableVertexIdTest, LineLoop)
{
    runTest(GL_LINE_LOOP, makeRange(4), 4);
}

// Test gl_VertexID when rendering lines
TEST_P(BuiltinVariableVertexIdTest, Lines)
{
    runTest(GL_LINES, makeRange(4), 4);
}

// Test gl_VertexID when rendering triangle strips
TEST_P(BuiltinVariableVertexIdTest, TriangleStrip)
{
    runTest(GL_TRIANGLE_STRIP, makeRange(4), 4);
}

// Test gl_VertexID when rendering triangle fans
TEST_P(BuiltinVariableVertexIdTest, TriangleFan)
{
    std::vector<GLint> indices;
    indices.push_back(0);
    indices.push_back(1);
    indices.push_back(3);
    indices.push_back(2);
    runTest(GL_TRIANGLE_FAN, indices, 4);
}

// Test gl_VertexID when rendering triangles
TEST_P(BuiltinVariableVertexIdTest, Triangles)
{
    std::vector<GLint> indices;
    indices.push_back(0);
    indices.push_back(1);
    indices.push_back(2);
    indices.push_back(1);
    indices.push_back(2);
    indices.push_back(3);
    runTest(GL_TRIANGLES, indices, 6);
}

// Use this to select which configurations (e.g. which renderer, which GLES major version) these
// tests should be run against.
ANGLE_INSTANTIATE_TEST(BuiltinVariableVertexIdTest, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGLES());