GLES30.glGetUniformLocation() randomly fails

I have a method in my shader class that I use to get my Uniform variables locations, but it seems to fail on specific variables with no reason…

Here’s my getUnfiform location method (in debug mode):

public int getUniformLocation(String uniform){
    int a = GLES30.glGetUniformLocation(programID,uniform);
    if(a == -1){
        Log.e("gul","an error occured : " + uniform );
        //Log.e("gul", "here's the program : "+ GLES30.glGetShaderSource(fragmentShader));
    }
    if(a > -1){
        Log.e("gul","it's fine " + uniform);
    }
    return a;
}

here’s my uniform variables section in my fragmemt shader code :

#version 300 es
precision mediump float;

#define ALPHA_CLIP      //in my specific case I erase, at runtime, this one;

#define MAP_ROUGHNESS
#define MAP_METALINESS  //this one;
#define MAP_SPECULAR    //this one;
#define TEXTURE
#define MAP_EMISSION    //and this one.
#define NORMAL_MAP

#ifdef MAP_ROUGHNESS
    uniform mediump sampler2D roughnessSampler;
#else
    uniform mediump float roughness;
#endif

#ifdef MAP_METALINESS
    uniform mediump sampler2D MetalinessSampler;
#else
    uniform mediump float Metaliness;
#endif

#ifdef MAP_SPECULAR
    uniform mediump sampler2D specularSampler;
#else
    uniform mediump float specular;
#endif

#ifdef TEXTURE
    uniform mediump sampler2D textureSampler;
#endif

#ifdef MAP_EMISSION
    uniform mediump sampler2D emissionSampler;
#else
    uniform mediump vec3 emission;
#endif

#ifdef NORMAL_MAP
    uniform mediump sampler2D normalMap;
#endif

Here are the inputs:

  1. roughnessSanmpler
  2. Metaliness
  3. specular
  4. emission
  5. IOR

and here’s the log output:

E/cleanEntity: about to build shader
E/gul: an error occured : roughnessSampler
E/gul: an error occured : Metaliness
E/gul: it's fine specular
E/gul: an error occured : emission
E/gul: it's fine IOR
E/cleanEntity: built shader

As you can see, three of them fail, but they don’t seem to be in a different situation to the other two. Why is that and how can I fix it?

>Solution :

A uniform variable does not become an active program resource until it is used in the shader program. The shader program is optimized and unnecessary code and resources are skipped. If you try to get the location of an unnecessary uniform that is not an active program resource, glGetUniformLocation returns -1.

Leave a Reply