Computer Graphics Index Buffers CO2409 Computer Graphics Week 9
Contents Vertex Buffers / Duplication Duplication with Lists and Strips Introducing Index Buffers
Vertex Buffers / Duplication So far we have typed in vertex data in C++ (CPU data) [We will load from files shortly, but the point is the same] Each triplet of vertices has represented a triangle… …but this has lead to vertex duplication For example, a cube needed 36 vertices rather than expected 8 Duplicate vertices affect efficiency: A vertex is at least three floats (X,Y,Z) – say 12 bytes Usually more, around 32-48 bytes (we will see why later) So duplication wastes memory Main RAM – our vertex array. Video card RAM – vertex buffer Video card (GPU) may perform redundant processing on these additional vertices
Vertex Buffers One triangle in a C++ vertex lists has looked like this: … D3DXVECTOR3(-0.5, 0.5,0.5),D3DXVECTOR4(1.0,1.0,0.0,0),// Position(xyz)… D3DXVECTOR3(-0.5,-0.5,0.5),D3DXVECTOR4(0.0,1.0,1.0,0),// …& colour(RGBA) D3DXVECTOR3( 0.5,-0.5,0.5),D3DXVECTOR4(0.0,1.0,0.0,0),// …for each vertex When grouped into triplets like this, it is a triangle list This C++ array is stored in CPU-local memory The DirectX code copies the data to GPU-local memory to allow efficient rendering [GPU = Graphics Processing Unit, a CPU for graphics] A block of vertices in GPU-memory is a Vertex Buffer Same layout as list above, just in different memory Access from C++ code to vertex buffer data is limited
Vertex Buffers / Triangle Lists Consider how duplication occurs in a triangle list: Each triangle represented by a triplet of vertices No sharing of vertices possible in this layout Vertices that occur in multiple triangles must be duplicated: In this simple example there is already 50% duplication A larger model may store 6 copies of each vertex 6 times more memory and processing than necessary Triangle List Geometry Vertex 0 Vertex 1 Vertex 2 Vertex 3 2 Triangles 4 Vertices Need 6 Vertices in list
Vertex Buffers / Triangle Strips Can reduce duplication by using triangle strips Just a reordering of the vertex data: First triangle is as normal – a triplet of vertices Each subsequent triangle adds one new vertex and reuses the last 2 Need to specify to API if using this different topology Clearly a benefit, but requires careful ordering of triangles Still get vertex duplication along strip edges: 3 duplicates here Larger models typically duplicate each vertex once Not yet ideal 1 2 3 4 5 1 6 3 7 5 8 Strip 1 Strip 2
Two triangle list with indexes Index Buffers An index buffer is just an array of integers They index the vertex buffer Define triangles using triplets of indexes (indices) This is for a triangle list Similar process for a strip Only store each vertex once Duplicate indexes, not vertices Index Buffer Vertex Buffer 1 2 3 Vertex 0 Vertex 1 Vertex 2 Vertex 3 Two triangle list with indexes No vertex duplication Indexes are integers – 2 or 4 bytes only If indexes are duplicated it is not as wasteful as duplicating a vertex (remember a typical vertex will be around 32-48 bytes)
Example Use of Buffers A square using a vertex buffer only { 5, 0, 5, 0xff00ff00 }, // Triangle 1 { 5, 10, 5, 0xffffff00 }, { 15, 0, 5, 0xff00ff00 }, { 5, 10, 5, 0xffffff00 }, // Triangle 2 { 15, 10, 5, 0xffffff00 }, Using an index buffer { 5, 0, 5, 0xff00ff00 }, // Vertex Buffer // Index Buffer { 0, 1, 2, // Triangle 1 1, 2, 3 } // Triangle 2
Using Index Buffers Most applications use index buffers In DirectX: Create our own an array of integers Ask DirectX to create a index buffer & copy data into it Similar process to that of a vertex buffer For rendering we use the function DrawIndexed Instead of Draw Two independent choices to store geometry: Store triangles in a list or a strip Use just vertices, or vertices & indexes
Vertex Caching Index buffers & triangle strips reduce data storage What about data processing? Given two triangles sharing a vertex, does the GPU process this vertex twice? No – the GPU has a cache of recently used vertices Given a vertex it has seen recently, it uses the previous result So we should order triangles to put adjacent ones together Triangle strips are naturally ordered this way But triangle lists can be reordered for similar benefit Almost negates the value of triangle strips A well-ordered list has close performance to a strip, but is simpler to set up and understand