forked from doodlum/skyrim-community-shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.hlsl
779 lines (672 loc) · 28 KB
/
Utility.hlsl
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
#include "Common/FrameBuffer.hlsli"
#include "Common/LodLandscape.hlsli"
#include "Common/Math.hlsli"
#include "Common/Random.hlsli"
#include "Common/SharedData.hlsli"
#include "Common/Skinned.hlsli"
#include "Common/VR.hlsli"
#if defined(RENDER_SHADOWMASK) || defined(RENDER_SHADOWMASKSPOT) || defined(RENDER_SHADOWMASKPB) || defined(RENDER_SHADOWMASKDPB)
# define RENDER_SHADOWMASK_ANY
#endif
struct VS_INPUT
{
float4 PositionMS : POSITION0;
#if defined(TEXTURE)
float2 TexCoord : TEXCOORD0;
#endif
#if defined(NORMALS)
float4 Normal : NORMAL0;
float4 Bitangent : BINORMAL0;
#endif
#if defined(VC)
float4 Color : COLOR0;
#endif
#if defined(SKINNED)
float4 BoneWeights : BLENDWEIGHT0;
float4 BoneIndices : BLENDINDICES0;
#endif
#if defined(VR)
uint InstanceID : SV_INSTANCEID;
#endif // VR
};
struct VS_OUTPUT
{
float4 PositionCS : SV_POSITION0;
#if !(defined(RENDER_DEPTH) && defined(RENDER_SHADOWMASK_ANY)) && SHADOWFILTER != 2
# if (defined(ALPHA_TEST) && ((!defined(RENDER_DEPTH) && !defined(RENDER_SHADOWMAP)) || defined(RENDER_SHADOWMAP_PB))) || defined(RENDER_NORMAL) || defined(DEBUG_SHADOWSPLIT) || defined(RENDER_BASE_TEXTURE)
float4 TexCoord0 : TEXCOORD0;
# endif
# if defined(RENDER_NORMAL)
float4 Normal : TEXCOORD1;
# endif
# if defined(RENDER_SHADOWMAP_PB)
float3 TexCoord1 : TEXCOORD2;
# elif defined(ALPHA_TEST) && (defined(RENDER_DEPTH) || defined(RENDER_SHADOWMAP))
float4 TexCoord1 : TEXCOORD2;
# elif defined(ADDITIONAL_ALPHA_MASK)
float2 TexCoord1 : TEXCOORD2;
# endif
# if defined(LOCALMAP_FOGOFWAR)
float Alpha : TEXCOORD3;
# endif
# if defined(RENDER_SHADOWMASK_ANY)
float4 PositionMS : TEXCOORD5;
# endif
# if defined(ALPHA_TEST) && defined(VC) && defined(RENDER_SHADOWMASK_ANY)
float2 Alpha : TEXCOORD4;
# elif (defined(ALPHA_TEST) && defined(VC) && !defined(TREE_ANIM)) || defined(RENDER_SHADOWMASK_ANY)
float Alpha : TEXCOORD4;
# endif
# if defined(DEBUG_SHADOWSPLIT)
float Depth : TEXCOORD2;
# endif
#endif
#if defined(VR)
float ClipDistance : SV_ClipDistance0; // o11
float CullDistance : SV_CullDistance0; // p11
uint EyeIndex : EYEIDX0;
#endif // VR
};
#ifdef VSHADER
cbuffer PerTechnique : register(b0)
{
# if !defined(VR)
float4 HighDetailRange[1] : packoffset(c0); // loaded cells center in xy, size in zw
float2 ParabolaParam : packoffset(c1); // inverse radius in x, y is 1 for forward hemisphere or -1 for backward hemisphere
# else
float4 HighDetailRange[2] : packoffset(c0); // loaded cells center in xy, size in zw
float2 ParabolaParam : packoffset(c2); // inverse radius in x, y is 1 for forward hemisphere or -1 for backward hemisphere
# endif // VR
};
cbuffer PerMaterial : register(b1)
{
float4 TexcoordOffset : packoffset(c0);
};
cbuffer PerGeometry : register(b2)
{
# if !defined(VR)
float4 ShadowFadeParam : packoffset(c0);
row_major float4x4 World[1] : packoffset(c1);
float4 EyePos[1] : packoffset(c5);
float4 WaterParams : packoffset(c6);
float4 TreeParams : packoffset(c7);
# else
float4 ShadowFadeParam : packoffset(c0);
row_major float4x4 World[2] : packoffset(c1);
float4 EyePos[2] : packoffset(c9);
float4 WaterParams : packoffset(c11);
float4 TreeParams : packoffset(c12);
# endif // VR
};
float2 SmoothSaturate(float2 value)
{
return value * value * (3 - 2 * value);
}
VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT vsout;
uint eyeIndex = Stereo::GetEyeIndexVS(
# if defined(VR)
input.InstanceID
# endif
);
# if (defined(RENDER_DEPTH) && defined(RENDER_SHADOWMASK_ANY)) || SHADOWFILTER == 2
vsout.PositionCS.xy = input.PositionMS.xy;
# if defined(RENDER_SHADOWMASKDPB)
vsout.PositionCS.z = ShadowFadeParam.z;
# else
vsout.PositionCS.z = HighDetailRange[eyeIndex].x;
# endif
vsout.PositionCS.w = 1;
# elif defined(STENCIL_ABOVE_WATER)
vsout.PositionCS.y = WaterParams.x * 2 + input.PositionMS.y;
vsout.PositionCS.xzw = input.PositionMS.xzw;
# else
precise float4 positionMS = float4(input.PositionMS.xyz, 1.0);
float4 positionCS = float4(0, 0, 0, 0);
float3 normalMS = float3(1, 1, 1);
# if defined(NORMALS)
normalMS = input.Normal.xyz * 2 - 1;
# endif
# if defined(VC) && defined(NORMALS) && defined(TREE_ANIM)
float2 treeTmp1 = SmoothSaturate(abs(2 * frac(float2(0.1, 0.25) * (TreeParams.w * TreeParams.y * TreeParams.x) + dot(input.PositionMS.xyz, 1.0.xxx) + 0.5) - 1));
float normalMult = (treeTmp1.x + 0.1 * treeTmp1.y) * (input.Color.w * TreeParams.z);
positionMS.xyz += normalMS.xyz * normalMult;
# endif
# if defined(LOD_LANDSCAPE)
positionMS = AdjustLodLandscapeVertexPositionMS(positionMS, World[eyeIndex], HighDetailRange[eyeIndex]);
# endif
# if defined(SKINNED)
precise int4 boneIndices = 765.01.xxxx * input.BoneIndices.xyzw;
float3x4 worldMatrix = GetBoneTransformMatrix(Bones, boneIndices, CameraPosAdjust[eyeIndex].xyz, input.BoneWeights);
precise float4 positionWS = float4(mul(positionMS, transpose(worldMatrix)), 1);
positionCS = mul(CameraViewProj[eyeIndex], positionWS);
# else
precise float4x4 modelViewProj = mul(CameraViewProj[eyeIndex], World[eyeIndex]);
positionCS = mul(modelViewProj, positionMS);
# endif
# if defined(RENDER_SHADOWMAP) && defined(RENDER_SHADOWMAP_CLAMPED)
positionCS.z = max(0, positionCS.z);
# endif
# if defined(LOD_LANDSCAPE)
vsout.PositionCS = AdjustLodLandscapeVertexPositionCS(positionCS);
# elif defined(RENDER_SHADOWMAP_PB)
float3 positionCSPerspective = positionCS.xyz / positionCS.w;
float3 shadowDirection = normalize(normalize(positionCSPerspective) + float3(0, 0, ParabolaParam.y));
vsout.PositionCS.xy = shadowDirection.xy / shadowDirection.z;
vsout.PositionCS.z = ParabolaParam.x * length(positionCSPerspective);
vsout.PositionCS.w = positionCS.w;
# else
vsout.PositionCS = positionCS;
# endif
# if defined(RENDER_NORMAL)
float3 normalVS = float3(1, 1, 1);
# if defined(SKINNED)
float3x3 boneRSMatrix = GetBoneRSMatrix(Bones, boneIndices, input.BoneWeights);
normalMS = normalize(mul(normalMS, transpose(boneRSMatrix)));
normalVS = mul(CameraView[eyeIndex], float4(normalMS, 0)).xyz;
# else
normalVS = mul(mul(CameraView[eyeIndex], World[eyeIndex]), float4(normalMS, 0)).xyz;
# endif
# if defined(RENDER_NORMAL_CLAMP)
normalVS = max(min(normalVS, 0.1), -0.1);
# endif
vsout.Normal.xyz = normalVS;
# if defined(VC)
vsout.Normal.w = input.Color.w;
# else
vsout.Normal.w = 1;
# endif
# endif
# if (defined(ALPHA_TEST) && ((!defined(RENDER_DEPTH) && !defined(RENDER_SHADOWMAP)) || defined(RENDER_SHADOWMAP_PB))) || defined(RENDER_NORMAL) || defined(DEBUG_SHADOWSPLIT) || defined(RENDER_BASE_TEXTURE)
float4 texCoord = float4(0, 0, 1, 1);
texCoord.xy = input.TexCoord * TexcoordOffset.zw + TexcoordOffset.xy;
# if defined(RENDER_NORMAL)
texCoord.z = max(1, 0.0013333333 * positionCS.z + 0.8);
float falloff = 1;
# if defined(RENDER_NORMAL_FALLOFF)
# if defined(SKINNED)
falloff = dot(normalMS, normalize(EyePos[eyeIndex].xyz - positionWS.xyz));
# else
falloff = dot(normalMS, normalize(EyePos[eyeIndex].xyz - positionMS.xyz));
# endif
# endif
texCoord.w = EyePos[eyeIndex].w * falloff;
# endif
vsout.TexCoord0 = texCoord;
# endif
# if defined(RENDER_SHADOWMAP_PB)
vsout.TexCoord1.x = ParabolaParam.x * length(positionCSPerspective);
vsout.TexCoord1.y = positionCS.w;
precise float parabolaParam = ParabolaParam.y * positionCS.z;
vsout.TexCoord1.z = parabolaParam * 0.5 + 0.5;
# elif defined(ALPHA_TEST) && (defined(RENDER_DEPTH) || defined(RENDER_SHADOWMAP))
float4 texCoord1 = float4(0, 0, 0, 0);
texCoord1.xy = positionCS.zw;
texCoord1.zw = input.TexCoord * TexcoordOffset.zw + TexcoordOffset.xy;
vsout.TexCoord1 = texCoord1;
# elif defined(ADDITIONAL_ALPHA_MASK)
vsout.TexCoord1 = positionCS.zw;
# elif defined(DEBUG_SHADOWSPLIT)
vsout.Depth = positionCS.z;
# endif
# if defined(RENDER_SHADOWMASK_ANY)
vsout.Alpha.x = 1 - pow(saturate(dot(positionCS.xyz, positionCS.xyz) / ShadowFadeParam.x), 8);
# if defined(SKINNED)
vsout.PositionMS.xyz = positionWS.xyz;
# else
vsout.PositionMS.xyz = positionMS.xyz;
# endif
vsout.PositionMS.w = positionCS.z;
# endif
# if (defined(ALPHA_TEST) && defined(VC)) || defined(LOCALMAP_FOGOFWAR)
# if defined(RENDER_SHADOWMASK_ANY)
vsout.Alpha.y = input.Color.w;
# elif !defined(TREE_ANIM)
vsout.Alpha.x = input.Color.w;
# endif
# endif
# endif
# if defined(OFFSET_DEPTH)
if (vsout.PositionCS.z < 4096)
vsout.PositionCS.z += 5.0;
# endif
# ifdef VR
vsout.EyeIndex = eyeIndex;
Stereo::VR_OUTPUT VRout = Stereo::GetVRVSOutput(vsout.PositionCS, eyeIndex);
vsout.PositionCS = VRout.VRPosition;
vsout.ClipDistance.x = VRout.ClipDistance;
vsout.CullDistance.x = VRout.CullDistance;
# endif // VR
return vsout;
}
#endif
typedef VS_OUTPUT PS_INPUT;
struct PS_OUTPUT
{
float4 Color : SV_Target0;
};
#ifdef PSHADER
SamplerState SampBaseSampler : register(s0);
SamplerState SampNormalSampler : register(s1);
SamplerState SampDepthSampler : register(s2);
SamplerState SampShadowMapSampler : register(s3);
SamplerComparisonState SampShadowMapSamplerComp : register(s4);
SamplerState SampStencilSampler : register(s5);
SamplerComparisonState SampFocusShadowMapSamplerComp : register(s6);
SamplerState SampGrayscaleSampler : register(s7);
Texture2D<float4> TexBaseSampler : register(t0);
Texture2D<float4> TexNormalSampler : register(t1);
Texture2D<float4> TexDepthUtilitySampler : register(t2);
Texture2DArray<float4> TexShadowMapSampler : register(t3);
Texture2DArray<float4> TexShadowMapSamplerComp : register(t4);
Texture2D<uint4> TexStencilSampler : register(t5);
Texture2DArray<float4> TexFocusShadowMapSamplerComp : register(t6);
Texture2D<float4> TexGrayscaleSampler : register(t7);
cbuffer PerTechnique : register(b0)
{
float4 VPOSOffset : packoffset(c0);
float4 ShadowSampleParam : packoffset(c1); // fPoissonRadiusScale / iShadowMapResolution in z and w
float4 EndSplitDistances : packoffset(c2); // cascade end distances int xyz, cascade count int z
float4 StartSplitDistances : packoffset(c3); // cascade start ditances int xyz, 4 int z
float4 FocusShadowFadeParam : packoffset(c4);
}
cbuffer PerMaterial : register(b1)
{
float RefractionPower : packoffset(c0);
float4 BaseColor : packoffset(c1);
}
cbuffer PerGeometry : register(b2)
{
float4 DebugColor : packoffset(c0);
float4 PropertyColor : packoffset(c1);
float4 AlphaTestRef : packoffset(c2);
float4 ShadowLightParam : packoffset(c3); // Falloff in x, ShadowDistance squared in z
# if !defined(VR)
float4x3 FocusShadowMapProj[4] : packoffset(c4);
# if defined(RENDER_SHADOWMASK)
float4x3 ShadowMapProj[1][3] : packoffset(c16); // 16, 19, 22
# elif defined(RENDER_SHADOWMASKSPOT) || defined(RENDER_SHADOWMASKPB) || defined(RENDER_SHADOWMASKDPB)
float4x4 ShadowMapProj[1][3] : packoffset(c16);
# endif
# else
float4 VRUnknown : packoffset(c4); // used to multiply by identity matrix, see e.g., 4202499.ps.bin.hlsl
/*
r1.x = dot(cb2[4].xz, icb[r0.w+0].xz);
r1.x = r0.x * cb12[86].x + -r1.x;
r0.w = (int)r0.w + 1;
r0.w = (int)r0.w + -1;
r0.w = dot(cb2[4].yw, icb[r0.w+0].xz);
*/
float4x3 FocusShadowMapProj[4] : packoffset(c5);
# if defined(RENDER_SHADOWMASK)
float4x3 ShadowMapProj[2][3] : packoffset(c29); // VR has a couple of offsets of 3, e.g., {29, 32, 35} and {38, 41, 44}, compare to Flat which does [16, 19, 22]
# elif defined(RENDER_SHADOWMASKSPOT) || defined(RENDER_SHADOWMASKPB) || defined(RENDER_SHADOWMASKDPB)
float4x4 ShadowMapProj[2][3] : packoffset(c29);
# endif
# endif // VR
}
# if !defined(VR)
cbuffer AlphaTestRefCB : register(b11)
{
float AlphaTestRefRS : packoffset(c0);
}
# endif // !VR
# if defined(RENDER_SHADOWMASKDPB)
float GetPoissonDiskFilteredShadowVisibility(float noise, float2x2 rotationMatrix, Texture2DArray<float4> tex, SamplerComparisonState samp, float3 baseUV, float layerIndex, float compareValue, bool asymmetric)
# else
float GetPoissonDiskFilteredShadowVisibility(float noise, float2x2 rotationMatrix, Texture2DArray<float4> tex, SamplerComparisonState samp, float2 baseUV, float layerIndex, float compareValue, bool asymmetric)
# endif
{
const int sampleCount = 8;
# if defined(RENDER_SHADOWMASK)
uint onePlusLayerIndex = 1.0 + layerIndex;
float layerIndexRcp = rcp(onePlusLayerIndex);
# endif
float visibility = 0;
for (int sampleIndex = 0; sampleIndex < sampleCount; ++sampleIndex) {
float2 sampleOffset = mul(Random::SpiralSampleOffsets8[sampleIndex], rotationMatrix);
sampleOffset *= 1.5;
# if defined(RENDER_SHADOWMASKDPB)
float2 sampleUV = sampleOffset + baseUV.xy;
baseUV.z += noise;
bool lowerHalf = baseUV.z * 0.5 + 0.5 < 0;
float3 normalizedPositionLS = normalize(float3(sampleUV.xy, baseUV.z));
float3 positionOffset = lowerHalf ? float3(0, 0, -1) : float3(0, 0, 1);
float3 lightDirection = normalize(normalizedPositionLS + positionOffset);
float2 shadowMapUV = lightDirection.xy / lightDirection.z * 0.5 + 0.5;
shadowMapUV.y = lowerHalf ? 1 - 0.5 * shadowMapUV.y : 0.5 * shadowMapUV.y;
visibility += tex.SampleCmpLevelZero(samp, float3(shadowMapUV, layerIndex), compareValue).x;
# elif defined(RENDER_SHADOWMASK)
float2 sampleUV = layerIndexRcp * sampleOffset * ShadowSampleParam.z + baseUV;
visibility += tex.SampleCmpLevelZero(samp, float3(sampleUV, layerIndex), compareValue).x;
# else
float2 sampleUV = sampleOffset * ShadowSampleParam.z + baseUV;
visibility += tex.SampleCmpLevelZero(samp, float3(sampleUV, layerIndex), compareValue).x;
# endif
}
return visibility * rcp((float)sampleCount);
}
PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT psout;
# if !defined(VR)
uint eyeIndex = 0;
# else
uint eyeIndex = input.EyeIndex;
# endif // !VR
# if defined(ADDITIONAL_ALPHA_MASK)
uint2 alphaMask = input.PositionCS.xy;
alphaMask.x = ((alphaMask.x << 2) & 12);
alphaMask.x = (alphaMask.y & 3) | (alphaMask.x & ~3);
const float maskValues[16] = {
0.003922,
0.533333,
0.133333,
0.666667,
0.800000,
0.266667,
0.933333,
0.400000,
0.200000,
0.733333,
0.066667,
0.600000,
0.996078,
0.466667,
0.866667,
0.333333,
};
if (AlphaTestRef.w - maskValues[alphaMask.x] < 0) {
discard;
}
# endif
# if defined(FOLIAGE)
float checkerboard = Random::InterleavedGradientNoise(input.PositionCS.xy, 0);
if (checkerboard > 0.75)
discard;
# endif
float2 baseTexCoord = 0;
# if !(defined(RENDER_DEPTH) && defined(RENDER_SHADOWMASK_ANY)) && SHADOWFILTER != 2
# if (defined(RENDER_DEPTH) || defined(RENDER_SHADOWMAP)) && defined(ALPHA_TEST) && !defined(RENDER_SHADOWMAP_PB)
baseTexCoord = input.TexCoord1.zw;
# elif (defined(ALPHA_TEST) && ((!defined(RENDER_DEPTH) && !defined(RENDER_SHADOWMAP)) || defined(RENDER_SHADOWMAP_PB))) || defined(RENDER_NORMAL) || defined(DEBUG_SHADOWSPLIT) || defined(RENDER_BASE_TEXTURE)
baseTexCoord = input.TexCoord0.xy;
# endif
# endif
float4 baseColor = TexBaseSampler.Sample(SampBaseSampler, baseTexCoord);
# if defined(RENDER_SHADOWMAP_PB)
if (input.TexCoord1.z < 0) {
discard;
}
# endif
float alpha = 1;
# if defined(ALPHA_TEST) && !(defined(RENDER_SHADOWMASK_ANY) && defined(RENDER_DEPTH))
alpha = baseColor.w;
# if defined(DEPTH_WRITE_DECALS) && !defined(GRAYSCALE_MASK)
alpha = saturate(1.05 * alpha);
# endif
# if defined(OPAQUE_EFFECT)
alpha *= BaseColor.w * PropertyColor.w;
# endif
# if (defined(RENDER_DEPTH) || defined(RENDER_SHADOWMAP) || ((defined(RENDER_NORMAL) || defined(RENDER_NORMAL_CLEAR)) && defined(DEBUG_COLOR))) && defined(VC) && !defined(TREE_ANIM) && !defined(LOD_OBJECT)
alpha *= input.Alpha;
# elif defined(RENDER_SHADOWMASK_ANY) && defined(VC)
alpha *= input.Alpha.y;
# endif
# if defined(GRAYSCALE_TO_ALPHA)
float grayScaleColor = TexGrayscaleSampler.Sample(SampGrayscaleSampler, float2(baseColor.w, alpha)).w;
if (grayScaleColor - AlphaTestRef.x < 0) {
discard;
}
# endif
# if defined(GRAYSCALE_MASK)
if (baseColor.y - AlphaTestRef.x < 0) {
discard;
}
# elif !defined(RENDER_SHADOWMAP)
if (alpha - AlphaTestRef.x < 0) {
discard;
}
# endif
# endif
# if defined(RENDER_SHADOWMASK_ANY)
float4 shadowColor = 1;
uint stencilValue = 0;
float shadowMapDepth = 0;
# if defined(RENDER_DEPTH)
float2 depthUv = input.PositionCS.xy * VPOSOffset.xy + VPOSOffset.zw;
float depth = TexDepthUtilitySampler.Sample(SampDepthSampler, depthUv).x;
shadowMapDepth = depth;
# if defined(FOCUS_SHADOW)
uint3 stencilDimensions;
TexStencilSampler.GetDimensions(0, stencilDimensions.x, stencilDimensions.y, stencilDimensions.z);
stencilValue = TexStencilSampler.Load(float3(stencilDimensions.xy * depthUv, 0)).x;
# endif
depthUv = Stereo::ConvertFromStereoUV(depthUv * DynamicResolutionParams2.xy, eyeIndex);
float4 positionCS = float4(2 * float2(depthUv.x, -depthUv.y + 1) - 1, depth, 1);
float4 positionMS = mul(CameraViewProjInverse[eyeIndex], positionCS);
positionMS.xyz = positionMS.xyz / positionMS.w;
float fadeFactor = 1 - pow(saturate(dot(positionMS.xyz, positionMS.xyz) / ShadowLightParam.z), 8);
# else
float4 positionMS = input.PositionMS.xyzw;
shadowMapDepth = positionMS.w;
float fadeFactor = input.Alpha.x;
# endif
float noise = Random::InterleavedGradientNoise(input.PositionCS.xy, FrameCount);
float2 rotation;
sincos(Math::TAU * noise, rotation.y, rotation.x);
float2x2 rotationMatrix = float2x2(rotation.x, rotation.y, -rotation.y, rotation.x);
noise = noise * 2.0 - 1.0;
# if defined(RENDER_SHADOWMASK)
if (EndSplitDistances.z >= shadowMapDepth) {
float4x3 lightProjectionMatrix = ShadowMapProj[eyeIndex][0];
float shadowMapThreshold = AlphaTestRef.y;
float cascadeIndex = 0;
if (2.5 < EndSplitDistances.w && EndSplitDistances.y < shadowMapDepth) {
lightProjectionMatrix = ShadowMapProj[eyeIndex][2];
shadowMapThreshold = AlphaTestRef.z;
cascadeIndex = 2;
} else if (EndSplitDistances.x < shadowMapDepth) {
lightProjectionMatrix = ShadowMapProj[eyeIndex][1];
shadowMapThreshold = AlphaTestRef.z;
cascadeIndex = 1;
}
float shadowVisibility = 0;
float3 positionLS = mul(transpose(lightProjectionMatrix), float4(positionMS.xyz, 1)).xyz;
# if SHADOWFILTER == 0
float shadowMapValue = TexShadowMapSampler.Sample(SampShadowMapSampler, float3(positionLS.xy, cascadeIndex)).x;
if (shadowMapValue >= positionLS.z - shadowMapThreshold) {
shadowVisibility = 1;
}
# elif SHADOWFILTER == 1
shadowVisibility = TexShadowMapSamplerComp.SampleCmpLevelZero(SampShadowMapSamplerComp, float3(positionLS.xy, cascadeIndex), positionLS.z - shadowMapThreshold).x;
# elif SHADOWFILTER == 3
shadowVisibility = GetPoissonDiskFilteredShadowVisibility(noise, rotationMatrix, TexShadowMapSamplerComp, SampShadowMapSamplerComp, positionLS.xy, cascadeIndex, positionLS.z - shadowMapThreshold, false);
# endif
if (cascadeIndex < 1 && StartSplitDistances.y < shadowMapDepth) {
float cascade1ShadowVisibility = 0;
float3 cascade1PositionLS = mul(transpose(ShadowMapProj[eyeIndex][1]), float4(positionMS.xyz, 1)).xyz;
# if SHADOWFILTER == 0
float cascade1ShadowMapValue = TexShadowMapSampler.Sample(SampShadowMapSampler, float3(cascade1PositionLS.xy, 1)).x;
if (cascade1ShadowMapValue >= cascade1PositionLS.z - AlphaTestRef.z) {
cascade1ShadowVisibility = 1;
}
# elif SHADOWFILTER == 1
cascade1ShadowVisibility = TexShadowMapSamplerComp.SampleCmpLevelZero(SampShadowMapSamplerComp, float3(cascade1PositionLS.xy, 1), cascade1PositionLS.z - AlphaTestRef.z).x;
# elif SHADOWFILTER == 3
cascade1ShadowVisibility = GetPoissonDiskFilteredShadowVisibility(noise, rotationMatrix, TexShadowMapSamplerComp, SampShadowMapSamplerComp, cascade1PositionLS.xy, 1, cascade1PositionLS.z - AlphaTestRef.z, false);
# endif
float cascade1BlendFactor = smoothstep(0, 1, (shadowMapDepth - StartSplitDistances.y) / (EndSplitDistances.x - StartSplitDistances.y));
shadowVisibility = lerp(shadowVisibility, cascade1ShadowVisibility, cascade1BlendFactor);
shadowMapThreshold = AlphaTestRef.z;
}
if (stencilValue != 0) {
uint focusShadowIndex = stencilValue - 1;
float3 focusShadowMapPosition = mul(transpose(FocusShadowMapProj[focusShadowIndex]), float4(positionMS.xyz, 1));
float3 focusShadowMapUv = float3(focusShadowMapPosition.xy, StartSplitDistances.w + focusShadowIndex);
float focusShadowMapCompareValue = focusShadowMapPosition.z - 3 * shadowMapThreshold;
# if SHADOWFILTER == 3
float focusShadowVisibility = GetPoissonDiskFilteredShadowVisibility(noise, rotationMatrix, TexFocusShadowMapSamplerComp, SampFocusShadowMapSamplerComp, focusShadowMapUv.xy, focusShadowMapUv.z, focusShadowMapCompareValue, false).x;
# else
float focusShadowVisibility = TexFocusShadowMapSamplerComp.SampleCmpLevelZero(SampFocusShadowMapSamplerComp, focusShadowMapUv, focusShadowMapCompareValue).x;
# endif
float focusShadowFade = FocusShadowFadeParam[focusShadowIndex];
shadowVisibility = min(shadowVisibility, lerp(1, focusShadowVisibility, focusShadowFade));
}
shadowColor.xyzw = fadeFactor * (shadowVisibility - 1) + 1;
}
# elif defined(RENDER_SHADOWMASKSPOT)
float4 positionLS = mul(transpose(ShadowMapProj[eyeIndex][0]), float4(positionMS.xyz, 1));
positionLS.xyz /= positionLS.w;
float2 shadowMapUv = positionLS.xy * 0.5 + 0.5;
float shadowBaseVisibility = 0;
# if SHADOWFILTER == 0
float shadowMapValue = TexShadowMapSampler.Sample(SampShadowMapSampler, float3(shadowMapUv, EndSplitDistances.x)).x;
if (shadowMapValue >= positionLS.z - AlphaTestRef.y) {
shadowBaseVisibility = 1;
}
# elif SHADOWFILTER == 1
shadowBaseVisibility = TexShadowMapSamplerComp.SampleCmpLevelZero(SampShadowMapSamplerComp, float3(shadowMapUv, EndSplitDistances.x), positionLS.z - AlphaTestRef.y).x;
# elif SHADOWFILTER == 3
shadowBaseVisibility = GetPoissonDiskFilteredShadowVisibility(noise, rotationMatrix, TexShadowMapSamplerComp, SampShadowMapSamplerComp, shadowMapUv.xy, EndSplitDistances.x, positionLS.z - AlphaTestRef.y, false);
# endif
float shadowVisibilityFactor = pow(2 * length(0.5 * positionLS.xy), ShadowLightParam.x);
float shadowVisibility = shadowBaseVisibility - shadowVisibilityFactor * shadowBaseVisibility;
if (stencilValue != 0) {
uint focusShadowIndex = stencilValue - 1;
float3 focusShadowMapPosition = mul(transpose(FocusShadowMapProj[focusShadowIndex]), float4(positionMS.xyz, 1));
float3 focusShadowMapUv = float3(focusShadowMapPosition.xy, StartSplitDistances.w + focusShadowIndex);
float focusShadowMapCompareValue = focusShadowMapPosition.z - 3 * AlphaTestRef.y;
float focusShadowVisibility = 0;
# if SHADOWFILTER == 0
float shadowMapValue = TexShadowMapSampler.Sample(SampShadowMapSampler, focusShadowMapUv).x;
if (shadowMapValue >= focusShadowMapCompareValue) {
focusShadowVisibility = 1;
}
# elif SHADOWFILTER == 1
focusShadowVisibility = TexShadowMapSamplerComp.SampleCmpLevelZero(SampShadowMapSamplerComp, focusShadowMapUv, focusShadowMapCompareValue).x;
# elif SHADOWFILTER == 3
focusShadowVisibility = GetPoissonDiskFilteredShadowVisibility(noise, rotationMatrix, TexShadowMapSamplerComp, SampShadowMapSamplerComp, focusShadowMapUv.xy, focusShadowMapUv.z, focusShadowMapCompareValue, false);
# endif
shadowVisibility = min(shadowVisibility, lerp(1, focusShadowVisibility, FocusShadowFadeParam[focusShadowIndex]));
}
shadowColor.xyzw = fadeFactor * shadowVisibility;
# elif defined(RENDER_SHADOWMASKPB)
float4 unadjustedPositionLS = mul(transpose(ShadowMapProj[eyeIndex][0]), float4(positionMS.xyz, 1));
float shadowVisibility = 0;
if (unadjustedPositionLS.z * 0.5 + 0.5 >= 0) {
float3 positionLS = unadjustedPositionLS.xyz / unadjustedPositionLS.w;
float3 lightDirection = normalize(normalize(positionLS) + float3(0, 0, 1));
float2 shadowMapUv = lightDirection.xy / lightDirection.z * 0.5 + 0.5;
float shadowMapCompareValue = saturate(length(positionLS) / ShadowLightParam.x) - AlphaTestRef.y;
# if SHADOWFILTER == 0
float shadowMapValue = TexShadowMapSampler.Sample(SampShadowMapSampler, float3(shadowMapUv, EndSplitDistances.x)).x;
if (shadowMapValue >= shadowMapCompareValue) {
shadowVisibility = 1;
}
# elif SHADOWFILTER == 1
shadowVisibility = TexShadowMapSamplerComp.SampleCmpLevelZero(SampShadowMapSamplerComp, float3(shadowMapUv, EndSplitDistances.x), shadowMapCompareValue).x;
# elif SHADOWFILTER == 3
shadowVisibility = GetPoissonDiskFilteredShadowVisibility(noise, rotationMatrix, TexShadowMapSamplerComp, SampShadowMapSamplerComp, shadowMapUv.xy, EndSplitDistances.x, shadowMapCompareValue, false);
# endif
} else {
shadowVisibility = 1;
}
shadowColor.xyzw = fadeFactor * shadowVisibility;
# elif defined(RENDER_SHADOWMASKDPB)
float3 positionLS = mul(transpose(ShadowMapProj[eyeIndex][0]), float4(positionMS.xyz, 1)).xyz;
bool lowerHalf = positionLS.z * 0.5 + 0.5 < 0;
float3 normalizedPositionLS = normalize(positionLS);
float shadowMapCompareValue = saturate(length(positionLS) / ShadowLightParam.x) - AlphaTestRef.y;
float3 positionOffset = lowerHalf ? float3(0, 0, -1) : float3(0, 0, 1);
float3 lightDirection = normalize(normalizedPositionLS + positionOffset);
float2 shadowMapUv = lightDirection.xy / lightDirection.z * 0.5 + 0.5;
shadowMapUv.y = lowerHalf ? 1 - 0.5 * shadowMapUv.y : 0.5 * shadowMapUv.y;
float shadowVisibility = 0;
# if SHADOWFILTER == 0
float shadowMapValue = TexShadowMapSampler.Sample(SampShadowMapSampler, float3(shadowMapUv, EndSplitDistances.x)).x;
if (shadowMapValue >= shadowMapCompareValue) {
shadowVisibility = 1;
}
# elif SHADOWFILTER == 1
shadowVisibility = TexShadowMapSamplerComp.SampleCmpLevelZero(SampShadowMapSamplerComp, float3(shadowMapUv, EndSplitDistances.x), shadowMapCompareValue).x;
# elif SHADOWFILTER == 3
shadowVisibility = GetPoissonDiskFilteredShadowVisibility(noise, rotationMatrix, TexShadowMapSamplerComp, SampShadowMapSamplerComp, positionLS.xyz, EndSplitDistances.x, shadowMapCompareValue, true);
# endif
shadowColor.xyzw = fadeFactor * shadowVisibility;
# endif
# endif
# if defined(TEXTURE)
float testAlpha = 1;
# if defined(RENDER_SHADOWMASK_ANY)
testAlpha = shadowColor.w;
# elif defined(DEBUG_SHADOWSPLIT) || defined(RENDER_BASE_TEXTURE)
testAlpha = baseColor.w;
# elif defined(LOCALMAP_FOGOFWAR)
testAlpha = input.Alpha;
# elif (defined(RENDER_DEPTH) || defined(RENDER_SHADOWMAP)) && defined(ALPHA_TEST)
testAlpha = alpha;
# elif defined(STENCIL_ABOVE_WATER)
testAlpha = 0.5;
# elif defined(RENDER_NORMAL_CLEAR) && !defined(DEBUG_COLOR)
testAlpha = 0;
# endif
if (-AlphaTestRefRS + testAlpha < 0) {
discard;
}
# endif
# if defined(RENDER_SHADOWMASK_ANY)
psout.Color.xyzw = shadowColor;
# elif defined(DEBUG_SHADOWSPLIT)
float3 splitFactor = 0;
if (input.Depth < EndSplitDistances.x) {
splitFactor.y += 1;
}
if (input.Depth < EndSplitDistances.w && input.Depth > EndSplitDistances.z) {
splitFactor.y += 1;
splitFactor.z += 1;
}
if (input.Depth < EndSplitDistances.y && input.Depth > EndSplitDistances.x) {
splitFactor.z += 1;
}
if (input.Depth < EndSplitDistances.z && input.Depth > EndSplitDistances.y) {
splitFactor.x += 1;
splitFactor.y += 1;
}
# if defined(DEBUG_COLOR)
psout.Color.xyz = (-splitFactor.xyz + DebugColor.xyz) * 0.9 + splitFactor.xyz;
# else
psout.Color.xyz = (-splitFactor.xyz + baseColor.xyz) * 0.9 + splitFactor.xyz;
# endif
psout.Color.w = baseColor.w;
# elif defined(DEBUG_COLOR)
psout.Color = float4(DebugColor.xyz, 1);
# elif defined(RENDER_BASE_TEXTURE)
psout.Color.xyzw = baseColor;
# elif defined(LOCALMAP_FOGOFWAR)
psout.Color = float4(0, 0, 0, input.Alpha);
# elif defined(GRAYSCALE_MASK)
psout.Color = float4(input.TexCoord1.x / input.TexCoord1.y, baseColor.yz, alpha);
# elif (defined(RENDER_SHADOWMAP) && defined(ALPHA_TEST)) || defined(RENDER_SHADOWMAP_PB)
psout.Color = float4(input.TexCoord1.xxx / input.TexCoord1.yyy, alpha);
# elif defined(RENDER_DEPTH) && (defined(ALPHA_TEST) || defined(ADDITIONAL_ALPHA_MASK))
psout.Color = float4(input.TexCoord1.x / input.TexCoord1.y, 1, 1, alpha);
# elif defined(STENCIL_ABOVE_WATER)
psout.Color = float4(1, 0, 0, 0.5);
# elif defined(RENDER_NORMAL_CLEAR)
psout.Color = float4(0.5, 0.5, 0, 0);
# elif defined(RENDER_NORMAL)
float2 normal = 2 * (-0.5 + TexNormalSampler.Sample(SampNormalSampler, input.TexCoord0.xy).xy);
# if defined(RENDER_NORMAL_CLAMP)
normal = clamp(normal, -0.1, 0.1);
# endif
psout.Color.xy = ((normal * 0.9 + input.Normal.xy) / input.TexCoord0.z) * 0.5 + 0.5;
psout.Color.z = input.Normal.w * (RefractionPower * input.TexCoord0.w);
psout.Color.w = 1;
# else
psout.Color = float4(1, 1, 1, 1);
# endif
return psout;
}
#endif