Skip to content

Commit

Permalink
Fix vgl quad's renderer context.
Browse files Browse the repository at this point in the history
Use the proper context for the vgl quads rather than the generic
context.  This makes cleaning up the layer's context more robust.  This
also tries to lose a context as soon as possible, if the browser
supports doing so.
  • Loading branch information
manthey committed Dec 12, 2017
1 parent b211096 commit b1d234c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/gl/quadFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ var gl_quadFeature = function (arg) {
* Build this feature.
*/
this._build = function () {
var mapper, mat, prog, srctex, unicrop, geom;
var mapper, mat, prog, srctex, unicrop, geom, context;

if (!m_this.position()) {
return;
Expand All @@ -174,10 +174,11 @@ var gl_quadFeature = function (arg) {
unicrop = new vgl.uniform(vgl.GL.FLOAT_VEC2, 'crop');
unicrop.set([1.0, 1.0]);
prog.addUniform(unicrop);
context = m_this.renderer()._glContext();
prog.addShader(vgl.getCachedShader(
vgl.GL.VERTEX_SHADER, vgl.GL, vertexShaderImageSource));
vgl.GL.VERTEX_SHADER, context, vertexShaderImageSource));
prog.addShader(vgl.getCachedShader(
vgl.GL.FRAGMENT_SHADER, vgl.GL, fragmentShaderImageSource));
vgl.GL.FRAGMENT_SHADER, context, fragmentShaderImageSource));
mat.addAttribute(prog);
mat.addAttribute(new vgl.blend());
/* This is similar to vgl.planeSource */
Expand Down Expand Up @@ -214,9 +215,10 @@ var gl_quadFeature = function (arg) {
prog.addUniform(new vgl.projectionUniform('projectionMatrix'));
prog.addUniform(new vgl.floatUniform('opacity', 1.0));
prog.addUniform(new vgl.uniform(vgl.GL.FLOAT_VEC3, 'vertexColor'));
context = m_this.renderer()._glContext();
prog.addShader(vgl.getCachedShader(
vgl.GL.VERTEX_SHADER, vgl.GL, vertexShaderColorSource));
prog.addShader(vgl.utils.createFragmentShader(vgl.GL));
vgl.GL.VERTEX_SHADER, context, vertexShaderColorSource));
prog.addShader(vgl.utils.createFragmentShader(context));
mat.addAttribute(prog);
mat.addAttribute(new vgl.blend());
/* This is similar to vgl.planeSource */
Expand Down
20 changes: 19 additions & 1 deletion src/gl/vglRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ var vglRenderer = function (arg) {
}
};

/**
* Get the GL context for this renderer.
*
* @returns {WebGLRenderingContext} The current context. If unavailable,
* falls back to the vgl generic context.
*/
this._glContext = function () {
if (m_viewer && m_viewer.renderWindow()) {
return m_viewer.renderWindow().context();
}
return vgl.GL;
};

/**
* Exit.
*/
Expand All @@ -187,9 +200,14 @@ var vglRenderer = function (arg) {
if (m_viewer) {
var renderState = new vgl.renderState();
renderState.m_renderer = m_viewer;
renderState.m_context = m_viewer.renderWindow().context();
renderState.m_context = this._glContext();
m_viewer.exit(renderState);
if (this._glContext() !== vgl.GL && this._glContext().getExtension('WEBGL_lose_context') && this._glContext().getExtension('WEBGL_lose_context').loseContext) {
this._glContext().getExtension('WEBGL_lose_context').loseContext();
}
}
// make sure we clear shaders associated with the generate context, too
vgl.clearCachedShaders(vgl.GL);
m_viewer = null;
s_exit();
};
Expand Down

0 comments on commit b1d234c

Please sign in to comment.