pipeline_compilations.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. Compiling pipelines ahead of time is the main mechanism Godot uses to mitigate
  53. shader stutters, but it's not a perfect solution. Being aware of the situations
  54. that can lead to pipeline stutters can be very helpful, and the workarounds are
  55. pretty straightforward compared to previous versions. These workarounds may be
  56. less necessary over time with future versions of Godot as more detection
  57. techniques are implemented.
  58. The Godot debugger offers monitors for tracking the amount of pipelines created
  59. by the game and the step that triggered their compilation. You can keep an eye
  60. on these monitors as the game runs to identify potential sources of shader
  61. stutters without having to wipe your driver cache every time you wish to test.
  62. Sudden increases of these values outside of loading screens can show up as
  63. hitches during gameplay the first time someone plays the game on their system.
  64. **It is recommended you take a look at these monitors to identify possible
  65. sources of stutter for your players**, as you might be unable to experience them
  66. yourself without deleting your driver cache or testing on a weaker system.
  67. .. figure:: img/pipeline_compilations_monitors.webp
  68. :align: center
  69. :alt: Screenshot of the Godot pipeline compilations monitor
  70. Pipeline compilations of one of the demo projects.
  71. .. note:: We can see the pipelines compiled during gameplay and
  72. verify which steps could possibly cause stuttters. Note
  73. that these values will only increase and never go down,
  74. as deleted pipelines are not tracked by these monitors
  75. and pipelines may be erased and recreated during gameplay.
  76. - **Canvas**: Compiled when drawing a 2D node. The engine does not currently
  77. feature precompilation for 2D elements and stutters will show up when the
  78. 2D node is drawn for the first time.
  79. - **Mesh**: Compiled as part of loading a 3D mesh and identifying what pipelines
  80. can be precompiled from its properties. These can lead to stutters if a mesh
  81. is loaded during gameplay, but they can be mitigated if the mesh is loaded by
  82. using a background thread. **Modifiers that are part of nodes such as material
  83. overrides can't be compiled on this step**.
  84. - **Surface**: Compiled when a frame is about to be drawn and 3D objects were
  85. instanced on the scene tree for the first time. This can also include
  86. compilation for nodes that aren't even visible on the scene tree. The stutter
  87. will occur only on the first frame the node is added to the scene, which won't
  88. result in an obvious stutter if it happens right after a loading screen.
  89. - **Draw**: Compiled on demand when a 3D object needs to be drawn and an
  90. ubershader was not precompiled ahead of time. The engine is unable to
  91. precompile this pipeline due to triggering a case that hasn't been covered
  92. yet or a modification that was done to the engine's code. Leads to stutters
  93. during gameplay. This is identical to Godot versions before 4.4. If you
  94. see compilations here, please
  95. `let the developers know <https://github.com/godotengine/godot/issues>`
  96. as this should never happen with the Ubershader system.
  97. Make sure to attach a minimal reproduction project when doing so.
  98. - **Specialization**: Compiled in the background during gameplay to optimize the
  99. framerate. Unable to cause stutters, but may result in reduced framerates if
  100. there are many happening per frame.
  101. Pipeline precompilation features
  102. --------------------------------
  103. Godot offers a lot of rendering features that are not necessarily used by every
  104. game. Unfortunately, pipeline precompilation can't know ahead of time if a
  105. particular feature is used by a project. Some of these features can only be
  106. detected when a user adds a node to the scene or toggles a particular setting in
  107. the project or the environment. The pipeline precompilation system will keep
  108. track of these features as they're encountered for the first time and enable
  109. precompilation of them for any meshes or surfaces that are created afterwards.
  110. If your game makes use of these features, **make sure to have an scene that uses
  111. them as early as possible** before loading the majority of the assets. This
  112. scene can be very simple and will do the job as long as it uses the features the
  113. game plans to use. It can even be rendered off-screen for at least one frame if
  114. necessary, e.g. by covering it with a :ref:`class_ColorRect` node or
  115. using a :ref:`class_SubViewport` located outside the window bounds.
  116. You should also keep in mind that changing any of these features during gameplay
  117. will result in immediate stutters. Make sure to only change these features from
  118. configuration screens if necessary and insert loading screens and messages when
  119. the changes are applied.
  120. - **MSAA Level**: Enabled when the level of 3D MSAA is changed on the project
  121. settings. Unfortunately, different MSAA levels being used on different
  122. viewports will lead to stutters as the engine only keeps track of one level at
  123. a time to perform precompilation.
  124. - **Reflection Probes**: Enabled when a ReflectionProbe node is placed on the
  125. scene.
  126. - **Separate Specular**: Enabled when using effects like sub-surface scattering
  127. or a compositor effect that relies on sampling the specularity directly off
  128. the screen.
  129. - **Motion Vectors**: Enabled when using effects such as TAA, FSR2 or a
  130. compositor effect that requires motion vectors (such as motion blur).
  131. - **Normal and Roughness**: Enabled when using SDFGI, VoxelGI, screen-space
  132. reflections, SSAO, SSIL, or using the ``normal_roughness_buffer`` in a custom
  133. shader or :ref:`class_CompositorEffect`.
  134. - **Lightmaps**: Enabled when a LightmapGI node is placed on the scene and a
  135. node uses a baked lightmap.
  136. - **VoxelGI**: Enabled when a VoxelGI node is placed on the scene.
  137. - **SDFGI**: Enabled when the WorldEnvironment enables SDFGI.
  138. - **Multiview**: Enabled for XR projects.
  139. - **16/32-bit Shadows**: Enabled when the configuration of the depth precision
  140. of shadowmaps is changed on the project settings.
  141. - **Omni Shadow Dual Paraboloid**: Enabled when an omni light casts shadows and
  142. uses the dual paraboloid mode.
  143. - **Omni Shadow Cubemap**: Enabled when an omni light casts shadows and uses the
  144. cubemap mode (which is the default).
  145. If you witness stutters during gameplay and the monitors report a sudden
  146. increase in compilations during the **Surface** step, it is very likely a
  147. feature was not enabled ahead of time. Ensuring that this effect is enabled
  148. while loading your game will likely mitigate the issue.
  149. Pipeline precompilation instancing
  150. ----------------------------------
  151. One common source of stutters in games is the fact that some effects are only
  152. instanced on the scene because of interactions that only happen during gameplay.
  153. For example, if you have a particle effect that is only added to the scene
  154. through a script when a player does an action. Even if the scene is preloaded,
  155. the engine might be unable to precompile the pipelines until the effect is added
  156. to the scene at least once.
  157. Luckily, it's possible for Godot 4.4 and later to
  158. precompile these pipelines as long as the scene is instantiated at least once on
  159. the scene, even if it's completely invisible or outside of the camera's view.
  160. .. figure:: img/pipeline_compilations_hidden_node.webp
  161. :align: center
  162. :alt: Screenshot of an example of a hidden Node for an effect
  163. Hidden bullet node attached to the player in one of the demo projects. This
  164. helps the engine precompile the effect's pipelines ahead of time.
  165. If you're aware of any effects that are added to the scene dynamically during
  166. gameplay and are seeing sudden increases on the compilations monitor when these
  167. effects show up, a workaround is to attach a hidden version of the effect
  168. somewhere that is guaranteed to show up.
  169. For example, if the player character is able to cause some sort of explosion,
  170. you can attach the effect as a child of the player as an invisible node. Make
  171. sure to disable the script attached to the hidden node or to hide any other
  172. nodes that could cause issues, which can be done by enabling **Editable
  173. Children** on the node.