Skip to content

Commit 876ae7c

Browse files
authored
Some fixes for library_webgl.js in CAN_ADDRESS_GB mode (#21220)
This is an overall size win too after compression.
1 parent 659988f commit 876ae7c

10 files changed

+56
-37
lines changed

.circleci/config.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,13 @@ jobs:
798798
steps:
799799
- run-tests-chrome:
800800
title: "browser"
801-
# skip test_4gb_fail as it OOMs on the current bot
802-
test_targets: "browser skip:browser.test_4gb_fail"
801+
# Skip test_4gb_fail as it OOMs on the current bot
802+
# Include an representative selection of browser_2gb
803+
test_targets: "
804+
browser_2gb.test_gles2_uniform_arrays
805+
browser_2gb.test_fetch_to_memory
806+
browser skip:browser.test_4gb_fail
807+
"
803808
test-browser-chrome-wasm64:
804809
executor: bionic
805810
steps:

src/library_webgl.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
8282
return HEAPU16;
8383
},
8484

85-
$heapAccessShiftForWebGLHeap: (heap) => 31 - Math.clz32(heap.BYTES_PER_ELEMENT),
85+
$toTypedArrayIndex: (pointer, heap) =>
86+
#if MEMORY64
87+
pointer / heap.BYTES_PER_ELEMENT,
88+
#else
89+
pointer >>> (31 - Math.clz32(heap.BYTES_PER_ELEMENT)),
90+
#endif
8691

8792
#if MIN_WEBGL_VERSION == 1
8893
$webgl_enable_ANGLE_instanced_arrays: (ctx) => {
@@ -1595,22 +1600,20 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
15951600
return colorChannels[format - 0x1902]||1;
15961601
},
15971602

1598-
$emscriptenWebGLGetTexPixelData__deps: ['$computeUnpackAlignedImageSize', '$colorChannelsInGlTextureFormat', '$heapObjectForWebGLType', '$heapAccessShiftForWebGLHeap'],
1603+
$emscriptenWebGLGetTexPixelData__deps: ['$computeUnpackAlignedImageSize', '$colorChannelsInGlTextureFormat', '$heapObjectForWebGLType', '$toTypedArrayIndex'],
15991604
$emscriptenWebGLGetTexPixelData: (type, format, width, height, pixels, internalFormat) => {
16001605
var heap = heapObjectForWebGLType(type);
1601-
var shift = heapAccessShiftForWebGLHeap(heap);
1602-
var byteSize = 1<<shift;
1603-
var sizePerPixel = colorChannelsInGlTextureFormat(format) * byteSize;
1606+
var sizePerPixel = colorChannelsInGlTextureFormat(format) * heap.BYTES_PER_ELEMENT;
16041607
var bytes = computeUnpackAlignedImageSize(width, height, sizePerPixel, GL.unpackAlignment);
16051608
#if GL_ASSERTIONS
1606-
assert((pixels >> shift) << shift == pixels, 'Pointer to texture data passed to texture get function must be aligned to the byte size of the pixel type!');
1609+
assert(pixels % heap.BYTES_PER_ELEMENT == 0, 'Pointer to texture data passed to texture get function must be aligned to the byte size of the pixel type!');
16071610
#endif
1608-
return heap.subarray(pixels >> shift, pixels + bytes >> shift);
1611+
return heap.subarray(toTypedArrayIndex(pixels, heap), toTypedArrayIndex(pixels + bytes, heap));
16091612
},
16101613

16111614
glTexImage2D__deps: ['$emscriptenWebGLGetTexPixelData'
16121615
#if MAX_WEBGL_VERSION >= 2
1613-
, '$heapObjectForWebGLType', '$heapAccessShiftForWebGLHeap'
1616+
, '$heapObjectForWebGLType', '$toTypedArrayIndex'
16141617
#endif
16151618
],
16161619
glTexImage2D: (target, level, internalFormat, width, height, border, format, type, pixels) => {
@@ -1641,7 +1644,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
16411644
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
16421645
} else if (pixels) {
16431646
var heap = heapObjectForWebGLType(type);
1644-
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, heap, pixels >> heapAccessShiftForWebGLHeap(heap));
1647+
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, heap, toTypedArrayIndex(pixels, heap));
16451648
} else {
16461649
GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, null);
16471650
}
@@ -1653,7 +1656,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
16531656

