iPhone GLSL动态分支行为

时间:2013-04-03 07:06:30

标签: iphone objective-c branch glsl shader

我知道分支不是编写着色器的好主意,但我没有想过要避免它的方法。这是我的片段着色器代码:

precision highp float;

varying vec4 v_fragmentColor;
varying vec4 v_pos;

uniform int u_numberOfParticles;

uniform mat4 u_MVPMatrix;

uniform vec3 u_waterVertices[100];

void main()
{
    vec4 finalColor = vec4(0.0, 0.0, 0.0, 0.0)

    vec2 currPos = v_pos.xy;

    float accum = 0.0;
    vec3 normal = vec3(0, 0, 0);

    for ( int i = 0; i < u_numberOfParticles; ++i )
    {
        // Some calculations here
    }
    normal = normalize(normal);

    float normalizeToEdge = 1.0 - (accum - threshold) / 2.0;

    if (normalizeToEdge < 0.3)
        finalColor = vec4( 0.1, normalizeToEdge + 0.5, 0.9-normalizeToEdge*0.4, 1.0);

    if ( normalizeToEdge < 0.2 )
    {
        finalColor = vec4( 120.0/255.0, 245.0/255.0, 245.0/255.0, 1.0);
        float shade = mix( 0.7, 1.0, normal.x);
        finalColor *= shade;
    }


    gl_FragColor = vec4(finalColor);

}

问题在于:

for ( int i = 0; i < u_numberOfParticles; ++i )
{
     // Some calculations here
}

将其更改为:

for ( int i = 0; i < 2; ++i )
{
    // Some calculations here
}
即使u_numberOfParticles也是2,

也会使帧速率加倍;

将其更改为

   for ( int i = 0; i < 100; ++i )
   {
       if( i == u_numberOfParticles)
            break;
       // Some calculations here
   }

不提供任何fps改进。

我如何应对这种着色器行为?是否有任何技术可以避免这种分支?我认为为不同数量的粒子编写50个不同的着色器是低效的......任何帮助都将受到赞赏。

0 个答案:

没有答案