# Optimize Lighting?

Hey, if you don’t mind, could you help me a bit? i have FS/VS shader pair that does normal mapping with specular for multiple lights…but the performance makes me really unhappy.

With the 220k triangles model fully normal mapped, 3 point lights(infinite radius) its 230+ FPS on GTX 295 and 5+ FPS on GF6150SE. With 14.5k tris model its 512FPS(limit i can’t override) on GTX 295 and 11+ on 6150. Is it bad?

VS

``````attribute vec3 Tangent;
varying vec3 ld[4];
varying vec3 eye;

void main()
{
vec4 V = gl_ModelViewMatrix * gl_Vertex;

gl_TexCoord[0] = gl_MultiTexCoord0;

vec4 T = -normalize(vec4(gl_NormalMatrix * Tangent, 1.0));
vec4 N = normalize(vec4(gl_NormalMatrix * gl_Normal, 1.0));
vec4 B = vec4(cross(N.xyz, T.xyz), 1.0);

mat3 TBN = mat3(vec3(B.x, T.x, N.x), vec3(B.y, T.y, N.y), vec3(B.z, T.z, N.z));

for(int f = 0; f < 4; f++)
{
if(gl_LightSource[f].position.w == 0.0)
continue;

ld[f] = TBN * (gl_LightSource[f].position - V).xyz;
}

eye = TBN * (-V).xyz;

gl_Position = gl_ProjectionMatrix * V;
}

``````

FS

``````uniform sampler2D colorMap;
uniform sampler2D bumpMap;
uniform vec3 Ambient;
uniform vec4 Material;
varying vec3 ld[4];
varying vec3 eye;

void Light(inout vec4 Specular, inout vec4 Diff, in int ID)
{
if(gl_LightSource[ID].position.w == 0.0)//Enabled?
return;

vec4 bump = texture2D(bumpMap, gl_TexCoord[0].st) * 2.0 - 1.0;
float Lambert;
vec4 lDir = normalize(vec4(ld[ID], 1.0));

Lambert = dot(lDir, bump);

if(Lambert <= 0.0)//Does Affect?
return;

vec4 lightDif = gl_LightSource[ID].diffuse * 5.0;

float Nhalf = 0.0;

Nhalf = pow(dot(reflect(-lDir, bump), normalize(vec4(eye, 1.0))), 64.0 * Material.x);

if(Nhalf > 0.0)
Specular += Nhalf * lightDif * 0.1;

Diff += lightDif * Lambert * lightDif.w;
}

void main (void)
{
vec4 Spec = Add + vec4(Ambient, 1.0);
vec4 Diffuse = vec4(Ambient, 1.0);

for(int lid = 0; lid < 4; lid++)
Light(Spec, Diffuse, lid);

gl_FragColor = texture2D(colorMap, gl_TexCoord[0].st) * Diffuse + Spec;
}
``````

The Nvidia 6150 is an OpenGL 2 GPU with only 1 vertex shader and 2 pixel shaders. It’ll be very sensitive to branching, and your shaders have quite a few conditionals. 5-11fps seems fairly reasonable for a relatively complex shader on such an underpowered hardware part.

Thank’s for your reply! It was helpful, i’ve removed all ‘if’ statements from FS and added some clamp’s. Also moved eye\lightDir normalization to vertex shader(and changed varying to vec4) and raised FPS about 15-20%.

But now i wonder, will it work same on AMD cards…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.