summaryrefslogtreecommitdiffstats
path: root/gfx/angle/src/tests/gl_tests/FramebufferMixedSamplesTest.cpp
blob: 5baf9303ab00502d2a109a0a43be78f16625bfc8 (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
//
// 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.
//
// CHROMIUMFramebufferMixedSamplesTest
//   Test CHROMIUM subset of NV_framebuffer_mixed_samples.
//   This extension allows rendering to a framebuffer that has different
//   sample counts for different render buffers (stencil, depth, color)

#include "test_utils/ANGLETest.h"
#include "shader_utils.h"

using namespace angle;

namespace
{

const GLuint kWidth  = 100;
const GLuint kHeight = 100;

class CHROMIUMFramebufferMixedSamplesTest : public ANGLETest
{
  protected:
    enum SetupFBOType
    {
        MixedSampleFBO,   // 1 color sample, N stencil samples.
        SingleSampleFBO,  // 1 color sample, 1 stencil sample.
    };

    bool isApplicable() const
    {
        return extensionEnabled("GL_CHROMIUM_framebuffer_mixed_samples") &&
               extensionEnabled("GL_OES_rgb8_rgba8");
    }

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

        // clang-format off
        static const char* kVertexShaderSource =
            "attribute mediump vec4 position;\n"
            "void main() {\n"
            "  gl_Position = position;\n"
            "}\n";

        static const char* kFragmentShaderSource =
            "uniform mediump vec4 color;\n"
            "void main() {\n"
            "  gl_FragColor = color;\n"
            "}\n";

        // clang-format on
        mProgram = CompileProgram(kVertexShaderSource, kFragmentShaderSource);

        GLuint position_loc = glGetAttribLocation(mProgram, "position");
        mColorLoc           = glGetUniformLocation(mProgram, "color");

        glGenBuffers(1, &mVBO);
        glBindBuffer(GL_ARRAY_BUFFER, mVBO);

        static float vertices[] = {
            1.0f,  1.0f, -1.0f, 1.0f,  -1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
            -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f,  -1.0f, 1.0f, 1.0f,
        };
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
        glEnableVertexAttribArray(position_loc);
        glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0);

        ASSERT_GL_NO_ERROR();
    }

    void TearDown() override
    {
        glDeleteBuffers(1, &mVBO);
        glDeleteProgram(mProgram);

        ASSERT_GL_NO_ERROR();

        ANGLETest::TearDown();
    }

    void prepareForDraw(SetupFBOType fbo_type)
    {
        glActiveTexture(GL_TEXTURE0);
        glGenTextures(1, &mTexture);
        glBindTexture(GL_TEXTURE_2D, mTexture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
                     NULL);
        glBindTexture(GL_TEXTURE_2D, 0);

        glGenRenderbuffers(1, &mStencilRB);
        glBindRenderbuffer(GL_RENDERBUFFER, mStencilRB);

        if (fbo_type == MixedSampleFBO)
        {
            // Create a sample buffer.
            GLsizei num_samples = 8, max_samples = 0;
            glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
            num_samples = std::min(num_samples, max_samples);
            glRenderbufferStorageMultisampleANGLE(GL_RENDERBUFFER, num_samples, GL_STENCIL_INDEX8,
                                                  kWidth, kHeight);
            GLint param = 0;
            glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &param);
            EXPECT_GT(param, 1);
        }
        else
        {
            glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, kWidth, kHeight);
        }
        glBindRenderbuffer(GL_RENDERBUFFER, 0);

        glGenFramebuffers(1, &mSampleFBO);
        glBindFramebuffer(GL_FRAMEBUFFER, mSampleFBO);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture, 0);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
                                  mStencilRB);
        EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
                  glCheckFramebufferStatus(GL_FRAMEBUFFER));

        glUseProgram(mProgram);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glViewport(0, 0, kWidth, kHeight);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClearStencil(1);
        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glEnable(GL_STENCIL_TEST);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glStencilMask(0xffffffff);
        glStencilFunc(GL_EQUAL, 1, 0xffffffff);
        glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);

        ASSERT_GL_NO_ERROR();
    }

    void cleanup()
    {
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glDeleteFramebuffers(1, &mSampleFBO);
        glDeleteTextures(1, &mTexture);
        glDeleteRenderbuffers(1, &mStencilRB);

        ASSERT_GL_NO_ERROR();
    }

    GLuint mSampleFBO;
    GLuint mStencilRB;
    GLuint mTexture;

    GLuint mProgram;
    GLuint mVBO;
    GLint mColorLoc;
};

}  //

