mgpuWriteFloat function

Future<void> mgpuWriteFloat(
  1. MGPUBuffer buffer,
  2. Float32List inputData,
  3. int size
)

Implementation

Future<void> mgpuWriteFloat(
  MGPUBuffer buffer,
  Float32List inputData,
  int size,
) async {
  final int elementsToWrite = size > 0 ? size : inputData.length;
  final int byteSize = elementsToWrite * Float32List.bytesPerElement;

  final JSNumber ptr = _malloc(byteSize.toJS);
  final int startIndex = ptr.toDartInt ~/ Float32List.bytesPerElement;

  try {
    final int actualElements = elementsToWrite <= inputData.length
        ? elementsToWrite
        : inputData.length;

    final currentHeapF32 = HEAPF32.toDart;

    if (startIndex < 0 || startIndex + actualElements > currentHeapF32.length) {
      throw StateError(
        'Float32 buffer allocation would exceed heap bounds: '
        'trying to write $actualElements elements at index $startIndex '
        'but heap size is ${currentHeapF32.length}',
      );
    }

    currentHeapF32.setRange(
      startIndex,
      startIndex + actualElements,
      inputData.sublist(0, actualElements),
    );

    // Make this async using ccall
    await ccall(
      "mgpuWriteFloat".toJS,
      "void".toJS,
      ["number", "number", "number"].toJSDeep,
      [buffer, ptr, byteSize.toJS].toJSDeep,
      {"async": true}.toJSDeep,
    ).toDart;
  } finally {
    _free(ptr);
  }
}