Debian Codegraph @

servers/rendering/renderer_rd/shaders/scene_forward_vertex_lights_inc.glsl

raw
1 // Simplified versions of light functions intended for the vertex shader.
2
3 // Eyeballed approximation of `exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25`.
4 // Uses slightly more FMA instructions (2x rate) to avoid special instructions (0.25x rate).
5 // Range is reduced to [0.64,4977] from [068,2,221,528] which makes mediump feasible for the rest of the shader.
6 half roughness_to_shininess(half roughness) {
7 half r = half(1.2) - roughness;
8 half r2 = r * r;
9 return r * r2 * r2 * half(2000.0);
10 }
11
12 void light_compute_vertex(hvec3 N, hvec3 L, hvec3 V, hvec3 light_color, bool is_directional, half roughness,
13 inout hvec3 diffuse_light, inout hvec3 specular_light) {
14 half NdotL = min(dot(N, L), half(1.0));
15 half cNdotL = max(NdotL, half(0.0)); // clamped NdotL
16
17 #if defined(DIFFUSE_LAMBERT_WRAP)
18 // Energy conserving lambert wrap shader.
19 // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/
20 half diffuse_brdf_NL = max(half(0.0), (cNdotL + roughness) / ((half(1.0) + roughness) * (half(1.0) + roughness))) * half(1.0 / M_PI);
21 #else
22 // lambert
23 half diffuse_brdf_NL = cNdotL * half(1.0 / M_PI);
24 #endif
25
26 diffuse_light += light_color * diffuse_brdf_NL;
27
28 #if !defined(SPECULAR_DISABLED)
29 half specular_brdf_NL = half(0.0);
30 // Normalized blinn always unless disabled.
31 hvec3 H = normalize(V + L);
32 half cNdotH = clamp(dot(N, H), half(0.0), half(1.0));
33 half shininess = roughness_to_shininess(roughness);
34 half blinn = pow(cNdotH, shininess);
35 blinn *= (shininess + half(2.0)) * half(1.0 / (8.0 * M_PI)) * cNdotL;
36 specular_brdf_NL = blinn;
37 specular_light += specular_brdf_NL * light_color;
38 #endif
39 }
40
41 half get_omni_attenuation(float distance, float inv_range, float decay) {
42 float nd = distance * inv_range;
43 nd *= nd;
44 nd *= nd; // nd^4
45 nd = max(1.0 - nd, 0.0);
46 nd *= nd; // nd^2
47 return half(nd * pow(max(distance, 0.0001), -decay));
48 }
49
50 void light_process_omni_vertex(uint idx, vec3 vertex, hvec3 eye_vec, hvec3 normal, half roughness,
51 inout hvec3 diffuse_light, inout hvec3 specular_light) {
52 vec3 light_rel_vec = omni_lights.data[idx].position - vertex;
53 float light_length = length(light_rel_vec);
54 hvec3 light_rel_vec_norm = hvec3(light_rel_vec / light_length);
55 half omni_attenuation = get_omni_attenuation(light_length, omni_lights.data[idx].inv_radius, omni_lights.data[idx].attenuation);
56 hvec3 color = hvec3(omni_lights.data[idx].color * omni_attenuation);
57
58 light_compute_vertex(normal, light_rel_vec_norm, eye_vec, color, false, roughness,
59 diffuse_light,
60 specular_light);
61 }
62
63 void light_process_spot_vertex(uint idx, vec3 vertex, hvec3 eye_vec, hvec3 normal, half roughness,
64 inout hvec3 diffuse_light,
65 inout hvec3 specular_light) {
66 vec3 light_rel_vec = spot_lights.data[idx].position - vertex;
67 float light_length = length(light_rel_vec);
68 hvec3 light_rel_vec_norm = hvec3(light_rel_vec / light_length);
69 half spot_attenuation = get_omni_attenuation(light_length, spot_lights.data[idx].inv_radius, spot_lights.data[idx].attenuation);
70 hvec3 spot_dir = hvec3(spot_lights.data[idx].direction);
71
72 half cone_angle = half(spot_lights.data[idx].cone_angle);
73 half scos = max(dot(-light_rel_vec_norm, spot_dir), cone_angle);
74
75 // This conversion to a highp float is crucial to prevent light leaking due to precision errors.
76 float spot_rim = max(1e-4, float(half(1.0) - scos) / float(half(1.0) - cone_angle));
77 spot_attenuation *= half(1.0 - pow(spot_rim, spot_lights.data[idx].cone_attenuation));
78
79 hvec3 color = hvec3(spot_lights.data[idx].color * spot_attenuation);
80
81 light_compute_vertex(normal, light_rel_vec_norm, eye_vec, color, false, roughness,
82 diffuse_light, specular_light);
83 }