CG Language Learning && Spring Snow GPU Programming Primer Learning

Source: Internet
Author: User
Tags new set

Although little is known, the spring snow lowbrow of "GPU programming and CG programming" really took me into the shader door, where I first clearly understood the meaning of "semantics", and thank you very much.

Introductory shader, I think you can read 3 books: "GPU Programming and CG programming Spring snow lowbrow" = "CG Tutorial" = "Real-time Rendering 3rd" (in Reading, recently busy, laid aside), lay a theoretical foundation.

The following is the "CG Tutorial" reading notes.

1. Basic CG Functions

1) Mathematical function: abs,acos inverse cosine, all (x) x of all components are not 0 is true,any (x) x has a component of not 0 is True,ceil/floor,clamp (x, A, b), Cross (A, b)/dot (A, b) fork multiply and point multiply , Degrees/radians radian angle Mutual rotation, exp (x) e x-Square, EXP2 (x) 2 x-Square, Fmod (x, y) the remainder (symbol with x), Frac take the fractional part, Lerp (A, B, f) f=0 take a,f=1 take B, otherwise the weight is mixed, LOG/LOG2/LOG10 based on E/2/10 logarithm, pow,round,sign (x) x>0 returns 1, otherwise 0,saturate (x) limits x to [0,1], Smoothstep (min, max, x) x<= Min returns 0,x>=max returns 1, otherwise returns a smoothing value of (0,1), step (A, B) a<=b returns 1, otherwise 0,mul matrix multiplication/vector matrix multiplication, sincos,transpose transpose, noise noise function, lit ( Ndotl, Ndoth, m) calculates ambient light factor x, diffuse reflection coefficient y, specular factor Z, w=1

2) geometric function: distance point and the Euclidean distance of the point, the Euclidean length of the length vector, normalize 1, reflect (I, N) The reflection vector, can be based on the Pt-eyepos to get the incident reverse, then calculate the reflection vector, and then sample the environment map, Mixing the current color and ambient color, you can reflect the environment on the surface of the object, Refract (I, N, eta) refraction vector, can be based on Pt-eyepos to get the incident reverse, and then calculate the refraction vector, and then use the environment map and based on transparency interpolation object color and environment color, then you can " Through the object "see the surrounding scenery

3) Texture sampling function: 1 D tex1d,2 tex2d,3 tex3d and cubic map sampling texcube

2.CG Program

OPENGL/DX's rendering pipeline, like the usual GPU rendering pipeline, I guess the relationship is that the OPENGL/DX library is embedded in the GPU to complete its rendering pipeline, so it's actually a thing.

That CG program, the CG program is the beginning of the text form, the application through the CG compiler to translate it into a middle form, and then through the CG core runtime to make the intermediate form the final form, the final form of the runtime into the GPU to execute. CG is also embedded in the application: The application uses the CG compiler to compile the CG program, then calls the CG Core runtime and the CGGL/CGDXD library to send the program to the driver, without having to deal directly with the GPU.

Here is the process of using the OpenGL interface's CG program text to GPU execution:

    

The CG program is dynamically compiled, that is, the application can dynamically run the process, update the final form of CG in the GPU, the GPU will perform the above final form program for each input vertex, that is, parallel computing, fragments similar.

CG Program compilation requires a CG program, you also need to specify the profile (vertex program designated one, fragment program designated one), a CG profile defines a "by a specific graphics hardware or API supported by the CG language subset", profile determines which CG function your CG program can use , if the use of unsupported features on the compilation does not pass, also determines the compiled program can be run on which hardware, hardware does not support can not run.

CG compilation errors include traditional errors and dependency profile errors, which are syntax errors, while CG programs use features that are not supported by the profile, which may be considered using a more advanced profile solution.

3. Vertex shader

Attributes: The vertex data provided by the Vertext array, such as spatial position, normal vector, texture coordinates, and vertex color, which is the data for each vertex. property is only in the vertex shader, and there are no attributes in the slice shader. a property can be interpreted as input data for each vertex. OpenGL ES 2.0 Specifies that the maximum number of properties that should be supported by all implementations should not be less than 8.

