title: Premultiplied Alpha And Alpha Test
body: <p>I have a shader that is supposed to work with either alpha blending or alpha testing, but the color values being passed in are premultiplied alpha values.</p>
  
  <p>Is there an easy/standard way to have it produce the "correct" results for both alpha blending and alpha testing when using premultiplied alpha?</p>
  
  <p>For example, if the final result of my shader is <code>RGBA(1,0,0,0.75)</code> straight alpha and therefore <code>RGBA(0.75,0,0,0.75)</code> premultiplied alpha, the result should be remain <code>RGBA(1,0,0,0.75)</code> when alpha testing.</p>
  
  <p>Perhaps one option is "dividing out" the alpha channel if we are alpha testing, i.e. divide the <code>R</code> by <code>A</code> above so that we get <code>0.75/0.75 = 1.0</code>, our original red value.  But this becomes non-deterministic when the alpha goes to 0.</p>
  
  <p>Say the back buffer is <code>RGBA(1,1,1,1)</code>, and i am rendering with a partially transparent green color <code>RGBA(0,1,0,0.75)</code> (straight alpha).  The color is passed in as premultiplied alpha as <code>RGBA(0,0.75,0,0.75)</code>.  When alpha <em>blending</em>, i blend using one + invsrcalpha and get a final color of <code>RGBA(0.25,1,0.25,1)</code>. So my green channel stays at 100%, because the back buffer had green at 100%, and so did my original green color, so no way to get less than 100% on the green channel.</p>
  
  <p>When Alpha <em>testing</em>, there is no blending going on. Instead, my green color will be rendered straight to the back buffer as <code>RGBA(0,0.75,0,1)</code>.  But the color I wanted was <code>RGBA(0,1,0,1)</code>.</p>