-
Notifications
You must be signed in to change notification settings - Fork 157
/
nlmeans.glsl
2723 lines (2442 loc) · 71.5 KB
/
nlmeans.glsl
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
/* vi: ft=c
*
* Based on vf_nlmeans.c from FFmpeg.
*
* Copyright (c) 2022 an3223 <ethanr2048@gmail.com>
* Copyright (c) 2016 Clément Bœsch <u pkh me>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Description: nlmeans.glsl: Default profile, general purpose, tuned for low noise
// The following is shader code injected from ./nlmeans_template
/* vi: ft=c
*
* Based on vf_nlmeans.c from FFmpeg.
*
* Copyright (c) 2022 an3223 <ethanr2048@gmail.com>
* Copyright (c) 2016 Clément Bœsch <u pkh me>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Description: nlmeans.glsl: Default profile, general purpose, tuned for low noise
//!HOOK LUMA
//!HOOK CHROMA
//!BIND HOOKED
//!DESC [nlmeans] (nlmeans.glsl)
//!SAVE G
// User variables
// It is generally preferable to denoise luma and chroma differently, so the
// user variables for luma and chroma are split.
// Denoising factor (sigma, higher means more blur)
#ifdef LUMA_raw
#define S 2.171842300643018
#else
#define S 3.472706940155897
#endif
/* Noise resistant adaptive sharpening
*
* AS:
* - 0: disable
* - 1: sharpen+denoise
* - 2: sharpen only
* ASF: Higher numbers make a sharper image
* ASA: Anti-ringing, higher numbers increase strength
* ASP: Power, lower numbers increase sharpening on lower frequency detail
* ASS: Equivalent to SS but for ASK instead of SK
* ASI:
* - 0: don't sharpen noise
* - 1: sharpen noise
*/
#ifdef LUMA_raw
#define AS 0
#define ASF 0.0
#define ASA 0.0
#define ASP 0.0
#define ASS 0.0
#define ASI 0
#else
#define AS 0
#define ASF 0.0
#define ASA 0.0
#define ASP 0.0
#define ASS 0.0
#define ASI 0
#endif
/* Starting weight
*
* AKA the center weight, the weight of the pixel-of-interest.
*/
#ifdef LUMA_raw
#define SW 0.874631892412684
#else
#define SW 0.3493678781214755
#endif
/* Spatial kernel
*
* Increasing the spatial sigma (SS) reduces the weight of further
* pixels.
*
* The intra-patch variants might help with larger patch sizes.
*
* SST: enables spatial kernel if R>=PST, 0 fully disables
* SS: spatial sigma
* PSS: intra-patch spatial sigma
* PST: enables intra-patch spatial kernel if P>=PST, 0 fully disables
*/
#ifdef LUMA_raw
#define SST 1
#define SS 0.1786363425615082
#define PST 0
#define PSS 0.0
#else
#define SST 1
#define SS 0.3241999209926485
#define PST 0
#define PSS 0.0
#endif
/* Extremes preserve
*
* This setting is dependent on code generation from shader_cfg, so this
* setting can only be enabled via shader_cfg.
*
* Reduce denoising in very bright/dark areas.
*
* The downscaling factor of the EP shader stage affects the size of the area
* checked for luminance.
*
* This is incompatible with RGB. If you have RGB hooks enabled then you will
* have to delete the EP shader stage or specify EP=0 through shader_cfg.
*
* EP: 1 to enable, 0 to disable
* DP: EP strength on dark areas, 0 to fully denoise
* BP: EP strength on bright areas, 0 to fully denoise
*/
#ifdef LUMA_raw
#define EP 0
#define BP 0.0
#define DP 0.0
#else
#define EP 0
#define BP 0.0
#define DP 0.0
#endif
/* ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS */
/* ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS */
/* ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS */
/* ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS */
/* ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS * ADVANCED OPTIONS */
/* textureGather applicable configurations:
*
* - PS={0,3,7,8}:P=3:RI={0,1,3,7}:RFI={0,1,2}
* - PS={0,8}:P=3:RI={0,1,3,7}:RFI={0,1,2}
* - PS=4:P=3:PST=0:RI=0:RFI=0
* - PS=6:RI=0:RFI=0
* - Currently the only scalable variant
*
* Options which always disable textureGather:
* - NG
* - SAMPLE
* - PD
*
* Running without textureGather may be much slower.
*/
/* Patch & research sizes
*
* P should be an odd number. Higher values are slower and not always better.
*
* R should be an odd number greater than or equal to 3. Higher values are
* generally better, but slower.
*/
#ifdef LUMA_raw
#define P 3
#define R 5
#else
#define P 3
#define R 5
#endif
/* Patch and research shapes
*
* Different shapes have different speed and quality characteristics. Every
* shape (besides square) is smaller than square.
*
* PS applies applies to patches, RS applies to research zones.
*
* 0: square (symmetrical)
* 1: horizontal line (asymmetric)
* 2: vertical line (asymmetric)
* 3: diamond (symmetrical)
* 4: triangle (asymmetric, pointing upward)
* 5: truncated triangle (asymmetric on two axis, last row halved)
* 6: even sized square (asymmetric on two axis)
* 7: plus (symmetrical)
* 8: plus X (symmetrical)
*/
#ifdef LUMA_raw
#define RS 3
#define PS 0
#else
#define RS 3
#define PS 3
#endif
/* Weight discard
*
* Reduces weights that fall below a fraction of the average weight. This culls
* the most dissimilar samples from the blur, which can yield a better result,
* especially around edges.
*
* WD:
* - 2: Mean. Better quality, but slower and requires GLSL 4.0 or later
* - 1: Moving cumulative average. Fast but inaccurate, blurs directionally.
* - 0: Disable
*
* WDT: Threshold coefficient, higher numbers discard more
* WDP (only for WD=1): Increasing reduces the threshold for small sample sizes
* WDS (not for WDK=is_zero): Higher numbers are more eager to reduce weights
*/
#ifdef LUMA_raw
#define WD 1
#define WDT 0.2935708849017379
#define WDP 1.2788519721382683
#define WDS 1.0
#else
#define WD 0
#define WDT 0.0
#define WDP 0.0
#define WDS 1.0
#endif
/* Connectivity
*
* Increases weights that are near high weights, decreases weights that are
* near low weights.
*
* C: Number of passes to do, more increases the effect, 0 does nothing
* CD: Distance between each pixel and its furthest neighbor
* CS: Strength of effect, higher is more
*/
#ifdef LUMA_raw
#define C 0
#define CD 1.0
#define CS 1.0
#else
#define C 0
#define CD 1.0
#define CS 1.0
#endif
/* Rotational/reflectional invariance
*
* Number of rotations/reflections to try for each patch comparison. Can be
* slow, but may improve feature preservation. More rotations/reflections gives
* diminishing returns. The most similar rotation/reflection is used.
*
* The angle in degrees of each rotation is 360/(RI+1), so RI=1 will do a
* single 180 degree rotation, RI=3 will do three 90 degree rotations, etc.
*
* Consider setting SAMPLE=1 if setting RI to a setting that would require
* sampling between pixels.
*
* RI: Rotational invariance
* RFI (0 to 2): Reflectional invariance
*/
#ifdef LUMA_raw
#define RI 0
#define RFI 2
#else
#define RI 0
#define RFI 0
#endif
/* Temporal denoising
*
* This setting is dependent on code generation from shader_cfg, so this
* setting can only be enabled via shader_cfg.
*
* Caveats:
* - Slower:
* - Gather optimizations only apply to the current frame
* - Requires vo=gpu-next
* - Luma-only (this is a bug)
* - Buggy
*
* May cause motion blur and may struggle more with noise that persists across
* multiple frames (e.g., from compression or duplicate frames).
*
* Motion estimation (ME) should improve quality without impacting speed.
*
* Increasing temporal distortion (TD) can reduce motion blur.
*
* T: number of frames used
* ME: motion estimation, 0 for none, 1 for max weight, 2 for weighted avg
* MEF: estimate factor, compensates for ME being one frame behind
* TRF: compare against the denoised frames
* TD: temporal distortion, higher numbers give less weight to previous frames
*/
#ifdef LUMA_raw
#define T 0
#define ME 1
#define MEF 2
#define TRF 0
#define TD 1.0
#else
#define T 0
#define ME 0
#define MEF 2
#define TRF 0
#define TD 1.0
#endif
/* Kernels
*
* SK: spatial kernel
* RK: range kernel (takes patch differences)
* ASK: adaptive sharpening kernel
* PSK: intra-patch spatial kernel
* WDK: weight discard kernel
* WD1TK (WD=1 only): weight discard tolerance kernel
*
* List of available kernels:
*
* bicubic
* cosine
* cosine_
* ffexp
* gaussian
* ginseng
* ginseng_ (unclamped)
* jinc
* jinc3
* jinc_ (unclamped)
* jincjinc
* jincjinc3
* jincjinc3_ (unclamped)
* jincjinc_ (unclamped)
* lanczos
* quadratic
* quadratic_ (unclamped)
* sinc
* sinc3
* sinc_ (unclamped)
* sphinx
* sphinx_ (unclamped)
* triangle
* triangle_ (unclamped)
*/
#ifdef LUMA_raw
#define SK gaussian
#define RK gaussian
#define ASK sinc
#define ASAK gaussian
#define PSK gaussian
#define WDK is_zero
#define WD1TK gaussian
#else
#define SK gaussian
#define RK gaussian
#define ASK sphinx_
#define ASAK gaussian
#define PSK gaussian
#define WDK is_zero
#define WD1TK gaussian
#endif
/* Kernel parameters
*
* The following kernels take these parameters:
*
* ffexp: K0
* ginseng: KWS
* jincjinc3: KWS
* jincjinc: KWS
* lanczos: KWS
*/
#ifdef LUMA_raw
#define K0 1.0
#define K1 1.0
#define KWS 1.0
#else
#define K0 1.0
#define K1 1.0
#define KWS 1.0
#endif
/* Negative kernel parameter offsets
*
* Usually kernels go high -> low. These parameters allow for a kernel to go
* low -> high -> low.
*
* Values of 0.0 mean no effect, higher values increase the effect.
*
* RO: range kernel (takes patch differences)
*/
#ifdef LUMA_raw
#define RO 6.373764517041128e-05
#else
#define RO 0.0
#endif
/* Sampling method
*
* In most cases this shouldn't make any difference, only set to bilinear if
* it's necessary to sample between pixels (e.g., RI=2).
*
* 0: nearest neighbor
* 1: bilinear
*/
#ifdef LUMA_raw
#define SAMPLE 0
#else
#define SAMPLE 0
#endif
/* Research scaling factor
*
* Higher numbers sample more sparsely as the distance from the POI grows.
*/
#ifdef LUMA_raw
#define RSF 0.0
#else
#define RSF 0.0
#endif
// Scaling factor (should match WIDTH/HEIGHT)
#ifdef LUMA_raw
#define SF 1
#else
#define SF 1
#endif
// Use the guide image as the input image
#ifdef LUMA_raw
#define GI 0
#else
#define GI 0
#endif
/* Visualization
*
* 0: off
* 1: absolute difference between input/output to the power of 0.25
* 2: difference between input/output centered on 0.5
* 3: post-WD average weight map
* 4: pre-WD average weight map
* 5: unsharp mask
* 6: EP
* 7: celled weight map (incompatible with temporal)
*/
#ifdef LUMA_raw
#define V 0
#else
#define V 0
#endif
// Fast approximate division
#ifdef LUMA_raw
#define FAST_DIV 0
#else
#define FAST_DIV 0
#endif
// Force disable textureGather
#ifdef LUMA_raw
#define NG 0
#else
#define NG 0
#endif
// Patch donut (probably useless)
#ifdef LUMA_raw
#define PD 0
#else
#define PD 0
#endif
// Duplicate 1st weight (for luma-guided-chroma)
#ifdef LUMA_raw
#define D1W 0
#else
#define D1W 0
#endif
// Skip patch comparison
#ifdef LUMA_raw
#define SKIP_PATCH 0
#else
#define SKIP_PATCH 0
#endif
// Shader code
#define EPSILON 1.2e-38
#define FLT_EPSILON 1.19209290E-07
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.78539816339744830962
#define M_1_PI 0.31830988618379067154
#define M_2_PI 0.63661977236758134308
#define POW2(x) ((x)*(x))
#define POW3(x) ((x)*(x)*(x))
#define POW4(x) ((x)*(x)*(x)*(x))
#define POW5(x) ((x)*(x)*(x)*(x)*(x))
#define POW6(x) ((x)*(x)*(x)*(x)*(x)*(x))
#define POW7(x) ((x)*(x)*(x)*(x)*(x)*(x)*(x))
// pow() implementation that gives -pow() when x<0
// avoids actually calling pow() since apparently it's buggy on nvidia
#define POW(x,y) (exp(log(abs(x)) * y) * sign(x))
// boolean logic w/ vectors
// from hdeband
#define NOT(x) (1 - (x))
#define AND *
#define TERNARY(cond, x, y) ((x)*(cond) + (y)*NOT(cond))
// from FSR
#if FAST_DIV
#define RECIPROCAL(x) uintBitsToFloat(uint(0x7ef07ebb) - floatBitsToUint(x))
#define DIV(x,y) ((x) * RECIPROCAL(y))
#define MED_RCP_B(x) uintBitsToFloat(uint(0x7ef19fff) - floatBitsToUint(x))
#define MED_RECIPROCAL(x) (MED_RCP_B(x) * (-MED_RCP_B(x) * x + 2))
#define MED_DIV(x,y) ((x) * MED_RECIPROCAL(y))
#else
#define RECIPROCAL(x) (1.0/(x))
#define MED_RECIPROCAL(x) (1.0/(x))
#define DIV(x,y) ((x)/(y))
#define MED_DIV(x,y) ((x)/(y))
#endif
// adapted from https://github.com/garamond13/Jinc
#define j1(x) TERNARY(step((x), 2.2931157), ((x) * 0.5) - (POW3(x) * 0.0625) + (POW5(x) * 0.00260416666666666666) - (POW7(x) * 0.00005425347222222222), sqrt(DIV(M_2_PI, (x))) * (1.0 + DIV(0.1875, POW2(x)) - DIV(0.1933594, POW4(x))) * cos((x) - 3.0 * M_PI_4 + DIV(0.375, (x)) - DIV(0.1640625, POW3(x))))
// XXX make kernel params capable of being set per-kernel, e.g., RK0, SK0...
// kernels, most of these are from libplacebo
// XXX sinc/jinc/sphinx: 1e-3 was selected tentatively; not sure what the correct value should be (1e-8 is too low, x is never considered to be lower than it for some reason)
#define bicubic(x) bicubic_(clamp(x, 0.0, 2.0))
#define bicubic_(x) ((1.0/6.0) * (POW3((x)+2) - 4 * POW3((x)+1) + 6 * POW3(x) - 4 * POW3(max((x)-1, 0))))
#define cosine(x) cos(clamp(x, 0, M_PI_2))
#define cosine_ cos
#define ffexp(x) (POW(cos(max(EPSILON, clamp(x, 0.0, 1.0) * M_PI)), K0) * 0.5 + 0.5) // "experimental" scaler from ffmpeg
#define gaussian(x) exp(-1 * POW2(x))
#define ginseng(x) ginseng_(clamp(x, 0.0, 3.0))
#define ginseng_(x) (jinc(x) * sinc(x*KWS))
#define is_zero(x) int((x) == 0)
#define jinc(x) jinc_(clamp(x, 0.0, 1.2196698912665045))
#define jinc3(x) jinc_(clamp(x, 0.0, 3.2383154841662362))
#define jinc_(x) TERNARY(step(x, 1e-3), 1.0, DIV(2 * j1((x)*M_PI), (x)*M_PI))
#define jincjinc(x) jincjinc_(clamp(x, 0.0, 3.2383154841662362))
#define jincjinc3(x) jincjinc3_(clamp(x, 0.0, 3.2383154841662362))
#define jincjinc3_(x) (jinc(x) * jinc3(x*KWS))
#define jincjinc_(x) (jinc(x) * jinc(x*KWS))
#define lanczos(x) lanczos_(clamp(x, 0.0, 3.0))
#define lanczos_(x) (sinc3(x) * sinc(x*KWS))
#define quadratic(x) quadratic_(clamp(x, 0.0, 1.5))
#define quadratic_(x) TERNARY(step(x, 0.5), 0.75 - POW2(x), 0.5 * POW2((x) - 1.5))
#define sinc(x) sinc_(clamp(x, 0.0, 1.0))
#define sinc3(x) sinc_(clamp(x, 0.0, 3.0))
#define sinc_(x) TERNARY(step(x, 1e-3), 1.0, DIV(sin((x)*M_PI), ((x)*M_PI)))
#define sphinx(x) sphinx_(clamp(x, 0.0, 1.4302966531242027))
#define sphinx_(x) TERNARY(step(x, 1e-3), 1.0, DIV(3.0 * (sin((x)*M_PI) - (x)*M_PI * cos((x)*M_PI)), POW3((x)*M_PI)))
#define triangle(x) triangle_(clamp(x, 0.0, 1.0))
#define triangle_(x) (1 - (x))
#define MAP1(f,param) f(param)
#define MAP2(f,param) vec2(f(param.x), f(param.y))
#define MAP3(f,param) vec3(f(param.x), f(param.y), f(param.z))
#if defined(LUMA_raw)
#define val float
#define val_swizz(v) (v.x)
#define unval(v) vec4(v, 0, 0, poi_.a)
#define val_packed val
#define val_pack(v) (v)
#define val_unpack(v) (v)
#define MAP MAP1
#elif defined(CHROMA_raw)
#define val vec2
#define val_swizz(v) (v.xy)
#define unval(v) vec4(v.x, v.y, 0, poi_.a)
#define val_packed uint
#define val_pack(v) packUnorm2x16(v)
#define val_unpack(v) unpackUnorm2x16(v)
#define MAP MAP2
#else
#define val vec3
#define val_swizz(v) (v.xyz)
#define unval(v) vec4(v.x, v.y, v.z, poi_.a)
#define val_packed val
#define val_pack(v) (v)
#define val_unpack(v) (v)
#define MAP MAP3
#endif
#if D1W // float guide
#define val_guide float
#define val_guide_swizz(v) (v.x)
#define val_guide_packed float
#define val_guide_pack(v) (v)
#define val_guide_unpack(v) (v)
#define MAP_GUIDE MAP1
#define unval_guide(v) vec4(v, 0, 0, poi2_.a)
#else // val guide
#define val_guide val
#define val_guide_swizz val_swizz
#define val_guide_packed val_packed
#define val_guide_pack val_pack
#define val_guide_unpack val_unpack
#define MAP_GUIDE MAP
#if defined(LUMA_raw)
#define unval_guide(v) vec4(v, 0, 0, poi2_.a)
#elif defined(CHROMA_raw)
#define unval_guide(v) vec4(v.x, v.y, 0, poi2_.a)
#else
#define unval_guide(v) vec4(v.x, v.y, v.z, poi2_.a)
#endif
#endif
#if PS == 6
const int hp = P/2;
#else
const float hp = int(P/2) - 0.5*(1-(P%2)); // sample between pixels for even patch sizes
#endif
#if RS == 6
const int hr = R/2;
#else
const float hr = int(R/2) - 0.5*(1-(R%2)); // sample between pixels for even research sizes
#endif
// patch/research shapes
// each shape is depicted in a comment, where Z=5 (Z corresponds to P or R)
// dots (.) represent samples (pixels) and X represents the pixel-of-interest
// Z .....
// Z .....
// Z ..X..
// Z .....
// Z .....
#define S_SQUARE(z,hz,incr) for (z.x = -hz; z.x <= hz; z.x++) for (z.y = -hz; z.y <= hz; incr)
// (in this instance Z=4)
// Z ....
// Z ....
// Z ..X.
// Z ....
#define S_SQUARE_EVEN(z,hz,incr) for (z.x = -hz; z.x < hz; z.x++) for (z.y = -hz; z.y < hz; incr)
// Z-4 .
// Z-2 ...
// Z ..X..
#define S_TRIANGLE(z,hz,incr) for (z.y = -hz; z.y <= 0; z.y++) for (z.x = -abs(abs(z.y) - hz); z.x <= abs(abs(z.y) - hz); incr)
// Z-4 .
// Z-2 ...
// hz+1 ..X
#define S_TRUNC_TRIANGLE(z,hz,incr) for (z.y = -hz; z.y <= 0; z.y++) for (z.x = -abs(abs(z.y) - hz); z.x <= abs(abs(z.y) - hz)*int(z.y!=0); incr)
#define S_TRIANGLE_A(hz,Z) int(hz*hz+Z)
// Z-4 .
// Z-2 ...
// Z ..X..
// Z-2 ...
// Z-4 .
#define S_DIAMOND(z,hz,incr) for (z.x = -hz; z.x <= hz; z.x++) for (z.y = -abs(abs(z.x) - hz); z.y <= abs(abs(z.x) - hz); incr)
#define S_DIAMOND_A(hz,Z) int(hz*hz*2+Z)
//
// Z ..X..
//
#define S_HORIZONTAL(z,hz,incr) for (z.y = 0; z.y <= 0; z.y++) for (z.x = -hz; z.x <= hz; incr)
// 90 degree rotation of S_HORIZONTAL
#define S_VERTICAL(z,hz,incr) for (z.x = 0; z.x <= 0; z.x++) for (z.y = -hz; z.y <= hz; incr)
// 1 .
// 1 .
// Z ..X..
// 1 .
// 1 .
#define S_PLUS(z,hz,incr) for (z.x = -hz; z.x <= hz; z.x++) for (z.y = -hz * int(z.x == 0); z.y <= hz * int(z.x == 0); incr)
#define S_PLUS_A(hz,Z) (Z*2 - 1)
// 3 . . .
// 3 ...
// Z ..X..
// 3 ...
// 3 . . .
#define S_PLUS_X(z,hz,incr) for (z.x = -hz; z.x <= hz; z.x++) for (z.y = -abs(z.x) + -hz * int(z.x == 0); z.y <= abs(z.x) + hz * int(z.x == 0); incr)
#define S_PLUS_X_A(hz,Z) (Z*4 - 3)
// 1x1 square
#define S_1X1(z) for (z = vec3(0); z.x <= 0; z.x++)
#define T1 (T+1)
#define FOR_FRAME(r) for (r.z = 0; r.z < T1; r.z++)
// donut increment, increments without landing on (0,0,0)
// much faster than a continue statement
#define DINCR(z,c,a) ((z.c += a),(z.c += int(z == vec3(0))))
#define R_AREA(a) (a * T1 - 1)
// research shapes
// XXX would be nice to have the option of temporally-varying research sizes
#if R == 0 || R == 1
#define FOR_RESEARCH(r) S_1X1(r)
const int r_area = R_AREA(1);
#elif RS == 8
#define FOR_RESEARCH(r) S_PLUS_X(r,hr,DINCR(r,y,max(1,abs(r.x))))
const int r_area = R_AREA(S_PLUS_X_A(hr,R));
#elif RS == 7
#define FOR_RESEARCH(r) S_PLUS(r,hr,DINCR(r,y,1))
const int r_area = R_AREA(S_PLUS_A(hr,R));
#elif RS == 6
#define FOR_RESEARCH(r) S_SQUARE_EVEN(r,hr,DINCR(r,y,1))
const int r_area = R_AREA(R*R);
#elif RS == 5
#define FOR_RESEARCH(r) S_TRUNC_TRIANGLE(r,hr,DINCR(r,x,1))
const int r_area = R_AREA(S_TRIANGLE_A(hr,hr));
#elif RS == 4
#define FOR_RESEARCH(r) S_TRIANGLE(r,hr,DINCR(r,x,1))
const int r_area = R_AREA(S_TRIANGLE_A(hr,R));
#elif RS == 3
#define FOR_RESEARCH(r) S_DIAMOND(r,hr,DINCR(r,y,1))
const int r_area = R_AREA(S_DIAMOND_A(hr,R));
#elif RS == 2
#define FOR_RESEARCH(r) S_VERTICAL(r,hr,DINCR(r,y,1))
const int r_area = R_AREA(R);
#elif RS == 1
#define FOR_RESEARCH(r) S_HORIZONTAL(r,hr,DINCR(r,x,1))
const int r_area = R_AREA(R);
#elif RS == 0
#define FOR_RESEARCH(r) S_SQUARE(r,hr,DINCR(r,y,1))
const int r_area = R_AREA(R*R);
#endif
#define RI1 (RI+1)
#define RFI1 (RFI+1)
#if RI
#define FOR_ROTATION for (float ri = 0; ri < 360; ri += DIV(360.0, RI1))
#else
#define FOR_ROTATION
#endif
#if RFI
#define FOR_REFLECTION for (int rfi = 0; rfi < RFI1; rfi++)
#else
#define FOR_REFLECTION
#endif
#if PD
#define PINCR DINCR
#else
#define PINCR(z,c,a) (z.c += a)
#endif
#define P_AREA(a) (a - PD)
// patch shapes
#if P == 0 || P == 1
#define FOR_PATCH(p) S_1X1(p)
const int p_area = P_AREA(1);
#elif PS == 8
#define FOR_PATCH(p) S_PLUS_X(p,hp,PINCR(p,y,max(1,abs(p.x))))
const int p_area = P_AREA(S_PLUS_X_A(hp,P));
#elif PS == 7
#define FOR_PATCH(p) S_PLUS(p,hp,PINCR(p,y,1))
const int p_area = P_AREA(S_PLUS_A(hp,P));
#elif PS == 6
#define FOR_PATCH(p) S_SQUARE_EVEN(p,hp,PINCR(p,y,1))
const int p_area = P_AREA(P*P);
#elif PS == 5
#define FOR_PATCH(p) S_TRUNC_TRIANGLE(p,hp,PINCR(p,x,1))
const int p_area = P_AREA(S_TRIANGLE_A(hp,hp));
#elif PS == 4
#define FOR_PATCH(p) S_TRIANGLE(p,hp,PINCR(p,x,1))
const int p_area = P_AREA(S_TRIANGLE_A(hp,P));
#elif PS == 3
#define FOR_PATCH(p) S_DIAMOND(p,hp,PINCR(p,y,1))
const int p_area = P_AREA(S_DIAMOND_A(hp,P));
#elif PS == 2
#define FOR_PATCH(p) S_VERTICAL(p,hp,PINCR(p,y,1))
const int p_area = P_AREA(P);
#elif PS == 1
#define FOR_PATCH(p) S_HORIZONTAL(p,hp,PINCR(p,x,1))
const int p_area = P_AREA(P);
#elif PS == 0
#define FOR_PATCH(p) S_SQUARE(p,hp,PINCR(p,y,1))
const int p_area = P_AREA(P*P);
#endif
const float r_scale = 1.0/r_area;
const float r1_scale = 1.0/(r_area+1);
const float p_scale = 1.0/p_area;
const float hr_scale = 1.0/hr;
#if SAMPLE == 0
#define sample(tex, pos, size, pt, off) tex((pos) + (pt) * (vec2(off) + 0.5 - fract((pos) * (size))))
#else
#define sample(tex, pos, size, pt, off) tex((pos) + (pt) * vec2(off))
#endif
/* Guide images are bound through the G (luma) GC (chroma, or non-luma)
* textures. When bound, these are where the weights are computed. Otherwise
* the weights are computed on the hooked texture.
*/
#if GI && defined(LUMA_raw)
#define GET_(off) sample(G_tex, G_pos, G_size, G_pt, off)
#elif GI
#define GET_(off) sample(GC_tex, GC_pos, GC_size, GC_pt, off)
#else
#define GET_(off) sample(HOOKED_tex, HOOKED_pos, HOOKED_size, HOOKED_pt, off)
#endif
#if defined(G_raw) && (defined(LUMA_raw) || D1W)
#define GET_GUIDE_(off) sample(G_tex, G_pos, G_size, G_pt, off)
#define gather_offs(off, off_arr) (G_mul * vec4(textureGatherOffsets(G_raw, G_pos + vec2(off) * G_pt, off_arr)))
#define gather(off) G_gather(G_pos + (off) * G_pt, 0)
#elif defined(GC_raw)
#define GET_GUIDE_(off) sample(GC_tex, GC_pos, GC_size, GC_pt, off)
#else
#define GET_GUIDE_(off) GET_(off)
#define gather_offs(off, off_arr) (HOOKED_mul * vec4(textureGatherOffsets(HOOKED_raw, HOOKED_pos + vec2(off) * HOOKED_pt, off_arr)))
#define gather(off) HOOKED_gather(HOOKED_pos + (off)*HOOKED_pt, 0)
#endif
#if T
val GET(vec3 off)
{
switch (min(int(off.z), frame)) {
case 0: return val_swizz(GET_(off));
}
}
val GET_GUIDE(vec3 off)
{
return off.z == 0 ? val_guide_swizz(GET_GUIDE_(off)) : GET(off);
}
#else
#define GET(off) val_swizz(GET_(off))
#define GET_GUIDE(off) val_guide_swizz(GET_GUIDE_(off))
#endif
vec4 poi_ = GET_(vec3(0));
vec4 poi2_ = GET_GUIDE_(vec3(0));
val poi = val_swizz(poi_); // pixel-of-interest
val_guide poi2 = val_guide_swizz(poi2_); // guide pixel-of-interest
#if RI // rotation
vec2 rot(vec2 p, float d)
{
return vec2(
p.x * cos(radians(d)) - p.y * sin(radians(d)),
p.y * sin(radians(d)) + p.x * cos(radians(d))
);
}
#else
#define rot(p, d) (p)
#endif
#if RFI // reflection
vec2 ref(vec2 p, int d)
{
switch (d) {
case 0: return p;
case 1: return p * vec2(1, -1);
case 2: return p * vec2(-1, 1);
}
}
#else
#define ref(p, d) (p)
#endif
#if SST && R >= SST
float spatial_r(vec3 v)
{
v.xy += 0.5 - fract(HOOKED_pos*HOOKED_size);
v.z *= TD;
return SK(length(v)*SS);
}
#else
#define spatial_r(v) (1)
#endif
// 2D blur for sharpening
#if AS
float spatial_as(vec3 v)
{
v.xy += 0.5 - fract(HOOKED_pos*HOOKED_size);
return ASK(length(v)*ASS) * int(v.z == 0);
}
#endif
#if PST && P >= PST
#define spatial_p(v) PSK(length(v)*PSS)
#define normalize_p(x,expr) DIV((x), (expr))
#else
#define spatial_p(v) (1)
#define normalize_p(x,expr) ((x) * p_scale)
#endif
const float pdiff_scale = 1.0/max(EPSILON,POW2(S*0.013));
val_guide range(val_guide pdiff_sq)
{
pdiff_sq = sqrt(abs(pdiff_sq - max(EPSILON, RO)) * pdiff_scale);
return RK(pdiff_sq);
}
#define GATHER (PD == 0 && NG == 0 && SAMPLE == 0) // never textureGather if any of these conditions are false
#define REGULAR_ROTATIONS (RI == 0 || RI == 1 || RI == 3 || RI == 7)
#if (defined(LUMA_gather) || D1W) && ((PS == 0 || ((PS == 3 || PS == 7) && RI != 7) || PS == 8) && P == 3) && REGULAR_ROTATIONS && GATHER
// 3x3 diamond/plus or square patch_comparison_gather
const ivec2 offsets_adj[4] = { ivec2(0,-1), ivec2(1,0), ivec2(0,1), ivec2(-1,0) };
const ivec2 offsets_adj_sf[4] = { ivec2(0,-1) * SF, ivec2(1,0) * SF, ivec2(0,1) * SF, ivec2(-1,0) * SF };
vec4 poi_patch_adj = gather_offs(0, offsets_adj);
#if PS == 0 || PS == 8
const ivec2 offsets_diag[4] = { ivec2(-1,-1), ivec2(1,-1), ivec2(1,1), ivec2(-1,1) };
const ivec2 offsets_diag_sf[4] = { ivec2(-1,-1) * SF, ivec2(1,-1) * SF, ivec2(1,1) * SF, ivec2(-1,1) * SF };
vec4 poi_patch_diag = gather_offs(0, offsets_diag);
#endif
float patch_comparison_gather(vec3 r)
{
float min_rot = p_area - 1;
vec4 transformer_adj = gather_offs(r, offsets_adj_sf);
#if PS == 0 || PS == 8
vec4 transformer_diag = gather_offs(r, offsets_diag_sf);
#endif
FOR_ROTATION {
FOR_REFLECTION {
#if RFI
/* xxy
* w y
* wzz
*/
switch(rfi) {
case 1:
transformer_adj = transformer_adj.zyxw;
#if PS == 0 || PS == 8
transformer_diag = transformer_diag.zyxw;
#endif
break;
case 2:
transformer_adj = transformer_adj.xwzy;
#if PS == 0 || PS == 8
transformer_diag = transformer_diag.xwzy;
#endif
break;
}
#endif
vec4 pdiff_sq = POW2(poi_patch_adj - transformer_adj) * spatial_p(vec2(1,0));
#if PS == 0 || PS == 8
pdiff_sq += POW2(poi_patch_diag - transformer_diag) * spatial_p(vec2(1,1));
#endif
min_rot = min(dot(pdiff_sq, vec4(1)), min_rot);
// un-reflect
#if RFI
switch(rfi) {
case 1:
transformer_adj = transformer_adj.zyxw;
#if PS == 0 || PS == 8
transformer_diag = transformer_diag.zyxw;
#endif
break;
case 2:
transformer_adj = transformer_adj.xwzy;
#if PS == 0 || PS == 8
transformer_diag = transformer_diag.xwzy;
#endif
break;
}
#endif
} // FOR_REFLECTION
#if RI == 7
transformer_adj = transformer_adj.wxyz;
// swap adjacents for diagonals
transformer_adj += transformer_diag;