WebGL with PROXY_TO_PTHREAD runtime error issue

146 views
Skip to first unread message

Greg Baxes

unread,
Jun 1, 2020, 5:34:50 PM6/1/20
to emscripten-discuss
Hi everyone,

I have been working successfully with Emscripten over the past few weeks porting a fairly heavy C++ GLES2 3D terrain-rendering engine and small glut-based application layer. It uses pthreads. Everything runs incredibly well in Chrome! I have always seen the JS console throw the warning: "Blocking on the main thread is very dangerous." To correct this blocking-on-main issue and to be able to add websockets support to the application, I have added the -s PROXY_TO_PTHREAD=1 flag to my build. With this, the application crashes with a pthread error when it first encounters a glGetString call. I have tried using the -s OFFSCREEN_FRAMEBUFFER=1 flag and things get further, but then crash on a glTexImage2D call.

To emulate the problem in a simpler context, I went back to my first simple GLES2 test, standard es2gears.c, which I successfully built using:

emcc -s WASM=1 -g es2gears.c -o demo.html

Similarly with it, when I add the -s PROXY_TO_PTHREAD=1 and -s USE_PTHREADS=1 flags, it crashes the same way when it encounters a glEnable call, throwing the same kind of error:

"pthread sent an error! ... Uncaught TypeError: Cannot read property 'enable' of undefined at _glEnable"

So, I think the es2gears.c build is a good test case of the issue I'm seeing.

Any suggestions to fix this? Is WebGL not able to run in a thread under main? Thank you for any insights!

Jukka Jylänki

unread,
Jun 2, 2020, 3:46:35 AM6/2/20
to emscripte...@googlegroups.com
WebGL API is only available in main browser thread by default.

If you build with -s OFFSCREEN_FRAMEBUFFER=1, then WebGL will be
available from pthreads via the Emscripten WebGL runtime proxying all
the GL calls to the main thread.

If you build with -s OFFSCREENCANVAS_SUPPORT=1, then WebGL will be
available from pthreads via the OffscreenCanvas web api. Check
https://caniuse.com/#feat=offscreencanvas for availability.

Browsers are really bad at printing JS errors: the error "Cannot read
property 'enable' of undefined" should read something like "GLctx is
undefined in GLctx.enable(x) at function _glEnable". That is, there is
no active GL context in the current thread where the GL call was
attempted.
> --
> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/b7bf634d-aaf2-4acd-988c-13058fdda1e5%40googlegroups.com.

Greg Baxes

unread,
Jun 2, 2020, 11:13:35 AM6/2/20
to emscripten-discuss
Hi jj,

Thanks for your reply, very helpful in that building with -s OFFSCREEN_FRAMEBUFFER=1 allows WebGL to be available from pthreads.

However, when I build with -s OFFSCREEN_FRAMEBUFFER=1, it throws "emscripten_async_queue_call_on_thread" errors from pthread at runtime. This occurs in both my terrain renderer builds as well as the simple es2gears.c example. As a simple test case, I have attached es2gears.c. It builds and runs nicely in chrome if I build it with:

  emcc -s WASM=1 -s USE_PTHREADS=1 -g es2gears.c -o demo.html

However, it crashes at runtime with the "emscripten_async_queue_call_on_thread" errors from pthreads if I build it with:

  emcc -s WASM=1 -s USE_PTHREADS=1 -s PROXY_TO_PTHREAD=1 -s OFFSCREEN_FRAMEBUFFER=1 -g es2gears.c -o demo.html

Can you advise what I'm missing to get es2gears.c running with WebGL and pthreads? Thank you.
> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
es2gears.c

Jukka Jylänki

unread,
Jun 3, 2020, 2:05:28 PM6/3/20
to emscripte...@googlegroups.com
Oh, the code uses GLUT to initialize the GL context. Unfortunately
OFFSCREEN_FRAMEBUFFER (and OFFSCREENCANVAS_SUPPORT) features are only
available for contexts created with HTML5 API and SDL2, but won't work
for contexts created with GLUT, GLEW, GLFW, EGL or SDL 1.
>> > To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
> --
> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/6239f4c9-6ab3-4ea8-8eab-b4d1cef15205%40googlegroups.com.

Greg Baxes

unread,
Jun 3, 2020, 3:06:27 PM6/3/20
to emscripten-discuss
jj,

OK, thanks, that answers what I'm seeing! We'll migrate it over to SDL2.

Best, Greg
>> > To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
>> > To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/b7bf634d-aaf2-4acd-988c-13058fdda1e5%40googlegroups.com.
>
> --
> You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages