Why enabling shadows are triggering UpdateDepthTexture on the main camera

Hi, while i was using frame debugger, i find out a behaviour that i dont understand. Enabling shadows causes camera to call UpdateDepthTexture, i am not sure why that is needed. It is rendering depth texture from main camera view angle which is not used by the shadow.

This is the depth texture from game view angle which is not needed at all. I am not using any image effect that enables camera’s depth texture mode. It re-draws every mesh again, causing unnecessary drawcalls

This is the shadowmap depth texture by the shadow caster.

Disabling shadows makes it disappear.

I would appreciate an explanation.

Unity’s main directional light cast its shadows on the camera depth texture to produce a screen space shadow texture rather than sampling the shadow map directly in the object shaders. This is an optimization technique a lot of games use.

In my case, UpdateDepthTexture is causing a massive scene to be rendered twice, and I think it would be much faster to actually sample the shadow map directly in the shaders rather than calling UpdateDepthTexture, if that’s possible. Is there any way/insight/documentation on using this approach?

On desktop, it’s rare for draw calls or vertex counts to actually be a limiting factor, and if it is it’s probably a sign you should be using LODs anyway.

The depth texture pass is extremely inexpensive compared to rendering the scene. I just checked a scene from Falcon Age and the depth only pass took less than 10% as long to render as rendering the actual scene. Less time than even the rendering of the screen space shadow texture itself. And that’s the key. Sampling the shadow map can be relatively expensive, especially when doing soft shadows. For normal forward rendering, one of the performance killers is over shading. That is rendering surfaces that aren’t ever seen in the final result. Things like back to front sorting helps a lot with this, but it’s not perfect. And since shadow map sampling can be one of the more expensive aspects of shading in general, over shading while sampling the shadow maps will usually be more expensive than rendering the depth, then shadows to a screen space texture, then rendering sampling from the screen space texture.

And if the next question is “why don’t we fix the over shading problem?” The answer is the fix is to rendering a depth only pass first …

Thanks for the in-depth reply. The GPU profiler reports my depth/shadow pass taking more than 2/3rd of the frame, so I suspect something is wrong there, I’d have to investigate. Would you happen to know of any common performance pitfalls when authoring custom shadow caster passes?

If you’ve got a complex scene with a lot of stuff outside the view of the main camera but in view of the lights’ shadow maps, that can plausibly make them more expensive. But 2/3rd of the frame time is quite excessive. I can’t really think of anything that would make it that much more expensive unless you’re doing really complex stuff in the fragment shaders of your shadow caster pass.

Whatever tool you’re using for doing GPU profiling should be able to tell you more.