GLSL vertex attributes

Started by
3 comments, last by 21st Century Moose 10 years, 10 months ago

Hello everyone.

My question is about vertex attributes. I'm targeting OpenGL 3.2 and above core profile with the corresponding version of GLSL. Is it ok to assign vertex attribute location using glBindAttribLocation before linking the program or is it preferable to just query them after linking. If i assign locations, some may not be contiguous e.g. position might be 0, normal 1 and texture coordiante 5, is that a problem ?

We think in generalities, but we live in details.
- Alfred North Whitehead
Advertisement

If you have GL_ARB_explicit_attrib_location available I'd advise using that.

If you can't, then it really doesn't matter which method you use, so pick the one that suits how you're going to structure your program the best. Personally I'd run with your first option (a set of glBindAttribLocation calls) as it means you could make assumptions such as "position is always going into attrib 0, so I don't need to make a new glVertexAttribPointer call if nothing but the shader needs to change". That may not suit your own code, and depending on how you have things structured elsewhere you may find it more convenient to query the locations.

Either way, it shouldn't matter if locations are not consecutive. Attrib locations are really just a software/driver level abstraction, and some commercially released major titles use non-consecutive locations (e.g Doom 3 BFG Edition used 0/2/3/4/8/9). I could imagine some lower quality GL implementations choking on non-consecutive locations though, so be sure to test it on those too.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

As far as your question of can you use them before you linking the shader, I believe the answer is no.

I don't think OpenGL doesn't know about attribute locations until you link them.

The details of when you can can can't call glBindAttribLocation are described in the OpenGL spec: http://www.opengl.org/registry/

This can be slightly confusing, so I'm going to clarify some more.

If you're using glBindAttribLocation then you must make your glBindAttribLocation calls before linking. In the old days (i.e. before GL_ARB_explicit_attrib_location) I liked to specify a struct describing the attribs, pass an array of those structs to my shader creation function, and loop through that array binding my locations before making my glLinkProgram call. I expect that's probably the way most people do it.

In this case if you've already linked the program, you don't need to worry too much - you can just make your glBindAttribLocation calls and then call glLinkProgram again. This is obviously not ideal - you really should just link once (for no reason other than it makes program startup faster) so view it as a "get out of jail" card that you can use pending a redesign rather than as a permanent working solution.

If you're using glGetAttribLocation you must make your glGetAttribLocation calls after linking. The same struct trick I mention previously could be used here, but in general I never liked using this call as I always wanted the same attribs in the same locations across different program objects (so that I could change programs without having to issue a new set of glVertexAttribPointer calls, for example).

If you're using explicit attrib locations then in theory you don't need to do anything. You specify an attrib location in your shader, you use that same location in your C/C++ code. You may if you wish query the attrib locations using glGetAttribLocation (after linking) if you want to generalise things a little more, but this never really bothered me as I see a danger in trying to generalise too much.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement