Sign in to follow this  

GLSL: why is this simple frag so slow?

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a .frag shader which is very slow. I simplified it to a minimal example which exhibits the problem: void main() { int i=0; vec4 dst; vec4 src[1]; src[0] = vec4(0., 0., 0., 0.); //dst = src[0]; // fast, ~30 updates per second dst = src[i]; // very slow, ~1 update per second! // Verify it executed this far (it does) gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } If I access the array "src" using a literal- say 0- it's fast. If I access it with an integer variable- say i with a value of 0- it's very slow, updating the display about once per second. How can this be? I have a Dell Inspiron 6000 with ATI Mobility Radeon, driver is 8.162.0.0 dated 8/3/2005, and no newer drivers are available as far as I can tell. Thanks for any insight, Mark

Share this post


Link to post
Share on other sites
Many Shader Model 3.0 GPUs can't use indexed fragment shader variables in hardware. In your special case it would be possible to replace "i" by a static value, but the compiler isn't smart enough....

Share this post


Link to post
Share on other sites
> Many Shader Model 3.0 GPUs can't use indexed fragment shader variables in hardware.

Thanks for the insight, ock21. Do you know whether I can query to see whether indexed fragment shader variables are supported in hardware? (I suspect that even if there was a way to query, it would simply respond "yes, it is supported", even if the support was slow.)

BTW, I'm not sure I made it clear, I do of course want to use index values other than zero. I was hoping to use a uniform vector of variable length and set the values from my app, like the convolution example in the Orange Book.

Mark

Share this post


Link to post
Share on other sites
The GLSL compiler should unroll the static controll flow and replace the variables with static values. That means that you can loop over a number of lights if the number is known at compile time, but not if the number of lights is set with a uniform variable.

This won't work with shader Model 3.0 (but in sh4.0 it works):

uniform int num_of_lights;
void main(void){
for (int i = 0; i < num_of_lights; i++){
gl_FragColor += gl_LightSource[i].diffuse; //* lighting terms
}
}


In shader model 3.0, the only possible solution is to use a fixes number:

void main(void){
for (int i = 0; i < 4; i++){
gl_FragColor += gl_LightSource[i].diffuse; //* lighting terms
}
}

After unrolling the code would be equal to:

void main(void){
gl_FragColor += gl_LightSource[0].diffuse;
gl_FragColor += gl_LightSource[1].diffuse;
gl_FragColor += gl_LightSource[2].diffuse;
gl_FragColor += gl_LightSource[3].diffuse;
}

That is only a compiler issue, but if you are using outdated drivers. It could be possible that the compiler didn't detect the possibility of unrolling that that part of code.
You should avoid indexed temporary or constant variables.

A workaround for variable length loops is to compile a shader for each required loop length. "#define" should help there...

Share this post


Link to post
Share on other sites
Thanks again, oc2k1. I'll unroll my loop and compile a separate shader for each required loop length. I will also try this on another machine and post the results in a few days- maybe the problem is very specific to my machine.

Share this post


Link to post
Share on other sites
Also, dig around on google about modifying the catalyst drivers to install on your laptop. I did a while ago and can't find the link and got a huge performance gain. Deell doesn't have the resources to update the drivers (or they would have), but have an agreement that the OEM drivers from ATI won't install. Pretty sad really.

edit: here's the link to upgrade your driver to something newer:
http://www.driverheaven.net/modtool/

Cheers,

Bob

Share this post


Link to post
Share on other sites

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this