mgpuCreateRenderPipeline function

MGPURenderPipeline? mgpuCreateRenderPipeline(
  1. String code,
  2. String vertex,
  3. String fragment,
  4. List<int> words,
)

Implementation

MGPURenderPipeline? mgpuCreateRenderPipeline(
  String code,
  String vertex,
  String fragment,
  List<int> words,
) {
  _requireRasterResourceV1();
  final codeBytes = utf8.encode(code);
  final identifier = RegExp(r'^[A-Za-z_][A-Za-z0-9_]{0,63}$');
  if (codeBytes.isEmpty ||
      codeBytes.length > 1048576 ||
      code.contains('\u0000') ||
      !identifier.hasMatch(vertex) ||
      !identifier.hasMatch(fragment)) {
    throw ArgumentError('Invalid render pipeline strings');
  }
  _checkRasterWords(words, _maxRasterDescriptorWords, 'descriptor');
  final allocations = <JSNumber>[];
  try {
    final codePointer = _renderUtf8(code, allocations);
    final vertexPointer = _renderUtf8(vertex, allocations);
    final fragmentPointer = _renderUtf8(fragment, allocations);
    final wordPointer = _checkedRasterMalloc(words.length * 4);
    allocations.add(wordPointer);
    final start = wordPointer.toDartInt;
    if ((start & 3) != 0) {
      throw StateError('Raster descriptor allocation was not aligned');
    }
    final heap = _heapU32;
    final index = start ~/ 4;
    if (index > heap.length || words.length > heap.length - index) {
      throw StateError('Raster descriptor allocation exceeded the heap');
    }
    HEAPU32.set(Uint32ListToJSUint32Array(_rasterWords(words)).toJS, index);
    final result = _mgpuCreateRenderPipeline(
      codePointer,
      vertexPointer,
      fragmentPointer,
      wordPointer,
      words.length.toJS,
    );
    if (result.toDartDouble == 0) return null;
    _checkWasm32Handle(result, 'pipeline');
    return result;
  } finally {
    for (final pointer in allocations.reversed) {
      _free(pointer);
    }
  }
}