-
-
Notifications
You must be signed in to change notification settings - Fork 246
Expand file tree
/
Copy pathshader.wgsl
More file actions
1600 lines (1378 loc) · 60.9 KB
/
shader.wgsl
File metadata and controls
1600 lines (1378 loc) · 60.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
struct Point {
x: f32,
y: f32,
_pad1: f32,
_pad2: f32,
}
struct HslColor {
hue: f32,
saturation: f32,
luminance: f32,
_pad: f32,
}
struct ColorGradeSettings {
hue: f32,
saturation: f32,
luminance: f32,
_pad: f32,
}
struct ColorCalibrationSettings {
shadows_tint: f32,
red_hue: f32,
red_saturation: f32,
green_hue: f32,
green_saturation: f32,
blue_hue: f32,
blue_saturation: f32,
_pad1: f32,
}
struct GlobalAdjustments {
exposure: f32,
brightness: f32,
contrast: f32,
highlights: f32,
shadows: f32,
whites: f32,
blacks: f32,
saturation: f32,
temperature: f32,
tint: f32,
vibrance: f32,
sharpness: f32,
luma_noise_reduction: f32,
color_noise_reduction: f32,
clarity: f32,
dehaze: f32,
structure: f32,
centre: f32,
vignette_amount: f32,
vignette_midpoint: f32,
vignette_roundness: f32,
vignette_feather: f32,
grain_amount: f32,
grain_size: f32,
grain_roughness: f32,
chromatic_aberration_red_cyan: f32,
chromatic_aberration_blue_yellow: f32,
show_clipping: u32,
is_raw_image: u32,
_pad_ca1: f32,
has_lut: u32,
lut_intensity: f32,
tonemapper_mode: u32,
_pad_lut2: f32,
_pad_lut3: f32,
_pad_lut4: f32,
_pad_lut5: f32,
_pad_agx1: f32,
_pad_agx2: f32,
_pad_agx3: f32,
agx_pipe_to_rendering_matrix: mat3x3<f32>,
agx_rendering_to_pipe_matrix: mat3x3<f32>,
_pad_cg1: f32,
_pad_cg2: f32,
_pad_cg3: f32,
_pad_cg4: f32,
color_grading_shadows: ColorGradeSettings,
color_grading_midtones: ColorGradeSettings,
color_grading_highlights: ColorGradeSettings,
color_grading_blending: f32,
color_grading_balance: f32,
_pad2: f32,
_pad3: f32,
color_calibration: ColorCalibrationSettings,
hsl: array<HslColor, 8>,
luma_curve: array<Point, 16>,
red_curve: array<Point, 16>,
green_curve: array<Point, 16>,
blue_curve: array<Point, 16>,
luma_curve_count: u32,
red_curve_count: u32,
green_curve_count: u32,
blue_curve_count: u32,
lab_curve_l: array<Point, 16>,
lab_curve_a: array<Point, 16>,
lab_curve_b: array<Point, 16>,
lab_curve_l_count: u32,
lab_curve_a_count: u32,
lab_curve_b_count: u32,
glow_amount: f32,
halation_amount: f32,
flare_amount: f32,
_pad_creative_1: f32,
}
struct MaskAdjustments {
exposure: f32,
brightness: f32,
contrast: f32,
highlights: f32,
shadows: f32,
whites: f32,
blacks: f32,
saturation: f32,
temperature: f32,
tint: f32,
vibrance: f32,
sharpness: f32,
luma_noise_reduction: f32,
color_noise_reduction: f32,
clarity: f32,
dehaze: f32,
structure: f32,
glow_amount: f32,
halation_amount: f32,
flare_amount: f32,
_pad1: f32,
_pad_cg1: f32,
_pad_cg2: f32,
_pad_cg3: f32,
color_grading_shadows: ColorGradeSettings,
color_grading_midtones: ColorGradeSettings,
color_grading_highlights: ColorGradeSettings,
color_grading_blending: f32,
color_grading_balance: f32,
_pad5: f32,
_pad6: f32,
hsl: array<HslColor, 8>,
luma_curve: array<Point, 16>,
red_curve: array<Point, 16>,
green_curve: array<Point, 16>,
blue_curve: array<Point, 16>,
luma_curve_count: u32,
red_curve_count: u32,
green_curve_count: u32,
blue_curve_count: u32,
lab_curve_l: array<Point, 16>,
lab_curve_a: array<Point, 16>,
lab_curve_b: array<Point, 16>,
lab_curve_l_count: u32,
lab_curve_a_count: u32,
lab_curve_b_count: u32,
_pad_end: f32,
}
struct AllAdjustments {
global: GlobalAdjustments,
mask_adjustments: array<MaskAdjustments, 8>,
mask_count: u32,
tile_offset_x: u32,
tile_offset_y: u32,
mask_atlas_cols: u32,
_pad_tail: array<vec4<f32>, 3>,
}
struct HslRange {
center: f32,
width: f32,
}
const HSL_RANGES: array<HslRange, 8> = array<HslRange, 8>(
HslRange(358.0, 35.0), // Red
HslRange(25.0, 45.0), // Orange
HslRange(60.0, 40.0), // Yellow
HslRange(115.0, 90.0), // Green
HslRange(180.0, 60.0), // Aqua
HslRange(225.0, 60.0), // Blue
HslRange(280.0, 55.0), // Purple
HslRange(330.0, 50.0) // Magenta
);
@group(0) @binding(0) var input_texture: texture_2d<f32>;
@group(0) @binding(1) var output_texture: texture_storage_2d<rgba8unorm, write>;
@group(0) @binding(2) var<uniform> adjustments: AllAdjustments;
@group(0) @binding(3) var mask0: texture_2d<f32>;
@group(0) @binding(4) var mask1: texture_2d<f32>;
@group(0) @binding(5) var mask2: texture_2d<f32>;
@group(0) @binding(6) var mask3: texture_2d<f32>;
@group(0) @binding(7) var mask4: texture_2d<f32>;
@group(0) @binding(8) var mask5: texture_2d<f32>;
@group(0) @binding(9) var mask6: texture_2d<f32>;
@group(0) @binding(10) var mask7: texture_2d<f32>;
@group(0) @binding(11) var lut_texture: texture_3d<f32>;
@group(0) @binding(12) var lut_sampler: sampler;
@group(0) @binding(13) var sharpness_blur_texture: texture_2d<f32>;
@group(0) @binding(14) var tonal_blur_texture: texture_2d<f32>;
@group(0) @binding(15) var clarity_blur_texture: texture_2d<f32>;
@group(0) @binding(16) var structure_blur_texture: texture_2d<f32>;
@group(0) @binding(17) var flare_texture: texture_2d<f32>;
@group(0) @binding(18) var flare_sampler: sampler;
const LUMA_COEFF = vec3<f32>(0.2126, 0.7152, 0.0722);
fn get_luma(c: vec3<f32>) -> f32 {
return dot(c, LUMA_COEFF);
}
fn srgb_to_linear(c: vec3<f32>) -> vec3<f32> {
let cutoff = vec3<f32>(0.04045);
let a = vec3<f32>(0.055);
let higher = pow((c + a) / (1.0 + a), vec3<f32>(2.4));
let lower = c / 12.92;
return select(higher, lower, c <= cutoff);
}
fn linear_to_srgb(c: vec3<f32>) -> vec3<f32> {
let c_clamped = clamp(c, vec3<f32>(0.0), vec3<f32>(1.0));
let cutoff = vec3<f32>(0.0031308);
let a = vec3<f32>(0.055);
let higher = (1.0 + a) * pow(c_clamped, vec3<f32>(1.0 / 2.4)) - a;
let lower = c_clamped * 12.92;
return select(higher, lower, c_clamped <= cutoff);
}
fn lab_f(t: f32) -> f32 {
return select(7.787 * t + 0.137931034, pow(max(t, 0.0), 0.33333334), t > 0.008856);
}
fn lab_inv_f(t: f32) -> f32 {
return select((t - 0.137931034) / 7.787, t * t * t, t > 0.20689655);
}
fn srgb_to_lab(c: vec3<f32>) -> vec3<f32> {
let lin = srgb_to_linear(c);
let x = lin.r * 0.4124564 + lin.g * 0.3575761 + lin.b * 0.1804375;
let y = lin.r * 0.2126729 + lin.g * 0.7151522 + lin.b * 0.0721750;
let z = lin.r * 0.0193339 + lin.g * 0.1191920 + lin.b * 0.9503041;
let xn = 0.95047;
let yn = 1.0;
let zn = 1.08883;
let fx = lab_f(x / xn);
let fy = lab_f(y / yn);
let fz = lab_f(z / zn);
let L = 116.0 * fy - 16.0;
let a = 500.0 * (fx - fy);
let b = 200.0 * (fy - fz);
return vec3<f32>(L, a, b);
}
fn lab_to_srgb(lab: vec3<f32>) -> vec3<f32> {
let xn = 0.95047;
let yn = 1.0;
let zn = 1.08883;
let fy = (lab.x + 16.0) / 116.0;
let fx = lab.y / 500.0 + fy;
let fz = fy - lab.z / 200.0;
let x = xn * lab_inv_f(fx);
let y = yn * lab_inv_f(fy);
let z = zn * lab_inv_f(fz);
let r = x * 3.2404542 + y * (-1.5371385) + z * (-0.4985314);
let g = x * (-0.9692660) + y * 1.8760108 + z * 0.0415560;
let b = x * 0.0556434 + y * (-0.2040259) + z * 1.0572252;
return linear_to_srgb(vec3<f32>(r, g, b));
}
fn rgb_to_hsv(c: vec3<f32>) -> vec3<f32> {
let c_max = max(c.r, max(c.g, c.b));
let c_min = min(c.r, min(c.g, c.b));
let delta = c_max - c_min;
var h: f32 = 0.0;
if (delta > 0.0) {
if (c_max == c.r) { h = 60.0 * (((c.g - c.b) / delta) % 6.0); }
else if (c_max == c.g) { h = 60.0 * (((c.b - c.r) / delta) + 2.0); }
else { h = 60.0 * (((c.r - c.g) / delta) + 4.0); }
}
if (h < 0.0) { h += 360.0; }
let s = select(0.0, delta / c_max, c_max > 0.0);
return vec3<f32>(h, s, c_max);
}
fn hsv_to_rgb(c: vec3<f32>) -> vec3<f32> {
let h = c.x; let s = c.y; let v = c.z;
let C = v * s;
let X = C * (1.0 - abs((h / 60.0) % 2.0 - 1.0));
let m = v - C;
var rgb_prime: vec3<f32>;
if (h < 60.0) { rgb_prime = vec3<f32>(C, X, 0.0); }
else if (h < 120.0) { rgb_prime = vec3<f32>(X, C, 0.0); }
else if (h < 180.0) { rgb_prime = vec3<f32>(0.0, C, X); }
else if (h < 240.0) { rgb_prime = vec3<f32>(0.0, X, C); }
else if (h < 300.0) { rgb_prime = vec3<f32>(X, 0.0, C); }
else { rgb_prime = vec3<f32>(C, 0.0, X); }
return rgb_prime + vec3<f32>(m, m, m);
}
fn get_raw_hsl_influence(hue: f32, center: f32, width: f32) -> f32 {
let dist = min(abs(hue - center), 360.0 - abs(hue - center));
const sharpness = 1.5;
let falloff = dist / (width * 0.5);
return exp(-sharpness * falloff * falloff);
}
fn hash(p: vec2<f32>) -> f32 {
var p3 = fract(vec3<f32>(p.xyx) * .1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
fn gradient_noise(p: vec2<f32>) -> f32 {
let i = floor(p);
let f = fract(p);
let u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
let ga = vec2<f32>(hash(i + vec2(0.0, 0.0)), hash(i + vec2(0.0, 0.0) + vec2(11.0, 37.0))) * 2.0 - 1.0;
let gb = vec2<f32>(hash(i + vec2(1.0, 0.0)), hash(i + vec2(1.0, 0.0) + vec2(11.0, 37.0))) * 2.0 - 1.0;
let gc = vec2<f32>(hash(i + vec2(0.0, 1.0)), hash(i + vec2(0.0, 1.0) + vec2(11.0, 37.0))) * 2.0 - 1.0;
let gd = vec2<f32>(hash(i + vec2(1.0, 1.0)), hash(i + vec2(1.0, 1.0) + vec2(11.0, 37.0))) * 2.0 - 1.0;
let dot_00 = dot(ga, f - vec2(0.0, 0.0));
let dot_10 = dot(gb, f - vec2(1.0, 0.0));
let dot_01 = dot(gc, f - vec2(0.0, 1.0));
let dot_11 = dot(gd, f - vec2(1.0, 1.0));
let bottom_interp = mix(dot_00, dot_10, u.x);
let top_interp = mix(dot_01, dot_11, u.x);
return mix(bottom_interp, top_interp, u.y);
}
fn dither(coords: vec2<u32>) -> f32 {
let p = vec2<f32>(coords);
return fract(sin(dot(p, vec2<f32>(12.9898, 78.233))) * 43758.5453) - 0.5;
}
fn interpolate_cubic_hermite(x: f32, p1: Point, p2: Point, m1: f32, m2: f32) -> f32 {
let dx = p2.x - p1.x;
if (dx <= 0.0) { return p1.y; }
let t = (x - p1.x) / dx;
let t2 = t * t;
let t3 = t2 * t;
let h00 = 2.0 * t3 - 3.0 * t2 + 1.0;
let h10 = t3 - 2.0 * t2 + t;
let h01 = -2.0 * t3 + 3.0 * t2;
let h11 = t3 - t2;
return h00 * p1.y + h10 * m1 * dx + h01 * p2.y + h11 * m2 * dx;
}
fn apply_curve(val: f32, points: array<Point, 16>, count: u32) -> f32 {
if (count < 2u) { return val; }
var local_points = points;
let x = val * 255.0;
if (x <= local_points[0].x) { return local_points[0].y / 255.0; }
if (x >= local_points[count - 1u].x) { return local_points[count - 1u].y / 255.0; }
for (var i = 0u; i < 15u; i = i + 1u) {
if (i >= count - 1u) { break; }
let p1 = local_points[i];
let p2 = local_points[i + 1u];
if (x <= p2.x) {
let p0 = local_points[max(0u, i - 1u)];
let p3 = local_points[min(count - 1u, i + 2u)];
let delta_before = (p1.y - p0.y) / max(0.001, p1.x - p0.x);
let delta_current = (p2.y - p1.y) / max(0.001, p2.x - p1.x);
let delta_after = (p3.y - p2.y) / max(0.001, p3.x - p2.x);
var tangent_at_p1: f32;
var tangent_at_p2: f32;
if (i == 0u) { tangent_at_p1 = delta_current; } else {
if (delta_before * delta_current <= 0.0) { tangent_at_p1 = 0.0; } else { tangent_at_p1 = (delta_before + delta_current) / 2.0; }
}
if (i + 1u == count - 1u) { tangent_at_p2 = delta_current; } else {
if (delta_current * delta_after <= 0.0) { tangent_at_p2 = 0.0; } else { tangent_at_p2 = (delta_current + delta_after) / 2.0; }
}
if (delta_current != 0.0) {
let alpha = tangent_at_p1 / delta_current;
let beta = tangent_at_p2 / delta_current;
if (alpha * alpha + beta * beta > 9.0) {
let tau = 3.0 / sqrt(alpha * alpha + beta * beta);
tangent_at_p1 = tangent_at_p1 * tau;
tangent_at_p2 = tangent_at_p2 * tau;
}
}
let result_y = interpolate_cubic_hermite(x, p1, p2, tangent_at_p1, tangent_at_p2);
return clamp(result_y / 255.0, 0.0, 1.0);
}
}
return local_points[count - 1u].y / 255.0;
}
fn get_shadow_mult(luma: f32, sh: f32, bl: f32) -> f32 {
var mult = 1.0;
let safe_luma = max(luma, 0.0001);
if (bl != 0.0) {
let limit = 0.05;
if (safe_luma < limit) {
let x = safe_luma / limit;
let mask = (1.0 - x) * (1.0 - x);
let factor = min(exp2(bl * 0.75), 3.9);
mult *= mix(1.0, factor, mask);
}
}
if (sh != 0.0) {
let limit = 0.1;
if (safe_luma < limit) {
let x = safe_luma / limit;
let mask = (1.0 - x) * (1.0 - x);
let factor = min(exp2(sh * 1.5), 3.9);
mult *= mix(1.0, factor, mask);
}
}
return mult;
}
fn apply_tonal_adjustments(
color: vec3<f32>,
blurred_color_input_space: vec3<f32>,
is_raw: u32,
con: f32,
sh: f32,
wh: f32,
bl: f32
) -> vec3<f32> {
var rgb = color;
var blurred_linear: vec3<f32>;
if (is_raw == 1u) {
blurred_linear = blurred_color_input_space;
} else {
blurred_linear = srgb_to_linear(blurred_color_input_space);
}
if (wh != 0.0) {
let white_level = 1.0 - wh * 0.25;
let w_mult = 1.0 / max(white_level, 0.01);
rgb *= w_mult;
blurred_linear *= w_mult;
}
let pixel_luma = get_luma(max(rgb, vec3<f32>(0.0)));
let blurred_luma = get_luma(max(blurred_linear, vec3<f32>(0.0)));
let safe_pixel_luma = max(pixel_luma, 0.0001);
let safe_blurred_luma = max(blurred_luma, 0.0001);
let perc_pixel = pow(safe_pixel_luma, 0.5);
let perc_blurred = pow(safe_blurred_luma, 0.5);
let edge_diff = abs(perc_pixel - perc_blurred);
let halo_protection = smoothstep(0.05, 0.25, edge_diff);
if (sh != 0.0 || bl != 0.0) {
let spatial_mult = get_shadow_mult(safe_blurred_luma, sh, bl);
let pixel_mult = get_shadow_mult(safe_pixel_luma, sh, bl);
let final_mult = mix(spatial_mult, pixel_mult, halo_protection);
rgb *= final_mult;
}
if (con != 0.0) {
let safe_rgb = max(rgb, vec3<f32>(0.0));
let g = 2.2;
let perceptual = pow(safe_rgb, vec3<f32>(1.0 / g));
let clamped_perceptual = clamp(perceptual, vec3<f32>(0.0), vec3<f32>(1.0));
let strength = pow(2.0, con * 1.25);
let condition = clamped_perceptual < vec3<f32>(0.5);
let high_part = 1.0 - 0.5 * pow(2.0 * (1.0 - clamped_perceptual), vec3<f32>(strength));
let low_part = 0.5 * pow(2.0 * clamped_perceptual, vec3<f32>(strength));
let curved_perceptual = select(high_part, low_part, condition);
let contrast_adjusted_rgb = pow(curved_perceptual, vec3<f32>(g));
let mix_factor = smoothstep(vec3<f32>(1.0), vec3<f32>(1.01), safe_rgb);
rgb = mix(contrast_adjusted_rgb, rgb, mix_factor);
}
return rgb;
}
fn apply_highlights_adjustment(
color_in: vec3<f32>,
blurred_color_input_space: vec3<f32>,
is_raw: u32,
highlights_adj: f32
) -> vec3<f32> {
if (highlights_adj == 0.0) { return color_in; }
let pixel_luma = get_luma(max(color_in, vec3<f32>(0.0)));
let safe_pixel_luma = max(pixel_luma, 0.0001);
let pixel_mask_input = tanh(safe_pixel_luma * 1.5);
let highlight_mask = smoothstep(0.3, 0.95, pixel_mask_input);
if (highlight_mask < 0.001) {
return color_in;
}
let luma = pixel_luma;
var final_adjusted_color: vec3<f32>;
if (highlights_adj < 0.0) {
var new_luma: f32;
if (luma <= 1.0) {
let gamma = 1.0 - highlights_adj * 1.75;
new_luma = pow(luma, gamma);
} else {
let luma_excess = luma - 1.0;
let compression_strength = -highlights_adj * 6.0;
let compressed_excess = luma_excess / (1.0 + luma_excess * compression_strength);
new_luma = 1.0 + compressed_excess;
}
let tonally_adjusted_color = color_in * (new_luma / max(luma, 0.0001));
let desaturation_amount = smoothstep(1.0, 10.0, luma);
let white_point = vec3<f32>(new_luma);
final_adjusted_color = mix(tonally_adjusted_color, white_point, desaturation_amount);
} else {
let adjustment = highlights_adj * 1.75;
let factor = pow(2.0, adjustment);
final_adjusted_color = color_in * factor;
}
return mix(color_in, final_adjusted_color, highlight_mask);
}
fn apply_linear_exposure(color_in: vec3<f32>, exposure_adj: f32) -> vec3<f32> {
if (exposure_adj == 0.0) {
return color_in;
}
return color_in * pow(2.0, exposure_adj);
}
fn apply_filmic_exposure(color_in: vec3<f32>, brightness_adj: f32) -> vec3<f32> {
if (brightness_adj == 0.0) {
return color_in;
}
const RATIONAL_CURVE_MIX: f32 = 0.95;
const MIDTONE_STRENGTH: f32 = 1.2;
let original_luma = get_luma(color_in);
if (abs(original_luma) < 0.00001) {
return color_in;
}
let direct_adj = brightness_adj * (1.0 - RATIONAL_CURVE_MIX);
let rational_adj = brightness_adj * RATIONAL_CURVE_MIX;
let scale = pow(2.0, direct_adj);
let k = pow(2.0, -rational_adj * MIDTONE_STRENGTH);
let luma_abs = abs(original_luma);
let luma_floor = floor(luma_abs);
let luma_fract = luma_abs - luma_floor;
let shaped_fract = luma_fract / (luma_fract + (1.0 - luma_fract) * k);
let shaped_luma_abs = luma_floor + shaped_fract;
let new_luma = sign(original_luma) * shaped_luma_abs * scale;
let chroma = color_in - vec3<f32>(original_luma);
let total_luma_scale = new_luma / original_luma;
let chroma_scale = pow(total_luma_scale, 0.8);
return vec3<f32>(new_luma) + chroma * chroma_scale;
}
fn apply_color_calibration(color: vec3<f32>, cal: ColorCalibrationSettings) -> vec3<f32> {
let h_r = cal.red_hue;
let h_g = cal.green_hue;
let h_b = cal.blue_hue;
let r_prime = vec3<f32>(1.0 - abs(h_r), max(0.0, h_r), max(0.0, -h_r));
let g_prime = vec3<f32>(max(0.0, -h_g), 1.0 - abs(h_g), max(0.0, h_g));
let b_prime = vec3<f32>(max(0.0, h_b), max(0.0, -h_b), 1.0 - abs(h_b));
let hue_matrix = mat3x3<f32>(r_prime, g_prime, b_prime);
var c = hue_matrix * color;
let luma = get_luma(max(vec3(0.0), c));
let desaturated_color = vec3<f32>(luma);
let sat_vector = c - desaturated_color;
let color_sum = c.r + c.g + c.b;
var masks = vec3<f32>(0.0);
if (color_sum > 0.001) {
masks = c / color_sum;
}
let total_sat_adjustment =
masks.r * cal.red_saturation +
masks.g * cal.green_saturation +
masks.b * cal.blue_saturation;
c += sat_vector * total_sat_adjustment;
let st = cal.shadows_tint;
if (abs(st) > 0.001) {
let shadow_luma = get_luma(max(vec3(0.0), c));
let mask = 1.0 - smoothstep(0.0, 0.3, shadow_luma);
let tint_mult = vec3<f32>(1.0 + st * 0.25, 1.0 - st * 0.25, 1.0 + st * 0.25);
c = mix(c, c * tint_mult, mask);
}
return c;
}
fn apply_white_balance(color: vec3<f32>, temp: f32, tnt: f32) -> vec3<f32> {
var rgb = color;
let temp_kelvin_mult = vec3<f32>(1.0 + temp * 0.2, 1.0 + temp * 0.05, 1.0 - temp * 0.2);
let tint_mult = vec3<f32>(1.0 + tnt * 0.25, 1.0 - tnt * 0.25, 1.0 + tnt * 0.25);
rgb *= temp_kelvin_mult * tint_mult;
return rgb;
}
fn apply_creative_color(color: vec3<f32>, sat: f32, vib: f32) -> vec3<f32> {
var processed = color;
let luma = get_luma(processed);
if (sat != 0.0) {
processed = mix(vec3<f32>(luma), processed, 1.0 + sat);
}
if (vib == 0.0) { return processed; }
let c_max = max(processed.r, max(processed.g, processed.b));
let c_min = min(processed.r, min(processed.g, processed.b));
let delta = c_max - c_min;
if (delta < 0.02) {
return processed;
}
let current_sat = delta / max(c_max, 0.001);
if (vib > 0.0) {
let sat_mask = 1.0 - smoothstep(0.4, 0.9, current_sat);
let hsv = rgb_to_hsv(processed);
let hue = hsv.x;
let skin_center = 25.0;
let hue_dist = min(abs(hue - skin_center), 360.0 - abs(hue - skin_center));
let is_skin = smoothstep(35.0, 10.0, hue_dist);
let skin_dampener = mix(1.0, 0.6, is_skin);
let amount = vib * sat_mask * skin_dampener * 3.0;
processed = mix(vec3<f32>(luma), processed, 1.0 + amount);
} else {
let desat_mask = 1.0 - smoothstep(0.2, 0.8, current_sat);
let amount = vib * desat_mask;
processed = mix(vec3<f32>(luma), processed, 1.0 + amount);
}
return processed;
}
fn apply_hsl_panel(color: vec3<f32>, hsl_adjustments: array<HslColor, 8>, coords_i: vec2<i32>) -> vec3<f32> {
if (distance(color.r, color.g) < 0.001 && distance(color.g, color.b) < 0.001) {
return color;
}
let original_hsv = rgb_to_hsv(color);
let original_luma = get_luma(color);
let saturation_mask = smoothstep(0.05, 0.20, original_hsv.y);
let luminance_weight = smoothstep(0.0, 1.0, original_hsv.y);
if (saturation_mask < 0.001 && luminance_weight < 0.001) {
return color;
}
let original_hue = original_hsv.x;
var raw_influences: array<f32, 8>;
var total_raw_influence: f32 = 0.0;
for (var i = 0u; i < 8u; i = i + 1u) {
let range = HSL_RANGES[i];
let influence = get_raw_hsl_influence(original_hue, range.center, range.width);
raw_influences[i] = influence;
total_raw_influence += influence;
}
var total_hue_shift: f32 = 0.0;
var total_sat_multiplier: f32 = 0.0;
var total_lum_adjust: f32 = 0.0;
for (var i = 0u; i < 8u; i = i + 1u) {
let normalized_influence = raw_influences[i] / total_raw_influence;
let hue_sat_influence = normalized_influence * saturation_mask;
let luma_influence = normalized_influence * luminance_weight;
total_hue_shift += hsl_adjustments[i].hue * 2.0 * hue_sat_influence;
total_sat_multiplier += hsl_adjustments[i].saturation * hue_sat_influence;
total_lum_adjust += hsl_adjustments[i].luminance * luma_influence;
}
if (original_hsv.y * (1.0 + total_sat_multiplier) < 0.0001) {
let final_luma = original_luma * (1.0 + total_lum_adjust);
return vec3<f32>(final_luma);
}
var hsv = original_hsv;
hsv.x = (hsv.x + total_hue_shift + 360.0) % 360.0;
hsv.y = clamp(hsv.y * (1.0 + total_sat_multiplier), 0.0, 1.0);
let hs_shifted_rgb = hsv_to_rgb(vec3<f32>(hsv.x, hsv.y, original_hsv.z));
let new_luma = get_luma(hs_shifted_rgb);
let target_luma = original_luma * (1.0 + total_lum_adjust);
if (new_luma < 0.0001) {
return vec3<f32>(max(0.0, target_luma));
}
let final_color = hs_shifted_rgb * (target_luma / new_luma);
return final_color;
}
fn apply_color_grading(color: vec3<f32>, shadows: ColorGradeSettings, midtones: ColorGradeSettings, highlights: ColorGradeSettings, blending: f32, balance: f32) -> vec3<f32> {
let luma = get_luma(max(vec3(0.0), color));
let base_shadow_crossover = 0.1;
let base_highlight_crossover = 0.5;
let balance_range = 0.5;
let shadow_crossover = base_shadow_crossover + max(0.0, -balance) * balance_range;
let highlight_crossover = base_highlight_crossover - max(0.0, balance) * balance_range;
let feather = 0.2 * blending;
let final_shadow_crossover = min(shadow_crossover, highlight_crossover - 0.01);
let shadow_mask = 1.0 - smoothstep(final_shadow_crossover - feather, final_shadow_crossover + feather, luma);
let highlight_mask = smoothstep(highlight_crossover - feather, highlight_crossover + feather, luma);
let midtone_mask = max(0.0, 1.0 - shadow_mask - highlight_mask);
var graded_color = color;
let shadow_sat_strength = 0.3;
let shadow_lum_strength = 0.5;
let midtone_sat_strength = 0.6;
let midtone_lum_strength = 0.8;
let highlight_sat_strength = 0.8;
let highlight_lum_strength = 1.0;
if (shadows.saturation > 0.001) { let tint_rgb = hsv_to_rgb(vec3<f32>(shadows.hue, 1.0, 1.0)); graded_color += (tint_rgb - 0.5) * shadows.saturation * shadow_mask * shadow_sat_strength; }
graded_color += shadows.luminance * shadow_mask * shadow_lum_strength;
if (midtones.saturation > 0.001) { let tint_rgb = hsv_to_rgb(vec3<f32>(midtones.hue, 1.0, 1.0)); graded_color += (tint_rgb - 0.5) * midtones.saturation * midtone_mask * midtone_sat_strength; }
graded_color += midtones.luminance * midtone_mask * midtone_lum_strength;
if (highlights.saturation > 0.001) { let tint_rgb = hsv_to_rgb(vec3<f32>(highlights.hue, 1.0, 1.0)); graded_color += (tint_rgb - 0.5) * highlights.saturation * highlight_mask * highlight_sat_strength; }
graded_color += highlights.luminance * highlight_mask * highlight_lum_strength;
return graded_color;
}
fn apply_local_contrast(
processed_color_linear: vec3<f32>,
blurred_color_input_space: vec3<f32>,
amount: f32,
is_raw: u32,
mode: u32
) -> vec3<f32> {
if (amount == 0.0) {
return processed_color_linear;
}
let center_luma = get_luma(processed_color_linear);
let shadow_threshold = select(0.03, 0.1, is_raw == 1u);
let shadow_protection = smoothstep(0.0, shadow_threshold, center_luma);
let highlight_protection = 1.0 - smoothstep(0.9, 1.0, center_luma);
let midtone_mask = shadow_protection * highlight_protection;
if (midtone_mask < 0.001) {
return processed_color_linear;
}
var blurred_color_linear: vec3<f32>;
if (is_raw == 1u) {
blurred_color_linear = blurred_color_input_space;
} else {
blurred_color_linear = srgb_to_linear(blurred_color_input_space);
}
let blurred_luma = get_luma(blurred_color_linear);
let safe_center_luma = max(center_luma, 0.0001);
let safe_blurred_luma = max(blurred_luma, 0.0001);
var final_color: vec3<f32>;
if (amount < 0.0) {
let blurred_color_projected = processed_color_linear * (safe_blurred_luma / safe_center_luma);
var blur_amount = -amount;
if (mode == 0u) {
blur_amount = blur_amount * 0.5;
}
final_color = mix(processed_color_linear, blurred_color_projected, blur_amount);
} else {
let log_ratio = log2(safe_center_luma / safe_blurred_luma);
var effective_amount = amount;
if (mode == 0u) {
let edge_magnitude = abs(log_ratio);
let normalized_edge = clamp(edge_magnitude / 3.0, 0.0, 1.0);
let edge_dampener = 1.0 - pow(normalized_edge, 0.5);
effective_amount = amount * edge_dampener * 0.8;
}
else {
effective_amount = amount;
}
let contrast_factor = exp2(log_ratio * effective_amount);
final_color = processed_color_linear * contrast_factor;
}
return mix(processed_color_linear, final_color, midtone_mask);
}
fn apply_centre_local_contrast(
color_in: vec3<f32>,
centre_amount: f32,
coords_i: vec2<i32>,
blurred_color_srgb: vec3<f32>,
is_raw: u32
) -> vec3<f32> {
if (centre_amount == 0.0) {
return color_in;
}
let full_dims_f = vec2<f32>(textureDimensions(input_texture));
let coord_f = vec2<f32>(coords_i);
let midpoint = 0.4;
let feather = 0.375;
let aspect = full_dims_f.y / full_dims_f.x;
let uv_centered = (coord_f / full_dims_f - 0.5) * 2.0;
let d = length(uv_centered * vec2<f32>(1.0, aspect)) * 0.5;
let vignette_mask = smoothstep(midpoint - feather, midpoint + feather, d);
let centre_mask = 1.0 - vignette_mask;
const CLARITY_SCALE: f32 = 0.9;
var processed_color = color_in;
let clarity_strength = centre_amount * (2.0 * centre_mask - 1.0) * CLARITY_SCALE;
if (abs(clarity_strength) > 0.001) {
processed_color = apply_local_contrast(processed_color, blurred_color_srgb, clarity_strength, is_raw, 1u);
}
return processed_color;
}
fn apply_centre_tonal_and_color(
color_in: vec3<f32>,
centre_amount: f32,
coords_i: vec2<i32>
) -> vec3<f32> {
if (centre_amount == 0.0) {
return color_in;
}
let full_dims_f = vec2<f32>(textureDimensions(input_texture));
let coord_f = vec2<f32>(coords_i);
let midpoint = 0.4;
let feather = 0.375;
let aspect = full_dims_f.y / full_dims_f.x;
let uv_centered = (coord_f / full_dims_f - 0.5) * 2.0;
let d = length(uv_centered * vec2<f32>(1.0, aspect)) * 0.5;
let vignette_mask = smoothstep(midpoint - feather, midpoint + feather, d);
let centre_mask = 1.0 - vignette_mask;
const EXPOSURE_SCALE: f32 = 0.5;
const VIBRANCE_SCALE: f32 = 0.4;
const SATURATION_CENTER_SCALE: f32 = 0.3;
const SATURATION_EDGE_SCALE: f32 = 0.8;
var processed_color = color_in;
let exposure_boost = centre_mask * centre_amount * EXPOSURE_SCALE;
processed_color = apply_filmic_exposure(processed_color, exposure_boost);
let vibrance_center_boost = centre_mask * centre_amount * VIBRANCE_SCALE;
let saturation_center_boost = centre_mask * centre_amount * SATURATION_CENTER_SCALE;
let saturation_edge_effect = -(1.0 - centre_mask) * centre_amount * SATURATION_EDGE_SCALE;
let total_saturation_effect = saturation_center_boost + saturation_edge_effect;
processed_color = apply_creative_color(processed_color, total_saturation_effect, vibrance_center_boost);
return processed_color;
}
fn apply_dehaze(color: vec3<f32>, amount: f32) -> vec3<f32> {
if (amount == 0.0) { return color; }
let atmospheric_light = vec3<f32>(0.95, 0.97, 1.0);
if (amount > 0.0) {
let dark_channel = min(color.r, min(color.g, color.b));
let transmission_estimate = 1.0 - dark_channel;
let t = 1.0 - amount * transmission_estimate;
let recovered = (color - atmospheric_light) / max(t, 0.1) + atmospheric_light;
var result = mix(color, recovered, amount);
result = 0.5 + (result - 0.5) * (1.0 + amount * 0.15);
let luma = get_luma(result);
result = mix(vec3<f32>(luma), result, 1.0 + amount * 0.1);
return result;
} else {
return mix(color, atmospheric_light, abs(amount) * 0.7);
}
}
fn apply_noise_reduction(color: vec3<f32>, coords_i: vec2<i32>, luma_amount: f32, color_amount: f32, scale: f32) -> vec3<f32> {
if (luma_amount <= 100.0 && color_amount <= 100.0) { return color; } // temporarily disable NR for now
let luma_threshold = 0.1 / scale;
let color_threshold = 0.2 / scale;
var accum_color = vec3<f32>(0.0);
var total_weight = 0.0;
let center_luma = get_luma(color);
let max_coords = vec2<i32>(textureDimensions(input_texture) - 1u);
for (var y = -1; y <= 1; y = y + 1) {
for (var x = -1; x <= 1; x = x + 1) {
let offset = vec2<i32>(x, y);
let sample_coords = clamp(coords_i + offset, vec2<i32>(0), max_coords);
let sample_color_linear = srgb_to_linear(textureLoad(input_texture, vec2<u32>(sample_coords), 0).rgb);
var luma_weight = 1.0;
if (luma_amount > 0.0) {
let luma_diff = abs(get_luma(sample_color_linear) - center_luma);
luma_weight = 1.0 - smoothstep(0.0, luma_threshold, luma_diff / luma_amount);
}
var color_weight = 1.0;
if (color_amount > 0.0) {
let color_diff = distance(sample_color_linear, color);
color_weight = 1.0 - smoothstep(0.0, color_threshold, color_diff / color_amount);
}
let weight = luma_weight * color_weight;
accum_color += sample_color_linear * weight;
total_weight += weight;
}
}
if (total_weight > 0.0) { return accum_color / total_weight; }
return color;
}
fn apply_ca_correction(coords: vec2<u32>, ca_rc: f32, ca_by: f32) -> vec3<f32> {
let dims = vec2<f32>(textureDimensions(input_texture));
let center = dims / 2.0;
let current_pos = vec2<f32>(coords);
let to_center = current_pos - center;
let dist = length(to_center);
if (dist == 0.0) {
return textureLoad(input_texture, coords, 0).rgb;
}
let dir = to_center / dist;
let red_shift = dir * dist * ca_rc;
let blue_shift = dir * dist * ca_by;
let red_coords = vec2<i32>(round(current_pos - red_shift));
let blue_coords = vec2<i32>(round(current_pos - blue_shift));
let green_coords = vec2<i32>(current_pos);
let max_coords = vec2<i32>(dims - 1.0);
let r = textureLoad(input_texture, vec2<u32>(clamp(red_coords, vec2<i32>(0), max_coords)), 0).r;
let g = textureLoad(input_texture, vec2<u32>(clamp(green_coords, vec2<i32>(0), max_coords)), 0).g;
let b = textureLoad(input_texture, vec2<u32>(clamp(blue_coords, vec2<i32>(0), max_coords)), 0).b;
return vec3<f32>(r, g, b);
}
const AGX_EPSILON: f32 = 1.0e-6;
const AGX_MIN_EV: f32 = -15.2;
const AGX_MAX_EV: f32 = 5.0;
const AGX_RANGE_EV: f32 = AGX_MAX_EV - AGX_MIN_EV;
const AGX_GAMMA: f32 = 2.4;
const AGX_SLOPE: f32 = 2.3843;
const AGX_TOE_POWER: f32 = 1.5;
const AGX_SHOULDER_POWER: f32 = 1.5;
const AGX_TOE_TRANSITION_X: f32 = 0.6060606;
const AGX_TOE_TRANSITION_Y: f32 = 0.43446;
const AGX_SHOULDER_TRANSITION_X: f32 = 0.6060606;
const AGX_SHOULDER_TRANSITION_Y: f32 = 0.43446;
const AGX_INTERCEPT: f32 = -1.0112;
const AGX_TOE_SCALE: f32 = -1.0359;
const AGX_SHOULDER_SCALE: f32 = 1.3475;
const AGX_TARGET_BLACK_PRE_GAMMA: f32 = 0.0;
const AGX_TARGET_WHITE_PRE_GAMMA: f32 = 1.0;
fn agx_sigmoid(x: f32, power: f32) -> f32 {
return x / pow(1.0 + pow(x, power), 1.0 / power);
}
fn agx_scaled_sigmoid(x: f32, scale: f32, slope: f32, power: f32, transition_x: f32, transition_y: f32) -> f32 {
return scale * agx_sigmoid(slope * (x - transition_x) / scale, power) + transition_y;
}
fn agx_apply_curve_channel(x: f32) -> f32 {
var result: f32 = 0.0;
if (x < AGX_TOE_TRANSITION_X) {
result = agx_scaled_sigmoid(x, AGX_TOE_SCALE, AGX_SLOPE, AGX_TOE_POWER, AGX_TOE_TRANSITION_X, AGX_TOE_TRANSITION_Y);
} else if (x <= AGX_SHOULDER_TRANSITION_X) {
result = AGX_SLOPE * x + AGX_INTERCEPT;
} else {
result = agx_scaled_sigmoid(x, AGX_SHOULDER_SCALE, AGX_SLOPE, AGX_SHOULDER_POWER, AGX_SHOULDER_TRANSITION_X, AGX_SHOULDER_TRANSITION_Y);
}
return clamp(result, AGX_TARGET_BLACK_PRE_GAMMA, AGX_TARGET_WHITE_PRE_GAMMA);
}
fn agx_compress_gamut(c: vec3<f32>) -> vec3<f32> {
let min_c = min(c.r, min(c.g, c.b));
if (min_c < 0.0) {
return c - min_c;
}
return c;
}
fn agx_tonemap(c: vec3<f32>) -> vec3<f32> {
let x_relative = max(c / 0.18, vec3<f32>(AGX_EPSILON));
let log_encoded = (log2(x_relative) - AGX_MIN_EV) / AGX_RANGE_EV;
let mapped = clamp(log_encoded, vec3<f32>(0.0), vec3<f32>(1.0));