writeBuffer method
queue.writeBuffer, from an offset that is a multiple of four. A length
that is not one is allowed only where it runs to the end of the buffer,
whose allocation was rounded up to four: anywhere else the padding
writeBuffer needs would land on bytes the caller did not name.
Implementation
@override
void writeBuffer(StorageBuffer target, int offsetInBytes, ByteData bytes) {
if (!features.has(DeviceFeature.compute)) {
features.require(DeviceFeature.buffers, backend: _backend);
}
final end = offsetInBytes + bytes.lengthInBytes;
if (offsetInBytes < 0 || end > target.lengthInBytes) {
throw ArgumentError(
'writeBuffer: $offsetInBytes + ${bytes.lengthInBytes} does not fit '
'inside a ${target.lengthInBytes}-byte buffer',
);
}
if (offsetInBytes % 4 != 0 ||
(bytes.lengthInBytes % 4 != 0 && end != target.lengthInBytes)) {
throw ArgumentError(
'writeBuffer: offset $offsetInBytes and length '
'${bytes.lengthInBytes} must be multiples of 4 short of the end',
);
}
if (!target.usage.contains(BufferUsage.copyDestination)) {
throw ArgumentError.value(
target.usage,
'target',
'was not made with BufferUsage.copyDestination',
);
}
_gpuDevice.queue.writeBuffer(
(target.backend as WebGpuStorage).buffer,
offsetInBytes,
gpuWritableBytes(bytes).toJS,
);
}