Skip to content

Commit

Permalink
Move __glGenObject to GL.genObject. NFC
Browse files Browse the repository at this point in the history
This function logically belongs alongside getNewId.

Split out from emscripten-core#18874
  • Loading branch information
sbc100 committed Feb 8, 2024
1 parent 01d0fc4 commit 9154146
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 55 deletions.
74 changes: 34 additions & 40 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,34 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
return ret;
},

// The code path for creating textures, buffers, framebuffers and other
// objects the same (and not in fast path), so we merge the functions
// together.
// 'createFunction' refers to the WebGL context function name to do the actual
// creation, 'objectTable' points to the GL object table where to populate the
// created objects, and 'functionName' carries the name of the caller for
// debug information.
genObject: (n, buffers, createFunction, objectTable
#if GL_ASSERTIONS
, functionName
#endif
) => {
for (var i = 0; i < n; i++) {
var buffer = GLctx[createFunction]();
var id = buffer && GL.getNewId(objectTable);
if (buffer) {
buffer.name = id;
objectTable[id] = buffer;
} else {
GL.recordError(0x502 /* GL_INVALID_OPERATION */);
#if GL_ASSERTIONS
err(`GL_INVALID_OPERATION in ${functionName}: GLctx.${createFunction} returned null - most likely GL context is lost!`);
#endif
}
{{{ makeSetValue('buffers', 'i*4', 'id', 'i32') }}};
}
},

#if FULL_ES2 || LEGACY_GL_EMULATION
// When user GL code wants to render from client-side memory, we need to
// upload the vertex data to a temp VBO for rendering. Maintain a set of
Expand Down Expand Up @@ -1770,46 +1798,16 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
return GLctx.isTexture(texture);
},
// The code path for creating textures, buffers, framebuffers and other
// objects is so identical to each other (and not in fast path), that merge
// the functions together to only have one generated copy of this.
// 'createFunction' refers to the WebGL context function name to do the actual
// creation, 'objectTable' points to the GL object table where to populate the
// created objects, and 'functionName' carries the name of the caller for
// debug information.
$__glGenObject: (n, buffers, createFunction, objectTable
#if GL_ASSERTIONS
, functionName
#endif
) => {
for (var i = 0; i < n; i++) {
var buffer = GLctx[createFunction]();
var id = buffer && GL.getNewId(objectTable);
if (buffer) {
buffer.name = id;
objectTable[id] = buffer;
} else {
GL.recordError(0x502 /* GL_INVALID_OPERATION */);
#if GL_ASSERTIONS
err(`GL_INVALID_OPERATION in ${functionName}: GLctx.${createFunction} returned null - most likely GL context is lost!`);
#endif
}
{{{ makeSetValue('buffers', 'i*4', 'id', 'i32') }}};
}
},
glGenBuffers__deps: ['$__glGenObject'],
glGenBuffers: (n, buffers) => {
__glGenObject(n, buffers, 'createBuffer', GL.buffers
GL.genObject(n, buffers, 'createBuffer', GL.buffers
#if GL_ASSERTIONS
, 'glGenBuffers'
#endif
);
},
glGenTextures__deps: ['$__glGenObject'],
glGenTextures: (n, textures) => {
__glGenObject(n, textures, 'createTexture', GL.textures
GL.genObject(n, textures, 'createTexture', GL.textures
#if GL_ASSERTIONS
, 'glGenTextures'
#endif
Expand Down Expand Up @@ -2055,9 +2053,8 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
return GLctx.isBuffer(b);
},
glGenRenderbuffers__deps: ['$__glGenObject'],
glGenRenderbuffers: (n, renderbuffers) => {
__glGenObject(n, renderbuffers, 'createRenderbuffer', GL.renderbuffers
GL.genObject(n, renderbuffers, 'createRenderbuffer', GL.renderbuffers
#if GL_ASSERTIONS
, 'glGenRenderbuffers'
#endif
Expand Down Expand Up @@ -3627,9 +3624,8 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
},
glGenFramebuffers__deps: ['$__glGenObject'],
glGenFramebuffers: (n, ids) => {
__glGenObject(n, ids, 'createFramebuffer', GL.framebuffers
GL.genObject(n, ids, 'createFramebuffer', GL.framebuffers
#if GL_ASSERTIONS
, 'glGenFramebuffers'
#endif
Expand Down Expand Up @@ -3678,19 +3674,17 @@ for (/**@suppress{duplicate}*/var i = 0; i < {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
return GLctx.isFramebuffer(fb);
},
glGenVertexArrays__deps: ['$__glGenObject'
#if LEGACY_GL_EMULATION
, '$emulGlGenVertexArrays'
glGenVertexArrays__deps: ['$emulGlGenVertexArrays'],
#endif
],
glGenVertexArrays: (n, arrays) => {
#if LEGACY_GL_EMULATION
emulGlGenVertexArrays(n, arrays);
#else
#if GL_ASSERTIONS
assert(GLctx.createVertexArray, 'Must have WebGL2 or OES_vertex_array_object to use vao');
#endif
__glGenObject(n, arrays, 'createVertexArray', GL.vaos
GL.genObject(n, arrays, 'createVertexArray', GL.vaos
#if GL_ASSERTIONS
, 'glGenVertexArrays'
#endif
Expand Down
9 changes: 3 additions & 6 deletions src/library_webgl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ var LibraryWebGL2 = {
},

// Queries
glGenQueries__deps: ['$__glGenObject'],
glGenQueries: (n, ids) => {
__glGenObject(n, ids, 'createQuery', GL.queries
GL.genObject(n, ids, 'createQuery', GL.queries
#if GL_ASSERTIONS
, 'glGenQueries'
#endif
Expand Down Expand Up @@ -246,9 +245,8 @@ var LibraryWebGL2 = {
},

// Sampler objects
glGenSamplers__deps: ['$__glGenObject'],
glGenSamplers: (n, samplers) => {
__glGenObject(n, samplers, 'createSampler', GL.samplers
GL.genObject(n, samplers, 'createSampler', GL.samplers
#if GL_ASSERTIONS
, 'glGenSamplers'
#endif
Expand Down Expand Up @@ -340,9 +338,8 @@ var LibraryWebGL2 = {
},

// Transform Feedback
glGenTransformFeedbacks__deps: ['$__glGenObject'],
glGenTransformFeedbacks: (n, ids) => {
__glGenObject(n, ids, 'createTransformFeedback', GL.transformFeedbacks
GL.genObject(n, ids, 'createTransformFeedback', GL.transformFeedbacks
#if GL_ASSERTIONS
, 'glGenTransformFeedbacks'
#endif
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.html": 567,
"a.html.gz": 379,
"a.js": 17795,
"a.js.gz": 7978,
"a.js.gz": 7979,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21485,
"total_gz": 11050
"total_gz": 11051
}
4 changes: 2 additions & 2 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"a.html": 567,
"a.html.gz": 379,
"a.js": 17267,
"a.js.gz": 7813,
"a.js.gz": 7812,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 20957,
"total_gz": 10885
"total_gz": 10884
}
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8355
8348
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
22908
22894
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7176
7169
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19532
19518
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
58114
58095

0 comments on commit 9154146

Please sign in to comment.