From 5c295a495e02b4987b63c682406f5f29402c00da Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 23 Jul 2015 13:42:28 +0200 Subject: [PATCH] collect timing information --- src/app/Application.hpp | 2 ++ src/app/FrameCounter.hpp | 60 +++++++++++++++++++++++++++++++ src/app/app.cpp | 76 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 src/app/FrameCounter.hpp diff --git a/src/app/Application.hpp b/src/app/Application.hpp index ea74211..97cac73 100644 --- a/src/app/Application.hpp +++ b/src/app/Application.hpp @@ -2,6 +2,7 @@ #define BLANK_APP_APPLICATION_HPP_ #include "Assets.hpp" +#include "FrameCounter.hpp" #include "init.hpp" #include "RandomWalk.hpp" #include "../graphics/BlendedSprite.hpp" @@ -64,6 +65,7 @@ private: GLContext ctx; InitGLEW init_glew; Assets assets; + FrameCounter counter; BlockLighting chunk_prog; DirectionalLighting entity_prog; diff --git a/src/app/FrameCounter.hpp b/src/app/FrameCounter.hpp new file mode 100644 index 0000000..069f622 --- /dev/null +++ b/src/app/FrameCounter.hpp @@ -0,0 +1,60 @@ +#ifndef BLANK_APP_FRAMECOUNTER_HPP_ +#define BLANK_APP_FRAMECOUNTER_HPP_ + +#include +#include + + +namespace blank { + +class FrameCounter { + +public: + void EnterFrame() noexcept; + void EnterHandle() noexcept; + void ExitHandle() noexcept; + void EnterUpdate() noexcept; + void ExitUpdate() noexcept; + void EnterRender() noexcept; + void ExitRender() noexcept; + void ExitFrame() noexcept; + + float AvgHandle() const noexcept { return avg.handle; } + float AvgUpdate() const noexcept { return avg.update; } + float AvgRender() const noexcept { return avg.render; } + float AvgFrame() const noexcept { return avg.total; } + float AvgRunning() const noexcept { return avg.handle + avg.update + avg.render; } + float AvgWaiting() const noexcept { return avg.total - AvgRunning(); } + + bool Changed() const noexcept { return changed; } + + void Print(std::ostream &) const; + +private: + int Tick() noexcept; + +private: + static constexpr int NUM_FRAMES = 32; + static constexpr float factor = 1.0f / float(NUM_FRAMES); + + template + struct Frame { + T handle = T(0); + T update = T(0); + T render = T(0); + T total = T(0); + }; + + Uint32 last_enter = 0; + Uint32 last_tick = 0; + + int cur_frame = 0; + Frame running = Frame{}; + Frame avg = Frame{}; + bool changed = false; + +}; + +} + +#endif diff --git a/src/app/app.cpp b/src/app/app.cpp index 9e83cd4..5c068a4 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -1,5 +1,6 @@ #include "Application.hpp" #include "Assets.hpp" +#include "FrameCounter.hpp" #include "../graphics/Font.hpp" #include "../world/BlockType.hpp" @@ -34,6 +35,7 @@ Application::Application(const Config &config) , ctx(window.CreateContext()) , init_glew() , assets(get_asset_path()) +, counter() , chunk_prog() , entity_prog() , sprite_prog() @@ -102,13 +104,16 @@ void Application::Run() { } void Application::Loop(int dt) { + counter.EnterFrame(); HandleEvents(); Update(dt); Render(); + counter.ExitFrame(); } void Application::HandleEvents() { + counter.EnterHandle(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { @@ -140,6 +145,7 @@ void Application::HandleEvents() { break; } } + counter.ExitHandle(); } void Application::Handle(const SDL_WindowEvent &event) { @@ -161,13 +167,18 @@ void Application::Handle(const SDL_WindowEvent &event) { } void Application::Update(int dt) { + counter.EnterUpdate(); interface.Update(dt); test_controller.Update(dt); world.Update(dt); + counter.ExitUpdate(); } void Application::Render() { + // gl implementation may (and will probably) delay vsync blocking until + // the first write after flipping, which is this clear call GLContext::Clear(); + counter.EnterRender(); chunk_prog.SetProjection(cam.Projection()); entity_prog.SetProjection(cam.Projection()); @@ -176,6 +187,7 @@ void Application::Render() { interface.Render(entity_prog, sprite_prog); + counter.ExitRender(); window.Flip(); } @@ -190,4 +202,68 @@ Font Assets::LoadFont(const string &name, int size) const { return Font(full.c_str(), size); } + +void FrameCounter::EnterFrame() noexcept { + last_enter = SDL_GetTicks(); + last_tick = last_enter; +} + +void FrameCounter::EnterHandle() noexcept { + Tick(); +} + +void FrameCounter::ExitHandle() noexcept { + running.handle += Tick(); +} + +void FrameCounter::EnterUpdate() noexcept { + Tick(); +} + +void FrameCounter::ExitUpdate() noexcept { + running.update += Tick(); +} + +void FrameCounter::EnterRender() noexcept { + Tick(); +} + +void FrameCounter::ExitRender() noexcept { + running.render += Tick(); +} + +void FrameCounter::ExitFrame() noexcept { + Uint32 now = SDL_GetTicks(); + running.total += now - last_enter; + ++cur_frame; + if (cur_frame >= NUM_FRAMES) { + avg.handle = running.handle * factor; + avg.update = running.update * factor; + avg.render = running.render * factor; + avg.total = running.total * factor; + running = Frame{}; + cur_frame = 0; + changed = true; + } else { + changed = false; + } +} + +int FrameCounter::Tick() noexcept { + Uint32 now = SDL_GetTicks(); + int delta = now - last_tick; + last_tick = now; + return delta; +} + +void FrameCounter::Print(std::ostream &out) const { + out << "frame: " << AvgFrame() << std::endl; + out << " handle: " << AvgHandle() << std::endl; + out << " update: " << AvgUpdate() << std::endl; + out << " render: " << AvgRender() << std::endl; + out << " running: " << AvgRunning() << std::endl; + out << " waiting: " << AvgWaiting() << std::endl; + out << std::endl; +} + } -- 2.39.2