Jump to content
  • Advertisement
Sign in to follow this  
rm1768

shader inner loop

This topic is 3909 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 * 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!