pipeline_compilations.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. .. _doc_pipeline_compilations:
  2. Reducing stutter from shader (pipeline) compilations
  3. ====================================================
  4. Pipeline compilation, also commonly known as shader compilation, is an expensive
  5. operation required by the engine to be able to draw any kind of content with the
  6. GPU.
  7. In more precise terms, *shader compilation* involves the translation of the GLSL
  8. code that Godot generates into an intermediate format that can be shared across
  9. systems (such as SPIR-V when using Vulkan). However, this format can't be used
  10. by the GPU directly.
  11. *Pipeline compilation* is the step where the GPU driver converts
  12. the intermediate shader format (the result from shader compilation) to something
  13. the GPU can actually use for rendering. Drivers usually keep a cache of
  14. pipelines stored somewhere in the system to avoid repeating the process every
  15. time a game is run. This cache is usually deleted when the driver is updated.
  16. Pipelines contain more information than just the shader code, which means that
  17. for each shader, there can be dozens of pipelines or more! This makes it
  18. difficult for an engine to compile them ahead of time, both because it would be
  19. very slow, and because it would take up a lot of memory. On top of that, this
  20. step can only be performed on the user's system and it is very tough to share
  21. the result between users unless they have the exact same hardware and driver
  22. version.
  23. Before Godot 4.4, there was no solution to pipeline compilation other than
  24. generating them when an object shows up inside the camera's view, leading to the
  25. infamous *shader stutter* or hitches that only occur during the first
  26. playthrough. **With Godot 4.4, new mechanisms have been introduced to mitigate
  27. stutters from pipeline compilation.**
  28. - **Ubershaders**: Godot makes use of specialization constants, a feature that
  29. allows the driver to optimize a pipeline's code around a set of parameters
  30. such as lighting, shadow quality, etc. Specialization constants are used to
  31. optimize a shader by limiting unnecessary features. Changing a specialization
  32. constant requires recompiling the pipeline. Ubershaders are a special version
  33. of the shader that are able to change these constants while rendering, which
  34. means Godot can precompile just one pipeline ahead of time and compile the
  35. more optimized versions on the background during gameplay. This reduces the
  36. amount of pipelines that need to be created significantly.
  37. - **Pipeline precompilation**: By using ubershaders, the engine can precompile
  38. pipelines ahead of time in multiple places such as when meshes are loaded or
  39. when nodes are added to the scene. By being part of the resource loading
  40. process, pipelines can even be precompiled in multiple background threads if
  41. possible during loading screens or even gameplay.
  42. Starting in Godot 4.4, Godot will detect which pipelines are needed and
  43. precompile them at load-time. This detection system is mostly automatic, but it
  44. relies on the RenderingServer seeing evidence of all shaders, meshes, or
  45. rendering features at load-time. For example, if you load a mesh and shader
  46. while the game is running, the pipeline for that mesh/shader combination won't
  47. be compiled until the mesh/shader is loaded. Similarly, things like enabling
  48. MSAA, or instancing a VoxelGI node while the game is running will trigger
  49. pipeline recompilations.
  50. Pipeline precompilation monitors
  51. --------------------------------
  52. .. UPDATE: Future versions mentioned.
  53. Compiling pipelines ahead of time is the main mechanism Godot uses to mitigate
  54. shader stutters, but it's not a perfect solution. Being aware of the situations
  55. that can lead to pipeline stutters can be very helpful, and the workarounds are
  56. pretty straightforward compared to previous versions. These workarounds may be
  57. less necessary over time with future versions of Godot as more detection
  58. techniques are implemented.
  59. The Godot debugger offers monitors for tracking the amount of pipelines created
  60. by the game and the step that triggered their compilation. You can keep an eye
  61. on these monitors as the game runs to identify potential sources of shader
  62. stutters without having to wipe your driver cache every time you wish to test.
  63. Sudden increases of these values outside of loading screens can show up as
  64. hitches during gameplay the first time someone plays the game on their system.
  65. **It is recommended you take a look at these monitors to identify possible
  66. sources of stutter for your players**, as you might be unable to experience them
  67. yourself without deleting your driver cache or testing on a weaker system.
  68. .. figure:: img/pipeline_compilations_monitors.webp
  69. :align: center
  70. :alt: Screenshot of the Godot pipeline compilations monitor
  71. Pipeline compilations of one of the demo projects.
  72. .. note:: We can see the pipelines compiled during gameplay and
  73. verify which steps could possibly cause stuttters. Note
  74. that these values will only increase and never go down,
  75. as deleted pipelines are not tracked by these monitors
  76. and pipelines may be erased and recreated during gameplay.
  77. - **Canvas**: Compiled when drawing a 2D node. The engine does not currently
  78. feature precompilation for 2D elements and stutters will show up when the
  79. 2D node is drawn for the first time.
  80. - **Mesh**: Compiled as part of loading a 3D mesh and identifying what pipelines
  81. can be precompiled from its properties. These can lead to stutters if a mesh
  82. is loaded during gameplay, but they can be mitigated if the mesh is loaded by
  83. using a background thread. **Modifiers that are part of nodes such as material
  84. overrides can't be compiled on this step**.
  85. - **Surface**: Compiled when a frame is about to be drawn and 3D objects were
  86. instanced on the scene tree for the first time. This can also include
  87. compilation for nodes that aren't even visible on the scene tree. The stutter
  88. will occur only on the first frame the node is added to the scene, which won't
  89. result in an obvious stutter if it happens right after a loading screen.
  90. - **Draw**: Compiled on demand when a 3D object needs to be drawn and an
  91. ubershader was not precompiled ahead of time. The engine is unable to
  92. precompile this pipeline due to triggering a case that hasn't been covered
  93. yet or a modification that was done to the engine's code. Leads to stutters
  94. during gameplay. This is identical to Godot versions before 4.4. If you
  95. see compilations here, please
  96. `let the developers know <https://github.com/godotengine/godot/issues>`
  97. as this should never happen with the Ubershader system.
  98. Make sure to attach a minimal reproduction project when doing so.
  99. - **Specialization**: Compiled in the background during gameplay to optimize the
  100. framerate. Unable to cause stutters, but may result in reduced framerates if
  101. there are many happening per frame.
  102. Pipeline precompilation features
  103. --------------------------------
  104. Godot offers a lot of rendering features that are not necessarily used by every
  105. game. Unfortunately, pipeline precompilation can't know ahead of time if a
  106. particular feature is used by a project. Some of these features can only be
  107. detected when a user adds a node to the scene or toggles a particular setting in
  108. the project or the environment. The pipeline precompilation system will keep
  109. track of these features as they're encountered for the first time and enable
  110. precompilation of them for any meshes or surfaces that are created afterwards.
  111. If your game makes use of these features, **make sure to have an scene that uses
  112. them as early as possible** before loading the majority of the assets. This
  113. scene can be very simple and will do the job as long as it uses the features the
  114. game plans to use. It can even be rendered off-screen for at least one frame if
  115. necessary, e.g. by covering it with a :ref:`class_ColorRect` node or
  116. using a :ref:`class_SubViewport` located outside the window bounds.
  117. You should also keep in mind that changing any of these features during gameplay
  118. will result in immediate stutters. Make sure to only change these features from
  119. configuration screens if necessary and insert loading screens and messages when
  120. the changes are applied.
  121. - **MSAA Level**: Enabled when the level of 3D MSAA is changed on the project
  122. settings. Unfortunately, different MSAA levels being used on different
  123. viewports will lead to stutters as the engine only keeps track of one level at
  124. a time to perform precompilation.
  125. - **Reflection Probes**: Enabled when a ReflectionProbe node is placed on the
  126. scene.
  127. - **Separate Specular**: Enabled when using effects like sub-surface scattering
  128. or a compositor effect that relies on sampling the specularity directly off
  129. the screen.
  130. - **Motion Vectors**: Enabled when using effects such as TAA, FSR2 or a
  131. compositor effect that requires motion vectors (such as motion blur).
  132. - **Normal and Roughness**: Enabled when using SDFGI, VoxelGI, screen-space
  133. reflections, SSAO, SSIL, or using the ``normal_roughness_buffer`` in a custom
  134. shader or :ref:`class_CompositorEffect`.
  135. - **Lightmaps**: Enabled when a LightmapGI node is placed on the scene and a
  136. node uses a baked lightmap.
  137. - **VoxelGI**: Enabled when a VoxelGI node is placed on the scene.
  138. - **SDFGI**: Enabled when the WorldEnvironment enables SDFGI.
  139. - **Multiview**: Enabled for XR projects.
  140. - **16/32-bit Shadows**: Enabled when the configuration of the depth precision
  141. of shadowmaps is changed on the project settings.
  142. - **Omni Shadow Dual Paraboloid**: Enabled when an omni light casts shadows and
  143. uses the dual paraboloid mode.
  144. - **Omni Shadow Cubemap**: Enabled when an omni light casts shadows and uses the
  145. cubemap mode (which is the default).
  146. If you witness stutters during gameplay and the monitors report a sudden
  147. increase in compilations during the **Surface** step, it is very likely a
  148. feature was not enabled ahead of time. Ensuring that this effect is enabled
  149. while loading your game will likely mitigate the issue.
  150. Pipeline precompilation instancing
  151. ----------------------------------
  152. One common source of stutters in games is the fact that some effects are only
  153. instanced on the scene because of interactions that only happen during gameplay.
  154. For example, if you have a particle effect that is only added to the scene
  155. through a script when a player does an action. Even if the scene is preloaded,
  156. the engine might be unable to precompile the pipelines until the effect is added
  157. to the scene at least once.
  158. Luckily, it's possible for Godot 4.4 and later to
  159. precompile these pipelines as long as the scene is instantiated at least once on
  160. the scene, even if it's completely invisible or outside of the camera's view.
  161. .. figure:: img/pipeline_compilations_hidden_node.webp
  162. :align: center
  163. :alt: Screenshot of an example of a hidden Node for an effect
  164. Hidden bullet node attached to the player in one of the demo projects. This
  165. helps the engine precompile the effect's pipelines ahead of time.
  166. If you're aware of any effects that are added to the scene dynamically during
  167. gameplay and are seeing sudden increases on the compilations monitor when these
  168. effects show up, a workaround is to attach a hidden version of the effect
  169. somewhere that is guaranteed to show up.
  170. For example, if the player character is able to cause some sort of explosion,
  171. you can attach the effect as a child of the player as an invisible node. Make
  172. sure to disable the script attached to the hidden node or to hide any other
  173. nodes that could cause issues, which can be done by enabling **Editable
  174. Children** on the node.