title: Shader code optimization
body: <p>I have this code snippet (for cubemap PCF filtering). I would like to optimize it for shader model 2. I tried eliminating the branches with permutation matrices stored in uniforms, but it requires too much (2x24).</p>
  
  <pre><code>float3 l = normalize(ldir);
  float3 al = abs(l);
  float3 off2, off3, off4;
  
  if( al.x &lt; al.y )
  {
      if( al.y &lt; al.z )
      {
          // z is dominant
          off2 = CubeOffset(l.zxy, float2(0, 1), texelsize).yzx;
          off3 = CubeOffset(l.zxy, float2(1, 0), texelsize).yzx;
          off4 = CubeOffset(l.zxy, float2(1, 1), texelsize).yzx;
      }
      else
      {
          // y is dominant
          off2 = CubeOffset(l.yxz, float2(0, 1), texelsize).yxz;
          off3 = CubeOffset(l.yxz, float2(1, 0), texelsize).yxz;
          off4 = CubeOffset(l.yxz, float2(1, 1), texelsize).yxz;
      }
  }
  else
  {
      if( al.x &lt; al.z )
      {
          // z is dominant
          off2 = CubeOffset(l.zxy, float2(0, 1), texelsize).yzx;
          off3 = CubeOffset(l.zxy, float2(1, 0), texelsize).yzx;
          off4 = CubeOffset(l.zxy, float2(1, 1), texelsize).yzx;
      }
      else
      {
          // x is dominant
          off2 = CubeOffset(l, float2(0, 1), texelsize);
          off3 = CubeOffset(l, float2(1, 0), texelsize);
          off4 = CubeOffset(l, float2(1, 1), texelsize);
      }
  }
  </code></pre>
  
  <p>Perhaps a mathematical relation can be found between the comparisons (al.xyy &lt; al.yzz) and the swizzles.</p>
  
  <p><strong>UPDATE:</strong> definition of cubeoffset</p>
  
  <pre><code>float3 CubeOffset(float3 swiz, float2 off, float2 texelsize)
  {
      float3 ret;
  
      ret.yz = swiz.yz + 2.0f * off * texelsize;
      ret.x = sqrt(1.0f - dot(ret.yz, ret.yz));
  
      if( swiz.x &lt; 0 )
          ret.x *= -1.0f;
  
      return ret;
  }
  </code></pre>
  
  <p>And the HLSL error when compiling SM 2.0:</p>
  
  <pre><code>error X5608: Compiled shader code uses too many arithmetic instruction slots (107).
  Max. allowed by the target (ps_2_0) is 64.
  
  error X5609: Compiled shader code uses too many instruction slots (111).
  Max. allowed by the target (ps_2_0) is 96.
  </code></pre>
  
  <p>GLSL handles it fine. The goal is backward compatibility.</p>
  
  <p>(btw. the algorithm is faulty, but that's not an issue right now)</p>