通常情况下,for循环中使用texture2D,不会出现什么问题,但是安卓的情况比较复杂,部分机型还是出现了问题。现把问题记录一下,可能对其他人有帮助。
-
机型 : nova 7 5G
-
系统 : Android 10.0
-
现象 : 画面闪屏
-
问题代码段:
... ... for(int i=radius;i>=1;i--) { vec2 curBottomCoordinate = uv+vec2(0.0, float(i))*unit_uv; vec2 curTopCoordinate = uv+vec2(0.0, float(-i))*unit_uv; sum+=texture2D(Img,curBottomCoordinate).rgb*t_weight[i]; sum+=texture2D(Img,curTopCoordinate).rgb*t_weight[i]; sum_weight+=t_weight[i]*2.0; } ... ...
-
问题反馈:
shader不符合spec,提示 “ Some texture functions (non-“Lod” and non-“Grad” versions) may require implicit derivatives. Implicit derivatives are undefined within non-uniform control flow and for vertex texture fetches” -
知识点备忘:
以下摘自参考文献,仅供参考。并不是所有平台均不适用。
在使用 for 循环时也有一些约束,如循环变量的值必须是编译时已知。如下:for (int i = 0; i < 3; i++) { sum += i; }
在 GLSL 中使用循环时一定要注意:只有一个循环变量,循环变量必须使用简单的语句来增减(如 i++, i–, i+=constant, i-=constant等),循环终止条件也必须是循环变量和常量的简单比较,在循环内部不能改变循环变量的值。
以下代码是 GLSL 中不支持的循环用法的示例(这里存在疑惑,并不是在所有平台都不适用):float myArr[4]; for (int i = 0; i < 3; i++) { // 错误, [ ]中只能为常量或 uniform 变量,不能为整数量变量(如:i,j,k) sum += myArr[i]; } ... uniform int loopIter; // 错误, 循环变量 loopIter 的值必须是编译时已知 for (int i = 0; i < loopIter; i++) { sum += i; }
参考:
GLSL 详解(基础篇)