Emscripten uses if (GL.currentContext.version >= 2) but with PlayCanvas this is undefined

Hi

When using Emscripten to create .js code it will use if (GL.currentContext.version >= 2)
in many places in the produced code ( works in standalone .js and .wasm websites when the Emscripten creates all the .html files )
But when i use this .js with PlayCanvas All browsers hangs with error
TypeError: Cannot read properties of undefined (reading ‘version’)
Why there is no GL.currentContext.version defined with PlayCanvas ?
( I used some 2 years old Emscripten previously which does not create these version checks and everything works perfectly )

Code snipped from Emscripten generated .js file:

  function _glTexImage2D(target, level, internalFormat, width, height, border, format, type, pixels) {
      if (GL.currentContext.version >= 2) {
        // WebGL 2 provides new garbage-free entry points to call to WebGL. Use those always when possible.
        if (GLctx.currentPixelUnpackBufferBinding) {
          GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
        } else if (pixels) {
          var heap = heapObjectForWebGLType(type);
          GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, heap, pixels >> heapAccessShiftForWebGLHeap(heap));
        } else {
          GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, null);
        }
        return;
      }
      GLctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels ? emscriptenWebGLGetTexPixelData(type, format, width, height, pixels, internalFormat) : null);
    }

This does not work with PlayCanvas but works with anything else. What is the catch here?
Thanks

I am not very familiar with Emscripten, but I think that the GL.currentContext should be referring to the rendering context that Emscripten is creating. The default WebGL2RenderingContext and its brothers don’t have such properties in them. Those are created by Emscripten for its own use. In case of PlayCanvas, the engine is creating a context, which it then uses. You should probably research how to feed that context to Emscripten then.

Not really PlayCanvas related, as you should have the same issue if you are using other frameworks as well. I am also not sure how that would work, as Emscripten will try to control the context, while PlayCanvas will do its own thing with it.

Ok I think i figured out atleast this problem.
If i force Emscripten to use a certain WebGL version, it does not create thes IFs.
In example for emcc commandline:
-s MIN_WEBGL_VERSION=2
-s MAX_WEBGL_VERSION=2
And both 1 1 if you want to do for WebGL1 without IFs

Yeah the GL context is still a problem ( worked 2 years go but now lots of trouble )
Emscripten supports also this with:
-s GL_PREINITIALIZED_CONTEXT=1
So It will skip creating its own. The problem might lie here. It still uses like it was its own creation.

I managed to do .js and .wasm custom rendering for PlayCanvas in year 2021. And I did manually compile Emscripten from the sdk 64bit upstream. But Today these all does not work anymore :frowning:
If I use the .js and .wasm files from my 2 year old backup files, they still work todays PlayCanvas ! But I cannot recreate them anymore in 2023