Skip to content

Commit 2bc6946

Browse files
committed
Optimize away temporary garbage created in WebGL 2 glInvalidateFramebuffer() and glInvalidateSubFramebuffer() functions.
1 parent dcde704 commit 2bc6946

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/library_gl.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ var LibraryGL = {
6262
stringCache: {},
6363
#if USE_WEBGL2
6464
stringiCache: {},
65+
tempFixedLengthArray: [],
6566
#endif
6667

6768
packAlignment: 4, // default alignment is 4 bytes
@@ -75,6 +76,14 @@ var LibraryGL = {
7576
for (var i = 0; i < GL.MINI_TEMP_BUFFER_SIZE; i++) {
7677
GL.miniTempBufferViews[i] = GL.miniTempBuffer.subarray(0, i+1);
7778
}
79+
80+
#if USE_WEBGL2
81+
// For glInvalidateFramebuffer and glInvalidateSubFramebuffer, create a set of short fixed-length arrays to avoid
82+
// having to generate any garbage in those functions.
83+
for (var i = 0; i < 32; i++) {
84+
GL.tempFixedLengthArray.push(new Array(i).fill(0));
85+
}
86+
#endif
7887
},
7988

8089
// Records a GL error condition that occurred, stored until user calls glGetError() to fetch it. As per GLES2 spec, only the first error
@@ -1674,18 +1683,26 @@ var LibraryGL = {
16741683
#if USE_WEBGL2
16751684
glInvalidateFramebuffer__sig: 'viii',
16761685
glInvalidateFramebuffer: function(target, numAttachments, attachments) {
1677-
var list = [];
1678-
for (var i = 0; i < numAttachments; i++)
1679-
list.push({{{ makeGetValue('attachments', 'i*4', 'i32') }}});
1686+
#if GL_ASSERTIONS
1687+
assert(numAttachments < GL.tempFixedLengthArray.length, 'Invalid count of numAttachments=' + numAttachments + ' passed to glInvalidateFramebuffer (that many attachment points do not exist in GL)';
1688+
#endif
1689+
var list = GL.tempFixedLengthArray[numAttachments];
1690+
for (var i = 0; i < numAttachments; i++) {
1691+
list[i] = {{{ makeGetValue('attachments', 'i*4', 'i32') }}};
1692+
}
16801693

16811694
GLctx['invalidateFramebuffer'](target, list);
16821695
},
16831696

16841697
glInvalidateSubFramebuffer__sig: 'viiiiiii',
16851698
glInvalidateSubFramebuffer: function(target, numAttachments, attachments, x, y, width, height) {
1686-
var list = [];
1687-
for (var i = 0; i < numAttachments; i++)
1688-
list.push({{{ makeGetValue('attachments', 'i*4', 'i32') }}});
1699+
#if GL_ASSERTIONS
1700+
assert(numAttachments < GL.tempFixedLengthArray.length, 'Invalid count of numAttachments=' + numAttachments + ' passed to glInvalidateSubFramebuffer (that many attachment points do not exist in GL)';
1701+
#endif
1702+
var list = GL.tempFixedLengthArray[numAttachments];
1703+
for (var i = 0; i < numAttachments; i++) {
1704+
list[i] = {{{ makeGetValue('attachments', 'i*4', 'i32') }}};
1705+
}
16891706

16901707
GLctx['invalidateSubFramebuffer'](target, list, x, y, width, height);
16911708
},

0 commit comments

Comments
 (0)