Context sharing and lifetime


Is it possible to share GL contexts among multiple canvas elements on the same page? If so, how - is it done by default?

Also, what approach do you recommond for unit testing where each test uses a new GL context? Currently, I am using the Jasmine framework and have each test (or sometimes group of tests) dynamically create and destroy a canvas element with document.createElement, document.body.appendChild, and document.body.removeChild. Firefox seems to cleanup reasonably well, but I’ve gotten Chrome to choke more than a few times.


Just an update: both Firefox and Chrome are now quite stable with my unit tests creating and destroying hundreds of canvas elements.

I made a small test, like the html code shown below, and different canvases on the same page do not share the same context (I don’t know how they would anyway) and do not share shaders. I assume the same is true for textures and other GL objects that can currently be shared in desktop GL. Perhaps there will be a future WebGL extension that allows context sharing or maybe there will be security concerns, but I don’t know what.

This isn’t a huge concern to me. I was really just curious.

<head><title>Multiple context test</title></head>

<canvas id="canvas0" style="border: none;" width="1" height="1"></canvas>
<canvas id="canvas1" style="border: none;" width="1" height="1"></canvas>

<script type="text/javascript">
    var gl0 = document.getElementById("canvas0").getContext("experimental-webgl");
    var gl1 = document.getElementById("canvas1").getContext("experimental-webgl");

    var s = gl0.createShader(gl0.VERTEX_SHADER);

    document.write(gl0.isShader(s) === gl1.isShader(s));
    document.write(gl0.getParameter(gl0.DEPTH_TEST) === gl1.getParameter(gl1.DEPTH_TEST));