[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Public WebGL] Lost Context



There was some discussion today about the current specification of how WebGL handles a lost context.

It seems like if you lose a context there is no real recovery and that you really need to start with a fresh context.

So, as a suggestion, what if the spec changed from this

interface WebGLResourceLostEvent : Event {
readonly attribute WebGLObject resource;
readonly attribute WebGLRenderingContext context;

void initWebGLResourceLostEvent(in DOMString type,
in boolean canBubble,
in boolean cancelable,
in WebGLObject resource,
in WebGLRenderingContext context);
};
to this

interface WebGLResourceLostEvent : Event {
readonly attribute WebGLRenderingContext context;

void initWebGLResourceLostEvent(in DOMString type,
in boolean canBubble,
in boolean cancelable,
in WebGLRenderingContext context);
};

Removing the "resource" argument, and that if you get one of these events the context in question is no longer valid. Every call to anything in that context from that point on will have no effect and context.getError will constantly return context.LOST_CONTEXT

To recover you have to create a new context by calling canvas.getContext("webgl")

As for an example of why it seems like this is necessary, imagine the following code.

var textures = [];
for (var ii = 0; ii < 100; ++ii) {
   var tex = ctx.createTexture();
   textures[ii] = tex;
   ctx.bindTexture(ctx.TEXTURE_2D, tex);
   ctx.texImage2D(ctx.TEXTURE_2D, 0, someCollectionOfImgTags[ii]);
}

Now imagine as this code is half way finished executing the context is lost (user pressed sleep on his phone). When the system comes back, if the code can continue it will end up in this strange state of the first set of resources gone and the second set of resources valid and no really easy to program around it.