Help with embind & Module & preinitializedWebGLContext

29 views
Skip to first unread message

キャロウ マーク

unread,
Jan 5, 2020, 4:59:54 PM1/5/20
to emscripten-discuss
I am working on a wrapper to provide a JS API for a C++ library and I am struggling mightily to get all the pieces in place. I have been completely unable find a simple and *complete* working example (.html, .js & .wasm). Finding and accessing Module is my current problem. (I feel like a newbie having to ask these questions.)

My wrapper code successfully compiles creating a .js and a .wasm file. I have an index.html file with

<script src="../mylib.js"></script>
<script src="webgl-demo.js"></script>

webgl-demo.js creates the canvas and WebGL context the code in mylib.js must use.

I first tried in weggl-demo.js just after the context has been created

Module.preinitializedWebGLContext = gl;

which tells me Module is undefined. The comments in the generated mylib.js say that a Module will be created if not already defined so why do I get “undefined” here? window.Module also gives me undefined. I’m running in a browser.

Then I added

<script>
var Module = {}
</script>

before the script tag with the link to mylib.js in index.html. It solves the undefined but will the setting of preinitializedWebGLContext be too late? The value to use will not be known before some part of webgl-demo.js runs. How can I initialize Module correctly?

I also need to access the GL object so I can find the WebGLTexture objects matching OpenGL ES texture names, so I can from JS create a texture that Emscripten’s OpenGL ES emulation will know about. This will be used for loading texture data. I am thinking of calling GL._glGenTextures and GL._bindTexture then looking in GL.textures to find the matching WebGLTexture. Is this viable? Is there a better way?

Will I find the GL object on Module once I successfully get it initialized?

Regards

-Mark



signature.asc

キャロウ マーク

unread,
Jan 7, 2020, 8:23:22 PM1/7/20
to emscripten-discuss
I think the issue is that Module will not be found until initialization of the runtime is finished. This raises the following question. Will

Module.preinitializedWebGLContext = gl;

in onRuntimeInitialized (before making any Emscripten OpenGL ES calls) work or do I need so somehow arrange for the JS app’s context creation to complete together with setting of preinitializedWebGLContext before Emscripten runtime initialization starts? If so, how do I accomplish that?

Regards

-Mark
signature.asc

キャロウ マーク

unread,
Jan 10, 2020, 12:39:35 PM1/10/20
to emscripten-discuss
This is largely undocumented. From reading what documentation there is, I was under the impression that all that is needed is to set Module.preinitializedWebGLContext prior to creating the module. However it appears that you also need to do

Module.GL.makeContextCurrent(Module.GL.createContext(null, { majorVersion: 2.0 }));

in the then() method when Module is created. Until I did that I was getting GLctx undefined errors.

So you could just as easily skip Module.preinitializedWebGLContext and pass the WebGL context to Module.GL.createContext instead of the null I have shown here.

Am I missing something,  i.e. doing something wrong, or is Module.preinitializedWebGLContext essentially useless?

By the way the Embind documentation really needs to say more about creating Modules. The noddy example didn’t help me at all in the case of creating a class binding.

Regards

    -Mark
signature.asc
Reply all
Reply to author
Forward
0 new messages