简单来说,textureGrad()是什么? [英] What is, in simple terms, textureGrad()?

查看:751
本文介绍了简单来说,textureGrad()是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此阅读了 khronos Wiki

但是我真的不明白它在说什么. TextureGrad到底是做什么的?

But I don;t really understand what it is saying. What exactly does textureGrad do?

我认为它对多个mipmap级别进行采样,并使用为其提供的显式导数向量来计算一些颜色混合.但是我不确定.

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.

推荐答案

对纹理进行采样时,需要特定的纹理坐标来对纹理数据进行采样.为了简单起见,我将假设一个2D纹理,因此纹理坐标为2D向量(s,t). (解释与其他维度类似.)

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. 纹理坐标是模型的一部分.每个顶点都包含2D纹理坐标作为顶点属性.在栅格化过程中,这些纹理坐标将在图元上进行插值.
  2. 您指定一个数学映射.例如,您可以定义一些将3D对象坐标映射到某些2D纹理坐标的函数.例如,您可以定义一些投影,然后将纹理投影到表面上,就像真实的投影仪将图像投影到某些真实对象上一样.

无论哪种情况,光栅化时生成的每个片段通常都具有不同的纹理坐标,因此屏幕上的每个绘制像素将获得纹理的不同部分.

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

关键是:每个片段都具有2D像素坐标(x,y)和2D纹理坐标(s,t),因此我们可以将这种关系基本上解释为数学函数:

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)

由于这是2D像素位置向量(x,y)中的向量函数,因此我们还可以沿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 telling use the rate of change of the texture coordinates along those directions.

textureGrad中的dTdxdTdy就是这样.

那GPU需要做什么呢?

当您要实际过滤纹理时(与简单的点采样相反),您需要了解纹理空间中的像素覆盖区.每个单个片段代表屏幕上一个像素的面积,您将使用纹理中的单个颜色值来代表整个像素(撇开多重采样).现在,像素覆盖区表示像素在纹理空间中将具有的实际面积.我们可以通过不是针对像素中心而是针对4个像素角插值texcoords来计算它.生成的texcoords将在纹理空间中形成梯形.

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 maginify it, each pixel will represent only a fraction of the corresponding texel (so the footprint is quiete small).

纹理足迹告诉您:

  • 如果纹理被缩小或放大(GL在每种情况下具有不同的滤镜设置)
  • 每个像素将映射多少个纹理像素,因此哪个mipmap级别是合适的
  • 像素覆盖区中有多少各向异性.屏幕上的每个像素和纹理空间中的每个纹理像素基本上都是正方形,但是像素覆盖区可能会明显偏离正方形,并且可能比宽度或过渡距离高得多(尤其是在透视失真较大的情况下).经典的双线性或三线性纹理滤镜始终使用正方形滤镜足迹,但是各向异性纹理滤镜将使用此信息来 实际上会生成一个与实际像素占用空间更加匹配的过滤器占用空间(以避免混入不应该真正属于像素的texel数据).
  • 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 than, and can be much taller than wide or the over 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.

下图显示了几何关系:

这表示纹理空间中四个相邻像素(2x2)的覆盖区,因此均匀网格为纹理像素,而四个梯形则代表4个像素覆盖区. 现在计算实际导数将暗示我们具有如上所述的或多或少的显式公式T(x,y). GPU通常使用另一种近似值: 只需查看每个2x2像素块中相邻片段的实际texcoords(无论如何将要计算),然后通过有限差分来近似估算占位面积-只需减去相邻像素的实际texcoords彼此的碎片. 结果显示为图中的平行四边形.

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.

在硬件中,这已实现,因此始终在同一扭曲/波前/SIMD-Group中并行着色2x2像素四边形. GLSL派生函数,例如dFdxdFdy 只需减去相邻片段的实际值即可工作.标准的texture函数仅在内部对纹理坐标参数使用此机制. textureGrad函数绕过该功能,并允许您指定自己的值,这意味着您可以控制GPU在进行实际的过滤/mipmap级别选择时所采用的像素尺寸.

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.

这篇关于简单来说,textureGrad()是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