From 47b5940a89b3044f25de80c99c13a9d760ee5c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 9 Jun 2021 13:13:30 +0200 Subject: [PATCH] Shaders: microoptimize redundant square root. Just to have all options tried out, no significant impact this time again. --- src/Magnum/Shaders/Phong.frag | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Magnum/Shaders/Phong.frag b/src/Magnum/Shaders/Phong.frag index fc5d30e71..478fa2996 100644 --- a/src/Magnum/Shaders/Phong.frag +++ b/src/Magnum/Shaders/Phong.frag @@ -462,14 +462,15 @@ void main() { /* Attenuation. Directional lights have the .w component set to 0, use that to make the distance zero -- which will then ensure the attenuation is always 1.0 */ - highp float dist = length(lightDirection.xyz)*lightDirection.w; + highp const float len = length(lightDirection.xyz); + highp const float dist = len*lightDirection.w; /* If range is 0 for whatever reason, clamp it to a small value to avoid a NaN when dist is 0 as well (which is the case for directional lights). */ highp float attenuation = clamp(1.0 - pow(dist/max(lightRange, 0.0001), 4.0), 0.0, 1.0); attenuation = attenuation*attenuation/(1.0 + dist*dist); - highp vec3 normalizedLightDirection = normalize(lightDirection.xyz); + highp vec3 normalizedLightDirection = lightDirection.xyz/len; lowp float intensity = max(0.0, dot(normalizedTransformedNormal, normalizedLightDirection))*attenuation; fragmentColor.rgb += finalDiffuseColor.rgb*lightColor*intensity;