Coder Social home page Coder Social logo

Vertical Sync / Frame limiter about pyboy HOT 9 CLOSED

baekalfen avatar baekalfen commented on May 13, 2024
Vertical Sync / Frame limiter

from pyboy.

Comments (9)

krs013 avatar krs013 commented on May 13, 2024 1

Sure thing--Discord's krs013#8895 and my email is in the commit logs.

from pyboy.

Baekalfen avatar Baekalfen commented on May 13, 2024

Frame limiter is only partially fixed. It only works correctly, when the host monitor refreshes at 60hz.

from pyboy.

krs013 avatar krs013 commented on May 13, 2024

So it seems to me like this would be the next issue to resolve. I've looked through the code a bit and it seems like PyBoy/MB/Coordinator.py and PyBoy/__init__.py are where the relevant code will be; is that right? Do you have a vision for how this could/should be implemented?

The SDL_Renderers in GameWindow_SDL2.py and GameWindow_Scanline.py do have an optional PRESENTVSYNC flag that I didn't add just yet, but I've tested it before and it doesn't seem to automatically force a 60 Hz refresh rate--I think it just ensures that the present step occurs during the monitor's Vsync period to prevent tearing. If multiple frames are presented faster than 60 Hz, I suppose it would just skip as many as needed. As far as I can tell, we will have to do it manually, at least to within some tolerance.

I did see there was some code that waits on a HALT instruction until an interrupt occurs. Something like that might work, if the VBLANK interrupt is triggered on a timer, perhaps? However, I've also been looking at the documentation for the gameboy's sound, and it will require faster updates than the screen (512 Hz), so something that just pauses the program until it's time to render a frame probably won't do, unless sound is managed in a separate thread or something. If we were in Python3, I'd say this is a job for coroutines and asyncio, but that functionality is very different or doesn't exist at all in Python2.

Anyway, if you have any thoughts on this, I'd be happy to hear them, or I can start experimenting and see if I find something that works. Once the frame limiter works, perhaps we could take a shot at sound, and then this will be a fully functional emulator!

from pyboy.

Baekalfen avatar Baekalfen commented on May 13, 2024

Around line 80 of Coordinator.py, right after rendering the screen, seems like the right place to do it.

The simple solution is to add a type of waiting statement, so we won't progress faster than 60Hz on average. This problem I ran into, was that the timing of time.sleep is not precise enough, and VSync is unreliable, as framerates are not always 60Hz anymore. A method of VSync could work, if we can drop frames, and duplicate frames — i.e. don't force an update, if we are behind, and repeat a frame, if we are too early.

Regular VSync will not skip frames. It will simply wait until the new frame is out, before continuing. In games, you will often see the GPU utilization drop, with VSync, as it spends more time waiting.

Depending on how the sound library works, we might be forced to run it in a separate thread anyway.

I like you enthusiasm! :D I would say, just go nuts and see what you find. From what I have seen, I trust your judgement. You can push any changes to a pull request, if you want feedback. Just remember, that there are monitors with more than 60Hz, and it's preferable not to spinlock too much, to synchronize. Maybe you'll find, that the sound library could help with synchronization?

from pyboy.

Baekalfen avatar Baekalfen commented on May 13, 2024

I'm sorry, if you had something in the works. I was working on some Cython stuff, and found the exact SDL functions we needed. I can confirm, that it works on a 144Hz monitor, and it doesn't eat CPU cycles.

from pyboy.

Baekalfen avatar Baekalfen commented on May 13, 2024

If you have a better implementation, it is still welcome.

from pyboy.

krs013 avatar krs013 commented on May 13, 2024

Nope, I hadn’t started anything yet; that’s why I asked! I look forward to seeing what you’ve got.

And briefly, about syncing to sound, I think I did see that an emulator called mGBA could be configured to sync to either the video or the sound. On my friend’s laptop, video syncing was slightly sped up (possibly 75 Hz display) but sound was much more stable.

from pyboy.

krs013 avatar krs013 commented on May 13, 2024

Didn’t get to look too much but I did try it out and it looks great! I may poke a bit more to see if there are possible improvements but as far as I can tell it’s already a solid solution to this issue. So that’s awesome! (Also, congrats on the thesis! Mine is killing me so I’m quite envious.)

from pyboy.

Baekalfen avatar Baekalfen commented on May 13, 2024

Please do, but unless I've made a mistake, I also doubt it can be made much different.

Thanks! Just handed in today. I would like to hear what your's is about. Can I find you on Twitter/Discord/email, so we don't fill up the GitHub issues?

from pyboy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.