Nathan Wride
Nathan Wride

Reputation: 965

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

My mind has been blown.

Upvotes: 9

Views: 2967

Answers (1)

jeremyong
jeremyong

Reputation: 484

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.

Upvotes: 2

Related Questions