Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
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
show 4 more comments

closed as too localized by Byte56, Josh Petrie, Sean Middleditch, Trevor Powell, bummzack Feb 13 '13 at 8:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

If you can do StarCraft at 30-40 fps then GLSL is most likely not your bottleneck. Reading your description, I get the feeling that you might be calling glTexImage2D for every frame, which could explain your low frame rate. I'm very confident your problem lies in your client rather then the shader.

share|improve this answer
I call it once per texture when the application starts. That is all. That is why I am so confused. –  Justin Feb 9 '13 at 21:23
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.