xref: /aosp_15_r20/external/angle/src/tests/gl_tests/RenderbufferMultisampleTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // RenderbufferMultisampleTest: Tests of multisampled renderbuffer
8 
9 #include "test_utils/ANGLETest.h"
10 #include "test_utils/gl_raii.h"
11 
12 using namespace angle;
13 
14 namespace
15 {
16 
17 class RenderbufferMultisampleTest : public ANGLETest<>
18 {
19   protected:
RenderbufferMultisampleTest()20     RenderbufferMultisampleTest()
21     {
22         setWindowWidth(64);
23         setWindowHeight(64);
24         setConfigRedBits(8);
25         setConfigGreenBits(8);
26         setConfigBlueBits(8);
27         setConfigAlphaBits(8);
28         setExtensionsEnabled(false);
29     }
30 
testSetUp()31     void testSetUp() override
32     {
33         glGenRenderbuffers(1, &mRenderbuffer);
34 
35         ASSERT_GL_NO_ERROR();
36     }
37 
testTearDown()38     void testTearDown() override
39     {
40         glDeleteRenderbuffers(1, &mRenderbuffer);
41         mRenderbuffer = 0;
42     }
43 
44     GLuint mRenderbuffer = 0;
45 };
46 
47 class RenderbufferMultisampleTestES31 : public RenderbufferMultisampleTest
48 {};
49 
50 // In GLES 3.0, if internalformat is integer (signed or unsigned), to allocate multisample
51 // renderbuffer storage for that internalformat is not supported. An INVALID_OPERATION is
52 // generated. In GLES 3.1, it is OK to allocate multisample renderbuffer storage for interger
53 // internalformat, but the max samples should be less than MAX_INTEGER_SAMPLES.
54 // MAX_INTEGER_SAMPLES should be at least 1.
TEST_P(RenderbufferMultisampleTest,IntegerInternalformat)55 TEST_P(RenderbufferMultisampleTest, IntegerInternalformat)
56 {
57     // Fixed in recent mesa.  http://crbug.com/1071142
58     ANGLE_SKIP_TEST_IF(IsVulkan() && IsLinux() && (IsIntel() || IsAMD()));
59 
60     glBindRenderbuffer(GL_RENDERBUFFER, mRenderbuffer);
61     glRenderbufferStorageMultisample(GL_RENDERBUFFER, 1, GL_RGBA8I, 64, 64);
62     if (getClientMajorVersion() < 3 || getClientMinorVersion() < 1)
63     {
64         ASSERT_GL_ERROR(GL_INVALID_OPERATION);
65 
66         // Check that NUM_SAMPLE_COUNTS is zero
67         GLint numSampleCounts = 0;
68         glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8I, GL_NUM_SAMPLE_COUNTS, 1,
69                               &numSampleCounts);
70         ASSERT_GL_NO_ERROR();
71         EXPECT_EQ(numSampleCounts, 0);
72     }
73     else
74     {
75         ASSERT_GL_NO_ERROR();
76 
77         GLint maxSamplesRGBA8I = 0;
78         glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8I, GL_SAMPLES, 1, &maxSamplesRGBA8I);
79         GLint maxIntegerSamples = 0;
80         glGetIntegerv(GL_MAX_INTEGER_SAMPLES, &maxIntegerSamples);
81         ASSERT_GL_NO_ERROR();
82         EXPECT_GE(maxIntegerSamples, 1);
83 
84         glRenderbufferStorageMultisample(GL_RENDERBUFFER, maxSamplesRGBA8I + 1, GL_RGBA8I, 64, 64);
85         ASSERT_GL_ERROR(GL_INVALID_OPERATION);
86         glRenderbufferStorageMultisample(GL_RENDERBUFFER, maxIntegerSamples + 1, GL_RGBA8I, 64, 64);
87         ASSERT_GL_ERROR(GL_INVALID_OPERATION);
88     }
89 }
90 
91 // Ensure that the following spec language is correctly implemented:
92 //
93 //   the resulting value for RENDERBUFFER_SAMPLES is guaranteed to be greater than or equal to
94 //   samples and no more than the next larger sample count supported by the implementation.
95 //
96 // For example, if 2, 4, and 8 samples are supported, if 5 samples are requested, ANGLE will
97 // use 8 samples, and return 8 when GL_RENDERBUFFER_SAMPLES is queried.
TEST_P(RenderbufferMultisampleTest,OddSampleCount)98 TEST_P(RenderbufferMultisampleTest, OddSampleCount)
99 {
100     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
101 
102     glBindRenderbuffer(GL_RENDERBUFFER, mRenderbuffer);
103     ASSERT_GL_NO_ERROR();
104 
105     // Lookup the supported number of sample counts
106     GLint numSampleCounts = 0;
107     std::vector<GLint> sampleCounts;
108     GLsizei queryBufferSize = 1;
109     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_NUM_SAMPLE_COUNTS, queryBufferSize,
110                           &numSampleCounts);
111     ANGLE_SKIP_TEST_IF((numSampleCounts < 2));
112     sampleCounts.resize(numSampleCounts);
113     queryBufferSize = numSampleCounts;
114     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_SAMPLES, queryBufferSize,
115                           sampleCounts.data());
116 
117     // Look for two sample counts that are not 1 apart (e.g. 2 and 4).  Request a sample count
118     // that's between those two samples counts (e.g. 3) and ensure that GL_RENDERBUFFER_SAMPLES
119     // is the higher number.
120     for (int i = 1; i < numSampleCounts; i++)
121     {
122         if (sampleCounts[i - 1] > (sampleCounts[i] + 1))
123         {
124             glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCounts[i] + 1, GL_RGBA8, 64,
125                                              64);
126             ASSERT_GL_NO_ERROR();
127             GLint renderbufferSamples = 0;
128             glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES,
129                                          &renderbufferSamples);
130             ASSERT_GL_NO_ERROR();
131             EXPECT_EQ(renderbufferSamples, sampleCounts[i - 1]);
132             break;
133         }
134     }
135 }
136 
137 // Test that when glBlend GL_COLORBURN_KHR is emulated with framebuffer fetch, and the framebuffer
138 // is multisampled, the emulation works fine. This simulates the deqp test:
139 // dEQP-GLES31.functional.blend_equation_advanced.msaa.colorburn.
TEST_P(RenderbufferMultisampleTestES31,ColorBurnBlend)140 TEST_P(RenderbufferMultisampleTestES31, ColorBurnBlend)
141 {
142     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_KHR_blend_equation_advanced"));
143     // Create shader programs
144     std::stringstream vs;
145     vs << "#version 310 es\n"
146           "in highp vec4 a_position;\n"
147           "in mediump vec4 a_color;\n"
148           "out mediump vec4 v_color;\n"
149           "void main()\n"
150           "{\n"
151           "gl_Position = a_position;\n"
152           "v_color = a_color;\n"
153           "}\n";
154 
155     std::stringstream fs;
156 
157     fs << "#version 310 es\n"
158           "#extension GL_KHR_blend_equation_advanced : require\n"
159           "in mediump vec4 v_color;\n"
160           "layout (blend_support_colorburn) out;\n"
161           "layout (location = 0) out mediump vec4 o_color;\n"
162           "void main()\n"
163           "{\n"
164           "o_color = v_color;\n"
165           "}\n";
166 
167     GLuint program;
168 
169     program = CompileProgram(vs.str().c_str(), fs.str().c_str());
170 
171     // Create vertex data and buffers
172     // Create vertex position data
173     std::array<Vector2, 4> quadPos = {
174         Vector2(-1.0f, -1.0f),
175         Vector2(-1.0f, 1.0f),
176         Vector2(1.0f, -1.0f),
177         Vector2(1.0f, 1.0f),
178     };
179 
180     // Create vertex color data
181     std::array<Vector4, 4> quadColor = {
182         Vector4(1.0f, 0.0f, 0.0f, 1.0f), Vector4(1.0f, 0.0f, 0.0f, 1.0f),
183         Vector4(1.0f, 0.0f, 0.0f, 1.0f), Vector4(1.0f, 0.0f, 0.0f, 1.0f)};
184 
185     std::array<uint16_t, 6> quadIndices = {0, 2, 1, 1, 2, 3};
186 
187     GLBuffer indexBuffer;
188     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
189     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quadIndices), quadIndices.data(), GL_STATIC_DRAW);
190 
191     GLBuffer posBuffer;
192     glBindBuffer(GL_ARRAY_BUFFER, posBuffer);
193     glBufferData(GL_ARRAY_BUFFER, sizeof(quadPos), quadPos.data(), GL_STATIC_DRAW);
194     const int posLoc = glGetAttribLocation(program, "a_position");
195     glEnableVertexAttribArray(posLoc);
196     glVertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, 0);
197 
198     GLBuffer colorBuffer;
199     glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
200     glBufferData(GL_ARRAY_BUFFER, sizeof(quadColor), quadColor.data(), GL_STATIC_DRAW);
201     const int colorLoc = glGetAttribLocation(program, "a_color");
202     glEnableVertexAttribArray(colorLoc);
203     glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, 0);
204 
205     // Create a multisampled render buffer and attach it to frame buffer color attachment
206     GLuint rbo;
207     glGenRenderbuffers(1, &rbo);
208     glBindRenderbuffer(GL_RENDERBUFFER, rbo);
209     glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, getWindowWidth(),
210                                      getWindowHeight());
211     GLuint fbo = 0;
212     glGenFramebuffers(1, &fbo);
213     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
214     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
215 
216     // Create a singlesampled render buffer and attach it to frame buffer color attachment
217     GLuint resolvedRbo;
218     glGenRenderbuffers(1, &resolvedRbo);
219     glBindRenderbuffer(GL_RENDERBUFFER, resolvedRbo);
220     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, getWindowWidth(), getWindowHeight());
221     GLuint resolvedFbo = 0;
222     glGenFramebuffers(1, &resolvedFbo);
223     glBindFramebuffer(GL_FRAMEBUFFER, resolvedFbo);
224     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, resolvedRbo);
225 
226     glUseProgram(program);
227     glBlendEquation(GL_COLORBURN_KHR);
228 
229     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
230     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
231     glClear(GL_COLOR_BUFFER_BIT);
232 
233     glDisable(GL_BLEND);
234     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);  // Validation error
235     EXPECT_GL_NO_ERROR();
236 
237     glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
238     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolvedFbo);
239     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
240                       getWindowHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
241     EXPECT_GL_NO_ERROR();  // Validation error
242 
243     glBindFramebuffer(GL_READ_FRAMEBUFFER, resolvedFbo);
244     EXPECT_PIXEL_COLOR_EQ(getWindowWidth() / 4, getWindowHeight() / 4,
245                           GLColor::red);  // Validation error
246     glBindFramebuffer(GL_FRAMEBUFFER, 0);
247 
248     glDeleteProgram(program);
249     glDeleteFramebuffers(1, &fbo);
250     glDeleteRenderbuffers(1, &rbo);
251     glDeleteFramebuffers(1, &resolvedFbo);
252     glDeleteRenderbuffers(1, &resolvedRbo);
253 }
254 
255 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(RenderbufferMultisampleTest);
256 ANGLE_INSTANTIATE_TEST_ES3_AND_ES31(RenderbufferMultisampleTest);
257 
258 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(RenderbufferMultisampleTestES31);
259 ANGLE_INSTANTIATE_TEST_ES31(RenderbufferMultisampleTestES31);
260 }  // namespace
261