Profiling rules. If you're a game developer, and don't profile, you owe it to yourself to learn how!Truth is, as humans we often guess wrong as to what's taking making our game slow. We aren't computers. So today, I was really feeling like I needed a faster graphics card, that I was pushing too many polys with my lower-end, onboard GF6xxxx-series. But I profiled. Here is what I found:
18.60 3.96 3.96 17989058 0.00 0.00 rstate::cull_sphere(vecco<double, 3> const&, double)Top function is view fustrum culling. It's taking 18.60% of cpu time (yeoch!). There are two ways to fix this: 1) reduce the number of calls (do this FIRST). and 2) improve the function itself.*** STEP 1 ***To reduce the number of calls, you need this information from the profiler:
0.20 0.00 907458/17989058 sprite::render(rstate&)  3.76 0.00 17081600/17989058 blockgrid::render(rstate&)  18.6 3.96 0.00 17989058 rstate::cull_sphere(vecco<double, 3> const&, double) What this means is, most of the time it's getting called from blockgrid::render; this renders the world map. I take my world map and divide it into blocks, where each one can be view-frustum culled; how big the blocks are is in a #define, at the top of blockgrid.cpp:
#define SEC_SIZE 5I changed this from 5 to 10.*** STEP 2 *** Then I had a look at the code itself. It's really simple:
for (int a = 0; a < 7; ++a) if (frustum[a] * P + frustum[a] * P + frustum[a] * P + frustum[a] <= -Radius) return true; return false;frustum is an array of plane equations. There are six of them.Six of them. Why "a < 7"? So I changed that.*** CONCLUSION ***So I changed a 5 to a 10, and a 7 to a 6. This was the optimization I did after profiling. Result?Game runs noticably faster. Really. now cull_sphere is #3 on the list of most-cpu-using methods:
8.88 4.30 1.31 5020718 0.00 0.00 rstate::cull_sphere(vecco<double, 3> const&, double)*** NEXT ROUND ***The new #1 on the list is this:
10.51 1.55 1.55 165696 0.00 0.00 std::vector<ref<blockgrid::block_section>, std::allocator<ref<blockgrid::block_section> > >::operator=(std::vector<ref<blockgrid::block_section>, std::allocator<ref<blockgrid::block_section> > > const&)Where am I copying these things, and can I avoid doing it?ONWARD, PROFILER HO!