TEST_P(CHROMIUMFramebufferMixedSamplesTest, StateSettingTest)
{
    if (!isApplicable())
    {
        return;
    }

    GLint value = -1;
    glGetIntegerv(GL_COVERAGE_MODULATION_CHROMIUM, &value);
    EXPECT_EQ(GL_NONE, value);
    GLenum kValues[] = {GL_NONE, GL_RGB, GL_RGBA, GL_ALPHA};
    for (auto expect : kValues)
    {
        glCoverageModulationCHROMIUM(expect);
        value = -1;
        glGetIntegerv(GL_COVERAGE_MODULATION_CHROMIUM, &value);
        EXPECT_EQ(expect, static_cast<GLenum>(value));
        EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
    }

    glCoverageModulationCHROMIUM(GL_BYTE);
    EXPECT_EQ(static_cast<GLenum>(GL_INVALID_ENUM), glGetError());
    value = -1;
    glGetIntegerv(GL_COVERAGE_MODULATION_CHROMIUM, &value);
    EXPECT_EQ(static_cast<GLenum>(GL_ALPHA), static_cast<GLenum>(value));
    EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError());
}

// The test pattern is as follows:
// A green triangle (bottom left, top right, top left).
// A blue triangle (top left, bottom right, bottom left).
// The triangles will overlap but overlap only contains green pixels,
// due to each draw erasing its area from stencil.
// The blue triangle will fill only the area (bottom left, center,
// bottom right).

// The test tests that CoverageModulation call works.
// The fractional pixels of both triangles end up being modulated
// by the coverage of the fragment. Test that drawing with and without
// CoverageModulation causes the result to be different.
TEST_P(CHROMIUMFramebufferMixedSamplesTest, CoverageModulation)
{
    if (!isApplicable())
    {
        return;
    }
    static const float kBlue[]  = {0.0f, 0.0f, 1.0f, 1.0f};
    static const float kGreen[] = {0.0f, 1.0f, 0.0f, 1.0f};
    std::unique_ptr<uint8_t[]> results[3];
    const GLint kResultSize = kWidth * kHeight * 4;

    for (int pass = 0; pass < 3; ++pass)
    {
        prepareForDraw(MixedSampleFBO);
        if (pass == 1)
        {
            glCoverageModulationCHROMIUM(GL_RGBA);
        }
        glUniform4fv(mColorLoc, 1, kGreen);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glUniform4fv(mColorLoc, 1, kBlue);
        glDrawArrays(GL_TRIANGLES, 3, 3);
        if (pass == 1)
        {
            glCoverageModulationCHROMIUM(GL_NONE);
        }
        results[pass].reset(new uint8_t[kResultSize]);
        memset(results[pass].get(), 123u, kResultSize);
        glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, results[pass].get());

        cleanup();
    }

    EXPECT_NE(0, memcmp(results[0].get(), results[1].get(), kResultSize));
    // Verify that rendering is deterministic, so that the pass above does not
    // come from non-deterministic rendering.
    EXPECT_EQ(0, memcmp(results[0].get(), results[2].get(), kResultSize));
}

// The test tests that the stencil buffer can be multisampled, even though the
// color buffer is single-sampled. Draws the same pattern with single-sample
// stencil buffer and with multisample stencil buffer. The images should differ.
TEST_P(CHROMIUMFramebufferMixedSamplesTest, MultisampleStencilEffective)
{
    if (!isApplicable())
    {
        return;
    }

    static const float kBlue[]  = {0.0f, 0.0f, 1.0f, 1.0f};
    static const float kGreen[] = {0.0f, 1.0f, 0.0f, 1.0f};

    std::unique_ptr<uint8_t[]> results[3];
    const GLint kResultSize = kWidth * kHeight * 4;

    for (int pass = 0; pass < 3; ++pass)
    {
        if (pass == 1)
        {
            prepareForDraw(MixedSampleFBO);
        }
        else
        {
            prepareForDraw(SingleSampleFBO);
        }
        glUniform4fv(mColorLoc, 1, kGreen);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        glUniform4fv(mColorLoc, 1, kBlue);
        glDrawArrays(GL_TRIANGLES, 3, 3);

        results[pass].reset(new uint8_t[kResultSize]);
        memset(results[pass].get(), 12u, kResultSize);
        glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, results[pass].get());

        cleanup();
    }

    EXPECT_NE(0, memcmp(results[0].get(), results[1].get(), kResultSize));
    // Verify that rendering is deterministic, so that the pass above does not
    // come from non-deterministic rendering.
    EXPECT_EQ(0, memcmp(results[0].get(), results[2].get(), kResultSize));
}

ANGLE_INSTANTIATE_TEST(CHROMIUMFramebufferMixedSamplesTest,
                       ES2_OPENGL(),
                       ES2_OPENGLES(),
                       ES3_OPENGL());