When I sample a texture in a pixel shader the texture unit needs to select a mipmap based on the texture gradient around the pixel being shaded.
There's a function tex2Dgrad()
which allows me to supply info about the gradient manually and tex2Dlod()
which allows me to select a mipmap manually but if I just call tex2D()
then where does the extra gradient information come from?
tex2D()
is the most common case for texture mapping, used in most shaders, but I have no idea where the gradient comes from. Mipmapping obviously works so it must come from somewhere.
I want to use a texture as a lookup table in a pixel shader using calculated U
and V
coordinates but I don't want any unexpected 'magic' happening in tex2D()
.
Do I need to use tex2Dlod()
to avoid this? I read that tex2Dlod()
is slower then tex2D()
.
GPUs shade a 'quad' of 4 pixels at a time and the gradients used for texture fetches come from the finite differences calculated from adjacent pairs of pixels. This is how the GPU is able to generate partial derivatives even for arbitrary expressions in the pixel shader. The tex2Dgrad function can be useful if you can calculate more accurate analytical derivatives for the values you are passing in as texture coordinates.