着色器编译产生不同的结果

Shader compilation produces different result

我在不同的设备上使用相同的着色器,iOS 和具有 OpenGL ES 配置文件的桌面 nVidia。编译着色器很好,没有任何错误或警告,但是当我尝试使用 glGetAttribLocation 时,nVidia 驱动程序似乎优化了我的属性 "a_color" 而 ios 没有。我知道使用 glBindAttribLocation 来确保属性在那里,但我只想知道为什么 nVidia 驱动程序会这样做。

顶点着色器:

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

}

片段着色器:

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

我认为您代码中的相关行是:

v_color = a_color;
v_color = tmpFrontColor;

因此,您将 a_color 分配给 v_color,但立即将其替换为其他内容。

Nvidia 正在优化 a_color,因为它可以。 iOS 不会优化 a_color ,因为它没有必要。除此之外,真的不值得去分析司机们在做什么。