From bde0ef6b161c6d304cc793cdb5c7d511bc4f6765 Mon Sep 17 00:00:00 2001 From: Xiaoji Chen Date: Tue, 5 Mar 2024 10:28:43 -0800 Subject: [PATCH] Avoid creating empty buffer for attributes (#8576) --- modules/core/src/lib/attribute/attribute.ts | 1 + modules/core/src/lib/attribute/data-column.ts | 9 +++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/modules/core/src/lib/attribute/attribute.ts b/modules/core/src/lib/attribute/attribute.ts index b447571e0e5..8b663199e76 100644 --- a/modules/core/src/lib/attribute/attribute.ts +++ b/modules/core/src/lib/attribute/attribute.ts @@ -214,6 +214,7 @@ export default class Attribute extends DataColumn { value: NumericArray | null; doublePrecision: boolean; - protected _buffer: Buffer | null; + protected _buffer: Buffer | null = null; protected state: DataColumnInternalState; /* eslint-disable max-statements */ @@ -190,9 +190,6 @@ export default class DataColumn { bounds: null, constant: false }; - - // TODO(v9): Can we pre-allocate the correct size, instead? - this._buffer = this._createBuffer(0); } /* eslint-enable max-statements */ @@ -429,7 +426,7 @@ export default class DataColumn { // 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); } @@ -479,7 +476,7 @@ export default class DataColumn { 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