Recent

Author Topic: SDL2+OpenGL and FPS count (and CPU usage)  (Read 7819 times)

Graeme

  • Hero Member
  • *****
  • Posts: 1428
    • Graeme on the web
SDL2+OpenGL and FPS count (and CPU usage)
« on: February 03, 2017, 05:29:50 pm »
When I run my application it reports 60 frames per second no matter what. At the moment it only renders a rectangle (from two triangles combined). I assume SDL automatically syncs with my monitor’s refresh rate of 60Hz to achive that low FPS count with such a simple scene.

Now if I call SDL_GL_SetSwapInterval(0); before I enter my game loop, I get around 4500 frames per second, but all 8 cores of my CPU sits at 100%. Obviously not good.

Out of curiocity I want the FPS count to be higher than 60, but not at the cost of 100% CPU usage. I then had a read in the SDL documentation and played around with a few things.

I noticed that if I call SDL_GL_SetSwapInterval(0); as before (let it render as soon as possible) - before I enter my game loop, then as the last call in my game loop (after I updated the screen), I call SDL_Sleep(2); then I get around 460 frames per second with 1-2% CPU usage. This seems pretty good, and more of what I wanted to achive. But then, thinking about it, I’m stealing 2ms (doing nothing) from my game loop. Now considering you only have ~33.3 milliseconds to achive 30FPS , or even worse ~16.6 milliseconds to achive 60FPS.  2ms (doing nothing) out of the latter is a bit worrying - and very expensive!


But is this the correct way to do things to get more that 60 FPS ,with still low CPU usage?
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: SDL2+OpenGL and FPS count (and CPU usage)
« Reply #2 on: February 07, 2017, 06:04:25 pm »
Up to about a decade ago, most games were simply a game loop, running at max speed. I/O (like, mouse and keyboard) was polled once each loop, and movement only depended on the speed of the game loop. That was why people wanted as high a FPS count as possible for online twitch gaming (like FPSes). A high FPS count made you a superman.

Since then, games use at least two threads / processes: one for events (like I/O), movement and physics calculations, which always runs at the same speed, and the other for rendering.

And most displays are LCDs nowadays, which are locked to 60 FPS anyway. So everything more than that is just generating heat.

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: SDL2+OpenGL and FPS count (and CPU usage)
« Reply #3 on: February 07, 2017, 06:10:33 pm »
try SDL_Sleep(0) :)

there's no need to getting more FPS than a monitor could handle.

100% CPU load, is definitely a message loop constantly doing nothing.
You could also try to use SDL_WaitEventTimeout, with 0 for timeout and see if it helps to drop CPU usage. Though I'd suspect the results would the same as SDL_sleep(0) with SDL_PollEvent()

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: SDL2+OpenGL and FPS count (and CPU usage)
« Reply #4 on: February 07, 2017, 06:15:13 pm »
In the old days, we are told that 30-60 FPS is acceptable rates for animations and games. But I often heard some serious gamers said they can notice the bad quality if the refresh rate is below 120.

It seems the new TV technology now pushes us to set the FPS higher:
https://www.cnet.com/news/what-is-refresh-rate/
http://www.pcmag.com/article2/0,2817,2379206,00.asp

Nitorami

  • Sr. Member
  • ****
  • Posts: 481
Re: SDL2+OpenGL and FPS count (and CPU usage)
« Reply #5 on: February 07, 2017, 07:03:02 pm »
Quote
I call SDL_Sleep(2); then I get around 460 frames per second with 1-2% CPU usage. This seems pretty good, and more of what I wanted to achive. But then, thinking about it, I’m stealing 2ms (doing nothing) from my game loop.

I think the proper name is "SDL_Delay". But anyway, it should not "steal" time from the game loop, except if you run it all in one thread in the main program. Similar as the "sleep" function in sysutils, SDL_Delay just pauses processing and makes the CPU available for other tasks. But you can only make use of it if you run your game loop and SDL in different threads.

 

TinyPortal © 2005-2018