xref: /aosp_15_r20/external/angle/src/tests/gl_tests/SampleVariablesTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2023 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // Test built-in variables added by OES_sample_variables
7*8975f5c5SAndroid Build Coastguard Worker 
8*8975f5c5SAndroid Build Coastguard Worker #include <unordered_set>
9*8975f5c5SAndroid Build Coastguard Worker 
10*8975f5c5SAndroid Build Coastguard Worker #include "common/mathutil.h"
11*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/ANGLETest.h"
12*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/gl_raii.h"
13*8975f5c5SAndroid Build Coastguard Worker 
14*8975f5c5SAndroid Build Coastguard Worker using namespace angle;
15*8975f5c5SAndroid Build Coastguard Worker 
16*8975f5c5SAndroid Build Coastguard Worker namespace
17*8975f5c5SAndroid Build Coastguard Worker {
18*8975f5c5SAndroid Build Coastguard Worker 
19*8975f5c5SAndroid Build Coastguard Worker class SampleVariablesTest : public ANGLETest<>
20*8975f5c5SAndroid Build Coastguard Worker {
21*8975f5c5SAndroid Build Coastguard Worker   protected:
SampleVariablesTest()22*8975f5c5SAndroid Build Coastguard Worker     SampleVariablesTest()
23*8975f5c5SAndroid Build Coastguard Worker     {
24*8975f5c5SAndroid Build Coastguard Worker         setConfigRedBits(8);
25*8975f5c5SAndroid Build Coastguard Worker         setConfigGreenBits(8);
26*8975f5c5SAndroid Build Coastguard Worker         setConfigBlueBits(8);
27*8975f5c5SAndroid Build Coastguard Worker         setConfigAlphaBits(8);
28*8975f5c5SAndroid Build Coastguard Worker     }
29*8975f5c5SAndroid Build Coastguard Worker };
30*8975f5c5SAndroid Build Coastguard Worker 
31*8975f5c5SAndroid Build Coastguard Worker // Test gl_MaxSamples == GL_MAX_SAMPLES
TEST_P(SampleVariablesTest,MaxSamples)32*8975f5c5SAndroid Build Coastguard Worker TEST_P(SampleVariablesTest, MaxSamples)
33*8975f5c5SAndroid Build Coastguard Worker {
34*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_sample_variables"));
35*8975f5c5SAndroid Build Coastguard Worker 
36*8975f5c5SAndroid Build Coastguard Worker     GLint maxSamples = -1;
37*8975f5c5SAndroid Build Coastguard Worker     glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
38*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GT(maxSamples, 0);
39*8975f5c5SAndroid Build Coastguard Worker     ASSERT_LE(maxSamples, 32);
40*8975f5c5SAndroid Build Coastguard Worker 
41*8975f5c5SAndroid Build Coastguard Worker     const char kFS[] = R"(#version 300 es
42*8975f5c5SAndroid Build Coastguard Worker #extension GL_OES_sample_variables : require
43*8975f5c5SAndroid Build Coastguard Worker precision highp float;
44*8975f5c5SAndroid Build Coastguard Worker out vec4 color;
45*8975f5c5SAndroid Build Coastguard Worker void main()
46*8975f5c5SAndroid Build Coastguard Worker {
47*8975f5c5SAndroid Build Coastguard Worker     color = vec4(float(gl_MaxSamples * 4) / 255.0, 0.0, 0.0, 1.0);
48*8975f5c5SAndroid Build Coastguard Worker })";
49*8975f5c5SAndroid Build Coastguard Worker 
50*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
51*8975f5c5SAndroid Build Coastguard Worker     drawQuad(program, essl3_shaders::PositionAttrib(), 0.0);
52*8975f5c5SAndroid Build Coastguard Worker 
53*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_NEAR(0, 0, maxSamples * 4, 0, 0, 255, 1.0);
54*8975f5c5SAndroid Build Coastguard Worker }
55*8975f5c5SAndroid Build Coastguard Worker 
56*8975f5c5SAndroid Build Coastguard Worker // Test gl_NumSamples == GL_SAMPLES
TEST_P(SampleVariablesTest,NumSamples)57*8975f5c5SAndroid Build Coastguard Worker TEST_P(SampleVariablesTest, NumSamples)
58*8975f5c5SAndroid Build Coastguard Worker {
59*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_sample_variables"));
60*8975f5c5SAndroid Build Coastguard Worker 
61*8975f5c5SAndroid Build Coastguard Worker     const char kFS[] = R"(#version 300 es
62*8975f5c5SAndroid Build Coastguard Worker #extension GL_OES_sample_variables : require
63*8975f5c5SAndroid Build Coastguard Worker precision highp float;
64*8975f5c5SAndroid Build Coastguard Worker out vec4 color;
65*8975f5c5SAndroid Build Coastguard Worker void main()
66*8975f5c5SAndroid Build Coastguard Worker {
67*8975f5c5SAndroid Build Coastguard Worker     color = vec4(float(gl_NumSamples * 4) / 255.0, 0.0, 0.0, 1.0);
68*8975f5c5SAndroid Build Coastguard Worker })";
69*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
70*8975f5c5SAndroid Build Coastguard Worker 
71*8975f5c5SAndroid Build Coastguard Worker     GLint numSampleCounts = 0;
72*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_NUM_SAMPLE_COUNTS, 1, &numSampleCounts);
73*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GT(numSampleCounts, 0);
74*8975f5c5SAndroid Build Coastguard Worker 
75*8975f5c5SAndroid Build Coastguard Worker     std::vector<GLint> sampleCounts;
76*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.resize(numSampleCounts);
77*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_SAMPLES, numSampleCounts,
78*8975f5c5SAndroid Build Coastguard Worker                           sampleCounts.data());
79*8975f5c5SAndroid Build Coastguard Worker 
80*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.push_back(0);
81*8975f5c5SAndroid Build Coastguard Worker 
82*8975f5c5SAndroid Build Coastguard Worker     for (GLint sampleCount : sampleCounts)
83*8975f5c5SAndroid Build Coastguard Worker     {
84*8975f5c5SAndroid Build Coastguard Worker         GLFramebuffer fbo;
85*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
86*8975f5c5SAndroid Build Coastguard Worker 
87*8975f5c5SAndroid Build Coastguard Worker         GLRenderbuffer rbo;
88*8975f5c5SAndroid Build Coastguard Worker         glBindRenderbuffer(GL_RENDERBUFFER, rbo);
89*8975f5c5SAndroid Build Coastguard Worker         glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_RGBA8, 1, 1);
90*8975f5c5SAndroid Build Coastguard Worker         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
91*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
92*8975f5c5SAndroid Build Coastguard Worker 
93*8975f5c5SAndroid Build Coastguard Worker         GLint samples = -1;
94*8975f5c5SAndroid Build Coastguard Worker         glGetIntegerv(GL_SAMPLES, &samples);
95*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(samples, sampleCount);
96*8975f5c5SAndroid Build Coastguard Worker 
97*8975f5c5SAndroid Build Coastguard Worker         drawQuad(program, essl3_shaders::PositionAttrib(), 0.0);
98*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_NO_ERROR();
99*8975f5c5SAndroid Build Coastguard Worker 
100*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
101*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
102*8975f5c5SAndroid Build Coastguard Worker         glBlitFramebuffer(0, 0, 1, 1, 0, 0, 1, 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
103*8975f5c5SAndroid Build Coastguard Worker 
104*8975f5c5SAndroid Build Coastguard Worker         GLubyte pixel[4];
105*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
106*8975f5c5SAndroid Build Coastguard Worker         glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
107*8975f5c5SAndroid Build Coastguard Worker 
108*8975f5c5SAndroid Build Coastguard Worker         EXPECT_NEAR(std::max(samples, 1) * 4, pixel[0], 1.0) << "Samples: " << sampleCount;
109*8975f5c5SAndroid Build Coastguard Worker     }
110*8975f5c5SAndroid Build Coastguard Worker }
111*8975f5c5SAndroid Build Coastguard Worker 
112*8975f5c5SAndroid Build Coastguard Worker // Test gl_SampleID values
TEST_P(SampleVariablesTest,SampleID)113*8975f5c5SAndroid Build Coastguard Worker TEST_P(SampleVariablesTest, SampleID)
114*8975f5c5SAndroid Build Coastguard Worker {
115*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_sample_variables"));
116*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_color_buffer_half_float"));
117*8975f5c5SAndroid Build Coastguard Worker 
118*8975f5c5SAndroid Build Coastguard Worker     const char kFS[] = R"(#version 300 es
119*8975f5c5SAndroid Build Coastguard Worker #extension GL_OES_sample_variables : require
120*8975f5c5SAndroid Build Coastguard Worker precision highp float;
121*8975f5c5SAndroid Build Coastguard Worker out vec4 color;
122*8975f5c5SAndroid Build Coastguard Worker uniform int id;
123*8975f5c5SAndroid Build Coastguard Worker void main()
124*8975f5c5SAndroid Build Coastguard Worker {
125*8975f5c5SAndroid Build Coastguard Worker     // 1.0 when the selected sample is processed, 0.0 otherwise
126*8975f5c5SAndroid Build Coastguard Worker     float r = float(gl_SampleID == id);
127*8975f5c5SAndroid Build Coastguard Worker     // Must always be 0.0
128*8975f5c5SAndroid Build Coastguard Worker     float g = float(gl_SampleID < 0 || gl_SampleID >= gl_NumSamples);
129*8975f5c5SAndroid Build Coastguard Worker 
130*8975f5c5SAndroid Build Coastguard Worker     color = vec4(r, g, 0.0, 1.0);
131*8975f5c5SAndroid Build Coastguard Worker })";
132*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
133*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
134*8975f5c5SAndroid Build Coastguard Worker 
135*8975f5c5SAndroid Build Coastguard Worker     GLint numSampleCounts = 0;
136*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA16F, GL_NUM_SAMPLE_COUNTS, 1, &numSampleCounts);
137*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GT(numSampleCounts, 0);
138*8975f5c5SAndroid Build Coastguard Worker 
139*8975f5c5SAndroid Build Coastguard Worker     std::vector<GLint> sampleCounts;
140*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.resize(numSampleCounts);
141*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA16F, GL_SAMPLES, numSampleCounts,
142*8975f5c5SAndroid Build Coastguard Worker                           sampleCounts.data());
143*8975f5c5SAndroid Build Coastguard Worker 
144*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.push_back(0);
145*8975f5c5SAndroid Build Coastguard Worker 
146*8975f5c5SAndroid Build Coastguard Worker     GLFramebuffer fboResolve;
147*8975f5c5SAndroid Build Coastguard Worker     glBindFramebuffer(GL_FRAMEBUFFER, fboResolve);
148*8975f5c5SAndroid Build Coastguard Worker 
149*8975f5c5SAndroid Build Coastguard Worker     GLRenderbuffer rboResolve;
150*8975f5c5SAndroid Build Coastguard Worker     glBindRenderbuffer(GL_RENDERBUFFER, rboResolve);
151*8975f5c5SAndroid Build Coastguard Worker     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA16F, 1, 1);
152*8975f5c5SAndroid Build Coastguard Worker     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rboResolve);
153*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
154*8975f5c5SAndroid Build Coastguard Worker 
155*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GL_NO_ERROR();
156*8975f5c5SAndroid Build Coastguard Worker 
157*8975f5c5SAndroid Build Coastguard Worker     for (GLint sampleCount : sampleCounts)
158*8975f5c5SAndroid Build Coastguard Worker     {
159*8975f5c5SAndroid Build Coastguard Worker         GLFramebuffer fbo;
160*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
161*8975f5c5SAndroid Build Coastguard Worker 
162*8975f5c5SAndroid Build Coastguard Worker         GLRenderbuffer rbo;
163*8975f5c5SAndroid Build Coastguard Worker         glBindRenderbuffer(GL_RENDERBUFFER, rbo);
164*8975f5c5SAndroid Build Coastguard Worker         glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_RGBA16F, 1, 1);
165*8975f5c5SAndroid Build Coastguard Worker         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
166*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
167*8975f5c5SAndroid Build Coastguard Worker 
168*8975f5c5SAndroid Build Coastguard Worker         for (int sample = 0; sample < std::max(sampleCount, 1); sample++)
169*8975f5c5SAndroid Build Coastguard Worker         {
170*8975f5c5SAndroid Build Coastguard Worker             glUniform1i(glGetUniformLocation(program, "id"), sample);
171*8975f5c5SAndroid Build Coastguard Worker 
172*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
173*8975f5c5SAndroid Build Coastguard Worker             drawQuad(program, essl3_shaders::PositionAttrib(), 0.0);
174*8975f5c5SAndroid Build Coastguard Worker             ASSERT_GL_NO_ERROR();
175*8975f5c5SAndroid Build Coastguard Worker 
176*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
177*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboResolve);
178*8975f5c5SAndroid Build Coastguard Worker             glBlitFramebuffer(0, 0, 1, 1, 0, 0, 1, 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
179*8975f5c5SAndroid Build Coastguard Worker 
180*8975f5c5SAndroid Build Coastguard Worker             GLfloat pixel[4];
181*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_READ_FRAMEBUFFER, fboResolve);
182*8975f5c5SAndroid Build Coastguard Worker             glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, pixel);
183*8975f5c5SAndroid Build Coastguard Worker             EXPECT_GL_NO_ERROR();
184*8975f5c5SAndroid Build Coastguard Worker 
185*8975f5c5SAndroid Build Coastguard Worker             // Only one sample has 1.0 value
186*8975f5c5SAndroid Build Coastguard Worker             EXPECT_NEAR(1.0 / std::max(sampleCount, 1), pixel[0], 0.001)
187*8975f5c5SAndroid Build Coastguard Worker                 << "Samples: " << sampleCount << ", SampleID: " << sample;
188*8975f5c5SAndroid Build Coastguard Worker             EXPECT_EQ(0.0, pixel[1]);
189*8975f5c5SAndroid Build Coastguard Worker         }
190*8975f5c5SAndroid Build Coastguard Worker     }
191*8975f5c5SAndroid Build Coastguard Worker }
192*8975f5c5SAndroid Build Coastguard Worker 
193*8975f5c5SAndroid Build Coastguard Worker // Test gl_SamplePosition values
TEST_P(SampleVariablesTest,SamplePosition)194*8975f5c5SAndroid Build Coastguard Worker TEST_P(SampleVariablesTest, SamplePosition)
195*8975f5c5SAndroid Build Coastguard Worker {
196*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_sample_variables"));
197*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_color_buffer_half_float"));
198*8975f5c5SAndroid Build Coastguard Worker 
199*8975f5c5SAndroid Build Coastguard Worker     const char kFS[] = R"(#version 300 es
200*8975f5c5SAndroid Build Coastguard Worker #extension GL_OES_sample_variables : require
201*8975f5c5SAndroid Build Coastguard Worker precision highp float;
202*8975f5c5SAndroid Build Coastguard Worker out vec4 color;
203*8975f5c5SAndroid Build Coastguard Worker uniform int id;
204*8975f5c5SAndroid Build Coastguard Worker void main()
205*8975f5c5SAndroid Build Coastguard Worker {
206*8975f5c5SAndroid Build Coastguard Worker     vec2 rg = (gl_SampleID == id) ? gl_SamplePosition : vec2(0.0, 0.0);
207*8975f5c5SAndroid Build Coastguard Worker 
208*8975f5c5SAndroid Build Coastguard Worker     // Must always be 0.0
209*8975f5c5SAndroid Build Coastguard Worker     float b = float(gl_SamplePosition.x < 0.0 || gl_SamplePosition.x > 1.0);
210*8975f5c5SAndroid Build Coastguard Worker     float a = float(gl_SamplePosition.y < 0.0 || gl_SamplePosition.y > 1.0);
211*8975f5c5SAndroid Build Coastguard Worker 
212*8975f5c5SAndroid Build Coastguard Worker     color = vec4(rg, b, a);
213*8975f5c5SAndroid Build Coastguard Worker })";
214*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
215*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
216*8975f5c5SAndroid Build Coastguard Worker 
217*8975f5c5SAndroid Build Coastguard Worker     GLint numSampleCounts = 0;
218*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA16F, GL_NUM_SAMPLE_COUNTS, 1, &numSampleCounts);
219*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GT(numSampleCounts, 0);
220*8975f5c5SAndroid Build Coastguard Worker 
221*8975f5c5SAndroid Build Coastguard Worker     std::vector<GLint> sampleCounts;
222*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.resize(numSampleCounts);
223*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA16F, GL_SAMPLES, numSampleCounts,
224*8975f5c5SAndroid Build Coastguard Worker                           sampleCounts.data());
225*8975f5c5SAndroid Build Coastguard Worker 
226*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.push_back(0);
227*8975f5c5SAndroid Build Coastguard Worker 
228*8975f5c5SAndroid Build Coastguard Worker     GLFramebuffer fboResolve;
229*8975f5c5SAndroid Build Coastguard Worker     glBindFramebuffer(GL_FRAMEBUFFER, fboResolve);
230*8975f5c5SAndroid Build Coastguard Worker 
231*8975f5c5SAndroid Build Coastguard Worker     GLRenderbuffer rboResolve;
232*8975f5c5SAndroid Build Coastguard Worker     glBindRenderbuffer(GL_RENDERBUFFER, rboResolve);
233*8975f5c5SAndroid Build Coastguard Worker     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA16F, 1, 1);
234*8975f5c5SAndroid Build Coastguard Worker     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rboResolve);
235*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
236*8975f5c5SAndroid Build Coastguard Worker 
237*8975f5c5SAndroid Build Coastguard Worker     for (GLint sampleCount : sampleCounts)
238*8975f5c5SAndroid Build Coastguard Worker     {
239*8975f5c5SAndroid Build Coastguard Worker         if (sampleCount > 16)
240*8975f5c5SAndroid Build Coastguard Worker         {
241*8975f5c5SAndroid Build Coastguard Worker             // Sample positions for MSAAx32 are not defined.
242*8975f5c5SAndroid Build Coastguard Worker             continue;
243*8975f5c5SAndroid Build Coastguard Worker         }
244*8975f5c5SAndroid Build Coastguard Worker         GLFramebuffer fbo;
245*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
246*8975f5c5SAndroid Build Coastguard Worker 
247*8975f5c5SAndroid Build Coastguard Worker         GLRenderbuffer rbo;
248*8975f5c5SAndroid Build Coastguard Worker         glBindRenderbuffer(GL_RENDERBUFFER, rbo);
249*8975f5c5SAndroid Build Coastguard Worker         glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_RGBA16F, 1, 1);
250*8975f5c5SAndroid Build Coastguard Worker         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
251*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
252*8975f5c5SAndroid Build Coastguard Worker 
253*8975f5c5SAndroid Build Coastguard Worker         std::unordered_set<GLfloat> positionX;
254*8975f5c5SAndroid Build Coastguard Worker         std::unordered_set<GLfloat> positionY;
255*8975f5c5SAndroid Build Coastguard Worker         for (int sample = 0; sample < std::max(sampleCount, 1); sample++)
256*8975f5c5SAndroid Build Coastguard Worker         {
257*8975f5c5SAndroid Build Coastguard Worker             glUniform1i(glGetUniformLocation(program, "id"), sample);
258*8975f5c5SAndroid Build Coastguard Worker 
259*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
260*8975f5c5SAndroid Build Coastguard Worker             drawQuad(program, essl3_shaders::PositionAttrib(), 0.0);
261*8975f5c5SAndroid Build Coastguard Worker             ASSERT_GL_NO_ERROR();
262*8975f5c5SAndroid Build Coastguard Worker 
263*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
264*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboResolve);
265*8975f5c5SAndroid Build Coastguard Worker             glBlitFramebuffer(0, 0, 1, 1, 0, 0, 1, 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
266*8975f5c5SAndroid Build Coastguard Worker 
267*8975f5c5SAndroid Build Coastguard Worker             GLfloat pixel[4];
268*8975f5c5SAndroid Build Coastguard Worker             glBindFramebuffer(GL_READ_FRAMEBUFFER, fboResolve);
269*8975f5c5SAndroid Build Coastguard Worker             glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, pixel);
270*8975f5c5SAndroid Build Coastguard Worker             EXPECT_GL_NO_ERROR();
271*8975f5c5SAndroid Build Coastguard Worker 
272*8975f5c5SAndroid Build Coastguard Worker             // Check that positions are unique for each sample
273*8975f5c5SAndroid Build Coastguard Worker             const int realSampleCount = std::max(sampleCount, 1);
274*8975f5c5SAndroid Build Coastguard Worker             EXPECT_TRUE(positionX.insert(pixel[0]).second)
275*8975f5c5SAndroid Build Coastguard Worker                 << "Samples: " << realSampleCount << " SampleID: " << sample
276*8975f5c5SAndroid Build Coastguard Worker                 << " X: " << (pixel[0] * realSampleCount);
277*8975f5c5SAndroid Build Coastguard Worker             EXPECT_TRUE(positionY.insert(pixel[1]).second)
278*8975f5c5SAndroid Build Coastguard Worker                 << "Samples: " << realSampleCount << " SampleID: " << sample
279*8975f5c5SAndroid Build Coastguard Worker                 << " Y: " << (pixel[1] * realSampleCount);
280*8975f5c5SAndroid Build Coastguard Worker 
281*8975f5c5SAndroid Build Coastguard Worker             // Check that sample positions are in the normalized range
282*8975f5c5SAndroid Build Coastguard Worker             EXPECT_EQ(0, pixel[2]);
283*8975f5c5SAndroid Build Coastguard Worker             EXPECT_EQ(0, pixel[3]);
284*8975f5c5SAndroid Build Coastguard Worker         }
285*8975f5c5SAndroid Build Coastguard Worker     }
286*8975f5c5SAndroid Build Coastguard Worker }
287*8975f5c5SAndroid Build Coastguard Worker 
288*8975f5c5SAndroid Build Coastguard Worker // Test gl_SampleMaskIn values
TEST_P(SampleVariablesTest,SampleMaskIn)289*8975f5c5SAndroid Build Coastguard Worker TEST_P(SampleVariablesTest, SampleMaskIn)
290*8975f5c5SAndroid Build Coastguard Worker {
291*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_sample_variables"));
292*8975f5c5SAndroid Build Coastguard Worker 
293*8975f5c5SAndroid Build Coastguard Worker     const char kFS[] = R"(#version 300 es
294*8975f5c5SAndroid Build Coastguard Worker #extension GL_OES_sample_variables : require
295*8975f5c5SAndroid Build Coastguard Worker precision highp float;
296*8975f5c5SAndroid Build Coastguard Worker out vec4 color;
297*8975f5c5SAndroid Build Coastguard Worker 
298*8975f5c5SAndroid Build Coastguard Worker uint popcount(uint v)
299*8975f5c5SAndroid Build Coastguard Worker {
300*8975f5c5SAndroid Build Coastguard Worker     uint c = 0u;
301*8975f5c5SAndroid Build Coastguard Worker     for (; v != 0u; v >>= 1) c += v & 1u;
302*8975f5c5SAndroid Build Coastguard Worker     return c;
303*8975f5c5SAndroid Build Coastguard Worker }
304*8975f5c5SAndroid Build Coastguard Worker 
305*8975f5c5SAndroid Build Coastguard Worker void main()
306*8975f5c5SAndroid Build Coastguard Worker {
307*8975f5c5SAndroid Build Coastguard Worker     float r = 0.0;
308*8975f5c5SAndroid Build Coastguard Worker     r = float(popcount(uint(gl_SampleMaskIn[0])));
309*8975f5c5SAndroid Build Coastguard Worker 
310*8975f5c5SAndroid Build Coastguard Worker     color = vec4(r * 4.0 / 255.0, 0, 0, 1);
311*8975f5c5SAndroid Build Coastguard Worker })";
312*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
313*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
314*8975f5c5SAndroid Build Coastguard Worker 
315*8975f5c5SAndroid Build Coastguard Worker     GLint numSampleCounts = 0;
316*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_NUM_SAMPLE_COUNTS, 1, &numSampleCounts);
317*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GT(numSampleCounts, 0);
318*8975f5c5SAndroid Build Coastguard Worker 
319*8975f5c5SAndroid Build Coastguard Worker     std::vector<GLint> sampleCounts;
320*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.resize(numSampleCounts);
321*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_SAMPLES, numSampleCounts,
322*8975f5c5SAndroid Build Coastguard Worker                           sampleCounts.data());
323*8975f5c5SAndroid Build Coastguard Worker 
324*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.push_back(0);
325*8975f5c5SAndroid Build Coastguard Worker 
326*8975f5c5SAndroid Build Coastguard Worker     auto test = [&](GLint sampleCount, bool sampleCoverageEnabled, GLfloat coverage) {
327*8975f5c5SAndroid Build Coastguard Worker         if (sampleCoverageEnabled)
328*8975f5c5SAndroid Build Coastguard Worker         {
329*8975f5c5SAndroid Build Coastguard Worker             glEnable(GL_SAMPLE_COVERAGE);
330*8975f5c5SAndroid Build Coastguard Worker         }
331*8975f5c5SAndroid Build Coastguard Worker         else
332*8975f5c5SAndroid Build Coastguard Worker         {
333*8975f5c5SAndroid Build Coastguard Worker             glDisable(GL_SAMPLE_COVERAGE);
334*8975f5c5SAndroid Build Coastguard Worker         }
335*8975f5c5SAndroid Build Coastguard Worker 
336*8975f5c5SAndroid Build Coastguard Worker         glSampleCoverage(coverage, false);
337*8975f5c5SAndroid Build Coastguard Worker 
338*8975f5c5SAndroid Build Coastguard Worker         GLFramebuffer fbo;
339*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
340*8975f5c5SAndroid Build Coastguard Worker 
341*8975f5c5SAndroid Build Coastguard Worker         GLRenderbuffer rbo;
342*8975f5c5SAndroid Build Coastguard Worker         glBindRenderbuffer(GL_RENDERBUFFER, rbo);
343*8975f5c5SAndroid Build Coastguard Worker         glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_RGBA8, 1, 1);
344*8975f5c5SAndroid Build Coastguard Worker         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
345*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
346*8975f5c5SAndroid Build Coastguard Worker 
347*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
348*8975f5c5SAndroid Build Coastguard Worker         glClear(GL_COLOR_BUFFER_BIT);
349*8975f5c5SAndroid Build Coastguard Worker         drawQuad(program, essl3_shaders::PositionAttrib(), 0.0);
350*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_NO_ERROR();
351*8975f5c5SAndroid Build Coastguard Worker 
352*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
353*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
354*8975f5c5SAndroid Build Coastguard Worker         glBlitFramebuffer(0, 0, 1, 1, 0, 0, 1, 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
355*8975f5c5SAndroid Build Coastguard Worker 
356*8975f5c5SAndroid Build Coastguard Worker         GLubyte pixel[4];
357*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
358*8975f5c5SAndroid Build Coastguard Worker         glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
359*8975f5c5SAndroid Build Coastguard Worker         EXPECT_GL_NO_ERROR();
360*8975f5c5SAndroid Build Coastguard Worker 
361*8975f5c5SAndroid Build Coastguard Worker         // Shader scales up the number of input samples to increase precision in unorm8 space.
362*8975f5c5SAndroid Build Coastguard Worker         int expected = std::max(sampleCount, 1) * 4;
363*8975f5c5SAndroid Build Coastguard Worker 
364*8975f5c5SAndroid Build Coastguard Worker         // Sample coverage must not affect single sampled buffers
365*8975f5c5SAndroid Build Coastguard Worker         if (sampleCoverageEnabled && sampleCount > 0)
366*8975f5c5SAndroid Build Coastguard Worker         {
367*8975f5c5SAndroid Build Coastguard Worker             // The number of samples in gl_SampleMaskIn must be affected by the sample
368*8975f5c5SAndroid Build Coastguard Worker             // coverage GL state and then the resolved value must be scaled down again.
369*8975f5c5SAndroid Build Coastguard Worker             expected *= coverage * coverage;
370*8975f5c5SAndroid Build Coastguard Worker         }
371*8975f5c5SAndroid Build Coastguard Worker         EXPECT_NEAR(expected, pixel[0], 1.0)
372*8975f5c5SAndroid Build Coastguard Worker             << "Samples: " << sampleCount
373*8975f5c5SAndroid Build Coastguard Worker             << ", Sample Coverage: " << (sampleCoverageEnabled ? "Enabled" : "Disabled")
374*8975f5c5SAndroid Build Coastguard Worker             << ", Coverage: " << coverage;
375*8975f5c5SAndroid Build Coastguard Worker     };
376*8975f5c5SAndroid Build Coastguard Worker 
377*8975f5c5SAndroid Build Coastguard Worker     for (GLint sampleCount : sampleCounts)
378*8975f5c5SAndroid Build Coastguard Worker     {
379*8975f5c5SAndroid Build Coastguard Worker         if (sampleCount > 32)
380*8975f5c5SAndroid Build Coastguard Worker         {
381*8975f5c5SAndroid Build Coastguard Worker             // The test shader will not work with MSAAx64.
382*8975f5c5SAndroid Build Coastguard Worker             continue;
383*8975f5c5SAndroid Build Coastguard Worker         }
384*8975f5c5SAndroid Build Coastguard Worker 
385*8975f5c5SAndroid Build Coastguard Worker         for (bool sampleCoverageEnabled : {false, true})
386*8975f5c5SAndroid Build Coastguard Worker         {
387*8975f5c5SAndroid Build Coastguard Worker             for (GLfloat coverage : {0.0, 0.5, 1.0})
388*8975f5c5SAndroid Build Coastguard Worker             {
389*8975f5c5SAndroid Build Coastguard Worker                 if (sampleCount == 1 && coverage != 0.0 && coverage != 1.0)
390*8975f5c5SAndroid Build Coastguard Worker                 {
391*8975f5c5SAndroid Build Coastguard Worker                     continue;
392*8975f5c5SAndroid Build Coastguard Worker                 }
393*8975f5c5SAndroid Build Coastguard Worker                 test(sampleCount, sampleCoverageEnabled, coverage);
394*8975f5c5SAndroid Build Coastguard Worker             }
395*8975f5c5SAndroid Build Coastguard Worker         }
396*8975f5c5SAndroid Build Coastguard Worker     }
397*8975f5c5SAndroid Build Coastguard Worker }
398*8975f5c5SAndroid Build Coastguard Worker 
399*8975f5c5SAndroid Build Coastguard Worker // Test gl_SampleMaskIn values with per-sample shading
TEST_P(SampleVariablesTest,SampleMaskInPerSample)400*8975f5c5SAndroid Build Coastguard Worker TEST_P(SampleVariablesTest, SampleMaskInPerSample)
401*8975f5c5SAndroid Build Coastguard Worker {
402*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_sample_variables"));
403*8975f5c5SAndroid Build Coastguard Worker 
404*8975f5c5SAndroid Build Coastguard Worker     const char kFS[] = R"(#version 300 es
405*8975f5c5SAndroid Build Coastguard Worker #extension GL_OES_sample_variables : require
406*8975f5c5SAndroid Build Coastguard Worker precision highp float;
407*8975f5c5SAndroid Build Coastguard Worker out vec4 color;
408*8975f5c5SAndroid Build Coastguard Worker 
409*8975f5c5SAndroid Build Coastguard Worker void main()
410*8975f5c5SAndroid Build Coastguard Worker {
411*8975f5c5SAndroid Build Coastguard Worker     float r = float(gl_SampleMaskIn[0] == (1 << gl_SampleID));
412*8975f5c5SAndroid Build Coastguard Worker     color = vec4(r, 0, 0, 1);
413*8975f5c5SAndroid Build Coastguard Worker })";
414*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
415*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
416*8975f5c5SAndroid Build Coastguard Worker 
417*8975f5c5SAndroid Build Coastguard Worker     GLint numSampleCounts = 0;
418*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_NUM_SAMPLE_COUNTS, 1, &numSampleCounts);
419*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GT(numSampleCounts, 0);
420*8975f5c5SAndroid Build Coastguard Worker 
421*8975f5c5SAndroid Build Coastguard Worker     std::vector<GLint> sampleCounts;
422*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.resize(numSampleCounts);
423*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA8, GL_SAMPLES, numSampleCounts,
424*8975f5c5SAndroid Build Coastguard Worker                           sampleCounts.data());
425*8975f5c5SAndroid Build Coastguard Worker 
426*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.push_back(0);
427*8975f5c5SAndroid Build Coastguard Worker 
428*8975f5c5SAndroid Build Coastguard Worker     for (GLint sampleCount : sampleCounts)
429*8975f5c5SAndroid Build Coastguard Worker     {
430*8975f5c5SAndroid Build Coastguard Worker         if (sampleCount > 32)
431*8975f5c5SAndroid Build Coastguard Worker         {
432*8975f5c5SAndroid Build Coastguard Worker             // The test shader will not work with MSAAx64.
433*8975f5c5SAndroid Build Coastguard Worker             continue;
434*8975f5c5SAndroid Build Coastguard Worker         }
435*8975f5c5SAndroid Build Coastguard Worker 
436*8975f5c5SAndroid Build Coastguard Worker         GLFramebuffer fbo;
437*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
438*8975f5c5SAndroid Build Coastguard Worker 
439*8975f5c5SAndroid Build Coastguard Worker         GLRenderbuffer rbo;
440*8975f5c5SAndroid Build Coastguard Worker         glBindRenderbuffer(GL_RENDERBUFFER, rbo);
441*8975f5c5SAndroid Build Coastguard Worker         glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_RGBA8, 1, 1);
442*8975f5c5SAndroid Build Coastguard Worker         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
443*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
444*8975f5c5SAndroid Build Coastguard Worker 
445*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
446*8975f5c5SAndroid Build Coastguard Worker         drawQuad(program, essl3_shaders::PositionAttrib(), 0.0);
447*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_NO_ERROR();
448*8975f5c5SAndroid Build Coastguard Worker 
449*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
450*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
451*8975f5c5SAndroid Build Coastguard Worker         glBlitFramebuffer(0, 0, 1, 1, 0, 0, 1, 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
452*8975f5c5SAndroid Build Coastguard Worker 
453*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
454*8975f5c5SAndroid Build Coastguard Worker         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red) << "Samples: " << sampleCount;
455*8975f5c5SAndroid Build Coastguard Worker     }
456*8975f5c5SAndroid Build Coastguard Worker }
457*8975f5c5SAndroid Build Coastguard Worker 
458*8975f5c5SAndroid Build Coastguard Worker // Test writing gl_SampleMask
TEST_P(SampleVariablesTest,SampleMask)459*8975f5c5SAndroid Build Coastguard Worker TEST_P(SampleVariablesTest, SampleMask)
460*8975f5c5SAndroid Build Coastguard Worker {
461*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_sample_variables"));
462*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_color_buffer_half_float"));
463*8975f5c5SAndroid Build Coastguard Worker 
464*8975f5c5SAndroid Build Coastguard Worker     const char kFS[] = R"(#version 300 es
465*8975f5c5SAndroid Build Coastguard Worker #extension GL_OES_sample_variables : require
466*8975f5c5SAndroid Build Coastguard Worker precision highp float;
467*8975f5c5SAndroid Build Coastguard Worker uniform highp int sampleMask;
468*8975f5c5SAndroid Build Coastguard Worker 
469*8975f5c5SAndroid Build Coastguard Worker out vec4 color;
470*8975f5c5SAndroid Build Coastguard Worker 
471*8975f5c5SAndroid Build Coastguard Worker void main()
472*8975f5c5SAndroid Build Coastguard Worker {
473*8975f5c5SAndroid Build Coastguard Worker     gl_SampleMask[0] = sampleMask;
474*8975f5c5SAndroid Build Coastguard Worker     color = vec4(1, 0, 0, 1);
475*8975f5c5SAndroid Build Coastguard Worker })";
476*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
477*8975f5c5SAndroid Build Coastguard Worker     glUseProgram(program);
478*8975f5c5SAndroid Build Coastguard Worker 
479*8975f5c5SAndroid Build Coastguard Worker     GLint numSampleCounts = 0;
480*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA16F, GL_NUM_SAMPLE_COUNTS, 1, &numSampleCounts);
481*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GT(numSampleCounts, 0);
482*8975f5c5SAndroid Build Coastguard Worker 
483*8975f5c5SAndroid Build Coastguard Worker     std::vector<GLint> sampleCounts;
484*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.resize(numSampleCounts);
485*8975f5c5SAndroid Build Coastguard Worker     glGetInternalformativ(GL_RENDERBUFFER, GL_RGBA16F, GL_SAMPLES, numSampleCounts,
486*8975f5c5SAndroid Build Coastguard Worker                           sampleCounts.data());
487*8975f5c5SAndroid Build Coastguard Worker 
488*8975f5c5SAndroid Build Coastguard Worker     sampleCounts.push_back(0);
489*8975f5c5SAndroid Build Coastguard Worker 
490*8975f5c5SAndroid Build Coastguard Worker     GLFramebuffer fboResolve;
491*8975f5c5SAndroid Build Coastguard Worker     glBindFramebuffer(GL_FRAMEBUFFER, fboResolve);
492*8975f5c5SAndroid Build Coastguard Worker 
493*8975f5c5SAndroid Build Coastguard Worker     GLRenderbuffer rboResolve;
494*8975f5c5SAndroid Build Coastguard Worker     glBindRenderbuffer(GL_RENDERBUFFER, rboResolve);
495*8975f5c5SAndroid Build Coastguard Worker     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA16F, 1, 1);
496*8975f5c5SAndroid Build Coastguard Worker     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rboResolve);
497*8975f5c5SAndroid Build Coastguard Worker     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
498*8975f5c5SAndroid Build Coastguard Worker 
499*8975f5c5SAndroid Build Coastguard Worker     auto test = [&](GLint sampleCount, GLint sampleMask, bool sampleCoverageEnabled,
500*8975f5c5SAndroid Build Coastguard Worker                     GLfloat coverage) {
501*8975f5c5SAndroid Build Coastguard Worker         if (sampleCoverageEnabled)
502*8975f5c5SAndroid Build Coastguard Worker         {
503*8975f5c5SAndroid Build Coastguard Worker             glEnable(GL_SAMPLE_COVERAGE);
504*8975f5c5SAndroid Build Coastguard Worker         }
505*8975f5c5SAndroid Build Coastguard Worker         else
506*8975f5c5SAndroid Build Coastguard Worker         {
507*8975f5c5SAndroid Build Coastguard Worker             glDisable(GL_SAMPLE_COVERAGE);
508*8975f5c5SAndroid Build Coastguard Worker         }
509*8975f5c5SAndroid Build Coastguard Worker 
510*8975f5c5SAndroid Build Coastguard Worker         ASSERT(coverage == 0.0 || coverage == 1.0);
511*8975f5c5SAndroid Build Coastguard Worker         glSampleCoverage(coverage, false);
512*8975f5c5SAndroid Build Coastguard Worker 
513*8975f5c5SAndroid Build Coastguard Worker         GLFramebuffer fbo;
514*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_FRAMEBUFFER, fbo);
515*8975f5c5SAndroid Build Coastguard Worker 
516*8975f5c5SAndroid Build Coastguard Worker         GLRenderbuffer rbo;
517*8975f5c5SAndroid Build Coastguard Worker         glBindRenderbuffer(GL_RENDERBUFFER, rbo);
518*8975f5c5SAndroid Build Coastguard Worker         glRenderbufferStorageMultisample(GL_RENDERBUFFER, sampleCount, GL_RGBA16F, 1, 1);
519*8975f5c5SAndroid Build Coastguard Worker         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
520*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
521*8975f5c5SAndroid Build Coastguard Worker 
522*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
523*8975f5c5SAndroid Build Coastguard Worker         glClear(GL_COLOR_BUFFER_BIT);
524*8975f5c5SAndroid Build Coastguard Worker         glUniform1i(glGetUniformLocation(program, "sampleMask"), sampleMask);
525*8975f5c5SAndroid Build Coastguard Worker         drawQuad(program, essl3_shaders::PositionAttrib(), 0.0);
526*8975f5c5SAndroid Build Coastguard Worker         ASSERT_GL_NO_ERROR();
527*8975f5c5SAndroid Build Coastguard Worker 
528*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
529*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboResolve);
530*8975f5c5SAndroid Build Coastguard Worker         glBlitFramebuffer(0, 0, 1, 1, 0, 0, 1, 1, GL_COLOR_BUFFER_BIT, GL_NEAREST);
531*8975f5c5SAndroid Build Coastguard Worker 
532*8975f5c5SAndroid Build Coastguard Worker         GLfloat pixel[4];
533*8975f5c5SAndroid Build Coastguard Worker         glBindFramebuffer(GL_READ_FRAMEBUFFER, fboResolve);
534*8975f5c5SAndroid Build Coastguard Worker         glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, pixel);
535*8975f5c5SAndroid Build Coastguard Worker         EXPECT_GL_NO_ERROR();
536*8975f5c5SAndroid Build Coastguard Worker 
537*8975f5c5SAndroid Build Coastguard Worker         float expected = 1.0;
538*8975f5c5SAndroid Build Coastguard Worker         if (sampleCount > 0)
539*8975f5c5SAndroid Build Coastguard Worker         {
540*8975f5c5SAndroid Build Coastguard Worker             if (sampleCoverageEnabled && coverage == 0.0)
541*8975f5c5SAndroid Build Coastguard Worker             {
542*8975f5c5SAndroid Build Coastguard Worker                 expected = 0.0;
543*8975f5c5SAndroid Build Coastguard Worker             }
544*8975f5c5SAndroid Build Coastguard Worker             else
545*8975f5c5SAndroid Build Coastguard Worker             {
546*8975f5c5SAndroid Build Coastguard Worker                 uint32_t fullSampleCount = std::max(sampleCount, 1);
547*8975f5c5SAndroid Build Coastguard Worker                 uint32_t realSampleMask  = sampleMask & (0xFFFFFFFFu >> (32 - fullSampleCount));
548*8975f5c5SAndroid Build Coastguard Worker                 expected = static_cast<float>(gl::BitCount(realSampleMask)) / fullSampleCount;
549*8975f5c5SAndroid Build Coastguard Worker             }
550*8975f5c5SAndroid Build Coastguard Worker         }
551*8975f5c5SAndroid Build Coastguard Worker         EXPECT_EQ(expected, pixel[0])
552*8975f5c5SAndroid Build Coastguard Worker             << "Samples: " << sampleCount << ", gl_SampleMask[0]: " << sampleMask
553*8975f5c5SAndroid Build Coastguard Worker             << ", Sample Coverage: " << (sampleCoverageEnabled ? "Enabled" : "Disabled")
554*8975f5c5SAndroid Build Coastguard Worker             << ", Coverage: " << coverage;
555*8975f5c5SAndroid Build Coastguard Worker     };
556*8975f5c5SAndroid Build Coastguard Worker 
557*8975f5c5SAndroid Build Coastguard Worker     for (GLint sampleCount : sampleCounts)
558*8975f5c5SAndroid Build Coastguard Worker     {
559*8975f5c5SAndroid Build Coastguard Worker         if (sampleCount > 32)
560*8975f5c5SAndroid Build Coastguard Worker         {
561*8975f5c5SAndroid Build Coastguard Worker             // The test shader will not work with MSAAx64.
562*8975f5c5SAndroid Build Coastguard Worker             continue;
563*8975f5c5SAndroid Build Coastguard Worker         }
564*8975f5c5SAndroid Build Coastguard Worker 
565*8975f5c5SAndroid Build Coastguard Worker         for (bool sampleCoverageEnabled : {false, true})
566*8975f5c5SAndroid Build Coastguard Worker         {
567*8975f5c5SAndroid Build Coastguard Worker             for (GLfloat coverage : {0.0, 1.0})
568*8975f5c5SAndroid Build Coastguard Worker             {
569*8975f5c5SAndroid Build Coastguard Worker                 for (GLint sampleMask : {0xFFFFFFFFu, 0x55555555u, 0xAAAAAAAAu, 0x00000000u})
570*8975f5c5SAndroid Build Coastguard Worker                 {
571*8975f5c5SAndroid Build Coastguard Worker                     test(sampleCount, sampleMask, sampleCoverageEnabled, coverage);
572*8975f5c5SAndroid Build Coastguard Worker                 }
573*8975f5c5SAndroid Build Coastguard Worker             }
574*8975f5c5SAndroid Build Coastguard Worker         }
575*8975f5c5SAndroid Build Coastguard Worker     }
576*8975f5c5SAndroid Build Coastguard Worker }
577*8975f5c5SAndroid Build Coastguard Worker 
578*8975f5c5SAndroid Build Coastguard Worker GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(SampleVariablesTest);
579*8975f5c5SAndroid Build Coastguard Worker ANGLE_INSTANTIATE_TEST_ES3(SampleVariablesTest);
580*8975f5c5SAndroid Build Coastguard Worker 
581*8975f5c5SAndroid Build Coastguard Worker }  // anonymous namespace
582