Before we learned how DirectX 11 draws a graphic (triangle) on the screen, which involves shaders, we just use the method, without explaining how the shader works, what the shader is, and so on, and what the shader is going to look like today ... Since there are a variety of shaders in DirectX 11, and some shaders are used in some advanced applications, it is OK to first understand the vertex shader and pixel shader as a beginner, and the others to get a deeper understanding of when they hit.
Shader:
In the previous video card, the image was rendered just like a pipeline, and could not be programmed. But then the GPU appeared, so that you can calculate a lot of things, it can be programmed, the shader is a programmable example, he passed the code to the GPU to calculate, and then through the screen display. As in the previous example, we created a cached data (Vertex buffer) to pass the coordinates of the triangle to the GPU. In the DirectX One SDK, three base shaders are supported: Vertex shader (Vertex Shader), Pixel shader (Pixel Shader) "Note: Some places are called Fragment Shader", as well as geometry shaders (Geometry Shader). Vertex shaders use vertices as input data, as long as each vertex cache data is passed into the GPU, and the pixel shader uses one pixel as the input data, and the geometry shader uses primitive (primitive) as the input, the primitive can be a point, a line, or a triangle. These three base shaders are encountered during rendering, and in Direct3D 11, the GPU must contain a correct vertex and pixel shader, and the geometry shader is optional, so that's why we first understand the vertex shader and pixel shader. In DirectX, of course, other shaders are included, such as Hull Shader,domain Shader (domain shader) for surface subdivision (some places are called Mosaic tessellation), and Compute Shader is used for calculations.
Vertex shader:
Vertex shaders support programming, you can see the vertex shader is a C language function, through the vertex as a parameter input this function, and return the programmed vertex information. When a program passes through the vertex shader to buffer data, the GPU iterates over the vertex buffer data and executes the shader function once for each vertex.
Vertex shaders can be used to do a lot of things, the most important thing is to use the coordinate transformation and so on, in the Direct3D programming involves a lot of coordinate transformation, such as: the world coordinates into screen coordinates and so on. For example a 3D triangle with (0, 0, 0) (1, 0, 0) (0, 1, 0) coordinates, when drawn to the 2D texture buffer, the GPU needs to know the coordinates in the 2D coordinate system, so as to know where to draw. On the coordinates of the transformation, we will also encounter, this is not discussed here, for the previous example we just need to know to pass in a vertex information, return a vertex information is OK, the specific code is as follows: 1 float4 VS (float4 pos:position): sv_posit ION
2 {
3 return Pos;
4}
The above code is HLSL, in the previous we also introduced, his language like C, passed a FLOAT4 data, position is a declarative string, and Sv_position has a special declarative meaning, in HLSL can be queried, That is, to tell the drawing pipeline that this is a coordinate data definition. This coordinate is what the GPU needs to know, i.e. where to draw.
Pixel shader:
In modern displays, the screen is composed of a lot of square lattice, these formats are small, we call it pixels, each pixel contains its own color, they do not rely on each other. In fact, when we need to draw a triangle on the screen, the rendering on the screen is a triangle, as shown in the figure below, we will be better understood.
A triangle that contains three vertices, linked together by these three vertices, is called rasterization. The GPU first needs to know which pixels need to be rendered, and then activates and assigns the color values to the pixels that need to be rendered (inside the triangle), and the pixel shader is to calculate which pixels need the color. The pixel shader passes the input pixel color value, and then calculates the color and returns it to the drawing pipeline. Pixel pipeline parameters are usually returned by the geometry shader, and if there are no geometry shaders, such as the one we mentioned in the previous section, it is returned through the vertex shader.
In the vertex shader, a FLOAT4 value is created with the Sv_position declaration description to return the coordinate position of the pixel, which acts as an input parameter to the pixel shader, which tells the current GPU coordinates, and a color value returned by the pixel shader, which is also the FLOAT4 value, and uses the SV The _target declaration description to represent the rendering format that will be used for the target. Its code is as follows: 1 float4 PS (float4 pos:sv_position): Sv_target
2 {
3 return Float4 (1.0f, 1.0f, 0.0f, 1.0f); Yellow, with Alpha = 1
4}
To compile the shader:
The code that the shader writes, HLSL, is saved in a text file that can be compiled by D3dx11compilefromfile () in Direct3D 11, with the code as follows: 1//Create the vertex shader
2 if (FAILED (D3dx11compilefromfile ("tutorial03.fx", NULL, NULL, "VS", "Vs_4_0", D3dcompile_enable_str Ictness, NULL, NULL, & Pvsblob, & Perrorblob, NULL)))
3 return FALSE;
4
5//Create the pixel shader
6 if (FAILED (D3dx11compilefromfile ("tutorial03.fx", NULL, NULL, "PS", "Ps_4_0", D3dcompile_enable_str Ictness, NULL, NULL, & Ppsblob, & Perrorblob, NULL)))
7 return FALSE;
Binding shaders:
This allows us to bind the vertex shader and pixel shader to the pipeline in C + + code through the Vssetshader () and Pssetshader () two methods, and when we use the Draw method, the vertex cache information vertex buffer is passed into the drawing pipeline for drawing So that the vertex shader and pixel shader are understood.
In fact, through this article is to understand the two east, vertex shader is to tell the pipeline coordinates, and the pixel shader is to tell the pipeline color, with these two things can be drawn graphics. Of course, in the actual demand can not be so cheap, writing code is quite a lot of, the main hope that Microsoft can also inherit the HLSL speech smart hint to VS, so I imagine that I write code of the person will bring a very cool feeling.
Report:
Directx11 Pipeline Line
Surface subdivision (Tessellator):
From the above technical analysis diagram, we can understand the Hull Shader, Tessellator, Domain Shader 3 new units of the specific role: Hull Shader is mainly responsible for defining the subdivision level (LOD) and related control points in the subdivision of the "deformation" trend, It is necessary to note that this deformation is only similar to the curvature changes and other small amplitude changes, rather than large polygon displacement; Tessellator is responsible for the hull shader transmission of information, through "violence" to increase the polygon to achieve hull shader requirements; Domain The most important function of shader is to achieve the deformation of the model through the mapping control, which is the high-detail picture we see in the DX11 subdivision surface.
Transferred from: http://www.cnblogs.com/xdotnet/archive/2011/07/31/directx_direct3d11_shaders.html