mgpuLoadKernel function

void mgpuLoadKernel(
  1. MGPUComputeShader shader,
  2. String kernelString
)

Implementation

void mgpuLoadKernel(MGPUComputeShader shader, String kernelString) {
  final bytes = utf8.encode(kernelString);
  final kernelBytes = Uint8List(bytes.length + 1)
    ..setRange(0, bytes.length, bytes)
    ..[bytes.length] = 0; // null terminator

  final allocSize = kernelBytes.length;
  final ptr = _malloc(allocSize.toJS);
  final int startIndex = ptr.toDartInt;

  // Bounds check
  if (startIndex < 0 || startIndex + kernelBytes.length > _heapU8.length) {
    _free(ptr);
    throw StateError(
      'Kernel string allocation would exceed heap bounds: '
      'trying to write ${kernelBytes.length} bytes at index $startIndex '
      'but heap size is ${_heapU8.length}',
    );
  }

  _heapU8.setRange(startIndex, startIndex + kernelBytes.length, kernelBytes);
  _mgpuLoadKernel(shader, ptr);

  _free(ptr);
}