Skip to content

Commit 42b17c1

Browse files
committed
Avoid garbage-free WebGL APIs when memory size is over 2gb.
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
1 parent 165133b commit 42b17c1

18 files changed

+106634
-102
lines changed

a.out.js

Lines changed: 26553 additions & 0 deletions
Large diffs are not rendered by default.

a.out.wasm

93 KB
Binary file not shown.

new.js

Lines changed: 26640 additions & 0 deletions
Large diffs are not rendered by default.

new.wasm

93 KB
Binary file not shown.

old-js.wasm

93 KB
Binary file not shown.

old.js

Lines changed: 26665 additions & 0 deletions
Large diffs are not rendered by default.

old.wasm

93 KB
Binary file not shown.

oldld.js

Lines changed: 26665 additions & 0 deletions
Large diffs are not rendered by default.

oldld.wasm

93 KB
Binary file not shown.

src/library_webgl.js

Lines changed: 58 additions & 72 deletions
Large diffs are not rendered by default.

src/library_webgl2.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ var LibraryWebGL2 = {
118118
return;
119119
}
120120
#endif
121+
#if WEBGL_USE_GARBAGE_FREE_APIS
121122
size && GLctx.getBufferSubData(target, offset, HEAPU8, data, size);
123+
#else
124+
size && GLctx.getBufferSubData(target, offset, HEAPU8.subarray(data, data+size));
125+
#endif
122126
},
123127

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

150-
glTexImage3D__deps: ['$heapObjectForWebGLType', '$toTypedArrayIndex'],
154+
glTexImage3D__deps: ['$heapObjectForWebGLType', '$toTypedArrayIndex',
155+
#if !WEBGL_USE_GARBAGE_FREE_APIS
156+
'$emscriptenWebGLGetTexPixelData',
157+
#endif
158+
],
151159
glTexImage3D: (target, level, internalFormat, width, height, depth, border, format, type, pixels) => {
152160
if (GLctx.currentPixelUnpackBufferBinding) {
153161
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixels);
154162
} else if (pixels) {
155163
var heap = heapObjectForWebGLType(type);
164+
#if WEBGL_USE_GARBAGE_FREE_APIS
156165
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, heap, toTypedArrayIndex(pixels, heap));
166+
#else
167+
var pixelData = emscriptenWebGLGetTexPixelData(type, format, width, height * depth, pixels, internalFormat);
168+
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixelData);
169+
#endif
157170
} else {
158171
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, null);
159172
}

src/settings_internal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,9 @@ var MINIFY_WHITESPACE = true;
270270
var ASYNCIFY_IMPORTS_EXCEPT_JS_LIBS = [];
271271

272272
var WARN_DEPRECATED = true;
273+
274+
// WebGL 2 provides new garbage-free entry points to call to WebGL. Use
275+
// those always when possible.
276+
// We currently set this to false for certain browser when large memory sizes
277+
// (2gb+ or 4gb+) are used
278+
var WEBGL_USE_GARBAGE_FREE_APIS = false;

test/code_size/hello_webgl2_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4589,
5-
"a.js.gz": 2341,
4+
"a.js": 4536,
5+
"a.js.gz": 2312,
66
"a.wasm": 10451,
77
"a.wasm.gz": 6724,
8-
"total": 15609,
9-
"total_gz": 9444
8+
"total": 15556,
9+
"total_gz": 9415
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"a.html": 354,
33
"a.html.gz": 266,
4-
"a.js": 22323,
5-
"a.js.gz": 11632,
6-
"total": 22677,
7-
"total_gz": 11898
4+
"a.js": 22270,
5+
"a.js.gz": 11604,
6+
"total": 22624,
7+
"total_gz": 11870
88
}

