0
\$\begingroup\$

im totally newbie and this just for fun

afaik lot of best practice suggest to minimize usage of branching.

can you help me optimize below glsl especially on branch part? ( target 120 )

the bgcolor used for minimum color level, what i want is alpha outcome become 0.1 if the bgColor choosen on the current fragment otherwise it will get the data from uniform.

bgColor n uLightIntensity will same value for all fragment aplied within this shader.

the texcolor will be varies for each fragment ( depend on texture pixel presentation)


    varying  vec3 v_fragmentColor;
    varying  vec2 v_texCoord;

    uniform sampler2D u_texture;
    uniform  float u_lightIntensity;
    uniform vec4 u_bgColor;

   void main()
{


    vec4 texColor = texture2D(u_texture,  v_texCoord);


    // TODO POSIBLE OPTIMIZE FOR NON BRANCHING
    if(step( u_bgColor.r, texColor.r) == 0.0)
    {
        texColor.a = 0.1;
    }else
    {
        texColor.a =  u_lightIntensity/10.0;
    }
    texColor.rgb = max(texColor.rgb, u_bgColor.rgb);

    gl_FragColor = texColor;

}

\$\endgroup\$
1
\$\begingroup\$

replace your whole if-statement with this:

float vstep = step( u_bgColor.r, texColor.r);
texColor.a = mix(0.1, u_lightIntensity/10.0, vstep);

mix will select either the 1st argument of the mix if vstep == 0, or the 2nd otherwise (which is vstep == 1)

\$\endgroup\$
  • \$\begingroup\$ yeahh, this gem what i looking for, step + mix remove any simple branching. babis thank you \$\endgroup\$ – rz_05 May 11 '15 at 22:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.