title: HLSL Depth buffer. Identify when a piexel is about to replace another at shader runtime
body: <p>I cannot find any useful info about the Depth Buffer and how it works. I know you can turn it on and off, but is it possible to do it at the shader runtime, in the PiexelShaderFunction? When does the clipping occur? Before the Pixel Shader starts? What if I want to track the event of clipping in the PiexelShaderFunction and do something when it happens?</p>
  
  <p>Ideally i need the following system:</p>
  
  <pre><code>    PixelShaderFunction
      {
      //... some code.
      if (clipping)
          return (0,0,value1,value2);
      else
          return(value1,value2,0,0);
      }
  </code></pre>
  
  <p>What i came up with from the top of my head is:</p>
  
  <pre><code>    PixelShaderFunction
      {
      //... some code.
      if (value1%2 == 0)
          return (0,0,value1,value2);
      else
          return(value1,value2,0,0);
      }
  </code></pre>
  
  <p>Which gives me 50% precision and is a good alternative, but I am forced to draw my scene twice because I need it to be drawn with depth stencil turned off first and then with the stencil turned on. If I could at least turn it on and off at PiexelShaderFunction at runtime that would help immensely, not to say that I still need to know if the clipping occurs.</p>
  
  <p>Edit: I've just read that "Write masks control what data gets written to a render target. The output write masks control on a per-render target, per-component level what data gets written to the render target(s)." Does it mean that I can specify which of the several render targets I have can use depth buffer and what cannot? If yes, how can I do it in XNA?</p>
  
  <p>More: How exactly is Stencil Buffer filled, and how exactly can you control it? How does it influence the depth buffer? Please if someone has links to examples, give them to me! MSDN is so sparse!</p>