Post date: Oct 25, 2008 10:4:49 PM
Okay, this post started because I wanted to post this extremely useful GLSL snippet I keep rediscovering, originally from here. Basically, it calculates a screen space normal in the fragment shader like so
vec3 n=normalize(vec3(dFdx(gl_FragCoord.z),dFdy(gl_FragCoord.z),1));
Which is pretty cool, but irked me because I'd never seen a satisfactory explanation of how the functions dFdx and dFdy actually worked. The explanation in the GLSL spec doesn't really clarify things to me
genType dFdx (genType p): Returns the derivative in x using local differencing for the input argument p.
genType dFdy (genType p): Returns the derivative in y using local differencing for the input argument p.
These two functions are commonly used to estimate the filter width used to anti-alias procedural textures. We are assuming that the expression is being evaluated in parallel on a SIMD array so that at any given point in time the value of the function is known at the grid points represented by the SIMD array. Local differencing between SIMD array elements can therefore be used to derive dFdx, dFdy, etc.
what?... That's just not clear language to me. But digging around I found the equivalent HLSL functions are called ddx and ddy (ooh, they saved a character) and are described as
ddx(x): Returns the partial derivative of x with respect to the screen-space x-coordinate.
ddy(x): Returns the partial derivative of x with respect to the screen-space y-coordinate.
Hmmm