what_are_shaders.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. .. _doc_what_are_shaders:
  2. What are shaders?
  3. =================
  4. Introduction
  5. ------------
  6. So, you have decided to give shaders a try. You have likely heard that they can be used to
  7. create interesting effects that run incredibly fast. You have also likely heard that they
  8. are terrifying. Both are true.
  9. Shaders can be used to create a wide range of effects (in fact everything drawn in a modern
  10. rendering engine is done with shaders).
  11. Writing shaders can also be very difficult for people unfamiliar with them. Godot tries to make writing
  12. shaders a little easier by exposing many useful built-in features and handling some of the
  13. lower-level initialization work for you. However, GLSL (the OpenGL Shading Language, which Godot uses)
  14. is still unintuitive and restricting, especially for users who are used to GDScript.
  15. But what are they?
  16. ------------------
  17. Shaders are a special kind of program that runs on Graphics Processing Units (GPUs). Most computers
  18. have some sort of GPU, either one integrated into their CPU or discrete (meaning it is a separate
  19. hardware component, for example, the typical graphics card). GPUs are especially useful for
  20. rendering because they are optimized for running thousands of instructions in parallel.
  21. The output of the shader is typically the colored pixels of the object drawn to the viewport. But some
  22. shaders allow for specialized outputs (this is especially true for APIs like Vulkan). Shaders operate
  23. inside the shader pipeline. The standard process is the vertex -> fragment shader pipeline. The vertex
  24. shader is used to decided where each vertex (point in a 3D model, or corner of a Sprite) goes and the
  25. fragment shader decides what color individual pixels receive.
  26. Suppose you want to update all the pixels in a texture to a given color, on the CPU you would write:
  27. ::
  28. for x in range(width):
  29. for y in range(height):
  30. set_color(x, y, some_color)
  31. In a shader you are given access only to the inside of the loop so what you write looks like this:
  32. .. code-block:: glsl
  33. // function called for each pixel
  34. void fragment() {
  35. COLOR = some_color;
  36. }
  37. You have no control over how this function is called. So you have to design your shaders
  38. differently from how you would design programs on the CPU.
  39. A consequence of the shader pipeline is that you cannot access the results from a previous
  40. run of the shader, you cannot access other pixels from the pixel being drawn, and you cannot
  41. write outside of the current pixel being drawn. This enables the GPU to execute the shader
  42. for different pixels in parallel, as they do not depend on each other. This lack of
  43. flexibility is designed to work with the GPU which allows shaders to be incredibly fast.
  44. What can they do
  45. ^^^^^^^^^^^^^^^^
  46. - position vertices very fast
  47. - compute color very fast
  48. - compute lighting very fast
  49. - lots and lots of math
  50. What can't they do
  51. ^^^^^^^^^^^^^^^^^^
  52. - draw outside mesh
  53. - access other pixels from current pixel (or vertices)
  54. - store previous iterations
  55. - update on the fly (they can, but they need to be compiled)
  56. Structure of a shader
  57. ---------------------
  58. In Godot, shaders are made up of 3 main functions: the ``vertex()`` function, the ``fragment()``
  59. function and the ``light()`` function.
  60. The ``vertex()`` function runs over all the vertices in the mesh and sets their positions as well
  61. as some other per-vertex variables.
  62. The ``fragment()`` function runs for every pixel that is covered by the mesh. It uses the variables
  63. from the ``vertex()`` function to run. The variables from the ``vertex()`` function are interpolated
  64. between the vertices to provide the values for the ``fragment()`` function.
  65. The ``light()`` function runs for every pixel and for every light. It takes variables from the
  66. ``fragment()`` function and from previous runs of itself.
  67. For more information about how shaders operate specifically in Godot see the :ref:`Shaders <doc_shaders>` doc.
  68. Technical overview
  69. ------------------
  70. GPUs are able to render graphics much faster than CPUs for a few reasons, but most notably,
  71. because they are able to run calculations massively in parallel. A CPU typically has 4 or 8 cores
  72. while a GPU typically has thousands. That means a GPU can do hundreds of tasks at once. GPU architects
  73. have exploited this in a way that allows for doing many calculations very quickly, but only when
  74. many or all cores are doing the same calculation at once, but with different data.
  75. That is where shaders come in. The GPU will call the shader a bunch of times simultaneously, and then
  76. operate on different bits of data (vertices, or pixels). These bunches of data are often called wavefronts.
  77. A shader will run the same for every thread in the wavefront. For example, if a given GPU can handle 100
  78. threads per wavefront, a wavefront will run on a 10x10 block of pixels together. And it will continue to
  79. run for all pixels in that wavefront until they are complete. Accordingly, if you have one pixel slower
  80. than the rest (due to excessive branching), the entire block will be slowed down, resulting in massively
  81. slower render times. This is different than CPU based operations, on a CPU if you can speed up even one
  82. pixel the entire rendering time will decrease. On a GPU, you have to speed up the entire wavefront
  83. to speed up rendering.