"Unity Shaders" learning notes--shader and rendering pipelines
Reprint Please specify source: http://www.cnblogs.com/-867259206/p/5595924.html
This series of articles was written using Unity5.3.
Before writing the code:
Of course, if unity wasn't installed, you wouldn't learn unity shaders, would it?
Before reading this series of articles you need to have some programming concepts.
In VS, Unity shaders does not have syntax highlighting and smart hints, and the VS party can refer to this article to make the code highlighted, shaderlabvs
or NShader
a plugin such as download or the like to highlight the code.
This is the basic knowledge of the unity shaders for small white, and if you have a foundation or you are a great God, then these articles are not for you.
Due to the limitation of the author's level, there may be some fallacy in the text, and I implore to point out.
Shader means the shader , which is "coloring the object," so the shader function is to calculate what color the object ultimately displays.
The term GPU is believed to be a stranger to everyone. It is a component of the computer processing image. The GPU is an abbreviation for the Graphic Processing unit (graphics processing Unit). Because GPUs have high parallel structures (highly parallel structure), GPUs have a higher efficiency than CPUs in processing graphics data and complex algorithms. Let's take a look at the comparison of the following two code, both of which extract the color value of each pixel on the 2D image, the first is the C + + code that is operating on the CPU, and the second is the CG code that is calculated on the GPU:
The CPU iterates through the pixels with a For loop statement, and the GPU is enough for just one line of code. GPU is very powerful in graphics processing, right?
Shader, however, is a program performed by the GPU that operates on 3D objects.
Shader development language
HLSL: Mainly used for Direct3D. Platform: Windows.
GLSL: Mainly used for OpenGL. Platform: Mobile platform (IOS, Android), Mac (only if you target Mac OS X or OpenGL ES 2.0)
CG: Fully compatible with DirectX 9.0 and OpenGL. Compile the GPU assembly code at runtime or in advance.
CG supports more platforms than HLSL, GLSL, and Unity shader uses CG/HLSL as the development language.
How CG compiles in Unity3d
Windows:direct3d GPU Assembler Code
MAC:OPENGL GPU Assembler Code
Flash:flash GPU Assembler Code
Ios/android:unity will convert CG to GLSL code
That is, in addition to the mobile platform will convert CG into GLSL code, the rest of the platform is converted into assembly code.
Rendering pipelines
Shader by pipeline classification is generally divided into fixed rendering pipeline and programmable rendering pipeline.
The rendering pipeline (Rendering Pipeline) is actually the GPU rendering process. You can understand the rendering in this way,在一个三维坐标系下,给定一个视点(即摄相机),给定三维物体、光源以及照明模式、纹理等信息,如何绘制一幅呈现在视点画面中的二维图像
The process.
The pipeline that the earlier GPU follows is called a fixed pipeline (Existing fixed Function Pipeline).
(English is not very good to check the words in each box, because I am too lazy to mark Chinese on the map)
The orange part of the figure is what the user can configure with the GPU. As the name implies, the fixed pipeline is a functional pipeline. The so-called function fixed, according to my understanding, is the object surface and light reflection, refraction algorithm fixed, can not be modified, so the object can only show the kind of texture similar to plastic (if you build a model with 3DMax, you will understand), although you can use stickers to compensate. Fixed pipelines can only be configured with some parameters such as "whether to turn on fog," "whether to turn on illumination", "to open depth test", and not to control the algorithm of object interacting with light. Different textures of objects, such as metal and wood, metal reflective strong, look hard, wood reflection is weak, the texture is softer, the algorithm of the two material coloring of metal and wood should be different.in short, fixed pipelines because of the fixed function, can not be on the program to the details of the object to give more free control, can not achieve more cool effect.
No longer need to learn the fixed pipeline, the GPU has entered the 2.0 era, the new graphics card is a programmable rendering pipeline.
The so-called programmable, which is the fixed part of the function described above, can be programmed to control by programmers. This freedom is even bigger and can show more special effects.
Let's take a look at the flow of programmable rendering pipelines.
The first Orange is vertex Shader (vertex coloring) and the second Orange is fragment Shader (element coloring). These two parts are the parts that programmers can program. The vertex shader program, whose function is to transform the vertices one after the other, get the contour of the object, and then linearly interpolate the pixels on the opposite side. The element coloring program, depending on the detection, determines whether to fix information such as color for each pixel, processing by pixel. The output of the vertex is the input of the fragment. Vertex is the vertex transform, both the input and output need to contain the position information of the vertex, fragment is the color depth change, the output is generally only color information. If you are in the first contact with this part of the knowledge, you may feel a little confused, but it doesn't matter, with the depth of study you will gradually understand. As far as you can see, the fixed-point shader mainly deals with the vertex, while the element shader mainly calculates the color of the pixel.
In fact there are other coloring programs, but this will not be covered, so it will not be explained in depth.
OpenGL rendering Pipeline
, this is the workflow flowchart for OpenGL.
About the individual processes in the diagram are doing what work, you can look at this blog, speak very detailed, I will not repeat.
The above process, beginners can be simply recorded as four parts
顶点变换 → 图元装配和光栅化 → 片元纹理映射和着色 → 写入帧缓存
The programmable part is the vertex transform and the slice element coloring.
Four kinds of shader of Unity3d
There are four types of unity shader:
The fixed function shader belongs to the stationary rendering pipeline shader, which is used for advanced shader standby shader when the old video card cannot be displayed.
Vertex and Fragment Shader the most powerful Shader type, which belongs to the programmable rendering pipeline. The CG/HLSL language is used. Which is what I said above.
Surface Shader Unity3d recommended type of shader. It is a code generator that helps us to omit the duplicated code, making it easier to write shader. The CG/HLSL language is also used.
Compute Shader This is one of the Unity3d additions. Look at the introduction of the Niang Encyclopedia:
Compute shader technology is a new feature of Microsoft DirectX One API, with the help of Compute shader, programmers can directly use the GPU as a parallel processor, the GPU will not only have 3D rendering capability, but also have other computing power, That is what we call the concept of GPGPU and the physical acceleration of operations.
Compute Shader uses the HLSL language.
Next we'll learn from the simplest surface shader.
"Unity Shaders" learning notes-rendering pipelines