Skip to content

Commit

Permalink
Avoid creating empty buffer for attributes (#8576)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Mar 5, 2024
1 parent 3737293 commit bde0ef6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
1 change: 1 addition & 0 deletions modules/core/src/lib/attribute/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export default class Attribute extends DataColumn<AttributeOptions, AttributeInt
// no value was assigned during update
} else if (
this.constant ||
!this.buffer ||
this.buffer.byteLength < (this.value as TypedArray).byteLength + this.byteOffset
) {
this.setData({
Expand Down
9 changes: 3 additions & 6 deletions modules/core/src/lib/attribute/data-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default class DataColumn<Options, State> {
value: NumericArray | null;
doublePrecision: boolean;

protected _buffer: Buffer | null;
protected _buffer: Buffer | null = null;
protected state: DataColumnInternalState<Options, State>;

/* eslint-disable max-statements */
Expand Down Expand Up @@ -190,9 +190,6 @@ export default class DataColumn<Options, State> {
bounds: null,
constant: false
};

// TODO(v9): Can we pre-allocate the correct size, instead?
this._buffer = this._createBuffer(0);
}
/* eslint-enable max-statements */

Expand Down Expand Up @@ -429,7 +426,7 @@ export default class DataColumn<Options, State> {
// A small over allocation is used as safety margin
// Shader attributes may try to access this buffer with bigger offsets
const requiredBufferSize = value.byteLength + byteOffset + stride * 2;
if (buffer.byteLength < requiredBufferSize) {
if (!buffer || buffer.byteLength < requiredBufferSize) {
buffer = this._createBuffer(requiredBufferSize);
}

Expand Down Expand Up @@ -479,7 +476,7 @@ export default class DataColumn<Options, State> {
const {byteOffset} = this;
let {buffer} = this;

if (buffer.byteLength < value.byteLength + byteOffset) {
if (!buffer || buffer.byteLength < value.byteLength + byteOffset) {
buffer = this._createBuffer(value.byteLength + byteOffset);
if (copy && oldValue) {
// Upload the full existing attribute value to the GPU, so that updateBuffer
Expand Down

0 comments on commit bde0ef6

Please sign in to comment.