variable_rate_shading.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. .. _doc_variable_rate_shading:
  2. Variable rate shading
  3. =====================
  4. What is variable rate shading?
  5. ------------------------------
  6. In modern 3D rendering engines, shaders are much more complex compared to
  7. before. The advent of physically-based rendering, real-time global illumination
  8. and screen-space effects has increased the number of *per-pixel* shading that
  9. must be performed to render each frame. Additionally, screen resolutions also
  10. have increased a lot, with 1440p and 4K now being common target resolutions.
  11. As a result, the total shading cost in scene rendering usually represents
  12. a significant amount of the time taken to render each frame.
  13. Variable rate shading (VRS) is a method of decreasing this shading cost by
  14. reducing the resolution of *per-pixel* shading (also called *fragment* shading),
  15. while keeping the original resolution for rendering geometry. This means geometry
  16. edges remain as sharp as they would without VRS. VRS can be combined with any
  17. :ref:`doc_3d_antialiasing` technique (MSAA, FXAA, TAA, SSAA).
  18. VRS allows specifying the shading quality in a local manner, which makes it
  19. possible to have certain parts of the viewport receive more detailed shading
  20. than others. This is particularly useful in virtual reality (VR) to achieve
  21. *foveated rendering*, where the center of the viewport is more detailed than the
  22. edges.
  23. Here's a scene rendered with rate shading disabled then enabled, using the
  24. density map linked at the bottom of this page:
  25. .. figure:: img/variable_rate_shading_textured_disabled.webp
  26. :align: center
  27. :alt: Variable rate shading disabled in textured scene
  28. Variable rate shading disabled in textured scene
  29. .. figure:: img/variable_rate_shading_textured_enabled.webp
  30. :align: center
  31. :alt: Variable rate shading enabled in textured scene (lower quality, but higher performance)
  32. Variable rate shading enabled in textured scene (lower quality, but higher performance)
  33. When used in scenes with low-frequency detail (such as scenes with a
  34. stylized/low-poly aesthetic), it's possible to achieve similar performance gains,
  35. but with less reduction in visual quality:
  36. .. figure:: img/variable_rate_shading_untextured_disabled.webp
  37. :align: center
  38. :alt: Variable rate shading disabled in untextured scene
  39. Variable rate shading disabled in untextured scene
  40. .. figure:: img/variable_rate_shading_untextured_enabled.webp
  41. :align: center
  42. :alt: Variable rate shading enabled in untextured scene (lower quality, but higher performance)
  43. Variable rate shading enabled in untextured scene (lower quality, but higher performance)
  44. Hardware support
  45. ----------------
  46. Variable rate shading is only supported on specific GPUs:
  47. **Desktop:**
  48. - NVIDIA Turing and newer (including GTX 1600 series)
  49. - AMD RDNA2 and newer (both integrated and dedicated GPUs – including Steam Deck)
  50. - Intel Arc Alchemist and newer **(dedicated GPUs only)**
  51. - Intel integrated graphics do not support variable rate shading.
  52. **Mobile SoCs:**
  53. - Snapdragon 888 and newer
  54. - MediaTek Dimensity 9000 and newer
  55. - ARM Mali-G615 and newer
  56. As of January 2023, Apple and Raspberry Pi GPUs do not support variable rate shading.
  57. Using variable rate shading in Godot
  58. ------------------------------------
  59. .. note::
  60. Both Forward+ and Mobile renderers support variable rate
  61. shading. VRS can be used in both pancake (non-XR) and XR display modes.
  62. The Compatibility renderer does **not** support variable rate shading.
  63. In the advanced Project Settings, the **Rendering > VRS** section offers settings
  64. to control variable rate shading on the root viewport:
  65. - **Mode:** Controls the variable rate shading mode. **Disabled** disables
  66. variable rate shading. **Texture** uses a manually authored texture to set
  67. shading density (see the property below). **XR** automatically generates a
  68. texture suited for foveated rendering in virtual/augmented reality.
  69. - **Texture:** The texture to use to control shading density on the root
  70. viewport. Only used if **Mode** is **Texture**.
  71. For custom viewports, the VRS mode and texture must be set manually to the
  72. :ref:`class_Viewport` node.
  73. .. note::
  74. On unsupported hardware, there is no visual difference when variable rate
  75. shading is enabled. You can check whether hardware supports variable rate
  76. shading by running the editor or project with the ``--verbose``
  77. :ref:`command line argument <doc_command_line_tutorial>`.
  78. Creating a VRS density map
  79. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  80. If using the **Texture** VRS mode, you *must* set a texture to be used as a
  81. density map. Otherwise, no effect will be visible.
  82. You can create your own VRS density map manually using an image editor, or
  83. generate it using another method (e.g. on the CPU using the Image class, or on
  84. the GPU using a shader). However, beware of performance implications when
  85. generating a VRS image dynamically. If opting for dynamic generation, make sure
  86. the VRS image generation process is fast enough to avoid outweighing the
  87. performance gains from VRS.
  88. The texture must follow these rules:
  89. - The texture *must* use a lossless compression format so that colors can be
  90. matched precisely.
  91. - The following VRS densities are mapped to various colors, with brighter colors
  92. representing a lower level of shading precision:
  93. +----------------------+--------------------------------+---------------------------------+
  94. | Density | Color | Comment |
  95. +======================+================================+=================================+
  96. | 1×1 (highest detail) | ``rgb(0, 0, 0) - #000000`` | |
  97. +----------------------+--------------------------------+---------------------------------+
  98. | 1×2 | ``rgb(0, 85, 0) - #005500`` | |
  99. +----------------------+--------------------------------+---------------------------------+
  100. | 2×1 | ``rgb(85, 0, 0) - #550000`` | |
  101. +----------------------+--------------------------------+---------------------------------+
  102. | 2×2 | ``rgb(85, 85, 0) - #555500`` | |
  103. +----------------------+--------------------------------+---------------------------------+
  104. | 2×4 | ``rgb(85, 170, 0) - #55aa00`` | |
  105. +----------------------+--------------------------------+---------------------------------+
  106. | 4×2 | ``rgb(170, 85, 0) - #aa5500`` | |
  107. +----------------------+--------------------------------+---------------------------------+
  108. | 4×4 | ``rgb(170, 170, 0) - #aaaa00`` | |
  109. +----------------------+--------------------------------+---------------------------------+
  110. | 4×8 | ``rgb(170, 255, 0) - #aaff00`` | Not supported on most hardware. |
  111. +----------------------+--------------------------------+---------------------------------+
  112. | 8×4 | ``rgb(255, 170, 0) - #ffaa00`` | Not supported on most hardware. |
  113. +----------------------+--------------------------------+---------------------------------+
  114. | 8×8 (lowest detail) | ``rgb(255, 255, 0) - #ffff00`` | Not supported on most hardware. |
  115. +----------------------+--------------------------------+---------------------------------+
  116. For example, this VRS density texture provides the highest shading density in
  117. the center of the viewport, and the lowest shading density in the corners:
  118. .. figure:: img/variable_rate_shading_texture_example.webp
  119. :align: center
  120. :alt: Example VRS density map texture, simulating foveated rendering
  121. Example VRS density map texture, simulating foveated rendering
  122. There are no size or aspect ratio requirements for the VRS density texture.
  123. However, there is no benefit to using a VRS density map that is larger than the
  124. viewport resolution divided by the GPU's *tile size*. The tile size is what
  125. determines the smallest area of pixels where the shading density can be changed
  126. separately from other tiles. On most GPUs, this tile size is 8×8 pixels. You can
  127. view the tile size by running Godot with the ``--verbose`` command line
  128. argument, as it's printed in the VRS debugging information.
  129. Therefore, sticking to a relatively low resolution such as 256×256 (square) or
  130. 480×270 (16:9) is recommended. Depending on your use cases, a square texture may
  131. be more suited compared to a texture that matches the most common viewport
  132. aspect ratio in your project (such as 16:9).
  133. .. tip::
  134. When using variable rate shading, you can use a negative
  135. :ref:`texture mipmap LOD bias <doc_resolution_scaling_mipmap_bias>`
  136. to reduce blurriness in areas with reduced shading rate.
  137. Note that the texture LOD bias is set globally, so this will also affect
  138. areas of the viewport with full shading rate. Don't use values that are too
  139. low, or textures will appear grainy.
  140. Performance comparison
  141. ^^^^^^^^^^^^^^^^^^^^^^
  142. To give an idea of how much VRS can improve performance in theory, here's a
  143. performance comparison with the textured example scene shown at the top of this
  144. page. The VRS density map example present on this page is used.
  145. Results were captured on a GeForce RTX 4090 with the NVIDIA 525.60.11 driver.
  146. +---------------------+--------------+-------------+-------------------------+
  147. | Resolution | VRS disabled | VRS enabled | Performance improvement |
  148. +=====================+==============+=============+=========================+
  149. | 1920×1080 (Full HD) | 2832 FPS | 3136 FPS | +10.7% |
  150. +---------------------+--------------+-------------+-------------------------+
  151. | 2560×1440 (QHD) | 2008 FPS | 2256 FPS | +12.3% |
  152. +---------------------+--------------+-------------+-------------------------+
  153. | 3840×2160 (4K) | 1236 FPS | 1436 FPS | +16.2% |
  154. +---------------------+--------------+-------------+-------------------------+
  155. | 7680×4320 (8K) | 384 FPS | 473 FPS | +23.1% |
  156. +---------------------+--------------+-------------+-------------------------+
  157. In terms of performance improvements, variable rate shading is more beneficial
  158. at higher target resolutions. The reduction in visual quality is also less
  159. noticeable at high resolutions.
  160. .. note::
  161. For non-VR games, you will probably have to use a less aggressive VRS texture
  162. than what was used in this example. As a result, the effective performance
  163. gains will be lower.