写了一些shader,在windows上测试是正确的,在安卓手机上测试,有些机型显示是正常的,有些机型则直接闪退,查log发现是glProgram并没有正确创建出来。又试了下网易的一款模拟器,显卡渲染模式选择directx就会崩溃,而选择opengl就是正常的。
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform float factor;
uniform float width;
uniform vec3 color;
uniform float offset;
void main()
{
vec4 texColor = texture2D(CC_Texture0, v_texCoord);
float distance = abs(v_texCoord[0]+v_texCoord[1]-offset)/1.414;
distance = 1.0-(1.0/width)*distance;
distance = max(distance, 0.0);
vec4 sample = vec4(0.0,0.0,0.0,0.0); sample[0] = color[0]/255 * distance; sample[1] = color[1]/255 * distance; sample[2] = color[2]/255 * distance; sample[3] = distance; float alpha = sample[3]*texColor[3]; texColor[0] = texColor[0] + sample[0]*alpha*factor; texColor[1] = texColor[1] + sample[1]*alpha*factor; texColor[2] = texColor[2] + sample[2]*alpha*factor; gl_FragColor = v_fragmentColor * texColor;
}
功能是比较简单的一个高亮斜条
然后我又拿灰度shader做测试,
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
void main(void)
{
vec4 c = texture2D(CC_Texture0, v_texCoord);
gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b);
gl_FragColor.w = c.w;
}
这种情况下是对的,在小米MAX和极速模式下表现正常,然而当我改成
vec4 c = texture2D(CC_Texture0, v_texCoord); float scale = 6.0/10; gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b)*scale; gl_FragColor.w = c.w;
在小米MAX和极速模式下就glProgram初始化失败了~但是却在部分机型和兼容模式下是表现正常的
看到6.0/10的时候我想了一下是不是可能浮点格式不够严谨造成的呢,然后就试了下改成6.0/10.0
结果发现正常了!!
于是我又将上面那一段shader改成严格的浮点数格式,终于在小米MAX上测试通过了!!