emscripten-core / emscripten-core/emscripten
Registered lose/restored webgl context callbacks never called
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.6 (1ddaae4d2d6dfbb678ecc193bc988820d1fc4633)
clang version 21.0.0git (https:/github.com/llvm/llvm-project 4775e6d9099467df9363e1a3cd5950cc3d2fde05)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\git-kf\emsdk\upstream\bin
**Full link command and output with `-v` appended:**
```
em++.bat -pthread -fexceptions -O3 -DNDEBUG -lembind --no-heap-copy --preload-file=assets.zip -pthread -fexceptions -sSTRICT=1 -sPTHREADS_DEBUG -sRUNTIME_DEBUG -sALLOW_MEMORY_GROWTH=1 -sABORTING_MALLOC=0 -sASSERTIONS=0 -g2 -sEXPORT_ALL=1
-sAUTO_JS_LIBRARIES=1 -sAUTO_NATIVE_LIBRARIES=1 -sFETCH=1 -sWASM=1 -sPTHREAD_POOL_SIZE=10 -sEXPORTED_RUNTIME_METHODS=ccall,UTF8ToString,HEAPU32,HEAPF32
-sINCOMING_MODULE_JS_API=locateFile,preRun,print,printErr,wasmMemory,buffer,instantiateWasm,wasm,onAbort -sAUDIO_WORKLET=1 -sWASM_WORKERS=1 -sASYNCIFY -sOFFSCREEN_FRAMEBUFFER=1 -sOFFSCREENCANVAS_SUPPORT=1
-sTEXTDECODER=0 -s USE_WEBGL2=1 -sMIN_WEBGL_VERSION=1 -sMAX_WEBGL_VERSION=2
```
Hello! I'm trying to detect WebGL context lost / restored events, but the registered callbacks on the emscripten side are never trigger.
However in the console I have two warnings:
```
WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost
```
The code looks like:
```
// called from js
pthread_t thread
pthread_attr_t threadAttributes;
pthread_attr_init(&threadAttributes);
emscripten_pthread_attr_settransferredcanvases(&threadAttributes, "mycanvas");
int success = pthread_create(&thread, &threadAttributes, _render_thread_entry, (void*)context);
if (success != 0)
{
// error !
}
pthread_attr_destroy(&threadAttributes);
```
```
// _render_thread_entry
EmscriptenWebGLContextAttributes contextAttributes;
emscripten_webgl_init_context_attributes(&contextAttributes);
contextAttributes.majorVersion = 1;
contextAttributes.minorVersion = 0;
contextAttributes.explicitSwapControl = EM_TRUE;
contextAttributes.alpha = true;
contextAttributes.premultipliedAlpha = false;
contextAttributes.depth = true;
contextAttributes.stencil = true;
contextAttributes.enableExtensionsByDefault = true;
contextAttributes.antialias = false;
contextAttributes.preserveDrawingBuffer = true;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE glContext = emscripten_webgl_create_context("mycanvas", &contextAttributes);
EMSCRIPTEN_RESULT resultContextLost = emscripten_set_webglcontextlost_callback(
"mycanvas", nullptr, false, _render_thread_webgl_context_lost);
if (resultContextLost != EMSCRIPTEN_RESULT_SUCCESS)
{
std::cerr << "Cannot register to web glcontext lost callback" << resultContextLost << std::endl;
}
EMSCRIPTEN_RESULT resultContextRestored = emscripten_set_webglcontextrestored_callback(
"mycanvas", nullptr, false, _render_thread_webgl_context_restored);
if (resultContextRestored != EMSCRIPTEN_RESULT_SUCCESS)
{
std::cerr << "Cannot register to web glcontext restored callback" << resultContextLost << std::endl;
}
```
```
bool _render_thread_webgl_context_lost(int eventType, const void* reserved, void* userData)
{
// Never called
}
bool _render_thread_webgl_context_restored(int eventType, const void* reserved, void* userData)
{
// Never called
}
```
But if I check like this, it works
```
if (emscripten_is_webgl_context_lost(glContext))
{
std::cerr << "context_lost" << std::endl;
}
```
Thanks!
Contributor guide
Assessment
This issue has not been assessed yet.