Skip to content

Commit

Permalink
Check whether context is lost before gl.shaderSource(...). (#9017)
Browse files Browse the repository at this point in the history
When context is lost most gl calls are no-ops but this one can throw.
When a program cannot be created `draw(...)` needs to fail silently just
like all gl calls.

fix #8986
  • Loading branch information
ansis authored Nov 25, 2019
1 parent 1215bf1 commit efc0caa
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/render/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Program<Us: UniformBindings> {
numAttributes: number;
fixedUniforms: Us;
binderUniforms: Array<BinderUniform>;
failedToCreate: boolean;

constructor(context: Context,
source: {fragmentSource: string, vertexSource: string},
Expand All @@ -44,12 +45,20 @@ class Program<Us: UniformBindings> {
const fragmentSource = defines.concat(prelude.fragmentSource, source.fragmentSource).join('\n');
const vertexSource = defines.concat(prelude.vertexSource, source.vertexSource).join('\n');
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
if (gl.isContextLost()) {
this.failedToCreate = true;
return;
}
gl.shaderSource(fragmentShader, fragmentSource);
gl.compileShader(fragmentShader);
assert(gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS), (gl.getShaderInfoLog(fragmentShader): any));
gl.attachShader(this.program, fragmentShader);

const vertexShader = gl.createShader(gl.VERTEX_SHADER);
if (gl.isContextLost()) {
this.failedToCreate = true;
return;
}
gl.shaderSource(vertexShader, vertexSource);
gl.compileShader(vertexShader);
assert(gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS), (gl.getShaderInfoLog(vertexShader): any));
Expand Down Expand Up @@ -110,6 +119,8 @@ class Program<Us: UniformBindings> {

const gl = context.gl;

if (this.failedToCreate) return;

context.program.set(this.program);
context.setDepthMode(depthMode);
context.setStencilMode(stencilMode);
Expand Down

0 comments on commit efc0caa

Please sign in to comment.