Skip to content

Commit

Permalink
move setVertexAttribPointers to Buffer
Browse files Browse the repository at this point in the history
since it no longer needs access to anything in Bucket

Set attribute pointers using info from the array type.
  • Loading branch information
ansis authored and Lucas Wojciechowski committed Apr 12, 2016
1 parent 0b17310 commit 31dc4ac
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 78 deletions.
86 changes: 18 additions & 68 deletions js/data/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Bucket.prototype.createArrays = function() {
var vertexBufferName = this.getBufferName(programName, 'vertex');

var VertexArrayType = new StructArrayType({
members: this.attributes[programName].coreAttributes,
members: this.attributes[programName].layoutAttributes,
alignment: Buffer.VERTEX_ATTRIBUTE_ALIGNMENT
});

Expand Down Expand Up @@ -201,80 +201,28 @@ Bucket.prototype.trimArrays = function() {
}
};

/**
* @enum {string} AttributeType
* @private
* @readonly
*/
var AttributeType = {
Int8: 'BYTE',
Uint8: 'UNSIGNED_BYTE',
Int16: 'SHORT',
Uint16: 'UNSIGNED_SHORT'
};

/**
* Set the attribute pointers in a WebGL context
* @private
* @param gl The WebGL context
* @param program The active WebGL program
* @param {number} offset The offset of the attribute data in the currently bound GL buffer.
* @param {Array} arguments to be passed to disabled attribute value functions
*/
Bucket.prototype.setAttribPointers = function(programName, gl, program, offset, layer, globalProperties, bindPaint) {
var attribute;

if (bindPaint) {
// Set disabled attributes
var disabledAttributes = this.attributes[programName].paintAttributes[layer.id].disabled;
for (var i = 0; i < disabledAttributes.length; i++) {
attribute = disabledAttributes[i];
var attributeId = program[attribute.name];
gl['uniform' + attribute.components + 'fv'](attributeId, attribute.getValue(layer, globalProperties));
}
}

// Set enabled attributes
var enabledAttributes = bindPaint ?
this.attributes[programName].paintAttributes[layer.id].enabled :
this.attributes[programName].coreAttributes;

var vertexBuffer = bindPaint ?
this.buffers[this.getBufferName(layer.id, programName)] :
this.buffers[this.getBufferName(programName, 'vertex')];

if (bindPaint) {
// TODO remove
var bytesPerElement = this.buffers[this.getBufferName(programName, 'vertex')].itemSize;
offset = offset / bytesPerElement * vertexBuffer.itemSize;
}

for (var j = 0; j < enabledAttributes.length; j++) {
attribute = enabledAttributes[j];
if (!getMember(attribute.name)) continue;

gl.vertexAttribPointer(
program[attribute.name],
attribute.components,
gl[AttributeType[attribute.type]],
false,
vertexBuffer.arrayType.bytesPerElement,
offset + getMember(attribute.name).offset
);
}
Bucket.prototype.setAttribPointers = function(programName, gl, program, offset) {
var vertexBuffer = this.buffers[this.getBufferName(programName, 'vertex')];
vertexBuffer.setVertexAttribPointers(gl, program, offset / vertexBuffer.itemSize);
};

function getMember(memberName) {
var members = vertexBuffer.arrayType.members;
for (var k = 0; k < members.length; k++) {
var member = members[k];
if (member.name === memberName) {
return member;
}
}
Bucket.prototype.setUniforms = function(gl, programName, program, layer, globalProperties) {
var disabledAttributes = this.attributes[programName].paintAttributes[layer.id].disabled;
for (var i = 0; i < disabledAttributes.length; i++) {
var attribute = disabledAttributes[i];
var attributeId = program[attribute.name];
gl['uniform' + attribute.components + 'fv'](attributeId, attribute.getValue(layer, globalProperties));
}
};

Bucket.prototype.bindBuffers = function(programInterfaceName, gl, options) {
Bucket.prototype.bindLayoutBuffers = function(programInterfaceName, gl, options) {
var programInterface = this.programInterfaces[programInterfaceName];

if (programInterface.vertexBuffer) {
Expand All @@ -293,8 +241,10 @@ Bucket.prototype.bindBuffers = function(programInterfaceName, gl, options) {
}
};

Bucket.prototype.bindPaintBuffer = function(programInterfaceName, gl, layerID) {
this.buffers[this.getBufferName(layerID, programInterfaceName)].bind(gl);
Bucket.prototype.bindPaintBuffer = function(gl, interfaceName, layerID, program, vertexStartIndex) {
var buffer = this.buffers[this.getBufferName(layerID, interfaceName)];
buffer.bind(gl);
buffer.setVertexAttribPointers(gl, program, vertexStartIndex);
};

/**
Expand Down Expand Up @@ -389,7 +339,7 @@ function capitalize(string) {
function createAttributes(bucket) {
var attributes = {};
for (var interfaceName in bucket.programInterfaces) {
var interfaceAttributes = attributes[interfaceName] = { coreAttributes: [], paintAttributes: {} };
var interfaceAttributes = attributes[interfaceName] = { layoutAttributes: [], paintAttributes: {} };
var layerPaintAttributes = interfaceAttributes.paintAttributes;

for (var c = 0; c < bucket.childLayers.length; c++) {
Expand All @@ -402,7 +352,7 @@ function createAttributes(bucket) {
var attribute = interface_.attributes[i];

if (attribute.paintProperty === undefined) {
interfaceAttributes.coreAttributes.push(attribute);
interfaceAttributes.layoutAttributes.push(attribute);
} else {
for (var j = 0; j < bucket.childLayers.length; j++) {
var layer = bucket.childLayers[j];
Expand Down
38 changes: 38 additions & 0 deletions js/data/buffer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var assert = require('assert');

module.exports = Buffer;

/**
Expand Down Expand Up @@ -41,6 +43,42 @@ Buffer.prototype.bind = function(gl) {
}
};

/**
* @enum {string} AttributeType
* @private
* @readonly
*/
var AttributeType = {
Int8: 'BYTE',
Uint8: 'UNSIGNED_BYTE',
Int16: 'SHORT',
Uint16: 'UNSIGNED_SHORT'
};

/**
* Set the attribute pointers in a WebGL context
* @private
* @param gl The WebGL context
* @param program The active WebGL program
* @param {number} offset The index offset of the attribute data in the currently bound GL buffer.
*/
Buffer.prototype.setVertexAttribPointers = function(gl, program, offset) {
for (var j = 0; j < this.attributes.length; j++) {
var member = this.attributes[j];
var attribIndex = program[member.name];
assert(attribIndex !== undefined, 'array member "' + member.name + '" name does not match shader attribute name');

gl.vertexAttribPointer(
attribIndex,
member.components,
gl[AttributeType[member.type]],
false,
this.arrayType.bytesPerElement,
offset * this.arrayType.bytesPerElement + member.offset
);
}
};

/**
* Destroy the GL buffer bound to the given WebGL context
* @private
Expand Down
8 changes: 4 additions & 4 deletions js/render/draw_circle.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ function drawCircles(painter, source, layer, coords) {
for (var k = 0; k < elementGroups.length; k++) {
var group = elementGroups[k];
var count = group.elementLength * 3;
bucket.bindBuffers('circle', gl);
bucket.setAttribPointers('circle', gl, program, group.vertexOffset, layer, {zoom: painter.transform.zoom});
bucket.bindPaintBuffer('circle', gl, layer.id);
bucket.setAttribPointers('circle', gl, program, group.vertexOffset, layer, {zoom: painter.transform.zoom}, true);
bucket.bindLayoutBuffers('circle', gl);
bucket.setAttribPointers('circle', gl, program, group.vertexOffset);
bucket.bindPaintBuffer(gl, 'circle', layer.id, program, group.vertexStartIndex);
bucket.setUniforms(gl, 'circle', program, layer, {zoom: painter.transform.zoom});
gl.drawElements(gl.TRIANGLES, count, gl.UNSIGNED_SHORT, group.elementOffset);
}
}
Expand Down
2 changes: 1 addition & 1 deletion js/render/draw_collision_debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function drawCollisionDebug(painter, source, layer, coords) {
if (!bucket.buffers) continue;
if (elementGroups[0].vertexLength === 0) continue;

bucket.bindBuffers('collisionBox', gl);
bucket.bindLayoutBuffers('collisionBox', gl);
bucket.setAttribPointers('collisionBox', gl, program, elementGroups[0].vertexOffset, layer);

painter.setPosMatrix(coord.posMatrix);
Expand Down
4 changes: 2 additions & 2 deletions js/render/draw_fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function drawFill(painter, source, layer, coord) {
var fillProgram = painter.useProgram('fill');
painter.setPosMatrix(translatedPosMatrix);

bucket.bindBuffers('fill', gl);
bucket.bindLayoutBuffers('fill', gl);

for (var i = 0; i < elementGroups.length; i++) {
var group = elementGroups[i];
Expand Down Expand Up @@ -183,7 +183,7 @@ function drawStroke(painter, source, layer, coord) {
if (image) { setPattern(image, opacity, tile, coord, painter, program); }

// Draw all buffers
bucket.bindBuffers('fill', gl, {secondElement: true});
bucket.bindLayoutBuffers('fill', gl, {secondElement: true});

painter.enableTileClippingMask(coord);

Expand Down
2 changes: 1 addition & 1 deletion js/render/draw_line.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ module.exports = function drawLine(painter, source, layer, coords) {
gl.uniform1f(program.u_ratio, ratio);
}

bucket.bindBuffers('line', gl);
bucket.bindLayoutBuffers('line', gl);

for (var i = 0; i < elementGroups.length; i++) {
var group = elementGroups[i];
Expand Down
4 changes: 2 additions & 2 deletions js/render/draw_symbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function drawSymbol(painter, layer, posMatrix, tile, bucket, elementGroups, pref

var group, count;

bucket.bindBuffers(programInterfaceName, gl);
bucket.bindLayoutBuffers(programInterfaceName, gl);

if (sdf) {
var sdfPx = 8;
Expand Down Expand Up @@ -179,7 +179,7 @@ function drawSymbol(painter, layer, posMatrix, tile, bucket, elementGroups, pref

for (var i = 0; i < elementGroups.length; i++) {
group = elementGroups[i];
bucket.bindBuffers(programInterfaceName, gl);
bucket.bindLayoutBuffers(programInterfaceName, gl);
bucket.setAttribPointers(programInterfaceName, gl, program, group.vertexOffset, layer);

count = group.elementLength * 3;
Expand Down

0 comments on commit 31dc4ac

Please sign in to comment.