1 //
2 // Copyright 2016 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 // BuiltinVariableTest:
7 // Tests the correctness of the builtin GLSL variables.
8 //
9
10 #include "test_utils/ANGLETest.h"
11 #include "test_utils/gl_raii.h"
12
13 using namespace angle;
14
15 class BuiltinVariableVertexIdTest : public ANGLETest
16 {
17 protected:
BuiltinVariableVertexIdTest()18 BuiltinVariableVertexIdTest()
19 {
20 setWindowWidth(64);
21 setWindowHeight(64);
22 setConfigRedBits(8);
23 setConfigGreenBits(8);
24 setConfigBlueBits(8);
25 setConfigAlphaBits(8);
26 setConfigDepthBits(24);
27 }
28
testSetUp()29 void testSetUp() override
30 {
31 constexpr char kVS[] =
32 "#version 300 es\n"
33 "precision highp float;\n"
34 "in vec4 position;\n"
35 "in int expectedID;"
36 "out vec4 color;\n"
37 "\n"
38 "void main()\n"
39 "{\n"
40 " gl_Position = position;\n"
41 " color = vec4(gl_VertexID != expectedID, gl_VertexID == expectedID, 0.0, 1.0);"
42 "}\n";
43
44 constexpr char kFS[] =
45 "#version 300 es\n"
46 "precision highp float;\n"
47 "in vec4 color;\n"
48 "out vec4 fragColor;\n"
49 "void main()\n"
50 "{\n"
51 " fragColor = color;\n"
52 "}\n";
53
54 mProgram = CompileProgram(kVS, kFS);
55 ASSERT_NE(0u, mProgram);
56
57 mPositionLocation = glGetAttribLocation(mProgram, "position");
58 ASSERT_NE(-1, mPositionLocation);
59 mExpectedIdLocation = glGetAttribLocation(mProgram, "expectedID");
60 ASSERT_NE(-1, mExpectedIdLocation);
61
62 static const float positions[] = {0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5};
63 glGenBuffers(1, &mPositionBuffer);
64 glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
65 glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
66
67 ASSERT_GL_NO_ERROR();
68 }
69
testTearDown()70 void testTearDown() override
71 {
72 glDeleteBuffers(1, &mPositionBuffer);
73 glDeleteBuffers(1, &mExpectedIdBuffer);
74 glDeleteBuffers(1, &mIndexBuffer);
75 glDeleteProgram(mProgram);
76 }
77
78 // Renders a primitive using the specified mode, each vertex color will
79 // be green if gl_VertexID is correct, red otherwise.
runTest(GLuint drawMode,const std::vector<GLint> & indices,int count)80 void runTest(GLuint drawMode, const std::vector<GLint> &indices, int count)
81 {
82 glClearColor(0.0, 0.0, 0.0, 1.0);
83 glClear(GL_COLOR_BUFFER_BIT);
84
85 glGenBuffers(1, &mIndexBuffer);
86 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
87 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * indices.size(), indices.data(),
88 GL_STATIC_DRAW);
89
90 std::vector<GLint> expectedIds = makeRange(count);
91
92 glGenBuffers(1, &mExpectedIdBuffer);
93 glBindBuffer(GL_ARRAY_BUFFER, mExpectedIdBuffer);
94 glBufferData(GL_ARRAY_BUFFER, sizeof(GLint) * expectedIds.size(), expectedIds.data(),
95 GL_STATIC_DRAW);
96
97 glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);
98 glVertexAttribPointer(mPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
99 glEnableVertexAttribArray(mPositionLocation);
100
101 glBindBuffer(GL_ARRAY_BUFFER, mExpectedIdBuffer);
102 glVertexAttribIPointer(mExpectedIdLocation, 1, GL_INT, 0, 0);
103 glEnableVertexAttribArray(mExpectedIdLocation);
104
105 glUseProgram(mProgram);
106 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
107 glDrawElements(drawMode, count, GL_UNSIGNED_INT, 0);
108
109 std::vector<GLColor> pixels(getWindowWidth() * getWindowHeight());
110 glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RGBA, GL_UNSIGNED_BYTE,
111 pixels.data());
112
113 ASSERT_GL_NO_ERROR();
114
115 const GLColor green(0, 255, 0, 255);
116 const GLColor black(0, 0, 0, 255);
117
118 for (const auto &pixel : pixels)
119 {
120 EXPECT_TRUE(pixel == green || pixel == black);
121 }
122 }
123
makeRange(int n) const124 std::vector<GLint> makeRange(int n) const
125 {
126 std::vector<GLint> result;
127 for (int i = 0; i < n; i++)
128 {
129 result.push_back(i);
130 }
131
132 return result;
133 }
134
135 GLuint mPositionBuffer = 0;
136 GLuint mExpectedIdBuffer = 0;
137 GLuint mIndexBuffer = 0;
138
139 GLuint mProgram = 0;
140 GLint mPositionLocation = -1;
141 GLint mExpectedIdLocation = -1;
142 };
143
144 // Test gl_VertexID when rendering points
TEST_P(BuiltinVariableVertexIdTest,Points)145 TEST_P(BuiltinVariableVertexIdTest, Points)
146 {
147 runTest(GL_POINTS, makeRange(4), 4);
148 }
149
150 // Test gl_VertexID when rendering line strips
TEST_P(BuiltinVariableVertexIdTest,LineStrip)151 TEST_P(BuiltinVariableVertexIdTest, LineStrip)
152 {
153 runTest(GL_LINE_STRIP, makeRange(4), 4);
154 }
155
156 // Test gl_VertexID when rendering line loops
TEST_P(BuiltinVariableVertexIdTest,LineLoop)157 TEST_P(BuiltinVariableVertexIdTest, LineLoop)
158 {
159 runTest(GL_LINE_LOOP, makeRange(4), 4);
160 }
161
162 // Test gl_VertexID when rendering lines
TEST_P(BuiltinVariableVertexIdTest,Lines)163 TEST_P(BuiltinVariableVertexIdTest, Lines)
164 {
165 runTest(GL_LINES, makeRange(4), 4);
166 }
167
168 // Test gl_VertexID when rendering triangle strips
TEST_P(BuiltinVariableVertexIdTest,TriangleStrip)169 TEST_P(BuiltinVariableVertexIdTest, TriangleStrip)
170 {
171 runTest(GL_TRIANGLE_STRIP, makeRange(4), 4);
172 }
173
174 // Test gl_VertexID when rendering triangle fans
TEST_P(BuiltinVariableVertexIdTest,TriangleFan)175 TEST_P(BuiltinVariableVertexIdTest, TriangleFan)
176 {
177 std::vector<GLint> indices;
178 indices.push_back(0);
179 indices.push_back(1);
180 indices.push_back(3);
181 indices.push_back(2);
182 runTest(GL_TRIANGLE_FAN, indices, 4);
183 }
184
185 // Test gl_VertexID when rendering triangles
TEST_P(BuiltinVariableVertexIdTest,Triangles)186 TEST_P(BuiltinVariableVertexIdTest, Triangles)
187 {
188 std::vector<GLint> indices;
189 indices.push_back(0);
190 indices.push_back(1);
191 indices.push_back(2);
192 indices.push_back(1);
193 indices.push_back(2);
194 indices.push_back(3);
195 runTest(GL_TRIANGLES, indices, 6);
196 }
197
198 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BuiltinVariableVertexIdTest);
199 ANGLE_INSTANTIATE_TEST_ES3(BuiltinVariableVertexIdTest);
200
201 class BuiltinVariableFragDepthClampingFloatRBOTest : public ANGLETest
202 {
203 protected:
testSetUp()204 void testSetUp() override
205 {
206 // Writes a fixed detph value and green.
207 // Section 15.2.3 of the GL 4.5 specification says that conversion is not
208 // done but clamping is so the output depth should be in [0.0, 1.0]
209 constexpr char kFS[] =
210 R"(#version 300 es
211 precision highp float;
212 layout(location = 0) out vec4 fragColor;
213 uniform float u_depth;
214 void main(){
215 gl_FragDepth = u_depth;
216 fragColor = vec4(0.0, 1.0, 0.0, 1.0);
217 })";
218
219 mProgram = CompileProgram(essl3_shaders::vs::Simple(), kFS);
220 ASSERT_NE(0u, mProgram);
221
222 mDepthLocation = glGetUniformLocation(mProgram, "u_depth");
223 ASSERT_NE(-1, mDepthLocation);
224
225 glBindTexture(GL_TEXTURE_2D, mColorTexture);
226 glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
229
230 glBindTexture(GL_TEXTURE_2D, mDepthTexture);
231 glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1, 1);
232 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
234
235 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
236 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColorTexture,
237 0);
238 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTexture,
239 0);
240
241 ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
242 ASSERT_GL_NO_ERROR();
243 }
244
testTearDown()245 void testTearDown() override { glDeleteProgram(mProgram); }
246
CheckDepthWritten(float expectedDepth,float fsDepth)247 void CheckDepthWritten(float expectedDepth, float fsDepth)
248 {
249 glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
250 glUseProgram(mProgram);
251
252 // Clear to red, the FS will write green on success
253 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
254 // Clear to the expected depth so it will be compared to the FS depth with
255 // DepthFunc(GL_EQUAL)
256 glClearDepthf(expectedDepth);
257 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
258
259 glUniform1f(mDepthLocation, fsDepth);
260 glDepthFunc(GL_EQUAL);
261 glEnable(GL_DEPTH_TEST);
262
263 drawQuad(mProgram, "a_position", 0.0f);
264 EXPECT_GL_NO_ERROR();
265
266 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
267 }
268
269 private:
270 GLuint mProgram;
271 GLint mDepthLocation;
272
273 GLTexture mColorTexture;
274 GLTexture mDepthTexture;
275 GLFramebuffer mFramebuffer;
276 };
277
278 // Test that gl_FragDepth is clamped above 0
TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest,Above0)279 TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest, Above0)
280 {
281 CheckDepthWritten(0.0f, -1.0f);
282 }
283
284 // Test that gl_FragDepth is clamped below 1
TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest,Below1)285 TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest, Below1)
286 {
287 // TODO(anglebug.com/5360): Failing on ARM-based Apple DTKs.
288 ANGLE_SKIP_TEST_IF(IsOSX() && IsARM64() && IsDesktopOpenGL());
289
290 CheckDepthWritten(1.0f, 42.0f);
291 }
292
293 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BuiltinVariableFragDepthClampingFloatRBOTest);
294 ANGLE_INSTANTIATE_TEST(BuiltinVariableFragDepthClampingFloatRBOTest,
295 ES3_D3D11(),
296 ES3_OPENGL(),
297 ES3_OPENGLES());
298