emscripten-core / emscripten-core/emscripten
Is it possible to perform gl calls according to context created on each pthread?
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
I'm working on a game engine that uses SharedGLContext, I need to create a MainGLContext on the engine Main Thread (which is a pthread) and create a SharedGLContext from MainGLContext on a Worker Thread (also a pthread).
This helps my engine doing asynchronized rendering tasks such as loading a very big png from file system and create a texture from it just on a Worker Thread without stucking the engine Main Thread.
For compatibility, I only use OffscreenFramebuffer, the WebGL context will be created from WebGL 1.0 if WebGL 2.0 is not supported, and every gl call will be proxified to browser main thread.
According to the online [document](https://emscripten.org/docs/api_reference/html5.h.html?highlight=framebuffer#c.EmscriptenWebGLContextAttributes.explicitSwapControl), the WebGL attributes should be set like this:
```c++
EmscriptenWebGLContextAttributes attrs;
attrs.majorVersion = 2;
attrs.minorVersion = 0;
attrs.explicitSwapControl = EM_TRUE;
attrs.renderViaOffscreenBackBuffer = EM_TRUE;
attrs.proxyContextToMainThread = EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS;
```
and link flags set like this:
```cmake
-lembind
-pthread
-sPROXY_TO_PTHREAD
-sALLOW_MEMORY_GROWTH=1
-sMAXIMUM_MEMORY=1GB
-sUSE_ZLIB=1
-sMAX_WEBGL_VERSION=2
-sMIN_WEBGL_VERSION=1
-sFULL_ES3
-sFULL_ES2
-sOFFSCREEN_FRAMEBUFFER
-sLLD_REPORT_UNDEFINED
-sWASM_BIGINT
-sENVIRONMENT=web,worker # TODO(luao): Allow web only for now, add `, webview` support in the future.
-sMODULARIZE
-sEXPORT_NAME=createChronosModule
```
From the settings above, I can create and access WebGL context on each thread, but it is impossible to invoke gl calls separately for each thread after making each context current since the main browser thread will always proxy those gl calls on one active context (the most recent context that is made current context in a pthread) in the main browser thread and it doesn't care which context the original calls belong to.
Without discussing SharedGLContext implementation from WebGL specification, is it possible to simply create separate WebGL context for each thread and proxify their gl calls according to each WebGLContext to browser main thread?
Contributor guide
Assessment
This issue has not been assessed yet.