# DirectX10 Tutorial 4: Indexed Buffers and Depth Testing

Okay so it’s been a while since my last tutorial, and I apologize for that. We dealt with textures in the last tutorial, and many of you might be wondering while I handled that so early? Well mainly because D3D 10 isn’t exactly an API designed for beginners, so a critical feature required for any scene rendering (depth testing or z-buffering) is done in D3D by use of a depth stencil texture, covering textures before depth testing makes sense in this case. Remember guys I’m not going to spoon feed you, these tutorials expect you to read the SDK docs for details on the variable types and the methods, these tutorials are just to give you a running start.

Before I get to Depth Testing, let’s draw something a little more complicated that a quad, how about a cube. Using the same method as in tutorial 3 the code to draw a six sided cube is as follows:

```//CUBE DRAW METHOD 1
//------------------------------------------------------------

//lock vertex buffer for CPU use

//vertices for a cube
v = vertex( D3DXVECTOR3(-1,-1,-1), D3DXVECTOR4(1,0,0,1) );
v = vertex( D3DXVECTOR3(-1,1,-1), D3DXVECTOR4(0,1,0,1) );
v = vertex( D3DXVECTOR3(1,-1,-1), D3DXVECTOR4(0,0,1,1) );
v = vertex( D3DXVECTOR3(1,1,-1), D3DXVECTOR4(1,1,0,1) );
v = vertex( D3DXVECTOR3(1,-1,-1), D3DXVECTOR4(1,0,0,1) );
v = vertex( D3DXVECTOR3(1,1,-1), D3DXVECTOR4(0,1,0,1) );
v = vertex( D3DXVECTOR3(1,-1,1), D3DXVECTOR4(0,0,1,1) );
v = vertex( D3DXVECTOR3(1,1,1), D3DXVECTOR4(1,1,0,1) );
v = vertex( D3DXVECTOR3(-1,-1,1), D3DXVECTOR4(1,0,0,1) );
v = vertex( D3DXVECTOR3(-1,1,1), D3DXVECTOR4(0,1,0,1) );
v = vertex( D3DXVECTOR3(-1,-1,-1), D3DXVECTOR4(0,0,1,1) );
v = vertex( D3DXVECTOR3(-1,1,-1), D3DXVECTOR4(1,1,0,1) );
v = vertex( D3DXVECTOR3(-1,-1,1), D3DXVECTOR4(1,0,0,1) );
v = vertex( D3DXVECTOR3(-1,1,1), D3DXVECTOR4(0,1,0,1) );
v = vertex( D3DXVECTOR3(1,-1,1), D3DXVECTOR4(0,0,1,1) );
v = vertex( D3DXVECTOR3(1,1,1), D3DXVECTOR4(1,1,0,1) );
v = vertex( D3DXVECTOR3(-1,-1,1), D3DXVECTOR4(1,0,0,1) );
v = vertex( D3DXVECTOR3(-1,-1,-1), D3DXVECTOR4(0,1,0,1) );
v = vertex( D3DXVECTOR3(1,-1,1), D3DXVECTOR4(0,0,1,1) );
v = vertex( D3DXVECTOR3(1,-1,-1), D3DXVECTOR4(1,1,0,1) );
v = vertex( D3DXVECTOR3(-1,1,-1), D3DXVECTOR4(1,0,0,1) );
v = vertex( D3DXVECTOR3(-1,1,1), D3DXVECTOR4(0,1,0,1) );
v = vertex( D3DXVECTOR3(1,1,-1), D3DXVECTOR4(0,0,1,1) );
v = vertex( D3DXVECTOR3(1,1,1), D3DXVECTOR4(1,1,0,1) );

pVertexBuffer-&gt;Unmap();

//send vertices down pipeline
for( UINT p = 0; p &lt; techDesc.Passes; p++ )
{
//apply technique
pBasicTechnique-&gt;GetPassByIndex( p )-&gt;Apply( 0 );

pD3DDevice-&gt;Draw( 4, 0 );
pD3DDevice-&gt;Draw( 4, 4 );
pD3DDevice-&gt;Draw( 4, 8 );
pD3DDevice-&gt;Draw( 4, 12 );
pD3DDevice-&gt;Draw( 4, 16 );
pD3DDevice-&gt;Draw( 4, 20 );
}
```

The code above produces the following cube (I’ve added rotation and moved the camera – take a look at the code for more details), noticed the depth problem, whatever face got drawn last is on top irrespective of whether it is obscure by another face. Image 1 – No Depth Testing

