-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathNGLighting-Shader.fxh
1126 lines (904 loc) · 36.8 KB
/
NGLighting-Shader.fxh
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
//Stochastic Screen Space Ray Tracing
//Written by MJ_Ehsan for Reshade
//Version 1.0.0
//Thanks Lord of Lunacy, Leftfarian, and other devs for helping me. <3
//Thanks Alea & MassiHancer for testing. <3
//Credits:
//Thanks Crosire for ReShade.
//https://reshade.me/
//Thanks Jakob for DRME.
//https://github.com/JakobPCoder/ReshadeMotionEstimation
//I learnt a lot from qUINT_SSR. Thanks Pascal Gilcher.
//https://github.com/martymcmodding/qUINT
//Also a lot from DH_RTGI. Thanks Demien Hambert.
//https://github.com/AlucardDH/dh-reshade-shaders
//Thanks Radegast for Unity Sponza Test Scene.
//https://mega.nz/#!qVwGhYwT!rEwOWergoVOCAoCP3jbKKiuWlRLuHo9bf1mInc9dDGE
//Thanks Timothy Lottes and AMD for the Tonemapper and the Inverse Tonemapper.
//https://gpuopen.com/learn/optimized-reversible-tonemapper-for-resolve/
//Thanks Eric Reinhard for the Luminance Tonemapper and the Inverse.
//https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf
//Thanks sujay for the noise function. Ported from ShaderToy.
//https://www.shadertoy.com/view/lldBRn
//////////////////////////////////////////
//TO DO
//1- [v]Add another spatial filtering pass
//2- [ ]Add Hybrid GI/Reflection
//3- [v]Add Simple Mode UI with setup assist
//4- [ ]Add internal comaptibility with Volumetric Fog V1 and V2
// By using the background texture provided by VFog to blend the Reflection.
// Then Blending back the fog to the image. This way fog affects the reflection.
// But the reflection doesn't break the fog.
//5- [x]Add ACEScg and or Filmic inverse tonemapping as optional alternatives to Timothy Lottes
//6- [v]Add AO support
//7- [v]Add second temporal pass after second spatial pass.
//8- [x]Add Spatiotemporal upscaling. have to either add jitter to the RayMarching pass or a checkerboard pattern.
//9- [v]Add Smooth Normals.
//10-[v]Use pre-calulated blue noise instead of white. From Nvidia's SpatioTemporal Blue Noise sequence
//11-[v]Add depth awareness to smooth normals. To do so, add depth in the alpha channel of
// NormTex and NormTex1 for optimization.
//12-[v]Make normal based edge awareness of all passes based on angular distance of the 2 normals.
//13-[ ]Make sample distance of smooth normals exponential.
//14-[ ]
///////////////Include/////////////////////
#include "ReShadeUI.fxh"
#include "ReShade.fxh"
#include "NGLightingUI.fxh"
uniform float Timer < source = "timer"; >;
uniform float Frame < source = "framecount"; >;
static const float2 pix = float2(BUFFER_RCP_WIDTH, BUFFER_RCP_HEIGHT);
#define LDepth ReShade::GetLinearizedDepth
#define FAR_PLANE RESHADE_DEPTH_LINEARIZATION_FAR_PLANE
#ifndef MV_METHOD
#define MV_METHOD 0
#endif
#define PI 3.1415927
static const float PI2div360 = 0.01745329;
#define rad(x) (x * PI2div360)
///////////////Include/////////////////////
///////////////PreProcessor-Definitions////
#include "NGLighting-Configs.fxh"
///////////////PreProcessor-Definitions////
///////////////Textures-Samplers///////////
texture TexColor : COLOR;
sampler sTexColor {Texture = TexColor; SRGBTexture = false;};
texture TexDepth : DEPTH;
sampler sTexDepth {Texture = TexDepth;};
#if MV_METHOD==0
texture MotVectTexVort { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RG16F; };
sampler sMotVectTexVort { Texture = MotVectTexVort; };
float2 sampleMotion(float2 texcoord){return tex2D(sMotVectTexVort, texcoord).rg;}
#endif
#if MV_METHOD==1
namespace Deferred
{
texture MotionVectorsTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RG16F; };
sampler sMotionVectorsTex { Texture = MotionVectorsTex; };
}
float2 sampleMotion(float2 texcoord){return tex2D(Deferred::sMotionVectorsTex, texcoord).rg;}
#endif
#if MV_METHOD==2
texture texMotionVectors { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RG16F; };
sampler SamplerMotionVectors { Texture = texMotionVectors; };
float2 sampleMotion(float2 texcoord){return tex2D(SamplerMotionVectors, texcoord).rg;}
#endif
texture SSSR_ReflectionTex { Width = BUFFER_WIDTH*RESOLUTION_SCALE_; Height = BUFFER_HEIGHT*RESOLUTION_SCALE_; Format = RGBA16f; MipLevels = 4; };
sampler sSSSR_ReflectionTex { Texture = SSSR_ReflectionTex; };
texture SSSR_HitDistTex { Width = BUFFER_WIDTH*RESOLUTION_SCALE_; Height = BUFFER_HEIGHT*RESOLUTION_SCALE_; Format = R16f; MipLevels = 7; };
sampler sSSSR_HitDistTex { Texture = SSSR_HitDistTex; };
texture SSSR_POGColTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16f; };
sampler sSSSR_POGColTex { Texture = SSSR_POGColTex; };
texture SSSR_FilterTex0 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex0 { Texture = SSSR_FilterTex0; };
texture SSSR_FilterTex1 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex1 { Texture = SSSR_FilterTex1; };
texture SSSR_FilterTex2 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex2 { Texture = SSSR_FilterTex2; };
texture SSSR_FilterTex3 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex3 { Texture = SSSR_FilterTex3; };
texture SSSR_PNormalTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_PNormalTex { Texture = SSSR_PNormalTex; };
texture SSSR_NormTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_NormTex { Texture = SSSR_NormTex; };
#if SMOOTH_NORMALS > 0
texture SSSR_NormTex1 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_NormTex1 { Texture = SSSR_NormTex1; };
#endif
#if __RENDERER__ >= 0xa000 // If DX10 or higher
#define RES_M 0.5
texture SSSR_LowResDepthTex { Width = BUFFER_WIDTH*RESOLUTION_SCALE_*RES_M; Height = BUFFER_HEIGHT*RESOLUTION_SCALE_*RES_M; Format = R16f; };
sampler sSSSR_LowResDepthTex { Texture = SSSR_LowResDepthTex; };
texture SSSR_LowResNormTex { Width = BUFFER_WIDTH*RESOLUTION_SCALE_*RES_M; Height = BUFFER_HEIGHT*RESOLUTION_SCALE_*RES_M; Format = RGBA16f; };
sampler sSSSR_LowResNormTex { Texture = SSSR_LowResNormTex; };
#endif //DX9
texture SSSR_HLTex0 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16f; };
sampler sSSSR_HLTex0 { Texture = SSSR_HLTex0; };
texture SSSR_HLTex1 { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R16f; };
sampler sSSSR_HLTex1 { Texture = SSSR_HLTex1; };
texture SSSR_RoughTex { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = R8; };
sampler sSSSR_RoughTex { Texture = SSSR_RoughTex; };
#if NGL_HYBRID_MODE
texture SSSR_ReflectionTexD { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_ReflectionTexD { Texture = SSSR_ReflectionTexD; };
texture SSSR_FilterTex0D { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex0D { Texture = SSSR_FilterTex0D; };
texture SSSR_FilterTex1D { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex1D { Texture = SSSR_FilterTex1D; };
texture SSSR_FilterTex2D { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex2D { Texture = SSSR_FilterTex2D; };
texture SSSR_FilterTex3D { Width = BUFFER_WIDTH; Height = BUFFER_HEIGHT; Format = RGBA16f; };
sampler sSSSR_FilterTex3D { Texture = SSSR_FilterTex3D; };
#endif //NGL_HYBRID_MODE
///////////////Textures-Samplers///////////
///////////////UI//////////////////////////
///////////////UI//////////////////////////
///////////////Vertex Shader///////////////
///////////////Vertex Shader///////////////
///////////////Functions///////////////////
//from: https://www.shadertoy.com/view/XsSfzV
// by Nikos Papadopoulos, 4rknova / 2015
// Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
float3 toYCC(float3 rgb)
{
float Y = .299 * rgb.x + .587 * rgb.y + .114 * rgb.z; // Luminance
float Cb = -.169 * rgb.x - .331 * rgb.y + .500 * rgb.z; // Chrominance Blue
float Cr = .500 * rgb.x - .419 * rgb.y - .081 * rgb.z; // Chrominance Red
return float3(Y,Cb + 128./255.,Cr + 128./255.);
}
float3 toRGB(float3 ycc)
{
float3 c = ycc - float3(0., 128./255., 128./255.);
float R = c.x + 1.400 * c.z;
float G = c.x - 0.343 * c.y - 0.711 * c.z;
float B = c.x + 1.765 * c.y;
return float3(R,G,B);
}
float GetSpecularDominantFactor(float NoV, float roughness)
{
float a = 0.298475 * log(39.4115 - 39.0029 * roughness);
float f = pow(saturate(1.0 - NoV), 10.8649)*(1.0 - a) + a;
return saturate(f);
}
float GetHLDivion(in float HL){return HL*HLDIV;}
float2 GetPixelSize()
{
float2 DepthSize = tex2Dsize(sTexDepth) / BUFFER_SCREEN_SIZE;
float2 ColorSize = rcp(RESOLUTION_SCALE_);
float2 MinResRcp = max(ColorSize, DepthSize);
return MinResRcp;
}
float2 GetPixelSizeWithMip(in float mip)
{
float2 DepthSize = tex2Dsize(sTexDepth) / BUFFER_SCREEN_SIZE;
float2 ColorSize = rcp(RESOLUTION_SCALE_);
ColorSize /= exp2(mip);
float2 MinResRcp = max(ColorSize, DepthSize);
return MinResRcp;
}
float checker(float4 vpos)
{
if(Frame%2)vpos.y++;
return (vpos.y+vpos.x%2)%2;
}
float checker(float2 uv)
{
uv *= BUFFER_SCREEN_SIZE;
return checker(uv.xyxy);
}
float WN(float2 co)
{
return frac(sin(dot(co.xy ,float2(1.0,73))) * 437580.5453);
}
float3 WN3dts(float2 co, float HL)
{
co += (Frame%HL)/120.3476687;
//co += s/16.3542625435332254;
return float3( WN(co), WN(co+0.6432168421), WN(co+0.19216811));
}
float IGN(float2 n)
{
float f = 0.06711056 * n.x + 0.00583715 * n.y;
return frac(52.9829189 * frac(f));
}
float3 IGN3dts(float2 texcoord, float HL)
{
float3 OutColor;
float2 seed = texcoord*BUFFER_SCREEN_SIZE+(Frame%HL)*5.588238;
OutColor.r = IGN(seed);
OutColor.g = IGN(seed + 91.534651 + 189.6854);
OutColor.b = IGN(seed + 167.28222 + 281.9874);
return OutColor;
}
texture SSSR_BlueNoise <source="BlueNoise-64frames128x128.png";> { Width = 1024; Height = 1024; Format = RGBA8;};
sampler sSSSR_BlueNoise { Texture = SSSR_BlueNoise; AddressU = REPEAT; AddressV = REPEAT; MipFilter = Point; MinFilter = Point; MagFilter = Point; };
float3 BN3dts(float2 texcoord, float HL)
{
texcoord *= BUFFER_SCREEN_SIZE; //convert to pixel index
texcoord = texcoord%128; //limit to texture size
float frame = Frame%HL; //limit frame index to history length
int2 F;
F.x = frame%8; //Go from left to right each frame. start over after 8th
F.y = floor(frame/8)%8; //Go from top to buttom each 8 frame. start over after 8th
F *= 128; //Each step jumps to the next texture
texcoord += F;
texcoord /= 1024; //divide by atlas size
float3 Tex = tex2D(sSSSR_BlueNoise, texcoord).rgb;
return Tex;
}
float3 UVtoPos(float2 texcoord)
{
float3 scrncoord = float3(texcoord.xy*2-1, LDepth(texcoord) * FAR_PLANE);
scrncoord.xy *= scrncoord.z;
scrncoord.x *= AspectRatio;
scrncoord.xy *= rad(fov);
//scrncoord.xy *= ;
return scrncoord.xyz;
}
float3 UVtoPos(float2 texcoord, float depth)
{
float3 scrncoord = float3(texcoord.xy*2-1, depth * FAR_PLANE);
scrncoord.xy *= scrncoord.z;
scrncoord.x *= AspectRatio;
scrncoord *= rad(fov);
//scrncoord.xy *= ;
return scrncoord.xyz;
}
float2 PostoUV(float3 position)
{
float2 scrnpos = position.xy;
scrnpos /= rad(fov);
scrnpos.x /= AspectRatio;
scrnpos /= position.z;
return scrnpos/2 + 0.5;
}
float3 Normal(float2 texcoord)
{
float2 p = pix;
float3 u,d,l,r,u2,d2,l2,r2;
u = UVtoPos( texcoord + float2( 0, p.y));
d = UVtoPos( texcoord - float2( 0, p.y));
l = UVtoPos( texcoord + float2( p.x, 0));
r = UVtoPos( texcoord - float2( p.x, 0));
p *= 2;
u2 = UVtoPos( texcoord + float2( 0, p.y));
d2 = UVtoPos( texcoord - float2( 0, p.y));
l2 = UVtoPos( texcoord + float2( p.x, 0));
r2 = UVtoPos( texcoord - float2( p.x, 0));
u2 = u + (u - u2);
d2 = d + (d - d2);
l2 = l + (l - l2);
r2 = r + (r - r2);
float3 c = UVtoPos( texcoord);
float3 v = u-c; float3 h = r-c;
if( abs(d2.z-c.z) < abs(u2.z-c.z) ) v = c-d;
if( abs(l2.z-c.z) < abs(r2.z-c.z) ) h = c-l;
return normalize(cross( v, h));
}
float lum(in float3 color)
{
return (color.r+color.g+color.b)/3;
}
float3 ClampLuma(float3 color, float luma)
{
float L = lum(color);
color /= L;
color *= L > luma ? luma : L;
return color;
}
float3 GetRoughTex(float2 texcoord, float4 normal)
{
float2 p = pix;
if(!GI)
{
//depth threshold to validate samples
const float Threshold = 0.00003;
float facing = dot(normal.rgb, normalize(UVtoPos(texcoord, normal.a)));
facing *= facing;
//calculating curve and levels
float roughfac; float2 fromrough, torough;
roughfac = (1 - roughness);
fromrough.x = lerp(0, 0.1, saturate(roughness*10));
fromrough.y = 0.8;
torough = float2(0, pow(roughness, roughfac));
float3 center = toYCC(tex2D(sTexColor, texcoord).rgb);
float depth = LDepth(texcoord);
float Roughness;
//cross (+)
float2 offsets[4] = {float2(p.x,0), float2(-p.x,0),float2( 0,-p.y),float2(0,p.y)};
[unroll]for(int x; x < 4; x++)
{
float2 SampleCoord = texcoord + offsets[x];
float SampleDepth = LDepth(SampleCoord);
if(abs(SampleDepth - depth)*facing < Threshold)
{
float3 SampleColor = toYCC(tex2D( sTexColor, SampleCoord).rgb);
SampleColor = min(abs(center.g - SampleColor.g), 0.25);
Roughness += SampleColor.r;
}
}
Roughness = pow( Roughness, roughfac*0.66);
Roughness = clamp(Roughness, fromrough.x, fromrough.y);
Roughness = (Roughness - fromrough.x) / ( 1 - fromrough.x );
Roughness = Roughness / fromrough.y;
Roughness = clamp(Roughness, torough.x, torough.y);
return saturate(Roughness);
}
return 0;//RoughnessTex
}
#define BT 1000
float3 Bump(float2 texcoord, float height)
{
float2 p = pix;
float3 s[3];
s[0] = tex2D(sTexColor, texcoord + float2(p.x, 0)).rgb;
s[1] = tex2D(sTexColor, texcoord + float2(0, p.y)).rgb;
s[2] = tex2D(sTexColor, texcoord).rgb;
float LC = rcp(lum(s[0]+s[1]+s[2])) * height;
s[0] *= LC; s[1] *= LC; s[2] *= LC;
float d[3];
d[0] = LDepth(texcoord + float2(p.x, 0));
d[1] = LDepth(texcoord + float2(0, p.y));
d[2] = LDepth(texcoord);
//s[0] *= saturate(1-abs(d[0] - d[2])*1000);
//s[1] *= saturate(1-abs(d[1] - d[2])*1000);
float3 XB = s[2]-s[0];
float3 YB = s[2]-s[1];
float3 bump = float3(lum(XB)*saturate(1-abs(d[0] - d[2])*BT), lum(YB)*saturate(1-abs(d[1] - d[2])*BT), 1);
bump = normalize(bump);
return bump;
}
float3 blend_normals(float3 n1, float3 n2)
{
n1 += float3( 0, 0, 1);
n2 *= float3(-1, -1, 1);
return n1*dot(n1, n2)/n1.z - n2;
}
static const float LinearGamma = 0.454545;
static const float sRGBGamma = 2.2;
float3 InvTonemapper(float3 color)
{
if(LinearConvert)color = pow(color, LinearGamma);
float3 L;
if(TM_Mode)L = max(max(color.r, color.g), color.b); //Lottes
else L = color; //Reinhardt
color = color / ((1.0 + max(1-IT_Intensity,0.00001)) - L);
return color;
}
float3 Tonemapper(float3 color)
{
float3 L;
if(TM_Mode)L = max(max(color.r, color.g), color.b); //Lottes
else L = color; //Reinhardt
color = color / ((1.0 + max(1-IT_Intensity,0.00001)) + L);
if(LinearConvert)color = pow(color, sRGBGamma);
return (color);
}
float3 FixWhitePoint()
{
return rcp(Tonemapper(InvTonemapper(float3(1,1,1))));
}
float InvTonemapper(float color)
{//Reinhardt reversible
return color / (1.001 - color);
}
bool IsSaturated(float2 coord)
{
float2 a = float2(max(coord.r, coord.g), min(coord.r, coord.g));
return coord.r > 1 || coord.g < 0;
}
bool IsSaturatedStrict(float2 coord)
{
float2 a = float2(max(coord.r, coord.g), min(coord.r, coord.g));
return coord.r >= 1 || coord.g <= 0;
}
// The following code is licensed under the MIT license: https://gist.github.com/TheRealMJP/bc503b0b87b643d3505d41eab8b332ae
// Samples a texture with Catmull-Rom filtering, using 9 texture fetches instead of 16.
// See http://vec3.ca/bicubic-filtering-in-fewer-taps/ for more details
float4 tex2Dcatrom(in sampler tex, in float2 uv, in float2 texsize, in float lod)
{
float4 result = 0.0f;
if(UseCatrom){
texsize /= exp2(lod);
float2 samplePos = uv; samplePos *= texsize;
float2 texPos1 = floor(samplePos - 0.5f) + 0.5f;
float2 f = samplePos - texPos1;
float2 w0 = f * (-0.5f + f * (1.0f - 0.5f * f));
float2 w1 = 1.0f + f * f * (-2.5f + 1.5f * f);
float2 w2 = f * (0.5f + f * (2.0f - 1.5f * f));
float2 w3 = f * f * (-0.5f + 0.5f * f);
float2 w12 = w1 + w2;
float2 offset12 = w2 / (w1 + w2);
float2 texPos0 = texPos1 - 1;
float2 texPos3 = texPos1 + 2;
float2 texPos12 = texPos1 + offset12;
texPos0 /= texsize;
texPos3 /= texsize;
texPos12 /= texsize;
result += tex2Dlod(tex, float4(texPos0.x, texPos0.y, 0, lod)) * w0.x * w0.y;
result += tex2Dlod(tex, float4(texPos12.x, texPos0.y, 0, lod)) * w12.x * w0.y;
result += tex2Dlod(tex, float4(texPos3.x, texPos0.y, 0, lod)) * w3.x * w0.y;
result += tex2Dlod(tex, float4(texPos0.x, texPos12.y, 0, lod)) * w0.x * w12.y;
result += tex2Dlod(tex, float4(texPos12.x, texPos12.y, 0, lod)) * w12.x * w12.y;
result += tex2Dlod(tex, float4(texPos3.x, texPos12.y, 0, lod)) * w3.x * w12.y;
result += tex2Dlod(tex, float4(texPos0.x, texPos3.y, 0, lod)) * w0.x * w3.y;
result += tex2Dlod(tex, float4(texPos12.x, texPos3.y, 0, lod)) * w12.x * w3.y;
result += tex2Dlod(tex, float4(texPos3.x, texPos3.y, 0, lod)) * w3.x * w3.y;
} //UseCatrom
else{
result = tex2Dlod(tex, float4(uv, 0, lod));
} //UseBilinear
return max(0, result);
}
float GetRoughness(float2 texcoord)
{ return GI?1:tex2Dlod(sSSSR_RoughTex, float4(texcoord,0,0)).x;}
///////////////Functions///////////////////
///////////////Pixel Shader////////////////
void GBuffer1
(
float4 vpos : SV_Position,
float2 texcoord : TexCoord,
out float4 normal : SV_Target0,
out float roughness : SV_Target1) //SSSR_NormTex
{
normal.rgb = Normal(texcoord.xy);
normal.a = LDepth(texcoord.xy);
#if SMOOTH_NORMALS <= 0
normal.rgb = blend_normals( Bump(texcoord, BUMP), normal.rgb);
#endif
roughness = GetRoughTex(texcoord, normal).r;
}
float4 SNH(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{
float4 color = tex2D(sSSSR_NormTex, texcoord);
float4 s, s1; float sc;
float2 p = pix; p*=SNWidth;
float T = SNThreshold * saturate(2*(1-color.a));
T = rcp(max(T, 0.0001));
for (int i = -SNSamples; i <= SNSamples; i++)
{
s = tex2D(sSSSR_NormTex, float2(texcoord + float2(i*p.x, 0)/*, 0, LODD*/));
float diff = dot(0.333, abs(s.rgb - color.rgb)) + abs(s.a - color.a)*SNDepthW;
diff = 1-saturate(diff*T);
s1 += s*diff;
sc += diff;
}
return float4(normalize(s1.rgb), s1.a);
}
#if SMOOTH_NORMALS > 0 //For the sake of compiler error due to removing the sampler for SMOOTH_NORMALS 0
float4 SNV(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{
float4 color = tex2Dlod(sSSSR_NormTex1, float4(texcoord, 0, 0));
float4 s, s1; float sc;
float2 p = pix; p*=SNWidth;
float T = SNThreshold * saturate(2*(1-color.a)); T = rcp(max(T, 0.0001));
for (int i = -SNSamples; i <= SNSamples; i++)
{
s = tex2D(sSSSR_NormTex1, float2(texcoord + float2(0, i*p.y)/*, 0, LODD*/));
float diff = dot(0.333, abs(s.rgb - color.rgb)) + abs(s.a - color.a)*SNDepthW;
diff = 1-saturate(diff*T*2);
s1 += s*diff;
sc += diff;
}
s1.rgba = float4(normalize(s1.rgb), s1.a);
s1.rgb = blend_normals( Bump(texcoord, BUMP), s1.rgb);
return float4(s1.rgb, LDepth(texcoord));
}
#endif
void CopyGBufferLowRes(float4 vpos : SV_Position, float2 texcoord : TexCoord, out float4 Normal : SV_Target0, out float Depth : SV_Target1)
{
Normal = tex2D(sSSSR_NormTex, texcoord);
Depth = Normal.a*FAR_PLANE;
}
#define LDepthLoRes(texcoord) tex2Dlod(sSSSR_LowResDepthTex, float4(texcoord.xy, 0, 0)).r
void DoRayMarch(float3 noise, float3 position, float3 raydir, out float3 Reflection, out float HitDistance, out float a)
{
float3 raypos; float2 UVraypos; float Check, steplength; bool hit; uint i;
float bias = -position.z * rcp(FAR_PLANE);
steplength = (1 + noise.x * STEPNOISE) * position.z * 0.01;
raypos = position + raydir * steplength;
float raydepth = -RAYDEPTH;
#if UI_DIFFICULTY == 1
float RayInc = RAYINC;
[loop]for(i = 0; i < UI_RAYSTEPS; i++)
#else
const int RaySteps[5] = {17, 65, 161, 321, 501};
const float RayIncPreset[5] = {2, 1.14, 1.045, 1.02, 1.012};
float RayInc = RayIncPreset[UI_QUALITY_PRESET];
[loop]for(i = 0; i < RaySteps[UI_QUALITY_PRESET]; i++)
#endif
{
UVraypos = PostoUV(raypos);
#if __RENDERER__ >= 0xa000 // If DX10 or higher
Check = LDepthLoRes(UVraypos) - raypos.z; //FAR_PLANE is multiplied in the texture
#else
Check = LDepth(UVraypos)*FAR_PLANE - raypos.z;
#endif //DX9 fallback
if(Check < bias && Check > raydepth * max(steplength, 1))
{
a= 1;
break;
}
else if(TemporalRefine&&Check < bias)break;
raypos += raydir * steplength;
steplength *= RayInc;
}
if(IsSaturatedStrict(UVraypos.xy)) Reflection = 0;
else Reflection = tex2D(sTexColor, UVraypos.xy).rgb*a;
HitDistance = a ? distance(raypos, position) : FAR_PLANE;
}
void RayMarch(float4 vpos : SV_Position, float2 texcoord : TexCoord, out float4 FinalColor : SV_Target0, out float HitDistance : SV_Target1)
{
float4 Geometry = tex2D(sSSSR_NormTex, texcoord);
if(Geometry.w>SkyDepth)
{
HitDistance = 0;
FinalColor.rgba = float4(0,0,0,GI?1:0);
}
else
{
float Roughness = GetRoughness(texcoord);
float HL = max(1, tex2D(sSSSR_HLTex0, texcoord).r);
float3 BlueNoise = BN3dts(texcoord, GI?MAX_Frames:max(1,HL));
float3 IGNoise = IGN3dts(texcoord, max(MAX_Frames,1)); //Interleaved Gradient Noise
float3 WhiteNoise = WN3dts(texcoord, HL);
float3 noise = (HL <= 0) ? IGNoise :
(HL > 64) ? WhiteNoise :
BlueNoise;
float3 position = UVtoPos (texcoord);
float3 normal = Geometry.xyz;
float3 eyedir = normalize(position);
float3 raydirG = reflect(eyedir, normal);
float3 raydirR = normalize(noise*2-1);
if(dot(raydirR, Normal(texcoord))>0) raydirR *= -1;
float raybias = dot(raydirG, raydirR);
float3 raydir;
float4 reflection;
float a;
if(!GI)raydir = lerp(raydirG, raydirR, pow(1-(0.5*cos(raybias*PI)+0.5), rsqrt(InvTonemapper((GI)?1:Roughness))));
else raydir = raydirR;
DoRayMarch(IGNoise, position, raydir, reflection.rgb, HitDistance, a);
FinalColor.rgb = max(ClampLuma(InvTonemapper(reflection.rgb), LUM_MAX),0);
float FadeFac = 1-pow(Geometry.w, InvTonemapper(depthfade));
if(!GI)FinalColor.a = a*FadeFac;
else
{
float AORadius = rcp(max(1, max(AO_Radius_Reflection, AO_Radius_Background)));
FinalColor.a = saturate((HitDistance)*20*AORadius/FAR_PLANE);
FinalColor.rgb *= a;
//depth fade
FinalColor.rgb *= FadeFac;
FinalColor.a = lerp(1, FinalColor.a, FadeFac);
}
}//depth check if end
}//ReflectionTex
float2 GetMotionVectorsDeflickered(float2 texcoord)
{
float2 p = pix;// p /= 1;
float2 MV = 0;
if(MVErrorTolerance<1)
{
MV = sampleMotion(texcoord);
if(abs(MV.x) < p.x && abs(MV.y) < p.y) MV = 0;
}
return sampleMotion(texcoord);
//return MV;
}
void TemporalFilter(float4 vpos : SV_Position, float2 texcoord : TexCoord, out float4 FinalColor : SV_Target0, out float HistoryLength : SV_Target1)
{
float2 MotionVectors = sampleMotion(texcoord);
float2 PastUV = texcoord + MotionVectors;
HistoryLength = tex2D(sSSSR_HLTex1, PastUV).r;
float depth = LDepth(texcoord);
bool mask;
if(depth>SkyDepth){mask=0;}else{
float4 past_normal; float3 normal, ogcolor; float2 outbound; float past_ogcolor, past_depth;
//Inputs
normal = tex2D(sSSSR_NormTex, texcoord).rgb;
past_normal = tex2D(sSSSR_PNormalTex, PastUV);
past_depth = past_normal.w;
ogcolor = toYCC(tex2D(sTexColor, texcoord).rgb);
past_ogcolor = tex2D(sSSSR_POGColTex, PastUV).r;
ogcolor.r += ogcolor.g + ogcolor.b;
//multiplying by facing fac estimates the expected depth difference, reducing oversensitivity in steep angles.
mask = abs(depth - past_depth) * dot(normal, normalize(UVtoPos(texcoord)))
+ abs(ogcolor.r - past_ogcolor)*saturate(1-MVErrorTolerance)
<= TemporalFilterDisocclusionThreshold;
}//sky mask end
float4 Current, History; float3 Xvirtual, eyedir; float past_depth;
float Roughness = GI?1:GetRoughness(texcoord);
float2 outbound = PastUV;
outbound = float2(max(outbound.r, outbound.g), min(outbound.r, outbound.g));
outbound.rg = (outbound.r > 1 || outbound.g < 0);
mask = min(1-outbound.r, mask);
HistoryLength.r *= mask; //sets the history length to 0 for discarded samples
HistoryLength.r = min(HistoryLength.r, MAX_Frames); //Limits the linear accumulation to MAX_Frames, The rest will be accumulated exponentialy with the speed = (1-1/Max_Frames)
#if ANTI_LAG_ENABLED
HistoryLength.r *= lerp(
1-saturate(
abs((GI?1:1.5)*
lum(tex2D(sSSSR_FilterTex3, PastUV).rgb) -
lum(History.rgb)
)
),
1,
saturate(1 - length(MotionVectors * MBSDMultiplier / 2))
);
#endif
if(!GI)HistoryLength.r = HistoryLength.r
* max(saturate(1 - length(MotionVectors) //Weaker history for faster movements
* (1 - sqrt(Roughness)) //More effective on lower roughnesses
* MBSDMultiplier), //Motion Based Deghosting Multiplier
MBSDThreshold); //Motion Based Deghosting Max Threshold
float lod = max((4 - HistoryLength.r), 0);
Current = tex2Dcatrom(sSSSR_ReflectionTex, texcoord, BUFFER_SCREEN_SIZE*RESOLUTION_SCALE_, lod).rgba;
History = tex2D(sSSSR_FilterTex1, PastUV);
HistoryLength.r++;
FinalColor = lerp(History, Current, 1 / HistoryLength.r);
FinalColor = max(1e-6, FinalColor);
}
float GetHitDistanceAdaptation(float2 texcoord, float Roughness)
{
float HD = tex2Dlod(sSSSR_HitDistTex, float4(texcoord, 0, 3)).r;
HD = lerp(saturate(4 * HD * rcp(FAR_PLANE)), 1, Roughness);
return HD;
}
void GetNormalAndDepthFromGeometry(in float2 texcoord, out float3 Normal, out float Depth)
{
float4 Geometry = tex2Dlod(sSSSR_NormTex, float4(texcoord,0,0));
Normal = Geometry.rgb;
Depth = Geometry.a;
}
float GetSpecularSpatialDenoiserRadius(float2 texcoord)
{
float Roughness = GetRoughness(texcoord);
float HitDistance = GetHitDistanceAdaptation(texcoord, Roughness);
float radius = saturate(Roughness * 4) * HitDistance;
return (max(0.000025, saturate((radius))));
}
//mode 0 is specular. mode 1 is diffuse
float4 AdaptiveBox(in int size, in sampler Tex, in float2 texcoord, in float checkertex, in float HL)
{
float2 p = pix;
float SpecRadius = 1;
if(!GI)SpecRadius = GetSpecularSpatialDenoiserRadius(texcoord);
p *= SpecRadius;
float3 normal; float depth;
GetNormalAndDepthFromGeometry(texcoord, normal, depth);
//Used to adapt depth thresholding so it considers the expected depth difference instead of a constant value
float facing = dot(normal, normalize(UVtoPos(texcoord, depth)));
facing *= facing;
//Makes the threshold adaptive to the amount of expected noise
const float STMulList[3] = {20, 10, 5};
float ST = lerp(Sthreshold * STMulList[size], Sthreshold, sqrt(saturate(HL/16)));
float STNormal = 1 - saturate(ST * 100);
float STDepth = ST/2.5;
const float SizeList[3] = {4,8,16};
const float SizeListSmall[3] = {0,1,2};
p *= round(lerp(SizeList[size], SizeListSmall[size], min(HL/64, 1)));
p += checkertex * p; //hides atrous artifacts
float2 pr = p * 0.70710678; //turns the square to circle
//circle pattern to make the blur appear smoother
float2 offset[8];
offset = {
float2(-pr.x,-pr.y),float2(0, p.y),float2( pr.x,-pr.y),
float2(-p.x, 0), float2( p.x, 0),
float2(-pr.x, pr.y),float2(0,-p.y),float2( pr.x, pr.y)};
float4 color = tex2Dlod(Tex, float4(texcoord, 0, 0));
float4 ColorSum = color, sColor = 0;
float wsum = 1, w = 0;
float4 Min = 1e+7, Max = 0;
float clum = Tonemapper(lum(color.rgb)).x;
float slum;
float lumDiffT = rcp(max(saturate(16-HL), 0.07));
float3 snormal; float sdepth; bool determinator;
[loop]for(int i = 0; i <= 7; i++)
{
offset[i] += texcoord;
GetNormalAndDepthFromGeometry(offset[i], snormal, sdepth);
determinator =
(dot(snormal, normal)) > STNormal
&& abs(sdepth - depth) * facing < STDepth;
sColor = tex2Dlod(Tex, float4(offset[i],0,0));
slum = Tonemapper(lum(sColor.rgb)).x;
w = exp(-abs(slum - clum) * lumDiffT);
w=1;
ColorSum += (sColor * determinator * w);
wsum += (determinator * w);
Min = min(sColor, Min);
Max = max(sColor, Max);
}
ColorSum /= wsum;
ColorSum = clamp(ColorSum, Min, Max);
return ColorSum;
}
void SpatialFilter0( in float4 vpos : SV_Position, in float2 texcoord : TexCoord, out float4 FinalColor : SV_Target0)
{
float HLOut = tex2D(sSSSR_HLTex0, texcoord).r;
float HL = GetHLDivion(HLOut);
float checkertex = checker(vpos);
float4 color = AdaptiveBox(0, sSSSR_FilterTex0, texcoord, checkertex, HL);
color.a = lerp(color.a, tex2D(sSSSR_FilterTex0, texcoord).a, 1);
FinalColor = max(color, 1e-6);
}
void SpatialFilter1( in float4 vpos : SV_Position, in float2 texcoord : TexCoord, out float4 FinalColor : SV_Target0)
{
float HLOut = tex2D(sSSSR_HLTex0, texcoord).r;
float HL = GetHLDivion(HLOut);
float checkertex = checker(vpos);
float4 color = AdaptiveBox(1, sSSSR_FilterTex1, texcoord, checkertex, HL);
color.a = lerp(color.a, tex2D(sSSSR_FilterTex1, texcoord).a, 1);
FinalColor = max(color, 1e-6);
}
void SpatialFilter2(
in float4 vpos : SV_Position,
in float2 texcoord : TexCoord,
out float4 FinalColor : SV_Target0,//FilterTex1
out float4 Geometry : SV_Target1,//PNormalTex
out float3 Ogcol : SV_Target2,//POGColTex
out float HLOut : SV_Target3,//HLTex1
out float4 TSHistory : SV_Target4)//FilterTex2
{
HLOut = tex2D(sSSSR_HLTex0, texcoord).r;
float HL = GetHLDivion(HLOut);
float checkertex = checker(vpos);
float4 color = AdaptiveBox(2, sSSSR_FilterTex0, texcoord, checkertex, HL);
color.a = lerp(color.a, tex2D(sSSSR_FilterTex0, texcoord).a, saturate((HLOut-8)/64));
FinalColor = max(color, 1e-6);
Geometry = tex2D(sSSSR_NormTex, texcoord);
TSHistory = tex2D(sSSSR_FilterTex3, texcoord).rgba;
float3 OGC = toYCC(tex2D(sTexColor, texcoord).rgb);
Ogcol = OGC.x+OGC.y+OGC.z;
}
void TemporalStabilizer(float4 vpos : SV_Position, float2 texcoord : TexCoord, out float4 FinalColor : SV_Target0)
{
float HL = tex2D(sSSSR_HLTex0, texcoord).r;
float2 p = pix;
float Roughness = tex2D(sSSSR_RoughTex, texcoord).x;
float2 MotionVectors = GetMotionVectorsDeflickered(texcoord);
float4 current = tex2D(sSSSR_FilterTex1, texcoord);
float4 history = tex2Dcatrom(sSSSR_FilterTex2, texcoord + MotionVectors, BUFFER_SCREEN_SIZE, 0);
//history.rgb = Tonemapper(history.rgb);
history.rgb = toYCC(history.rgb);
float4 CurrToYCC = float4(toYCC(current.rgb), current.a);
float4 SharpenMin = 1000000, SharpenMax = 0;
float4 SharpenMean = current;
#if TEMPORAL_STABILIZER_MINMAX_CLAMPING
float4 Max = CurrToYCC, Min = CurrToYCC;
#endif
float4 PreSqr = CurrToYCC * CurrToYCC, PostSqr = CurrToYCC;
float4 SCurrent; int x, y;
float2 pr = p * 0.707;
float2 offsets[8] =
{
float2(p.x, 0), float2(-p.x, 0), float2( 0,-p.y), float2( 0,p.y),
float2(pr.x,pr.y), float2(-pr.x,-pr.y), float2(pr.x,-pr.y), float2(-pr.x,pr.y)
};
[unroll]for(int x = 0; x < shape; x++)
{
SCurrent = tex2D(sSSSR_FilterTex1, texcoord + offsets[x]);
SharpenMin = min(SCurrent, SharpenMin);
SharpenMax = max(SCurrent, SharpenMax);
SharpenMean += SCurrent;
SCurrent.rgb = toYCC(SCurrent.rgb);
#if TEMPORAL_STABILIZER_MINMAX_CLAMPING
Max = max(SCurrent, Max);
Min = min(SCurrent, Min);
#endif
PreSqr += SCurrent * SCurrent;
PostSqr += SCurrent;
}
//Min/Max Clamping
#if TEMPORAL_STABILIZER_MINMAX_CLAMPING
float4 chistory = lerp(history, clamp(history, Min, Max), 1);
#else
float4 chistory = history;
#endif
//Variance Clipping
PostSqr /= shape+1; PreSqr /= shape+1;
PostSqr *= PostSqr;
float4 Var = sqrt(abs(PostSqr - PreSqr));