Page 2 of 2

Re: Frame rate, vsync and CPU usage

Posted: Mon Aug 25, 2008 10:05 pm
by Terohnon
60 fps with 99% cpu ussage. I've got an AMD3600+ single-core with an ATI Radeon 3870.

Re: Frame rate, vsync and CPU usage

Posted: Tue Aug 26, 2008 7:58 pm
by rude
I hope DirectX is less anal about this. Does anyone know?

Re: Frame rate, vsync and CPU usage

Posted: Tue Sep 30, 2008 10:04 pm
by natasky
Hi everyone, I'm new here.
First: great job on 'love', very easy and fun, and with a fun name ;) .

Sorry if I'm popping an old thread here, but the CPU usage was something that I noticed right away when trying the demos - with a single core machine, everything else on the system gets jerky while love is running. I think its ok for a fullscreen game, but not for a windowed one.

This is the thread I found regarding the issue, it has no solution to the problem. Here's my quick solution:

Code: Select all

function delay(fps)
	if (fps > 50) then
		fps = fps + (fps / 10) - 5
	end
	local toSleep = 2 / fps - love.timer.getDelta()
	if toSleep > 0 then
		love.timer.sleep(toSleep * 1000)
	end
end
By setting the vsync option on 'game.conf' to false, and calling this function at the beginning of every update call with the desired frame rate (something like: delay(60)), the game will pretty much be in the desired fps, with almost no CPU usage (well, cpu usage is now only up to how much you game actually needs it, the demo 'NO' used only 1% CPU, as opposed to constant 100% without this)

Any other (more elegant) solution?

Re: Frame rate, vsync and CPU usage

Posted: Wed Oct 01, 2008 12:11 pm
by muku
The vsync issue is something we can't really fix because it seems to be a driver issue. If the driver decides to do a busy spin while waiting for the vsync, then I guess we're out of luck.

That said, yes, a manual solution using sleep() is the next best thing, even though it isn't real vsync and thus doesn't get the advantage of eliminating tearing. I'm not quite sure what your code above does, but here is my variant for staying as close as possible to a desired framerate:

Code: Select all

function update(delta)
	local now = love.timer.getTime()
	while now < nextFrame do
		love.timer.sleep(1000 * 0.8 * (nextFrame - now))
		now = love.timer.getTime()
	end
	nextFrame = now + 1 / frameRate
	
	[... do rest of your update code here ...]
So, it's very straightforward, it just calculates when the next frame is supposed to start based on the current time and the desired frame rate and sleeps until that time rolls around. The factor 0.8 is to mitigate the effect of low precision of the sleep call.

Re: Frame rate, vsync and CPU usage

Posted: Fri Oct 03, 2008 9:23 am
by rude
Thanks for these pieces of code, guys.
natasky wrote:Any other (more elegant) solution?
V-sync was supposed to be the elegant sulution, but like muku said, there's nothing we can do if the driver decides to do a busy-wait.

For Windows-users, we can pray that the DirectX specification includes how drivers should wait for vertical retrace. For everyone else, the OpenGL drivers will just have to not suck.

Re: Frame rate, vsync and CPU usage

Posted: Sat Oct 25, 2008 9:51 pm
by zapwow
Wow, I'm glad I found this thread! My game has gone from 60fps/100% cpu to 500fps/0%cpu by disabling vsync and putting sleep(1) in the update function. I'm on a P43.0GHz / Radeon x1650.

I'm considering recompiling LOVE with these settings in the error screen so my system doesn't come to a crawl every time I make a mistake...

Re: Frame rate, vsync and CPU usage

Posted: Sat Oct 25, 2008 10:27 pm
by rude
Good point regarding the error screen. I'll make sure it uses < 1% CPU next version.