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

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

## 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 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: http://msdn.microsoft.com/en-us/library/windows/desktop/bb174715%28v=vs.85%29.aspx. 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 on other sites

Do you think nesting loops would be a good choice or you have a better workaround?

##### Share on other sites
Yeah nesting the loops should work, or just doing multiple loops in the shader.

##### 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.

Cheers
Dark Sylinc

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
12

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013738
×