1

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

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 个不同的着色器是低效的……任何帮助将不胜感激。

4

0 回答 0