I am trying to understand the dFdx()
and dFdy()
functions in GLSL.
I understand the following:
dFdx()
and dFdy()
find the rate that a value changes between the current fragment and a neighboring fragment.I don't understand what the rate of change is referring to. Is it the rate of change of fragment coordinates?
Could it possibly be that you can find the rate of change of an arbitrary variable between two invocations of the fragment shader? Are the shader invocations "reading" variables from neighboring invocations? For a (simplistic) example:
// invocation for fragment 1 float x = 1.0; float d = dFdx(x); // invocation for fragment next to fragment 1 along the x axis. float x = 2.0; float d = dFdx(x);
Would d
be -1.0 and 1.0 respectively?
dFdx(p) is simply the difference between the value of p at pixel x+1 and the value of p at pixel x, and similarly for dFdy(p) .
fwidth - returns sum of approximate window-space partial derivatives magnitudes.
The fragCoord variable is a built-in variable that contains the coordinates of the pixel where the shader is being applied. More concretely: fragCoord : is a vec2 that is between 0 > 640 on the X axis and 0 > 360 on the Y axis. iResolution : is a vec2 with an X value of 640 and a Y value of 360.
To understand how these instructions work, it helps to understand the basic execution architecture of GPUs and how fragment programs map to that architecture.
GPUs run a bunch of threads in 'lock-step' over the same program, which each thread having its own set of registers. So it fetches an instruction, then executes that instruction N times, once for each running thread. To deal with conditional branches and such, they also have an 'active mask' for the currently running group of threads. Threads that are not active in the mask don't actually run (so their registers don't change). Whenever there is a conditional branch or join (branch target) the thread mask is changed appropriately.
Now when a fragment program is run, the fragments to be run are arranged into "quads" -- 2x2 squares of 4 pixels that always run together in a thread group. Each thread in the group knows its own pixel coordinate, and can easily find the coordinate of the adjacent pixel in the quad by flipping the lowest bit of the x (or y) coord.
When the GPU executes a DDX or DDY instruction, what happens is that it peeks at the registers for the thread for the adjacent pixel and does a subtract with the value from the current pixel -- subtracting the value for the higher coordinate (lowest bit 1) from the lower (lowest bit 0).
This has implications if you use dFdx
or dFdy
in a conditional branch -- if one of the threads in a quad is active while the other is not, the GPU will still look at the register of the inactive thread, which might have any old value in it, so the result could be anything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With