A Buffer class that manages memory allocation and provides BufferView creation functionality. This class wraps an ArrayBuffer and manages byte allocation with alignment considerations. It tracks the used bytes and provides methods to create BufferView instances from the underlying buffer.

const buffer = new Buffer({
byteLength: 1024,
buffer: new ArrayBuffer(1024),
name: 'MyBuffer',
byteAlign: 4
});

const bufferViewResult = buffer.takeBufferView({
byteLengthToNeed: 64,
byteStride: 16
});

Constructors

  • Creates a new Buffer instance.

    Parameters

    • options: {
          buffer: ArrayBuffer;
          byteAlign: number;
          byteLength: number;
          name: string;
      }

      Configuration object for the buffer

      • buffer: ArrayBuffer

        The underlying ArrayBuffer or Uint8Array to wrap

      • byteAlign: number

        The byte alignment requirement for memory allocation

      • byteLength: number

        The total byte length of the buffer

      • name: string

        A descriptive name for the buffer

    Returns Buffer

Accessors

  • get byteLength(): number
  • Gets the total byte length of the buffer.

    Returns number

    The total capacity of the buffer in bytes

  • get byteOffsetInRawArrayBuffer(): number
  • Gets the byte offset of this buffer within the raw ArrayBuffer. This is useful when the buffer is a view into a larger ArrayBuffer.

    Returns number

    The byte offset within the raw ArrayBuffer

  • get takenSizeInByte(): number
  • Gets the number of bytes currently allocated from this buffer.

    Returns number

    The number of bytes that have been taken from the buffer

Methods

  • Internal

    Manually adds to the taken byte index counter. This is an internal method used to track memory usage.

    Parameters

    • value: number

      The number of bytes to add to the taken bytes index

    Returns void

  • Gets the underlying ArrayBuffer.

    Returns ArrayBuffer

    The raw ArrayBuffer instance

  • Creates a typed array view into the buffer at a specific offset. This method provides direct access to the buffer data as a typed array, which is useful for reading and writing numeric data efficiently.

    Parameters

    • offset4bytesUnit: number

      The offset in 4-byte units from the start of the buffer

    • compositionType: CompositionTypeEnum

      The composition type (scalar, vector, matrix, etc.)

    • componentType: ComponentTypeEnum

      The component type (float, int, etc.)

    • length: number = 100

      The number of elements to include in the typed array (default: 100)

    Returns TypedArray

    A typed array view into the buffer data

    const floatArray = buffer.getTypedArray(
    0,
    CompositionType.Vec3,
    ComponentType.Float,
    10
    );
  • Checks if this buffer uses the same underlying ArrayBuffer as another buffer. This is useful for determining if two Buffer instances share the same memory.

    Parameters

    • buffer: Buffer

      The other buffer to compare with

    Returns boolean

    True if both buffers use the same underlying ArrayBuffer, false otherwise

    const buffer1 = new Buffer({...});
    const buffer2 = new Buffer({...});

    if (buffer1.isSame(buffer2)) {
    console.log('Buffers share the same memory');
    }
  • Creates a new BufferView from the available space in this buffer. This method allocates a portion of the buffer and returns a BufferView that provides typed access to that memory region.

    Parameters

    • options: {
          byteLengthToNeed: number;
          byteStride: number;
      }

      Configuration for the BufferView to create

      • byteLengthToNeed: number

        The number of bytes needed for the BufferView

      • byteStride: number

        The stride between elements in bytes

    Returns Result<BufferView, {
        Buffer.byteLength: number;
        Buffer.takenSizeInByte: number;
    }>

    A Result containing either the created BufferView or an error with buffer state information

    const result = buffer.takeBufferView({
    byteLengthToNeed: 256,
    byteStride: 16
    });

    if (result.isOk()) {
    const bufferView = result.get();
    // Use the bufferView...
    }
  • Creates a new BufferView at a specific byte offset within this buffer. Unlike takeBufferView, this method allows you to specify the exact offset where the BufferView should be created, which is useful for accessing pre-defined memory layouts.

    Parameters

    • options: {
          byteLengthToNeed: number;
          byteOffset: number;
          byteStride: number;
      }

      Configuration for the BufferView to create

      • byteLengthToNeed: number

        The number of bytes needed for the BufferView

      • byteOffset: number

        The specific byte offset within the buffer where the BufferView should start

      • byteStride: number

        The stride between elements in bytes

    Returns Result<BufferView, undefined>

    A Result containing either the created BufferView or an error

    const result = buffer.takeBufferViewWithByteOffset({
    byteLengthToNeed: 128,
    byteStride: 8,
    byteOffset: 256
    });