Sign in to follow this  
bpoint

OpenGL indices per attribute impossible?

Recommended Posts

I've been using VBOs to store (for the time being) just indices and vertex data, and have decided to add vertex colors into the mix. I went back and took a look at how to setup glColorPointer and then realized that the indicies that I would be passing to glDrawElements only represent the index into both arrays, not an individual index for vertex and color separately! This means that a large number of vertices (and colors) will have to be duplicated, because I cannot index through both arrays with different indicies per vertex. For example, a simple cube with only 8 vertices where each face had a different color would end up requiring 8*6=48 vertices (even though the actual vertex positions are the same) because the color table requires different values for each vertex. [ Edit: Actually not 48, but 24. See my reply below. ] Sorry, I come from a GameCube background where we were able to do this. :) Is there something I'm overlooking, or perhaps an extension which provides this functionality? Or is this just impossible under OpenGL? (I'd also like to know if it's possible or not under DirectX if anyone else out there knows the answer.) [Edited by - bpoint on November 30, 2005 3:48:07 AM]

Share this post


Link to post
Share on other sites
Urggh... I was hoping you wouldn't say that.

I know the GameCube used a specialized ATI chipset, but I'd presume that after 3+ years something as useful as having multiple indices per attribute would have at least made it into consumer-level hardware by now.

*grumble*

Share this post


Link to post
Share on other sites
Future version of D3D for PC will most probably allow for manual vertex fetching from within vertex shader. You could use vertex texture fetch (within vertex shader) nowadays, but it won't probably be an optimal solution.

Edit: Oops, somehow I thought I was on another forum ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by bpoint
Urggh... I was hoping you wouldn't say that.

I know the GameCube used a specialized ATI chipset, but I'd presume that after 3+ years something as useful as having multiple indices per attribute would have at least made it into consumer-level hardware by now.

*grumble*


Sorry to be pedantic, but no it doesn't.
NGC uses an ArtX chip, which firm was later bought by ATI (some months before the NGC release), so it has nothing to do with ATI PC products. (But is related to XBox & Revolution GPUs)

Share this post


Link to post
Share on other sites
Is there anyone other than me who thinks that having separate indicies for each attribute would be incredibly useful? I guess you'd have to actually use it first to realize the amount of data it can save. :)

MickeyMouse's comment on pulling vertices from a vertex shader sounds interesting though... I wonder how long until something like that gets implemented.

I don't exactly know how OpenGL extensions are decided upon, but I'd be more than willing to put together an extension specification _just_ to have future drivers and hardware vendors support this functionality.

Comments, anyone?

Share this post


Link to post
Share on other sites
Quote:
Original post by bpoint
I've been using VBOs to store (for the time being) just indices and vertex data, and have decided to add vertex colors into the mix.

I went back and took a look at how to setup glColorPointer and then realized that the indicies that I would be passing to glDrawElements only represent the index into both arrays, not an individual index for vertex and color separately! This means that a large number of vertices (and colors) will have to be duplicated, because I cannot index through both arrays with different indicies per vertex.

For example, a simple cube with only 8 vertices where each face had a different color would end up requiring 8*6=48 vertices (even though the actual vertex positions are the same) because the color table requires different values for each vertex.

Sorry, I come from a GameCube background where we were able to do this. :)

Is there something I'm overlooking, or perhaps an extension which provides this functionality? Or is this just impossible under OpenGL? (I'd also like to know if it's possible or not under DirectX if anyone else out there knows the answer.)


I am not understanding how you are getting 48? You don't create new vertices after you specify them unless you are tesselating or doing a displacement mapping of some kind? If you have 8 vertices then you have 8 sets of vertex data which includes position, color, texture coords, secondary color, normals. For color OpenGL needs the color per vertex if I am right (Please correct me if I am wrong) to do shading from vertex to vertex if you declare a new color at each vertex...

Share this post


Link to post
Share on other sites
I apologize -- I was wrong about the original calculation of 48. A single vertex would be shared by only three polygons, not 6, so the actual number of required vertices is 8*3=24. Regardless, vertex data still needs to be unnecessarily duplicated.

Anyway, I'll explain. Given a simple cube, with 8 vertices, and the indices to render it (ignore the vertex winding order for example's sake):

Vector3 verts[8] = {
{ -1.0f, -1.0f, -1.0f },
{ -1.0f, -1.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f },
{ -1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f },
{ 1.0f, -1.0f, 1.0f },
{ 1.0f, 1.0f, -1.0f },
{ 1.0f, 1.0f, 1.0f }
};

u16 indices[4][6] = {
0, 1, 2, 3,
4, 5, 6, 7,
0, 1, 4, 5,
2, 3, 6, 7,
0, 2, 4, 6,
1, 3, 5, 7
};


This can now be rendered with:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, verts);
for (int t = 0; t < 6; t++)
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, &indicies[t]);


Now I want to add color per vertex, with a specific color per face. This means that each vertex requires its own color. Since indicies only represent a vertex and not an individual attribute, the vertices will need to be duplicated for each color that is different. This is best shown by using interleaved data:

typedef struct
{
Vector3 pos;
u32 rgba;
} Vertex;

