Skip to content

Commit

Permalink
Avoid garbage-free WebGL APIs when memory size is over 2gb.
Browse files Browse the repository at this point in the history
Both chrome and firefox see have some issues with passing 2gb+ and 4gb+
offsets to these APIs.

Once the browser issues are addressed we can lift these restrictions
over time.

Fixes: #20533
  • Loading branch information
sbc100 committed Feb 29, 2024
1 parent 165133b commit 5fe50e6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 86 deletions.
130 changes: 58 additions & 72 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1518,14 +1518,14 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
glCompressedTexImage2D: (target, level, internalFormat, width, height, border, imageSize, data) => {
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) {
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
if (GLctx.currentPixelUnpackBufferBinding || !imageSize) {
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, imageSize, data);
} else {
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize);
return;
}
#if WEBGL_USE_GARBAGE_FREE_APIS
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, HEAPU8, data, imageSize);
return;
#endif
}
#endif
GLctx.compressedTexImage2D(target, level, internalFormat, width, height, border, data ? {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}} : null);
Expand All @@ -1535,14 +1535,14 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
glCompressedTexSubImage2D: (target, level, xoffset, yoffset, width, height, format, imageSize, data) => {
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) {
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
if (GLctx.currentPixelUnpackBufferBinding || !imageSize) {
GLctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, data);
} else {
GLctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, HEAPU8, data, imageSize);
return;
}
#if WEBGL_USE_GARBAGE_FREE_APIS
GLctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, HEAPU8, data, imageSize);
return;
#endif
}
#endif
GLctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, data ? {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}} : null);
Expand Down Expand Up @@ -1637,20 +1637,21 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
}
#endif
if ({{{ isCurrentContextWebGL2() }}}) {
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
if (GLctx.currentPixelUnpackBufferBinding) {
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
} else if (pixels) {
return;
}
#if WEBGL_USE_GARBAGE_FREE_APIS
if (pixels) {
var heap = heapObjectForWebGLType(type);
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, heap, toTypedArrayIndex(pixels, heap));
} else {
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, null);
return;
}
return;
#endif
}
#endif
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels ? emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, internalFormat) : null);
var pixelData = pixels ? emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, internalFormat) : null;
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixelData);
},

