aboutsummaryrefslogtreecommitdiff
path: root/tests/angle_tests/DrawBuffersTest.cpp
blob: fdf2bdbb084893ff6ee36bd9dffa045cbbff7777 (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
#include "ANGLETest.h"

class DrawBuffersTest : public ANGLETest
{
  protected:
    DrawBuffersTest(int clientVersion)
    {
        setWindowWidth(128);
        setWindowHeight(128);
        setConfigRedBits(8);
        setConfigGreenBits(8);
        setConfigBlueBits(8);
        setConfigAlphaBits(8);
        setConfigDepthBits(24);
        setClientVersion(clientVersion);
    }

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

        glGenFramebuffers(1, &mFBO);
        glBindFramebuffer(GL_FRAMEBUFFER, mFBO);

        glGenTextures(4, mTextures);

        for (size_t texIndex = 0; texIndex < ArraySize(mTextures); texIndex++)
        {
            glBindTexture(GL_TEXTURE_2D, mTextures[texIndex]);
            glTexStorage2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
        }

        GLfloat data[] =
        {
            -1.0f, 1.0f,
            -1.0f, -2.0f,
            2.0f, 1.0f
        };

        glGenBuffers(1, &mBuffer);
        glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6, data, GL_STATIC_DRAW);

        GLint maxDrawBuffers;
        glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
        ASSERT_EQ(maxDrawBuffers, 8);

        ASSERT_GL_NO_ERROR();
    }

    virtual void TearDown()
    {
        glDeleteFramebuffers(1, &mFBO);
        glDeleteTextures(4, mTextures);
        glDeleteBuffers(1, &mBuffer);

        ANGLETest::TearDown();
    }

    void setupMRTProgramESSL3(bool bufferEnabled[8], GLuint *programOut)
    {
        const std::string vertexShaderSource =
            "#version 300 es\n"
            "in vec4 position;\n"
            "void main() {\n"
            "    gl_Position = position;\n"
            "}\n";

        std::stringstream strstr;

        strstr << "#version 300 es\n"
                  "precision highp float;\n";

        for (unsigned int index = 0; index < 8; index++)
        {
            if (bufferEnabled[index])
            {
                strstr << "layout(location = " << index << ") "
                          "out vec4 value" << index << ";\n";
            }
        }

        strstr << "void main()\n"
                  "{\n";

        for (unsigned int index = 0; index < 8; index++)
        {
            if (bufferEnabled[index])
            {
                unsigned int r = (index + 1) & 1;
                unsigned int g = (index + 1) & 2;
                unsigned int b = (index + 1) & 4;

                strstr << "    value" << index << " = vec4("
                       << r << ".0, " << g << ".0, "
                       << b << ".0, 1.0);\n";
            }
        }

        strstr << "}\n";

        *programOut = CompileProgram(vertexShaderSource, strstr.str());
        if (*programOut == 0)
        {
            FAIL() << "shader compilation failed.";
        }

        glUseProgram(*programOut);

        GLint location = glGetAttribLocation(*programOut, "position");
        ASSERT_NE(location, -1);
        glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
        glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 8, NULL);
        glEnableVertexAttribArray(location);
    }

    void setupMRTProgramESSL1(bool bufferEnabled[8], GLuint *programOut)
    {
        const std::string vertexShaderSource =
            "attribute vec4 position;\n"
            "void main() {\n"
            "    gl_Position = position;\n"
            "}\n";

        std::stringstream strstr;

        strstr << "#extension GL_EXT_draw_buffers : enable\n"
                  "precision highp float;\n"
                  "void main()\n"
                  "{\n";

        for (unsigned int index = 0; index < 8; index++)
        {
            if (bufferEnabled[index])
            {
                unsigned int r = (index + 1) & 1;
                unsigned int g = (index + 1) & 2;
                unsigned int b = (index + 1) & 4;

                strstr << "    gl_FragData[" << index << "] = vec4("
                    << r << ".0, " << g << ".0, "
                    << b << ".0, 1.0);\n";
            }
        }

        strstr << "}\n";

        *programOut = CompileProgram(vertexShaderSource, strstr.str());
        if (*programOut == 0)
        {
            FAIL() << "shader compilation failed.";
        }

        glUseProgram(*programOut);

        GLint location = glGetAttribLocation(*programOut, "position");
        ASSERT_NE(location, -1);
        glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
        glVertexAttribPointer(location, 2, GL_FLOAT, GL_FALSE, 8, NULL);
        glEnableVertexAttribArray(location);
    }

    void setupMRTProgram(bool bufferEnabled[8], GLuint *programOut)
    {
        if (getClientVersion() == 3)
        {
            setupMRTProgramESSL3(bufferEnabled, programOut);
        }
        else
        {
            ASSERT_EQ(getClientVersion(), 2);
            setupMRTProgramESSL1(bufferEnabled, programOut);
        }
    }

    void verifyAttachment(unsigned int index, GLuint textureName)
    {
        for (unsigned int colorAttachment = 0; colorAttachment < 8; colorAttachment++)
        {
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttachment, GL_TEXTURE_2D, 0, 0);
        }

        glBindTexture(GL_TEXTURE_2D, textureName);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);

        unsigned int r = (((index + 1) & 1) > 0) ? 255 : 0;
        unsigned int g = (((index + 1) & 2) > 0) ? 255 : 0;
        unsigned int b = (((index + 1) & 4) > 0) ? 255 : 0;

        EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, r, g, b, 255);
    }

    void gapsTest()
    {
        glBindTexture(GL_TEXTURE_2D, mTextures[0]);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, mTextures[0], 0);

        bool flags[8] = { false, true };

        GLuint program;
        setupMRTProgram(flags, &program);

        const GLenum bufs[] =
        {
            GL_NONE,
            GL_COLOR_ATTACHMENT1
        };
        glUseProgram(program);
        glDrawBuffersEXT(2, bufs);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        verifyAttachment(1, mTextures[0]);

        glDeleteProgram(program);
    }

    void firstAndLastTest()
    {
        glBindTexture(GL_TEXTURE_2D, mTextures[0]);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextures[0], 0);

        glBindTexture(GL_TEXTURE_2D, mTextures[1]);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, mTextures[1], 0);

        bool flags[8] = { true, false, false, true };

        GLuint program;
        setupMRTProgram(flags, &program);

        const GLenum bufs[] =
        {
            GL_COLOR_ATTACHMENT0,
            GL_NONE,
            GL_NONE,
            GL_COLOR_ATTACHMENT3
        };

        glUseProgram(program);
        glDrawBuffersEXT(4, bufs);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        verifyAttachment(0, mTextures[0]);
        verifyAttachment(3, mTextures[1]);

        EXPECT_GL_NO_ERROR();

        glDeleteProgram(program);
    }

    void firstHalfNULLTest()
    {
        bool flags[8] = { false };
        GLenum bufs[8] = { GL_NONE };

        for (unsigned int texIndex = 0; texIndex < 4; texIndex++)
        {
            glBindTexture(GL_TEXTURE_2D, mTextures[texIndex]);
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT4 + texIndex, GL_TEXTURE_2D, mTextures[texIndex], 0);
            flags[texIndex + 4] = true;
            bufs[texIndex + 4] = GL_COLOR_ATTACHMENT4 + texIndex;
        }

        GLuint program;
        setupMRTProgram(flags, &program);

        glUseProgram(program);
        glDrawBuffersEXT(8, bufs);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        for (unsigned int texIndex = 0; texIndex < 4; texIndex++)
        {
            verifyAttachment(texIndex + 4, mTextures[texIndex]);
        }

        EXPECT_GL_NO_ERROR();

        glDeleteProgram(program);
    }

    GLuint mFBO;
    GLuint mTextures[4];
    GLuint mBuffer;
};

class DrawBuffersTestESSL3 : public DrawBuffersTest
{
  protected:
    DrawBuffersTestESSL3()
        : DrawBuffersTest(3)
    {}
};

class DrawBuffersTestESSL1 : public DrawBuffersTest
{
  protected:
    DrawBuffersTestESSL1()
        : DrawBuffersTest(2)
    {}
};

TEST_F(DrawBuffersTestESSL3, Gaps)
{
    gapsTest();
}

TEST_F(DrawBuffersTestESSL1, Gaps)
{
    gapsTest();
}

TEST_F(DrawBuffersTestESSL3, FirstAndLast)
{
    firstAndLastTest();
}

TEST_F(DrawBuffersTestESSL1, FirstAndLast)
{
    firstAndLastTest();
}

TEST_F(DrawBuffersTestESSL3, FirstHalfNULL)
{
    firstHalfNULLTest();
}

TEST_F(DrawBuffersTestESSL1, FirstHalfNULL)
{
    firstHalfNULLTest();
}