webgl glsl程序出错: Loop index cannot be compared with non-constant expression

techbrood 发表于 2019-07-12 20:40:31

标签: glsl, webgl, shader, error

- +

出现错误的代码如下:

#ifdef GL_ES
precision mediump float;
#endif

// our texture
uniform sampler2D u_image;

// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;

uniform float u_glowRange;

void main()
{
    vec4 color = vec4(0,0,0,0);
    for( float j = 1.0; j<=u_glowRange; j += 1.0 )
    {
        //calculate color
    }

    gl_FragColor = color;
}

发生这种情况是因为在某些硬件上,glsl循环没有被内置入到本机GPU机器指令中,这意味着for循环的执行次数需要有一个硬上限,该上限控制将生成多少循环内部代码的副本。如果用const float或甚至是define指令替换uniform,那么着色器编译器可以确定编译时的循环次数(代码拷贝次数),并相应地生成代码。但如果是通过uniform常量来传递的,那么这个上限在编译时就不知道了。

所以解决问题的办法是把u_glowRange常量换成一个RANGE常数(const float RANGE = 10.0 )

possitive(16) views9010 comments0

发送私信

最新评论

请先 登录 再评论.
相关文章