Jump to content
  • Advertisement

[Directx11][C++] Help with hardware instancing

Recommended Posts

I'm a total beginner with Directx/3D programming. I need help with implementing hardware instancing on Directx 11. I'm trying to render multiple cubes on the screen, to create some sort of Minecraft-esque voxel engine. The problem is, I don't know where to start to achieve this. This is how my "render frame" function looks:

void RenderFrame(void)
	D3DXMATRIX matView, matProjection;
	D3DXMATRIX matFinal;

	// create a view matrix
		&D3DXVECTOR3(0.0f, 9.0f, 24.0f),   // the camera position
		&D3DXVECTOR3(0.0f, 0.0f, 0.0f),    // the look-at position
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));   // the up direction

// create a projection matrix
		(FLOAT)D3DXToRadian(45),                    // field of view
		1.0f,                                       // near view-plane
		100.0f);                                    // far view-plane

// create the final transform
	matFinal = matView * matProjection;

	// clear the back buffer to a deep blue
	devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));

	// clear the depth buffer
	devcon->ClearDepthStencilView(zbuffer, D3D11_CLEAR_DEPTH, 1.0f, 0);

	// select which vertex buffer to display
	UINT stride = sizeof(VERTEX);
	UINT offset = 0;
	devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
	devcon->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R32_UINT, 0);

	// select which primtive type we are using

	// draw the Hypercraft
	devcon->UpdateSubresource(pCBuffer, 0, 0, &matFinal, 0, 0);
	devcon->DrawIndexed(24, 0, 0);

	// switch the back buffer and the front buffer
	swapchain->Present(0, 0);

Notice that there's a single vertex buffer containing the verteces of a cube, and index buffer containing its indeces. I want to render many (5000+) cubes on the screen at once on a single draw call, without performance issues, so I know instancing is the way to go, but I don't know how to implement it in my code. What changes do I need to do to my code in order to display multiple instances of the cube?

Thanks in advance!

Share this post

Link to post
Share on other sites

A good way to learn about this is the Rastertek series.

Alternatively, the following book from Frank Luna is excellent as a way to learn.

A very high level summary is that you have 2 buffers:

1. Containing the vertices for the model in model space
2. Contains the transform to apply to the model (the instances)

Your vertex shader then applies the values from #2 to each of the input vertices

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • 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!