Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FPS timing with chrono #7867

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

FPS timing with chrono #7867

wants to merge 13 commits into from

Conversation

dimitre
Copy link
Member

@dimitre dimitre commented Jan 18, 2024

This PR has the intent of modernize OF framerate managing.
The idea is using only std::chrono for two reasons : let std::chrono manage all time divisions in the best way possible, and unifying code across platforms.
it was tested in macOS and reduced FPS drift compared to ofTimer implementation

@dimitre dimitre marked this pull request as ready for review January 18, 2024 17:12
@dimitre
Copy link
Member Author

dimitre commented Jan 18, 2024

cc @artificiel

@dimitre
Copy link
Member Author

dimitre commented Jan 23, 2024

I've made a proof of concept here and I'm attaching one image and the code.
the code itself is standalone it can be run on OF master
it changes from OF to this new mode each 4 seconds to compare the drift
it can be run with different framerates, vertical sync on and off to test.
tests run better on macOS with this new setFps.
there is a new getFps in there too, stabilize quicker, but is not as filtered as the OF default one

Screenshot 2024-01-22 at 21 52 04

fpsTest.zip

cc @ofTheo @artificiel

@ofTheo
Copy link
Member

ofTheo commented Feb 5, 2024

@dimitre - Awesome!
Might be good to test on Windows and Linux before merging.

@dimitre
Copy link
Member Author

dimitre commented Feb 5, 2024

Great! I'll be testing in the next days and report back here.

@dimitre
Copy link
Member Author

dimitre commented May 6, 2024

@artificiel can you please test this PR with your tests mentioned here?

I think this PR can be a slight improvement to actual FPS.
and here is also an alternative way of counting FPS

#include <chrono>
using namespace std::chrono;
using namespace std::chrono_literals;

struct fpsCounter {
public:
	int nAverages = 20;
	using space = std::chrono::duration<long double, std::nano>;
	time_point<steady_clock> lastTick;
	steady_clock::duration onesec = 1s;
	std::vector <space> intervals;
	space interval;
	space average;
	bool firstTick = true;
	int cursor = 0;

	void tick() {
		if (firstTick) {
			firstTick = false;
			lastTick = steady_clock::now();
			return;
		}

		interval = steady_clock::now() - lastTick;
		lastTick = steady_clock::now();
		if (intervals.size() < nAverages) {
			intervals.emplace_back(interval);
		} else {
			intervals[cursor] = interval;
			cursor = (cursor+1)%nAverages;
		}
	}
	
	double getFps()  {
		average = std::reduce(intervals.begin(), intervals.end());
		return (double)intervals.size() * onesec / average;
	}
	
	float get() {
		average = std::reduce(intervals.begin(), intervals.end());
		return (float)intervals.size() * onesec / average;
	}
};

@dimitre
Copy link
Member Author

dimitre commented May 8, 2024

finally a decent way of testing fps drift, measuring with ofGetFrameRate, and with chrono.
you can toggle (pressing key "t") between OF classic FPS control and new one to see differences in drift.
ofw/apps/devApps/fps
Screenshot 2024-05-08 at 17 02 29

@dimitre
Copy link
Member Author

dimitre commented May 9, 2024

now alternative fps counter has a subtle low pass filter (average of 4 last results) so both modes can be compared better.
it is a great improvement in macOS. I'll be testing on Ubuntu soon
Screenshot 2024-05-09 at 00 36 51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants