WebGPU writeBuffer overWrites with zeros in special cases

Here’s a demonstration that the current WebGPU updateDirectly is broken for transfer sizes less than 4. And a new setSubData that fixes it for one use case.

(this comes from another thread on modifying PointsCloudSystem to do a partial update on VertexBuffer.)

On lines 142 and 143, you can change testNewGPU and chunkSize.

  • testNewGPU - false uses standard VertexBuffer.updateDirectly. true uses my setSubDataAlign function (when playground is set for gpu).
  • chunkSize = use 1, 2, 4, or 12. splits the 12-byte update into chunks of this number of bytes.

Result:

WebGL2 updateDirectly: works with all chunk sizes (1, 2, 4, 12)
WebGPU updateDirectly: works with chunk sizes 4 and 12. fails to update GPUBuffer with sizes 1 or 2.
WebGPU setSubDataAlign: works with all chunk sizes (1, 2, 4, 12)

When testing setSubDataAlign, the numbers flickering on the lower left are the number of bytes transferred that are within the ArrayBuffer but outside the TypedArray overlaying it.

Note that setSubDataAlign is still likely to fail if dstOffset and/or byteLength are not aligned and there’s not enough data in the ArrayBuffer behind the TypedArray. There’s a way to fix this, but it would pad with zeros.

Edit: setSubDataAlign can replace setSubData on bufferManager (when the engine is WebGPU):

engine._bufferManager.setSubData = setSubDataAlign

There’s also a PointsCloudSystem problem on WebGPU where the size parameter is not respected. I’ll post more on that when I get time to debug in a week or so.

1 Like