Getting depth texture in OnRenderImage()

Hi all,

I’m using a compute shader to generate some fancy effects and need access to the depth texture.
The part I’m stuck on though is how to get access to it in OnRenderImage().

I’m using the standard 3D project pipeline and targeting Windows stand alone. I’m rendering in deffered mode.

I have turned on depth mode for the camera using: m_Camera.depthTextureMode = DepthTextureMode.Depth;

I tried:

void OnRenderImage(RenderTexture source, RenderTexture destination)
{
    Texture depth = Shader.GetGlobalTexture("_CameraDepthTexture");
        if(depth != null)
        {
            m_RayMarchShader.SetTexture(0, "_DepthTexture", depth);
        }
///more code here

But the texture that arrives in the shader is empty.

I tried blitting it straight to the screen and just get a black screen:

void OnRenderImage(RenderTexture source, RenderTexture destination)
{
    Texture depth = Shader.GetGlobalTexture("_CameraDepthTexture");

    Graphics.Blit(depth, destination);
}

I also tried other ways of getting the depth texture:
m_RayMarchShader.SetTextureFromGlobal(0, "_DepthTexture", "_CameraDepthTexture");

and

m_RayMarchShader.SetTexture(0, "_DepthTexture", source, 0, UnityEngine.Rendering.RenderTextureSubElement.Depth);

but got the same result.

Am I missing something? Is there a better way to get the depth texture for the current frame in OnRenderImage()?

Nevermind, fixed it.
In case anyone else has this issue: it was actually working fine, but the values in the depth texture are very close to zero and so appear black.