From 0e3f96ecb9ade07a7b831078fee025aff44d44d4 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 31 Jul 2015 15:32:21 +0200 Subject: [PATCH] also show peak in frame counter --- src/app/FrameCounter.hpp | 40 ++++++++++++++++------------- src/app/app.cpp | 55 +++++++++++++++++++++++++++------------- src/ui/ui.cpp | 4 ++- 3 files changed, 64 insertions(+), 35 deletions(-) diff --git a/src/app/FrameCounter.hpp b/src/app/FrameCounter.hpp index 069f622..16b6b4e 100644 --- a/src/app/FrameCounter.hpp +++ b/src/app/FrameCounter.hpp @@ -9,6 +9,18 @@ namespace blank { class FrameCounter { +public: + template + struct Frame { + T handle = T(0); + T update = T(0); + T render = T(0); + T running = T(0); + T waiting = T(0); + T total = T(0); + }; + + public: void EnterFrame() noexcept; void EnterHandle() noexcept; @@ -19,38 +31,32 @@ public: 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(); } + const Frame &Peak() const noexcept { return peak; } + const Frame &Average() const noexcept { return avg; } bool Changed() const noexcept { return changed; } - void Print(std::ostream &) const; - private: int Tick() noexcept; + void Accumulate() noexcept; + void Push() 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 current = Frame{}; + Frame sum = Frame{}; + Frame max = Frame{}; + + Frame peak = Frame{}; Frame avg = Frame{}; + bool changed = false; }; diff --git a/src/app/app.cpp b/src/app/app.cpp index 1c4b4db..d13dfc6 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -194,7 +194,7 @@ void FrameCounter::EnterHandle() noexcept { } void FrameCounter::ExitHandle() noexcept { - running.handle += Tick(); + current.handle = Tick(); } void FrameCounter::EnterUpdate() noexcept { @@ -202,7 +202,7 @@ void FrameCounter::EnterUpdate() noexcept { } void FrameCounter::ExitUpdate() noexcept { - running.update += Tick(); + current.update = Tick(); } void FrameCounter::EnterRender() noexcept { @@ -210,19 +210,19 @@ void FrameCounter::EnterRender() noexcept { } void FrameCounter::ExitRender() noexcept { - running.render += Tick(); + current.render = Tick(); } void FrameCounter::ExitFrame() noexcept { Uint32 now = SDL_GetTicks(); - running.total += now - last_enter; + current.total = now - last_enter; + current.running = current.handle + current.update + current.render; + current.waiting = current.total - current.running; + Accumulate(); + ++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{}; + Push(); cur_frame = 0; changed = true; } else { @@ -237,14 +237,35 @@ int FrameCounter::Tick() noexcept { 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; +void FrameCounter::Accumulate() noexcept { + sum.handle += current.handle; + sum.update += current.update; + sum.render += current.render; + sum.running += current.running; + sum.waiting += current.waiting; + sum.total += current.total; + + max.handle = std::max(current.handle, max.handle); + max.update = std::max(current.update, max.update); + max.render = std::max(current.render, max.render); + max.running = std::max(current.running, max.running); + max.waiting = std::max(current.waiting, max.waiting); + max.total = std::max(current.total, max.total); + + current = Frame(); +} + +void FrameCounter::Push() noexcept { + peak = max; + avg.handle = sum.handle * factor; + avg.update = sum.update * factor; + avg.render = sum.render * factor; + avg.running = sum.running * factor; + avg.waiting = sum.waiting * factor; + avg.total = sum.total * factor; + + sum = Frame(); + max = Frame(); } } diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index 0f2b81d..43a5f5d 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -317,7 +317,9 @@ void Interface::ToggleCounter() { void Interface::UpdateCounter() { std::stringstream s; - s << std::setprecision(3) << counter.AvgRunning() << "ms"; + s << std::setprecision(3) << + "avg: " << counter.Average().running << "ms, " + "peak: " << counter.Peak().running << "ms"; std::string text = s.str(); counter_text.Set(font, text); } -- 2.39.2