Author Topic: Bug and improvement report  (Read 13807 times)

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Bug and improvement report
« Reply #15 on: 2011-01-20, 11:30:01 AM »
I think I found a glitch concerning LimitFrameRate in SGDK2 version 2.1.5.  :o

When I use LimitFrameRate(60) on my computer at home (5+ years old CPU) my fps counter shows 60 fps more or less steady.

When I run it on my computer at work (brand new desktop, very powerful) if I remove the LimitFrameRate, it runs at about 1500 fps.  But when I run it with a LimitFrameRate(60) rule it runs at a very very steady 50 fps.  Never goes higher or lower...  The game is quite slower because of that.  I looked at the LimitFrameRate code, but the maths there elude me...  Can someone explain the math here?  The frequency with the timestamps (standing for frames) are confusing me.

I don't see a good reason for the game to run slower on the high end computer, I think there might be a glitch in this function.

Thanks! :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bug and improvement report
« Reply #16 on: 2011-01-21, 06:20:24 AM »
Can you reproduce the problem in a simpler project?

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Bug and improvement report
« Reply #17 on: 2011-01-21, 11:18:38 AM »
Sure thing!

See the attached zip file for a very light fps test project.  It behaves exactly like my full game.  It runs at 50 fps at work on the powerful computer and at 60 fps at home on my old computer.

Also, it would be very useful to set a game fps in the project properties rather than in a plan rule or sprite rule... But I like that fact that we can change the fps at runtime also so keeping this woul be good too.

I hope it helps. :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bug and improvement report
« Reply #18 on: 2011-01-22, 07:18:58 AM »
I don't know that I will be able to find a system that can reproduce the problem here.  Oh, my wife says her project had the same problem on my laptop, so maybe I can look into it too when she's done playing games on the laptop :).  Meanwhile here are some ideas for debugging on the environment where you can reproduce it.  I think it will help to use the simpler project.

Inside the LimitFrameRate function it retrieves a "freq" value and calculates a "sleepTime" value.  Can you use LogDebugValue to display the values of these variables (only display sleepTime when it's not equal to zero) on the problem system?
The problem is that "freq" is a "long" and LogDebugValue expects an "int".  I think you can just change the definition of LogDebugValue to accept a "long" instead... it's just a couple functions down from LimitFrameRate.
Perhaps the behavior of the "Sleep" function is different on the problem system's OS.  The other thing to try (as a test only) would be to delete or comment out the "System.Threading.Thread.Sleep" line.  This would cause the game to take 100% of the cpu instead of allowing the OS to use the CPU that the game doesn't need while waiting for the next frame.  If that makes it work better, maybe it could be fixed by adding a condition like:
Code: [Select]
if (sleepTime > 2) System.Threading.Thread.Sleep(sleepTime - 2);

Strange, I thought the LimitFrameRate function was more complicated... I thought it had some complicated problems that I fixed... I wonder if I put the wrong version in.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Bug and improvement report
« Reply #19 on: 2011-01-24, 07:33:30 AM »
Hi bluemonkmn!

I made the adjustments in the test program.  I tested it and got these results:

At home where it works:
freq: 1869890000
sleeptime: around 15 and 16

At work (where it fails):
freq: 2857783
sleeptime: jumps between 11-12 and 15-16

Is that helpful?
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bug and improvement report
« Reply #20 on: 2011-01-25, 03:38:10 AM »
Sorry, I keep forgetting to debug it on my laptop here.  Strange that freq is so much lower at work.  But still it should be good enough.  It seems to be calculating about the same value.  But maybe the sleep function isn't performing as well.  Maybe you should try the other test -- what happens if you comment out or delete the "Sleep" line?  If that works, what happens if you replace the "sleep" line with by suggested code?

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Bug and improvement report
« Reply #21 on: 2011-01-25, 11:34:34 AM »
In the test project, if I remove "System.Threading.Thread.Sleep(sleepTime);" it runs at 60 fps!  If I replace the same line with the code you suggested, it runs at 50 fps...

If tried it in my game and I get the same results.

So it would seem removing the sleep line solves the problem!

Thanks bluemonkmn! :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bug and improvement report
« Reply #22 on: 2011-01-25, 05:58:23 PM »
That's not a good solution because it causes the game to use 100% of the CPU.  Sleep is required to give up some CPU time.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Bug and improvement report
« Reply #23 on: 2011-01-26, 07:23:43 AM »
Hum...  Then what should be done?  ???
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bug and improvement report
« Reply #24 on: 2011-01-26, 03:17:19 PM »
I tried reproducing the problem on my laptop, but I couldn't get the laptop to behave as well as your experiment.  Deleting the "Sleep" line did not make the game run at the correct FPS.  So I guess it will be back to you to help figure this out by testing my ideas.  My first idea is to compare your current solution (see that it is using 100% CPU, or maybe 50% CPU if you have a dual core processor) to one where you use Sleep(0) instead of Sleep(sleepTime).  If that doesn't work try only using sleep(0) when sleepTime is greater than 10. See how that affects the frame rate and the CPU usage.  My next idea is to change both instances of the number "2" in my suggested alternative to a larger number.  Since sleepTime was always a number greater than 10, you could try using something as high as 10 instead of 2, and still expect some extra CPU time back.  Translating from code to English, that line would then be saying something like this:
"If the number of milliseconds before the end of this frame's time is greater than 2, then wait for the calculated number of milliseconds minus 2.".  The intention is to sleep less if the Sleep function is having trouble returning control at the right time.  We calculated that we should sleep for 11 milliseconds after this frame is done processing in order to wait for achieving 60 FPS, but apparently when we tell the system to sleep for 11 milliseconds (or even 9 milliseconds, apparently) it waits longer.

So I don't know if you have any other way to verify that or any other ideas to test.  I know sleep(0) is supposedly a special value that yields the CPU only for the remainder of this process' time slice.  Not entirely sure what that means, but that's why I suggested that as a test too.

Aha -- it looks like there are some answers here: http://social.msdn.microsoft.com/Forums/en/clr/thread/facc2b57-9a27-4049-bb32-ef093fbf4c29.  I will have to read that article and see if there is another solution.

Edit: I wonder if Thread.SpinWait would give CPU back to the system.  It's hard to calculate what parameter value to pass to spinwait because apparently it depends on the speed of the processor.  But if that does yield the CPU, maybe we could just use SpinWait(1000) and see if that is precise enough.  (Surely 1000 CPU cycles is less than 1 millisecond... but I'm not sure if SpinWait counts CPU cycles.)

Edit 2: SpinWait does not return any CPU time.  Maybe simply removing the Sleep is the best solution.  People generally do not do other things at the same time they play games anyway.  And if you de-activate the window do do something else, some CPU time is returned while the game is paused anyway.  And most computers have dual core CPUs anyway so it will only be taking 1 core, and the other is available for other tasks.  I guess it's alright to simply remove Sleep if you want reliable frame rates.
« Last Edit: 2011-01-26, 05:41:38 PM by bluemonkmn »

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Bug and improvement report
« Reply #25 on: 2011-01-27, 11:24:33 AM »
Hi bluemonkmn!

I tried many of your ideas, but there's only one thing that makes the fps go back to 60 (where it should be).  If I always do Sleep(0), then it works.  I guess it seems pretty useless but since you said it does free some marginal CPU time, I think I'm going to let it in there.  I'm going to delete the whole sleeptime calculation, it's not useful anymore.

Thanks again! :)
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Bug and improvement report
« Reply #26 on: 2011-01-27, 08:13:57 PM »
It seems odd that the framerate stays at exactly 60 even when you're not limiting the framerate.  Maybe the framerate is otherwise limited via a vsync setting on your graphics card or something, in which case extra sleeping could cause the decreased framerate.  I wonder if there's a way to determine if the graphics card or something else is forcing a vsync at runtime and not manually limit the framerate in that case (unless you want to decrease the framerate, but then how would we know what number to use to decrease it?)
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Bug and improvement report
« Reply #27 on: 2011-01-28, 06:18:21 AM »
I don't think anybody was running without LimitFrameRate.  We were just running without the Sleep in LimitFrameRate.

Vincent

  • SGDK2 Addict
  • Expert
  • Fanatic
  • *****
  • Posts: 612
  • Legacy of Kain: Revival is completed!!!
    • View Profile
    • Chivalrous Games
    • Email
Re: Bug and improvement report
« Reply #28 on: 2011-01-28, 07:38:15 AM »
Just like bluemonkmn said, I'm always calling LimitFrameRate,  I simply eliminated the sleeptime calculation.  If I don't call LimitFrameRate, it goes up near 1500 fps.
Legacy of Kain: Revival completed!
http://lokrevival.webs.com

See also my company website:
http://chivalrousgames.com

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Bug and improvement report
« Reply #29 on: 2011-01-28, 12:25:05 PM »
Wait, so how does it stay at 60 fps if you only call sleep(0)?
Edward Dassmesser