fix color space for raster editing

This commit is contained in:
Skyler Lehmkuhl
2026-03-04 11:25:15 -05:00
parent 885c52c02a
commit 759e41d84a
3 changed files with 169 additions and 167 deletions
@@ -1,8 +1,12 @@
// Canvas blit shader.
//
// Renders a GPU raster canvas (at document resolution) into the layer's sRGB
// render buffer (at viewport resolution), applying the camera transform
// (pan + zoom) to map document-space pixels to viewport-space pixels.
// Renders a GPU raster canvas (at document resolution) into an Rgba16Float HDR
// buffer (at viewport resolution), applying the camera transform (pan + zoom)
// to map document-space pixels to viewport-space pixels.
//
// The canvas stores premultiplied linear RGBA. We output it as-is so the HDR
// compositor sees the same premultiplied-linear format it always works with,
// bypassing the sRGB intermediate used for Vello layers.
//
// Any viewport pixel whose corresponding document coordinate falls outside
// [0, canvas_w) × [0, canvas_h) outputs transparent black.
@@ -42,17 +46,6 @@ fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {
return out;
}
// Linear → sRGB encoding for a single channel.
// Applied to premultiplied linear values so the downstream srgb_to_linear
// pass round-trips correctly without darkening semi-transparent edges.
fn linear_to_srgb(c: f32) -> f32 {
return select(
1.055 * pow(max(c, 0.0), 1.0 / 2.4) - 0.055,
c * 12.92,
c <= 0.0031308,
);
}
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
// Map viewport UV [0,1] → viewport pixel
@@ -71,23 +64,11 @@ fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
}
// The canvas stores premultiplied linear RGBA.
// The downstream pipeline (srgb_to_linear → compositor) expects the sRGB
// buffer to contain straight-alpha sRGB, i.e. the same format Vello outputs:
// sRGB buffer: srgb(r_straight), srgb(g_straight), srgb(b_straight), a
// srgb_to_linear: r_straight, g_straight, b_straight, a (linear straight)
// compositor: r_straight * a * opacity (premultiplied, correct)
//
// Without unpremultiplying, the compositor would double-premultiply:
// src = (premul_r, premul_g, premul_b, a) → output = premul_r * a = r * a²
// which produces a dark halo over transparent regions.
// The compositor expects straight-alpha linear (it premultiplies by src_alpha itself),
// so unpremultiply here. No sRGB conversion — the HDR buffer is linear throughout.
let c = textureSample(canvas_tex, canvas_sampler, canvas_uv);
let mask = textureSample(mask_tex, mask_sampler, canvas_uv).r;
let masked_a = c.a * mask;
let inv_a = select(0.0, 1.0 / c.a, c.a > 1e-6);
return vec4<f32>(
linear_to_srgb(c.r * inv_a),
linear_to_srgb(c.g * inv_a),
linear_to_srgb(c.b * inv_a),
masked_a,
);
return vec4<f32>(c.r * inv_a, c.g * inv_a, c.b * inv_a, masked_a);
}