jitter_stutter.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. Sometimes, only some objects appear to jitter (character or background). This
  32. happens when they are processed in different time sources (one is processed in
  33. the physics step while another is processed in the idle step).
  34. This cause of jitter can be alleviated by enabling
  35. :ref:`physics interpolation <doc_physics_interpolation_quick_start_guide>`
  36. in the Project Settings. Physics interpolation will smooth out physics updates by
  37. interpolating the transforms of physics objects between physics frames.
  38. This way, the visual representation of physics objects will always look
  39. smooth no matter the framerate and physics tick rate.
  40. Enabling physics interpolation has some caveats you should be aware of.
  41. For example, care should be taken when teleporting objects so that they
  42. don't visibly interpolate between the old position and new position
  43. when it's not intended. See the
  44. :ref:`doc_physics_interpolation` documentation for details.
  45. .. note::
  46. Enabling physics interpolation will increase input lag for behavior
  47. that depends on the physics tick, such as player movement.
  48. In most games, this is generally preferable to jitter, but consider this carefully
  49. for games that operate on a fixed framerate (like fighting or rhythm games).
  50. This increase in input lag can be compensated by increasing the physics
  51. tick rate as described in the :ref:`doc_jitter_stutter_input_lag` section.
  52. Stutter
  53. -------
  54. Stutter may happen due to several different reasons. One reason is the game
  55. not being able to keep full framerate performance due to a CPU or GPU bottleneck.
  56. Solving this is game-specific and will require
  57. :ref:`optimization <doc_general_optimization>`.
  58. Another common reason for stuttering is *shader compilation stutter*. This occurs
  59. when a shader needs to be compiled when a new material or particle effect is spawned
  60. for the first time in a game. This kind of stuttering generally only happens on the first
  61. playthrough, or after a graphics driver update when the shader cache is invalidated.
  62. Since Godot 4.4, when using the Forward+ or Mobile renderers, the engine tries to
  63. avoid shader compilation stutter using an ubershader approach.
  64. For this approach to be most effective, care must be taken
  65. when designing scenes and resources so that Godot can gather as much information as
  66. possible when the scene/resource is loaded, as opposed as to when it's being drawn
  67. for the first time. See :ref:`doc_pipeline_compilations` for more information.
  68. However, when using the Compatibility renderer, it is not possible to use this
  69. ubershader approach due to technical limitations in OpenGL. Therefore, to avoid
  70. shader compilation stutter in the Compatibility renderer, you need to spawn every
  71. mesh and visual effect in front of the camera for a single frame when the level is loading.
  72. This will ensure the shader is compiled when the level is loaded, as opposed to
  73. occurring during gameplay. This can be done behind solid 2D UI (such as a fullscreen
  74. :ref:`class_ColorRect` node) so that it's not visible to the player.
  75. .. note::
  76. On platforms that support disabling V-Sync, stuttering can be made less
  77. noticeable by disabling V-Sync in the project settings. This will however cause
  78. tearing to appear, especially on monitors with low refresh rates. If your
  79. monitor supports it, consider enabling variable refresh rate (G-Sync/FreeSync)
  80. while leaving V-Sync enabled. This allows mitigating some forms of stuttering
  81. without introducing tearing. However, it will not help with large stutters,
  82. such as the ones caused by shader compilation stutter.
  83. Forcing your graphics card to use the maximum performance profile can also help
  84. reduce stuttering, at the cost of increased GPU power draw.
  85. Additionally, stutter may be induced by the underlying operating system.
  86. Here is some information regarding stutter on different OSes:
  87. Windows
  88. ~~~~~~~
  89. Windows is known to cause stutter in windowed games. This mostly depends on the
  90. hardware installed, drivers version and processes running in parallel (e.g.
  91. having many browser tabs open may cause stutter in a running game). To avoid
  92. this, Godot raises the game priority to "Above Normal". This helps considerably,
  93. but may not completely eliminate stutter.
  94. Eliminating this completely requires giving your game full privileges to become
  95. "Time Critical", which is not advised. Some games may do it, but it is advised
  96. to learn to live with this problem, as it is common for Windows games and most
  97. users won't play games windowed (games that are played in a window, e.g. puzzle
  98. games, will usually not exhibit this problem anyway).
  99. For fullscreen, Windows gives special priority to the game so stutter is no
  100. longer visible and very rare. This is how most games are played.
  101. When using a mouse with a polling rate of 1,000 Hz or more, consider using a
  102. fully up-to-date Windows 11 installation which comes with fixes related to high
  103. CPU utilization with high polling rate mice. These fixes are not available in
  104. Windows 10 and older versions.
  105. .. tip::
  106. Games should use the **Exclusive Fullscreen** window mode, as opposed to
  107. **Fullscreen** which is designed to prevent Windows from automatically
  108. treating the window as if it was exclusive fullscreen.
  109. **Fullscreen** is meant to be used by GUI applications that want to use
  110. per-pixel transparency without a risk of having it disabled by the OS. It
  111. achieves this by leaving a 1-pixel line at the bottom of the screen. By
  112. contrast, **Exclusive Fullscreen** uses the actual screen size and allows
  113. Windows to reduce jitter and input lag for fullscreen games.
  114. Linux
  115. ~~~~~
  116. Stutter may be visible on desktop Linux, but this is usually associated with
  117. different video drivers and compositors. Some compositors may also trigger this
  118. problem (e.g. KWin), so it is advised to try using a different one to rule it
  119. out as the cause. Some window managers such as KWin and Xfwm allow you to
  120. manually disable compositing, which can improve performance (at the cost of
  121. tearing).
  122. There is no workaround for driver or compositor stuttering, other than reporting
  123. it as an issue to the driver or compositor developers. Stutter may be more
  124. present when playing in windowed mode as opposed to fullscreen, even with
  125. compositing disabled.
  126. `Feral GameMode <https://github.com/FeralInteractive/gamemode>`__ can be used
  127. to automatically apply optimizations (such as forcing the GPU performance profile)
  128. when running specific processes.
  129. macOS
  130. ~~~~~
  131. Generally, macOS is stutter-free, although recently some bugs were reported when
  132. running on fullscreen (this is a macOS bug). If you have a machine exhibiting
  133. this behavior, please let us know.
  134. Android
  135. ~~~~~~~
  136. Generally, Android is stutter and jitter-free because the running activity gets
  137. all the priority. That said, there may be problematic devices (older Kindle Fire
  138. is known to be one). If you see this problem on Android, please let us know.
  139. iOS
  140. ~~~
  141. iOS devices are generally stutter-free, but older devices running newer versions
  142. of the operating system may exhibit problems. This is generally unavoidable.
  143. .. _doc_jitter_stutter_input_lag:
  144. Input lag
  145. ---------
  146. Project configuration
  147. ~~~~~~~~~~~~~~~~~~~~~
  148. On platforms that support disabling V-Sync, input lag can be made less
  149. noticeable by disabling V-Sync in the project settings. This will however cause
  150. tearing to appear, especially on monitors with low refresh rates. It's suggested
  151. to make V-Sync available as an option for players to toggle.
  152. When using the Forward+ or Mobile rendering methods, another way to reduce
  153. visual latency when V-Sync is enabled is to use double-buffered V-Sync instead
  154. of the default triple-buffered V-Sync. Since Godot 4.3, this can be achieved by
  155. reducing the **Display > Window > V-Sync > Swapchain Image Count** project
  156. setting to ``2``. The downside of using double buffering is that framerate will
  157. be less stable if the display refresh rate can't be reached due to a CPU or GPU
  158. bottleneck. For instance, on a 60 Hz display, if the framerate would normally
  159. drop to 55 FPS during gameplay with triple buffering, it will have to drop down
  160. to 30 FPS momentarily with double buffering (and then go back to 60 FPS when
  161. possible). As a result, double-buffered V-Sync is only recommended if you can
  162. *consistently* reach the display refresh rate on the target hardware.
  163. Increasing the number of physics iterations per second can also reduce
  164. physics-induced input latency. This is especially noticeable when using physics
  165. interpolation (which improves smoothness but increases latency). To do so, set
  166. **Physics > Common > Physics Ticks Per Second** to a value higher than the
  167. default ``60``, or set ``Engine.physics_ticks_per_second`` at runtime in a
  168. script. Values that are a multiple of the monitor refresh rate (typically
  169. ``60``) work best when physics interpolation is disabled, as they will avoid
  170. jitter. This means values such as ``120``, ``180`` and ``240`` are good starting
  171. points. As a bonus, higher physics FPSes make tunneling and physics instability
  172. issues less likely to occur.
  173. The downside of increasing physics FPS is that CPU usage will increase, which
  174. can lead to performance bottlenecks in games that have heavy physics simulation
  175. code. This can be alleviated by increasing physics FPS only in situations where
  176. low latency is critical, or by letting players adjust physics FPS to match their
  177. hardware. However, different physics FPS will lead to different outcomes in
  178. physics simulation, even when ``delta`` is consistently used in your game logic.
  179. This can give certain players an advantage over others. Therefore, allowing the
  180. player to change the physics FPS themselves should be avoided for competitive
  181. multiplayer games.
  182. Lastly, you can disable input buffering on a per-rendered frame basis by calling
  183. ``Input.set_use_accumulated_input(false)`` in a script. This will make it so the
  184. ``_input()`` and ``_unhandled_input()`` functions in your scripts are called on
  185. every input, rather than accumulating inputs and waiting for a frame to be
  186. rendered. Disabling input accumulation will increase CPU usage, so it should be
  187. done with caution.
  188. .. tip::
  189. On any Godot project, you can use the ``--disable-vsync``
  190. :ref:`command line argument <doc_command_line_tutorial>` to forcibly disable V-Sync.
  191. Since Godot 4.2, ``--max-fps <fps>`` can also be used to set a FPS limit
  192. (``0`` is unlimited). These arguments can be used at the same time.
  193. Hardware/OS-specific
  194. ~~~~~~~~~~~~~~~~~~~~
  195. If your monitor supports it, consider enabling variable refresh rate
  196. (G-Sync/FreeSync) while leaving V-Sync enabled, then cap the framerate in the
  197. project settings to a slightly lower value than your monitor's maximum refresh
  198. rate as per `this page <https://blurbusters.com/howto-low-lag-vsync-on/>`__.
  199. For example, on a 144 Hz monitor, you can set the project's framerate cap to
  200. ``141``. This may be counterintuitive at first, but capping the FPS below the
  201. maximum refresh rate range ensures that the OS never has to wait for vertical
  202. blanking to finish. This leads to *similar* input lag as V-Sync disabled with
  203. the same framerate cap (usually less than 1 ms greater), but without any
  204. tearing.
  205. This can be done by changing the **Application > Run > Max FPS** project
  206. setting or assigning ``Engine.max_fps`` at runtime in a script.
  207. On some platforms, you can also opt into a low-latency mode in the graphics
  208. driver options (such as the NVIDIA Control Panel on Windows). The **Ultra**
  209. setting will give you the lowest possible latency, at the cost of slightly lower
  210. average framerates. Forcing the GPU to use the maximum performance profile
  211. can also further reduce input lag, at the cost of higher power consumption
  212. (and resulting heat/fan noise).
  213. Finally, make sure your monitor is running at its highest possible refresh rate
  214. in the OS' display settings.
  215. Also, ensure that your mouse is configured to use its highest polling rate
  216. (typically 1,000 Hz for gaming mice, sometimes more). High USB polling rates can
  217. however result in high CPU usage, so 500 Hz may be a safer bet on low-end CPUs.
  218. If your mouse offers multiple :abbr:`DPI (Dots Per Inch)` settings, consider also
  219. `using the highest possible setting and reducing in-game sensitivity to reduce mouse latency <https://www.youtube.com/watch?v=6AoRfv9W110>`__.
  220. On Linux when using X11, disabling compositing in window managers that allow it
  221. (such as KWin or Xfwm) can reduce input lag significantly.
  222. Reporting jitter, stutter or input lag problems
  223. -----------------------------------------------
  224. If you are reporting a stutter or jitter problem (opening an issue) not caused
  225. by any of the above reasons, please specify very clearly all the information
  226. possible about device, operating system, driver versions, etc. This may help to
  227. better troubleshoot it.
  228. If you are reporting input lag problems, please include a capture made with a
  229. high speed camera (such as your phone's slow motion video mode). The capture
  230. **must** have both the screen and the input device visible so that the number of
  231. frames between an input and the on-screen result can be counted. Also, make
  232. sure to mention your monitor's refresh rate and your input device's polling rate
  233. (especially for mice).
  234. Also, make sure to use the correct term (jitter, stutter, input lag) based on the
  235. exhibited behavior. This will help understand your issue much faster. Provide a
  236. project that can be used to reproduce the issue, and if possible, include a
  237. screen capture demonstrating the bug.