• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ANGLE_INSTANTIATE_TEST_ES3(BuiltinVariableVertexIdTest);
199 
200 class BuiltinVariableFragDepthClampingFloatRBOTest : public ANGLETest
201 {
202   protected:
testSetUp()203     void testSetUp() override
204     {
205         // Writes a fixed detph value and green.
206         // Section 15.2.3 of the GL 4.5 specification says that conversion is not
207         // done but clamping is so the output depth should be in [0.0, 1.0]
208         constexpr char kFS[] =
209             R"(#version 300 es
210             precision highp float;
211             layout(location = 0) out vec4 fragColor;
212             uniform float u_depth;
213             void main(){
214                 gl_FragDepth = u_depth;
215                 fragColor = vec4(0.0, 1.0, 0.0, 1.0);
216             })";
217 
218         mProgram = CompileProgram(essl3_shaders::vs::Simple(), kFS);
219         ASSERT_NE(0u, mProgram);
220 
221         mDepthLocation = glGetUniformLocation(mProgram, "u_depth");
222         ASSERT_NE(-1, mDepthLocation);
223 
224         glBindTexture(GL_TEXTURE_2D, mColorTexture);
225         glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);
226         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
227         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
228 
229         glBindTexture(GL_TEXTURE_2D, mDepthTexture);
230         glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1, 1);
231         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
232         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
233 
234         glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
235         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColorTexture,
236                                0);
237         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTexture,
238                                0);
239 
240         ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
241         ASSERT_GL_NO_ERROR();
242     }
243 
testTearDown()244     void testTearDown() override { glDeleteProgram(mProgram); }
245 
CheckDepthWritten(float expectedDepth,float fsDepth)246     void CheckDepthWritten(float expectedDepth, float fsDepth)
247     {
248         glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
249         glUseProgram(mProgram);
250 
251         // Clear to red, the FS will write green on success
252         glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
253         // Clear to the expected depth so it will be compared to the FS depth with
254         // DepthFunc(GL_EQUAL)
255         glClearDepthf(expectedDepth);
256         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
257 
258         glUniform1f(mDepthLocation, fsDepth);
259         glDepthFunc(GL_EQUAL);
260         glEnable(GL_DEPTH_TEST);
261 
262         drawQuad(mProgram, "a_position", 0.0f);
263         EXPECT_GL_NO_ERROR();
264 
265         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
266     }
267 
268   private:
269     GLuint mProgram;
270     GLint mDepthLocation;
271 
272     GLTexture mColorTexture;
273     GLTexture mDepthTexture;
274     GLFramebuffer mFramebuffer;
275 };
276 
277 // Test that gl_FragDepth is clamped above 0
TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest,Above0)278 TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest, Above0)
279 {
280     CheckDepthWritten(0.0f, -1.0f);
281 }
282 
283 // Test that gl_FragDepth is clamped below 1
TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest,Below1)284 TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest, Below1)
285 {
286     CheckDepthWritten(1.0f, 42.0f);
287 }
288 
289 ANGLE_INSTANTIATE_TEST(BuiltinVariableFragDepthClampingFloatRBOTest,
290                        ES3_D3D11(),
291                        ES3_OPENGL(),
292                        ES3_OPENGLES());
293