screen-reading_shaders.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. TexScreen shader instruction
  16. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  17. Godot :ref:`doc_shading_language` has a special instruction, "texscreen", it takes as
  18. 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=vec4( texscreen(SCREEN_UV), 1.0 );
  23. results in an invisible object, because it just shows what lies behind.
  24. The same shader using the visual editor looks like this:
  25. .. image:: /img/texscreen_visual_shader.png
  26. TexScreen example
  27. ~~~~~~~~~~~~~~~~~
  28. Texscreen instruction can be used for a lot of things. There is a
  29. special demo for *Screen Space Shaders*, that you can download to see
  30. and learn. One example is a simple shader to adjust brightness, contrast
  31. and saturation:
  32. ::
  33. uniform float brightness = 1.0;
  34. uniform float contrast = 1.0;
  35. uniform float saturation = 1.0;
  36. vec3 c = texscreen(SCREEN_UV);
  37. c.rgb = mix(vec3(0.0), c.rgb, brightness);
  38. c.rgb = mix(vec3(0.5), c.rgb, contrast);
  39. c.rgb = mix(vec3(dot(vec3(1.0), c.rgb)*0.33333), c.rgb, saturation);
  40. COLOR.rgb = c;
  41. Behind the scenes
  42. ~~~~~~~~~~~~~~~~~
  43. While this seems magical, it's not. The Texscreen instruction, when
  44. first found in a node that is about to be drawn, does a full-screen
  45. copy to a back-buffer. Subsequent nodes that use texscreen() in
  46. shaders will not have the screen copied for them, because this ends up
  47. being very inefficient.
  48. As a result, if shaders that use texscreen() overlap, the second one
  49. will not use the result of the first one, resulting in unexpected
  50. visuals:
  51. .. image:: /img/texscreen_demo1.png
  52. In the above image, the second sphere (top right) is using the same
  53. source for texscreen() as the first one below, so the first one
  54. "disappears", or is not visible.
  55. To correct this, a
  56. :ref:`BackBufferCopy <class_BackBufferCopy>`
  57. node can be instanced between both spheres. BackBufferCopy can work by
  58. either specifying a screen region or the whole screen:
  59. .. image:: /img/texscreen_bbc.png
  60. With correct back-buffer copying, the two spheres blend correctly:
  61. .. image:: /img/texscreen_demo2.png
  62. Back-buffer logic
  63. ~~~~~~~~~~~~~~~~~
  64. So, to make it clearer, here's how the backbuffer copying logic works in
  65. Godot:
  66. - If a node uses the texscreen(), the entire screen is copied to the
  67. back buffer before drawing that node. This only happens the first
  68. time, subsequent nodes do not trigger this.
  69. - If a BackBufferCopy node was processed before the situation in the
  70. point above (even if texscreen() was not used), this behavior
  71. described in the point above does not happen. In other words,
  72. automatic copying of the entire screen only happens if texscreen() is
  73. used in a node for the first time and no BackBufferCopy node (not
  74. disabled) was found before in tree-order.
  75. - BackBufferCopy can copy either the entire screen or a region. If set
  76. to only a region (not the whole screen) and your shader uses pixels
  77. not in the region copied, the result of that read is undefined
  78. (most likely garbage from previous frames). In other words, it's
  79. possible to use BackBufferCopy to copy back a region of the screen
  80. and then use texscreen() on a different region. Avoid this behavior!