Skip to content

Commit 2b33db9

Browse files
committed
fix(core): shrink LUTs wider than the GPU texture limit instead of rendering black
1 parent 6cb7c8c commit 2b33db9

4 files changed

Lines changed: 144 additions & 5 deletions

File tree

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

Lines changed: 73 additions & 1 deletion
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
@@ -119,3 +125,69 @@ describe("cube LUT parsing", () => {
119125
).toThrow(CubeLutParseError);
120126
});
121127
});
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)