gpu_optimization.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. .. _doc_gpu_optimization:
  2. GPU optimization
  3. ================
  4. Introduction
  5. ~~~~~~~~~~~~
  6. The demand for new graphics features and progress almost guarantees that you
  7. will encounter graphics bottlenecks. Some of these can be on the CPU side, for
  8. instance in calculations inside the Godot engine to prepare objects for
  9. rendering. Bottlenecks can also occur on the CPU in the graphics driver, which
  10. sorts instructions to pass to the GPU, and in the transfer of these
  11. instructions. And finally, bottlenecks also occur on the GPU itself.
  12. Where bottlenecks occur in rendering is highly hardware-specific.
  13. Mobile GPUs in particular may struggle with scenes that run easily on desktop.
  14. Understanding and investigating GPU bottlenecks is slightly different to the
  15. situation on the CPU. This is because, often, you can only change performance
  16. indirectly by changing the instructions you give to the GPU. Also, it may be
  17. more difficult to take measurements. In many cases, the only way of measuring
  18. performance is by examining changes in the time spent rendering each frame.
  19. Draw calls, state changes, and APIs
  20. ===================================
  21. .. note:: The following section is not relevant to end-users, but is useful to
  22. provide background information that is relevant in later sections.
  23. Godot sends instructions to the GPU via a graphics API (OpenGL, OpenGL ES or
  24. Vulkan). The communication and driver activity involved can be quite costly,
  25. especially in OpenGL and OpenGL ES. If we can provide these instructions in a
  26. way that is preferred by the driver and GPU, we can greatly increase
  27. performance.
  28. Nearly every API command in OpenGL requires a certain amount of validation to
  29. make sure the GPU is in the correct state. Even seemingly simple commands can
  30. lead to a flurry of behind-the-scenes housekeeping. Therefore, the goal is to
  31. reduce these instructions to a bare minimum and group together similar objects
  32. as much as possible so they can be rendered together, or with the minimum number
  33. of these expensive state changes.
  34. 2D batching
  35. ~~~~~~~~~~~
  36. In 2D, the costs of treating each item individually can be prohibitively high -
  37. there can easily be thousands of them on the screen. This is why 2D *batching*
  38. is used. Multiple similar items are grouped together and rendered in a batch,
  39. via a single draw call, rather than making a separate draw call for each item.
  40. In addition, this means state changes, material and texture changes can be kept
  41. to a minimum.
  42. For more information on 2D batching, see :ref:`doc_batching`.
  43. 3D batching
  44. ~~~~~~~~~~~
  45. In 3D, we still aim to minimize draw calls and state changes. However, it can be
  46. more difficult to batch together several objects into a single draw call. 3D
  47. meshes tend to comprise hundreds or thousands of triangles, and combining large
  48. meshes in real-time is prohibitively expensive. The costs of joining them quickly
  49. exceeds any benefits as the number of triangles grows per mesh. A much better
  50. alternative is to **join meshes ahead of time** (static meshes in relation to each
  51. other). This can either be done by artists, or programmatically within Godot.
  52. There is also a cost to batching together objects in 3D. Several objects
  53. rendered as one cannot be individually culled. An entire city that is off-screen
  54. will still be rendered if it is joined to a single blade of grass that is on
  55. screen. Thus, you should always take objects' location and culling into account
  56. when attempting to batch 3D objects together. Despite this, the benefits of
  57. joining static objects often outweigh other considerations, especially for large
  58. numbers of distant or low-poly objects.
  59. For more information on 3D specific optimizations, see
  60. :ref:`doc_optimizing_3d_performance`.
  61. Reuse Shaders and Materials
  62. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  63. The Godot renderer is a little different to what is out there. It's designed to
  64. minimize GPU state changes as much as possible. :ref:`SpatialMaterial
  65. <class_SpatialMaterial>` does a good job at reusing materials that need similar
  66. shaders. if custom shaders are used, make sure to reuse them as much as
  67. possible. Godot's priorities are:
  68. - **Reusing Materials:** The fewer different materials in the
  69. scene, the faster the rendering will be. If a scene has a huge amount
  70. of objects (in the hundreds or thousands), try reusing the materials.
  71. In the worst case, use atlases to decrease the amount of texture changes.
  72. - **Reusing Shaders:** If materials can't be reused, at least try to re-use
  73. shaders. Note: shaders are automatically reused between
  74. SpatialMaterials that share the same configuration (features
  75. that are enabled or disabled with a check box) even if they have different
  76. parameters.
  77. If a scene has, for example, ``20,000`` objects with ``20,000`` different
  78. materials each, rendering will be slow. If the same scene has ``20,000``
  79. objects, but only uses ``100`` materials, rendering will be much faster.
  80. Pixel cost versus vertex cost
  81. =============================
  82. You may have heard that the lower the number of polygons in a model, the faster
  83. it will be rendered. This is *really* relative and depends on many factors.
  84. On a modern PC and console, vertex cost is low. GPUs originally only rendered
  85. triangles. This meant that every frame:
  86. 1. All vertices had to be transformed by the CPU (including clipping).
  87. 2. All vertices had to be sent to the GPU memory from the main RAM.
  88. Nowadays, all this is handled inside the GPU, greatly increasing performance.
  89. 3D artists usually have the wrong feeling about polycount performance because 3D
  90. DCCs (such as Blender, Max, etc.) need to keep geometry in CPU memory for it to
  91. be edited, reducing actual performance. Game engines rely on the GPU more, so
  92. they can render many triangles much more efficiently.
  93. On mobile devices, the story is different. PC and console GPUs are
  94. brute-force monsters that can pull as much electricity as they need from
  95. the power grid. Mobile GPUs are limited to a tiny battery, so they need
  96. to be a lot more power efficient.
  97. To be more efficient, mobile GPUs attempt to avoid *overdraw*. Overdraw occurs
  98. when the same pixel on the screen is being rendered more than once. Imagine a
  99. town with several buildings. GPUs don't know what is visible and what is hidden
  100. until they draw it. For example, a house might be drawn and then another house
  101. in front of it (which means rendering happened twice for the same pixel). PC
  102. GPUs normally don't care much about this and just throw more pixel processors to
  103. the hardware to increase performance (which also increases power consumption).
  104. Using more power is not an option on mobile so mobile devices use a technique
  105. called *tile-based rendering* which divides the screen into a grid. Each cell
  106. keeps the list of triangles drawn to it and sorts them by depth to minimize
  107. *overdraw*. This technique improves performance and reduces power consumption,
  108. but takes a toll on vertex performance. As a result, fewer vertices and
  109. triangles can be processed for drawing.
  110. Additionally, tile-based rendering struggles when there are small objects with a
  111. lot of geometry within a small portion of the screen. This forces mobile GPUs to
  112. put a lot of strain on a single screen tile, which considerably decreases
  113. performance as all the other cells must wait for it to complete before
  114. displaying the frame.
  115. To summarize, don't worry about vertex count on mobile, but
  116. **avoid concentration of vertices in small parts of the screen**.
  117. If a character, NPC, vehicle, etc. is far away (which means it looks tiny), use
  118. a smaller level of detail (LOD) model. Even on desktop GPUs, it's preferable to
  119. avoid having triangles smaller than the size of a pixel on screen.
  120. Pay attention to the additional vertex processing required when using:
  121. - Skinning (skeletal animation)
  122. - Morphs (shape keys)
  123. - Vertex-lit objects (common on mobile)
  124. Pixel/fragment shaders and fill rate
  125. ====================================
  126. In contrast to vertex processing, the costs of fragment (per-pixel) shading have
  127. increased dramatically over the years. Screen resolutions have increased (the
  128. area of a 4K screen is 8,294,400 pixels, versus 307,200 for an old 640×480 VGA
  129. screen, that is 27x the area), but also the complexity of fragment shaders has
  130. exploded. Physically-based rendering requires complex calculations for each
  131. fragment.
  132. You can test whether a project is fill rate-limited quite easily. Turn off
  133. V-Sync to prevent capping the frames per second, then compare the frames per
  134. second when running with a large window, to running with a very small window.
  135. You may also benefit from similarly reducing your shadow map size if using
  136. shadows. Usually, you will find the FPS increases quite a bit using a small
  137. window, which indicates you are to some extent fill rate-limited. On the other
  138. hand, if there is little to no increase in FPS, then your bottleneck lies
  139. elsewhere.
  140. You can increase performance in a fill rate-limited project by reducing the
  141. amount of work the GPU has to do. You can do this by simplifying the shader
  142. (perhaps turn off expensive options if you are using a :ref:`SpatialMaterial
  143. <class_SpatialMaterial>`), or reducing the number and size of textures used.
  144. **When targeting mobile devices, consider using the simplest possible shaders
  145. you can reasonably afford to use.**
  146. Reading textures
  147. ~~~~~~~~~~~~~~~~
  148. The other factor in fragment shaders is the cost of reading textures. Reading
  149. textures is an expensive operation, especially when reading from several
  150. textures in a single fragment shader. Also, consider that filtering may slow it
  151. down further (trilinear filtering between mipmaps, and averaging). Reading
  152. textures is also expensive in terms of power usage, which is a big issue on
  153. mobiles.
  154. **If you use third-party shaders or write your own shaders, try to use
  155. algorithms that require as few texture reads as possible.**
  156. Texture compression
  157. ~~~~~~~~~~~~~~~~~~~
  158. By default, Godot compresses textures of 3D models when imported using video RAM
  159. (VRAM) compression. Video RAM compression isn't as efficient in size as PNG or
  160. JPG when stored, but increases performance enormously when drawing large enough
  161. textures.
  162. This is because the main goal of texture compression is bandwidth reduction
  163. between memory and the GPU.
  164. In 3D, the shapes of objects depend more on the geometry than the texture, so
  165. compression is generally not noticeable. In 2D, compression depends more on
  166. shapes inside the textures, so the artifacts resulting from 2D compression are
  167. more noticeable.
  168. As a warning, most Android devices do not support texture compression of
  169. textures with transparency (only opaque), so keep this in mind.
  170. .. note::
  171. Even in 3D, "pixel art" textures should have VRAM compression disabled as it
  172. will negatively affect their appearance, without improving performance
  173. significantly due to their low resolution.
  174. Post-processing and shadows
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. Post-processing effects and shadows can also be expensive in terms of fragment
  177. shading activity. Always test the impact of these on different hardware.
  178. **Reducing the size of shadowmaps can increase performance**, both in terms of
  179. writing and reading the shadowmaps. On top of that, the best way to improve
  180. performance of shadows is to turn shadows off for as many lights and objects as
  181. possible. Smaller or distant OmniLights/SpotLights can often have their shadows
  182. disabled with only a small visual impact.
  183. Transparency and blending
  184. =========================
  185. Transparent objects present particular problems for rendering efficiency. Opaque
  186. objects (especially in 3D) can be essentially rendered in any order and the
  187. Z-buffer will ensure that only the front most objects get shaded. Transparent or
  188. blended objects are different. In most cases, they cannot rely on the Z-buffer
  189. and must be rendered in "painter's order" (i.e. from back to front) to look
  190. correct.
  191. Transparent objects are also particularly bad for fill rate, because every item
  192. has to be drawn even if other transparent objects will be drawn on top
  193. later on.
  194. Opaque objects don't have to do this. They can usually take advantage of the
  195. Z-buffer by writing to the Z-buffer only first, then only performing the
  196. fragment shader on the "winning" fragment, the object that is at the front at a
  197. particular pixel.
  198. Transparency is particularly expensive where multiple transparent objects
  199. overlap. It is usually better to use transparent areas as small as possible to
  200. minimize these fill rate requirements, especially on mobile, where fill rate is
  201. very expensive. Indeed, in many situations, rendering more complex opaque
  202. geometry can end up being faster than using transparency to "cheat".
  203. Multi-platform advice
  204. =====================
  205. If you are aiming to release on multiple platforms, test *early* and test
  206. *often* on all your platforms, especially mobile. Developing a game on desktop
  207. but attempting to port it to mobile at the last minute is a recipe for disaster.
  208. In general, you should design your game for the lowest common denominator, then
  209. add optional enhancements for more powerful platforms. For example, you may want
  210. to use the GLES2 backend for both desktop and mobile platforms where you target
  211. both.
  212. Mobile/tiled renderers
  213. ======================
  214. As described above, GPUs on mobile devices work in dramatically different ways
  215. from GPUs on desktop. Most mobile devices use tile renderers. Tile renderers
  216. split up the screen into regular-sized tiles that fit into super fast cache
  217. memory, which reduces the number of read/write operations to the main memory.
  218. There are some downsides though. Tiled rendering can make certain techniques
  219. much more complicated and expensive to perform. Tiles that rely on the results
  220. of rendering in different tiles or on the results of earlier operations being
  221. preserved can be very slow. Be very careful to test the performance of shaders,
  222. viewport textures and post processing.