Skip to content

Commit a6a0686

Browse files
committed
Remove support for WebGL 2 in Chrome 57 due to its high maintenance burden on code size. Reduces -15.9% of code size (997 bytes) in https://github.com/juj/webgl_render_test for WebGL 2. Introduce new setting -s OLDEST_SUPPORTED_CHROME_VERSION=x to enable customizing which Chrome versions to target.
1 parent dc5407e commit a6a0686

File tree

3 files changed

+53
-93
lines changed

3 files changed

+53
-93
lines changed

src/library_webgl.js

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,18 @@ var LibraryGL = {
472472
webGLContextAttributes['preserveDrawingBuffer'] = true;
473473
#endif
474474

475+
#if USE_WEBGL2 && OLDEST_SUPPORTED_CHROME_VERSION <= 57
476+
// BUG: Workaround Chrome WebGL 2 issue: the first shipped versions of WebGL 2 in Chrome did not actually implement the new WebGL 2 functions.
477+
// Those are supported only in Chrome 58 and newer. For Chrome 57 (and older), disable WebGL 2 support altogether.
478+
function getChromeVersion() {
479+
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
480+
return raw ? parseInt(raw[2], 10) : false;
481+
}
482+
if (getChromeVersion() <= 57) {
483+
WebGL2RenderingContext = undefined;
484+
}
485+
#endif
486+
475487
#if GL_DEBUG
476488
var errorInfo = '?';
477489
function onContextCreationError(event) {
@@ -814,16 +826,6 @@ var LibraryGL = {
814826
GLctx: ctx
815827
};
816828

817-
#if USE_WEBGL2
818-
// BUG: Workaround Chrome WebGL 2 issue: the first shipped versions of WebGL 2 in Chrome did not actually implement the new WebGL 2 functions.
819-
// Those are supported only in Chrome 58 and newer.
820-
function getChromeVersion() {
821-
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
822-
return raw ? parseInt(raw[2], 10) : false;
823-
}
824-
context.supportsWebGL2EntryPoints = (context.version >= 2) && (getChromeVersion() === false || getChromeVersion() >= 58);
825-
#endif
826-
827829
#if WORKAROUND_OLD_WEBGL_UNIFORM_UPLOAD_IGNORED_OFFSET_BUG
828830
context.cannotHandleOffsetsInUniformArrayViews = (function(g) {
829831
function b(c, t) {
@@ -1323,7 +1325,7 @@ var LibraryGL = {
13231325
glCompressedTexImage2D__sig: 'viiiiiiii',
13241326
glCompressedTexImage2D: function(target, level, internalFormat, width, height, border, imageSize, data) {
13251327
#if USE_WEBGL2
1326-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1328+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
13271329
if (GLctx.currentPixelUnpackBufferBinding) {
13281330
GLctx['compressedTexImage2D'](target, level, internalFormat, width, height, border, imageSize, data);
13291331
} else {
@@ -1339,7 +1341,7 @@ var LibraryGL = {
13391341
glCompressedTexSubImage2D__sig: 'viiiiiiiii',
13401342
glCompressedTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, imageSize, data) {
13411343
#if USE_WEBGL2
1342-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1344+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
13431345
if (GLctx.currentPixelUnpackBufferBinding) {
13441346
GLctx['compressedTexSubImage2D'](target, level, xoffset, yoffset, width, height, format, imageSize, data);
13451347
} else {
@@ -1497,7 +1499,7 @@ var LibraryGL = {
14971499
}
14981500
}
14991501
#endif
1500-
if (GL.currentContext.supportsWebGL2EntryPoints) {
1502+
if (GL.currentContext.version >= 2) {
15011503
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
15021504
if (GLctx.currentPixelUnpackBufferBinding) {
15031505
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
@@ -1528,7 +1530,7 @@ var LibraryGL = {
15281530
if (type == 0x8d61/*GL_HALF_FLOAT_OES*/) type = 0x140B /*GL_HALF_FLOAT*/;
15291531
}
15301532
#endif
1531-
if (GL.currentContext.supportsWebGL2EntryPoints) {
1533+
if (GL.currentContext.version >= 2) {
15321534
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
15331535
if (GLctx.currentPixelUnpackBufferBinding) {
15341536
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
@@ -1553,7 +1555,7 @@ var LibraryGL = {
15531555
],
15541556
glReadPixels: function(x, y, width, height, format, type, pixels) {
15551557
#if USE_WEBGL2
1556-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1558+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
15571559
if (GLctx.currentPixelPackBufferBinding) {
15581560
GLctx.readPixels(x, y, width, height, format, type, pixels);
15591561
} else {
@@ -1730,7 +1732,7 @@ var LibraryGL = {
17301732
}
17311733
#endif
17321734
#if USE_WEBGL2
1733-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1735+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
17341736
if (data) {
17351737
GLctx.bufferData(target, HEAPU8, usage, data, size);
17361738
} else {
@@ -1749,7 +1751,7 @@ var LibraryGL = {
17491751
glBufferSubData__sig: 'viiii',
17501752
glBufferSubData: function(target, offset, size, data) {
17511753
#if USE_WEBGL2
1752-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
1754+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
17531755
GLctx.bufferSubData(target, offset, HEAPU8, data, size);
17541756
return;
17551757
}
@@ -2166,7 +2168,7 @@ var LibraryGL = {
21662168
#endif
21672169

21682170
#if USE_WEBGL2
2169-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2171+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
21702172
GLctx.uniform1iv(GL.uniforms[location], HEAP32, value>>2, count);
21712173
return;
21722174
}
@@ -2183,7 +2185,7 @@ var LibraryGL = {
21832185
#endif
21842186

21852187
#if USE_WEBGL2
2186-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2188+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
21872189
GLctx.uniform2iv(GL.uniforms[location], HEAP32, value>>2, count*2);
21882190
return;
21892191
}
@@ -2200,7 +2202,7 @@ var LibraryGL = {
22002202
#endif
22012203

22022204
#if USE_WEBGL2
2203-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2205+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
22042206
GLctx.uniform3iv(GL.uniforms[location], HEAP32, value>>2, count*3);
22052207
return;
22062208
}
@@ -2217,7 +2219,7 @@ var LibraryGL = {
22172219
#endif
22182220

22192221
#if USE_WEBGL2
2220-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2222+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
22212223
GLctx.uniform4iv(GL.uniforms[location], HEAP32, value>>2, count*4);
22222224
return;
22232225
}
@@ -2234,7 +2236,7 @@ var LibraryGL = {
22342236
#endif
22352237

22362238
#if USE_WEBGL2
2237-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2239+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
22382240
GLctx.uniform1fv(GL.uniforms[location], HEAPF32, value>>2, count);
22392241
return;
22402242
}
@@ -2266,7 +2268,7 @@ var LibraryGL = {
22662268
#endif
22672269

22682270
#if USE_WEBGL2
2269-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2271+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
22702272
GLctx.uniform2fv(GL.uniforms[location], HEAPF32, value>>2, count*2);
22712273
return;
22722274
}
@@ -2299,7 +2301,7 @@ var LibraryGL = {
22992301
#endif
23002302

23012303
#if USE_WEBGL2
2302-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2304+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
23032305
GLctx.uniform3fv(GL.uniforms[location], HEAPF32, value>>2, count*3);
23042306
return;
23052307
}
@@ -2333,7 +2335,7 @@ var LibraryGL = {
23332335
#endif
23342336

23352337
#if USE_WEBGL2
2336-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2338+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
23372339
GLctx.uniform4fv(GL.uniforms[location], HEAPF32, value>>2, count*4);
23382340
return;
23392341
}
@@ -2368,7 +2370,7 @@ var LibraryGL = {
23682370
#endif
23692371

23702372
#if USE_WEBGL2
2371-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2373+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
23722374
GLctx.uniformMatrix2fv(GL.uniforms[location], !!transpose, HEAPF32, value>>2, count*4);
23732375
return;
23742376
}
@@ -2403,7 +2405,7 @@ var LibraryGL = {
24032405
#endif
24042406

24052407
#if USE_WEBGL2
2406-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2408+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
24072409
GLctx.uniformMatrix3fv(GL.uniforms[location], !!transpose, HEAPF32, value>>2, count*9);
24082410
return;
24092411
}
@@ -2443,7 +2445,7 @@ var LibraryGL = {
24432445
#endif
24442446

24452447
#if USE_WEBGL2
2446-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
2448+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
24472449
GLctx.uniformMatrix4fv(GL.uniforms[location], !!transpose, HEAPF32, value>>2, count*16);
24482450
return;
24492451
}
@@ -3486,7 +3488,7 @@ var LibraryGL = {
34863488
GL.mappedBuffers[buffer] = null;
34873489

34883490
if (!(mapping.access & 0x10)) /* GL_MAP_FLUSH_EXPLICIT_BIT */
3489-
if (GL.currentContext.supportsWebGL2EntryPoints) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
3491+
if (GL.currentContext.version >= 2) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
34903492
GLctx.bufferSubData(target, mapping.offset, HEAPU8, mapping.mem, mapping.length);
34913493
} else {
34923494
GLctx.bufferSubData(target, mapping.offset, HEAPU8.subarray(mapping.mem, mapping.mem+mapping.length));

0 commit comments

Comments
 (0)