DirectX 11 Game Programming Learning Note 7:6th Chapter drawing in Direct3D (drawn in Direct3D) (Focus review + Errata)

Source: Internet
Author: User

This article by Harry _ Spider-Man original, reproduced please indicate the source! Have a question Welcome to contact [email protected]

Note: I give the electronic version is more than 700 pages, and the physical book is more than 800 pages, so when I mention the relevant concepts, I will use the chapter number instead of the page number. The same situation is appropriate for the second edition of "Dragon book".

Address of previous issue:

DX 11 Game Programming Learning Notes 6

This chapter should be the longest chapter of the book, may also be the most difficult chapter, so we must be good digestion, study carefully! This chapter is roughly equivalent to chapter 7th and 8th of the second edition of the Dragon book, and adds something else.

Since this chapter is long, I put the problem part of the exercise solution in the next issue.

Key reviews:


We continue to place our learning objectives here:

1. To discover the Direct3D interfaces ' methods for defining, storing, and drawing geometric data. (Found Direct3D interfaces are used to define, store, and draw geometric data. )

2. To learn how to write basic vertex and pixel shaders. (Learn how to write basic vertex shaders and pixel shaders.) )

3. To configure the rendering pipeline with render states. (know how to set the render state for the rendering pipeline.) )

4. Tol earn how the effects framework can is used to logically group shaders and render states into a rendering technique , and how to use the effects framework as a "shader generator". (Know how the effect framework can be used to logically combine shaders and render states into one rendering technique, and how to use the effect framework as a "shader generator"). )

The content of this chapter is too big for the second edition of "Dragon Book", so this issue may rarely refer to the latter.

6.1 Vertices and Input Layouts

This section defines how the vertex format in DX 11 is defined. This is somewhat similar to the method defined by the D3DVERTEXELEMENT9 array in the second edition of the Dragon book, but there are several distinct points:

1. The D3DVERTEXELEMENT9 array finally has a d3ddecl_end () that represents the end of the array, and the D3D11_INPUT_ELEMENT_DESC array used by DX 11 does not have this thing.

2, D3d11_input_element_desc introduced the Semanticname and semanticindex these two things, you can let us customize the vertex structure of the use of each component, not like D3DVERTEXELEMENT9 Only a few predefined D3ddeclusage enumeration type variables can be used in the array.

3, because Semanticname is our own creation of a word, so we can not be like in DirectX 9.0c by defining the use of vertex components as D3ddeclusage_positiont to define the 2D vertices representing screen coordinates; All vertices of 11 are 3-dimensional.

Of course, as mentioned earlier, the content of this section changes too much, the specific people still look at the book bar!

6.2 Vertex Buffers

This section describes three steps to creating a vertex cache (in fact, only two, the third one is omitted). Although the steps seem cumbersome and not well understood, they can actually be seen as an upgrade to the Idirect3ddevice9::createvertexbuffer method in DirectX 9 (and the name of the method does not contain vertex, So it can create any cache, including the index cache to be talked about right away. Beginners feel uncomfortable and can always use the code given by the author, because the code is (basically) the same for most examples of this book.

However, unlike DirectX 9, when the vertex cache is created, it needs to be bound to an input slot in the device so that the render pipeline can be entered with those vertices. This is the function you need to help: void Id3d11devicecontext::iasetvertexbuffers.

We can then call Id3d11devicecontext::D the raw function to draw the vertices. This function is less than the IDirect3DDevice9 of DirectX 9::D rawprimitive A parameter that represents an element type, because this parameter is set in DX11 with another function.

6.3 Indices and Index buffers

As I said earlier, the Createbuffer function of DX 11 can be used to create an index cache, so the content here is omitted a lot. Once you have created the index cache, you also need to bind it to the input assembler stage, and this is done through Id3d11devicecontext::iasetindexbuffer. This function is easier to use than the above id3d11devicecontext::iasetvertexbuffers, after all, a band s, one without.

It is worth noting that the format of the index cache used in this book has always been a 32-bit unsigned integer, whereas in the second edition of "Dragon Book", it is a 16-bit unsigned integer.

If you want to use indexed data when drawing vertices, Id3d11devicecontext::D Raw will be replaced with Id3d11devicecontext::D rawindexed function. This function than IDirect3DDevice9::D rawindexedprimitive less than 3 parameters, so it is more convenient to use.

6.4 Example Vertex Shader

This section describes the basics of vertex shaders. Similar to the second edition of the Dragon book, but there are some syntactic differences.

6.5 Constant Buffers

This section describes what is not used in the second edition of "Dragon book": Cbuffer object. In the second edition of "Long Book", all the effect parameters are tied, on the one hand looks more bloated, on the other hand is not good management. It's all right now!

6.6 Example Pixel Shader

This section is also relatively simple, and the "Dragon book" the second version of the same speech. But there is one important difference: The book says notice that the pixel shader input exactly matches the vertex shaderoutput; This is a requirement. In the second edition of "Long book", the input parameters of the pixel shader do not need to represent the coordinate position of the homogeneous coordinates, that is to say, you can think that float4 PosH in the "Dragon book" in the second edition of the pixel shader as the default input parameters exist.

I also made a special experiment, the pixel shader float4 PosH to remove, and then will pop up an error window to tell you that a connection error occurred.

6.7 Render states

This is where the rendering state is spoken. There is a lot of difference here and DirectX9, because it binds many different rendering states into a struct. The advantage is that it's easier to set up (save the rendering you might use in the first place, then use it again), and the downside is that there are a lot less interesting possibilities (not being able to assign each render state individually, and you obviously can't populate so many render structures).