So as you can see to draw our cube we need to define each vertex and add it to the vertex buffer, then call the draw method 6 times. Each draw call draws a single triangle list with 4 vertices, drawing each face. So in this method we send 24 vertices down the pipeline and use 4 draw calls. This is a little crazy just to draw a single cube with only 8 vertices. There must be a simple more efficient method of doing this and there is: indexing.

## Index Buffers

What indexing does is let you pass a vertex buffer containing all the key vertices down the pipeline, and also a list of the order the vertices must be drawn in. So for our cube you’ll send the 8 vertices down the pipeline followed by a list of the order to draw them in. It’ll be a bit clearer once you see the code and of course read the index buffer sections in the SDK docs.

```//lock vertex buffer for CPU use

//vertices for a cube
v = vertex( D3DXVECTOR3(-1,1,-1), D3DXVECTOR4(1,0,0,1) ); //front top left
v = vertex( D3DXVECTOR3(1,1,-1), D3DXVECTOR4(0,1,0,1) ); //front top right
v = vertex( D3DXVECTOR3(-1,-1,-1), D3DXVECTOR4(0,0,1,1) ); //front bottom left
v = vertex( D3DXVECTOR3(1,-1,-1), D3DXVECTOR4(1,1,0,1) ); //front bottom right
v = vertex( D3DXVECTOR3(-1,1,1), D3DXVECTOR4(1,0,0,1) ); //back top left
v = vertex( D3DXVECTOR3(1,1,1), D3DXVECTOR4(0,1,0,1) ); //back top right
v = vertex( D3DXVECTOR3(-1,-1,1), D3DXVECTOR4(0,0,1,1) ); //back bottom left
v = vertex( D3DXVECTOR3(1,-1,1), D3DXVECTOR4(1,1,0,1) ); //back bottom right

pVertexBuffer-&gt;Unmap();

//create indexes for a cube
unsigned int* i = NULL;

//front face
i = 2;
i = 0;
i = 3;
i = 1;
i = 0xffffffff; //start new strip

//right face
i = 3;
i = 1;
i = 7;
i = 5;
i = 0xffffffff;

//left face
i = 6;
i = 4;
i = 2;
i = 0;
i = 0xffffffff;

//back face
i = 7;
i = 5;
i = 6;
i = 4;
i = 0xffffffff;

//top face
i = 0;
i = 4;
i = 1;
i = 5;
i = 0xffffffff;

//bottom face
i = 6;
i = 2;
i = 7;
i = 3;

pIndexBuffer-&gt;Unmap();

//send vertices down pipeline
for( UINT p = 0; p &lt; techDesc.Passes; p++ )
{
//apply technique
pBasicTechnique-&gt;GetPassByIndex( p )-&gt;Apply( 0 );

//draw 5 quads - 29 indexes = 4 indexes x 6 faces + 5 breaks
pD3DDevice-&gt;DrawIndexed( 29, 0, 0 );
}
```

Wow! That’s a lot of code! Well not really once you look at it, we add the 8 key vertices to the vertex buffer exactly as before. Then we map the index buffer in exactly the same way as the vertex buffer and start filling it with the indexes of vertices in the vertex buffer. So for the front face we’re telling it, draw the front bottom left vertex, then the front top left vertex, then the front bottom right and finally the front top right. Now whats the 0xffffffff mean? Well that indicates that a new line list or triangle list must be started at that point, It does the same job as calling a draw call for each face without any of the overhead.

So lets do some basic maths on what we saved by using indexing rather than the standard vertex buffer method. We had a vertex buffer filled with 24 vertices (each vertex weighing in at 224bits) so the vertex buffer 672bytes large. In the second case the vertex buffer is 224bytes, and we have an index buffer with 29 32bit ints (116bytes) so a total of: 340bytes. That’s nearly 50% reduction in memory used, not to mention we are only using a single draw call compared to 4 draw calls when using just a vertex buffer.

So how do we create the index buffer? Well in DX10 all buffers are the same so we create an index buffer the in the same manner we created a vertex buffer with a few minor changes:

