android 不同机型 bug,[Android] Opengl ES 机型适配 bug 汇总

标签 : glsl, shader, opengl, android, 黑屏

自己写的 opengl shader 脚本有时会出现部分机型上运行正常, 部分机型上黑屏的 bug. 下面是笔者开发过程中遇到的问题及解决方案.

1 部分机型不支持在 main() 函数中调用 return

原本计划在着色器中实现以下 shader 逻辑

//...

varying vec2 textureCoordinate;

uniform samplerExternalOES s_texture;

uniform int flag;

void main() {

if (flag > 0) {

gl_FragColor = texture2D(s_texture, textureCoordinate);

return;

}

//...

}

该方法在大部分机型上都运行正常, 但是少部分机型上黑屏. 调试发现, 这部分机型不支持在 main() 函数中调用 return. 所以唯一的修改方案是:

//...

varying vec2 textureCoordinate;

uniform samplerExternalOES s_texture;

uniform int flag;

void main() {

if (flag > 0) {

gl_FragColor = texture2D(s_texture, textureCoordinate);

} else {

//...

}

}

2 部分机型不支持在 main() 函数中申明新的浮点变量

这个 bug 比较无语. 预期想在图片四角实现效果, 逻辑如下:

//...

varying vec2 textureCoordinate;

uniform samplerExternalOES s_texture;

void main() {

float r = abs(textureCoordinate.x - 0.5) + abs(textureCoordinate.y - 0.5) - 0.5;

if (r > 0.0) {

//...

} else {

//...

}

}

稳定的解法是把原来的局部变量变成全局的:

//...

varying vec2 textureCoordinate;

uniform samplerExternalOES s_texture;

highp float r;

void main() {

r = abs(textureCoordinate.x - 0.5) + abs(textureCoordinate.y - 0.5) - 0.5;

if (r > 0.0) {

//...

} else {

//...

}

}

2 OpenGL ES 1.0 以后版本不支持在申明数组同时初始化数组

There is no mechanism for initializing arrays at declaration time from within a shader.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值