Skip to content

Commit 5b24a09

Browse files
authored
Fix cpu profiler with -sMODULARIZE=1 (emscripten-core#20429)
* Fix typo in includes function * Add missing makeXRCompatible to list of functions * Handle injection for more webgl2 methods with multiple number of args * Run test_cpuprofiler_memoryprofiler also with -sMODULARIZE Fixes emscripten-core#20425
1 parent 5b63268 commit 5b24a09

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

src/cpuprofiler.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ var emscriptenCpuProfiler = {
504504

505505
// Work around Microsoft Edge bug where webGLContext.function.length always returns 0.
506506
webGLFunctionLength: function(f) {
507-
var l0 = ['getContextAttributes','isContextLost','getSupportedExtensions','createBuffer','createFramebuffer','createProgram','createRenderbuffer','createTexture','finish','flush','getError', 'createVertexArray', 'createQuery', 'createSampler', 'createTransformFeedback', 'endTransformFeedback', 'pauseTransformFeedback', 'resumeTransformFeedback'];
507+
var l0 = ['getContextAttributes','isContextLost','getSupportedExtensions','createBuffer','createFramebuffer','createProgram','createRenderbuffer','createTexture','finish','flush','getError', 'createVertexArray', 'createQuery', 'createSampler', 'createTransformFeedback', 'endTransformFeedback', 'pauseTransformFeedback', 'resumeTransformFeedback', 'makeXRCompatible'];
508508
var l1 = ['getExtension','activeTexture','blendEquation','checkFramebufferStatus','clear','clearDepth','clearStencil','compileShader','createShader','cullFace','deleteBuffer','deleteFramebuffer','deleteProgram','deleteRenderbuffer','deleteShader','deleteTexture','depthFunc','depthMask','disable','disableVertexAttribArray','enable','enableVertexAttribArray','frontFace','generateMipmap','getAttachedShaders','getParameter','getProgramInfoLog','getShaderInfoLog','getShaderSource','isBuffer','isEnabled','isFramebuffer','isProgram','isRenderbuffer','isShader','isTexture','lineWidth','linkProgram','stencilMask','useProgram','validateProgram', 'deleteQuery', 'isQuery', 'deleteVertexArray', 'bindVertexArray', 'isVertexArray', 'drawBuffers', 'readBuffer', 'endQuery', 'deleteSampler', 'isSampler', 'isSync', 'deleteSync', 'deleteTransformFeedback', 'isTransformFeedback', 'beginTransformFeedback'];
509509
var l2 = ['attachShader','bindBuffer','bindFramebuffer','bindRenderbuffer','bindTexture','blendEquationSeparate','blendFunc','depthRange','detachShader','getActiveAttrib','getActiveUniform','getAttribLocation','getBufferParameter','getProgramParameter','getRenderbufferParameter','getShaderParameter','getShaderPrecisionFormat','getTexParameter','getUniform','getUniformLocation','getVertexAttrib','getVertexAttribOffset','hint','pixelStorei','polygonOffset','sampleCoverage','shaderSource','stencilMaskSeparate','uniform1f','uniform1fv','uniform1i','uniform1iv','uniform2fv','uniform2iv','uniform3fv','uniform3iv','uniform4fv','uniform4iv','vertexAttrib1f','vertexAttrib1fv','vertexAttrib2fv','vertexAttrib3fv','vertexAttrib4fv', 'vertexAttribDivisor', 'beginQuery', 'invalidateFramebuffer', 'getFragDataLocation', 'uniform1ui', 'uniform1uiv', 'uniform2uiv', 'uniform3uiv', 'uniform4uiv', 'vertexAttribI4iv', 'vertexAttribI4uiv', 'getQuery', 'getQueryParameter', 'bindSampler', 'getSamplerParameter', 'fenceSync', 'getSyncParameter', 'bindTransformFeedback', 'getTransformFeedbackVarying', 'getIndexedParameter', 'getUniformIndices', 'getUniformBlockIndex', 'getActiveUniformBlockName'];
510510
var l3 = ['bindAttribLocation','bufferData','bufferSubData','drawArrays','getFramebufferAttachmentParameter','stencilFunc','stencilOp','texParameterf','texParameteri','uniform2f','uniform2i','uniformMatrix2fv','uniformMatrix3fv','uniformMatrix4fv','vertexAttrib2f', 'getBufferSubData', 'getInternalformatParameter', 'uniform2ui', 'uniformMatrix2x3fv', 'uniformMatrix3x2fv', 'uniformMatrix2x4fv', 'uniformMatrix4x2fv', 'uniformMatrix3x4fv', 'uniformMatrix4x3fv', 'clearBufferiv', 'clearBufferuiv', 'clearBufferfv', 'samplerParameteri', 'samplerParameterf', 'clientWaitSync', 'waitSync', 'transformFeedbackVaryings', 'bindBufferBase', 'getActiveUniforms', 'getActiveUniformBlockParameter', 'uniformBlockBinding'];
@@ -578,7 +578,7 @@ var emscriptenCpuProfiler = {
578578
},
579579

580580
hookWebGLFunction: function(f, glCtx) {
581-
var section = (this.hotGLFunctions.incudes(f) || f.startsWith('uniform') || f.startsWith('vertexAttrib')) ? 0 : 1;
581+
var section = (this.hotGLFunctions.includes(f) || f.startsWith('uniform') || f.startsWith('vertexAttrib')) ? 0 : 1;
582582
var realf = 'real_' + f;
583583
glCtx[realf] = glCtx[f];
584584
var numArgs = this.webGLFunctionLength(f); // On Firefox & Chrome, could do "glCtx[realf].length", but that doesn't work on Edge, which always reports 0.
@@ -641,6 +641,27 @@ var emscriptenCpuProfiler = {
641641
this.endSection(0);
642642
return ret;
643643
};
644+
glCtx['bufferData'] = (a1, a2, a3, a4, a5) => {
645+
// WebGL1/2 versions have different parameters (not just extra ones)
646+
var ret = (a4 !== undefined) ? glCtx['real_bufferData'](a1, a2, a3, a4, a5) : glCtx['real_bufferData'](a1, a2, a3);
647+
return ret;
648+
};
649+
const matrixFuncs = ['uniformMatrix2fv', 'uniformMatrix3fv', 'uniformMatrix4fv'];
650+
matrixFuncs.forEach(f => {
651+
glCtx[f] = (a1, a2, a3, a4, a5) => {
652+
// WebGL2 version has 2 extra optional parameters, ensure we forward them
653+
var ret = (a4 !== undefined) ? glCtx['real_' + f](a1, a2, a3, a4, a5) : glCtx['real_' + f](a1, a2, a3);
654+
return ret;
655+
}
656+
});
657+
const ndvFuncs = ['uniform1fv', 'uniform1iv', 'uniform2fv', 'uniform2iv', 'uniform3fv', 'uniform3iv', 'uniform4fv', 'uniform4iv'];
658+
ndvFuncs.forEach(f => {
659+
glCtx[f] = (a1, a2, a3, a4) => {
660+
// WebGL2 version has 1 extra parameter, ensure we forward them
661+
var ret = (a4 !== undefined) ? glCtx['real_' + f](a1, a2, a3, a4) : glCtx['real_' + f](a1, a2, a3);
662+
return ret;
663+
}
664+
});
644665
}
645666
};
646667

@@ -674,3 +695,6 @@ function cpuprofiler_add_hooks() { emscriptenCpuProfiler.initialize(); }
674695
if (typeof document != 'undefined') {
675696
emscriptenCpuProfiler.initialize();
676697
}
698+
699+
// Declared in globalThis so that `onclick` handlers work when `-sMODULARIZE=1`
700+
globalThis.emscriptenCpuProfiler = emscriptenCpuProfiler;

src/memoryprofiler.js

+3
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,7 @@ if (typeof document != 'undefined' && typeof window != 'undefined' && typeof pro
637637
emscriptenMemoryProfiler.initialize();
638638
}
639639

640+
// Declared in globalThis so that `onclick` handlers work when `-sMODULARIZE=1`
641+
globalThis.emscriptenMemoryProfiler = emscriptenMemoryProfiler;
642+
640643
#endif

test/test_browser.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -2585,8 +2585,12 @@ def test_preload_module(self, args):
25852585
# This does not actually verify anything except that --cpuprofiler and --memoryprofiler compiles.
25862586
# Run interactive.test_cpuprofiler_memoryprofiler for interactive testing.
25872587
@requires_graphics_hardware
2588-
def test_cpuprofiler_memoryprofiler(self):
2589-
self.btest_exit('hello_world_gles.c', args=['-DLONGTEST=1', '-DTEST_MEMORYPROFILER_ALLOCATIONS_MAP=1', '--cpuprofiler', '--memoryprofiler', '-lGL', '-lglut', '-DANIMATE'])
2588+
@parameterized({
2589+
'': ([],),
2590+
'modularized': (['-sMODULARIZE=1', '-sEXPORT_NAME=MyModule', '--shell-file', test_file('shell_that_launches_modularize.html')],),
2591+
})
2592+
def test_cpuprofiler_memoryprofiler(self, opts):
2593+
self.btest_exit('hello_world_gles.c', args=['-DLONGTEST=1', '-DTEST_MEMORYPROFILER_ALLOCATIONS_MAP=1', '--cpuprofiler', '--memoryprofiler', '-lGL', '-lglut', '-DANIMATE'] + opts)
25902594

25912595
def test_uuid(self):
25922596
# Run with ./runner browser.test_uuid

0 commit comments

Comments
 (0)