```//create vertex and index buffers (space for 100 entries)
//---------------------------------------------------------------------------------

//create vertex buffer
UINT numVertices = 100;
D3D10_BUFFER_DESC bd;
bd.Usage = D3D10_USAGE_DYNAMIC;
bd.ByteWidth = sizeof( vertex ) * numVertices;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;

if ( FAILED( pD3DDevice-&gt;CreateBuffer( &amp;bd, NULL, &amp;pVertexBuffer ) ) ) return fatalError(&quot;Could not create vertex buffer!&quot;);

//change buffer desc bytewidth to index type and set bind type to index buffer
bd.ByteWidth = sizeof( unsigned int ) * numVertices;
bd.BindFlags = D3D10_BIND_INDEX_BUFFER;

if ( FAILED( pD3DDevice-&gt;CreateBuffer( &amp;bd, NULL, &amp;pIndexBuffer ) ) ) return fatalError(&quot;Could not create index buffer!&quot;);

//set vertex and index buffers
UINT stride = sizeof( vertex );
UINT offset = 0;
pD3DDevice-&gt;IASetVertexBuffers( 0, 1, &amp;pVertexBuffer, &amp;stride, &amp;offset );
pD3DDevice-&gt;IASetIndexBuffer( pIndexBuffer, DXGI_FORMAT_R32_UINT, offset );
```

We still use the createBuffer method to create the index buffer, we just change the bytewidth since the buffer stores 32bit unsigned ints. Once the buffer is created we bind it to the Input Assembly by calling the IASetIndexBuffer method ( all we need to specify is a pointer to the buffer, the format of the indexes, and the offset, in case we wish to use only a set portion of the buffer ).

## Depth Testing (Z-buffering)

I’m not going to explain in depth what depth testing/depth buffering/z-buffering is (its covered in almost all beginner graphics tutorials), you guys can use google for that , but here’s a quick link to the basics: http://en.wikipedia.org/wiki/Z-buffer.

In DX10, depth testing is accomplished by making use of a depth stencil, there is a nicely detailed section in the SDK docs regarding the Output-Merger Stage, and here they cover how DX10 accomplishes the depth stencil test internally.

So lets just briefly go over what depth testing is, we have a depth buffer that stores the distance for each pixel in the screen to the camera, so for every pixel we draw from the pixel shader, we compare it’s distance to the camera to the distance stored in the depth buffer, if the new pixel is closer than the distance in the depth buffer then it is drawn and the depth buffer is updated with that pixels distance. That way we only draw the closest visible objects to the viewer, obstruction further objects.

So lets enable this in DX10:

```//dx manager members
ID3D10Texture2D* pDepthStencil;
ID3D10DepthStencilView* pDepthStencilView;

bool dxManager::createRenderTargetsAndDepthBuffer( UINT width, UINT height )
{
//try to get the back buffer
ID3D10Texture2D* pBackBuffer;

if ( FAILED( pSwapChain-&gt;GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &amp;pBackBuffer) ) ) return fatalError(&quot;Could not get back buffer&quot;);

//try to create render target view
if ( FAILED( pD3DDevice-&gt;CreateRenderTargetView(pBackBuffer, NULL, &amp;pRenderTargetView) ) ) return fatalError(&quot;Could not create render target view&quot;);
pBackBuffer-&gt;Release();

//create depth stencil texture
D3D10_TEXTURE2D_DESC descDepth;

descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D32_FLOAT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

if( FAILED( pD3DDevice-&gt;CreateTexture2D( &amp;descDepth, NULL, &amp;pDepthStencil ) ) ) return fatalError(&quot;Could not create depth stencil texture&quot;);

// Create the depth stencil view
D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;

if( FAILED( pD3DDevice-&gt;CreateDepthStencilView( pDepthStencil, &amp;descDSV, &amp;pDepthStencilView ) ) ) return fatalError(&quot;Could not create depth stencil view&quot;);

//set render targets
pD3DDevice-&gt;OMSetRenderTargets( 1, &amp;pRenderTargetView, pDepthStencilView );

return true;
}
```

First we add two new members in the dxmanager class, a ID3D10Texture2D depth stencil pointer and a depth stencil view pointer. Then we create a new texture and assign it to the depth stencil pointer. After this we create a view to the texture by making use of a depth stencil view desc, sort of like the way we created texture views.

The final step is to modify the Output manager’s render targets to include the depth stencil, this automatically enables depth testing. Once we run the program, we get this result: Image 2 – Depth Testing Enabled

So that’s basically it for this short tutorial, I’m sorry its so short and simple, I’m just flooded with other work right now. I’m going to be covering meshes and lighting in the next several tutorials.

## Source Code

Source Code + VS2k8 project files:  GitHub

1. Seth says: