Skip to content

Commit

Permalink
Trim buffers after populating
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wojciechowski committed Feb 25, 2016
1 parent 17a653d commit 7700ecd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions js/data/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Bucket.prototype.addFeatures = function() {
for (var i = 0; i < this.features.length; i++) {
this.addFeature(this.features[i]);
}
this.trimBuffers();
};

/**
Expand Down Expand Up @@ -179,6 +180,12 @@ Bucket.prototype.destroy = function(gl) {
}
};

Bucket.prototype.trimBuffers = function() {
for (var bufferName in this.buffers) {
this.buffers[bufferName].trim();
}
};

/**
* Get the name of the method used to add an item to a buffer.
* @param {string} shaderName The name of the shader that will use the buffer
Expand Down
2 changes: 2 additions & 0 deletions js/data/bucket/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ SymbolBucket.prototype.addFeatures = function(collisionTile, stacks, icons) {
}

this.placeFeatures(collisionTile, this.collisionDebug);

this.trimBuffers();
};

SymbolBucket.prototype.addFeature = function(lines, shapedText, shapedIcon) {
Expand Down
12 changes: 9 additions & 3 deletions js/data/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Buffer.prototype.bind = function(gl) {
if (!this.buffer) {
this.buffer = gl.createBuffer();
gl.bindBuffer(type, this.buffer);
gl.bufferData(type, this.arrayBuffer.slice(0, this.length * this.itemSize), gl.STATIC_DRAW);
gl.bufferData(type, this.arrayBuffer, gl.STATIC_DRAW);

// dump array buffer once it's bound to gl
this.arrayBuffer = null;
Expand Down Expand Up @@ -173,12 +173,18 @@ Buffer.prototype.validate = function(args) {
assert(argIndex === args.length);
};

Buffer.prototype.trim = function() {
this.capacity = align(this.itemSize * this.length, Buffer.CAPACITY_ALIGNMENT);
this.arrayBuffer = this.arrayBuffer.slice(0, this.capacity);
this._refreshViews();
};

Buffer.prototype._resize = function(capacity) {
var old = this.views.UNSIGNED_BYTE;
var oldUByteView = this.views.UNSIGNED_BYTE;
this.capacity = align(capacity, Buffer.CAPACITY_ALIGNMENT);
this.arrayBuffer = new ArrayBuffer(this.capacity);
this._refreshViews();
this.views.UNSIGNED_BYTE.set(old);
this.views.UNSIGNED_BYTE.set(oldUByteView);
};

Buffer.prototype._refreshViews = function() {
Expand Down
13 changes: 13 additions & 0 deletions test/js/data/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,18 @@ test('Buffer', function(t) {
t.end();
});

t.test('trims', function(t) {
var buffer = create();
var capacityInitial = buffer.capacity;

buffer.push(1, 1, 1);
t.equal(buffer.capacity, capacityInitial);

buffer.trim();
t.equal(buffer.capacity, buffer.itemSize);

t.end();
});

t.end();
});

0 comments on commit 7700ecd

Please sign in to comment.