Search code examples
sfmlvisual-studio-debuggingbounding-boxfrustumculling

Frustum culling decreases framerate in debug but improves it in release


I'm making a tilebased game in SFML and decided to implement a simple bounding-box frustum culling to only draw the tiles that are in the viewport of the camera. The result was a success, as it tripled my framerate! However, there was a cache. When running the game in debug the framerate is instead lowered by the implemented frustum culling. It went from 90 to 30 fps in debug, while in release it went from 100 to about 350 fps.

The culling simply checks for each tiles rectangle, whether it intersects with the rectangle of the viewport. If it does, it is drawn. The game renders several views for splitscreen and the code looks like this:

for (unsigned int i = 0; i < views.size(); i++)
    {

....

// Calculate viewport rectangle.
        IntRect viewRect;
        viewRect.width = views[i]->getSize().x;
        viewRect.height = views[i]->getSize().y;
        viewRect.left = views[i]->getCenter().x - views[i]->getSize().x / 2;
        viewRect.top = views[i]->getCenter().y - views[i]->getSize().y / 2;

        // Loop throug level tiles and render them.
        for (unsigned int j = 0; j < tileLayers.size(); j++)
        {
            for (unsigned int k = 0; k < tileLayers[j].size(); k++)
            {
                if (tileLayers[j][k]->collide(viewRect))
                    tileLayers[j][k]->drawSprite(window);
            }
        }

....

bool StaticGameObject::collide(IntRect rectangle)
{
    // Private member variable collisionRect.
    // Width and height is set in constructor.
    collisionRect.left = getPosition().x;
    collisionRect.top = getPosition().y;

    if (collisionRect.intersects(rectangle))
    {
        return true;
    }
    return false;
}

When I remove the intersect calls for the rectangle, the framerate goes back up to 90 fps. I'm very confused to as why this is happening.


Solution

  • A synopsis of the lengthy discussion in the comments:

    Use of the frustum culling algorithm resulted in significant overhead, which had the effect of decreasing the overall framerate in a debug build. In a release build the compiler optimized away the extra overhead resulting in a framerate increase. Unfortunately this is a necessary evil, as optimized code makes debugging impossible.