screen-reading_shaders.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. .. _doc_screen-reading_shaders:
  2. Screen-reading shaders
  3. ======================
  4. Introduction
  5. ~~~~~~~~~~~~
  6. Very often it is desired to make a shader that reads from the same
  7. screen it's writing to. 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 sort 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 makes this process easy!
  15. SCREEN_TEXTURE built-in texture.
  16. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  17. Godot :ref:`doc_shading_language` has a special texture, "SCREEN_TEXTURE" (and "DEPTH_TEXTURE" for depth, in case of 3D).
  18. It takes as parameter the UV of the screen and returns a vec3 RGB with the color. A
  19. special built-in varying: SCREEN_UV can be used to obtain the UV for
  20. the current fragment. As a result, this simple 2D fragment shader:
  21. ::
  22. COLOR=textureLod( SCREEN_TEXTURE, SCREEN_UV, 0.0);
  23. results in an invisible object, because it just shows what lies behind.
  24. The reason why textureLod must be used is because, when Godot copies back
  25. a chunk of the screen, it also does an efficient separatable gaussian blur to it's mipmaps.
  26. This allows for not only reading from the screen, but reading from it with different amounts
  27. of blur at no cost.
  28. SCREEN_TEXTURE example
  29. ~~~~~~~~~~~~~~~~~~~~~~
  30. SCREEN_TEXTURE can be used for a lot of things. There is a
  31. special demo for *Screen Space Shaders*, that you can download to see
  32. and learn. One example is a simple shader to adjust brightness, contrast
  33. and saturation:
  34. ::
  35. shader_type canvas_item;
  36. uniform float brightness = 1.0;
  37. uniform float contrast = 1.0;
  38. uniform float saturation = 1.0;
  39. vec3 c = textureLod(SCREEN_TEXTURE, SCREEN_UV, 0.0).rgb;
  40. c.rgb = mix(vec3(0.0), c.rgb, brightness);
  41. c.rgb = mix(vec3(0.5), c.rgb, contrast);
  42. c.rgb = mix(vec3(dot(vec3(1.0), c.rgb)*0.33333), c.rgb, saturation);
  43. COLOR.rgb = c;
  44. Behind the scenes
  45. ~~~~~~~~~~~~~~~~~
  46. While this seems magical, it's not. The SCREEN_TEXTURE built-in, when
  47. first found in a node that is about to be drawn, does a full-screen
  48. copy to a back-buffer. Subsequent nodes that use it in
  49. shaders will not have the screen copied for them, because this ends up
  50. being very inefficient.
  51. As a result, if shaders that use SCREEN_TEXTURE overlap, the second one
  52. will not use the result of the first one, resulting in unexpected
  53. visuals:
  54. .. image:: img/texscreen_demo1.png
  55. In the above image, the second sphere (top right) is using the same
  56. source for SCREEN_TEXTURE as the first one below, so the first one
  57. "disappears", or is not visible.
  58. In 3D, this is unavoidable because copying happens when opaque rendering
  59. completes.
  60. In 2D this can be corrected via the :ref:`BackBufferCopy <class_BackBufferCopy>`
  61. node, which can be instantiated between both spheres. BackBufferCopy can work by
  62. either specifying a screen region or the whole screen:
  63. .. image:: img/texscreen_bbc.png
  64. With correct back-buffer copying, the two spheres blend correctly:
  65. .. image:: img/texscreen_demo2.png
  66. Back-buffer logic
  67. ~~~~~~~~~~~~~~~~~
  68. So, to make it clearer, here's how the backbuffer copying logic works in
  69. Godot:
  70. - If a node uses the SCREEN_TEXTURE, the entire screen is copied to the
  71. back buffer before drawing that node. This only happens the first
  72. time, subsequent nodes do not trigger this.
  73. - If a BackBufferCopy node was processed before the situation in the
  74. point above (even if SCREEN_TEXTURE was not used), this behavior
  75. described in the point above does not happen. In other words,
  76. automatic copying of the entire screen only happens if SCREEN_TEXTURE is
  77. used in a node for the first time and no BackBufferCopy node (not
  78. disabled) was found before in tree-order.
  79. - BackBufferCopy can copy either the entire screen or a region. If set
  80. to only a region (not the whole screen) and your shader uses pixels
  81. not in the region copied, the result of that read is undefined
  82. (most likely garbage from previous frames). In other words, it's
  83. possible to use BackBufferCopy to copy back a region of the screen
  84. and then use SCREEN_TEXTURE on a different region. Avoid this behavior!
  85. DEPTH_TEXTURE
  86. ~~~~~~~~~~~~~
  87. For 3D Shaders, it's also possible to access the screen depth buffer. For this,
  88. the DEPTH_TEXTURE built-in is used. This texture is not linear, it must be
  89. converted via the inverse projection matrix.
  90. The following code retrieves the 3D position below the pixel being drawn:
  91. ::
  92. float depth = textureLod(DEPTH_TEXTURE,SCREEN_UV,0.0).r;
  93. vec4 upos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV*2.0-1.0,depth*2.0-1.0,1.0);
  94. vec3 pixel_position = upos.xyz/upos.w;