Skip to content

Commit

Permalink
chore(core): Remove getBufferData helper (visgl#8425)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Jan 19, 2024
1 parent 66b4bc8 commit 3abe309
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 31 deletions.
40 changes: 11 additions & 29 deletions modules/core/src/lib/attribute/attribute-transition-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type {Device, TypedArrayConstructor} from '@luma.gl/core';
import type {Device} from '@luma.gl/core';
import type {Buffer} from '@luma.gl/core';
import {padArray} from '../../utils/array-utils';
import {NumericArray, TypedArray} from '../../types/types';
import Attribute from './attribute';
import type {BufferAccessor} from './data-column';
import {GL} from '@luma.gl/constants';
import {VertexFormat as LumaVertexFormat} from '@luma.gl/core';

export interface TransitionSettings {
Expand Down Expand Up @@ -178,7 +177,8 @@ export function padBuffer({

const toData = isConstant
? (attribute.value as TypedArray)
: getBufferData(attribute.getBuffer()!, Float32Array);
: // TODO(v9.1): Avoid non-portable synchronous reads.
toFloat32Array(attribute.getBuffer()!.readSyncWebGL2());
if (attribute.settings.normalized && !isConstant) {
const getter = getData;
getData = (value, chunk) => attribute.normalizeConstant(getter(value, chunk));
Expand All @@ -188,12 +188,8 @@ export function padBuffer({
? (i, chunk) => getData(toData, chunk)
: (i, chunk) => getData(toData.subarray(i + byteOffset, i + byteOffset + size), chunk);

const source = getBufferData(
buffer,
Float32Array,
0,
fromLength * Float32Array.BYTES_PER_ELEMENT
);
// TODO(v9.1): Avoid non-portable synchronous reads.
const source = toFloat32Array(buffer.readSyncWebGL2());
const target = new Float32Array(toLength);
padArray({
source,
Expand All @@ -211,24 +207,10 @@ export function padBuffer({
return buffer;
}

/** @deprecated TODO(v9.1): Buffer reads should be asynchronous and avoid accessing GL context. */
export function getBufferData(
buffer: Buffer,
TypedArray: TypedArrayConstructor,
byteOffset = 0,
byteLength = buffer.byteLength
): TypedArray {
const _buffer = buffer as any;
_buffer.device.assertWebGL2();

const dstLength = byteLength / TypedArray.BYTES_PER_ELEMENT;
const dstArray = new TypedArray(dstLength);
const dstOffset = 0;

// Use GL.COPY_READ_BUFFER to avoid disturbing other targets and locking type
_buffer.gl.bindBuffer(GL.COPY_READ_BUFFER, _buffer.handle);
_buffer.gl2.getBufferSubData(GL.COPY_READ_BUFFER, byteOffset, dstArray, dstOffset, dstLength);
_buffer.gl.bindBuffer(GL.COPY_READ_BUFFER, null);

return dstArray;
function toFloat32Array(bytes: Uint8Array): Float32Array {
return new Float32Array(
bytes.buffer,
bytes.byteOffset,
bytes.byteLength / Float32Array.BYTES_PER_ELEMENT
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,28 @@ if (device.info.type === 'webgl2') {
t.notOk(positionTransform._handle, 'instancePositions transform is deleted');
t.is(sizeTransition.buffers[0].byteLength, 4 * 4 + 8, 'buffer has correct size');

// TODO(v9): Previous 'expected' values for these tests indicated that padding should be
// overwritten with new values. Padding is _not_ overwritten as of visgl/deck.gl#8425, but the
// PR strictly improves `test/apps/attribute-transition`. Test cases below merit a closer look,
// when resolving remaining bugs in attribute transitions for deck.gl v9.
//
// current: [0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
// expected: [0, 0, 0, 0, 1, 1, 1, 1, 1, 1]

attributes.instanceSizes.setData({value: new Float32Array(10).fill(1)});
manager.update({attributes, transitions: {getSize: 1000}, numInstances: 10});
manager.run();
let transitioningBuffer = manager.getAttributes().instanceSizes.getBuffer();
let actual = await readArray(transitioningBuffer);
t.deepEquals(actual, [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], 'buffer is extended with new data');
t.deepEquals(actual, [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], 'buffer is extended with new data');
t.is(transitioningBuffer.byteLength, 10 * 4, 'buffer has correct size');

attributes.instanceSizes.setData({constant: true, value: [2]});
manager.update({attributes, transitions: {getSize: 1000}, numInstances: 12});
manager.run();
transitioningBuffer = manager.getAttributes().instanceSizes.getBuffer();
actual = await readArray(transitioningBuffer);
t.deepEquals(actual, [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2], 'buffer is extended with new data');
t.deepEquals(actual, [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2], 'buffer is extended with new data');
t.is(transitioningBuffer.byteLength, 12 * 4, 'buffer has correct size');

manager.finalize();
Expand Down

0 comments on commit 3abe309

Please sign in to comment.