copyTextureToBuffer method

void copyTextureToBuffer(
  1. TextureRegion source,
  2. BufferView destination
)

Copies a texture region into a tightly packed destination buffer view.

The destination buffer must be large enough to hold the source region rounded up to whole pixel-format blocks.

Implementation

void copyTextureToBuffer(TextureRegion source, BufferView destination) {
  source._validate(allowMipAndSlice: false);
  if (destination.offsetInBytes < 0 ||
      destination.lengthInBytes < 0 ||
      destination.offsetInBytes + destination.lengthInBytes >
          destination.buffer.sizeInBytes) {
    throw Exception('BufferView range is out of bounds');
  }
  final int expectedSize = source._sizeInBytes();
  if (destination.lengthInBytes != expectedSize) {
    throw Exception(
      'The destination BufferView length (bytes: ${destination.lengthInBytes}) '
      'must match the source texture region size (bytes: $expectedSize)',
    );
  }
  final String? error = _copyTextureToBuffer(
    source.texture,
    source.x,
    source.y,
    source._resolvedWidth(),
    source._resolvedHeight(),
    destination.buffer,
    destination.offsetInBytes,
  );
  if (error != null) {
    throw Exception(error);
  }
}