screen-reading_shaders.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. .. _doc_screen-reading_shaders:
  2. Screen-reading shaders
  3. ======================
  4. Introduction
  5. ~~~~~~~~~~~~
  6. It is often desired to make a shader that reads from the same
  7. screen to which it's writing. 3D APIs, such as OpenGL or DirectX, make this very
  8. difficult because of internal hardware limitations. GPUs are extremely
  9. parallel, so reading and writing causes all sorts of cache and coherency
  10. problems. As a result, not even the most modern hardware supports this
  11. properly.
  12. The workaround is to make a copy of the screen, or a part of the screen,
  13. to a back-buffer and then read from it while drawing. Godot provides a
  14. few tools that make this process easy.
  15. Screen texture
  16. ~~~~~~~~~~~~~~
  17. Godot :ref:`doc_shading_language` has a special texture to access the already
  18. rendered contents of the screen. It is used by specifying a hint when declaring
  19. a ``sampler2D`` uniform: ``hint_screen_texture``. A special built-in varying
  20. ``SCREEN_UV`` can be used to obtain the UV relative to the screen for the current
  21. fragment. As a result, this canvas_item fragment shader results in an invisible
  22. object, because it only shows what lies behind:
  23. .. code-block:: glsl
  24. shader_type canvas_item;
  25. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  26. void fragment() {
  27. COLOR = textureLod(screen_texture, SCREEN_UV, 0.0);
  28. }
  29. ``textureLod`` is used here as we only want to read from the bottom mipmap. If
  30. you want to read from a blurred version of the texture instead, you can increase
  31. the third argument to ``textureLod`` and change the hint ``filter_nearest`` to
  32. ``filter_nearest_mipmap`` (or any other filter with mipmaps enabled). If using a
  33. filter with mipmaps, Godot will automatically calculate the blurred texture for
  34. you.
  35. Screen texture example
  36. ~~~~~~~~~~~~~~~~~~~~~~
  37. The screen texture can be used for many things. There is a
  38. special demo for *Screen Space Shaders*, that you can download to see
  39. and learn. One example is a simple shader to adjust brightness, contrast
  40. and saturation:
  41. .. code-block:: glsl
  42. shader_type canvas_item;
  43. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  44. uniform float brightness = 1.0;
  45. uniform float contrast = 1.0;
  46. uniform float saturation = 1.0;
  47. void fragment() {
  48. vec3 c = textureLod(screen_texture, SCREEN_UV, 0.0).rgb;
  49. c.rgb = mix(vec3(0.0), c.rgb, brightness);
  50. c.rgb = mix(vec3(0.5), c.rgb, contrast);
  51. c.rgb = mix(vec3(dot(vec3(1.0), c.rgb) * 0.33333), c.rgb, saturation);
  52. COLOR.rgb = c;
  53. }
  54. Behind the scenes
  55. ~~~~~~~~~~~~~~~~~
  56. While this seems magical, it's not. In 2D, when ``hint_screen_texture`` is first
  57. found in a node that is about to be drawn, Godot does a full-screen copy to a
  58. back-buffer. Subsequent nodes that use it in shaders will not have the screen
  59. copied for them, because this ends up being inefficient. In 3D, the screen is
  60. copied after the opaque geometry pass, but before the transparent geometry pass,
  61. so transparent objects will not be captured in the screen texture.
  62. As a result, in 2D, if shaders that use ``hint_screen_texture`` overlap, the
  63. second one will not use the result of the first one, resulting in unexpected
  64. visuals:
  65. .. image:: img/texscreen_demo1.png
  66. In the above image, the second sphere (top right) is using the same source for
  67. the screen texture as the first one below, so the first one "disappears", or is
  68. not visible.
  69. In 2D, this can be corrected via the :ref:`BackBufferCopy <class_BackBufferCopy>`
  70. node, which can be instantiated between both spheres. BackBufferCopy can work by
  71. either specifying a screen region or the whole screen:
  72. .. image:: img/texscreen_bbc.png
  73. With correct back-buffer copying, the two spheres blend correctly:
  74. .. image:: img/texscreen_demo2.png
  75. .. warning:
  76. In 3D, materials that use ``hint_screen_texture`` are considered transparent themselves and
  77. will not appear in the resulting screen texture of other materials.
  78. If you plan to instance a scene that uses a material with ``hint_screen_texture``,
  79. you will need to use a BackBufferCopy node.
  80. In 3D, there is less flexibility to solve this particular issue because the
  81. screen texture is only captured once. Be careful when using the screen texture
  82. in 3D as it won't capture transparent objects and may capture some opaque
  83. objects that are in front of the object using the screen texture.
  84. You can reproduce the back-buffer logic in 3D by creating a :ref:`Viewport <class_Viewport>`
  85. with a camera in the same position as your object, and then use the
  86. :ref:`Viewport's <class_Viewport>` texture instead of the screen texture.
  87. Back-buffer logic
  88. ~~~~~~~~~~~~~~~~~
  89. So, to make it clearer, here's how the backbuffer copying logic works in 2D in
  90. Godot:
  91. - If a node uses ``hint_screen_texture``, the entire screen is copied to the
  92. back buffer before drawing that node. This only happens the first
  93. time; subsequent nodes do not trigger this.
  94. - If a BackBufferCopy node was processed before the situation in the point
  95. above (even if ``hint_screen_texture`` was not used), the behavior described
  96. in the point above does not happen. In other words, automatic copying of the
  97. entire screen only happens if ``hint_screen_texture`` is used in a node for
  98. the first time and no BackBufferCopy node (not disabled) was found before in
  99. tree-order.
  100. - BackBufferCopy can copy either the entire screen or a region. If set to only
  101. a region (not the whole screen) and your shader uses pixels not in the region
  102. copied, the result of that read is undefined (most likely garbage from
  103. previous frames). In other words, it's possible to use BackBufferCopy to copy
  104. back a region of the screen and then read the screen texture from a different
  105. region. Avoid this behavior!
  106. Depth texture
  107. ~~~~~~~~~~~~~
  108. For 3D shaders, it's also possible to access the screen depth buffer. For this,
  109. the ``hint_depth_texture`` hint is used. This texture is not linear; it must be
  110. converted using the inverse projection matrix.
  111. The following code retrieves the 3D position below the pixel being drawn:
  112. .. code-block:: glsl
  113. uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;
  114. void fragment() {
  115. float depth = textureLod(depth_texture, SCREEN_UV, 0.0).r;
  116. vec4 upos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, depth, 1.0);
  117. vec3 pixel_position = upos.xyz / upos.w;
  118. }
  119. Normal-roughness texture
  120. ~~~~~~~~~~~~~~~~~~~~~~~~
  121. Similarly, the normal-roughness texture can be used to read the normals and
  122. roughness of objects rendered in the depth prepass. The normal is stored in the
  123. ``.xyz`` channels (mapped to the 0-1 range) while the roughness is stored in the
  124. ``.w`` channel.
  125. .. code-block:: glsl
  126. uniform sampler2D normal_roughness_texture : hint_normal_roughness_texture, repeat_disable, filter_nearest;
  127. void fragment() {
  128. float screen_roughness = texture(normal_roughness_texture, SCREEN_UV).w;
  129. vec3 screen_normal = texture(normal_roughness_texture, SCREEN_UV).xyz;
  130. screen_normal = screen_normal * 2.0 - 1.0;
  131. Redefining screen textures
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. The screen texture hints (``hint_screen_texture``, ``hint_depth_texture``, and
  134. ``hint_normal_roughness_texture``) can be used with multiple uniforms. For
  135. example, you may want to read from the texture multiple times with a different
  136. repeat flag or filter flag.
  137. The following example shows a shader that reads the screen space normal with
  138. linear filtering, but reads the screen space roughness using nearest neighbor
  139. filtering.
  140. .. code-block:: glsl
  141. uniform sampler2D normal_roughness_texture : hint_normal_roughness_texture, repeat_disable, filter_nearest;
  142. uniform sampler2D normal_roughness_texture2 : hint_normal_roughness_texture, repeat_enable, filter_linear;
  143. void fragment() {
  144. float screen_roughness = texture(normal_roughness_texture, SCREEN_UV).w;
  145. vec3 screen_normal = texture(normal_roughness_texture2, SCREEN_UV).xyz;
  146. screen_normal = screen_normal * 2.0 - 1.0;