Simple triangular drawing of Directx11 HelloWorld

Source: Internet
Author: User
Tags bind

Learn the SDK and make a simple triangular drawing in d3d11 to familiarize yourself with the Direct11 drawing process. This article refers to the basic demo of the previous Directx11.

The relatively Directx11 basic demo, which draws a triangle in Directx11, initializes the work primarily in Initdevice () 1: Create a vertex shader vertex shader;2: Describe our own vertex format d3d11_ Input_element_desc;3: Create a pixel shader pixel shader;4: Description, create a buffer that stores the vertices we want to draw and bind it to pipeline.

Then in render section 1: Specify the pixel shader and vertex shader we want to invoke; 2: Call Draw ().

The following is a detailed description of its initialization work in Initdevice ().

HRESULT Initdevice ()

{

//======================

The basic initialization work of Initdevice discussed earlier, mainly using D3d11createdeviceandswapchain () to create Idxgiswapchain,id3d11device,id3d11devicecontext ;

............................................................

//=======================

The following begins the work of initializing the basic vertex drawing. /

(1) Creating a vertex shader

(1.1) The vertex shader code in the FX file is compiled with the function d3dx11compilefromfile (), and the compiled binary code exists in id3dblob*.

id3dblob* Verrorblob;

id3dblob* Pvsblob = NULL;

hr = D3dx11compilefromfile (L "basic.fx", NULL, NULL, "VS", "Vs_4_0",

Dwshaderflags, 0, NULL, &PVSBLOB, &VERRORBLOB, NULL);

(1.2) creates vertex shaderwith compiled shader code through Id3d11device::createvertexshader ().

hr = G_pd3ddevice->createvertexshader (Pvsblob->getbufferpointer (), Pvsblob->getbuffersize (), NULL, & ; g_pvertexshader);

2: use D3d11_input_element_desc to describe our own vertex format (very similar to D3d9 's D3dvertexelement9). It then binds the vertex format to pipeline and activates the initialization of this vertex format. The Sdk:d3d11_input_element_desc of reference D3D is: A description of a single ELEMENT for the Input-assembler stage. Let's look at the code in detail below.

(2.1) Define the input vertex format D3d11_input_element_desc

D3d11_input_element_desc layout[] =
{
{"POSITION", 0, dxgi_format_r32g32b32_float, 0, 0, d3d11_input_per_vertex_data, 0},
};
UINT numelements = ARRAYSIZE (layout);

(2.2) then create an interface that describes the above vertex format interface:id3d11inputlayout . (PS: Do not know this explanation is correct, the SDK explanation is D3d11inputlayout:an Input-layout interface accesses the input data for the Input-assembler Stage)

hr = g_pd3ddevice->createinputlayout (layout, numelements, Pvsblob->getbufferpointer (),
Pvsblob->getbuffersize (), &g_pvertexlayout);

The code for the vertex shader has finished its function, releasing it.

Pvsblob->release ();

(2.3) Call Iasetinputlayout to bind the created id3dinputlayout above to the Input-assembler stage . (Do not understand the Input-assembler stage can read the explanation of the SDK, or see my last appendix)

G_pimmediatecontext->iasetinputlayout (g_pvertexlayout);

3: Create a pixel shader

///(3.1) is similar to a vertex shader by compiling the pixel shader code in the FX file with the function d3dx11compilefromfile () to present the compiled binary code The id3dblob* .

id3dblob* Perrorblob;

id3dblob* Psblob = NULL;

hr = D3dx11compilefromfile (L "basic.fx", NULL, NULL, "PS", "Ps_4_0",

Dwshaderflags, 0, NULL, &PSBLOB, &PERRORBLOB, NULL);

(3.2) using id3d11device::createpixelshader () to create pixel shader with compiled pixel shader code

hr = G_pd3ddevice->createpixelshader (Ppsblob->getbufferpointer (), Ppsblob->getbuffersize (), NULL , &g_ppixelshader);

Freeing storage space for shader code
Ppsblob->release ();

//4: Create a buffer to store the vertices we want to draw

//(4.1) Define the vertex structure that we understand (C + + understands), here the main position. and create an array that specifically describes several vertices.

struct SIMPLEVERTEX
{
XMFLOAT3 Pos;
};
Simplevertex vertices[] =
{
XMFLOAT3 (0.0f, 0.5f, 0.5f),
XMFLOAT3 (0.5f, -0.5f, 0.5f),
XMFLOAT3 ( -0.5f, -0.5f, 0.5f),
};

//(4.2) Describe and create a cache id3d11buffer to store our vertices. we'll use D3d11_buffer_desc and d3d11_subresource_data to describe the id3d11buffer we're going to create separately.
D3D11_BUFFER_DESC BD;
ZeroMemory (&BD, sizeof (BD));
Bd. Usage = D3d11_usage_default;
Bd. Bytewidth = sizeof (SIMPLEVERTEX) * 3;
Bd. Bindflags = D3d11_bind_vertex_buffer;
Bd. cpuaccessflags = 0;
d3d11_subresource_data InitData;
ZeroMemory (&initdata, sizeof (InitData));
Initdata.psysmem = vertices;
hr = g_pd3ddevice-createbuffer(&BD, &initdata, &g_pvertexbuffer);

(4.3) use id3d11devicecontext::iasetvertexbuffers () to set the vertex cache we defined above ( I don't know if that's right , the SDK says: Bind an Array of vertex buffers to the Input-assembler stage).

UINT stride = sizeof (SIMPLEVERTEX);
UINT offset = 0;
G_pimmediatecontext->iasetvertexbuffers(0, 1, &g_pvertexbuffer, &stride, &offset);

5: Set the topology between our vertices.

G_pimmediatecontext->iasetprimitivetopology (d3d11_primitive_topology_trianglelist);

}

The remaining steps are in the render () function of the periodic call.

void Render ()
{
Clear the back buffer
Float Clearcolor[4] = {0.0f, 0.125f, 0.3f, 1.0f}; Red,green,blue,alpha
G_pimmediatecontext->clearrendertargetview (G_prendertargetview, Clearcolor);

//Assign a vertex shader to the device

G_pimmediatecontext->vssetshader (G_pvertexshader, NULL, 0);

Assigning a pixel shader to a device
G_pimmediatecontext->pssetshader (G_ppixelshader, NULL, 0);
G_pimmediatecontext->draw (3, 0);

Present the information rendered to the back buffer to the front buffer (the screen)
G_pswapchain->present (0, 0);
}

The above is my summary of the process of simple drawing in d3d11. The final accompanying Input-assembler stage official explanation, although is d3d10 but 11 of the same.

Input-assembler Stage (Direct3D)

The Direct3D API separates functional areas of the pipeline into stages; The first stage of the pipeline is the Input-assembler (IA) stage.

The purpose of the Input-assembler stage is to read primitive data (points, lines and/or triangles) from user-filled Buffe RS and assemble the data into primitives so is used by the other pipeline stages. The IA stage can assemble vertices into several different primitive types (such as line lists, triangle strips, or primiti Ves with adjacency). New primitive types (such as a line list with adjacency or a triangle list with adjacency) has been added to the Geometry shader.

There is a few steps necessary to initialize the Input-assembler (IA) stage. For example, you need to create the buffer resources with the vertex data, the pipeline needs, tell the IA stage where the Buffers is and what type of data they contain, and specify the type of primitives to assemble from the data.

The basic steps involved in setting up the IA stage, shown in the following table, is covered in this topic.

Step Description
Create Input Buffers Create and initialize input buffers with input vertex data.
Create the Input-layout Object Define How the vertex buffer data would be streamed to the IA stage by using a Input-layout object.
Bind Objects to the Input-assembler Stage Bind the created objects (input buffers and the Input-layout object) to the IA stage.
Specify the Primitive Type Identify how the vertices is assembled into primitives.
Call Draw Methods Send the data bound to the IA stage through the pipeline.

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.