Light cookie performance cost ?

Ok , so this may be some elephant in the room type of topic as I can’t find any information about this.

So , like everything in life, nothing is free… I get that, but there isn’t any real explanation on how light cookie works in behind the scene to cause extra tri count and draw calls. They are not as significant as using shadow, but it still adds a lot.

Does any one have information about the exact performance cost of using the light cookie? Especially direction light cookie? How are they calculated and how does their additional cost comes to be? And why?

I guess it may be different from lighting path, so I am using deferred lighting.

Thanks!

A light cookie vs a light with the same settings but without a cookie should be nearly identical in performance for most situations. The difference between the two is an additional matrix multiplication in the vertex shader and an additional texture lookup in the fragment shader. In a spotlight or point light I believe this is the same matrix mul done for shadows, so it’s even less of an impact if you already have shadows enabled for those. Unless you’re already either limited by vertex performance or texture reads it’s unlikely you’ll be able to even see a difference in the framerate with the cookie enabled or disabled on any desktop computer made in the last several years.

As for additional draw calls you might be seeing, I don’t really have an answer for you. Might be a bug, or something I’m missing in my personal understanding of the deferred rendering path, but I don’t know of any reason why light cookies should add any additional draw calls in any situation.

That is what I thought myself as well, but enabling cookie for directional light does increase tri count as well as draw calls for some reason… just really puzzling to me.

I would suggest you use the frame debugger to try and see what’s happening.

Sorry for responding to this old thread but I have been debugging why a directional light cookie tanks frame rate by 30% and I found that it is far from “free”. Using nvidia nsight frame debugger I was able to figure out that when light cookie is enabled, the entire scene is rendered twice. Once with ForwardBase pass with no directional light and once with ForwardAdd pass adding the light with cookie. The scene I was testing this on had a single quad with standard material and there was a single directional light.

Is there any way to do single-pass light cookie? Or am I missing something obvious in the setup?

This is how Unity’s built in forward renderer handles rendering multiple lights, and the base pass does not support light cookies. So no, there’s no way to do a directional light with a light cookie in a single pass.*

  • But you can do it without a light cookie!

There are multiple ways to go about this. For example you could write a custom shader that does apply a “light cookie”, or any arbitrary texture, to the base pass’s shadows. But that requires completely replacing the Standard shader with your custom shader on all materials in the game you want the cookie to be visible on.

The alternative is you could apply a “light cookie” to the screen space shadow texture, assuming you’re targeting a platform that uses the screen space shadow texture for directional lights (that is as long as you’re not targeting mobile).

You’d want to take the existing screen space shadow shader, and modify it to take an input “cookie” texture instead. Then use command buffers to Blit a material using that shader to the main light’s screen space shadow buffer during the LightEvent.AfterScreenspaceMask event.

https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/DefaultResourcesExtra/Internal-ScreenSpaceShadows.shader

1 Like

Thank you for the quick answer, bgolus! Would you happen to know why Unity’s built in forward renderer base pass does not support light cookies? I have not found any documentation about this and it really caught me off-guard. The only good article I’ve found that talks about it is this one: https://en.wikibooks.org/wiki/Cg_Programming/Unity/Cookies

Thanks for suggesting alternative solutions. Rewriting all shaders to support cheap cookie sounds like a huge task. We use lots of custom shaders incl. surface shaders which would presumably have to be all rewritten to non-surface shaders to inject custom shadows handling.

Applying cookie to the screen space shadow texture sounds interesting, I will investigate this option.