Uniforms:uniforms saves read-only constant data that is passed to the shader by the application. In a vertex shader, these data are usually transformation matrices, illumination parameters, colors, and so on. A variable modified by the uniform modifier is a global variable that is visible to both the vertex shader and the slice shader, that is, if the two shaders are connected to the same application, they share the same set of uniform global variables. So if you declare a uniform variable with the same name in both shaders, make sure it is exactly the same as the same name: the same name + the same type, because they are actually the same variable. In addition, the uniform variable is stored in a constant store, thus limiting the number of uniform variables, OpenGL ES 2.0 also specifies that all implementations should support the maximum number of vertex shaders uniform variables can not be less than 128, the largest slice shader uniform variable number does not can be less than 16.

The uniforms variable is only the initial value and the other variables, which are allowed to be changed (unless modified by const), as with other variables. In Unity's shader, in addition to the above data calculation uniforms, the open attributes should also be uniforms, such as regular texture mapping, etc.

Samplers: A special kind of uniform used to render textures. The sampler can be used for vertex shaders and slice shaders.

Output of the vertex shader:

The varying:varying variable is used to store the output data of the vertex shader and, of course, the input data of the slice shader, and the Varying variable will eventually be linearly interpolated during the rasterization process. Vertex shader If you declare a varying variable, it must be passed to the slice shader to further pass to the next stage, so the varying variable declared in the vertex shader should re-declare the varying variable of the same type in the slice shader. OpenGL ES 2.0 also specifies that the maximum number of varying variables that should be supported by all implementations should not be less than 8.

At least the location information should be output at the vertex shader stage-that is, the built-in variable: gl_position, the other two optional variables are: Gl_frontfacing and gl_pointsize.

4. Fragment shader, similar to vertex shader.

5. Vertex transformations

The projection transformation is to change the eye space to the clipping space = "The Four corners of the flat section corresponding to the four corners of the cube, all points are right by this matrix, not in the square body culling (-w<x<w,yz), at this time the cube is not a unit cube;"

Then, perspective division, mapped to the unit in the cube, and then according to the size of each viewport (camera size) mapped to the corresponding location of the viewport "finally this transformation understand very little, doubt", here is a question, cutting is in the cutting space, or in the unit in the cube? In fact, the answer to this question is not necessarily important, because it may only be a different approach. I saw in a predecessor implementation of the Rasterizer code, he was cut in the-w,w,w, after cutting the perspective Division, then the window transformation, complete the vertex transformation.

6. The data passed in by the application, that is, the data that shader can read (if the model is provided, such as normal), there should be a corresponding global variable or vertex data in the Unityshader

7. Particle system, the so-called vertex as a particle, and then control the position, size and color of the particle, but normal, the vertex is the size of the wood, the book of this wood has given specific practices; what is a particle map? How is it used? Doubts.

8. Vertex blending, with the skin to understand, the vertex is the point on the mesh, the matrix is the bone set, we have a basic posture: the basic Posture Matrix set (all units matrix), vertex set, matrix set of each of the matrix of each vertex impact weight (generally not more than 4 matrix affect a vertex) and Other posture Information: a new set of matrices corresponding to the basic pose matrix, which most likely corresponds to the basic posture---the Unit matrix. So that we can express a variety of actions, the matrix in the matrix is the physical meaning of transformation: from the position of the base pose to the position of other positions of transformation. The piece of code in the book is worth studying, with 2 points to think about:

  

Why is the model 3x4, that is why it is not a normal change 4x4? Estimate: The model here is used to calculate the position of the vertex transform, only XYZ, and the model of the 4th row only affects the POS 4th variable, so can be omitted, in fact, Originally transformed into 4x4 is also for the convenience of the matrix, here discard useless information only.

Why do we just take the model one corner 3x3 to transform the normal vector? First, because it is an affine transformation, instead of its inverse transpose, the 4x4 matrix transform 3-dimensional vector can be directly used with its 3x3 portion of the left-multiplicative vector, because:

| A1, A2, A3, a4|

| B1, B2, B3, b4|

| C1, C2, C3, c4|

| D1, D2,D3, d4|

The vector normal expands to 4 dimensions (normal, 0), the result is a 4x1 vector, the first 3 dimensions of this vector are only affected by the upper left corner 3x3*normal, and the 4th dimension is only auxiliary calculation, directly kill, proof. This conclusion can be used to transform normal vectors or other vectors. Remember not to confuse why choose Model and why choose the upper left corner of the model 3x3! The former is the chosen transformation matrix, the latter is the essence of Shanfanjiujian.

