WebGPU: reading back a half-float texture converts each value in JavaScript; Float16Array does it natively

Playground: https://playground.babylonjs.com/?webgpu#OU10I4#0

When a half-float texture is read back on WebGPU (readPixels), WebGPUBufferManager._getHalfFloatAsFloatRGBAArrayBuffer converts every value with FromHalfFloat, in a loop. Where Float16Array exists (Chrome and Edge 135, Firefox 129, Safari 18.2), the same conversion is one destArray.set(new Float16Array(arrayBuffer, 0, dataLength)), done by the browser.

The Playground reads a 712 × 448 RGBA half-float render target, 1,275,904 values, 10 times with each conversion, alternating. Measured on 9.32.0 (Chromium 147, RTX 3080 Laptop GPU, Windows 11), medians of two runs:

Conversion Whole readPixels
As shipped 17.3 and 26.1 ms 29.4 and 44.1 ms
With Float16Array 3.3 and 4.7 ms 15.4 and 22.1 ms

Every value read is the same with both conversions, and so is the conversion of each of the 65,536 half-float bit patterns, compared with Object.is. Only NaNs can differ in their bits: the loop always returns the same NaN, while Float16Array keeps the sign and payload of some of them (1,022 of the 2,046 half-float NaNs here).

We found it while reading back an HDR target to compute automatic exposure from a histogram, for a realistic world: at this size, the conversion alone takes longer than a frame at 60 fps.

Would a PR be welcome? It would use Float16Array where it exists and keep the current loop as the fallback. The TODO next to it, doing the conversion on the GPU, would stay open.

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

Thanks for investigating this and providing the benchmark! A PR would be welcome.

My initial concern was backward compatibility: half-float texture readback currently returns a Float32Array by default, and returning a Float16Array instead would be a breaking change. But your proposal keeps the Float32Array destination and uses Float16Array only as the source view for .set(), so it preserves the existing API while moving the conversion into the browser.

Please keep the current loop as a fallback where Float16Array is unavailable, and preserve the behavior when a destination buffer is supplied, including the existing length clamping. Coverage for padded texture rows and special values would also be useful. Thanks for explicitly calling out the NaN payload differences!

1 Like

Opened #19072. It keeps the Float32Array destination: a given buffer is filled with the same length clamp, padded rows are dropped as before, and the loop stays as the fallback where Float16Array is missing. The tests run every case both ways: all 65,536 half-float bit patterns against FromHalfFloat, the special values (zeros, infinities, NaN, subnormals, ±65504), a destination shorter than the data, and rows padded to 256 bytes.

(Sorry for answering all four topics at once: I had them ready and preferred to send them together.)

Prepared by Mema, the AI of our studio 53ON (Claude in the 53ON harness, with MemoriaBios, our long-term memory), and reviewed by me.

1 Like