Jump to content
  • Advertisement
Sign in to follow this  
Gumgo

OpenGL Index Buffers

This topic is 3995 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

Well I finally got everything to render how I want it without the use of vertex buffers, shaders, etc. but now I want to speed things up a bit. Someone described to me "index buffers":
Quote:
for a small example. lets say that you had a cube. this cube will have 8 verts and 12 triangles. if we were just to send each triangle down by itself, we would send the 3 verts it has and transform it and render the triangle. which will end up doing 24 transformations in order to render the cube. which in some cases this leads to extreme amount of replicated work. what we would like to do is only process each vertex once and reuse the work that we did. which is where index buffers come in Oh, that makes sense. so we can just send an array of 8 verts down and process each once. then when we need to render a triangle we instead specify each as 3 indexes into the array of 8 verts that we already processed Can these two (vertex buffers and index buffers) both be used at once? yes, you have to in order to index. so what you do is set up a vertex buffer the size of the total verticies in the object. and then a separate index buffer to specify the triangles. or a vertex buffer can be used by itself and just specify the triangles inline, but as I mentioned this will replicate some data
I have a few questions about index buffers being used with vertex buffers. 1) Where is everything stored while/after being transformed? So far I've only used glVertex and so I have to send the vertices for each triangle in groups of 3 - if the 3 vertices making up a triangle aren't sent one after another, the wrong triangle will be rendered. With index buffers being used with vertex buffers (correct me if I'm wrong) it sounds like you'd have to transform all the vertices for an object and store them to be used later when you render the triangles. How does this work? 2) What is the best way to store each vertex in files and RAM? I can think of two ways: the first is to have each vertex consist of position, normal, and texture coords, and have each triangle have 3 indexes for its vertices. The second way is to store 3 arrays: position, normal, and texture coords. Each triangle would have 9 indexes in this case, but it would seem to greatly reduce the number of duplicated componants. With the first method, if a vertex was shared by 3 triangles but the triangles were flat-shaded, since the normals would be different for them all then 3 different vertices would have to be transformed completely. However with the second way, since the normals would be the only difference, the position would not have to be transformed 3 times. Since I haven't used index buffers, I don't know which is best, or possible to do, or if I'm looking at this entirely the wrong way. 3) Also, the person who explained index buffers uses DirectX and not OpenGL. If anyone could point me to the OpenGL API for index buffers that'd be great.

Share this post


Link to post
Share on other sites
Advertisement
1) It is not stored in any permanent way. Why would you want to store it?

2) This page explains on a common usage pattern
http://www.opengl.org/wiki/index.php/GL_ARB_vertex_buffer_object

Share this post


Link to post
Share on other sites
I don't want to store it permanently, but if a vertex is going to be used several times in different triangles, you have to temporarily store its transformed position, right? Also thanks for the link, I'll have a look at it right now.

Share this post


Link to post
Share on other sites
The driver/GPU temporarily stores previously processed vertex data in it's cache. It uses this cache to avoid multiple processing of the same data. But the it's size is limited so it can only do this if the vertex is close. I mean that in the indices list matching indexes have to be as close to each other as it can be. So if you have and IndexList of (0, 1, 2, 1, 2, 3, 3, 4, 1, ...) or similar than the driver/GPU can take advantage of the cached vertices. If you have a list like (0, 1, 2, 3, 4, ... a lot of index that not mach at all ... 1, 4, 3, 2, 234, 2 ...) then the driver/GPU can't take advantage of the cache, as it will long forget about the previously processed 1st, 2nd, 3rd and so on vertices.

Share this post


Link to post
Share on other sites
So index buffers only really help performance if the triangles that share vertices are drawn one after another?

Share this post


Link to post
Share on other sites
Yes, it's a very small cache.
It varies from GPU. 16 vertices for Geforce1/2, 24 vertices for Geforce 3/4.
I think it's 32 geforce fx, 48 geforce 6xxx

nVidia has released this lib to optimize meshes
http://developer.nvidia.com/object/nvtristrip_library.html

Share this post


Link to post
Share on other sites
Alright, so I've gotten to the point where I'm actually trying to implement these things. Vertex buffers seem fairly easy, I just looked through the NeHe tutorial, but I'm not sure how to do index buffers.

I have 3 buffers set up: vertex data, normal data, and texture coord data. Then I created a 4th buffer to store the vertex, normal, and texture IDs for each triangle. How do I draw the correct vertices, normals, and textures, from the 4th buffer?

By the way, NeHe states that "you should be aware that normals operate "one for one" with vertices, meaning that if you are using normals, each vertex should have an accompanying normal". Does this mean that I can't store the vertices seperate from the normals? If so then I will have to include the vertices and normals into one buffer and I'd prefer to keep them seperate so I can reduce memory usage if possible. Also it would lead to much more duplication of vertex and normal data which I wouldn't like very much.

Or does this just mean that each normal goes with a vertex, not with a triangle? If that is the case (which now I think it is because I noticed GL_NORMAL_ARRAY) I will be happy. =)

Share this post


Link to post
Share on other sites
1) The vertices are transformed on the fly. Graphics hardwares are modeled after a conceptual rendering pipeline. A rendering pipeline is composed of several stages, each of which may have a pipeline in itself. Why do we have pipelines you might ask? Well, the discussion is somehow lengthy and so I leave it to another time. But for our purposes, all you need to know is that each stage has its own set of responsibilities. The Geometry stage is responsible for transforming the geometry (among MANY other things). For triangle lists, it conceptually follows the following algorithm:

// Note: Every trio of indices in the index buffer represents a triangle
For every trio of indices (triangle) in the index buffer:
For every index of this trio:

Check the post TnL vertex cache for this index. Each index references one
vertex and one vertex only. If the index exists in the cache, the
vertex that it references has been transformed lately so we don't need to
re-transform it. The transformed vertex is stored in the cache.

If the vertex is inside the post TnL vertex cache
Fetch the transformed vertex and skip to the next iteration of the loop
else
Check the pre-transform vertex cache to see if the untransformed vertex
that we are going to transform has been fetched before, so we don't need
to re-fetch it from memory. This is totally different from the post TnL
cache (see the description below)

If the untransformed vertex is inside the pre-transform vertex cache
Fetch the untransformed vertex from the cache. This saves us a memory
access.
else
Fetch the untransformed vertex from video/AGP memory since we couldn't
find it inside the cache; look inside the vertex buffer and find the
untransformed vertex that this index references.

Now that we have the untransformed vertex, transform it and store the
results in the post TnL cache.


Note that there is nothing magical with a cache. A cache is simply a high performance memory that stores the results of a computation (or a memory access) in order to decrease a delay. They can be used anywhere. Actually we use them all the time. A simple variable that stores the results of a computation is a simple cache, sine we would have otherwise needed to recompute everything had we not stored the results in that variable. To that end, there's also another small cache, called the pre-transform cache (as oposed to post TnL or post-transform cache) which stores the vertices that have been lately fetched from memory.

So to sum it up:
The pre-transform vertex cache is a cache that stores the results of a memory access. It stores untransformed vertices that have been lately referenced by an index. To make good use of this cache, reorder your vertex buffer so vertices are relatively stored in the order that they are referenced. This is the cache that people mostly neglect.

The post-transform (also called post TnL) vertex cache is a cache that stores the results of a computation. It stores transformed vertices after the transformation pipeline is done with them. To make good use of this cache, reorder your index buffer so triangles are stored in an order which makes them share as many vertices as possible. This is the cache that people are mostly referring to when they refer to vertex caches in general.

Pretty confusing, isn't it? :) Just give it some time.

2) I suspect that the first approach yields better results, as its a better use of pre-transform cache.

Quote:
Original post by V-man
It varies from GPU. 16 vertices for Geforce1/2, 24 vertices for Geforce 3/4.
I think it's 32 geforce fx, 48 geforce 6xxx

nVidia has released this lib to optimize meshes
http://developer.nvidia.com/object/nvtristrip_library.html

I doubt there are any cards that have caches larger than 32 entries. As Hugues Hoppe showed in "Optimization of mesh locality for transparent vertex caching", its very unlikely that any hardware benefits from caches larger than 32 entries.

Besides, triangle strips are really outdated. I haven't used NvTriStrip, but it's unlikely that stripification of geometry yields better results than triangle lists that are optimized for good cache coherency.

[Edited by - Ashkan on January 4, 2008 2:52:17 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Gumgo
Alright, so I've gotten to the point where I'm actually trying to implement these things. Vertex buffers seem fairly easy, I just looked through the NeHe tutorial, but I'm not sure how to do index buffers.

I have 3 buffers set up: vertex data, normal data, and texture coord data. Then I created a 4th buffer to store the vertex, normal, and texture IDs for each triangle. How do I draw the correct vertices, normals, and textures, from the 4th buffer?

By the way, NeHe states that "you should be aware that normals operate "one for one" with vertices, meaning that if you are using normals, each vertex should have an accompanying normal". Does this mean that I can't store the vertices seperate from the normals? If so then I will have to include the vertices and normals into one buffer and I'd prefer to keep them seperate so I can reduce memory usage if possible. Also it would lead to much more duplication of vertex and normal data which I wouldn't like very much.

Or does this just mean that each normal goes with a vertex, not with a triangle? If that is the case (which now I think it is because I noticed GL_NORMAL_ARRAY) I will be happy. =)


Use the link that I gave you. It explains a common usage pattern.
4th buffer? Don't put texture ID in a vertex buffer.

Each vertex should have 1 normal, 1 texcoord.
Example, you can't have 5 vertex, 2 normals, 3 texcoords. Have 5 of each.

The link I gave also shows how to make an index buffer.

Quote:
Besides, triangle strips are really outdated. I haven't used NvTriStrip, but it's unlikely that stripification of geometry yields better results than triangle lists that are optimized for good cache coherency.


I didn't say to use or not to use GL_TRIANLE_STRIP.
You can use GL_TRIANGLES if you want.

Share this post


Link to post
Share on other sites
Quote:
Each vertex should have 1 normal, 1 texcoord.
Example, you can't have 5 vertex, 2 normals, 3 texcoords. Have 5 of each.

Hmm, okay. Well, I guess I misunderstood a bit and this complicates things some, but I'll have another look at the link.

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!