diff --git a/js/data/bucket.js b/js/data/bucket.js index c58db549931..d17ff666ed7 100644 --- a/js/data/bucket.js +++ b/js/data/bucket.js @@ -4,6 +4,7 @@ var featureFilter = require('feature-filter'); var Buffer = require('./buffer'); var StyleLayer = require('../style/style_layer'); var util = require('../util/util'); +var assert = require('assert'); module.exports = Bucket; @@ -232,7 +233,12 @@ Bucket.prototype.setAttribPointers = function(shaderName, gl, shader, offset, ar } // Set enabled attributes - this.buffers[this.getBufferName(shaderName, 'vertex')].setAttribPointers(gl, shader, offset); + var enabledAttributes = this.getAttributes(shaderName, {isEnabled: true}); + var vertexBuffer = this.buffers[this.getBufferName(shaderName, 'vertex')]; + assert(enabledAttributes.length === vertexBuffer.attributes.length); + vertexBuffer.setAttribPointers(gl, shader, offset, util.mapObjectKV(enabledAttributes, function(attribute) { + return [attribute.name, 'a_' + attribute.name]; + })); }; /** diff --git a/js/data/buffer.js b/js/data/buffer.js index 7f8d26364ab..8fa7d04bc31 100644 --- a/js/data/buffer.js +++ b/js/data/buffer.js @@ -123,13 +123,21 @@ Buffer.prototype.destroy = function(gl) { * @param shader The active WebGL shader * @param {number} offset The offset of the attribute data in the currently bound GL buffer. */ -Buffer.prototype.setAttribPointers = function(gl, shader, offset) { - for (var i = 0; i < this.attributes.length; i++) { - var attrib = this.attributes[i]; +Buffer.prototype.setAttribPointers = function(gl, shader, offset, attribs) { + for (var bufferAttribName in attribs) { + var shaderAttribName = attribs[bufferAttribName]; + var attrib = this.attributes.find(function(attribute) { + return attribute.name === bufferAttribName; + }); gl.vertexAttribPointer( - shader['a_' + attrib.name], attrib.components, gl[attrib.type.name], - false, this.itemSize, offset + attrib.offset); + shader[shaderName], + attrib.components, + gl[attrib.type.name], + false, + this.itemSize, + offset + attrib.offset + ); } }; diff --git a/js/render/draw_fill.js b/js/render/draw_fill.js index 63b46b29029..7c227ca8660 100644 --- a/js/render/draw_fill.js +++ b/js/render/draw_fill.js @@ -105,13 +105,15 @@ function drawFill(painter, source, layer, coord) { for (var i = 0; i < elementGroups.length; i++) { var group = elementGroups[i]; var offset = group.vertexStartIndex * vertex.itemSize; - vertex.setAttribPointers(gl, painter.fillShader, offset); + bucket.setAttribPointers('fill', gl, shader, offset, [{$zoom: painter.transform.zoom}]); var count = group.elementLength * 3; var elementOffset = group.elementStartIndex * elements.itemSize; gl.drawElements(gl.TRIANGLES, count, gl.UNSIGNED_SHORT, elementOffset); } + bucket.unsetAttribPointers('fill', gl, shader); + // Now that we have the stencil mask in the stencil buffer, we can start // writing to the color buffer. gl.colorMask(true, true, true, true); diff --git a/js/render/draw_symbol.js b/js/render/draw_symbol.js index 0cfbea8b1c8..e5552d6b68e 100644 --- a/js/render/draw_symbol.js +++ b/js/render/draw_symbol.js @@ -99,7 +99,7 @@ function drawSymbol(painter, layer, posMatrix, tile, bucket, elementGroups, pref var extra = (topedgelength + x) / topedgelength - 1; var text = prefix === 'text'; - var shader, vertex, elements, texsize; + var shader, shaderName, vertex, elements, texsize; if (!text && !painter.style.sprite.loaded()) return; @@ -190,7 +190,7 @@ function drawSymbol(painter, layer, posMatrix, tile, bucket, elementGroups, pref group = elementGroups[i]; offset = group.vertexStartIndex * vertex.itemSize; vertex.bind(gl); - vertex.setAttribPointers(gl, shader, offset); + bucket.setAttribPointers('symbol', gl, shader, offset); count = group.elementLength * 3; elementOffset = group.elementStartIndex * elements.itemSize; @@ -203,7 +203,7 @@ function drawSymbol(painter, layer, posMatrix, tile, bucket, elementGroups, pref group = elementGroups[k]; offset = group.vertexStartIndex * vertex.itemSize; vertex.bind(gl); - vertex.setAttribPointers(gl, shader, offset); + bucket.setAttribPointers('symbol', gl, shader, offset); count = group.elementLength * 3; elementOffset = group.elementStartIndex * elements.itemSize; diff --git a/js/util/util.js b/js/util/util.js index 6577ea5cf83..e5df0f2aff8 100644 --- a/js/util/util.js +++ b/js/util/util.js @@ -430,6 +430,15 @@ exports.mapObject = function(input, iterator, context) { return output; }; +exports.mapObjectKV = function(input, iterator, context) { + var output = {}; + for (var key in input) { + var pair = iterator.call(context || this, input[key], key, input); + output[pair[0]] = pair[1]; + } + return output; +}; + /** * Create an object by filtering out values of an existing object * @param {Object} input