From 26fa8d9d5331b889e95ab7d790d1bde18e2c6456 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 31 Oct 2013 16:48:37 +0100 Subject: [PATCH] Track current program in context, rather than renderers --- src/ol/renderer/webgl/webglmaprenderer.js | 37 ++++++++----------- .../renderer/webgl/webgltilelayerrenderer.js | 2 +- src/ol/webgl/context.js | 23 ++++++++++++ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/ol/renderer/webgl/webglmaprenderer.js b/src/ol/renderer/webgl/webglmaprenderer.js index c1c5561f07..87dd4f886f 100644 --- a/src/ol/renderer/webgl/webglmaprenderer.js +++ b/src/ol/renderer/webgl/webglmaprenderer.js @@ -425,7 +425,6 @@ ol.renderer.webgl.Map.prototype.renderFrame = function(frameState) { context.bindBuffer(goog.webgl.ARRAY_BUFFER, this.arrayBuffer_); - var currentProgram = null; var locations; for (i = 0, ii = layersArray.length; i < ii; ++i) { layer = layersArray[i]; @@ -453,29 +452,26 @@ ol.renderer.webgl.Map.prototype.renderFrame = function(frameState) { } var program = context.getProgram(fragmentShader, vertexShader); - if (program != currentProgram) { - gl.useProgram(program); - currentProgram = program; - - if (useColor) { - if (goog.isNull(this.colorLocations_)) { - locations = - new ol.renderer.webgl.map.shader.Color.Locations(gl, program); - this.colorLocations_ = locations; - } else { - locations = this.colorLocations_; - } + if (useColor) { + if (goog.isNull(this.colorLocations_)) { + locations = + new ol.renderer.webgl.map.shader.Color.Locations(gl, program); + this.colorLocations_ = locations; } else { - if (goog.isNull(this.defaultLocations_)) { - locations = - new ol.renderer.webgl.map.shader.Default.Locations(gl, program); - this.defaultLocations_ = locations; - } else { - locations = this.defaultLocations_; - } + locations = this.colorLocations_; } + } else { + if (goog.isNull(this.defaultLocations_)) { + locations = + new ol.renderer.webgl.map.shader.Default.Locations(gl, program); + this.defaultLocations_ = locations; + } else { + locations = this.defaultLocations_; + } + } + if (context.useProgram(program)) { gl.enableVertexAttribArray(locations.a_position); gl.vertexAttribPointer( locations.a_position, 2, goog.webgl.FLOAT, false, 16, 0); @@ -483,7 +479,6 @@ ol.renderer.webgl.Map.prototype.renderFrame = function(frameState) { gl.vertexAttribPointer( locations.a_texCoord, 2, goog.webgl.FLOAT, false, 16, 8); gl.uniform1i(locations.u_texture, 0); - } layerRenderer = this.getLayerRenderer(layer); diff --git a/src/ol/renderer/webgl/webgltilelayerrenderer.js b/src/ol/renderer/webgl/webgltilelayerrenderer.js index 00e40b9bc7..cf73e018b8 100644 --- a/src/ol/renderer/webgl/webgltilelayerrenderer.js +++ b/src/ol/renderer/webgl/webgltilelayerrenderer.js @@ -175,7 +175,7 @@ ol.renderer.webgl.TileLayer.prototype.renderFrame = gl.disable(goog.webgl.BLEND); var program = context.getProgram(this.fragmentShader_, this.vertexShader_); - gl.useProgram(program); + context.useProgram(program); if (goog.isNull(this.locations_)) { this.locations_ = new ol.renderer.webgl.tilelayer.shader.Locations(gl, program); diff --git a/src/ol/webgl/context.js b/src/ol/webgl/context.js index 404fe4361e..bca86a5dc2 100644 --- a/src/ol/webgl/context.js +++ b/src/ol/webgl/context.js @@ -56,6 +56,12 @@ ol.webgl.Context = function(canvas, gl) { */ this.programCache_ = {}; + /** + * @private + * @type {WebGLProgram} + */ + this.currentProgram_ = null; + goog.events.listen(this.canvas_, ol.webgl.WebGLContextEventType.LOST, this.handleWebGLContextLost, false, this); goog.events.listen(this.canvas_, ol.webgl.WebGLContextEventType.RESTORED, @@ -226,6 +232,7 @@ ol.webgl.Context.prototype.handleWebGLContextLost = function() { goog.object.clear(this.bufferCache_); goog.object.clear(this.shaderCache_); goog.object.clear(this.programCache_); + this.currentProgram_ = null; }; @@ -236,6 +243,22 @@ ol.webgl.Context.prototype.handleWebGLContextRestored = function() { }; +/** + * @param {WebGLProgram} program Program. + * @return {boolean} Changed. + */ +ol.webgl.Context.prototype.useProgram = function(program) { + if (program == this.currentProgram_) { + return false; + } else { + var gl = this.getGL(); + gl.useProgram(program); + this.currentProgram_ = program; + return true; + } +}; + + /** * @private * @type {goog.log.Logger}