Does texel space offset result in dependent read?

Ok, so here is the problem. I have PCF, and I am doing something like:

[unroll] for (int y = ...; ...; ++y)

[unroll] for (int x = ...; ...; ++x)

tex.Sample(sampler, input.texcoord, int2(x, y));

Everything is fine, unless I want to optimize it and for example do PCF only for shadowed points. I put loops into an if() branch, and the compiler puts texture sampling instructions outside the branch, leaving only arithmetic ones inside. Obviously not what I wanted.

Ok, I had the same issue with D3D9, where texel-space offset needs to be simulated via arithmetic instructions, which causes a dependent read - and this in turn causes a problem with mip level estimation - because if a branch is executed in one pixel in a block (2x2?) of pixels, and is not executed in the other, it is not exactly clear how to compute derivatives - so sampling instructions are moved outside the loop.

This is perfectly sane and clear. And it is solved with using tex2Dlod (or tex2Dgrad) instead of tex2D.

And in my case with D3D10 it is again solved by using SampleLevel instead of Sample. Though I can't find the sane explanation. I pass the texture coordinate which is not dependant (i.e. computed in shader), and I expect the sampling instruction to compute derivative, select mip level and THEN offset by several texels.

Or perhaps texel space offset is specified in the space of the first mip level, not in the space of the used one? But no, DX SDK explicitly states "The offset parameter is added to the texture coordinates, in texel space, relative to each miplevel being accessed."

So, I am wondering - what's the case? Is it the HLSL compiler issue (I can't verify that, because there is no - at least, official - way to convert asm shader into binary one), D3D10 design issue, documentation issue or do I simply miss something?

[1893 byte] By [KapoulkineArseny] at [2008-2-11]
# 1

The texel space offset shouldn't cause a dependent read by itself, the only restriction we have on the offset is that it has to be literal (which you've taken care of by unrolling the loops). If the condition of your if is not constant across the 2x2 block, then we have to pull it out, for the same reasons as in D3D9. The difference from 9 is that we have a bit more complicated logic that allows us to decide if it is constant across the 2x2 block, instead of treating all branches this way.

In this case it looks like you're using the input texcoord, which *should* be valid, but I know we had to go back and forth a few times on derivative issues, so I can't say for sure without going back and looking at the code.

That said, we did fix a few bugs with offset instructions since the last time we released the 10 compiler, and it may be that you're hitting them, but I seem to recall them being limited to the multisample load intrinsics.

JohnRapp at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Direct3D 10...
# 2
Thank you for the answer.

I will try the compiler from the October SDK and post the results here (if the problem remains, I'll post the shader code that is enough to reproduce the behavior).

KapoulkineArseny at 2007-9-3 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Direct3D 10...