Sign in to follow this  

Is there a limit to the count of iterations in HLSL for loop?

Recommended Posts

Hi All,

Assume i'm drawing a simple cube while applying the following pixel shader to it (ps_3_0):


float4 PS_Program(VertexShaderOutput input) : COLOR0

float4 clr = float4(0, 0, 0, 0);
float4 step = float4 (1.0f, 1.0f, 1.0f, 1.0f) / Iterations;

for (int i = 0; i < Iterations; ++i)
clr += step;

return clr;


and I'm controlling the count of Iterations from the application (change it in the range [1..1000])

As you may expect this should result in a white cube no matter what the Iterations value is (since it will sum to 1).

However, when the value of the Iterations goes greater than around 256-400 the white cube is no longer white but gray.


and it will decay more when I increase the count of Iterations.
which makes me believe that there is an upper limit to the count of iterations that can performed otherwise why this code doesn't always add up to white = float4(1, 1, 1, 1)?

Share this post

Link to post
Share on other sites
If actual looping instructions are used in conjunction with an integer register, the limit on the number of iterations is 255. The docs stating that are here: [url=""][/url]. If you want to know what your shader is actually doing, then you can look at the disassembly either by using fxc.exe to compile the shader or by using PIX to inspect the pixel shader object in a captured frame.

Share this post

Link to post
Share on other sites
Hi! Watch out that if your loop gets unrolled, there's also an instruction slot count limit. It's GPU-dependant, but you have a minimum guaranteed of 512 instruction slots in Shader Model 3.0

You have to watch out of these because usually the Ati X1000 series have the bare minimum, and if you use more, your shaders will unexpectedly fail to load (with little information about the cause).
This is the kind of problem that makes a shader run in one GPU, and fail in another. Even though they are both SM 3.0 capable.
They're strange though, since you rarely hit the mark (specially since any DX10 capable HW can do a lot more); but it's disorienting when it happens; usually triggered by loops being unrolled.

Dark Sylinc

Share this post

Link to post
Share on other sites

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