From 0b7c7f824928c508578e0c5222cb8c37d143b23e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 9 Jun 2021 13:10:30 +0200 Subject: [PATCH] Shaders: hoist camera calculation calculation out of the loop. While (of course) having zero effect on a single-light scenario, with five lights it saves about 10% in the classic uniform case (on Intel). Not bad (also, FFS, what the compiler is doing if it's not able to optimize this?!). --- src/Magnum/Shaders/Phong.frag | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Magnum/Shaders/Phong.frag b/src/Magnum/Shaders/Phong.frag index 79792e3f9..fc5d30e71 100644 --- a/src/Magnum/Shaders/Phong.frag +++ b/src/Magnum/Shaders/Phong.frag @@ -419,6 +419,8 @@ void main() { normalizedTransformedNormal = tbn*(normalize((texture(normalTexture, interpolatedTextureCoordinates).rgb*2.0 - vec3(1.0))*vec3(normalTextureScale, normalTextureScale, 1.0))); #endif + highp const vec3 cameraDirection = normalize(-transformedPosition); + /* Add diffuse color for each light */ #ifndef UNIFORM_BUFFERS for(int i = 0; i < LIGHT_COUNT; ++i) @@ -475,7 +477,7 @@ void main() { if(intensity > 0.001) { highp vec3 reflection = reflect(-normalizedLightDirection, normalizedTransformedNormal); /* Use attenuation for the specularity as well */ - mediump float specularity = clamp(pow(max(0.0, dot(normalize(-transformedPosition), reflection)), shininess), 0.0, 1.0)*attenuation; + mediump float specularity = clamp(pow(max(0.0, dot(cameraDirection, reflection)), shininess), 0.0, 1.0)*attenuation; fragmentColor += vec4(finalSpecularColor.rgb*lightSpecularColor.rgb*specularity, finalSpecularColor.a); } }