Skip to content

Commit d8ee4c8

Browse files
authored
[WebGL] Add doBufferSubData helper function. NFC (#26892)
Followup to #26893. There are several places in the codebase where we call `GLctx.bufferSubData`. With webgl2 this function take an offset into another TypedArray whereas in webgl1 we needed use a `.subarray` here. With this change all the calls now go though our `GL.doBufferSubData` helper.
1 parent 4e31feb commit d8ee4c8

3 files changed

Lines changed: 37 additions & 40 deletions

File tree

src/lib/libglemu.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ var LibraryGLEmulation = {
857857
#if !FULL_ES2
858858
$GLImmediate__postset: 'GLImmediate.setupFuncs(); Browser.moduleContextCreatedCallbacks.push(() => GLImmediate.init());',
859859
#endif
860-
$GLImmediate__deps: ['$Browser', '$GL', '$GLEmulation'],
860+
$GLImmediate__deps: ['$Browser', '$GL', '$GLEmulation', '$webglBufferSubData'],
861861
$GLImmediate: {
862862
MapTreeLib: null,
863863
spawnMapTreeLib: () => {
@@ -2552,7 +2552,7 @@ var LibraryGLEmulation = {
25522552
GLImmediate.lastArrayBuffer = arrayBuffer;
25532553
}
25542554

2555-
GLctx.bufferSubData(GLctx.ARRAY_BUFFER, start, GLImmediate.vertexData.subarray(start >> 2, end >> 2));
2555+
webglBufferSubData(GLctx.ARRAY_BUFFER, start, (end - start) >> 2, start >> 2, GLImmediate.vertexData);
25562556
}
25572557
#if GL_UNSAFE_OPTS
25582558
if (canSkip) return;
@@ -3046,12 +3046,13 @@ var LibraryGLEmulation = {
30463046
}
30473047
if (!GLctx.currentElementArrayBufferBinding) {
30483048
// If no element array buffer is bound, then indices is a literal pointer to clientside data
3049+
var byteSize = numProvidedIndexes << 1;
30493050
#if ASSERTIONS
3050-
assert(numProvidedIndexes << 1 <= GL.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (a)');
3051+
assert(byteSize <= GL.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (a)');
30513052
#endif
3052-
var indexBuffer = GL.getTempIndexBuffer(numProvidedIndexes << 1);
3053+
var indexBuffer = GL.getTempIndexBuffer(byteSize);
30533054
GLctx.bindBuffer(GLctx.ELEMENT_ARRAY_BUFFER, indexBuffer);
3054-
GLctx.bufferSubData(GLctx.ELEMENT_ARRAY_BUFFER, 0, {{{ makeHEAPView('U16', 'ptr', 'ptr + (numProvidedIndexes << 1)') }}});
3055+
webglBufferSubData(GLctx.ELEMENT_ARRAY_BUFFER, 0, byteSize, ptr);
30553056
ptr = 0;
30563057
emulatedElementArrayBuffer = true;
30573058
}

src/lib/libwebgl.js

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
249249
#endif // GL_SUPPORT_AUTOMATIC_ENABLE_EXTENSIONS
250250
#if FULL_ES2 || LEGACY_GL_EMULATION
251251
'$registerPreMainLoop',
252+
'$webglBufferSubData',
252253
#endif
253254
],
254255
#if FULL_ES2 || LEGACY_GL_EMULATION
@@ -584,9 +585,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
584585
var size = GL.calcBufLength(cb.size, cb.type, cb.stride, count);
585586
var buf = GL.getTempVertexBuffer(size);
586587
GLctx.bindBuffer(0x8892 /*GL_ARRAY_BUFFER*/, buf);
587-
GLctx.bufferSubData(0x8892 /*GL_ARRAY_BUFFER*/,
588-
0,
589-
HEAPU8.subarray(cb.ptr, cb.ptr + size));
588+
webglBufferSubData(0x8892 /*GL_ARRAY_BUFFER*/, 0, size, cb.ptr);
590589
#if GL_ASSERTIONS
591590
GL.validateVertexAttribPointer(cb.size, cb.type, cb.stride, 0);
592591
#endif
@@ -1258,6 +1257,23 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
12581257

12591258
},
12601259

1260+
// Wrapper around GLctx.bufferSubData that can hangle both WebGL1 (which
1261+
// requires new subarray on each call) and WebGL2 (which does not).
1262+
// Argument ordering is a little strange here, since we want a default
1263+
// for `src` is has to come last.
1264+
$webglBufferSubData__internal: true,
1265+
$webglBufferSubData: (target, offset, size, data, src = HEAPU8) => {
1266+
#if WEBGL_USE_GARBAGE_FREE_APIS
1267+
if ({{{ isCurrentContextWebGL2() }}}) {
1268+
size && GLctx.bufferSubData(target, offset, src, data, size);
1269+
return;
1270+
}
1271+
#endif
1272+
#if INCLUDE_WEBGL1_FALLBACK
1273+
GLctx.bufferSubData(target, offset, src.subarray(data, data + size));
1274+
#endif
1275+
},
1276+
12611277
$webglGetExtensions__internal: true,
12621278
$webglGetExtensions__deps: ['$getEmscriptenSupportedExtensions'],
12631279
$webglGetExtensions: () => {
@@ -1915,17 +1931,10 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
19151931
#endif
19161932
},
19171933

1918-
glBufferSubData: (target, offset, size, data) => {
1919-
#if WEBGL_USE_GARBAGE_FREE_APIS
1920-
if ({{{ isCurrentContextWebGL2() }}}) {
1921-
size && GLctx.bufferSubData(target, offset, HEAPU8, data, size);
1922-
return;
1923-
}
1924-
#endif
1925-
#if INCLUDE_WEBGL1_FALLBACK
1926-
GLctx.bufferSubData(target, offset, HEAPU8.subarray(data, data+size));
1927-
#endif
1928-
},
1934+
// This cannot be simple alias because under wasm64 we need to be able modify
1935+
// the function at compile time to provide automatically marshal of the pointer arguments.
1936+
glBufferSubData__deps: ['$webglBufferSubData'],
1937+
glBufferSubData: (target, offset, size, data) => webglBufferSubData(target, offset, size, data),
19291938

19301939
// Queries EXT
19311940
glGenQueriesEXT__sig: 'vip',
@@ -3874,6 +3883,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
38743883
#endif
38753884
},
38763885

3886+
glDrawElements__deps: ['$webglBufferSubData'],
38773887
glDrawElements: (mode, count, type, indices) => {
38783888
#if FULL_ES2
38793889
var buf;
@@ -3882,9 +3892,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
38823892
var size = GL.calcBufLength(1, type, 0, count);
38833893
buf = GL.getTempIndexBuffer(size);
38843894
GLctx.bindBuffer(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, buf);
3885-
GLctx.bufferSubData(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/,
3886-
0,
3887-
HEAPU8.subarray(indices, indices + size));
3895+
webglBufferSubData(0x8893 /*GL_ELEMENT_ARRAY_BUFFER*/, 0, size, indices);
38883896

38893897
// Calculating vertex count if shader's attribute data is on client side
38903898
if (count > 0) {
@@ -4210,7 +4218,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
42104218
}
42114219
},
42124220

4213-
glFlushMappedBufferRange__deps: ['$emscriptenWebGLGetBufferBinding', '$emscriptenWebGLValidateMapBufferTarget'],
4221+
glFlushMappedBufferRange__deps: ['$emscriptenWebGLGetBufferBinding', '$emscriptenWebGLValidateMapBufferTarget', '$webglBufferSubData'],
42144222
glFlushMappedBufferRange: (target, offset, length) => {
42154223
if (!emscriptenWebGLValidateMapBufferTarget(target)) {
42164224
GL.recordError(0x500/*GL_INVALID_ENUM*/);
@@ -4236,10 +4244,7 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
42364244
return;
42374245
}
42384246

4239-
GLctx.bufferSubData(
4240-
target,
4241-
mapping.offset,
4242-
HEAPU8.subarray(mapping.mem + offset, mapping.mem + offset + length));
4247+
webglBufferSubData(target, mapping.offset, length, mapping.mem + offset);
42434248
},
42444249

42454250
glUnmapBuffer__deps: ['$emscriptenWebGLGetBufferBinding', '$emscriptenWebGLValidateMapBufferTarget', 'free'],
@@ -4259,18 +4264,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
42594264
}
42604265

42614266
if (!(mapping.access & 0x10)) { /* GL_MAP_FLUSH_EXPLICIT_BIT */
4262-
#if WEBGL_USE_GARBAGE_FREE_APIS
4263-
if ({{{ isCurrentContextWebGL2() }}}) {
4264-
GLctx.bufferSubData(target, mapping.offset, HEAPU8, mapping.mem, mapping.length);
4265-
}
4266-
#if INCLUDE_WEBGL1_FALLBACK
4267-
else
4268-
#endif
4269-
#endif
4270-
#if INCLUDE_WEBGL1_FALLBACK
4271-
GLctx.bufferSubData(target, mapping.offset, HEAPU8.subarray(mapping.mem, mapping.mem+mapping.length));
4272-
#endif
4267+
webglBufferSubData(target, mapping.offset, mapping.length, mapping.mem);
42734268
}
4269+
42744270
_free(mapping.mem);
42754271
mapping.mem = 0;
42764272
return 1;

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 268033,
2+
"a.out.js": 267889,
33
"a.out.nodebug.wasm": 587151,
4-
"total": 855184,
4+
"total": 855040,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

0 commit comments

Comments
 (0)