Skip to content

Commit

Permalink
Add dirtyAttribute for buffer update optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Mar 5, 2017
1 parent ea04c93 commit 3e7a626
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 25 additions & 6 deletions src/StaticGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ define(function (require) {
var vec3Add = vec3.add;
var vec3Set = vec3.set;

function makeAttrKey(attrName) {
return 'attr_' + attrName;
}
/**
* @constructor qtek.StaticGeometry
* @extends qtek.Geometry
Expand Down Expand Up @@ -87,7 +90,10 @@ define(function (require) {
},

dirty: function () {
this._cache.dirtyAll('attributes');
var enabledAttributes = this.getEnabledAttributes();
for (var i = 0; i < enabledAttributes.length; i++) {
this.dirtyAttribute(enabledAttributes[i]);
}
this.dirtyIndices();
this._enabledAttributes = null;
},
Expand All @@ -96,6 +102,11 @@ define(function (require) {
this._cache.dirtyAll('indices');
},

dirtyAttribute: function (attrName) {
this._cache.dirtyAll(makeAttrKey(attrName));
this._cache.dirtyAll('attributes');
},

getTriangleIndices: function (idx, out) {
if (idx < this.triangleCount && idx >= 0) {
if (!out) {
Expand Down Expand Up @@ -202,14 +213,19 @@ define(function (require) {
var isIndicesDirty = cache.isDirty('indices');
if (isAttributesDirty || isIndicesDirty) {
this._updateBuffer(_gl, isAttributesDirty, isIndicesDirty);
var enabledAttributes = this.getEnabledAttributes();
for (var i = 0; i < enabledAttributes.length; i++) {
cache.fresh(makeAttrKey(enabledAttributes[i]));
}
cache.fresh('attributes');
cache.fresh('indices');
}
return cache.get('chunks');
},

_updateBuffer: function (_gl, isAttributesDirty, isIndicesDirty) {
var chunks = this._cache.get('chunks');
var cache = this._cache;
var chunks = cache.get('chunks');
var firstUpdate = false;
if (!chunks) {
chunks = [];
Expand All @@ -218,7 +234,7 @@ define(function (require) {
attributeBuffers: [],
indicesBuffer: null
};
this._cache.put('chunks', chunks);
cache.put('chunks', chunks);
firstUpdate = true;
}

Expand Down Expand Up @@ -252,9 +268,12 @@ define(function (require) {
else {
buffer = _gl.createBuffer();
}
//TODO: Use BufferSubData?
_gl.bindBuffer(_gl.ARRAY_BUFFER, buffer);
_gl.bufferData(_gl.ARRAY_BUFFER, attribute.value, this.hint);
if (cache.isDirty(makeAttrKey(name))) {
// Only update when they are dirty.
// TODO: Use BufferSubData?
_gl.bindBuffer(_gl.ARRAY_BUFFER, buffer);
_gl.bufferData(_gl.ARRAY_BUFFER, attribute.value, this.hint);
}

attributeBuffers[k] = new Geometry.AttributeBuffer(name, attribute.type, buffer, attribute.size, attribute.semantic);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ define(function () {

'use strict';

var DIRTY_PREFIX = '__dirty__';
var DIRTY_PREFIX = '__dt__';

var Cache = function () {

Expand Down

0 comments on commit 3e7a626

Please sign in to comment.