[DX10] HLSL: gradient based operation error
When you do a regular texture sampling operation (Texture2D.Sample), gradients are calculated using the partial derivatives of the texture coordinates in screen space. These derivatives determine which mipmap level to use. When you're using dynamic branching, your shader can't automatically calculate derivatives for texture coordinates inside a dynamic branch. This is because adjacent pixels in a quad might not take the same branch.
You have two easy solutions:
-Explicitly calculate the derivatives outside the branch, and pass them in to Texture2D.SampleGrad.
-Just use Texture2D.SampleLevel and explicitly specify the mipmap level. Since you're sampling G-Buffer textures, your probably don't have mipmaps which means this would be your best option.
Every pages I read about this warning, people talks about Sampling. In my case, the warning appear at this line (taken from GPU Gems 3, AndyTx code):
float2 GetFilterSize(float2 dx, float2 dy, float2 TexSize){ return 2 * (abs(dx) + abs(dy)) * TexSize;}
dx and dy are the already calculated derivative values of the uvs using ddx() and ddy(). Texture reading is done using .Load (no sample) after. Both ddx() and .Load() can be in a "if" branch without warning, but not the simple math function above. Why?