Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of dFdx

I am trying to understand the dFdx() and dFdy() functions in GLSL.

I understand the following:

  1. The derivative is the rate of change
  2. The partial derivative of a function with two parameters is when you differentiate the function while keeping one of the parameters constant.
  3. 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?

like image 718
bwroga Avatar asked May 03 '13 18:05

bwroga


People also ask

What does dFdx mean?

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

What is Fwidth?

fwidth - returns sum of approximate window-space partial derivatives magnitudes.

What is fragCoord?

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.


1 Answers

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.

like image 152
Chris Dodd Avatar answered Sep 28 '22 09:09

Chris Dodd