mgpuWriteInt16 function

void mgpuWriteInt16(
  1. MGPUBuffer buffer,
  2. Int16List inputData,
  3. int size
)

Implementation

void mgpuWriteInt16(MGPUBuffer buffer, Int16List inputData, int size) {
  final int elementsToWrite = size > 0 ? size : inputData.length;
  final int byteSize = elementsToWrite * Int16List.bytesPerElement;
  final JSNumber ptr = _malloc(byteSize.toJS);
  final int startByteIndex = ptr.toDartInt;
  try {
    final int startElementIndex = startByteIndex ~/ Int16List.bytesPerElement;
    final int actualElements = elementsToWrite <= inputData.length
        ? elementsToWrite
        : inputData.length;

    if (startElementIndex + actualElements > _heapI16.length) {
      throw StateError('Int16 buffer allocation would exceed heap bounds');
    }

    _heapI16.setRange(
      startElementIndex,
      startElementIndex + actualElements,
      inputData.sublist(0, actualElements),
    );
    _mgpuWriteInt16(buffer, ptr, byteSize.toJS);
  } finally {
    _free(ptr);
  }
}