glTexSubImage2D__deps: ['$emscriptenWebGLGetTexPixelData'
Expand All @@ -1670,15 +1671,17 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
}
#endif
if ({{{ isCurrentContextWebGL2() }}}) {
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
if (GLctx.currentPixelUnpackBufferBinding) {
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
} else if (pixels) {
return;
}
#if WEBGL_USE_GARBAGE_FREE_APIS
if (pixels) {
var heap = heapObjectForWebGLType(type);
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, heap, toTypedArrayIndex(pixels, heap));
return;
}
#endif
}
#endif
var pixelData = pixels ? emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, 0) : null;
Expand All @@ -1693,16 +1696,16 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
glReadPixels: (x, y, width, height, format, type, pixels) => {
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) {
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
if (GLctx.currentPixelPackBufferBinding) {
GLctx.readPixels(x, y, width, height, format, type, pixels);
} else {
var heap = heapObjectForWebGLType(type);
var target = toTypedArrayIndex(pixels, heap);
GLctx.readPixels(x, y, width, height, format, type, heap, target);
return;
}
#if WEBGL_USE_GARBAGE_FREE_APIS
var heap = heapObjectForWebGLType(type);
var target = toTypedArrayIndex(pixels, heap);
GLctx.readPixels(x, y, width, height, format, type, heap, target);
return;
#endif
}
#endif
var pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, format);
Expand Down Expand Up @@ -1839,14 +1842,12 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
}
#endif
#if MAX_WEBGL_VERSION >= 2
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible. If size is zero, WebGL would interpret
// uploading the whole input arraybuffer (starting from given offset),
// which would not make sense in WebAssembly, so avoid uploading if size
// is zero. However we must still call bufferData to establish a backing
// storage of zero bytes.
// If size is zero, WebGL would interpret uploading the whole input
// arraybuffer (starting from given offset), which would not make sense in
// WebAssembly, so avoid uploading if size is zero. However we must still
// call bufferData to establish a backing storage of zero bytes.
if (data && size) {
GLctx.bufferData(target, HEAPU8, usage, data, size);
} else {
Expand All @@ -1863,10 +1864,8 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
},
glBufferSubData: (target, offset, size, data) => {
#if MAX_WEBGL_VERSION >= 2
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
size && GLctx.bufferSubData(target, offset, HEAPU8, data, size);
return;
}
Expand Down Expand Up @@ -2429,8 +2428,8 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniform1iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count);
#else
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform1iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count);
return;
}
Expand Down Expand Up @@ -2470,8 +2469,8 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniform2iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count*2);
#else
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform2iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count*2);
return;
}
Expand Down Expand Up @@ -2512,8 +2511,8 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniform3iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count*3);
#else
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform3iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count*3);
return;
}
Expand Down Expand Up @@ -2555,9 +2554,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniform4iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count*4);
#else
#if MAX_WEBGL_VERSION >= 2
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform4iv(webglGetUniformLocation(location), HEAP32, {{{ getHeapOffset('value', 'i32') }}}, count*4);
return;
Expand Down Expand Up @@ -2601,8 +2598,8 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniform1fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count);
#else
#if MAX_WEBGL_VERSION >= 2
if ({{{ isCurrentContextWebGL2() }}}) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform1fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count);
return;
}
Expand Down Expand Up @@ -2642,9 +2639,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniform2fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*2);
#else
#if MAX_WEBGL_VERSION >= 2
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform2fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*2);
return;
Expand All @@ -2669,7 +2664,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
},
glUniform3fv__deps: ['$webglGetUniformLocation'
#if GL_POOL_TEMP_BUFFERS && MIN_WEBGL_VERSION == 1
#if GL_POOL_TEMP_BUFFERS && !(MIN_WEBGL_VERSION >= 2 && WEBGL_USE_GARBAGE_FREE_APIS)
, '$miniTempWebGLFloatBuffers'
#endif
],
Expand All @@ -2679,16 +2674,14 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
assert((value % 4) == 0, 'Pointer to float data passed to glUniform3fv must be aligned to four bytes!' + value);
#endif
#if MIN_WEBGL_VERSION >= 2
#if MIN_WEBGL_VERSION >= 2 && WEBGL_USE_GARBAGE_FREE_APIS
#if GL_ASSERTIONS
assert(GL.currentContext.version >= 2);
#endif
count && GLctx.uniform3fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*3);
#else
#if MAX_WEBGL_VERSION >= 2
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform3fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*3);
return;
Expand Down Expand Up @@ -2731,9 +2724,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniform4fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*4);
#else
#if MAX_WEBGL_VERSION >= 2
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniform4fv(webglGetUniformLocation(location), HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*4);
return;
Expand Down Expand Up @@ -2781,9 +2772,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniformMatrix2fv(webglGetUniformLocation(location), !!transpose, HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*4);
#else
#if MAX_WEBGL_VERSION >= 2
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniformMatrix2fv(webglGetUniformLocation(location), !!transpose, HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*4);
return;
Expand Down Expand Up @@ -2827,9 +2816,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
count && GLctx.uniformMatrix3fv(webglGetUniformLocation(location), !!transpose, HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*9);
#else
#if MAX_WEBGL_VERSION >= 2
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniformMatrix3fv(webglGetUniformLocation(location), !!transpose, HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*9);
return;
Expand Down Expand Up @@ -2861,7 +2848,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
},
glUniformMatrix4fv__deps: ['$webglGetUniformLocation'
#if GL_POOL_TEMP_BUFFERS && MIN_WEBGL_VERSION == 1
#if GL_POOL_TEMP_BUFFERS && !(MIN_WEBGL_VERSION >= 2 && WEBGL_USE_GARBAGE_FREE_APIS)
, '$miniTempWebGLFloatBuffers'
#endif
],
Expand All @@ -2871,16 +2858,14 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
assert((value & 3) == 0, 'Pointer to float data passed to glUniformMatrix4fv must be aligned to four bytes!');
#endif
#if MIN_WEBGL_VERSION >= 2
#if MIN_WEBGL_VERSION >= 2 && WEBGL_USE_GARBAGE_FREE_APIS
#if GL_ASSERTIONS
assert(GL.currentContext.version >= 2);
#endif
count && GLctx.uniformMatrix4fv(webglGetUniformLocation(location), !!transpose, HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*16);
#else
#if MAX_WEBGL_VERSION >= 2
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
count && GLctx.uniformMatrix4fv(webglGetUniformLocation(location), !!transpose, HEAPF32, {{{ getHeapOffset('value', 'float') }}}, count*16);
return;
Expand Down Expand Up @@ -2919,7 +2904,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
var view = {{{ makeHEAPView('F32', 'value', 'value+count*64') }}};
}
GLctx.uniformMatrix4fv(webglGetUniformLocation(location), !!transpose, view);
#endif // MIN_WEBGL_VERSION >= 2
#endif // MIN_WEBGL_VERSION >= 2 && WEBGL_USE_GARBAGE_FREE_APIS
},
glBindBuffer: (target, buffer) => {
Expand Down Expand Up @@ -4155,11 +4140,12 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
}
if (!(mapping.access & 0x10)) { /* GL_MAP_FLUSH_EXPLICIT_BIT */
if ({{{ isCurrentContextWebGL2() }}}) { // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
#if WEBGL_USE_GARBAGE_FREE_APIS
if ({{{ isCurrentContextWebGL2() }}}) {
GLctx.bufferSubData(target, mapping.offset, HEAPU8, mapping.mem, mapping.length);
} else {
GLctx.bufferSubData(target, mapping.offset, HEAPU8.subarray(mapping.mem, mapping.mem+mapping.length));
}
} else
#endif
GLctx.bufferSubData(target, mapping.offset, HEAPU8.subarray(mapping.mem, mapping.mem+mapping.length));
}
_free(mapping.mem);
mapping.mem = 0;
Expand Down
15 changes: 14 additions & 1 deletion src/library_webgl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ var LibraryWebGL2 = {
return;
}
#endif
#if WEBGL_USE_GARBAGE_FREE_APIS
size && GLctx.getBufferSubData(target, offset, HEAPU8, data, size);
#else
size && GLctx.getBufferSubData(target, offset, HEAPU8.subarray(data, data+size));
#endif
},

