Sign in to follow this  

shader inner loop

This topic is 3629 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 All, I have a pixel shader that contains a inner loop: for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { a = a * b[i] * c[j]; } } I find that the shader is very very slow. If I can the code to this, for (int j = 0; j < 2; j++) { a = a * b[0] * c[j]; } for (int j = 0; j < 2; j++) { a = a * b[1] * c[j]; } The shader is fast again. Can anybody tell me why? Thanks, Simon

Share this post


Link to post
Share on other sites
What PS version do you use? In PS3.0 there is only one Loop Counter Register so it can be the reason (the code can be better optimized when there is no inner loop).
That what you did is called loop unrolling. It is optimization that is wide used by compiler etc.

Share this post


Link to post
Share on other sites
How much faster is it when you write the code like this:


a = a * b[0] * c[0];
a = a * b[0] * c[1];

a = a * b[1] * c[0];
a = a * b[1] * c[1];



It's best to avoid loops in shaders because they require branching, which is very slow on GPUs. I'm surprised the compiler is not unrolling the loop when the number of iterations is constant. Can you post the shader disassembly?

Share this post


Link to post
Share on other sites

This topic is 3629 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