# How Often do you change Vertex/Index/etc Buffers?

So im still learning but I think i've got a grasp of most things DX11 (Well....beginner things) And I REALLY do apologize for all the questions.....but I'd rather understand EVERYTHING before I move on. I have 2 main functions (well 3 technically) that set up everything. I have Initgraphics(), InitD3D(), and Initpipeline()

D3D sets up the swap chain, viewport, depth buffer and back buffer. Initgraphics() Creates the Verticies (for the cube/whatever im drawing) and then creates the vertex buffer and copies it in.

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

bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VERTEX) * 24;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

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

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


it also does the same thing for the Index buffer.

I also have initpipeline() (see code below)

// compile the shaders
ID3D10Blob *VS, *PS;

// create the input element object
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

// use the input element descriptions to create the input layout
dev->CreateInputLayout(ied, 3, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devcon->IASetInputLayout(pLayout);

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

bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = 176;
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

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

devcon->VSSetConstantBuffers(0, 1, &pCBuffer);


Which from my understanding, compiles the shaders, makes the shader objects, creates the elements object and descriptions (Can someone explain what exactly this is?) and the Constant Buffer (which I only somewhat understand).

I guess my question(s) is.....when do these things need to change. I have a grip on the vertex and index buffers.....like I understand WHAT they are, but when would you change them? Same goes for Depth buffer, when would I ever want to change that (I understand back buffer). I know in my RenderFrame() function I select which ones to use using: right?

   devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
devcon->IASetIndexBuffer(pIBuffer, DXGI_FORMAT_R32_UINT, 0);


Secondly, Do I only need to compile the shaders once and make the objects once? (unless I use different shader files presumably...maybe one for lights for one object and one for textures for a different object)....and what the heck does this:

// create the input element object
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
};

// use the input element descriptions to create the input layout
dev->CreateInputLayout(ied, 3, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout);
devcon->IASetInputLayout(pLayout);


Stuff mean?. Also what is a Stencil Buffer.....im not really sure I understand why you create it (Does it ever need to change for that matter?)

• Instead of L"text string" use the TEXT macro (so TEXT("text string")). TEXT prepends an L if need be (ie if _UNICODE is defined) Jul 29, 2011 at 19:12

I'd recommend reading the Direct3D documentation as it will do a fantastic job at explaining most of your questions. For example, the page on Constant Buffers.

Stencil Buffers are described very elequantly on Wikipedia. You only need to create one if you plan on doing operations that involve creating and using masks to control the pixels to apply an operation to (much like a stencil in painting).

As with most API calls, the less you can call them the better. Most static resources (i.e. those that don't change during the course of the application running) are created once (for vertex buffers this can also involve populating with the vertices) and then set whenever they're needed. There are dynamic resources (again such as vertex buffers) that you might want to create the resource, but then you may change the contents of on a regular basis (such as for rendering UI or dynamic particles among other things).

You might want to change to a different depth buffer if you want to render to a different render target such as a dynamic cubemap, reflection, etc.

That last part where you create the InputLayout, you're basically telling D3D where to find the vertex data inside the vertex buffer. You can put whatever you want in the buffer, all D3D asks is you tell it how many bytes it needs to skip to the next vertex and what offset important bits are from the start of the vertex (such as position, normal and texture coordinates).

I am a newbie to directx, but as I have been learning it I find it helpful that I do already know a little bit about video cards and that might be relevant.

Directx makes you feel like you are creating typical programming resources, but I expect under the hood the important copies of those resources reside on the video card, i.e. your vertex buffer is probably created in memory, then copied to the video cards memory when you set it as the current vertex buffer. Your effect is probably loaded onto the video card.

Moving stuff to and from the video card causes a certain performance hit. That's probably why directx is set up as a state machine, because by and large it represents states of your video card.