views:

267

answers:

1

I have a piece of HLSL code which looks like this:

float4 GetIndirection(float2 TexCoord)
{
    float4 indirection = tex2D(IndirectionSampler, TexCoord);

    for (half mip = indirection.b * 255; mip > 1 && indirection.a < 128; mip--)
    {
        indirection = tex2Dlod(IndirectionSampler, float4(TexCoord, 0, mip));
    }
    return indirection;
}

The results I am getting are consistent with that loop only executing once. I checked the shader in PIX and things got even more weird, the yellow arrow indicating position in the code gets to the loop, goes through it once, and jumps back to the start, at that point the yellow arrow never moves again but the cursor moves through the code and returns a result (a bug in PIX, or am I just using it wrong?)

I have a suspicion this may be a problem to do with texture reads getting moved outside the loop by the compiler, however I thought that didn't happen with tex2Dlod since I'm setting the LOD manually :/

So:

1) What's the problem?

2) Any suggested solutions?

A: 

Problem was solved, it was a simple coding mistake, I needed to increase mip level on each iteration, not decrease it.

float4 GetIndirection(float2 TexCoord)
{
    float4 indirection = tex2D(IndirectionSampler, TexCoord);

    for (half mip = indirection.b * 255; mip > 1 && indirection.a < 128; mip++)
    {
     indirection = tex2Dlod(IndirectionSampler, float4(TexCoord, 0, mip));
    }
    return indirection;
}
Martin