Skip to content

Commit 9284e69

Browse files
fix(core): accept 65-point .cube LUTs, the size grading apps export (#6921)
* fix(core): accept 65-point .cube LUTs, the size grading apps export * fix(core): shrink LUTs wider than the GPU texture limit instead of rendering black
1 parent 5540f49 commit 9284e69

10 files changed

Lines changed: 165 additions & 18 deletions

File tree

‎docs/guides/media-effects.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ puts skin tones on one known line.
9393

9494
## LUTs
9595

96-
A LUT (a `.cube` file, up to 64 points per side) maps every color to another, blended in at the strength you set.
96+
A LUT (a `.cube` file, up to 65 points per side) maps every color to another, blended in at the strength you set.
9797
Use creative Rec.709 LUTs. HyperFrames doesn't convert camera LOG footage first, so a LOG clip needs its camera's own
9898
transform or comes out wrong.
9999

‎packages/cli/src/commands/grade-compare.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ export async function prepareGradeCompareTempProject(opts: {
447447
}
448448
const lutText = readFileSync(sourcePath, "utf-8");
449449
try {
450-
parseCubeLut(lutText, { maxSize: 64 });
450+
parseCubeLut(lutText);
451451
} catch (err) {
452452
throw new Error(
453453
`LUT for "${cell.label}" is not a valid .cube: ${normalizeErrorMessage(err)}`,

‎packages/cli/src/media-use/lib/cube-validate.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { readFileSync } from "node:fs";
88
import { isMainModule } from "./main-module.mjs";
99

10-
export const DEFAULT_MAX_CUBE_LUT_SIZE = 64;
10+
export const DEFAULT_MAX_CUBE_LUT_SIZE = 65;
1111

1212
const DEFAULT_DOMAIN_MIN = [0, 0, 0];
1313
const DEFAULT_DOMAIN_MAX = [1, 1, 1];

‎packages/cli/src/media-use/lib/cube-validate.test.mjs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ test("accepts a valid minimal 3D cube LUT", () => {
2828
});
2929

3030
test("rejects oversize LUTs with the core parser message", () => {
31-
const result = validateCube("LUT_3D_SIZE 65", { maxSize: 64 });
31+
const result = validateCube("LUT_3D_SIZE 66");
3232
assert.equal(result.ok, false);
33-
assert.match(result.error, /LUT_3D_SIZE 65 exceeds max 64/);
33+
assert.match(result.error, /LUT_3D_SIZE 66 exceeds max 65/);
34+
});
35+
36+
test("accepts the 65-point cube that grading tools export", () => {
37+
const rows = Array.from({ length: 65 ** 3 }, () => "0 0 0").join("\n");
38+
assert.deepEqual(validateCube(`LUT_3D_SIZE 65\n${rows}`), { ok: true, size: 65 });
3439
});
3540

3641
test("rejects data rows before LUT_3D_SIZE", () => {
@@ -96,7 +101,7 @@ test("CLI exits zero for valid files and non-zero for invalid files", () => {
96101
const valid = join(dir, "valid.cube");
97102
const invalid = join(dir, "invalid.cube");
98103
writeFileSync(valid, IDENTITY_2);
99-
writeFileSync(invalid, "LUT_3D_SIZE 65");
104+
writeFileSync(invalid, "LUT_3D_SIZE 66");
100105

101106
const out = execFileSync(
102107
process.execPath,
@@ -117,7 +122,7 @@ test("CLI exits zero for valid files and non-zero for invalid files", () => {
117122
stdio: "pipe",
118123
},
119124
),
120-
(err) => err.status === 1 && String(err.stderr).includes("exceeds max 64"),
125+
(err) => err.status === 1 && String(err.stderr).includes("exceeds max 65"),
121126
);
122127
} finally {
123128
rmSync(dir, { recursive: true, force: true });

‎packages/cli/src/media-use/resolve.test.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ test("--from rejects invalid lut cube without registering or leaving a frozen fi
13141314
assert.equal(proc.status, 1, proc.stderr);
13151315
const parsed = JSON.parse(proc.stdout);
13161316
assert.equal(parsed.ok, false);
1317-
assert.match(parsed.error, /^ingested LUT is invalid: LUT_3D_SIZE 999 exceeds max 64/);
1317+
assert.match(parsed.error, /^ingested LUT is invalid: LUT_3D_SIZE 999 exceeds max 65/);
13181318
assert.equal(readManifest(tmp).length, 0);
13191319
const lutDir = join(tmp, ".media/luts");
13201320
assert.deepEqual(existsSync(lutDir) ? readdirSync(lutDir) : [], []);

‎packages/core/src/colorGrading.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe("color grading", () => {
155155
expect(capabilities.palettes.map(({ id }) => id)).toContain("handheld-green");
156156
expect(capabilities.animatable.map(({ path }) => path)).toContain("effects.kuwahara");
157157
expect(capabilities.palette).toEqual({ minColors: 2, maxColors: 6, colorFormat: "#rrggbb" });
158-
expect(capabilities.lut).toMatchObject({ format: "3d-cube", maxCubeSize: 64 });
158+
expect(capabilities.lut).toMatchObject({ format: "3d-cube", maxCubeSize: 65 });
159159
expect(capabilities.effects.find(({ key }) => key === "ascii")).toMatchObject({
160160
supportsPalette: true,
161161
renderLane: "single-pass",

‎packages/core/src/colorLuts.test.ts‎

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { describe, expect, it } from "vitest";
2-
import { CubeLutParseError, packCubeLutToRgba8, parseCubeLut } from "./colorLuts";
2+
import {
3+
CubeLutParseError,
4+
fitCubeLutToTexture,
5+
packCubeLutToRgba8,
6+
parseCubeLut,
7+
type CubeLut3D,
8+
} from "./colorLuts";
39

410
const IDENTITY_2 = `
511
# comment
@@ -102,9 +108,12 @@ describe("cube LUT parsing", () => {
102108
`),
103109
).toThrow("Expected 8 LUT rows");
104110

105-
expect(() => parseCubeLut("LUT_3D_SIZE 65", { maxSize: 64 })).toThrow(
106-
"LUT_3D_SIZE 65 exceeds max 64",
107-
);
111+
expect(() => parseCubeLut("LUT_3D_SIZE 66")).toThrow("LUT_3D_SIZE 66 exceeds max 65");
112+
});
113+
114+
it("accepts the 65-point cube that grading tools export", () => {
115+
const rows = Array.from({ length: 65 ** 3 }, () => "0 0 0").join("\n");
116+
expect(parseCubeLut(`LUT_3D_SIZE 65\n${rows}`).size).toBe(65);
108117
});
109118

110119
it("reports invalid numbers as cube parse errors", () => {
@@ -116,3 +125,69 @@ describe("cube LUT parsing", () => {
116125
).toThrow(CubeLutParseError);
117126
});
118127
});
128+
129+
function identityCube(size: number): CubeLut3D {
130+
const data = new Float32Array(size ** 3 * 3);
131+
for (let b = 0; b < size; b++)
132+
for (let g = 0; g < size; g++)
133+
for (let r = 0; r < size; r++) {
134+
const at = ((b * size + g) * size + r) * 3;
135+
data.set([r / (size - 1), g / (size - 1), b / (size - 1)], at);
136+
}
137+
return { title: null, size, domainMin: [0, 0, 0], domainMax: [1, 1, 1], data };
138+
}
139+
140+
describe("fitCubeLutToTexture", () => {
141+
it("resamples a cube whose packed row is wider than the texture limit, keeping an identity identity", () => {
142+
const fitted = fitCubeLutToTexture(identityCube(65), 4096);
143+
144+
expect(fitted.size).toBe(64);
145+
expect(packCubeLutToRgba8(fitted).width).toBeLessThanOrEqual(4096);
146+
const expected = identityCube(64).data;
147+
const worst = expected.reduce((m, v, i) => Math.max(m, Math.abs(v - fitted.data[i]!)), 0);
148+
expect(worst).toBeLessThan(1e-5);
149+
});
150+
151+
it("keeps the domain and title of the cube it resamples", () => {
152+
const cube = {
153+
...identityCube(5),
154+
title: "warm",
155+
domainMin: [0, 0, 0.1],
156+
domainMax: [1, 2, 3],
157+
};
158+
159+
const fitted = fitCubeLutToTexture(cube as CubeLut3D, 16);
160+
161+
expect(fitted).toMatchObject({
162+
size: 4,
163+
title: "warm",
164+
domainMin: [0, 0, 0.1],
165+
domainMax: [1, 2, 3],
166+
});
167+
});
168+
169+
it("interpolates between the source points rather than dropping some", () => {
170+
const cube = identityCube(5);
171+
[0, 0, 1, 1, 1].forEach((red, r) => {
172+
cube.data[r * 3] = red;
173+
});
174+
175+
const fitted = fitCubeLutToTexture(cube, 16);
176+
177+
const alongRed = [0, 1, 2, 3].map((r) => fitted.data[r * 3]!);
178+
expect(alongRed[0]).toBeCloseTo(0, 5);
179+
expect(alongRed[1]).toBeCloseTo(1 / 3, 5);
180+
expect(alongRed[2]).toBeCloseTo(1, 5);
181+
expect(alongRed[3]).toBeCloseTo(1, 5);
182+
});
183+
184+
it.each([
185+
["a cube that already fits", 64, 4096],
186+
["a texture limit it could not read", 65, Number.NaN],
187+
["a texture limit of zero", 65, 0],
188+
])("returns the same cube for %s", (_label, size, limit) => {
189+
const cube = identityCube(size);
190+
191+
expect(fitCubeLutToTexture(cube, limit)).toBe(cube);
192+
});
193+
});

‎packages/core/src/colorLuts.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class CubeLutParseError extends Error {
3030

3131
const DEFAULT_DOMAIN_MIN: CubeLutVec3 = [0, 0, 0];
3232
const DEFAULT_DOMAIN_MAX: CubeLutVec3 = [1, 1, 1];
33-
export const DEFAULT_MAX_CUBE_LUT_SIZE = 64;
33+
export const DEFAULT_MAX_CUBE_LUT_SIZE = 65;
3434

3535
function stripComment(line: string): string {
3636
let inQuote = false;
@@ -224,3 +224,38 @@ export function packCubeLutToRgba8(lut: CubeLut3D): PackedCubeLut2D {
224224

225225
return { width, height, data: packed };
226226
}
227+
228+
export function fitCubeLutToTexture(lut: CubeLut3D, maxTextureSize: number): CubeLut3D {
229+
if (!(maxTextureSize >= 4) || lut.size * lut.size <= maxTextureSize) return lut;
230+
const size = Math.max(2, Math.floor(Math.sqrt(maxTextureSize)));
231+
const data = new Float32Array(size * size * size * 3);
232+
const last = lut.size - 1;
233+
const at = (r: number, g: number, b: number, c: number): number =>
234+
lut.data[((b * lut.size + g) * lut.size + r) * 3 + c] ?? 0;
235+
const lerp = (a: number, b: number, t: number): number => a + (b - a) * t;
236+
for (let b = 0; b < size; b++) {
237+
for (let g = 0; g < size; g++) {
238+
for (let r = 0; r < size; r++) {
239+
const pr = (r / (size - 1)) * last;
240+
const pg = (g / (size - 1)) * last;
241+
const pb = (b / (size - 1)) * last;
242+
const r0 = Math.floor(pr);
243+
const g0 = Math.floor(pg);
244+
const b0 = Math.floor(pb);
245+
const r1 = Math.min(last, r0 + 1);
246+
const g1 = Math.min(last, g0 + 1);
247+
const b1 = Math.min(last, b0 + 1);
248+
for (let c = 0; c < 3; c++) {
249+
const plane = (bi: number): number =>
250+
lerp(
251+
lerp(at(r0, g0, bi, c), at(r1, g0, bi, c), pr - r0),
252+
lerp(at(r0, g1, bi, c), at(r1, g1, bi, c), pr - r0),
253+
pg - g0,
254+
);
255+
data[((b * size + g) * size + r) * 3 + c] = lerp(plane(b0), plane(b1), pb - b0);
256+
}
257+
}
258+
}
259+
}
260+
return { ...lut, size, data };
261+
}

‎packages/core/src/runtime/colorGrading.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ LUT_3D_SIZE 2
3232
1 1 1
3333
`;
3434

35+
const IDENTITY_3 = `
36+
LUT_3D_SIZE 3
37+
${Array.from({ length: 27 }, (_, i) => {
38+
const [r, g, b] = [i % 3, Math.floor(i / 3) % 3, Math.floor(i / 9)];
39+
return `${r / 2} ${g / 2} ${b / 2}`;
40+
}).join("\n")}
41+
`;
42+
3543
function createMockWebGl(
3644
options: {
3745
failMediaUpload?: boolean;
3846
halfFloatSupported?: boolean;
3947
drawingBuffer?: { width: number; height: number };
48+
maxTextureSize?: number;
4049
} = {},
4150
): WebGLRenderingContext {
4251
const shader = {};
@@ -58,6 +67,8 @@ function createMockWebGl(
5867
TEXTURE_MIN_FILTER: 0x2801,
5968
TEXTURE_MAG_FILTER: 0x2800,
6069
CLAMP_TO_EDGE: 0x812f,
70+
MAX_TEXTURE_SIZE: 0x0d33,
71+
getParameter: vi.fn(() => options.maxTextureSize ?? 16384),
6172
LINEAR: 0x2601,
6273
NEAREST: 0x2600,
6374
RGBA: 0x1908,
@@ -568,6 +579,24 @@ describe("createColorGradingRuntime", () => {
568579
toDataUrl.mockRestore();
569580
});
570581

582+
it("shrinks a LUT whose packed width exceeds the GPU texture limit", async () => {
583+
getContextSpy.mockImplementation((type: string) =>
584+
type === "webgl" ? createMockWebGl({ maxTextureSize: 4 }) : mock2d(),
585+
);
586+
const video = makeDrawableVideo();
587+
video.removeAttribute(HF_COLOR_GRADING_ATTR);
588+
document.body.appendChild(video);
589+
stubCubeLutFetch(IDENTITY_3);
590+
runtime = createColorGradingRuntime();
591+
592+
await runtime.renderPreviews("#hero-video", [
593+
{ id: "lut", grading: { lut: { src: "/looks/test.cube", intensity: 1 } } },
594+
]);
595+
596+
const lutUpload = texImage2DCalls.filter((args) => args.length === 9).at(-1);
597+
expect(lutUpload?.slice(3, 5)).toEqual([4, 2]);
598+
});
599+
571600
it("serializes preview batches while a LUT is loading", async () => {
572601
const video = makeDrawableVideo();
573602
video.removeAttribute(HF_COLOR_GRADING_ATTR);

‎packages/core/src/runtime/colorGrading.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
} from "../colorGradingCurves";
3535
import {
3636
DEFAULT_MAX_CUBE_LUT_SIZE,
37+
fitCubeLutToTexture,
3738
packCubeLutToRgba8,
3839
parseCubeLut,
3940
unitFloatToByte,
@@ -2057,13 +2058,14 @@ function uploadEntryLut(
20572058
lut: CubeLut3D,
20582059
): RuntimeLutTexture | null {
20592060
if (entry.lut?.src === src) return entry.lut;
2060-
const packed = packCubeLutToRgba8(lut);
20612061
const { gl, program } = entry;
2062+
const fitted = fitCubeLutToTexture(lut, gl.getParameter(gl.MAX_TEXTURE_SIZE));
2063+
const packed = packCubeLutToRgba8(fitted);
20622064
try {
20632065
uploadLutTexture(gl, program.lutTexture, packed);
20642066
entry.lut = {
20652067
src,
2066-
size: lut.size,
2068+
size: fitted.size,
20672069
domainMin: lut.domainMin,
20682070
domainMax: lut.domainMax,
20692071
textureWidth: packed.width,
@@ -2114,12 +2116,13 @@ function uploadPreviewLut(
21142116
src: string,
21152117
lut: CubeLut3D,
21162118
): RuntimeLutTexture {
2117-
const packed = packCubeLutToRgba8(lut);
21182119
const { gl, program } = renderer;
2120+
const fitted = fitCubeLutToTexture(lut, gl.getParameter(gl.MAX_TEXTURE_SIZE));
2121+
const packed = packCubeLutToRgba8(fitted);
21192122
uploadLutTexture(gl, program.lutTexture, packed);
21202123
return {
21212124
src,
2122-
size: lut.size,
2125+
size: fitted.size,
21232126
domainMin: lut.domainMin,
21242127
domainMax: lut.domainMax,
21252128
textureWidth: packed.width,

0 commit comments

Comments
 (0)