What is, in simple terms, textureGrad()?
Asked Answered
S

1

19

I read the Khronos wiki on this, but I don't really understand what it is saying. What exactly does textureGrad do?

I think it samples multiple mipmap levels and computes some color mixing using the explicit derivative vectors given to it, but I am not sure.

Sallet answered 24/10, 2018 at 18:37 Comment(0)
H
40

When you sample a texture, you need the specific texture coordinates to sample the texture data at. For sake of simplicity, I'm going to assume a 2D texture, so the texture coordinates are a 2D vector (s,t). (The explanation is analogous for other dimensionalities).

If you want to texture-map a triangle, one typically uses one of two strategies to get to the texture coordinates:

  1. The texture coordinates are part of the model. Every vertex contains the 2D texture coordinates as a vertex attribute. During rasterization, those texture coordinates are interpolated across the primitive.
  2. You specify a mathematic mapping. For example, you could define some function mapping the 3D object coordinates to some 2D texture coordinates. You can for example define some projection, and project the texture onto a surface, just like a real projector would project an image onto some real-world objects.

In either case, each fragment generated when rasterizing typically gets different texture coordinates, so each drawn pixel on the screen will get a different part of the texture.

The key point is this: each fragment has 2D pixel coordinates (x,y) as well as 2D texture coordinates (s,t), so we can basically interpret this relationship as a mathematical function:

(s,t) = T(x,y)

Since this is a vector function in the 2D pixel position vector (x,y), we can also build the partial derivatives along x direction (to the right), and y direction (upwards), which are the rate of change of the texture coordinates along those directions.

And the dTdx and dTdy in textureGrad are just that.

So what does the GPU need this for?

When you want to actually filter the texture (in contrast to simple point sampling), you need to know the pixel footprint in texture space. Each single fragment represents the area of one pixel on the screen, and you are going to use a single color value from the texture to represent the whole pixel (multisampling aside). The pixel footprint now represent the actual area the pixel would have in texture space. We could calculate it by interpolating the texcoords not for the pixel center, but for the 4 pixel corners. The resulting texcoords would form a trapezoid in texture space.

When you minify the texture, several texels are mapped to the same pixel (so the pixel footprint is large in texture space). When you magnify it, each pixel will represent only a fraction of the corresponding texel (so the footprint is quite small).

The texture footprint tells you:

  • if the texture is minified or magnified (GL has different filter settings for each case)
  • how many texels would be mapped to each pixel, so which mipmap level would be appropriate
  • how much anisotropy there is in the pixel footprint. Each pixel on the screen and each texel in texture space is basically a square, but the pixel footprint might significantly deviate from that, and can be much taller than wide or the other way around (especially in situations with high perspective distortion). Classic bilinear or trilinear texture filters always use a square filter footprint, but the anisotropic texture filter will uses this information to actually generate a filter footprint which more closely matches that of the actual pixel footprint (to avoid to mix in texel data which shouldn't really belong to the pixel).

Instead of calculating the texture coordinates at all pixel corners, we are going to use the partial derivatives at the fragment center as an approximation for the pixel footprint.

The following diagram shows the geometric relationship:

diagram of pixel footprint in texture space

This represents the footprint of four neighboring pixels (2x2) in texture space, so the uniform grid are the texels, and the 4 trapezoids represent the 4 pixel footprints.

Now calculating the actual derivatives would imply that we have some more or less explicit formula T(x,y) as described above. GPUs usually use another approximation: the just look at the actual texcoords the the neighboring fragments (which are going to be calculated anyway) in each 2x2 pixel block, and just approximate the footprint by finite differencing - the just subtracting the actual texcoords for neighboring fragments from each other. The result is shown as the dotted parallelogram in the diagram.

In hardware, this is implemented so that always 2x2 pixel quads are shaded in parallel in the same warp/wavefront/SIMD-Group. The GLSL derivative functions like dFdx and dFdy simply work by subtracting the actual values of the neighboring fragments. And the standard texture function just internally uses this mechanism on the texture coordinate argument. The textureGrad functions bypass that and allow you to specify your own values, which means you control the what pixel footprint the GPU assumes when doing the actual filtering / mipmap level selection.

Howarth answered 24/10, 2018 at 20:34 Comment(2)
If sampler have enabled anisotropic filtering (and profile supports it) can textureGrad do anisotropic filtering internally?Swob
Yes, it can (assuming that your implementation supports the anisotropic filter, and you enabled it).Howarth

© 2022 - 2024 — McMap. All rights reserved.