understanding the basics of dFdX and dFdY

人走茶凉 提交于 2019-12-21 05:22:27

问题


I've read numerous descriptions of the behavior of dFdX(n) and dFdY(n) and do believe I still have a handle on partial derivatives from school. What I don't follow is where does 'n' come from in the simplest possible example?

Reading the glsl built-in functions dFdx(n) and dFdy(n) without any context other than mathematics I would interpret them as "I have some function of x and y: f(x,y), I take the partial derivative of that function w.r.t. x d/dx(x,y), and I evaluate the partial derivative formula for some value of x and y which I assume is the input parameter n above.

I've read many descriptions of how dFdx() and dFdy() allow you to find a window-space gradient for output fragments. The output-fragment case is what I'm most interested in at the moment as I'm not trying to determine the rate of change of texture coordinates w.r.t how the texture is being rasterized.

I'm looking to use dFdx(n) and dFdy(n) to find window-space color gradient of output fragments. I don't fully understand how to mentally construct the function being differentiated and how that relates to the frame buffer and how n relates to that (e.g. does n relate to the 2x2 fragment neighborhood of the current fragment, window coordinate space of the entire framebuffer such that I'm evaluating the gradient at that value, other) ?

I'm hoping that the input type of n in any responses to this question is a scalar (float) and that we just discuss one dimension, dFdx(), to simplify the discussion.


回答1:


Lets check the man page:

genType dFdx(     genType p);

genType dFdy(     genType p);

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 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.

Concentrating on the Fine variant. As each fragment process reaches the dFd* call the GPU will collect the values passed in and based on those values, typically through getting the difference between neighbouring values and dividing by the fragment size.

In other words the fragment shader has calculated the F(x,y) for the fragment and passes it on to the GPU to collect them and pass back the dFdX based on the fragments right next to it Which would have passed F(x+e, y)

GenType means that you can put floats in it, you can also pass in a vec4 and get the component-wise dFd* value.



来源:https://stackoverflow.com/questions/28246413/understanding-the-basics-of-dfdx-and-dfdy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!