0

I use the same shaders on different devices, iOS and desktop nVidia with OpenGL ES profile. Compiling shaders was fine without any errors or warnings, but when I tried to use glGetAttribLocation, nVidia drivers seemed to have optimized my attrib "a_color" out while ios didn't. I know to use glBindAttribLocation to ensure the attrib is there, but I just want to know why nVidia driver did it.

Vertex shader:

#define IN_VERT  attribute
#define OUT_VERT varying
#define IN_FRAG  varying
#define OUT_FRAG


#define MAX_LIGHTS   3

/////////////////////////////////////////////////////////////////////////////////////
// VERTEX ATTRIBUTES
/////////////////////////////////////////////////////////////////////////////////////
IN_VERT         vec4 a_position;

IN_VERT         vec3 a_normal;
IN_VERT lowp    vec4 a_color;

/////////////////////////////////////////////////////////////////////////////////////
// Global State
/////////////////////////////////////////////////////////////////////////////////////
uniform bool        perPixelLightingEnabled;
uniform lowp vec4   lightModelAmbientColor;

/////////////////////////////////////////////////////////////////////////////////////
// TRANSFORM
/////////////////////////////////////////////////////////////////////////////////////
uniform mat4    modelviewMatrix;
uniform mat3    normalMatrix;
uniform mat4    mvpMatrix;

/////////////////////////////////////////////////////////////////////////////////////
// TEXTURE
/////////////////////////////////////////////////////////////////////////////////////
#define MAX_TEXTURES    3
#define MAX_TEX_COORDS  3
#define S               0
#define T               1
#define R               2
#define STR             3

#define GLKTexGenModeObjectLinear      0
#define GLKTexGenModeEyeLinear         1
#define GLKTexGenModeSphereMap         2
#define GLKTexGenModeReflectionMap     3

struct texGen_s
{
    lowp int        mode;
    highp vec4      plane;
    highp vec4      eyePlaneByInvModelview;
};

struct texture_s
{
    mat4                matrix;
    lowp vec4           envColor;
    texGen_s            texGen[STR];
};

uniform texture_s       textures[MAX_TEXTURES];
uniform sampler2D       unit2d[3];
uniform samplerCube     unitCube[3];


/////////////////////////////////////////////////////////////////////////////////////
// LIGHT
/////////////////////////////////////////////////////////////////////////////////////
uniform bool        light_enabled[MAX_LIGHTS];
uniform vec4        light_position[MAX_LIGHTS];
uniform vec4        light_positionEye[MAX_LIGHTS];
uniform lowp vec4   light_ambientColor[MAX_LIGHTS];
uniform lowp vec4   light_diffuseColor[MAX_LIGHTS];
uniform lowp vec4   light_specularColor[MAX_LIGHTS];
uniform vec3        light_normalizedSpotDirectionEye[MAX_LIGHTS];
uniform float       light_spotExponent[MAX_LIGHTS];
uniform float       light_spotCutoffAngle[MAX_LIGHTS];
uniform float       light_constantAttenuation[MAX_LIGHTS];
uniform float       light_linearAttenuation[MAX_LIGHTS];
uniform float       light_quadraticAttenuation[MAX_LIGHTS];

/////////////////////////////////////////////////////////////////////////////////////
// MATERIAL
/////////////////////////////////////////////////////////////////////////////////////
struct material_s
{
    lowp vec4 ambientColor;
    lowp vec4 diffuseColor;
    lowp vec4 specularColor;
    lowp vec4 emissiveColor;
    float shininess;
};

uniform material_s  material;

/////////////////////////////////////////////////////////////////////////////////////
// FOG
/////////////////////////////////////////////////////////////////////////////////////
#define GLKFogModeExp           0
#define GLKFogModeExp2          1
#define GLKFogModeLinear     2

struct fog_s
{
    lowp int     mode;
    lowp vec4    color;
    float   density;
    float   start;
    float   end;
};

uniform fog_s fog;


