pioneerspacesim/pioneer — pull request #2541 by fluffyfreak (2013-11-10 11:59:21+00:00)
Title: Single frame profiling instrumentation.

# Description:
*NOTE* - this is two-parts-in-one due to my studipity

# 1st part - the profiler:
Whilst testing a bug recently I noticed very long, ~15 seconds, pauses in some frame times. Apparently without cause.

To track these down I reimplemented the usage of [this instrumented profiler](https://code.google.com/p/high-performance-cplusplus-profiler/). It's intrusive and requires code changes which I've made.

I added it as a contrib project and have added it to the vc2012 and vc2013 solutions but nothing else yet as my laptop doesn't have vc2010 on it.

# 2nd part - The optimisations:
...having done the above I plowed head first into optimising what I found and that was that the old GUI, and a few other places, were still calling the glGet* functions all over the place. This causes a sync event with the GPU and drivers and generally stalls things - *this varies massively between systems and GPU manufacturers / drivers so your mileage may vary drastically.*

It meant for example that Gui::Screen::EnterOrtho was taking 88% of our CPU time on my nVidia 650M... *alone*.

Dealing with these means tracking all of our `glPushMatrix`, `glPopMatrix`, `glTranslate`, `glRotate` (none), `glScale`, `glMatrixMode`, `glLoadIdentity`, `glLoadMatrix` calls. I think that's all of them!

I've created an interface for all of these in the Renderer class and implemented it within the RendererLegacy class.
Then I changed everything over to calling the Renderer implementations rather than OpenGLs.

I'm still getting some minor hiccups but they're on the order of ~0.5 seconds rather than the ~15 seconds I was having. If you'd like to test the before and after then hyperspace to Canti (3, 5, -1) and head for `Ibarrez` which should be about 1014 AU distant. You'll have to watch it for quite a while but I got the enormous stutters (~15 secs) with alarming regularity.

# Usage:
To enable the profiler you will need uncomment `//#define __PROFILER_ENABLED__` in `Profiler.h`.
Then to actually use it I have bound it to a `DEVKEY` so you need to press `Ctrl+p` whilst ingame.

This will dump a html file into the projects folder with the timings and call tree information that you can view using any browser. If you've got a lot of missing images that define the links of the tree itself then there's a folder in vc2013Express called `img`, just copy that folder to wherever your html file is and refresh.

# AAAARGH!
Yes, I should have separated these out into two different PR's but my Git-foo is weak. After trying for a while I decided to just do this PR instead before I ruined it further and smashed my laptop into my face.

# Future work:
To add an auto-dump feature for frames which exceed a maximum time so that I can capture the ones which have had something horrible happen within them which cause the single frame stutters/hiccups.

Andy