# GLSL code is very slow…why? [closed]

I know this is a very difficult thing to simply modify without the full project code, but I am having a massive performance problem with GLSL code that seems to be very efficient to me. I am stuck at 14 fps if I use mipmaps and only 23 if I am simply using the normal glTexImage2D() method, yet have it setup to render 30 times per second. The problem is in the fragment shader - the vertex shader is ridiculously fast, as it is just passing the parameters in to the fragment shader.

varying vec4 color;
varying vec3 normal;
varying vec4 position;

uniform sampler2D t_diffuse;
uniform sampler2D t_bump;
uniform sampler2D t_specular;
uniform mat4 light_color;
uniform vec4 light_position;

void main() {
vec4 diff = texture2D(t_diffuse, gl_TexCoord[0].st);
vec3 bump = normalize(texture2D(t_bump, gl_TexCoord[0].st).xyz * 2.0 - 1.0);
vec4 spec = texture2D(t_specular, gl_TexCoord[0].st);

vec4 ambient = vec4(light_color[0][0], light_color[0][1], light_color[0][2], light_color[0][3]);
vec4 diffuse = vec4(light_color[1][0], light_color[1][1], light_color[1][2], light_color[1][3]) * diff;
vec4 specular = vec4(light_color[2][0], light_color[2][1], light_color[2][2], light_color[2][3]) * spec;

vec3 vector = light_position.xyz - position.xyz;
float distance = sqrt((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));

vec4 final = ambient + diffuse + specular;
final.xyz *= max(0.0, dot(normalize(vector), bump)) / distance;

gl_FragColor = final;
}


The light_color variable holds one color in each row, so light_color[0] is red, light_color[1] is green, et cetera.

The textures aren't even vary large - and even then, I can do Starcraft 2 at max settings (30-40 fps), so it isn't like they are much of an issue. What is in here that can possibly be so slow?

• How do you know that the problem is the fragment shader? – Patrick Hughes Feb 9 '13 at 21:35
• Because I copied all of the other relevant code from one of my other apps, which functions very well. – Justin Feb 9 '13 at 21:38
• Two tests: 1) reduce your shader to a minimum and see if it helps, 2) are draw counts similar between your functioning code or does this new app draw way more? – Patrick Hughes Feb 9 '13 at 21:50
• You may also want to try and use gDeBugger . You should also try to use a general profiler. Your fragment shader doesn't seem to be doing anything too intensive. – Grieverheart Feb 9 '13 at 22:03
• "The light_color variable holds one color in each row" No, it holds one color in each column. Columns come first, then rows. – Nicol Bolas Feb 10 '13 at 0:41