3d_performance_and_limitations.rst 8.0 KB

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