Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 1 | #include "ANGLETest.h" |
| 2 | |
| 3 | // Use this to select which configurations (e.g. which renderer, which GLES |
| 4 | // major version) these tests should be run against. |
| 5 | // |
| 6 | // Some of the pointsprite tests below were ported from Khronos WebGL |
| 7 | // conformance test suite. |
| 8 | |
| 9 | // |
| 10 | // We test on D3D11 9_3 because the existing D3D11 PointSprite implementation |
| 11 | // uses Geometry Shaders which are not supported for 9_3. |
| 12 | // D3D9 and D3D11 are also tested to ensure no regressions. |
| 13 | ANGLE_TYPED_TEST_CASE(PointSpritesTest, ES2_D3D9, ES2_D3D11, ES2_D3D11_FL9_3); |
| 14 | |
| 15 | template<typename T> |
| 16 | class PointSpritesTest : public ANGLETest |
| 17 | { |
| 18 | protected: |
| 19 | const int windowWidth = 256; |
| 20 | const int windowHeight = 256; |
| 21 | PointSpritesTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetPlatform()) |
| 22 | { |
| 23 | setWindowWidth(windowWidth); |
| 24 | setWindowHeight(windowHeight); |
| 25 | setConfigRedBits(8); |
| 26 | setConfigGreenBits(8); |
| 27 | setConfigBlueBits(8); |
| 28 | setConfigAlphaBits(8); |
| 29 | } |
| 30 | |
| 31 | virtual void SetUp() |
| 32 | { |
| 33 | ANGLETest::SetUp(); |
| 34 | } |
| 35 | |
| 36 | float s2p(float s) |
| 37 | { |
| 38 | return (s + 1.0f) * 0.5f * (GLfloat)windowWidth; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | // Checks gl_PointCoord and gl_PointSize |
| 43 | // https://www.khronos.org/registry/webgl/sdk/tests/conformance/glsl/variables/gl-pointcoord.html |
| 44 | TYPED_TEST(PointSpritesTest, PointCoordAndPointSizeCompliance) |
| 45 | { |
| 46 | const std::string fs = SHADER_SOURCE |
| 47 | ( |
| 48 | precision mediump float; |
| 49 | void main() |
| 50 | { |
| 51 | gl_FragColor = vec4( |
| 52 | gl_PointCoord.x, |
| 53 | gl_PointCoord.y, |
| 54 | 0, |
| 55 | 1); |
| 56 | } |
| 57 | ); |
| 58 | |
| 59 | const std::string vs = SHADER_SOURCE |
| 60 | ( |
| 61 | attribute vec4 vPosition; |
| 62 | uniform float uPointSize; |
| 63 | void main() |
| 64 | { |
| 65 | gl_PointSize = uPointSize; |
| 66 | gl_Position = vPosition; |
| 67 | } |
| 68 | ); |
| 69 | |
| 70 | GLuint program = CompileProgram(vs, fs); |
| 71 | ASSERT_NE(program, 0u); |
| 72 | ASSERT_GL_NO_ERROR(); |
| 73 | |
| 74 | glUseProgram(program); |
| 75 | |
| 76 | GLfloat pointSizeRange[2] = {}; |
| 77 | glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange); |
| 78 | |
| 79 | GLfloat maxPointSize = pointSizeRange[1]; |
| 80 | |
| 81 | ASSERT_TRUE(maxPointSize >= 1); |
| 82 | maxPointSize = floorf(maxPointSize); |
| 83 | ASSERT_TRUE((int)maxPointSize % 1 == 0); |
| 84 | |
| 85 | maxPointSize = std::min(maxPointSize, 64.0f); |
| 86 | GLfloat pointWidth = maxPointSize / windowWidth; |
| 87 | GLfloat step = floorf(maxPointSize / 4); |
| 88 | GLfloat pointStep = std::max<GLfloat>(1.0f, step); |
| 89 | |
| 90 | GLint pointSizeLoc = glGetUniformLocation(program, "uPointSize"); |
| 91 | ASSERT_GL_NO_ERROR(); |
| 92 | |
| 93 | glUniform1f(pointSizeLoc, maxPointSize); |
| 94 | ASSERT_GL_NO_ERROR(); |
| 95 | |
| 96 | GLfloat pixelOffset = ((int)maxPointSize % 2) ? (1.0f / (GLfloat)windowWidth) : 0; |
| 97 | GLuint vertexObject = 0; |
| 98 | glGenBuffers(1, &vertexObject); |
| 99 | ASSERT_NE(vertexObject, 0U); |
| 100 | ASSERT_GL_NO_ERROR(); |
| 101 | |
| 102 | glBindBuffer(GL_ARRAY_BUFFER, vertexObject); |
| 103 | ASSERT_GL_NO_ERROR(); |
| 104 | |
| 105 | GLfloat thePoints[] = { -0.5 + pixelOffset, -0.5 + pixelOffset, |
| 106 | 0.5 + pixelOffset, -0.5 + pixelOffset, |
| 107 | -0.5 + pixelOffset, 0.5 + pixelOffset, |
| 108 | 0.5 + pixelOffset, 0.5 + pixelOffset }; |
| 109 | |
| 110 | glBufferData(GL_ARRAY_BUFFER, sizeof(thePoints), thePoints, GL_STATIC_DRAW); |
| 111 | ASSERT_GL_NO_ERROR(); |
| 112 | |
| 113 | glEnableVertexAttribArray(0); |
| 114 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 115 | |
| 116 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 117 | |
| 118 | glDrawArrays(GL_POINTS, 0, 4); |
| 119 | ASSERT_GL_NO_ERROR(); |
| 120 | |
| 121 | glDeleteBuffers(1, &vertexObject); |
| 122 | |
| 123 | std::string debugText; |
| 124 | for (float py = 0; py < 2; ++py) { |
| 125 | for (float px = 0; px < 2; ++px) { |
| 126 | float pointX = -0.5 + px + pixelOffset; |
| 127 | float pointY = -0.5 + py + pixelOffset; |
| 128 | for (int yy = 0; yy < maxPointSize; yy += pointStep) { |
| 129 | for (int xx = 0; xx < maxPointSize; xx += pointStep) { |
| 130 | // formula for s and t from OpenGL ES 2.0 spec section 3.3 |
| 131 | float xw = s2p(pointX); |
| 132 | float yw = s2p(pointY); |
| 133 | float u = xx / maxPointSize * 2 - 1; |
| 134 | float v = yy / maxPointSize * 2 - 1; |
| 135 | float xf = floorf(s2p(pointX + u * pointWidth)); |
| 136 | float yf = floorf(s2p(pointY + v * pointWidth)); |
| 137 | float s = 0.5 + (xf + 0.5 - xw) / maxPointSize; |
| 138 | float t = 0.5 + (yf + 0.5 - yw) / maxPointSize; |
| 139 | GLubyte color[4] = { floorf(s * 255), floorf((1 - t) * 255), 0, 255 }; |
| 140 | EXPECT_PIXEL_NEAR(xf, yf, color[0], color[1], color[2], color[3], 4); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Verify that drawing a point without enabling any attributes succeeds |
| 148 | // https://www.khronos.org/registry/webgl/sdk/tests/conformance/rendering/point-no-attributes.html |
| 149 | TYPED_TEST(PointSpritesTest, PointWithoutAttributesCompliance) |
| 150 | { |
| 151 | const std::string fs = SHADER_SOURCE |
| 152 | ( |
| 153 | precision mediump float; |
| 154 | void main() |
| 155 | { |
| 156 | gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); |
| 157 | } |
| 158 | ); |
| 159 | |
| 160 | const std::string vs = SHADER_SOURCE |
| 161 | ( |
| 162 | void main() |
| 163 | { |
| 164 | gl_PointSize = 1.0; |
| 165 | gl_Position = vec4(0.0, 0.0, 0.0, 1.0); |
| 166 | } |
| 167 | ); |
| 168 | |
| 169 | GLuint program = CompileProgram(vs, fs); |
| 170 | ASSERT_NE(program, 0u); |
| 171 | ASSERT_GL_NO_ERROR(); |
| 172 | |
| 173 | glUseProgram(program); |
| 174 | |
| 175 | glDrawArrays(GL_POINTS, 0, 1); |
| 176 | ASSERT_GL_NO_ERROR(); |
| 177 | |
| 178 | // expect the center pixel to be green |
| 179 | EXPECT_PIXEL_EQ((windowWidth - 1) / 2, (windowHeight - 1) / 2, 0, 255, 0, 255); |
| 180 | } |
| 181 | |
| 182 | // This is a regression test for a graphics driver bug affecting end caps on roads in MapsGL |
| 183 | // https://www.khronos.org/registry/webgl/sdk/tests/conformance/rendering/point-with-gl-pointcoord-in-fragment-shader.html |
| 184 | TYPED_TEST(PointSpritesTest, PointCoordRegressionTest) |
| 185 | { |
| 186 | const std::string fs = SHADER_SOURCE |
| 187 | ( |
| 188 | precision mediump float; |
| 189 | varying vec4 v_color; |
| 190 | void main() |
| 191 | { |
| 192 | // It seems as long as this mathematical expression references |
| 193 | // gl_PointCoord, the fragment's color is incorrect. |
| 194 | vec2 diff = gl_PointCoord - vec2(.5, .5); |
| 195 | if (length(diff) > 0.5) |
| 196 | discard; |
| 197 | |
| 198 | // The point should be a solid color. |
| 199 | gl_FragColor = v_color; |
| 200 | } |
| 201 | ); |
| 202 | |
| 203 | const std::string vs = SHADER_SOURCE |
| 204 | ( |
| 205 | varying vec4 v_color; |
| 206 | // The X and Y coordinates of the center of the point. |
| 207 | attribute vec2 a_vertex; |
| 208 | uniform float u_pointSize; |
| 209 | void main() |
| 210 | { |
| 211 | gl_PointSize = u_pointSize; |
| 212 | gl_Position = vec4(a_vertex, 0.0, 1.0); |
| 213 | // The color of the point. |
| 214 | v_color = vec4(0.0, 1.0, 0.0, 1.0); |
| 215 | } |
| 216 | ); |
| 217 | |
| 218 | GLuint program = CompileProgram(vs, fs); |
| 219 | ASSERT_NE(program, 0u); |
| 220 | ASSERT_GL_NO_ERROR(); |
| 221 | |
| 222 | glUseProgram(program); |
| 223 | |
| 224 | GLfloat pointSizeRange[2] = {}; |
| 225 | glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange); |
| 226 | |
| 227 | GLfloat maxPointSize = pointSizeRange[1]; |
| 228 | |
| 229 | ASSERT_TRUE(maxPointSize > 2); |
| 230 | |
| 231 | glClearColor(0, 0, 0, 1); |
| 232 | glDisable(GL_DEPTH_TEST); |
| 233 | glClear(GL_COLOR_BUFFER_BIT); |
| 234 | |
| 235 | GLint pointSizeLoc = glGetUniformLocation(program, "u_pointSize"); |
| 236 | ASSERT_GL_NO_ERROR(); |
| 237 | |
| 238 | GLfloat pointSize = std::min<GLfloat>(20.0f, maxPointSize); |
| 239 | glUniform1f(pointSizeLoc, pointSize); |
| 240 | ASSERT_GL_NO_ERROR(); |
| 241 | |
| 242 | GLuint vertexObject = 0; |
| 243 | glGenBuffers(1, &vertexObject); |
| 244 | ASSERT_NE(vertexObject, 0U); |
| 245 | ASSERT_GL_NO_ERROR(); |
| 246 | |
| 247 | glBindBuffer(GL_ARRAY_BUFFER, vertexObject); |
| 248 | ASSERT_GL_NO_ERROR(); |
| 249 | |
| 250 | GLfloat thePoints[] = { 0.0f, 0.0f }; |
| 251 | |
| 252 | glBufferData(GL_ARRAY_BUFFER, sizeof(thePoints), thePoints, GL_STATIC_DRAW); |
| 253 | ASSERT_GL_NO_ERROR(); |
| 254 | |
| 255 | glEnableVertexAttribArray(0); |
| 256 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 257 | |
| 258 | glDrawArrays(GL_POINTS, 0, 1); |
| 259 | ASSERT_GL_NO_ERROR(); |
| 260 | |
| 261 | // expect the center pixel to be green |
| 262 | EXPECT_PIXEL_EQ((windowWidth - 1) / 2, (windowHeight - 1) / 2, 0, 255, 0, 255); |
| 263 | |
| 264 | glDeleteBuffers(1, &vertexObject); |
| 265 | } |
| 266 | |
| 267 | // Verify GL_VERTEX_PROGRAM_POINT_SIZE is enabled |
| 268 | // https://www.khronos.org/registry/webgl/sdk/tests/conformance/rendering/point-size.html |
| 269 | TYPED_TEST(PointSpritesTest, PointSizeEnabledCompliance) |
| 270 | { |
| 271 | const std::string fs = SHADER_SOURCE |
| 272 | ( |
| 273 | precision mediump float; |
| 274 | varying vec4 color; |
| 275 | |
| 276 | void main() |
| 277 | { |
| 278 | gl_FragColor = color; |
| 279 | } |
| 280 | ); |
| 281 | |
| 282 | const std::string vs = SHADER_SOURCE |
| 283 | ( |
| 284 | attribute vec3 pos; |
| 285 | attribute vec4 colorIn; |
| 286 | uniform float pointSize; |
| 287 | varying vec4 color; |
| 288 | |
| 289 | void main() |
| 290 | { |
| 291 | gl_PointSize = pointSize; |
| 292 | color = colorIn; |
| 293 | gl_Position = vec4(pos, 1.0); |
| 294 | } |
| 295 | ); |
| 296 | |
| 297 | GLuint program = CompileProgram(vs, fs); |
| 298 | ASSERT_NE(program, 0u); |
| 299 | ASSERT_GL_NO_ERROR(); |
| 300 | |
| 301 | glUseProgram(program); |
| 302 | |
| 303 | glDisable(GL_BLEND); |
| 304 | |
| 305 | // The choice of (0.4, 0.4) ensures that the centers of the surrounding |
| 306 | // pixels are not contained within the point when it is of size 1, but |
| 307 | // that they definitely are when it is of size 2. |
| 308 | GLfloat vertices[] = { 0.4f, 0.4f, 0.0f }; |
| 309 | GLubyte colors[] = { 255, 0, 0, 255 }; |
| 310 | |
| 311 | GLuint vertexObject = 0; |
| 312 | glGenBuffers(1, &vertexObject); |
| 313 | ASSERT_NE(vertexObject, 0U); |
| 314 | ASSERT_GL_NO_ERROR(); |
| 315 | |
| 316 | glBindBuffer(GL_ARRAY_BUFFER, vertexObject); |
| 317 | ASSERT_GL_NO_ERROR(); |
| 318 | |
| 319 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW); |
| 320 | ASSERT_GL_NO_ERROR(); |
| 321 | |
| 322 | glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); |
| 323 | ASSERT_GL_NO_ERROR(); |
| 324 | |
| 325 | glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors); |
| 326 | ASSERT_GL_NO_ERROR(); |
| 327 | |
| 328 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 329 | |
| 330 | glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); |
| 331 | glEnableVertexAttribArray(0); |
| 332 | |
| 333 | glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (GLvoid*)sizeof(vertices)); |
| 334 | glEnableVertexAttribArray(1); |
| 335 | |
| 336 | GLint pointSizeLoc = glGetUniformLocation(program, "pointSize"); |
| 337 | ASSERT_GL_NO_ERROR(); |
| 338 | |
| 339 | glUniform1f(pointSizeLoc, 1.0f); |
| 340 | ASSERT_GL_NO_ERROR(); |
| 341 | |
| 342 | glDrawArrays(GL_POINTS, 0, _countof(vertices) / 3); |
| 343 | ASSERT_GL_NO_ERROR(); |
| 344 | |
| 345 | // Test the pixels around the target Red pixel to ensure |
| 346 | // they are the expected color values |
| 347 | for (GLint y = 178; y < 180; ++y) |
| 348 | { |
| 349 | for (GLint x = 178; x < 180; ++x) |
| 350 | { |
| 351 | // 179x179 is expected to be a red pixel |
| 352 | // All others are black |
| 353 | GLubyte expectedColor[4] = { 0, 0, 0, 0 }; |
| 354 | if (x == 179 && y == 179) |
| 355 | { |
| 356 | expectedColor[0] = 255; |
| 357 | expectedColor[3] = 255; |
| 358 | } |
| 359 | EXPECT_PIXEL_EQ(x, y, expectedColor[0], expectedColor[1], expectedColor[2], expectedColor[3]); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | swapBuffers(); |
| 364 | |
| 365 | GLfloat pointSizeRange[2] = {}; |
| 366 | glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, pointSizeRange); |
| 367 | |
| 368 | if (pointSizeRange[1] >= 2.0) |
| 369 | { |
| 370 | // Draw a point of size 2 and verify it fills the appropriate region. |
| 371 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 372 | |
| 373 | glUniform1f(pointSizeLoc, 2.0f); |
| 374 | ASSERT_GL_NO_ERROR(); |
| 375 | |
| 376 | glDrawArrays(GL_POINTS, 0, _countof(vertices) / 3); |
| 377 | ASSERT_GL_NO_ERROR(); |
| 378 | |
| 379 | // Test the pixels to ensure the target is ALL Red pixels |
| 380 | for (GLint y = 178; y < 180; ++y) |
| 381 | { |
| 382 | for (GLint x = 178; x < 180; ++x) |
| 383 | { |
| 384 | EXPECT_PIXEL_EQ(x, y, 255, 0, 0, 255); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | glDeleteBuffers(1, &vertexObject); |
| 390 | } |
Cooper Partin | 4e47b92 | 2015-02-03 09:04:20 -0800 | [diff] [blame^] | 391 | |
| 392 | // Verify that rendering works correctly when gl_PointSize is declared in a shader but isn't used |
| 393 | TYPED_TEST(PointSpritesTest, PointSizeDeclaredButUnused) |
| 394 | { |
| 395 | const std::string vs = SHADER_SOURCE |
| 396 | ( |
| 397 | attribute highp vec4 position; |
| 398 | |
| 399 | void main(void) |
| 400 | { |
| 401 | gl_PointSize = 1.0; |
| 402 | gl_Position = position; |
| 403 | } |
| 404 | ); |
| 405 | |
| 406 | const std::string fs = SHADER_SOURCE |
| 407 | ( |
| 408 | void main(void) |
| 409 | { |
| 410 | gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); |
| 411 | } |
| 412 | ); |
| 413 | |
| 414 | GLuint program = CompileProgram(vs, fs); |
| 415 | ASSERT_NE(program, 0u); |
| 416 | ASSERT_GL_NO_ERROR(); |
| 417 | |
| 418 | glUseProgram(program); |
| 419 | drawQuad(program, "position", 0.5f, 1.0f); |
| 420 | ASSERT_GL_NO_ERROR(); |
| 421 | |
| 422 | // expect the center pixel to be red |
| 423 | EXPECT_PIXEL_EQ(getWindowWidth() / 2, getWindowHeight() / 2, 255, 0, 0, 255); |
| 424 | |
| 425 | glDeleteProgram(program); |
| 426 | } |