Dan Webster
Dan Webster

Reputation: 1253

Difference between uniform location and uniform index?

What is the difference between uniform location and uniform index in OpenGL? It seems like you need the location to assign a value to the uniform, and you need the index to query information about the uniform.

Why is there a need of two values? Why can't one of them fulfill both purposes?

Upvotes: 6

Views: 1916

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473192

It seems like you need the location to assign a value to the uniform, and you need the index to query information about the uniform.

Yes, that's the difference between the two.

Why is there a need of two values?

Don't assume anything that the OpenGL API does is based purely on "need". Sometimes it's just "we were stupid" or "this was kinda a good idea at one point, but now it doesn't make sense anymore."

Uniform locations generally fall into the later.

The original idea, going back to 3D Labs's original GLSL proposal, was that uniform locations could represent some kind of byte offset. That the location was a meaningful number that represented an actual location for where the uniform's memory is. Therefore, the implementation wouldn't have to use a lookup table to convert an arbitrary number into a byte offset to where the uniform's actual storage lies. The location would be the byte offset or whatever other data was needed to quickly convert from a program object to the uniform's data address.

However, with the exception of possibly 3D Labs's own GLSL implementation, this was never actually used by implementations. Uniform locations were almost always just a second form of index, and glUniform calls would look them up in a table.

This is why we now have the ability to explicitly specify uniform locations. That's basically the ARB saying, "we give up; they're indices now". But they couldn't just change the old APIs that have the distinction, so they have to remain two separate ways to talk about uniforms.

And they didn't really want to. Keeping the distinction makes the query APIs a bit easier to use, in some respects. The available uniform indices are all indices from 0 to some queriable count (GL_ACTIVE_UNIFORMS). Once you can specify uniform locations explicitly, the dichotomy is useful because the indices remain just a queriable count of numbers which are independent of the location.

That way, the API doesn't have to have a "get all available uniform locations" query. You just query the count, and then make a loop from 0 to that count to query information. It's easier for the user and it's easier for the implementation.

Upvotes: 10

Related Questions