CommandBuffer: Can't find _CameraDepthTexture in non-temporary RenderTexture

If you do something like this:

StencilTempTexture = new RenderTexture(Screen.width, Screen.height, 24);
StencilTempTextureIdentifier = new RenderTargetIdentifier(StencilTempTexture);

StencilBuffer = new CommandBuffer();
StencilBuffer.name = "Stencil Post Processor";
         
StencilBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.CameraTarget);
StencilBuffer.Blit(BuiltinRenderTextureType.CameraTarget, StencilTempTexture, PostprocessMaterial);

Camera.main.AddCommandBuffer(CameraEvent.[TriedThemAll], StencilBuffer);

The above code will generate an error:

CommandBuffer: temporary render texture _CameraDepthTexture not found while executing (blit destination / source):

I don’t see why it should complain about the lack of a depth texture, when my StencilTempTexture’s depth is set to 24, and I specifically told it in SetRenderTarget() to target the depth from BuiltinRenderTextureType.CameraTarget.

I event tried doing this:

Camera.main.targetTexture = StencilTempTexture;

This renders the camera directly into my render texture… And it STILL complains that there’s no _CameraDepthTexture found.

I tried this also:

StencilBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, BuiltinRenderTextureType.Depth);

But then it gives this error:

So it won’t let me set the RenderTarget depth to BuiltinRenderTextureType.Depth, and neither can it seem to find the camera’s depth texture in StencilTempTexture even if my camera is rendering directly to it.

I’m trying to use a non-temporary rendertexture so that I can feed it back into my post processor shader at the end of the rendering pipeline.

PS. I also tried NOT telling the camera to render to any texture, and also tried NOT setting the SetRenderTarget(), and I even tried other depth values other than 24, and I tried every variation of these (and other things), I even tried doing all of this on a second camera (after a previous camera had already finished rendering), and no matter what I’ve tried it still fails with these same errors regardless.

It seems like this is unreasonably hard. If you use any temporary RT these errors go away. But I need non-temporary (I think?) so that I can use it later in my post-processing shader.

I never did figure out any of the reason why all the above code didn’t work, or why these errors were happening. However, I did finally get past these errors and I got a working example scene finished. I posted that scene here in case anyone else is struggling with an issue similar: Has anyone ever gotten a stencil buffer to copy with CommandBuffer.Blit? - Unity Engine - Unity Discussions

1 Like