纹理的各向异性过滤

概念介绍

“各向异性过滤”(Anisotropic Filtering,缩写为 AF) 是一种纹理过滤方法。在一些情况下可以提升画面的清晰度。

下面这幅图是来自 ARM 网站。https://developer.arm.com/documentation/101897/0200/buffers-and-textures/anisotropic-sampling-performance

其中,左图采用一般的“三线性过滤”,右图采用了 2x 的“各向异性过滤”。
可以看到在这个木头箱子的侧面,左图较为模糊,右图较为清晰。

ecj1571048092103.png

原理说明

对于有 mipmap 的情形。
如果是“三线性过滤”,会根据纹理坐标的变化率,计算其应该采用哪一级 mipmap。
但,纹理坐标是二维的,有 u,有 v。
如果 v 和 v 的变化率并不一致,则根据 u 和根据 v 可计算得到不同的 mipmap 级别。
一般“三线性过滤”会取两者之间较大的 mipmap,但这样就不精确了。
“各向异性过滤”则会根据 mipmap_u 和 mipmap_v 的差异,进行多次采样,然后综合。
这样就能提升画质。

实际的采样次数由硬件决定。
当物体的表面正对摄像机时,u 和 v 的变化率差异小,采样的次数也少。
当物体表面近乎垂直于视线时,u 和 v 的变化率差异大,采样的次数也多。

程序可以通过图形 API(Direct3D、OpenGL)设置最大采样次数。
设置 2x 的“各向异性过滤”,则最多会采样 2 次。
如果设置 16x 的“各向异性过滤”,则最多会采样 16 次。
次数越多,画质越好,但性能越差。

下图是各种设置的视觉效果对比(为了凸显问题,图片已经被放大至 200%)

1_HMi0_WQIiNE1TwKyLxSUTA.gif

对性能的影响

“各向异性过滤”会导致更多次数的纹理采样,影响性能。

看各个 GPU 厂商的说法。

  • 高通
    https://developer.qualcomm.com/qfile/34706/80-nb295-7_a-adreno_vulkan_developer_guide.pdf

    Texture filtering can influence the speed of texture sampling. A bilinear texture lookup in a 2D
    texture on a 32-bit format costs a single cycle. Adding trilinear filtering doubles that cost to two
    cycles. Mipmap clamping may reduce this to bilinear cost though, so the average cost might be
    lower in real-world cases. Adding anisotropic filtering multiplies with the degree of anisotropy.
    That means a 16x anisotropic lookup can be 16 times slower than a regular isotropic lookup.
    However, because anisotropic filtering is adaptive, this hit is taken only on fragments that require
    anisotropic filtering. It could be only a few fragments in all. A rule of a thumb for real-world
    cases is that anisotropic filtering is, on average, less than double the cost of isotropic.
    翻译:
    单次的 bilinear 采样,耗费一个 cycle。
    trilinear 采样会加倍,也就是两个 cycle。
    而有时候 mipmap clamping 会使开销降低至与 bilinear 等同。所以平均是低于两个 cycle 的。
    各向异性过滤会导致 N 倍开销。如果是 16x 各向异性过滤,则比正常的过滤慢 16 倍。
    然而,各向异性过滤是自适应的,只在最倾斜的那些像素(片元)才会有 16 倍。通常只会增加少量开销。
    通常而言,平均是小于“不用各向异性过滤”时的两倍

  • ARM
    https://developer.arm.com/documentation/101897/0200/buffers-and-textures/anisotropic-sampling-performance

    Using 2x bilinear AF, instead of trilinear filtering, increases image quality and can also improve performance.
    Using high levels of max anisotropy can improve image quality, but at the cost of performance.
    翻译:使用 2x bilinear AF 性能和质量都会比 trilinear 更好。但 AF 的倍数增加之后,质量会进一步提升,但是性能下降。

  • 苹果
    暂时没有找到对此问题有准确描述的文档。
  • nVidia
    没仔细找官方文档。但看起来似乎影响很微弱。
    在这个《泰坦坠落 2》游戏的推荐配置中,使用各向异性过滤之后,帧率几乎没有变化。
    https://www.nvidia.com/en-us/geforce/news/titanfall-2-graphics-performance-and-tweaking-guide/
    25603081-bfbd56bdf408c838.png

    可能不同游戏会有不同,例如这个游戏的瓶颈不在纹理采样,则当使用各向异性过滤,可能不会影响帧率。

按 ARM 的说法,2x 的各向异性过滤是很不错的。不过接下来就需要进一步测试了。特别是苹果,没有找到对应的文章。

扩展阅读

用 shader 来做过滤,达到更佳的画质:
https://bgolus.medium.com/sharper-mipmapping-using-shader-based-supersampling-ed7aadb47bec

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容