Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(rhi): every WebGL2 frame was thrown away as a device loss

esenginea345afc840a2
diff --git a/src/esengine/renderer/rhi/GLDevice.cpp b/src/esengine/renderer/rhi/GLDevice.cpp
index b7be7ca1b..0b9dca845 100644
--- a/src/esengine/renderer/rhi/GLDevice.cpp
+++ b/src/esengine/renderer/rhi/GLDevice.cpp
@@ -267,7 +267,8 @@ bool GLDevice::recreateDevice() {
#ifdef __EMSCRIPTEN__
// There is no device to build: the browser restores the context on the same
// handle, and until it does there is nothing to do but wait.
- if (emscripten_is_webgl_context_lost(0)) return false;
+ const EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context();
+ if (ctx == 0 || emscripten_is_webgl_context_lost(ctx)) return false;
#endif
// Every GL object the old context held is gone. Layout DESCRIPTORS stay:
@@ -330,7 +331,12 @@ bool GLDevice::pollDeviceLost() {
// Asked directly rather than waited for: a `webglcontextlost` listener
// attached after the loss never fires, and a renderer that missed the event
// would submit into a dead context forever.
- if (emscripten_is_webgl_context_lost(0)) {
+ //
+ // The handle has to be the CURRENT context, not 0. Zero is not "whichever
+ // context is bound" — it is no context, which reports itself lost, and every
+ // frame would then be thrown away as a loss that never happened.
+ const EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context();
+ if (ctx != 0 && emscripten_is_webgl_context_lost(ctx)) {
markDeviceLost(GfxDeviceLostReason::ContextLost,
"WebGL reports the context is lost", "pollDeviceLost");
return true;