understanding the basics of dFdX and dFdY
Asked Answered
K

1

3

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.

Kero answered 30/1, 2015 at 23:39 Comment(1)
possible duplicate of Explanation of dFdxLeandro
E
3

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.

Eleonoreleonora answered 30/1, 2015 at 23:57 Comment(2)
What is the unit of dx and dy? Are those dx=1/window width in pixles, 1/window height i pixels??Ep
I belive the unit is one pixel. For example: - We are in pixel (x, y) - The value of some varying "var" is 0.1 at (x, y) - dFdx(var) is 0.35 That means we can compute "var" at (x+1, y) as 0.1+0.35 = 0.45Barogram

© 2022 - 2024 — McMap. All rights reserved.