When can a (WebGL) context be lost


I am trying to change my code to be able to recover from lost contexts.

However, it’s a PITA to change the code everytime the context is used.

My question : can the context be lost in the MIDDLE of a frame rendering, or can I assume that once the context is successfuly made current, It will not be lost unless the next webglcontextlost event?

That is, can I simply do this :

  • set a flag that ensures webGL is not used after a webglcontextlost event
  • inside the webglcontextrestored handler, recreate all the shaders, … and clear the above flag.

The thing is, I am afraid that the context could get lost inside my event loop. “Ruggedizing” my code against these random context losses would be is a much bigger challenge!

Thanks for your help

You can read about “context lost” in this book WebGL Programming Guide

This is an example from the book: ch10/RotatingTriangle_contextLost: ch10/RotatingTriangle_contextLost - JSFiddle - Code Playground

All examples from the book here: Examples from "WebGL Programming Guide" on JSFiddle