Shadow technique #4

· by Steve · Read in about 4 min · (706 Words)

I just added a new shadow technique to OGRE, namely additive texture shadows, bringing the total number of techniques available to 4 (modulative stencil, additive stencil, modulative texture and additive texture). This technique allows you to gain the benefit of the additional lighting accuracy you get by using masked-out additive lighting, whilst using texture-based shadows which are more GPU-friendly than stencils. As before you can of course use custom caster & receiver shaders, and custom shadow texture formats, in order to implement more complex shadow setups such as depth shadow mapping.

Modulative shadows works by substracting light from the scene as a post-render pass. This is relatively fast, but also rather unrealistic since shadows darken areas where the light that cast them doesn’t reach - especially noticeable when there are multiple lights and / or ambient and emissive lighting. It also doesn’t play nice at all with things like specular effects, since the specular highlight from a light that can’t actually be seen will still be rendered, but just dulled afterward. This all adds up to a feeling of ‘wrongness’ when you look at a scene lit with a complex lighting set up using modulative shadows.

In constrast, additive lighting works by only ever adding light to the scene, and masking out areas that are in shadow for each light as they are rendered. This means that the light behaves like it does in real life, and extra effects like specular highlights are completely absent in areas of shadow, as they should be. The colour of shadows is also correct based on the absence of light rather than a constant darkening - when you’re using coloured lights this is very obvious. The downside is that it’s more expensive - requiring all materials to be split into ambient, per-light and decal passes; whilst OGRE organises this for you automatically if additive lighting is enabled it can’t change the fact that it’ll probably require more rendering passes.

OGRE has supported additive lighting when using stencil shadows for a long time indeed - about 18 months in fact. However it has only supported modulative shadows with texture shadows until now, mainly because I ran out of time in 0.14 when shadows appeared. I picked it up again since I had to re-analyse all the render queueing code anyway to implement custom render queue invocations (discussed a few days ago), so I took the time to make notes on what I would do to support this shadow technique too.

But why revisit texture shadows? Well, stencil shadows are great and all, but they’re becoming less attractive the more time goes past - because they require CPU-based geometry processing and dynamic vertex buffers, all the while that GPUs are increasingly pulling away from CPUs in terms of power, the expense of transferring data across the bus from CPU to GPU gets proportionately worse, and art assets are getting more and more complex. The beauty of texture shadows is that they require no extra geometry, no bus transfers, and are performed entirely in the GPU. They require a good fillrate to avoid hitting a ceiling, but the balance of recent cards is that they have this in spades and so texture-based shadows are becoming more and more the norm rather than the exception. My card is a couple of years old now, and it can still perform texture shadows faster than stencil shadows even for scenes of only a few thousand polys. You can also filter texture shadows to create soft shadows, and use shaders to perform some interesting tricks, neither of which you can do with a stencil based approach. So, in practice texture-based shadows are the future (you only have to look at any next-gen game to see that - they’re all using filtered texture shadows) and OGRE reflects that by offering you plenty of options to use them. In the end, it’s up to you to choose between the 4 methods we give you (and the custom shader option you can build on top of the texture shadow techniques) based on your needs / constraints.

I’ve tinkered with a few other things too, and have a couple more things in mind, details as usual are in the Dagon Notes.