C ++ directx11 Development NOTE 4: vertex paintors and pixel paintors

Source: Internet
Author: User
Tags pixel coloring

We have learned how to draw a graph (triangle) on the screen of DirectX 11, which involves the coloring tool. We just used the method, but didn't explain how the coloring tool works, what is a shadow? Today, let's take a look at what it is !!! Because DirectX 11 contains a variety of coloring devices, some of which are used in some advanced applications, as a beginner, first, you can understand the vertex coloring tool and the pixel coloring tool. If you encounter other problems, you can gain an in-depth understanding.

 

Shader:

In the previous graphics card, the image is displayed like a pipeline and cannot be programmed. However, when the GPU came into being, we could compute a lot of things and program it. The coloring tool is a programmable example.CodeIt is sent to the GPU for computing and displayed on the screen. Just as in the previous example, we created a cache data (vertex buffer) to pass the coordinate of the triangle to the GPU. In the DirectX 11 SDK, three basic pasters are supported: vertex shader and pixel shader. Note: fragment shader is also called in some places ], and geometry shader ). The vertex shader uses the vertex as the input data. As long as the cached data of each vertex is transferred to the GPU, the pixel shader uses one pixel as the input data, and the geometric shader uses the primitive (primitive) as an input, a primitive can be a vertex, a line, or a triangle. These three base paintors will be encountered during rendering. In direct3d 11, the GPU must contain a correct vertex and pixel paintors, which are optional, this is why we first understand the vertex and pixel pasters. Of course, in DirectX, other pasters, such as Hull shader and domain shader, are used for surface subdivision (in some cases, it is called Mosaic tessellation), and compute shader is used for computing.

 

Vertex shader:

The Vertex coloring tool supports programming. We can see that the vertex coloring tool is a function in C language. You can use the vertex as a parameter to input this function and return the vertex information after programming. WhenProgramWhen we pass in the vertex buffer data through the vertex coloring tool, the GPU iterates the vertex buffer data and executes the coloring tool function for each vertex.

The Vertex coloring tool can be used to do a lot of things. The most important thing is to use coordinate transformation and so on. In direct3d programming, many coordinate transformations are involved, such: convert the world coordinates to screen coordinates. For example, a 3D triangle has (0, 0, 0) (1, 0, 0) (0, 1, 0) coordinates, when drawn to 2D Texture buffering, the GPU needs to know the coordinates in the 2D coordinate system so that it can know where to draw. We will discuss the transformation of coordinates in the future. We will not discuss it here. For the previous example, we only need to know the information of passing in a vertex and return a vertex information, the Code is as follows:

1 Float4 vs (float4 pos: Position): sv_position
2 {
3 Return Pos;
4 }

The above code is HLSL. We also introduced earlier that its language is like C, passing in a float4 data, position is a declarative string, sv_position has a special declarative meaning. It can be queried in HLSL, that is, it tells the drawing pipeline that it defines a coordinate data. This coordinate is what the GPU needs to know, that is, where to draw.

 

Pixel shader:

In modern displays, the screen is composed of many square grids. These formats are small. We call them pixels. Each pixel contains its own color, they do not need to depend on each other. In fact, when we need to draw a triangle on the screen, we will understand it better if we do not cut the triangle on the screen, as shown in.

 

A triangle contains three vertices. These three vertices are connected together, which is called grating. The GPU first needs to know which pixels need to be presented, and then activates these (inside the triangle) pixels and gives the color value. The pixel shader is used to calculate which pixels need the color. The pixel shader calculates the color and returns it to the drawing pipeline by entering the pixel color value. The pixel pipeline parameters are generally returned by the geometric coloring tool. If there is no geometric coloring tool, for example, the example described in the previous section, it is returned through the vertex coloring tool.

In the vertex shader, A float4 value is created using the sv_position declaration description to return the Coordinate Position of the pixel. This will be used as the input parameter of the pixel shader, so that the current coordinate of the GPU is reached, A color value returned by the pixel shader is also a float4 value, and the description is declared using sv_target to indicate the rendering format of the target. The 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 }

 

Compile the shader:

The code written by the shader is that HLSL is saved in a text file. You can useD3dx11compilefromfile ()Compile the Code as follows:

1 // Create the vertex shader
2 If (Failed (d3dx11compilefromfile ( " Tutorial03.fx " , Null, null, " VS " , " Vs_4_0 " , D3dcompile_enable_strictness, 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_strictness, null, null, & Ppsblob, & Perrorblob, null )))
7 Return False;

 

Bind the shadow:

In this way, we can useVssetshader ()AndPssetshader ()Two methods are used to bind vertex paintors and pixel paintors to the pipeline. When we use the draw method, the vertex Cache Information vertex buffer is transferred to the drawing pipeline for painting, in this way, we have finished learning about the vertex paintors and pixel paintors.

 

Pass this articleArticleIn fact, we have learned that the vertex coloring tool tells the pipeline coordinates, and the pixel coloring tool tells the pipeline color. With these two items, we can draw a picture. Of course, the actual demand cannot be so cheap, and there are still a lot of code to be written. The main hope is that Microsoft can inherit the intelligent prompt of HLSL into, as a result, I think it will be nice to write code like me.

 

Appendix:

Directx11 Pipeline

 

Surface Subdivision (tessellator ):

from the above technical analysis diagram, we can understand the specific functions of the three new units hull shader, tessellator, and domain shader: hull shader is mainly responsible for defining the "deformation" trend of the subdivision level (SLS) and related control points, it should be noted that this deformation is only a small change similar to a curvature change, rather than a large polygon displacement. tessellator is responsible for transmitting the information according to the hull shader, add a polygon to implement the hull shader through "brute force". The most important function of domain shader is to implement model deformation through Texture Control, this is the high-Detail picture we all see in the subdivided surface of dx11.

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.