jitter_stutter.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. .. _doc_jitter_stutter:
  2. Fixing jitter, stutter and input lag
  3. ====================================
  4. What is jitter, stutter and input lag?
  5. --------------------------------------
  6. *Jitter* and *stutter* are two different alterations to visible motion of
  7. objects on screen that may affect a game, even when running at full speed. These
  8. effects are mostly visible in games where the world moves at a constant speed in
  9. a fixed direction, like runners or platformers.
  10. *Input lag* is unrelated to jitter and stutter, but is sometimes discussed
  11. alongside. Input lag refers to visible on-screen delay when performing actions
  12. with the mouse, keyboard, controller or touchscreen. It can be related to game
  13. code, engine code or external factors (such as hardware). Input lag is most
  14. noticeable in games that use the mouse to aim, such as first-person games.
  15. Input lag can't be completely eliminated, but it can be reduced in several ways.
  16. Distinguishing between jitter and stutter
  17. -----------------------------------------
  18. A game running at a normal framerate without exhibiting any effect will appear smooth:
  19. .. image:: img/motion_normal.gif
  20. A game exhibiting *jitter* will shake constantly in a very subtle way:
  21. .. image:: img/motion_jitter.gif
  22. Finally, a game exhibiting *stutter* will appear smooth, but appear to *stop* or
  23. *roll back a frame* every few seconds:
  24. .. image:: img/motion_stutter.gif
  25. Jitter
  26. ------
  27. There can be many causes of jitter, the most typical one happens when the game
  28. *physics frequency* (usually 60 Hz) runs at a different resolution than the
  29. monitor refresh rate. Check whether your monitor refresh rate is different from
  30. 60 Hz.
  31. This is generally not a problem, given that most monitors are 60 Hz, and
  32. starting with Godot 3.1, a frame timer was introduced that tries to synchronize
  33. with refresh as well as possible.
  34. Sometimes only some objects appear to jitter (character or background). This
  35. happens when they are processed in different time sources (one is processed in
  36. the physics step while another is processed in the idle step). Godot 3.1 does
  37. some improvements to this, from allowing kinematic bodies to be animated in the
  38. regular ``_process()`` loop, to further fixes in the frame timer.
  39. .. note::
  40. You can use physics interpolation to mitigate physics-related jittering.
  41. See `lawnjelly's smoothing-addon <https://github.com/lawnjelly/smoothing-addon>`__
  42. for an add-on that can be dropped into any project to enable physics interpolation.
  43. Stutter
  44. -------
  45. Stutter may happen due to two different reasons. The first, and most obvious
  46. one, is the game not being able to keep full framerate performance. Solving this
  47. is game specific and will require optimization.
  48. The second is more complicated, because it is often not associated to the engine
  49. or game but the underlying operating system. Here is some information regarding
  50. stutter on different OSs.
  51. On platforms that support disabling V-Sync, stuttering can be made less
  52. noticeable by disabling V-Sync in the project settings. This will however cause
  53. tearing to appear, especially on monitors with low refresh rates. If your
  54. monitor supports it, consider enabling variable refresh rate (G-Sync/FreeSync)
  55. while leaving V-Sync enabled. This avoids mitigating some forms of stuttering
  56. without introducing tearing.
  57. Forcing your graphics card to use the maximum performance profile can also help
  58. reduce stuttering, at the cost of increased GPU power draw.
  59. Windows
  60. ^^^^^^^
  61. Windows is known to cause stutter in windowed games. This mostly depends on the
  62. hardware installed, drivers version and processes running in parallel (e.g.
  63. having many browser tabs open may cause stutter in a running game). To avoid
  64. this, starting with 3.1, Godot raises the game priority to "Above Normal". This
  65. helps considerably but may not completely eliminate stutter.
  66. Eliminating this completely requires giving your game full privileges to become
  67. "time critical", which is not advised. Some games may do it, but it is advised
  68. to learn to live with this problem, as it is common for Windows games and most
  69. users won't play games windowed (games that are played in a window, e.g. puzzle
  70. games, will usually not exhibit this problem anyway).
  71. For fullscreen, Windows gives special priority to the game so stutter is no
  72. longer visible and very rare. This is how most games are played.
  73. .. tip::
  74. Games should use the **Exclusive Fullscreen** window mode, as opposed to
  75. **Fullscreen** which is designed to prevent Windows from automatically
  76. treating the window as if it was exclusive fullscreen.
  77. **Fullscreen** is meant to be used by GUI applications that want to use
  78. per-pixel transparency without a risk of having it disabled by the OS. It
  79. achieves this by leaving a 1-pixel line at the bottom of the screen. By
  80. contrast, **Exclusive Fullscreen** uses the actual screen size and allows
  81. Windows to reduce jitter and input lag for fullscreen games.
  82. Linux
  83. ^^^^^
  84. Stutter may be visible on desktop Linux, but this is usually associated with
  85. different video drivers and compositors. Some compositors may also trigger this
  86. problem (e.g. KWin), so it is advised to try using a different one to rule it
  87. out as the cause. Some window managers such as KWin and Xfwm allow you to
  88. manually disable compositing, which can improve performance (at the cost of
  89. tearing).
  90. There is no workaround for driver or compositor stuttering, other than reporting
  91. it as an issue to the driver or compositor developers. Stutter may be more
  92. present when playing in windowed mode as opposed to fullscreen, even with
  93. compositing disabled.
  94. `Feral GameMode <https://github.com/FeralInteractive/gamemode>`__ can be used
  95. to automatically apply optimizations (such as forcing the GPU performance profile)
  96. when running specific processes.
  97. macOS
  98. ^^^^^
  99. Generally, macOS is stutter-free, although recently some bugs were reported when
  100. running on fullscreen (this is a macOS bug). If you have a machine exhibiting
  101. this behavior, please let us know.
  102. Android
  103. ^^^^^^^
  104. Generally, Android is stutter and jitter-free because the running activity gets
  105. all the priority. That said, there may be problematic devices (older Kindle Fire
  106. is known to be one). If you see this problem on Android, please let us know.
  107. iOS
  108. ^^^
  109. iOS devices are generally stutter-free, but older devices running newer versions
  110. of the operating system may exhibit problems. This is generally unavoidable.
  111. Input lag
  112. ---------
  113. Project configuration
  114. ^^^^^^^^^^^^^^^^^^^^^
  115. On platforms that support disabling V-Sync, input lag can be made less
  116. noticeable by disabling V-Sync in the project settings. This will however cause
  117. tearing to appear, especially on monitors with low refresh rates.
  118. Increasing the number of physics iterations per second can also reduce
  119. physics-induced input latency. This is especially noticeable when using physics
  120. interpolation (which improves smoothness but increases latency). To do so, set
  121. **Physics > Common > Physics Ticks Per Second** to a value higher than the
  122. default ``60``, or set ``Engine.physics_ticks_per_second`` at run-time in a
  123. script. Values that are a multiple of the monitor refresh rate (typically
  124. ``60``) work best when physics interpolation is disabled, as they will avoid
  125. jitter. This means values such as ``120``, ``180`` and ``240`` are good starting
  126. points. As a bonus, higher physics FPSes make tunneling and physics unstability
  127. issues less likely to occur.
  128. The downside of increasing physics FPS is that CPU usage will increase, which
  129. can lead to performance bottlenecks in games that have heavy physics simulation
  130. code. This can be alleviated by increasing physics FPS only in situations where
  131. low latency is critical, or by letting players adjust physics FPS to match their
  132. hardware. However, different physics FPS will lead to different outcomes in
  133. physics simulation, even when ``delta`` is consistently used in your game logic.
  134. This can give certain players an advantage over others. Therefore, allowing the
  135. player to change the physics FPS themselves should be avoided for competitive
  136. multiplayer games.
  137. Lastly, you can disable input buffering on a per-rendered frame basis by calling
  138. ``Input.set_use_accumulated_input(false)`` in a script. This will make it so the
  139. ``_input()`` and ``_unhandled_input()`` functions in your scripts are called on
  140. every input, rather than accumulating inputs and waiting for a frame to be
  141. rendered. Disabling input accumulation will increase CPU usage, so it should be
  142. done with caution.
  143. Hardware/OS-specific
  144. ^^^^^^^^^^^^^^^^^^^^
  145. If your monitor supports it, consider enabling variable refresh rate
  146. (G-Sync/FreeSync) while leaving V-Sync enabled, then cap the framerate in the
  147. project settings to a slightly lower value than your monitor's maximum refresh
  148. rate as per `this page <https://blurbusters.com/howto-low-lag-vsync-on/>`__.
  149. For example, on a 144 Hz monitor, you can set the project's framerate cap to
  150. ``141``. This may be counterintuitive at first, but capping the FPS below the
  151. maximum refresh rate range ensures that the OS never has to wait for vertical
  152. blanking to finish. This leads to *similar* input lag as V-Sync disabled with
  153. the same framerate cap (usually less than 1 ms greater), but without any
  154. tearing.
  155. This can be done by changing the **Application > Run > Max FPS** project
  156. setting or assigning ``Engine.max_fps`` at run-time in a script.
  157. On some platforms, you can also opt into a low-latency mode in the graphics
  158. driver options (such as the NVIDIA Control Panel on Windows). The **Ultra**
  159. setting will give you the lowest possible latency, at the cost of slightly lower
  160. average framerates. Forcing the GPU to use the maximum performance profile
  161. can also further reduce input lag, at the cost of higher power consumption
  162. (and resulting heat/fan noise).
  163. Finally, make sure your monitor is running at its highest possible refresh rate
  164. in the OS' display settings.
  165. Also, ensure that your mouse is configured to use its highest polling rate
  166. (typically 1,000 Hz for gaming mice, sometimes more). High USB polling rates can
  167. however result in high CPU usage, so 500 Hz may be a safer bet on low-end CPUs.
  168. If your mouse offers multiple :abbr:`DPI (Dots Per Inch)` settings, consider also
  169. `using the highest possible setting and reducing in-game sensitivity to reduce mouse latency <https://www.youtube.com/watch?v=6AoRfv9W110>`__.
  170. On Linux, disabling compositing in window managers that allow it (such as KWin
  171. or Xfwm) can reduce input lag significantly.
  172. Reporting jitter, stutter or input lag problems
  173. -----------------------------------------------
  174. If you are reporting a stutter or jitter problem (opening an issue) not caused
  175. by any of the above reasons, please specify very clearly all the information
  176. possible about device, operating system, driver versions, etc. This may help to
  177. better troubleshoot it.
  178. If you are reporting input lag problems, please include a capture made with a
  179. high speed camera (such as your phone's slow motion video mode). The capture
  180. **must** have both the screen and the input device visible so that the number of
  181. frames between an input and the on-screen result can be counted. Also, make
  182. sure to mention your monitor's refresh rate and your input device's polling rate
  183. (especially for mice).
  184. Also, make sure to use the correct term (jitter, stutter, input lag) based on the
  185. exhibited behavior. This will help understand your issue much faster. Provide a
  186. project that can be used to reproduce the issue, and if possible, include a
  187. screen capture demonstrating the bug.