test/code_size/hello_webgl_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 569,
33
"a.html.gz": 379,
4-
"a.js": 4075,
5-
"a.js.gz": 2170,
4+
"a.js": 4056,
5+
"a.js.gz": 2152,
66
"a.wasm": 10451,
77
"a.wasm.gz": 6724,
8-
"total": 15095,
9-
"total_gz": 9273
8+
"total": 15076,
9+
"total_gz": 9255
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"a.html": 354,
33
"a.html.gz": 266,
4-
"a.js": 21794,
5-
"a.js.gz": 11450,
6-
"total": 22148,
7-
"total_gz": 11716
4+
"a.js": 21775,
5+
"a.js.gz": 11436,
6+
"total": 22129,
7+
"total_gz": 11702
88
}

test/test_browser.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,6 @@ def test_webgl_parallel_shader_compile(self):
13181318
self.btest_exit('webgl_parallel_shader_compile.cpp')
13191319

13201320
@requires_graphics_hardware
1321-
@no_4gb('readPixels fails: https://crbug.com/324992397')
13221321
def test_webgl_explicit_uniform_location(self):
13231322
self.btest_exit('webgl_explicit_uniform_location.c', args=['-sGL_EXPLICIT_UNIFORM_LOCATION', '-sMIN_WEBGL_VERSION=2'])
13241323

@@ -1327,7 +1326,6 @@ def test_webgl_sampler_layout_binding(self):
13271326
self.btest_exit('webgl_sampler_layout_binding.c', args=['-sGL_EXPLICIT_UNIFORM_BINDING'])
13281327

13291328
@requires_graphics_hardware
1330-
@no_4gb('readPixels fails: https://crbug.com/324992397')
13311329
def test_webgl2_ubo_layout_binding(self):
13321330
self.btest_exit('webgl2_ubo_layout_binding.c', args=['-sGL_EXPLICIT_UNIFORM_BINDING', '-sMIN_WEBGL_VERSION=2'])
13331331

@@ -1529,7 +1527,6 @@ def test_sdl_gl_read(self):
15291527
self.btest_exit('test_sdl_gl_read.c', args=['-lSDL', '-lGL'])
15301528

15311529
@requires_graphics_hardware
1532-
@no_4gb('readPixels fails: https://crbug.com/324992397')
15331530
def test_sdl_gl_mapbuffers(self):
15341531
self.btest_exit('test_sdl_gl_mapbuffers.c', args=['-sFULL_ES3', '-lSDL', '-lGL'])
15351532

@@ -2032,12 +2029,10 @@ def test_gl_stride(self):
20322029
self.reftest('gl_stride.c', 'gl_stride.png', args=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
20332030

20342031
@requires_graphics_hardware
2035-
@no_4gb('assertion failure')
20362032
def test_gl_vertex_buffer_pre(self):
20372033
self.reftest('gl_vertex_buffer_pre.c', 'gl_vertex_buffer_pre.png', args=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'])
20382034

20392035
@requires_graphics_hardware
2040-
@no_4gb('assertion failure')
20412036
def test_gl_vertex_buffer(self):
20422037
self.reftest('gl_vertex_buffer.c', 'gl_vertex_buffer.png', args=['-sGL_UNSAFE_OPTS=0', '-sLEGACY_GL_EMULATION', '-lGL', '-lSDL'], reference_slack=1)
20432038

@@ -2779,7 +2774,6 @@ def test_webgl2_pbo(self):
27792774

27802775
@no_firefox('fails on CI likely due to GPU drivers there')
27812776
@requires_graphics_hardware
2782-
@no_4gb('fails to render')
27832777
def test_webgl2_sokol_mipmap(self):
27842778
self.reftest('third_party/sokol/mipmap-emsc.c', 'third_party/sokol/mipmap-emsc.png',
27852779
args=['-sMAX_WEBGL_VERSION=2', '-lGL', '-O1'], reference_slack=2)
@@ -4487,12 +4481,13 @@ def test_small_js_flags(self):
44874481
self.assertLess(abs(size - 4800), 100)
44884482

44894483
# Tests that it is possible to initialize and render WebGL content in a
4490-
# pthread by using OffscreenCanvas. -DTEST_CHAINED_WEBGL_CONTEXT_PASSING:
4491-
# Tests that it is possible to transfer WebGL canvas in a chain from main
4492-
# thread -> thread 1 -> thread 2 and then init and render WebGL content there.
4493-
@no_chrome('see https://crbug.com/961765')
4484+
# pthread by using OffscreenCanvas.
4485+
@no_chrome('https://crbug.com/961765')
44944486
@parameterized({
44954487
'': ([],),
4488+
# -DTEST_CHAINED_WEBGL_CONTEXT_PASSING:
4489+
# Tests that it is possible to transfer WebGL canvas in a chain from main
4490+
# thread -> thread 1 -> thread 2 and then init and render WebGL content there.
44964491
'chained': (['-DTEST_CHAINED_WEBGL_CONTEXT_PASSING'],),
44974492
})
44984493
@requires_threads
@@ -4563,7 +4558,6 @@ def test_webgl_draw_base_vertex_base_instance(self):
45634558
'-DWEBGL_CONTEXT_VERSION=2'])
45644559

