DX12 Long Shu the 6th chapter exercises

Source: Internet
Author: User

1.

{        { "POSITION",0, Dxgi_format_r32g32b32_float,0,0, D3d12_input_classification_per_vertex_data,0 },        { "TANGENT",0, Dxgi_format_r32g32b32_float,0, A, D3d12_input_classification_per_vertex_data,0 },        { "NORMAL",0, Dxgi_format_r32g32b32_float,0, -, D3d12_input_classification_per_vertex_data,0 },        { "TEXTURE",0, Dxgi_format_r32g32_float,0, $, D3d12_input_classification_per_vertex_data,0 },        { "TEXTURE",1, Dxgi_format_r32g32_float,0, -, D3d12_input_classification_per_vertex_data,0 },        { "COLOR",0, Dxgi_format_r8g8b8a8_sint,0, the, D3d12_input_classification_per_vertex_data,0 },};

2.

First, there are some things that are prepared, so it's not intuitive, but it's good for you to find the code location.

In the front of the BoxApp.cpp, the structure is changed first:

// struct Vertex // {//    XMFLOAT3 Pos; //     XMFLOAT4 Color; // }; // Change End struct vposdata{    XMFLOAT3 Pos;}; struct vcolordata{    XMFLOAT4 Color;};

Then the D3d12_input_element_desc object is changed to:

Minputlayout =    {        "POSITION"0000 },        "COLOR"0100 // Change end    };

In the application there is a geometry class Meshgeometry

This class is used to record the rendering of aggregate data, as well as to assist in creating buffer data and methods, which are modified:

// microsoft::wrl::comptr<id3dblob> vertexbuffercpu = nullptr;     // Change End    microsoft::wrl::comptr<id3dblob> vertexposbuffercpu = nullptr;    Microsoft::wrl::comptr<ID3DBlob> vertexcolorbuffercpu = nullptr;
// microsoft::wrl::comptr<id3d12resource> vertexbuffergpu = nullptr;     // Change End    microsoft::wrl::comptr<id3d12resource> Vertexposbuffergpu = nullptr;    Microsoft::wrl::comptr<ID3D12Resource> vertexcolorbuffergpu = nullptr;
 // microsoft::wrl::comptr<    id3d12resource> vertexbufferuploader = nullptr;  // change end  MICROSOFT::WRL::    comptr<id3d12resource> vertexposbufferuploader = nullptr; Microsoft::wrl::comptr  <ID3D12Resource> vertexcolorbufferuploader = nullptr; 
// Data about the buffers.     // UINT vertexbytestride = 0;     // UINT vertexbufferbytesize = 0;     // Change End    0 ;     0 ;     0 ;     0;

