Sign in to follow this  
bpoint

OpenGL indices per attribute impossible?

Recommended Posts

bpoint    464
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
bpoint    464
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
MickeyMouse    201
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
Ingenu    1629
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
bpoint    464
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
MARS_999    1627
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
bpoint    464
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
Xero-X2    184
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
bpoint    464
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_phantom_    11250
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
bpoint    464
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
_the_phantom_    11250
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
bpoint    464
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  

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now