mgpuWriteRawBytesAt function

void mgpuWriteRawBytesAt(
  1. MGPUBuffer buffer,
  2. Uint8List bytes,
  3. int offset
)

Upload exactly the caller's byte view into minigpu's own device/buffer. The typed-array overload preserves a subview's offset and length. Flushing the open compute batch orders earlier work before this queue write.

Implementation

void mgpuWriteRawBytesAt(MGPUBuffer buffer, Uint8List bytes, int offset) {
  if (offset < 0 || offset % 4 != 0 || bytes.lengthInBytes % 4 != 0) {
    throw ArgumentError('Raw upload offset/length must be aligned to 4 bytes');
  }
  final target = getWebGpuJsObject(mgpuGetWGPUBufferHandle(buffer));
  final device = getWebGpuJsObject(mgpuGetWGPUDeviceHandle());
  if (target == null || device == null)
    throw StateError('GPU buffer/device unavailable');
  if (offset + bytes.lengthInBytes > (target as _SizedGPUBuffer).size) {
    throw RangeError('Raw upload exceeds GPU buffer');
  }
  if (bytes.isEmpty) return;
  mgpuFlushBatch();
  final typedBytes = Uint8ListToJSUint8Array(bytes).toJS;
  ((device as GPUDevice).queue as _GPUQueue).writeBuffer(
    target,
    offset,
    typedBytes,
  );
}