Skip to content

Commit

Permalink
Prep Attribute class for partial update (#3091)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed May 15, 2019
1 parent 73e167d commit 9ffe6df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 23 deletions.
23 changes: 18 additions & 5 deletions modules/core/src/lib/attribute.js
Expand Up @@ -171,6 +171,11 @@ export default class Attribute extends BaseAttribute {

this.constant = false;
this.value = new ArrayType(this.size * allocCount);

if (this.buffer && this.buffer.byteLength < this.value.byteLength) {
this.buffer.reallocate(this.value.byteLength);
}

state.needsUpdate = true;
state.allocedInstances = allocCount;
return true;
Expand All @@ -186,16 +191,24 @@ export default class Attribute extends BaseAttribute {

const state = this.userData;

const {update} = state;
const {update, noAlloc} = state;

let updated = true;
if (update) {
// Custom updater - typically for non-instanced layers
update.call(context, this, {data, props, numInstances, bufferLayout});
this.update({
value: this.value,
constant: this.constant
});
if (noAlloc || this.constant || !this.buffer) {
// Full update
this.update({
value: this.value,
constant: this.constant
});
} else {
// Only update the changed part of the attribute
this.buffer.subData({
data: this.value.subarray(0, numInstances * this.size)
});
}
this._checkAttributeArray();
} else {
updated = false;
Expand Down
40 changes: 22 additions & 18 deletions modules/core/src/lib/base-attribute.js
Expand Up @@ -76,24 +76,7 @@ export default class BaseAttribute {

// Create buffer if needed
if (!constant && this.gl) {
// Move accessor fields to accessor object
const props = {
...opts,
id: this.id,
target: this.target,
accessor: {
type: this.type
}
};
if (Number.isFinite(props.divisor)) {
props.accessor.divisor = props.divisor;
}
delete props.divisor;
if (Number.isFinite(props.size)) {
props.accessor.size = props.size;
}
delete props.size;
this.buffer = this.buffer || new Buffer(this.gl, props);
this.buffer = this.buffer || this._createBuffer(opts);
this.buffer.setData({data: value});
this.type = this.buffer.accessor.type;
}
Expand All @@ -120,6 +103,27 @@ export default class BaseAttribute {
return null;
}

_createBuffer(opts) {
// Move accessor fields to accessor object
const props = Object.assign({}, opts, {
id: this.id,
target: this.target,
accessor: {
type: this.type
}
});
if (Number.isFinite(props.divisor)) {
props.accessor.divisor = props.divisor;
}
delete props.divisor;
if (Number.isFinite(props.size)) {
props.accessor.size = props.size;
}
delete props.size;

return new Buffer(this.gl, props);
}

// Sets all accessor props except type
// TODO - store on `this.accessor`
_setAccessor(opts) {
Expand Down

0 comments on commit 9ffe6df

Please sign in to comment.