WebGL shader and partial derivatives dFdx introduced dFdy

This paper suitable for webgl, computer graphics, visualization front-end reader interest.

Partial derivative function (in the HLSL ddx and ddy, GLSL and the dFdx dFdy) is a fragment shader instructions for a computing any variable (function) the rate of change based on the screen space coordinates. In WebGL using the dFdx and dFdy, there is another function fwidth = dFdx + dFdy.

Calculating the partial derivative

During triangle rasterization, the GPU simultaneously multiple instances Paopian shader, but a pixel is not a pixel to be executed, but executed in parallel to organize a set of 2x2 blocks of pixels. Is the partial derivative by the differential pixel block variables (variation rate) calculated. dFdx represents the pixel value of the right side of the block minus the value of the left pixel in the block element, and is a value represented by the following dFdy subtracting the pixel values ​​of the pixel above. As shown below, is shown in FIG. FIG rendering screen pixels, the red areas in a block of pixels, p (x, y) represent the coordinates (x, y) of the sheet element in screen space coordinates (pixels) either one variable, the figure shows the calculation of dFdx and dFdy.

Calculating the partial derivativeCalculating the partial derivative

Partial derivative functions can be used in any variable fragment shader. The vector and matrix-type variable, the partial derivative of the function calculates each element of the variable.

Partial derivative function is the basis of texture mipmaps implemented, can achieve the effect of a series of algorithms and, in particular, which (such as rendering uniform width of the screen frame depends on the spatial coordinates refer to my another article: https: // www .jianshu.com / p / 1a0979a2d972).

Partial derivative and mipmaps

FIG Mipmaps for a series of sub-texture calculation, each sub FIG downsizing than the previous two times. They used to refine serration texture (texture mapping to itself is smaller than the size of the surface) of the time.
Mipmaps is also important for the texture cache consistency, traversing a triangle (the fragment), when it will be forced to get a nearest pixel ratio: The ratio of a pixel on the triangle to ensure as far as possible corresponds to one pixel on the texture. Mipmaps is one of the few techniques may simultaneously visualization and performance.
Using the partial derivatives in the texture sampling process to select the best mipmap stages. The rate of change in texture coordinates in screen space according to a selected mimmap series, the greater the rate of change, the larger the number of stages mimap, the smaller the contrary.

Calculated surface normal vector (flat shader)

Partial derivative functions can be used to calculate the normal vector of the current face (triangles) in the fragment shader. The current level of the world coordinate system of the fragment partial derivatives and the vertical partial derivative of the two vectors on the two triangular faces, their cross product result is a vector normal to the surface, owned by the vector a of the result face Method line vectors. Of particular note is the order of the cross product of two vectors. Here is the code GLSL camera coordinate system by the coordinate calculation of the surface normal vector:

normalize(  cross(dFdx(pos),  dFdy(pos))  );

One of the applications on the partial derivative of the function can be referred to "the WebGL rendering wireframe Single Channel", more applications will be described in later.

Reference Document
http://www.aclockworkberry.com/shader-derivative-functions/#footnote_3_1104

Welcome to public concern number "ITman tert-Biao." Tert-Biao, has 10 years of development experience, the incumbent system architects, technical director, technical trainer, professional planners. Familiar with Java, JavaScript. There are in-depth studies in computer graphics, WebGL, front-end visualization. For programmers thinking skills training and training, career planning programmers and programmers have a keen interest in financial investments.

No. tert-Biao ITman publicNo. tert-Biao ITman public

Guess you like

Origin flyfox1982.iteye.com/blog/2442454