migrating_to_godot_shader_language.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. .. _doc_migrating_to_godot_shader_language:
  2. Migrating to Godot shading language
  3. ===================================
  4. Introduction
  5. ------------
  6. This document explains the differences between Godot's shading language
  7. and GLSL and gives practical advice on how to migrate shaders from other
  8. sources, such as Shadertoy and The Book of Shaders, into Godot shaders.
  9. For detailed information on Godot's shading language please refer to the :ref:`Shading Language <doc_shading_language>`
  10. reference.
  11. GLSL
  12. ----
  13. Godot uses a shading language based on GLSL with the addition of a few quality-of-life features.
  14. Accordingly, most features available in GLSL are available in Godot's shading language.
  15. Shader Programs
  16. ^^^^^^^^^^^^^^^
  17. In GLSL each shader uses a separate program. You have one program for the vertex shader and one
  18. for the fragment shader. In Godot you have a single shader that contains a ``vertex`` and/or a
  19. ``fragment`` function. If you only choose to write one, Godot will supply the other.
  20. Godot allows uniform variables and functions to be shared by defining the fragment and vertex
  21. shaders in one file. In GLSL the vertex and fragment programs cannot share variables except
  22. when varyings are used.
  23. Varyings
  24. ^^^^^^^^
  25. Varyings are a type of variable that can be passed from the vertex shader to the fragment shader. In
  26. modern GLSL (3.0 and up) varyings are defined with the ``in`` and ``out`` keywords. A variable going
  27. out of the vertex shader is defined with ``out`` in the vertex shader and ``in`` inside the fragment shader.
  28. Main
  29. ^^^^
  30. In GLSL each shader program looks like a self-contained C-style program. Accordingly, the main entry point
  31. is ``main``. If you are copying a vertex shader, rename ``main`` to ``vertex`` and if you are copying a
  32. fragment shader, rename ``main`` to ``fragment``.
  33. Constants
  34. ^^^^^^^^^
  35. Godot currently does not support constants. You can fake the functionality by using a uniform initialized
  36. to the value, but you will not benefit from the increased speed from using a constant.
  37. Macros
  38. ^^^^^^
  39. In keeping with its similarity to C, GLSL lets you use macros. Commonly ``#define`` is used to define
  40. constants or small functions. There is no straightforward way to translate defines to Godot's shading language.
  41. If it is a function that is defined, then replace with a function, and if it is a constant then replace with
  42. a uniform. For other macros (``#if``, ``#ifdef``, etc.) there is no equivalent because they run during the
  43. pre-processing stage of compilation.
  44. Variables
  45. ^^^^^^^^^
  46. GLSL has many built in variables that are hard-coded. These variables are not uniforms, so they
  47. are not editable from the main program.
  48. +---------------------+---------+------------------------+-----------------------------------------------------+
  49. |Variable |Type |Equivalent |Description |
  50. +=====================+=========+========================+=====================================================+
  51. |gl_FragColor |out vec4 |COLOR |Output color for each pixel. |
  52. +---------------------+---------+------------------------+-----------------------------------------------------+
  53. |gl_FragCoord |vec4 |FRAGCOORD |For full screen quads. For smaller quads use UV. |
  54. +---------------------+---------+------------------------+-----------------------------------------------------+
  55. |gl_Position |vec4 |VERTEX |Position of Vertex, output from Vertex Shader. |
  56. +---------------------+---------+------------------------+-----------------------------------------------------+
  57. |gl_PointSize |float |POINT_SIZE |Size of Point primitive. |
  58. +---------------------+---------+------------------------+-----------------------------------------------------+
  59. |gl_PointCoord |vec2 |POINT_COORD |Position on point when drawing Point primitives. |
  60. +---------------------+---------+------------------------+-----------------------------------------------------+
  61. |gl_FrontFacing |bool |FRONT_FACING |True if front face of primitive. |
  62. +---------------------+---------+------------------------+-----------------------------------------------------+
  63. .. _glsl_coordinates:
  64. Coordinates
  65. ^^^^^^^^^^^
  66. ``gl_FragCoord`` in GLSL and ``FRAGCOORD`` in the Godot shading language use the same coordinate system.
  67. If using UV in Godot, the y-coordinate will be flipped upside down.
  68. Precision
  69. ^^^^^^^^^
  70. In GLSL you can define the precision of a given type (float or int) at the top of the shader with the
  71. ``precision`` keyword. In Godot you can set the precision of individual variables as you need by placing
  72. precision qualifiers ``lowp``, ``mediump``, and ``highp`` before the type when defining the variable. For
  73. more information see the :ref:`Shading Language <doc_shading_language>` reference.
  74. Shadertoy
  75. ---------
  76. `Shadertoy <https://www.shadertoy.com>`_ is a website that makes it easy to write fragment shaders and
  77. create `pure magic <https://www.shadertoy.com/view/4tjGRh>`_.
  78. Shadertoy does not give the user full control over the shader. It only allows the user to write a
  79. fragment shader. It handles all the input and uniforms and only lets the user write the fragment
  80. shader.
  81. Types
  82. ^^^^^
  83. Shadertoy uses the webgl spec so it runs a slightly different version of GLSL. However, it still
  84. has the regular types, including `Constants`_ and macros.
  85. mainImage
  86. ^^^^^^^^^
  87. The main point of entry to a Shadertoy shader is the ``mainImage`` function. ``mainImage`` has two
  88. parameters, ``fragColor`` and ``fragCoord`` which correspond to ``COLOR`` and ``FRAGCOORD`` in Godot
  89. respectively. These parameters are handled automatically in Godot, so you do not need to include them
  90. as parameters yourself. Anything in the ``mainImage`` function should be copied into the ``fragment``
  91. function when porting to Godot.
  92. Variables
  93. ^^^^^^^^^
  94. In order to make writing fragment shaders straightforward and easy, Shadertoy handles passing a lot
  95. of helpful information from the main program into the fragment shader for you. A few of these
  96. have no equivalents in Godot becuase Godot has chosen not to make them available by default.
  97. This is okay becuase Godot gives you the ability to make your own uniforms. For variables whose
  98. equivalents are listed as "Provide with Uniform", the user is responsible for creating that
  99. uniform themself. The description gives the reader a hint about what they can pass in as a substitute.
  100. +---------------------+---------+------------------------+-----------------------------------------------------+
  101. |Variable |Type |Equivalent |Description |
  102. +=====================+=========+========================+=====================================================+
  103. |fragColor |out vec4 |COLOR |Output color for each pixel. |
  104. +---------------------+---------+------------------------+-----------------------------------------------------+
  105. |fragCoord |vec2 |FRAGCOORD |For full screen quads. For smaller quads use UV. |
  106. +---------------------+---------+------------------------+-----------------------------------------------------+
  107. |iResolution |vec3 |1.0 / SCREEN_PIXEL_SIZE |Can also pass in manually. |
  108. +---------------------+---------+------------------------+-----------------------------------------------------+
  109. |iTime |float |TIME |Time since shader started. |
  110. +---------------------+---------+------------------------+-----------------------------------------------------+
  111. |iTimeDelta |float |Provide with Uniform |Time to render previous frame. |
  112. +---------------------+---------+------------------------+-----------------------------------------------------+
  113. |iFrame |float |Provide with Uniform |Frame number. |
  114. +---------------------+---------+------------------------+-----------------------------------------------------+
  115. |iChannelTime[4] |float |Provide with Uniform |Time since that particular texture started. |
  116. +---------------------+---------+------------------------+-----------------------------------------------------+
  117. |iMouse |vec4 |Provide with Uniform |Mouse position in pixel coordinates. |
  118. +---------------------+---------+------------------------+-----------------------------------------------------+
  119. |iDate |vec4 |Provide with Uniform |Current date, expressed in seconds. |
  120. +---------------------+---------+------------------------+-----------------------------------------------------+
  121. |iChannelResolution[4]|vec3 |1.0 / TEXTURE_PIXEL_SIZE|Resolution of particular texture. |
  122. +---------------------+---------+------------------------+-----------------------------------------------------+
  123. |iChanneli |Sampler2D|TEXTURE |Godot provides only one built in, user can make more.|
  124. +---------------------+---------+------------------------+-----------------------------------------------------+
  125. Coordinates
  126. ^^^^^^^^^^^
  127. ``fragCoord`` behaves the same as ``gl_FragCoord`` in :ref:`GLSL <glsl_coordinates>` and ``FRAGCOORD`` in Godot.
  128. The Book of Shaders
  129. -------------------
  130. Similar to Shadertoy, `The Book of Shaders <https://thebookofshaders.com>`_ provides access to a fragment
  131. shader in the web browser for the user to interact with. The user is restricted to writing fragment
  132. shader code with a set list of uniforms passed in and with no ability to add additional uniforms.
  133. For further help on porting shaders to various frameworks generally, The Book of Shaders provides
  134. a `page <https://thebookofshaders.com/04>`_ on running shaders in various frameworks.
  135. Types
  136. ^^^^^
  137. The Book of Shaders uses the webgl spec so it runs a slightly different version of GLSL. However, it still
  138. has the regular types, including `Constants`_ and macros.
  139. Main
  140. ^^^^
  141. The entry point for a Book of Shaders fragment shader is ``main``, just like in GLSL. Everything written in
  142. a Book of Shaders ``main`` function should be copied into Godot's ``fragment`` function.
  143. Variables
  144. ^^^^^^^^^
  145. The Book of Shaders sticks closer to plain GLSL than Shadertoy does. It also implements fewer uniforms than
  146. Shadertoy.
  147. +---------------------+---------+------------------------+-----------------------------------------------------+
  148. |Variable |Type |Equivalent |Description |
  149. +=====================+=========+========================+=====================================================+
  150. |gl_FragColor |out vec4 |COLOR |Output color for each pixel. |
  151. +---------------------+---------+------------------------+-----------------------------------------------------+
  152. |gl_FragCoord |vec4 |FRAGCOORD |For full screen quads. For smaller quads use UV. |
  153. +---------------------+---------+------------------------+-----------------------------------------------------+
  154. |u_resolution |vec2 |1.0 / SCREEN_PIXEL_SIZE |Can also pass in manually. |
  155. +---------------------+---------+------------------------+-----------------------------------------------------+
  156. |u_time |float |TIME |Time since shader started. |
  157. +---------------------+---------+------------------------+-----------------------------------------------------+
  158. |u_mouse |vec2 |Provide with Uniform |Mouse position in pixel coordinates. |
  159. +---------------------+---------+------------------------+-----------------------------------------------------+
  160. Coordinates
  161. ^^^^^^^^^^^
  162. The Book of Shaders uses the same coordinate system as :ref:`GLSL <glsl_coordinates>`.