optimizing_3d_performance.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. .. meta::
  2. :keywords: optimization
  3. .. _doc_optimizing_3d_performance:
  4. Optimizing 3D performance
  5. =========================
  6. Introduction
  7. ~~~~~~~~~~~~
  8. Godot follows a balanced performance philosophy. In the performance world,
  9. there are always trade-offs, which consist of trading speed for
  10. usability and flexibility. Some practical examples of this are:
  11. - Rendering objects efficiently in high amounts is easy, but when a
  12. large scene must be rendered, it can become inefficient. To solve
  13. this, visibility computation must be added to the rendering, which
  14. makes rendering less efficient, but, at the same time, fewer objects are
  15. rendered, so efficiency overall improves.
  16. - Configuring the properties of every material for every object that
  17. needs to be rendered is also slow. To solve this, objects are sorted
  18. by material to reduce the costs, but at the same time sorting has a
  19. cost.
  20. - In 3D physics a similar situation happens. The best algorithms to
  21. handle large amounts of physics objects (such as SAP) are slow
  22. at insertion/removal of objects and ray-casting. Algorithms that
  23. allow faster insertion and removal, as well as ray-casting, will not
  24. be able to handle as many active objects.
  25. And there are many more examples of this! Game engines strive to be
  26. general purpose in nature, so balanced algorithms are always favored
  27. over algorithms that might be fast in some situations and slow in
  28. others.. or algorithms that are fast but make usability more difficult.
  29. Godot is not an exception and, while it is designed to have backends
  30. swappable for different algorithms, the default ones (or more like, the
  31. only ones that are there for now) prioritize balance and flexibility
  32. over performance.
  33. With this clear, the aim of this tutorial is to explain how to get the
  34. maximum performance out of Godot.
  35. Rendering
  36. ~~~~~~~~~
  37. 3D rendering is one of the most difficult areas to get performance from,
  38. so this section will have a list of tips.
  39. Reuse shaders and materials
  40. ---------------------------
  41. The Godot renderer is a little different to what is out there. It's designed
  42. to minimize GPU state changes as much as possible.
  43. :ref:`class_StandardMaterial3D`
  44. does a good job at reusing materials that need similar shaders but, if
  45. custom shaders are used, make sure to reuse them as much as possible.
  46. Godot's priorities will be like this:
  47. - **Reusing Materials**: The fewer different materials in the
  48. scene, the faster the rendering will be. If a scene has a huge amount
  49. of objects (in the hundreds or thousands) try reusing the materials
  50. or in the worst case use atlases.
  51. - **Reusing Shaders**: If materials can't be reused, at least try to
  52. re-use shaders (or StandardMaterial3Ds with different parameters but the same
  53. configuration).
  54. If a scene has, for example, 20.000 objects with 20.000 different
  55. materials each, rendering will be slow. If the same scene has
  56. 20.000 objects, but only uses 100 materials, rendering will be blazingly
  57. fast.
  58. Pixel cost vs vertex cost
  59. -------------------------
  60. It is a common thought that the lower the number of polygons in a model, the
  61. faster it will be rendered. This is *really* relative and depends on
  62. many factors.
  63. On a modern PC and console, vertex cost is low. GPUs
  64. originally only rendered triangles, so all the vertices:
  65. 1. Had to be transformed by the CPU (including clipping).
  66. 2. Had to be sent to the GPU memory from the main RAM.
  67. Nowadays, all this is handled inside the GPU, so the performance is
  68. extremely high. 3D artists usually have the wrong feeling about
  69. polycount performance because 3D DCCs (such as Blender, Max, etc.) need
  70. to keep geometry in CPU memory in order for it to be edited, reducing
  71. actual performance. Truth is, a model rendered by a 3D engine is much
  72. more optimal than how 3D DCCs display them.
  73. On mobile devices, the story is different. PC and Console GPUs are
  74. brute-force monsters that can pull as much electricity as they need from
  75. the power grid. Mobile GPUs are limited to a tiny battery, so they need
  76. to be a lot more power efficient.
  77. To be more efficient, mobile GPUs attempt to avoid *overdraw*. This
  78. means, the same pixel on the screen being rendered (as in, with lighting
  79. calculation, etc.) more than once. Imagine a town with several buildings,
  80. GPUs don't know what is visible and what is hidden until they
  81. draw it. A house might be drawn and then another house in front of it
  82. (rendering happened twice for the same pixel!). PC GPUs normally don't
  83. care much about this and just throw more pixel processors to the
  84. hardware to increase performance (but this also increases power
  85. consumption).
  86. On mobile, pulling more power is not an option, so a technique called
  87. "Tile Based Rendering" is used (almost every mobile hardware uses a
  88. variant of it), which divides the screen into a grid. Each cell keeps the
  89. list of triangles drawn to it and sorts them by depth to minimize
  90. *overdraw*. This technique improves performance and reduces power
  91. consumption, but takes a toll on vertex performance. As a result, fewer
  92. vertices and triangles can be processed for drawing.
  93. Generally, this is not so bad, but there is a corner case on mobile that
  94. must be avoided, which is to have small objects with a lot of geometry
  95. within a small portion of the screen. This forces mobile GPUs to put a
  96. lot of strain on a single screen cell, considerably decreasing
  97. performance (as all the other cells must wait for it to complete in
  98. order to display the frame).
  99. To make it short, do not worry about vertex count so much on mobile, but
  100. avoid concentration of vertices in small parts of the screen. If, for
  101. example, a character, NPC, vehicle, etc. is far away (so it looks tiny),
  102. use a smaller level of detail (LOD) model instead.
  103. An extra situation where vertex cost must be considered is objects that
  104. have extra processing per vertex, such as:
  105. - Skinning (skeletal animation)
  106. - Morphs (shape keys)
  107. - Vertex Lit Objects (common on mobile)
  108. Texture compression
  109. -------------------
  110. Godot offers to compress textures of 3D models when imported (VRAM
  111. compression). Video RAM compression is not as efficient in size as PNG
  112. or JPG when stored, but increases performance enormously when drawing.
  113. This is because the main goal of texture compression is bandwidth
  114. reduction between memory and the GPU.
  115. In 3D, the shapes of objects depend more on the geometry than the
  116. texture, so compression is generally not noticeable. In 2D, compression
  117. depends more on shapes inside the textures, so the artifacts resulting
  118. from 2D compression are more noticeable.
  119. As a warning, most Android devices do not support texture compression of
  120. textures with transparency (only opaque), so keep this in mind.
  121. Transparent objects
  122. -------------------
  123. As mentioned before, Godot sorts objects by material and shader to
  124. improve performance. This, however, can not be done on transparent
  125. objects. Transparent objects are rendered from back to front to make
  126. blending with what is behind work. As a result, please try to keep
  127. transparent objects to a minimum! If an object has a small section with
  128. transparency, try to make that section a separate material.
  129. Level of detail (LOD)
  130. ---------------------
  131. As also mentioned before, using objects with fewer vertices can improve
  132. performance in some cases. Godot has a simple system to change level
  133. of detail,
  134. :ref:`GeometryInstance <class_GeometryInstance>`
  135. based objects have a visibility range that can be defined. Having
  136. several GeometryInstance objects in different ranges works as LOD.
  137. Use instancing (MultiMesh)
  138. --------------------------
  139. If several identical objects have to be drawn in the same place or
  140. nearby, try using :ref:`MultiMesh <class_MultiMesh>`
  141. instead. MultiMesh allows the drawing of dozens of thousands of objects at
  142. very little performance cost, making it ideal for flocks, grass,
  143. particles, etc.
  144. Bake lighting
  145. -------------
  146. Small lights are usually not a performance issue. Shadows a little more.
  147. In general, if several lights need to affect a scene, it's ideal to bake
  148. it (:ref:`doc_baked_lightmaps`). Baking can also improve the scene quality by
  149. adding indirect light bounces.
  150. If working on mobile, baking to texture is recommended, since this
  151. method is even faster.