Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid garbage-free WebGL APIs when memory size is over 2gb. #21445

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 52 additions & 67 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,18 +1637,18 @@ 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);
return;
}
#if WEBGL_USE_GARBAGE_FREE_APIS
if (pixels) {
var heap = heapObjectForWebGLType(type);
var index = toTypedArrayIndex(pixels, heap);
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, heap, index);
return;
}
#endif
}
#endif
var pixelData = pixels ? emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, internalFormat) : null;
Expand All @@ -1672,15 +1672,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 @@ -1695,16 +1697,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 @@ -1841,14 +1843,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 @@ -1865,10 +1865,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 @@ -2431,8 +2429,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 @@ -2472,8 +2470,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 @@ -2514,8 +2512,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 @@ -2557,9 +2555,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 @@ -2603,8 +2599,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 @@ -2644,9 +2640,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 @@ -2671,7 +2665,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 @@ -2681,16 +2675,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 @@ -2733,9 +2725,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 @@ -2783,9 +2773,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 @@ -2829,9 +2817,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 @@ -2863,7 +2849,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 @@ -2873,16 +2859,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 @@ -2921,7 +2905,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 @@ -4157,11 +4141,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 certain browser when large memory sizes
// (2gb+ or 4gb+) are used
var WEBGL_USE_GARBAGE_FREE_APIS = false;
Loading
Loading