Track current program in context, rather than renderers

This commit is contained in:
Tom Payne
2013-10-31 16:48:37 +01:00
parent 10c41cd064
commit 26fa8d9d53
3 changed files with 40 additions and 22 deletions

View File

@@ -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,10 +452,6 @@ 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_)) {
@@ -476,6 +471,7 @@ ol.renderer.webgl.Map.prototype.renderFrame = function(frameState) {
}
}
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);

View File

@@ -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);

View File

@@ -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}