I have a RenderTexture with a depth component. I render objects and depth into it and verify this in RenderDoc. I then pass that texture to another shader like so
m_blitToScreenCommand.SetGlobalTexture( "_MainTex", m_overrideTexture.colorBuffer );
m_blitToScreenCommand.SetGlobalTexture( "_MainDepth", m_overrideTexture.depthBuffer );
m_blitToScreenCommand.DrawMesh( screenMesh, Matrix4x4.identity, customBlit );
However, when I check in RenderDoc, the shader is receiving two copies of the colour buffer. It doesn’t seem to matter if the camera is running in deferred or forward rendering. Unity version 2017.2
Here is the shader
Shader "Test"
{
// Command buffers write to global keywords so "white" will always take priority because it is more specific than the global
//Properties
//{
// _MainTex ("Texture", 2D) = "white" {}
//}
SubShader
{
Tags{ "Queue" = "Geometry" "PreviewType" = "Plane" }
CGINCLUDE
#pragma enable_d3d11_debug_symbols
#pragma target 3.0
#include "UnityCG.cginc"
struct appdata
{
float3 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _MainDepth;
v2f vert(appdata v)
{
v2f o;
o.vertex = float4(v.vertex, 1);
o.uv = v.uv;
#ifndef UNITY_UV_STARTS_AT_TOP
o.uv.y = 1 - o.uv.y;
#endif
return o;
}
fixed4 frag(v2f i, out float out_depth : SV_Depth) : SV_Target
{
out_depth = UNITY_SAMPLE_DEPTH(tex2D(_MainDepth, i.uv));
return tex2D(_MainTex, i.uv);
}
ENDCG
Pass
{
ZWrite on
ZTest off
Cull off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
}