agentcut 0.2.25 → 0.2.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -148
- package/dist/config.d.ts +0 -1
- package/dist/config.js +0 -5
- package/dist/config.js.map +1 -1
- package/dist/studio/agents/claude.js +3 -2
- package/dist/studio/agents/claude.js.map +1 -1
- package/dist/studio/chat.js +1 -1
- package/dist/studio/chat.js.map +1 -1
- package/dist/studio/cli.js +40 -21
- package/dist/studio/cli.js.map +1 -1
- package/dist/studio/download.d.ts +24 -0
- package/dist/studio/download.js +234 -0
- package/dist/studio/download.js.map +1 -0
- package/dist/studio/host.d.ts +1 -2
- package/dist/studio/host.js +36 -8
- package/dist/studio/host.js.map +1 -1
- package/dist/studio/housekeeping.d.ts +2 -2
- package/dist/studio/housekeeping.js +18 -17
- package/dist/studio/housekeeping.js.map +1 -1
- package/dist/studio/mcp-guides.js +1 -18
- package/dist/studio/mcp-guides.js.map +1 -1
- package/dist/studio/proc.js +16 -6
- package/dist/studio/proc.js.map +1 -1
- package/dist/studio/proxies.d.ts +4 -0
- package/dist/studio/proxies.js +15 -6
- package/dist/studio/proxies.js.map +1 -1
- package/dist/studio/release-cursor.js +21 -21
- package/dist/studio/release-video.js +10 -10
- package/dist/studio/schema.d.ts +0 -1
- package/dist/studio/schema.js +0 -1
- package/dist/studio/schema.js.map +1 -1
- package/dist/studio/tools.d.ts +0 -4
- package/dist/studio/tools.js +32 -31
- package/dist/studio/tools.js.map +1 -1
- package/dist/studio/voice/kokoro-catalog.d.ts +56 -28
- package/dist/studio/voice/kokoro-catalog.js +56 -28
- package/dist/studio/voice/kokoro-catalog.js.map +1 -1
- package/dist/studio/voice/ming-catalog.d.ts +19 -0
- package/dist/studio/voice/ming-catalog.js +40 -0
- package/dist/studio/voice/ming-catalog.js.map +1 -0
- package/dist/studio/voice/ming.d.ts +12 -0
- package/dist/studio/voice/ming.js +397 -0
- package/dist/studio/voice/ming.js.map +1 -0
- package/dist/studio/voice/previews.d.ts +5 -0
- package/dist/studio/voice/previews.js +50 -0
- package/dist/studio/voice/previews.js.map +1 -0
- package/dist/studio/voice/registry.js +2 -0
- package/dist/studio/voice/registry.js.map +1 -1
- package/dist/studio/voice.d.ts +1 -1
- package/dist/studio/voice.js +5 -6
- package/dist/studio/voice.js.map +1 -1
- package/package.json +56 -56
- package/template/AGENTS.md +14 -14
- package/template/components/README.md +70 -70
- package/template/components/before-after/BeforeAfter.tsx +89 -89
- package/template/components/catalog.ts +107 -107
- package/template/components/cinematic/Device3D.tsx +324 -324
- package/template/components/cinematic/devices3d/display-model.ts +184 -184
- package/template/components/cinematic/devices3d/iphone-model.ts +643 -643
- package/template/components/cinematic/devices3d/macbook-model.ts +608 -608
- package/template/components/cinematic/devices3d/models.ts +657 -657
- package/template/components/cinematic/devices3d/runtime.ts +267 -267
- package/template/components/cinematic/math.ts +100 -100
- package/template/components/color/grade-effect.ts +650 -650
- package/template/components/color/grade.ts +512 -512
- package/template/components/color/looks.ts +154 -154
- package/template/components/countdown/Countdown.tsx +166 -166
- package/template/components/dot-trace-title/DotTraceTitle.tsx +181 -181
- package/template/components/dot-trace-title/meta.json +51 -51
- package/template/components/generate/GenerateReveal.tsx +381 -381
- package/template/components/hook-card/HookCard.tsx +554 -554
- package/template/components/index.ts +199 -199
- package/template/components/lib.ts +191 -191
- package/template/components/line-diagram/LineDiagram.tsx +205 -205
- package/template/components/line-diagram/meta.json +215 -215
- package/template/components/list-card/ListCard.tsx +104 -104
- package/template/components/live-clip.ts +75 -75
- package/template/components/looks.tsx +1447 -1447
- package/template/components/loops/LoopCanvas.tsx +132 -132
- package/template/components/loops/engine.ts +184 -184
- package/template/components/loops/library.ts +667 -667
- package/template/components/loops/library2.ts +777 -777
- package/template/components/motion-loop/MotionLoop.tsx +37 -37
- package/template/components/motion-loop/meta.json +311 -311
- package/template/components/props.ts +73 -73
- package/template/components/quote-card/QuoteCard.tsx +71 -71
- package/template/components/style-layer.tsx +233 -233
- package/template/components/styles.ts +113 -113
- package/template/components/types.ts +66 -66
- package/template/components/word-pop/WordPop.tsx +59 -59
- package/template/components/word-stack/WordStack.tsx +122 -122
- package/template/presets/charcoal-sketch/PROMPT.md +48 -48
- package/template/presets/charcoal-sketch/scripts/duck_music.py +34 -34
- package/template/presets/punk-cut-paper/scenes/Motif.tsx +376 -376
- package/template/presets/punk-cut-paper/scenes/Photo.tsx +73 -73
- package/template/presets/punk-cut-paper/scenes/Ransom.tsx +119 -119
- package/template/runtime/composition.tsx +1189 -1185
- package/template/runtime/helpers.tsx +230 -230
- package/template/runtime/index.tsx +22 -22
- package/template/runtime/types.ts +315 -315
- package/template/skill/SKILL.md +186 -351
- package/template/skill/references/broll.md +44 -44
- package/template/skill/references/browser-export.md +61 -61
- package/template/skill/references/cinematic.md +206 -206
- package/template/skill/references/color.md +79 -79
- package/template/skill/references/craft.md +54 -10
- package/template/skill/references/editing-styles.md +57 -57
- package/template/skill/references/feature-launch.md +5 -1
- package/template/skill/references/footage.md +72 -72
- package/template/skill/references/format.md +142 -142
- package/template/skill/references/how-to.md +0 -15
- package/template/skill/references/launch-direction.md +89 -0
- package/template/skill/references/loops.md +151 -151
- package/template/skill/references/mcp-external.md +19 -0
- package/template/skill/references/performance.md +93 -93
- package/template/skill/references/person-cutout.md +19 -19
- package/template/skill/references/presets.md +60 -60
- package/template/skill/references/product-launch.md +4 -1
- package/template/skill/references/release-capture.md +53 -0
- package/template/skill/references/sound.md +167 -155
- package/template/skill/references/style-spy.md +160 -160
- package/template/skill/references/website-howto.md +45 -45
- package/template/starter/.studio/chat.json +1 -1
- package/template/starter/layers.json +46 -46
- package/template/starter/notes.md +4 -4
- package/template/starter/project.json +13 -13
- package/template/starter/scenes/Title.tsx +38 -38
|
@@ -1,650 +1,650 @@
|
|
|
1
|
-
// The grade on the GPU: one shader for the colour, one optional pass for halation and bloom. A Remotion
|
|
2
|
-
// effect, so the live preview, captures and exports all draw exactly the same pixels.
|
|
3
|
-
import { createEffect, type EffectDescriptor } from "remotion";
|
|
4
|
-
import { bakeCurve, gradePasses, gradeUniforms, hasCurves, scaleGrade, type ColorGrade, type CubeLut, type GradeFields, type GradeUniforms } from "./grade";
|
|
5
|
-
import { gradeLook } from "./looks";
|
|
6
|
-
|
|
7
|
-
const VERTEX = `#version 300 es
|
|
8
|
-
layout(location = 0) in vec2 aPos;
|
|
9
|
-
layout(location = 1) in vec2 aUv;
|
|
10
|
-
out vec2 vUv;
|
|
11
|
-
void main() { vUv = aUv; gl_Position = vec4(aPos, 0.0, 1.0); }
|
|
12
|
-
`;
|
|
13
|
-
|
|
14
|
-
const COMMON = `
|
|
15
|
-
const vec3 REC709 = vec3(0.2126, 0.7152, 0.0722);
|
|
16
|
-
vec3 toLinear(vec3 c) {
|
|
17
|
-
vec3 s = sign(c); c = abs(c);
|
|
18
|
-
return s * mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c));
|
|
19
|
-
}
|
|
20
|
-
vec3 toGamma(vec3 c) {
|
|
21
|
-
vec3 s = sign(c); c = abs(c);
|
|
22
|
-
return s * mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c));
|
|
23
|
-
}
|
|
24
|
-
float hash(vec2 p, float seed) {
|
|
25
|
-
vec3 q = fract(vec3(p.xyx) * 0.1031 + seed * 0.1173);
|
|
26
|
-
q += dot(q, q.yzx + 33.33);
|
|
27
|
-
return fract((q.x + q.y) * q.z);
|
|
28
|
-
}
|
|
29
|
-
// Half a step of triangular noise, so smooth gradients survive the 8-bit output without banding.
|
|
30
|
-
vec3 dither(vec3 c, vec2 p) {
|
|
31
|
-
float n = hash(p, 1.0) + hash(p, 2.0) - 1.0;
|
|
32
|
-
return c + n / 255.0;
|
|
33
|
-
}
|
|
34
|
-
`;
|
|
35
|
-
|
|
36
|
-
const COLOR_FRAGMENT = `#version 300 es
|
|
37
|
-
precision highp float;
|
|
38
|
-
precision highp sampler3D;
|
|
39
|
-
in vec2 vUv;
|
|
40
|
-
out vec4 fragColor;
|
|
41
|
-
uniform sampler2D uSource;
|
|
42
|
-
uniform sampler2D uCurves;
|
|
43
|
-
uniform sampler3D uLut;
|
|
44
|
-
uniform float uBrightness, uContrast, uPivot;
|
|
45
|
-
uniform mat3 uMatrix;
|
|
46
|
-
uniform vec3 uGains;
|
|
47
|
-
uniform float uMatchContrast, uMatchSaturation;
|
|
48
|
-
uniform float uShadows, uHighlights, uWhites, uBlacks;
|
|
49
|
-
uniform float uBlackPoint, uWhitePoint, uGamma;
|
|
50
|
-
uniform vec3 uLift, uWheelGamma, uGain;
|
|
51
|
-
uniform float uVibrance, uSkinProtect, uLutStrength;
|
|
52
|
-
uniform bool uHasCurves, uHasHsl, uHasLut, uDither;
|
|
53
|
-
uniform vec3 uHsl[8];
|
|
54
|
-
uniform vec3 uDomainMin, uDomainMax;
|
|
55
|
-
${COMMON}
|
|
56
|
-
|
|
57
|
-
vec3 rgb2hsv(vec3 c) {
|
|
58
|
-
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
|
59
|
-
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
|
|
60
|
-
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
|
|
61
|
-
float d = q.x - min(q.w, q.y);
|
|
62
|
-
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + 1e-6)), d / (q.x + 1e-6), q.x);
|
|
63
|
-
}
|
|
64
|
-
vec3 hsv2rgb(vec3 c) {
|
|
65
|
-
vec3 p = abs(fract(c.xxx + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0);
|
|
66
|
-
return c.z * mix(vec3(1.0), clamp(p - 1.0, 0.0, 1.0), c.y);
|
|
67
|
-
}
|
|
68
|
-
// Band centres in turns: red, orange, yellow, green, aqua, blue, purple, magenta.
|
|
69
|
-
const float CENTERS[9] = float[9](0.0, 0.0833, 0.1667, 0.3333, 0.5, 0.6667, 0.7778, 0.8889, 1.0);
|
|
70
|
-
vec3 mixer(vec3 c) {
|
|
71
|
-
vec3 hsv = rgb2hsv(clamp(c, 0.0, 1.0));
|
|
72
|
-
float h = fract(hsv.x);
|
|
73
|
-
int i = 0;
|
|
74
|
-
for (int n = 0; n < 8; n++) if (h >= CENTERS[n]) i = n;
|
|
75
|
-
float t = smoothstep(0.0, 1.0, (h - CENTERS[i]) / (CENTERS[i + 1] - CENTERS[i]));
|
|
76
|
-
vec3 band = mix(uHsl[i], uHsl[(i + 1) % 8], t);
|
|
77
|
-
float colorful = smoothstep(0.0, 0.25, hsv.y);
|
|
78
|
-
hsv.x = fract(h + band.x * 0.083);
|
|
79
|
-
vec3 shifted = hsv2rgb(hsv);
|
|
80
|
-
// Saturation around brightness: a desaturated blue goes dark grey, not light grey.
|
|
81
|
-
shifted = mix(vec3(dot(shifted, REC709)), shifted, 1.0 + band.y);
|
|
82
|
-
return shifted * (1.0 + band.z * 0.6 * colorful);
|
|
83
|
-
}
|
|
84
|
-
float skinMask(vec3 c) {
|
|
85
|
-
// Skin of every complexion sits near one hue on the vectorscope (the I line, about 123 degrees).
|
|
86
|
-
float y = dot(c, REC709);
|
|
87
|
-
float cb = (c.b - y) * 0.5389;
|
|
88
|
-
float cr = (c.r - y) * 0.635;
|
|
89
|
-
float chroma = length(vec2(cb, cr));
|
|
90
|
-
if (chroma < 1e-4) return 0.0;
|
|
91
|
-
float angle = atan(cr, cb);
|
|
92
|
-
float distance = abs(angle - 2.147);
|
|
93
|
-
float hue = 1.0 - smoothstep(0.14, 0.4, distance);
|
|
94
|
-
return hue * smoothstep(0.012, 0.04, chroma) * (1.0 - smoothstep(0.26, 0.4, chroma)) * smoothstep(0.06, 0.18, y);
|
|
95
|
-
}
|
|
96
|
-
vec3 sampleLut(vec3 c) {
|
|
97
|
-
vec3 n = clamp((c - uDomainMin) / (uDomainMax - uDomainMin), 0.0, 1.0);
|
|
98
|
-
vec3 size = vec3(textureSize(uLut, 0));
|
|
99
|
-
return texture(uLut, (n * (size - 1.0) + 0.5) / size).rgb;
|
|
100
|
-
}
|
|
101
|
-
void main() {
|
|
102
|
-
vec4 source = texture(uSource, vUv);
|
|
103
|
-
if (source.a <= 0.0) { fragColor = vec4(0.0); return; }
|
|
104
|
-
vec3 original = source.rgb / source.a;
|
|
105
|
-
vec3 c = original;
|
|
106
|
-
|
|
107
|
-
// The original controls, in the order and space CSS filters use.
|
|
108
|
-
c = clamp(c * uBrightness, 0.0, 1.0);
|
|
109
|
-
c = clamp((c - uPivot) * uContrast + uPivot, 0.0, 1.0);
|
|
110
|
-
c = clamp(uMatrix * c, 0.0, 1.0);
|
|
111
|
-
|
|
112
|
-
// Light: exposure, white balance and shot matching are gains on linear light.
|
|
113
|
-
c = toGamma(toLinear(c) * uGains);
|
|
114
|
-
if (uMatchContrast != 1.0) c = (c - 0.45) * uMatchContrast + 0.45;
|
|
115
|
-
if (uMatchSaturation != 1.0) c = mix(vec3(dot(c, REC709)), c, uMatchSaturation);
|
|
116
|
-
|
|
117
|
-
// Tone regions, picked by brightness.
|
|
118
|
-
float y = dot(c, REC709);
|
|
119
|
-
c += uShadows * 0.32 * pow(clamp(1.0 - y, 0.0, 1.0), 2.0) * smoothstep(0.0, 0.08, y);
|
|
120
|
-
c += uHighlights * 0.32 * pow(smoothstep(0.35, 1.0, y), 2.0);
|
|
121
|
-
c += uWhites * 0.18 * smoothstep(0.7, 1.0, y);
|
|
122
|
-
c += uBlacks * 0.14 * (1.0 - smoothstep(0.0, 0.28, y));
|
|
123
|
-
|
|
124
|
-
c = (c - uBlackPoint) / (uWhitePoint - uBlackPoint);
|
|
125
|
-
if (uGamma != 1.0) c = pow(max(c, 0.0), vec3(1.0 / uGamma));
|
|
126
|
-
|
|
127
|
-
// Colour wheels: lift, gamma, gain.
|
|
128
|
-
c = c + uLift * (1.0 - clamp(c, 0.0, 1.0));
|
|
129
|
-
c = pow(max(c, 0.0), 1.0 / uWheelGamma);
|
|
130
|
-
c *= uGain;
|
|
131
|
-
|
|
132
|
-
if (uHasCurves) {
|
|
133
|
-
c = clamp(c, 0.0, 1.0);
|
|
134
|
-
c = vec3(texture(uCurves, vec2(c.r, 0.5)).r, texture(uCurves, vec2(c.g, 0.5)).g, texture(uCurves, vec2(c.b, 0.5)).b);
|
|
135
|
-
c = vec3(texture(uCurves, vec2(c.r, 0.5)).a, texture(uCurves, vec2(c.g, 0.5)).a, texture(uCurves, vec2(c.b, 0.5)).a);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (uVibrance != 0.0) {
|
|
139
|
-
// Lifts quiet colours more than loud ones, and leaves skin alone.
|
|
140
|
-
float top = max(c.r, max(c.g, c.b));
|
|
141
|
-
float low = min(c.r, min(c.g, c.b));
|
|
142
|
-
float amount = uVibrance * (1.0 - clamp(top - low, 0.0, 1.0)) * (1.0 - 0.6 * skinMask(original));
|
|
143
|
-
c = mix(vec3(dot(c, REC709)), c, 1.0 + amount);
|
|
144
|
-
}
|
|
145
|
-
if (uHasHsl) c = mixer(c);
|
|
146
|
-
|
|
147
|
-
if (uHasLut && uLutStrength > 0.0) {
|
|
148
|
-
c = clamp(c, 0.0, 1.0);
|
|
149
|
-
c = mix(c, sampleLut(c), uLutStrength);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
if (uSkinProtect > 0.0) {
|
|
153
|
-
// Keep the new brightness, bring back the skin's own colour.
|
|
154
|
-
float newY = dot(c, REC709);
|
|
155
|
-
vec3 skin = newY + (original - dot(original, REC709));
|
|
156
|
-
c = mix(c, skin, uSkinProtect * skinMask(original));
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
c = clamp(c, 0.0, 1.0);
|
|
160
|
-
if (uDither) c = dither(c, gl_FragCoord.xy);
|
|
161
|
-
fragColor = vec4(clamp(c, 0.0, 1.0) * source.a, source.a);
|
|
162
|
-
}
|
|
163
|
-
`;
|
|
164
|
-
|
|
165
|
-
const BRIGHT_FRAGMENT = `#version 300 es
|
|
166
|
-
precision highp float;
|
|
167
|
-
in vec2 vUv;
|
|
168
|
-
out vec4 fragColor;
|
|
169
|
-
uniform sampler2D uSource;
|
|
170
|
-
${COMMON}
|
|
171
|
-
void main() {
|
|
172
|
-
vec4 source = texture(uSource, vUv);
|
|
173
|
-
vec3 c = source.a > 0.0 ? source.rgb / source.a : vec3(0.0);
|
|
174
|
-
float y = dot(c, REC709);
|
|
175
|
-
fragColor = vec4(c * smoothstep(0.62, 0.98, y) * source.a, source.a);
|
|
176
|
-
}
|
|
177
|
-
`;
|
|
178
|
-
|
|
179
|
-
const BLUR_FRAGMENT = `#version 300 es
|
|
180
|
-
precision highp float;
|
|
181
|
-
in vec2 vUv;
|
|
182
|
-
out vec4 fragColor;
|
|
183
|
-
uniform sampler2D uSource;
|
|
184
|
-
uniform vec2 uStep;
|
|
185
|
-
void main() {
|
|
186
|
-
vec4 sum = texture(uSource, vUv) * 0.1964825501511404;
|
|
187
|
-
sum += (texture(uSource, vUv + uStep * 1.411764705882353) + texture(uSource, vUv - uStep * 1.411764705882353)) * 0.2969069646728344;
|
|
188
|
-
sum += (texture(uSource, vUv + uStep * 3.2941176470588234) + texture(uSource, vUv - uStep * 3.2941176470588234)) * 0.09447039785044732;
|
|
189
|
-
sum += (texture(uSource, vUv + uStep * 5.176470588235294) + texture(uSource, vUv - uStep * 5.176470588235294)) * 0.010381362401148057;
|
|
190
|
-
fragColor = sum;
|
|
191
|
-
}
|
|
192
|
-
`;
|
|
193
|
-
|
|
194
|
-
const FINISH_FRAGMENT = `#version 300 es
|
|
195
|
-
precision highp float;
|
|
196
|
-
in vec2 vUv;
|
|
197
|
-
out vec4 fragColor;
|
|
198
|
-
uniform sampler2D uSource, uHalationTexture, uBloomTexture;
|
|
199
|
-
uniform float uHalation, uBloom, uVignette, uGrain, uGrainCell, uSeed, uAspect;
|
|
200
|
-
${COMMON}
|
|
201
|
-
vec3 screen(vec3 a, vec3 b) { return 1.0 - (1.0 - a) * (1.0 - clamp(b, 0.0, 1.0)); }
|
|
202
|
-
void main() {
|
|
203
|
-
vec4 source = texture(uSource, vUv);
|
|
204
|
-
if (source.a <= 0.0) { fragColor = vec4(0.0); return; }
|
|
205
|
-
vec3 c = source.rgb / source.a;
|
|
206
|
-
// Halation: light scattering back through the film base shows as a red-orange rim on highlights.
|
|
207
|
-
if (uHalation > 0.0) c = screen(c, texture(uHalationTexture, vUv).rgb * vec3(1.0, 0.32, 0.1) * uHalation * 2.2);
|
|
208
|
-
if (uBloom > 0.0) c = screen(c, texture(uBloomTexture, vUv).rgb * uBloom * 1.6);
|
|
209
|
-
if (uVignette != 0.0) {
|
|
210
|
-
vec2 p = (vUv - 0.5) * vec2(uAspect, 1.0) / sqrt(uAspect * uAspect + 1.0) * 2.0;
|
|
211
|
-
float fall = smoothstep(0.3, 1.0, length(p));
|
|
212
|
-
c = uVignette > 0.0 ? c * (1.0 - uVignette * 0.85 * fall) : screen(c, vec3(-uVignette * 0.5 * fall));
|
|
213
|
-
}
|
|
214
|
-
if (uGrain > 0.0) {
|
|
215
|
-
vec2 cell = floor(gl_FragCoord.xy / uGrainCell);
|
|
216
|
-
float y = dot(c, REC709);
|
|
217
|
-
// Film grain is strongest in the midtones and fades out in pure black and white.
|
|
218
|
-
float weight = 0.25 + 0.75 * (1.0 - pow(abs(2.0 * y - 1.0), 2.0));
|
|
219
|
-
float mono = hash(cell, uSeed) + hash(cell, uSeed + 7.0) - 1.0;
|
|
220
|
-
vec3 tint = vec3(hash(cell, uSeed + 13.0), hash(cell, uSeed + 19.0), hash(cell, uSeed + 23.0)) - 0.5;
|
|
221
|
-
c += (mono + tint * 0.3) * uGrain * 0.2 * weight;
|
|
222
|
-
}
|
|
223
|
-
c = dither(clamp(c, 0.0, 1.0), gl_FragCoord.xy);
|
|
224
|
-
fragColor = vec4(clamp(c, 0.0, 1.0) * source.a, source.a);
|
|
225
|
-
}
|
|
226
|
-
`;
|
|
227
|
-
|
|
228
|
-
interface Program { handle: WebGLProgram; uniforms: Map<string, WebGLUniformLocation | null> }
|
|
229
|
-
|
|
230
|
-
interface RenderTarget { texture: WebGLTexture; framebuffer: WebGLFramebuffer; width: number; height: number }
|
|
231
|
-
|
|
232
|
-
export interface GradeRendererState {
|
|
233
|
-
gl: WebGL2RenderingContext;
|
|
234
|
-
color: Program;
|
|
235
|
-
/** The finish's programs, compiled the first time a grade uses them: compiling is slow. */
|
|
236
|
-
extra: { bright?: Program; blur?: Program; finish?: Program };
|
|
237
|
-
vao: WebGLVertexArrayObject;
|
|
238
|
-
vbo: WebGLBuffer;
|
|
239
|
-
source: WebGLTexture;
|
|
240
|
-
curves: WebGLTexture;
|
|
241
|
-
lut: WebGLTexture;
|
|
242
|
-
floatTargets: boolean;
|
|
243
|
-
targets: Map<string, RenderTarget>;
|
|
244
|
-
curveKey: string | null;
|
|
245
|
-
lutKey: string | null;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function makeProgram(gl: WebGL2RenderingContext, fragment: string): Program {
|
|
249
|
-
const compile = (type: number, text: string) => {
|
|
250
|
-
const shader = gl.createShader(type)!;
|
|
251
|
-
gl.shaderSource(shader, text);
|
|
252
|
-
gl.compileShader(shader);
|
|
253
|
-
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
254
|
-
const log = gl.getShaderInfoLog(shader);
|
|
255
|
-
gl.deleteShader(shader);
|
|
256
|
-
throw new Error(`Grade shader failed to compile: ${log ?? "(no log)"}`);
|
|
257
|
-
}
|
|
258
|
-
return shader;
|
|
259
|
-
};
|
|
260
|
-
const vertexShader = compile(gl.VERTEX_SHADER, VERTEX);
|
|
261
|
-
const fragmentShader = compile(gl.FRAGMENT_SHADER, fragment);
|
|
262
|
-
const handle = gl.createProgram()!;
|
|
263
|
-
gl.attachShader(handle, vertexShader);
|
|
264
|
-
gl.attachShader(handle, fragmentShader);
|
|
265
|
-
gl.linkProgram(handle);
|
|
266
|
-
gl.deleteShader(vertexShader);
|
|
267
|
-
gl.deleteShader(fragmentShader);
|
|
268
|
-
if (!gl.getProgramParameter(handle, gl.LINK_STATUS)) {
|
|
269
|
-
const log = gl.getProgramInfoLog(handle);
|
|
270
|
-
gl.deleteProgram(handle);
|
|
271
|
-
throw new Error(`Grade shader failed to link: ${log ?? "(no log)"}`);
|
|
272
|
-
}
|
|
273
|
-
return { handle, uniforms: new Map() };
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
function uniform(gl: WebGL2RenderingContext, program: Program, name: string) {
|
|
277
|
-
if (!program.uniforms.has(name)) program.uniforms.set(name, gl.getUniformLocation(program.handle, name));
|
|
278
|
-
return program.uniforms.get(name) ?? null;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function texture2d(gl: WebGL2RenderingContext) {
|
|
282
|
-
const texture = gl.createTexture()!;
|
|
283
|
-
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
284
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
285
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
286
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
287
|
-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
288
|
-
return texture;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
export function setupGradeRenderer(canvas: HTMLCanvasElement | OffscreenCanvas): GradeRendererState {
|
|
292
|
-
const gl = canvas.getContext("webgl2", { premultipliedAlpha: true, alpha: true, preserveDrawingBuffer: true }) as WebGL2RenderingContext | null;
|
|
293
|
-
if (!gl) throw new Error("The color grade needs WebGL2, which this browser could not provide.");
|
|
294
|
-
const vao = gl.createVertexArray()!;
|
|
295
|
-
const vbo = gl.createBuffer()!;
|
|
296
|
-
gl.bindVertexArray(vao);
|
|
297
|
-
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
|
|
298
|
-
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1]), gl.STATIC_DRAW);
|
|
299
|
-
gl.enableVertexAttribArray(0);
|
|
300
|
-
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 16, 0);
|
|
301
|
-
gl.enableVertexAttribArray(1);
|
|
302
|
-
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 16, 8);
|
|
303
|
-
gl.bindVertexArray(null);
|
|
304
|
-
const lut = gl.createTexture()!;
|
|
305
|
-
gl.bindTexture(gl.TEXTURE_3D, lut);
|
|
306
|
-
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
307
|
-
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
308
|
-
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
309
|
-
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
310
|
-
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
|
|
311
|
-
gl.bindTexture(gl.TEXTURE_3D, null);
|
|
312
|
-
return {
|
|
313
|
-
gl,
|
|
314
|
-
color: makeProgram(gl, COLOR_FRAGMENT),
|
|
315
|
-
extra: {},
|
|
316
|
-
vao,
|
|
317
|
-
vbo,
|
|
318
|
-
source: texture2d(gl),
|
|
319
|
-
curves: texture2d(gl),
|
|
320
|
-
lut,
|
|
321
|
-
floatTargets: Boolean(gl.getExtension("EXT_color_buffer_float")),
|
|
322
|
-
targets: new Map(),
|
|
323
|
-
curveKey: null,
|
|
324
|
-
lutKey: null,
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
export function cleanupGradeRenderer(state: GradeRendererState) {
|
|
329
|
-
const { gl } = state;
|
|
330
|
-
for (const program of [state.color, ...Object.values(state.extra)]) gl.deleteProgram(program.handle);
|
|
331
|
-
for (const target of state.targets.values()) {
|
|
332
|
-
gl.deleteTexture(target.texture);
|
|
333
|
-
gl.deleteFramebuffer(target.framebuffer);
|
|
334
|
-
}
|
|
335
|
-
state.targets.clear();
|
|
336
|
-
gl.deleteTexture(state.source);
|
|
337
|
-
gl.deleteTexture(state.curves);
|
|
338
|
-
gl.deleteTexture(state.lut);
|
|
339
|
-
gl.deleteBuffer(state.vbo);
|
|
340
|
-
gl.deleteVertexArray(state.vao);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
const EXTRA_FRAGMENTS = { bright: BRIGHT_FRAGMENT, blur: BLUR_FRAGMENT, finish: FINISH_FRAGMENT };
|
|
344
|
-
|
|
345
|
-
function extraProgram(state: GradeRendererState, name: keyof typeof EXTRA_FRAGMENTS) {
|
|
346
|
-
return (state.extra[name] ??= makeProgram(state.gl, EXTRA_FRAGMENTS[name]));
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
function renderTarget(state: GradeRendererState, name: string, width: number, height: number): RenderTarget {
|
|
350
|
-
const { gl } = state;
|
|
351
|
-
const existing = state.targets.get(name);
|
|
352
|
-
if (existing && existing.width === width && existing.height === height) return existing;
|
|
353
|
-
if (existing) {
|
|
354
|
-
gl.deleteTexture(existing.texture);
|
|
355
|
-
gl.deleteFramebuffer(existing.framebuffer);
|
|
356
|
-
}
|
|
357
|
-
// Creating a texture binds it to the active unit: keep that away from the units the shaders read.
|
|
358
|
-
gl.activeTexture(gl.TEXTURE7);
|
|
359
|
-
const texture = texture2d(gl);
|
|
360
|
-
// Float targets keep the extra precision between passes when the GPU can render to them.
|
|
361
|
-
if (state.floatTargets) gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, width, height, 0, gl.RGBA, gl.HALF_FLOAT, null);
|
|
362
|
-
else gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
|
363
|
-
const framebuffer = gl.createFramebuffer()!;
|
|
364
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
|
|
365
|
-
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
|
|
366
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
367
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
368
|
-
const target = { texture, framebuffer, width, height };
|
|
369
|
-
state.targets.set(name, target);
|
|
370
|
-
return target;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function uploadCurves(state: GradeRendererState, fields: GradeFields) {
|
|
374
|
-
const key = JSON.stringify(fields.curves ?? null);
|
|
375
|
-
if (state.curveKey === key) return;
|
|
376
|
-
const { gl } = state;
|
|
377
|
-
const size = 1024;
|
|
378
|
-
const r = bakeCurve(fields.curves?.r, size);
|
|
379
|
-
const g = bakeCurve(fields.curves?.g, size);
|
|
380
|
-
const b = bakeCurve(fields.curves?.b, size);
|
|
381
|
-
const master = bakeCurve(fields.curves?.master, size);
|
|
382
|
-
const data = new Float32Array(size * 4);
|
|
383
|
-
for (let i = 0; i < size; i += 1) data.set([r[i], g[i], b[i], master[i]], i * 4);
|
|
384
|
-
gl.bindTexture(gl.TEXTURE_2D, state.curves);
|
|
385
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
386
|
-
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
|
|
387
|
-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, size, 1, 0, gl.RGBA, gl.FLOAT, data);
|
|
388
|
-
state.curveKey = key;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
function uploadLut(state: GradeRendererState, lut: CubeLut, key: string) {
|
|
392
|
-
if (state.lutKey === key) return;
|
|
393
|
-
const { gl } = state;
|
|
394
|
-
const max = gl.getParameter(gl.MAX_3D_TEXTURE_SIZE) as number;
|
|
395
|
-
if (lut.size > max) throw new Error(`This LUT (size ${lut.size}) is larger than this GPU's limit of ${max}.`);
|
|
396
|
-
gl.bindTexture(gl.TEXTURE_3D, state.lut);
|
|
397
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
398
|
-
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
|
|
399
|
-
gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA16F, lut.size, lut.size, lut.size, 0, gl.RGBA, gl.FLOAT, lut.data);
|
|
400
|
-
gl.bindTexture(gl.TEXTURE_3D, null);
|
|
401
|
-
state.lutKey = key;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
function setColorUniforms(state: GradeRendererState, u: GradeUniforms, useCurves: boolean, useLut: boolean, lut: CubeLut | null, dither: boolean) {
|
|
405
|
-
const { gl, color } = state;
|
|
406
|
-
const at = (name: string) => uniform(gl, color, name);
|
|
407
|
-
gl.uniform1i(at("uSource"), 0);
|
|
408
|
-
gl.uniform1i(at("uCurves"), 1);
|
|
409
|
-
gl.uniform1i(at("uLut"), 2);
|
|
410
|
-
gl.uniform1f(at("uBrightness"), u.brightness);
|
|
411
|
-
gl.uniform1f(at("uContrast"), u.contrast);
|
|
412
|
-
gl.uniform1f(at("uPivot"), u.pivot);
|
|
413
|
-
// Our rows, GL's columns.
|
|
414
|
-
gl.uniformMatrix3fv(at("uMatrix"), true, u.matrix.flat());
|
|
415
|
-
gl.uniform3fv(at("uGains"), u.gains);
|
|
416
|
-
gl.uniform1f(at("uMatchContrast"), u.matchContrast);
|
|
417
|
-
gl.uniform1f(at("uMatchSaturation"), u.matchSaturation);
|
|
418
|
-
gl.uniform1f(at("uShadows"), u.shadows);
|
|
419
|
-
gl.uniform1f(at("uHighlights"), u.highlights);
|
|
420
|
-
gl.uniform1f(at("uWhites"), u.whites);
|
|
421
|
-
gl.uniform1f(at("uBlacks"), u.blacks);
|
|
422
|
-
gl.uniform1f(at("uBlackPoint"), u.blackPoint);
|
|
423
|
-
gl.uniform1f(at("uWhitePoint"), u.whitePoint);
|
|
424
|
-
gl.uniform1f(at("uGamma"), u.gamma);
|
|
425
|
-
gl.uniform3fv(at("uLift"), u.lift);
|
|
426
|
-
gl.uniform3fv(at("uWheelGamma"), u.wheelGamma);
|
|
427
|
-
gl.uniform3fv(at("uGain"), u.gain);
|
|
428
|
-
gl.uniform1f(at("uVibrance"), u.vibrance);
|
|
429
|
-
gl.uniform1f(at("uSkinProtect"), u.skinProtect);
|
|
430
|
-
gl.uniform1f(at("uLutStrength"), u.lutStrength);
|
|
431
|
-
gl.uniform1i(at("uHasCurves"), useCurves ? 1 : 0);
|
|
432
|
-
gl.uniform1i(at("uHasHsl"), u.hasHsl ? 1 : 0);
|
|
433
|
-
gl.uniform1i(at("uHasLut"), useLut ? 1 : 0);
|
|
434
|
-
gl.uniform1i(at("uDither"), dither ? 1 : 0);
|
|
435
|
-
gl.uniform3fv(at("uHsl"), u.hsl);
|
|
436
|
-
gl.uniform3fv(at("uDomainMin"), lut?.domainMin ?? [0, 0, 0]);
|
|
437
|
-
gl.uniform3fv(at("uDomainMax"), lut?.domainMax ?? [1, 1, 1]);
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
export interface GradeDrawParams {
|
|
441
|
-
fields: GradeFields;
|
|
442
|
-
/** Parsed file for fields.lut, when it has loaded. */
|
|
443
|
-
lut: CubeLut | null;
|
|
444
|
-
/** Grain changes every frame by this number. */
|
|
445
|
-
seed: number;
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
/** Draw `source` through the grade to the canvas this renderer was set up on. */
|
|
449
|
-
export function drawGrade(
|
|
450
|
-
state: GradeRendererState,
|
|
451
|
-
{ source, width, height, flipSourceY, params }: { source: TexImageSource; width: number; height: number; flipSourceY: boolean; params: GradeDrawParams },
|
|
452
|
-
) {
|
|
453
|
-
const { gl, vao } = state;
|
|
454
|
-
const { fields, lut, seed } = params;
|
|
455
|
-
const u = gradeUniforms(fields);
|
|
456
|
-
const useCurves = hasCurves(fields);
|
|
457
|
-
const useLut = Boolean(lut && fields.lut);
|
|
458
|
-
const useFinish = u.halation > 0 || u.bloom > 0 || u.vignette !== 0 || u.grain > 0;
|
|
459
|
-
|
|
460
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
461
|
-
gl.bindTexture(gl.TEXTURE_2D, state.source);
|
|
462
|
-
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
|
|
463
|
-
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
|
|
464
|
-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
465
|
-
|
|
466
|
-
if (useCurves) {
|
|
467
|
-
gl.activeTexture(gl.TEXTURE1);
|
|
468
|
-
uploadCurves(state, fields);
|
|
469
|
-
gl.bindTexture(gl.TEXTURE_2D, state.curves);
|
|
470
|
-
}
|
|
471
|
-
if (useLut) {
|
|
472
|
-
gl.activeTexture(gl.TEXTURE2);
|
|
473
|
-
uploadLut(state, lut!, fields.lut!);
|
|
474
|
-
gl.bindTexture(gl.TEXTURE_3D, state.lut);
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
gl.bindVertexArray(vao);
|
|
478
|
-
gl.disable(gl.BLEND);
|
|
479
|
-
const quad = () => gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
480
|
-
|
|
481
|
-
// Colour: straight to the canvas, or to a float target when the finish still has to run.
|
|
482
|
-
const graded = useFinish ? renderTarget(state, "graded", width, height) : null;
|
|
483
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, graded?.framebuffer ?? null);
|
|
484
|
-
gl.viewport(0, 0, width, height);
|
|
485
|
-
gl.clearColor(0, 0, 0, 0);
|
|
486
|
-
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
487
|
-
gl.useProgram(state.color.handle);
|
|
488
|
-
setColorUniforms(state, u, useCurves, useLut, lut, !useFinish);
|
|
489
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
490
|
-
gl.bindTexture(gl.TEXTURE_2D, state.source);
|
|
491
|
-
quad();
|
|
492
|
-
|
|
493
|
-
if (graded) {
|
|
494
|
-
let halation: RenderTarget | null = null;
|
|
495
|
-
let bloom: RenderTarget | null = null;
|
|
496
|
-
if (u.halation > 0 || u.bloom > 0) {
|
|
497
|
-
const small = (name: string, divisor: number) =>
|
|
498
|
-
renderTarget(state, name, Math.max(2, Math.round(width / divisor)), Math.max(2, Math.round(height / divisor)));
|
|
499
|
-
const run = (program: Program, input: WebGLTexture, output: RenderTarget, step?: [number, number]) => {
|
|
500
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, output.framebuffer);
|
|
501
|
-
gl.viewport(0, 0, output.width, output.height);
|
|
502
|
-
gl.useProgram(program.handle);
|
|
503
|
-
gl.uniform1i(uniform(gl, program, "uSource"), 0);
|
|
504
|
-
if (step) gl.uniform2f(uniform(gl, program, "uStep"), step[0], step[1]);
|
|
505
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
506
|
-
gl.bindTexture(gl.TEXTURE_2D, input);
|
|
507
|
-
quad();
|
|
508
|
-
};
|
|
509
|
-
// Radii are fractions of the frame's height, so a 4K source and its preview look the same.
|
|
510
|
-
const aspect = height / width;
|
|
511
|
-
const bright = small("bright", 4);
|
|
512
|
-
const blur = extraProgram(state, "blur");
|
|
513
|
-
run(extraProgram(state, "bright"), graded.texture, bright);
|
|
514
|
-
const halationH = small("halation-h", 4);
|
|
515
|
-
halation = small("halation", 4);
|
|
516
|
-
run(blur, bright.texture, halationH, [0.0022 * aspect, 0]);
|
|
517
|
-
run(blur, halationH.texture, halation, [0, 0.0022]);
|
|
518
|
-
if (u.bloom > 0) {
|
|
519
|
-
const bloomH = small("bloom-h", 8);
|
|
520
|
-
bloom = small("bloom", 8);
|
|
521
|
-
run(blur, halation.texture, bloomH, [0.008 * aspect, 0]);
|
|
522
|
-
run(blur, bloomH.texture, bloom, [0, 0.008]);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
526
|
-
gl.viewport(0, 0, width, height);
|
|
527
|
-
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
528
|
-
const finish = extraProgram(state, "finish");
|
|
529
|
-
gl.useProgram(finish.handle);
|
|
530
|
-
const at = (name: string) => uniform(gl, finish, name);
|
|
531
|
-
gl.uniform1i(at("uSource"), 0);
|
|
532
|
-
gl.uniform1i(at("uHalationTexture"), 3);
|
|
533
|
-
gl.uniform1i(at("uBloomTexture"), 4);
|
|
534
|
-
gl.uniform1f(at("uHalation"), u.halation);
|
|
535
|
-
gl.uniform1f(at("uBloom"), u.bloom);
|
|
536
|
-
gl.uniform1f(at("uVignette"), u.vignette);
|
|
537
|
-
gl.uniform1f(at("uGrain"), u.grain);
|
|
538
|
-
// Grain is sized against 1080p, so it stays visible once a 4K frame is scaled down.
|
|
539
|
-
gl.uniform1f(at("uGrainCell"), Math.max(1, (u.grainSize * height) / 1080));
|
|
540
|
-
gl.uniform1f(at("uSeed"), seed % 1024);
|
|
541
|
-
gl.uniform1f(at("uAspect"), width / height);
|
|
542
|
-
gl.activeTexture(gl.TEXTURE3);
|
|
543
|
-
gl.bindTexture(gl.TEXTURE_2D, halation?.texture ?? graded.texture);
|
|
544
|
-
gl.activeTexture(gl.TEXTURE4);
|
|
545
|
-
gl.bindTexture(gl.TEXTURE_2D, bloom?.texture ?? graded.texture);
|
|
546
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
547
|
-
gl.bindTexture(gl.TEXTURE_2D, graded.texture);
|
|
548
|
-
quad();
|
|
549
|
-
}
|
|
550
|
-
|
|
551
|
-
gl.bindVertexArray(null);
|
|
552
|
-
gl.useProgram(null);
|
|
553
|
-
gl.activeTexture(gl.TEXTURE0);
|
|
554
|
-
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
const gradePass = createEffect<GradeDrawParams, GradeRendererState>({
|
|
558
|
-
type: "agentcut/grade",
|
|
559
|
-
label: "grade()",
|
|
560
|
-
documentationLink: null,
|
|
561
|
-
backend: "webgl2",
|
|
562
|
-
calculateKey: ({ fields, lut, seed }) =>
|
|
563
|
-
JSON.stringify([fields, lut ? lut.size : 0, fields.grain ? seed : 0]),
|
|
564
|
-
setup: (target) => setupGradeRenderer(target),
|
|
565
|
-
apply: ({ source, width, height, state, params, flipSourceY }) =>
|
|
566
|
-
drawGrade(state, { source: source as TexImageSource, width, height, flipSourceY, params }),
|
|
567
|
-
cleanup: cleanupGradeRenderer,
|
|
568
|
-
schema: {},
|
|
569
|
-
validateParams: () => {},
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
export interface GradeEffectOptions {
|
|
573
|
-
/** Clip-local frame, for keys. */
|
|
574
|
-
frame: number;
|
|
575
|
-
/** Absolute frame, for grain. */
|
|
576
|
-
seed: number;
|
|
577
|
-
/** Loaded .cube files by project path. */
|
|
578
|
-
luts?: ReadonlyMap<string, CubeLut>;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
/** The effects that draw `grades`, first to last (a clip's own grade, then what sits on top of it). */
|
|
582
|
-
export function gradeEffects(grades: { grade: ColorGrade; frame: number; strength?: number }[], { seed, luts }: Omit<GradeEffectOptions, "frame">): EffectDescriptor<unknown>[] {
|
|
583
|
-
const effects: EffectDescriptor<unknown>[] = [];
|
|
584
|
-
for (const { grade, frame, strength } of grades) {
|
|
585
|
-
if (strength !== undefined && strength <= 0) continue;
|
|
586
|
-
for (const pass of gradePasses(grade, frame, gradeLook)) {
|
|
587
|
-
const fields = strength === undefined ? pass : scaleGrade(pass, strength);
|
|
588
|
-
const lut = fields.lut ? luts?.get(fields.lut) ?? null : null;
|
|
589
|
-
effects.push(gradePass({ fields, lut, seed }));
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
return effects;
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
/** The .cube files a grade needs, including ones only its keys use. */
|
|
596
|
-
export function gradeLutPaths(grade: ColorGrade | undefined | null): string[] {
|
|
597
|
-
if (!grade) return [];
|
|
598
|
-
const paths = new Set<string>();
|
|
599
|
-
if (grade.lut) paths.add(grade.lut);
|
|
600
|
-
for (const key of grade.keys ?? []) if (key.lut) paths.add(key.lut);
|
|
601
|
-
return [...paths];
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
let warmed = false;
|
|
605
|
-
|
|
606
|
-
/**
|
|
607
|
-
* Compile the shaders in the background once, so the browser has them cached before the first slider
|
|
608
|
-
* moves. Without this, the first grade on a clip stalls the page while they compile.
|
|
609
|
-
*/
|
|
610
|
-
export function warmGradeShaders() {
|
|
611
|
-
if (warmed || typeof document === "undefined") return;
|
|
612
|
-
warmed = true;
|
|
613
|
-
const gl = document.createElement("canvas").getContext("webgl2");
|
|
614
|
-
if (!gl) return;
|
|
615
|
-
const parallel = gl.getExtension("KHR_parallel_shader_compile") as { COMPLETION_STATUS_KHR: number } | null;
|
|
616
|
-
const handles = [COLOR_FRAGMENT, BRIGHT_FRAGMENT, BLUR_FRAGMENT, FINISH_FRAGMENT].map((fragment) => {
|
|
617
|
-
const handle = gl.createProgram()!;
|
|
618
|
-
for (const [type, text] of [[gl.VERTEX_SHADER, VERTEX], [gl.FRAGMENT_SHADER, fragment]] as const) {
|
|
619
|
-
const shader = gl.createShader(type)!;
|
|
620
|
-
gl.shaderSource(shader, text);
|
|
621
|
-
gl.compileShader(shader);
|
|
622
|
-
gl.attachShader(handle, shader);
|
|
623
|
-
}
|
|
624
|
-
gl.linkProgram(handle);
|
|
625
|
-
return handle;
|
|
626
|
-
});
|
|
627
|
-
// Without the extension the driver still finishes on its own; just give it time before letting go.
|
|
628
|
-
const release = () => gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
629
|
-
if (!parallel) {
|
|
630
|
-
window.setTimeout(release, 5000);
|
|
631
|
-
return;
|
|
632
|
-
}
|
|
633
|
-
const poll = () => {
|
|
634
|
-
if (handles.every((handle) => gl.getProgramParameter(handle, parallel.COMPLETION_STATUS_KHR))) release();
|
|
635
|
-
else window.setTimeout(poll, 50);
|
|
636
|
-
};
|
|
637
|
-
poll();
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
let webgl2Available: boolean | null = null;
|
|
641
|
-
/** Pages and machines without WebGL2 keep the CSS part of a grade. */
|
|
642
|
-
export function canDrawGrade(): boolean {
|
|
643
|
-
if (webgl2Available !== null) return webgl2Available;
|
|
644
|
-
try {
|
|
645
|
-
webgl2Available = typeof document !== "undefined" && Boolean(document.createElement("canvas").getContext("webgl2"));
|
|
646
|
-
} catch {
|
|
647
|
-
webgl2Available = false;
|
|
648
|
-
}
|
|
649
|
-
return webgl2Available;
|
|
650
|
-
}
|
|
1
|
+
// The grade on the GPU: one shader for the colour, one optional pass for halation and bloom. A Remotion
|
|
2
|
+
// effect, so the live preview, captures and exports all draw exactly the same pixels.
|
|
3
|
+
import { createEffect, type EffectDescriptor } from "remotion";
|
|
4
|
+
import { bakeCurve, gradePasses, gradeUniforms, hasCurves, scaleGrade, type ColorGrade, type CubeLut, type GradeFields, type GradeUniforms } from "./grade";
|
|
5
|
+
import { gradeLook } from "./looks";
|
|
6
|
+
|
|
7
|
+
const VERTEX = `#version 300 es
|
|
8
|
+
layout(location = 0) in vec2 aPos;
|
|
9
|
+
layout(location = 1) in vec2 aUv;
|
|
10
|
+
out vec2 vUv;
|
|
11
|
+
void main() { vUv = aUv; gl_Position = vec4(aPos, 0.0, 1.0); }
|
|
12
|
+
`;
|
|
13
|
+
|
|
14
|
+
const COMMON = `
|
|
15
|
+
const vec3 REC709 = vec3(0.2126, 0.7152, 0.0722);
|
|
16
|
+
vec3 toLinear(vec3 c) {
|
|
17
|
+
vec3 s = sign(c); c = abs(c);
|
|
18
|
+
return s * mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), step(0.04045, c));
|
|
19
|
+
}
|
|
20
|
+
vec3 toGamma(vec3 c) {
|
|
21
|
+
vec3 s = sign(c); c = abs(c);
|
|
22
|
+
return s * mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c));
|
|
23
|
+
}
|
|
24
|
+
float hash(vec2 p, float seed) {
|
|
25
|
+
vec3 q = fract(vec3(p.xyx) * 0.1031 + seed * 0.1173);
|
|
26
|
+
q += dot(q, q.yzx + 33.33);
|
|
27
|
+
return fract((q.x + q.y) * q.z);
|
|
28
|
+
}
|
|
29
|
+
// Half a step of triangular noise, so smooth gradients survive the 8-bit output without banding.
|
|
30
|
+
vec3 dither(vec3 c, vec2 p) {
|
|
31
|
+
float n = hash(p, 1.0) + hash(p, 2.0) - 1.0;
|
|
32
|
+
return c + n / 255.0;
|
|
33
|
+
}
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
const COLOR_FRAGMENT = `#version 300 es
|
|
37
|
+
precision highp float;
|
|
38
|
+
precision highp sampler3D;
|
|
39
|
+
in vec2 vUv;
|
|
40
|
+
out vec4 fragColor;
|
|
41
|
+
uniform sampler2D uSource;
|
|
42
|
+
uniform sampler2D uCurves;
|
|
43
|
+
uniform sampler3D uLut;
|
|
44
|
+
uniform float uBrightness, uContrast, uPivot;
|
|
45
|
+
uniform mat3 uMatrix;
|
|
46
|
+
uniform vec3 uGains;
|
|
47
|
+
uniform float uMatchContrast, uMatchSaturation;
|
|
48
|
+
uniform float uShadows, uHighlights, uWhites, uBlacks;
|
|
49
|
+
uniform float uBlackPoint, uWhitePoint, uGamma;
|
|
50
|
+
uniform vec3 uLift, uWheelGamma, uGain;
|
|
51
|
+
uniform float uVibrance, uSkinProtect, uLutStrength;
|
|
52
|
+
uniform bool uHasCurves, uHasHsl, uHasLut, uDither;
|
|
53
|
+
uniform vec3 uHsl[8];
|
|
54
|
+
uniform vec3 uDomainMin, uDomainMax;
|
|
55
|
+
${COMMON}
|
|
56
|
+
|
|
57
|
+
vec3 rgb2hsv(vec3 c) {
|
|
58
|
+
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
|
59
|
+
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
|
|
60
|
+
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
|
|
61
|
+
float d = q.x - min(q.w, q.y);
|
|
62
|
+
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + 1e-6)), d / (q.x + 1e-6), q.x);
|
|
63
|
+
}
|
|
64
|
+
vec3 hsv2rgb(vec3 c) {
|
|
65
|
+
vec3 p = abs(fract(c.xxx + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0);
|
|
66
|
+
return c.z * mix(vec3(1.0), clamp(p - 1.0, 0.0, 1.0), c.y);
|
|
67
|
+
}
|
|
68
|
+
// Band centres in turns: red, orange, yellow, green, aqua, blue, purple, magenta.
|
|
69
|
+
const float CENTERS[9] = float[9](0.0, 0.0833, 0.1667, 0.3333, 0.5, 0.6667, 0.7778, 0.8889, 1.0);
|
|
70
|
+
vec3 mixer(vec3 c) {
|
|
71
|
+
vec3 hsv = rgb2hsv(clamp(c, 0.0, 1.0));
|
|
72
|
+
float h = fract(hsv.x);
|
|
73
|
+
int i = 0;
|
|
74
|
+
for (int n = 0; n < 8; n++) if (h >= CENTERS[n]) i = n;
|
|
75
|
+
float t = smoothstep(0.0, 1.0, (h - CENTERS[i]) / (CENTERS[i + 1] - CENTERS[i]));
|
|
76
|
+
vec3 band = mix(uHsl[i], uHsl[(i + 1) % 8], t);
|
|
77
|
+
float colorful = smoothstep(0.0, 0.25, hsv.y);
|
|
78
|
+
hsv.x = fract(h + band.x * 0.083);
|
|
79
|
+
vec3 shifted = hsv2rgb(hsv);
|
|
80
|
+
// Saturation around brightness: a desaturated blue goes dark grey, not light grey.
|
|
81
|
+
shifted = mix(vec3(dot(shifted, REC709)), shifted, 1.0 + band.y);
|
|
82
|
+
return shifted * (1.0 + band.z * 0.6 * colorful);
|
|
83
|
+
}
|
|
84
|
+
float skinMask(vec3 c) {
|
|
85
|
+
// Skin of every complexion sits near one hue on the vectorscope (the I line, about 123 degrees).
|
|
86
|
+
float y = dot(c, REC709);
|
|
87
|
+
float cb = (c.b - y) * 0.5389;
|
|
88
|
+
float cr = (c.r - y) * 0.635;
|
|
89
|
+
float chroma = length(vec2(cb, cr));
|
|
90
|
+
if (chroma < 1e-4) return 0.0;
|
|
91
|
+
float angle = atan(cr, cb);
|
|
92
|
+
float distance = abs(angle - 2.147);
|
|
93
|
+
float hue = 1.0 - smoothstep(0.14, 0.4, distance);
|
|
94
|
+
return hue * smoothstep(0.012, 0.04, chroma) * (1.0 - smoothstep(0.26, 0.4, chroma)) * smoothstep(0.06, 0.18, y);
|
|
95
|
+
}
|
|
96
|
+
vec3 sampleLut(vec3 c) {
|
|
97
|
+
vec3 n = clamp((c - uDomainMin) / (uDomainMax - uDomainMin), 0.0, 1.0);
|
|
98
|
+
vec3 size = vec3(textureSize(uLut, 0));
|
|
99
|
+
return texture(uLut, (n * (size - 1.0) + 0.5) / size).rgb;
|
|
100
|
+
}
|
|
101
|
+
void main() {
|
|
102
|
+
vec4 source = texture(uSource, vUv);
|
|
103
|
+
if (source.a <= 0.0) { fragColor = vec4(0.0); return; }
|
|
104
|
+
vec3 original = source.rgb / source.a;
|
|
105
|
+
vec3 c = original;
|
|
106
|
+
|
|
107
|
+
// The original controls, in the order and space CSS filters use.
|
|
108
|
+
c = clamp(c * uBrightness, 0.0, 1.0);
|
|
109
|
+
c = clamp((c - uPivot) * uContrast + uPivot, 0.0, 1.0);
|
|
110
|
+
c = clamp(uMatrix * c, 0.0, 1.0);
|
|
111
|
+
|
|
112
|
+
// Light: exposure, white balance and shot matching are gains on linear light.
|
|
113
|
+
c = toGamma(toLinear(c) * uGains);
|
|
114
|
+
if (uMatchContrast != 1.0) c = (c - 0.45) * uMatchContrast + 0.45;
|
|
115
|
+
if (uMatchSaturation != 1.0) c = mix(vec3(dot(c, REC709)), c, uMatchSaturation);
|
|
116
|
+
|
|
117
|
+
// Tone regions, picked by brightness.
|
|
118
|
+
float y = dot(c, REC709);
|
|
119
|
+
c += uShadows * 0.32 * pow(clamp(1.0 - y, 0.0, 1.0), 2.0) * smoothstep(0.0, 0.08, y);
|
|
120
|
+
c += uHighlights * 0.32 * pow(smoothstep(0.35, 1.0, y), 2.0);
|
|
121
|
+
c += uWhites * 0.18 * smoothstep(0.7, 1.0, y);
|
|
122
|
+
c += uBlacks * 0.14 * (1.0 - smoothstep(0.0, 0.28, y));
|
|
123
|
+
|
|
124
|
+
c = (c - uBlackPoint) / (uWhitePoint - uBlackPoint);
|
|
125
|
+
if (uGamma != 1.0) c = pow(max(c, 0.0), vec3(1.0 / uGamma));
|
|
126
|
+
|
|
127
|
+
// Colour wheels: lift, gamma, gain.
|
|
128
|
+
c = c + uLift * (1.0 - clamp(c, 0.0, 1.0));
|
|
129
|
+
c = pow(max(c, 0.0), 1.0 / uWheelGamma);
|
|
130
|
+
c *= uGain;
|
|
131
|
+
|
|
132
|
+
if (uHasCurves) {
|
|
133
|
+
c = clamp(c, 0.0, 1.0);
|
|
134
|
+
c = vec3(texture(uCurves, vec2(c.r, 0.5)).r, texture(uCurves, vec2(c.g, 0.5)).g, texture(uCurves, vec2(c.b, 0.5)).b);
|
|
135
|
+
c = vec3(texture(uCurves, vec2(c.r, 0.5)).a, texture(uCurves, vec2(c.g, 0.5)).a, texture(uCurves, vec2(c.b, 0.5)).a);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (uVibrance != 0.0) {
|
|
139
|
+
// Lifts quiet colours more than loud ones, and leaves skin alone.
|
|
140
|
+
float top = max(c.r, max(c.g, c.b));
|
|
141
|
+
float low = min(c.r, min(c.g, c.b));
|
|
142
|
+
float amount = uVibrance * (1.0 - clamp(top - low, 0.0, 1.0)) * (1.0 - 0.6 * skinMask(original));
|
|
143
|
+
c = mix(vec3(dot(c, REC709)), c, 1.0 + amount);
|
|
144
|
+
}
|
|
145
|
+
if (uHasHsl) c = mixer(c);
|
|
146
|
+
|
|
147
|
+
if (uHasLut && uLutStrength > 0.0) {
|
|
148
|
+
c = clamp(c, 0.0, 1.0);
|
|
149
|
+
c = mix(c, sampleLut(c), uLutStrength);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (uSkinProtect > 0.0) {
|
|
153
|
+
// Keep the new brightness, bring back the skin's own colour.
|
|
154
|
+
float newY = dot(c, REC709);
|
|
155
|
+
vec3 skin = newY + (original - dot(original, REC709));
|
|
156
|
+
c = mix(c, skin, uSkinProtect * skinMask(original));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
c = clamp(c, 0.0, 1.0);
|
|
160
|
+
if (uDither) c = dither(c, gl_FragCoord.xy);
|
|
161
|
+
fragColor = vec4(clamp(c, 0.0, 1.0) * source.a, source.a);
|
|
162
|
+
}
|
|
163
|
+
`;
|
|
164
|
+
|
|
165
|
+
const BRIGHT_FRAGMENT = `#version 300 es
|
|
166
|
+
precision highp float;
|
|
167
|
+
in vec2 vUv;
|
|
168
|
+
out vec4 fragColor;
|
|
169
|
+
uniform sampler2D uSource;
|
|
170
|
+
${COMMON}
|
|
171
|
+
void main() {
|
|
172
|
+
vec4 source = texture(uSource, vUv);
|
|
173
|
+
vec3 c = source.a > 0.0 ? source.rgb / source.a : vec3(0.0);
|
|
174
|
+
float y = dot(c, REC709);
|
|
175
|
+
fragColor = vec4(c * smoothstep(0.62, 0.98, y) * source.a, source.a);
|
|
176
|
+
}
|
|
177
|
+
`;
|
|
178
|
+
|
|
179
|
+
const BLUR_FRAGMENT = `#version 300 es
|
|
180
|
+
precision highp float;
|
|
181
|
+
in vec2 vUv;
|
|
182
|
+
out vec4 fragColor;
|
|
183
|
+
uniform sampler2D uSource;
|
|
184
|
+
uniform vec2 uStep;
|
|
185
|
+
void main() {
|
|
186
|
+
vec4 sum = texture(uSource, vUv) * 0.1964825501511404;
|
|
187
|
+
sum += (texture(uSource, vUv + uStep * 1.411764705882353) + texture(uSource, vUv - uStep * 1.411764705882353)) * 0.2969069646728344;
|
|
188
|
+
sum += (texture(uSource, vUv + uStep * 3.2941176470588234) + texture(uSource, vUv - uStep * 3.2941176470588234)) * 0.09447039785044732;
|
|
189
|
+
sum += (texture(uSource, vUv + uStep * 5.176470588235294) + texture(uSource, vUv - uStep * 5.176470588235294)) * 0.010381362401148057;
|
|
190
|
+
fragColor = sum;
|
|
191
|
+
}
|
|
192
|
+
`;
|
|
193
|
+
|
|
194
|
+
const FINISH_FRAGMENT = `#version 300 es
|
|
195
|
+
precision highp float;
|
|
196
|
+
in vec2 vUv;
|
|
197
|
+
out vec4 fragColor;
|
|
198
|
+
uniform sampler2D uSource, uHalationTexture, uBloomTexture;
|
|
199
|
+
uniform float uHalation, uBloom, uVignette, uGrain, uGrainCell, uSeed, uAspect;
|
|
200
|
+
${COMMON}
|
|
201
|
+
vec3 screen(vec3 a, vec3 b) { return 1.0 - (1.0 - a) * (1.0 - clamp(b, 0.0, 1.0)); }
|
|
202
|
+
void main() {
|
|
203
|
+
vec4 source = texture(uSource, vUv);
|
|
204
|
+
if (source.a <= 0.0) { fragColor = vec4(0.0); return; }
|
|
205
|
+
vec3 c = source.rgb / source.a;
|
|
206
|
+
// Halation: light scattering back through the film base shows as a red-orange rim on highlights.
|
|
207
|
+
if (uHalation > 0.0) c = screen(c, texture(uHalationTexture, vUv).rgb * vec3(1.0, 0.32, 0.1) * uHalation * 2.2);
|
|
208
|
+
if (uBloom > 0.0) c = screen(c, texture(uBloomTexture, vUv).rgb * uBloom * 1.6);
|
|
209
|
+
if (uVignette != 0.0) {
|
|
210
|
+
vec2 p = (vUv - 0.5) * vec2(uAspect, 1.0) / sqrt(uAspect * uAspect + 1.0) * 2.0;
|
|
211
|
+
float fall = smoothstep(0.3, 1.0, length(p));
|
|
212
|
+
c = uVignette > 0.0 ? c * (1.0 - uVignette * 0.85 * fall) : screen(c, vec3(-uVignette * 0.5 * fall));
|
|
213
|
+
}
|
|
214
|
+
if (uGrain > 0.0) {
|
|
215
|
+
vec2 cell = floor(gl_FragCoord.xy / uGrainCell);
|
|
216
|
+
float y = dot(c, REC709);
|
|
217
|
+
// Film grain is strongest in the midtones and fades out in pure black and white.
|
|
218
|
+
float weight = 0.25 + 0.75 * (1.0 - pow(abs(2.0 * y - 1.0), 2.0));
|
|
219
|
+
float mono = hash(cell, uSeed) + hash(cell, uSeed + 7.0) - 1.0;
|
|
220
|
+
vec3 tint = vec3(hash(cell, uSeed + 13.0), hash(cell, uSeed + 19.0), hash(cell, uSeed + 23.0)) - 0.5;
|
|
221
|
+
c += (mono + tint * 0.3) * uGrain * 0.2 * weight;
|
|
222
|
+
}
|
|
223
|
+
c = dither(clamp(c, 0.0, 1.0), gl_FragCoord.xy);
|
|
224
|
+
fragColor = vec4(clamp(c, 0.0, 1.0) * source.a, source.a);
|
|
225
|
+
}
|
|
226
|
+
`;
|
|
227
|
+
|
|
228
|
+
interface Program { handle: WebGLProgram; uniforms: Map<string, WebGLUniformLocation | null> }
|
|
229
|
+
|
|
230
|
+
interface RenderTarget { texture: WebGLTexture; framebuffer: WebGLFramebuffer; width: number; height: number }
|
|
231
|
+
|
|
232
|
+
export interface GradeRendererState {
|
|
233
|
+
gl: WebGL2RenderingContext;
|
|
234
|
+
color: Program;
|
|
235
|
+
/** The finish's programs, compiled the first time a grade uses them: compiling is slow. */
|
|
236
|
+
extra: { bright?: Program; blur?: Program; finish?: Program };
|
|
237
|
+
vao: WebGLVertexArrayObject;
|
|
238
|
+
vbo: WebGLBuffer;
|
|
239
|
+
source: WebGLTexture;
|
|
240
|
+
curves: WebGLTexture;
|
|
241
|
+
lut: WebGLTexture;
|
|
242
|
+
floatTargets: boolean;
|
|
243
|
+
targets: Map<string, RenderTarget>;
|
|
244
|
+
curveKey: string | null;
|
|
245
|
+
lutKey: string | null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function makeProgram(gl: WebGL2RenderingContext, fragment: string): Program {
|
|
249
|
+
const compile = (type: number, text: string) => {
|
|
250
|
+
const shader = gl.createShader(type)!;
|
|
251
|
+
gl.shaderSource(shader, text);
|
|
252
|
+
gl.compileShader(shader);
|
|
253
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
254
|
+
const log = gl.getShaderInfoLog(shader);
|
|
255
|
+
gl.deleteShader(shader);
|
|
256
|
+
throw new Error(`Grade shader failed to compile: ${log ?? "(no log)"}`);
|
|
257
|
+
}
|
|
258
|
+
return shader;
|
|
259
|
+
};
|
|
260
|
+
const vertexShader = compile(gl.VERTEX_SHADER, VERTEX);
|
|
261
|
+
const fragmentShader = compile(gl.FRAGMENT_SHADER, fragment);
|
|
262
|
+
const handle = gl.createProgram()!;
|
|
263
|
+
gl.attachShader(handle, vertexShader);
|
|
264
|
+
gl.attachShader(handle, fragmentShader);
|
|
265
|
+
gl.linkProgram(handle);
|
|
266
|
+
gl.deleteShader(vertexShader);
|
|
267
|
+
gl.deleteShader(fragmentShader);
|
|
268
|
+
if (!gl.getProgramParameter(handle, gl.LINK_STATUS)) {
|
|
269
|
+
const log = gl.getProgramInfoLog(handle);
|
|
270
|
+
gl.deleteProgram(handle);
|
|
271
|
+
throw new Error(`Grade shader failed to link: ${log ?? "(no log)"}`);
|
|
272
|
+
}
|
|
273
|
+
return { handle, uniforms: new Map() };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function uniform(gl: WebGL2RenderingContext, program: Program, name: string) {
|
|
277
|
+
if (!program.uniforms.has(name)) program.uniforms.set(name, gl.getUniformLocation(program.handle, name));
|
|
278
|
+
return program.uniforms.get(name) ?? null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function texture2d(gl: WebGL2RenderingContext) {
|
|
282
|
+
const texture = gl.createTexture()!;
|
|
283
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
284
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
285
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
286
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
287
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
288
|
+
return texture;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function setupGradeRenderer(canvas: HTMLCanvasElement | OffscreenCanvas): GradeRendererState {
|
|
292
|
+
const gl = canvas.getContext("webgl2", { premultipliedAlpha: true, alpha: true, preserveDrawingBuffer: true }) as WebGL2RenderingContext | null;
|
|
293
|
+
if (!gl) throw new Error("The color grade needs WebGL2, which this browser could not provide.");
|
|
294
|
+
const vao = gl.createVertexArray()!;
|
|
295
|
+
const vbo = gl.createBuffer()!;
|
|
296
|
+
gl.bindVertexArray(vao);
|
|
297
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
|
|
298
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1, 1, 1, 1, 1]), gl.STATIC_DRAW);
|
|
299
|
+
gl.enableVertexAttribArray(0);
|
|
300
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 16, 0);
|
|
301
|
+
gl.enableVertexAttribArray(1);
|
|
302
|
+
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 16, 8);
|
|
303
|
+
gl.bindVertexArray(null);
|
|
304
|
+
const lut = gl.createTexture()!;
|
|
305
|
+
gl.bindTexture(gl.TEXTURE_3D, lut);
|
|
306
|
+
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
307
|
+
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
308
|
+
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
309
|
+
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
310
|
+
gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
|
|
311
|
+
gl.bindTexture(gl.TEXTURE_3D, null);
|
|
312
|
+
return {
|
|
313
|
+
gl,
|
|
314
|
+
color: makeProgram(gl, COLOR_FRAGMENT),
|
|
315
|
+
extra: {},
|
|
316
|
+
vao,
|
|
317
|
+
vbo,
|
|
318
|
+
source: texture2d(gl),
|
|
319
|
+
curves: texture2d(gl),
|
|
320
|
+
lut,
|
|
321
|
+
floatTargets: Boolean(gl.getExtension("EXT_color_buffer_float")),
|
|
322
|
+
targets: new Map(),
|
|
323
|
+
curveKey: null,
|
|
324
|
+
lutKey: null,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function cleanupGradeRenderer(state: GradeRendererState) {
|
|
329
|
+
const { gl } = state;
|
|
330
|
+
for (const program of [state.color, ...Object.values(state.extra)]) gl.deleteProgram(program.handle);
|
|
331
|
+
for (const target of state.targets.values()) {
|
|
332
|
+
gl.deleteTexture(target.texture);
|
|
333
|
+
gl.deleteFramebuffer(target.framebuffer);
|
|
334
|
+
}
|
|
335
|
+
state.targets.clear();
|
|
336
|
+
gl.deleteTexture(state.source);
|
|
337
|
+
gl.deleteTexture(state.curves);
|
|
338
|
+
gl.deleteTexture(state.lut);
|
|
339
|
+
gl.deleteBuffer(state.vbo);
|
|
340
|
+
gl.deleteVertexArray(state.vao);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const EXTRA_FRAGMENTS = { bright: BRIGHT_FRAGMENT, blur: BLUR_FRAGMENT, finish: FINISH_FRAGMENT };
|
|
344
|
+
|
|
345
|
+
function extraProgram(state: GradeRendererState, name: keyof typeof EXTRA_FRAGMENTS) {
|
|
346
|
+
return (state.extra[name] ??= makeProgram(state.gl, EXTRA_FRAGMENTS[name]));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function renderTarget(state: GradeRendererState, name: string, width: number, height: number): RenderTarget {
|
|
350
|
+
const { gl } = state;
|
|
351
|
+
const existing = state.targets.get(name);
|
|
352
|
+
if (existing && existing.width === width && existing.height === height) return existing;
|
|
353
|
+
if (existing) {
|
|
354
|
+
gl.deleteTexture(existing.texture);
|
|
355
|
+
gl.deleteFramebuffer(existing.framebuffer);
|
|
356
|
+
}
|
|
357
|
+
// Creating a texture binds it to the active unit: keep that away from the units the shaders read.
|
|
358
|
+
gl.activeTexture(gl.TEXTURE7);
|
|
359
|
+
const texture = texture2d(gl);
|
|
360
|
+
// Float targets keep the extra precision between passes when the GPU can render to them.
|
|
361
|
+
if (state.floatTargets) gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, width, height, 0, gl.RGBA, gl.HALF_FLOAT, null);
|
|
362
|
+
else gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
|
363
|
+
const framebuffer = gl.createFramebuffer()!;
|
|
364
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
|
|
365
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
|
|
366
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
367
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
368
|
+
const target = { texture, framebuffer, width, height };
|
|
369
|
+
state.targets.set(name, target);
|
|
370
|
+
return target;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function uploadCurves(state: GradeRendererState, fields: GradeFields) {
|
|
374
|
+
const key = JSON.stringify(fields.curves ?? null);
|
|
375
|
+
if (state.curveKey === key) return;
|
|
376
|
+
const { gl } = state;
|
|
377
|
+
const size = 1024;
|
|
378
|
+
const r = bakeCurve(fields.curves?.r, size);
|
|
379
|
+
const g = bakeCurve(fields.curves?.g, size);
|
|
380
|
+
const b = bakeCurve(fields.curves?.b, size);
|
|
381
|
+
const master = bakeCurve(fields.curves?.master, size);
|
|
382
|
+
const data = new Float32Array(size * 4);
|
|
383
|
+
for (let i = 0; i < size; i += 1) data.set([r[i], g[i], b[i], master[i]], i * 4);
|
|
384
|
+
gl.bindTexture(gl.TEXTURE_2D, state.curves);
|
|
385
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
386
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
|
|
387
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, size, 1, 0, gl.RGBA, gl.FLOAT, data);
|
|
388
|
+
state.curveKey = key;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function uploadLut(state: GradeRendererState, lut: CubeLut, key: string) {
|
|
392
|
+
if (state.lutKey === key) return;
|
|
393
|
+
const { gl } = state;
|
|
394
|
+
const max = gl.getParameter(gl.MAX_3D_TEXTURE_SIZE) as number;
|
|
395
|
+
if (lut.size > max) throw new Error(`This LUT (size ${lut.size}) is larger than this GPU's limit of ${max}.`);
|
|
396
|
+
gl.bindTexture(gl.TEXTURE_3D, state.lut);
|
|
397
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
398
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
|
|
399
|
+
gl.texImage3D(gl.TEXTURE_3D, 0, gl.RGBA16F, lut.size, lut.size, lut.size, 0, gl.RGBA, gl.FLOAT, lut.data);
|
|
400
|
+
gl.bindTexture(gl.TEXTURE_3D, null);
|
|
401
|
+
state.lutKey = key;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function setColorUniforms(state: GradeRendererState, u: GradeUniforms, useCurves: boolean, useLut: boolean, lut: CubeLut | null, dither: boolean) {
|
|
405
|
+
const { gl, color } = state;
|
|
406
|
+
const at = (name: string) => uniform(gl, color, name);
|
|
407
|
+
gl.uniform1i(at("uSource"), 0);
|
|
408
|
+
gl.uniform1i(at("uCurves"), 1);
|
|
409
|
+
gl.uniform1i(at("uLut"), 2);
|
|
410
|
+
gl.uniform1f(at("uBrightness"), u.brightness);
|
|
411
|
+
gl.uniform1f(at("uContrast"), u.contrast);
|
|
412
|
+
gl.uniform1f(at("uPivot"), u.pivot);
|
|
413
|
+
// Our rows, GL's columns.
|
|
414
|
+
gl.uniformMatrix3fv(at("uMatrix"), true, u.matrix.flat());
|
|
415
|
+
gl.uniform3fv(at("uGains"), u.gains);
|
|
416
|
+
gl.uniform1f(at("uMatchContrast"), u.matchContrast);
|
|
417
|
+
gl.uniform1f(at("uMatchSaturation"), u.matchSaturation);
|
|
418
|
+
gl.uniform1f(at("uShadows"), u.shadows);
|
|
419
|
+
gl.uniform1f(at("uHighlights"), u.highlights);
|
|
420
|
+
gl.uniform1f(at("uWhites"), u.whites);
|
|
421
|
+
gl.uniform1f(at("uBlacks"), u.blacks);
|
|
422
|
+
gl.uniform1f(at("uBlackPoint"), u.blackPoint);
|
|
423
|
+
gl.uniform1f(at("uWhitePoint"), u.whitePoint);
|
|
424
|
+
gl.uniform1f(at("uGamma"), u.gamma);
|
|
425
|
+
gl.uniform3fv(at("uLift"), u.lift);
|
|
426
|
+
gl.uniform3fv(at("uWheelGamma"), u.wheelGamma);
|
|
427
|
+
gl.uniform3fv(at("uGain"), u.gain);
|
|
428
|
+
gl.uniform1f(at("uVibrance"), u.vibrance);
|
|
429
|
+
gl.uniform1f(at("uSkinProtect"), u.skinProtect);
|
|
430
|
+
gl.uniform1f(at("uLutStrength"), u.lutStrength);
|
|
431
|
+
gl.uniform1i(at("uHasCurves"), useCurves ? 1 : 0);
|
|
432
|
+
gl.uniform1i(at("uHasHsl"), u.hasHsl ? 1 : 0);
|
|
433
|
+
gl.uniform1i(at("uHasLut"), useLut ? 1 : 0);
|
|
434
|
+
gl.uniform1i(at("uDither"), dither ? 1 : 0);
|
|
435
|
+
gl.uniform3fv(at("uHsl"), u.hsl);
|
|
436
|
+
gl.uniform3fv(at("uDomainMin"), lut?.domainMin ?? [0, 0, 0]);
|
|
437
|
+
gl.uniform3fv(at("uDomainMax"), lut?.domainMax ?? [1, 1, 1]);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export interface GradeDrawParams {
|
|
441
|
+
fields: GradeFields;
|
|
442
|
+
/** Parsed file for fields.lut, when it has loaded. */
|
|
443
|
+
lut: CubeLut | null;
|
|
444
|
+
/** Grain changes every frame by this number. */
|
|
445
|
+
seed: number;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Draw `source` through the grade to the canvas this renderer was set up on. */
|
|
449
|
+
export function drawGrade(
|
|
450
|
+
state: GradeRendererState,
|
|
451
|
+
{ source, width, height, flipSourceY, params }: { source: TexImageSource; width: number; height: number; flipSourceY: boolean; params: GradeDrawParams },
|
|
452
|
+
) {
|
|
453
|
+
const { gl, vao } = state;
|
|
454
|
+
const { fields, lut, seed } = params;
|
|
455
|
+
const u = gradeUniforms(fields);
|
|
456
|
+
const useCurves = hasCurves(fields);
|
|
457
|
+
const useLut = Boolean(lut && fields.lut);
|
|
458
|
+
const useFinish = u.halation > 0 || u.bloom > 0 || u.vignette !== 0 || u.grain > 0;
|
|
459
|
+
|
|
460
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
461
|
+
gl.bindTexture(gl.TEXTURE_2D, state.source);
|
|
462
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
|
|
463
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
|
|
464
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
465
|
+
|
|
466
|
+
if (useCurves) {
|
|
467
|
+
gl.activeTexture(gl.TEXTURE1);
|
|
468
|
+
uploadCurves(state, fields);
|
|
469
|
+
gl.bindTexture(gl.TEXTURE_2D, state.curves);
|
|
470
|
+
}
|
|
471
|
+
if (useLut) {
|
|
472
|
+
gl.activeTexture(gl.TEXTURE2);
|
|
473
|
+
uploadLut(state, lut!, fields.lut!);
|
|
474
|
+
gl.bindTexture(gl.TEXTURE_3D, state.lut);
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
gl.bindVertexArray(vao);
|
|
478
|
+
gl.disable(gl.BLEND);
|
|
479
|
+
const quad = () => gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
480
|
+
|
|
481
|
+
// Colour: straight to the canvas, or to a float target when the finish still has to run.
|
|
482
|
+
const graded = useFinish ? renderTarget(state, "graded", width, height) : null;
|
|
483
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, graded?.framebuffer ?? null);
|
|
484
|
+
gl.viewport(0, 0, width, height);
|
|
485
|
+
gl.clearColor(0, 0, 0, 0);
|
|
486
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
487
|
+
gl.useProgram(state.color.handle);
|
|
488
|
+
setColorUniforms(state, u, useCurves, useLut, lut, !useFinish);
|
|
489
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
490
|
+
gl.bindTexture(gl.TEXTURE_2D, state.source);
|
|
491
|
+
quad();
|
|
492
|
+
|
|
493
|
+
if (graded) {
|
|
494
|
+
let halation: RenderTarget | null = null;
|
|
495
|
+
let bloom: RenderTarget | null = null;
|
|
496
|
+
if (u.halation > 0 || u.bloom > 0) {
|
|
497
|
+
const small = (name: string, divisor: number) =>
|
|
498
|
+
renderTarget(state, name, Math.max(2, Math.round(width / divisor)), Math.max(2, Math.round(height / divisor)));
|
|
499
|
+
const run = (program: Program, input: WebGLTexture, output: RenderTarget, step?: [number, number]) => {
|
|
500
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, output.framebuffer);
|
|
501
|
+
gl.viewport(0, 0, output.width, output.height);
|
|
502
|
+
gl.useProgram(program.handle);
|
|
503
|
+
gl.uniform1i(uniform(gl, program, "uSource"), 0);
|
|
504
|
+
if (step) gl.uniform2f(uniform(gl, program, "uStep"), step[0], step[1]);
|
|
505
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
506
|
+
gl.bindTexture(gl.TEXTURE_2D, input);
|
|
507
|
+
quad();
|
|
508
|
+
};
|
|
509
|
+
// Radii are fractions of the frame's height, so a 4K source and its preview look the same.
|
|
510
|
+
const aspect = height / width;
|
|
511
|
+
const bright = small("bright", 4);
|
|
512
|
+
const blur = extraProgram(state, "blur");
|
|
513
|
+
run(extraProgram(state, "bright"), graded.texture, bright);
|
|
514
|
+
const halationH = small("halation-h", 4);
|
|
515
|
+
halation = small("halation", 4);
|
|
516
|
+
run(blur, bright.texture, halationH, [0.0022 * aspect, 0]);
|
|
517
|
+
run(blur, halationH.texture, halation, [0, 0.0022]);
|
|
518
|
+
if (u.bloom > 0) {
|
|
519
|
+
const bloomH = small("bloom-h", 8);
|
|
520
|
+
bloom = small("bloom", 8);
|
|
521
|
+
run(blur, halation.texture, bloomH, [0.008 * aspect, 0]);
|
|
522
|
+
run(blur, bloomH.texture, bloom, [0, 0.008]);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
526
|
+
gl.viewport(0, 0, width, height);
|
|
527
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
528
|
+
const finish = extraProgram(state, "finish");
|
|
529
|
+
gl.useProgram(finish.handle);
|
|
530
|
+
const at = (name: string) => uniform(gl, finish, name);
|
|
531
|
+
gl.uniform1i(at("uSource"), 0);
|
|
532
|
+
gl.uniform1i(at("uHalationTexture"), 3);
|
|
533
|
+
gl.uniform1i(at("uBloomTexture"), 4);
|
|
534
|
+
gl.uniform1f(at("uHalation"), u.halation);
|
|
535
|
+
gl.uniform1f(at("uBloom"), u.bloom);
|
|
536
|
+
gl.uniform1f(at("uVignette"), u.vignette);
|
|
537
|
+
gl.uniform1f(at("uGrain"), u.grain);
|
|
538
|
+
// Grain is sized against 1080p, so it stays visible once a 4K frame is scaled down.
|
|
539
|
+
gl.uniform1f(at("uGrainCell"), Math.max(1, (u.grainSize * height) / 1080));
|
|
540
|
+
gl.uniform1f(at("uSeed"), seed % 1024);
|
|
541
|
+
gl.uniform1f(at("uAspect"), width / height);
|
|
542
|
+
gl.activeTexture(gl.TEXTURE3);
|
|
543
|
+
gl.bindTexture(gl.TEXTURE_2D, halation?.texture ?? graded.texture);
|
|
544
|
+
gl.activeTexture(gl.TEXTURE4);
|
|
545
|
+
gl.bindTexture(gl.TEXTURE_2D, bloom?.texture ?? graded.texture);
|
|
546
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
547
|
+
gl.bindTexture(gl.TEXTURE_2D, graded.texture);
|
|
548
|
+
quad();
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
gl.bindVertexArray(null);
|
|
552
|
+
gl.useProgram(null);
|
|
553
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
554
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const gradePass = createEffect<GradeDrawParams, GradeRendererState>({
|
|
558
|
+
type: "agentcut/grade",
|
|
559
|
+
label: "grade()",
|
|
560
|
+
documentationLink: null,
|
|
561
|
+
backend: "webgl2",
|
|
562
|
+
calculateKey: ({ fields, lut, seed }) =>
|
|
563
|
+
JSON.stringify([fields, lut ? lut.size : 0, fields.grain ? seed : 0]),
|
|
564
|
+
setup: (target) => setupGradeRenderer(target),
|
|
565
|
+
apply: ({ source, width, height, state, params, flipSourceY }) =>
|
|
566
|
+
drawGrade(state, { source: source as TexImageSource, width, height, flipSourceY, params }),
|
|
567
|
+
cleanup: cleanupGradeRenderer,
|
|
568
|
+
schema: {},
|
|
569
|
+
validateParams: () => {},
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
export interface GradeEffectOptions {
|
|
573
|
+
/** Clip-local frame, for keys. */
|
|
574
|
+
frame: number;
|
|
575
|
+
/** Absolute frame, for grain. */
|
|
576
|
+
seed: number;
|
|
577
|
+
/** Loaded .cube files by project path. */
|
|
578
|
+
luts?: ReadonlyMap<string, CubeLut>;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/** The effects that draw `grades`, first to last (a clip's own grade, then what sits on top of it). */
|
|
582
|
+
export function gradeEffects(grades: { grade: ColorGrade; frame: number; strength?: number }[], { seed, luts }: Omit<GradeEffectOptions, "frame">): EffectDescriptor<unknown>[] {
|
|
583
|
+
const effects: EffectDescriptor<unknown>[] = [];
|
|
584
|
+
for (const { grade, frame, strength } of grades) {
|
|
585
|
+
if (strength !== undefined && strength <= 0) continue;
|
|
586
|
+
for (const pass of gradePasses(grade, frame, gradeLook)) {
|
|
587
|
+
const fields = strength === undefined ? pass : scaleGrade(pass, strength);
|
|
588
|
+
const lut = fields.lut ? luts?.get(fields.lut) ?? null : null;
|
|
589
|
+
effects.push(gradePass({ fields, lut, seed }));
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return effects;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/** The .cube files a grade needs, including ones only its keys use. */
|
|
596
|
+
export function gradeLutPaths(grade: ColorGrade | undefined | null): string[] {
|
|
597
|
+
if (!grade) return [];
|
|
598
|
+
const paths = new Set<string>();
|
|
599
|
+
if (grade.lut) paths.add(grade.lut);
|
|
600
|
+
for (const key of grade.keys ?? []) if (key.lut) paths.add(key.lut);
|
|
601
|
+
return [...paths];
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
let warmed = false;
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Compile the shaders in the background once, so the browser has them cached before the first slider
|
|
608
|
+
* moves. Without this, the first grade on a clip stalls the page while they compile.
|
|
609
|
+
*/
|
|
610
|
+
export function warmGradeShaders() {
|
|
611
|
+
if (warmed || typeof document === "undefined") return;
|
|
612
|
+
warmed = true;
|
|
613
|
+
const gl = document.createElement("canvas").getContext("webgl2");
|
|
614
|
+
if (!gl) return;
|
|
615
|
+
const parallel = gl.getExtension("KHR_parallel_shader_compile") as { COMPLETION_STATUS_KHR: number } | null;
|
|
616
|
+
const handles = [COLOR_FRAGMENT, BRIGHT_FRAGMENT, BLUR_FRAGMENT, FINISH_FRAGMENT].map((fragment) => {
|
|
617
|
+
const handle = gl.createProgram()!;
|
|
618
|
+
for (const [type, text] of [[gl.VERTEX_SHADER, VERTEX], [gl.FRAGMENT_SHADER, fragment]] as const) {
|
|
619
|
+
const shader = gl.createShader(type)!;
|
|
620
|
+
gl.shaderSource(shader, text);
|
|
621
|
+
gl.compileShader(shader);
|
|
622
|
+
gl.attachShader(handle, shader);
|
|
623
|
+
}
|
|
624
|
+
gl.linkProgram(handle);
|
|
625
|
+
return handle;
|
|
626
|
+
});
|
|
627
|
+
// Without the extension the driver still finishes on its own; just give it time before letting go.
|
|
628
|
+
const release = () => gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
629
|
+
if (!parallel) {
|
|
630
|
+
window.setTimeout(release, 5000);
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
const poll = () => {
|
|
634
|
+
if (handles.every((handle) => gl.getProgramParameter(handle, parallel.COMPLETION_STATUS_KHR))) release();
|
|
635
|
+
else window.setTimeout(poll, 50);
|
|
636
|
+
};
|
|
637
|
+
poll();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
let webgl2Available: boolean | null = null;
|
|
641
|
+
/** Pages and machines without WebGL2 keep the CSS part of a grade. */
|
|
642
|
+
export function canDrawGrade(): boolean {
|
|
643
|
+
if (webgl2Available !== null) return webgl2Available;
|
|
644
|
+
try {
|
|
645
|
+
webgl2Available = typeof document !== "undefined" && Boolean(document.createElement("canvas").getContext("webgl2"));
|
|
646
|
+
} catch {
|
|
647
|
+
webgl2Available = false;
|
|
648
|
+
}
|
|
649
|
+
return webgl2Available;
|
|
650
|
+
}
|