Using an already existing WebGL context requires a somewhat confusing dance with preinitializedWebGLContext.
To activate a GL context (as in https://github.com/emscripten-core/emscripten/blob/25c255b66fe553a4bff3761cbeb7add8f1583d25/test/browser/test_preinitialized_webgl_context.c):
- set
preinitalizedWebGLContext on module creation
- call
emscripten_webgl_context_create(), necessarily passing a valid canvas element reference and context attributes. These arguments will not really be used, this function returns (a handle to) the preinitializedWebGLContext.
- call
emscripten_webgl_make_context_current(handle)
This appears more confusing than necessary. Also, this process does not seem to allow using the module with more than one WebGL context. This is something that we'd like to, to reuse our module to render into multiple (dynamically created) canvases.
To work around this, we've extended the Module with this --pre-js file.
// Activates the given WebGL context for rendering. Does _not_ register a context handle for the context and thus it cannot be referenced from WASM.
Module.makeWebGL2ContextCurrent = (ctx) => {
GL.currentContext = {
handle: -1, // unused?
attributes: {}, // unused?
version: 2,
GLctx: ctx
};
Module['ctx'] = GLctx = GL.currentContext?.GLctx;
}
With this, we don't need any of the steps 1. - 3. above; we only call module.makeWebGL2ContextCurrent(gl) for our canvas and proceed to call into the WASM to render.
Questions:
- Is there a pre-existing functionality that already achieves what our --pre-js extension does?
- If not, would it be useful to add this to emscripten directly?
Using an already existing WebGL context requires a somewhat confusing dance with
preinitializedWebGLContext.To activate a GL context (as in https://github.com/emscripten-core/emscripten/blob/25c255b66fe553a4bff3761cbeb7add8f1583d25/test/browser/test_preinitialized_webgl_context.c):
preinitalizedWebGLContexton module creationemscripten_webgl_context_create(), necessarily passing a valid canvas element reference and context attributes. These arguments will not really be used, this function returns (a handle to) thepreinitializedWebGLContext.emscripten_webgl_make_context_current(handle)This appears more confusing than necessary. Also, this process does not seem to allow using the module with more than one WebGL context. This is something that we'd like to, to reuse our module to render into multiple (dynamically created) canvases.
To work around this, we've extended the Module with this
--pre-jsfile.With this, we don't need any of the steps 1. - 3. above; we only call
module.makeWebGL2ContextCurrent(gl)for our canvas and proceed to call into the WASM to render.Questions: