Frame rate, vsync and CPU usage

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Terohnon
Prole
Posts: 1
Joined: Mon Aug 25, 2008 10:03 pm

Re: Frame rate, vsync and CPU usage

Post by Terohnon »

60 fps with 99% cpu ussage. I've got an AMD3600+ single-core with an ATI Radeon 3870.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Frame rate, vsync and CPU usage

Post by rude »

I hope DirectX is less anal about this. Does anyone know?
natasky
Prole
Posts: 1
Joined: Tue Sep 30, 2008 8:44 pm

Re: Frame rate, vsync and CPU usage

Post 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?
User avatar
muku
Prole
Posts: 18
Joined: Wed Aug 20, 2008 11:35 am

Re: Frame rate, vsync and CPU usage

Post 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.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Frame rate, vsync and CPU usage

Post 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.
User avatar
zapwow
Prole
Posts: 48
Joined: Fri Oct 24, 2008 11:37 pm

Re: Frame rate, vsync and CPU usage

Post 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...
Last edited by zapwow on Sat Oct 25, 2008 10:31 pm, edited 1 time in total.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Frame rate, vsync and CPU usage

Post by rude »

Good point regarding the error screen. I'll make sure it uses < 1% CPU next version.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 79 guests