1 //
2 // Copyright 2015 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 // Some of the pointsprite tests below were ported from Khronos WebGL
7 // conformance test suite.
8
9 #include "test_utils/ANGLETest.h"
10 #include "test_utils/gl_raii.h"
11
12 #include <cmath>
13
14 using namespace angle;
15
16 constexpr char kVertexShaderSource[] =
17 R"(attribute vec4 vPosition;
18 uniform float uPointSize;
19 void main()
20 {
21 gl_PointSize = uPointSize;
22 gl_Position = vPosition;
23 })";
24
25 constexpr GLfloat kMinMaxPointSize = 1.0f;
26
27 class PointSpritesTest : public ANGLETest<>
28 {
29 protected:
30 const int windowWidth = 256;
31 const int windowHeight = 256;
PointSpritesTest()32 PointSpritesTest()
33 {
34 setWindowWidth(windowWidth);
35 setWindowHeight(windowHeight);
36 setConfigRedBits(8);
37 setConfigGreenBits(8);
38 setConfigBlueBits(8);
39 setConfigAlphaBits(8);
40 }
41
s2p(float s)42 float s2p(float s) { return (s + 1.0f) * 0.5f * (GLfloat)windowWidth; }
43
testPointCoordAndPointSizeCompliance(GLProgram & program)44 void testPointCoordAndPointSizeCompliance(GLProgram &program)
45 {
46 glUseProgram(program);
47
48 GLfloat pointSizeRange[2] = {};
49 glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange);
50
51 GLfloat maxPointSize = pointSizeRange[1];
52
53 ASSERT_TRUE(maxPointSize >= 1);
54 maxPointSize = floorf(maxPointSize);
55 ASSERT_TRUE((int)maxPointSize % 1 == 0);
56
57 maxPointSize = std::min(maxPointSize, 64.0f);
58 GLfloat pointWidth = maxPointSize / windowWidth;
59 GLint step = static_cast<GLint>(floorf(maxPointSize / 4));
60 GLint pointStep = std::max<GLint>(1, step);
61
62 GLint pointSizeLoc = glGetUniformLocation(program, "uPointSize");
63 ASSERT_GL_NO_ERROR();
64
65 glUniform1f(pointSizeLoc, maxPointSize);
66 ASSERT_GL_NO_ERROR();
67
68 GLfloat pixelOffset = ((int)maxPointSize % 2) ? (1.0f / (GLfloat)windowWidth) : 0;
69 GLBuffer vertexObject;
70
71 glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
72 ASSERT_GL_NO_ERROR();
73
74 GLfloat thePoints[] = {-0.5f + pixelOffset, -0.5f + pixelOffset, 0.5f + pixelOffset,
75 -0.5f + pixelOffset, -0.5f + pixelOffset, 0.5f + pixelOffset,
76 0.5f + pixelOffset, 0.5f + pixelOffset};
77
78 glBufferData(GL_ARRAY_BUFFER, sizeof(thePoints), thePoints, GL_STATIC_DRAW);
79 ASSERT_GL_NO_ERROR();
80
81 glEnableVertexAttribArray(0);
82 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
83
84 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
85
86 glDrawArrays(GL_POINTS, 0, 4);
87 ASSERT_GL_NO_ERROR();
88
89 for (float py = 0; py < 2; ++py)
90 {
91 for (float px = 0; px < 2; ++px)
92 {
93 float pointX = -0.5f + px + pixelOffset;
94 float pointY = -0.5f + py + pixelOffset;
95 for (int yy = 0; yy < maxPointSize; yy += pointStep)
96 {
97 for (int xx = 0; xx < maxPointSize; xx += pointStep)
98 {
99 // formula for s and t from OpenGL ES 2.0 spec section 3.3
100 float xw = s2p(pointX);
101 float yw = s2p(pointY);
102 float u = xx / maxPointSize * 2 - 1;
103 float v = yy / maxPointSize * 2 - 1;
104 int xf = static_cast<int>(floorf(s2p(pointX + u * pointWidth)));
105 int yf = static_cast<int>(floorf(s2p(pointY + v * pointWidth)));
106 float s = 0.5f + (xf + 0.5f - xw) / maxPointSize;
107 float t = 0.5f + (yf + 0.5f - yw) / maxPointSize;
108 GLubyte color[4] = {static_cast<GLubyte>(floorf(s * 255)),
109 static_cast<GLubyte>(floorf((1 - t) * 255)), 0, 255};
110 EXPECT_PIXEL_NEAR(xf, yf, color[0], color[1], color[2], color[3], 4);
111 }
112 }
113 }
114 }
115 }
116 };
117
118 // Checks gl_PointCoord and gl_PointSize
119 // https://www.khronos.org/registry/webgl/sdk/tests/conformance/glsl/variables/gl-pointcoord.html
TEST_P(PointSpritesTest,PointCoordAndPointSizeCompliance)120 TEST_P(PointSpritesTest, PointCoordAndPointSizeCompliance)
121 {
122 constexpr char fs[] =
123 R"(precision mediump float;
124 void main()
125 {
126 gl_FragColor = vec4(gl_PointCoord.x, gl_PointCoord.y, 0, 1);
127 })";
128
129 ANGLE_GL_PROGRAM(program, kVertexShaderSource, fs);
130
131 testPointCoordAndPointSizeCompliance(program);
132 }
133
134 // Checks gl_PointCoord and gl_PointSize, but use the gl_PointCoord inside a function.
135 // In Vulkan, we need to inject some code into the shader to flip the Y coordinate, and we
136 // need to make sure this code injection works even if someone uses gl_PointCoord outside the
137 // main function.
TEST_P(PointSpritesTest,UsingPointCoordInsideFunction)138 TEST_P(PointSpritesTest, UsingPointCoordInsideFunction)
139 {
140 constexpr char fs[] =
141 R"(precision mediump float;
142 void foo()
143 {
144 gl_FragColor = vec4(gl_PointCoord.x, gl_PointCoord.y, 0, 1);
145 }
146
147 void main()
148 {
149 foo();
150 })";
151
152 ANGLE_GL_PROGRAM(program, kVertexShaderSource, fs);
153
154 testPointCoordAndPointSizeCompliance(program);
155 }
156
157 // Verify that drawing a point without enabling any attributes succeeds
158 // https://www.khronos.org/registry/webgl/sdk/tests/conformance/rendering/point-no-attributes.html
TEST_P(PointSpritesTest,PointWithoutAttributesCompliance)159 TEST_P(PointSpritesTest, PointWithoutAttributesCompliance)
160 {
161 GLfloat pointSizeRange[2] = {};
162 glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange);
163 GLfloat maxPointSize = pointSizeRange[1];
164 ANGLE_SKIP_TEST_IF(maxPointSize < kMinMaxPointSize);
165
166 constexpr char kVS[] = R"(void main()
167 {
168 gl_PointSize = 2.0;
169 gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
170 })";
171
172 ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Blue());
173 ASSERT_GL_NO_ERROR();
174
175 glUseProgram(program);
176
177 glDrawArrays(GL_POINTS, 0, 1);
178 ASSERT_GL_NO_ERROR();
179
180 // expect the center pixel to be blue
181 EXPECT_PIXEL_COLOR_EQ((windowWidth - 1) / 2, (windowHeight - 1) / 2, GLColor::blue);
182 }
183
184 // This is a regression test for a graphics driver bug affecting end caps on roads in MapsGL
185 // https://www.khronos.org/registry/webgl/sdk/tests/conformance/rendering/point-with-gl-pointcoord-in-fragment-shader.html
TEST_P(PointSpritesTest,PointCoordRegressionTest)186 TEST_P(PointSpritesTest, PointCoordRegressionTest)
187 {
188 GLfloat pointSizeRange[2] = {};
189 glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange);
190 GLfloat maxPointSize = pointSizeRange[1];
191 ANGLE_SKIP_TEST_IF(maxPointSize < kMinMaxPointSize);
192
193 constexpr char kFS[] = R"(precision mediump float;
194 varying vec4 v_color;
195 void main()
196 {
197 // It seems as long as this mathematical expression references
198 // gl_PointCoord, the fragment's color is incorrect.
199 vec2 diff = gl_PointCoord - vec2(.5, .5);
200 if (length(diff) > 0.5)
201 discard;
202
203 // The point should be a solid color.
204 gl_FragColor = v_color;
205 })";
206
207 constexpr char kVS[] = R"(varying vec4 v_color;
208 // The X and Y coordinates of the center of the point.
209 attribute vec2 a_vertex;
210 uniform float u_pointSize;
211 void main()
212 {
213 gl_PointSize = u_pointSize;
214 gl_Position = vec4(a_vertex, 0.0, 1.0);
215 // The color of the point.
216 v_color = vec4(0.0, 1.0, 0.0, 1.0);
217 })";
218
219 ANGLE_GL_PROGRAM(program, kVS, kFS);
220 ASSERT_GL_NO_ERROR();
221
222 glUseProgram(program);
223
224 glClearColor(0, 0, 0, 1);
225 glDisable(GL_DEPTH_TEST);
226 glClear(GL_COLOR_BUFFER_BIT);
227
228 GLint pointSizeLoc = glGetUniformLocation(program, "u_pointSize");
229 ASSERT_GL_NO_ERROR();
230
231 GLfloat pointSize = std::min<GLfloat>(20.0f, maxPointSize);
232 glUniform1f(pointSizeLoc, pointSize);
233 ASSERT_GL_NO_ERROR();
234
235 GLBuffer vertexObject;
236 ASSERT_GL_NO_ERROR();
237
238 glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
239 ASSERT_GL_NO_ERROR();
240
241 GLfloat thePoints[] = {0.0f, 0.0f};
242
243 glBufferData(GL_ARRAY_BUFFER, sizeof(thePoints), thePoints, GL_STATIC_DRAW);
244 ASSERT_GL_NO_ERROR();
245
246 glEnableVertexAttribArray(0);
247 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
248
249 glDrawArrays(GL_POINTS, 0, 1);
250 ASSERT_GL_NO_ERROR();
251
252 // expect the center pixel to be green
253 EXPECT_PIXEL_EQ((windowWidth - 1) / 2, (windowHeight - 1) / 2, 0, 255, 0, 255);
254 }
255
256 // Verify GL_VERTEX_PROGRAM_POINT_SIZE is enabled
257 // https://www.khronos.org/registry/webgl/sdk/tests/conformance/rendering/point-size.html
TEST_P(PointSpritesTest,PointSizeEnabledCompliance)258 TEST_P(PointSpritesTest, PointSizeEnabledCompliance)
259 {
260 constexpr char kFS[] = R"(precision mediump float;
261 varying vec4 color;
262
263 void main()
264 {
265 gl_FragColor = color;
266 })";
267
268 constexpr char kVS[] = R"(attribute vec3 pos;
269 attribute vec4 colorIn;
270 uniform float pointSize;
271 varying vec4 color;
272
273 void main()
274 {
275 gl_PointSize = pointSize;
276 color = colorIn;
277 gl_Position = vec4(pos, 1.0);
278 })";
279
280 // The WebGL test is drawn on a 2x2 canvas. Emulate this by setting a 2x2 viewport.
281 glViewport(0, 0, 2, 2);
282
283 ANGLE_GL_PROGRAM(program, kVS, kFS);
284 ASSERT_GL_NO_ERROR();
285
286 glUseProgram(program);
287
288 glDisable(GL_BLEND);
289
290 // The choice of (0.4, 0.4) ensures that the centers of the surrounding
291 // pixels are not contained within the point when it is of size 1, but
292 // that they definitely are when it is of size 2.
293 GLfloat vertices[] = {0.4f, 0.4f, 0.0f};
294 GLubyte colors[] = {255, 0, 0, 255};
295
296 GLBuffer vertexObject;
297 ASSERT_GL_NO_ERROR();
298
299 glBindBuffer(GL_ARRAY_BUFFER, vertexObject);
300 ASSERT_GL_NO_ERROR();
301
302 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), nullptr, GL_STATIC_DRAW);
303 ASSERT_GL_NO_ERROR();
304
305 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
306 ASSERT_GL_NO_ERROR();
307
308 glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
309 ASSERT_GL_NO_ERROR();
310
311 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
312
313 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
314 glEnableVertexAttribArray(0);
315
316 glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0,
317 reinterpret_cast<void *>(sizeof(vertices)));
318 glEnableVertexAttribArray(1);
319
320 GLint pointSizeLoc = glGetUniformLocation(program, "pointSize");
321 ASSERT_GL_NO_ERROR();
322
323 glUniform1f(pointSizeLoc, 1.0f);
324 ASSERT_GL_NO_ERROR();
325
326 glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(ArraySize(vertices)) / 3);
327 ASSERT_GL_NO_ERROR();
328
329 // Test the pixels around the target Red pixel to ensure
330 // they are the expected color values
331 for (GLint y = 0; y < 2; ++y)
332 {
333 for (GLint x = 0; x < 2; ++x)
334 {
335 // 1x1 is expected to be a red pixel
336 // All others are black
337 GLubyte expectedColor[4] = {0, 0, 0, 0};
338 if (x == 1 && y == 1)
339 {
340 expectedColor[0] = 255;
341 expectedColor[3] = 255;
342 }
343 EXPECT_PIXEL_EQ(x, y, expectedColor[0], expectedColor[1], expectedColor[2],
344 expectedColor[3]);
345 }
346 }
347
348 GLfloat pointSizeRange[2] = {};
349 glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange);
350
351 if (pointSizeRange[1] >= 2.0)
352 {
353 // Draw a point of size 2 and verify it fills the appropriate region.
354 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
355
356 glUniform1f(pointSizeLoc, 2.0f);
357 ASSERT_GL_NO_ERROR();
358
359 glDrawArrays(GL_POINTS, 0, static_cast<GLsizei>(ArraySize(vertices)) / 3);
360 ASSERT_GL_NO_ERROR();
361
362 // Test the pixels to ensure the target is ALL Red pixels
363 for (GLint y = 0; y < 2; ++y)
364 {
365 for (GLint x = 0; x < 2; ++x)
366 {
367 EXPECT_PIXEL_EQ(x, y, 255, 0, 0, 255);
368 }
369 }
370 }
371 }
372
373 // Verify that rendering works correctly when gl_PointSize is declared in a shader but isn't used
TEST_P(PointSpritesTest,PointSizeDeclaredButUnused)374 TEST_P(PointSpritesTest, PointSizeDeclaredButUnused)
375 {
376 constexpr char kVS[] = R"(attribute highp vec4 position;
377 void main(void)
378 {
379 gl_PointSize = 1.0;
380 gl_Position = position;
381 })";
382
383 ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red());
384 ASSERT_GL_NO_ERROR();
385
386 glUseProgram(program);
387 drawQuad(program, "position", 0.5f, 1.0f);
388 ASSERT_GL_NO_ERROR();
389
390 // expect the center pixel to be red
391 EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 255, 0, 0, 255);
392 }
393
394 // Test to cover a bug where the D3D11 rasterizer state would not be update when switching between
395 // draw types. This causes the cull face to potentially be incorrect when drawing emulated point
396 // spites.
TEST_P(PointSpritesTest,PointSpriteAlternatingDrawTypes)397 TEST_P(PointSpritesTest, PointSpriteAlternatingDrawTypes)
398 {
399 // TODO(anglebug.com/42262976): Investigate possible ARM driver bug.
400 ANGLE_SKIP_TEST_IF(IsFuchsia() && IsARM() && IsVulkan());
401
402 GLfloat pointSizeRange[2] = {};
403 glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange);
404 GLfloat maxPointSize = pointSizeRange[1];
405 ANGLE_SKIP_TEST_IF(maxPointSize < kMinMaxPointSize);
406
407 constexpr char kVS[] = R"(uniform float u_pointSize;
408 void main()
409 {
410 gl_PointSize = u_pointSize;
411 gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
412 })";
413
414 ANGLE_GL_PROGRAM(pointProgram, kVS, essl1_shaders::fs::Blue());
415
416 ANGLE_GL_PROGRAM(quadProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
417 ASSERT_GL_NO_ERROR();
418
419 glEnable(GL_CULL_FACE);
420 glCullFace(GL_FRONT);
421
422 const GLfloat quadVertices[] = {
423 -1.0f, 1.0f, 0.5f, 1.0f, -1.0f, 0.5f, -1.0f, -1.0f, 0.5f,
424
425 -1.0f, 1.0f, 0.5f, 1.0f, 1.0f, 0.5f, 1.0f, -1.0f, 0.5f,
426 };
427
428 glUseProgram(quadProgram);
429 GLint positionLocation = glGetAttribLocation(quadProgram, essl1_shaders::PositionAttrib());
430 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, quadVertices);
431 glEnableVertexAttribArray(positionLocation);
432 glDrawArrays(GL_TRIANGLES, 0, 6);
433
434 glUseProgram(pointProgram);
435 GLint pointSizeLoc = glGetUniformLocation(pointProgram, "u_pointSize");
436 ASSERT_GL_NO_ERROR();
437 GLfloat pointSize = std::min<GLfloat>(16.0f, maxPointSize);
438 glUniform1f(pointSizeLoc, pointSize);
439 ASSERT_GL_NO_ERROR();
440
441 glDrawArrays(GL_POINTS, 0, 1);
442 ASSERT_GL_NO_ERROR();
443
444 // expect the center pixel to be blue
445 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() / 2, getWindowHeight() / 2, GLColor::blue);
446 }
447
448 // This checks for an NVIDIA driver bug where points larger than the maximum reported point size can
449 // be drawn. Point size should be clamped to the point size range as specified in GLES 3.0.5 section
450 // 3.4.
TEST_P(PointSpritesTest,PointSizeAboveMaxIsClamped)451 TEST_P(PointSpritesTest, PointSizeAboveMaxIsClamped)
452 {
453 // Failed on NVIDIA GeForce GTX 1080 - no pixels from the point were detected in the
454 // framebuffer. http://anglebug.com/42260857
455 ANGLE_SKIP_TEST_IF(IsD3D9());
456
457 // Failed on AMD OSX and Windows trybots - no pixels from the point were detected in the
458 // framebuffer. http://anglebug.com/42260859
459 ANGLE_SKIP_TEST_IF(IsAMD() && IsOpenGL());
460
461 // TODO(anglebug.com/40096805)
462 ANGLE_SKIP_TEST_IF(IsMetal() && IsAMD());
463
464 GLfloat pointSizeRange[2] = {};
465 glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange);
466 GLfloat maxPointSize = pointSizeRange[1];
467
468 if (maxPointSize < 4)
469 {
470 // This test is only able to test larger points.
471 return;
472 }
473
474 // Create a renderbuffer that has height and width equal to the max point size.
475 GLRenderbuffer renderbuffer;
476 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
477 glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, static_cast<GLsizei>(maxPointSize),
478 static_cast<GLsizei>(maxPointSize));
479 // Switch the render target from the default window surface to the newly created renderbuffer so
480 // that the test can handle implementations with a very large max point size.
481 GLFramebuffer framebuffer;
482 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
483 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
484
485 glViewport(0, 0, maxPointSize, maxPointSize);
486 ASSERT_GL_NO_ERROR();
487
488 constexpr char kVS[] =
489 "attribute vec2 vPosition;\n"
490 "uniform float uPointSize;\n"
491 "void main()\n"
492 "{\n"
493 " gl_PointSize = uPointSize;\n"
494 " gl_Position = vec4(vPosition, 0, 1);\n"
495 "}\n";
496 ANGLE_GL_PROGRAM(program, kVS, essl1_shaders::fs::Red());
497 glUseProgram(program);
498 ASSERT_GL_NO_ERROR();
499
500 GLfloat testPointSize = floorf(maxPointSize * 2.0f);
501
502 GLint pointSizeLoc = glGetUniformLocation(program, "uPointSize");
503 glUniform1f(pointSizeLoc, testPointSize);
504 ASSERT_GL_NO_ERROR();
505
506 // The point will be a square centered at gl_Position. As the framebuffer is the same size as
507 // the square, setting the center of the point on the right edge of the viewport will result in
508 // the left edge of the point square to be at the center of the viewport.
509 GLfloat pointXPosition = 1;
510
511 GLBuffer vertexBuffer;
512 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
513 GLfloat thePoints[] = {pointXPosition, 0.0f};
514 glBufferData(GL_ARRAY_BUFFER, sizeof(thePoints), thePoints, GL_STATIC_DRAW);
515 ASSERT_GL_NO_ERROR();
516
517 glEnableVertexAttribArray(0);
518 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
519
520 // Clear the framebuffer to green
521 glClearColor(0, 1, 0, 1);
522 glClear(GL_COLOR_BUFFER_BIT);
523
524 // Draw the red point
525 glDrawArrays(GL_POINTS, 0, 1);
526 ASSERT_GL_NO_ERROR();
527
528 // Pixels to the right of the framebuffer center should be covered by the point.
529 EXPECT_PIXEL_NEAR(maxPointSize / 2 + 2, maxPointSize / 2, 255, 0, 0, 255, 1);
530
531 // Pixels to the left of the framebuffer center should not be covered by the point.
532 EXPECT_PIXEL_NEAR(maxPointSize / 2 - 2, maxPointSize / 2, 0, 255, 0, 255, 1);
533 }
534
535 // Use this to select which configurations (e.g. which renderer, which GLES
536 // major version) these tests should be run against.
537 //
538 // We test on D3D11 9_3 because the existing D3D11 PointSprite implementation
539 // uses Geometry Shaders which are not supported for 9_3.
540 // D3D9 and D3D11 are also tested to ensure no regressions.
541 ANGLE_INSTANTIATE_TEST_ES2_AND(PointSpritesTest,
542 ES2_VULKAN().enable(Feature::EmulatedPrerotation90),
543 ES2_VULKAN().enable(Feature::EmulatedPrerotation180),
544 ES2_VULKAN().enable(Feature::EmulatedPrerotation270));
545