1// Wave 36 — Color Science, MatteControl & MultiMerge compute shaders (100% WebGPU-safe).
2//
3// All input textures are bound as unfilterable `texture_2d<f32>` and read via `textureLoad`
4// (with manual bilinear interpolation where needed), so every pass works uniformly on
5// `Rgba8Unorm`, `Rgba16Float`, and `Rgba32Float` on native and `wasm32` without requiring
6// `FLOAT32_FILTERABLE` or `read_write` storage textures.
7
8@group(0) @binding(0) var tex_a: texture_2d<f32>;
9@group(0) @binding(1) var tex_b: texture_2d<f32>;
10@group(0) @binding(2) var tex_c: texture_2d<f32>;
11@group(0) @binding(3) var mask_tex: texture_2d<f32>;
12@group(0) @binding(4) var dst_tex: texture_storage_2d<rgba8unorm, write>;
13
14struct Params {
15 p: array<vec4<f32>, 16>,
16};
17@group(0) @binding(5) var<uniform> u: Params;
18
19const HDR_FLOAT: bool = false;
20
21fn store_dst(p: vec2<i32>, col: vec4<f32>) {
22 if (HDR_FLOAT) {
23 textureStore(dst_tex, p, col);
24 } else {
25 textureStore(dst_tex, p, clamp(col, vec4<f32>(0.0), vec4<f32>(1.0)));
26 }
27}
28
29fn load_clamped(tex: texture_2d<f32>, p: vec2<i32>) -> vec4<f32> {
30 let d = vec2<i32>(textureDimensions(tex));
31 let c = clamp(p, vec2<i32>(0), max(d - vec2<i32>(1), vec2<i32>(0)));
32 return textureLoad(tex, c, 0);
33}
34
35fn sample_bilinear(tex: texture_2d<f32>, uv: vec2<f32>) -> vec4<f32> {
36 let dims = vec2<f32>(textureDimensions(tex));
37 let pos = uv * dims - vec2<f32>(0.5);
38 let p0 = vec2<i32>(floor(pos));
39 let f = pos - vec2<f32>(p0);
40 let c00 = load_clamped(tex, p0);
41 let c10 = load_clamped(tex, p0 + vec2<i32>(1, 0));
42 let c01 = load_clamped(tex, p0 + vec2<i32>(0, 1));
43 let c11 = load_clamped(tex, p0 + vec2<i32>(1, 1));
44 return mix(mix(c00, c10, f.x), mix(c01, c11, f.x), f.y);
45}
46
47fn read_mask(gid: vec2<u32>, dims: vec2<u32>, has_mask: f32, inv: f32) -> f32 {
48 if (has_mask < 0.5) {
49 return 1.0;
50 }
51 let uv = (vec2<f32>(gid) + vec2<f32>(0.5)) / vec2<f32>(dims);
52 let m = sample_bilinear(mask_tex, uv).a;
53 return select(m, 1.0 - m, inv > 0.5);
54}
55
56fn apply_mask_blend(
57 src: vec4<f32>,
58 res: vec4<f32>,
59 gid: vec2<u32>,
60 dims: vec2<u32>,
61 blend: f32,
62 has_mask: f32,
63 mask_inv: f32,
64 mbm: f32,
65 skip: vec4<f32>,
66) -> vec4<f32> {
67 let raw_mask = read_mask(gid, dims, has_mask, mask_inv);
68 let m = clamp(raw_mask * blend, 0.0, 1.0);
69 let use_mbm = has_mask > 0.5 && mbm > 0.5;
70 var out_col = select(mix(src, res, m), res * m, use_mbm);
71 let unproc = select(src, vec4<f32>(0.0), use_mbm);
72 out_col = select(out_col, unproc, skip > vec4<f32>(0.5));
73 return out_col;
74}
75
76fn mul_mat3_rows(r0: vec3<f32>, r1: vec3<f32>, r2: vec3<f32>, v: vec3<f32>) -> vec3<f32> {
77 return vec3<f32>(dot(r0, v), dot(r1, v), dot(r2, v));
78}
79
80// ============================================================================
81// Transfer functions (exact algebraic inverses for ACES 1.3 / sRGB / Rec.709)
82// ============================================================================
83fn srgb_to_linear_ch(c: f32) -> f32 {
84 let s = select(1.0, -1.0, c < 0.0);
85 let a = abs(c);
86 let lin = select(pow((a + 0.055) / 1.055, 2.4), a / 12.92, a <= 0.04045);
87 return s * lin;
88}
89
90fn linear_to_srgb_ch(c: f32) -> f32 {
91 let s = select(1.0, -1.0, c < 0.0);
92 let a = abs(c);
93 let enc = select(1.055 * pow(a, 1.0 / 2.4) - 0.055, a * 12.92, a <= 0.0031308);
94 return s * enc;
95}
96
97fn rec709_to_linear_ch(c: f32) -> f32 {
98 let s = select(1.0, -1.0, c < 0.0);
99 let a = abs(c);
100 let lin = select(pow((a + 0.099) / 1.099, 1.0 / 0.45), a / 4.5, a < 0.081);
101 return s * lin;
102}
103
104fn linear_to_rec709_ch(c: f32) -> f32 {
105 let s = select(1.0, -1.0, c < 0.0);
106 let a = abs(c);
107 let enc = select(1.099 * pow(a, 0.45) - 0.099, a * 4.5, a < 0.018);
108 return s * enc;
109}
110
111// Academy S-2016-001 ACEScct exact transfer
112const ACESCCT_X_BRK: f32 = 0.0078125;
113const ACESCCT_Y_BRK: f32 = 0.155251141552511;
114const ACESCCT_A: f32 = 10.5402377416545;
115const ACESCCT_B: f32 = 0.0729055341958355;
116
117fn acescct_to_linear_ch(y: f32) -> f32 {
118 if (y <= ACESCCT_Y_BRK) {
119 return (y - ACESCCT_B) / ACESCCT_A;
120 }
121 let y_max = (log2(65504.0) + 9.72) / 17.52;
122 if (y < y_max) {
123 return exp2(y * 17.52 - 9.72);
124 }
125 return 65504.0;
126}
127
128fn linear_to_acescct_ch(x: f32) -> f32 {
129 if (x <= ACESCCT_X_BRK) {
130 return ACESCCT_A * x + ACESCCT_B;
131 }
132 return (log2(x) + 9.72) / 17.52;
133}
134
135// Academy S-2014-003 ACEScc exact transfer
136fn acescc_to_linear_ch(y: f32) -> f32 {
137 let y_lo = (9.72 - 15.0) / 17.52;
138 if (y < y_lo) {
139 return (exp2(y * 17.52 - 9.72) - exp2(-16.0)) * 2.0;
140 }
141 let y_max = (log2(65504.0) + 9.72) / 17.52;
142 if (y < y_max) {
143 return exp2(y * 17.52 - 9.72);
144 }
145 return 65504.0;
146}
147
148fn linear_to_acescc_ch(x: f32) -> f32 {
149 if (x <= 0.0) {
150 return (-16.0 + 9.72) / 17.52;
151 }
152 if (x < exp2(-15.0)) {
153 return (log2(exp2(-16.0) + x * 0.5) + 9.72) / 17.52;
154 }
155 return (log2(x) + 9.72) / 17.52;
156}
157
158fn decode_transfer(rgb: vec3<f32>, xfer: i32) -> vec3<f32> {
159 switch xfer {
160 case 1: { return vec3<f32>(acescct_to_linear_ch(rgb.r), acescct_to_linear_ch(rgb.g), acescct_to_linear_ch(rgb.b)); }
161 case 2: { return vec3<f32>(acescc_to_linear_ch(rgb.r), acescc_to_linear_ch(rgb.g), acescc_to_linear_ch(rgb.b)); }
162 case 3: { return vec3<f32>(srgb_to_linear_ch(rgb.r), srgb_to_linear_ch(rgb.g), srgb_to_linear_ch(rgb.b)); }
163 case 4: { return vec3<f32>(rec709_to_linear_ch(rgb.r), rec709_to_linear_ch(rgb.g), rec709_to_linear_ch(rgb.b)); }
164 default: { return rgb; }
165 }
166}
167
168fn encode_transfer(rgb: vec3<f32>, xfer: i32) -> vec3<f32> {
169 switch xfer {
170 case 1: { return vec3<f32>(linear_to_acescct_ch(rgb.r), linear_to_acescct_ch(rgb.g), linear_to_acescct_ch(rgb.b)); }
171 case 2: { return vec3<f32>(linear_to_acescc_ch(rgb.r), linear_to_acescc_ch(rgb.g), linear_to_acescc_ch(rgb.b)); }
172 case 3: { return vec3<f32>(linear_to_srgb_ch(rgb.r), linear_to_srgb_ch(rgb.g), linear_to_srgb_ch(rgb.b)); }
173 case 4: { return vec3<f32>(linear_to_rec709_ch(rgb.r), linear_to_rec709_ch(rgb.g), linear_to_rec709_ch(rgb.b)); }
174 default: { return rgb; }
175 }
176}
177
178// Academy S-2021-001 ACES 1.3 Reference Gamut Compression (in AP1 linear space)
179fn rgc_compress_ch(dist: f32, lim: f32, thr: f32, pwr: f32) -> f32 {
180 if (dist <= thr) {
181 return dist;
182 }
183 let s = (lim - thr) / pow(pow((1.0 - thr) / (lim - thr), -pwr) - 1.0, 1.0 / pwr);
184 let nd = (dist - thr) / s;
185 return thr + s * nd / pow(1.0 + pow(nd, pwr), 1.0 / pwr);
186}
187
188fn aces_reference_gamut_compress(ap1: vec3<f32>) -> vec3<f32> {
189 let ach = max(ap1.r, max(ap1.g, ap1.b));
190 if (ach <= 1e-6) {
191 return ap1;
192 }
193 let inv_ach = 1.0 / abs(ach);
194 let d = (vec3<f32>(ach) - ap1) * inv_ach;
195 let dc = vec3<f32>(
196 rgc_compress_ch(d.r, 1.147, 0.815, 1.2),
197 rgc_compress_ch(d.g, 1.264, 0.803, 1.2),
198 rgc_compress_ch(d.b, 1.312, 0.880, 1.2),
199 );
200 return vec3<f32>(ach) - dc * abs(ach);
201}
202
203// ACES 1.3 RRT + ODT fit in AP1 space (with RRT_SAT=0.96, ODT_SAT=0.93)
204fn aces_rrt_odt_ap1(ap1_in: vec3<f32>, lum_scale: f32) -> vec3<f32> {
205 let ap1_w = vec3<f32>(0.2722287168, 0.6740817658, 0.0536895174);
206 let v_scaled = max(ap1_in * lum_scale, vec3<f32>(0.0));
207 let luma0 = dot(v_scaled, ap1_w);
208 let v = mix(vec3<f32>(luma0), v_scaled, 0.96);
209 let a = v * (v + 0.0245786) - 0.000090537;
210 let b = v * (0.983729 * v + 0.4329510) + 0.238081;
211 let t = max(a / b, vec3<f32>(0.0));
212 let luma1 = dot(t, ap1_w);
213 return mix(vec3<f32>(luma1), t, 0.93);
214}
215
216// ============================================================================
217// 1. AcesTransform
218// p[0] = (in_xfer, out_xfer, use_rgc, use_rrt_odt)
219// p[1] = (lum_scale, blend, has_mask, mask_inv)
220// p[2] = (mbm, 0, 0, 0)
221// p[3] = (skip_r, skip_g, skip_b, skip_a)
222// p[4..6] = M1 rows (in_linear -> AP1 if rgc/rrt, else direct in_linear -> out_linear)
223// p[7..9] = M2 rows (AP1 -> out_linear when rgc/rrt active)
224// ============================================================================
225@compute @workgroup_size(8, 8)
226fn aces_transform_op(@builtin(global_invocation_id) gid: vec3<u32>) {
227 let dims = textureDimensions(dst_tex);
228 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
229 let p = vec2<i32>(gid.xy);
230 let src = textureLoad(tex_a, p, 0);
231
232 let in_xfer = i32(round(u.p[0].x));
233 let out_xfer = i32(round(u.p[0].y));
234 let use_rgc = u.p[0].z > 0.5;
235 let use_rrt = u.p[0].w > 0.5;
236 let lum_scale = u.p[1].x;
237
238 var rgb = decode_transfer(src.rgb, in_xfer);
239 rgb = mul_mat3_rows(u.p[4].xyz, u.p[5].xyz, u.p[6].xyz, rgb);
240
241 if (use_rgc || use_rrt) {
242 if (use_rgc) {
243 rgb = aces_reference_gamut_compress(rgb);
244 }
245 if (use_rrt) {
246 rgb = aces_rrt_odt_ap1(rgb, lum_scale);
247 }
248 rgb = mul_mat3_rows(u.p[7].xyz, u.p[8].xyz, u.p[9].xyz, rgb);
249 if (use_rrt) {
250 rgb = clamp(rgb, vec3<f32>(0.0), vec3<f32>(1.0));
251 }
252 }
253
254 rgb = encode_transfer(rgb, out_xfer);
255 let res = vec4<f32>(rgb, src.a);
256 let out_col = apply_mask_blend(
257 src, res, gid.xy, dims, u.p[1].y, u.p[1].z, u.p[1].w, u.p[2].x, u.p[3],
258 );
259 store_dst(p, out_col);
260}
261
262// ============================================================================
263// 2. ColorSpace (RGB <-> BW / YUV / YIQ / CMY / HLS / HSV / XYZ / LAB / YCbCr)
264// p[0] = (conv_dir: 0=none 1=to_color 2=from_color, color_type: 0..8, blend, has_mask)
265// p[1] = (mask_inv, mbm, 0, 0)
266// p[2] = (skip_r, skip_g, skip_b, skip_a)
267// ============================================================================
268fn cs_rgb_to_hls(c: vec3<f32>) -> vec3<f32> {
269 let mx = max(c.r, max(c.g, c.b));
270 let mn = min(c.r, min(c.g, c.b));
271 let l = 0.5 * (mx + mn);
272 let d = mx - mn;
273 if (d <= 1e-6) {
274 return vec3<f32>(0.0, l, 0.0);
275 }
276 let denom = 1.0 - abs(2.0 * l - 1.0);
277 let s = select(0.0, d / denom, denom > 1e-6);
278 var h: f32;
279 if (mx == c.r) {
280 h = (c.g - c.b) / d + select(0.0, 6.0, c.g < c.b);
281 } else if (mx == c.g) {
282 h = (c.b - c.r) / d + 2.0;
283 } else {
284 h = (c.r - c.g) / d + 4.0;
285 }
286 return vec3<f32>(fract(h / 6.0), l, s);
287}
288
289fn cs_hue_to_rgb(p: f32, q: f32, t_in: f32) -> f32 {
290 let t = fract(t_in);
291 if (t < 1.0 / 6.0) { return p + (q - p) * 6.0 * t; }
292 if (t < 0.5) { return q; }
293 if (t < 2.0 / 3.0) { return p + (q - p) * (2.0 / 3.0 - t) * 6.0; }
294 return p;
295}
296
297fn cs_hls_to_rgb(hls: vec3<f32>) -> vec3<f32> {
298 let h = hls.x;
299 let l = hls.y;
300 let s = hls.z;
301 if (s <= 1e-6) {
302 return vec3<f32>(l);
303 }
304 let q = select(l + s - l * s, l * (1.0 + s), l < 0.5);
305 let p = 2.0 * l - q;
306 return vec3<f32>(
307 cs_hue_to_rgb(p, q, h + 1.0 / 3.0),
308 cs_hue_to_rgb(p, q, h),
309 cs_hue_to_rgb(p, q, h - 1.0 / 3.0),
310 );
311}
312
313fn cs_rgb_to_hsv(c: vec3<f32>) -> vec3<f32> {
314 let mx = max(c.r, max(c.g, c.b));
315 let mn = min(c.r, min(c.g, c.b));
316 let d = mx - mn;
317 let s = select(0.0, d / mx, mx > 1e-6);
318 if (d <= 1e-6) {
319 return vec3<f32>(0.0, s, mx);
320 }
321 var h: f32;
322 if (mx == c.r) {
323 h = (c.g - c.b) / d + select(0.0, 6.0, c.g < c.b);
324 } else if (mx == c.g) {
325 h = (c.b - c.r) / d + 2.0;
326 } else {
327 h = (c.r - c.g) / d + 4.0;
328 }
329 return vec3<f32>(fract(h / 6.0), s, mx);
330}
331
332fn cs_hsv_to_rgb(hsv: vec3<f32>) -> vec3<f32> {
333 let h = fract(hsv.x) * 6.0;
334 let s = hsv.y;
335 let v = hsv.z;
336 let c = v * s;
337 let x = c * (1.0 - abs(h - 2.0 * floor(h * 0.5) - 1.0));
338 let m = v - c;
339 var rgb: vec3<f32>;
340 if (h < 1.0) { rgb = vec3<f32>(c, x, 0.0); }
341 else if (h < 2.0) { rgb = vec3<f32>(x, c, 0.0); }
342 else if (h < 3.0) { rgb = vec3<f32>(0.0, c, x); }
343 else if (h < 4.0) { rgb = vec3<f32>(0.0, x, c); }
344 else if (h < 5.0) { rgb = vec3<f32>(x, 0.0, c); }
345 else { rgb = vec3<f32>(c, 0.0, x); }
346 return rgb + vec3<f32>(m);
347}
348
349fn cs_rgb_to_xyz(c: vec3<f32>) -> vec3<f32> {
350 return vec3<f32>(
351 0.4124564 * c.r + 0.3575761 * c.g + 0.1804375 * c.b,
352 0.2126729 * c.r + 0.7151522 * c.g + 0.0721750 * c.b,
353 0.0193339 * c.r + 0.1191920 * c.g + 0.9503041 * c.b,
354 );
355}
356
357fn cs_xyz_to_rgb(xyz: vec3<f32>) -> vec3<f32> {
358 return vec3<f32>(
359 3.2404542 * xyz.x - 1.5371385 * xyz.y - 0.4985314 * xyz.z,
360 -0.9692660 * xyz.x + 1.8760108 * xyz.y + 0.0415560 * xyz.z,
361 0.0556434 * xyz.x - 0.2040259 * xyz.y + 1.0572252 * xyz.z,
362 );
363}
364
365fn lab_f(t: f32) -> f32 {
366 let eps = 216.0 / 24389.0;
367 let k = 24389.0 / 27.0;
368 if (t > eps) {
369 return pow(t, 1.0 / 3.0);
370 }
371 return (k * t + 16.0) / 116.0;
372}
373
374fn lab_f_inv(t: f32) -> f32 {
375 let delta = 6.0 / 29.0;
376 if (t > delta) {
377 return t * t * t;
378 }
379 return 3.0 * delta * delta * (t - 4.0 / 29.0);
380}
381
382fn cs_rgb_to_lab(c: vec3<f32>) -> vec3<f32> {
383 let xyz = cs_rgb_to_xyz(c);
384 let fx = lab_f(xyz.x / 0.95047);
385 let fy = lab_f(xyz.y / 1.00000);
386 let fz = lab_f(xyz.z / 1.08883);
387 let l_star = 116.0 * fy - 16.0;
388 let a_star = 500.0 * (fx - fy);
389 let b_star = 200.0 * (fy - fz);
390 return vec3<f32>(l_star / 100.0, a_star / 256.0 + 0.5, b_star / 256.0 + 0.5);
391}
392
393fn cs_lab_to_rgb(lab: vec3<f32>) -> vec3<f32> {
394 let l_star = lab.x * 100.0;
395 let a_star = (lab.y - 0.5) * 256.0;
396 let b_star = (lab.z - 0.5) * 256.0;
397 let fy = (l_star + 16.0) / 116.0;
398 let fx = fy + a_star / 500.0;
399 let fz = fy - b_star / 200.0;
400 let xyz = vec3<f32>(0.95047 * lab_f_inv(fx), 1.00000 * lab_f_inv(fy), 1.08883 * lab_f_inv(fz));
401 return cs_xyz_to_rgb(xyz);
402}
403
404fn cs_rgb_to_yuv_k(c: vec3<f32>, kr: f32, kg: f32, kb: f32) -> vec3<f32> {
405 let y = kr * c.r + kg * c.g + kb * c.b;
406 let u_ch = (c.b - y) / (2.0 * (1.0 - kb)) + 0.5;
407 let v_ch = (c.r - y) / (2.0 * (1.0 - kr)) + 0.5;
408 return vec3<f32>(y, u_ch, v_ch);
409}
410
411fn cs_yuv_to_rgb_k(yuv: vec3<f32>, kr: f32, kg: f32, kb: f32) -> vec3<f32> {
412 let y = yuv.x;
413 let u_ch = yuv.y - 0.5;
414 let v_ch = yuv.z - 0.5;
415 let r = y + 2.0 * (1.0 - kr) * v_ch;
416 let b = y + 2.0 * (1.0 - kb) * u_ch;
417 let g = (y - kr * r - kb * b) / kg;
418 return vec3<f32>(r, g, b);
419}
420
421fn cs_rgb_to_yiq(c: vec3<f32>) -> vec3<f32> {
422 let y = 0.299 * c.r + 0.587 * c.g + 0.114 * c.b;
423 let i = 0.595716 * c.r - 0.274453 * c.g - 0.321263 * c.b;
424 let q = 0.211456 * c.r - 0.522591 * c.g + 0.311135 * c.b;
425 return vec3<f32>(y, i * 0.5 + 0.5, q * 0.5 + 0.5);
426}
427
428fn cs_yiq_to_rgb(yiq: vec3<f32>) -> vec3<f32> {
429 let y = yiq.x;
430 let i = (yiq.y - 0.5) * 2.0;
431 let q = (yiq.z - 0.5) * 2.0;
432 return vec3<f32>(
433 y + 0.9562957 * i + 0.6210244 * q,
434 y - 0.2721221 * i - 0.6473806 * q,
435 y - 1.1069890 * i + 1.7046150 * q,
436 );
437}
438
439@compute @workgroup_size(8, 8)
440fn color_space_op(@builtin(global_invocation_id) gid: vec3<u32>) {
441 let dims = textureDimensions(dst_tex);
442 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
443 let p = vec2<i32>(gid.xy);
444 let src = textureLoad(tex_a, p, 0);
445
446 let dir = i32(round(u.p[0].x));
447 let ctype = i32(round(u.p[0].y));
448 var rgb = src.rgb;
449
450 if (dir == 1) {
451 switch ctype {
452 case 0: { // BW
453 let l = dot(rgb, vec3<f32>(0.299, 0.587, 0.114));
454 rgb = vec3<f32>(l);
455 }
456 case 1: { rgb = cs_rgb_to_yuv_k(rgb, 0.299, 0.587, 0.114); }
457 case 2: { rgb = cs_rgb_to_yiq(rgb); }
458 case 3: { rgb = vec3<f32>(1.0) - rgb; }
459 case 4: { rgb = cs_rgb_to_hls(rgb); }
460 case 5: { rgb = cs_rgb_to_hsv(rgb); }
461 case 6: { rgb = cs_rgb_to_xyz(rgb); }
462 case 7: { rgb = cs_rgb_to_lab(rgb); }
463 case 8: { rgb = cs_rgb_to_yuv_k(rgb, 0.2126, 0.7152, 0.0722); }
464 default: {}
465 }
466 } else if (dir == 2) {
467 switch ctype {
468 case 0: {} // BW -> RGB is already grayscale
469 case 1: { rgb = cs_yuv_to_rgb_k(rgb, 0.299, 0.587, 0.114); }
470 case 2: { rgb = cs_yiq_to_rgb(rgb); }
471 case 3: { rgb = vec3<f32>(1.0) - rgb; }
472 case 4: { rgb = cs_hls_to_rgb(rgb); }
473 case 5: { rgb = cs_hsv_to_rgb(rgb); }
474 case 6: { rgb = cs_xyz_to_rgb(rgb); }
475 case 7: { rgb = cs_lab_to_rgb(rgb); }
476 case 8: { rgb = cs_yuv_to_rgb_k(rgb, 0.2126, 0.7152, 0.0722); }
477 default: {}
478 }
479 }
480
481 let res = vec4<f32>(rgb, src.a);
482 let out_col = apply_mask_blend(
483 src, res, gid.xy, dims, u.p[0].z, u.p[0].w, u.p[1].x, u.p[1].y, u.p[2],
484 );
485 store_dst(p, out_col);
486}
487
488// ============================================================================
489// 3. ColorMatrix (5x4 RGBA matrix + offset)
490// p[0..3] = matrix rows R, G, B, A (each vec4(c_r, c_g, c_b, c_a))
491// p[4] = offsets (off_r, off_g, off_b, off_a)
492// p[5] = (prediv, blend, has_mask, mask_inv)
493// p[6] = (mbm, clip_black, clip_white, 0)
494// p[7] = (skip_r, skip_g, skip_b, skip_a)
495// ============================================================================
496@compute @workgroup_size(8, 8)
497fn color_matrix_op(@builtin(global_invocation_id) gid: vec3<u32>) {
498 let dims = textureDimensions(dst_tex);
499 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
500 let p = vec2<i32>(gid.xy);
501 let src = textureLoad(tex_a, p, 0);
502
503 let prediv = u.p[5].x > 0.5;
504 var c = src;
505 if (prediv && src.a > 1e-6) {
506 c = vec4<f32>(src.rgb / src.a, src.a);
507 }
508
509 var res = vec4<f32>(
510 dot(u.p[0], c) + u.p[4].x,
511 dot(u.p[1], c) + u.p[4].y,
512 dot(u.p[2], c) + u.p[4].z,
513 dot(u.p[3], c) + u.p[4].w,
514 );
515
516 if (prediv) {
517 res = vec4<f32>(res.rgb * res.a, res.a);
518 }
519 if (u.p[6].y > 0.5) {
520 res = max(res, vec4<f32>(0.0));
521 }
522 if (u.p[6].z > 0.5) {
523 res = min(res, vec4<f32>(1.0));
524 }
525
526 let out_col = apply_mask_blend(
527 src, res, gid.xy, dims, u.p[5].y, u.p[5].z, u.p[5].w, u.p[6].x, u.p[7],
528 );
529 store_dst(p, out_col);
530}
531
532// ============================================================================
533// 4. WhiteBalance (Kelvin temperature / tint / sampled white-point gains)
534// p[0] = (gain_r, gain_g, gain_b, gamma_mode: 0=linear, 1=sRGB, 2=Rec709)
535// p[1] = (prediv, blend, has_mask, mask_inv)
536// p[2] = (mbm, 0, 0, 0)
537// p[3] = (skip_r, skip_g, skip_b, skip_a)
538// ============================================================================
539@compute @workgroup_size(8, 8)
540fn white_balance_op(@builtin(global_invocation_id) gid: vec3<u32>) {
541 let dims = textureDimensions(dst_tex);
542 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
543 let p = vec2<i32>(gid.xy);
544 let src = textureLoad(tex_a, p, 0);
545
546 let prediv = u.p[1].x > 0.5;
547 let gamma_mode = i32(round(u.p[0].w));
548 var rgb = src.rgb;
549 if (prediv && src.a > 1e-6) {
550 rgb = rgb / src.a;
551 }
552 if (gamma_mode == 1) {
553 rgb = decode_transfer(rgb, 3);
554 } else if (gamma_mode == 2) {
555 rgb = decode_transfer(rgb, 4);
556 }
557
558 rgb = rgb * u.p[0].xyz;
559
560 if (gamma_mode == 1) {
561 rgb = encode_transfer(rgb, 3);
562 } else if (gamma_mode == 2) {
563 rgb = encode_transfer(rgb, 4);
564 }
565 if (prediv) {
566 rgb = rgb * src.a;
567 }
568
569 let res = vec4<f32>(rgb, src.a);
570 let out_col = apply_mask_blend(
571 src, res, gid.xy, dims, u.p[1].y, u.p[1].z, u.p[1].w, u.p[2].x, u.p[3],
572 );
573 store_dst(p, out_col);
574}
575
576// ============================================================================
577// 5. MatteControl
578// Pass A: matte_combine_op
579// mode 0 (combine): tex_a = BG, tex_b = FG -> outputs vec4(m)
580// p[0] = (mode=0, matte_combine: 0..5, combine_op: 0..6, has_fg)
581// p[1] = (inv_fg, 0, 0, 0)
582// mode 1 (refine): tex_a = matte, tex_c = garbage, mask_tex = solid -> outputs vec4(m)
583// p[0] = (mode=1, thresh_lo, thresh_hi, matte_gamma)
584// p[1] = (invert_matte, has_garbage, garbage_inv, has_solid)
585// p[2] = (solid_inv, 0, 0, 0)
586// ============================================================================
587@compute @workgroup_size(8, 8)
588fn matte_combine_op(@builtin(global_invocation_id) gid: vec3<u32>) {
589 let dims = textureDimensions(dst_tex);
590 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
591 let p = vec2<i32>(gid.xy);
592 let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims);
593 let mode = i32(round(u.p[0].x));
594
595 if (mode == 0) {
596 let bg_a = textureLoad(tex_a, p, 0).a;
597 let mc = i32(round(u.p[0].y));
598 let op = i32(round(u.p[0].z));
599 let has_fg = u.p[0].w > 0.5;
600 var m = bg_a;
601 if (has_fg && mc > 0) {
602 let fg = sample_bilinear(tex_b, uv);
603 var fg_m: f32;
604 switch mc {
605 case 1: { fg_m = fg.a; }
606 case 2: { fg_m = dot(fg.rgb, vec3<f32>(0.299, 0.587, 0.114)); }
607 case 3: { fg_m = fg.r; }
608 case 4: { fg_m = fg.g; }
609 case 5: { fg_m = fg.b; }
610 default: { fg_m = fg.a; }
611 }
612 if (u.p[1].x > 0.5) {
613 fg_m = 1.0 - fg_m;
614 }
615 switch op {
616 case 0: { m = fg_m; } // Copy
617 case 1: { m = clamp(bg_a + fg_m, 0.0, 1.0); } // Add
618 case 2: { m = clamp(bg_a - fg_m, 0.0, 1.0); } // Subtract
619 case 3: { m = min(bg_a, fg_m); } // And / Min
620 case 4: { m = max(bg_a, fg_m); } // Or / Max
621 case 5: { m = bg_a * (1.0 - fg_m) + fg_m * (1.0 - bg_a); } // Xor
622 case 6: { m = bg_a * fg_m; } // Multiply
623 default: { m = fg_m; }
624 }
625 }
626 store_dst(p, vec4<f32>(m, m, m, m));
627 return;
628 }
629
630 // Refine pass: threshold, gamma, invert, Garbage.Matte, Solid.Matte
631 var m = textureLoad(tex_a, p, 0).a;
632 let lo = u.p[0].y;
633 let hi = u.p[0].z;
634 let gamma = max(u.p[0].w, 1e-4);
635 m = clamp((m - lo) / max(hi - lo, 1e-5), 0.0, 1.0);
636 if (abs(gamma - 1.0) > 1e-5) {
637 m = pow(m, 1.0 / gamma);
638 }
639 if (u.p[1].x > 0.5) {
640 m = 1.0 - m;
641 }
642 if (u.p[1].y > 0.5) {
643 var gm = sample_bilinear(tex_c, uv).a;
644 if (u.p[1].z > 0.5) {
645 gm = 1.0 - gm;
646 }
647 m = m * (1.0 - clamp(gm, 0.0, 1.0));
648 }
649 if (u.p[1].w > 0.5) {
650 var sm = sample_bilinear(mask_tex, uv).a;
651 if (u.p[2].x > 0.5) {
652 sm = 1.0 - sm;
653 }
654 m = max(m, clamp(sm, 0.0, 1.0));
655 }
656 store_dst(p, vec4<f32>(m, m, m, m));
657}
658
659// Pass B: matte_finish_op
660// tex_a = BG, tex_b = refined matte, mask_tex = EffectMask
661// p[0] = (spill_amount, spill_color: 0=G 1=B 2=R, post_multiply, blend)
662// p[1] = (has_mask, mask_inv, mbm, 0)
663// p[2] = (skip_r, skip_g, skip_b, skip_a)
664@compute @workgroup_size(8, 8)
665fn matte_finish_op(@builtin(global_invocation_id) gid: vec3<u32>) {
666 let dims = textureDimensions(dst_tex);
667 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
668 let p = vec2<i32>(gid.xy);
669 let src = textureLoad(tex_a, p, 0);
670 let m = clamp(textureLoad(tex_b, p, 0).a, 0.0, 1.0);
671
672 var rgb = src.rgb;
673 let spill = clamp(u.p[0].x, 0.0, 1.0);
674 if (spill > 1e-5) {
675 let sc = i32(round(u.p[0].y));
676 if (sc == 1) {
677 let lim = 0.5 * (rgb.r + rgb.g);
678 let ex = max(rgb.b - lim, 0.0);
679 rgb.b = rgb.b - ex * spill;
680 } else if (sc == 2) {
681 let lim = 0.5 * (rgb.g + rgb.b);
682 let ex = max(rgb.r - lim, 0.0);
683 rgb.r = rgb.r - ex * spill;
684 } else {
685 let lim = 0.5 * (rgb.r + rgb.b);
686 let ex = max(rgb.g - lim, 0.0);
687 rgb.g = rgb.g - ex * spill;
688 }
689 }
690
691 if (u.p[0].z > 0.5) {
692 rgb = rgb * m;
693 }
694
695 let res = vec4<f32>(rgb, m);
696 let out_col = apply_mask_blend(
697 src, res, gid.xy, dims, u.p[0].w, u.p[1].x, u.p[1].y, u.p[1].z, u.p[2],
698 );
699 store_dst(p, out_col);
700}
701
702// ============================================================================
703// 6. Global Blend/Mask combiner (used by MultiMerge when global Blend/EffectMask active)
704// tex_a = original BG, tex_b = composited result, mask_tex = EffectMask
705// p[0] = (blend, has_mask, mask_inv, mbm)
706// p[1] = (skip_r, skip_g, skip_b, skip_a)
707// ============================================================================
708@compute @workgroup_size(8, 8)
709fn blend_mask_op(@builtin(global_invocation_id) gid: vec3<u32>) {
710 let dims = textureDimensions(dst_tex);
711 if (gid.x >= dims.x || gid.y >= dims.y) { return; }
712 let p = vec2<i32>(gid.xy);
713 let uv = (vec2<f32>(gid.xy) + vec2<f32>(0.5)) / vec2<f32>(dims);
714 let bg = sample_bilinear(tex_a, uv);
715 let fg = textureLoad(tex_b, p, 0);
716 let out_col = apply_mask_blend(
717 bg, fg, gid.xy, dims, u.p[0].x, u.p[0].y, u.p[0].z, u.p[0].w, u.p[1],
718 );
719 store_dst(p, out_col);
720}