shading_language.rst 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. .. _doc_shading_language:
  2. Shading language
  3. ================
  4. Introduction
  5. ------------
  6. Godot uses a shading language similar to GLSL ES 3.0. Most datatypes and
  7. functions are supported, and the few remaining ones will likely be added over
  8. time.
  9. If you are already familiar with GLSL, the :ref:`Godot Shader Migration
  10. Guide<doc_converting_glsl_to_godot_shaders>` is a resource that will help you
  11. transition from regular GLSL to Godot's shading language.
  12. .. _doc_shading_language_data_types:
  13. Data types
  14. ----------
  15. Most GLSL ES 3.0 datatypes are supported:
  16. +------------------------+---------------------------------------------------------------------------------+
  17. | Type | Description |
  18. +========================+=================================================================================+
  19. | **void** | Void datatype, useful only for functions that return nothing. |
  20. +------------------------+---------------------------------------------------------------------------------+
  21. | **bool** | Boolean datatype, can only contain ``true`` or ``false``. |
  22. +------------------------+---------------------------------------------------------------------------------+
  23. | **bvec2** | Two-component vector of booleans. |
  24. +------------------------+---------------------------------------------------------------------------------+
  25. | **bvec3** | Three-component vector of booleans. |
  26. +------------------------+---------------------------------------------------------------------------------+
  27. | **bvec4** | Four-component vector of booleans. |
  28. +------------------------+---------------------------------------------------------------------------------+
  29. | **int** | 32 bit signed scalar integer. |
  30. +------------------------+---------------------------------------------------------------------------------+
  31. | **ivec2** | Two-component vector of signed integers. |
  32. +------------------------+---------------------------------------------------------------------------------+
  33. | **ivec3** | Three-component vector of signed integers. |
  34. +------------------------+---------------------------------------------------------------------------------+
  35. | **ivec4** | Four-component vector of signed integers. |
  36. +------------------------+---------------------------------------------------------------------------------+
  37. | **uint** | Unsigned scalar integer; can't contain negative numbers. |
  38. +------------------------+---------------------------------------------------------------------------------+
  39. | **uvec2** | Two-component vector of unsigned integers. |
  40. +------------------------+---------------------------------------------------------------------------------+
  41. | **uvec3** | Three-component vector of unsigned integers. |
  42. +------------------------+---------------------------------------------------------------------------------+
  43. | **uvec4** | Four-component vector of unsigned integers. |
  44. +------------------------+---------------------------------------------------------------------------------+
  45. | **float** | 32 bit floating-point scalar. |
  46. +------------------------+---------------------------------------------------------------------------------+
  47. | **vec2** | Two-component vector of floating-point values. |
  48. +------------------------+---------------------------------------------------------------------------------+
  49. | **vec3** | Three-component vector of floating-point values. |
  50. +------------------------+---------------------------------------------------------------------------------+
  51. | **vec4** | Four-component vector of floating-point values. |
  52. +------------------------+---------------------------------------------------------------------------------+
  53. | **mat2** | 2x2 matrix, in column major order. |
  54. +------------------------+---------------------------------------------------------------------------------+
  55. | **mat3** | 3x3 matrix, in column major order. |
  56. +------------------------+---------------------------------------------------------------------------------+
  57. | **mat4** | 4x4 matrix, in column major order. |
  58. +------------------------+---------------------------------------------------------------------------------+
  59. | **sampler2D** | Sampler type for binding 2D textures, which are read as float. |
  60. +------------------------+---------------------------------------------------------------------------------+
  61. | **isampler2D** | Sampler type for binding 2D textures, which are read as signed integer. |
  62. +------------------------+---------------------------------------------------------------------------------+
  63. | **usampler2D** | Sampler type for binding 2D textures, which are read as unsigned integer. |
  64. +------------------------+---------------------------------------------------------------------------------+
  65. | **sampler2DArray** | Sampler type for binding 2D texture arrays, which are read as float. |
  66. +------------------------+---------------------------------------------------------------------------------+
  67. | **isampler2DArray** | Sampler type for binding 2D texture arrays, which are read as signed integer. |
  68. +------------------------+---------------------------------------------------------------------------------+
  69. | **usampler2DArray** | Sampler type for binding 2D texture arrays, which are read as unsigned integer. |
  70. +------------------------+---------------------------------------------------------------------------------+
  71. | **sampler3D** | Sampler type for binding 3D textures, which are read as float. |
  72. +------------------------+---------------------------------------------------------------------------------+
  73. | **isampler3D** | Sampler type for binding 3D textures, which are read as signed integer. |
  74. +------------------------+---------------------------------------------------------------------------------+
  75. | **usampler3D** | Sampler type for binding 3D textures, which are read as unsigned integer. |
  76. +------------------------+---------------------------------------------------------------------------------+
  77. | **samplerCube** | Sampler type for binding Cubemaps, which are read as float. |
  78. +------------------------+---------------------------------------------------------------------------------+
  79. | **samplerCubeArray** | Sampler type for binding Cubemap arrays, which are read as float. |
  80. | | Only supported in Forward+ and Mobile, not Compatibility. |
  81. +------------------------+---------------------------------------------------------------------------------+
  82. | **samplerExternalOES** | External sampler type. |
  83. | | Only supported in Compatibility/Android platform. |
  84. +------------------------+---------------------------------------------------------------------------------+
  85. .. warning::
  86. Local variables are not initialized to a default value such as ``0.0``. If
  87. you use a variable without assigning it first, it will contain whatever
  88. value was already present at that memory location, and unpredictable visual
  89. glitches will appear. However, uniforms and varyings are initialized to a
  90. default value.
  91. Comments
  92. ~~~~~~~~
  93. The shading language supports the same comment syntax as used in C# and C++:
  94. .. code-block:: glsl
  95. // Single-line comment.
  96. int a = 2; // Another single-line comment.
  97. /*
  98. Multi-line comment.
  99. The comment ends when the ending delimiter is found
  100. (here, it's on the line below).
  101. */
  102. int b = 3;
  103. Additionally, you can use documentation comments that are displayed in the
  104. inspector when hovering a shader parameter. Documentation comments are currently
  105. only supported when placed immediately above a ``uniform`` declaration. These
  106. documentation comments only support the **multiline** comment syntax and must use
  107. **two** leading asterisks (``/**``) instead of just one (``/*``):
  108. .. code-block:: glsl
  109. /**
  110. * This is a documentation comment.
  111. * These lines will appear in the inspector when hovering the shader parameter
  112. * named "Something".
  113. * You can use [b]BBCode[/b] [i]formatting[/i] in the comment.
  114. */
  115. uniform int something = 1;
  116. The asterisks on the follow-up lines are not required, but are recommended as
  117. per the :ref:`doc_shaders_style_guide`. These asterisks are automatically
  118. stripped by the inspector, so they won't appear in the tooltip.
  119. Casting
  120. ~~~~~~~
  121. Just like GLSL ES 3.0, implicit casting between scalars and vectors of the same
  122. size but different type is not allowed. Casting of types of different size is
  123. also not allowed. Conversion must be done explicitly via constructors.
  124. Example:
  125. .. code-block:: glsl
  126. float a = 2; // invalid
  127. float a = 2.0; // valid
  128. float a = float(2); // valid
  129. Default integer constants are signed, so casting is always needed to convert to
  130. unsigned:
  131. .. code-block:: glsl
  132. int a = 2; // valid
  133. uint a = 2; // invalid
  134. uint a = uint(2); // valid
  135. Members
  136. ~~~~~~~
  137. Individual scalar members of vector types are accessed via the "x", "y", "z" and
  138. "w" members. Alternatively, using "r", "g", "b" and "a" also works and is
  139. equivalent. Use whatever fits best for your needs.
  140. For matrices, use the ``m[column][row]`` indexing syntax to access each scalar,
  141. or ``m[column]`` to access a vector by column index. For example, for accessing the
  142. y-component of the translation from a mat4 transform matrix (4th column, 2nd line) you use ``m[3][1]`` or ``m[3].y``.
  143. Constructing
  144. ~~~~~~~~~~~~
  145. Construction of vector types must always pass:
  146. .. code-block:: glsl
  147. // The required amount of scalars
  148. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  149. // Complementary vectors and/or scalars
  150. vec4 a = vec4(vec2(0.0, 1.0), vec2(2.0, 3.0));
  151. vec4 a = vec4(vec3(0.0, 1.0, 2.0), 3.0);
  152. // A single scalar for the whole vector
  153. vec4 a = vec4(0.0);
  154. Construction of matrix types requires vectors of the same dimension as the
  155. matrix, interpreted as columns. You can also build a diagonal matrix using ``matx(float)`` syntax.
  156. Accordingly, ``mat4(1.0)`` is an identity matrix.
  157. .. code-block:: glsl
  158. mat2 m2 = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0));
  159. mat3 m3 = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  160. mat4 identity = mat4(1.0);
  161. Matrices can also be built from a matrix of another dimension. There are two
  162. rules:
  163. 1. If a larger matrix is constructed from a smaller matrix, the additional rows
  164. and columns are set to the values they would have in an identity matrix.
  165. 2. If a smaller matrix is constructed from a larger matrix, the top, left
  166. submatrix of the larger matrix is used.
  167. .. code-block:: glsl
  168. mat3 basis = mat3(MODEL_MATRIX);
  169. mat4 m4 = mat4(basis);
  170. mat2 m2 = mat2(m4);
  171. Swizzling
  172. ~~~~~~~~~
  173. It is possible to obtain any combination of components in any order, as long as
  174. the result is another vector type (or scalar). This is easier shown than
  175. explained:
  176. .. code-block:: glsl
  177. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  178. vec3 b = a.rgb; // Creates a vec3 with vec4 components.
  179. vec3 b = a.ggg; // Also valid; creates a vec3 and fills it with a single vec4 component.
  180. vec3 b = a.bgr; // "b" will be vec3(2.0, 1.0, 0.0).
  181. vec3 b = a.xyz; // Also rgba, xyzw are equivalent.
  182. vec3 b = a.stp; // And stpq (for texture coordinates).
  183. float c = b.w; // Invalid, because "w" is not present in vec3 b.
  184. vec3 c = b.xrt; // Invalid, mixing different styles is forbidden.
  185. b.rrr = a.rgb; // Invalid, assignment with duplication.
  186. b.bgr = a.rgb; // Valid assignment. "b"'s "blue" component will be "a"'s "red" and vice versa.
  187. Precision
  188. ~~~~~~~~~
  189. It is possible to add precision modifiers to datatypes; use them for uniforms,
  190. variables, arguments and varyings:
  191. .. code-block:: glsl
  192. lowp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
  193. mediump vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
  194. highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (32 bit default)
  195. Using lower precision for some operations can speed up the math involved (at the
  196. cost of less precision). This is rarely needed in the vertex processor function
  197. (where full precision is needed most of the time), but is often useful in the
  198. fragment processor.
  199. Some architectures (mainly mobile) can benefit significantly from this, but
  200. there are downsides such as the additional overhead of conversion between
  201. precisions. Refer to the documentation of the target architecture for further
  202. information. In many cases, mobile drivers cause inconsistent or unexpected
  203. behavior and it is best to avoid specifying precision unless necessary.
  204. Arrays
  205. ------
  206. Arrays are containers for multiple variables of a similar type.
  207. Local arrays
  208. ~~~~~~~~~~~~
  209. Local arrays are declared in functions. They can use all of the allowed
  210. datatypes, except samplers. The array declaration follows a C-style syntax:
  211. ``[const] + [precision] + typename + identifier + [array size]``.
  212. .. code-block:: glsl
  213. void fragment() {
  214. float arr[3];
  215. }
  216. They can be initialized at the beginning like:
  217. .. code-block:: glsl
  218. float float_arr[3] = float[3] (1.0, 0.5, 0.0); // first constructor
  219. int int_arr[3] = int[] (2, 1, 0); // second constructor
  220. vec2 vec2_arr[3] = { vec2(1.0, 1.0), vec2(0.5, 0.5), vec2(0.0, 0.0) }; // third constructor
  221. bool bool_arr[] = { true, true, false }; // fourth constructor - size is defined automatically from the element count
  222. You can declare multiple arrays (even with different sizes) in one expression:
  223. .. code-block:: glsl
  224. float a[3] = float[3] (1.0, 0.5, 0.0),
  225. b[2] = { 1.0, 0.5 },
  226. c[] = { 0.7 },
  227. d = 0.0,
  228. e[5];
  229. To access an array element, use the indexing syntax:
  230. .. code-block:: glsl
  231. float arr[3];
  232. arr[0] = 1.0; // setter
  233. COLOR.r = arr[0]; // getter
  234. Arrays also have a built-in function ``.length()`` (not to be confused with the
  235. built-in ``length()`` function). It doesn't accept any parameters and will
  236. return the array's size.
  237. .. code-block:: glsl
  238. float arr[] = { 0.0, 1.0, 0.5, -1.0 };
  239. for (int i = 0; i < arr.length(); i++) {
  240. // ...
  241. }
  242. .. note::
  243. If you use an index either below 0 or greater than array size - the shader will
  244. crash and break rendering. To prevent this, use ``length()``, ``if``, or
  245. ``clamp()`` functions to ensure the index is between 0 and the array's
  246. length. Always carefully test and check your code. If you pass a constant
  247. expression or a number, the editor will check its bounds to prevent
  248. this crash.
  249. Global arrays
  250. ~~~~~~~~~~~~~
  251. You can declare arrays in global space as either ``const`` or ``uniform``:
  252. .. code-block:: glsl
  253. shader_type spatial;
  254. const lowp vec3 v[1] = lowp vec3[1] ( vec3(0, 0, 1) );
  255. uniform lowp vec3 w[1];
  256. void fragment() {
  257. ALBEDO = v[0] + w[0];
  258. }
  259. .. note::
  260. Global arrays use the same syntax as local arrays, except with a ``const``
  261. or ``uniform`` added to their declaration. Note that uniform arrays can't
  262. have a default value.
  263. Constants
  264. ---------
  265. Use the ``const`` keyword before the variable declaration to make that variable
  266. immutable, which means that it cannot be modified. All basic types, except
  267. samplers can be declared as constants. Accessing and using a constant value is
  268. slightly faster than using a uniform. Constants must be initialized at their
  269. declaration.
  270. .. code-block:: glsl
  271. const vec2 a = vec2(0.0, 1.0);
  272. vec2 b;
  273. a = b; // invalid
  274. b = a; // valid
  275. Constants cannot be modified and additionally cannot have hints, but multiple of
  276. them (if they have the same type) can be declared in a single expression e.g
  277. .. code-block:: glsl
  278. const vec2 V1 = vec2(1, 1), V2 = vec2(2, 2);
  279. Similar to variables, arrays can also be declared with ``const``.
  280. .. code-block:: glsl
  281. const float arr[] = { 1.0, 0.5, 0.0 };
  282. arr[0] = 1.0; // invalid
  283. COLOR.r = arr[0]; // valid
  284. Constants can be declared both globally (outside of any function) or locally
  285. (inside a function). Global constants are useful when you want to have access to
  286. a value throughout your shader that does not need to be modified. Like uniforms,
  287. global constants are shared between all shader stages, but they are not
  288. accessible outside of the shader.
  289. .. code-block:: glsl
  290. shader_type spatial;
  291. const float GOLDEN_RATIO = 1.618033988749894;
  292. Constants of the ``float`` type must be initialized using ``.`` notation after the
  293. decimal part or by using the scientific notation. The optional ``f`` post-suffix is
  294. also supported.
  295. .. code-block:: glsl
  296. float a = 1.0;
  297. float b = 1.0f; // same, using suffix for clarity
  298. float c = 1e-1; // gives 0.1 by using the scientific notation
  299. Constants of the ``uint`` (unsigned int) type must have a ``u`` suffix to differentiate them from signed integers.
  300. Alternatively, this can be done by using the ``uint(x)`` built-in conversion function.
  301. .. code-block:: glsl
  302. uint a = 1u;
  303. uint b = uint(1);
  304. Structs
  305. -------
  306. Structs are compound types which can be used for better abstraction of shader
  307. code. You can declare them at the global scope like:
  308. .. code-block:: glsl
  309. struct PointLight {
  310. vec3 position;
  311. vec3 color;
  312. float intensity;
  313. };
  314. After declaration, you can instantiate and initialize them like:
  315. .. code-block:: glsl
  316. void fragment()
  317. {
  318. PointLight light;
  319. light.position = vec3(0.0);
  320. light.color = vec3(1.0, 0.0, 0.0);
  321. light.intensity = 0.5;
  322. }
  323. Or use struct constructor for same purpose:
  324. .. code-block:: glsl
  325. PointLight light = PointLight(vec3(0.0), vec3(1.0, 0.0, 0.0), 0.5);
  326. Structs may contain other struct or array, you can also instance them as global
  327. constant:
  328. .. code-block:: glsl
  329. shader_type spatial;
  330. ...
  331. struct Scene {
  332. PointLight lights[2];
  333. };
  334. const Scene scene = Scene(PointLight[2](PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0), PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0)));
  335. void fragment()
  336. {
  337. ALBEDO = scene.lights[0].color;
  338. }
  339. You can also pass them to functions:
  340. .. code-block:: glsl
  341. shader_type canvas_item;
  342. ...
  343. Scene construct_scene(PointLight light1, PointLight light2) {
  344. return Scene({light1, light2});
  345. }
  346. void fragment()
  347. {
  348. COLOR.rgb = construct_scene(PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0), PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 1.0), 1.0)).lights[0].color;
  349. }
  350. Operators
  351. ---------
  352. Godot shading language supports the same set of operators as GLSL ES 3.0. Below
  353. is the list of them in precedence order:
  354. .. table::
  355. :class: nowrap-col3
  356. +-------------+------------------------+------------------+
  357. | Precedence | Class | Operator |
  358. +-------------+------------------------+------------------+
  359. | 1 (highest) | parenthetical grouping | **()** |
  360. +-------------+------------------------+------------------+
  361. | 2 | unary | **+, -, !, ~** |
  362. +-------------+------------------------+------------------+
  363. | 3 | multiplicative | **/, \*, %** |
  364. +-------------+------------------------+------------------+
  365. | 4 | additive | **+, -** |
  366. +-------------+------------------------+------------------+
  367. | 5 | bit-wise shift | **<<, >>** |
  368. +-------------+------------------------+------------------+
  369. | 6 | relational | **<, >, <=, >=** |
  370. +-------------+------------------------+------------------+
  371. | 7 | equality | **==, !=** |
  372. +-------------+------------------------+------------------+
  373. | 8 | bit-wise AND | **&** |
  374. +-------------+------------------------+------------------+
  375. | 9 | bit-wise exclusive OR | **^** |
  376. +-------------+------------------------+------------------+
  377. | 10 | bit-wise inclusive OR | **|** |
  378. +-------------+------------------------+------------------+
  379. | 11 | logical AND | **&&** |
  380. +-------------+------------------------+------------------+
  381. | 12 (lowest) | logical inclusive OR | **||** |
  382. +-------------+------------------------+------------------+
  383. .. note::
  384. Most operators that accept vectors or matrices (multiplication, division, etc) operate component-wise, meaning the function
  385. is applied to the first value of each vector and then on the second value of each vector, etc. Some examples:
  386. .. table::
  387. :class: nowrap-col2 nowrap-col1
  388. :widths: auto
  389. +---------------------------------------+------------------------------------------------------+
  390. | Operation | Equivalent Scalar Operation |
  391. +=======================================+======================================================+
  392. | ``vec3(4, 5, 6) + 2`` | ``vec3(4 + 2, 5 + 2, 6 + 2)`` |
  393. +---------------------------------------+------------------------------------------------------+
  394. | ``vec2(3, 4) * vec2(10, 20)`` | ``vec2(3 * 10, 4 * 20)`` |
  395. +---------------------------------------+------------------------------------------------------+
  396. | ``mat2(vec2(1, 2), vec2(3, 4)) + 10`` | ``mat2(vec2(1 + 10, 2 + 10), vec2(3 + 10, 4 + 10))`` |
  397. +---------------------------------------+------------------------------------------------------+
  398. The `GLSL Language Specification <http://www.opengl.org/registry/doc/GLSLangSpec.4.30.6.pdf>`_ says under section 5.10 Vector and Matrix Operations:
  399. With a few exceptions, operations are component-wise. Usually, when an operator operates on a
  400. vector or matrix, it is operating independently on each component of the vector or matrix,
  401. in a component-wise fashion. [...] The exceptions are matrix multiplied by vector,
  402. vector multiplied by matrix, and matrix multiplied by matrix. These do not operate component-wise,
  403. but rather perform the correct linear algebraic multiply.
  404. Flow control
  405. ------------
  406. Godot Shading language supports the most common types of flow control:
  407. .. code-block:: glsl
  408. // `if` and `else`.
  409. if (cond) {
  410. } else {
  411. }
  412. // Ternary operator.
  413. // This is an expression that behaves like `if`/`else` and returns the value.
  414. // If `cond` evaluates to `true`, `result` will be `9`.
  415. // Otherwise, `result` will be `5`.
  416. int result = cond ? 9 : 5;
  417. // `switch`.
  418. switch (i) { // `i` should be a signed integer expression.
  419. case -1:
  420. break;
  421. case 0:
  422. return; // `break` or `return` to avoid running the next `case`.
  423. case 1: // Fallthrough (no `break` or `return`): will run the next `case`.
  424. case 2:
  425. break;
  426. //...
  427. default: // Only run if no `case` above matches. Optional.
  428. break;
  429. }
  430. // `for` loop. Best used when the number of elements to iterate on
  431. // is known in advance.
  432. for (int i = 0; i < 10; i++) {
  433. }
  434. // `while` loop. Best used when the number of elements to iterate on
  435. // is not known in advance.
  436. while (cond) {
  437. }
  438. // `do while`. Like `while`, but always runs at least once even if `cond`
  439. // never evaluates to `true`.
  440. do {
  441. } while (cond);
  442. Keep in mind that in modern GPUs, an infinite loop can exist and can freeze
  443. your application (including editor). Godot can't protect you from this, so be
  444. careful not to make this mistake!
  445. Also, when comparing floating-point values against a number, make sure to
  446. compare them against a *range* instead of an exact number.
  447. A comparison like ``if (value == 0.3)`` may not evaluate to ``true``.
  448. Floating-point math is often approximate and can defy expectations. It can also
  449. behave differently depending on the hardware.
  450. **Don't** do this.
  451. .. code-block:: glsl
  452. float value = 0.1 + 0.2;
  453. // May not evaluate to `true`!
  454. if (value == 0.3) {
  455. // ...
  456. }
  457. Instead, always perform a range comparison with an epsilon value. The larger the
  458. floating-point number (and the less precise the floating-point number), the
  459. larger the epsilon value should be.
  460. .. code-block:: glsl
  461. const float EPSILON = 0.0001;
  462. if (value >= 0.3 - EPSILON && value <= 0.3 + EPSILON) {
  463. // ...
  464. }
  465. See `floating-point-gui.de <https://floating-point-gui.de/>`__ for more
  466. information.
  467. Discarding
  468. ----------
  469. Fragment, light, and custom functions (called from fragment or light) can use the
  470. ``discard`` keyword. If used, the fragment is discarded and nothing is written.
  471. Beware that ``discard`` has a performance cost when used, as it will prevent the
  472. depth prepass from being effective on any surfaces using the shader. Also, a
  473. discarded pixel still needs to be rendered in the vertex shader, which means a
  474. shader that uses ``discard`` on all of its pixels is still more expensive to
  475. render compared to not rendering any object in the first place.
  476. Functions
  477. ---------
  478. It is possible to define functions in a Godot shader. They use the following
  479. syntax:
  480. .. code-block:: glsl
  481. ret_type func_name(args) {
  482. return ret_type; // if returning a value
  483. }
  484. // a more specific example:
  485. int sum2(int a, int b) {
  486. return a + b;
  487. }
  488. You can only use functions that have been defined above (higher in the editor)
  489. the function from which you are calling them. Redefining a function that has
  490. already been defined above (or is a built-in function name) will cause an error.
  491. Function arguments can have special qualifiers:
  492. * **in**: Means the argument is only for reading (default).
  493. * **out**: Means the argument is only for writing.
  494. * **inout**: Means the argument is fully passed via reference.
  495. * **const**: Means the argument is a constant and cannot be changed, may be
  496. combined with **in** qualifier.
  497. Example below:
  498. .. code-block:: glsl
  499. void sum2(int a, int b, inout int result) {
  500. result = a + b;
  501. }
  502. Function overloading is supported. You can define multiple functions with the same
  503. name, but different arguments. Note that `implicit casting <Casting_>`_ in overloaded
  504. function calls is not allowed, such as from ``int`` to ``float`` (``1`` to ``1.0``).
  505. .. code-block:: glsl
  506. vec3 get_color(int t) {
  507. return vec3(1, 0, 0); // Red color.
  508. }
  509. vec3 get_color(float t) {
  510. return vec3(0, 1, 0); // Green color.
  511. }
  512. void fragment() {
  513. vec3 red = get_color(1);
  514. vec3 green = get_color(1.0);
  515. }
  516. .. _doc_shading_language_varyings:
  517. Varyings
  518. --------
  519. To send data from the vertex to the fragment (or light) processor function, *varyings* are
  520. used. They are set for every primitive vertex in the *vertex processor*, and the
  521. value is interpolated for every pixel in the *fragment processor*.
  522. .. code-block:: glsl
  523. shader_type spatial;
  524. varying vec3 some_color;
  525. void vertex() {
  526. some_color = NORMAL; // Make the normal the color.
  527. }
  528. void fragment() {
  529. ALBEDO = some_color;
  530. }
  531. void light() {
  532. DIFFUSE_LIGHT = some_color * 100; // optionally
  533. }
  534. Varying can also be an array:
  535. .. code-block:: glsl
  536. shader_type spatial;
  537. varying float var_arr[3];
  538. void vertex() {
  539. var_arr[0] = 1.0;
  540. var_arr[1] = 0.0;
  541. }
  542. void fragment() {
  543. ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
  544. }
  545. It's also possible to send data from *fragment* to *light* processors using *varying* keyword. To do so you can assign it in the *fragment* and later use it in the *light* function.
  546. .. code-block:: glsl
  547. shader_type spatial;
  548. varying vec3 some_light;
  549. void fragment() {
  550. some_light = ALBEDO * 100.0; // Make a shining light.
  551. }
  552. void light() {
  553. DIFFUSE_LIGHT = some_light;
  554. }
  555. Note that varying may not be assigned in custom functions or a *light processor* function like:
  556. .. code-block:: glsl
  557. shader_type spatial;
  558. varying float test;
  559. void foo() {
  560. test = 0.0; // Error.
  561. }
  562. void vertex() {
  563. test = 0.0;
  564. }
  565. void light() {
  566. test = 0.0; // Error too.
  567. }
  568. This limitation was introduced to prevent incorrect usage before initialization.
  569. Interpolation qualifiers
  570. ------------------------
  571. Certain values are interpolated during the shading pipeline. You can modify how
  572. these interpolations are done by using *interpolation qualifiers*.
  573. .. code-block:: glsl
  574. shader_type spatial;
  575. varying flat vec3 our_color;
  576. void vertex() {
  577. our_color = COLOR.rgb;
  578. }
  579. void fragment() {
  580. ALBEDO = our_color;
  581. }
  582. There are two possible interpolation qualifiers:
  583. +-------------------+---------------------------------------------------------------------------------+
  584. | Qualifier | Description |
  585. +===================+=================================================================================+
  586. | **flat** | The value is not interpolated. |
  587. +-------------------+---------------------------------------------------------------------------------+
  588. | **smooth** | The value is interpolated in a perspective-correct fashion. This is the default.|
  589. +-------------------+---------------------------------------------------------------------------------+
  590. Uniforms
  591. --------
  592. Passing values to shaders is possible with *uniforms*, which are defined in the
  593. global scope of the shader, outside of functions. When a shader is later
  594. assigned to a material, the uniforms will appear as editable parameters in the
  595. material's inspector. Uniforms can't be written from within the shader. Any
  596. :ref:`data type <doc_shading_language_data_types>` except for ``void`` can be a uniform.
  597. .. code-block:: glsl
  598. shader_type spatial;
  599. uniform float some_value;
  600. uniform vec3 colors[3];
  601. You can set uniforms in the editor in the material's inspector. Alternately, you
  602. can set them :ref:`from code <doc_shading_language_setting_uniforms_from_code>`.
  603. Uniform hints
  604. ~~~~~~~~~~~~~
  605. Godot provides optional uniform hints to make the compiler understand what the
  606. uniform is used for, and how the editor should allow users to modify it.
  607. .. code-block:: glsl
  608. shader_type spatial;
  609. uniform vec4 color : source_color;
  610. uniform float amount : hint_range(0, 1);
  611. uniform vec4 other_color : source_color = vec4(1.0); // Default values go after the hint.
  612. uniform sampler2D image : source_color;
  613. Uniforms can also be assigned default values:
  614. .. code-block:: glsl
  615. shader_type spatial;
  616. uniform vec4 some_vector = vec4(0.0);
  617. uniform vec4 some_color : source_color = vec4(1.0);
  618. Note that when adding a default value and a hint, the default value goes after the hint.
  619. Full list of uniform hints below:
  620. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  621. | Type | Hint | Description |
  622. +======================+==================================================+=============================================================================+
  623. | **vec3, vec4** | source_color | Used as color. |
  624. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  625. | **int** | hint_enum("String1", "String2") | Displays int input as a dropdown widget in the editor. |
  626. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  627. | **int, float** | hint_range(min, max[, step]) | Restricted to values in a range (with min/max/step). |
  628. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  629. | **sampler2D** | source_color | Used as albedo color. |
  630. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  631. | **sampler2D** | hint_normal | Used as normalmap. |
  632. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  633. | **sampler2D** | hint_default_white | As value or albedo color, default to opaque white. |
  634. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  635. | **sampler2D** | hint_default_black | As value or albedo color, default to opaque black. |
  636. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  637. | **sampler2D** | hint_default_transparent | As value or albedo color, default to transparent black. |
  638. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  639. | **sampler2D** | hint_anisotropy | As flowmap, default to right. |
  640. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  641. | **sampler2D** | hint_roughness[_r, _g, _b, _a, _normal, _gray] | Used for roughness limiter on import (attempts reducing specular aliasing). |
  642. | | | ``_normal`` is a normal map that guides the roughness limiter, |
  643. | | | with roughness increasing in areas that have high-frequency detail. |
  644. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  645. | **sampler2D** | filter[_nearest, _linear][_mipmap][_anisotropic] | Enabled specified texture filtering. |
  646. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  647. | **sampler2D** | repeat[_enable, _disable] | Enabled texture repeating. |
  648. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  649. | **sampler2D** | hint_screen_texture | Texture is the screen texture. |
  650. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  651. | **sampler2D** | hint_depth_texture | Texture is the depth texture. |
  652. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  653. | **sampler2D** | hint_normal_roughness_texture | Texture is the normal roughness texture (only supported in Forward+). |
  654. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  655. Using ``hint_enum``
  656. ^^^^^^^^^^^^^^^^^^^
  657. You can access ``int`` values as a readable dropdown widget using the ``hint_enum`` uniform:
  658. .. code-block::
  659. uniform int noise_type : hint_enum("OpenSimplex2", "Cellular", "Perlin", "Value") = 0;
  660. You can assign explicit values to the ``hint_enum`` uniform using colon syntax similar to GDScript:
  661. .. code-block::
  662. uniform int character_speed: hint_enum("Slow:30", "Average:60", "Very Fast:200") = 60;
  663. The value will be stored as an integer, corresponding to the index of the selected
  664. option (i.e. ``0``, ``1``, or ``2``) or the value assigned by colon syntax
  665. (i.e. ``30``, ``60``, or ``200``). When setting the value with
  666. ``set_shader_parameter()``, you must use the integer value, not the ``String``
  667. name.
  668. Using ``source_color``
  669. ^^^^^^^^^^^^^^^^^^^^^^
  670. Any texture which contains *sRGB color data* requires a ``source_color`` hint
  671. in order to be correctly sampled. This is because Godot renders in linear
  672. color space, but some textures contain sRGB color data. If this hint is not
  673. used, the texture will appear washed out.
  674. Albedo and color textures should typically have a ``source_color`` hint. Normal,
  675. roughness, metallic, and height textures typically do not need a ``source_color``
  676. hint.
  677. Using ``source_color`` hint is required in the Forward+ and Mobile renderers,
  678. and in ``canvas_item`` shaders when :ref:`HDR 2D<class_ProjectSettings_property_rendering/viewport/hdr_2d>`
  679. is enabled. The ``source_color`` hint is optional for the Compatibility renderer,
  680. and for ``canvas_item`` shaders if ``HDR 2D`` is disabled. However, it is
  681. recommended to always use the ``source_color`` hint, because it works even
  682. if you change renderers or disable ``HDR 2D``.
  683. Uniform groups
  684. ~~~~~~~~~~~~~~
  685. To group multiple uniforms in a section in the inspector, you can use a
  686. ``group_uniform`` keyword like this:
  687. .. code-block:: glsl
  688. group_uniforms MyGroup;
  689. uniform sampler2D test;
  690. You can close the group by using:
  691. .. code-block:: glsl
  692. group_uniforms;
  693. The syntax also supports subgroups (it's not mandatory to declare the base group before this):
  694. .. code-block:: glsl
  695. group_uniforms MyGroup.MySubgroup;
  696. .. _doc_shading_language_global_uniforms:
  697. Global uniforms
  698. ~~~~~~~~~~~~~~~
  699. Sometimes, you want to modify a parameter in many different shaders at once.
  700. With a regular uniform, this takes a lot of work as all these shaders need to be
  701. tracked and the uniform needs to be set for each of them. Global uniforms allow
  702. you to create and update uniforms that will be available in all shaders, in
  703. every shader type (``canvas_item``, ``spatial``, ``particles``, ``sky`` and
  704. ``fog``).
  705. Global uniforms are especially useful for environmental effects that affect many
  706. objects in a scene, like having foliage bend when the player is nearby, or having
  707. objects move with the wind.
  708. .. note:: *Global uniforms* are not the same as *global scope* for an individual
  709. shader. While regular uniforms are defined outside of shader functions and are
  710. therefore the global scope of the shader, global uniforms are global to all
  711. shaders in the entire project (but within each shader, are also in the global
  712. scope).
  713. To create a global uniform, open the **Project Settings** then go to the
  714. **Shader Globals** tab. Specify a name for the uniform (case-sensitive) and a
  715. type, then click **Add** in the top-right corner of the dialog. You can then
  716. edit the value assigned to the uniform by clicking the value in the list of
  717. uniforms:
  718. .. figure:: img/shading_language_adding_global_uniforms.webp
  719. :align: center
  720. :alt: Adding a global uniform in the Shader Globals tab of the Project Settings
  721. Adding a global uniform in the Shader Globals tab of the Project Settings
  722. After creating a global uniform, you can use it in a shader as follows:
  723. .. code-block:: glsl
  724. shader_type canvas_item;
  725. global uniform vec4 my_color;
  726. void fragment() {
  727. COLOR = my_color.rgb;
  728. }
  729. Note that the global uniform *must* exist in the Project Settings at the time
  730. the shader is saved, or compilation will fail. While you can assign a default
  731. value using ``global uniform vec4 my_color = ...`` in the shader code, it will
  732. be ignored as the global uniform must always be defined in the Project Settings
  733. anyway.
  734. To change the value of a global uniform at runtime, use the
  735. :ref:`RenderingServer.global_shader_parameter_set <class_RenderingServer_method_global_shader_parameter_set>`
  736. method in a script:
  737. .. code-block:: gdscript
  738. RenderingServer.global_shader_parameter_set("my_color", Color(0.3, 0.6, 1.0))
  739. Assigning global uniform values can be done as many times as desired without
  740. impacting performance, as setting data doesn't require synchronization between
  741. the CPU and GPU.
  742. You can also add or remove global uniforms at runtime:
  743. .. code-block:: gdscript
  744. RenderingServer.global_shader_parameter_add("my_color", RenderingServer.GLOBAL_VAR_TYPE_COLOR, Color(0.3, 0.6, 1.0))
  745. RenderingServer.global_shader_parameter_remove("my_color")
  746. Adding or removing global uniforms at runtime has a performance cost, although
  747. it's not as pronounced compared to getting global uniform values from a script
  748. (see the warning below).
  749. .. warning::
  750. While you *can* query the value of a global uniform at runtime in a script
  751. using ``RenderingServer.global_shader_parameter_get("uniform_name")``, this
  752. has a large performance penalty as the rendering thread needs to synchronize
  753. with the calling thread.
  754. Therefore, it's not recommended to read global shader uniform values
  755. continuously in a script. If you need to read values in a script after
  756. setting them, consider creating an :ref:`autoload <doc_singletons_autoload>`
  757. where you store the values you need to query at the same time you're setting
  758. them as global uniforms.
  759. .. _doc_shading_language_per_instance_uniforms:
  760. Per-instance uniforms
  761. ~~~~~~~~~~~~~~~~~~~~~
  762. .. note::
  763. Per-instance uniforms are only available in ``spatial`` (3D) shaders.
  764. Sometimes, you want to modify a parameter on each node using the material. As an
  765. example, in a forest full of trees, when you want each tree to have a slightly
  766. different color that is editable by hand. Without per-instance uniforms, this
  767. requires creating a unique material for each tree (each with a slightly
  768. different hue). This makes material management more complex, and also has a
  769. performance overhead due to the scene requiring more unique material instances.
  770. Vertex colors could also be used here, but they'd require creating unique copies
  771. of the mesh for each different color, which also has a performance overhead.
  772. Per-instance uniforms are set on each GeometryInstance3D, rather than on each
  773. Material instance. Take this into account when working with meshes that have
  774. multiple materials assigned to them, or MultiMesh setups.
  775. .. code-block:: glsl
  776. shader_type spatial;
  777. // Provide a hint to edit as a color. Optionally, a default value can be provided.
  778. // If no default value is provided, the type's default is used (e.g. opaque black for colors).
  779. instance uniform vec4 my_color : source_color = vec4(1.0, 0.5, 0.0, 1.0);
  780. void fragment() {
  781. ALBEDO = my_color.rgb;
  782. }
  783. After saving the shader, you can change the per-instance uniform's value using
  784. the inspector:
  785. .. figure:: img/shading_language_per_instance_uniforms_inspector.webp
  786. :align: center
  787. :alt: Setting a per-instance uniform's value in the GeometryInstance3D section of the inspector
  788. Setting a per-instance uniform's value in the GeometryInstance3D section of the inspector
  789. Per-instance uniform values can also be set at runtime using
  790. :ref:`set_instance_shader_parameter <class_GeometryInstance3D_method_set_instance_shader_parameter>`
  791. method on a node that inherits from :ref:`class_GeometryInstance3D`:
  792. .. code-block:: gdscript
  793. $MeshInstance3D.set_instance_shader_parameter("my_color", Color(0.3, 0.6, 1.0))
  794. When using per-instance uniforms, there are some restrictions you should be aware of:
  795. - **Per-instance uniforms do not support textures**, only regular scalar and
  796. vector types. As a workaround, you can pass a texture array as a regular
  797. uniform, then pass the index of the texture to be drawn using a per-instance
  798. uniform.
  799. - There is a practical maximum limit of 16 instance uniforms per shader.
  800. - If your mesh uses multiple materials, the parameters for the first mesh
  801. material found will "win" over the subsequent ones, unless they have the same
  802. name, index *and* type. In this case, all parameters are affected correctly.
  803. - If you run into the above situation, you can avoid clashes by manually
  804. specifying the index (0-15) of the instance uniform by using the
  805. ``instance_index`` hint:
  806. .. code-block:: glsl
  807. instance uniform vec4 my_color : source_color, instance_index(5);
  808. .. _doc_shading_language_setting_uniforms_from_code:
  809. Setting uniforms from code
  810. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  811. You can set uniforms from GDScript:
  812. .. code-block:: gdscript
  813. material.set_shader_parameter("some_value", some_value)
  814. material.set_shader_parameter("colors", [Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)])
  815. .. note:: The first argument to ``set_shader_parameter`` is the name of the uniform
  816. in the shader. It must match *exactly* to the name of the uniform in
  817. the shader or else it will not be recognized.
  818. GDScript uses different variable types than GLSL does, so when passing variables
  819. from GDScript to shaders, Godot converts the type automatically. Below is a
  820. table of the corresponding types:
  821. +------------------------+-------------------------+------------------------------------------------------------+
  822. | GLSL type | GDScript type | Notes |
  823. +========================+=========================+============================================================+
  824. | **bool** | **bool** | |
  825. +------------------------+-------------------------+------------------------------------------------------------+
  826. | **bvec2** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  827. | | | |
  828. | | | For example, a bvec2 of (bx, by) could be created in |
  829. | | | the following way: |
  830. | | | |
  831. | | | .. code-block:: gdscript |
  832. | | | |
  833. | | | bvec2_input: int = (int(bx)) | (int(by) << 1) |
  834. | | | |
  835. +------------------------+-------------------------+------------------------------------------------------------+
  836. | **bvec3** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  837. +------------------------+-------------------------+------------------------------------------------------------+
  838. | **bvec4** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  839. +------------------------+-------------------------+------------------------------------------------------------+
  840. | **int** | **int** | |
  841. +------------------------+-------------------------+------------------------------------------------------------+
  842. | **ivec2** | **Vector2i** | |
  843. +------------------------+-------------------------+------------------------------------------------------------+
  844. | **ivec3** | **Vector3i** | |
  845. +------------------------+-------------------------+------------------------------------------------------------+
  846. | **ivec4** | **Vector4i** | |
  847. +------------------------+-------------------------+------------------------------------------------------------+
  848. | **uint** | **int** | |
  849. +------------------------+-------------------------+------------------------------------------------------------+
  850. | **uvec2** | **Vector2i** | |
  851. +------------------------+-------------------------+------------------------------------------------------------+
  852. | **uvec3** | **Vector3i** | |
  853. +------------------------+-------------------------+------------------------------------------------------------+
  854. | **uvec4** | **Vector4i** | |
  855. +------------------------+-------------------------+------------------------------------------------------------+
  856. | **float** | **float** | |
  857. +------------------------+-------------------------+------------------------------------------------------------+
  858. | **vec2** | **Vector2** | |
  859. +------------------------+-------------------------+------------------------------------------------------------+
  860. | **vec3** | **Vector3**, **Color** | When Color is used, it will be interpreted as (r, g, b). |
  861. +------------------------+-------------------------+------------------------------------------------------------+
  862. | **vec4** | **Vector4**, **Color**, | When Color is used, it will be interpreted as (r, g, b, a).|
  863. | | **Rect2**, **Plane**, | |
  864. | | **Quaternion** | When Rect2 is used, it will be interpreted as |
  865. | | | (position.x, position.y, size.x, size.y). |
  866. | | | |
  867. | | | When Plane is used it will be interpreted as |
  868. | | | (normal.x, normal.y, normal.z, d). |
  869. | | | |
  870. | | | |
  871. +------------------------+-------------------------+------------------------------------------------------------+
  872. | **mat2** | **Transform2D** | |
  873. | | | |
  874. +------------------------+-------------------------+------------------------------------------------------------+
  875. | **mat3** | **Basis** | |
  876. +------------------------+-------------------------+------------------------------------------------------------+
  877. | **mat4** | **Projection**, | When a Transform3D is used, the w Vector is set to the |
  878. | | **Transform3D** | identity. |
  879. +------------------------+-------------------------+------------------------------------------------------------+
  880. | **sampler2D** | **Texture2D** | |
  881. +------------------------+-------------------------+------------------------------------------------------------+
  882. | **isampler2D** | **Texture2D** | |
  883. +------------------------+-------------------------+------------------------------------------------------------+
  884. | **usampler2D** | **Texture2D** | |
  885. +------------------------+-------------------------+------------------------------------------------------------+
  886. | **sampler2DArray** | **Texture2DArray** | |
  887. +------------------------+-------------------------+------------------------------------------------------------+
  888. | **isampler2DArray** | **Texture2DArray** | |
  889. +------------------------+-------------------------+------------------------------------------------------------+
  890. | **usampler2DArray** | **Texture2DArray** | |
  891. +------------------------+-------------------------+------------------------------------------------------------+
  892. | **sampler3D** | **Texture3D** | |
  893. +------------------------+-------------------------+------------------------------------------------------------+
  894. | **isampler3D** | **Texture3D** | |
  895. +------------------------+-------------------------+------------------------------------------------------------+
  896. | **usampler3D** | **Texture3D** | |
  897. +------------------------+-------------------------+------------------------------------------------------------+
  898. | **samplerCube** | **Cubemap** | See :ref:`doc_importing_images_changing_import_type` for |
  899. | | | instructions on importing cubemaps for use in Godot. |
  900. +------------------------+-------------------------+------------------------------------------------------------+
  901. | **samplerCubeArray** | **CubemapArray** | Only supported in Forward+ and Mobile, not Compatibility. |
  902. +------------------------+-------------------------+------------------------------------------------------------+
  903. | **samplerExternalOES** | **ExternalTexture** | Only supported in Compatibility/Android platform. |
  904. +------------------------+-------------------------+------------------------------------------------------------+
  905. .. note:: Be careful when setting shader uniforms from GDScript, no error will
  906. be thrown if the type does not match. Your shader will just exhibit
  907. undefined behavior.
  908. .. warning::
  909. As with the last note, no error will be thrown if the typing does not match while setting a shader uniform, this unintuitively includes setting a (GDscript) 64 bit int/float into a Godot shader language int/float (32 bit). This may lead to unintentional consequences in cases where high precision is required.
  910. Uniform limits
  911. ~~~~~~~~~~~~~~
  912. There is a limit to the total size of shader uniforms that you can use
  913. in a single shader. On most desktop platforms, this limit is ``65536``
  914. bytes, or 4096 ``vec4`` uniforms. On mobile platforms, the limit is
  915. typically ``16384`` bytes, or 1024 ``vec4`` uniforms. Vector uniforms
  916. smaller than a ``vec4``, such as ``vec2`` or ``vec3``, are padded to
  917. the size of a ``vec4``. Scalar uniforms such as ``int`` or ``float``
  918. are not padded, and ``bool`` is padded to the size of an ``int``.
  919. Arrays count as the total size of their contents. If you need a uniform
  920. array that is larger than this limit, consider packing the data into a
  921. texture instead, since the *contents* of a texture do not count towards
  922. this limit, only the size of the sampler uniform.
  923. Built-in variables
  924. ------------------
  925. A large number of built-in variables are available, like ``UV``, ``COLOR`` and
  926. ``VERTEX``. What variables are available depends on the type of shader
  927. (``spatial``, ``canvas_item``, ``particle``, etc) and the
  928. function used (``vertex``, ``fragment``, ``light``, ``start``, ``process,
  929. ``sky``, or ``fog``). For a list of the built-in variables that are available,
  930. please see the corresponding pages:
  931. - :ref:`Spatial shaders <doc_spatial_shader>`
  932. - :ref:`Canvas item shaders <doc_canvas_item_shader>`
  933. - :ref:`Particle shaders <doc_particle_shader>`
  934. - :ref:`Sky shaders <doc_sky_shader>`
  935. - :ref:`Fog shaders <doc_fog_shader>`
  936. Built-in functions
  937. ------------------
  938. A large number of built-in functions are supported, conforming to GLSL ES 3.0.
  939. See the :ref:`Built-in functions <doc_shader_functions>` page for details.