mapBuffer method

  1. @override
Future<MappedBuffer> mapBuffer(
  1. StorageBuffer buffer,
  2. MapMode mode, {
  3. int offsetInBytes = 0,
  4. int? sizeInBytes,
})

mapAsync, on the buffer itself where it was made mappable and through a staging copy or a queue write where its other usages rule that out — see gpuBufferUsageOf.

Implementation

@override
Future<MappedBuffer> mapBuffer(
  StorageBuffer buffer,
  MapMode mode, {
  int offsetInBytes = 0,
  int? sizeInBytes,
}) async {
  features.require(DeviceFeature.mappedBuffers, backend: _backend);
  final size = sizeInBytes ?? buffer.lengthInBytes - offsetInBytes;
  webgpuCheckRange(buffer, offsetInBytes, size, what: 'mapBuffer');
  switch (mode) {
    case MapMode.read:
      if (!buffer.usage.contains(BufferUsage.hostReadable) &&
          !buffer.hostReadable) {
        throw ArgumentError.value(
          buffer.usage,
          'buffer',
          'was not made with BufferUsage.hostReadable',
        );
      }
      final mapped = await webgpuMapRead(
        _gpuDevice,
        buffer,
        offsetInBytes,
        size,
      );
      return WebGpuMappedBuffer(mapped.bytes, mapped.release);
    case MapMode.write:
      if (!buffer.usage.contains(BufferUsage.hostWritable)) {
        throw ArgumentError.value(
          buffer.usage,
          'buffer',
          'was not made with BufferUsage.hostWritable',
        );
      }
      return webgpuMapWrite(_gpuDevice, buffer, offsetInBytes, size);
    default:
      throw UnsupportedError('WebGPU has no map mode $mode');
  }
}