converting_glsl_to_godot_shaders.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. .. _doc_converting_glsl_to_godot_shaders:
  2. Converting GLSL to Godot shaders
  3. ================================
  4. This document explains the differences between Godot's shading language and GLSL
  5. and gives practical advice on how to migrate shaders from other sources, such as
  6. Shadertoy and The Book of Shaders, into Godot shaders.
  7. For detailed information on Godot's shading language, please refer to the
  8. :ref:`Shading Language <doc_shading_language>` reference.
  9. GLSL
  10. ----
  11. Godot uses a shading language based on GLSL with the addition of a few
  12. quality-of-life features. Accordingly, most features available in GLSL are
  13. available in Godot's shading language.
  14. Shader programs
  15. ^^^^^^^^^^^^^^^
  16. In GLSL, each shader uses a separate program. You have one program for the
  17. vertex shader and one for the fragment shader. In Godot, you have a single
  18. shader that contains a ``vertex`` and/or a ``fragment`` function. If you only
  19. choose to write one, Godot will supply the other.
  20. Godot allows uniform variables and functions to be shared by defining the
  21. fragment and vertex shaders in one file. In GLSL, the vertex and fragment
  22. programs cannot share variables except when varyings are used.
  23. Vertex attributes
  24. ^^^^^^^^^^^^^^^^^
  25. In GLSL, you can pass in per-vertex information using attributes and have the
  26. flexibility to pass in as much or as little as you want. In Godot, you have a
  27. set number of input attributes, including ``VERTEX`` (position), ``COLOR``,
  28. ``UV``, ``UV2``, ``NORMAL``. Each shaders' page in the shader reference section
  29. of the documentation comes with a complete list of its vertex attributes.
  30. gl_Position
  31. ^^^^^^^^^^^
  32. ``gl_Position`` receives the final position of a vertex specified in the vertex
  33. shader. It is specified by the user in clip space. Typically, in GLSL, the model
  34. space vertex position is passed in using a vertex attribute called ``position``
  35. and you handle the conversion from model space to clip space manually.
  36. In Godot, ``VERTEX`` specifies the vertex position in model space at the
  37. beginning of the ``vertex`` function. Godot also handles the final conversion to
  38. clip space after the user-defined ``vertex`` function is run. If you want to
  39. skip the conversion from model to view space, you can set the ``render_mode`` to
  40. ``skip_vertex_transform``. If you want to skip all transforms, set
  41. ``render_mode`` to ``skip_vertex_transform`` and set the ``PROJECTION_MATRIX``
  42. to ``mat4(1.0)`` in order to nullify the final transform from view space to clip
  43. space.
  44. Varyings
  45. ^^^^^^^^
  46. Varyings are a type of variable that can be passed from the vertex shader to the
  47. fragment shader. In modern GLSL (3.0 and up), varyings are defined with the
  48. ``in`` and ``out`` keywords. A variable going out of the vertex shader is
  49. defined with ``out`` in the vertex shader and ``in`` inside the fragment shader.
  50. Main
  51. ^^^^
  52. In GLSL, each shader program looks like a self-contained C-style program.
  53. Accordingly, the main entry point is ``main``. If you are copying a vertex
  54. shader, rename ``main`` to ``vertex`` and if you are copying a fragment shader,
  55. rename ``main`` to ``fragment``.
  56. Macros
  57. ^^^^^^
  58. The :ref:`Godot shader preprocessor<doc_shader_preprocessor>` supports the following macros:
  59. * ``#define`` / ``#undef``
  60. * ``#if``, ``#elif``, ``#else``, ``#endif``, ``defined()``, ``#ifdef``, ``#ifndef``
  61. * ``#include`` (only ``.gdshaderinc`` files and with a maximum depth of 25)
  62. * ``#pragma disable_preprocessor``, which disables preprocessing for the rest of the file
  63. Variables
  64. ^^^^^^^^^
  65. GLSL has many built-in variables that are hard-coded. These variables are not
  66. uniforms, so they are not editable from the main program.
  67. +---------------------+---------+------------------------+-----------------------------------------------------+
  68. |Variable |Type |Equivalent |Description |
  69. +=====================+=========+========================+=====================================================+
  70. |gl_FragColor |out vec4 |COLOR |Output color for each pixel. |
  71. +---------------------+---------+------------------------+-----------------------------------------------------+
  72. |gl_FragCoord |vec4 |FRAGCOORD |For full screen quads. For smaller quads, use UV. |
  73. +---------------------+---------+------------------------+-----------------------------------------------------+
  74. |gl_Position |vec4 |VERTEX |Position of Vertex, output from Vertex Shader. |
  75. +---------------------+---------+------------------------+-----------------------------------------------------+
  76. |gl_PointSize |float |POINT_SIZE |Size of Point primitive. |
  77. +---------------------+---------+------------------------+-----------------------------------------------------+
  78. |gl_PointCoord |vec2 |POINT_COORD |Position on point when drawing Point primitives. |
  79. +---------------------+---------+------------------------+-----------------------------------------------------+
  80. |gl_FrontFacing |bool |FRONT_FACING |True if front face of primitive. |
  81. +---------------------+---------+------------------------+-----------------------------------------------------+
  82. .. _glsl_coordinates:
  83. Coordinates
  84. ^^^^^^^^^^^
  85. ``gl_FragCoord`` in GLSL and ``FRAGCOORD`` in the Godot shading language use the
  86. same coordinate system. If using UV in Godot, the y-coordinate will be flipped
  87. upside down.
  88. Precision
  89. ^^^^^^^^^
  90. In GLSL, you can define the precision of a given type (float or int) at the top
  91. of the shader with the ``precision`` keyword. In Godot, you can set the
  92. precision of individual variables as you need by placing precision qualifiers
  93. ``lowp``, ``mediump``, and ``highp`` before the type when defining the variable.
  94. For more information, see the :ref:`Shading Language <doc_shading_language>`
  95. reference.
  96. Shadertoy
  97. ---------
  98. `Shadertoy <https://www.shadertoy.com/results?query=&sort=popular&from=10&num=4>`_
  99. is a website that makes it easy to write fragment shaders and
  100. create `pure magic <https://www.shadertoy.com/view/4tjGRh>`_.
  101. Shadertoy does not give the user full control over the shader. It handles all
  102. the input and uniforms and only lets the user write the fragment shader.
  103. Types
  104. ^^^^^
  105. Shadertoy uses the webgl spec, so it runs a slightly different version of GLSL.
  106. However, it still has the regular types, including constants and macros.
  107. mainImage
  108. ^^^^^^^^^
  109. The main point of entry to a Shadertoy shader is the ``mainImage`` function.
  110. ``mainImage`` has two parameters, ``fragColor`` and ``fragCoord``, which
  111. correspond to ``COLOR`` and ``FRAGCOORD`` in Godot, respectively. These
  112. parameters are handled automatically in Godot, so you do not need to include
  113. them as parameters yourself. Anything in the ``mainImage`` function should be
  114. copied into the ``fragment`` function when porting to Godot.
  115. Variables
  116. ^^^^^^^^^
  117. In order to make writing fragment shaders straightforward and easy, Shadertoy
  118. handles passing a lot of helpful information from the main program into the
  119. fragment shader for you. A few of these have no equivalents in Godot because
  120. Godot has chosen not to make them available by default. This is okay because
  121. Godot gives you the ability to make your own uniforms. For variables whose
  122. equivalents are listed as "Provide with Uniform", users are responsible for
  123. creating that uniform themselves. The description gives the reader a hint about
  124. what they can pass in as a substitute.
  125. +---------------------+---------+------------------------+-----------------------------------------------------+
  126. |Variable |Type |Equivalent |Description |
  127. +=====================+=========+========================+=====================================================+
  128. |fragColor |out vec4 |COLOR |Output color for each pixel. |
  129. +---------------------+---------+------------------------+-----------------------------------------------------+
  130. |fragCoord |vec2 |FRAGCOORD.xy |For full screen quads. For smaller quads, use UV. |
  131. +---------------------+---------+------------------------+-----------------------------------------------------+
  132. |iResolution |vec3 |1.0 / SCREEN_PIXEL_SIZE |Can also pass in manually. |
  133. +---------------------+---------+------------------------+-----------------------------------------------------+
  134. |iTime |float |TIME |Time since shader started. |
  135. +---------------------+---------+------------------------+-----------------------------------------------------+
  136. |iTimeDelta |float |Provide with Uniform |Time to render previous frame. |
  137. +---------------------+---------+------------------------+-----------------------------------------------------+
  138. |iFrame |float |Provide with Uniform |Frame number. |
  139. +---------------------+---------+------------------------+-----------------------------------------------------+
  140. |iChannelTime[4] |float |Provide with Uniform |Time since that particular texture started. |
  141. +---------------------+---------+------------------------+-----------------------------------------------------+
  142. |iMouse |vec4 |Provide with Uniform |Mouse position in pixel coordinates. |
  143. +---------------------+---------+------------------------+-----------------------------------------------------+
  144. |iDate |vec4 |Provide with Uniform |Current date, expressed in seconds. |
  145. +---------------------+---------+------------------------+-----------------------------------------------------+
  146. |iChannelResolution[4]|vec3 |1.0 / TEXTURE_PIXEL_SIZE|Resolution of particular texture. |
  147. +---------------------+---------+------------------------+-----------------------------------------------------+
  148. |iChanneli |Sampler2D|TEXTURE |Godot provides only one built-in; user can make more.|
  149. +---------------------+---------+------------------------+-----------------------------------------------------+
  150. Coordinates
  151. ^^^^^^^^^^^
  152. ``fragCoord`` behaves the same as ``gl_FragCoord`` in :ref:`GLSL
  153. <glsl_coordinates>` and ``FRAGCOORD`` in Godot.
  154. The Book of Shaders
  155. -------------------
  156. Similar to Shadertoy, `The Book of Shaders <https://thebookofshaders.com>`_
  157. provides access to a fragment shader in the web browser, with which the user may
  158. interact. The user is restricted to writing fragment shader code with a set list
  159. of uniforms passed in and with no ability to add additional uniforms.
  160. For further help on porting shaders to various frameworks generally, The Book of
  161. Shaders provides a `page <https://thebookofshaders.com/04>`_ on running shaders
  162. in various frameworks.
  163. Types
  164. ^^^^^
  165. The Book of Shaders uses the webgl spec, so it runs a slightly different version
  166. of GLSL. However, it still has the regular types, including constants and
  167. macros.
  168. Main
  169. ^^^^
  170. The entry point for a Book of Shaders fragment shader is ``main``, just like in
  171. GLSL. Everything written in a Book of Shaders ``main`` function should be copied
  172. into Godot's ``fragment`` function.
  173. Variables
  174. ^^^^^^^^^
  175. The Book of Shaders sticks closer to plain GLSL than Shadertoy does. It also
  176. implements fewer uniforms than Shadertoy.
  177. +---------------------+---------+------------------------+-----------------------------------------------------+
  178. |Variable |Type |Equivalent |Description |
  179. +=====================+=========+========================+=====================================================+
  180. |gl_FragColor |out vec4 |COLOR |Output color for each pixel. |
  181. +---------------------+---------+------------------------+-----------------------------------------------------+
  182. |gl_FragCoord |vec4 |FRAGCOORD |For full screen quads. For smaller quads, use UV. |
  183. +---------------------+---------+------------------------+-----------------------------------------------------+
  184. |u_resolution |vec2 |1.0 / SCREEN_PIXEL_SIZE |Can also pass in manually. |
  185. +---------------------+---------+------------------------+-----------------------------------------------------+
  186. |u_time |float |TIME |Time since shader started. |
  187. +---------------------+---------+------------------------+-----------------------------------------------------+
  188. |u_mouse |vec2 |Provide with Uniform |Mouse position in pixel coordinates. |
  189. +---------------------+---------+------------------------+-----------------------------------------------------+
  190. Coordinates
  191. ^^^^^^^^^^^
  192. The Book of Shaders uses the same coordinate system as
  193. :ref:`GLSL <glsl_coordinates>`.