Search Unity

Time.deltaTime Not Constant: VSync CameraFollow and Jitter

Discussion in 'General Graphics' started by Zullar, Sep 9, 2016.

  1. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    You'll be able to insert code before "PumpOSMessages", effectively pushing PumpOSMessages later in the frame.

    It opens Feedback Hub on my machine. Does it not on yours?
     
    SonicBloomEric likes this.
  2. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    958
    @AnsisMalinsUbisoft wants to get the most up-to-date input messages they can. How best to do this? Do all input-agnostic processing before running the PumpOSMessages logic. This would allow them to process input that the system detected while their input-agnostic logic was processing.

    They simply wish to do Input-dependent logic as close to the Input "reading" as possible.

    No. The link leads to a Microsoft Feedback Hub link. It requires the Feedback Hub app to open the relevant information.
     
  3. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    203
    Oh I see, it was blank page. I'm not gonna install some MS app to see it tho, thanks.

    Hmm, I see. Not sure it would be much of a gain, FPS have to be pretty high for it to really matter imho. But then higher FPS implies that logic processed very fast as well so...
     
  4. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    958
    Actually, the lower the FPS the more this would matter. If you are able to hit a frametime of 3ms then by definition your response time is capped at 3ms.

    If your frametime, however, is 45ms-or-more (lower framerate) then it can start to feel unresponsive.
     
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    Last edited: Oct 20, 2020
    SonicBloomEric likes this.
  6. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    958
    Is there a way to share the information with those that are not on Windows 10?
     
  7. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    No idea :(. But Microsoft asks us to report all OS bugs through there.
     
    SonicBloomEric likes this.
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    I have a better vision of this now. You could do something like this:

    Code (csharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Runtime.InteropServices;
    4. using System.Threading;
    5. using UnityEngine;
    6. using UnityEngine.LowLevel;
    7. using UnityEngine.PlayerLoop;
    8. using UnityEngine.Profiling;
    9.  
    10. public class NewBehaviourScript : MonoBehaviour
    11. {
    12.     struct CustomTimeUpdate
    13.     {
    14.         public static PlayerLoopSystem.UpdateFunction s_EngineTimeUpdate;
    15.  
    16.         public static void OnTimeUpdate()
    17.         {
    18.             Profiler.BeginSample("Before WaitForLastPresentationAndGetTimestamp");
    19.             Profiler.EndSample();
    20.  
    21.             s_EngineTimeUpdate?.Invoke();
    22.  
    23.             Profiler.BeginSample("After WaitForLastPresentationAndGetTimestamp");
    24.             Thread.Sleep(10);
    25.             Profiler.EndSample();
    26.         }
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.         var playerLoop = PlayerLoop.GetDefaultPlayerLoop();
    32.         WalkPlayerLoop(ref playerLoop, 0);
    33.         PlayerLoop.SetPlayerLoop(playerLoop);
    34.     }
    35.  
    36.     private void WalkPlayerLoop(ref PlayerLoopSystem currentSystem, int depth)
    37.     {
    38.         if (currentSystem.type == typeof(Initialization.PlayerUpdateTime))
    39.         {
    40.             var functionPtr = Marshal.ReadIntPtr(currentSystem.updateFunction);
    41.             if (functionPtr != IntPtr.Zero)
    42.                 CustomTimeUpdate.s_EngineTimeUpdate = Marshal.GetDelegateForFunctionPointer<PlayerLoopSystem.UpdateFunction>(functionPtr);
    43.  
    44.             currentSystem.type = typeof(CustomTimeUpdate);
    45.             currentSystem.updateFunction = IntPtr.Zero;
    46.             currentSystem.updateDelegate = CustomTimeUpdate.OnTimeUpdate;
    47.         }
    48.  
    49.         if (currentSystem.subSystemList != null)
    50.         {
    51.             int subSystemCount = currentSystem.subSystemList.Length;
    52.             for (int i = 0; i < subSystemCount; i++)
    53.             {
    54.                 WalkPlayerLoop(ref currentSystem.subSystemList[i], depth + 1);
    55.             }
    56.         }
    57.     }
    58. }
    This callback (CustomTimeUpdate.OnTimeUpdate) would get called in place of "WaitForLastPresentationAndGetTimestamp()". Then, you can do stuff before it, forward the call to the original engine function and then do stuff afterwards too. This code will compile in current Unity versions too, but the behaviour will be slightly different:

    • In 2020.2 (after my fix), this will get called before OS events are pumped.
    • In the current 2020.2 beta, this will get called after OS events are pumped, but the original engine function will be null: the time was updated before OS events had been pumped (this is due to the issue I introduced).
    • In 2020.1 and older, this will get called after OS events are pumped.

    Also, it's not going to be called "Initialization.PlayerUpdateTime" after my fix lands. I had to move it to its own player loop category. However, this code will continue working because script updater will handle the automatic rename.
     
    Last edited: Oct 20, 2020
    SonicBloomEric, rz_0lento and valarus like this.
  9. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,229
    Just to be clear, new input system relies on this snapshot still and doesn't do it's own polling? I ask because I was under impression that the new input system could do it's own polling outside of the main loop.

    If the new input system relies on only getting new data at a specific moment in the player loop, it's totally pointless that it lets even users select where it fires in the player loop (there's a project wide setting for this on the input system).

    I trigger it manually because I have custom devices that I want to poll at the exact moment I trigger the poll and that definitely works without having to wait for another main loop iteration but it also uses my own native plugin so it's not tied to Unity internals..
     
    Last edited: Oct 21, 2020
  10. Rene-Damm

    Rene-Damm

    Unity Technologies

    Joined:
    Sep 15, 2012
    Posts:
    1,689
    Depends largely on the device and platform. There's no one single way here.

    Where available we pick up input in event form tapped either async on the UI thread (e.g. Android) or once per loop on the main thread (e.g. Windows). Examples are mouse, keyboard, pen, and tablet on Windows and all of Android input. At what resolution these events are created is up to the platform/API.

    Where we have to poll, we try to do so async on a separate thread at a user-controlled frequency. Example: XInput on Windows. Unfortunately, not all APIs that only offer polling allow that.

    Finally, there's some APIs that done async through different mechanisms. E.g. HIDs on Windows have their own async input feed that generally runs at the reporting frequency of the individual devices.

    So yeah, gist is that input *can* be gathered async and *does* retain the granularity of OS events, what that translates to in practice is generally dependent on the specific device/platform combo.
     
    rz_0lento likes this.
  11. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    958
    Fantastic. This is amazing.

    Will you be able to contribute to the PlayerLoop documentation for the newly added system? I hope so... the current docs are extremely thin and your explanations here are like a breath of fresh air...
     
    goncalo-vasconcelos likes this.
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    I agree with you, the whole thing needs its documentation rewritten from scratch.

    For the new system I added, I wrote this in the script reference:

    Update phase in the native player loop that waits for the operating system (OS) to flip the back buffer to the display and update the time in the engine.

    TimeUpdate happens at the transition between two different frames.
     
    SonicBloomEric likes this.
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    @AnsisMalinsUbisoft, this has landed to 2020.2.0b12. Please see https://forum.unity.com/threads/tim...follow-and-jitter.430339/page-10#post-6438914 on details on how you'd be able to use it.
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    Forgot to update this thread: Android fix landed to 2021.1.0a2 for both OpenGL ES and Vulkan.
     
  15. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    The fix for this landed to 2020.2.0b12 as well. Furthermore, while investigating it I also found that Time.deltaTime broke badly if you went to your driver settings (like Nvidia control panel) and force disabled VSync, while the game thought that it is enabled. So I fixed that too at the same time.
     
    m0nsky, SonicBloomEric and rz_0lento like this.
  16. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,229
    I'm now assuming Vulkan fix hasn't landed for PC?
     
  17. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,229
    You have any estimate for the multimonitor fix for 2021.1 alphas? :)
     
  18. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    It has not, no.

    2021.1.0a5.
     
    m0nsky and rz_0lento like this.
  19. Shawn-Halwes

    Shawn-Halwes

    Joined:
    Jul 17, 2013
    Posts:
    51
    @Tautvydas-Zilys Can you post the bug link for this issue please? And do you know if this fix was backported to 2019.4 LTS?
     
  20. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    SonicBloomEric likes this.
  21. keeponshading

    keeponshading

    Joined:
    Sep 6, 2018
    Posts:
    822
    Hi. Would it be possible to get an little Git Repro with some Testprojects according to this.

    "Time.deltaTime stability improvements. Frame time calculations are more stable now, providing much smoother object movement when the game is running at a stable frame rate. Unity 2020.2 has time stability improvements on iOS, macOS, PS4, Switch, tvOS, UWP, Windows and Xbox One. Improvements on Android, Windows/Linux on Vulkan and XR will come later. See this forum discussion for more details."

    For me it s a great improvment but it s a little intransparent what´s done, what s possible now and how to use correct.

    In special in multi monitor setups and cinemachine and standard camera jittering following rigid bodys and so on.

    A little Blog Post would be nice too.

    And many thanks to
    rz_0lento

    who backfired this.
     
    Last edited: Dec 15, 2020
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
    I wrote a blog post about this a couple months ago: https://blogs.unity3d.com/2020/10/0...020-2-for-smoother-gameplay-what-did-it-take/

    Let me know if you still have questions after reading it.
     
  23. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    223
    cool, but not for android, (VR/Oculus Quest2)

    a realistic assessment of when android will be fixed?
     
  24. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,046
  25. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    223
  26. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    223
    it's getting better and better..... now nothing works with Unity 2020.2

    > texture browser lags as hell
    > preview packages checkbox for package manager doesn't work! They don't show up!

    enough problems already to not start with that version as well! pfff....

    Guys, UNITY is a mess and by what happened to the company last year, and all the employers who left the company (core developers!!! Whole HQ are empty (see job postings)....

    ....so much for the future oft game developing!
     
  27. xtytyx

    xtytyx

    Joined:
    Dec 29, 2017
    Posts:
    15
    First off i want to say, thank you for all of your work with this project once all the bits get ironed out this will make for some great improvements across the board!

    I seem to be having a problem with any kind of interaction with the 2d pixel perfect camera.

    This is only a problem in 2020.2, in 2020.1 this issue is non existant!

    I have a video showing the problem. when enabled it jitters very badly, and offsets the world relative to the camera

    using fixedUpdate to calculate positions with a low fixedDeltaTime AND having a high framerate <- best option
    (using a low fixedDeltaTime and calculating positions within fixedUpdate was how i was getting around the non consistent delta times previous to these fixes)

    using fixedUpdate to calculate positions with a low fixedDeltaTime AND vsync <- close second

    using deltaTime with high framerate <- third

    using deltaTime with vsync at 60 (basically unplayable) <fourth

    but in all scenarios when pixel perfect camera is enabled we see the characters jump forward and stutter back and fourth

    Again these are only valid when using the 2d pixel perfect camera (as you can see in the video) when pixel perfect is disabled things are smooth (although not pixel perfect)

    I'm not sure if here or if in the URP 2d threads is a better place to post this.

    I also understand this was a huge undertaking and am extremely happy to see it implemented and only wanted to flag this as an issue so it can be known!

    I will also be uploading a bug if this is an unknown issue!
     
    Zullar likes this.
  28. MartyMcFly1337

    MartyMcFly1337

    Joined:
    Dec 9, 2016
    Posts:
    2
    On behalf of all Unity devs can I thank @Zullar for pushing this deep issue to a great conclusion from a standing start.
     
    xtytyx, Edy and Vincent13122 like this.
  29. stereorobo

    stereorobo

    Joined:
    Aug 16, 2018
    Posts:
    1
    Glad to see the fix added to Android, but when can we expect the deltatime fix to make it to Linux as well?
     
    jaymei likes this.
  30. Starburst999

    Starburst999

    Joined:
    May 8, 2017
    Posts:
    17
    Will the fix land on 2020.2 eventually?
     
  31. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    223
  32. Justin-Wasilenko

    Justin-Wasilenko

    Joined:
    Mar 10, 2015
    Posts:
    92
  33. SOIL

    SOIL

    Joined:
    Sep 2, 2011
    Posts:
    223
    I imported my 2020.x project to the 2021.1 beta and made a build. No jittering anymore! Everything runs smooth on the Oculus Quest 2. Finally! Did you made a build with 2021.1, activated phase sync as well, and set the time step to 0.01111? btw multiview/URP. and btw the quest2 is not really a strong device. I still go for render scale 1.0. Because only there you get the full fps. Also I use the Oculus Metrics Tool, to test the framerate on every corner.

    appendix: ups, I see you are aware of all the things xD Now I wonder....
     
    Last edited: Feb 13, 2021
  34. rahul_ak

    rahul_ak

    Joined:
    Mar 17, 2019
    Posts:
    1
    Try Time.fixedDeltaTime instead of Time.deltaTime, it worked for me.
     
  35. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,318
    It does not work, because fixedDeltaTime is the physics framerate, and not related to the display framerate.

    --Eric
     
    Prodigga likes this.
  36. tessiof

    tessiof

    Joined:
    Dec 6, 2017
    Posts:
    25
    Last edited: May 29, 2021
unityunity