Android GLSL片段着色器不会在未注释texture2D调用的情况下编译
我有一个N910f注释4和最后一个棒棒糖5.0.1。片段着色器没有编译,但是如果我注释掉纹理采样线texture2D,则调用着色器编译。我不知道编译器发生了什么,因为Log.eTAG、GLES20.glGetShaderInfoLogshader调用没有抛出任何信息 这是我的片段glsl代码:Android GLSL片段着色器不会在未注释texture2D调用的情况下编译,android,opengl-es,glsl,opengl-es-2.0,glsles,Android,Opengl Es,Glsl,Opengl Es 2.0,Glsles,我有一个N910f注释4和最后一个棒棒糖5.0.1。片段着色器没有编译,但是如果我注释掉纹理采样线texture2D,则调用着色器编译。我不知道编译器发生了什么,因为Log.eTAG、GLES20.glGetShaderInfoLogshader调用没有抛出任何信息 这是我的片段glsl代码: #define numLights 1 #pragma glsl precision mediump float; struct LightSourceParameters { vec3 am
#define numLights 1
#pragma glsl
precision mediump float;
struct LightSourceParameters {
vec3 ambient;
vec3 lightColor;
vec4 position;
float spotExponent;
float spotCutoff; // (range: [0.0,90.0], 180.0)
vec3 spotDirection;
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
};
uniform LightSourceParameters LightSource[numLights];
struct MaterialParameters {
vec4 emission;
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
bool hasDiffuseTexture;
bool hasSpecularTexture;
bool hasEmissionTexture;
bool hasAmbientTexture;
bool hasNormalTexture;
sampler2D diffuseTexture;
sampler2D specularTexture;
sampler2D emissionTexture;
sampler2D ambientTexture;
sampler2D normalTexture;
};
uniform MaterialParameters Material;
varying vec2 outTextCoord;
varying vec3 outNormal;
varying vec3 outViewVector;
varying vec3 outLightVector[numLights];
/* Declaramos cabecera de funcion, necesaria para que GLSL no diga que la funcion no existe, al definirse despues de main */
vec4 computeLight(in MaterialParameters material, in LightSourceParameters lightSource, in vec3 normal, in vec2 textCoord, in vec3 lightVector, in vec3 halfVector);
vec4 computeEmissionLight(in MaterialParameters material, in vec2 textCoord);
void main(){
// Normalize the incoming vectors
vec3 normal = normalize(outNormal);
vec3 viewVector = normalize(outViewVector);
// BACKFACE CULLING:
float NdotVV = dot(normal,viewVector);
if (NdotVV <= 0.0) discard;
// normalize lightvector, compute half vectors and lights
vec4 totalColorLighting = vec4(0.0);
for (int indexComputeLights = 0; indexComputeLights < numLights; indexComputeLights++){
LightSourceParameters light = LightSource[indexComputeLights];
vec3 currentLightVector = vec3(0.0);
vec3 currentHalfVector = vec3(0.0);
if (length(light.ambient) == 0.0 ){ /* no es ambiental, que no tienen vector ni half vector */
currentLightVector = outLightVector[indexComputeLights]; // normalizamos posteriormente, para poder obtener la distancia a la luz del m�dulo de este vector
currentHalfVector = normalize(outLightVector[indexComputeLights] + outViewVector);
}
else {
// nothing
}
/* Si la luz es ambiental, halfVector y lightVector son
* indefinidos para esa luz, pero da igual porque no son
* utilizados en el algoritmo que calcula las luces
*/
/*
totalColorLighting = totalColorLighting + computeLight(Material, light, normal, outTextCoord, currentLightVector, currentHalfVector);
totalColorLighting = clamp(totalColorLighting,vec4(0.0),vec4(1.0));
*/
}
totalColorLighting = totalColorLighting + computeEmissionLight(Material, outTextCoord);
totalColorLighting = clamp(totalColorLighting,vec4(0.0),vec4(1.0));
/* Devolvemos el color de fragmento calculado para almacenarlo en el framebuffer */
totalColorLighting = vec4(1.0,1.0,0.34, 0.68);
gl_FragColor = clamp(totalColorLighting,vec4(0.0),vec4(1.0));
// gl_FragColor = vec4(1.0)*vec4(outTextCoord,1.0,1.0);
}
vec4 computeEmissionLight(in MaterialParameters material, in vec2 textCoord){
vec4 totalEmissionColorLighting = vec4(0.0, 0.0, 0.0, 0.0);
if ((length(vec3(material.emission)) != 0.0) || (material.hasEmissionTexture)) {
/* El material tiene un termino emisivo, es decir, emite luz. Lo andimos al total de color calculado */
if (!material.hasEmissionTexture) {
totalEmissionColorLighting = material.emission;
}
else {
totalEmissionColorLighting.rgba = texture2D(material.emissionTexture, textCoord).rgba;
// texture2D(material.diffuseTexture, textCoord);
}
}
return totalEmissionColorLighting;
}
除了我糟糕的编码方式之外,是否有人看到了错误?我不知道为什么GLSL编译器在编译此着色器失败时不返回编译器信息日志,而是使用不透明类型的结构(例如sampler2D)会导致问题。不透明类型不能在着色器运行时指定值,因此将它们作为结构的一部分传递给函数并不总是可行的。一些编译器允许这样做,但许多编译器不允许这样做 如果编译器遇到以下问题,重写着色器可能会有所帮助:
我在过去写过一个相关的答案,更详细地解释了这一点。用不透明类型sampler2D实例化结构对于GLSL中的统一之外的任何东西都是无效的。如果您直接使用材质统一,您的函数可能会比传递材质参数更好。@Andon M.Coleman谢谢!它正在按照你的建议工作。请添加您的评论作为选择答案,并向上投票。
totalEmissionColorLighting.rgba = texture2D(material.emissionTexture, textCoord).rgba; // tried also without rgba
vec4 computeEmissionLight(in vec2 textCoord){
vec4 totalEmissionColorLighting = vec4(0.0, 0.0, 0.0, 0.0);
if ((length(vec3(Material.emission)) != 0.0) || (Material.hasEmissionTexture)) {
/* El material tiene un termino emisivo, es decir, emite luz. Lo andimos al total de color calculado */
if (!Material.hasEmissionTexture) {
totalEmissionColorLighting = Material.emission;
}
else {
totalEmissionColorLighting.rgba = texture2D(Material.emissionTexture, textCoord).rgba;
// texture2D(Material.diffuseTexture, textCoord);
}
}
return totalEmissionColorLighting;
}