A few more notable differences:

1. The D3D11_RASTERIZER_DESC structure does not represent a member of shade mode. In DX 9, we can set the shade mode to D3dshade_flat or D3dshade_gouraud to get the flat shading or the GAO Luo coloring effect respectively. Judging from the running results of the sample program, the shade mode of DX 11 is always the GAO Luo coloring mode.

2. The member variables of the D3d11_rasterizer_desc d3d11_fill_mode type are only D3d11_fill_wireframe and d3d11_fill_solid, while DX 9 also has D3dfill_ Point of this type.

In addition, there is a note, this must be noted, otherwise it is likely to be problematic. is when creating a rasterizer State, in zeromemory a d3d11_rasterizer_desc structure, be sure to explicitly Fillmode and the Cullmode Make settings!

6.8 Effects

This section is longer, and there are more things to be aware of.

At the very beginning, the author tells us what we need to do to use Effectsframework. Specifically, here are two things to do:

1. will be d3dx11Effect.h This header file include in your project.

2. will be Effects11.lib and the Effects11d.lib These two library files are connected to your project.

Here are a few things to keep in mind:

1. d3dx11Effect.h header files are located in DirectX sdk\samples\c++\effects11\inc directory.

2, EFFECTS11.LIB  effects11d.lib You need to make your own directx sdk\samples\c++\effects11 This project opens (with vs vs vs should also be possible, respectively, in the release debug debug effects11.lib d letters.

3. in the author's given Common The three files are already in the folder, but make sure you are VS2010 , or it will be an error. In addition I put the VS version of these two library files in the newer version of the Common folder, Welcome to use!

6.8.2 Compiling Shaders

Here are the two steps you need to take to create a effect. Step one is to compile the shader program that is defined in an. fx file. This is done through the D3dx11compilefromfile function. We then use the D3dx11createeffectfrommemory function to create a effect. This is different from DX 9, in which we just need to use a d3dxcreateeffectfromfile function to get it done.

6.8.3 interfacing with Effects from the C + + application

This section is about how variables within an application relate to variables in the. fx file. The content in this is familiar, but the usage has changed a little.

6.8.4 Using Effects to Draw

This section describes the steps to draw using effect. and DX 9 version of the difference is still pretty big. The most obvious is that there are no more beginpass and Endpass functions.

6.8.5 compiling an Effect at Build time

This section is very important! It tells us how to compile effect when we build the project. The article lists three steps. If you're using VS 2010, then follow the book. But if you're using vs 2013, then the steps are different.

The reason for this is that vs 2013 does not offer the Custombuild Tool function for. FX; instead, vs 2013 has a built-in HLSL Compiler; so you need to make some settings for this HLSL Compiler. In the README–DX 11.txt documentation that accompanies each later version of the code, I will describe in detail how the code is used, including the setup methods described in this section. For convenience, I post the section in the documentation related to the latter two steps in the three steps of this section:

7 , in Solution Explorer Right-click the project name, select Add-New Filter , and then name it FX , and then right-click FX , select Add-Existing Item , and then under the project folder , FX all of the sub-folders . FX The file is added in.

8 , right-click each . FX file, select Properties , and then in HLSL Compiler under the section All Options , make the following settings:

----Disable optimaztions Selected as Yes (/od) (This is in Debug option in the mode; Release mode does not need to be selected) ;

----Enable Debugging information Selected as Yes (/zi) (This is in Debug option in the mode; Release mode does not need to be selected) ;

----entrypoint Name empty;

----Object File Name fill in as % (Relativedir) \% (Filename). Fxo

----Shader Model Selected as Shader Model 5 (/5_0)

----Shader Type Selected as Effect (/fx)

Attentive readers will find that there is no setting outputthe assembly listing. In fact, I want to, but I found that regardless of the assembleroutput Set Why, will be wrong. I guess there was a problem with the assembly language that generated the renderer for some reason, but I don't know why it went wrong. I hope to have an expert to answer it!

6.8.6 the Effects Framework as a "Shader Generator"

This section tells us how to use the uniform variable to generate a lot of technique from a few finite vs and PS. Doing so can improve the efficiency of the runtime. So this section is also very important!

6.8.7 What's the Assembly Looks like

I don't understand this section at all. I am impressed by the elegant and sharp note here: The original note can also play this way!

6.11 Shapes Demo

This section describes the methods for generating cylinders (which are actually round tables, including cylinders and cones) and balls, and shows how to combine the vertex caches and index caches of several different objects into one large vertex cache and index cache, respectively. This section is still relatively wonderful, and may not be very good understanding, you may need to combine the following exercises to slowly deepen understanding.

This refers to the concept that we have not yet touched, which is the normal vector, tangent vector, and texture coordinates. But don't worry, we'll be in touch with this later. It is also worth mentioning that because the vertex defined in GeometryGenerator.h contains these things that we do not have, and does not contain the color components we have been using, we need to make some conversions.

Read Long Shu Second edition of the classmate may have doubts: in DX9, we can use the D3dxcreatesphere function to establish the spherical surface, but also can use the D3dxcreatecylinder function to build cylindrical face (Round table), then DX 11 there is no similar function? Unfortunately, it does not seem to be the reason why the author wants to open up this section to tell the story. But that's okay, so we can see more clearly how these functions work, and on the other hand these functions are more humane-their vertices contain more components, such as texture coordinates, while in DX 9 we use the built-in D3dxcreatecylinder After you create a cylindrical face, you also need to perform a conversion operation to have its vertices contain texture coordinates, and the D3dxcreatecylinder function generates cylindrical faces that are arranged along the y axis, rather than being arranged along the z-axis as in DX 9, and more humane.

In addition, this section also describes another method of constructing a spherical surface: the Geosphere method. This is a good approach, but since its number of polygons is growing exponentially, we should not use excessively high segments, and there is less freedom. The code given here actually looks at it, and the key is actually the subdivide function, which has the ability to subdivide vertices and provide indexing for the new vertices.

6.13 Dynamic Vertex Buffers

This section is about dynamic vertex caching. In fact, this is more advanced, in the back of the particle system will be described in detail, and now we have an impression on the line.




1. The interfaces in the first article of the objective of the introductory part of this chapter should be interfaces '.

2. Section 6.2 begins by saying that to create a vertex cache, you need three steps. But later the author forgot to say how the third step was used. I checked the SDK documentation, actually it is very simple to use. This is exactly the case:

HRESULT Createbuffer (


[In] Constd3d11_subresource_data *pinitialdata,

[out] Id3d11buffer **ppbuffer


Explanation of the parameters;

Pdesc [In]


Pointer to a buffer description.

Pinitialdata [In]


Pointer to the initialization data; Use NULL for toallocate space only (with the exception, that it cannot being null if the Usageflag is d3d11_usage_immutable).

Ppbuffer [out]


Address of a pointer to the buffer created. Setthis parameter to NULL to validate the other input parameters (S_falseindicates a pass).

3, section 6.2 describes the Iasetvertexbuffers function, the following 4th of the narrative there is a problem, that is, the last sentence a stride is the size, in bytes, of an element in the CORRESPONDINGV Ertex buffer should be a stride is the size, in bytes, and of all elements in thecorresponding vertex buffer.

4, Section 6.3 explains the Drawindexed function, the third parameter basevertexlocation interpretation of "before the vertices is fetched" suspected "after the vertices is fetched" 。

5, section 6.6, the first code of the PS in the return statement is wrong, should not be a pin. Color, which should be color.

6, section 6.7 gives the prototype of the Rssetstate function after the sentence of the following code shows how to create a rasterize state ... The rasterize should be rasterizer.

7, section 6.7 Close to the end of the code block the first sentence of the comment:

Create RenderState objects as initialization time

The as in should be at.

8. D3DX11Effects.lib and D3DX11EffectsD.lib in section 6.8 should be changed to Effects11.lib and Effects11d.lib respectively (and generated in debug mode. lib The file is still called Effects11.lib; you need to manually add a D at the end. )

9. Section 6.8 at the beginning of the second natural paragraph in the "This" book, we'll only have the using the effects framework as is should be changed in Thisbook, we'll only have using The effects framework as it is.


This is the time to be here! Finally I give the example code for this chapter. Sample code has two parts, one is the author, the other is I processed, please feel free to use! (The subsequent release of the sample code follows this pattern as well.) ):

The 6th Chapter Example code

DirectX 11 Game Programming Learning Note 7:6th Chapter drawing in Direct3D (drawn in Direct3D) (Focus review + Errata)

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: 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.