Debian Codegraph @

servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.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 our forward mobile UBOs definitions etc. */
11 #include "scene_forward_mobile_inc.glsl"
12
13 #define SHADER_IS_SRGB false
14 #define SHADER_SPACE_FAR 0.0
15
16 #ifdef SHADOW_PASS
17 #define IN_SHADOW_PASS true
18 #else
19 #define IN_SHADOW_PASS false
20 #endif
21
22 #ifdef USE_MULTIVIEW
23 #define OUTPUT_IS_MULTIVIEW true
24 #else
25 #define OUTPUT_IS_MULTIVIEW false
26 #endif
27
28 /* INPUT ATTRIBS */
29
30 // Always contains vertex position in XYZ, can contain tangent angle in W.
31 layout(location = 0) in vec4 vertex_angle_attrib;
32
33 //only for pure render depth when normal is not used
34
35 #ifdef NORMAL_USED
36 // Contains Normal/Axis in RG, can contain tangent in BA.
37 layout(location = 1) in vec4 axis_tangent_attrib;
38 #endif
39
40 // Location 2 is unused.
41
42 #if defined(COLOR_USED)
43 layout(location = 3) in vec4 color_attrib;
44 #endif
45
46 #ifdef UV_USED
47 layout(location = 4) in vec2 uv_attrib;
48 #endif
49
50 #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(MODE_RENDER_MATERIAL)
51 layout(location = 5) in vec2 uv2_attrib;
52 #endif // MODE_RENDER_MATERIAL
53
54 #if defined(CUSTOM0_USED)
55 layout(location = 6) in vec4 custom0_attrib;
56 #endif
57
58 #if defined(CUSTOM1_USED)
59 layout(location = 7) in vec4 custom1_attrib;
60 #endif
61
62 #if defined(CUSTOM2_USED)
63 layout(location = 8) in vec4 custom2_attrib;
64 #endif
65
66 #if defined(CUSTOM3_USED)
67 layout(location = 9) in vec4 custom3_attrib;
68 #endif
69
70 #if defined(BONES_USED) || defined(USE_PARTICLE_TRAILS)
71 layout(location = 10) in uvec4 bone_attrib;
72 #endif
73
74 #if defined(WEIGHTS_USED) || defined(USE_PARTICLE_TRAILS)
75 layout(location = 11) in vec4 weight_attrib;
76 #endif
77
78 #if defined(MODE_RENDER_MOTION_VECTORS)
79 layout(location = 12) in vec4 previous_vertex_attrib;
80
81 #if defined(NORMAL_USED) || defined(TANGENT_USED)
82 layout(location = 13) in vec4 previous_normal_attrib;
83 #endif
84
85 #endif // MODE_RENDER_MOTION_VECTORS
86
87 void axis_angle_to_tbn(vec3 axis, float angle, out vec3 tangent, out vec3 binormal, out vec3 normal) {
88 float c = cos(angle);
89 float s = sin(angle);
90 vec3 omc_axis = (1.0 - c) * axis;
91 vec3 s_axis = s * axis;
92 tangent = omc_axis.xxx * axis + vec3(c, -s_axis.z, s_axis.y);
93 binormal = omc_axis.yyy * axis + vec3(s_axis.z, c, -s_axis.x);
94 normal = omc_axis.zzz * axis + vec3(-s_axis.y, s_axis.x, c);
95 }
96
97 /* Varyings */
98
99 layout(location = 0) out vec3 vertex_interp;
100
101 #ifdef NORMAL_USED
102 layout(location = 1) out vec3 normal_interp;
103 #endif
104
105 #if defined(COLOR_USED)
106 layout(location = 2) out vec4 color_interp;
107 #endif
108
109 #ifdef UV_USED
110 layout(location = 3) out vec2 uv_interp;
111 #endif
112
113 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
114 layout(location = 4) out vec2 uv2_interp;
115 #endif
116
117 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
118 layout(location = 5) out vec3 tangent_interp;
119 layout(location = 6) out vec3 binormal_interp;
120 #endif
121 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
122 layout(location = 7) out vec4 diffuse_light_interp;
123 layout(location = 8) out vec4 specular_light_interp;
124
125 #include "../scene_forward_vertex_lights_inc.glsl"
126 #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
127 #ifdef MATERIAL_UNIFORMS_USED
128 /* clang-format off */
129 layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms {
130 #MATERIAL_UNIFORMS
131 } material;
132 /* clang-format on */
133 #endif
134
135 #ifdef MODE_DUAL_PARABOLOID
136
137 layout(location = 9) out float dp_clip;
138
139 #endif
140
141 #if defined(MODE_RENDER_MOTION_VECTORS)
142 layout(location = 12) out highp vec4 screen_position;
143 layout(location = 13) out highp vec4 prev_screen_position;
144 #endif
145
146 #ifdef USE_MULTIVIEW
147 #extension GL_EXT_multiview : enable
148 #define ViewIndex gl_ViewIndex
149 vec3 multiview_uv(vec2 uv) {
150 return vec3(uv, ViewIndex);
151 }
152 ivec3 multiview_uv(ivec2 uv) {
153 return ivec3(uv, int(ViewIndex));
154 }
155 #else // USE_MULTIVIEW
156 #define ViewIndex 0
157 vec2 multiview_uv(vec2 uv) {
158 return uv;
159 }
160 ivec2 multiview_uv(ivec2 uv) {
161 return uv;
162 }
163 #endif // !USE_MULTIVIEW
164
165 #if defined(POINT_SIZE_USED) && defined(POINT_COORD_USED)
166 layout(location = 14) out vec2 point_coord_interp;
167 #endif
168
169 invariant gl_Position;
170
171 #GLOBALS
172
173 #define scene_data scene_data_block.data
174
175 #ifdef USE_DOUBLE_PRECISION
176 // Helper functions for emulating double precision when adding floats.
177 vec3 quick_two_sum(vec3 a, vec3 b, out vec3 out_p) {
178 vec3 s = a + b;
179 out_p = b - (s - a);
180 return s;
181 }
182
183 vec3 two_sum(vec3 a, vec3 b, out vec3 out_p) {
184 vec3 s = a + b;
185 vec3 v = s - a;
186 out_p = (a - (s - v)) + (b - v);
187 return s;
188 }
189
190 vec3 double_add_vec3(vec3 base_a, vec3 prec_a, vec3 base_b, vec3 prec_b, out vec3 out_precision) {
191 vec3 s, t, se, te;
192 s = two_sum(base_a, base_b, se);
193 t = two_sum(prec_a, prec_b, te);
194 se += t;
195 s = quick_two_sum(s, se, se);
196 se += te;
197 s = quick_two_sum(s, se, out_precision);
198 return s;
199 }
200 #endif
201
202 uint multimesh_stride() {
203 uint stride = sc_multimesh_format_2d() ? 2 : 3;
204 stride += sc_multimesh_has_color() ? 1 : 0;
205 stride += sc_multimesh_has_custom_data() ? 1 : 0;
206 return stride;
207 }
208
209 void _unpack_vertex_attributes(vec4 p_vertex_in, vec3 p_compressed_aabb_position, vec3 p_compressed_aabb_size,
210 #if defined(NORMAL_USED) || defined(TANGENT_USED)
211 vec4 p_normal_in,
212 #ifdef NORMAL_USED
213 out vec3 r_normal,
214 #endif
215 out vec3 r_tangent,
216 out vec3 r_binormal,
217 #endif
218 out vec3 r_vertex) {
219
220 r_vertex = p_vertex_in.xyz * p_compressed_aabb_size + p_compressed_aabb_position;
221 #ifdef NORMAL_USED
222 r_normal = oct_to_vec3(p_normal_in.xy * 2.0 - 1.0);
223 #endif
224
225 #if defined(NORMAL_USED) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
226
227 float binormal_sign;
228
229 // This works because the oct value (0, 1) maps onto (0, 0, -1) which encodes to (1, 1).
230 // Accordingly, if p_normal_in.z contains octahedral values, it won't equal (0, 1).
231 if (p_normal_in.z > 0.0 || p_normal_in.w < 1.0) {
232 // Uncompressed format.
233 vec2 signed_tangent_attrib = p_normal_in.zw * 2.0 - 1.0;
234 r_tangent = oct_to_vec3(vec2(signed_tangent_attrib.x, abs(signed_tangent_attrib.y) * 2.0 - 1.0));
235 binormal_sign = sign(signed_tangent_attrib.y);
236 r_binormal = normalize(cross(r_normal, r_tangent) * binormal_sign);
237 } else {
238 // Compressed format.
239 float angle = p_vertex_in.w;
240 binormal_sign = angle > 0.5 ? 1.0 : -1.0; // 0.5 does not exist in UNORM16, so values are either greater or smaller.
241 angle = abs(angle * 2.0 - 1.0) * M_PI; // 0.5 is basically zero, allowing to encode both signs reliably.
242 vec3 axis = r_normal;
243 axis_angle_to_tbn(axis, angle, r_tangent, r_binormal, r_normal);
244 r_binormal *= binormal_sign;
245 }
246 #endif
247 }
248
249 void vertex_shader(in vec3 vertex,
250 #ifdef NORMAL_USED
251 in vec3 normal_highp,
252 #endif
253 #if defined(NORMAL_USED) || defined(TANGENT_USED)
254 in vec3 tangent_highp,
255 in vec3 binormal_highp,
256 #endif
257 in uint instance_index, in uint multimesh_offset, in mat3x4 in_model_matrix,
258 #ifdef USE_DOUBLE_PRECISION
259 in vec3 model_precision,
260 in vec3 view_precision,
261 #endif
262 #ifdef MODE_DUAL_PARABOLOID
263 in float dual_paraboloid_side,
264 in float z_far,
265 #endif
266 #if defined(MODE_RENDER_DEPTH) || defined(MODE_RENDER_MATERIAL)
267 in uint scene_flags,
268 #endif
269 in mat4 projection_matrix,
270 in mat4 inv_projection_matrix,
271 #ifdef USE_MULTIVIEW
272 in vec4 scene_eye_offset,
273 #endif
274 in mat3x4 in_view_matrix,
275 in mat3x4 in_inv_view_matrix,
276 in vec2 viewport_size,
277 in uint scene_directional_light_count,
278 out vec4 screen_position_output) {
279 vec4 instance_custom = vec4(0.0);
280 #if defined(COLOR_USED)
281 vec4 color_highp = color_attrib;
282 #endif
283
284 mat4 inv_view_matrix = transpose(mat4(in_inv_view_matrix[0],
285 in_inv_view_matrix[1],
286 in_inv_view_matrix[2],
287 vec4(0.0, 0.0, 0.0, 1.0)));
288
289 mat4 model_matrix = transpose(mat4(in_model_matrix[0],
290 in_model_matrix[1],
291 in_model_matrix[2],
292 vec4(0.0, 0.0, 0.0, 1.0)));
293
294 mat3 model_normal_matrix;
295 if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
296 model_normal_matrix = transpose(inverse(mat3(model_matrix)));
297 } else {
298 model_normal_matrix = mat3(model_matrix);
299 }
300
301 mat4 matrix;
302 mat4 read_model_matrix = model_matrix;
303
304 if (sc_multimesh()) {
305 //multimesh, instances are for it
306
307 #ifdef USE_PARTICLE_TRAILS
308 uint trail_size = (instances.data[instance_index].flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK;
309 uint stride = 3 + 1 + 1; //particles always uses this format
310
311 uint offset = trail_size * stride * INSTANCE_INDEX;
312
313 #ifdef COLOR_USED
314 vec4 pcolor;
315 #endif
316 {
317 uint boffset = offset + bone_attrib.x * stride;
318 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;
319 #ifdef COLOR_USED
320 pcolor = transforms.data[boffset + 3] * weight_attrib.x;
321 #endif
322 }
323 if (weight_attrib.y > 0.001) {
324 uint boffset = offset + bone_attrib.y * stride;
325 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;
326 #ifdef COLOR_USED
327 pcolor += transforms.data[boffset + 3] * weight_attrib.y;
328 #endif
329 }
330 if (weight_attrib.z > 0.001) {
331 uint boffset = offset + bone_attrib.z * stride;
332 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;
333 #ifdef COLOR_USED
334 pcolor += transforms.data[boffset + 3] * weight_attrib.z;
335 #endif
336 }
337 if (weight_attrib.w > 0.001) {
338 uint boffset = offset + bone_attrib.w * stride;
339 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;
340 #ifdef COLOR_USED
341 pcolor += transforms.data[boffset + 3] * weight_attrib.w;
342 #endif
343 }
344
345 instance_custom = transforms.data[offset + 4];
346
347 #ifdef COLOR_USED
348 color_highp *= pcolor;
349 #endif
350
351 #else
352 uint stride = multimesh_stride();
353 uint offset = stride * (INSTANCE_INDEX + multimesh_offset);
354
355 if (sc_multimesh_format_2d()) {
356 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));
357 offset += 2;
358 } else {
359 matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], transforms.data[offset + 2], vec4(0.0, 0.0, 0.0, 1.0));
360 offset += 3;
361 }
362
363 if (sc_multimesh_has_color()) {
364 #ifdef COLOR_USED
365 color_highp *= transforms.data[offset];
366 #endif
367 offset += 1;
368 }
369
370 if (sc_multimesh_has_custom_data()) {
371 instance_custom = transforms.data[offset];
372 }
373
374 #endif
375 //transpose
376 matrix = transpose(matrix);
377
378 #if !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED) || defined(MODEL_MATRIX_USED)
379 // Normally we can bake the multimesh transform into the model matrix, but when using double precision
380 // we avoid baking it in so we can emulate high precision.
381 read_model_matrix = model_matrix * matrix;
382 #if !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED)
383 model_matrix = read_model_matrix;
384 #endif // !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED)
385 #endif // !defined(USE_DOUBLE_PRECISION) || defined(SKIP_TRANSFORM_USED) || defined(VERTEX_WORLD_COORDS_USED) || defined(MODEL_MATRIX_USED)
386 model_normal_matrix = model_normal_matrix * mat3(matrix);
387 }
388
389 #ifdef UV_USED
390 uv_interp = uv_attrib;
391 #endif
392
393 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
394 uv2_interp = uv2_attrib;
395 #endif
396
397 vec4 uv_scale = instances.data[instance_index].uv_scale;
398
399 if (uv_scale != vec4(0.0)) { // Compression enabled
400 #ifdef UV_USED
401 uv_interp = (uv_interp - 0.5) * uv_scale.xy;
402 #endif
403 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
404 uv2_interp = (uv2_interp - 0.5) * uv_scale.zw;
405 #endif
406 }
407
408 #ifdef OVERRIDE_POSITION
409 vec4 position = vec4(1.0);
410 #endif
411
412 #ifdef USE_MULTIVIEW
413 vec3 eye_offset = scene_eye_offset.xyz;
414 #else
415 vec3 eye_offset = vec3(0.0, 0.0, 0.0);
416 #endif // USE_MULTIVIEW
417
418 //using world coordinates
419 #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
420
421 vertex = (model_matrix * vec4(vertex, 1.0)).xyz;
422
423 #ifdef NORMAL_USED
424 normal_highp = model_normal_matrix * normal_highp;
425 #endif
426
427 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
428
429 tangent_highp = model_normal_matrix * tangent_highp;
430 binormal_highp = model_normal_matrix * binormal_highp;
431
432 #endif
433 #endif
434
435 #ifdef Z_CLIP_SCALE_USED
436 float z_clip_scale = 1.0;
437 #endif
438
439 float roughness_highp = 1.0;
440
441 mat4 read_view_matrix = transpose(mat4(in_view_matrix[0],
442 in_view_matrix[1],
443 in_view_matrix[2],
444 vec4(0.0, 0.0, 0.0, 1.0)));
445
446 #ifdef USE_DOUBLE_PRECISION
447 mat4 modelview = read_view_matrix * model_matrix;
448
449 // We separate the basis from the origin because the basis is fine with single point precision.
450 // Then we combine the translations from the model matrix and the view matrix using emulated doubles.
451 // We add the result to the vertex and ignore the final lost precision.
452 vec3 model_origin = model_matrix[3].xyz;
453 if (sc_multimesh()) {
454 modelview = modelview * matrix;
455
456 vec3 instance_origin = mat3(model_matrix) * matrix[3].xyz;
457 model_origin = double_add_vec3(model_origin, model_precision, instance_origin, vec3(0.0), model_precision);
458 }
459
460 // Overwrite the translation part of modelview with improved precision.
461 vec3 temp_precision; // Will be ignored.
462 modelview[3].xyz = double_add_vec3(model_origin, model_precision, inv_view_matrix[3].xyz, view_precision, temp_precision);
463 modelview[3].xyz = mat3(read_view_matrix) * modelview[3].xyz;
464 #else
465 mat4 modelview = read_view_matrix * model_matrix;
466 #endif
467 mat3 modelview_normal = mat3(read_view_matrix) * model_normal_matrix;
468 vec2 read_viewport_size = scene_data.viewport_size;
469
470 #ifdef POINT_SIZE_USED
471 float point_size = 1.0;
472 #endif
473
474 {
475 #CODE : VERTEX
476 }
477
478 #if defined(COLOR_USED)
479 color_interp = hvec4(color_highp);
480 #endif
481
482 half roughness = half(roughness_highp);
483
484 // using local coordinates (default)
485 #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
486
487 vertex = (modelview * vec4(vertex, 1.0)).xyz;
488
489 #ifdef NORMAL_USED
490 normal_highp = modelview_normal * normal_highp;
491 #endif
492
493 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
494
495 binormal_highp = modelview_normal * binormal_highp;
496 tangent_highp = modelview_normal * tangent_highp;
497 #endif
498 #endif // !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
499
500 //using world coordinates
501 #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
502
503 vertex = (read_view_matrix * vec4(vertex, 1.0)).xyz;
504 #ifdef NORMAL_USED
505 normal_highp = (read_view_matrix * vec4(normal_highp, 0.0)).xyz;
506 #endif
507
508 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
509 binormal_highp = (read_view_matrix * vec4(binormal_highp, 0.0)).xyz;
510 tangent_highp = (read_view_matrix * vec4(tangent_highp, 0.0)).xyz;
511 #endif
512 #endif
513
514 vertex_interp = vertex;
515
516 // Normalize TBN vectors before interpolation, per MikkTSpace.
517 // See: http://www.mikktspace.com/
518 #ifdef NORMAL_USED
519 normal_interp = hvec3(normalize(normal_highp));
520 #endif
521
522 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) || defined(BENT_NORMAL_MAP_USED)
523 tangent_interp = hvec3(normalize(tangent_highp));
524 binormal_interp = hvec3(normalize(binormal_highp));
525 #endif
526
527 // VERTEX LIGHTING
528 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
529 hvec3 normal = hvec3(normal_interp);
530
531 #ifdef USE_MULTIVIEW
532 hvec3 view = hvec3(-normalize(vertex_interp - eye_offset));
533 #else
534 hvec3 view = hvec3(-normalize(vertex_interp));
535 #endif
536
537 hvec4 diffuse_light = hvec4(0.0);
538 hvec4 specular_light = hvec4(0.0);
539
540 uint omni_light_count = sc_omni_lights(8);
541 uvec2 omni_light_indices = instances.data[instance_index].omni_lights;
542 for (uint i = 0; i < omni_light_count; i++) {
543 uint light_index = (i > 3) ? ((omni_light_indices.y >> ((i - 4) * 8)) & 0xFF) : ((omni_light_indices.x >> (i * 8)) & 0xFF);
544 if (i > 0 && light_index == 0xFF) {
545 break;
546 }
547
548 light_process_omni_vertex(light_index, vertex, view, normal, roughness, diffuse_light.rgb, specular_light.rgb);
549 }
550
551 uint spot_light_count = sc_spot_lights(8);
552 uvec2 spot_light_indices = instances.data[instance_index].spot_lights;
553 for (uint i = 0; i < spot_light_count; i++) {
554 uint light_index = (i > 3) ? ((spot_light_indices.y >> ((i - 4) * 8)) & 0xFF) : ((spot_light_indices.x >> (i * 8)) & 0xFF);
555 if (i > 0 && light_index == 0xFF) {
556 break;
557 }
558
559 light_process_spot_vertex(light_index, vertex, view, normal, roughness, diffuse_light.rgb, specular_light.rgb);
560 }
561
562 uint directional_lights_count = sc_directional_lights(scene_directional_light_count);
563 if (directional_lights_count > 0) {
564 // We process the first directional light separately as it may have shadows.
565 hvec3 directional_diffuse = hvec3(0.0);
566 hvec3 directional_specular = hvec3(0.0);
567
568 for (uint i = 0; i < directional_lights_count; i++) {
569 if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
570 continue; // Not masked, skip.
571 }
572
573 if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
574 continue; // Statically baked light and object uses lightmap, skip.
575 }
576 if (i == 0) {
577 light_compute_vertex(normal, hvec3(directional_lights.data[0].direction), view,
578 hvec3(directional_lights.data[0].color * directional_lights.data[0].energy),
579 true, roughness,
580 directional_diffuse,
581 directional_specular);
582 } else {
583 light_compute_vertex(normal, hvec3(directional_lights.data[i].direction), view,
584 hvec3(directional_lights.data[i].color * directional_lights.data[i].energy),
585 true, roughness,
586 diffuse_light.rgb,
587 specular_light.rgb);
588 }
589 }
590
591 // Calculate the contribution from the shadowed light so we can scale the shadows accordingly.
592 half diff_avg = dot(diffuse_light.rgb, hvec3(0.33333));
593 half diff_dir_avg = dot(directional_diffuse, hvec3(0.33333));
594 if (diff_avg > half(0.0)) {
595 diffuse_light.a = diff_dir_avg / (diff_avg + diff_dir_avg);
596 } else {
597 diffuse_light.a = half(1.0);
598 }
599
600 diffuse_light.rgb += directional_diffuse;
601
602 half spec_avg = dot(specular_light.rgb, hvec3(0.33333));
603 half spec_dir_avg = dot(directional_specular, hvec3(0.33333));
604 if (spec_avg > half(0.0)) {
605 specular_light.a = spec_dir_avg / (spec_avg + spec_dir_avg);
606 } else {
607 specular_light.a = half(1.0);
608 }
609
610 specular_light.rgb += directional_specular;
611 }
612
613 diffuse_light_interp = hvec4(diffuse_light);
614 specular_light_interp = hvec4(specular_light);
615
616 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
617
618 #ifdef MODE_RENDER_DEPTH
619
620 #ifdef MODE_DUAL_PARABOLOID
621
622 vertex_interp.z *= dual_paraboloid_side;
623
624 dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
625
626 //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
627
628 vec3 vtx = vertex_interp;
629 float distance = length(vtx);
630 vtx = normalize(vtx);
631 vtx.xy /= 1.0 - vtx.z;
632 vtx.z = (distance / z_far);
633 vtx.z = vtx.z * 2.0 - 1.0;
634 vertex_interp = vtx;
635
636 #endif
637
638 #endif //MODE_RENDER_DEPTH
639
640 #ifdef OVERRIDE_POSITION
641 gl_Position = position;
642 #else
643 gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
644 #endif // OVERRIDE_POSITION
645
646 #if defined(Z_CLIP_SCALE_USED) && !defined(SHADOW_PASS)
647 gl_Position.z = mix(gl_Position.w, gl_Position.z, z_clip_scale);
648 #endif
649
650 #ifdef MODE_RENDER_DEPTH
651 if (bool(scene_flags & SCENE_DATA_FLAGS_USE_PANCAKE_SHADOWS)) {
652 if (gl_Position.z >= 0.9999) {
653 gl_Position.z = 0.9999;
654 }
655 }
656 #endif // MODE_RENDER_DEPTH
657 #ifdef MODE_RENDER_MATERIAL
658 if (bool(scene_flags & SCENE_DATA_FLAGS_USE_UV2_MATERIAL)) {
659 vec2 uv_dest_attrib;
660 if (uv_scale != vec4(0.0)) {
661 uv_dest_attrib = (uv2_attrib.xy - 0.5) * uv_scale.zw;
662 } else {
663 uv_dest_attrib = uv2_attrib.xy;
664 }
665
666 vec2 uv_offset = unpackHalf2x16(draw_call.uv_offset);
667 gl_Position.xy = (uv_dest_attrib + uv_offset) * 2.0 - 1.0;
668 gl_Position.z = 0.00001;
669 gl_Position.w = 1.0;
670 }
671 #endif // MODE_RENDER_MATERIAL
672 #ifdef MODE_RENDER_MOTION_VECTORS
673 screen_position_output = gl_Position;
674 #endif // MODE_RENDER_MOTION_VECTORS
675
676 #ifdef POINT_SIZE_USED
677 if (sc_emulate_point_size) {
678 vec2 point_coords[6] = vec2[](
679 vec2(0, 1),
680 vec2(0, 0),
681 vec2(1, 1),
682 vec2(0, 0),
683 vec2(1, 0),
684 vec2(1, 1));
685
686 vec2 point_coord = point_coords[gl_VertexIndex % 6];
687 gl_Position.xy += (point_coord * 2.0 - 1.0) * (point_size / scene_data.viewport_size) * gl_Position.w;
688
689 #ifdef POINT_COORD_USED
690 point_coord_interp = point_coord;
691 #endif
692 } else {
693 gl_PointSize = point_size;
694 }
695 #endif
696 }
697
698 void main() {
699 #if defined(MODE_RENDER_MOTION_VECTORS)
700 vec3 prev_vertex;
701 #ifdef NORMAL_USED
702 vec3 prev_normal;
703 #endif
704 #if defined(NORMAL_USED) || defined(TANGENT_USED)
705 vec3 prev_tangent;
706 vec3 prev_binormal;
707 #endif
708
709 _unpack_vertex_attributes(
710 previous_vertex_attrib,
711 instances.data[draw_call.instance_index].compressed_aabb_position_pad.xyz,
712 instances.data[draw_call.instance_index].compressed_aabb_size_pad.xyz,
713 #if defined(NORMAL_USED) || defined(TANGENT_USED)
714 previous_normal_attrib,
715 #ifdef NORMAL_USED
716 prev_normal,
717 #endif
718 prev_tangent,
719 prev_binormal,
720 #endif
721 prev_vertex);
722
723 vertex_shader(prev_vertex,
724 #ifdef NORMAL_USED
725 prev_normal,
726 #endif
727 #if defined(NORMAL_USED) || defined(TANGENT_USED)
728 prev_tangent,
729 prev_binormal,
730 #endif
731 draw_call.instance_index, draw_call.multimesh_motion_vectors_previous_offset, instances.data[draw_call.instance_index].prev_transform,
732 #ifdef USE_DOUBLE_PRECISION
733 instances.data[draw_call.instance_index].prev_model_precision.xyz,
734 scene_data_block.prev_data.inv_view_precision,
735 #endif
736
737 #ifdef MODE_DUAL_PARABOLOID
738 scene_data_block.prev_data.dual_paraboloid_side,
739 scene_data_block.prev_data.z_far,
740 #endif
741 #if defined(MODE_RENDER_DEPTH) || defined(MODE_RENDER_MATERIAL)
742 scene_data_block.prev_data.flags,
743 #endif
744 #ifdef USE_MULTIVIEW
745 scene_data_block.prev_data.projection_matrix_view[ViewIndex],
746 scene_data_block.prev_data.inv_projection_matrix_view[ViewIndex],
747 scene_data_block.prev_data.eye_offset[ViewIndex],
748 #else
749 scene_data_block.prev_data.projection_matrix,
750 scene_data_block.prev_data.inv_projection_matrix,
751 #endif
752 scene_data_block.prev_data.view_matrix,
753 scene_data_block.prev_data.inv_view_matrix,
754 scene_data_block.prev_data.viewport_size,
755 scene_data_block.prev_data.directional_light_count,
756 prev_screen_position);
757 #else
758 // Unused output.
759 vec4 screen_position;
760 #endif // MODE_RENDER_MOTION_VECTORS
761
762 vec3 vertex;
763 #ifdef NORMAL_USED
764 vec3 normal;
765 #endif
766 #if defined(NORMAL_USED) || defined(TANGENT_USED)
767 vec3 tangent;
768 vec3 binormal;
769 #endif
770
771 _unpack_vertex_attributes(
772 vertex_angle_attrib,
773 instances.data[draw_call.instance_index].compressed_aabb_position_pad.xyz,
774 instances.data[draw_call.instance_index].compressed_aabb_size_pad.xyz,
775 #if defined(NORMAL_USED) || defined(TANGENT_USED)
776 axis_tangent_attrib,
777 #ifdef NORMAL_USED
778 normal,
779 #endif
780 tangent,
781 binormal,
782 #endif
783 vertex);
784
785 vertex_shader(vertex,
786 #ifdef NORMAL_USED
787 normal,
788 #endif
789 #if defined(NORMAL_USED) || defined(TANGENT_USED)
790 tangent,
791 binormal,
792 #endif
793 draw_call.instance_index, draw_call.multimesh_motion_vectors_current_offset, instances.data[draw_call.instance_index].transform,
794 #ifdef USE_DOUBLE_PRECISION
795 instances.data[draw_call.instance_index].model_precision.xyz,
796 scene_data_block.data.inv_view_precision,
797 #endif
798 #ifdef MODE_DUAL_PARABOLOID
799 scene_data_block.data.dual_paraboloid_side,
800 scene_data_block.data.z_far,
801 #endif
802 #if defined(MODE_RENDER_DEPTH) || defined(MODE_RENDER_MATERIAL)
803 scene_data_block.data.flags,
804 #endif
805 #ifdef USE_MULTIVIEW
806 scene_data_block.data.projection_matrix_view[ViewIndex],
807 scene_data_block.data.inv_projection_matrix_view[ViewIndex],
808 scene_data_block.data.eye_offset[ViewIndex],
809 #else
810 scene_data_block.data.projection_matrix,
811 scene_data_block.data.inv_projection_matrix,
812 #endif
813 scene_data_block.data.view_matrix,
814 scene_data_block.data.inv_view_matrix,
815 scene_data_block.data.viewport_size,
816 scene_data_block.data.directional_light_count,
817 screen_position);
818 }
819
820 #[fragment]
821
822 #version 450
823
824 #VERSION_DEFINES
825
826 #define SHADER_IS_SRGB false
827 #define SHADER_SPACE_FAR 0.0
828
829 #ifdef SHADOW_PASS
830 #define IN_SHADOW_PASS true
831 #else
832 #define IN_SHADOW_PASS false
833 #endif
834
835 #ifdef USE_MULTIVIEW
836 #define OUTPUT_IS_MULTIVIEW true
837 #else
838 #define OUTPUT_IS_MULTIVIEW false
839 #endif
840
841 /* Include half precision types. */
842 #include "../half_inc.glsl"
843
844 /* Include our forward mobile UBOs definitions etc. */
845 #include "scene_forward_mobile_inc.glsl"
846
847 /* Varyings */
848
849 // All interpolators are intentionally kept at full precision as storageInputOutput16 is not
850 // checked for support. Devices with Adreno GPUs don't usually support this capability.
851
852 layout(location = 0) in vec3 vertex_interp;
853
854 #ifdef NORMAL_USED
855 layout(location = 1) in vec3 normal_interp;
856 #endif
857
858 #if defined(COLOR_USED)
859 layout(location = 2) in vec4 color_interp;
860 #endif
861
862 #ifdef UV_USED
863 layout(location = 3) in vec2 uv_interp;
864 #endif
865
866 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
867 layout(location = 4) in vec2 uv2_interp;
868 #endif
869
870 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(BENT_NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
871 layout(location = 5) in vec3 tangent_interp;
872 layout(location = 6) in vec3 binormal_interp;
873 #endif
874
875 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && defined(USE_VERTEX_LIGHTING)
876 layout(location = 7) in vec4 diffuse_light_interp;
877 layout(location = 8) in vec4 specular_light_interp;
878 #endif
879
880 #ifdef MODE_DUAL_PARABOLOID
881
882 layout(location = 9) in float dp_clip;
883
884 #endif
885
886 #if defined(MODE_RENDER_MOTION_VECTORS)
887 layout(location = 12) in highp vec4 screen_position;
888 layout(location = 13) in highp vec4 prev_screen_position;
889 #endif
890
891 #ifdef USE_LIGHTMAP
892 // w0, w1, w2, and w3 are the four cubic B-spline basis functions
893 float w0(float a) {
894 return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);
895 }
896
897 float w1(float a) {
898 return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);
899 }
900
901 float w2(float a) {
902 return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);
903 }
904
905 float w3(float a) {
906 return (1.0 / 6.0) * (a * a * a);
907 }
908
909 // g0 and g1 are the two amplitude functions
910 float g0(float a) {
911 return w0(a) + w1(a);
912 }
913
914 float g1(float a) {
915 return w2(a) + w3(a);
916 }
917
918 // h0 and h1 are the two offset functions
919 float h0(float a) {
920 return -1.0 + w1(a) / (w0(a) + w1(a));
921 }
922
923 float h1(float a) {
924 return 1.0 + w3(a) / (w2(a) + w3(a));
925 }
926
927 vec4 textureArray_bicubic(texture2DArray tex, vec3 uv, vec2 texture_size) {
928 vec2 texel_size = vec2(1.0) / texture_size;
929
930 uv.xy = uv.xy * texture_size + vec2(0.5);
931
932 vec2 iuv = floor(uv.xy);
933 vec2 fuv = fract(uv.xy);
934
935 float g0x = g0(fuv.x);
936 float g1x = g1(fuv.x);
937 float h0x = h0(fuv.x);
938 float h1x = h1(fuv.x);
939 float h0y = h0(fuv.y);
940 float h1y = h1(fuv.y);
941
942 vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;
943 vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;
944 vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;
945 vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;
946
947 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)))) +
948 (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))));
949 }
950 #endif //USE_LIGHTMAP
951
952 #ifdef USE_MULTIVIEW
953 #extension GL_EXT_multiview : enable
954 #define ViewIndex gl_ViewIndex
955 vec3 multiview_uv(vec2 uv) {
956 return vec3(uv, ViewIndex);
957 }
958 ivec3 multiview_uv(ivec2 uv) {
959 return ivec3(uv, int(ViewIndex));
960 }
961 #else // USE_MULTIVIEW
962 #define ViewIndex 0
963 vec2 multiview_uv(vec2 uv) {
964 return uv;
965 }
966 ivec2 multiview_uv(ivec2 uv) {
967 return uv;
968 }
969 #endif // !USE_MULTIVIEW
970
971 #if defined(POINT_SIZE_USED) && defined(POINT_COORD_USED)
972 layout(location = 14) in vec2 point_coord_interp;
973 #endif
974
975 //defines to keep compatibility with vertex
976
977 #ifdef USE_MULTIVIEW
978 #define projection_matrix scene_data.projection_matrix_view[ViewIndex]
979 #define inv_projection_matrix scene_data.inv_projection_matrix_view[ViewIndex]
980 #else
981 #define projection_matrix scene_data.projection_matrix
982 #define inv_projection_matrix scene_data.inv_projection_matrix
983 #endif
984
985 #if defined(ENABLE_SSS) && defined(ENABLE_TRANSMITTANCE)
986 //both required for transmittance to be enabled
987 #define LIGHT_TRANSMITTANCE_USED
988 #endif
989
990 #ifdef MATERIAL_UNIFORMS_USED
991 /* clang-format off */
992 layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms {
993 #MATERIAL_UNIFORMS
994 } material;
995 /* clang-format on */
996 #endif
997
998 #GLOBALS
999
1000 #define scene_data scene_data_block.data
1001
1002 /* clang-format on */
1003
1004 #ifdef MODE_RENDER_DEPTH
1005
1006 #ifdef MODE_RENDER_MATERIAL
1007
1008 layout(location = 0) out vec4 albedo_output_buffer;
1009 layout(location = 1) out vec4 normal_output_buffer;
1010 layout(location = 2) out vec4 orm_output_buffer;
1011 layout(location = 3) out vec4 emission_output_buffer;
1012 layout(location = 4) out float depth_output_buffer;
1013
1014 #endif // MODE_RENDER_MATERIAL
1015
1016 #else // RENDER DEPTH
1017
1018 #ifdef MODE_MULTIPLE_RENDER_TARGETS
1019
1020 layout(location = 0) out vec4 diffuse_buffer; //diffuse (rgb) and roughness
1021 layout(location = 1) out vec4 specular_buffer; //specular and SSS (subsurface scatter)
1022 #else
1023
1024 layout(location = 0) out vec4 frag_color;
1025 #endif // MODE_MULTIPLE_RENDER_TARGETS
1026
1027 #endif // RENDER DEPTH
1028
1029 #include "../scene_forward_aa_inc.glsl"
1030
1031 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) // && !defined(USE_VERTEX_LIGHTING)
1032
1033 // Default to SPECULAR_SCHLICK_GGX.
1034 #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON)
1035 #define SPECULAR_SCHLICK_GGX
1036 #endif
1037
1038 #include "../scene_forward_lights_inc.glsl"
1039
1040 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED) && !defined(USE_VERTEX_LIGHTING)
1041
1042 #ifndef MODE_RENDER_DEPTH
1043
1044 /*
1045 Only supporting normal fog here.
1046 */
1047
1048 hvec4 fog_process(vec3 vertex) {
1049 vec3 fog_color = scene_data_block.data.fog_light_color;
1050
1051 if (sc_use_fog_aerial_perspective()) {
1052 vec3 sky_fog_color = vec3(0.0);
1053 vec3 cube_view = scene_data_block.data.radiance_inverse_xform * vertex;
1054 // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
1055 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));
1056 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1057 float roughness_lod, blend;
1058 blend = modf(mip_level * MAX_ROUGHNESS_LOD, roughness_lod);
1059 float cube_lod = vec3_to_oct_lod(dFdx(cube_view), dFdy(cube_view), scene_data_block.data.radiance_pixel_size);
1060 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));
1061 vec3 sky_sample_a = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(cube_uv, roughness_lod), cube_lod).rgb;
1062 vec3 sky_sample_b = textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(cube_uv, roughness_lod + 1), cube_lod).rgb;
1063 sky_fog_color = mix(sky_sample_a, sky_sample_b, blend);
1064 #else
1065 float roughness_lod = mip_level * MAX_ROUGHNESS_LOD;
1066 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));
1067 sky_fog_color = textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), cube_uv, roughness_lod).rgb;
1068 #endif //USE_RADIANCE_OCTMAP_ARRAY
1069 fog_color = mix(fog_color, sky_fog_color, scene_data_block.data.fog_aerial_perspective);
1070 }
1071
1072 if (sc_use_fog_sun_scatter()) {
1073 vec4 sun_scatter = vec4(0.0);
1074 float sun_total = 0.0;
1075 vec3 view = normalize(vertex);
1076
1077 uint directional_lights_count = sc_directional_lights(scene_data.directional_light_count);
1078 for (uint i = 0; i < directional_lights_count; i++) {
1079 vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
1080 float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
1081 fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
1082 }
1083 }
1084
1085 float fog_amount = 0.0;
1086
1087 if (sc_use_depth_fog()) {
1088 float fog_z = smoothstep(scene_data_block.data.fog_depth_begin, scene_data_block.data.fog_depth_end, length(vertex));
1089 float fog_quad_amount = pow(fog_z, scene_data_block.data.fog_depth_curve) * scene_data_block.data.fog_density;
1090 fog_amount = fog_quad_amount;
1091 } else {
1092 fog_amount = 1 - exp(min(0.0, -length(vertex) * scene_data_block.data.fog_density));
1093 }
1094
1095 if (sc_use_fog_height_density()) {
1096 mat4 inv_view_matrix = transpose(mat4(scene_data_block.data.inv_view_matrix[0],
1097 scene_data_block.data.inv_view_matrix[1],
1098 scene_data_block.data.inv_view_matrix[2],
1099 vec4(0.0, 0.0, 0.0, 1.0)));
1100
1101 float y = (inv_view_matrix * vec4(vertex, 1.0)).y;
1102
1103 float y_dist = y - scene_data_block.data.fog_height;
1104
1105 float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data_block.data.fog_height_density));
1106
1107 fog_amount = max(vfog_amount, fog_amount);
1108 }
1109
1110 return hvec4(fog_color, fog_amount);
1111 }
1112
1113 #endif //!MODE_RENDER DEPTH
1114
1115 void main() {
1116 #ifdef UBERSHADER
1117 bool front_facing = gl_FrontFacing;
1118 if (uc_cull_mode() == POLYGON_CULL_BACK && !front_facing) {
1119 discard;
1120 } else if (uc_cull_mode() == POLYGON_CULL_FRONT && front_facing) {
1121 discard;
1122 }
1123 #endif
1124 #ifdef MODE_DUAL_PARABOLOID
1125
1126 if (dp_clip > 0.0) {
1127 discard;
1128 }
1129 #endif
1130
1131 //lay out everything, whatever is unused is optimized away anyway
1132 vec3 vertex = vertex_interp;
1133 #ifdef USE_MULTIVIEW
1134 vec3 eye_offset = scene_data.eye_offset[ViewIndex].xyz;
1135 vec3 view_highp = -normalize(vertex_interp - eye_offset);
1136 #else
1137 vec3 eye_offset = vec3(0.0, 0.0, 0.0);
1138 vec3 view_highp = -normalize(vertex_interp);
1139 #endif
1140 vec3 albedo_highp = vec3(1.0);
1141 vec3 backlight_highp = vec3(0.0);
1142 vec4 transmittance_color_highp = vec4(0.0);
1143 float transmittance_depth_highp = 0.0;
1144 float transmittance_boost_highp = 0.0;
1145 float metallic_highp = 0.0;
1146 float specular_highp = 0.5;
1147 vec3 emission_highp = vec3(0.0);
1148 float roughness_highp = 1.0;
1149 float rim_highp = 0.0;
1150 float rim_tint_highp = 0.0;
1151 float clearcoat_highp = 0.0;
1152 float clearcoat_roughness_highp = 0.0;
1153 float anisotropy_highp = 0.0;
1154 vec2 anisotropy_flow_highp = vec2(1.0, 0.0);
1155 #ifdef PREMUL_ALPHA_USED
1156 float premul_alpha_highp = 1.0;
1157 #endif
1158 vec4 fog_highp = vec4(0.0);
1159 #if defined(CUSTOM_RADIANCE_USED)
1160 vec4 custom_radiance_highp = vec4(0.0);
1161 #endif
1162 #if defined(CUSTOM_IRRADIANCE_USED)
1163 vec4 custom_irradiance_highp = vec4(0.0);
1164 #endif
1165
1166 float ao_highp = 1.0;
1167 float ao_light_affect_highp = 0.0;
1168
1169 float alpha_highp = 1.0;
1170
1171 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) || defined(BENT_NORMAL_MAP_USED)
1172 vec3 binormal_highp = binormal_interp;
1173 vec3 tangent_highp = tangent_interp;
1174 #else // TANGENT_USED || NORMAL_MAP_USED || LIGHT_ANISOTROPY_USED || BENT_NORMAL_MAP_USED
1175 vec3 binormal_highp = vec3(0.0);
1176 vec3 tangent_highp = vec3(0.0);
1177 #endif
1178
1179 #ifdef NORMAL_USED
1180 vec3 normal_highp = normal_interp;
1181 #if defined(DO_SIDE_CHECK)
1182 if (!gl_FrontFacing) {
1183 normal_highp = -normal_highp;
1184 }
1185 #endif // DO_SIDE_CHECK
1186 #endif // NORMAL_USED
1187
1188 #ifdef UV_USED
1189 vec2 uv = uv_interp;
1190 #endif
1191
1192 #if defined(UV2_USED) || defined(USE_LIGHTMAP)
1193 vec2 uv2 = uv2_interp;
1194 #endif
1195
1196 #if defined(COLOR_USED)
1197 vec4 color_highp = color_interp;
1198 #endif
1199
1200 #if defined(NORMAL_MAP_USED)
1201
1202 vec3 normal_map_highp = vec3(0.5);
1203 #endif
1204
1205 #if defined(BENT_NORMAL_MAP_USED)
1206 hvec3 bent_normal_vector;
1207 vec3 bent_normal_map_highp = vec3(0.5);
1208 #endif
1209
1210 float normal_map_depth_highp = 1.0;
1211
1212 vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size;
1213
1214 float sss_strength_highp = 0.0;
1215
1216 #ifdef ALPHA_SCISSOR_USED
1217 float alpha_scissor_threshold_highp = 1.0;
1218 #endif // ALPHA_SCISSOR_USED
1219
1220 #ifdef ALPHA_HASH_USED
1221 float alpha_hash_scale_highp = 1.0;
1222 #endif // ALPHA_HASH_USED
1223
1224 #ifdef ALPHA_ANTIALIASING_EDGE_USED
1225 float alpha_antialiasing_edge_highp = 0.0;
1226 vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
1227 #endif // ALPHA_ANTIALIASING_EDGE_USED
1228
1229 mat4 inv_view_matrix = transpose(mat4(scene_data.inv_view_matrix[0],
1230 scene_data.inv_view_matrix[1],
1231 scene_data.inv_view_matrix[2],
1232 vec4(0.0, 0.0, 0.0, 1.0)));
1233 mat4 read_model_matrix = transpose(mat4(instances.data[draw_call.instance_index].transform[0],
1234 instances.data[draw_call.instance_index].transform[1],
1235 instances.data[draw_call.instance_index].transform[2],
1236 vec4(0.0, 0.0, 0.0, 1.0)));
1237
1238 #ifdef LIGHT_VERTEX_USED
1239 vec3 light_vertex = vertex;
1240 #endif //LIGHT_VERTEX_USED
1241
1242 mat3 model_normal_matrix;
1243 if (bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
1244 model_normal_matrix = transpose(inverse(mat3(read_model_matrix)));
1245 } else {
1246 model_normal_matrix = mat3(read_model_matrix);
1247 }
1248
1249 mat4 read_view_matrix = transpose(mat4(scene_data.view_matrix[0],
1250 scene_data.view_matrix[1],
1251 scene_data.view_matrix[2],
1252 vec4(0.0, 0.0, 0.0, 1.0)));
1253 vec2 read_viewport_size = scene_data.viewport_size;
1254
1255 #ifdef POINT_COORD_USED
1256 #ifdef POINT_SIZE_USED
1257 vec2 point_coord;
1258 if (sc_emulate_point_size) {
1259 point_coord = point_coord_interp;
1260 } else {
1261 point_coord = gl_PointCoord;
1262 }
1263 #else // !POINT_SIZE_USED
1264 vec2 point_coord = vec2(0.5);
1265 #endif
1266 #endif
1267
1268 {
1269 #CODE : FRAGMENT
1270 }
1271
1272 // Store variables in half precision after user shader code is run.
1273 hvec3 view = hvec3(view_highp);
1274 hvec3 albedo = hvec3(albedo_highp);
1275 hvec3 backlight = hvec3(backlight_highp);
1276 hvec4 transmittance_color = hvec4(transmittance_color_highp);
1277 half transmittance_depth = half(transmittance_depth_highp);
1278 half transmittance_boost = half(transmittance_boost_highp);
1279 half metallic = half(metallic_highp);
1280 half specular = half(specular_highp);
1281 hvec3 emission = hvec3(emission_highp);
1282 half roughness = half(roughness_highp);
1283 half rim = half(rim_highp);
1284 half rim_tint = half(rim_tint_highp);
1285 half clearcoat = half(clearcoat_highp);
1286 half clearcoat_roughness = half(clearcoat_roughness_highp);
1287 half anisotropy = half(anisotropy_highp);
1288 hvec2 anisotropy_flow = hvec2(anisotropy_flow_highp);
1289 half ao = half(ao_highp);
1290 half ao_light_affect = half(ao_light_affect_highp);
1291 half alpha = half(alpha_highp);
1292 half normal_map_depth = half(normal_map_depth_highp);
1293 half sss_strength = half(sss_strength_highp);
1294 #ifdef PREMUL_ALPHA_USED
1295 half premul_alpha = half(premul_alpha_highp);
1296 #endif
1297 hvec4 fog = hvec4(fog_highp);
1298 #ifdef CUSTOM_RADIANCE_USED
1299 hvec4 custom_radiance = hvec4(custom_radiance_highp);
1300 #endif
1301 #ifdef CUSTOM_IRRADIANCE_USED
1302 hvec4 custom_irradiance = hvec4(custom_irradiance_highp);
1303 #endif
1304 #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) || defined(BENT_NORMAL_MAP_USED)
1305 hvec3 binormal = hvec3(binormal_highp);
1306 hvec3 tangent = hvec3(tangent_highp);
1307 #else
1308 hvec3 binormal = hvec3(binormal_highp);
1309 hvec3 tangent = hvec3(tangent_highp);
1310 #endif
1311 #ifdef NORMAL_USED
1312 hvec3 normal = hvec3(normal_highp);
1313 #endif
1314 #if defined(COLOR_USED)
1315 hvec4 color = hvec4(color_highp);
1316 #endif
1317 #if defined(NORMAL_MAP_USED)
1318 hvec3 normal_map = hvec3(normal_map_highp);
1319 #endif
1320 #if defined(BENT_NORMAL_MAP_USED)
1321 hvec3 bent_normal_map = hvec3(bent_normal_map_highp);
1322 #endif
1323 #ifdef ALPHA_SCISSOR_USED
1324 half alpha_scissor_threshold = half(alpha_scissor_threshold_highp);
1325 #endif
1326 #ifdef ALPHA_HASH_USED
1327 half alpha_hash_scale = half(alpha_hash_scale_highp);
1328 #endif
1329 #ifdef ALPHA_ANTIALIASING_EDGE_USED
1330 half alpha_antialiasing_edge = half(alpha_antialiasing_edge_highp);
1331 #endif
1332
1333 #ifdef LIGHT_VERTEX_USED
1334 vertex = light_vertex;
1335 #ifdef USE_MULTIVIEW
1336 view = hvec3(-normalize(vertex - eye_offset));
1337 #else
1338 view = hvec3(-normalize(vertex));
1339 #endif //USE_MULTIVIEW
1340 #endif //LIGHT_VERTEX_USED
1341
1342 #ifdef NORMAL_USED
1343 hvec3 geo_normal = normalize(normal);
1344 #endif // NORMAL_USED
1345
1346 #ifdef LIGHT_TRANSMITTANCE_USED
1347 #ifdef SSS_MODE_SKIN
1348 transmittance_color.a = sss_strength;
1349 #else
1350 transmittance_color.a *= sss_strength;
1351 #endif
1352 #endif
1353
1354 #ifndef USE_SHADOW_TO_OPACITY
1355
1356 #ifdef ALPHA_SCISSOR_USED
1357 #ifdef MODE_RENDER_MATERIAL
1358 if (alpha < alpha_scissor_threshold) {
1359 alpha = half(0.0);
1360 } else {
1361 alpha = half(1.0);
1362 }
1363 #else
1364 if (alpha < alpha_scissor_threshold) {
1365 discard;
1366 }
1367 #endif // MODE_RENDER_MATERIAL
1368 #endif // ALPHA_SCISSOR_USED
1369
1370 // alpha hash can be used in unison with alpha antialiasing
1371 #ifdef ALPHA_HASH_USED
1372 vec3 object_pos = (inverse(read_model_matrix) * inv_view_matrix * vec4(vertex, 1.0)).xyz;
1373 #ifdef MODE_RENDER_MATERIAL
1374 if (alpha < compute_alpha_hash_threshold(object_pos, alpha_hash_scale)) {
1375 alpha = half(0.0);
1376 } else {
1377 alpha = half(1.0);
1378 }
1379 #else
1380 if (alpha < compute_alpha_hash_threshold(object_pos, alpha_hash_scale)) {
1381 discard;
1382 }
1383 #endif // MODE_RENDER_MATERIAL
1384 #endif // ALPHA_HASH_USED
1385
1386 // If we are not edge antialiasing, we need to remove the output alpha channel from scissor and hash
1387 #if (defined(ALPHA_SCISSOR_USED) || defined(ALPHA_HASH_USED)) && !defined(ALPHA_ANTIALIASING_EDGE_USED) && !defined(MODE_RENDER_MATERIAL)
1388 alpha = half(1.0);
1389 #endif
1390
1391 #ifdef ALPHA_ANTIALIASING_EDGE_USED
1392 // If alpha scissor is used, we must further the edge threshold, otherwise we won't get any edge feather
1393 #ifdef ALPHA_SCISSOR_USED
1394 alpha_antialiasing_edge = clamp(alpha_scissor_threshold + alpha_antialiasing_edge, half(0.0), half(1.0));
1395 #endif
1396 alpha = compute_alpha_antialiasing_edge(alpha, alpha_texture_coordinate, alpha_antialiasing_edge);
1397 #endif // ALPHA_ANTIALIASING_EDGE_USED
1398
1399 #ifdef MODE_RENDER_DEPTH
1400 #if defined(USE_OPAQUE_PREPASS) || defined(ALPHA_ANTIALIASING_EDGE_USED)
1401 if (alpha < half(scene_data.opaque_prepass_threshold)) {
1402 discard;
1403 }
1404 #endif // USE_OPAQUE_PREPASS || ALPHA_ANTIALIASING_EDGE_USED
1405 #endif // MODE_RENDER_DEPTH
1406
1407 #endif // !USE_SHADOW_TO_OPACITY
1408
1409 #if defined(NORMAL_MAP_USED)
1410 normal_map.xy = normal_map.xy * half(2.0) - half(1.0);
1411 normal_map.z = sqrt(max(half(0.0), half(1.0) - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
1412
1413 // Tangent-space transformation is performed using unnormalized TBN vectors, per MikkTSpace.
1414 // See: http://www.mikktspace.com/
1415 normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
1416 #elif defined(NORMAL_USED)
1417 normal = geo_normal;
1418 #endif // NORMAL_MAP_USED
1419
1420 #ifdef BENT_NORMAL_MAP_USED
1421 bent_normal_map.xy = bent_normal_map.xy * half(2.0) - half(1.0);
1422 bent_normal_map.z = sqrt(max(half(0.0), half(1.0) - dot(bent_normal_map.xy, bent_normal_map.xy)));
1423
1424 bent_normal_vector = normalize(tangent * bent_normal_map.x + binormal * bent_normal_map.y + normal * bent_normal_map.z);
1425 #endif
1426
1427 #ifdef LIGHT_ANISOTROPY_USED
1428
1429 if (anisotropy > half(0.01)) {
1430 hmat3 rot = hmat3(tangent, binormal, normal);
1431 // Make local to space.
1432 tangent = normalize(rot * hvec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
1433 binormal = normalize(rot * hvec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
1434 }
1435
1436 #endif
1437
1438 #ifdef ENABLE_CLIP_ALPHA
1439 #ifdef MODE_RENDER_MATERIAL
1440 if (albedo.a < half(0.99)) {
1441 // Used for doublepass and shadowmapping.
1442 albedo.a = half(0.0);
1443 alpha = half(0.0);
1444 } else {
1445 albedo.a = half(1.0);
1446 alpha = half(1.0);
1447 }
1448 #else
1449 if (albedo.a < half(0.99)) {
1450 //used for doublepass and shadowmapping
1451 discard;
1452 }
1453 #endif // MODE_RENDER_MATERIAL
1454 #endif
1455
1456 /////////////////////// FOG //////////////////////
1457 #ifndef MODE_RENDER_DEPTH
1458
1459 #ifndef FOG_DISABLED
1460 #ifndef CUSTOM_FOG_USED
1461 // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
1462
1463 if (!sc_disable_fog() && bool(scene_data.flags & SCENE_DATA_FLAGS_USE_FOG)) {
1464 fog = fog_process(vertex);
1465 }
1466
1467 #endif //!CUSTOM_FOG_USED
1468
1469 #endif //!FOG_DISABLED
1470 #endif //!MODE_RENDER_DEPTH
1471
1472 /////////////////////// DECALS ////////////////////////////////
1473
1474 #ifndef MODE_RENDER_DEPTH
1475
1476 vec3 vertex_ddx = dFdx(vertex);
1477 vec3 vertex_ddy = dFdy(vertex);
1478
1479 uint decal_count = sc_decals(8);
1480 uvec2 decal_indices = instances.data[draw_call.instance_index].decals;
1481 for (uint i = 0; i < decal_count; i++) {
1482 uint decal_index = (i > 3) ? ((decal_indices.y >> ((i - 4) * 8)) & 0xFF) : ((decal_indices.x >> (i * 8)) & 0xFF);
1483 if (decal_index == 0xFF) {
1484 break;
1485 }
1486
1487 vec3 uv_local = (decals.data[decal_index].xform * vec4(vertex, 1.0)).xyz;
1488 if (any(lessThan(uv_local, vec3(0.0, -1.0, 0.0))) || any(greaterThan(uv_local, vec3(1.0)))) {
1489 continue; //out of decal
1490 }
1491
1492 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);
1493
1494 if (decals.data[decal_index].normal_fade > 0.0) {
1495 fade *= smoothstep(decals.data[decal_index].normal_fade, 1.0, dot(vec3(geo_normal), decals.data[decal_index].normal) * 0.5 + 0.5);
1496 }
1497
1498 //we need ddx/ddy for mipmaps, so simulate them
1499 vec2 ddx = (decals.data[decal_index].xform * vec4(vertex_ddx, 0.0)).xz;
1500 vec2 ddy = (decals.data[decal_index].xform * vec4(vertex_ddy, 0.0)).xz;
1501
1502 if (decals.data[decal_index].albedo_rect != vec4(0.0)) {
1503 //has albedo
1504 vec4 decal_albedo;
1505 if (sc_decal_use_mipmaps()) {
1506 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);
1507 } else {
1508 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);
1509 }
1510 decal_albedo *= decals.data[decal_index].modulate;
1511 decal_albedo.a *= fade;
1512 albedo = hvec3(mix(vec3(albedo), decal_albedo.rgb, decal_albedo.a * decals.data[decal_index].albedo_mix));
1513
1514 if (decals.data[decal_index].normal_rect != vec4(0.0)) {
1515 vec3 decal_normal;
1516 if (sc_decal_use_mipmaps()) {
1517 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;
1518 } else {
1519 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;
1520 }
1521 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
1522 decal_normal.z = sqrt(max(0.0, 1.0 - dot(decal_normal.xy, decal_normal.xy)));
1523 //convert to view space, use xzy because y is up
1524 decal_normal = (decals.data[decal_index].normal_xform * decal_normal.xzy).xyz;
1525
1526 normal = hvec3(normalize(mix(vec3(normal), decal_normal, decal_albedo.a)));
1527 }
1528
1529 if (decals.data[decal_index].orm_rect != vec4(0.0)) {
1530 vec3 decal_orm;
1531 if (sc_decal_use_mipmaps()) {
1532 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;
1533 } else {
1534 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;
1535 }
1536 ao = half(mix(float(ao), decal_orm.r, decal_albedo.a));
1537 roughness = half(mix(float(roughness), decal_orm.g, decal_albedo.a));
1538 metallic = half(mix(float(metallic), decal_orm.b, decal_albedo.a));
1539 }
1540 }
1541
1542 if (decals.data[decal_index].emission_rect != vec4(0.0)) {
1543 //emission is additive, so its independent from albedo
1544 if (sc_decal_use_mipmaps()) {
1545 emission += hvec3(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].emission_energy * fade);
1546 } else {
1547 emission += hvec3(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].emission_energy * fade);
1548 }
1549 }
1550 }
1551 #endif //!MODE_RENDER_DEPTH
1552
1553 /////////////////////// LIGHTING //////////////////////////////
1554
1555 #ifdef NORMAL_USED
1556 if (sc_scene_roughness_limiter_enabled()) {
1557 //https://www.jp.square-enix.com/tech/library/pdf/ImprovedGeometricSpecularAA.pdf
1558 // SPIR-V Validation claims that derivatives of FP16 vectors are not valid code generation (see #108009).
1559 vec3 dn = vec3(normal);
1560 vec3 dndu = dFdx(dn), dndv = dFdy(dn);
1561 half roughness2 = roughness * roughness;
1562 half variance = half(scene_data.roughness_limiter_amount) * half(dot(dndu, dndu) + dot(dndv, dndv));
1563 half kernelRoughness2 = min(half(2.0) * variance, half(scene_data.roughness_limiter_limit));
1564 half filteredRoughness2 = min(half(1.0), roughness2 + kernelRoughness2);
1565 roughness = sqrt(filteredRoughness2);
1566 }
1567 #endif // NORMAL_USED
1568 //apply energy conservation
1569
1570 hvec3 indirect_specular_light = hvec3(0.0);
1571 hvec3 direct_specular_light = hvec3(0.0);
1572 hvec3 diffuse_light = hvec3(0.0);
1573 hvec3 ambient_light = hvec3(0.0);
1574
1575 #ifndef MODE_UNSHADED
1576 // Used in regular draw pass and when drawing SDFs for SDFGI and materials for VoxelGI.
1577 emission *= half(scene_data.emissive_exposure_normalization);
1578 #endif
1579
1580 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1581
1582 #ifndef AMBIENT_LIGHT_DISABLED
1583 #ifdef BENT_NORMAL_MAP_USED
1584 hvec3 indirect_normal = bent_normal_vector;
1585 #else
1586 hvec3 indirect_normal = normal;
1587 #endif
1588
1589 if (sc_scene_use_reflection_cubemap()) {
1590 #ifdef LIGHT_ANISOTROPY_USED
1591 // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
1592 hvec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
1593 hvec3 anisotropic_tangent = cross(anisotropic_direction, view);
1594 hvec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
1595 hvec3 bent_normal = normalize(mix(indirect_normal, anisotropic_normal, anisotropy * clamp(half(5.0) * roughness, half(0.0), half(1.0))));
1596 hvec3 ref_vec = reflect(-view, bent_normal);
1597 ref_vec = mix(ref_vec, bent_normal, roughness * roughness);
1598 #else
1599 hvec3 ref_vec = reflect(-view, indirect_normal);
1600 ref_vec = mix(ref_vec, indirect_normal, roughness * roughness);
1601 #endif
1602 half horizon = min(half(1.0) + dot(ref_vec, indirect_normal), half(1.0));
1603 ref_vec = hvec3(scene_data.radiance_inverse_xform * vec3(ref_vec));
1604 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1605 float lod;
1606 half blend = half(modf(float(sqrt(roughness) * MAX_ROUGHNESS_LOD), lod));
1607
1608 float ref_lod = vec3_to_oct_lod(dFdx(vec3(ref_vec)), dFdy(vec3(ref_vec)), scene_data_block.data.radiance_pixel_size);
1609 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));
1610 hvec3 indirect_sample_a = hvec3(textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, float(lod)), ref_lod).rgb);
1611 hvec3 indirect_sample_b = hvec3(textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, float(lod) + 1.0), ref_lod).rgb);
1612 indirect_specular_light = mix(indirect_sample_a, indirect_sample_b, blend);
1613 #else // USE_RADIANCE_OCTMAP_ARRAY
1614 float lod = sqrt(roughness) * MAX_ROUGHNESS_LOD;
1615 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));
1616 indirect_specular_light = hvec3(textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), ref_uv, lod).rgb);
1617 #endif //USE_RADIANCE_OCTMAP_ARRAY
1618 indirect_specular_light *= REFLECTION_MULTIPLIER;
1619 indirect_specular_light *= half(scene_data.IBL_exposure_normalization);
1620 indirect_specular_light *= horizon * horizon;
1621 indirect_specular_light *= half(scene_data.ambient_light_color_energy.a);
1622 }
1623
1624 #if defined(CUSTOM_RADIANCE_USED)
1625 indirect_specular_light = mix(indirect_specular_light, custom_radiance.rgb, custom_radiance.a);
1626 #endif // CUSTOM_RADIANCE_USED
1627
1628 #ifndef USE_LIGHTMAP
1629 //lightmap overrides everything
1630 if (bool(scene_data.flags & SCENE_DATA_FLAGS_USE_AMBIENT_LIGHT)) {
1631 ambient_light = hvec3(scene_data.ambient_light_color_energy.rgb);
1632
1633 if (sc_scene_use_ambient_cubemap()) {
1634 vec3 ambient_dir = scene_data.radiance_inverse_xform * indirect_normal;
1635 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1636 float ambient_lod = vec3_to_oct_lod(dFdx(ambient_dir), dFdy(ambient_dir), scene_data_block.data.radiance_pixel_size);
1637 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));
1638 hvec3 octmap_ambient = hvec3(textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ambient_uv, MAX_ROUGHNESS_LOD), ambient_lod).rgb);
1639 #else
1640 float roughness_lod = MAX_ROUGHNESS_LOD;
1641 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));
1642 hvec3 octmap_ambient = hvec3(textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), ambient_uv, roughness_lod).rgb);
1643 #endif //USE_RADIANCE_OCTMAP_ARRAY
1644 octmap_ambient *= REFLECTION_MULTIPLIER;
1645 octmap_ambient *= half(scene_data.IBL_exposure_normalization);
1646 ambient_light = mix(ambient_light, octmap_ambient * half(scene_data.ambient_light_color_energy.a), half(scene_data.ambient_color_sky_mix));
1647 }
1648 }
1649 #endif // !USE_LIGHTMAP
1650
1651 #if defined(CUSTOM_IRRADIANCE_USED)
1652 ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a);
1653 #endif // CUSTOM_IRRADIANCE_USED
1654 #ifdef LIGHT_CLEARCOAT_USED
1655
1656 if (sc_scene_use_reflection_cubemap()) {
1657 half NoV = max(dot(geo_normal, view), half(0.0001));
1658 hvec3 ref_vec = reflect(-view, geo_normal);
1659 ref_vec = mix(ref_vec, geo_normal, clearcoat_roughness * clearcoat_roughness);
1660 // The clear coat layer assumes an IOR of 1.5 (4% reflectance)
1661 half Fc = clearcoat * (half(0.04) + half(0.96) * SchlickFresnel(NoV));
1662 half attenuation = half(1.0) - Fc;
1663 ambient_light *= attenuation;
1664 indirect_specular_light *= attenuation;
1665
1666 half horizon = min(half(1.0) + dot(ref_vec, indirect_normal), half(1.0));
1667 ref_vec = hvec3(scene_data.radiance_inverse_xform * vec3(ref_vec));
1668 float roughness_lod = mix(0.001, 0.1, sqrt(float(clearcoat_roughness))) * MAX_ROUGHNESS_LOD;
1669 #ifdef USE_RADIANCE_OCTMAP_ARRAY
1670 float lod;
1671 half blend = half(modf(roughness_lod, lod));
1672
1673 float ref_lod = vec3_to_oct_lod(dFdx(vec3(ref_vec)), dFdy(vec3(ref_vec)), scene_data_block.data.radiance_pixel_size);
1674 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));
1675 hvec3 clearcoat_sample_a = hvec3(textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, lod), ref_lod).rgb);
1676 hvec3 clearcoat_sample_b = hvec3(textureLod(sampler2DArray(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3(ref_uv, lod + 1), ref_lod).rgb);
1677 hvec3 clearcoat_light = mix(clearcoat_sample_a, clearcoat_sample_b, blend);
1678 #else
1679 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));
1680 hvec3 clearcoat_light = hvec3(textureLod(sampler2D(radiance_octmap, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), ref_uv, roughness_lod).rgb);
1681 #endif //USE_RADIANCE_OCTMAP_ARRAY
1682 indirect_specular_light += clearcoat_light * horizon * horizon * Fc * half(scene_data.ambient_light_color_energy.a);
1683 }
1684 #endif // LIGHT_CLEARCOAT_USED
1685 #endif // !AMBIENT_LIGHT_DISABLED
1686 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1687
1688 //radiance
1689
1690 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1691 #ifndef AMBIENT_LIGHT_DISABLED
1692 #ifdef USE_LIGHTMAP
1693
1694 //lightmap
1695 if (bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture
1696 uint index = instances.data[draw_call.instance_index].gi_offset;
1697
1698 // The world normal.
1699 hvec3 wnormal = hmat3(inv_view_matrix) * indirect_normal;
1700
1701 // The SH coefficients used for evaluating diffuse data from SH probes.
1702 const half c[5] = half[](
1703 half(0.886227), // l0 sqrt(1.0/(4.0*PI)) * PI
1704 half(1.023327), // l1 sqrt(3.0/(4.0*PI)) * PI*2.0/3.0
1705 half(0.858086), // l2n2, l2n1, l2p1 sqrt(15.0/(4.0*PI)) * PI*1.0/4.0
1706 half(0.247708), // l20 sqrt(5.0/(16.0*PI)) * PI*1.0/4.0
1707 half(0.429043) // l2p2 sqrt(15.0/(16.0*PI))* PI*1.0/4.0
1708 );
1709
1710 half norm = half(scene_data.IBL_exposure_normalization);
1711 ambient_light += c[0] * hvec3(lightmap_captures.data[index].sh[0].rgb) * norm;
1712 ambient_light += c[1] * hvec3(lightmap_captures.data[index].sh[1].rgb) * wnormal.y * norm;
1713 ambient_light += c[1] * hvec3(lightmap_captures.data[index].sh[2].rgb) * wnormal.z * norm;
1714 ambient_light += c[1] * hvec3(lightmap_captures.data[index].sh[3].rgb) * wnormal.x * norm;
1715 ambient_light += c[2] * hvec3(lightmap_captures.data[index].sh[4].rgb) * wnormal.x * wnormal.y * norm;
1716 ambient_light += c[2] * hvec3(lightmap_captures.data[index].sh[5].rgb) * wnormal.y * wnormal.z * norm;
1717 ambient_light += c[3] * hvec3(lightmap_captures.data[index].sh[6].rgb) * (half(3.0) * wnormal.z * wnormal.z - half(1.0)) * norm;
1718 ambient_light += c[2] * hvec3(lightmap_captures.data[index].sh[7].rgb) * wnormal.x * wnormal.z * norm;
1719 ambient_light += c[4] * hvec3(lightmap_captures.data[index].sh[8].rgb) * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) * norm;
1720
1721 } else if (bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
1722 bool uses_sh = bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
1723 uint ofs = instances.data[draw_call.instance_index].gi_offset & 0xFFFF;
1724 uint slice = instances.data[draw_call.instance_index].gi_offset >> 16;
1725 vec3 uvw;
1726 uvw.xy = uv2 * instances.data[draw_call.instance_index].lightmap_uv_scale.zw + instances.data[draw_call.instance_index].lightmap_uv_scale.xy;
1727 uvw.z = float(slice);
1728
1729 if (uses_sh) {
1730 uvw.z *= 4.0; //SH textures use 4 times more data
1731 hvec3 lm_light_l0;
1732 hvec3 lm_light_l1n1;
1733 hvec3 lm_light_l1_0;
1734 hvec3 lm_light_l1p1;
1735
1736 if (sc_use_lightmap_bicubic_filter()) {
1737 lm_light_l0 = hvec3(textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 0.0), lightmaps.data[ofs].light_texture_size).rgb);
1738 lm_light_l1n1 = hvec3((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);
1739 lm_light_l1_0 = hvec3((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);
1740 lm_light_l1p1 = hvec3((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);
1741 } else {
1742 lm_light_l0 = hvec3(textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb);
1743 lm_light_l1n1 = hvec3((textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb - vec3(0.5)) * 2.0);
1744 lm_light_l1_0 = hvec3((textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb - vec3(0.5)) * 2.0);
1745 lm_light_l1p1 = hvec3((textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb - vec3(0.5)) * 2.0);
1746 }
1747
1748 hvec3 n = hvec3(normalize(lightmaps.data[ofs].normal_xform * indirect_normal));
1749 half exposure_normalization = half(lightmaps.data[ofs].exposure_normalization);
1750
1751 ambient_light += lm_light_l0 * exposure_normalization;
1752 ambient_light += lm_light_l1n1 * n.y * lm_light_l0 * exposure_normalization * half(4.0);
1753 ambient_light += lm_light_l1_0 * n.z * lm_light_l0 * exposure_normalization * half(4.0);
1754 ambient_light += lm_light_l1p1 * n.x * lm_light_l0 * exposure_normalization * half(4.0);
1755 } else {
1756 if (sc_use_lightmap_bicubic_filter()) {
1757 ambient_light += hvec3(textureArray_bicubic(lightmap_textures[ofs], uvw, lightmaps.data[ofs].light_texture_size).rgb * lightmaps.data[ofs].exposure_normalization);
1758 } else {
1759 ambient_light += hvec3(textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization);
1760 }
1761 }
1762 }
1763
1764 // No GI nor non low end mode...
1765
1766 #endif // USE_LIGHTMAP
1767
1768 // skipping ssao, do we remove ssao totally?
1769
1770 uint reflection_probe_count = sc_reflection_probes(8);
1771 if (reflection_probe_count > 0) {
1772 hvec4 reflection_accum = hvec4(0.0);
1773 hvec4 ambient_accum = hvec4(0.0);
1774
1775 #ifdef LIGHT_ANISOTROPY_USED
1776 // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy
1777 hvec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent;
1778 hvec3 anisotropic_tangent = cross(anisotropic_direction, view);
1779 hvec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction);
1780 hvec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(half(5.0) * roughness, half(0.0), half(1.0))));
1781 #else
1782 hvec3 bent_normal = normal;
1783 #endif
1784 hvec3 ref_vec = normalize(reflect(-view, bent_normal));
1785 // Interpolate between mirror and rough reflection by using linear_roughness * linear_roughness.
1786 ref_vec = mix(ref_vec, bent_normal, roughness * roughness * roughness * roughness);
1787
1788 uvec2 reflection_indices = instances.data[draw_call.instance_index].reflection_probes;
1789 for (uint i = 0; i < reflection_probe_count; i++) {
1790 uint reflection_index = (i > 3) ? ((reflection_indices.y >> ((i - 4) * 8)) & 0xFF) : ((reflection_indices.x >> (i * 8)) & 0xFF);
1791 if (reflection_index == 0xFF) {
1792 break;
1793 }
1794
1795 if (reflection_accum.a >= half(1.0) && ambient_accum.a >= half(1.0)) {
1796 break;
1797 }
1798
1799 reflection_process(reflection_index, vertex, ref_vec, normal, roughness, ambient_light, indirect_specular_light, ambient_accum, reflection_accum);
1800 }
1801
1802 if (ambient_accum.a < half(1.0)) {
1803 ambient_accum.rgb = ambient_light * (half(1.0) - ambient_accum.a) + ambient_accum.rgb;
1804 }
1805
1806 if (reflection_accum.a < half(1.0)) {
1807 reflection_accum.rgb = indirect_specular_light * (half(1.0) - reflection_accum.a) + reflection_accum.rgb;
1808 }
1809
1810 if (reflection_accum.a > half(0.0)) {
1811 indirect_specular_light = reflection_accum.rgb;
1812 }
1813
1814 #if !defined(USE_LIGHTMAP)
1815 if (ambient_accum.a > half(0.0)) {
1816 ambient_light = ambient_accum.rgb;
1817 }
1818 #endif
1819 } //Reflection probes
1820
1821 // finalize ambient light here
1822 ambient_light *= ao;
1823 #ifndef SPECULAR_OCCLUSION_DISABLED
1824 #ifdef BENT_NORMAL_MAP_USED
1825 // Simplified bent normal occlusion.
1826 half cos_b = max(dot(reflect(-view, normal), bent_normal_vector), half(0.0));
1827 half specular_occlusion = clamp((ao - (half(1.0) - cos_b)) / roughness, half(0.0), half(1.0));
1828 specular_occlusion = mix(specular_occlusion, cos_b * (half(1.0) - ao), roughness);
1829 indirect_specular_light *= specular_occlusion;
1830 #else // BENT_NORMAL_MAP_USED
1831 half specular_occlusion = (ambient_light.r * half(0.3) + ambient_light.g * half(0.59) + ambient_light.b * half(0.11)) * half(2.0); // Luminance of ambient light.
1832 specular_occlusion = min(specular_occlusion * half(4.0), half(1.0)); // This multiplication preserves speculars on bright areas.
1833
1834 half reflective_f = (half(1.0) - roughness) * metallic;
1835 // 10.0 is a magic number, it gives the intended effect in most scenarios.
1836 // Low enough for occlusion, high enough for reaction to lights and shadows.
1837 specular_occlusion = max(min(reflective_f * specular_occlusion * half(10.0), half(1.0)), specular_occlusion);
1838 indirect_specular_light *= specular_occlusion;
1839 #endif // BENT_NORMAL_MAP_USED
1840 #endif // USE_SPECULAR_OCCLUSION
1841 ambient_light *= albedo.rgb;
1842
1843 #endif // !AMBIENT_LIGHT_DISABLED
1844
1845 // convert ao to direct light ao
1846 ao = mix(half(1.0), ao, ao_light_affect);
1847
1848 //this saves some VGPRs
1849 hvec3 f0 = F0(metallic, specular, albedo);
1850
1851 #ifndef AMBIENT_LIGHT_DISABLED
1852 {
1853 #if defined(DIFFUSE_TOON)
1854 //simplify for toon, as
1855 indirect_specular_light *= specular * metallic * albedo * half(2.0);
1856 #else
1857
1858 // scales the specular reflections, needs to be computed before lighting happens,
1859 // but after environment, GI, and reflection probes are added
1860 // Environment brdf approximation (Lazarov 2013)
1861 // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
1862 const hvec4 c0 = hvec4(-1.0, -0.0275, -0.572, 0.022);
1863 const hvec4 c1 = hvec4(1.0, 0.0425, 1.04, -0.04);
1864 hvec4 r = roughness * c0 + c1;
1865 half ndotv = clamp(dot(normal, view), half(0.0), half(1.0));
1866 half a004 = min(r.x * r.x, exp2(half(-9.28) * ndotv)) * r.x + r.y;
1867 hvec2 env = hvec2(-1.04, 1.04) * a004 + r.zw;
1868
1869 indirect_specular_light *= env.x * f0 + env.y * clamp(half(50.0) * f0.g, metallic, half(1.0));
1870 #endif
1871 }
1872
1873 #endif // !AMBIENT_LIGHT_DISABLED
1874 #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1875
1876 // LIGHTING
1877 #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
1878 #ifdef USE_VERTEX_LIGHTING
1879 diffuse_light += hvec3(diffuse_light_interp.rgb);
1880 direct_specular_light += hvec3(specular_light_interp.rgb) * f0;
1881 #endif
1882
1883 uint directional_lights_count = sc_directional_lights(scene_data.directional_light_count);
1884 if (directional_lights_count > 0) {
1885 #ifndef SHADOWS_DISABLED
1886 // Do shadow and lighting in two passes to reduce register pressure
1887 half shadows[8];
1888
1889 half shadowmask = half(1.0);
1890
1891 #ifdef USE_LIGHTMAP
1892 uint shadowmask_mode = LIGHTMAP_SHADOWMASK_MODE_NONE;
1893
1894 if (bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
1895 const uint ofs = instances.data[draw_call.instance_index].gi_offset & 0xFFFF;
1896 shadowmask_mode = lightmaps.data[ofs].flags;
1897
1898 if (shadowmask_mode != LIGHTMAP_SHADOWMASK_MODE_NONE) {
1899 const uint slice = instances.data[draw_call.instance_index].gi_offset >> 16;
1900 const vec2 scaled_uv = uv2 * instances.data[draw_call.instance_index].lightmap_uv_scale.zw + instances.data[draw_call.instance_index].lightmap_uv_scale.xy;
1901 const vec3 uvw = vec3(scaled_uv, float(slice));
1902
1903 if (sc_use_lightmap_bicubic_filter()) {
1904 shadowmask = half(textureArray_bicubic(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], uvw, lightmaps.data[ofs].light_texture_size).x);
1905 } else {
1906 shadowmask = half(textureLod(sampler2DArray(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).x);
1907 }
1908 }
1909 }
1910
1911 if (shadowmask_mode != LIGHTMAP_SHADOWMASK_MODE_ONLY) {
1912 #endif // USE_LIGHTMAP
1913
1914 #ifdef USE_VERTEX_LIGHTING
1915 // Only process the first light's shadow for vertex lighting.
1916 for (uint i = 0; i < 1; i++) {
1917 #else
1918 for (uint i = 0; i < directional_lights_count; i++) {
1919 #endif
1920 if (!bool(directional_lights.data[i].mask & instances.data[draw_call.instance_index].layer_mask)) {
1921 continue; //not masked
1922 }
1923
1924 if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
1925 continue; // Statically baked light and object uses lightmap, skip.
1926 }
1927
1928 half shadow = half(1.0);
1929
1930 if (directional_lights.data[i].shadow_opacity > 0.001) {
1931 float depth_z = -vertex.z;
1932
1933 vec4 pssm_coord;
1934 float blur_factor;
1935 hvec3 light_dir = hvec3(directional_lights.data[i].direction);
1936 hvec3 base_normal_bias = geo_normal * (half(1.0) - max(half(0.0), dot(light_dir, -geo_normal)));
1937
1938 #define BIAS_FUNC(m_var, m_idx) \
1939 hvec3 normal_bias = base_normal_bias * half(directional_lights.data[i].shadow_normal_bias[m_idx]); \
1940 normal_bias -= light_dir * dot(light_dir, normal_bias); \
1941 normal_bias += light_dir * half(directional_lights.data[i].shadow_bias[m_idx]); \
1942 m_var.xyz += vec3(normal_bias);
1943
1944 if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
1945 vec4 v = vec4(vertex, 1.0);
1946
1947 BIAS_FUNC(v, 0)
1948
1949 pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
1950 blur_factor = 1.0;
1951 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
1952 vec4 v = vec4(vertex, 1.0);
1953
1954 BIAS_FUNC(v, 1)
1955
1956 pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
1957 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
1958 blur_factor = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.y;
1959 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
1960 vec4 v = vec4(vertex, 1.0);
1961
1962 BIAS_FUNC(v, 2)
1963
1964 pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
1965 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
1966 blur_factor = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.z;
1967 } else {
1968 vec4 v = vec4(vertex, 1.0);
1969
1970 BIAS_FUNC(v, 3)
1971
1972 pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
1973 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
1974 blur_factor = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.w;
1975 }
1976
1977 pssm_coord /= pssm_coord.w;
1978
1979 bool blend_split = sc_directional_light_blend_split(i);
1980 float blend_split_weight = blend_split ? 1.0f : 0.0f;
1981 shadow = half(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) * blend_split_weight), pssm_coord, scene_data.taa_frame_count));
1982
1983 if (blend_split) {
1984 half pssm_blend;
1985 float blur_factor2;
1986
1987 if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
1988 vec4 v = vec4(vertex, 1.0);
1989 BIAS_FUNC(v, 1)
1990 pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
1991 pssm_blend = half(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));
1992 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
1993 blur_factor2 = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.y;
1994 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
1995 vec4 v = vec4(vertex, 1.0);
1996 BIAS_FUNC(v, 2)
1997 pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
1998 pssm_blend = half(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));
1999 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2000 blur_factor2 = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.z;
2001 } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
2002 vec4 v = vec4(vertex, 1.0);
2003 BIAS_FUNC(v, 3)
2004 pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
2005 pssm_blend = half(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));
2006 // Adjust shadow blur with reference to the first split to reduce discrepancy between shadow splits.
2007 blur_factor2 = directional_lights.data[i].shadow_split_offsets.x / directional_lights.data[i].shadow_split_offsets.w;
2008 } else {
2009 pssm_blend = half(0.0); //if no blend, same coord will be used (divide by z will result in same value, and already cached)
2010 blur_factor2 = 1.0;
2011 }
2012
2013 pssm_coord /= pssm_coord.w;
2014
2015 half shadow2 = half(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) * blend_split_weight), pssm_coord, scene_data.taa_frame_count));
2016 shadow = mix(shadow, shadow2, pssm_blend);
2017 }
2018
2019 #ifdef USE_LIGHTMAP
2020 if (shadowmask_mode == LIGHTMAP_SHADOWMASK_MODE_REPLACE) {
2021 shadow = mix(shadow, shadowmask, half(smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z))); //done with negative values for performance
2022 } else if (shadowmask_mode == LIGHTMAP_SHADOWMASK_MODE_OVERLAY) {
2023 shadow = shadowmask * mix(shadow, half(1.0), half(smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z))); //done with negative values for performance
2024 } else {
2025 #endif
2026 shadow = mix(shadow, half(1.0), half(smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)));
2027 #ifdef USE_LIGHTMAP
2028 }
2029 #endif
2030
2031 #ifdef USE_VERTEX_LIGHTING
2032 diffuse_light *= mix(half(1.0), shadow, half(diffuse_light_interp.a));
2033 direct_specular_light *= mix(half(1.0), shadow, half(specular_light_interp.a));
2034 #endif
2035 #undef BIAS_FUNC
2036 }
2037
2038 shadows[i] = shadow;
2039 }
2040
2041 #ifdef USE_LIGHTMAP
2042 } else { // shadowmask_mode == LIGHTMAP_SHADOWMASK_MODE_ONLY
2043
2044 #ifdef USE_VERTEX_LIGHTING
2045 diffuse_light *= mix(half(1.0), shadowmask, half(diffuse_light_interp.a));
2046 direct_specular_light *= mix(half(1.0), shadowmask, half(specular_light_interp.a));
2047 #endif
2048
2049 shadows[0] = shadowmask;
2050 }
2051 #endif // USE_LIGHTMAP
2052
2053 #endif // SHADOWS_DISABLED
2054
2055 #ifndef USE_VERTEX_LIGHTING
2056 uint directional_lights_count = sc_directional_lights(scene_data.directional_light_count);
2057 for (uint i = 0; i < directional_lights_count; i++) {
2058 if (!bool(directional_lights.data[i].mask & instances.data[draw_call.instance_index].layer_mask)) {
2059 continue; //not masked
2060 }
2061
2062 if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[draw_call.instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
2063 continue; // Statically baked light and object uses lightmap, skip.
2064 }
2065
2066 // We're not doing light transmittence
2067
2068 half shadow = half(1.0);
2069 #ifndef SHADOWS_DISABLED
2070 shadow = mix(half(1.0), shadows[i], half(directional_lights.data[i].shadow_opacity));
2071 #endif
2072 blur_shadow(shadow);
2073
2074 vec3 tint = vec3(1.0);
2075 #ifdef DEBUG_DRAW_PSSM_SPLITS
2076 if (-vertex.z < directional_lights.data[i].shadow_split_offsets.x) {
2077 tint = vec3(1.0, 0.0, 0.0);
2078 } else if (-vertex.z < directional_lights.data[i].shadow_split_offsets.y) {
2079 tint = vec3(0.0, 1.0, 0.0);
2080 } else if (-vertex.z < directional_lights.data[i].shadow_split_offsets.z) {
2081 tint = vec3(0.0, 0.0, 1.0);
2082 } else {
2083 tint = vec3(1.0, 1.0, 0.0);
2084 }
2085 tint = mix(tint, vec3(1.0), float(shadow));
2086 shadow = half(1.0);
2087 #endif
2088
2089 float size_A = sc_use_light_soft_shadows() ? directional_lights.data[i].size : 0.0;
2090
2091 light_compute(normal, hvec3(directional_lights.data[i].direction), view, saturateHalf(size_A),
2092 hvec3(directional_lights.data[i].color * directional_lights.data[i].energy * tint),
2093 true, shadow, f0, roughness, metallic, half(directional_lights.data[i].specular), albedo, alpha,
2094 screen_uv, hvec3(1.0),
2095 #ifdef LIGHT_BACKLIGHT_USED
2096 backlight,
2097 #endif
2098 /* not supported here
2099 #ifdef LIGHT_TRANSMITTANCE_USED
2100 transmittance_color,
2101 transmittance_depth,
2102 transmittance_boost,
2103 transmittance_z,
2104 #endif
2105 */
2106 #ifdef LIGHT_RIM_USED
2107 rim, rim_tint,
2108 #endif
2109 #ifdef LIGHT_CLEARCOAT_USED
2110 clearcoat, clearcoat_roughness, geo_normal,
2111 #endif // LIGHT_CLEARCOAT_USED
2112 #ifdef LIGHT_ANISOTROPY_USED
2113 binormal, tangent, anisotropy,
2114 #endif
2115 diffuse_light,
2116 direct_specular_light);
2117 }
2118 #endif // USE_VERTEX_LIGHTING
2119 } //directional light
2120
2121 #ifndef USE_VERTEX_LIGHTING
2122 uint omni_light_count = sc_omni_lights(8);
2123 uvec2 omni_indices = instances.data[draw_call.instance_index].omni_lights;
2124 for (uint i = 0; i < omni_light_count; i++) {
2125 uint light_index = (i > 3) ? ((omni_indices.y >> ((i - 4) * 8)) & 0xFF) : ((omni_indices.x >> (i * 8)) & 0xFF);
2126 if (i > 0 && light_index == 0xFF) {
2127 break;
2128 }
2129
2130 light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, roughness, metallic, scene_data.taa_frame_count, albedo, alpha, screen_uv, hvec3(1.0),
2131 #ifdef LIGHT_BACKLIGHT_USED
2132 backlight,
2133 #endif
2134 /*
2135 #ifdef LIGHT_TRANSMITTANCE_USED
2136 transmittance_color,
2137 transmittance_depth,
2138 transmittance_boost,
2139 #endif
2140 */
2141 #ifdef LIGHT_RIM_USED
2142 rim,
2143 rim_tint,
2144 #endif
2145 #ifdef LIGHT_CLEARCOAT_USED
2146 clearcoat, clearcoat_roughness, geo_normal,
2147 #endif // LIGHT_CLEARCOAT_USED
2148 #ifdef LIGHT_ANISOTROPY_USED
2149 binormal, tangent, anisotropy,
2150 #endif
2151 diffuse_light, direct_specular_light);
2152 }
2153
2154 uint spot_light_count = sc_spot_lights(8);
2155 uvec2 spot_indices = instances.data[draw_call.instance_index].spot_lights;
2156 for (uint i = 0; i < spot_light_count; i++) {
2157 uint light_index = (i > 3) ? ((spot_indices.y >> ((i - 4) * 8)) & 0xFF) : ((spot_indices.x >> (i * 8)) & 0xFF);
2158 if (i > 0 && light_index == 0xFF) {
2159 break;
2160 }
2161
2162 light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, roughness, metallic, scene_data.taa_frame_count, albedo, alpha, screen_uv, hvec3(1.0),
2163 #ifdef LIGHT_BACKLIGHT_USED
2164 backlight,
2165 #endif
2166 /*
2167 #ifdef LIGHT_TRANSMITTANCE_USED
2168 transmittance_color,
2169 transmittance_depth,
2170 transmittance_boost,
2171 #endif
2172 */
2173 #ifdef LIGHT_RIM_USED
2174 rim,
2175 rim_tint,
2176 #endif
2177 #ifdef LIGHT_CLEARCOAT_USED
2178 clearcoat, clearcoat_roughness, geo_normal,
2179 #endif // LIGHT_CLEARCOAT_USED
2180 #ifdef LIGHT_ANISOTROPY_USED
2181 binormal, tangent, anisotropy,
2182 #endif
2183 diffuse_light, direct_specular_light);
2184 }
2185 #endif // !VERTEX_LIGHTING
2186
2187 #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
2188
2189 #ifdef USE_SHADOW_TO_OPACITY
2190 #ifndef MODE_RENDER_DEPTH
2191 alpha = min(alpha, clamp(length(ambient_light), half(0.0), half(1.0)));
2192
2193 #if defined(ALPHA_SCISSOR_USED)
2194 #ifdef MODE_RENDER_MATERIAL
2195 if (alpha < alpha_scissor_threshold) {
2196 alpha = half(0.0);
2197 } else {
2198 alpha = half(1.0);
2199 }
2200 #else
2201 if (alpha < alpha_scissor_threshold) {
2202 discard;
2203 }
2204 #endif // MODE_RENDER_MATERIAL
2205 #endif // !ALPHA_SCISSOR_USED
2206
2207 #endif // !MODE_RENDER_DEPTH
2208 #endif // USE_SHADOW_TO_OPACITY
2209
2210 #ifdef MODE_RENDER_DEPTH
2211
2212 #ifdef MODE_RENDER_MATERIAL
2213
2214 albedo_output_buffer.rgb = albedo;
2215 albedo_output_buffer.a = alpha;
2216
2217 normal_output_buffer.rgb = normal * 0.5 + 0.5;
2218 normal_output_buffer.a = 0.0;
2219 depth_output_buffer.r = -vertex.z;
2220
2221 orm_output_buffer.r = ao;
2222 orm_output_buffer.g = roughness;
2223 orm_output_buffer.b = metallic;
2224 orm_output_buffer.a = sss_strength;
2225
2226 emission_output_buffer.rgb = emission;
2227 emission_output_buffer.a = 0.0;
2228 #endif // MODE_RENDER_MATERIAL
2229
2230 #else // MODE_RENDER_DEPTH
2231
2232 // multiply by albedo
2233 diffuse_light *= albedo; // ambient must be multiplied by albedo at the end
2234
2235 // apply direct light AO
2236 diffuse_light *= ao;
2237 direct_specular_light *= ao;
2238
2239 // apply metallic
2240 diffuse_light *= half(1.0) - metallic;
2241 ambient_light *= half(1.0) - metallic;
2242
2243 #ifdef MODE_MULTIPLE_RENDER_TARGETS
2244
2245 #ifdef MODE_UNSHADED
2246 diffuse_buffer = vec4(albedo.rgb, 0.0);
2247 specular_buffer = vec4(0.0);
2248
2249 #else // MODE_UNSHADED
2250
2251 #ifdef SSS_MODE_SKIN
2252 sss_strength = -sss_strength;
2253 #endif // SSS_MODE_SKIN
2254 diffuse_buffer = vec4(emission + diffuse_light + ambient_light, sss_strength);
2255 specular_buffer = vec4(direct_specular_light + indirect_specular_light, metallic);
2256 #endif // MODE_UNSHADED
2257
2258 #ifndef FOG_DISABLED
2259 diffuse_buffer.rgb = mix(diffuse_buffer.rgb, fog.rgb, fog.a);
2260 specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), fog.a);
2261 #endif // !FOG_DISABLED
2262
2263 #else //MODE_MULTIPLE_RENDER_TARGETS
2264
2265 #ifdef MODE_UNSHADED
2266 hvec4 out_color = hvec4(albedo, alpha);
2267 #else // MODE_UNSHADED
2268 hvec4 out_color = hvec4(emission + ambient_light + diffuse_light + direct_specular_light + indirect_specular_light, alpha);
2269 #endif // MODE_UNSHADED
2270
2271 #ifndef FOG_DISABLED
2272 // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
2273 out_color.rgb = mix(out_color.rgb, fog.rgb, fog.a);
2274 #endif // !FOG_DISABLED
2275
2276 // On mobile we use a UNORM buffer with 10bpp which results in a range from 0.0 - 1.0 resulting in HDR breaking
2277 // We divide by sc_luminance_multiplier to support a range from 0.0 - 2.0 both increasing precision on bright and darker images
2278 out_color.rgb = out_color.rgb / sc_luminance_multiplier();
2279 #ifdef PREMUL_ALPHA_USED
2280 out_color.rgb *= premul_alpha;
2281 #endif
2282
2283 frag_color = out_color;
2284
2285 if (sc_use_material_debanding()) {
2286 // From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf
2287 // and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom)
2288 // NOTE: `gl_FragCoord` is in pixels (i.e. not normalized UV).
2289 // This dithering must be applied after encoding changes (linear/nonlinear) have been applied
2290 // as the final step before quantization from floating point to integer values.
2291
2292 // Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR.
2293 // Removed the time component to avoid passing time into this shader.
2294 // This dither offset was chosen because it meshes nicely with the no-offset dither that
2295 // is used for Viewport debanding.
2296 const vec2 dither_offset = vec2(0.535, 8.715);
2297 vec3 dither = vec3(dot(vec2(171.0, 231.0), gl_FragCoord.xy + dither_offset));
2298 dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0));
2299
2300 // Subtract 0.5 to avoid slightly brightening the whole viewport.
2301 // Use a dither strength of 100% rather than the 37.5% suggested by the original source.
2302 // Assume that this shader always writes to a 10-bit buffer, so divide by 1023 to align
2303 // to 10-bit quantization.
2304 frag_color.rgb += (dither.rgb - 0.5) / 1023.0;
2305 }
2306
2307 #endif //MODE_MULTIPLE_RENDER_TARGETS
2308
2309 #endif //MODE_RENDER_DEPTH
2310
2311 #ifdef MODE_RENDER_MOTION_VECTORS
2312 // These motion vectors are in NDC space (as opposed to screen space) to fit the OpenXR XR_FB_space_warp specification.
2313 // https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#XR_FB_space_warp
2314
2315 vec3 ndc = screen_position.xyz / screen_position.w;
2316 ndc.y = -ndc.y;
2317 vec3 prev_ndc = prev_screen_position.xyz / prev_screen_position.w;
2318 prev_ndc.y = -prev_ndc.y;
2319 frag_color = vec4(ndc - prev_ndc, 0.0);
2320 #endif
2321 }