glInvalidateFramebuffer__deps: ['$tempFixedLengthArray'],
Expand Down Expand Up @@ -147,13 +151,22 @@ var LibraryWebGL2 = {
GLctx.invalidateSubFramebuffer(target, list, x, y, width, height);
},

glTexImage3D__deps: ['$heapObjectForWebGLType', '$toTypedArrayIndex'],
glTexImage3D__deps: ['$heapObjectForWebGLType', '$toTypedArrayIndex',
#if !WEBGL_USE_GARBAGE_FREE_APIS
'$emscriptenWebGLGetTexPixelData',
#endif
],
glTexImage3D: (target, level, internalFormat, width, height, depth, border, format, type, pixels) => {
if (GLctx.currentPixelUnpackBufferBinding) {
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixels);
} else if (pixels) {
var heap = heapObjectForWebGLType(type);
#if WEBGL_USE_GARBAGE_FREE_APIS
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, heap, toTypedArrayIndex(pixels, heap));
#else
var pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height * depth, pixels, internalFormat);
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixelData);
#endif
} else {
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, null);
}
Expand Down
6 changes: 6 additions & 0 deletions src/settings_internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,9 @@ var MINIFY_WHITESPACE = true;
var ASYNCIFY_IMPORTS_EXCEPT_JS_LIBS = [];

var WARN_DEPRECATED = true;

// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
// those always when possible.
// We currently set this to false for memory sizes over 2GB due to browser
// bugs.
var WEBGL_USE_GARBAGE_FREE_APIS = false;
Loading

0 comments on commit 5fe50e6

Please sign in to comment.