Jump to content
  • Advertisement
Sign in to follow this  

DX11 how does instancing10 example works?

This topic is 2123 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 I follow what the example do and I work it out?But I still don't know why we should create two vertex buffer . One include vertex data and one include the matrix??

Also ,how can i update my matrix??



When i read the example instancing10 in dx10;I can not understand how it works.

How to define the Vertex. Does it need to include the matrix.Why does it create two vertex buffer for instancing.

I want to do it in dx11.How should I do??

Edited by gavlge

Share this post

Link to post
Share on other sites

 Hi there! wink.png


How to define the Vertex. 



A vertex is what you define it as. Sounds weird, doesn't it?


It all depends on what kind of data you want the GPU to process, let us look at this example:


You want to send 3 vertices, all containing their positions and colors. You start by defining the structure, like so:



struct MyVertex
    D3DXVECTOR3 Position; // or float x, y, z;
    D3DXCOLOR Color; // or float r, g, b, a;

Then when after creating your shader, you'll need to tell the vertex shader what data is there in each individual vertex and how the data is aligned, like so:


	if (dev->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout) != S_OK)
		//Error stuff...

Now Directx operates with Buffers (Their own special format), so you'll also need to create some buffers, depending on what you wish to draw, an index buffer may also be required:

// create the vertex buffer
ZeroMemory(&bd, sizeof(bd));

bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = (sizeof(D3DXVECTOR3) + sizeof(D3DXCOLOR)) * (vertexCount); // vertexCount = 3 in this case
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
dev->CreateBuffer(&bd, NULL, &pVertexBuffer);

// copy the vertices into the buffer
devcon->Map(pVertexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);    // map the buffer
memcpy(ms.pData, vertexArray, sizeof(vertexArray));                 // copy the data
devcon->Unmap(pVertexBuffer, NULL);

// create the index buffer
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(DWORD) * (indexCount);
bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;

dev->CreateBuffer(&bd, NULL, &indexBuffer);

devcon->Map(indexBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);    // map the buffer
memcpy(ms.pData, indexArray, sizeof(indexArray));                   // copy the data
devcon->Unmap(indexBuffer, NULL);

Now your data is ready to be sent to the GPU. So when rendering your object, you should send the Vertex and the Index buffers, then draw it:

UINT stride = sizeof(D3DXVECTOR3) + sizeof(D3DXCOLOR);
UINT offset = 0;

devcon->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
devcon->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);

devcon->DrawIndexed(indexCount, 0, 0);

PS. The parameters WILL depend on what you wish to do.




Does it need to include the matrix.



Again, completely depends on what you want to do. But normally you would need to send the Model (Defines the location, rotation and scalation), View (Relative camera matrix), Projection (Shape of your viewport). But these are all fancy names, you could also combine it to one matrix, and maybe call it gModelViewProjection in your shader. The vertex buffer itself doesn't need the matrix, you would need to send it to the shader as a global constant.




Why does it create two vertex buffer for instancing.


The instance buffer is a complete different buffer, that is sent along with the vertex buffer (Which might be why you said "two vertex buffers"), which can define whatever you want, and as an example, it could contain a D3DXVECTOR3 (Position), and a float (Something), if you want this data to be accessible by the shader, you'll need to change the input layout of the shader, Example:

// Add this line in the array


// And remember to increase the number of slots (I don't remember what they are called) by 1.

An example of sending this:

unsigned int strides[2];
unsigned int offsets[2];
ID3D11Buffer* bufferPointers[2];

// Set the buffer strides.
strides[0] = sizeof(MESH_STRUCT); 
strides[1] = sizeof(D3DXVECTOR3) + sizeof(float); 

// Set the buffer offsets.
offsets[0] = 0;
offsets[1] = 0;

// Set the array of pointers to the vertex and instance buffers.
bufferPointers[0] = v;
bufferPointers[1] = b;

// Set the vertex buffer to active in the input assembler so it can be rendered.
devcon->IASetVertexBuffers(0, 2, bufferPointers, strides, offsets);
devcon->IASetIndexBuffer(i, DXGI_FORMAT_R32_UINT, 0);

PS. Why doesn't GameDev allow me to create an empty line after a code block, it becomes really messy...


I hope this helps you smile.png


Edited by Migi0027

Share this post

Link to post
Share on other sites

Please don't edit your first question to ask a new question and give a response of what I wrote, it confuses me, instead leave a new post down here.


I really recommend this site:




I know some people say that it isn't top quality and/or misses some fundamental parts, but it was the site which got me started with Directx.



Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!