在着色器中访问时,OpenGL 如何决定使用 MAG_FILTER 还是 MIN_Filter?

How does OpenGL decide between using MAG_FILTER and MIN_Filter when accessing in shader?

用glTexParamteri(GL_Texture_2D, GL_TEXTURE_MAG_FILTER, ...) 和glTexParamteri(GL_Texture_2D, GL_TEXTURE_MIN_FILTER, ...)配置OpenGL时,OpenGL如何决定在使用 texture(...) 访问着色器中的纹理时使用哪个过滤器?

我唯一的猜测是它正在计算像素足迹,但由于您可以在片段或顶点着色器中访问纹理,因此它无法知道在哪个基元上投影了什么纹理。

My only guess it's that it is calculating the pixel footprint

是的,这就是它的作用。它将通过计算纹理坐标相对于 window space xy 方向的导数来近似纹理 space 中的像素足迹,并且它将在 2x2 像素四边形中通过 有限差分 近似这些导数,就像 dFdx and dFdy GLSL functions 正在工作一样。它将使用两个偏导向量中较长的一个作为大小,并据此计算 Level-Of-Detail 值。

but since you could access the texture in either the fragment or vertex shader it can't know on which primitive what texture is projected.

正确,这就是 GLSL 规范的原因, (版本 4.60)8.9 纹理函数 部分的开头陈述了以下内容:

Texture lookup functions are available in all shading stages. However, automatic level of detail is computed only for fragment shaders. Other shaders operate as though the base level of detail were computed as zero

一些(即:大多数)GLSL 纹理访问函数说它们需要 "implicit derivatives"。所有这些功能只能完全工作:

  1. In the fragment shader.

  2. uniform control flow of the FS内。

如果您在 non-fragment 着色器中调用需要隐式导数的纹理访问函数,那么它只会从基础 mipmap 级别进行访问。但是,如果您在片段着色器中但在统一控制流之外,则所有此类函数都具有未定义的行为。

因此,如果您不在片段着色器中,您要么想要从基础 mipmap 级别访问(在这种情况下,MAG_FILTER 适用),要么想要使用显式提供值的函数用于进行插值:Lod(您 explicitly say what mipmap level(s) you fetch from), Grad (where you explicitly specify derivatives 用于决定像素足迹的位置),或任何 texelFetchtextureGather 函数(不做根本没有插值)。