Skip to content

Commit

Permalink
Use shader source rewriting to disable attributes
Browse files Browse the repository at this point in the history
fixes #1714
  • Loading branch information
Lucas Wojciechowski committed Apr 6, 2016
1 parent 75dc9f1 commit d60f64a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
30 changes: 26 additions & 4 deletions js/data/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ Bucket.prototype.setAttribPointers = function(programName, gl, program, offset,
if (attribute.isLayerConstant === false && layer.id !== attribute.layerId) continue;

var attributeId = program[attribute.programName];
gl.disableVertexAttribArray(attributeId);
gl['vertexAttrib' + attribute.components + 'fv'](attributeId, attribute.getValue.apply(this, args));
// NOCOMMIT change this to set uniforms instead of attribs
gl['uniform' + attribute.components + 'fv'](attributeId, attribute.getValue.apply(this, args));
}

// Set enabled attributes
Expand Down Expand Up @@ -337,6 +337,26 @@ Bucket.prototype.recalculateStyleLayers = function() {
}
};

Bucket.prototype.getUseProgramTokens = function(programInterface, layer) {
var tokens = {};

var enabledAttributes = this.attributes[programInterface].enabled;
for (var i = 0; i < enabledAttributes.length; i++) {
var enabledAttribute = enabledAttributes[i];
if (enabledAttribute.isLayerConstant !== false && enabledAttribute.layerId !== layer.id) continue;
tokens[enabledAttribute.originalName + 'Type'] = 'attribute';
}

var disabledAttributes = this.attributes[programInterface].disabled;
for (var j = 0; j < this.attributes[programInterface].disabled.length; j++) {
var disabledAttribute = disabledAttributes[j];
if (disabledAttribute.isLayerConstant !== false && disabledAttribute.layerId !== layer.id) continue;
tokens[disabledAttribute.originalName + 'Type'] = 'uniform';
}

return tokens;
};

var createVertexAddMethodCache = {};
function createVertexAddMethod(bucket, interfaceName) {
var enabledAttributes = bucket.attributes[interfaceName].enabled;
Expand Down Expand Up @@ -450,14 +470,16 @@ function createAttributes(bucket) {
name: layer.id + '__' + attribute.name,
programName: 'a_' + attribute.name,
layerId: layer.id,
layerIndex: j
layerIndex: j,
originalName: attribute.name
}));
} else {
interfaceAttributes.enabled.push(util.extend({}, attribute, {
name: layer.id + '__' + attribute.name,
programName: 'a_' + attribute.name,
layerId: layer.id,
layerIndex: j
layerIndex: j,
originalName: attribute.name
}));
}
}
Expand Down
14 changes: 5 additions & 9 deletions js/render/draw_circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ function drawCircles(painter, source, layer, coords) {

var gl = painter.gl;

var program = painter.useProgram('circle', {
colorType: 'attribute'
});

painter.setDepthSublayer(0);
painter.depthMask(false);

Expand All @@ -26,9 +22,6 @@ function drawCircles(painter, source, layer, coords) {
// are inversely related.
var antialias = 1 / browser.devicePixelRatio / layer.paint['circle-radius'];

gl.uniform1f(program.u_blur, Math.max(layer.paint['circle-blur'], antialias));
gl.uniform1f(program.u_size, layer.paint['circle-radius']);

for (var i = 0; i < coords.length; i++) {
var coord = coords[i];

Expand All @@ -38,6 +31,11 @@ function drawCircles(painter, source, layer, coords) {
var elementGroups = bucket.elementGroups.circle;
if (!elementGroups) continue;

var program = painter.useProgram('circle', bucket.getUseProgramTokens('circle', layer));

gl.uniform1f(program.u_blur, Math.max(layer.paint['circle-blur'], antialias));
gl.uniform1f(program.u_size, layer.paint['circle-radius']);

painter.setPosMatrix(painter.translatePosMatrix(
coord.posMatrix,
tile,
Expand All @@ -54,6 +52,4 @@ function drawCircles(painter, source, layer, coords) {
gl.drawElements(gl.TRIANGLES, count, gl.UNSIGNED_SHORT, group.elementOffset);
}
}

gl.enableVertexAttribArray(program.a_color);
}

0 comments on commit d60f64a

Please sign in to comment.