Fix gamma handling and improve brush canvas performance

Color correctness:
- Unpremultiply before the sRGB OETF on the display and export blits;
  encoding premultiplied color corrupted antialiased/transparent edges.
- Tag exported video as full-range BT.709 (matrix/primaries/transfer).
- Run perception effects (invert, brightness/contrast, hue/saturation)
  in gamma space to match standard editors.
- Interpolate gradients in gamma space across the raster and vector paths.
- Render effect thumbnails in the same linear space as the live pipeline.

Brush performance:
- Store the raster canvas as Rgba16Float (no shadow banding from 8-bit
  linear), with an incremental per-tile ping-pong sync replacing the
  per-frame full-canvas copy.
- Do the linear->sRGB readback conversion on the GPU and reuse a cached
  scratch texture, dropping a ~110ms-per-stroke CPU decode.

Cleanup:
- Single COLOR_WGSL prelude and shared CPU sRGB scalars instead of ~8
  duplicated copies of the transfer functions.
- Shared compute-pipeline builder; smudge folded onto the tile-sync path.
This commit is contained in:
Skyler Lehmkuhl
2026-06-16 08:32:39 -04:00
parent 7257cdf9c4
commit 318720f89d
18 changed files with 582 additions and 285 deletions
@@ -31,14 +31,23 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
return out;
}
// The HDR pipeline feeds these shaders LINEAR light, but brightness/contrast
// (additive brightness, contrast pivoting around 0.5 perceptual mid-gray) are
// defined in gamma/display space. Convert to sRGB, adjust there, then convert
// back to linear so the controls behave like standard editors.
// sRGB helpers (linear_to_srgb / srgb_to_linear) come from the prepended
// COLOR_WGSL prelude.
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let brightness = uniforms.params0.x; // -1 to 1
let contrast = uniforms.params0.y; // 0 to 3
let src_srgb = linear_to_srgb(src.rgb);
// Apply brightness (additive)
var color = src.rgb + vec3<f32>(brightness);
var color = src_srgb + vec3<f32>(brightness);
// Apply contrast (multiply around midpoint 0.5)
color = (color - vec3<f32>(0.5)) * contrast + vec3<f32>(0.5);
@@ -46,6 +55,6 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// Clamp to valid range
color = clamp(color, vec3<f32>(0.0), vec3<f32>(1.0));
let result = mix(src.rgb, color, uniforms.mix);
return vec4<f32>(result, src.a);
let result_srgb = mix(src_srgb, color, uniforms.mix);
return vec4<f32>(srgb_to_linear(result_srgb), src.a);
}
@@ -84,6 +84,12 @@ fn hsl_to_rgb(hsl: vec3<f32>) -> vec3<f32> {
);
}
// The HDR pipeline feeds this shader LINEAR light, but the HSL model (and the
// lightness/saturation axes users expect) is defined on gamma-encoded sRGB.
// Convert to sRGB, run the HSL adjustment there, then convert back to linear.
// sRGB helpers (linear_to_srgb / srgb_to_linear) come from the prepended
// COLOR_WGSL prelude.
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
@@ -91,8 +97,10 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let saturation = uniforms.params0.y; // Multiplier (1.0 = no change)
let lightness = uniforms.params0.z; // Additive (-1 to 1)
let src_srgb = linear_to_srgb(src.rgb);
// Convert to HSL
var hsl = rgb_to_hsl(src.rgb);
var hsl = rgb_to_hsl(src_srgb);
// Apply adjustments
hsl.x = fract(hsl.x + hue_shift); // Shift hue (wrapping)
@@ -102,6 +110,6 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// Convert back to RGB
let adjusted = hsl_to_rgb(hsl);
let result = mix(src.rgb, adjusted, uniforms.mix);
return vec4<f32>(result, src.a);
let result_srgb = mix(src_srgb, adjusted, uniforms.mix);
return vec4<f32>(srgb_to_linear(result_srgb), src.a);
}
@@ -33,13 +33,19 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
return out;
}
// The HDR pipeline feeds these shaders LINEAR light, but "invert" is a
// perceptual operation defined in gamma/display space (Photoshop, GIMP, etc.).
// Convert to sRGB, invert there, then convert back to linear. The sRGB helpers
// (linear_to_srgb / srgb_to_linear) come from the prepended COLOR_WGSL prelude.
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let src = textureSample(source_tex, source_sampler, in.uv);
let amount = uniforms.params0.x; // params[0]
let inverted = vec3<f32>(1.0) - src.rgb;
let result = mix(src.rgb, inverted, amount * uniforms.mix);
let src_srgb = linear_to_srgb(src.rgb);
let inverted = vec3<f32>(1.0) - src_srgb;
let result_srgb = mix(src_srgb, inverted, amount * uniforms.mix);
return vec4<f32>(result, src.a);
return vec4<f32>(srgb_to_linear(result_srgb), src.a);
}