Accessing _CameraDepthTexture usage in Deferred vs Forward shader

Hi all,

Looking at this page in the documentation:

It describes the availability of _CameraDepthTexture within shaders, and indeed, this works perfectly when reading from _CameraDepthTexture in a Forward shader.

The documentation (linked above) sort of implies that this texture is also available in Deferred Shading:

“Typically when using Deferred Shading or Legacy Deferred Lighting rendering paths, the depth textures come “for free” since they are a product of the G-buffer rendering anyway”

There is nothing on that page that says or implies that _CameraDepthTexture is only available in Forward.

However, when using a Deferred version of the same shader, reading from _CameraDepthTexture does not give a valid result.

Can someone maybe confirm for sure that _CameraDepthTexture is not supposed to be available in a Deferred shader (i.e. one that outputs to G-buffer render targets)?

More info:

  • Render path is set to deferred.
  • Scene is rendered partially deferred and partially forward.
  • Camera inspector shows Depth texture is being created.

Thanks!

The bit of information that’s missing is that the depth texture is only available during the transparency queues (ie: on objects that are forward rendered) when using deferred rendering. The depth texture is generated by the deferred gbuffer pass, which means if you’re currently rendering to it you can’t access it.

Basically deferred goes like this:

  • Render all opaque objects to gbuffer
  • Copy depth buffer into _CameraDepthTexture after gbuffer is filled
  • Render all non-deferred objects using forward rendering

Forward rendering the depth texture is generated is a extra pass before rendering the main view.

  • Render all opaque objects to depth buffer
  • Copy depth buffer into _CameraDepthTexture
  • Render everything normally

Thus the texture is available essentially from the beginning. There’s a lot of other stuff that can happen between those steps that I’m not listing, but that’s the order.

If you need a depth texture during the gbuffer pass, you’ll need to generate this yourself using a replacement shader pass.

Thank you very much for clarifying - it now makes sense that the _CameraRenderTexture variable can only be read during forward rendering or in forward transparency phase after deferred.

However - I do think that page in the documentation should be made clearer in this regard.
I can’t be the first person to get confused over what that page is implying / not implying.

Thanks again!