write method

WebGpuSlice write(
  1. ByteData bytes
)

bytes written into the arena, and where they went.

The length is rounded up to four before the cursor moves, because writeBuffer refuses a size that is not a multiple of four — see gpuWritableBytes, which does the same to the data. The padding is never read: a draw is bounded by its own count.

Implementation

WebGpuSlice write(ByteData bytes) {
  final length = bytes.lengthInBytes;
  final need = (length + 3) & ~3;
  var at = (_cursor + alignment - 1) & ~(alignment - 1);
  if (at + need > _capacity) {
    _retired.add(_buffer);
    var grown = _capacity * 2;
    while (grown < need) {
      grown *= 2;
    }
    _capacity = grown;
    _buffer = _make(_capacity);
    at = 0;
  }
  _gpu.queue.writeBuffer(_buffer, at, gpuWritableBytes(bytes).toJS);
  _cursor = at + need;
  if (_cursor > _peak) _peak = _cursor;
  return (buffer: _buffer, offset: at, length: length);
}