I want to draw on canvases (more than 100) and have their content preserved. My setup is that I create canvases in a loop, and for each one I get the webgl2
context of it and do some drawing. The problem is, that after 16 canvases, when i get the webgl2
context from the 17th, the first one shows a missing image logo
I first thought that I should gracefully free up and release the context, but after googling a while, my only luck was to find some hack to call loseContext()
on an extension, which ended in making the canvas look like the one in the image right after the call.
I also tried deleting all the resources used in the context (like buffers, programs and etc.) but no luck there either.
So, how do I achieve that? Is it possible at all?
2
Answers
Based on WebGL Fundamentals most browsers support only 8 webgl contexts simultaneously.
Chromium had a registered request to make a number of contexts adjustable. They also stated that it is not easy to make this value configurable because the actual contexts are tightly connected with hardware and, if exceeded, can make the video driver stop.
However, the new config value
--max-active-webgl-contexts=32
was introduced, and the official default value for Chromium is 16.There is also a project that allows virtualizing WebGL contexts, but it is not clear what impact its usage may have.
To summarize:
Please let me know if this helps.
The simple answer is that each browser limits the amount of active (webgl) contexts. As you found out yourself, with Chromium-based browsers the limit is 16, while in Firefox it’s 300 for example.
If you exceed this limit, the browser will start throwing away the oldest contexts, what means that it frees up all resources related to that particular context, including the backbuffer used to store it’s pixeldata. On Chrome you’ll get a warning like:
Unfortunately the amount of contexts can’t be changed. What you can do depends on your definition of
preserving content
. If it means you want to backup the contents of it’s backbuffer, you can workaround like this:canvases=[]
canvases
arraywebgl
canvas usingdrawImage()
WEBGL_lose_context
extension, get rid of the old webgl contextwebglcontextlost
event, so you know it’s save to create a new webgl contextHere’s an example of generating 64 webgl contexts in the aforementioned way: