uploadRect method

void uploadRect(
  1. Uint8List data,
  2. Rect rect
)

Implementation

void uploadRect(Uint8List data, Rect rect) {
  if (_disposed) return;
  final wgpu = WebgpuRend.instance.wgpu;
  final int x = rect.left.toInt();
  final int y = rect.top.toInt();
  final int w = rect.width.toInt();
  final int h = rect.height.toInt();

  // Basic validation
  if (data.length != w * h * 4) {
    throw ArgumentError(
        "Data size (${data.length}) does not match rect size ($w x $h x 4 = ${w * h * 4})");
  }

  beginAccess();

  using((arena) {
    final destination = arena<WGPUTexelCopyTextureInfo>();
    destination.ref.texture = texture;
    destination.ref.mipLevel = 0;
    destination.ref.origin.x = x;
    destination.ref.origin.y = y;
    destination.ref.origin.z = 0;
    destination.ref.aspect = WGPUTextureAspect.WGPUTextureAspect_All;

    final layout = arena<WGPUTexelCopyBufferLayout>();
    layout.ref.offset = 0;
    layout.ref.bytesPerRow = w * 4;
    layout.ref.rowsPerImage = h;

    final writeSize = arena<WGPUExtent3D>();
    writeSize.ref.width = w;
    writeSize.ref.height = h;
    writeSize.ref.depthOrArrayLayers = 1;

    final ptr = arena<Uint8>(data.length);
    ptr.asTypedList(data.length).setAll(0, data);

    wgpu.wgpuQueueWriteTexture(WebgpuRend.instance.queue, destination,
        ptr.cast(), data.length, layout, writeSize);
  });

  endAccess();
}