GLSL Optimizer

https://github.com/aras-p/glsl-optimizer


GLSL Optimizer is a C++ library that takes GLSL shaders, performs some GPU-independent optimizations on them and outputs GLSL or Metal source. The tool has been developed by Aras Pranckevičius of Unity Technologies.

GLSL Optimizer provides optimizations in the form of function in-lining, dead code removal, copy propagation, constant folding, constant propagation, arithmetic optimizations and many more.

Example of GLSL Optimized Code with GLSL Optimizer

uniform vec4 _Color;
uniform vec4 _LightColor0;
uniform sampler2D _MainTex;
uniform vec4 _WorldSpaceLightPos0;
void main ()
{
    vec4 c;
    vec4 tmpvar_32;
    tmpvar_32 = texture2D (_MainTex, gl_TexCoord[0].xy) * _Color;
    vec3 tmpvar_33;
    tmpvar_33 = tmpvar_32.xyz;
    float tmpvar_34;
    tmpvar_34 = tmpvar_32.w;
    vec4 c_i0_i1;
    c_i0_i1.xyz = ((tmpvar_33 * _LightColor0.xyz) *
      (max (0.0, dot (gl_TexCoord[1].xyz, _WorldSpaceLightPos0.xyz)) * 2.0)).xyz;
    c_i0_i1.w = (vec4(tmpvar_34)).w;
    c = c_i0_i1;
    c.xyz = (c_i0_i1.xyz + (tmpvar_33 * gl_TexCoord[2].xyz)).xyz;
    c.w = (vec4(tmpvar_34)).w;
    gl_FragData[0] = c.xyzw;
}

Example of Difference between Non-Optimized and Optimized GLSL

non-optimized vs optimized glsl