I have been writing GLSL shaders for over a year but this is the first time I have run into this problem. I am on the iOS platform - if that matters.

In a vertex shader I have this:

uniform mediump mat4 projectionViewModelMatrix;

In a fragment shader I have been using for months I have the following uniforms:

uniform int channels;
uniform sampler2D hero;

Nothing to write home about. I added three additional uniforms to the fragment shader:

uniform int heroChannels;
uniform int heroWidth;
uniform int heroHeight;



I tried to grab the location of all my uniforms. This is when my own personal hell began. This is the result:

uniform heroChannels location -1
uniform channels location 4
uniform hero location 5
uniform heroHeight location -1
uniform projectionViewModelMatrix location 0
uniform heroWidth location -1

Huh? Why are the recently added uniforms not given a valid location by the GL Gods?


1 Answer 1


Couple things come to mind.

First, it's possible each of your int uniforms actually takes a whole vector in uniform space, and thus you may be running out of space - you can try to combine them into vectors to see if it helps.

Second, it's possible that the shader compiler happily discards the uniforms in case they're not actually in use. If this is the case, then they naturally won't get a location.

  • \$\begingroup\$ Jari, is there any way to force the compiler not to discard unused variables? \$\endgroup\$
    – dugla
    Jan 18, 2011 at 16:39
  • \$\begingroup\$ @dugla: The glUniform function family specifically does not error on location -1 to make situations like this work, as unused variables are fairly common with stitched shaders. Why worry about it? \$\endgroup\$
    – user744
    Jan 18, 2011 at 16:46
  • \$\begingroup\$ Jari you nailed it. The newly added uniforms were indeed unused. Assigning them to int dev_null forced them to be recognized. Good catcht. Sigh, GL is such a pain. Cheers. \$\endgroup\$
    – dugla
    Jan 18, 2011 at 16:47
  • \$\begingroup\$ @Joe_Wreschnig, I am building a basic shader dictionary for an iOS app I am writing. My thought was to create an dictionary for each shader in my library with the uniforms and attributes as elements in the shader dictionary. Just a way to clean up some of the endless bookkeeping in GL. \$\endgroup\$
    – dugla
    Jan 18, 2011 at 16:49
  • 1
    \$\begingroup\$ @dugla: But you're doing more bookkeeping than necessary. Just store the glGetUniform returns, pass them back in to set the uniforms later, and you don't need to care if they're -1 or not. \$\endgroup\$
    – user744
    Jan 18, 2011 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .