Skip to content

Commit

Permalink
Removed Buffer2 shim and serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Sep 23, 2015
1 parent a9a9010 commit 0c01274
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 99 deletions.
35 changes: 16 additions & 19 deletions js/data/buffer/triangle_element_buffer.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
'use strict';

var util = require('../../util/util');
var Buffer = require('./buffer');

module.exports = TriangleElementBuffer;

function TriangleElementBuffer(buffer) {
Buffer.call(this, buffer);
var Buffer2 = require('../buffer2');

function TriangleElementBuffer(options) {
Buffer2.call(this, options || {
type: Buffer2.BufferType.ELEMENT,
attributes: {
verticies: {
components: 3,
type: Buffer2.AttributeType.UNSIGNED_SHORT
}
}
});
}

TriangleElementBuffer.prototype = util.inherit(Buffer, {
itemSize: 6, // bytes per triangle (3 * unsigned short == 6 bytes)
arrayType: 'ELEMENT_ARRAY_BUFFER',

TriangleElementBuffer.prototype = util.inherit(Buffer2, {
add: function(a, b, c) {
var pos2 = this.pos / 2;

this.resize();

this.ushorts[pos2 + 0] = a;
this.ushorts[pos2 + 1] = b;
this.ushorts[pos2 + 2] = c;

this.pos += this.itemSize;
this.push([a, b, c]);
}
});

module.exports = TriangleElementBuffer;
83 changes: 30 additions & 53 deletions js/data/buffer2.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ var util = require('../util/util');
* @param {Object.<string, BufferAttribute>} options.attributes
*/
function Buffer(options) {
util.assert(options.type);
this.type = options.type;

// Create a new Buffer
if (!options.isSerializedMapboxBuffer) {
if (options.arrayBuffer) {

this.arrayBuffer = options.arrayBuffer;
this.size = options.size;
this.length = options.length;
this.attributes = options.attributes;
this.itemSize = options.itemSize;

this._refreshArrayBufferViews();

} else {

this.type = options.type;
this.size = align(Buffer.SIZE_DEFAULT, Buffer.SIZE_ALIGNMENT);
this.length = 0;
this.arrayBuffer = new ArrayBuffer(this.size);
Expand All @@ -35,12 +45,7 @@ function Buffer(options) {

// Vertex buffer attributes must be aligned to "word" boundaries (4 bytes) but element
// buffer attributes do not need to be aligned.
var attributeAlignment;
if (this.type === Buffer.BufferType.VERTEX) {
attributeAlignment = Buffer.VERTEX_ATTRIBUTE_OFFSET_ALIGNMENT;
} else {
attributeAlignment = null;
}
var attributeAlignment = this.type === Buffer.BufferType.VERTEX ? Buffer.VERTEX_ATTRIBUTE_OFFSET_ALIGNMENT : null;

// Normalize the attributes
for (var key in options.attributes) {
Expand All @@ -56,23 +61,8 @@ function Buffer(options) {

this.attributes[attribute.name] = attribute;
}

// Restore a serialized buffer
} else {
var clone = options;

this.type = clone.type;
this.size = clone.size;
this.length = clone.index;
this.arrayBuffer = clone.arrayBuffer;
this.attributes = clone.attributes;
this.itemSize = clone.itemSize;

this._refreshArrayBufferViews();
}

util.assert(this.type);

// Enable some shortcuts if there is only one attribute on this buffer.
var attributeNames = Object.keys(this.attributes);
if (attributeNames.length === 1) {
Expand All @@ -82,7 +72,21 @@ function Buffer(options) {
}
}

Buffer.prototype.isMapboxBuffer = true;
// Start Buffer1 compatability code
var Buffer1 = require('./buffer/buffer');
Buffer.prototype = {
get pos() { return this.length * this.itemSize; },
get array() { return this.arrayBuffer; },
get arrayType() { return this.type; },
bind: Buffer1.prototype.bind,
destroy: Buffer1.prototype.destroy,
setupViews: function() { },
set array(value) {
util.assert(value === null);
this.arrayBuffer = null;
}
};
// End Buffer1 compatability code

/**
* Push an item onto the end of the buffer. Grows the buffer if necessary.
Expand Down Expand Up @@ -167,33 +171,6 @@ Buffer.prototype.get = function(index) {
return item;
};

/**
* Serialize the buffer to be transferred between threads via `postMessage`. This is a destructive
* operation because it is assumed that the ownership of the buffer will be transferred to another
* thread.
* @private
* @returns { serialized: Object, transferables: Array.<ArrayBuffer> }
*/
Buffer.prototype.serialize = function() {
var output = {
serialized: {
type: this.type,
attributes: this.attributes,
itemSize: this.itemSize,
size: this.size,
index: this.length,
arrayBuffer: this.arrayBuffer,
isSerializedMapboxBuffer: true
},
transferables: [this.arrayBuffer]
};

this.arrayBuffer = null;
this.arrayBufferViews = null;

return output;
};

/**
* Get the byte offset of a particular index.
* @private
Expand Down Expand Up @@ -277,7 +254,7 @@ Buffer.prototype._refreshArrayBufferViews = function() {
*/
Buffer.BufferType = {
VERTEX: 'ARRAY_BUFFER',
ELEMENT: 'ELEMENT_ARRAY_BUFFER'
ELEMENT: 'ELEMENT_ARRAY_BUFFER'
};

/**
Expand Down
3 changes: 1 addition & 2 deletions test/js/buffer/triangle_element_buffer.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

var test = require('prova');
var Buffer = require('../../../js/data/buffer/buffer');
var TriangleElementBuffer = require('../../../js/data/buffer/triangle_element_buffer');

test('TriangleElementBuffer', function(t) {
var buf = new Buffer();
var buf = new TriangleElementBuffer();
t.ok(new TriangleElementBuffer(buf), 'default buffer');
var triangleElems = new TriangleElementBuffer(buf);
triangleElems.setupViews();
Expand Down
25 changes: 0 additions & 25 deletions test/js/data/buffer2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ test('Buffer2', function(t) {
t.equal(buffer.itemSize, 8);
t.ok(buffer.arrayBuffer);
t.notOk(buffer.singleAttribute);
t.ok(buffer.isMapboxBuffer);

t.equal(buffer.attributes.map.name, 'map');
t.equal(buffer.attributes.map.components, 1);
Expand All @@ -51,30 +50,6 @@ test('Buffer2', function(t) {
t.end();
});

t.test('serializes and unserializes', function(t) {
var bufferReference = create();
bufferReference.push({map: [1], box: [7, 3]});

var bufferInput = create();
bufferInput.push({map: [1], box: [7, 3]});
var bufferInputSerialized = bufferInput.serialize();

t.notOk(bufferInput.arrayBuffer);
t.notOk(bufferInput.arrayBufferViews);

var bufferOutput = new Buffer(bufferInputSerialized.serialized);

t.equal(bufferOutput.type, bufferReference.type);
t.equal(bufferOutput.size, bufferReference.size);
t.equal(bufferOutput.length, bufferReference.length);
t.equal(bufferOutput.itemSize, bufferReference.itemSize);
t.equal(bufferOutput.singleAttribute, bufferReference.singleAttribute);
t.deepEqual(bufferOutput.attributes, bufferReference.attributes);
t.deepEqual(bufferOutput.get(0), {map: [1], box: [7, 3]});

t.end();
});

t.test('pushes items', function(t) {
var buffer = create();

Expand Down

0 comments on commit 0c01274

Please sign in to comment.