webgpuUploadGeometry function

GeometryBuffer webgpuUploadGeometry(
  1. GPUDevice gpu,
  2. List<GPUBuffer> tracked,
  3. ByteData bytes,
  4. GeometryUsage usage,
)

Geometry uploaded once and bound many times, until the device that made it is disposed. See GraphicsDevice.uploadGeometry.

usage is not a hint here any more than it is on WebGL2: a WebGPU buffer declares VERTEX or INDEX when it is made and cannot be bound as the other afterwards. That the contract already carries the word is one of the places it turned out not to be describing a single API.

Implementation

GeometryBuffer webgpuUploadGeometry(
  GPUDevice gpu,
  List<GPUBuffer> tracked,
  ByteData bytes,
  GeometryUsage usage,
) {
  final buffer = gpu.createBuffer(
    GPUBufferDescriptor(
      // Rounded up to four, which `writeBuffer` demands of every size it is
      // given — see `gpuWritableBytes` for the bytes' half of the same rule.
      size: (bytes.lengthInBytes + 3) & ~3,
      usage:
          (usage == GeometryUsage.vertices
              ? GpuBufferUsage.vertex
              : GpuBufferUsage.index) |
          GpuBufferUsage.copyDst,
      label: 'geometry ${bytes.lengthInBytes}B ${usage.name}',
    ),
  );
  gpu.queue.writeBuffer(buffer, 0, gpuWritableBytes(bytes).toJS);
  tracked.add(buffer);
  return GeometryBuffer(
    backend: WebGpuGeometry(buffer),
    offsetInBytes: 0,
    lengthInBytes: bytes.lengthInBytes,
  );
}