Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From my experience, OpenGL does not define the concept of attributes or uniforms semantics.</p> <p>All you can do is define your own way of mapping <em>your</em> semantics to OpenGL variables, using the only parameter you can control about these variables: their <em>location</em>.</p> <p>If you're not constrained by platform issues, you could try to use the 'new' <a href="http://www.opengl.org/registry/specs/ARB/explicit_attrib_location.txt" rel="nofollow noreferrer">GL_ARB_explicit_attrib_location</a> (core in OpenGL 3.3 if I'm not mistaken) that allows shaders to explicitly express which location is intended for which attribute. This way, you can hardcode (or configure) which data you want to bind on which attribute location, and query the shaders' locations after it's compiled. It seems that this feature is not yet mature, and perhaps subject to bugs in various drivers.</p> <p>The other way around is to bind the locations of your attributes using <em>glBindAttribLocation</em>. For this, you have to know the names of the attributes that you want to bind, and the locations you want to assign them.</p> <p>To find out the names used in a shader, you can:</p> <ul> <li>query the shader for active attributes</li> <li>parse the shader source code to find them yourself</li> </ul> <p>I would not recommend using the GLSL parsing way (although it may suit your needs if you're in simple enough contexts): the parser can easily be defeated by the preprocessor. Supposing that your shader code becomes somewhat complex, you may want to start using #includes, #defines, #ifdef, etc. Robust parsing supposes that you have a robust preprocessor, which can become quite a heavy lift to set up.</p> <p>Anyway, with your active attributes names, you have to assign them locations (and/or semantics), for this, you're alone with your use case.</p> <p>In our engine, we happily hardcode locations of predefined names to specific values, such as:</p> <pre><code>glBindAttribLocation(prog, 0, "mg_Position"); glBindAttribLocation(prog, 1, "mg_Color"); glBindAttribLocation(prog, 2, "mg_Normal"); ... </code></pre> <p>After that, it's up to the shader writer to conform to the predefined semantics of the attributes.</p> <p>AFAIK it's the most common way of doing things, <a href="http://www.stevestreeting.com/2008/04/26/working-around-glsl-attribute-aliasing-problems-on-os-x/" rel="nofollow noreferrer">OGRE</a> uses it for example. It's not rocket science but works well in practice.</p> <p>If you want to add some control, you could provide an API to define the semantics on a shader basis, perhaps even having this description in an additional file, easily parsable, living near the shader source code.</p> <p>I don't get into uniforms where the situation is almost the same, except that the 'newer' extensions allow you to force GLSL uniform blocks to a memory layout that is compatible with your application.</p> <p>I'm not satisfied by all this myself, so I'll be happy to have some contradictory information :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload