GLES30.glGetUniformLocation() 随机失败

GLES30.glGetUniformLocation() randomly fails

我在我的着色器 class 中有一个方法,我用它来获取我的统一变量位置,但它似乎无缘无故地在特定变量上失败...

这是我的 getUnfiform 定位方法(在调试模式下):

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;
}

这是我的 fragmemt 着色器代码中的统一变量部分:

#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

输入如下:

  1. 粗糙度采样器
  2. 金属感
  3. 高光
  4. 发射
  5. IOR

这是日志输出:

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

如你所见,其中三个失败了,但他们的情况似乎与另外两个没有什么不同。为什么会这样,我该如何解决?

统一变量只有在着色器程序中使用后才会成为活动的程序资源。优化着色器程序,跳过不必要的代码和资源。如果您尝试获取非活动程序资源的不必要制服的位置,glGetUniformLocation returns -1.