Debian Codegraph @

servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl

raw
1 // Functions related to lighting
2
3 #extension GL_EXT_control_flow_attributes : require
4
5 // This annotation macro must be placed before any loops that rely on specialization constants as their upper bound.
6 // Drivers may choose to unroll these loops based on the possible range of the value that can be deduced from the
7 // spec constant, which can lead to their code generation taking a much longer time than desired.
8 #ifdef UBERSHADER
9 // Prefer to not unroll loops on the ubershader to reduce code size as much as possible.
10 #define SPEC_CONSTANT_LOOP_ANNOTATION [[dont_unroll]]
11 #else
12 // Don't make an explicit hint on specialized shaders.
13 #define SPEC_CONSTANT_LOOP_ANNOTATION
14 #endif
15
16 half D_GGX(half NoH, half roughness, hvec3 n, hvec3 h) {
17 half a = NoH * roughness;
18 #ifdef EXPLICIT_FP16
19 hvec3 NxH = cross(n, h);
20 half k = roughness / (dot(NxH, NxH) + a * a);
21 #else
22 float k = roughness / (1.0 - NoH * NoH + a * a);
23 #endif
24 half d = k * k * half(1.0 / M_PI);
25 return saturateHalf(d);
26 }
27
28 // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
29 half V_GGX(half NdotL, half NdotV, half alpha) {
30 half v = half(0.5) / mix(half(2.0) * NdotL * NdotV, NdotL + NdotV, alpha);
31 return saturateHalf(v);
32 }
33
34 half D_GGX_anisotropic(half cos_theta_m, half alpha_x, half alpha_y, half cos_phi, half sin_phi) {
35 half alpha2 = alpha_x * alpha_y;
36 vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m);
37 float v2 = dot(v, v);
38 half w2 = half(float(alpha2) / v2);
39 return alpha2 * w2 * w2 * half(1.0 / M_PI);
40 }
41
42 half V_GGX_anisotropic(half alpha_x, half alpha_y, half TdotV, half TdotL, half BdotV, half BdotL, half NdotV, half NdotL) {
43 half Lambda_V = NdotL * length(hvec3(alpha_x * TdotV, alpha_y * BdotV, NdotV));
44 half Lambda_L = NdotV * length(hvec3(alpha_x * TdotL, alpha_y * BdotL, NdotL));
45 half v = half(0.5) / (Lambda_V + Lambda_L);
46 return saturateHalf(v);
47 }
48
49 half SchlickFresnel(half u) {
50 half m = half(1.0) - u;
51 half m2 = m * m;
52 return m2 * m2 * m; // pow(m,5)
53 }
54
55 hvec3 F0(half metallic, half specular, hvec3 albedo) {
56 half dielectric = half(0.16) * specular * specular;
57 // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials;
58 // see https://google.github.io/filament/Filament.md.html
59 return mix(hvec3(dielectric), albedo, hvec3(metallic));
60 }
61
62 void light_compute(hvec3 N, hvec3 L, hvec3 V, half A, hvec3 light_color, bool is_directional, half attenuation, hvec3 f0, half roughness, half metallic, half specular_amount, hvec3 albedo, inout half alpha, vec2 screen_uv, hvec3 energy_compensation,
63 #ifdef LIGHT_BACKLIGHT_USED
64 hvec3 backlight,
65 #endif
66 #ifdef LIGHT_TRANSMITTANCE_USED
67 hvec4 transmittance_color,
68 half transmittance_depth,
69 half transmittance_boost,
70 half transmittance_z,
71 #endif
72 #ifdef LIGHT_RIM_USED
73 half rim, half rim_tint,
74 #endif
75 #ifdef LIGHT_CLEARCOAT_USED
76 half clearcoat, half clearcoat_roughness, hvec3 vertex_normal,
77 #endif
78 #ifdef LIGHT_ANISOTROPY_USED
79 hvec3 B, hvec3 T, half anisotropy,
80 #endif
81 inout hvec3 diffuse_light, inout hvec3 specular_light) {
82 #if defined(LIGHT_CODE_USED)
83 // Light is written by the user shader.
84 mat4 inv_view_matrix = transpose(mat4(scene_data_block.data.inv_view_matrix[0],
85 scene_data_block.data.inv_view_matrix[1],
86 scene_data_block.data.inv_view_matrix[2],
87 vec4(0.0, 0.0, 0.0, 1.0)));
88 mat4 read_view_matrix = transpose(mat4(scene_data_block.data.view_matrix[0],
89 scene_data_block.data.view_matrix[1],
90 scene_data_block.data.view_matrix[2],
91 vec4(0.0, 0.0, 0.0, 1.0)));
92
93 #ifdef USING_MOBILE_RENDERER
94 uint instance_index = draw_call.instance_index;
95 #else
96 uint instance_index = instance_index_interp;
97 #endif
98
99 mat4 read_model_matrix = transpose(mat4(instances.data[instance_index].transform[0],
100 instances.data[instance_index].transform[1],
101 instances.data[instance_index].transform[2],
102 vec4(0.0, 0.0, 0.0, 1.0)));
103
104 #undef projection_matrix
105 #define projection_matrix scene_data_block.data.projection_matrix
106 #undef inv_projection_matrix
107 #define inv_projection_matrix scene_data_block.data.inv_projection_matrix
108
109 vec2 read_viewport_size = scene_data_block.data.viewport_size;
110
111 #ifdef LIGHT_BACKLIGHT_USED
112 vec3 backlight_highp = vec3(backlight);
113 #endif
114 float roughness_highp = float(roughness);
115 float metallic_highp = float(metallic);
116 vec3 albedo_highp = vec3(albedo);
117 float alpha_highp = float(alpha);
118 vec3 normal_highp = vec3(N);
119 vec3 light_highp = vec3(L);
120 vec3 view_highp = vec3(V);
121 float specular_amount_highp = float(specular_amount);
122 vec3 light_color_highp = vec3(light_color);
123 float attenuation_highp = float(attenuation);
124 vec3 diffuse_light_highp = vec3(diffuse_light);
125 vec3 specular_light_highp = vec3(specular_light);
126
127 #CODE : LIGHT
128
129 alpha = half(alpha_highp);
130 diffuse_light = hvec3(diffuse_light_highp);
131 specular_light = hvec3(specular_light_highp);
132 #else // !LIGHT_CODE_USED
133 half NdotL = min(A + dot(N, L), half(1.0));
134 half cNdotV = max(dot(N, V), half(1e-4));
135
136 #ifdef LIGHT_TRANSMITTANCE_USED
137 {
138 #ifdef SSS_MODE_SKIN
139 half scale = half(8.25) / transmittance_depth;
140 half d = scale * abs(transmittance_z);
141 float dd = float(-d * d);
142 hvec3 profile = hvec3(vec3(0.233, 0.455, 0.649) * exp(dd / 0.0064) +
143 vec3(0.1, 0.336, 0.344) * exp(dd / 0.0484) +
144 vec3(0.118, 0.198, 0.0) * exp(dd / 0.187) +
145 vec3(0.113, 0.007, 0.007) * exp(dd / 0.567) +
146 vec3(0.358, 0.004, 0.0) * exp(dd / 1.99) +
147 vec3(0.078, 0.0, 0.0) * exp(dd / 7.41));
148
149 diffuse_light += profile * transmittance_color.a * light_color * clamp(transmittance_boost - NdotL, half(0.0), half(1.0)) * half(1.0 / M_PI);
150 #else
151
152 half scale = half(8.25) / transmittance_depth;
153 half d = scale * abs(transmittance_z);
154 half dd = -d * d;
155 diffuse_light += exp(dd) * transmittance_color.rgb * transmittance_color.a * light_color * clamp(transmittance_boost - NdotL, half(0.0), half(1.0)) * half(1.0 / M_PI);
156 #endif
157 }
158 #endif //LIGHT_TRANSMITTANCE_USED
159
160 #if defined(LIGHT_RIM_USED)
161 // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior.
162 half rim_light = pow(max(half(1e-4), half(1.0) - cNdotV), max(half(0.0), (half(1.0) - roughness) * half(16.0)));
163 diffuse_light += rim_light * rim * mix(hvec3(1.0), albedo, rim_tint) * light_color;
164 #endif
165
166 // We skip checking on attenuation on directional lights to avoid a branch that is not as beneficial for directional lights as the other ones.
167 if (is_directional || attenuation > HALF_FLT_MIN) {
168 half cNdotL = max(NdotL, half(0.0));
169 #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED)
170 hvec3 H = normalize(V + L);
171 half cLdotH = clamp(A + dot(L, H), half(0.0), half(1.0));
172 #endif
173 #if defined(LIGHT_CLEARCOAT_USED)
174 // Clearcoat ignores normal_map, use vertex normal instead
175 half ccNdotL = clamp(A + dot(vertex_normal, L), half(0.0), half(1.0));
176 half ccNdotH = clamp(A + dot(vertex_normal, H), half(0.0), half(1.0));
177 half ccNdotV = max(dot(vertex_normal, V), half(1e-4));
178 half cLdotH5 = SchlickFresnel(cLdotH);
179
180 half Dr = D_GGX(ccNdotH, half(mix(half(0.001), half(0.1), clearcoat_roughness)), vertex_normal, H);
181 half Gr = half(0.25) / (cLdotH * cLdotH + half(1e-4));
182 half Fr = mix(half(0.04), half(1.0), cLdotH5);
183 half clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL;
184
185 specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount;
186
187 // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR)
188 // but to do so we need to rearrange this entire function
189 #endif // LIGHT_CLEARCOAT_USED
190
191 if (metallic < half(1.0)) {
192 half diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
193
194 #if defined(DIFFUSE_LAMBERT_WRAP)
195 // Energy conserving lambert wrap shader.
196 // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
197 half op_roughness = half(1.0) + roughness;
198 diffuse_brdf_NL = max(half(0.0), (NdotL + roughness) / (op_roughness * op_roughness)) * half(1.0 / M_PI);
199 #elif defined(DIFFUSE_TOON)
200
201 diffuse_brdf_NL = smoothstep(-roughness, max(roughness, half(0.01)), NdotL) * half(1.0 / M_PI);
202
203 #elif defined(DIFFUSE_BURLEY)
204 {
205 half FD90_minus_1 = half(2.0) * cLdotH * cLdotH * roughness - half(0.5);
206 half FdV = half(1.0) + FD90_minus_1 * SchlickFresnel(cNdotV);
207 half FdL = half(1.0) + FD90_minus_1 * SchlickFresnel(cNdotL);
208 diffuse_brdf_NL = half(1.0 / M_PI) * FdV * FdL * cNdotL;
209 }
210 #else
211 // lambert
212 diffuse_brdf_NL = cNdotL * half(1.0 / M_PI);
213 #endif
214
215 diffuse_light += light_color * diffuse_brdf_NL * attenuation;
216
217 #if defined(LIGHT_BACKLIGHT_USED)
218 diffuse_light += light_color * (hvec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation;
219 #endif
220 }
221
222 if (roughness > half(0.0)) {
223 #if defined(SPECULAR_SCHLICK_GGX)
224 half cNdotH = clamp(A + dot(N, H), half(0.0), half(1.0));
225 #endif
226 // Apply specular light.
227 // FIXME: roughness == 0 should not disable specular light entirely
228 #if defined(SPECULAR_TOON)
229 hvec3 R = normalize(-reflect(L, N));
230 half RdotV = dot(R, V);
231 half mid = half(1.0) - roughness;
232 mid *= mid;
233 half intensity = smoothstep(mid - roughness * half(0.5), mid + roughness * half(0.5), RdotV) * mid;
234 diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection
235
236 #elif defined(SPECULAR_DISABLED)
237 // Do nothing.
238
239 #elif defined(SPECULAR_SCHLICK_GGX)
240 // shlick+ggx as default
241 half alpha_ggx = roughness * roughness;
242 #if defined(LIGHT_ANISOTROPY_USED)
243 half aspect = sqrt(half(1.0) - anisotropy * half(0.9));
244 half ax = alpha_ggx / aspect;
245 half ay = alpha_ggx * aspect;
246 half XdotH = dot(T, H);
247 half YdotH = dot(B, H);
248 half D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH);
249 half G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL);
250 #else // LIGHT_ANISOTROPY_USED
251 half D = D_GGX(cNdotH, alpha_ggx, N, H);
252 half G = V_GGX(cNdotL, cNdotV, alpha_ggx);
253 #endif // LIGHT_ANISOTROPY_USED
254 // F
255 #if !defined(LIGHT_CLEARCOAT_USED)
256 half cLdotH5 = SchlickFresnel(cLdotH);
257 #endif
258 // Calculate Fresnel using specular occlusion term from Filament:
259 // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion
260 half f90 = clamp(dot(f0, hvec3(50.0 * 0.33)), metallic, half(1.0));
261 hvec3 F = f0 + (f90 - f0) * cLdotH5;
262 hvec3 specular_brdf_NL = energy_compensation * cNdotL * D * F * G;
263 specular_light += specular_brdf_NL * light_color * attenuation * specular_amount;
264 #endif
265 }
266
267 #ifdef USE_SHADOW_TO_OPACITY
268 alpha = min(alpha, clamp(half(1.0 - attenuation), half(0.0), half(1.0)));
269 #endif
270 }
271 #endif // LIGHT_CODE_USED
272 }
273
274 #ifndef SHADOWS_DISABLED
275
276 // Interleaved Gradient Noise
277 // https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
278 float quick_hash(vec2 pos) {
279 const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);
280 return fract(magic.z * fract(dot(pos, magic.xy)));
281 }
282
283 half sample_directional_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, vec4 coord, float taa_frame_count) {
284 vec2 pos = coord.xy;
285 float depth = coord.z;
286
287 //if only one sample is taken, take it from the center
288 if (sc_directional_soft_shadow_samples() == 0) {
289 return half(textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0)));
290 }
291
292 mat2 disk_rotation;
293 {
294 float r = quick_hash(gl_FragCoord.xy + vec2(taa_frame_count * 5.588238)) * 2.0 * M_PI;
295 float sr = sin(r);
296 float cr = cos(r);
297 disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
298 }
299
300 float avg = 0.0;
301
302 SPEC_CONSTANT_LOOP_ANNOTATION
303 for (uint i = 0; i < sc_directional_soft_shadow_samples(); i++) {
304 avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data_block.data.directional_soft_shadow_kernel[i].xy), depth, 1.0));
305 }
306
307 return half(avg * (1.0 / float(sc_directional_soft_shadow_samples())));
308 }
309
310 half sample_pcf_shadow(texture2D shadow, vec2 shadow_pixel_size, vec3 coord, float taa_frame_count) {
311 vec2 pos = coord.xy;
312 float depth = coord.z;
313
314 //if only one sample is taken, take it from the center
315 if (sc_soft_shadow_samples() == 0) {
316 return half(textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0)));
317 }
318
319 mat2 disk_rotation;
320 {
321 float r = quick_hash(gl_FragCoord.xy + vec2(taa_frame_count * 5.588238)) * 2.0 * M_PI;
322 float sr = sin(r);
323 float cr = cos(r);
324 disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
325 }
326
327 float avg = 0.0;
328
329 SPEC_CONSTANT_LOOP_ANNOTATION
330 for (uint i = 0; i < sc_soft_shadow_samples(); i++) {
331 avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos + shadow_pixel_size * (disk_rotation * scene_data_block.data.soft_shadow_kernel[i].xy), depth, 1.0));
332 }
333
334 return half(avg * (1.0 / float(sc_soft_shadow_samples())));
335 }
336
337 half sample_omni_pcf_shadow(texture2D shadow, float blur_scale, vec2 coord, vec4 uv_rect, vec2 flip_offset, float depth, float taa_frame_count) {
338 //if only one sample is taken, take it from the center
339 if (sc_soft_shadow_samples() == 0) {
340 vec2 pos = coord * 0.5 + 0.5;
341 pos = uv_rect.xy + pos * uv_rect.zw;
342 return half(textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(pos, depth, 1.0)));
343 }
344
345 mat2 disk_rotation;
346 {
347 float r = quick_hash(gl_FragCoord.xy + vec2(taa_frame_count * 5.588238)) * 2.0 * M_PI;
348 float sr = sin(r);
349 float cr = cos(r);
350 disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
351 }
352
353 float avg = 0.0;
354 vec2 offset_scale = blur_scale * 2.0 * scene_data_block.data.shadow_atlas_pixel_size / uv_rect.zw;
355
356 SPEC_CONSTANT_LOOP_ANNOTATION
357 for (uint i = 0; i < sc_soft_shadow_samples(); i++) {
358 vec2 offset = offset_scale * (disk_rotation * scene_data_block.data.soft_shadow_kernel[i].xy);
359 vec2 sample_coord = coord + offset;
360
361 float sample_coord_length_squared = dot(sample_coord, sample_coord);
362 bool do_flip = sample_coord_length_squared > 1.0;
363
364 if (do_flip) {
365 float len = sqrt(sample_coord_length_squared);
366 sample_coord = sample_coord * (2.0 / len - 1.0);
367 }
368
369 sample_coord = sample_coord * 0.5 + 0.5;
370 sample_coord = uv_rect.xy + sample_coord * uv_rect.zw;
371
372 if (do_flip) {
373 sample_coord += flip_offset;
374 }
375 avg += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(sample_coord, depth, 1.0));
376 }
377
378 return half(avg * (1.0 / float(sc_soft_shadow_samples())));
379 }
380
381 half sample_directional_soft_shadow(texture2D shadow, vec3 pssm_coord, vec2 tex_scale, float taa_frame_count) {
382 //find blocker
383 float blocker_count = 0.0;
384 float blocker_average = 0.0;
385
386 mat2 disk_rotation;
387 {
388 float r = quick_hash(gl_FragCoord.xy + vec2(taa_frame_count * 5.588238)) * 2.0 * M_PI;
389 float sr = sin(r);
390 float cr = cos(r);
391 disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
392 }
393
394 SPEC_CONSTANT_LOOP_ANNOTATION
395 for (uint i = 0; i < sc_directional_penumbra_shadow_samples(); i++) {
396 vec2 suv = pssm_coord.xy + (disk_rotation * scene_data_block.data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
397 float d = textureLod(sampler2D(shadow, SAMPLER_LINEAR_CLAMP), suv, 0.0).r;
398 if (d > pssm_coord.z) {
399 blocker_average += d;
400 blocker_count += 1.0;
401 }
402 }
403
404 if (blocker_count > 0.0) {
405 //blockers found, do soft shadow
406 blocker_average /= blocker_count;
407 float penumbra = (-pssm_coord.z + blocker_average) / (1.0 - blocker_average);
408 tex_scale *= penumbra;
409
410 float s = 0.0;
411
412 SPEC_CONSTANT_LOOP_ANNOTATION
413 for (uint i = 0; i < sc_directional_penumbra_shadow_samples(); i++) {
414 vec2 suv = pssm_coord.xy + (disk_rotation * scene_data_block.data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
415 s += textureProj(sampler2DShadow(shadow, shadow_sampler), vec4(suv, pssm_coord.z, 1.0));
416 }
417
418 return half(s / float(sc_directional_penumbra_shadow_samples()));
419
420 } else {
421 //no blockers found, so no shadow
422 return half(1.0);
423 }
424 }
425
426 #endif // SHADOWS_DISABLED
427
428 half get_omni_attenuation(float distance, float inv_range, float decay) {
429 float nd = distance * inv_range;
430 nd *= nd;
431 nd *= nd; // nd^4
432 nd = max(1.0 - nd, 0.0);
433 nd *= nd; // nd^2
434 return half(nd * pow(max(distance, 0.0001), -decay));
435 }
436
437 void light_process_omni(uint idx, vec3 vertex, hvec3 eye_vec, hvec3 normal, vec3 vertex_ddx, vec3 vertex_ddy, hvec3 f0, half roughness, half metallic, float taa_frame_count, hvec3 albedo, inout half alpha, vec2 screen_uv, hvec3 energy_compensation,
438 #ifdef LIGHT_BACKLIGHT_USED
439 hvec3 backlight,
440 #endif
441 #ifdef LIGHT_TRANSMITTANCE_USED
442 hvec4 transmittance_color,
443 half transmittance_depth,
444 half transmittance_boost,
445 #endif
446 #ifdef LIGHT_RIM_USED
447 half rim, half rim_tint,
448 #endif
449 #ifdef LIGHT_CLEARCOAT_USED
450 half clearcoat, half clearcoat_roughness, hvec3 vertex_normal,
451 #endif
452 #ifdef LIGHT_ANISOTROPY_USED
453 hvec3 binormal, hvec3 tangent, half anisotropy,
454 #endif
455 inout hvec3 diffuse_light, inout hvec3 specular_light) {
456
457 // Omni light attenuation.
458 vec3 light_rel_vec = omni_lights.data[idx].position - vertex;
459 float light_length = length(light_rel_vec);
460 half omni_attenuation = get_omni_attenuation(light_length, omni_lights.data[idx].inv_radius, omni_lights.data[idx].attenuation);
461
462 // Compute size.
463 half size = half(0.0);
464 if (sc_use_light_soft_shadows() && omni_lights.data[idx].size > 0.0) {
465 half t = half(omni_lights.data[idx].size / max(0.001, light_length));
466 size = half(1.0) / sqrt(half(1.0) + t * t);
467 size = max(half(1.0) - size, half(0.0));
468 }
469
470 half shadow = half(1.0);
471 #ifndef SHADOWS_DISABLED
472 // Omni light shadow.
473 if (omni_attenuation > HALF_FLT_MIN && omni_lights.data[idx].shadow_opacity > 0.001) {
474 // there is a shadowmap
475 vec2 texel_size = scene_data_block.data.shadow_atlas_pixel_size;
476 vec4 base_uv_rect = omni_lights.data[idx].atlas_rect;
477 base_uv_rect.xy += texel_size;
478 base_uv_rect.zw -= texel_size * 2.0;
479
480 // Omni lights use direction.xy to store to store the offset between the two paraboloid regions
481 vec2 flip_offset = omni_lights.data[idx].direction.xy;
482
483 vec3 local_vert = (omni_lights.data[idx].shadow_matrix * vec4(vertex, 1.0)).xyz;
484
485 float shadow_len = length(local_vert); //need to remember shadow len from here
486 vec3 shadow_dir = normalize(local_vert);
487
488 vec3 local_normal = normalize(mat3(omni_lights.data[idx].shadow_matrix) * vec3(normal));
489 vec3 normal_bias = local_normal * omni_lights.data[idx].shadow_normal_bias * (1.0 - abs(dot(local_normal, shadow_dir)));
490
491 if (sc_use_light_soft_shadows() && omni_lights.data[idx].soft_shadow_size > 0.0) {
492 //soft shadow
493
494 //find blocker
495
496 float blocker_count = 0.0;
497 float blocker_average = 0.0;
498
499 mat2 disk_rotation;
500 {
501 float r = quick_hash(gl_FragCoord.xy + vec2(taa_frame_count * 5.588238)) * 2.0 * M_PI;
502 float sr = sin(r);
503 float cr = cos(r);
504 disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
505 }
506
507 vec3 basis_normal = shadow_dir;
508 vec3 v0 = abs(basis_normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
509 vec3 tangent = normalize(cross(v0, basis_normal));
510 vec3 bitangent = normalize(cross(tangent, basis_normal));
511 float z_norm = 1.0 - shadow_len * omni_lights.data[idx].inv_radius;
512
513 tangent *= omni_lights.data[idx].soft_shadow_size * omni_lights.data[idx].soft_shadow_scale;
514 bitangent *= omni_lights.data[idx].soft_shadow_size * omni_lights.data[idx].soft_shadow_scale;
515
516 SPEC_CONSTANT_LOOP_ANNOTATION
517 for (uint i = 0; i < sc_penumbra_shadow_samples(); i++) {
518 vec2 disk = disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy;
519
520 vec3 pos = local_vert + tangent * disk.x + bitangent * disk.y;
521
522 pos = normalize(pos);
523
524 vec4 uv_rect = base_uv_rect;
525
526 if (pos.z >= 0.0) {
527 uv_rect.xy += flip_offset;
528 }
529
530 pos.z = 1.0 + abs(pos.z);
531 pos.xy /= pos.z;
532
533 pos.xy = pos.xy * 0.5 + 0.5;
534 pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
535
536 float d = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), pos.xy, 0.0).r;
537 if (d > z_norm) {
538 blocker_average += d;
539 blocker_count += 1.0;
540 }
541 }
542
543 if (blocker_count > 0.0) {
544 //blockers found, do soft shadow
545 blocker_average /= blocker_count;
546 float penumbra = (-z_norm + blocker_average) / (1.0 - blocker_average);
547 tangent *= penumbra;
548 bitangent *= penumbra;
549
550 z_norm += omni_lights.data[idx].inv_radius * omni_lights.data[idx].shadow_bias;
551
552 shadow = half(0.0);
553
554 SPEC_CONSTANT_LOOP_ANNOTATION
555 for (uint i = 0; i < sc_penumbra_shadow_samples(); i++) {
556 vec2 disk = disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy;
557 vec3 pos = local_vert + tangent * disk.x + bitangent * disk.y;
558
559 pos = normalize(pos);
560 pos = normalize(pos + normal_bias);
561
562 vec4 uv_rect = base_uv_rect;
563
564 if (pos.z >= 0.0) {
565 uv_rect.xy += flip_offset;
566 }
567
568 pos.z = 1.0 + abs(pos.z);
569 pos.xy /= pos.z;
570
571 pos.xy = pos.xy * 0.5 + 0.5;
572 pos.xy = uv_rect.xy + pos.xy * uv_rect.zw;
573 shadow += half(textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(pos.xy, z_norm, 1.0)));
574 }
575
576 shadow /= half(sc_penumbra_shadow_samples());
577 shadow = mix(half(1.0), shadow, half(omni_lights.data[idx].shadow_opacity));
578
579 } else {
580 //no blockers found, so no shadow
581 shadow = half(1.0);
582 }
583 } else {
584 vec4 uv_rect = base_uv_rect;
585
586 vec3 shadow_sample = normalize(shadow_dir + normal_bias);
587 if (shadow_sample.z >= 0.0) {
588 uv_rect.xy += flip_offset;
589 flip_offset *= -1.0;
590 }
591
592 shadow_sample.z = 1.0 + abs(shadow_sample.z);
593 vec2 pos = shadow_sample.xy / shadow_sample.z;
594 float depth = shadow_len - omni_lights.data[idx].shadow_bias;
595 depth *= omni_lights.data[idx].inv_radius;
596 depth = 1.0 - depth;
597 shadow = mix(half(1.0), sample_omni_pcf_shadow(shadow_atlas, omni_lights.data[idx].soft_shadow_scale / shadow_sample.z, pos, uv_rect, flip_offset, depth, taa_frame_count), half(omni_lights.data[idx].shadow_opacity));
598 }
599 }
600 #endif
601
602 vec3 color = omni_lights.data[idx].color;
603
604 #ifdef LIGHT_TRANSMITTANCE_USED
605 half transmittance_z = transmittance_depth; //no transmittance by default
606 transmittance_color.a *= omni_attenuation;
607 #ifndef SHADOWS_DISABLED
608 if (omni_lights.data[idx].shadow_opacity > 0.001) {
609 // Redo shadowmapping, but shrink the model a bit to avoid artifacts.
610 vec2 texel_size = scene_data_block.data.shadow_atlas_pixel_size;
611 vec4 uv_rect = omni_lights.data[idx].atlas_rect;
612 uv_rect.xy += texel_size;
613 uv_rect.zw -= texel_size * 2.0;
614
615 // Omni lights use direction.xy to store to store the offset between the two paraboloid regions
616 vec2 flip_offset = omni_lights.data[idx].direction.xy;
617
618 vec3 local_vert = (omni_lights.data[idx].shadow_matrix * vec4(vertex - normal * omni_lights.data[idx].transmittance_bias, 1.0)).xyz;
619
620 float shadow_len = length(local_vert); //need to remember shadow len from here
621 vec3 shadow_sample = normalize(local_vert);
622
623 if (shadow_sample.z >= 0.0) {
624 uv_rect.xy += flip_offset;
625 flip_offset *= -1.0;
626 }
627
628 shadow_sample.z = 1.0 + abs(shadow_sample.z);
629 vec2 pos = shadow_sample.xy / shadow_sample.z;
630 float depth = shadow_len * omni_lights.data[idx].inv_radius;
631 depth = 1.0 - depth;
632
633 pos = pos * 0.5 + 0.5;
634 pos = uv_rect.xy + pos * uv_rect.zw;
635 float shadow_z = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), pos, 0.0).r;
636 transmittance_z = half((depth - shadow_z) / omni_lights.data[idx].inv_radius);
637 }
638 #endif // !SHADOWS_DISABLED
639 #endif // LIGHT_TRANSMITTANCE_USED
640
641 if (sc_use_light_projector() && omni_lights.data[idx].projector_rect != vec4(0.0)) {
642 vec3 local_v = (omni_lights.data[idx].shadow_matrix * vec4(vertex, 1.0)).xyz;
643 local_v = normalize(local_v);
644
645 vec4 atlas_rect = omni_lights.data[idx].projector_rect;
646
647 if (local_v.z >= 0.0) {
648 atlas_rect.y += atlas_rect.w;
649 }
650
651 local_v.z = 1.0 + abs(local_v.z);
652
653 local_v.xy /= local_v.z;
654 local_v.xy = local_v.xy * 0.5 + 0.5;
655 vec2 proj_uv = local_v.xy * atlas_rect.zw;
656
657 if (sc_projector_use_mipmaps()) {
658 vec2 proj_uv_ddx;
659 vec2 proj_uv_ddy;
660 {
661 vec3 local_v_ddx = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddx, 1.0)).xyz;
662 local_v_ddx = normalize(local_v_ddx);
663
664 if (local_v_ddx.z >= 0.0) {
665 local_v_ddx.z += 1.0;
666 } else {
667 local_v_ddx.z = 1.0 - local_v_ddx.z;
668 }
669
670 local_v_ddx.xy /= local_v_ddx.z;
671 local_v_ddx.xy = local_v_ddx.xy * 0.5 + 0.5;
672
673 proj_uv_ddx = local_v_ddx.xy * atlas_rect.zw - proj_uv;
674
675 vec3 local_v_ddy = (omni_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddy, 1.0)).xyz;
676 local_v_ddy = normalize(local_v_ddy);
677
678 if (local_v_ddy.z >= 0.0) {
679 local_v_ddy.z += 1.0;
680 } else {
681 local_v_ddy.z = 1.0 - local_v_ddy.z;
682 }
683
684 local_v_ddy.xy /= local_v_ddy.z;
685 local_v_ddy.xy = local_v_ddy.xy * 0.5 + 0.5;
686
687 proj_uv_ddy = local_v_ddy.xy * atlas_rect.zw - proj_uv;
688 }
689
690 vec4 proj = textureGrad(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + atlas_rect.xy, proj_uv_ddx, proj_uv_ddy);
691 color *= proj.rgb * proj.a;
692 } else {
693 vec4 proj = textureLod(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + atlas_rect.xy, 0.0);
694 color *= proj.rgb * proj.a;
695 }
696 }
697
698 vec3 light_rel_vec_norm = light_rel_vec / light_length;
699 light_compute(normal, hvec3(light_rel_vec_norm), eye_vec, size, hvec3(color), false, omni_attenuation * shadow, f0, roughness, metallic, half(omni_lights.data[idx].specular_amount), albedo, alpha, screen_uv, energy_compensation,
700 #ifdef LIGHT_BACKLIGHT_USED
701 backlight,
702 #endif
703 #ifdef LIGHT_TRANSMITTANCE_USED
704 transmittance_color,
705 transmittance_depth,
706 transmittance_boost,
707 transmittance_z,
708 #endif
709 #ifdef LIGHT_RIM_USED
710 rim * omni_attenuation, rim_tint,
711 #endif
712 #ifdef LIGHT_CLEARCOAT_USED
713 clearcoat, clearcoat_roughness, vertex_normal,
714 #endif
715 #ifdef LIGHT_ANISOTROPY_USED
716 binormal, tangent, anisotropy,
717 #endif
718 diffuse_light,
719 specular_light);
720 }
721
722 vec2 normal_to_panorama(vec3 n) {
723 n = normalize(n);
724 vec2 panorama_coords = vec2(atan(n.x, n.z), acos(-n.y));
725
726 if (panorama_coords.x < 0.0) {
727 panorama_coords.x += M_PI * 2.0;
728 }
729
730 panorama_coords /= vec2(M_PI * 2.0, M_PI);
731 return panorama_coords;
732 }
733
734 void light_process_spot(uint idx, vec3 vertex, hvec3 eye_vec, hvec3 normal, vec3 vertex_ddx, vec3 vertex_ddy, hvec3 f0, half roughness, half metallic, float taa_frame_count, hvec3 albedo, inout half alpha, vec2 screen_uv, hvec3 energy_compensation,
735 #ifdef LIGHT_BACKLIGHT_USED
736 hvec3 backlight,
737 #endif
738 #ifdef LIGHT_TRANSMITTANCE_USED
739 hvec4 transmittance_color,
740 half transmittance_depth,
741 half transmittance_boost,
742 #endif
743 #ifdef LIGHT_RIM_USED
744 half rim, half rim_tint,
745 #endif
746 #ifdef LIGHT_CLEARCOAT_USED
747 half clearcoat, half clearcoat_roughness, hvec3 vertex_normal,
748 #endif
749 #ifdef LIGHT_ANISOTROPY_USED
750 hvec3 binormal, hvec3 tangent, half anisotropy,
751 #endif
752 inout hvec3 diffuse_light,
753 inout hvec3 specular_light) {
754
755 // Spot light attenuation.
756 vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
757 float light_length = length(light_rel_vec);
758 hvec3 light_rel_vec_norm = hvec3(light_rel_vec / light_length);
759 half spot_attenuation = get_omni_attenuation(light_length, spot_lights.data[idx].inv_radius, spot_lights.data[idx].attenuation);
760 vec3 spot_dir = spot_lights.data[idx].direction;
761 float cone_angle = spot_lights.data[idx].cone_angle;
762 float scos = max(dot(-vec3(light_rel_vec_norm), spot_dir), cone_angle);
763
764 // This conversion to a highp float is crucial to prevent light leaking due to precision errors.
765 float spot_rim = max(1e-4, (1.0 - scos) / (1.0 - cone_angle));
766 spot_attenuation *= half(1.0 - pow(spot_rim, spot_lights.data[idx].cone_attenuation));
767
768 // Compute size.
769 half size = half(0.0);
770 if (sc_use_light_soft_shadows() && spot_lights.data[idx].size > 0.0) {
771 half t = half(spot_lights.data[idx].size / max(0.001, light_length));
772 size = half(1.0) / sqrt(half(1.0) + t * t);
773 size = max(half(1.0) - size, half(0.0));
774 }
775
776 half shadow = half(1.0);
777 #ifndef SHADOWS_DISABLED
778 // Spot light shadow.
779 if (spot_attenuation > HALF_FLT_MIN && spot_lights.data[idx].shadow_opacity > 0.001) {
780 vec3 normal_bias = vec3(normal) * light_length * spot_lights.data[idx].shadow_normal_bias * (1.0 - abs(dot(normal, light_rel_vec_norm)));
781
782 //there is a shadowmap
783 vec4 v = vec4(vertex + normal_bias, 1.0);
784
785 vec4 splane = (spot_lights.data[idx].shadow_matrix * v);
786 splane.z += spot_lights.data[idx].shadow_bias;
787 splane /= splane.w;
788
789 if (sc_use_light_soft_shadows() && spot_lights.data[idx].soft_shadow_size > 0.0) {
790 //soft shadow
791
792 //find blocker
793 float z_norm = dot(spot_dir, -light_rel_vec) * spot_lights.data[idx].inv_radius;
794
795 vec2 shadow_uv = splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy;
796
797 float blocker_count = 0.0;
798 float blocker_average = 0.0;
799
800 mat2 disk_rotation;
801 {
802 float r = quick_hash(gl_FragCoord.xy + vec2(taa_frame_count * 5.588238)) * 2.0 * M_PI;
803 float sr = sin(r);
804 float cr = cos(r);
805 disk_rotation = mat2(vec2(cr, -sr), vec2(sr, cr));
806 }
807
808 float uv_size = spot_lights.data[idx].soft_shadow_size * z_norm * spot_lights.data[idx].soft_shadow_scale;
809 vec2 clamp_max = spot_lights.data[idx].atlas_rect.xy + spot_lights.data[idx].atlas_rect.zw;
810
811 SPEC_CONSTANT_LOOP_ANNOTATION
812 for (uint i = 0; i < sc_penumbra_shadow_samples(); i++) {
813 vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
814 suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
815 float d = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), suv, 0.0).r;
816 if (d > splane.z) {
817 blocker_average += d;
818 blocker_count += 1.0;
819 }
820 }
821
822 if (blocker_count > 0.0) {
823 //blockers found, do soft shadow
824 blocker_average /= blocker_count;
825 float penumbra = (-z_norm + blocker_average) / (1.0 - blocker_average);
826 uv_size *= penumbra;
827
828 shadow = half(0.0);
829
830 SPEC_CONSTANT_LOOP_ANNOTATION
831 for (uint i = 0; i < sc_penumbra_shadow_samples(); i++) {
832 vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
833 suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
834 shadow += half(textureProj(sampler2DShadow(shadow_atlas, shadow_sampler), vec4(suv, splane.z, 1.0)));
835 }
836
837 shadow /= half(sc_penumbra_shadow_samples());
838 shadow = mix(half(1.0), shadow, half(spot_lights.data[idx].shadow_opacity));
839
840 } else {
841 //no blockers found, so no shadow
842 shadow = half(1.0);
843 }
844 } else {
845 //hard shadow
846 vec3 shadow_uv = vec3(splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy, splane.z);
847 shadow = mix(half(1.0), sample_pcf_shadow(shadow_atlas, spot_lights.data[idx].soft_shadow_scale * scene_data_block.data.shadow_atlas_pixel_size, shadow_uv, taa_frame_count), half(spot_lights.data[idx].shadow_opacity));
848 }
849 }
850 #endif // SHADOWS_DISABLED
851
852 vec3 color = spot_lights.data[idx].color;
853
854 #ifdef LIGHT_TRANSMITTANCE_USED
855 half transmittance_z = transmittance_depth;
856 transmittance_color.a *= spot_attenuation;
857 #ifndef SHADOWS_DISABLED
858 if (spot_lights.data[idx].shadow_opacity > 0.001) {
859 vec4 splane = (spot_lights.data[idx].shadow_matrix * vec4(vertex - vec3(normal) * spot_lights.data[idx].transmittance_bias, 1.0));
860 splane /= splane.w;
861
862 vec3 shadow_uv = vec3(splane.xy * spot_lights.data[idx].atlas_rect.zw + spot_lights.data[idx].atlas_rect.xy, splane.z);
863 float shadow_z = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), shadow_uv.xy, 0.0).r;
864
865 shadow_z = shadow_z * 2.0 - 1.0;
866 float z_far = 1.0 / spot_lights.data[idx].inv_radius;
867 float z_near = 0.01;
868 shadow_z = 2.0 * z_near * z_far / (z_far + z_near - shadow_z * (z_far - z_near));
869
870 //distance to light plane
871 float z = dot(spot_dir, -light_rel_vec);
872 transmittance_z = half(z - shadow_z);
873 }
874 #endif // !SHADOWS_DISABLED
875 #endif // LIGHT_TRANSMITTANCE_USED
876
877 if (sc_use_light_projector() && spot_lights.data[idx].projector_rect != vec4(0.0)) {
878 vec4 splane = (spot_lights.data[idx].shadow_matrix * vec4(vertex, 1.0));
879 splane /= splane.w;
880
881 vec2 proj_uv = splane.xy * spot_lights.data[idx].projector_rect.zw;
882
883 if (sc_projector_use_mipmaps()) {
884 //ensure we have proper mipmaps
885 vec4 splane_ddx = (spot_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddx, 1.0));
886 splane_ddx /= splane_ddx.w;
887 vec2 proj_uv_ddx = splane_ddx.xy * spot_lights.data[idx].projector_rect.zw - proj_uv;
888
889 vec4 splane_ddy = (spot_lights.data[idx].shadow_matrix * vec4(vertex + vertex_ddy, 1.0));
890 splane_ddy /= splane_ddy.w;
891 vec2 proj_uv_ddy = splane_ddy.xy * spot_lights.data[idx].projector_rect.zw - proj_uv;
892
893 vec4 proj = textureGrad(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + spot_lights.data[idx].projector_rect.xy, proj_uv_ddx, proj_uv_ddy);
894 color *= proj.rgb * proj.a;
895 } else {
896 vec4 proj = textureLod(sampler2D(decal_atlas_srgb, light_projector_sampler), proj_uv + spot_lights.data[idx].projector_rect.xy, 0.0);
897 color *= proj.rgb * proj.a;
898 }
899 }
900
901 light_compute(normal, hvec3(light_rel_vec_norm), eye_vec, size, hvec3(color), false, spot_attenuation * shadow, f0, roughness, metallic, half(spot_lights.data[idx].specular_amount), albedo, alpha, screen_uv, energy_compensation,
902 #ifdef LIGHT_BACKLIGHT_USED
903 backlight,
904 #endif
905 #ifdef LIGHT_TRANSMITTANCE_USED
906 transmittance_color,
907 transmittance_depth,
908 transmittance_boost,
909 transmittance_z,
910 #endif
911 #ifdef LIGHT_RIM_USED
912 rim * spot_attenuation, rim_tint,
913 #endif
914 #ifdef LIGHT_CLEARCOAT_USED
915 clearcoat, clearcoat_roughness, vertex_normal,
916 #endif
917 #ifdef LIGHT_ANISOTROPY_USED
918 binormal, tangent, anisotropy,
919 #endif
920 diffuse_light, specular_light);
921 }
922
923 void reflection_process(uint ref_index, vec3 vertex, hvec3 ref_vec, hvec3 normal, half roughness, hvec3 ambient_light, hvec3 specular_light, inout hvec4 ambient_accum, inout hvec4 reflection_accum) {
924 vec3 box_extents = reflections.data[ref_index].box_extents;
925 vec3 local_pos = (reflections.data[ref_index].local_matrix * vec4(vertex, 1.0)).xyz;
926
927 if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box
928 return;
929 }
930
931 half blend = half(1.0);
932 if (reflections.data[ref_index].blend_distance != 0.0) {
933 vec3 axis_blend_distance = min(vec3(reflections.data[ref_index].blend_distance), box_extents);
934 vec3 blend_axes_highp = abs(local_pos) - box_extents + axis_blend_distance;
935 hvec3 blend_axes = hvec3(blend_axes_highp / axis_blend_distance);
936 blend_axes = clamp(half(1.0) - blend_axes, hvec3(0.0), hvec3(1.0));
937 blend = pow(blend_axes.x * blend_axes.y * blend_axes.z, half(2.0));
938 }
939
940 vec2 border_size = scene_data_block.data.reflection_atlas_border_size;
941 if (reflections.data[ref_index].intensity > 0.0 && reflection_accum.a < half(1.0)) { // compute reflection
942
943 vec3 local_ref_vec = (reflections.data[ref_index].local_matrix * vec4(ref_vec, 0.0)).xyz;
944
945 if (reflections.data[ref_index].box_project) { //box project
946
947 vec3 nrdir = normalize(local_ref_vec);
948 vec3 rbmax = (box_extents - local_pos) / nrdir;
949 vec3 rbmin = (-box_extents - local_pos) / nrdir;
950
951 vec3 rbminmax = mix(rbmin, rbmax, greaterThan(nrdir, vec3(0.0, 0.0, 0.0)));
952
953 float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
954 vec3 posonbox = local_pos + nrdir * fa;
955 local_ref_vec = posonbox - reflections.data[ref_index].box_offset;
956 }
957
958 hvec4 reflection;
959 half reflection_blend = max(half(0.0), blend - reflection_accum.a);
960
961 float roughness_lod = sqrt(roughness) * MAX_ROUGHNESS_LOD;
962 vec2 reflection_uv = vec3_to_oct_with_border(local_ref_vec, border_size);
963 reflection.rgb = hvec3(textureLod(sampler2DArray(reflection_atlas, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(reflection_uv, reflections.data[ref_index].index), roughness_lod).rgb) * REFLECTION_MULTIPLIER;
964 reflection.rgb *= half(reflections.data[ref_index].exposure_normalization);
965 reflection.a = reflection_blend;
966
967 reflection.rgb *= half(reflections.data[ref_index].intensity);
968 reflection.rgb *= reflection.a;
969
970 reflection_accum += reflection;
971 }
972
973 if (ambient_accum.a >= half(1.0)) {
974 return;
975 }
976
977 switch (reflections.data[ref_index].ambient_mode) {
978 case REFLECTION_AMBIENT_DISABLED: {
979 //do nothing
980 } break;
981 case REFLECTION_AMBIENT_ENVIRONMENT: {
982 vec3 local_amb_vec = (reflections.data[ref_index].local_matrix * vec4(normal, 0.0)).xyz;
983 hvec4 ambient_out;
984 half ambient_blend = max(half(0.0), blend - ambient_accum.a);
985
986 float roughness_lod = MAX_ROUGHNESS_LOD;
987 vec2 ambient_uv = vec3_to_oct_with_border(local_amb_vec, border_size);
988 ambient_out.rgb = hvec3(textureLod(sampler2DArray(reflection_atlas, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ambient_uv, reflections.data[ref_index].index), roughness_lod).rgb);
989 ambient_out.rgb *= half(reflections.data[ref_index].exposure_normalization);
990 ambient_out.a = ambient_blend;
991 ambient_out.rgb *= ambient_out.a;
992 ambient_accum += ambient_out;
993 } break;
994 case REFLECTION_AMBIENT_COLOR: {
995 hvec4 ambient_out;
996 half ambient_blend = max(half(0.0), blend - ambient_accum.a);
997
998 ambient_out.rgb = hvec3(reflections.data[ref_index].ambient);
999 ambient_out.a = ambient_blend;
1000 ambient_out.rgb *= ambient_out.a;
1001 ambient_accum += ambient_out;
1002 } break;
1003 }
1004 }
1005
1006 half blur_shadow(half shadow) {
1007 return shadow;
1008 #if 0
1009 //disabling for now, will investigate later
1010 float interp_shadow = shadow;
1011 if (gl_HelperInvocation) {
1012 interp_shadow = -4.0; // technically anything below -4 will do but just to make sure
1013 }
1014
1015 uvec2 fc2 = uvec2(gl_FragCoord.xy);
1016 interp_shadow -= dFdx(interp_shadow) * (float(fc2.x & 1) - 0.5);
1017 interp_shadow -= dFdy(interp_shadow) * (float(fc2.y & 1) - 0.5);
1018
1019 if (interp_shadow >= 0.0) {
1020 shadow = interp_shadow;
1021 }
1022 return shadow;
1023 #endif
1024 }