16541657
glTexSubImage2D__deps: ['$emscriptenWebGLGetTexPixelData'
16551658
#if MAX_WEBGL_VERSION >= 2
1656-
, '$heapObjectForWebGLType', '$heapAccessShiftForWebGLHeap'
1659+
, '$heapObjectForWebGLType', '$toTypedArrayIndex'
16571660
#endif
16581661
],
16591662
glTexSubImage2D: (target, level, xoffset, yoffset, width, height, format, type, pixels) => {
@@ -1674,7 +1677,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
16741677
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
16751678
} else if (pixels) {
16761679
var heap = heapObjectForWebGLType(type);
1677-
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, heap, pixels >> heapAccessShiftForWebGLHeap(heap));
1680+
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, heap, toTypedArrayIndex(pixels, heap));
16781681
} else {
16791682
GLctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, null);
16801683
}
@@ -1688,7 +1691,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
16881691

16891692
glReadPixels__deps: ['$emscriptenWebGLGetTexPixelData'
16901693
#if MAX_WEBGL_VERSION >= 2
1691-
, '$heapObjectForWebGLType', '$heapAccessShiftForWebGLHeap'
1694+
, '$heapObjectForWebGLType', '$toTypedArrayIndex'
16921695
#endif
16931696
],
16941697
glReadPixels: (x, y, width, height, format, type, pixels) => {
@@ -1700,7 +1703,7 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
17001703
GLctx.readPixels(x, y, width, height, format, type, pixels);
17011704
} else {
17021705
var heap = heapObjectForWebGLType(type);
1703-
GLctx.readPixels(x, y, width, height, format, type, heap, pixels >> heapAccessShiftForWebGLHeap(heap));
1706+
GLctx.readPixels(x, y, width, height, format, type, heap, toTypedArrayIndex(pixels, heap));
17041707
}
17051708
return;
17061709
}

src/library_webgl2.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,25 @@ var LibraryWebGL2 = {
147147
GLctx.invalidateSubFramebuffer(target, list, x, y, width, height);
148148
},
149149

150-
glTexImage3D__deps: ['$heapObjectForWebGLType', '$heapAccessShiftForWebGLHeap'],
150+
glTexImage3D__deps: ['$heapObjectForWebGLType', '$toTypedArrayIndex'],
151151
glTexImage3D: (target, level, internalFormat, width, height, depth, border, format, type, pixels) => {
152152
if (GLctx.currentPixelUnpackBufferBinding) {
153153
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, pixels);
154154
} else if (pixels) {
155155
var heap = heapObjectForWebGLType(type);
156-
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, heap, pixels >> heapAccessShiftForWebGLHeap(heap));
156+
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, heap, toTypedArrayIndex(pixels, heap));
157157
} else {
158158
GLctx.texImage3D(target, level, internalFormat, width, height, depth, border, format, type, null);
159159
}
160160
},
161161

