3.3 Derivatives

The ability to take derivatives of functions relative to screen-space position is important for shader antialiasing. Sh offers functions to compute derivatives of any value with respect to screen-space derivatives, but these can only be used inside a fragment shader. Also, derivatives may be approximated with differences on some platforms (or, unfortunately, may be unavailable). A summary of the derivative functions is provided in Table 3.5.

/ E  The dx and dy functions compute the derivative of their argument with respect to screen-space position x and y respectively. If an n-tuple is given as an argument, an n-tuple is returned, with each component being the derivative of the corresponding component in the input. The jacobian function computes a matrix. The first row is basically the result of applying dx to the input, the second row the result of dy applied to the input. The fwidth function computes the componentwise maximum of the absolute values of dx and dy. This is the usual rule used to select a MIP-map level in a texture. Finally, the gradient function only takes a scalar as an argument, and returns a 2-tuple which is the result of taking the derivative of the provided quantity in the x and y directions.





Operation  r <-- a,b,c,...Description






/ E  dx  N <-- N Derivative with respect to screen-space x coordinate



/ E  dy  N <-- N Derivative with respect to screen-space y coordinate



/ E  fwidth  N <-- N Maximum absolute value of derivatives:
         ( |   | |   |)
           ||dai|| ||dai||
ri <-- max   |dx |,|dy | .



/ E  gradient 2 <-- 1 Compute pair of screen-space derivatives for a scalar



/ E  jacobian 2 N <-- N Compute the Jacobian matrix with respect to screen-space x and y



 


Table 3.5: Derivative functions. Only defined inside fragment shaders.


Note: This manual is available as a bound book from AK Peters, including better formatting, in-depth examples, and about 200 pages not available on-line.