Debian Codegraph @

servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl

raw
1 #[vertex]
2
3 #version 450
4
5 #VERSION_DEFINES
6
7 /* Include half precision types. */
8 #include "../half_inc.glsl"
9
10 #include "scene_forward_clustered_inc.glsl"
11
12 #define SHADER_IS_SRGB false
13 #define SHADER_SPACE_FAR 0.0
14
15 #ifdef USE_MULTIVIEW
16 #define OUTPUT_IS_MULTIVIEW true
17 #else
18 #define OUTPUT_IS_MULTIVIEW false
19 #endif
20
21 /* INPUT ATTRIBS */
22
23 // Always contains vertex position in XYZ, can contain tangent angle in W.
24 layout(location = 0) in vec4 vertex_angle_attrib;
25
26 //only for pure render depth when normal is not used
27
28 #if defined(NORMAL_USED) || defined(TANGENT_USED)
29 // Contains Normal/Axis in RG, can contain tangent in BA.
30 layout(location = 1) in vec4 axis_tangent_attrib;
31 #endif
32
33 // Location 2 is unused.
34
35 #if defined(COLOR_USED)
36 layout(location = 3) in vec4 color_attrib;
37 #endif
38
39 #ifdef UV_USED
40 layout(location = 4) in vec2 uv_attrib;
41 #endif
42
43 #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(MODE_RENDER_MATERIAL)
44 layout(location = 5) in vec2 uv2_attrib;
45 #endif
46
47 #if defined(CUSTOM0_USED)
48 layout(location = 6) in vec4 custom0_attrib;
49 #endif
50
51 #if defined(CUSTOM1_USED)
52 layout(location = 7) in vec4 custom1_attrib;
53 #endif
54
55 #if defined(CUSTOM2_USED)
56 layout(location = 8) in vec4 custom2_attrib;
57 #endif
58
59 #if defined(CUSTOM3_USED)
60 layout(location = 9) in vec4 custom3_attrib;
61 #endif
62
63 #if defined(BONES_USED) || defined(USE_PARTICLE_TRAILS)
64 layout(location = 10) in uvec4 bone_attrib;
65 #endif
66
67 #if defined(WEIGHTS_USED) || defined(USE_PARTICLE_TRAILS)
68 layout(location = 11) in vec4 weight_attrib;
69 #endif
70
71 #ifdef MOTION_VECTORS
72 layout(location = 12) in vec4 previous_vertex_attrib;
73
74 #if defined(NORMAL_USED) || defined(TANGENT_USED)
75 layout(location = 13) in vec4 previous_normal_attrib;
76 #endif
77
78 #endif // MOTION_VECTORS
79
80 void axis_angle_to_tbn(vec3 axis, float angle, out vec3 tangent, out vec3 binormal, out vec3 normal) {
81 float c = cos(angle);
82 float s = sin(angle);
83 vec3 omc_axis = (1.0 - c) * axis;
84 vec3 s_axis = s * axis;
85 tangent = omc_axis.xxx * axis + vec3(c, -s_axis.z, s_axis.y);
86 binormal = omc_axis.yyy * axis + vec3(s_axis.z, c, -s_axis.x);
87 normal = omc_axis.zzz * axis + vec3(-s_axis.y, s_axis.x, c);
88 }
89
90 /* Varyings */
91
92 layout(location = 0) out vec3 vertex_interp;
93
94 #ifdef NORMAL_USED
95 layout(location = 1) out vec3 normal_interp;
96 #endif
97
98 #if defined(COLOR_USED)
99 layout(location = 2) out vec4 color_interp;
100 #endif
101
102 #ifdef UV_USED
103 layout(location = 3) out vec2 uv_interp;
104 #endif
105
106 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
107 layout(location = 4) out vec2 uv2_interp;
108 #endif
109
110 #ifdef TANGENT_USED
111 layout(location = 5) out vec3 tangent_interp;
112 layout(location = 6) out vec3 binormal_interp;
113 #endif
114
115 #ifdef MOTION_VECTORS
116 layout(location = 7) out vec4 screen_position;
117 layout(location = 8) out vec4 prev_screen_position;
118 #endif
119
120 #ifdef MATERIAL_UNIFORMS_USED
121 /* clang-format off */
122 layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms {
123 #MATERIAL_UNIFORMS
124 } material;
125 /* clang-format on */
126 #endif
127
128 float global_time;
129
130 #ifdef MODE_DUAL_PARABOLOID
131
132 layout(location = 9) out float dp_clip;
133
134 #endif
135
136 layout(location = 10) out flat uint instance_index_interp;
137
138 #ifdef USE_MULTIVIEW
139 #extension GL_EXT_multiview : enable
140 #define ViewIndex gl_ViewIndex
141
142 vec3 multiview_uv(vec2 uv) {
143 return vec3(uv, ViewIndex);
144 }
145 ivec3 multiview_uv(ivec2 uv) {
146 return ivec3(uv, int(ViewIndex));
147 }
148 layout(location = 11) out vec4 combined_projected;
149 #else // USE_MULTIVIEW
150 #define ViewIndex 0
151 vec2 multiview_uv(vec2 uv) {
152 return uv;
153 }
154 ivec2 multiview_uv(ivec2 uv) {
155 return uv;
156 }
157 #endif //USE_MULTIVIEW
158
159 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
160 layout(location = 12) out vec4 diffuse_light_interp;
161 layout(location = 13) out vec4 specular_light_interp;
162
163 #include "../scene_forward_vertex_lights_inc.glsl"
164
165 void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
166 uint item_min_max = cluster_buffer.data[p_offset];
167 item_min = item_min_max & 0xFFFFu;
168 item_max = item_min_max >> 16;
169
170 item_from = item_min >> 5;
171 item_to = (item_max == 0) ? 0 : ((item_max - 1) >> 5) + 1; //side effect of how it is stored, as item_max 0 means no elements
172 }
173
174 uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
175 int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
176 int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
177 return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
178 }
179 #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
180
181 #if defined(POINT_SIZE_USED) && defined(POINT_COORD_USED)
182 layout(location = 14) out vec2 point_coord_interp;
183 #endif
184
185 invariant gl_Position;
186
187 #GLOBALS
188
189 #ifdef USE_DOUBLE_PRECISION
190 // Helper functions for emulating double precision when adding floats.
191 vec3 quick_two_sum(vec3 a, vec3 b, out vec3 out_p) {
192 vec3 s = a + b;
193 out_p = b - (s - a);
194 return s;
195 }
196
197 vec3 two_sum(vec3 a, vec3 b, out vec3 out_p) {
198 vec3 s = a + b;
199 vec3 v = s - a;
200 out_p = (a - (s - v)) + (b - v);
201 return s;
202 }
203
204 vec3 double_add_vec3(vec3 base_a, vec3 prec_a, vec3 base_b, vec3 prec_b, out vec3 out_precision) {
205 vec3 s, t, se, te;
206 s = two_sum(base_a, base_b, se);
207 t = two_sum(prec_a, prec_b, te);
208 se += t;
209 s = quick_two_sum(s, se, se);
210 se += te;
211 s = quick_two_sum(s, se, out_precision);
212 return s;
213 }
214 #endif
215
216 uint multimesh_stride() {
217 uint stride = sc_multimesh_format_2d() ? 2 : 3;
218 stride += sc_multimesh_has_color() ? 1 : 0;
219 stride += sc_multimesh_has_custom_data() ? 1 : 0;
220 return stride;
221 }
222
223 void vertex_shader(vec3 vertex_input,
224 #ifdef NORMAL_USED
225 in vec3 normal_input,
226 #endif
227 #ifdef TANGENT_USED
228 in vec3 tangent_input,
229 in vec3 binormal_input,
230 #endif
231 in uint instance_index, in uint multimesh_offset, in SceneData scene_data, in mat3x4 in_model_matrix,
232 #ifdef USE_DOUBLE_PRECISION
233 in vec3 model_precision,
234 #endif
235 out vec4 screen_pos) {
236 vec4 instance_custom = vec4(0.0);
237 #if defined(COLOR_USED)
238 color_interp = color_attrib;
239 #endif
240
241 mat4 inv_view_matrix = transpose(mat4(scene_data.inv_view_matrix[0],
242 scene_data.inv_view_matrix[1],
243 scene_data.inv_view_matrix[2],
244 vec4(0.0, 0.0, 0.0, 1.0)));
245
246 mat4 model_matrix = transpose(mat4(in_model_matrix[0],
247 in_model_matrix[1],
248 in_model_matrix[2],
249 vec4(0.0, 0.0, 0.0, 1.0)));
250
251 #ifdef USE_DOUBLE_PRECISION
252 vec3 view_precision = scene_data.inv_view_precision.xyz;
253 #endif
254
255 mat3 model_normal_matrix;
256 if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
257 model_normal_matrix = transpose(inverse(mat3(model_matrix)));
258 } else {
259 model_normal_matrix = mat3(model_matrix);
260 }
261
262 mat4 matrix;
263 mat4 read_model_matrix = model_matrix;
264
265 if (sc_multimesh()) {
266 //multimesh, instances are for it
267
268 #ifdef USE_PARTICLE_TRAILS
269 uint trail_size = (instances.data[instance_index].flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK;
270 uint stride = 3 + 1 + 1; //particles always uses this format
271
272 uint offset = trail_size * stride * INSTANCE_INDEX;
273
274 #ifdef COLOR_USED
275 vec4 pcolor;
276 #endif
277 {
278 uint boffset = offset + bone_attrib.x * stride;
279 matrix = mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.x;
280 #ifdef COLOR_USED
281 pcolor = transforms.data[boffset + 3] * weight_attrib.x;
282 #endif
283 }
284 if (weight_attrib.y > 0.001) {
285 uint boffset = offset + bone_attrib.y * stride;
286 matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.y;
287 #ifdef COLOR_USED
288 pcolor += transforms.data[boffset + 3] * weight_attrib.y;
289 #endif
290 }
291 if (weight_attrib.z > 0.001) {
292 uint boffset = offset + bone_attrib.z * stride;
293 matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.z;
294 #ifdef COLOR_USED
295 pcolor += transforms.data[boffset + 3] * weight_attrib.z;
296 #endif
297 }
298 if (weight_attrib.w > 0.001) {
299 uint boffset = offset + bone_attrib.w * stride;
300 matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.w;
301 #ifdef COLOR_USED
302 pcolor += transforms.data[boffset + 3] * weight_attrib.w;
303 #endif
304 }
305
306 instance_custom = transforms.data[offset + 4];
307
308 #ifdef COLOR_USED
309 color_interp *= pcolor;
310 #endif
311
312 #else
313 uint stride = multimesh_stride();
314 uint offset = stride * (INSTANCE_INDEX + multimesh_offset);
315
316 if (sc_multimesh_format_2d()) {
317 matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
318 offset += 2;
319 } else {
320 matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], transforms.data[offset + 2], vec4(0.0, 0.0, 0.0, 1.0));
321 offset += 3;
322 }
323
324 if (sc_multimesh_has_color()) {
325 #ifdef COLOR_USED
326 color_interp *= transforms.data[offset];
327 #endif
328 offset += 1;
329 }
330
331 if (sc_multimesh_has_custom_data()) {
332 instance_custom = transforms.data[offset];
333 }
334
335 #endif
336 //transpose
337 matrix = transpose(matrix);
338 #if !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED) || defined(MODEL_MATRIX_USED)
339 // Normally we can bake the multimesh transform into the model matrix, but when using double precision
340 // we avoid baking it in so we can emulate high precision.
341 read_model_matrix = model_matrix * matrix;
342 #if !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED)
343 model_matrix = read_model_matrix;
344 #endif // !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED)
345 #endif // !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED) || defined(MODEL_MATRIX_USED)
346 model_normal_matrix = model_normal_matrix * mat3(matrix);
347 }
348
349 vec3 vertex = vertex_input;
350 #ifdef NORMAL_USED
351 vec3 normal_highp = normal_input;
352 #endif
353
354 #ifdef TANGENT_USED
355 vec3 tangent = tangent_input;
356 vec3 binormal = binormal_input;
357 #endif
358
359 #ifdef UV_USED
360 uv_interp = uv_attrib;
361 #endif
362
363 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
364 uv2_interp = uv2_attrib;
365 #endif
366
367 vec4 uv_scale = instances.data[instance_index].uv_scale;
368
369 if (uv_scale != vec4(0.0)) { // Compression enabled
370 #ifdef UV_USED
371 uv_interp = (uv_interp - 0.5) * uv_scale.xy;
372 #endif
373 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
374 uv2_interp = (uv2_interp - 0.5) * uv_scale.zw;
375 #endif
376 }
377
378 #ifdef OVERRIDE_POSITION
379 vec4 position = vec4(1.0);
380 #endif
381
382 #ifdef USE_MULTIVIEW
383 mat4 combined_projection = scene_data.projection_matrix;
384 mat4 projection_matrix = scene_data.projection_matrix_view[ViewIndex];
385 mat4 inv_projection_matrix = scene_data.inv_projection_matrix_view[ViewIndex];
386 vec3 eye_offset = scene_data.eye_offset[ViewIndex].xyz;
387 #else
388 mat4 projection_matrix = scene_data.projection_matrix;
389 mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
390 vec3 eye_offset = vec3(0.0, 0.0, 0.0);
391 #endif //USE_MULTIVIEW
392
393 //using world coordinates
394 #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
395
396 vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
397
398 #ifdef NORMAL_USED
399 normal_highp = model_normal_matrix * normal_highp;
400 #endif
401
402 #ifdef TANGENT_USED
403
404 tangent = model_normal_matrix * tangent;
405 binormal = model_normal_matrix * binormal;
406
407 #endif
408 #endif
409
410 #ifdef Z_CLIP_SCALE_USED
411 float z_clip_scale = 1.0;
412 #endif
413
414 float roughness_highp = 1.0;
415
416 mat4 read_view_matrix = transpose(mat4(scene_data.view_matrix[0],
417 scene_data.view_matrix[1],
418 scene_data.view_matrix[2],
419 vec4(0.0, 0.0, 0.0, 1.0)));
420
421 #ifdef USE_DOUBLE_PRECISION
422 mat4 modelview = read_view_matrix * model_matrix;
423
424 // We separate the basis from the origin because the basis is fine with single point precision.
425 // Then we combine the translations from the model matrix and the view matrix using emulated doubles.
426 // We add the result to the vertex and ignore the final lost precision.
427 vec3 model_origin = model_matrix[3].xyz;
428 if (sc_multimesh()) {
429 modelview = modelview * matrix;
430
431 vec3 instance_origin = mat3(model_matrix) * matrix[3].xyz;
432 model_origin = double_add_vec3(model_origin, model_precision, instance_origin, vec3(0.0), model_precision);
433 }
434
435 // Overwrite the translation part of modelview with improved precision.
436 vec3 temp_precision; // Will be ignored.
437 modelview[3].xyz = double_add_vec3(model_origin, model_precision, inv_view_matrix[3].xyz, view_precision, temp_precision);
438 modelview[3].xyz = mat3(read_view_matrix) * modelview[3].xyz;
439 #else
440 mat4 modelview = read_view_matrix * model_matrix;
441 #endif
442 mat3 modelview_normal = mat3(read_view_matrix) * model_normal_matrix;
443 vec2 read_viewport_size = scene_data.viewport_size;
444
445 #ifdef POINT_SIZE_USED
446 float point_size = 1.0;
447 #endif
448
449 {
450 #CODE : VERTEX
451 }
452
453 float roughness = roughness_highp;
454 #ifdef NORMAL_USED
455 vec3 normal = normal_highp;
456 #endif
457
458 // using local coordinates (default)
459 #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
460
461 vertex = (modelview * vec4(vertex, 1.0)).xyz;
462
463 #ifdef NORMAL_USED
464 normal = modelview_normal * normal;
465 #endif
466
467 #ifdef TANGENT_USED
468
469 binormal = modelview_normal * binormal;
470 tangent = modelview_normal * tangent;
471 #endif
472 #endif // !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
473
474 //using world coordinates
475 #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
476
477 vertex = (read_view_matrix * vec4(vertex, 1.0)).xyz;
478 #ifdef NORMAL_USED
479 normal = (read_view_matrix * vec4(normal, 0.0)).xyz;
480 #endif
481
482 #ifdef TANGENT_USED
483 binormal = (read_view_matrix * vec4(binormal, 0.0)).xyz;
484 tangent = (read_view_matrix * vec4(tangent, 0.0)).xyz;
485 #endif
486 #endif
487
488 vertex_interp = vertex;
489
490 // Normalize TBN vectors before interpolation, per MikkTSpace.
491 // See: http://www.mikktspace.com/
492 #ifdef NORMAL_USED
493 normal_interp = normalize(normal);
494 #endif
495
496 #ifdef TANGENT_USED
497 tangent_interp = normalize(tangent);
498 binormal_interp = normalize(binormal);
499 #endif
500
501 #ifdef MODE_RENDER_DEPTH
502
503 #ifdef MODE_DUAL_PARABOLOID
504
505 vertex_interp.z *= scene_data.dual_paraboloid_side;
506
507 dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
508
509 //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
510
511 vec3 vtx = vertex_interp;
512 float distance = length(vtx);
513 vtx = normalize(vtx);
514 vtx.xy /= 1.0 - vtx.z;
515 vtx.z = (distance / scene_data.z_far);
516 vtx.z = vtx.z * 2.0 - 1.0;
517 vertex_interp = vtx;
518
519 #endif
520
521 #endif //MODE_RENDER_DEPTH
522
523 #ifdef OVERRIDE_POSITION
524 gl_Position = position;
525 #else
526 gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
527 #endif
528
529 #ifdef USE_MULTIVIEW
530 combined_projected = combined_projection * vec4(vertex_interp, 1.0);
531 #endif
532
533 #ifdef MOTION_VECTORS
534 screen_pos = gl_Position;
535 #endif
536
537 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
538 diffuse_light_interp = vec4(0.0);
539 specular_light_interp = vec4(0.0);
540
541 #ifdef USE_MULTIVIEW
542 vec3 view = -normalize(vertex_interp - eye_offset);
543 vec2 clip_pos = clamp((combined_projected.xy / combined_projected.w) * 0.5 + 0.5, 0.0, 1.0);
544 #else
545 vec3 view = -normalize(vertex_interp);
546 vec2 clip_pos = clamp((gl_Position.xy / gl_Position.w) * 0.5 + 0.5, 0.0, 1.0);
547 #endif
548
549 uvec2 screen_size = uvec2(1.0 / scene_data.screen_pixel_size);
550 uvec2 screen_pixel = clamp(uvec2(clip_pos * vec2(screen_size)), uvec2(0), screen_size - uvec2(1));
551 uvec2 cluster_pos = screen_pixel >> implementation_data.cluster_shift;
552 uint cluster_offset = (implementation_data.cluster_width * cluster_pos.y + cluster_pos.x) * (implementation_data.max_cluster_element_count_div_32 + 32);
553 uint cluster_z = uint(clamp((-vertex_interp.z / scene_data.z_far) * 32.0, 0.0, 31.0));
554
555 { //omni lights
556
557 uint cluster_omni_offset = cluster_offset;
558
559 uint item_min;
560 uint item_max;
561 uint item_from;
562 uint item_to;
563
564 cluster_get_item_range(cluster_omni_offset + implementation_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
565
566 for (uint i = item_from; i < item_to; i++) {
567 uint mask = cluster_buffer.data[cluster_omni_offset + i];
568 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
569 uint merged_mask = mask;
570
571 while (merged_mask != 0) {
572 uint bit = findMSB(merged_mask);
573 merged_mask &= ~(1u << bit);
574 uint light_index = 32 * i + bit;
575
576 if (!bool(omni_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
577 continue; //not masked
578 }
579
580 if (omni_lights.data[light_index].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
581 continue; // Statically baked light and object uses lightmap, skip
582 }
583
584 light_process_omni_vertex(light_index, vertex, view, normal, roughness,
585 diffuse_light_interp.rgb, specular_light_interp.rgb);
586 }
587 }
588 }
589
590 { //spot lights
591 uint cluster_spot_offset = cluster_offset + implementation_data.cluster_type_size;
592
593 uint item_min;
594 uint item_max;
595 uint item_from;
596 uint item_to;
597
598 cluster_get_item_range(cluster_spot_offset + implementation_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
599
600 for (uint i = item_from; i < item_to; i++) {
601 uint mask = cluster_buffer.data[cluster_spot_offset + i];
602 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
603 uint merged_mask = mask;
604
605 while (merged_mask != 0) {
606 uint bit = findMSB(merged_mask);
607 merged_mask &= ~(1u << bit);
608
609 uint light_index = 32 * i + bit;
610
611 if (!bool(spot_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
612 continue; //not masked
613 }
614
615 if (spot_lights.data[light_index].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
616 continue; // Statically baked light and object uses lightmap, skip
617 }
618
619 light_process_spot_vertex(light_index, vertex, view, normal, roughness,
620 diffuse_light_interp.rgb, specular_light_interp.rgb);
621 }
622 }
623 }
624
625 { // Directional light.
626
627 // We process the first directional light separately as it may have shadows.
628 vec3 directional_diffuse = vec3(0.0);
629 vec3 directional_specular = vec3(0.0);
630
631 for (uint i = 0; i < scene_data.directional_light_count; i++) {
632 if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
633 continue; // Not masked, skip.
634 }
635
636 if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
637 continue; // Statically baked light and object uses lightmap, skip.
638 }
639 if (i == 0) {
640 light_compute_vertex(normal, directional_lights.data[0].direction, view,
641 directional_lights.data[0].color * directional_lights.data[0].energy,
642 true, roughness,
643 directional_diffuse,
644 directional_specular);
645 } else {
646 light_compute_vertex(normal, directional_lights.data[i].direction, view,
647 directional_lights.data[i].color * directional_lights.data[i].energy,
648 true, roughness,
649 diffuse_light_interp.rgb,
650 specular_light_interp.rgb);
651 }
652 }
653
654 // Calculate the contribution from the shadowed light so we can scale the shadows accordingly.
655 float diff_avg = dot(diffuse_light_interp.rgb, vec3(0.33333));
656 float diff_dir_avg = dot(directional_diffuse, vec3(0.33333));
657 if (diff_avg > 0.0) {
658 diffuse_light_interp.a = diff_dir_avg / (diff_avg + diff_dir_avg);
659 } else {
660 diffuse_light_interp.a = 1.0;
661 }
662
663 diffuse_light_interp.rgb += directional_diffuse;
664
665 float spec_avg = dot(specular_light_interp.rgb, vec3(0.33333));
666 float spec_dir_avg = dot(directional_specular, vec3(0.33333));
667 if (spec_avg > 0.0) {
668 specular_light_interp.a = spec_dir_avg / (spec_avg + spec_dir_avg);
669 } else {
670 specular_light_interp.a = 1.0;
671 }
672
673 specular_light_interp.rgb += directional_specular;
674 }
675
676 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
677
678 #ifdef MODE_RENDER_DEPTH
679 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_PANCAKE_SHADOWS)) {
680 if (gl_Position.z >= 0.9999) {
681 gl_Position.z = 0.9999;
682 }
683 }
684 #endif
685 #ifdef MODE_RENDER_MATERIAL
686 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_UV2_MATERIAL)) {
687 vec2 uv_dest_attrib;
688 if (uv_scale != vec4(0.0)) {
689 uv_dest_attrib = (uv2_attrib.xy - 0.5) * uv_scale.zw;
690 } else {
691 uv_dest_attrib = uv2_attrib.xy;
692 }
693
694 vec2 uv_offset = unpackHalf2x16(draw_call.uv_offset);
695 gl_Position.xy = (uv_dest_attrib + uv_offset) * 2.0 - 1.0;
696 gl_Position.z = 0.00001;
697 gl_Position.w = 1.0;
698 }
699 #endif
700
701 #ifdef Z_CLIP_SCALE_USED
702 if (!bool(scene_data.flags & SCENE_DATA_FLAGS_IN_SHADOW_PASS)) {
703 gl_Position.z = mix(gl_Position.w, gl_Position.z, z_clip_scale);
704 }
705 #endif
706
707 #ifdef POINT_SIZE_USED
708 if (sc_emulate_point_size) {
709 vec2 point_coords[6] = vec2[](
710 vec2(0, 1),
711 vec2(0, 0),
712 vec2(1, 1),
713 vec2(0, 0),
714 vec2(1, 0),
715 vec2(1, 1));
716
717 vec2 point_coord = point_coords[gl_VertexIndex % 6];
718 gl_Position.xy += (point_coord * 2.0 - 1.0) * (point_size / scene_data.viewport_size) * gl_Position.w;
719
720 #ifdef POINT_COORD_USED
721 point_coord_interp = point_coord;
722 #endif
723 } else {
724 gl_PointSize = point_size;
725 }
726 #endif
727 }
728
729 void _unpack_vertex_attributes(vec4 p_vertex_in, vec3 p_compressed_aabb_position, vec3 p_compressed_aabb_size,
730 #if defined(NORMAL_USED) || defined(TANGENT_USED)
731 vec4 p_normal_in,
732 #ifdef NORMAL_USED
733 out vec3 r_normal,
734 #endif
735 out vec3 r_tangent,
736 out vec3 r_binormal,
737 #endif
738 out vec3 r_vertex) {
739
740 r_vertex = p_vertex_in.xyz * p_compressed_aabb_size + p_compressed_aabb_position;
741 #ifdef NORMAL_USED
742 r_normal = oct_to_vec3(p_normal_in.xy * 2.0 - 1.0);
743 #endif
744
745 #if defined(NORMAL_USED) || defined(TANGENT_USED)
746
747 float binormal_sign;
748
749 // This works because the oct value (0, 1) maps onto (0, 0, -1) which encodes to (1, 1).
750 // Accordingly, if p_normal_in.z contains octahedral values, it won't equal (0, 1).
751 if (p_normal_in.z > 0.0 || p_normal_in.w < 1.0) {
752 // Uncompressed format.
753 vec2 signed_tangent_attrib = p_normal_in.zw * 2.0 - 1.0;
754 r_tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0));
755 binormal_sign = sign(signed_tangent_attrib.y);
756 r_binormal = normalize(cross(r_normal, r_tangent) * binormal_sign);
757 } else {
758 // Compressed format.
759 float angle = p_vertex_in.w;
760 binormal_sign = angle > 0.5 ? 1.0 : -1.0; // 0.5 does not exist in UNORM16, so values are either greater or smaller.
761 angle = abs(angle * 2.0 - 1.0) * M_PI; // 0.5 is basically zero, allowing to encode both signs reliably.
762 vec3 axis = r_normal;
763 axis_angle_to_tbn(axis, angle, r_tangent, r_binormal, r_normal);
764 r_binormal *= binormal_sign;
765 }
766 #endif
767 }
768
769 void main() {
770 uint instance_index = draw_call.instance_index;
771 if (!sc_multimesh()) {
772 instance_index += INSTANCE_INDEX;
773 }
774
775 instance_index_interp = instance_index;
776
777 #ifdef MOTION_VECTORS
778 // Previous vertex.
779 vec3 prev_vertex;
780 #ifdef NORMAL_USED
781 vec3 prev_normal;
782 #endif
783 #if defined(NORMAL_USED) || defined(TANGENT_USED)
784 vec3 prev_tangent;
785 vec3 prev_binormal;
786 #endif
787
788 _unpack_vertex_attributes(
789 previous_vertex_attrib,
790 instances.data[instance_index].compressed_aabb_position_pad.xyz,
791 instances.data[instance_index].compressed_aabb_size_pad.xyz,
792
793 #if defined(NORMAL_USED) || defined(TANGENT_USED)
794 previous_normal_attrib,
795 #ifdef NORMAL_USED
796 prev_normal,
797 #endif
798 prev_tangent,
799 prev_binormal,
800 #endif
801 prev_vertex);
802
803 global_time = scene_data_block.prev_data.time;
804 vertex_shader(prev_vertex,
805 #ifdef NORMAL_USED
806 prev_normal,
807 #endif
808 #ifdef TANGENT_USED
809 prev_tangent,
810 prev_binormal,
811 #endif
812 instance_index, draw_call.multimesh_motion_vectors_previous_offset, scene_data_block.prev_data, instances.data[instance_index].prev_transform,
813 #ifdef USE_DOUBLE_PRECISION
814 instances.data[instance_index].prev_model_precision.xyz,
815 #endif
816 prev_screen_position);
817 #else
818 // Unused output.
819 vec4 screen_position;
820 #endif
821
822 vec3 vertex;
823 #ifdef NORMAL_USED
824 vec3 normal;
825 #endif
826 #if defined(NORMAL_USED) || defined(TANGENT_USED)
827 vec3 tangent;
828 vec3 binormal;
829 #endif
830
831 _unpack_vertex_attributes(
832 vertex_angle_attrib,
833 instances.data[instance_index].compressed_aabb_position_pad.xyz,
834 instances.data[instance_index].compressed_aabb_size_pad.xyz,
835 #if defined(NORMAL_USED) || defined(TANGENT_USED)
836 axis_tangent_attrib,
837 #ifdef NORMAL_USED
838 normal,
839 #endif
840 tangent,
841 binormal,
842 #endif
843 vertex);
844
845 // Current vertex.
846 global_time = scene_data_block.data.time;
847 vertex_shader(vertex,
848 #ifdef NORMAL_USED
849 normal,
850 #endif
851 #ifdef TANGENT_USED
852 tangent,
853 binormal,
854 #endif
855 instance_index, draw_call.multimesh_motion_vectors_current_offset, scene_data_block.data, instances.data[instance_index].transform,
856 #ifdef USE_DOUBLE_PRECISION
857 instances.data[instance_index].model_precision.xyz,
858 #endif
859
860 screen_position);
861 }
862
863 #[fragment]
864
865 #version 450
866
867 #VERSION_DEFINES
868
869 #define SHADER_IS_SRGB false
870 #define SHADER_SPACE_FAR 0.0
871
872 #ifdef USE_MULTIVIEW
873 #define OUTPUT_IS_MULTIVIEW true
874 #else
875 #define OUTPUT_IS_MULTIVIEW false
876 #endif
877
878 /* Include half precision types. */
879 #include "../half_inc.glsl"
880
881 #include "scene_forward_clustered_inc.glsl"
882
883 /* Varyings */
884
885 layout(location = 0) in vec3 vertex_interp;
886
887 #ifdef NORMAL_USED
888 layout(location = 1) in vec3 normal_interp;
889 #endif
890
891 #if defined(COLOR_USED)
892 layout(location = 2) in vec4 color_interp;
893 #endif
894
895 #ifdef UV_USED
896 layout(location = 3) in vec2 uv_interp;
897 #endif
898
899 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
900 layout(location = 4) in vec2 uv2_interp;
901 #endif
902
903 #ifdef TANGENT_USED
904 layout(location = 5) in vec3 tangent_interp;
905 layout(location = 6) in vec3 binormal_interp;
906 #endif
907
908 #ifdef MOTION_VECTORS
909 layout(location = 7) in vec4 screen_position;
910 layout(location = 8) in vec4 prev_screen_position;
911 #endif
912
913 #ifdef MODE_DUAL_PARABOLOID
914
915 layout(location = 9) in float dp_clip;
916
917 #endif
918
919 layout(location = 10) in flat uint instance_index_interp;
920
921 #ifdef USE_LIGHTMAP
922 // w0, w1, w2, and w3 are the four cubic B-spline basis functions
923 float w0(float a) {
924 return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);
925 }
926
927 float w1(float a) {
928 return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);
929 }
930
931 float w2(float a) {
932 return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);
933 }
934
935 float w3(float a) {
936 return (1.0 / 6.0) * (a * a * a);
937 }
938
939 // g0 and g1 are the two amplitude functions
940 float g0(float a) {
941 return w0(a) + w1(a);
942 }
943
944 float g1(float a) {
945 return w2(a) + w3(a);
946 }
947
948 // h0 and h1 are the two offset functions
949 float h0(float a) {
950 return -1.0 + w1(a) / (w0(a) + w1(a));
951 }
952
953 float h1(float a) {
954 return 1.0 + w3(a) / (w2(a) + w3(a));
955 }
956
957 vec4 textureArray_bicubic(texture2DArray tex, vec3 uv, vec2 texture_size) {
958 vec2 texel_size = vec2(1.0) / texture_size;
959
960 uv.xy = uv.xy * texture_size + vec2(0.5);
961
962 vec2 iuv = floor(uv.xy);
963 vec2 fuv = fract(uv.xy);
964
965 float g0x = g0(fuv.x);
966 float g1x = g1(fuv.x);
967 float h0x = h0(fuv.x);
968 float h1x = h1(fuv.x);
969 float h0y = h0(fuv.y);
970 float h1y = h1(fuv.y);
971
972 vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;
973 vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;
974 vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;
975 vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;
976
977 return (g0(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p0, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p1, uv.z)))) +
978 (g1(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p2, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p3, uv.z))));
979 }
980 #endif //USE_LIGHTMAP
981
982 #ifdef USE_MULTIVIEW
983 #extension GL_EXT_multiview : enable
984 #define ViewIndex gl_ViewIndex
985 vec3 multiview_uv(vec2 uv) {
986 return vec3(uv, ViewIndex);
987 }
988 ivec3 multiview_uv(ivec2 uv) {
989 return ivec3(uv, int(ViewIndex));
990 }
991 layout(location = 11) in vec4 combined_projected;
992 #else // USE_MULTIVIEW
993 #define ViewIndex 0
994 vec2 multiview_uv(vec2 uv) {
995 return uv;
996 }
997 ivec2 multiview_uv(ivec2 uv) {
998 return uv;
999 }
1000 #endif // !USE_MULTIVIEW
1001 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
1002 layout(location = 12) in vec4 diffuse_light_interp;
1003 layout(location = 13) in vec4 specular_light_interp;
1004 #endif
1005
1006 #if defined(POINT_SIZE_USED) && defined(POINT_COORD_USED)
1007 layout(location = 14) in vec2 point_coord_interp;
1008 #endif
1009
1010 //defines to keep compatibility with vertex
1011
1012 #ifdef USE_MULTIVIEW
1013 #define projection_matrix scene_data.projection_matrix_view[ViewIndex]
1014 #define inv_projection_matrix scene_data.inv_projection_matrix_view[ViewIndex]
1015 #else
1016 #define projection_matrix scene_data.projection_matrix
1017 #define inv_projection_matrix scene_data.inv_projection_matrix
1018 #endif
1019
1020 #define global_time scene_data_block.data.time
1021
1022 #if defined(ENABLE_SSS) && defined(ENABLE_TRANSMITTANCE)
1023 //both required for transmittance to be enabled
1024 #define LIGHT_TRANSMITTANCE_USED
1025 #endif
1026
1027 #ifdef MATERIAL_UNIFORMS_USED
1028 /* clang-format off */
1029 layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms {
1030 #MATERIAL_UNIFORMS
1031 } material;
1032 /* clang-format on */
1033 #endif
1034
1035 #GLOBALS
1036
1037 #ifdef MODE_RENDER_DEPTH
1038
1039 #ifdef MODE_RENDER_MATERIAL
1040
1041 layout(location = 0) out vec4 albedo_output_buffer;
1042 layout(location = 1) out vec4 normal_output_buffer;
1043 layout(location = 2) out vec4 orm_output_buffer;
1044 layout(location = 3) out vec4 emission_output_buffer;
1045 layout(location = 4) out float depth_output_buffer;
1046
1047 #endif // MODE_RENDER_MATERIAL
1048
1049 #ifdef MODE_RENDER_NORMAL_ROUGHNESS
1050 layout(location = 0) out vec4 normal_roughness_output_buffer;
1051
1052 #ifdef MODE_RENDER_VOXEL_GI
1053 layout(location = 1) out uvec2 voxel_gi_buffer;
1054 #endif
1055
1056 #endif //MODE_RENDER_NORMAL
1057 #else // RENDER DEPTH
1058
1059 #ifdef MODE_SEPARATE_SPECULAR
1060
1061 layout(location = 0) out vec4 diffuse_buffer; //diffuse (rgb) and roughness
1062 layout(location = 1) out vec4 specular_buffer; //specular and SSS (subsurface scatter)
1063 #else
1064
1065 layout(location = 0) out vec4 frag_color;
1066 #endif // MODE_SEPARATE_SPECULAR
1067
1068 #endif // RENDER DEPTH
1069
1070 #ifdef MOTION_VECTORS
1071 layout(location = 2) out vec2 motion_vector;
1072 #endif
1073
1074 #include "../scene_forward_aa_inc.glsl"
1075
1076 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1077
1078 // Default to SPECULAR_SCHLICK_GGX.
1079 #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON)
1080 #define SPECULAR_SCHLICK_GGX
1081 #endif
1082
1083 #include "../scene_forward_lights_inc.glsl"
1084
1085 #include "../scene_forward_gi_inc.glsl"
1086
1087 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1088
1089 #ifndef MODE_RENDER_DEPTH
1090
1091 vec4 volumetric_fog_process(vec2 screen_uv, float z) {
1092 vec3 fog_pos = vec3(screen_uv, z * implementation_data.volumetric_fog_inv_length);
1093 if (fog_pos.z < 0.0) {
1094 return vec4(0.0, 0.0, 0.0, 1.0);
1095 } else if (fog_pos.z < 1.0) {
1096 fog_pos.z = pow(fog_pos.z, implementation_data.volumetric_fog_detail_spread);
1097 }
1098
1099 return texture(sampler3D(volumetric_fog_texture, SAMPLER_LINEAR_CLAMP), fog_pos);
1100 }
1101
1102 vec4 fog_process(vec3 vertex) {
1103 vec3 fog_color = scene_data_block.data.fog_light_color;
1104
1105 if (scene_data_block.data.fog_aerial_perspective > 0.0) {
1106 vec3 sky_fog_color = vec3(0.0);
1107 vec3 cube_view = scene_data_block.data.radiance_inverse_xform * vertex;
1108 // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
1109 float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data_block.data.z_near) / (scene_data_block.data.z_far - scene_data_block.data.z_near));
1110 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1111 float roughness_lod, blend;
1112 blend = modf(mip_level * MAX_ROUGHNESS_LOD, roughness_lod);
1113 float cube_lod = vec3_to_oct_lod(dFdx(cube_view), dFdy(cube_view), scene_data_block.data.radiance_pixel_size);
1114 vec2 cube_uv = vec3_to_oct_with_border(cube_view, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1115 vec3 sky_sample_a = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(cube_uv, roughness_lod), cube_lod).rgb;
1116 vec3 sky_sample_b = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(cube_uv, roughness_lod + 1), cube_lod).rgb;
1117 sky_fog_color = mix(sky_sample_a, sky_sample_b, blend);
1118 #else
1119 float roughness_lod = mip_level * MAX_ROUGHNESS_LOD;
1120 vec2 cube_uv = vec3_to_oct_with_border(cube_view, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1121 sky_fog_color = textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), cube_uv, roughness_lod).rgb;
1122 #endif //USE_RADIANCE_OCTMAP_ARRAY
1123 fog_color = mix(fog_color, sky_fog_color, scene_data_block.data.fog_aerial_perspective);
1124 }
1125
1126 if (scene_data_block.data.fog_sun_scatter > 0.001) {
1127 vec4 sun_scatter = vec4(0.0);
1128 float sun_total = 0.0;
1129 vec3 view = normalize(vertex);
1130
1131 for (uint i = 0; i < scene_data_block.data.directional_light_count; i++) {
1132 vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
1133 float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
1134 fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
1135 }
1136 }
1137
1138 float fog_amount = 0.0;
1139
1140 if (sc_use_depth_fog()) {
1141 float fog_z = smoothstep(scene_data_block.data.fog_depth_begin, scene_data_block.data.fog_depth_end, length(vertex));
1142 float fog_quad_amount = pow(fog_z, scene_data_block.data.fog_depth_curve) * scene_data_block.data.fog_density;
1143 fog_amount = fog_quad_amount;
1144 } else {
1145 fog_amount = 1 - exp(min(0.0, -length(vertex) * scene_data_block.data.fog_density));
1146 }
1147
1148 if (abs(scene_data_block.data.fog_height_density) >= 0.0001) {
1149 mat4 inv_view_matrix = transpose(mat4(scene_data_block.data.inv_view_matrix[0],
1150 scene_data_block.data.inv_view_matrix[1],
1151 scene_data_block.data.inv_view_matrix[2],
1152 vec4(0.0, 0.0, 0.0, 1.0)));
1153
1154 float y = (inv_view_matrix * vec4(vertex, 1.0)).y;
1155
1156 float y_dist = y - scene_data_block.data.fog_height;
1157
1158 float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data_block.data.fog_height_density));
1159
1160 fog_amount = max(vfog_amount, fog_amount);
1161 }
1162
1163 return vec4(fog_color, fog_amount);
1164 }
1165
1166 void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
1167 uint item_min_max = cluster_buffer.data[p_offset];
1168 item_min = item_min_max & 0xFFFFu;
1169 item_max = item_min_max >> 16;
1170
1171 item_from = item_min >> 5;
1172 item_to = (item_max == 0) ? 0 : ((item_max - 1) >> 5) + 1; //side effect of how it is stored, as item_max 0 means no elements
1173 }
1174
1175 uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
1176 int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
1177 int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
1178 return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
1179 }
1180
1181 #endif //!MODE_RENDER DEPTH
1182
1183 #if defined(MODE_RENDER_NORMAL_ROUGHNESS) || defined(MODE_RENDER_MATERIAL)
1184 // https://advances.realtimerendering.com/s2010/Kaplanyan-CryEngine3(SIGGRAPH%202010%20Advanced%20RealTime%20Rendering%20Course).pdf
1185 vec3 encode24(vec3 v) {
1186 // Unsigned normal (handles most symmetry)
1187 vec3 vNormalUns = abs(v);
1188 // Get the major axis for our collapsed cubemap lookup
1189 float maxNAbs = max(vNormalUns.z, max(vNormalUns.x, vNormalUns.y));
1190 // Get the collapsed cubemap texture coordinates
1191 vec2 vTexCoord = vNormalUns.z < maxNAbs ? (vNormalUns.y < maxNAbs ? vNormalUns.yz : vNormalUns.xz) : vNormalUns.xy;
1192 vTexCoord /= maxNAbs;
1193 vTexCoord = vTexCoord.x < vTexCoord.y ? vTexCoord.yx : vTexCoord.xy;
1194 // Stretch:
1195 vTexCoord.y /= vTexCoord.x;
1196 float fFittingScale = texture(sampler2D(best_fit_normal_texture, SAMPLER_NEAREST_CLAMP), vTexCoord).r;
1197 // Make vector touch unit cube
1198 vec3 result = v / maxNAbs;
1199 // scale the normal to get the best fit
1200 result *= fFittingScale;
1201 return result;
1202 }
1203 #endif // MODE_RENDER_NORMAL_ROUGHNESS
1204
1205 void fragment_shader(in SceneData scene_data) {
1206 uint instance_index = instance_index_interp;
1207
1208 #ifdef PREMUL_ALPHA_USED
1209 float premul_alpha = 1.0;
1210 #endif // PREMUL_ALPHA_USED
1211 //lay out everything, whatever is unused is optimized away anyway
1212 vec3 vertex = vertex_interp;
1213 #ifdef USE_MULTIVIEW
1214 vec3 eye_offset = scene_data.eye_offset[ViewIndex].xyz;
1215 vec3 view_highp = -normalize(vertex_interp - eye_offset);
1216
1217 // UV in our combined frustum space is used for certain screen uv processes where it's
1218 // overkill to render separate left and right eye views
1219 vec2 combined_uv = (combined_projected.xy / combined_projected.w) * 0.5 + 0.5;
1220 #else
1221 vec3 eye_offset = vec3(0.0, 0.0, 0.0);
1222 vec3 view_highp = -normalize(vertex_interp);
1223 #endif
1224 vec3 albedo_highp = vec3(1.0);
1225 vec3 backlight = vec3(0.0);
1226 vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0);
1227 float transmittance_depth = 0.0;
1228 float transmittance_boost = 0.0;
1229 float metallic_highp = 0.0;
1230 float specular = 0.5;
1231 vec3 emission = vec3(0.0);
1232 float roughness_highp = 1.0;
1233 float rim = 0.0;
1234 float rim_tint = 0.0;
1235 float clearcoat = 0.0;
1236 float clearcoat_roughness = 0.0;
1237 float anisotropy = 0.0;
1238 vec2 anisotropy_flow = vec2(1.0, 0.0);
1239 vec3 energy_compensation = vec3(1.0);
1240 vec4 fog = vec4(0.0, 0.0, 0.0, 1.0);
1241 #if defined(CUSTOM_RADIANCE_USED)
1242 vec4 custom_radiance = vec4(0.0);
1243 #endif
1244 #if defined(CUSTOM_IRRADIANCE_USED)
1245 vec4 custom_irradiance = vec4(0.0);
1246 #endif
1247
1248 float ao = 1.0;
1249 float ao_light_affect = 0.0;
1250
1251 float alpha_highp = float(instances.data[instance_index].flags >> INSTANCE_FLAGS_FADE_SHIFT) / float(255.0);
1252
1253 #ifdef TANGENT_USED
1254 vec3 binormal = binormal_interp;
1255 vec3 tangent = tangent_interp;
1256 #else
1257 vec3 binormal = vec3(0.0);
1258 vec3 tangent = vec3(0.0);
1259 #endif
1260
1261 #ifdef NORMAL_USED
1262 vec3 normal_highp = normal_interp;
1263 #if defined(DO_SIDE_CHECK)
1264 if (!gl_FrontFacing) {
1265 normal_highp = -normal_highp;
1266 }
1267 #endif // DO_SIDE_CHECK
1268 #endif // NORMAL_USED
1269
1270 #ifdef UV_USED
1271 vec2 uv = uv_interp;
1272 #endif
1273
1274 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
1275 vec2 uv2 = uv2_interp;
1276 #endif
1277
1278 #if defined(COLOR_USED)
1279 vec4 color = color_interp;
1280 #endif
1281
1282 #if defined(NORMAL_MAP_USED)
1283
1284 vec3 normal_map = vec3(0.5);
1285 #endif
1286
1287 #if defined(BENT_NORMAL_MAP_USED)
1288 vec3 bent_normal_vector = vec3(0.5);
1289 vec3 bent_normal_map = vec3(0.5);
1290 #endif
1291
1292 float normal_map_depth = 1.0;
1293
1294 vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size;
1295
1296 float sss_strength = 0.0;
1297
1298 #ifdef ALPHA_SCISSOR_USED
1299 float alpha_scissor_threshold = 1.0;
1300 #endif // ALPHA_SCISSOR_USED
1301
1302 #ifdef ALPHA_HASH_USED
1303 float alpha_hash_scale = 1.0;
1304 #endif // ALPHA_HASH_USED
1305
1306 #ifdef ALPHA_ANTIALIASING_EDGE_USED
1307 float alpha_antialiasing_edge = 0.0;
1308 vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
1309 #endif // ALPHA_ANTIALIASING_EDGE_USED
1310
1311 mat4 inv_view_matrix = transpose(mat4(scene_data.inv_view_matrix[0],
1312 scene_data.inv_view_matrix[1],
1313 scene_data.inv_view_matrix[2],
1314 vec4(0.0, 0.0, 0.0, 1.0)));
1315 mat4 read_model_matrix = transpose(mat4(instances.data[instance_index].transform[0],
1316 instances.data[instance_index].transform[1],
1317 instances.data[instance_index].transform[2],
1318 vec4(0.0, 0.0, 0.0, 1.0)));
1319
1320 #ifdef LIGHT_VERTEX_USED
1321 vec3 light_vertex = vertex;
1322 #endif //LIGHT_VERTEX_USED
1323
1324 mat3 model_normal_matrix;
1325 if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
1326 model_normal_matrix = transpose(inverse(mat3(read_model_matrix)));
1327 } else {
1328 model_normal_matrix = mat3(read_model_matrix);
1329 }
1330
1331 mat4 read_view_matrix = transpose(mat4(scene_data.view_matrix[0],
1332 scene_data.view_matrix[1],
1333 scene_data.view_matrix[2],
1334 vec4(0.0, 0.0, 0.0, 1.0)));
1335 vec2 read_viewport_size = scene_data.viewport_size;
1336
1337 #ifdef POINT_COORD_USED
1338 #ifdef POINT_SIZE_USED
1339 vec2 point_coord;
1340 if (sc_emulate_point_size) {
1341 point_coord = point_coord_interp;
1342 } else {
1343 point_coord = gl_PointCoord;
1344 }
1345 #else // !POINT_SIZE_USED
1346 vec2 point_coord = vec2(0.5);
1347 #endif
1348 #endif
1349
1350 {
1351 #CODE : FRAGMENT
1352 }
1353
1354 float roughness = roughness_highp;
1355 float metallic = metallic_highp;
1356 vec3 albedo = albedo_highp;
1357 float alpha = alpha_highp;
1358 #ifdef NORMAL_USED
1359 vec3 normal = normal_highp;
1360 #endif
1361
1362 #ifdef LIGHT_TRANSMITTANCE_USED
1363 transmittance_color.a *= sss_strength;
1364 #endif
1365
1366 #ifdef LIGHT_VERTEX_USED
1367 vertex = light_vertex;
1368 #ifdef USE_MULTIVIEW
1369 vec3 view = -normalize(vertex - eye_offset);
1370 #else
1371 vec3 view = -normalize(vertex);
1372 #endif //USE_MULTIVIEW
1373 #else
1374 vec3 view = view_highp;
1375 #endif //LIGHT_VERTEX_USED
1376
1377 #ifdef NORMAL_USED
1378 vec3 geo_normal = normalize(normal);
1379 #endif // NORMAL_USED
1380
1381 #ifndef USE_SHADOW_TO_OPACITY
1382
1383 #ifdef ALPHA_SCISSOR_USED
1384 #ifdef MODE_RENDER_MATERIAL
1385 if (alpha < alpha_scissor_threshold) {
1386 alpha = 0.0;
1387 } else {
1388 alpha = 1.0;
1389 }
1390 #else
1391 if (alpha < alpha_scissor_threshold) {
1392 discard;
1393 }
1394 #endif // MODE_RENDER_MATERIAL
1395 #endif // ALPHA_SCISSOR_USED
1396
1397 // alpha hash can be used in unison with alpha antialiasing
1398 #ifdef ALPHA_HASH_USED
1399 vec3 object_pos = (inverse(read_model_matrix) * inv_view_matrix * vec4(vertex, 1.0)).xyz;
1400 #ifdef MODE_RENDER_MATERIAL
1401 if (alpha < compute_alpha_hash_threshold(object_pos, alpha_hash_scale)) {
1402 alpha = 0.0;
1403 } else {
1404 alpha = 1.0;
1405 }
1406 #else
1407 if (alpha < compute_alpha_hash_threshold(object_pos, alpha_hash_scale)) {
1408 discard;
1409 }
1410 #endif // MODE_RENDER_MATERIAL
1411 #endif // ALPHA_HASH_USED
1412
1413 // If we are not edge antialiasing, we need to remove the output alpha channel from scissor and hash
1414 #if (defined(ALPHA_SCISSOR_USED) || defined(ALPHA_HASH_USED)) && !defined(ALPHA_ANTIALIASING_EDGE_USED) && !defined(MODE_RENDER_MATERIAL)
1415 alpha = 1.0;
1416 #endif
1417
1418 #ifdef ALPHA_ANTIALIASING_EDGE_USED
1419 // If alpha scissor is used, we must further the edge threshold, otherwise we won't get any edge feather
1420 #ifdef ALPHA_SCISSOR_USED
1421 alpha_antialiasing_edge = clamp(alpha_scissor_threshold + alpha_antialiasing_edge, 0.0, 1.0);
1422 #endif
1423 alpha = compute_alpha_antialiasing_edge(alpha, alpha_texture_coordinate, alpha_antialiasing_edge);
1424 #endif // ALPHA_ANTIALIASING_EDGE_USED
1425
1426 #ifdef MODE_RENDER_DEPTH
1427 #if defined(USE_OPAQUE_PREPASS) || defined(ALPHA_ANTIALIASING_EDGE_USED)
1428 if (alpha < scene_data.opaque_prepass_threshold) {
1429 discard;
1430 }
1431 #endif // USE_OPAQUE_PREPASS || ALPHA_ANTIALIASING_EDGE_USED
1432 #endif // MODE_RENDER_DEPTH
1433
1434 #endif // !USE_SHADOW_TO_OPACITY
1435
1436 #if defined(NORMAL_MAP_USED)
1437 normal_map.xy = normal_map.xy * 2.0 - 1.0;
1438 normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
1439
1440 // Tangent-space transformation is performed using unnormalized TBN vectors, per MikkTSpace.
1441 // See: http://www.mikktspace.com/
1442 normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
1443 #elif defined(NORMAL_USED)
1444 normal = geo_normal;
1445 #endif // NORMAL_MAP_USED
1446
1447 #ifdef BENT_NORMAL_MAP_USED
1448 bent_normal_map.xy = bent_normal_map.xy * 2.0 - 1.0;
1449 bent_normal_map.z = sqrt(max(0.0, 1.0 - dot(bent_normal_map.xy, bent_normal_map.xy)));
1450
1451 bent_normal_vector = normalize(tangent * bent_normal_map.x + binormal * bent_normal_map.y + normal * bent_normal_map.z);
1452 #endif
1453
1454 #ifdef LIGHT_ANISOTROPY_USED
1455
1456 if (anisotropy > 0.01) {
1457 mat3 rot = mat3(normalize(tangent), normalize(binormal), normal);
1458 // Make local to space.
1459 tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
1460 binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
1461 }
1462
1463 #endif
1464
1465 #ifdef ENABLE_CLIP_ALPHA
1466 #ifdef MODE_RENDER_MATERIAL
1467 if (albedo.a < 0.99) {
1468 // Used for doublepass and shadowmapping.
1469 albedo.a = 0.0;
1470 alpha = 0.0;
1471 } else {
1472 albedo.a = 1.0;
1473 alpha = 1.0;
1474 }
1475 #else
1476 if (albedo.a < 0.99) {
1477 //used for doublepass and shadowmapping
1478 discard;
1479 }
1480 #endif // MODE_RENDER_MATERIAL
1481 #endif
1482
1483 /////////////////////// FOG //////////////////////
1484 #ifndef MODE_RENDER_DEPTH
1485
1486 #ifndef FOG_DISABLED
1487 #ifndef CUSTOM_FOG_USED
1488 // fog must be processed as early as possible and then packed.
1489 // to maximize VGPR usage
1490 // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
1491
1492 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_FOG)) {
1493 fog = fog_process(vertex);
1494 // Premultiply by opacity and convert opacity to transmittance to match volumetric fog.
1495 fog.rgb *= fog.a;
1496 fog.a = 1.0 - fog.a;
1497 }
1498
1499 if (implementation_data.volumetric_fog_enabled) {
1500 #ifdef USE_MULTIVIEW
1501 vec4 volumetric_fog = volumetric_fog_process(combined_uv, -vertex.z);
1502 #else
1503 vec4 volumetric_fog = volumetric_fog_process(screen_uv, -vertex.z);
1504 #endif
1505 vec4 res = vec4(0.0);
1506 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_FOG)) {
1507 //must use the full blending equation here to blend fogs
1508 if (sc_fog_use_legacy_blending()) {
1509 res.a = mix(0.0, fog.a, volumetric_fog.a);
1510 res.rgb = mix(volumetric_fog.rgb, fog.rgb, volumetric_fog.a);
1511 } else {
1512 res.a = fog.a * volumetric_fog.a;
1513 res.rgb = fog.rgb * volumetric_fog.a + volumetric_fog.rgb;
1514 }
1515 } else {
1516 res = volumetric_fog;
1517 if (sc_fog_use_legacy_blending()) {
1518 res.rgb *= 1.0 - res.a;
1519 }
1520 }
1521 fog = res;
1522 }
1523 #else
1524 // Premultiply by opacity and convert opacity to transmittance to match volumetric fog.
1525 fog.rgb *= fog.a;
1526 fog.a = 1.0 - fog.a;
1527 #endif //!CUSTOM_FOG_USED
1528
1529 uint fog_rg = packHalf2x16(fog.rg);
1530 uint fog_ba = packHalf2x16(fog.ba);
1531
1532 #endif //!FOG_DISABLED
1533 #endif //!MODE_RENDER_DEPTH
1534
1535 /////////////////////// DECALS ////////////////////////////////
1536
1537 #ifndef MODE_RENDER_DEPTH
1538
1539 #ifdef USE_MULTIVIEW
1540 uvec2 cluster_pos = uvec2(combined_uv.xy / scene_data.screen_pixel_size) >> implementation_data.cluster_shift;
1541 #else
1542 uvec2 cluster_pos = uvec2(gl_FragCoord.xy) >> implementation_data.cluster_shift;
1543 #endif
1544 uint cluster_offset = (implementation_data.cluster_width * cluster_pos.y + cluster_pos.x) * (implementation_data.max_cluster_element_count_div_32 + 32);
1545
1546 uint cluster_z = uint(clamp((-vertex.z / scene_data.z_far) * 32.0, 0.0, 31.0));
1547
1548 //used for interpolating anything cluster related
1549 vec3 vertex_ddx = dFdx(vertex);
1550 vec3 vertex_ddy = dFdy(vertex);
1551
1552 { // process decals
1553
1554 uint cluster_decal_offset = cluster_offset + implementation_data.cluster_type_size * 2;
1555
1556 uint item_min;
1557 uint item_max;
1558 uint item_from;
1559 uint item_to;
1560
1561 cluster_get_item_range(cluster_decal_offset + implementation_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
1562
1563 item_from = subgroupBroadcastFirst(subgroupMin(item_from));
1564 item_to = subgroupBroadcastFirst(subgroupMax(item_to));
1565
1566 for (uint i = item_from; i < item_to; i++) {
1567 uint mask = cluster_buffer.data[cluster_decal_offset + i];
1568 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
1569
1570 uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
1571 while (merged_mask != 0) {
1572 uint bit = findMSB(merged_mask);
1573 merged_mask &= ~(1u << bit);
1574
1575 if (((1u << bit) & mask) == 0) { //do not process if not originally here
1576 continue;
1577 }
1578
1579 uint decal_index = 32 * i + bit;
1580
1581 if (!bool(decals.data[decal_index].mask & instances.data[instance_index].layer_mask)) {
1582 continue; //not masked
1583 }
1584
1585 vec3 uv_local = (decals.data[decal_index].xform * vec4(vertex, 1.0)).xyz;
1586 if (any(lessThan(uv_local, vec3(0.0, -1.0, 0.0))) || any(greaterThan(uv_local, vec3(1.0)))) {
1587 continue; //out of decal
1588 }
1589
1590 float fade = pow(1.0 - (uv_local.y > 0.0 ? uv_local.y : -uv_local.y), uv_local.y > 0.0 ? decals.data[decal_index].upper_fade : decals.data[decal_index].lower_fade);
1591
1592 if (decals.data[decal_index].normal_fade > 0.0) {
1593 fade *= smoothstep(decals.data[decal_index].normal_fade, 1.0, dot(geo_normal, decals.data[decal_index].normal) * 0.5 + 0.5);
1594 }
1595
1596 //we need ddx/ddy for mipmaps, so simulate them
1597 vec2 ddx = (decals.data[decal_index].xform * vec4(vertex_ddx, 0.0)).xz;
1598 vec2 ddy = (decals.data[decal_index].xform * vec4(vertex_ddy, 0.0)).xz;
1599
1600 if (decals.data[decal_index].albedo_rect != vec4(0.0)) {
1601 //has albedo
1602 vec4 decal_albedo;
1603 if (sc_decal_use_mipmaps()) {
1604 decal_albedo = textureGrad(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].albedo_rect.zw + decals.data[decal_index].albedo_rect.xy, ddx * decals.data[decal_index].albedo_rect.zw, ddy * decals.data[decal_index].albedo_rect.zw);
1605 } else {
1606 decal_albedo = textureLod(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].albedo_rect.zw + decals.data[decal_index].albedo_rect.xy, 0.0);
1607 }
1608 decal_albedo *= decals.data[decal_index].modulate;
1609 decal_albedo.a *= fade;
1610 albedo = mix(albedo, decal_albedo.rgb, decal_albedo.a * decals.data[decal_index].albedo_mix);
1611
1612 if (decals.data[decal_index].normal_rect != vec4(0.0)) {
1613 vec3 decal_normal;
1614 if (sc_decal_use_mipmaps()) {
1615 decal_normal = textureGrad(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].normal_rect.zw + decals.data[decal_index].normal_rect.xy, ddx * decals.data[decal_index].normal_rect.zw, ddy * decals.data[decal_index].normal_rect.zw).xyz;
1616 } else {
1617 decal_normal = textureLod(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].normal_rect.zw + decals.data[decal_index].normal_rect.xy, 0.0).xyz;
1618 }
1619 decal_normal.xy = decal_normal.xy * vec2(2.0, -2.0) - vec2(1.0, -1.0); //users prefer flipped y normal maps in most authoring software
1620 decal_normal.z = sqrt(max(0.0, 1.0 - dot(decal_normal.xy, decal_normal.xy)));
1621 //convert to view space, use xzy because y is up
1622 decal_normal = (decals.data[decal_index].normal_xform * decal_normal.xzy).xyz;
1623
1624 normal = normalize(mix(normal, decal_normal, decal_albedo.a));
1625 }
1626
1627 if (decals.data[decal_index].orm_rect != vec4(0.0)) {
1628 vec3 decal_orm;
1629 if (sc_decal_use_mipmaps()) {
1630 decal_orm = textureGrad(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].orm_rect.zw + decals.data[decal_index].orm_rect.xy, ddx * decals.data[decal_index].orm_rect.zw, ddy * decals.data[decal_index].orm_rect.zw).xyz;
1631 } else {
1632 decal_orm = textureLod(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].orm_rect.zw + decals.data[decal_index].orm_rect.xy, 0.0).xyz;
1633 }
1634 ao = mix(ao, decal_orm.r, decal_albedo.a);
1635 roughness = mix(roughness, decal_orm.g, decal_albedo.a);
1636 metallic = mix(metallic, decal_orm.b, decal_albedo.a);
1637 }
1638 }
1639
1640 if (decals.data[decal_index].emission_rect != vec4(0.0)) {
1641 //emission is additive, so its independent from albedo
1642 if (sc_decal_use_mipmaps()) {
1643 emission += textureGrad(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].emission_rect.zw + decals.data[decal_index].emission_rect.xy, ddx * decals.data[decal_index].emission_rect.zw, ddy * decals.data[decal_index].emission_rect.zw).xyz * decals.data[decal_index].modulate.rgb * decals.data[decal_index].emission_energy * fade;
1644 } else {
1645 emission += textureLod(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].emission_rect.zw + decals.data[decal_index].emission_rect.xy, 0.0).xyz * decals.data[decal_index].modulate.rgb * decals.data[decal_index].emission_energy * fade;
1646 }
1647 }
1648 }
1649 }
1650 }
1651
1652 //pack albedo until needed again, saves 2 VGPRs in the meantime
1653
1654 #endif //not render depth
1655 /////////////////////// LIGHTING //////////////////////////////
1656
1657 #ifdef NORMAL_USED
1658 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_ROUGHNESS_LIMITER)) {
1659 //https://www.jp.square-enix.com/tech/library/pdf/ImprovedGeometricSpecularAA.pdf
1660 float roughness2 = roughness * roughness;
1661 vec3 dndu = dFdx(normal), dndv = dFdy(normal);
1662 float variance = scene_data.roughness_limiter_amount * (dot(dndu, dndu) + dot(dndv, dndv));
1663 float kernelRoughness2 = min(2.0 * variance, scene_data.roughness_limiter_limit); //limit effect
1664 float filteredRoughness2 = min(1.0, roughness2 + kernelRoughness2);
1665 roughness = sqrt(filteredRoughness2);
1666
1667 // Reject very small roughness values. Lack of precision can collapse
1668 // roughness^4 to 0 in GGX specular equations and cause divisions by zero.
1669 if (roughness < 0.00000001) {
1670 roughness = 0.0;
1671 }
1672 }
1673 #endif
1674 //apply energy conservation
1675
1676 vec3 direct_specular_light = vec3(0.0, 0.0, 0.0);
1677 vec3 indirect_specular_light = vec3(0.0, 0.0, 0.0);
1678 vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
1679 vec3 ambient_light = vec3(0.0, 0.0, 0.0);
1680 #ifndef MODE_UNSHADED
1681 // Used in regular draw pass and when drawing SDFs for SDFGI and materials for VoxelGI.
1682 emission *= scene_data.emissive_exposure_normalization;
1683 #endif
1684
1685 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1686
1687 #ifndef AMBIENT_LIGHT_DISABLED
1688 // Use bent normal for indirect lighting where possible
1689 #ifdef BENT_NORMAL_MAP_USED
1690 vec3 indirect_normal = bent_normal_vector;
1691 #else
1692 vec3 indirect_normal = normal;
1693 #endif
1694
1695 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_REFLECTION_CUBEMAP)) {
1696 #ifdef LIGHT_ANISOTROPY_USED
1697 // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
1698 vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
1699 vec3 anisotropic_tangent = cross(anisotropic_direction, view);
1700 vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
1701 vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
1702 vec3 ref_vec = reflect(-view, bent_normal);
1703 ref_vec = mix(ref_vec, bent_normal, roughness * roughness);
1704 #else
1705 vec3 ref_vec = reflect(-view, normal);
1706 ref_vec = mix(ref_vec, normal, roughness * roughness);
1707 #endif
1708
1709 float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
1710 ref_vec = scene_data.radiance_inverse_xform * ref_vec;
1711
1712 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1713
1714 float roughness_lod, blend;
1715
1716 blend = modf(sqrt(roughness) * MAX_ROUGHNESS_LOD, roughness_lod);
1717
1718 float ref_lod = vec3_to_oct_lod(dFdx(ref_vec), dFdy(ref_vec), scene_data_block.data.radiance_pixel_size);
1719 vec2 ref_uv = vec3_to_oct_with_border(ref_vec, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1720 vec3 indirect_sample_a = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, roughness_lod), ref_lod).rgb;
1721 vec3 indirect_sample_b = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, roughness_lod + 1), ref_lod).rgb;
1722 indirect_specular_light = mix(indirect_sample_a, indirect_sample_b, blend);
1723
1724 #else
1725 float roughness_lod = sqrt(roughness) * MAX_ROUGHNESS_LOD;
1726 vec2 ref_uv = vec3_to_oct_with_border(ref_vec, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1727 indirect_specular_light = textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), ref_uv, roughness_lod).rgb;
1728
1729 #endif //USE_RADIANCE_OCTMAP_ARRAY
1730 indirect_specular_light *= scene_data.IBL_exposure_normalization;
1731 indirect_specular_light *= horizon * horizon;
1732 indirect_specular_light *= scene_data.ambient_light_color_energy.a;
1733 }
1734
1735 #if defined(CUSTOM_RADIANCE_USED)
1736 indirect_specular_light = mix(indirect_specular_light, custom_radiance.rgb, custom_radiance.a);
1737 #endif
1738
1739 #ifndef USE_LIGHTMAP
1740 //lightmap overrides everything
1741 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_AMBIENT_LIGHT)) {
1742 ambient_light = scene_data.ambient_light_color_energy.rgb;
1743
1744 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_AMBIENT_CUBEMAP)) {
1745 vec3 ambient_dir = scene_data.radiance_inverse_xform * indirect_normal;
1746 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1747 float ambient_lod = vec3_to_oct_lod(dFdx(ambient_dir), dFdy(ambient_dir), scene_data_block.data.radiance_pixel_size);
1748 vec2 ambient_uv = vec3_to_oct_with_border(ambient_dir, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1749 vec3 cubemap_ambient = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ambient_uv, MAX_ROUGHNESS_LOD), ambient_lod).rgb;
1750 #else
1751 float roughness_lod = MAX_ROUGHNESS_LOD;
1752 vec2 ambient_uv = vec3_to_oct_with_border(ambient_dir, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1753 vec3 cubemap_ambient = textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), ambient_uv, roughness_lod).rgb;
1754 #endif //USE_RADIANCE_OCTMAP_ARRAY
1755 cubemap_ambient *= scene_data.IBL_exposure_normalization;
1756 ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
1757 }
1758 }
1759 #endif // USE_LIGHTMAP
1760 #if defined(CUSTOM_IRRADIANCE_USED)
1761 ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a);
1762 #endif
1763
1764 #ifdef LIGHT_CLEARCOAT_USED
1765
1766 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_REFLECTION_CUBEMAP)) {
1767 float NoV = max(dot(geo_normal, view), 0.0001); // We want to use geometric normal, not normal_map
1768 vec3 ref_vec = reflect(-view, geo_normal);
1769 ref_vec = mix(ref_vec, geo_normal, clearcoat_roughness * clearcoat_roughness);
1770 // The clear coat layer assumes an IOR of 1.5 (4% reflectance)
1771 float Fc = clearcoat * (0.04 + 0.96 * SchlickFresnel(NoV));
1772 float attenuation = 1.0 - Fc;
1773 ambient_light *= attenuation;
1774 indirect_specular_light *= attenuation;
1775
1776 float horizon = min(1.0 + dot(ref_vec, indirect_normal), 1.0);
1777 ref_vec = scene_data.radiance_inverse_xform * ref_vec;
1778 float roughness_lod = mix(0.001, 0.1, sqrt(clearcoat_roughness)) * MAX_ROUGHNESS_LOD;
1779 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1780
1781 float lod, blend;
1782 blend = modf(roughness_lod, lod);
1783
1784 float ref_lod = vec3_to_oct_lod(dFdx(ref_vec), dFdy(ref_vec), scene_data_block.data.radiance_pixel_size);
1785 vec2 ref_uv = vec3_to_oct_with_border(ref_vec, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1786 vec3 clearcoat_sample_a = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, lod), ref_lod).rgb;
1787 vec3 clearcoat_sample_b = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, lod + 1), ref_lod).rgb;
1788 vec3 clearcoat_light = mix(clearcoat_sample_a, clearcoat_sample_b, blend);
1789
1790 #else
1791 vec2 ref_uv = vec3_to_oct_with_border(ref_vec, vec2(scene_data_block.data.radiance_border_size, 1.0 - scene_data_block.data.radiance_border_size * 2.0));
1792 vec3 clearcoat_light = textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), ref_uv, roughness_lod).rgb;
1793
1794 #endif //USE_RADIANCE_OCTMAP_ARRAY
1795 indirect_specular_light += clearcoat_light * horizon * horizon * Fc * scene_data.ambient_light_color_energy.a;
1796 }
1797 #endif // LIGHT_CLEARCOAT_USED
1798 #endif // !AMBIENT_LIGHT_DISABLED
1799 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1800
1801 //radiance
1802
1803 /// GI ///
1804 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1805 #ifndef AMBIENT_LIGHT_DISABLED
1806 #ifdef USE_LIGHTMAP
1807
1808 //lightmap
1809 if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture
1810 uint index = instances.data[instance_index].gi_offset;
1811
1812 // The world normal.
1813 vec3 wnormal = mat3(inv_view_matrix) * indirect_normal;
1814
1815 // The SH coefficients used for evaluating diffuse data from SH probes.
1816 const float c[5] = float[](
1817 0.886227, // l0 sqrt(1.0/(4.0*PI)) * PI
1818 1.023327, // l1 sqrt(3.0/(4.0*PI)) * PI*2.0/3.0
1819 0.858086, // l2n2, l2n1, l2p1 sqrt(15.0/(4.0*PI)) * PI*1.0/4.0
1820 0.247708, // l20 sqrt(5.0/(16.0*PI)) * PI*1.0/4.0
1821 0.429043 // l2p2 sqrt(15.0/(16.0*PI))* PI*1.0/4.0
1822 );
1823
1824 ambient_light += (c[0] * lightmap_captures.data[index].sh[0].rgb +
1825 c[1] * lightmap_captures.data[index].sh[1].rgb * wnormal.y +
1826 c[1] * lightmap_captures.data[index].sh[2].rgb * wnormal.z +
1827 c[1] * lightmap_captures.data[index].sh[3].rgb * wnormal.x +
1828 c[2] * lightmap_captures.data[index].sh[4].rgb * wnormal.x * wnormal.y +
1829 c[2] * lightmap_captures.data[index].sh[5].rgb * wnormal.y * wnormal.z +
1830 c[3] * lightmap_captures.data[index].sh[6].rgb * (3.0 * wnormal.z * wnormal.z - 1.0) +
1831 c[2] * lightmap_captures.data[index].sh[7].rgb * wnormal.x * wnormal.z +
1832 c[4] * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y)) *
1833 scene_data.IBL_exposure_normalization;
1834
1835 } else if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
1836 bool uses_sh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
1837 uint ofs = instances.data[instance_index].gi_offset & 0xFFFF;
1838 uint slice = instances.data[instance_index].gi_offset >> 16;
1839 vec3 uvw;
1840 uvw.xy = uv2 * instances.data[instance_index].lightmap_uv_scale.zw + instances.data[instance_index].lightmap_uv_scale.xy;
1841 uvw.z = float(slice);
1842
1843 if (uses_sh) {
1844 uvw.z *= 4.0; //SH textures use 4 times more data
1845 vec3 lm_light_l0;
1846 vec3 lm_light_l1n1;
1847 vec3 lm_light_l1_0;
1848 vec3 lm_light_l1p1;
1849
1850 if (sc_use_lightmap_bicubic_filter()) {
1851 lm_light_l0 = textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 0.0), lightmaps.data[ofs].light_texture_size).rgb;
1852 lm_light_l1n1 = (textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 1.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;
1853 lm_light_l1_0 = (textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 2.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;
1854 lm_light_l1p1 = (textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 3.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;
1855 } else {
1856 lm_light_l0 = textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb;
1857 lm_light_l1n1 = (textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb - vec3(0.5)) * 2.0;
1858 lm_light_l1_0 = (textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb - vec3(0.5)) * 2.0;
1859 lm_light_l1p1 = (textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb - vec3(0.5)) * 2.0;
1860 }
1861
1862 vec3 n = normalize(lightmaps.data[ofs].normal_xform * indirect_normal);
1863 float en = lightmaps.data[ofs].exposure_normalization;
1864
1865 ambient_light += lm_light_l0 * en;
1866 ambient_light += lm_light_l1n1 * n.y * (lm_light_l0 * en * 4.0);
1867 ambient_light += lm_light_l1_0 * n.z * (lm_light_l0 * en * 4.0);
1868 ambient_light += lm_light_l1p1 * n.x * (lm_light_l0 * en * 4.0);
1869
1870 } else {
1871 if (sc_use_lightmap_bicubic_filter()) {
1872 ambient_light += textureArray_bicubic(lightmap_textures[ofs], uvw, lightmaps.data[ofs].light_texture_size).rgb * lightmaps.data[ofs].exposure_normalization;
1873 } else {
1874 ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;
1875 }
1876 }
1877 }
1878 #else
1879
1880 if (sc_use_forward_gi() && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SDFGI)) { //has lightmap capture
1881
1882 //make vertex orientation the world one, but still align to camera
1883 vec3 cam_pos = mat3(inv_view_matrix) * vertex;
1884 vec3 cam_normal = mat3(inv_view_matrix) * indirect_normal;
1885 vec3 cam_reflection = mat3(inv_view_matrix) * reflect(-view, indirect_normal);
1886
1887 //apply y-mult
1888 cam_pos.y *= sdfgi.y_mult;
1889 cam_normal.y *= sdfgi.y_mult;
1890 cam_normal = normalize(cam_normal);
1891 cam_reflection.y *= sdfgi.y_mult;
1892 cam_normal = normalize(cam_normal);
1893 cam_reflection = normalize(cam_reflection);
1894
1895 vec4 light_accum = vec4(0.0);
1896 float weight_accum = 0.0;
1897
1898 vec4 light_blend_accum = vec4(0.0);
1899 float weight_blend_accum = 0.0;
1900
1901 float blend = -1.0;
1902
1903 // helper constants, compute once
1904
1905 uint cascade = 0xFFFFFFFF;
1906 vec3 cascade_pos;
1907 vec3 cascade_normal;
1908
1909 for (uint i = 0; i < sdfgi.max_cascades; i++) {
1910 cascade_pos = (cam_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
1911
1912 if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
1913 continue; //skip cascade
1914 }
1915
1916 cascade = i;
1917 break;
1918 }
1919
1920 if (cascade < SDFGI_MAX_CASCADES) {
1921 bool use_specular = true;
1922 float blend;
1923 vec3 diffuse, specular;
1924 sdfgi_process(cascade, cascade_pos, cam_pos, cam_normal, cam_reflection, use_specular, roughness, diffuse, specular, blend);
1925
1926 if (blend > 0.0) {
1927 //blend
1928 if (cascade == sdfgi.max_cascades - 1) {
1929 diffuse = mix(diffuse, ambient_light, blend);
1930 if (use_specular) {
1931 indirect_specular_light = mix(specular, indirect_specular_light, blend);
1932 }
1933 } else {
1934 vec3 diffuse2, specular2;
1935 float blend2;
1936 cascade_pos = (cam_pos - sdfgi.cascades[cascade + 1].position) * sdfgi.cascades[cascade + 1].to_probe;
1937 sdfgi_process(cascade + 1, cascade_pos, cam_pos, cam_normal, cam_reflection, use_specular, roughness, diffuse2, specular2, blend2);
1938 diffuse = mix(diffuse, diffuse2, blend);
1939 if (use_specular) {
1940 specular = mix(specular, specular2, blend);
1941 }
1942 }
1943 }
1944
1945 ambient_light = diffuse;
1946 if (use_specular) {
1947 indirect_specular_light = specular;
1948 }
1949 }
1950 }
1951
1952 if (sc_use_forward_gi() && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_VOXEL_GI)) { // process voxel_gi_instances
1953 uint index1 = instances.data[instance_index].gi_offset & 0xFFFF;
1954 // Make vertex orientation the world one, but still align to camera.
1955 vec3 cam_pos = mat3(inv_view_matrix) * vertex;
1956 vec3 cam_normal = mat3(inv_view_matrix) * indirect_normal;
1957 vec3 ref_vec = mat3(inv_view_matrix) * normalize(reflect(-view, indirect_normal));
1958
1959 //find arbitrary tangent and bitangent, then build a matrix
1960 vec3 v0 = abs(cam_normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
1961 vec3 tangent = normalize(cross(v0, cam_normal));
1962 vec3 bitangent = normalize(cross(tangent, cam_normal));
1963 mat3 normal_mat = mat3(tangent, bitangent, cam_normal);
1964
1965 vec4 amb_accum = vec4(0.0);
1966 vec4 spec_accum = vec4(0.0);
1967 voxel_gi_compute(index1, cam_pos, cam_normal, ref_vec, normal_mat, roughness * roughness, ambient_light, indirect_specular_light, spec_accum, amb_accum);
1968
1969 uint index2 = instances.data[instance_index].gi_offset >> 16;
1970
1971 if (index2 != 0xFFFF) {
1972 voxel_gi_compute(index2, cam_pos, cam_normal, ref_vec, normal_mat, roughness * roughness, ambient_light, indirect_specular_light, spec_accum, amb_accum);
1973 }
1974
1975 if (amb_accum.a > 0.0) {
1976 amb_accum.rgb /= amb_accum.a;
1977 }
1978
1979 if (spec_accum.a > 0.0) {
1980 spec_accum.rgb /= spec_accum.a;
1981 }
1982
1983 indirect_specular_light = spec_accum.rgb;
1984 ambient_light = amb_accum.rgb;
1985 }
1986
1987 if (!sc_use_forward_gi() && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_GI_BUFFERS)) { //use GI buffers
1988
1989 vec2 coord;
1990
1991 if (implementation_data.gi_upscale_for_msaa) {
1992 vec2 base_coord = screen_uv;
1993 vec2 closest_coord = base_coord;
1994 #ifdef USE_MULTIVIEW
1995 float closest_ang = dot(indirect_normal, normalize(textureLod(sampler2DArray(normal_roughness_buffer, SAMPLER_LINEAR_CLAMP), vec3(base_coord, ViewIndex), 0.0).xyz * 2.0 - 1.0));
1996 #else // USE_MULTIVIEW
1997 float closest_ang = dot(indirect_normal, normalize(textureLod(sampler2D(normal_roughness_buffer, SAMPLER_LINEAR_CLAMP), base_coord, 0.0).xyz * 2.0 - 1.0));
1998 #endif // USE_MULTIVIEW
1999
2000 for (int i = 0; i < 4; i++) {
2001 const vec2 neighbors[4] = vec2[](vec2(-1, 0), vec2(1, 0), vec2(0, -1), vec2(0, 1));
2002 vec2 neighbour_coord = base_coord + neighbors[i] * scene_data.screen_pixel_size;
2003 #ifdef USE_MULTIVIEW
2004 float neighbour_ang = dot(indirect_normal, normalize(textureLod(sampler2DArray(normal_roughness_buffer, SAMPLER_LINEAR_CLAMP), vec3(neighbour_coord, ViewIndex), 0.0).xyz * 2.0 - 1.0));
2005 #else // USE_MULTIVIEW
2006 float neighbour_ang = dot(indirect_normal, normalize(textureLod(sampler2D(normal_roughness_buffer, SAMPLER_LINEAR_CLAMP), neighbour_coord, 0.0).xyz * 2.0 - 1.0));
2007 #endif // USE_MULTIVIEW
2008 if (neighbour_ang > closest_ang) {
2009 closest_ang = neighbour_ang;
2010 closest_coord = neighbour_coord;
2011 }
2012 }
2013
2014 coord = closest_coord;
2015
2016 } else {
2017 coord = screen_uv;
2018 }
2019
2020 #ifdef USE_MULTIVIEW
2021 vec4 buffer_ambient = textureLod(sampler2DArray(ambient_buffer, SAMPLER_LINEAR_CLAMP), vec3(coord, ViewIndex), 0.0);
2022 vec4 buffer_reflection = textureLod(sampler2DArray(reflection_buffer, SAMPLER_LINEAR_CLAMP), vec3(coord, ViewIndex), 0.0);
2023 #else // USE_MULTIVIEW
2024 vec4 buffer_ambient = textureLod(sampler2D(ambient_buffer, SAMPLER_LINEAR_CLAMP), coord, 0.0);
2025 vec4 buffer_reflection = textureLod(sampler2D(reflection_buffer, SAMPLER_LINEAR_CLAMP), coord, 0.0);
2026 #endif // USE_MULTIVIEW
2027
2028 ambient_light = mix(ambient_light, buffer_ambient.rgb, buffer_ambient.a);
2029 indirect_specular_light = mix(indirect_specular_light, buffer_reflection.rgb, buffer_reflection.a);
2030 }
2031 #endif // !USE_LIGHTMAP
2032
2033 if (bool(implementation_data.ss_effects_flags & SCREEN_SPACE_EFFECTS_FLAGS_USE_SSAO)) {
2034 #ifdef USE_MULTIVIEW
2035 float ssao = texture(sampler2DArray(ao_buffer, SAMPLER_LINEAR_CLAMP), vec3(screen_uv, ViewIndex)).r;
2036 #else
2037 float ssao = texture(sampler2D(ao_buffer, SAMPLER_LINEAR_CLAMP), screen_uv).r;
2038 #endif
2039 ao = min(ao, ssao);
2040 ao_light_affect = mix(ao_light_affect, max(ao_light_affect, implementation_data.ssao_light_affect), implementation_data.ssao_ao_affect);
2041 }
2042
2043 { // process reflections
2044
2045 vec4 reflection_accum = vec4(0.0, 0.0, 0.0, 0.0);
2046 vec4 ambient_accum = vec4(0.0, 0.0, 0.0, 0.0);
2047
2048 uint cluster_reflection_offset = cluster_offset + implementation_data.cluster_type_size * 3;
2049
2050 uint item_min;
2051 uint item_max;
2052 uint item_from;
2053 uint item_to;
2054
2055 cluster_get_item_range(cluster_reflection_offset + implementation_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
2056
2057 item_from = subgroupBroadcastFirst(subgroupMin(item_from));
2058 item_to = subgroupBroadcastFirst(subgroupMax(item_to));
2059
2060 #ifdef LIGHT_ANISOTROPY_USED
2061 // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
2062 vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
2063 vec3 anisotropic_tangent = cross(anisotropic_direction, view);
2064 vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
2065 vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0)));
2066 #else
2067 vec3 bent_normal = normal;
2068 #endif
2069 vec3 ref_vec = normalize(reflect(-view, bent_normal));
2070 // Interpolate between mirror and rough reflection by using linear_roughness * linear_roughness.
2071 ref_vec = mix(ref_vec, bent_normal, roughness * roughness * roughness * roughness);
2072
2073 for (uint i = item_from; i < item_to; i++) {
2074 uint mask = cluster_buffer.data[cluster_reflection_offset + i];
2075 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
2076
2077 uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
2078 while (merged_mask != 0) {
2079 uint bit = findMSB(merged_mask);
2080 merged_mask &= ~(1u << bit);
2081
2082 if (((1u << bit) & mask) == 0) { //do not process if not originally here
2083 continue;
2084 }
2085
2086 uint reflection_index = 32 * i + bit;
2087
2088 if (!bool(reflections.data[reflection_index].mask & instances.data[instance_index].layer_mask)) {
2089 continue; //not masked
2090 }
2091
2092 if (reflection_accum.a >= 1.0 && ambient_accum.a >= 1.0) {
2093 break;
2094 }
2095
2096 reflection_process(reflection_index, vertex, ref_vec, normal, roughness, ambient_light, indirect_specular_light, ambient_accum, reflection_accum);
2097 }
2098 }
2099
2100 if (ambient_accum.a < 1.0) {
2101 ambient_accum.rgb = ambient_light * (1.0 - ambient_accum.a) + ambient_accum.rgb;
2102 }
2103
2104 if (reflection_accum.a < 1.0) {
2105 reflection_accum.rgb = indirect_specular_light * (1.0 - reflection_accum.a) + reflection_accum.rgb;
2106 }
2107
2108 if (reflection_accum.a > 0.0) {
2109 indirect_specular_light = reflection_accum.rgb;
2110 }
2111
2112 #if !defined(USE_LIGHTMAP)
2113 if (ambient_accum.a > 0.0) {
2114 ambient_light = ambient_accum.rgb;
2115 }
2116 #endif
2117 }
2118
2119 //finalize ambient light here
2120 {
2121 ambient_light *= ao;
2122 #ifndef SPECULAR_OCCLUSION_DISABLED
2123 #ifdef BENT_NORMAL_MAP_USED
2124 // Apply cone to cone intersection with cosine weighted assumption:
2125 // https://blog.selfshadow.com/publications/s2016-shading-course/activision/s2016_pbs_activision_occlusion.pdf
2126 float cos_a_v = sqrt(1.0 - ao);
2127 float limited_roughness = max(roughness, 0.01); // Avoid artifacts at really low roughness.
2128 float cos_a_s = exp2((-log(10.0) / log(2.0)) * limited_roughness * limited_roughness);
2129 float cos_b = dot(bent_normal_vector, reflect(-view, normal));
2130
2131 // Intersection between the spherical caps of the visibility and specular cone.
2132 // Based on Christopher Oat and Pedro V. Sander's "Ambient aperture lighting":
2133 // https://advances.realtimerendering.com/s2006/Chapter8-Ambient_Aperture_Lighting.pdf
2134 float r1 = acos(cos_a_v);
2135 float r2 = acos(cos_a_s);
2136 float d = acos(cos_b);
2137 float area = 0.0;
2138
2139 if (d <= max(r1, r2) - min(r1, r2)) {
2140 // One cap is enclosed in the other.
2141 area = M_TAU - M_TAU * max(cos_a_v, cos_a_s);
2142 } else if (d >= r1 + r2) {
2143 // No intersection.
2144 area = 0.0;
2145 } else {
2146 float delta = abs(r1 - r2);
2147 float x = 1.0 - clamp((d - delta) / (r1 + r2 - delta), 0.0, 1.0);
2148 area = smoothstep(0.0, 1.0, x);
2149 area *= M_TAU - M_TAU * max(cos_a_v, cos_a_s);
2150 }
2151
2152 float specular_occlusion = area / (M_TAU * (1.0 - cos_a_s));
2153 indirect_specular_light *= specular_occlusion;
2154 #else // BENT_NORMAL_MAP_USED
2155 float specular_occlusion = (ambient_light.r * 0.3 + ambient_light.g * 0.59 + ambient_light.b * 0.11) * 2.0; // Luminance of ambient light.
2156 specular_occlusion = min(specular_occlusion * 4.0, 1.0); // This multiplication preserves speculars on bright areas.
2157
2158 float reflective_f = (1.0 - roughness) * metallic;
2159 // 10.0 is a magic number, it gives the intended effect in most scenarios.
2160 // Low enough for occlusion, high enough for reaction to lights and shadows.
2161 specular_occlusion = max(min(reflective_f * specular_occlusion * 10.0, 1.0), specular_occlusion);
2162 indirect_specular_light *= specular_occlusion;
2163 #endif // BENT_NORMAL_MAP_USED
2164 #endif // SPECULAR_OCCLUSION_DISABLED
2165 ambient_light *= albedo.rgb;
2166
2167 if (bool(implementation_data.ss_effects_flags & SCREEN_SPACE_EFFECTS_FLAGS_USE_SSIL)) {
2168 #ifdef USE_MULTIVIEW
2169 vec4 ssil = textureLod(sampler2DArray(ssil_buffer, SAMPLER_LINEAR_CLAMP), vec3(screen_uv, ViewIndex), 0.0);
2170 #else
2171 vec4 ssil = textureLod(sampler2D(ssil_buffer, SAMPLER_LINEAR_CLAMP), screen_uv, 0.0);
2172 #endif // USE_MULTIVIEW
2173 ambient_light *= 1.0 - ssil.a;
2174 ambient_light += ssil.rgb * albedo.rgb;
2175 }
2176
2177 //process ssr
2178 if (bool(implementation_data.ss_effects_flags & SCREEN_SPACE_EFFECTS_FLAGS_USE_SSR)) {
2179 bool resolve_ssr = bool(implementation_data.ss_effects_flags & SCREEN_SPACE_EFFECTS_FLAGS_RESOLVE_SSR);
2180
2181 float ssr_mip_level = 0.0;
2182 if (resolve_ssr) {
2183 #ifdef USE_MULTIVIEW
2184 ssr_mip_level = textureLod(sampler2DArray(ssr_mip_level_buffer, SAMPLER_NEAREST_CLAMP), vec3(screen_uv, ViewIndex), 0.0).x;
2185 #else
2186 ssr_mip_level = textureLod(sampler2D(ssr_mip_level_buffer, SAMPLER_NEAREST_CLAMP), screen_uv, 0.0).x;
2187 #endif // USE_MULTIVIEW
2188
2189 ssr_mip_level *= 14.0;
2190 }
2191
2192 #ifdef USE_MULTIVIEW
2193 vec4 ssr = textureLod(sampler2DArray(ssr_buffer, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(screen_uv, ViewIndex), ssr_mip_level);
2194 #else
2195 vec4 ssr = textureLod(sampler2D(ssr_buffer, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), screen_uv, ssr_mip_level);
2196 #endif // USE_MULTIVIEW
2197
2198 if (resolve_ssr) {
2199 const vec3 rec709_luminance_weights = vec3(0.2126, 0.7152, 0.0722);
2200 ssr.rgb /= 1.0 - dot(ssr.rgb, rec709_luminance_weights);
2201 }
2202
2203 // Apply fade when approaching 0.7 roughness to smoothen the harsh cutoff in the main SSR trace pass.
2204 ssr *= smoothstep(0.0, 1.0, 1.0 - clamp((roughness - 0.6) / (0.7 - 0.6), 0.0, 1.0));
2205
2206 // Alpha is premultiplied.
2207 indirect_specular_light = indirect_specular_light * (1.0 - ssr.a) + ssr.rgb;
2208 }
2209 }
2210 #endif // AMBIENT_LIGHT_DISABLED
2211
2212 // convert ao to direct light ao
2213 ao = mix(1.0, ao, ao_light_affect);
2214
2215 //this saves some VGPRs
2216 vec3 f0 = F0(metallic, specular, albedo);
2217 #ifndef AMBIENT_LIGHT_DISABLED
2218 {
2219 #if defined(DIFFUSE_TOON)
2220 //simplify for toon, as
2221 indirect_specular_light *= specular * metallic * albedo * 2.0;
2222 #else
2223 // Base Layer
2224 float NdotV = clamp(dot(normal, view), 0.0001, 1.0);
2225 vec2 envBRDF = prefiltered_dfg(roughness, NdotV).xy;
2226 // Multiscattering
2227 energy_compensation = get_energy_compensation(f0, envBRDF.y);
2228
2229 // cheap luminance approximation
2230 float f90 = clamp(50.0 * f0.g, metallic, 1.0);
2231 indirect_specular_light *= energy_compensation * ((f90 - f0) * envBRDF.x + f0 * envBRDF.y);
2232 #endif
2233 }
2234
2235 #endif // !AMBIENT_LIGHT_DISABLED
2236 #endif //GI !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
2237
2238 // LIGHTING
2239 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
2240
2241 #ifdef USE_VERTEX_LIGHTING
2242 diffuse_light += diffuse_light_interp.rgb;
2243 direct_specular_light += specular_light_interp.rgb * f0;
2244 #endif
2245
2246 { // Directional light.
2247
2248 // Do shadow and lighting in two passes to reduce register pressure.
2249 #ifndef SHADOWS_DISABLED
2250 uint shadow0 = 0;
2251 uint shadow1 = 0;
2252
2253 float shadowmask = 1.0;
2254
2255 #ifdef USE_LIGHTMAP
2256 uint shadowmask_mode = LIGHTMAP_SHADOWMASK_MODE_NONE;
2257
2258 if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
2259 const uint ofs = instances.data[instance_index].gi_offset & 0xFFFF;
2260 shadowmask_mode = lightmaps.data[ofs].flags;
2261
2262 if (shadowmask_mode != LIGHTMAP_SHADOWMASK_MODE_NONE) {
2263 const uint slice = instances.data[instance_index].gi_offset >> 16;
2264 const vec2 scaled_uv = uv2 * instances.data[instance_index].lightmap_uv_scale.zw + instances.data[instance_index].lightmap_uv_scale.xy;
2265 const vec3 uvw = vec3(scaled_uv, float(slice));
2266
2267 if (sc_use_lightmap_bicubic_filter()) {
2268 shadowmask = textureArray_bicubic(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], uvw, lightmaps.data[ofs].light_texture_size).x;
2269 } else {
2270 shadowmask = textureLod(sampler2DArray(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).x;
2271 }
2272 }
2273 }
2274
2275 if (shadowmask_mode != LIGHTMAP_SHADOWMASK_MODE_ONLY) {
2276 #endif // USE_LIGHTMAP
2277
2278 #ifdef USE_VERTEX_LIGHTING
2279 // Only process the first light's shadow for vertex lighting.
2280 for (uint i = 0; i < 1; i++) {
2281 #else
2282 for (uint i = 0; i < 8; i++) {
2283 if (i >= scene_data.directional_light_count) {
2284 break;
2285 }
2286 #endif
2287
2288 if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
2289 continue; //not masked
2290 }
2291
2292 if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
2293 continue; // Statically baked light and object uses lightmap, skip
2294 }
2295
2296 float shadow = 1.0;
2297
2298 if (directional_lights.data[i].shadow_opacity > 0.001) {
2299 float depth_z = -vertex.z;
2300 vec3 light_dir = directional_lights.data[i].direction;
2301 vec3 base_normal_bias = geo_normal * (1.0 - max(0.0, dot(light_dir, -geo_normal)));
2302
2303 #define BIAS_FUNC(m_var, m_idx) \
2304 m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
2305 vec3 normal_bias = base_normal_bias * directional_lights.data[i].shadow_normal_bias[m_idx]; \
2306 normal_bias -= light_dir * dot(light_dir, normal_bias); \
2307 m_var.xyz += normal_bias;
2308
2309 //version with soft shadows, more expensive
2310 if (sc_use_directional_soft_shadows() && directional_lights.data[i].softshadow_angle > 0) {
2311 uint blend_count = 0;
2312 const uint blend_max = directional_lights.data[i].blend_splits ? 2 : 1;
2313
2314 if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
2315 vec4 v = vec4(vertex, 1.0);
2316
2317 BIAS_FUNC(v, 0)
2318
2319 vec4 pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
2320 pssm_coord /= pssm_coord.w;
2321
2322 float range_pos = dot(directional_lights.data[i].direction, v.xyz);
2323 float range_begin = directional_lights.data[i].shadow_range_begin.x;
2324 float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
2325 vec2 tex_scale = directional_lights.data[i].uv_scale1 * test_radius;
2326 shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale, scene_data.taa_frame_count);
2327 blend_count++;
2328 }
2329
2330 if (blend_count < blend_max && depth_z < directional_lights.data[i].shadow_split_offsets.y) {
2331 vec4 v = vec4(vertex, 1.0);
2332
2333 BIAS_FUNC(v, 1)
2334
2335 vec4 pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
2336 pssm_coord /= pssm_coord.w;
2337
2338 float range_pos = dot(directional_lights.data[i].direction, v.xyz);
2339 float range_begin = directional_lights.data[i].shadow_range_begin.y;
2340 float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
2341 vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
2342 float s = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale, scene_data.taa_frame_count);
2343
2344 if (blend_count == 0) {
2345 shadow = s;
2346 } else {
2347 //blend
2348 float blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
2349 shadow = mix(shadow, s, blend);
2350 }
2351
2352 blend_count++;
2353 }
2354
2355 if (blend_count < blend_max && depth_z < directional_lights.data[i].shadow_split_offsets.z) {
2356 vec4 v = vec4(vertex, 1.0);
2357
2358 BIAS_FUNC(v, 2)
2359
2360 vec4 pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
2361 pssm_coord /= pssm_coord.w;
2362
2363 float range_pos = dot(directional_lights.data[i].direction, v.xyz);
2364 float range_begin = directional_lights.data[i].shadow_range_begin.z;
2365 float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
2366 vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
2367 float s = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale, scene_data.taa_frame_count);
2368
2369 if (blend_count == 0) {
2370 shadow = s;
2371 } else {
2372 //blend
2373 float blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
2374 shadow = mix(shadow, s, blend);
2375 }
2376
2377 blend_count++;
2378 }
2379
2380 if (blend_count < blend_max) {
2381 vec4 v = vec4(vertex, 1.0);
2382
2383 BIAS_FUNC(v, 3)
2384
2385 vec4 pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
2386 pssm_coord /= pssm_coord.w;
2387
2388 float range_pos = dot(directional_lights.data[i].direction, v.xyz);
2389 float range_begin = directional_lights.data[i].shadow_range_begin.w;
2390 float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
2391 vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
2392 float s = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale, scene_data.taa_frame_count);
2393
2394 if (blend_count == 0) {
2395 shadow = s;
2396 } else {
2397 //blend
2398 float blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
2399 shadow = mix(shadow, s, blend);
2400 }
2401 }
2402
2403 } else { //no soft shadows
2404
2405 vec4 pssm_coord;
2406 float blur_factor;
2407
2408 if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
2409 vec4 v = vec4(vertex, 1.0);
2410
2411 BIAS_FUNC(v, 0)
2412
2413 pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
2414 blur_factor = 1.0;
2415 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
2416 vec4 v = vec4(vertex, 1.0);
2417
2418 BIAS_FUNC(v, 1)
2419
2420 pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
2421 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2422 blur_factor = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.y;
2423 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
2424 vec4 v = vec4(vertex, 1.0);
2425
2426 BIAS_FUNC(v, 2)
2427
2428 pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
2429 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2430 blur_factor = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.z;
2431 } else {
2432 vec4 v = vec4(vertex, 1.0);
2433
2434 BIAS_FUNC(v, 3)
2435
2436 pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
2437 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2438 blur_factor = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.w;
2439 }
2440
2441 pssm_coord /= pssm_coord.w;
2442
2443 shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale * (blur_factor + (1.0 - blur_factor) * float(directional_lights.data[i].blend_splits)), pssm_coord, scene_data.taa_frame_count);
2444
2445 if (directional_lights.data[i].blend_splits) {
2446 float pssm_blend;
2447 float blur_factor2;
2448
2449 if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
2450 vec4 v = vec4(vertex, 1.0);
2451 BIAS_FUNC(v, 1)
2452 pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
2453 pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x - directional_lights.data[i].shadow_split_offsets.x * 0.1, directional_lights.data[i].shadow_split_offsets.x, depth_z);
2454 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2455 blur_factor2 = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.y;
2456 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
2457 vec4 v = vec4(vertex, 1.0);
2458 BIAS_FUNC(v, 2)
2459 pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
2460 pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y - directional_lights.data[i].shadow_split_offsets.y * 0.1, directional_lights.data[i].shadow_split_offsets.y, depth_z);
2461 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2462 blur_factor2 = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.z;
2463 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
2464 vec4 v = vec4(vertex, 1.0);
2465 BIAS_FUNC(v, 3)
2466 pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
2467 pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.z - directional_lights.data[i].shadow_split_offsets.z * 0.1, directional_lights.data[i].shadow_split_offsets.z, depth_z);
2468 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2469 blur_factor2 = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.w;
2470 } else {
2471 pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
2472 blur_factor2 = 1.0;
2473 }
2474
2475 pssm_coord /= pssm_coord.w;
2476
2477 float shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale * (blur_factor2 + (1.0 - blur_factor2) * float(directional_lights.data[i].blend_splits)), pssm_coord, scene_data.taa_frame_count);
2478 shadow = mix(shadow, shadow2, pssm_blend);
2479 }
2480 }
2481
2482 #ifdef USE_LIGHTMAP
2483 if (shadowmask_mode == LIGHTMAP_SHADOWMASK_MODE_REPLACE) {
2484 shadow = mix(shadow, shadowmask, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
2485 } else if (shadowmask_mode == LIGHTMAP_SHADOWMASK_MODE_OVERLAY) {
2486 shadow = shadowmask * mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
2487 } else {
2488 #endif
2489 shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
2490 #ifdef USE_LIGHTMAP
2491 }
2492 #endif
2493
2494 #ifdef USE_VERTEX_LIGHTING
2495 diffuse_light *= mix(1.0, shadow, diffuse_light_interp.a);
2496 direct_specular_light *= mix(1.0, shadow, specular_light_interp.a);
2497 #endif
2498
2499 #undef BIAS_FUNC
2500 } // shadows
2501
2502 if (i < 4) {
2503 shadow0 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << (i * 8);
2504 } else {
2505 shadow1 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << ((i - 4) * 8);
2506 }
2507 }
2508
2509 #ifdef USE_LIGHTMAP
2510 } else { // shadowmask_mode == LIGHTMAP_SHADOWMASK_MODE_ONLY
2511
2512 #ifdef USE_VERTEX_LIGHTING
2513 diffuse_light *= mix(1.0, shadowmask, diffuse_light_interp.a);
2514 direct_specular_light *= mix(1.0, shadowmask, specular_light_interp.a);
2515 #endif
2516
2517 shadow0 |= uint(clamp(shadowmask * 255.0, 0.0, 255.0));
2518 }
2519 #endif // USE_LIGHTMAP
2520
2521 #endif // SHADOWS_DISABLED
2522
2523 #ifndef USE_VERTEX_LIGHTING
2524
2525 for (uint i = 0; i < 8; i++) {
2526 if (i >= scene_data.directional_light_count) {
2527 break;
2528 }
2529
2530 if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
2531 continue; //not masked
2532 }
2533
2534 if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
2535 continue; // Statically baked light and object uses lightmap, skip
2536 }
2537
2538 #ifdef LIGHT_TRANSMITTANCE_USED
2539 float transmittance_z = transmittance_depth;
2540 #ifndef SHADOWS_DISABLED
2541 if (directional_lights.data[i].shadow_opacity > 0.001) {
2542 float depth_z = -vertex.z;
2543
2544 if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
2545 vec4 trans_vertex = vec4(vertex - geo_normal * directional_lights.data[i].shadow_transmittance_bias.x, 1.0);
2546 vec4 trans_coord = directional_lights.data[i].shadow_matrix1 * trans_vertex;
2547 trans_coord /= trans_coord.w;
2548
2549 float shadow_z = textureLod(sampler2D(directional_shadow_atlas, SAMPLER_LINEAR_CLAMP), trans_coord.xy, 0.0).r;
2550 shadow_z *= directional_lights.data[i].shadow_z_range.x;
2551 float z = trans_coord.z * directional_lights.data[i].shadow_z_range.x;
2552
2553 transmittance_z = z - shadow_z;
2554 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
2555 vec4 trans_vertex = vec4(vertex - geo_normal * directional_lights.data[i].shadow_transmittance_bias.y, 1.0);
2556 vec4 trans_coord = directional_lights.data[i].shadow_matrix2 * trans_vertex;
2557 trans_coord /= trans_coord.w;
2558
2559 float shadow_z = textureLod(sampler2D(directional_shadow_atlas, SAMPLER_LINEAR_CLAMP), trans_coord.xy, 0.0).r;
2560 shadow_z *= directional_lights.data[i].shadow_z_range.y;
2561 float z = trans_coord.z * directional_lights.data[i].shadow_z_range.y;
2562
2563 transmittance_z = z - shadow_z;
2564 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
2565 vec4 trans_vertex = vec4(vertex - geo_normal * directional_lights.data[i].shadow_transmittance_bias.z, 1.0);
2566 vec4 trans_coord = directional_lights.data[i].shadow_matrix3 * trans_vertex;
2567 trans_coord /= trans_coord.w;
2568
2569 float shadow_z = textureLod(sampler2D(directional_shadow_atlas, SAMPLER_LINEAR_CLAMP), trans_coord.xy, 0.0).r;
2570 shadow_z *= directional_lights.data[i].shadow_z_range.z;
2571 float z = trans_coord.z * directional_lights.data[i].shadow_z_range.z;
2572
2573 transmittance_z = z - shadow_z;
2574 } else {
2575 vec4 trans_vertex = vec4(vertex - geo_normal * directional_lights.data[i].shadow_transmittance_bias.w, 1.0);
2576 vec4 trans_coord = directional_lights.data[i].shadow_matrix4 * trans_vertex;
2577 trans_coord /= trans_coord.w;
2578
2579 float shadow_z = textureLod(sampler2D(directional_shadow_atlas, SAMPLER_LINEAR_CLAMP), trans_coord.xy, 0.0).r;
2580 shadow_z *= directional_lights.data[i].shadow_z_range.w;
2581 float z = trans_coord.z * directional_lights.data[i].shadow_z_range.w;
2582
2583 transmittance_z = z - shadow_z;
2584 }
2585 }
2586 #endif // !SHADOWS_DISABLED
2587 #endif // LIGHT_TRANSMITTANCE_USED
2588
2589 float shadow = 1.0;
2590 #ifndef SHADOWS_DISABLED
2591 if (i < 4) {
2592 shadow = float(shadow0 >> (i * 8u) & 0xFFu) / 255.0;
2593 } else {
2594 shadow = float(shadow1 >> ((i - 4u) * 8u) & 0xFFu) / 255.0;
2595 }
2596
2597 shadow = mix(1.0, shadow, directional_lights.data[i].shadow_opacity);
2598 #endif
2599
2600 blur_shadow(shadow);
2601
2602 #ifdef DEBUG_DRAW_PSSM_SPLITS
2603 vec3 tint = vec3(1.0);
2604 if (-vertex.z < directional_lights.data[i].shadow_split_offsets.x) {
2605 tint = vec3(1.0, 0.0, 0.0);
2606 } else if (-vertex.z < directional_lights.data[i].shadow_split_offsets.y) {
2607 tint = vec3(0.0, 1.0, 0.0);
2608 } else if (-vertex.z < directional_lights.data[i].shadow_split_offsets.z) {
2609 tint = vec3(0.0, 0.0, 1.0);
2610 } else {
2611 tint = vec3(1.0, 1.0, 0.0);
2612 }
2613 tint = mix(tint, vec3(1.0), shadow);
2614 shadow = 1.0;
2615 #endif
2616
2617 float size_A = sc_use_directional_soft_shadows() ? directional_lights.data[i].size : 0.0;
2618
2619 light_compute(normal, directional_lights.data[i].direction, normalize(view), size_A,
2620 #ifndef DEBUG_DRAW_PSSM_SPLITS
2621 directional_lights.data[i].color * directional_lights.data[i].energy,
2622 #else
2623 directional_lights.data[i].color * directional_lights.data[i].energy * tint,
2624 #endif
2625 true, shadow, f0, roughness, metallic, directional_lights.data[i].specular, albedo, alpha, screen_uv, energy_compensation,
2626 #ifdef LIGHT_BACKLIGHT_USED
2627 backlight,
2628 #endif
2629 #ifdef LIGHT_TRANSMITTANCE_USED
2630 transmittance_color,
2631 transmittance_depth,
2632 transmittance_boost,
2633 transmittance_z,
2634 #endif
2635 #ifdef LIGHT_RIM_USED
2636 rim, rim_tint,
2637 #endif
2638 #ifdef LIGHT_CLEARCOAT_USED
2639 clearcoat, clearcoat_roughness, geo_normal,
2640 #endif // LIGHT_CLEARCOAT_USED
2641 #ifdef LIGHT_ANISOTROPY_USED
2642 binormal,
2643 tangent, anisotropy,
2644 #endif
2645 diffuse_light,
2646 direct_specular_light);
2647 }
2648 #endif // USE_VERTEX_LIGHTING
2649 }
2650
2651 #ifndef USE_VERTEX_LIGHTING
2652 { //omni lights
2653
2654 uint cluster_omni_offset = cluster_offset;
2655
2656 uint item_min;
2657 uint item_max;
2658 uint item_from;
2659 uint item_to;
2660
2661 cluster_get_item_range(cluster_omni_offset + implementation_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
2662
2663 item_from = subgroupBroadcastFirst(subgroupMin(item_from));
2664 item_to = subgroupBroadcastFirst(subgroupMax(item_to));
2665
2666 for (uint i = item_from; i < item_to; i++) {
2667 uint mask = cluster_buffer.data[cluster_omni_offset + i];
2668 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
2669
2670 uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
2671 while (merged_mask != 0) {
2672 uint bit = findMSB(merged_mask);
2673 merged_mask &= ~(1u << bit);
2674
2675 if (((1u << bit) & mask) == 0) { //do not process if not originally here
2676 continue;
2677 }
2678
2679 uint light_index = 32 * i + bit;
2680
2681 if (!bool(omni_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
2682 continue; //not masked
2683 }
2684
2685 if (omni_lights.data[light_index].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
2686 continue; // Statically baked light and object uses lightmap, skip
2687 }
2688
2689 light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, roughness, metallic, scene_data.taa_frame_count, albedo, alpha, screen_uv, energy_compensation,
2690 #ifdef LIGHT_BACKLIGHT_USED
2691 backlight,
2692 #endif
2693 #ifdef LIGHT_TRANSMITTANCE_USED
2694 transmittance_color,
2695 transmittance_depth,
2696 transmittance_boost,
2697 #endif
2698 #ifdef LIGHT_RIM_USED
2699 rim,
2700 rim_tint,
2701 #endif
2702 #ifdef LIGHT_CLEARCOAT_USED
2703 clearcoat, clearcoat_roughness, geo_normal,
2704 #endif // LIGHT_CLEARCOAT_USED
2705 #ifdef LIGHT_ANISOTROPY_USED
2706 binormal, tangent, anisotropy,
2707 #endif
2708 diffuse_light, direct_specular_light);
2709 }
2710 }
2711 }
2712
2713 { //spot lights
2714
2715 uint cluster_spot_offset = cluster_offset + implementation_data.cluster_type_size;
2716
2717 uint item_min;
2718 uint item_max;
2719 uint item_from;
2720 uint item_to;
2721
2722 cluster_get_item_range(cluster_spot_offset + implementation_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
2723
2724 item_from = subgroupBroadcastFirst(subgroupMin(item_from));
2725 item_to = subgroupBroadcastFirst(subgroupMax(item_to));
2726
2727 for (uint i = item_from; i < item_to; i++) {
2728 uint mask = cluster_buffer.data[cluster_spot_offset + i];
2729 mask &= cluster_get_range_clip_mask(i, item_min, item_max);
2730
2731 uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
2732 while (merged_mask != 0) {
2733 uint bit = findMSB(merged_mask);
2734 merged_mask &= ~(1u << bit);
2735
2736 if (((1u << bit) & mask) == 0) { //do not process if not originally here
2737 continue;
2738 }
2739
2740 uint light_index = 32 * i + bit;
2741
2742 if (!bool(spot_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
2743 continue; //not masked
2744 }
2745
2746 if (spot_lights.data[light_index].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
2747 continue; // Statically baked light and object uses lightmap, skip
2748 }
2749
2750 light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, roughness, metallic, scene_data.taa_frame_count, albedo, alpha, screen_uv, energy_compensation,
2751 #ifdef LIGHT_BACKLIGHT_USED
2752 backlight,
2753 #endif
2754 #ifdef LIGHT_TRANSMITTANCE_USED
2755 transmittance_color,
2756 transmittance_depth,
2757 transmittance_boost,
2758 #endif
2759 #ifdef LIGHT_RIM_USED
2760 rim,
2761 rim_tint,
2762 #endif
2763 #ifdef LIGHT_CLEARCOAT_USED
2764 clearcoat, clearcoat_roughness, geo_normal,
2765 #endif // LIGHT_CLEARCOAT_USED
2766 #ifdef LIGHT_ANISOTROPY_USED
2767 binormal, tangent, anisotropy,
2768 #endif
2769 diffuse_light, direct_specular_light);
2770 }
2771 }
2772 }
2773 #endif // !USE_VERTEX_LIGHTING
2774 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
2775
2776 #ifdef USE_SHADOW_TO_OPACITY
2777 #ifndef MODE_RENDER_DEPTH
2778 alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
2779
2780 #if defined(ALPHA_SCISSOR_USED)
2781 #ifdef MODE_RENDER_MATERIAL
2782 if (alpha < alpha_scissor_threshold) {
2783 alpha = 0.0;
2784 } else {
2785 alpha = 1.0;
2786 }
2787 #else
2788 if (alpha < alpha_scissor_threshold) {
2789 discard;
2790 }
2791 #endif // MODE_RENDER_MATERIAL
2792 #endif // ALPHA_SCISSOR_USED
2793
2794 #endif // !MODE_RENDER_DEPTH
2795 #endif // USE_SHADOW_TO_OPACITY
2796
2797 #ifdef MODE_RENDER_DEPTH
2798
2799 #ifdef MODE_RENDER_SDF
2800
2801 {
2802 vec3 local_pos = (implementation_data.sdf_to_bounds * vec4(vertex, 1.0)).xyz;
2803 ivec3 grid_pos = implementation_data.sdf_offset + ivec3(local_pos * vec3(implementation_data.sdf_size));
2804
2805 uint albedo16 = 0x1; //solid flag
2806 albedo16 |= clamp(uint(albedo.r * 31.0), 0, 31) << 11;
2807 albedo16 |= clamp(uint(albedo.g * 31.0), 0, 31) << 6;
2808 albedo16 |= clamp(uint(albedo.b * 31.0), 0, 31) << 1;
2809
2810 imageStore(albedo_volume_grid, grid_pos, uvec4(albedo16));
2811
2812 uint facing_bits = 0;
2813 const vec3 aniso_dir[6] = vec3[](
2814 vec3(1, 0, 0),
2815 vec3(0, 1, 0),
2816 vec3(0, 0, 1),
2817 vec3(-1, 0, 0),
2818 vec3(0, -1, 0),
2819 vec3(0, 0, -1));
2820
2821 vec3 cam_normal = mat3(inv_view_matrix) * normalize(normal_interp);
2822
2823 float closest_dist = -1e20;
2824
2825 for (uint i = 0; i < 6; i++) {
2826 float d = dot(cam_normal, aniso_dir[i]);
2827 if (d > closest_dist) {
2828 closest_dist = d;
2829 facing_bits = (1 << i);
2830 }
2831 }
2832
2833 #ifdef NO_IMAGE_ATOMICS
2834 imageStore(geom_facing_grid, grid_pos, uvec4(imageLoad(geom_facing_grid, grid_pos).r | facing_bits)); //store facing bits
2835 #else
2836 imageAtomicOr(geom_facing_grid, grid_pos, facing_bits); //store facing bits
2837 #endif
2838
2839 if (length(emission) > 0.001) {
2840 float lumas[6];
2841 vec3 light_total = vec3(0);
2842
2843 for (int i = 0; i < 6; i++) {
2844 float strength = max(0.0, dot(cam_normal, aniso_dir[i]));
2845 vec3 light = emission * strength;
2846 light_total += light;
2847 lumas[i] = max(light.r, max(light.g, light.b));
2848 }
2849
2850 float luma_total = max(light_total.r, max(light_total.g, light_total.b));
2851
2852 uint light_aniso = 0;
2853
2854 for (int i = 0; i < 6; i++) {
2855 light_aniso |= min(31, uint((lumas[i] / luma_total) * 31.0)) << (i * 5);
2856 }
2857
2858 //compress to RGBE9995 to save space
2859
2860 const float pow2to9 = 512.0f;
2861 const float B = 15.0f;
2862 const float N = 9.0f;
2863 const float LN2 = 0.6931471805599453094172321215;
2864
2865 float cRed = clamp(light_total.r, 0.0, 65408.0);
2866 float cGreen = clamp(light_total.g, 0.0, 65408.0);
2867 float cBlue = clamp(light_total.b, 0.0, 65408.0);
2868
2869 float cMax = max(cRed, max(cGreen, cBlue));
2870
2871 float expp = max(-B - 1.0f, floor(log(cMax) / LN2)) + 1.0f + B;
2872
2873 float sMax = floor((cMax / pow(2.0f, expp - B - N)) + 0.5f);
2874
2875 float exps = expp + 1.0f;
2876
2877 if (0.0 <= sMax && sMax < pow2to9) {
2878 exps = expp;
2879 }
2880
2881 float sRed = floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
2882 float sGreen = floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
2883 float sBlue = floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
2884 //store as 8985 to have 2 extra neighbor bits
2885 uint light_rgbe = ((uint(sRed) & 0x1FFu) >> 1) | ((uint(sGreen) & 0x1FFu) << 8) | (((uint(sBlue) & 0x1FFu) >> 1) << 17) | ((uint(exps) & 0x1Fu) << 25);
2886
2887 imageStore(emission_grid, grid_pos, uvec4(light_rgbe));
2888 imageStore(emission_aniso_grid, grid_pos, uvec4(light_aniso));
2889 }
2890 }
2891
2892 #endif
2893
2894 #ifdef MODE_RENDER_MATERIAL
2895
2896 albedo_output_buffer.rgb = albedo;
2897 albedo_output_buffer.a = alpha;
2898
2899 normal_output_buffer.rgb = encode24(normal) * 0.5 + 0.5;
2900 normal_output_buffer.a = 0.0;
2901 depth_output_buffer.r = -vertex.z;
2902
2903 orm_output_buffer.r = ao;
2904 orm_output_buffer.g = roughness;
2905 orm_output_buffer.b = metallic;
2906 orm_output_buffer.a = sss_strength;
2907
2908 emission_output_buffer.rgb = emission;
2909 emission_output_buffer.a = 0.0;
2910 #endif
2911
2912 #ifdef MODE_RENDER_NORMAL_ROUGHNESS
2913 normal_roughness_output_buffer = vec4(encode24(normal) * 0.5 + 0.5, roughness);
2914
2915 // We encode the dynamic static into roughness.
2916 // Values over 0.5 are dynamic, under 0.5 are static.
2917 normal_roughness_output_buffer.w = normal_roughness_output_buffer.w * (127.0 / 255.0);
2918 if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_DYNAMIC)) {
2919 normal_roughness_output_buffer.w = 1.0 - normal_roughness_output_buffer.w;
2920 }
2921 normal_roughness_output_buffer.w = normal_roughness_output_buffer.w;
2922
2923 #ifdef MODE_RENDER_VOXEL_GI
2924 if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_VOXEL_GI)) { // process voxel_gi_instances
2925 uint index1 = instances.data[instance_index].gi_offset & 0xFFFF;
2926 uint index2 = instances.data[instance_index].gi_offset >> 16;
2927 voxel_gi_buffer.x = index1 & 0xFFu;
2928 voxel_gi_buffer.y = index2 & 0xFFu;
2929 } else {
2930 voxel_gi_buffer.x = 0xFF;
2931 voxel_gi_buffer.y = 0xFF;
2932 }
2933 #endif
2934
2935 #endif //MODE_RENDER_NORMAL_ROUGHNESS
2936
2937 //nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
2938 #else
2939
2940 // multiply by albedo
2941 diffuse_light *= albedo; // ambient must be multiplied by albedo at the end
2942
2943 // apply direct light AO
2944 diffuse_light *= ao;
2945 direct_specular_light *= ao;
2946
2947 // apply metallic
2948 diffuse_light *= 1.0 - metallic;
2949 ambient_light *= 1.0 - metallic;
2950
2951 #ifndef FOG_DISABLED
2952 //restore fog
2953 fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
2954 #endif //!FOG_DISABLED
2955
2956 #ifdef MODE_SEPARATE_SPECULAR
2957
2958 #ifdef MODE_UNSHADED
2959 diffuse_buffer = vec4(albedo.rgb, 0.0);
2960 specular_buffer = vec4(0.0);
2961
2962 #else
2963
2964 #ifdef SSS_MODE_SKIN
2965 sss_strength = -sss_strength;
2966 #endif
2967 diffuse_buffer = vec4(emission + diffuse_light + ambient_light, sss_strength);
2968 specular_buffer = vec4(direct_specular_light + indirect_specular_light, metallic);
2969 #endif
2970
2971 #ifndef FOG_DISABLED
2972 diffuse_buffer.rgb = diffuse_buffer.rgb * fog.a + fog.rgb;
2973 specular_buffer.rgb = specular_buffer.rgb * fog.a;
2974 #endif //!FOG_DISABLED
2975
2976 #else //MODE_SEPARATE_SPECULAR
2977
2978 alpha *= scene_data.pass_alpha_multiplier;
2979
2980 #ifdef MODE_UNSHADED
2981 frag_color = vec4(albedo, alpha);
2982 #else
2983 frag_color = vec4(emission + ambient_light + diffuse_light + direct_specular_light + indirect_specular_light, alpha);
2984 //frag_color = vec4(1.0);
2985 #endif //USE_NO_SHADING
2986
2987 #ifndef FOG_DISABLED
2988 frag_color.rgb = frag_color.rgb * fog.a + fog.rgb;
2989 #endif //!FOG_DISABLED
2990
2991 #if defined(PREMUL_ALPHA_USED) && !defined(MODE_RENDER_DEPTH)
2992 frag_color.rgb *= premul_alpha;
2993 #endif //PREMUL_ALPHA_USED
2994
2995 #endif //MODE_SEPARATE_SPECULAR
2996
2997 #endif //MODE_RENDER_DEPTH
2998 #ifdef MOTION_VECTORS
2999 vec2 position_clip = (screen_position.xy / screen_position.w) - scene_data.taa_jitter;
3000 vec2 prev_position_clip = (prev_screen_position.xy / prev_screen_position.w) - scene_data_block.prev_data.taa_jitter;
3001
3002 vec2 position_uv = position_clip * vec2(0.5, 0.5);
3003 vec2 prev_position_uv = prev_position_clip * vec2(0.5, 0.5);
3004
3005 motion_vector = prev_position_uv - position_uv;
3006 #endif
3007 }
3008
3009 void main() {
3010 #ifdef UBERSHADER
3011 bool front_facing = gl_FrontFacing;
3012 if (uc_cull_mode() == POLYGON_CULL_BACK && !front_facing) {
3013 discard;
3014 } else if (uc_cull_mode() == POLYGON_CULL_FRONT && front_facing) {
3015 discard;
3016 }
3017 #endif
3018 #ifdef MODE_DUAL_PARABOLOID
3019
3020 if (dp_clip > 0.0) {
3021 discard;
3022 }
3023 #endif
3024
3025 fragment_shader(scene_data_block.data);
3026 }