getPixels method

dynamic getPixels()

Implementation

getPixels() {
  var device = _device!;

  var renderTarget = getRenderTarget();
  int width = renderTarget.width.toInt();
  int height = renderTarget.height.toInt();

  int bytes_per_pixel = Uint32List.bytesPerElement;
  int unpadded_bytes_per_row = width * bytes_per_pixel;
  int align = 256;
  int padded_bytes_per_row_padding =
      (align - unpadded_bytes_per_row % align) % align;
  int padded_bytes_per_row =
      unpadded_bytes_per_row + padded_bytes_per_row_padding;

  int bufferSize = padded_bytes_per_row * height;


  var renderTargetProperties = this._properties.get(renderTarget);

  GPUTexture colorTextureGPU = renderTargetProperties["colorTextureGPU"];

  var commandEncoder2 = device.createCommandEncoder();
  var copyTexture =
      GPUImageCopyTexture(texture: colorTextureGPU, origin: GPUOrigin3D());

  var bufferDesc = GPUBufferDescriptor(
      size: bufferSize,
      usage: GPUBufferUsage.MapRead | GPUBufferUsage.CopyDst);
  var outputBuffer = device.createBuffer(bufferDesc);
  var copyBuffer = GPUImageCopyBuffer(
      buffer: outputBuffer, bytesPerRow: padded_bytes_per_row);

  var _textureExtent = GPUExtent3D(width: width, height: height);
  commandEncoder2.copyTextureToBuffer(copyTexture, copyBuffer, _textureExtent);


  var commandBuffer2 = commandEncoder2.finish(GPUCommandBufferDescriptor());
  device.queue.submit(commandBuffer2);

  outputBuffer.mapAsync(mode: WGPUMapMode_Read, size: bufferSize);

  device.poll(true);

  var data = outputBuffer.getMappedRange(offset: 0, size: bufferSize);

  Pointer<Uint8> pixles = data.cast<Uint8>();
  outputBuffer.unmap();

  return pixles.asTypedList(bufferSize);

}