mgpuSubmitRender function

int mgpuSubmitRender(
  1. MGPURenderPipeline pipeline,
  2. MGPUTextureView color,
  3. MGPUTextureView? depth,
  4. List<double> clear,
  5. int flags,
  6. List<MGPUBuffer> buffers,
  7. List<MGPUBuffer> views,
  8. List<MGPUBuffer> samplers,
  9. List<int> commands,
)

Implementation

int mgpuSubmitRender(
  MGPURenderPipeline pipeline,
  MGPUTextureView color,
  MGPUTextureView? depth,
  List<double> clear,
  int flags,
  List<MGPUBuffer> buffers,
  List<MGPUTextureView> views,
  List<MGPUSampler> samplers,
  List<int> commands,
) {
  _requireRasterResourceV1();
  _checkWasm32Handle(pipeline, 'pipeline');
  if (clear.length != 5) {
    throw ArgumentError.value(clear, 'clear', 'Must contain RGBA and depth');
  }
  final clear32 = Float32List(5);
  for (var i = 0; i < clear.length; i++) {
    final value = clear[i];
    if (!value.isFinite) {
      throw ArgumentError.value(value, 'clear', 'Must be finite');
    }
    clear32[i] = value;
    if (!clear32[i].isFinite) {
      throw ArgumentError.value(
        value,
        'clear',
        'Must be representable as float32',
      );
    }
  }
  if (clear[4] < 0 || clear[4] > 1) {
    throw ArgumentError.value(clear[4], 'clear', 'Depth must be in 0..1');
  }
  if (flags < 0 || flags > 3 || (depth == null && (flags & 2) != 0)) {
    throw ArgumentError.value(flags, 'flags', 'Invalid render load flags');
  }
  if (buffers.length > _maxRasterResourcesPerFamily ||
      views.length > _maxRasterResourcesPerFamily ||
      samplers.length > _maxRasterResourcesPerFamily) {
    throw ArgumentError('Render resource count exceeds 256');
  }
  _checkRasterWords(commands, _maxRasterCommandWords, 'commands');
  _checkWasm32Handle(color, 'color');
  for (final handle in buffers) {
    _checkWasm32Handle(handle, 'buffers');
  }
  for (final handle in views) {
    _checkWasm32Handle(handle, 'views');
  }
  for (final handle in samplers) {
    _checkWasm32Handle(handle, 'samplers');
  }
  if (depth != null) {
    _checkWasm32Handle(depth, 'depth');
  }

  final allocations = <JSNumber>[];
  JSNumber allocate(int byteLength) {
    final pointer = _checkedRasterMalloc(byteLength);
    allocations.add(pointer);
    if ((pointer.toDartInt & 3) != 0) {
      throw StateError('Raster allocation was not 4-byte aligned');
    }
    return pointer;
  }

  try {
    final clearPointer = allocate(5 * 4);
    final bufferPointer = buffers.isEmpty
        ? 0.toJS
        : allocate(buffers.length * 4);
    final viewPointer = views.isEmpty ? 0.toJS : allocate(views.length * 4);
    final samplerPointer = samplers.isEmpty
        ? 0.toJS
        : allocate(samplers.length * 4);
    final commandPointer = commands.isEmpty
        ? 0.toJS
        : allocate(commands.length * 4);

    final heapBytes = _heapU8;
    final clearStart = clearPointer.toDartInt;
    if (clearStart > heapBytes.length || 20 > heapBytes.length - clearStart) {
      throw StateError('Raster clear allocation exceeded the heap');
    }
    HEAPF32.set(Float32ListToJSFloat32Array(clear32).toJS, clearStart ~/ 4);

    final heapWords = _heapU32;
    void writeHandles(JSNumber pointer, List<JSNumber> handles) {
      if (handles.isEmpty) return;
      final index = pointer.toDartInt ~/ 4;
      if (index > heapWords.length ||
          handles.length > heapWords.length - index) {
        throw StateError('Raster pointer array exceeded the heap');
      }
      for (var i = 0; i < handles.length; i++) {
        heapWords[index + i] = _checkWasm32Handle(handles[i], 'handles');
      }
    }

    writeHandles(bufferPointer, buffers);
    writeHandles(viewPointer, views);
    writeHandles(samplerPointer, samplers);
    if (commands.isNotEmpty) {
      final index = commandPointer.toDartInt ~/ 4;
      if (index > heapWords.length ||
          commands.length > heapWords.length - index) {
        throw StateError('Raster command array exceeded the heap');
      }
      HEAPU32.set(
        Uint32ListToJSUint32Array(_rasterWords(commands)).toJS,
        index,
      );
    }
    return _mgpuSubmitRender(
      pipeline,
      color,
      depth ?? 0.toJS,
      clearPointer,
      flags.toJS,
      bufferPointer,
      buffers.length.toJS,
      viewPointer,
      views.length.toJS,
      samplerPointer,
      samplers.length.toJS,
      commandPointer,
      commands.length.toJS,
    ).toDartInt;
  } finally {
    for (final pointer in allocations.reversed) {
      _free(pointer);
    }
  }
}