Environment mapping

9. Cubic Map Texture

The new GPU supports cubic map textures (Texcube can be sampled), a cubic map is a cube of 6 textures sampled like a light emitted from the center, the point that penetrates the cube is sampled, which is equivalent to the color seen in the center outward, and is typically used for environment mapping.

Generate cubic Map: Put a camera in the center of the scene, left, right, up, down, front, and then take a photo, the camera in each direction of placement is about 90 degrees, up and down into 90 degrees, such as:

  

In this way, the 6 images are compact enough to fully display the surrounding data.

R = Reflect (N, i), I is the incident ray that points to the vertex, the return value is the reflected light emitted from the vertex, N needs to be normalized, but I do not need to, and the return value R goes to query the texture does not need to be normalized, because the length of I does not affect the direction of reflect return R , and R is interpolated by the rasterizer prior to sampling in the fragment program, and the non-normalized R is more accurately interpolated (somewhat like spherical interpolation and ellipsoidal decent interpolation, why the latter is not studied more accurately).

The environment map depends only on the direction but not on the position, so the sampling result is the same in the same position, and it is best used for the surface to show its mottled beauty.

  

Environment maps usually need to be sampled in the vector of the world coordinate system, so we have to turn N and I to the world coordinate system after reflect, "of course, can also first in the model coordinate system reflect, and then reflect to the world coordinate system, not tested", the vector transformation method reference above 8th.

  

Why use 3x3 to go to the left, look at the 8th of the above proof is good. In addition, here we assume that POSITION.W = 1 (which is also the normal value), if its W component has been specially processed to cause not 1, we need to divide all its components by the W, so as to make the position.w=1, conveniently only 3-dimensional calculation (default w=1)

10. Control Map

Using textures to control variable parameters, this idea is respected by the book, it is not known because of its novelty or indeed good, did not study the performance of shader sampling texture and multi-texture sampling. But this is a good thing for art, like an example of an environment map, adding a control texture, giving a "reflection factor" to each fragment, and then controlling the effect of each point reflection, which exists in the control map. In fact, the normal map is also a control map: the control method vector.

11. Refraction, through the object to see the environment: from the point of view to the vertex emit light, after the object refraction (only into the object refraction), sampling environment map, done.

Refract (i, N, etaratio); Similar reflect,n need normalization I do not need.

12. Fresnel effect: Into the eyes of the reflected light and refraction of light, the book gives the calculation of the weight of 2 approximate formula:

  

The color value of the final vertex is:

  

Basically just provide a more accurate mixing parameters, more convenient than control map, global variables, but if you want to apply, such as to see the fish in the water and other real refraction? In the book of Wood, do you want to use ray tracing to see what reflects the light reflected by refraction? Doubts.

Fresnel effect with color dispersion: In the vertex program, the reflection vector is computed, the Fresnel coefficients are computed, the refractive vectors corresponding to the RGB3 components are computed (because of the different refractive coefficients), the reflected ambient color values are first obtained in the fragment program, and then the color values of the RGB3 refraction vectors are obtained. The 3 color values respectively take the RGB component to form the final refractive value, and then use the Fresnel coefficient to mix the reflection color value and the refractive color value, done.

13. Bump Map.

has become this article: in-depth understanding of normal maps

14. Performance aspects, scattered in this article: write some small details of shader

15. Appendix

Tips (technique), "each technique describes an implementation effect, a CG program can have multiple tricks, each with a level of graphics processor", so here's the trick similar to unity Shader's subshader, general unity Shader are one or more subshader+ a fallback,2+ skill to adapt to different hardware. When executing shader, the hardware does not conform to the next subshader, starting with the first Subshader, until it is compliant.

Process (Pass), "a trick can include one or more procedures, a process that represents a separate rendering process, which includes a set of render states (some additional settings) and a set of shaders, such as procedure 0 can only set the depth value for subsequent processes to use, etc." the process here is unity Pass in shader, when performing subshader, sometimes only one pass is rendered, and sometimes each pass is rendered sequentially. The default seems to be the latter, in doubt.

CG Language Learning && Spring Snow GPU Programming Primer Learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.