2D Shadow Rendering: Too Many ShadowTex_0 Allocated?

Hello, I’ve started digging into our game’s memory profile and and seeing a mystery I’m hoping to get some insight into. Our game uses the 2D lighting + shadow system and the Max Shadow Render Textures is set to 1.

Looking at the code in ShadowRendering.cs:48, I would expect this to correctly allocate a single shadow texture named “ShadowTex_0”. However, snapshotting our player with the Memory Profiler reveals that many such textures are actually being allocated, all with the same name.

It happens to be 16 in this screenshot, but it keeps increasing as the game executes, and they really chew through our memory budget. Putting a Log statement in ShadowRendering.cs as the texture(s) get allocated only prints once, which I would expect.

Can someone at Unity give me some insight into where these other textures are coming from? Thanks -

After some more profiling, the number of shadow textures seems correlated to the number of Light2D components in the hierarchy and isn’t capped by the Max Shadow Render Textures value.

The only way it seems to limit the memory allocated towards shadow textures is to reduce the overall lighting render scale, limit the number of Light2Ds in the hierarchy, or perhaps turn off 2D shadow caster components?

The good news is that setting lighting render scale to 0.25 makes each of our dozens of shadow textures go from 24mb to 1.5mb apiece, this seems like a huge improvement with a very acceptable loss in visual quality.

I think I answered my own question, thanks for a solid 2D lighting system, Unity! Been a pleasure to work with and has really upped the look of our game : )

1 Like