forked from doodlum/skyrim-community-shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sky.hlsl
262 lines (210 loc) · 6.77 KB
/
Sky.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
#include "Common/FrameBuffer.hlsli"
#include "Common/VR.hlsli"
struct VS_INPUT
{
float4 Position : POSITION0;
#if defined(TEX) || defined(HORIZFADE)
float2 TexCoord : TEXCOORD0;
#endif
float4 Color : COLOR0;
#if defined(VR)
uint InstanceID : SV_INSTANCEID;
#endif // VR
};
struct VS_OUTPUT
{
float4 Position : SV_POSITION0;
#if defined(DITHER) && defined(TEX)
float4 TexCoord0 : TEXCOORD0;
#elif defined(DITHER)
float2 TexCoord0 : TEXCOORD3;
#elif defined(TEX) || defined(HORIZFADE)
float2 TexCoord0 : TEXCOORD0;
#endif
#if defined(TEXLERP)
float2 TexCoord1 : TEXCOORD1;
#endif
#if defined(HORIZFADE)
float TexCoord2 : TEXCOORD2;
#endif
#if defined(TEX) || defined(DITHER) || defined(HORIZFADE)
float4 Color : COLOR0;
#endif
float4 WorldPosition : POSITION1;
float4 PreviousWorldPosition : POSITION2;
#if defined(VR)
float ClipDistance : SV_ClipDistance0; // o11
float CullDistance : SV_CullDistance0; // p11
uint EyeIndex : EYEIDX0;
#endif // VR
};
#ifdef VSHADER
cbuffer PerGeometry : register(b2)
{
# if !defined(VR)
row_major float4x4 WorldViewProj[1] : packoffset(c0);
row_major float4x4 World[1] : packoffset(c4);
row_major float4x4 PreviousWorld[1] : packoffset(c8);
float3 EyePosition[1] : packoffset(c12);
float VParams : packoffset(c12.w);
float4 BlendColor[3] : packoffset(c13);
float2 TexCoordOff : packoffset(c16);
# else
row_major float4x4 WorldViewProj[2] : packoffset(c0);
row_major float4x4 World[2] : packoffset(c8);
row_major float4x4 PreviousWorld[2] : packoffset(c16);
float3 EyePosition[2] : packoffset(c24);
float VParams : packoffset(c25.w);
float4 BlendColor[3] : packoffset(c26);
float2 TexCoordOff : packoffset(c29);
# endif // !VR
};
VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT vsout;
uint eyeIndex = Stereo::GetEyeIndexVS(
# if defined(VR)
input.InstanceID
# endif
);
float4 inputPosition = float4(input.Position.xyz, 1.0);
# if defined(OCCLUSION)
// Intentionally left blank
# elif defined(MOONMASK)
vsout.TexCoord0 = input.TexCoord;
vsout.Color = float4(VParams.xxx, 1.0);
# elif defined(HORIZFADE)
float worldHeight = mul(World[eyeIndex], inputPosition).z;
float eyeHeightDelta = -EyePosition[eyeIndex].z + worldHeight;
vsout.TexCoord0.xy = input.TexCoord;
vsout.TexCoord2.x = saturate((1.0 / 17.0) * eyeHeightDelta);
vsout.Color.xyz = BlendColor[0].xyz * VParams;
vsout.Color.w = BlendColor[0].w;
# else // MOONMASK HORIZFADE
# if defined(DITHER)
# if defined(TEX)
vsout.TexCoord0.xyzw = input.TexCoord.xyxy * float4(1.0, 1.0, 501.0, 501.0);
# else
float3 inputDirection = normalize(input.Position.xyz);
inputDirection.y += inputDirection.z;
vsout.TexCoord0.x = 501 * acos(inputDirection.x);
vsout.TexCoord0.y = 501 * asin(inputDirection.y);
# endif // TEX
# elif defined(CLOUDS)
vsout.TexCoord0.xy = TexCoordOff + input.TexCoord;
# else
vsout.TexCoord0.xy = input.TexCoord;
# endif // DITHER CLOUDS
# ifdef TEXLERP
vsout.TexCoord1.xy = TexCoordOff + input.TexCoord;
# endif // TEXLERP
float3 skyColor = BlendColor[0].xyz * input.Color.xxx + BlendColor[1].xyz * input.Color.yyy +
BlendColor[2].xyz * input.Color.zzz;
vsout.Color.xyz = VParams * skyColor;
vsout.Color.w = BlendColor[0].w * input.Color.w;
# endif // OCCLUSION MOONMASK HORIZFADE
vsout.Position = mul(WorldViewProj[eyeIndex], inputPosition).xyww;
vsout.WorldPosition = mul(World[eyeIndex], inputPosition);
vsout.PreviousWorldPosition = mul(PreviousWorld[eyeIndex], inputPosition);
# ifdef VR
vsout.EyeIndex = eyeIndex;
Stereo::VR_OUTPUT VRout = Stereo::GetVRVSOutput(vsout.Position, eyeIndex);
vsout.Position = 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;
float4 MotionVectors : SV_Target1;
float4 Normal : SV_Target2;
#if defined(CLOUD_SHADOWS) && defined(CLOUDS) && !defined(DEFERRED)
float4 CloudShadows : SV_Target3;
#endif
};
#ifdef PSHADER
SamplerState SampBaseSampler : register(s0);
SamplerState SampBlendSampler : register(s1);
SamplerState SampNoiseGradSampler : register(s2);
Texture2D<float4> TexBaseSampler : register(t0);
Texture2D<float4> TexBlendSampler : register(t1);
Texture2D<float4> TexNoiseGradSampler : register(t2);
cbuffer PerGeometry : register(b2)
{
float2 PParams : packoffset(c0);
};
# if !defined(VR)
cbuffer AlphaTestRefCB : register(b11)
{
float AlphaTestRefRS : packoffset(c0);
}
# endif
# include "Common/MotionBlur.hlsli"
# include "Common/SharedData.hlsli"
# if defined(CLOUD_SHADOWS)
# include "CloudShadows/CloudShadows.hlsli"
# endif
PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT psout;
# if !defined(VR)
uint eyeIndex = 0;
# else
uint eyeIndex = input.EyeIndex;
# endif // !VR
# ifndef OCCLUSION
# ifndef TEXLERP
float4 baseColor = TexBaseSampler.Sample(SampBaseSampler, input.TexCoord0.xy);
# ifdef TEXFADE
baseColor.w *= PParams.x;
# endif
# else
float4 blendColor = TexBlendSampler.Sample(SampBlendSampler, input.TexCoord1.xy);
float4 baseColor = TexBaseSampler.Sample(SampBaseSampler, input.TexCoord0.xy);
baseColor = PParams.xxxx * (-baseColor + blendColor) + baseColor;
# endif
# if defined(DITHER)
float2 noiseGradUv = float2(0.125, 0.125) * input.Position.xy;
float noiseGrad =
TexNoiseGradSampler.Sample(SampNoiseGradSampler, noiseGradUv).x * 0.03125 + -0.0078125;
# ifdef TEX
psout.Color.xyz = (input.Color.xyz * baseColor.xyz + PParams.yyy) + noiseGrad;
psout.Color.w = baseColor.w * input.Color.w;
# else
psout.Color.xyz = (PParams.yyy + input.Color.xyz) + noiseGrad;
psout.Color.w = input.Color.w;
# endif // TEX
# elif defined(MOONMASK)
psout.Color.xyzw = baseColor;
if (baseColor.w - AlphaTestRefRS.x < 0) {
discard;
}
# elif defined(HORIZFADE)
psout.Color.xyz = float3(1.5, 1.5, 1.5) * (input.Color.xyz * baseColor.xyz + PParams.yyy);
psout.Color.w = input.TexCoord2.x * (baseColor.w * input.Color.w);
# else
psout.Color.w = input.Color.w * baseColor.w;
psout.Color.xyz = input.Color.xyz * baseColor.xyz + PParams.yyy;
# endif
# else
psout.Color = float4(0, 0, 0, 1.0);
# endif // OCCLUSION
float3 viewPosition = mul(CameraView[eyeIndex], float4(input.WorldPosition.xyz, 1)).xyz;
float2 screenUV = FrameBuffer::ViewToUV(viewPosition, true, eyeIndex);
float4 positionWS = half4(2 * half2(screenUV.x, -screenUV.y + 1) - 1, 1.0, 1);
positionWS = mul(CameraViewProjInverse[eyeIndex], positionWS);
positionWS.xyz = positionWS.xyz / positionWS.w;
positionWS.w = 1;
float2 screenMotionVector = GetSSMotionVector(positionWS, positionWS, eyeIndex);
psout.MotionVectors = float4(screenMotionVector, 0, psout.Color.w);
psout.Normal = float4(0.5, 0.5, 0, psout.Color.w);
# if defined(CLOUD_SHADOWS) && defined(CLOUDS) && !defined(DEFERRED)
psout.CloudShadows = psout.Color;
# endif
return psout;
}
#endif