162-
glTexSubImage3D__deps: ['$heapObjectForWebGLType', '$heapAccessShiftForWebGLHeap'],
162+
glTexSubImage3D__deps: ['$heapObjectForWebGLType', '$toTypedArrayIndex'],
163163
glTexSubImage3D: (target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels) => {
164164
if (GLctx.currentPixelUnpackBufferBinding) {
165165
GLctx.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels);
166166
} else if (pixels) {
167167
var heap = heapObjectForWebGLType(type);
168-
GLctx.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, heap, pixels >> heapAccessShiftForWebGLHeap(heap));
168+
GLctx.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, heap, toTypedArrayIndex(pixels, heap));
169169
} else {
170170
GLctx.texSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, null);
171171
}

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": 4540,
5-
"a.js.gz": 2345,
4+
"a.js": 4589,
5+
"a.js.gz": 2341,
66
"a.wasm": 10451,
77
"a.wasm.gz": 6724,
8-
"total": 15560,
9-
"total_gz": 9448
8+
"total": 15609,
9+
"total_gz": 9444
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 17746,
5-
"a.js.gz": 7982,
4+
"a.js": 17795,
5+
"a.js.gz": 7978,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 21436,
9-
"total_gz": 11054
8+
"total": 21485,
9+
"total_gz": 11050
1010
}

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": 4027,
5-
"a.js.gz": 2173,
4+
"a.js": 4075,
5+
"a.js.gz": 2170,
66
"a.wasm": 10451,
77
"a.wasm.gz": 6724,
8-
"total": 15047,
9-
"total_gz": 9276
8+
"total": 15095,
9+
"total_gz": 9273
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 567,
33
"a.html.gz": 379,
4-
"a.js": 17219,
5-
"a.js.gz": 7814,
4+
"a.js": 17267,
5+
"a.js.gz": 7813,
66
"a.mem": 3123,
77
"a.mem.gz": 2693,
8-
"total": 20909,
9-
"total_gz": 10886
8+
"total": 20957,
9+
"total_gz": 10885
1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
58197
1+
58187

test/runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
'bigint',
9999
'browser64',
100100
'browser64_4gb',
101+
'browser_2gb',
101102
]
102103

103104

test/test_browser.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from common import BrowserCore, RunnerCore, path_from_root, has_browser, EMTEST_BROWSER, Reporting
2525
from common import create_file, parameterized, ensure_dir, disabled, test_file, WEBIDL_BINDER
26-
from common import read_file, also_with_minimal_runtime, EMRUN, no_wasm64, no_4gb
26+
from common import read_file, also_with_minimal_runtime, EMRUN, no_wasm64, no_2gb, no_4gb
2727
from tools import shared
2828
from tools import ports
2929
from tools import utils
@@ -5572,6 +5572,7 @@ def test_wasm_worker_proxied_function(self):
55725572
self.btest('wasm_worker/proxied_function.c', expected='0', args=['--js-library', test_file('wasm_worker/proxied_function.js'), '-sWASM_WORKERS', '-sASSERTIONS=0'])
55735573

55745574
@no_firefox('no 4GB support yet')
5575+
@no_2gb('uses MAXIMUM_MEMORY')
55755576
@no_4gb('uses MAXIMUM_MEMORY')
55765577
def test_4gb(self):
55775578
# TODO Convert to an actual browser test when it reaches stable.
@@ -5668,6 +5669,7 @@ def test_emmalloc_memgrowth(self, *args):
56685669
self.btest('emmalloc_memgrowth.cpp', expected='0', args=['-sMALLOC=emmalloc', '-sALLOW_MEMORY_GROWTH=1', '-sABORTING_MALLOC=0', '-sASSERTIONS=2', '-sMINIMAL_RUNTIME=1', '-sMAXIMUM_MEMORY=4GB'])
56695670

56705671
@no_firefox('no 4GB support yet')
5672+
@no_2gb('uses MAXIMUM_MEMORY')
56715673
@no_4gb('uses MAXIMUM_MEMORY')
56725674
def test_2gb_fail(self):
56735675
# TODO Convert to an actual browser test when it reaches stable.
@@ -5682,6 +5684,7 @@ def test_2gb_fail(self):
56825684
self.do_run_in_out_file_test('browser/test_2GB_fail.cpp')
56835685

56845686
@no_firefox('no 4GB support yet')
5687+
@no_2gb('uses MAXIMUM_MEMORY')
56855688
@no_4gb('uses MAXIMUM_MEMORY')
56865689
def test_4gb_fail(self):
56875690
# TODO Convert to an actual browser test when it reaches stable.
@@ -5917,3 +5920,10 @@ def setUp(self):
59175920
self.set_setting('GLOBAL_BASE', '4gb')
59185921
self.emcc_args.append('-Wno-experimental')
59195922
self.require_wasm64()
5923+
5924+
5925+
class browser_2gb(browser):
5926+
def setUp(self):
5927+
super().setUp()
5928+
self.set_setting('INITIAL_MEMORY', '2200mb')
5929+
self.set_setting('GLOBAL_BASE', '2gb')

0 commit comments

Comments
 (0)