/////////////////////////////////////////////////////////////////////////////////////
// Varyings
/////////////////////////////////////////////////////////////////////////////////////

OUT_VERT lowp vec4  v_color;



/////////////////////////////////////////////////////////////////////////////////////
// Temps
/////////////////////////////////////////////////////////////////////////////////////
highp vec3 normalEye;

#define materialAmbientColor (material.ambientColor)
#define materialDiffuseColor (material.diffuseColor)
uniform lowp vec4 baseLightingColor;
uniform vec4 ambientTerm[3];

void main(void)
{
    // int currLight;
    lowp vec4 tmpFrontColor, tmpBackColor;
    vec3 normalizedNormal;

    // Default value for eye space normal
    normalEye = vec3(0.0, 0.0, 1.0);

    /* if( perVertexLightingEnabled == true ||
        perPixelLightingEnabled == true ||
        (textures[0].texGen[S].enabled == true && textures[0].texGen[S].mode == GLKTexGenModeSphereMap) ||
        (textures[0].texGen[T].enabled == true && textures[0].texGen[T].mode == GLKTexGenModeSphereMap) ||
        (textures[0].texGen[S].enabled == true && textures[0].texGen[S].mode == GLKTexGenModeReflectionMap) ||
        (textures[0].texGen[T].enabled == true && textures[0].texGen[T].mode == GLKTexGenModeReflectionMap) ||
        (textures[0].texGen[R].enabled == true && textures[0].texGen[R].mode == GLKTexGenModeReflectionMap) ||

        (textures[1].texGen[S].enabled == true && textures[1].texGen[S].mode == GLKTexGenModeSphereMap) ||
        (textures[1].texGen[T].enabled == true && textures[1].texGen[T].mode == GLKTexGenModeSphereMap) ||
        (textures[1].texGen[S].enabled == true && textures[1].texGen[S].mode == GLKTexGenModeReflectionMap) ||
        (textures[1].texGen[T].enabled == true && textures[1].texGen[T].mode == GLKTexGenModeReflectionMap) ||
        (textures[1].texGen[R].enabled == true && textures[1].texGen[R].mode == GLKTexGenModeReflectionMap)) */

    {
        normalEye = normalize((normalMatrix * a_normal));
    }

    vec4 specularTerm, diffuseTerm;
    vec3 vertexToLightVec;
    highp float attenuationFactor, spotFactor, nDotL;

    // if(perVertexLightingEnabled == true)
    {

            // else
            {
            // materialAmbientColor = material.ambientColor;
            // materialDiffuseColor = material.diffuseColor;  
            // baseLightingColor = material.emissiveColor + (materialAmbientColor * lightModelAmbientColor);
            }


        tmpFrontColor = baseLightingColor;

         // for(0 = 0; 0 < numLights; 0++) 
        {
            // if(light_enabled[0] == false) continue;


            // For directional lights light_positionEye[0].xyz is normalized on the CPU side
            vertexToLightVec = light_positionEye[0].xyz;

            attenuationFactor = 1.0;


            // Calculate diffuse and specular terms
            nDotL = max(dot(normalEye, vertexToLightVec), 0.0);

            diffuseTerm = nDotL * materialDiffuseColor * light_diffuseColor[0];


            specularTerm = vec4(0.0);

            spotFactor = 1.0;


            tmpFrontColor += attenuationFactor * spotFactor * (ambientTerm[0] + diffuseTerm + specularTerm);


        }

    }


    v_color = a_color;


    v_color = tmpFrontColor;
    v_color.a = materialDiffuseColor.a;



    v_color = clamp(v_color, 0.0, 1.0);



    gl_Position = mvpMatrix * a_position;

}

fragment shader:

#define MAX_LIGHTS      3

#define MAX_TEXTURES    3
#define MAX_TEX_COORDS  3
#define S               0
#define T               1
#define R               2
#define STR             3

#define GLKTextureTarget2d              0
#define GLKTextureTargetCubeMap         1

#define GLKTextureEnvAttribReplace         0
#define GLKTextureEnvAttribModulate        1
#define GLKTextureEnvAttribDecal           2

