Give every mask its own tone curve.

Local masks could only shift tone globally, so a burnt sky or a backlit face had
to fight the frame-wide sliders. Each mask now carries an RGB point curve,
applied to the masked pixels before the mask blend. All eight curves ride in one
256×8 LUT texture, so the grade shader needs a single extra sampler rather than
eight. Masks saved before this exist migrate to an identity curve, so old edits
keep their look; the render check fails if the curve stops reaching the grade,
and a unit test pins the LUT's identity, per-row isolation and monotonicity.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-28 15:42:24 +00:00
parent 00e6fee9bd
commit acf582251f
6 changed files with 159 additions and 2 deletions
+7
View File
@@ -1670,6 +1670,13 @@ export function LocalPanel({
))}
</div>
<div className="local-subhead">Mask curve</div>
<CurveEditor
points={active.curve}
onChange={(points) => patch({ curve: points }, 'Mask curve')}
color="#f2f4f8"
/>
<div className="local-subhead">Light</div>
<Slider
label="Exposure"
+57 -1
View File
@@ -1,6 +1,6 @@
import type { LensCorrection } from '../lib/lensEngine';
import type { Preview16 } from '../lib/preview16';
import type { Edits, GeometryEdits } from '../types';
import type { CurvePoint, Edits, GeometryEdits, LocalLayer } from '../types';
import { buildSampler, clamp01 } from '../lib/curves';
import { bwFilterRgb, MAX_HEAL_SPOTS, MAX_LOCAL_LAYERS } from '../lib/edits';
import { wbGainFor } from '../lib/whiteBalance';
@@ -82,6 +82,9 @@ uniform vec4 u_localAdj2[8]; // saturation, highlights, shadows, unused
uniform vec4 u_localAdj3[8]; // whites, blacks, vibrance, hue degrees
uniform vec4 u_localRange[8]; // lumaMin, lumaMax, hue, hueRange
uniform vec4 u_localRange2[8]; // satMin, enabled, unused, unused
// 256×8 LUT, one row per layer: the per-mask tone curve, applied to the masked
// pixels only so a burnt sky can hold its own contrast independently.
uniform sampler2D u_localCurves;
// Lens/perspective correction: distortion, vertical, horizontal, rotation.
uniform vec4 u_geom;
@@ -543,6 +546,13 @@ void main(){
1.0,
adj3.z
);
// Per-mask curve, looked up in this layer's LUT row before the mask blend.
float curveRow = (float(i) + 0.5) / 8.0;
local = vec3(
texture(u_localCurves, vec2(clamp(local.r, 0.0, 1.0), curveRow)).r,
texture(u_localCurves, vec2(clamp(local.g, 0.0, 1.0), curveRow)).g,
texture(u_localCurves, vec2(clamp(local.b, 0.0, 1.0), curveRow)).b
);
srgb = mix(srgb, clamp(local, 0.0, 1.0), m);
}
}
@@ -1137,6 +1147,28 @@ export function buildCurveLUT(edits: Edits): Uint8Array {
return data;
}
/**
* Per-mask curve LUT: 256 samples per row, eight rows (one per layer), RGB in
* the colour channels. The running maximum keeps the curve monotonic for the
* same reason the global one does — a folded mask curve posterises the area.
*/
export function buildLocalCurveLUT(layers: { curve?: CurvePoint[] }[]): Uint8Array {
const data = new Uint8Array(256 * 8 * 4);
for (let row = 0; row < 8; row++) {
const points = layers[row]?.curve;
const sampler = points && points.length >= 2 ? buildSampler(points) : (x: number) => x;
let previous = 0;
for (let i = 0; i < 256; i++) {
const value = Math.max(previous, Math.min(1, sampler(i / 255)));
previous = value;
const o = (row * 256 + i) * 4;
data[o] = data[o + 1] = data[o + 2] = Math.round(value * 255);
data[o + 3] = 255;
}
}
return data;
}
class Prog {
private cache = new Map<string, WebGLUniformLocation | null>();
constructor(private gl: WebGL2RenderingContext, public program: WebGLProgram) {}
@@ -1277,6 +1309,8 @@ export class Renderer {
private denMix = 0;
private denBitmap: ImageBitmap | null = null;
private curveTex: WebGLTexture;
/** 256×8 LUT of per-mask curves; created on first use. */
private localCurveTex: WebGLTexture | null = null;
private targets: { graded?: Target; smallA?: Target; smallB?: Target; bigA?: Target; bigB?: Target } = {};
private histTarget: Target | null = null;
private pickTarget: Target | null = null;
@@ -1706,6 +1740,26 @@ export class Renderer {
return layers.length;
}
/** One 256×8 texture carries every layer's mask curve, so no extra units. */
private uploadLocalCurves(layers: LocalLayer[]): WebGLTexture {
const gl = this.gl;
let tex = this.localCurveTex;
if (!tex) {
tex = gl.createTexture();
this.localCurveTex = tex;
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
}
const data = buildLocalCurveLUT(layers);
gl.bindTexture(gl.TEXTURE_2D, tex);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, 256, 8, 0, gl.RGBA, gl.UNSIGNED_BYTE, data);
return tex;
}
private uploadLensMaps() {
const gl = this.gl;
const correction = this.lensCorrection;
@@ -1810,8 +1864,10 @@ export class Renderer {
g.f('u_lensScale', lens?.scale ?? 1);
g.f('u_lensSplit', this.lensSplit);
const curveTex = this.uploadLocalCurves(edits.local ?? []);
g.tex('u_masks', this.maskTex, 2);
g.tex('u_masks2', this.maskTex2, 7);
g.tex('u_localCurves', curveTex, 8);
g.i('u_localCount', localCount);
for (let i = 0; i < localCount; i++) {
const layer = edits.local[i];
+19
View File
@@ -131,6 +131,7 @@ export function defaultLocalLayer(kind: LocalMaskKind, index: number): LocalLaye
linear: { x1: 0.5, y1: 0.2, x2: 0.5, y2: 0.8 },
radial: { cx: 0.5, cy: 0.5, rx: 0.25, ry: 0.3 },
range: { enabled: false, lumaMin: 0, lumaMax: 100, hue: 30, hueRange: 180, satMin: 0 },
curve: identityCurve(),
adjust: defaultLocalAdjust(),
};
}
@@ -269,6 +270,23 @@ function rasterMask(input: unknown): RasterMask | undefined {
return { data: raw.data, width, height };
}
/**
* Per-mask curve points. Masks saved before the field existed have none, and
* they must come back as the identity rather than leaving the shader LUT row
* undefined.
*/
function localCurve(input: unknown): CurvePoint[] {
if (!Array.isArray(input)) return identityCurve();
const points: CurvePoint[] = [];
for (const raw of input.slice(0, 16)) {
if (!raw || typeof raw !== 'object') continue;
const point = raw as Partial<CurvePoint>;
points.push({ x: clamp01(num(point.x, 0, 0, 1)), y: clamp01(num(point.y, 0, 0, 1)) });
}
points.sort((a, b) => a.x - b.x);
return points.length >= 2 ? points : identityCurve();
}
function localLayers(input: unknown): LocalLayer[] {
if (!Array.isArray(input)) return [];
const out: LocalLayer[] = [];
@@ -311,6 +329,7 @@ function localLayers(input: unknown): LocalLayer[] {
hueRange: num(range.hueRange, 180, 0, 180),
satMin: num(range.satMin, 0, 0, 100),
},
curve: localCurve(layer.curve),
adjust: {
exposure: num(adjust.exposure, 0, -4, 4),
contrast: num(adjust.contrast, 0, -100, 100),
+40
View File
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest';
import { buildLocalCurveLUT } from '../gl/renderer';
/** Bytes of one LUT row (red channel; R/G/B are written identically). */
function row(data: Uint8Array, index: number) {
return Array.from(data.slice(index * 256 * 4, (index + 1) * 256 * 4)).filter((_, i) => i % 4 === 0);
}
describe('mask curve LUT', () => {
it('is the identity for a mask without a curve', () => {
const values = row(buildLocalCurveLUT([{}]), 0);
expect(values[0]).toBe(0);
expect(values[64]).toBeGreaterThan(60);
expect(values[64]).toBeLessThan(69);
expect(values[255]).toBe(255);
});
it('lifts only the row belonging to that mask', () => {
const data = buildLocalCurveLUT([{}, { curve: [{ x: 0, y: 0.25 }, { x: 1, y: 1 }] }]);
expect(row(data, 0)[64]).toBeLessThan(80);
expect(row(data, 1)[64]).toBeGreaterThan(100);
});
it('keeps an inverted curve monotonic instead of folding', () => {
// A curve that dips and rises again would posterise the masked area.
const values = row(
buildLocalCurveLUT([
{ curve: [{ x: 0, y: 0 }, { x: 0.3, y: 0.8 }, { x: 0.7, y: 0.2 }, { x: 1, y: 1 }] },
]),
0,
);
for (let i = 1; i < values.length; i++) expect(values[i]).toBeGreaterThanOrEqual(values[i - 1]);
});
it('fills all eight rows so a ninth mask cannot read stale data', () => {
const data = buildLocalCurveLUT([{ curve: [{ x: 0, y: 0 }, { x: 1, y: 0.5 }] }]);
expect(data.length).toBe(256 * 8 * 4);
expect(row(data, 7)[255]).toBe(255);
});
});
+5
View File
@@ -86,6 +86,11 @@ export interface LocalLayer {
hueRange: number;
satMin: number;
};
/**
* Per-mask RGB tone curve, applied to the masked pixels only. Identity for
* masks created before this existed, so old edits keep their look.
*/
curve: CurvePoint[];
adjust: {
exposure: number;
contrast: number;