dFdxFine 和 dFdxCoarse 之间的区别

Difference between dFdxFine and dFdxCoarse

来自 OpenGL 文档:

dFdxFine and dFdyFine calculate derivatives using local differencing based on on the value of p for the current fragment and its immediate neighbor(s).

dFdxCoarse and dFdyCoarse calculate derivatives using local differencing based on the value of p for the current fragment's neighbors, and will possibly, but not necessarily, include the value for the current fragment. That is, over a given area, the implementation can compute derivatives in fewer unique locations than would be allowed for the corresponding dFdxFine and dFdyFine functions.

它们有什么区别?我应该什么时候关心?

我知道两者都根据 window 坐标计算值的导数,但我不明白用于计算它们的方法。

我想它们都是用硬件实现的,但是你能post dFdx 伪代码实现吗?

来自 GLSL 规范:

It is typical to consider a 2x2 square of fragments or samples, and compute independent dFdxFine per row and independent dFdyFine per column, while computing only a single dFdxCoarse and a single dFdyCoarse for the entire 2x2 square.

基本上计算导数的方式是通过数值微分。为了简单起见,假设我们正在渲染到单采样帧缓冲区中,并假设我们要计算 dFdx(a)。然后通常会同时对相邻片段的 2x2 正方形进行着色(即在同一工作组内):

    a00  a10
    a01  a11

从概念上讲,所有着色器调用都将计算它们的值 a,将其写入共享内存,并发出屏障。然后在屏障之后,导数可以近似为:

dFdxFine(a) = (a10 - a00)/dx       at xy = 00, 10
dFdxFine(a) = (a11 - a01)/dx       at xy = 01, 11

对于粗导数,规范明确允许对整个 2x2 像素块只计算一个导数。因此,符合规范的实现也可以计算:

dFdxCoarse(a) = (a10 - a00)/dx     at xy = 00, 10, 01, 11

两者是否存在性能差异取决于硬件。如果他们在你的硬件上做 return 不同的结果,那么 'coarse' 版本应该更快。然而,通常你不应该关心这些功能。只需使用 dFdxdFdy 变体,它们使用实现默认变体(精细或粗糙)。