Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What and why about GLSL textureGrad

So I read the OpenGL info page on textureGrad but it doesn't really explain anything. So you can explicitly specify the partial derivatives of P relative to X and Y

  • What does this actually do?
  • How does OpenGL calculate the derivatives when you sample without specific partial derivatives?
  • Why does texture sampling use or even need derivatives?

My mind has been blown.

like image 761
Nathan Wride Avatar asked Jan 09 '14 01:01

Nathan Wride


1 Answers

When you invoke a texture sample operation on the GPU, the GPU does the following:

  1. The texture request is broadcasted to neighboring pixels in a 2x2 quad fragment
  2. The partial derivatives of the texture coordinates with respect to x and y are computed
  3. These partials are used to compute the MIP level (accounting for level of detail and anisotropic filtering)
  4. Finally, the relevant texels are fetched (possibly from a texture cache) and interpolated to produce the final sampled result

Now, suppose you have different neighboring fragments that branch (say due to some material or something) such that on invocation of that shader, sample requests to different textures are made at the same time. You run into a problem if the tiling rates for these textures are different which will break the MIP selection (the incoherence of this undefined operation will result in a noisy render). The solution is to query for the gradients yourself using dFdx and dFdy outside of the branch and use the textureGrad function within the branch, applying the correct tiling multipliers to your gradients.

like image 57
jeremyong Avatar answered Sep 20 '22 11:09

jeremyong