//D3d12_vertex_buffer_view Vertexbufferview () const//{    //D3d12_vertex_buffer_view VBV; //VBV.    Bufferlocation = Vertexbuffergpu->getgpuvirtualaddress (); //VBV.    Strideinbytes = Vertexbytestride; //VBV.    sizeInBytes = vertexbufferbytesize; //return VBV; //}    //Change EndD3d12_vertex_buffer_view Vertexposbufferview ()Const{D3d12_vertex_buffer_view vbv; Vbv. Bufferlocation= vertexposbuffergpu->getgpuvirtualaddress (); Vbv. Strideinbytes=Vertexposbytestride; Vbv. sizeInBytes=vertexposbufferbytesize; returnVBV; } D3d12_vertex_buffer_view Vertexcolorbufferview ()Const{D3d12_vertex_buffer_view vbv; Vbv. Bufferlocation= vertexcolorbuffergpu->getgpuvirtualaddress (); Vbv. Strideinbytes=Vertexcolorbytestride; Vbv. sizeInBytes=vertexcolorbufferbytesize; returnVBV; }
void disposeuploaders ()    {        //vertexbufferuploader = nullptr;         // Change End        Vertexposbufferuploader = nullptr;         = nullptr;         = nullptr;    }
These describe the structure of the input data after processing is the work of the rendering process, d3d12 create buffer when the first in the main memory to build a data segment for future collision detection, pick a class of functions, and then in memory built buffer, and this buffer is very fastidious, It is a memory that can not be arbitrarily modified by the CPU, in order to maintain the buffer and CPU isolation, it will also build an intermediate memory called the upload buffer. The following three places are changed:

This is actually about preparing some basic data, and giving us the second and third places to use

// const UINT vbbytesize = (UINT) vertices.size () * sizeof (VERTEX);     // Change End    Const sizeof (vposdata);     Const sizeof (Vcolordata);

The following paragraph is the main memory data section, the reason is called Cpublob to indicate that it is for the CPU

// throwiffailed (D3dcreateblob (Vbbytesize, &mboxgeo->vertexbuffercpu));     // CopyMemory (Mboxgeo->vertexbuffercpu->getbufferpointer (), Vertices.data (), vbbytesize);     // Change End    Throwiffailed (D3dcreateblob (vpbbytesize, &mBoxGeo->vertexposbuffercpu));    CopyMemory (Mboxgeo->vertexposbuffercpu->getbufferpointer (), Verticespos.data (), vpbByteSize);     &mBoxGeo->vertexcolorbuffercpu));    CopyMemory (Mboxgeo->vertexcolorbuffercpu->getbufferpointer (), Verticescolor.data (), vcbByteSize);

The following paragraph blends the uploadbuffer and the final vertexbuffer:

// Mboxgeo->vertexbuffergpu = D3dutil::createdefaultbuffer (Md3ddevice.get (),    //     Mcommandlist.get (), Vertices.data (), vbbytesize, Mboxgeo->vertexbufferuploader);     // Change End    Mboxgeo->vertexposbuffergpu = D3dutil::createdefaultbuffer (Md3ddevice.get (),        mcommandlist.get (), Verticespos.data (), Vpbbytesize, Mboxgeo-vertexposbufferuploader);    Mboxgeo->vertexcolorbuffergpu = d3dutil::createdefaultbuffer (Md3ddevice.get (),        Mcommandlist.get (), Verticescolor.data (), vcbbytesize, Mboxgeo->vertexcolorbufferuploader);

The above is the code for building aggregates and their data, and if you are rendering multiple objects with multiple aggregates, you should also pay attention to modifying the position of the vertex positions of those aggregates in the entire memory:

// mboxgeo->vertexbytestride = sizeof (Vertex);     // mboxgeo->vertexbufferbytesize = vbbytesize;     // Change End    sizeof (vposdata);    Mboxgeo->vertexposbufferbytesize = vpbbytesize;    Mboxgeosizeof(vcolordata);    Mboxgeo->vertexcolorbufferbytesize = vcbbytesize;

And then to the real draw stage, set the buffer to render Pipleline

// mcommandlist->iasetvertexbuffers (0, 1, &mboxgeo->vertexbufferview ());     // Change End    Mcommandlist->iasetvertexbuffers (01, &mBoxGeo->Vertexposbufferview ());    Mcommandlist->iasetvertexbuffers (11, &mboxgeo->vertexcolorbufferview ());

The above are ready to enter your data, my data is hardcore into the code, very ugly:

//Std::array<vertex, 8> vertices =//  {  //Vertex ({XMFLOAT3 ( -1.0f, -1.0f, -1.0f), XMFLOAT4 (Colors::white)}),//Vertex ({XMFLOAT3 ( -1.0f, +1.0f, -1.0f), XMFLOAT4 (Colors::black)}),//Vertex ({XMFLOAT3 (+1.0f, +1.0f, -1.0f), XMFLOAT4 (colors::red)}),//Vertex ({XMFLOAT3 (+1.0f, -1.0f, -1.0f), XMFLOAT4 (Colors::green)}),//Vertex ({XMFLOAT3 ( -1.0f, -1.0f, +1.0f), XMFLOAT4 (Colors::blue)}),//Vertex ({XMFLOAT3 ( -1.0f, +1.0f, +1.0f), XMFLOAT4 (Colors::yellow)}),//Vertex ({XMFLOAT3 (+1.0f, +1.0f, +1.0f), XMFLOAT4 (Colors::cyan)}),//Vertex ({XMFLOAT3 (+1.0f, -1.0f, +1.0f), XMFLOAT4 (Colors::magenta)})//  }; //Change EndStd::array<vposdata,8> Verticespos ={vposdata ({XMFLOAT3 (-1.0f, -1.0f, -1.0f)}), Vposdata ({XMFLOAT3 (-1.0f, +1.0f, -1.0f)}), Vposdata ({XMFLOAT3 (+1.0f, +1.0f, -1.0f)}), Vposdata ({XMFLOAT3 (+1.0f, -1.0f, -1.0f)}), Vposdata ({XMFLOAT3 (-1.0f, -1.0f, +1.0f)}), Vposdata ({XMFLOAT3 (-1.0f, +1.0f, +1.0f)}), Vposdata ({XMFLOAT3 (+1.0f, +1.0f, +1.0f)}), Vposdata ({XMFLOAT3 (+1.0f, -1.0f, +1.0f) })    }; Std::array<vcolordata,8> Verticescolor ={vcolordata ({XMFLOAT4 (Colors::white)}), Vcolordata ({XMFLOAT4 (Colors::black)}), Vcolordat A ({XMFLOAT4 (colors::red)}), Vcolordata ({XMFLOAT4 (Colors::green)}), Vcolordata ({XMFLOAT4 (colors::blue)) }), Vcolordata ({XMFLOAT4 (Colors::yellow)}), Vcolordata ({XMFLOAT4 (Colors::cyan)}), Vcolordata ({X MFLOAT4 (Colors::magenta)});

Then recompile and run it.

DX12 Long Shu the 6th chapter exercises

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.