web: hand the RAW develop's plane to Skia as half, so the GPU keeps its shadows

An RGBA_F32 image with an sRGB tag comes back off the GPU backend sampled on a
1/255 grid; the same shader on a raster surface returns the floats untouched.
The plane is raw/65535, so the shadows the black level is there to keep sit at
1e-3 and quantise to zero -- a 3010x2012 develop landed 41189 pixels under luma
2 with the dark end speckled blue/yellow, against none on the raster surface.
A half is uploaded as float, so the plane stays exact either way.

Rejects the earlier guess that the render target's colour space was to blame:
gpu+rt-srgb and gpu+img-untagged came back byte-identical to gpu.

scripts/half-check.mjs checks the conversion: the named encodings, and no plane
value in a 14-bit sensor's range moving more than 4.8e-4 relative.
This commit is contained in:
2026-09-27 08:56:42 +07:00
parent 610a274103
commit b824308182
3 changed files with 108 additions and 3 deletions
+42
View File
@@ -0,0 +1,42 @@
// Float32 → half, for the one place in the pipeline that hands raw numbers to
// Skia: the RAW develop's band plane.
//
// Skia's GPU backend does not keep a float32 texture. A band uploaded as
// RGBA_F32 + an sRGB tag comes back sampled off a 1/255 grid — the shader asked
// for fract(v * 100) and got the wrap of round(v * 255) / 255 on every value
// (probed in Chromium/ANGLE against the same shader on a raster surface, which
// returned the floats untouched). The plane is raw/65535, so the shadows — the
// whole point of the develop's black level — sit at 1e-3 and quantise to zero:
// the browser's own develop came back with 41189 pixels at luma < 2 and the dark
// end speckled blue/yellow, while the same shader on a raster surface produced
// none. A half IS uploaded as float, so this keeps the plane exact on both.
//
// ponytail: 10-bit mantissa is 0.05% relative — a tenth of an output level at
// the white point and finer than the plane's own 1/65535 step in the shadows.
// Take RGBA_16161616 unorm instead if a develop ever has to be bit-exact.
const scratch = new Float32Array(1);
const scratchBits = new Uint32Array(scratch.buffer);
export function f32ToF16(src: Float32Array, dst: Uint16Array, count: number): void {
for (let i = 0; i < count; i++) {
scratch[0] = src[i];
const x = scratchBits[0];
const sign = (x >> 16) & 0x8000;
let exp = (x >> 23) & 0xff;
const man = x & 0x7fffff;
if (exp === 0xff) { dst[i] = sign | 0x7c00; continue; } // inf / nan
if (exp < 103) { dst[i] = sign; continue; } // underflows the subnormal range
if (exp < 113) { // subnormal half
const shift = 113 - exp;
const full = man | 0x800000;
dst[i] = sign | ((full >> (shift + 13)) + ((full >> (shift + 12)) & 1));
continue;
}
if (exp > 142) { dst[i] = sign | 0x7c00; continue; } // overflows to inf
exp -= 112;
const rest = man & 0x1fff; // round to nearest even
// The mantissa's rounding carry has to reach the exponent, so add — `|`
// would drop it, since bit 10 is set in most exponents.
dst[i] = sign | ((exp << 10) + (man >> 13) + (rest > 0x1000 || (rest === 0x1000 && ((man >> 13) & 1)) ? 1 : 0));
}
}
+8 -3
View File
@@ -18,6 +18,7 @@
// with an OffscreenCanvas if the develop ever blocks the UI visibly.
import LibRaw from 'libraw-wasm';
import { cameraMatch, type Mat3 } from './cameraMatch';
import { f32ToF16 } from './halfFloat';
import { Skia } from './skiaShim';
// What `imageData()` returns for the settings below: 16-bit, 3 channels, with
@@ -136,6 +137,9 @@ export async function developRaw(bytes: Uint8Array): Promise<Uint8Array> {
const bandH = Math.max(1, Math.min(h, Math.floor(BAND_PIXELS / w)));
const f32 = new Float32Array(w * bandH * 4);
// The band goes up as half, not float32: the GPU backend puts an F32 image
// on the 1/255 grid and the shadows quantise to black (see halfFloat.ts).
const half = new Uint16Array(w * bandH * 4);
for (let y0 = 0; y0 < h; y0 += bandH) {
const rows = Math.min(bandH, h - y0);
let o = 0;
@@ -145,10 +149,11 @@ export async function developRaw(bytes: Uint8Array): Promise<Uint8Array> {
f32[o++] = data[i + 2] / SAMPLE_MAX;
f32[o++] = 1;
}
f32ToF16(f32, half, w * rows * 4);
const band = Skia.Image.MakeImage(
{ width: w, height: rows, colorType: Skia.ColorType.RGBA_F32, alphaType: Skia.AlphaType.Unpremul },
new Uint8Array(f32.buffer, 0, w * rows * 16),
w * 16
{ width: w, height: rows, colorType: Skia.ColorType.RGBA_F16, alphaType: Skia.AlphaType.Unpremul },
new Uint8Array(half.buffer, 0, w * rows * 8),
w * 8
);
if (!band) throw new Error('band image failed');
const child = band.makeShaderOptions(