Monday, March 18, 2013

Instancing and a bit of shader play

I sat down and started to refactor my code for serious. FPS rate was still silly low, but the code looked ok. I've implement instancing and saw that it accelerated draw function by 4-5 times, also frustum culling lowers the drawing time at average 4 times. It was really ok, but still...

Consider following shader code:

What will happen in 28 and 29th line?

I'm setting a value for gRenderedPointLights to 7, so it will be logical if this loop unrolls and just copy its code 7 times with different i values.


HLSL attributes are very sensitive. I don't know why, but in that case the loop unrolled circa 50 times, removing the [unroll] attribute solves the problem. Now rendering time jumped like +100FPS.



  1. I don't even understand why it compiled before. How can loop be unrolled when number of iterations is unknown at compile time...?

    I would suppose compiler just guessed the number of iterations to unroll and still left some code to control the flow depending on actual iteration number value.

  2. You're right. I'm also suprised that it compiled. The more I'm using HLSL shaders the more I see that they don't need variable initialization and can do really weird things about it.