45654560
@requires_graphics_hardware
4566-
@no_4gb('fails to render')
45674561
def test_webgl_sample_query(self):
45684562
self.btest_exit('webgl_sample_query.cpp', args=['-sMAX_WEBGL_VERSION=2', '-lGL'])
45694563

@@ -4621,7 +4615,6 @@ def test_webgl_offscreen_framebuffer_state_restoration(self):
46214615

46224616
# Tests that using an array of structs in GL uniforms works.
46234617
@requires_graphics_hardware
4624-
@no_4gb('fails to render')
46254618
def test_webgl_array_of_structs_uniform(self):
46264619
self.reftest('webgl_array_of_structs_uniform.c', 'webgl_array_of_structs_uniform.png', args=['-lGL', '-sMAX_WEBGL_VERSION=2'])
46274620

@@ -4688,7 +4681,7 @@ def test_webgl_simple_extensions(self, simple_enable_extensions, webgl_version):
46884681
self.btest_exit('webgl2_simple_enable_extensions.c', args=cmd)
46894682

46904683
@parameterized({
4691-
'default': ([],),
4684+
'': ([],),
46924685
'closure': (['-sASSERTIONS', '--closure=1'],),
46934686
'main_module': (['-sMAIN_MODULE=1'],),
46944687
})

tools/link.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,17 @@ def check_memory_setting(setting):
16111611
if not settings.MEMORY64 and settings.MAXIMUM_MEMORY > 2 * 1024 * 1024 * 1024:
16121612
settings.CAN_ADDRESS_2GB = 1
16131613

1614+
if settings.MAX_WEBGL_VERSION >= 2:
1615+
settings.WEBGL_USE_GARBAGE_FREE_APIS = 1
1616+
# Some browsers have issues using the WebGL2 garbage-free APIs when the
1617+
# memory offsets are over 2^31 or 2^32
1618+
# For firefox see: https://bugzilla.mozilla.org/show_bug.cgi?id=1838218
1619+
if settings.MIN_FIREFOX_VERSION != feature_matrix.UNSUPPORTED and settings.MAXIMUM_MEMORY > 2 ** 31:
1620+
settings.WEBGL_USE_GARBAGE_FREE_APIS = 0
1621+
# For chrome see: https://crbug.com/324992397
1622+
if settings.MIN_CHROME_VERSION != feature_matrix.UNSUPPORTED and settings.MEMORY64 and settings.MAXIMUM_MEMORY > 2 ** 32:
1623+
settings.WEBGL_USE_GARBAGE_FREE_APIS = 0
1624+
16141625
if settings.MINIMAL_RUNTIME:
16151626
if settings.EXIT_RUNTIME:
16161627
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['proc_exit', '$callRuntimeCallbacks']

0 commit comments

Comments
 (0)