Vertex verts[24] = {
{ { -1.0f, -1.0f, -1.0f }, RED },
{ { -1.0f, -1.0f, 1.0f }, RED },
{ { -1.0f, 1.0f, -1.0f }, RED },
{ { -1.0f, 1.0f, 1.0f }, RED },
{ { 1.0f, -1.0f, -1.0f }, GREEN },
{ { 1.0f, -1.0f, 1.0f }, GREEN },
{ { 1.0f, 1.0f, -1.0f }, GREEN },
{ { 1.0f, 1.0f, 1.0f }, GREEN },
{ { -1.0f, -1.0f, -1.0f }, BLUE },
{ { -1.0f, -1.0f, 1.0f }, BLUE },
{ { 1.0f, -1.0f, -1.0f }, BLUE },
{ { 1.0f, -1.0f, 1.0f }, BLUE },
{ { -1.0f, 1.0f, -1.0f }, YELLOW },
{ { -1.0f, 1.0f, 1.0f }, YELLOW },
{ { 1.0f, 1.0f, -1.0f }, YELLOW },
{ { 1.0f, 1.0f, 1.0f }, YELLOW },
{ { -1.0f, -1.0f, -1.0f }, CYAN },
{ { -1.0f, 1.0f, -1.0f }, CYAN },
{ { 1.0f, -1.0f, -1.0f }, CYAN },
{ { 1.0f, 1.0f, -1.0f }, CYAN },
{ { -1.0f, -1.0f, 1.0f }, MAGENTA },
{ { -1.0f, 1.0f, 1.0f }, MAGENTA },
{ { 1.0f, -1.0f, 1.0f }, MAGENTA },
{ { 1.0f, 1.0f, 1.0f }, MAGENTA }
};


As you can see, there are three times the number of vertices required than would be necessary if the position and color data were split into different buffers and likewise indexed separately. On the GameCube, we were able to do this:

Vector3 verts[8] = {
{ -1.0f, -1.0f, -1.0f },
{ -1.0f, -1.0f, 1.0f },
{ -1.0f, 1.0f, -1.0f },
{ -1.0f, 1.0f, 1.0f },
{ 1.0f, -1.0f, -1.0f },
{ 1.0f, -1.0f, 1.0f },
{ 1.0f, 1.0f, -1.0f },
{ 1.0f, 1.0f, 1.0f }
};

u32 colors[6] = {
RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA
};


And then send two indicies per vertex. While this is a fairly simple test case, larger models might be able to have a decent amount of unnecessary data trimmed down. Skinned models might not benefit that _much_ from having separate index buffers because most polygons are joined together with the same normal, same vertex color, and same texture coordinate. Basically any vertex that does not share the _exact_ same attributes for all polygons attached to it needs to be duplicated.

Technically, this could be accomplished under OpenGL as well by maintaining multiple index buffers and calling glColorPointer after each call to glDrawElements. This would allow the same vertex buffer to be used while referencing different colors, but this is a fairly specialized case and not very efficient.

I also don't think that it would be that hard to implement from a hardware perspective. There are already pointers to different attribute buffers, so why aren't there multiple indicies for each of those attributes?

Share this post


Link to post
Share on other sites
I definitely agree that there should be a way to do this, I have commonly wished it were possible, because most modeling formats and models have more texture coords then vertices because they are specialized for each face. If we could specify which color/texcoord/vertex indices separately it would be much easier to render these. To bad there isn't a way presently.

Share this post


Link to post
Share on other sites
Exactly -- changes in texture coordinates also requires vertex duplication. An extension like this would definitely cut down on some excess memory while improving performance (less vertex data to process / cross the memory bus, or if you're using VBOs, more texture space).

If there are enough people that think something like this might be useful, I'll write up an OpenGL extension specification and send e-mail to some of the contacts at nVidia and ATI to see what the best way to progress would be. It may never actually be implemented, but it certainly can't hurt to try. ;)

Share this post


Link to post
Share on other sites
The only problem is, afaik, the hardware doesnt work in this manner. Just from a vertex cache pov I can see this becoming a small nightmare, never mind the fetching of the data.

Infact, data fetching and cache coherance is the main reason you interlace the vertex, so that everything is located next to each other in memory, making data transfers faster as the GPU doesnt need to jump around memory AND improving cache coherance when it comes to reusing the data.

While it might seem like a 'handy' idea, its probably a going to be a nightmare to do in hardware in the future and certainly wont work now.

Share this post


Link to post
Share on other sites
Not necessarily. Well-compacted indices will tend to have more cache hits because the data is already in the cache, rather than needing to fetch extra (read: duplicated) vertex data from memory. I can understand interleaving being faster due to the size of a cache line pulling in multiple vertices at a time, but in the end, there would be more data to plough through than if smaller buffers (even laid out sporadically in memory) were used more efficiently.

I don't doubt the hardware doesn't work like this now, as the GameCube was specialized hardware. But if a call was put in to request a feature like this, nVidia, ATI and whoever else can decide how easy or difficult it would be and either implement it for future designs or simply ignore it. If they do implement it, great. If not, oh well -- I'll just have to stick with the existing methods. :)

I guess I might as well try starting some e-mail dialogue with them to see what they think...

Share this post


Link to post
Share on other sites
You should probably check the OpenGL extensions/suggestions forum first on Opengl.org (infact, do a general search) as I'm sure this kinda thing comes up often.

As for the cache issue, the gain probably wouldnt be as significant as you think. For ALOT of models there are very few shared verticies, the cube example often comes up but its a very bad model for 'real world' data. In real world conditions you have to duplicate around 1/3 of the vertices at the most. As such you'll end up throwing more indicies down the pipeline and probably not gaining anything in the real world.

Share this post


Link to post
Share on other sites
For those of you interested, I've made a post on OpenGL.org's discussion boards regarding this. We'll see where it goes from here...

Phantom: How much performance would be affected is really dependent on the kind of data being rendered. Like I said before, skinned models tend to use the same attributes for all shared vertices, but map models would benefit from using repetitive data (vertex color and normals come to mind). I agree, my cube example was rather extreme. However, from experience, being able to use multiple indices for each attribute definitely help cut down on memory requirements -- which can only be a good thing. :)

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

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627736
    • Total Posts
      2978866
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now