是片元着色器中的一个用于计算任何变量基于屏幕空间坐标的变化率的指令。只能在片段着色器中使用。dFdx要么是通过dFdxFine来计算,要么是通过dFdxCoarse来计算。需要在片元着色器前面加上
#extension GL_OES_standard_derivatives : enable
HLSL中的ddx和ddy,GLSL中的dFdx和dFdy
在三角形栅格化期间,GPU会同时跑片元着色器的多个实例,但并不是一个pixel一个pixel去执行的,而是将其组织在2x2的一组pixels块中并行执行。偏导数就是通过像素块中的变量的差值(变化率)而计算出来的。dFdx表示的是像素块中右边像素的值减去素块中左边像素的值,而dFdy表示的是下面像素的值减去上面像素的值。如下图所示,图中显示的是渲染的屏幕像素,图中红色区域是一个像素块,p(x,y)表示在屏幕空间坐标系中坐标(x,y)的片元(像素)上的某一个变量,图中显示了dFdx和dFdy的计算过程。
偏导数计算
偏导数函数可以用于片元着色器中的任何变量。对于向量和矩阵类型的变量,该函数会计算变量的每一个元素的偏导数。
dFdxFine和dFdyFine基于当前片段及其直接邻居的p值,使用本地差分计算导数。
dFdxCoarse和dFdyCoarse使用基于当前片段的邻居的p值的局部差分来计算导数,并且可能但不一定包括当前片段的值。 也就是说,在给定区域上,实现可以在比对应的dFdxFine和dFdyFine函数所允许的更少的唯一位置中计算导数
genType dFdx( genType p);
genType dFdy( genType p);
genType dFdxCoarse( genType p);
genType dFdyCoarse( genType p);
genType dFdxFine( genType p);
genType dFdyFine( genType p);
p
Specifies the expression of which to take the partial derivative.
Available only in the fragment shader, these functions return the partial derivative of expression p with respect to the window x coordinate (for dFdx*) and y
coordinate (for dFdy*).
dFdxFine and dFdyFine calculate derivatives using local differencing based 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.
dFdx returns either dFdxCoarse or dFdxFine. dFdy returns either dFdyCoarse or dFdyFine. The implementation may choose which calculation to perform based upon factors such as performance or the value of the API GL_FRAGMENT_SHADER_DERIVATIVE_HINT hint.
Expressions that imply higher order derivatives such as dFdx(dFdx(n)) have undefined results, as do mixed-order derivatives such as dFdx(dFdy(n)). It is assumed that the expression p is continuous and therefore, expressions evaluated via non-uniform control flow may be undefined.
本文介绍了OpenGL片段着色器中的dFdx和dFdy函数,用于计算变量相对于屏幕空间坐标的偏导数。这些函数在2x2像素块中并行执行,dFdx表示水平方向上的变化率,dFdy表示垂直方向上的变化率。dFdxFine和dFdyFine考虑当前片段和直接邻居,而dFdxCoarse和dFdyCoarse可能不包含当前片段的值。在片段着色器中,这些函数用于连续表达式的导数计算,但混合或高阶导数可能导致未定义的结果。
5万+

被折叠的 条评论
为什么被折叠?



