Ok, so I've spent the past week working on performance improvements. It's not simple to debug a fairly complex scene, especially for VR where there's little prior knowledge to draw from and the requirements for "playable" are much more strict.
However, I think I've made a ton of progress and would like to share what I've done, and how I've gone about doing it so that other developers might not have to go through the same 'trial and error' as I have!
First, a general overview of what's required for VR: constant 90fps
That's all I'm going for, but that constant is important. 89 fps, even for an instant, just won't fly, which is why we should be aiming for 95 or so.
At 100fps we have 10ms per frame to work with, so that's been my target for rendering. We'll be talking in milliseconds (ms) from now on.
I'm using Unity3D to develop QuiVr, and it comes with a (not spectacularly intuitive) profiler to see how much time things cost per frame. This is primarily what I've been using to gauge performance hits/improvements.
Base Line
Before we dig into the game's GPU/CPU times, we need to establish a baseline. Everything has overhead, including the profiler itself, so the first thing I needed to do was to see what the Unity Editor was taking up.
Here we can see that the editor (while not playing the game) has about a 5ms per frame hit with occasional spikes that immediately put our frame time out of the 10ms budget. That's fine (reprojection can handle it), but we just need to realize that spikes in the profiler while running the game may not actually be from the game itself.
Now it's time to profile the game when nothing is happening. Most people have reported that issues only occur when there are a fair few enemies in the game (Multiplayer has way more enemies on screen at a time), so I think we can assume that while not perfectly optimized, the base scene is ok.
Let's take a look at the CPU time:
That's pretty good. Our CPU is waiting for the GPU for a third of the time, so and even then we're around 200-150 fps in the editor (which has much higher overhead then a built executable)
Now lets look at the GPU (which were currently bound by)
That's not very helpful... About 2ms is being taken by rendering the static and skinned geometry, 0.2ms for setting render textures.... The other 7.8ms? ... Other
I can only speculate that I think the polling of the GPU and rendering this graph is taking a lot of the time, as I can hit the game much harder with graphics when the profiler is not running.
Can't get much more out of it than that, but we at least have a working baseline. 2.5ms of camera rendering on the CPU and 2ms of rendering on the GPU.
Let's get to some debugging!
Physics
Someone mentioned that they thought spiders were a potential cause for the issues, so I did what was reasonable and spawned in 20 spiders on top of each other. This was lucky
The profiler is showing 38ms on Physics processing...
After taking a look, it seems that my testing environment for ragdolls had leaked into the actual game, and the ragdoll physics were being calculated all the time! That is a simple enough fix, just turn the rigidbodies to kinematic (Animation overrides it which is why it doesn't act differently).
The results?
Over 5x performance improvement... but it's still 7ms which is way too much of our 10ms budget. Spreading the spiders out so they're not overlapping?
0.98ms is something I can live with for now, still 10% of our budget but the spiders have the most physics objects of any of the enemies by a huge margin (30 for the spiders as opposed to 7 for a ghoul).
However, that Animation.Update is 1.2ms, I don't like that at all...
Animation
Don't have any pictures for this one, but it turns out that every enemy had about 30 animations ready to play, when at most they need 7 (a couple attacks, idle, run, death).
Removing the extra animations from every enemy dropped that time down to ~0.2ms with 20 enemies in front of the low base.
The next thing I needed to check was that someone said that the Orcs were causing a serious issue.
Orcs
And boy were they right...
8ms of animations for just 10 orcs on screen...
From the start, this looks like another animation problem, but if we open it up, it looks like there's some PhysX issue going on?
It turns out that I was using a mesh collider on the armor to make the arrows bounce off. And because the mesh was animated there was some serious calculations that needed to be made (even though the armor was only about 100 triangles).
Turning that collider off dropped the physics down to 0.01ms and let me do this without dropping a single frame...
A bit hard to tell but the mini profiler is reporting 109fps with 40 animated orcs on screen.
Menu
Another thing I was able to find when digging through the baseline profiler was this...
over 5% of our budget is going to the EventSystem that drives the UI for the menu, that's silly if the menu isn't up, so that was able to be dropped to 0ms by disabling the EventSystem unless the menu is on screen.
Health Bars
Also in the UI department, health bars turn to face the player camera, which is actually a bit expensive to do every frame. Since the health bars aren't shown each frame, I set it to only rotate when visible, which was able to drop the Behavior.Update down to 0.5ms from 1.1ms.
Conclusion
There's still a fair bit to go, but these I think were some of the low hanging fruit that just needed a bit of digging to uncover and fix (and hopefully others can use this information as well!) so I wanted to show what I was doing to fix the issues.
Also in this patch
I've also done things like correctly list the scores in descending order, reset scores when a new game starts, and made the bosses faster and have more health.
The beetle has been fixed so that it should correctly despawn, the stingray has been made a bit larger, and a couple other detail changes have been made.
Please let me know if things are working better, and the more specific you can be about what causes the issue (spiders and orcs here were incredibly helpful) the better!
