blob: 628d905f30f532bafb4523918cc87eb672cdf8a0 [file] [log] [blame]
Geoff Langc287ea62016-09-16 14:46:51 -04001//
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
7// WebGLCompatibilityTest.cpp : Tests of the GL_ANGLE_webgl_compatibility extension.
8
9#include "test_utils/ANGLETest.h"
10
11#include "test_utils/gl_raii.h"
12
13namespace angle
14{
15
16class WebGLCompatibilityTest : public ANGLETest
17{
18 protected:
19 WebGLCompatibilityTest()
20 {
21 setWindowWidth(128);
22 setWindowHeight(128);
23 setConfigRedBits(8);
24 setConfigGreenBits(8);
25 setConfigBlueBits(8);
26 setConfigAlphaBits(8);
27 setWebGLCompatibilityEnabled(true);
28 }
29
30 void SetUp() override
31 {
32 ANGLETest::SetUp();
Geoff Langc339c4e2016-11-29 10:37:36 -050033 glRequestExtensionANGLE = reinterpret_cast<PFNGLREQUESTEXTENSIONANGLEPROC>(
34 eglGetProcAddress("glRequestExtensionANGLE"));
Geoff Langc287ea62016-09-16 14:46:51 -040035 }
36
37 void TearDown() override { ANGLETest::TearDown(); }
38
Geoff Langc339c4e2016-11-29 10:37:36 -050039 PFNGLREQUESTEXTENSIONANGLEPROC glRequestExtensionANGLE = nullptr;
Geoff Langc287ea62016-09-16 14:46:51 -040040};
41
Corentin Wallezfd456442016-12-21 17:57:00 -050042class WebGL2CompatibilityTest : public WebGLCompatibilityTest
43{
44};
45
Geoff Langc287ea62016-09-16 14:46:51 -040046// Context creation would fail if EGL_ANGLE_create_context_webgl_compatibility was not available so
47// the GL extension should always be present
48TEST_P(WebGLCompatibilityTest, ExtensionStringExposed)
49{
50 EXPECT_TRUE(extensionEnabled("GL_ANGLE_webgl_compatibility"));
51}
52
53// Verify that all extension entry points are available
54TEST_P(WebGLCompatibilityTest, EntryPoints)
55{
Geoff Langc339c4e2016-11-29 10:37:36 -050056 if (extensionEnabled("GL_ANGLE_request_extension"))
Geoff Langc287ea62016-09-16 14:46:51 -040057 {
Geoff Langc339c4e2016-11-29 10:37:36 -050058 EXPECT_NE(nullptr, eglGetProcAddress("glRequestExtensionANGLE"));
Geoff Langc287ea62016-09-16 14:46:51 -040059 }
60}
61
62// WebGL 1 allows GL_DEPTH_STENCIL_ATTACHMENT as a valid binding point. Make sure it is usable,
63// even in ES2 contexts.
64TEST_P(WebGLCompatibilityTest, DepthStencilBindingPoint)
65{
66 GLRenderbuffer renderbuffer;
67 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer.get());
68 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 32, 32);
69
70 GLFramebuffer framebuffer;
71 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
72 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
73 renderbuffer.get());
74
75 EXPECT_GL_NO_ERROR();
76}
77
78// Test that attempting to enable an extension that doesn't exist generates GL_INVALID_OPERATION
79TEST_P(WebGLCompatibilityTest, EnableExtensionValidation)
80{
Geoff Langc339c4e2016-11-29 10:37:36 -050081 glRequestExtensionANGLE("invalid_extension_string");
Geoff Langc287ea62016-09-16 14:46:51 -040082 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
83}
84
85// Test enabling the GL_OES_element_index_uint extension
86TEST_P(WebGLCompatibilityTest, EnableExtensionUintIndices)
87{
88 if (getClientMajorVersion() != 2)
89 {
90 // This test only works on ES2 where uint indices are not available by default
91 return;
92 }
93
94 EXPECT_FALSE(extensionEnabled("GL_OES_element_index_uint"));
95
96 GLBuffer indexBuffer;
97 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
98
99 GLuint data[] = {0, 1, 2, 1, 3, 2};
100 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
101
102 ANGLE_GL_PROGRAM(program, "void main() { gl_Position = vec4(0, 0, 0, 1); }",
103 "void main() { gl_FragColor = vec4(0, 1, 0, 1); }")
104 glUseProgram(program.get());
105
106 glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_INT, nullptr);
107 EXPECT_GL_ERROR(GL_INVALID_ENUM);
108
Geoff Langc339c4e2016-11-29 10:37:36 -0500109 if (extensionRequestable("GL_OES_element_index_uint"))
Geoff Langc287ea62016-09-16 14:46:51 -0400110 {
Geoff Langc339c4e2016-11-29 10:37:36 -0500111 glRequestExtensionANGLE("GL_OES_element_index_uint");
Geoff Langc287ea62016-09-16 14:46:51 -0400112 EXPECT_GL_NO_ERROR();
113 EXPECT_TRUE(extensionEnabled("GL_OES_element_index_uint"));
114
115 glDrawElements(GL_TRIANGLES, 2, GL_UNSIGNED_INT, nullptr);
116 EXPECT_GL_NO_ERROR();
117 }
118}
119
Bryan Bernhart87c182e2016-11-02 11:23:22 -0700120// Verify that shaders are of a compatible spec when the extension is enabled.
121TEST_P(WebGLCompatibilityTest, ExtensionCompilerSpec)
122{
123 EXPECT_TRUE(extensionEnabled("GL_ANGLE_webgl_compatibility"));
124
125 // Use of reserved _webgl prefix should fail when the shader specification is for WebGL.
126 const std::string &vert =
127 "struct Foo {\n"
128 " int _webgl_bar;\n"
129 "};\n"
130 "void main()\n"
131 "{\n"
132 " Foo foo = Foo(1);\n"
133 "}";
134
135 // Default fragement shader.
136 const std::string &frag =
137 "void main()\n"
138 "{\n"
139 " gl_FragColor = vec4(1.0,0.0,0.0,1.0);\n"
140 "}";
141
142 GLuint program = CompileProgram(vert, frag);
143 EXPECT_EQ(0u, program);
144 glDeleteProgram(program);
145}
146
Corentin Wallez327411e2016-12-09 11:09:17 -0500147// Test that client-side array buffers are forbidden in WebGL mode
148TEST_P(WebGLCompatibilityTest, ForbidsClientSideArrayBuffer)
149{
150 const std::string &vert =
151 "attribute vec3 a_pos;\n"
152 "void main()\n"
153 "{\n"
154 " gl_Position = vec4(a_pos, 1.0);\n"
155 "}\n";
156
157 const std::string &frag =
158 "precision highp float;\n"
159 "void main()\n"
160 "{\n"
161 " gl_FragColor = vec4(1.0);\n"
162 "}\n";
163
164 ANGLE_GL_PROGRAM(program, vert, frag);
165
166 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
167 ASSERT_NE(-1, posLocation);
168 glUseProgram(program.get());
169
170 const auto &vertices = GetQuadVertices();
Corentin Wallezfd456442016-12-21 17:57:00 -0500171 glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 4, vertices.data());
Corentin Wallez327411e2016-12-09 11:09:17 -0500172 glEnableVertexAttribArray(posLocation);
173
174 ASSERT_GL_NO_ERROR();
175 glDrawArrays(GL_TRIANGLES, 0, 6);
176 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
177}
178
179// Test that client-side element array buffers are forbidden in WebGL mode
180TEST_P(WebGLCompatibilityTest, ForbidsClientSideElementBuffer)
181{
182 const std::string &vert =
183 "attribute vec3 a_pos;\n"
184 "void main()\n"
185 "{\n"
186 " gl_Position = vec4(a_pos, 1.0);\n"
187 "}\n";
188
189 const std::string &frag =
190 "precision highp float;\n"
191 "void main()\n"
192 "{\n"
193 " gl_FragColor = vec4(1.0);\n"
194 "}\n";
195
196 ANGLE_GL_PROGRAM(program, vert, frag);
197
198 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
199 ASSERT_NE(-1, posLocation);
200 glUseProgram(program.get());
201
202 const auto &vertices = GetQuadVertices();
203
204 GLBuffer vertexBuffer;
205 glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer.get());
206 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
207 GL_STATIC_DRAW);
208
209 glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
210 glEnableVertexAttribArray(posLocation);
211
212 const GLubyte indices[] = {0, 1, 2, 3, 4, 5};
213
214 ASSERT_GL_NO_ERROR();
215 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
216 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
217}
218
Corentin Wallezb1d0a2552016-12-19 16:15:54 -0500219// Tests the WebGL requirement of having the same stencil mask, writemask and ref for fron and back
220TEST_P(WebGLCompatibilityTest, RequiresSameStencilMaskAndRef)
221{
222 // Run the test in an FBO to make sure we have some stencil bits.
223 GLRenderbuffer renderbuffer;
224 glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer.get());
225 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 32, 32);
226
227 GLFramebuffer framebuffer;
228 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer.get());
229 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
230 renderbuffer.get());
231
232 ANGLE_GL_PROGRAM(program, "void main() { gl_Position = vec4(0, 0, 0, 1); }",
233 "void main() { gl_FragColor = vec4(0, 1, 0, 1); }")
234 glUseProgram(program.get());
235 ASSERT_GL_NO_ERROR();
236
237 // Having ref and mask the same for front and back is valid.
238 glStencilMask(255);
239 glStencilFunc(GL_ALWAYS, 0, 255);
240 glDrawArrays(GL_TRIANGLES, 0, 6);
241 ASSERT_GL_NO_ERROR();
242
243 // Having a different front - back write mask generates an error.
244 glStencilMaskSeparate(GL_FRONT, 1);
245 glDrawArrays(GL_TRIANGLES, 0, 6);
246 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
247
248 // Setting both write masks separately to the same value is valid.
249 glStencilMaskSeparate(GL_BACK, 1);
250 glDrawArrays(GL_TRIANGLES, 0, 6);
251 ASSERT_GL_NO_ERROR();
252
253 // Having a different stencil front - back mask generates an error
254 glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 0, 1);
255 glDrawArrays(GL_TRIANGLES, 0, 6);
256 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
257
258 // Setting both masks separately to the same value is valid.
259 glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 0, 1);
260 glDrawArrays(GL_TRIANGLES, 0, 6);
261 ASSERT_GL_NO_ERROR();
262
263 // Having a different stencil front - back reference generates an error
264 glStencilFuncSeparate(GL_FRONT, GL_ALWAYS, 255, 1);
265 glDrawArrays(GL_TRIANGLES, 0, 6);
266 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
267
268 // Setting both references separately to the same value is valid.
269 glStencilFuncSeparate(GL_BACK, GL_ALWAYS, 255, 1);
270 glDrawArrays(GL_TRIANGLES, 0, 6);
271 ASSERT_GL_NO_ERROR();
272
273 // Using different stencil funcs, everything being equal is valid.
274 glStencilFuncSeparate(GL_BACK, GL_NEVER, 255, 1);
275 glDrawArrays(GL_TRIANGLES, 0, 6);
276 ASSERT_GL_NO_ERROR();
277}
278
Corentin Wallez506fc9c2016-12-21 16:53:33 -0500279// Test that GL_FIXED is forbidden
280TEST_P(WebGLCompatibilityTest, ForbidsGLFixed)
281{
282 GLBuffer buffer;
283 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
284 glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
285
286 glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
287 ASSERT_GL_NO_ERROR();
288
289 glVertexAttribPointer(0, 1, GL_FIXED, GL_FALSE, 0, nullptr);
290 EXPECT_GL_ERROR(GL_INVALID_ENUM);
291}
292
293// Test the WebGL limit of 255 for the attribute stride
294TEST_P(WebGLCompatibilityTest, MaxStride)
295{
296 GLBuffer buffer;
297 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
298 glBufferData(GL_ARRAY_BUFFER, 1024, nullptr, GL_STATIC_DRAW);
299
300 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 255, nullptr);
301 ASSERT_GL_NO_ERROR();
302
303 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 256, nullptr);
304 EXPECT_GL_ERROR(GL_INVALID_VALUE);
305}
306
Corentin Wallezfd456442016-12-21 17:57:00 -0500307// Test the checks for OOB reads in the vertex buffers, non-instanced version
308TEST_P(WebGLCompatibilityTest, DrawArraysBufferOutOfBoundsNonInstanced)
309{
310 const std::string &vert =
311 "attribute float a_pos;\n"
312 "void main()\n"
313 "{\n"
314 " gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
315 "}\n";
316
317 const std::string &frag =
318 "precision highp float;\n"
319 "void main()\n"
320 "{\n"
321 " gl_FragColor = vec4(1.0);\n"
322 "}\n";
323
324 ANGLE_GL_PROGRAM(program, vert, frag);
325
326 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
327 ASSERT_NE(-1, posLocation);
328 glUseProgram(program.get());
329
330 GLBuffer buffer;
331 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
332 glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
333
334 glEnableVertexAttribArray(posLocation);
335
336 const uint8_t* zeroOffset = nullptr;
337
338 // Test touching the last element is valid.
339 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 12);
340 glDrawArrays(GL_POINTS, 0, 4);
341 ASSERT_GL_NO_ERROR();
342
343 // Test touching the last element + 1 is invalid.
344 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 13);
345 glDrawArrays(GL_POINTS, 0, 4);
346 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
347
348 // Test touching the last element is valid, using a stride.
349 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 9);
350 glDrawArrays(GL_POINTS, 0, 4);
351 ASSERT_GL_NO_ERROR();
352
353 // Test touching the last element + 1 is invalid, using a stride.
354 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 10);
355 glDrawArrays(GL_POINTS, 0, 4);
356 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
357
358 // Test any offset is valid if no vertices are drawn.
359 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 32);
360 glDrawArrays(GL_POINTS, 0, 0);
361 ASSERT_GL_NO_ERROR();
362}
363
Geoff Lang5f319a42017-01-09 16:49:19 -0500364// Tests that NPOT is not enabled by default in WebGL 1 and that it can be enabled
365TEST_P(WebGLCompatibilityTest, NPOT)
366{
367 EXPECT_FALSE(extensionEnabled("GL_OES_texture_npot"));
368
369 // Create a texture and set an NPOT mip 0, should always be acceptable.
370 GLTexture texture;
371 glBindTexture(GL_TEXTURE_2D, texture.get());
372 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 10, 10, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
373 ASSERT_GL_NO_ERROR();
374
375 // Try setting an NPOT mip 1 and verify the error if WebGL 1
376 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 5, 5, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
377 if (getClientMajorVersion() < 3)
378 {
379 ASSERT_GL_ERROR(GL_INVALID_VALUE);
380 }
381 else
382 {
383 ASSERT_GL_NO_ERROR();
384 }
385
386 if (extensionRequestable("GL_OES_texture_npot"))
387 {
388 glRequestExtensionANGLE("GL_OES_texture_npot");
389 ASSERT_GL_NO_ERROR();
390
391 // Try again to set NPOT mip 1
392 glTexImage2D(GL_TEXTURE_2D, 1, GL_RGBA, 5, 5, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
393 ASSERT_GL_NO_ERROR();
394 }
395}
396
Corentin Wallezfd456442016-12-21 17:57:00 -0500397// Test the checks for OOB reads in the vertex buffers, instanced version
398TEST_P(WebGL2CompatibilityTest, DrawArraysBufferOutOfBoundsInstanced)
399{
400 const std::string &vert =
401 "attribute float a_pos;\n"
402 "void main()\n"
403 "{\n"
404 " gl_Position = vec4(a_pos, a_pos, a_pos, 1.0);\n"
405 "}\n";
406
407 const std::string &frag =
408 "precision highp float;\n"
409 "void main()\n"
410 "{\n"
411 " gl_FragColor = vec4(1.0);\n"
412 "}\n";
413
414 ANGLE_GL_PROGRAM(program, vert, frag);
415
416 GLint posLocation = glGetAttribLocation(program.get(), "a_pos");
417 ASSERT_NE(-1, posLocation);
418 glUseProgram(program.get());
419
420 GLBuffer buffer;
421 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
422 glBufferData(GL_ARRAY_BUFFER, 16, nullptr, GL_STATIC_DRAW);
423
424 glEnableVertexAttribArray(posLocation);
425 glVertexAttribDivisor(posLocation, 1);
426
427 const uint8_t* zeroOffset = nullptr;
428
429 // Test touching the last element is valid.
430 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 12);
431 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
432 ASSERT_GL_NO_ERROR();
433
434 // Test touching the last element + 1 is invalid.
435 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 13);
436 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
437 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
438
439 // Test touching the last element is valid, using a stride.
440 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 9);
441 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
442 ASSERT_GL_NO_ERROR();
443
444 // Test touching the last element + 1 is invalid, using a stride.
445 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 2, zeroOffset + 10);
446 glDrawArraysInstanced(GL_POINTS, 0, 1, 4);
447 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
448
449 // Test any offset is valid if no vertices are drawn.
450 glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, zeroOffset + 32);
451 glDrawArraysInstanced(GL_POINTS, 0, 1, 0);
452 ASSERT_GL_NO_ERROR();
453}
454
Geoff Langc287ea62016-09-16 14:46:51 -0400455// Use this to select which configurations (e.g. which renderer, which GLES major version) these
456// tests should be run against.
457ANGLE_INSTANTIATE_TEST(WebGLCompatibilityTest,
458 ES2_D3D9(),
459 ES2_D3D11(),
460 ES3_D3D11(),
461 ES2_D3D11_FL9_3(),
462 ES2_OPENGL(),
463 ES3_OPENGL(),
464 ES2_OPENGLES(),
465 ES3_OPENGLES());
466
Corentin Wallezfd456442016-12-21 17:57:00 -0500467ANGLE_INSTANTIATE_TEST(WebGL2CompatibilityTest,
468 ES3_D3D11(),
469 ES3_OPENGL(),
470 ES3_OPENGLES());
471
Geoff Langc287ea62016-09-16 14:46:51 -0400472} // namespace