#define GLKTexGenModeObjectLinear      0
#define GLKTexGenModeEyeLinear         1
#define GLKTexGenModeSphereMap         2
#define GLKTexGenModeReflectionMap     3

#define IN_VERT  attribute
#define OUT_VERT varying
#define IN_FRAG  varying
#define OUT_FRAG

precision highp float;
#define out_color gl_FragColor

struct texGen_s
{
    lowp  int       mode;
    highp vec4      plane;
    highp vec4      eyePlaneByInvModelview;
};

struct texture_s
{
    mat4                matrix;
    lowp vec4           envColor;
    texGen_s            texGen[STR];
};

uniform lowp sampler2D         unit2d[3];
uniform lowp samplerCube     unitCube[3];

/////////////////////////////////////////////////////////////////////////////////////
// Globals
/////////////////////////////////////////////////////////////////////////////////////
uniform lowp vec4   lightModelAmbientColor;


/////////////////////////////////////////////////////////////////////////////////////
// LIGHT
/////////////////////////////////////////////////////////////////////////////////////
uniform bool        light_enabled[MAX_LIGHTS];
uniform vec4        light_positionEye[MAX_LIGHTS];
uniform lowp vec4   light_ambientColor[MAX_LIGHTS];
uniform lowp vec4   light_diffuseColor[MAX_LIGHTS];
uniform lowp vec4   light_specularColor[MAX_LIGHTS];
uniform vec3        light_normalizedSpotDirectionEye[MAX_LIGHTS];
uniform float       light_spotExponent[MAX_LIGHTS];
uniform float       light_spotCutoffAngle[MAX_LIGHTS];
uniform float       light_constantAttenuation[MAX_LIGHTS];
uniform float       light_linearAttenuation[MAX_LIGHTS];
uniform float       light_quadraticAttenuation[MAX_LIGHTS];


/////////////////////////////////////////////////////////////////////////////////////
// MATERIAL
/////////////////////////////////////////////////////////////////////////////////////
struct material_s
{
    lowp vec4 ambientColor;
    lowp vec4 diffuseColor;
    lowp vec4 specularColor;
    lowp vec4 emissiveColor;
    float shininess;
};

uniform material_s  material;


/////////////////////////////////////////////////////////////////////////////////////
// FOG
/////////////////////////////////////////////////////////////////////////////////////
#define GLKFogModeExp           0
#define GLKFogModeExp2          1
#define GLKFogModeExpLinear     2

struct fog_s
{
    lowp int  mode;
    lowp vec4 color;
    float     density;
    float     start;
    float     end;
};

uniform texture_s    textures[MAX_TEXTURES];
uniform fog_s        fog;


/////////////////////////////////////////////////////////////////////////////////////
// Reflection Mapping
/////////////////////////////////////////////////////////////////////////////////////
uniform highp mat3   reflectionMapMatrix;


/////////////////////////////////////////////////////////////////////////////////////
// Varyings
/////////////////////////////////////////////////////////////////////////////////////
IN_FRAG lowp    vec4 v_color;




/////////////////////////////////////////////////////////////////////////////////////
// Temps
/////////////////////////////////////////////////////////////////////////////////////

#define materialAmbientColor (material.ambientColor)
#define materialDiffuseColor (material.diffuseColor)
uniform lowp vec4 baseLightingColor;
uniform vec4 ambientTerm[3];

void main()
{
    // int currLight;
    lowp vec4 color;


    color = v_color;


    color = clamp(color, 0.0, 1.0);


    out_color = color;
}

1 Answer 1

2

I think the relevant lines from your code are:

v_color = a_color;
v_color = tmpFrontColor;

So, you're assigning a_color to v_color, but immediately replacing it with something else.

Nvidia is optimizing a_color away because it can. iOS does not optimize a_color away because it doesn't have to. It's not really worth trying to analyze what the drivers are doing beyond that.

Not the answer you're looking for? Browse other questions tagged or ask your own question.