Performance with dynamic shadows

I have now found that dynamic shadows have a very big impact on performance. For testing I created a terrain (1000x1000) with hightmap and walk navigation. I placed about 3500 TCastleScene and TCastleTransform on it.

Directional light with shadows = true → 21 FPS
Directional light with shadows = false → 65 FPS

It doesn’t matter whether the items have castshadows set to true or false.
So I can choose between good performance or shadows.

It would be very usefull to have some shadows on only some important items.
Grass, rocks or flowers do not need shadow in my oppinion, but these make up the majority of the items. Setting them to castshadow = false shows no performance gain.

I am able to get high framerates on my similarly sized tiled terrain. But I don’t have a zillion scenes yet maybe as many as 100 so far. I have tied shadow settings to my LOD handling, so the distant terrain tiles don’t cast or receive shadows, and distant objects don’t cast or receive shadows. I think this may help. My terrain is tiled though. Tilesize is 100x100 and then the grid size is about 11x11 right now, so about the same sized terrain. If you want to explore tiling your terrain (which then allows you to drop distance vertex count too)… I could help, but my code is pretty unruly in that area as it has experienced constant flux.

Shadows did so much to bring my world to life, and make me introduce moving sun and day night cycle… I would rather suffer at 20fps than give them up!

Also you might remove that distant grass completely, or set to exists=false when stuff like grass is really far away. Just having to figure out if it should cast shadow for 1000s of objects is time consuming I am sure.

Maybe you can add a switch for shadow effects in the option page.So players choose good performance or shadows themselves. :grinning:

Yes, dividing the terrain seems to me to be a good way to achieve better performance. I’ll do some research in this direction.
Yes, shadows are really important, you can’t really do without them.

Yes, the next game will get the option shadow yes/no. :+1:t4:

The key to tiling the terrain is the Offset field that Michalis added to the TCastleTerrain a year ago. Then the trick is having access to 3 neighbors to connect the edge seams. … Actually you don’t need to worry about seams if you are just using the standard noise. I do because I make a copy of the noise result data so it can be modified.

Yes.I just have disabled the shadows in your game GemIslands and gained a better performance.So i think is the shadow that have a huge impact on its fps.

Sorry for long delay in answering on forum – I had a backlog of unanswered stuff that I’m addressing now.

@Didi Thanks for investigating, and I think the toggle to show/hide shadows is a good idea.

Shadows will always have some performance cost. There are some things one just has to do to make Shadow Volumes | Manual | Castle Game Engine work (they need essentially rendering scene 2 times, with and without lights – that’s why even things that don’t cast shadows still affect the overall performance, basically everything is more costly with shadow volumes, not only shadow casters; only DistanceCulling, Castle Game Engine: CastleScene: Class TCastleScene , will help as it really removes shapes). We work on making a different shadows technique, Shadow Maps | Castle Game Engine , easily accessible, but – spoiler alert :slight_smile: – it comes with a different set of problems (quality and performance). There’s no perfect solution.

Still, on the engine side, let me see can I bring down the difference 21 vs 65 that you observe as close as possible :slight_smile: Let me plat with the testcase you have, I’m sure I can improve some things . From what I understand, you already made a testcase that is nice (separate from GemIslands game),

terrain (1000x1000) with hightmap and walk navigation. I placed about 3500 TCastleScene and TCastleTransform on it.

Can you send it to me? Preferably as new GitHub issue Issues · castle-engine/castle-engine · GitHub , so that it hangs on me :slight_smile: Thank you!

2 Likes

I made an issue on GitHub #614 with project included as zip.
Please let me know if I did this correctly.

@Didi Thank you, yes, Performance of dynamic shadows - testcase · Issue #614 · castle-engine/castle-engine · GitHub is perfect. I can reproduce the slowness on my system (27 vs 60 FPS for me). This is absolutely something I can play with and optimize. Thanks again, I’ll return here once I’ll make some conclusions! I’ll try to look at it this week.

1 Like

It took me some time, but I’ve done a significant optimization to the code I found was a bottleneck in this case (shadow volumes + world with many TCastleTransform, Performance of dynamic shadows - testcase · Issue #614 · castle-engine/castle-engine · GitHub ). This actually optimizes all rendering, whether it uses shadow volumes or not, but is most impactful for the shadow volumes case. So I’m really happy about it, it’s a very reasonable optimization (that actually cleans up some code) that just improves performance of ~everything.

Thank you everyone for participating in this thread, and testing the latest CGE with this optimization is most welcome!

3 Likes

With the latest version of CGE, I can see a 20 - 60% improvement in FPS. It always depends on how many objects are currently in the view. So thumbs up and thank you very much for that.

:+1:t4: :+1:t4: :+1:t4:

3 Likes