-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathdeferred1.glsl
More file actions
421 lines (335 loc) · 13.5 KB
/
deferred1.glsl
File metadata and controls
421 lines (335 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/////////////////////////////////////
// Complementary Shaders by EminGT //
/////////////////////////////////////
//Common//
#include "/lib/common.glsl"
//////////Fragment Shader//////////Fragment Shader//////////Fragment Shader//////////
#ifdef FRAGMENT_SHADER
noperspective in vec2 texCoord;
flat in vec3 upVec, sunVec, eastVec;
#if defined LIGHTSHAFTS_ACTIVE && (LIGHTSHAFT_BEHAVIOUR == 1 && SHADOW_QUALITY >= 1 || defined END)
flat in float vlFactor;
#endif
//Pipeline Constants//
const bool colortex0MipmapEnabled = true;
//Common Variables//
float SdotU = dot(sunVec, upVec);
float sunFactor = SdotU < 0.0 ? clamp(SdotU + 0.375, 0.0, 0.75) / 0.75 : clamp(SdotU + 0.03125, 0.0, 0.0625) / 0.0625;
float sunVisibility = clamp(SdotU + 0.0625, 0.0, 0.125) / 0.125;
float sunVisibility2 = sunVisibility * sunVisibility;
float shadowTimeVar1 = abs(sunVisibility - 0.5) * 2.0;
float shadowTimeVar2 = shadowTimeVar1 * shadowTimeVar1;
float shadowTime = shadowTimeVar2 * shadowTimeVar2;
float farMinusNear = far - near;
vec2 view = vec2(viewWidth, viewHeight);
#ifdef OVERWORLD
vec3 lightVec = sunVec * ((timeAngle < 0.5325 || timeAngle > 0.9675) ? 1.0 : -1.0);
#else
vec3 lightVec = sunVec;
#endif
#if defined LIGHTSHAFTS_ACTIVE && (LIGHTSHAFT_BEHAVIOUR == 1 && SHADOW_QUALITY >= 1 || defined END)
#else
float vlFactor = 0.0;
#endif
#ifdef IRIS_FEATURE_FADE_VARIABLE
float chunkFade = 1.0;
#endif
//Common Functions//
float GetLinearDepth(float depth) {
return (2.0 * near) / (far + near - depth * farMinusNear);
}
#if SSAO_QUALI > 0
vec2 OffsetDist(float x, int s) {
float n = fract(x * 1.414) * 3.1415;
return pow2(vec2(cos(n), sin(n)) * x / s);
}
float DoAmbientOcclusion(float z0, float linearZ0, float dither) {
if (z0 < 0.56) return 1.0;
float ao = 0.0;
#if SSAO_QUALI == 2
int samples = 4;
float scm = 0.4;
#elif SSAO_QUALI == 3
int samples = 12;
float scm = 0.6;
#endif
#define SSAO_I_FACTOR 0.004
float sampleDepth = 0.0, angle = 0.0, dist = 0.0;
float fovScale = gbufferProjection[1][1];
float distScale = max(farMinusNear * linearZ0 + near, 3.0);
vec2 scale = vec2(scm / aspectRatio, scm) * fovScale / distScale;
for (int i = 1; i <= samples; i++) {
vec2 offset = OffsetDist(i + dither, samples) * scale;
if (i % 2 == 0) offset.y = -offset.y;
vec2 coord1 = texCoord + offset;
vec2 coord2 = texCoord - offset;
sampleDepth = GetLinearDepth(texture2D(depthtex0, coord1).r);
float aosample = farMinusNear * (linearZ0 - sampleDepth) * 2.0;
angle = clamp(0.5 - aosample, 0.0, 1.0);
dist = clamp(0.5 * aosample - 1.0, 0.0, 1.0);
sampleDepth = GetLinearDepth(texture2D(depthtex0, coord2).r);
aosample = farMinusNear * (linearZ0 - sampleDepth) * 2.0;
angle += clamp(0.5 - aosample, 0.0, 1.0);
dist += clamp(0.5 * aosample - 1.0, 0.0, 1.0);
ao += clamp(angle + dist, 0.0, 1.0);
}
ao /= samples;
#define SSAO_IM SSAO_I * SSAO_I_FACTOR
return pow(ao, SSAO_IM);
}
#endif
//Includes//
#include "/lib/util/spaceConversion.glsl"
#include "/lib/util/dither.glsl"
#include "/lib/atmospherics/fog/mainFog.glsl"
#include "/lib/colors/skyColors.glsl"
#include "/lib/colors/lightAndAmbientColors.glsl"
#if AURORA_STYLE > 0
#include "/lib/atmospherics/auroraBorealis.glsl"
#endif
#if NIGHT_NEBULAE == 1
#include "/lib/atmospherics/nightNebula.glsl"
#endif
#ifdef VL_CLOUDS_ACTIVE
#include "/lib/atmospherics/clouds/mainClouds.glsl"
#endif
#ifdef END
#include "/lib/atmospherics/enderStars.glsl"
#endif
#ifdef WORLD_OUTLINE
#include "/lib/misc/worldOutline.glsl"
#endif
#ifdef DARK_OUTLINE
#include "/lib/misc/darkOutline.glsl"
#endif
#ifdef ATM_COLOR_MULTS
#include "/lib/colors/colorMultipliers.glsl"
#endif
#ifdef MOON_PHASE_INF_ATMOSPHERE
#include "/lib/colors/moonPhaseInfluence.glsl"
#endif
#ifdef DISTANT_LIGHT_BOKEH
#include "/lib/misc/distantLightBokeh.glsl"
#endif
#ifdef SSGI_ENABLED
#include "/lib/lighting/screenSpaceGI.glsl"
#endif
//Program//
void main() {
vec4 color = vec4(texelFetch(colortex0, texelCoord, 0).rgb, 1.0);
float z0 = texelFetch(depthtex0, texelCoord, 0).r;
vec4 screenPos = vec4(texCoord, z0, 1.0);
vec4 viewPos = gbufferProjectionInverse * (screenPos * 2.0 - 1.0);
viewPos /= viewPos.w;
float lViewPos = length(viewPos);
vec3 nViewPos = normalize(viewPos.xyz);
vec3 playerPos = ViewToPlayer(viewPos.xyz);
float dither = texture2DLod(noisetex, texCoord * vec2(viewWidth, viewHeight) / 128.0, 0.0).b;
#ifdef TAA
dither = fract(dither + goldenRatio * mod(float(frameCounter), 3600.0));
#endif
#ifdef ATM_COLOR_MULTS
atmColorMult = GetAtmColorMult();
sqrtAtmColorMult = sqrt(atmColorMult);
#endif
float VdotU = dot(nViewPos, upVec);
float VdotS = dot(nViewPos, sunVec);
float skyFade = 0.0;
vec3 waterRefColor = vec3(0.0);
vec3 auroraBorealis = vec3(0.0);
vec3 nightNebula = vec3(0.0);
vec3 normalM = vec3(0);
float fresnelM = 0.0;
if (z0 < 1.0) {
#ifdef DISTANT_LIGHT_BOKEH
int dlbo = 1;
vec3 dlbColor = color.rgb;
dlbColor += texelFetch(colortex0, texelCoord + ivec2( 0, dlbo), 0).rgb;
dlbColor += texelFetch(colortex0, texelCoord + ivec2( 0,-dlbo), 0).rgb;
dlbColor += texelFetch(colortex0, texelCoord + ivec2( dlbo, 0), 0).rgb;
dlbColor += texelFetch(colortex0, texelCoord + ivec2(-dlbo, 0), 0).rgb;
dlbColor = max(color.rgb, dlbColor * 0.2);
float dlbMix = GetDistantLightBokehMix(lViewPos);
color.rgb = mix(color.rgb, dlbColor, dlbMix);
#endif
#if SSAO_QUALI > 0 || defined WORLD_OUTLINE
float linearZ0 = GetLinearDepth(z0);
#define SSGI_LINEAR_Z0_DEFINED
#endif
#if SSAO_QUALI > 0
float ssao = DoAmbientOcclusion(z0, linearZ0, dither);
#else
float ssao = 1.0;
#endif
vec3 texture6 = texelFetch(colortex6, texelCoord, 0).rgb;
bool entityOrParticle = z0 < 0.56;
int materialMaskInt = int(texture6.g * 255.1);
float intenseFresnel = 0.0;
float smoothnessD = texture6.r;
vec3 reflectColor = vec3(1.0);
#include "/lib/materials/materialHandling/deferredMaterials.glsl"
#ifdef WORLD_OUTLINE
#ifndef WORLD_OUTLINE_ON_ENTITIES
if (!entityOrParticle)
#endif
DoWorldOutline(color.rgb, linearZ0);
#endif
#ifdef DARK_OUTLINE
DoDarkOutline(color.rgb, z0);
#endif
color.rgb *= ssao;
#ifdef SSGI_ENABLED
#ifndef SSGI_LINEAR_Z0_DEFINED
float linearZ0SSGI = GetLinearDepth(z0);
#else
float linearZ0SSGI = linearZ0;
#endif
vec3 ssgiWorldNormal = texelFetch(colortex4, texelCoord, 0).rgb;
vec3 ssgiViewNormal = mat3(gbufferModelView) * ssgiWorldNormal;
vec3 giColor;
float giHit = DoScreenSpaceGlobalIllumination(
viewPos.xyz, ssgiViewNormal, z0, linearZ0SSGI, dither, giColor
);
if (giHit > 0.0) {
#define SSGI_INTENSITY_SCALE 0.15
float giStrength = RT_GI_STRENGTH * 0.01;
color.rgb += giColor * giStrength * SSGI_INTENSITY_SCALE;
}
#endif
#ifdef PBR_REFLECTIONS
vec3 texture4 = texelFetch(colortex4, texelCoord, 0).rgb;
normalM = mat3(gbufferModelView) * texture4;
float fresnel = clamp(1.0 + dot(normalM, nViewPos), 0.0, 1.0);
#if WORLD_SPACE_REFLECTIONS_INTERNAL == -1
// Way steeper fresnel falloff on SSR-only mode to hide SSR limitation and gain performance
float fresnelFactor = (1.0 - smoothnessD) * 0.7;
fresnelM = max(fresnel - fresnelFactor, 0.0) / (1.0 - fresnelFactor);
#else
fresnelM = fresnel * 0.7 + 0.3;
#endif
fresnelM = mix(pow2(fresnelM), fresnelM * 0.75 + 0.25, intenseFresnel);
fresnelM = fresnelM * sqrt1(smoothnessD) - dither * 0.01;
#endif
#ifdef IRIS_FEATURE_FADE_VARIABLE
chunkFade = texture6.b > 0.50001 ? (1.0 - texture6.b) * 2.0 : 1.0;
#endif
waterRefColor = color.rgb;
DoFog(color, skyFade, lViewPos, playerPos, VdotU, VdotS, dither, false, 0.0);
} else { // Sky
#ifdef DISTANT_HORIZONS
float z0DH = texelFetch(dhDepthTex, texelCoord, 0).r;
if (z0DH < 1.0) { // Distant Horizons Chunks
vec4 screenPosDH = vec4(texCoord, z0DH, 1.0);
vec4 viewPosDH = dhProjectionInverse * (screenPosDH * 2.0 - 1.0);
viewPosDH /= viewPosDH.w;
lViewPos = length(viewPosDH.xyz);
playerPos = ViewToPlayer(viewPosDH.xyz);
waterRefColor = color.rgb;
DoFog(color, skyFade, lViewPos, playerPos, VdotU, VdotS, dither, false, 0.0);
} else { // Start of Actual Sky
#endif
skyFade = 1.0;
#ifdef OVERWORLD
#if AURORA_STYLE > 0
auroraBorealis = GetAuroraBorealis(viewPos.xyz, VdotU, dither);
color.rgb += auroraBorealis;
#endif
#if NIGHT_NEBULAE == 1
nightNebula += GetNightNebula(viewPos.xyz, VdotU, VdotS);
color.rgb += nightNebula;
#endif
#endif
#ifdef NETHER
color.rgb = netherColor * (1.0 - maxBlindnessDarkness);
#ifdef ATM_COLOR_MULTS
color.rgb *= atmColorMult;
#endif
#endif
#ifdef END
color.rgb = endSkyColor;
color.rgb += GetEnderStars(viewPos.xyz, VdotU);
color.rgb *= 1.0 - maxBlindnessDarkness;
#ifdef ATM_COLOR_MULTS
color.rgb *= atmColorMult;
#endif
#endif
#ifdef DISTANT_HORIZONS
} // End of Actual Sky
#endif
}
float cloudLinearDepth = 1.0;
vec4 clouds = vec4(0.0);
#ifdef VL_CLOUDS_ACTIVE
float cloudZCheck = 0.56;
if (z0 > cloudZCheck) {
clouds = GetClouds(cloudLinearDepth, skyFade, vec3(0.0), playerPos,
lViewPos, VdotS, VdotU, dither, auroraBorealis, nightNebula);
color = mix(color, vec4(clouds.rgb, 0.0), clouds.a);
}
#endif
#ifdef SKY_EFFECT_REFLECTION
waterRefColor = mix(waterRefColor, clouds.rgb, clouds.a);
#endif
waterRefColor = sqrt(waterRefColor) * 0.5;
#if defined LIGHTSHAFTS_ACTIVE && (LIGHTSHAFT_BEHAVIOUR == 1 && SHADOW_QUALITY >= 1 || defined END)
if (viewWidth + viewHeight - gl_FragCoord.x - gl_FragCoord.y < 1.5)
cloudLinearDepth = vlFactor;
#endif
#if defined OVERWORLD && defined ATMOSPHERIC_FOG && (defined SPECIAL_BIOME_WEATHER || RAIN_STYLE == 2)
if (isEyeInWater == 0) {
float altitudeFactorRaw = GetAtmFogAltitudeFactor(playerPos.y + cameraPosition.y);
vec3 atmFogColor = GetAtmFogColor(altitudeFactorRaw, VdotS);
#if RAIN_STYLE == 2
float factor = 1.0;
#else
float factor = max(inSnowy, inDry);
#endif
color = mix(color, vec4(atmFogColor, 0.0), 0.5 * rainFactor * factor * sqrt1(skyFade));
}
#endif
/*DRAWBUFFERS:05*/
gl_FragData[0] = vec4(color.rgb, 1.0);
gl_FragData[1] = vec4(waterRefColor, cloudLinearDepth);
// same check as #ifdef PBR_REFLECTIONS but for Optifine to understand:
#if BLOCK_REFLECT_QUALITY >= 2 && RP_MODE >= 1
/*DRAWBUFFERS:054*/
gl_FragData[2] = vec4(mat3(gbufferModelViewInverse) * normalM, sqrt(fresnelM * color.a));
#endif
}
#endif
//////////Vertex Shader//////////Vertex Shader//////////Vertex Shader//////////
#ifdef VERTEX_SHADER
noperspective out vec2 texCoord;
flat out vec3 upVec, sunVec, eastVec;
#if defined LIGHTSHAFTS_ACTIVE && (LIGHTSHAFT_BEHAVIOUR == 1 && SHADOW_QUALITY >= 1 || defined END)
flat out float vlFactor;
#endif
//Attributes//
//Common Variables//
//Common Functions//
//Includes//
//Program//
void main() {
gl_Position = ftransform();
texCoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
upVec = normalize(gbufferModelView[1].xyz);
sunVec = GetSunVector();
eastVec = normalize(gbufferModelView[0].xyz);
#if defined LIGHTSHAFTS_ACTIVE && (LIGHTSHAFT_BEHAVIOUR == 1 && SHADOW_QUALITY >= 1 || defined END)
vlFactor = texelFetch(colortex5, ivec2(viewWidth-1, viewHeight-1), 0).a;
#ifdef END
if (frameCounter % int(0.06666 / frameTimeSmooth + 0.5) == 0) { // Change speed is not too different above 10 fps
#if MC_VERSION >= 12106
bool isEnderDragonDead = !heavyFog;
#else
vec2 absCamPosXZ = abs(cameraPosition.xz);
float maxCamPosXZ = max(absCamPosXZ.x, absCamPosXZ.y);
bool isEnderDragonDead = gl_Fog.start / far > 0.5 || maxCamPosXZ > 350.0;
#endif
if (isEnderDragonDead) vlFactor = max(vlFactor - OSIEBCA*2, 0.0);
else vlFactor = min(vlFactor + OSIEBCA*2, 1.0);
}
#endif
#endif
}
#endif