title: unity3d: Use main camera's depth buffer for rendering another camera view
body: <p>After my main camera renders, I'd like to use (or copy) its depth buffer to a (disabled) camera's depth buffer. 
  My goal is to draw particles onto a smaller render target (using a separate camera) while using the depth buffer after opaque objects are drawn.
  I can't do this in a single camera, since the goal is to use a smaller render target for the particles for performance reasons.</p>
  
  <p>Replacement shaders in Unity aren't an option either: I want my particles to use their existing shaders - i just want the depth buffer of the particle camera to be overwritten with a subsampled version of the main camera's depth buffer before the particles are drawn.</p>
  
  <p>I didn't get any reply <a href="https://stackoverflow.com/questions/22653093/unity3d-drawing-particles-after-aa-resolve-for-performance">to my earlier question</a>; hence, the repost.</p>
  
  <p>Here's the script attached to my main camera. It renders all the non-particle layers and I use <code>OnRenderImage</code> to invoke the particle camera.</p>
  
  <pre><code>public class MagicRenderer : MonoBehaviour {
  public Shader   particleShader; // shader that uses the main camera's depth buffer to depth test particle Z
  public Material blendMat;       // material that uses a simple blend shader
  public int      downSampleFactor = 1;
  
  private RenderTexture particleRT;
  private static GameObject pCam;
  
  void Awake () {
      // make the main cameras depth buffer available to the shaders via _CameraDepthTexture
      camera.depthTextureMode = DepthTextureMode.Depth;
  }
  
  // Update is called once per frame
  void Update () {
  
  }
  
  void OnRenderImage(RenderTexture src, RenderTexture dest) {
              // create tmp RT
              particleRT = RenderTexture.GetTemporary (Screen.width / downSampleFactor, Screen.height / downSampleFactor, 0);
              particleRT.antiAliasing = 1;
  
              // create particle cam
              Camera pCam = GetPCam ();
              pCam.CopyFrom (camera); 
              pCam.clearFlags = CameraClearFlags.SolidColor;
              pCam.backgroundColor = new Color (0.0f, 0.0f, 0.0f, 0.0f);
              pCam.cullingMask = 1 &lt;&lt; LayerMask.NameToLayer ("Particles");
              pCam.useOcclusionCulling = false;
              pCam.targetTexture = particleRT;
              pCam.depth = 0;
  
              // Draw to particleRT's colorBuffer using mainCam's depth buffer
              // ?? - how do i transfer this camera's depth buffer to pCam?
              pCam.Render ();
              // pCam.RenderWithShader (particleShader, "Transparent"); // I don't want to replace the shaders my particles use; os shader replacement isnt an option.
  
      // blend mainCam's colorBuffer with particleRT's colorBuffer
      // Graphics.Blit(pCam.targetTexture, src, blendMat);        
  
      // copy resulting buffer to destination
      Graphics.Blit (pCam.targetTexture, dest);
  
  
      // clean up
      RenderTexture.ReleaseTemporary(particleRT);
  }
  
  static public Camera GetPCam() {
      if (!pCam) {
          GameObject oldpcam = GameObject.Find("pCam");
          Debug.Log (oldpcam);
          if (oldpcam) Destroy(oldpcam);
  
          pCam = new GameObject("pCam");
          pCam.AddComponent&lt;Camera&gt;();
          pCam.camera.enabled = false;
          pCam.hideFlags = HideFlags.DontSave;
      }
  
      return pCam.camera;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I've a few additional questions:</p>
  
  <p>1) Why does <code>camera.depthTextureMode = DepthTextureMode.Depth;</code> end up drawing all the objects in the scene just to write to the Z-buffer? Using Intel GPA, I see two passes before <code>OnRenderImage</code> gets called: 
  (i) Z-PrePass, that only writes to the depth buffer 
  (ii) Color pass, that writes to both the color and depth buffer.</p>
  
  <p>2) I re-rendered the opaque objects to pCam's RT using a replacement shader that writes (0,0,0,0) to the colorBuffer with <code>ZWrite On</code> (to overcome the depth buffer transfer problem). After that, I reset the layers and clear mask as follows:</p>
  
  <pre><code>pCam.cullingMask = 1 &lt;&lt; LayerMask.NameToLayer ("Particles");
  pCam.clearFlags = CameraClearFlags.Nothing;
  </code></pre>
  
  <p>and rendered them using <code>pCam.Render()</code>.</p>
  
  <p>I thought this would render the particles using their existing shaders with the ZTest.
  Unfortunately, what I notice is that the depth-stencil buffer is cleared before the particles are drawn (inspite me not clearing anything..). </p>
  
  <p>Why does this happen?</p>