Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid creating empty buffer for attributes #8576

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading