Search Unity

Disabling V Sync on Android

Discussion in '2D' started by Jakems, Apr 1, 2019.

  1. Jakems

    Jakems

    Joined:
    Nov 23, 2017
    Posts:
    1
    Hi, I was wondering if it is possible to disable Vsync for Android Devices.
    I allready unchecked the Vsync Checkbox in the Quality settings for the "Very Low" Graphic Setting,
    which is the Default one for Android Builds.
    When running the Game in the Editor the Profiler shows me, that no CPU usage goes to the Vsync
    calculation, but when I Build the Game on my Android device, Vsync takes so long to Calculate, that
    my game lags.
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    272
    Hi Jakob,

    While the profiler shows V Sync the same way s it does other calculations, it is basically idle time where nothing is calculated. It just waits until the next appropriate V Blank comes along to flip the frame over.

    Here is some further explanation on commonly confusing Samples from an upcoming update to the documentation:
    • WaitForTargetFPS: Time spent waiting for the targeted FPS specified by Application.targetFrameRate. The Editor doesn’t VSync on the GPU and instead also uses WaitForTargetFPS to simulate the delay for VSync.

    • Gfx.ProcessCommands: This sample on the render thread encompasses all processing of the Rendering commands on the render thread. Some of that time might be spend waiting for VSync or new commands from the main thread, which can be seen from it’s child sample Gfx.WaitForPresent.

    • Gfx.WaitForCommands: This sample indicates that the render thread is ready for new commands and might indicate a bottle neck on the main thread.

    • Gfx.PresentFrame: This render thread sample is time spent waiting for the GPU to render and present the frame, which might include waiting for VSync.
    • Gfx.WaitForPresent: When the main thread is ready to start rendering the next frame, but the render thread has not finished waiting on the GPU to Present the frame. This might indicate that your game is GPU bound. Look at the Timeline view to see if the render thread is simultaneously spending time in Gfx.PresentFrame. If the render thread is still spending time in Camera.Render, your game is CPU bound and e.g. spending too much time sending draw calls/textures to the GPU.
    So to get to the bottom of this, you'll need to profile the game on your target device (which is always advisable, as the Editor functions fundamentally different, even to a Standalone build). Since this is an issue around the interplay of the Main thread, the Render thread and the GPU, you'll want to look at the Timeline view of the CPU Usage Profiler module instead of the Hierarchy view, which only shows the Main thread.

    One thing that happens on mobile devices is that Application.targetFrameRate is by default set to -1, from the documentation of that field:
    "- On mobile platforms the default frame rate is less than the maximum achievable frame rate due to need to conserve battery power. Typically on mobile platforms the default frame rate is 30 frames per second."

    so you might want to set that to 60, but keep in mind that you're phone will then use more power, heat up faster and as it heats up, the device might get throttled by the OS.

    Additionally I'd check if you get any changes if the VSync setting for all quality levels is set to not vsync. If you want to only affect mobile devices to not VSync you can also add this code to e.g. some startup logic of the game

    Code (CSharp):
    1. if(Application.isMobilePlatform)
    2.     QualitySettings.vSyncCount = 0
     
  3. Pacosmico

    Pacosmico

    Joined:
    Jan 16, 2014
    Posts:
    8
    Hello, I just want to say,
    I have been screaming of pain because I had a massive VSync Time (30 fps cap) with the Quality Setting . VSync set to -Don't Sync- and turns out Don't Sync actually means Sync Every2 (30fps)????? WTF???
    I just tested it, Don't Sync == Every Second V Blank (same effect)
    Every V Blank == NO CAP (250 fps according to Profiler connected to actual Android device)

    I'm using 2019.1.8 (not sure if this was always like that)

    This is very non intuitive and really really very annoying indeed!!. Sorry for the tone.

    Saludos
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    272
    Please Check this behavior against what the Documentation for targetFrameRate says. That the documentation for VSync does not alert to this behavior is a separate issue that I've noted down to be addressed in documentation.

    If the behavior is other than documented, please file a bug and post the issue ID here.
     
    Pacosmico likes this.
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    272
    To be clear, I'm not saying it's fine that what you described should be expected behavior, just that VSync settings and targetFrameRate have interleafing effects on each other and you did not describe if you also change them/ what their status was.
     
    Pacosmico likes this.
  6. Pacosmico

    Pacosmico

    Joined:
    Jan 16, 2014
    Posts:
    8
    There is not a single line in the project that touches "Application.targetFrameRate". That member is not referenced in the code at all, this is just a Quality setting change.
    According to my teammates, it's unclear when or why the Quality configuration was changed (that's on Us probably, no problem) but when trying to fix the low fps (30) issue, we checked the VSync setting to make sure it was not set to "Every Second Blank", and it was not (it was on Don't Sync) and we did not associated that setting with the documentation of the "targetFrameRate", it might be useful to link that in the Manual Page of the Quality Settings
    What also intrigues me, is that, the setting is currently "Every Blank" and yet the app runs at 250, as if no cap is being applied (I would expect it to run at 60?)
    this is all on device (not unity editor)

    thanks for answering so fast, btw

    DOCS (targetFrameRate)>
    Additionally if the QualitySettings.vSyncCount property is set, the targetFrameRate will be ignored and instead the game will use the vSyncCount and the platform's default render rate to determine the target frame rate. For example, if the platform's default render rate is 60 frames per second and vSyncCount is set to 2, the game will target 30 frames per second.
    (Note: neither this setting is modified or even referenced in the project code)
    I think the problem is that "VSync = Don't Sync" reads kinda like "Dont drop frames" and actually is more like "Device Default"
    The other thing that still I don't quite understand is why setting Every Blank does not cap to 60. I mean, maybe it's just that device refresh rate is actually 250, but then Every Second VBlank, shouldnt drop to 30. I also can see how I might be misunderstanding some fundamentals here.

    sorry for the eclectic post, I was writing and reading your pointers at the same time

    So, to wrap up, I think the documentation is not wrong, or the behaviour. It is that Quality Settings "Dont Sync" means, use "platform default target framerate", and that is only pointed out in the documentation for target framerate (which is not linked to in the Quality Settings Manual Page) and I think that may be misleading?
    Like this, targetFrameRate default = -1, which means default for the platform. Android default = 30fps
    Quality Settings Default = Every Blank (which overrides "targetFrameRate" according to docs, hiding this behaviour from start) meaning fps = device RefreshRate (very high this days?)
    so setting Quality Settings to Don't Sync, makes "targetFrameRate" to NOT being overrided, and there is where it kicks in (by default set to -1, which in turns means default for platform, which for Android means 30fps which is the same as vSync = 2)

    That's my summary, I hope it's not too confusing
    Greetings
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    272
    Hey again,

    yes, I think one part of the confusion is, that there is VSync, and then there is capping the framerate due to target OS defaults and restrictions, that might look like VSync but aren't, though also behave the same on some devices... it could definitely be highlighted.

    Regarding the 250 FPS on Android, I think regardless of what settings you choose, reaching 250 FPS should not happen. That's just going to needlessly burn the battery and heat up the phone, which is why there is a 30 FPS cap by default and an enforced 60 FPS in the first place. So for that, it would be super nice if you could create a bug report for that, also stating what android device this happened with. The mobile team seemed unaware of any such issues.

    P.S. running at 250 FPS is rather inadvisable, not only are your players going to complain about the battery drain and burning their fingers on the phone. a hot phone will also go into throttled mode at some point to keep it from overheating entirely. And at that stage, performance is going to drop. Not sure if that makes a difference when running at 250 FPS but still ;)
     
    Helladah and Pacosmico like this.
  8. Pacosmico

    Pacosmico

    Joined:
    Jan 16, 2014
    Posts:
    8
    Hi, sorry for not responding for a while,
    I just made a very simple project to just be sure I understood everything correctly. In the meanwhile I discovered that the profiler (via adb) seems to not show always the VSync cost of the app (hence why I saw 250fps on profiler) but I added a fps counter on the app and as expected, it never goes over 60fps

    Gracias por la ayuda, un abrazo!
     
    MartinTilo likes this.
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    272
    That's problematic. In what way does it not show it?
    If you look in the CPU Profiler's Timeline View, does it show WaitingForTargetFPS? Is there a WaitingForSemaphore sample underneath? Does it not show VSync in the CPU Profiler's Frame Chart? If it's that, it's likely due to this bug. If not, I'd appreciate a bug report so we can fix it :)