4

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().

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Jinstarr
  • 43
  • 1
  • 3
  • It comes from "magic" similar to that of `ddx` and `ddy`, applied to the depth component – harold Dec 20 '11 at 17:54
  • Harold... unlike depth it can't be based on any inputs from the vertex stage. I can calculate an arbitrary texture (u,v) coordinate in the pixel shader and mipmapping still works. – Jinstarr Dec 20 '11 at 19:29
  • The u,v coordinate does not affect the mip level though – harold Dec 20 '11 at 19:37
  • Texture gradient cannot be deduced from depth gradient if the texture coordinates are calculated in the pixel shader. – Jinstarr Dec 20 '11 at 20:30
  • Why do you think that? It's precisely what happens. Unless of course you're using the tex lookups with explicit gradient. – harold Dec 20 '11 at 20:57
  • I think that because the coordinates I generate in the shader could have any gradient at all. They might even be exponential across the polygon (u = v^2). – Jinstarr Dec 22 '11 at 17:55
  • But the gradient is all about the angle between the polygon and the camera (not the difference in the u/v coords between pixels), the texture coordinates do not affect that at all.. – harold Dec 22 '11 at 23:21

1 Answers1

1

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.

mattnewport
  • 13,728
  • 2
  • 35
  • 39
  • I found a paper which partly explains the process: http://developer.amd.com/media/gpu_assets/04%20clever%20shader%20tricks.pdf – Jinstarr Jan 01 '12 at 15:20