Skip to content

Commit

Permalink
Changed UpdateFpsValue() in vtk UI actor class to use a deque for a s…
Browse files Browse the repository at this point in the history
…liding average of time between frames
  • Loading branch information
GabrielNakamoto committed Jan 6, 2025
1 parent fa483f2 commit 42bc7e4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
17 changes: 8 additions & 9 deletions vtkext/private/module/vtkF3DRenderer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1547,20 +1547,19 @@ void vtkF3DRenderer::Render()

if (! uiOnly)
{
// Get CPU frame per seconds
int fps = static_cast<int>(std::round(
1.0 / (std::chrono::duration_cast<std::chrono::microseconds>(cpuElapsed).count() * 1e-6)));
// Get CPU frame time
double elapsedTime = std::chrono::duration_cast<std::chrono::microseconds>(cpuElapsed).count() * 1e-6;

#if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
glEndQuery(GL_TIME_ELAPSED);
GLint elapsed;
glGetQueryObjectiv(this->Timer, GL_QUERY_RESULT, &elapsed);
glEndQuery(GL_TIME_ELAPSED);
GLint elapsed;
glGetQueryObjectiv(this->Timer, GL_QUERY_RESULT, &elapsed);

// Get min between CPU frame per seconds and GPU frame per seconds
fps = std::min(fps, static_cast<int>(std::round(1.0 / (elapsed * 1e-9))));
// Get min between CPU frame time and GPU frame time
elapsedTime = std::min(elapsedTime, elapsed * 1e-9);
#endif

this->UIActor->UpdateFpsValue(fps);
this->UIActor->UpdateFpsValue(elapsedTime);
}
}

Expand Down
21 changes: 12 additions & 9 deletions vtkext/private/module/vtkF3DUIActor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <vtkOpenGLRenderWindow.h>
#include <vtkViewport.h>

#include <numeric>

vtkObjectFactoryNewMacro(vtkF3DUIActor);

//----------------------------------------------------------------------------
Expand Down Expand Up @@ -67,18 +69,19 @@ void vtkF3DUIActor::SetFpsCounterVisibility(bool show)
}

//----------------------------------------------------------------------------
void vtkF3DUIActor::UpdateFpsValue(int fps)
void vtkF3DUIActor::UpdateFpsValue(const double elapsedMicroSeconds)
{
this->AccumulatedFpsValue += fps;
this->FramesAccumulated++;
this->FrameTimes.push_back(elapsedMicroSeconds);

if (this->FramesAccumulated == vtkF3DUIActor::FramesToAverage)
{
this->FpsValue = static_cast<int>(this->AccumulatedFpsValue / this->FramesAccumulated);
// add window size check here
if (this->FrameTimes.size() == 6)
{
this->FrameTimes.pop_front();
}

this->AccumulatedFpsValue = 0;
this->FramesAccumulated = 0;
}
double averageFrameTime = std::accumulate(this->FrameTimes.begin(), this->FrameTimes.end(), 0.0) / this->FrameTimes.size();

this->FpsValue = static_cast<int>(std::round(1.0 / averageFrameTime));
}

//----------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions vtkext/private/module/vtkF3DUIActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define vtkF3DUIActor_h

#include <vtkProp.h>
#include <deque>

class vtkOpenGLRenderWindow;

Expand Down Expand Up @@ -84,7 +85,7 @@ class vtkF3DUIActor : public vtkProp
* Updates the fps value
* 0 by default
*/
void UpdateFpsValue(int fps);
void UpdateFpsValue(const double elapsedMicroSeconds);

/**
* Set the font file path
Expand Down Expand Up @@ -160,10 +161,9 @@ class vtkF3DUIActor : public vtkProp

bool FpsCounterVisible = false;

static const int FramesToAverage = 5;
int FramesAccumulated = 0;
// deque instead of queue to allow for iteration
std::deque<double> FrameTimes;

int AccumulatedFpsValue = 0;
int FpsValue = 0;

std::string FontFile = "";
Expand Down

0 comments on commit 42bc7e4

Please sign in to comment.