shading_language.rst 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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. Data types
  13. ----------
  14. Most GLSL ES 3.0 datatypes are supported:
  15. +----------------------+---------------------------------------------------------------------------------+
  16. | Type | Description |
  17. +======================+=================================================================================+
  18. | **void** | Void datatype, useful only for functions that return nothing. |
  19. +----------------------+---------------------------------------------------------------------------------+
  20. | **bool** | Boolean datatype, can only contain ``true`` or ``false``. |
  21. +----------------------+---------------------------------------------------------------------------------+
  22. | **bvec2** | Two-component vector of booleans. |
  23. +----------------------+---------------------------------------------------------------------------------+
  24. | **bvec3** | Three-component vector of booleans. |
  25. +----------------------+---------------------------------------------------------------------------------+
  26. | **bvec4** | Four-component vector of booleans. |
  27. +----------------------+---------------------------------------------------------------------------------+
  28. | **int** | 32 bit signed scalar integer. |
  29. +----------------------+---------------------------------------------------------------------------------+
  30. | **ivec2** | Two-component vector of signed integers. |
  31. +----------------------+---------------------------------------------------------------------------------+
  32. | **ivec3** | Three-component vector of signed integers. |
  33. +----------------------+---------------------------------------------------------------------------------+
  34. | **ivec4** | Four-component vector of signed integers. |
  35. +----------------------+---------------------------------------------------------------------------------+
  36. | **uint** | Unsigned scalar integer; can't contain negative numbers. |
  37. +----------------------+---------------------------------------------------------------------------------+
  38. | **uvec2** | Two-component vector of unsigned integers. |
  39. +----------------------+---------------------------------------------------------------------------------+
  40. | **uvec3** | Three-component vector of unsigned integers. |
  41. +----------------------+---------------------------------------------------------------------------------+
  42. | **uvec4** | Four-component vector of unsigned integers. |
  43. +----------------------+---------------------------------------------------------------------------------+
  44. | **float** | 32 bit floating-point scalar. |
  45. +----------------------+---------------------------------------------------------------------------------+
  46. | **vec2** | Two-component vector of floating-point values. |
  47. +----------------------+---------------------------------------------------------------------------------+
  48. | **vec3** | Three-component vector of floating-point values. |
  49. +----------------------+---------------------------------------------------------------------------------+
  50. | **vec4** | Four-component vector of floating-point values. |
  51. +----------------------+---------------------------------------------------------------------------------+
  52. | **mat2** | 2x2 matrix, in column major order. |
  53. +----------------------+---------------------------------------------------------------------------------+
  54. | **mat3** | 3x3 matrix, in column major order. |
  55. +----------------------+---------------------------------------------------------------------------------+
  56. | **mat4** | 4x4 matrix, in column major order. |
  57. +----------------------+---------------------------------------------------------------------------------+
  58. | **sampler2D** | Sampler type for binding 2D textures, which are read as float. |
  59. +----------------------+---------------------------------------------------------------------------------+
  60. | **isampler2D** | Sampler type for binding 2D textures, which are read as signed integer. |
  61. +----------------------+---------------------------------------------------------------------------------+
  62. | **usampler2D** | Sampler type for binding 2D textures, which are read as unsigned integer. |
  63. +----------------------+---------------------------------------------------------------------------------+
  64. | **sampler2DArray** | Sampler type for binding 2D texture arrays, which are read as float. |
  65. +----------------------+---------------------------------------------------------------------------------+
  66. | **isampler2DArray** | Sampler type for binding 2D texture arrays, which are read as signed integer. |
  67. +----------------------+---------------------------------------------------------------------------------+
  68. | **usampler2DArray** | Sampler type for binding 2D texture arrays, which are read as unsigned integer. |
  69. +----------------------+---------------------------------------------------------------------------------+
  70. | **sampler3D** | Sampler type for binding 3D textures, which are read as float. |
  71. +----------------------+---------------------------------------------------------------------------------+
  72. | **isampler3D** | Sampler type for binding 3D textures, which are read as signed integer. |
  73. +----------------------+---------------------------------------------------------------------------------+
  74. | **usampler3D** | Sampler type for binding 3D textures, which are read as unsigned integer. |
  75. +----------------------+---------------------------------------------------------------------------------+
  76. | **samplerCube** | Sampler type for binding Cubemaps, which are read as float. |
  77. +----------------------+---------------------------------------------------------------------------------+
  78. | **samplerCubeArray** | Sampler type for binding Cubemap arrays, which are read as float. |
  79. | | Only supported in Forward+ and Mobile, not Compatibility. |
  80. +----------------------+---------------------------------------------------------------------------------+
  81. .. warning::
  82. Local variables are not initialized to a default value such as ``0.0``. If
  83. you use a variable without assigning it first, it will contain whatever
  84. value was already present at that memory location, and unpredictable visual
  85. glitches will appear. However, uniforms and varyings are initialized to a
  86. default value.
  87. Comments
  88. ~~~~~~~~
  89. The shading language supports the same comment syntax as used in C# and C++:
  90. .. code-block:: glsl
  91. // Single-line comment.
  92. int a = 2; // Another single-line comment.
  93. /*
  94. Multi-line comment.
  95. The comment ends when the ending delimiter is found
  96. (here, it's on the line below).
  97. */
  98. int b = 3;
  99. Additionally, you can use documentation comments that are displayed in the
  100. inspector when hovering a shader parameter. Documentation comments are currently
  101. only supported when placed immediately above a ``uniform`` declaration. These
  102. documentation comments only support the **multiline** comment syntax and must use
  103. **two** leading asterisks (``/**``) instead of just one (``/*``):
  104. .. code-block:: glsl
  105. /**
  106. * This is a documentation comment.
  107. * These lines will appear in the inspector when hovering the shader parameter
  108. * named "Something".
  109. * You can use [b]BBCode[/b] [i]formatting[/i] in the comment.
  110. */
  111. uniform int something = 1;
  112. The asterisks on the follow-up lines are not required, but are recommended as
  113. per the :ref:`doc_shaders_style_guide`. These asterisks are automatically
  114. stripped by the inspector, so they won't appear in the tooltip.
  115. Casting
  116. ~~~~~~~
  117. Just like GLSL ES 3.0, implicit casting between scalars and vectors of the same
  118. size but different type is not allowed. Casting of types of different size is
  119. also not allowed. Conversion must be done explicitly via constructors.
  120. Example:
  121. .. code-block:: glsl
  122. float a = 2; // invalid
  123. float a = 2.0; // valid
  124. float a = float(2); // valid
  125. Default integer constants are signed, so casting is always needed to convert to
  126. unsigned:
  127. .. code-block:: glsl
  128. int a = 2; // valid
  129. uint a = 2; // invalid
  130. uint a = uint(2); // valid
  131. Members
  132. ~~~~~~~
  133. Individual scalar members of vector types are accessed via the "x", "y", "z" and
  134. "w" members. Alternatively, using "r", "g", "b" and "a" also works and is
  135. equivalent. Use whatever fits best for your needs.
  136. For matrices, use the ``m[column][row]`` indexing syntax to access each scalar,
  137. or ``m[column]`` to access a vector by column index. For example, for accessing the
  138. y-component of the translation from a mat4 transform matrix (4th column, 2nd line) you use ``m[3][1]`` or ``m[3].y``.
  139. Constructing
  140. ~~~~~~~~~~~~
  141. Construction of vector types must always pass:
  142. .. code-block:: glsl
  143. // The required amount of scalars
  144. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  145. // Complementary vectors and/or scalars
  146. vec4 a = vec4(vec2(0.0, 1.0), vec2(2.0, 3.0));
  147. vec4 a = vec4(vec3(0.0, 1.0, 2.0), 3.0);
  148. // A single scalar for the whole vector
  149. vec4 a = vec4(0.0);
  150. Construction of matrix types requires vectors of the same dimension as the
  151. matrix, interpreted as columns. You can also build a diagonal matrix using ``matx(float)`` syntax.
  152. Accordingly, ``mat4(1.0)`` is an identity matrix.
  153. .. code-block:: glsl
  154. mat2 m2 = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0));
  155. mat3 m3 = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  156. mat4 identity = mat4(1.0);
  157. Matrices can also be built from a matrix of another dimension. There are two
  158. rules:
  159. 1. If a larger matrix is constructed from a smaller matrix, the additional rows
  160. and columns are set to the values they would have in an identity matrix.
  161. 2. If a smaller matrix is constructed from a larger matrix, the top, left
  162. submatrix of the larger matrix is used.
  163. .. code-block:: glsl
  164. mat3 basis = mat3(MODEL_MATRIX);
  165. mat4 m4 = mat4(basis);
  166. mat2 m2 = mat2(m4);
  167. Swizzling
  168. ~~~~~~~~~
  169. It is possible to obtain any combination of components in any order, as long as
  170. the result is another vector type (or scalar). This is easier shown than
  171. explained:
  172. .. code-block:: glsl
  173. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  174. vec3 b = a.rgb; // Creates a vec3 with vec4 components.
  175. vec3 b = a.ggg; // Also valid; creates a vec3 and fills it with a single vec4 component.
  176. vec3 b = a.bgr; // "b" will be vec3(2.0, 1.0, 0.0).
  177. vec3 b = a.xyz; // Also rgba, xyzw are equivalent.
  178. vec3 b = a.stp; // And stpq (for texture coordinates).
  179. float c = b.w; // Invalid, because "w" is not present in vec3 b.
  180. vec3 c = b.xrt; // Invalid, mixing different styles is forbidden.
  181. b.rrr = a.rgb; // Invalid, assignment with duplication.
  182. b.bgr = a.rgb; // Valid assignment. "b"'s "blue" component will be "a"'s "red" and vice versa.
  183. Precision
  184. ~~~~~~~~~
  185. It is possible to add precision modifiers to datatypes; use them for uniforms,
  186. variables, arguments and varyings:
  187. .. code-block:: glsl
  188. lowp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
  189. mediump vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
  190. highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (32 bit default)
  191. Using lower precision for some operations can speed up the math involved (at the
  192. cost of less precision). This is rarely needed in the vertex processor function
  193. (where full precision is needed most of the time), but is often useful in the
  194. fragment processor.
  195. Some architectures (mainly mobile) can benefit significantly from this, but
  196. there are downsides such as the additional overhead of conversion between
  197. precisions. Refer to the documentation of the target architecture for further
  198. information. In many cases, mobile drivers cause inconsistent or unexpected
  199. behavior and it is best to avoid specifying precision unless necessary.
  200. Arrays
  201. ------
  202. Arrays are containers for multiple variables of a similar type.
  203. Local arrays
  204. ~~~~~~~~~~~~
  205. Local arrays are declared in functions. They can use all of the allowed
  206. datatypes, except samplers. The array declaration follows a C-style syntax:
  207. ``[const] + [precision] + typename + identifier + [array size]``.
  208. .. code-block:: glsl
  209. void fragment() {
  210. float arr[3];
  211. }
  212. They can be initialized at the beginning like:
  213. .. code-block:: glsl
  214. float float_arr[3] = float[3] (1.0, 0.5, 0.0); // first constructor
  215. int int_arr[3] = int[] (2, 1, 0); // second constructor
  216. vec2 vec2_arr[3] = { vec2(1.0, 1.0), vec2(0.5, 0.5), vec2(0.0, 0.0) }; // third constructor
  217. bool bool_arr[] = { true, true, false }; // fourth constructor - size is defined automatically from the element count
  218. You can declare multiple arrays (even with different sizes) in one expression:
  219. .. code-block:: glsl
  220. float a[3] = float[3] (1.0, 0.5, 0.0),
  221. b[2] = { 1.0, 0.5 },
  222. c[] = { 0.7 },
  223. d = 0.0,
  224. e[5];
  225. To access an array element, use the indexing syntax:
  226. .. code-block:: glsl
  227. float arr[3];
  228. arr[0] = 1.0; // setter
  229. COLOR.r = arr[0]; // getter
  230. Arrays also have a built-in function ``.length()`` (not to be confused with the
  231. built-in ``length()`` function). It doesn't accept any parameters and will
  232. return the array's size.
  233. .. code-block:: glsl
  234. float arr[] = { 0.0, 1.0, 0.5, -1.0 };
  235. for (int i = 0; i < arr.length(); i++) {
  236. // ...
  237. }
  238. .. note::
  239. If you use an index either below 0 or greater than array size - the shader will
  240. crash and break rendering. To prevent this, use ``length()``, ``if``, or
  241. ``clamp()`` functions to ensure the index is between 0 and the array's
  242. length. Always carefully test and check your code. If you pass a constant
  243. expression or a number, the editor will check its bounds to prevent
  244. this crash.
  245. Global arrays
  246. ~~~~~~~~~~~~~
  247. You can declare arrays at global space like:
  248. .. code-block:: glsl
  249. shader_type spatial;
  250. const lowp vec3 v[1] = lowp vec3[1] ( vec3(0, 0, 1) );
  251. void fragment() {
  252. ALBEDO = v[0];
  253. }
  254. .. note::
  255. Global arrays have to be declared as global constants, otherwise they can be
  256. declared the same as local arrays.
  257. Constants
  258. ---------
  259. Use the ``const`` keyword before the variable declaration to make that variable
  260. immutable, which means that it cannot be modified. All basic types, except
  261. samplers can be declared as constants. Accessing and using a constant value is
  262. slightly faster than using a uniform. Constants must be initialized at their
  263. declaration.
  264. .. code-block:: glsl
  265. const vec2 a = vec2(0.0, 1.0);
  266. vec2 b;
  267. a = b; // invalid
  268. b = a; // valid
  269. Constants cannot be modified and additionally cannot have hints, but multiple of
  270. them (if they have the same type) can be declared in a single expression e.g
  271. .. code-block:: glsl
  272. const vec2 V1 = vec2(1, 1), V2 = vec2(2, 2);
  273. Similar to variables, arrays can also be declared with ``const``.
  274. .. code-block:: glsl
  275. const float arr[] = { 1.0, 0.5, 0.0 };
  276. arr[0] = 1.0; // invalid
  277. COLOR.r = arr[0]; // valid
  278. Constants can be declared both globally (outside of any function) or locally
  279. (inside a function). Global constants are useful when you want to have access to
  280. a value throughout your shader that does not need to be modified. Like uniforms,
  281. global constants are shared between all shader stages, but they are not
  282. accessible outside of the shader.
  283. .. code-block:: glsl
  284. shader_type spatial;
  285. const float GOLDEN_RATIO = 1.618033988749894;
  286. Constants of the ``float`` type must be initialized using ``.`` notation after the
  287. decimal part or by using the scientific notation. The optional ``f`` post-suffix is
  288. also supported.
  289. .. code-block:: glsl
  290. float a = 1.0;
  291. float b = 1.0f; // same, using suffix for clarity
  292. float c = 1e-1; // gives 0.1 by using the scientific notation
  293. Constants of the ``uint`` (unsigned int) type must have a ``u`` suffix to differentiate them from signed integers.
  294. Alternatively, this can be done by using the ``uint(x)`` built-in conversion function.
  295. .. code-block:: glsl
  296. uint a = 1u;
  297. uint b = uint(1);
  298. Structs
  299. -------
  300. Structs are compound types which can be used for better abstraction of shader
  301. code. You can declare them at the global scope like:
  302. .. code-block:: glsl
  303. struct PointLight {
  304. vec3 position;
  305. vec3 color;
  306. float intensity;
  307. };
  308. After declaration, you can instantiate and initialize them like:
  309. .. code-block:: glsl
  310. void fragment()
  311. {
  312. PointLight light;
  313. light.position = vec3(0.0);
  314. light.color = vec3(1.0, 0.0, 0.0);
  315. light.intensity = 0.5;
  316. }
  317. Or use struct constructor for same purpose:
  318. .. code-block:: glsl
  319. PointLight light = PointLight(vec3(0.0), vec3(1.0, 0.0, 0.0), 0.5);
  320. Structs may contain other struct or array, you can also instance them as global
  321. constant:
  322. .. code-block:: glsl
  323. shader_type spatial;
  324. ...
  325. struct Scene {
  326. PointLight lights[2];
  327. };
  328. 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)));
  329. void fragment()
  330. {
  331. ALBEDO = scene.lights[0].color;
  332. }
  333. You can also pass them to functions:
  334. .. code-block:: glsl
  335. shader_type canvas_item;
  336. ...
  337. Scene construct_scene(PointLight light1, PointLight light2) {
  338. return Scene({light1, light2});
  339. }
  340. void fragment()
  341. {
  342. 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;
  343. }
  344. Operators
  345. ---------
  346. Godot shading language supports the same set of operators as GLSL ES 3.0. Below
  347. is the list of them in precedence order:
  348. +-------------+------------------------+------------------+
  349. | Precedence | Class | Operator |
  350. +-------------+------------------------+------------------+
  351. | 1 (highest) | parenthetical grouping | **()** |
  352. +-------------+------------------------+------------------+
  353. | 2 | unary | **+, -, !, ~** |
  354. +-------------+------------------------+------------------+
  355. | 3 | multiplicative | **/, \*, %** |
  356. +-------------+------------------------+------------------+
  357. | 4 | additive | **+, -** |
  358. +-------------+------------------------+------------------+
  359. | 5 | bit-wise shift | **<<, >>** |
  360. +-------------+------------------------+------------------+
  361. | 6 | relational | **<, >, <=, >=** |
  362. +-------------+------------------------+------------------+
  363. | 7 | equality | **==, !=** |
  364. +-------------+------------------------+------------------+
  365. | 8 | bit-wise AND | **&** |
  366. +-------------+------------------------+------------------+
  367. | 9 | bit-wise exclusive OR | **^** |
  368. +-------------+------------------------+------------------+
  369. | 10 | bit-wise inclusive OR | **|** |
  370. +-------------+------------------------+------------------+
  371. | 11 | logical AND | **&&** |
  372. +-------------+------------------------+------------------+
  373. | 12 (lowest) | logical inclusive OR | **||** |
  374. +-------------+------------------------+------------------+
  375. Flow control
  376. ------------
  377. Godot Shading language supports the most common types of flow control:
  378. .. code-block:: glsl
  379. // `if` and `else`.
  380. if (cond) {
  381. } else {
  382. }
  383. // Ternary operator.
  384. // This is an expression that behaves like `if`/`else` and returns the value.
  385. // If `cond` evaluates to `true`, `result` will be `9`.
  386. // Otherwise, `result` will be `5`.
  387. int result = cond ? 9 : 5;
  388. // `switch`.
  389. switch (i) { // `i` should be a signed integer expression.
  390. case -1:
  391. break;
  392. case 0:
  393. return; // `break` or `return` to avoid running the next `case`.
  394. case 1: // Fallthrough (no `break` or `return`): will run the next `case`.
  395. case 2:
  396. break;
  397. //...
  398. default: // Only run if no `case` above matches. Optional.
  399. break;
  400. }
  401. // `for` loop. Best used when the number of elements to iterate on
  402. // is known in advance.
  403. for (int i = 0; i < 10; i++) {
  404. }
  405. // `while` loop. Best used when the number of elements to iterate on
  406. // is not known in advance.
  407. while (cond) {
  408. }
  409. // `do while`. Like `while`, but always runs at least once even if `cond`
  410. // never evaluates to `true`.
  411. do {
  412. } while (cond);
  413. Keep in mind that in modern GPUs, an infinite loop can exist and can freeze
  414. your application (including editor). Godot can't protect you from this, so be
  415. careful not to make this mistake!
  416. Also, when comparing floating-point values against a number, make sure to
  417. compare them against a *range* instead of an exact number.
  418. A comparison like ``if (value == 0.3)`` may not evaluate to ``true``.
  419. Floating-point math is often approximate and can defy expectations. It can also
  420. behave differently depending on the hardware.
  421. **Don't** do this.
  422. .. code-block:: glsl
  423. float value = 0.1 + 0.2;
  424. // May not evaluate to `true`!
  425. if (value == 0.3) {
  426. // ...
  427. }
  428. Instead, always perform a range comparison with an epsilon value. The larger the
  429. floating-point number (and the less precise the floating-point number), the
  430. larger the epsilon value should be.
  431. .. code-block:: glsl
  432. const float EPSILON = 0.0001;
  433. if (value >= 0.3 - EPSILON && value <= 0.3 + EPSILON) {
  434. // ...
  435. }
  436. See `floating-point-gui.de <https://floating-point-gui.de/>`__ for more
  437. information.
  438. Discarding
  439. ----------
  440. Fragment and light functions can use the ``discard`` keyword. If used, the
  441. fragment is discarded and nothing is written.
  442. Beware that ``discard`` has a performance cost when used, as it will prevent the
  443. depth prepass from being effective on any surfaces using the shader. Also, a
  444. discarded pixel still needs to be rendered in the vertex shader, which means a
  445. shader that uses ``discard`` on all of its pixels is still more expensive to
  446. render compared to not rendering any object in the first place.
  447. Functions
  448. ---------
  449. It is possible to define functions in a Godot shader. They use the following
  450. syntax:
  451. .. code-block:: glsl
  452. ret_type func_name(args) {
  453. return ret_type; // if returning a value
  454. }
  455. // a more specific example:
  456. int sum2(int a, int b) {
  457. return a + b;
  458. }
  459. You can only use functions that have been defined above (higher in the editor)
  460. the function from which you are calling them. Redefining a function that has
  461. already been defined above (or is a built-in function name) will cause an error.
  462. Function arguments can have special qualifiers:
  463. * **in**: Means the argument is only for reading (default).
  464. * **out**: Means the argument is only for writing.
  465. * **inout**: Means the argument is fully passed via reference.
  466. * **const**: Means the argument is a constant and cannot be changed, may be
  467. combined with **in** qualifier.
  468. Example below:
  469. .. code-block:: glsl
  470. void sum2(int a, int b, inout int result) {
  471. result = a + b;
  472. }
  473. .. note::
  474. Unlike GLSL, Godot's shader language does **not** support function
  475. overloading. This means that a function cannot be defined several times with
  476. different argument types or numbers of arguments. As a workaround, use
  477. different names for functions that accept a different number of arguments or
  478. arguments of different types.
  479. Varyings
  480. --------
  481. To send data from the vertex to the fragment (or light) processor function, *varyings* are
  482. used. They are set for every primitive vertex in the *vertex processor*, and the
  483. value is interpolated for every pixel in the *fragment processor*.
  484. .. code-block:: glsl
  485. shader_type spatial;
  486. varying vec3 some_color;
  487. void vertex() {
  488. some_color = NORMAL; // Make the normal the color.
  489. }
  490. void fragment() {
  491. ALBEDO = some_color;
  492. }
  493. void light() {
  494. DIFFUSE_LIGHT = some_color * 100; // optionally
  495. }
  496. Varying can also be an array:
  497. .. code-block:: glsl
  498. shader_type spatial;
  499. varying float var_arr[3];
  500. void vertex() {
  501. var_arr[0] = 1.0;
  502. var_arr[1] = 0.0;
  503. }
  504. void fragment() {
  505. ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
  506. }
  507. 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.
  508. .. code-block:: glsl
  509. shader_type spatial;
  510. varying vec3 some_light;
  511. void fragment() {
  512. some_light = ALBEDO * 100.0; // Make a shining light.
  513. }
  514. void light() {
  515. DIFFUSE_LIGHT = some_light;
  516. }
  517. Note that varying may not be assigned in custom functions or a *light processor* function like:
  518. .. code-block:: glsl
  519. shader_type spatial;
  520. varying float test;
  521. void foo() {
  522. test = 0.0; // Error.
  523. }
  524. void vertex() {
  525. test = 0.0;
  526. }
  527. void light() {
  528. test = 0.0; // Error too.
  529. }
  530. This limitation was introduced to prevent incorrect usage before initialization.
  531. Interpolation qualifiers
  532. ------------------------
  533. Certain values are interpolated during the shading pipeline. You can modify how
  534. these interpolations are done by using *interpolation qualifiers*.
  535. .. code-block:: glsl
  536. shader_type spatial;
  537. varying flat vec3 our_color;
  538. void vertex() {
  539. our_color = COLOR.rgb;
  540. }
  541. void fragment() {
  542. ALBEDO = our_color;
  543. }
  544. There are two possible interpolation qualifiers:
  545. +-------------------+---------------------------------------------------------------------------------+
  546. | Qualifier | Description |
  547. +===================+=================================================================================+
  548. | **flat** | The value is not interpolated. |
  549. +-------------------+---------------------------------------------------------------------------------+
  550. | **smooth** | The value is interpolated in a perspective-correct fashion. This is the default.|
  551. +-------------------+---------------------------------------------------------------------------------+
  552. Uniforms
  553. --------
  554. Passing values to shaders is possible. These are global to the whole shader and
  555. are called *uniforms*. When a shader is later assigned to a material, the
  556. uniforms will appear as editable parameters in it. Uniforms can't be written
  557. from within the shader.
  558. .. code-block:: glsl
  559. shader_type spatial;
  560. uniform float some_value;
  561. uniform vec3 colors[3];
  562. You can set uniforms in the editor in the material. Or you can set them through
  563. GDScript:
  564. .. code-block:: gdscript
  565. material.set_shader_parameter("some_value", some_value)
  566. material.set_shader_parameter("colors", [Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)])
  567. .. note:: The first argument to ``set_shader_parameter`` is the name of the uniform
  568. in the shader. It must match *exactly* to the name of the uniform in
  569. the shader or else it will not be recognized.
  570. .. note:: There is a limit to the total size of shader uniforms that you can use
  571. in a single shader. On most desktop platforms, this limit is ``65536``
  572. bytes, or 4096 ``vec4`` uniforms. On mobile platforms, the limit is
  573. typically ``16384`` bytes, or 1024 ``vec4`` uniforms. Vector uniforms
  574. smaller than a ``vec4``, such as ``vec2`` or ``vec3``, are padded to
  575. the size of a ``vec4``. Scalar uniforms such as ``int`` or ``float``
  576. are not padded, and ``bool`` is padded to the size of an ``int``.
  577. Arrays count as the total size of their contents. If you need a uniform
  578. array that is larger than this limit, consider packing the data into a
  579. texture instead, since the *contents* of a texture do not count towards
  580. this limit, only the size of the sampler uniform.
  581. Any GLSL type except for *void* can be a uniform. Additionally, Godot provides
  582. optional shader hints to make the compiler understand for what the uniform is
  583. used, and how the editor should allow users to modify it.
  584. .. code-block:: glsl
  585. shader_type spatial;
  586. uniform vec4 color : source_color;
  587. uniform float amount : hint_range(0, 1);
  588. uniform vec4 other_color : source_color = vec4(1.0); // Default values go after the hint.
  589. uniform sampler2D image : source_color;
  590. It's important to understand that textures *that are supplied as color* require
  591. hints for proper sRGB -> linear conversion (i.e. ``source_color``), as Godot's
  592. 3D engine renders in linear color space. If this is not done, the texture will
  593. appear washed out.
  594. .. note::
  595. The 2D renderer also renders in linear color space if the
  596. **Rendering > Viewport > HDR 2D** project setting is enabled, so
  597. ``source_color`` must also be used in ``canvas_item`` shaders. If 2D HDR is
  598. disabled, ``source_color`` will keep working correctly in ``canvas_item``
  599. shaders, so it's recommend to use it either way.
  600. Full list of hints below:
  601. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  602. | Type | Hint | Description |
  603. +======================+==================================================+=============================================================================+
  604. | **vec3, vec4** | source_color | Used as color. |
  605. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  606. | **int, float** | hint_range(min, max[, step]) | Restricted to values in a range (with min/max/step). |
  607. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  608. | **sampler2D** | source_color | Used as albedo color. |
  609. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  610. | **sampler2D** | hint_normal | Used as normalmap. |
  611. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  612. | **sampler2D** | hint_default_white | As value or albedo color, default to opaque white. |
  613. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  614. | **sampler2D** | hint_default_black | As value or albedo color, default to opaque black. |
  615. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  616. | **sampler2D** | hint_default_transparent | As value or albedo color, default to transparent black. |
  617. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  618. | **sampler2D** | hint_anisotropy | As flowmap, default to right. |
  619. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  620. | **sampler2D** | hint_roughness[_r, _g, _b, _a, _normal, _gray] | Used for roughness limiter on import (attempts reducing specular aliasing). |
  621. | | | ``_normal`` is a normal map that guides the roughness limiter, |
  622. | | | with roughness increasing in areas that have high-frequency detail. |
  623. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  624. | **sampler2D** | filter[_nearest, _linear][_mipmap][_anisotropic] | Enabled specified texture filtering. |
  625. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  626. | **sampler2D** | repeat[_enable, _disable] | Enabled texture repeating. |
  627. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  628. | **sampler2D** | hint_screen_texture | Texture is the screen texture. |
  629. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  630. | **sampler2D** | hint_depth_texture | Texture is the depth texture. |
  631. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  632. | **sampler2D** | hint_normal_roughness_texture | Texture is the normal roughness texture (only supported in Forward+). |
  633. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  634. GDScript uses different variable types than GLSL does, so when passing variables
  635. from GDScript to shaders, Godot converts the type automatically. Below is a
  636. table of the corresponding types:
  637. +----------------------+-------------------------+------------------------------------------------------------+
  638. | GLSL type | GDScript type | Notes |
  639. +======================+=========================+============================================================+
  640. | **bool** | **bool** | |
  641. +----------------------+-------------------------+------------------------------------------------------------+
  642. | **bvec2** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  643. | | | |
  644. | | | For example, a bvec2 of (bx, by) could be created in |
  645. | | | the following way: |
  646. | | | |
  647. | | | .. code-block:: gdscript |
  648. | | | |
  649. | | | bvec2_input: int = (int(bx)) | (int(by) << 1) |
  650. | | | |
  651. +----------------------+-------------------------+------------------------------------------------------------+
  652. | **bvec3** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  653. +----------------------+-------------------------+------------------------------------------------------------+
  654. | **bvec4** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  655. +----------------------+-------------------------+------------------------------------------------------------+
  656. | **int** | **int** | |
  657. +----------------------+-------------------------+------------------------------------------------------------+
  658. | **ivec2** | **Vector2i** | |
  659. +----------------------+-------------------------+------------------------------------------------------------+
  660. | **ivec3** | **Vector3i** | |
  661. +----------------------+-------------------------+------------------------------------------------------------+
  662. | **ivec4** | **Vector4i** | |
  663. +----------------------+-------------------------+------------------------------------------------------------+
  664. | **uint** | **int** | |
  665. +----------------------+-------------------------+------------------------------------------------------------+
  666. | **uvec2** | **Vector2i** | |
  667. +----------------------+-------------------------+------------------------------------------------------------+
  668. | **uvec3** | **Vector3i** | |
  669. +----------------------+-------------------------+------------------------------------------------------------+
  670. | **uvec4** | **Vector4i** | |
  671. +----------------------+-------------------------+------------------------------------------------------------+
  672. | **float** | **float** | |
  673. +----------------------+-------------------------+------------------------------------------------------------+
  674. | **vec2** | **Vector2** | |
  675. +----------------------+-------------------------+------------------------------------------------------------+
  676. | **vec3** | **Vector3**, **Color** | When Color is used, it will be interpreted as (r, g, b). |
  677. +----------------------+-------------------------+------------------------------------------------------------+
  678. | **vec4** | **Vector4**, **Color**, | When Color is used, it will be interpreted as (r, g, b, a).|
  679. | | **Rect2**, **Plane**, | |
  680. | | **Quaternion** | When Rect2 is used, it will be interpreted as |
  681. | | | (position.x, position.y, size.x, size.y). |
  682. | | | |
  683. | | | When Plane is used it will be interpreted as |
  684. | | | (normal.x, normal.y, normal.z, d). |
  685. | | | |
  686. | | | |
  687. +----------------------+-------------------------+------------------------------------------------------------+
  688. | **mat2** | **Transform2D** | |
  689. | | | |
  690. +----------------------+-------------------------+------------------------------------------------------------+
  691. | **mat3** | **Basis** | |
  692. +----------------------+-------------------------+------------------------------------------------------------+
  693. | **mat4** | **Projection**, | When a Transform3D is used, the w Vector is set to the |
  694. | | **Transform3D** | identity. |
  695. +----------------------+-------------------------+------------------------------------------------------------+
  696. | **sampler2D** | **Texture2D** | |
  697. +----------------------+-------------------------+------------------------------------------------------------+
  698. | **isampler2D** | **Texture2D** | |
  699. +----------------------+-------------------------+------------------------------------------------------------+
  700. | **usampler2D** | **Texture2D** | |
  701. +----------------------+-------------------------+------------------------------------------------------------+
  702. | **sampler2DArray** | **Texture2DArray** | |
  703. +----------------------+-------------------------+------------------------------------------------------------+
  704. | **isampler2DArray** | **Texture2DArray** | |
  705. +----------------------+-------------------------+------------------------------------------------------------+
  706. | **usampler2DArray** | **Texture2DArray** | |
  707. +----------------------+-------------------------+------------------------------------------------------------+
  708. | **sampler3D** | **Texture3D** | |
  709. +----------------------+-------------------------+------------------------------------------------------------+
  710. | **isampler3D** | **Texture3D** | |
  711. +----------------------+-------------------------+------------------------------------------------------------+
  712. | **usampler3D** | **Texture3D** | |
  713. +----------------------+-------------------------+------------------------------------------------------------+
  714. | **samplerCube** | **Cubemap** | See :ref:`doc_importing_images_changing_import_type` for |
  715. | | | instructions on importing cubemaps for use in Godot. |
  716. +----------------------+-------------------------+------------------------------------------------------------+
  717. | **samplerCubeArray** | **CubemapArray** | Only supported in Forward+ and Mobile, not Compatibility. |
  718. +----------------------+-------------------------+------------------------------------------------------------+
  719. .. note:: Be careful when setting shader uniforms from GDScript, no error will
  720. be thrown if the type does not match. Your shader will just exhibit
  721. undefined behavior.
  722. .. warning::
  723. 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.
  724. Uniforms can also be assigned default values:
  725. .. code-block:: glsl
  726. shader_type spatial;
  727. uniform vec4 some_vector = vec4(0.0);
  728. uniform vec4 some_color : source_color = vec4(1.0);
  729. Note that when adding a default value and a hint, the default value goes after the hint.
  730. If you need to make multiple uniforms to be grouped in the specific category of an inspector, you can use a `group_uniform` keyword like:
  731. .. code-block:: glsl
  732. group_uniforms MyGroup;
  733. uniform sampler2D test;
  734. You can close the group by using:
  735. .. code-block:: glsl
  736. group_uniforms;
  737. The syntax also supports subgroups (it's not mandatory to declare the base group before this):
  738. .. code-block:: glsl
  739. group_uniforms MyGroup.MySubgroup;
  740. .. _doc_shading_language_global_uniforms:
  741. Global uniforms
  742. ~~~~~~~~~~~~~~~
  743. Sometimes, you want to modify a parameter in many different shaders at once.
  744. With a regular uniform, this takes a lot of work as all these shaders need to be
  745. tracked and the uniform needs to be set for each of them. Global uniforms allow
  746. you to create and update uniforms that will be available in all shaders, in
  747. every shader type (``canvas_item``, ``spatial``, ``particles``, ``sky`` and
  748. ``fog``).
  749. Global uniforms are especially useful for environmental effects that affect many
  750. objects in a scene, like having foliage bend when the player is nearby, or having
  751. objects move with the wind.
  752. To create a global uniform, open the **Project Settings** then go to the
  753. **Shader Globals** tab. Specify a name for the uniform (case-sensitive) and a
  754. type, then click **Add** in the top-right corner of the dialog. You can then
  755. edit the value assigned to the uniform by clicking the value in the list of
  756. uniforms:
  757. .. figure:: img/shading_language_adding_global_uniforms.webp
  758. :align: center
  759. :alt: Adding a global uniform in the Shader Globals tab of the Project Settings
  760. Adding a global uniform in the Shader Globals tab of the Project Settings
  761. After creating a global uniform, you can use it in a shader as follows:
  762. .. code-block:: glsl
  763. shader_type canvas_item;
  764. global uniform vec4 my_color;
  765. void fragment() {
  766. COLOR = my_color.rgb;
  767. }
  768. Note that the global uniform *must* exist in the Project Settings at the time
  769. the shader is saved, or compilation will fail. While you can assign a default
  770. value using ``global uniform vec4 my_color = ...`` in the shader code, it will
  771. be ignored as the global uniform must always be defined in the Project Settings
  772. anyway.
  773. To change the value of a global uniform at run-time, use the
  774. :ref:`RenderingServer.global_shader_parameter_set <class_RenderingServer_method_global_shader_parameter_set>`
  775. method in a script:
  776. .. code-block:: gdscript
  777. RenderingServer.global_shader_parameter_set("my_color", Color(0.3, 0.6, 1.0))
  778. Assigning global uniform values can be done as many times as desired without
  779. impacting performance, as setting data doesn't require synchronization between
  780. the CPU and GPU.
  781. You can also add or remove global uniforms at run-time:
  782. .. code-block:: gdscript
  783. RenderingServer.global_shader_parameter_add("my_color", RenderingServer.GLOBAL_VAR_TYPE_COLOR, Color(0.3, 0.6, 1.0))
  784. RenderingServer.global_shader_parameter_remove("my_color")
  785. Adding or removing global uniforms at run-time has a performance cost, although
  786. it's not as pronounced compared to getting global uniform values from a script
  787. (see the warning below).
  788. .. warning::
  789. While you *can* query the value of a global uniform at run-time in a script
  790. using ``RenderingServer.global_shader_parameter_get("uniform_name")``, this
  791. has a large performance penalty as the rendering thread needs to synchronize
  792. with the calling thread.
  793. Therefore, it's not recommended to read global shader uniform values
  794. continuously in a script. If you need to read values in a script after
  795. setting them, consider creating an :ref:`autoload <doc_singletons_autoload>`
  796. where you store the values you need to query at the same time you're setting
  797. them as global uniforms.
  798. .. _doc_shading_language_per_instance_uniforms:
  799. Per-instance uniforms
  800. ~~~~~~~~~~~~~~~~~~~~~
  801. .. note::
  802. Per-instance uniforms are only available in ``spatial`` (3D) shaders.
  803. .. note::
  804. Per-instance uniforms are not supported when using the Compatibility renderer.
  805. Sometimes, you want to modify a parameter on each node using the material. As an
  806. example, in a forest full of trees, when you want each tree to have a slightly
  807. different color that is editable by hand. Without per-instance uniforms, this
  808. requires creating a unique material for each tree (each with a slightly
  809. different hue). This makes material management more complex, and also has a
  810. performance overhead due to the scene requiring more unique material instances.
  811. Vertex colors could also be used here, but they'd require creating unique copies
  812. of the mesh for each different color, which also has a performance overhead.
  813. Per-instance uniforms are set on each GeometryInstance3D, rather than on each
  814. Material instance. Take this into account when working with meshes that have
  815. multiple materials assigned to them, or MultiMesh setups.
  816. .. code-block:: glsl
  817. shader_type spatial;
  818. // Provide a hint to edit as a color. Optionally, a default value can be provided.
  819. // If no default value is provided, the type's default is used (e.g. opaque black for colors).
  820. instance uniform vec4 my_color : source_color = vec4(1.0, 0.5, 0.0, 1.0);
  821. void fragment() {
  822. ALBEDO = my_color.rgb;
  823. }
  824. After saving the shader, you can change the per-instance uniform's value using
  825. the inspector:
  826. .. figure:: img/shading_language_per_instance_uniforms_inspector.webp
  827. :align: center
  828. :alt: Setting a per-instance uniform's value in the GeometryInstance3D section of the inspector
  829. Setting a per-instance uniform's value in the GeometryInstance3D section of the inspector
  830. Per-instance uniform values can also be set at run-time using
  831. :ref:`set_instance_shader_parameter <class_GeometryInstance3D_method_set_instance_shader_parameter>`
  832. method on a node that inherits from :ref:`class_GeometryInstance3D`:
  833. .. code-block:: gdscript
  834. $MeshInstance3D.set_instance_shader_parameter("my_color", Color(0.3, 0.6, 1.0))
  835. When using per-instance uniforms, there are some restrictions you should be aware of:
  836. - **Per-instance uniforms do not support textures**, only regular scalar and
  837. vector types. As a workaround, you can pass a texture array as a regular
  838. uniform, then pass the index of the texture to be drawn using a per-instance
  839. uniform.
  840. - There is a practical maximum limit of 16 instance uniforms per shader.
  841. - If your mesh uses multiple materials, the parameters for the first mesh
  842. material found will "win" over the subsequent ones, unless they have the same
  843. name, index *and* type. In this case, all parameters are affected correctly.
  844. - If you run into the above situation, you can avoid clashes by manually
  845. specifying the index (0-15) of the instance uniform by using the
  846. ``instance_index`` hint:
  847. .. code-block:: glsl
  848. instance uniform vec4 my_color : source_color, instance_index(5);
  849. Built-in variables
  850. ------------------
  851. A large number of built-in variables are available, like ``UV``, ``COLOR`` and ``VERTEX``. What variables are available depends on the type of shader (``spatial``, ``canvas_item`` or ``particle``) and the function used (``vertex``, ``fragment`` or ``light``).
  852. For a list of the built-in variables that are available, please see the corresponding pages:
  853. - :ref:`Spatial shaders <doc_spatial_shader>`
  854. - :ref:`Canvas item shaders <doc_canvas_item_shader>`
  855. - :ref:`Particle shaders <doc_particle_shader>`
  856. - :ref:`Sky shaders <doc_sky_shader>`
  857. - :ref:`Fog shaders <doc_fog_shader>`
  858. Built-in functions
  859. ------------------
  860. A large number of built-in functions are supported, conforming to GLSL ES 3.0.
  861. When vec_type (float), vec_int_type, vec_uint_type, vec_bool_type nomenclature
  862. is used, it can be scalar or vector.
  863. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  864. | Function | Description / Return value |
  865. +=============================================================================+=====================================================================+
  866. | vec_type **radians** (vec_type degrees) | Convert degrees to radians. |
  867. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  868. | vec_type **degrees** (vec_type radians) | Convert radians to degrees. |
  869. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  870. | vec_type **sin** (vec_type x) | Sine. |
  871. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  872. | vec_type **cos** (vec_type x) | Cosine. |
  873. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  874. | vec_type **tan** (vec_type x) | Tangent. |
  875. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  876. | vec_type **asin** (vec_type x) | Arcsine. |
  877. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  878. | vec_type **acos** (vec_type x) | Arccosine. |
  879. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  880. | vec_type **atan** (vec_type y_over_x) | Arctangent. |
  881. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  882. | vec_type **atan** (vec_type y, vec_type x) | Arctangent. |
  883. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  884. | vec_type **sinh** (vec_type x) | Hyperbolic sine. |
  885. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  886. | vec_type **cosh** (vec_type x) | Hyperbolic cosine. |
  887. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  888. | vec_type **tanh** (vec_type x) | Hyperbolic tangent. |
  889. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  890. | vec_type **asinh** (vec_type x) | Inverse hyperbolic sine. |
  891. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  892. | vec_type **acosh** (vec_type x) | Inverse hyperbolic cosine. |
  893. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  894. | vec_type **atanh** (vec_type x) | Inverse hyperbolic tangent. |
  895. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  896. | vec_type **pow** (vec_type x, vec_type y) | Power (undefined if ``x`` < 0 or if ``x`` == 0 and ``y`` <= 0). |
  897. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  898. | vec_type **exp** (vec_type x) | Base-e exponential. |
  899. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  900. | vec_type **exp2** (vec_type x) | Base-2 exponential. |
  901. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  902. | vec_type **log** (vec_type x) | Natural logarithm. |
  903. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  904. | vec_type **log2** (vec_type x) | Base-2 logarithm. |
  905. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  906. | vec_type **sqrt** (vec_type x) | Square root. |
  907. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  908. | vec_type **inversesqrt** (vec_type x) | Inverse square root. |
  909. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  910. | vec_type **abs** (vec_type x) | Absolute value (returns positive value if negative). |
  911. | | |
  912. | ivec_type **abs** (ivec_type x) | |
  913. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  914. | vec_type **sign** (vec_type x) | Sign (returns ``1.0`` if positive, ``-1.0`` if negative, |
  915. | | ``0.0`` if zero). |
  916. | ivec_type **sign** (ivec_type x) | |
  917. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  918. | vec_type **floor** (vec_type x) | Round to the integer below. |
  919. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  920. | vec_type **round** (vec_type x) | Round to the nearest integer. |
  921. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  922. | vec_type **roundEven** (vec_type x) | Round to the nearest even integer. |
  923. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  924. | vec_type **trunc** (vec_type x) | Truncation. |
  925. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  926. | vec_type **ceil** (vec_type x) | Round to the integer above. |
  927. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  928. | vec_type **fract** (vec_type x) | Fractional (returns ``x - floor(x)``). |
  929. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  930. | vec_type **mod** (vec_type x, vec_type y) | Modulo (division remainder). |
  931. | | |
  932. | vec_type **mod** (vec_type x, float y) | |
  933. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  934. | vec_type **modf** (vec_type x, out vec_type i) | Fractional of ``x``, with ``i`` as integer part. |
  935. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  936. | vec_type **min** (vec_type a, vec_type b) | Lowest value between ``a`` and ``b``. |
  937. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  938. | vec_type **max** (vec_type a, vec_type b) | Highest value between ``a`` and ``b``. |
  939. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  940. | vec_type **clamp** (vec_type x, vec_type min, vec_type max) | Clamp ``x`` between ``min`` and ``max`` (inclusive). |
  941. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  942. | float **mix** (float a, float b, float c) | Linear interpolate between ``a`` and ``b`` by ``c``. |
  943. | | |
  944. | vec_type **mix** (vec_type a, vec_type b, float c) | |
  945. | | |
  946. | vec_type **mix** (vec_type a, vec_type b, bvec_type c) | |
  947. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  948. | vec_type **fma** (vec_type a, vec_type b, vec_type c) | Performs a fused multiply-add operation: ``(a * b + c)`` |
  949. | | (faster than doing it manually). |
  950. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  951. | vec_type **step** (vec_type a, vec_type b) | ``b[i] < a[i] ? 0.0 : 1.0``. |
  952. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  953. | vec_type **step** (float a, vec_type b) | ``b[i] < a ? 0.0 : 1.0``. |
  954. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  955. | vec_type **smoothstep** (vec_type a, vec_type b, vec_type c) | Hermite interpolate between ``a`` and ``b`` by ``c``. |
  956. | | |
  957. | vec_type **smoothstep** (float a, float b, vec_type c) | |
  958. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  959. | bvec_type **isnan** (vec_type x) | Returns ``true`` if scalar or vector component is ``NaN``. |
  960. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  961. | bvec_type **isinf** (vec_type x) | Returns ``true`` if scalar or vector component is ``INF``. |
  962. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  963. | ivec_type **floatBitsToInt** (vec_type x) | Float->Int bit copying, no conversion. |
  964. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  965. | uvec_type **floatBitsToUint** (vec_type x) | Float->UInt bit copying, no conversion. |
  966. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  967. | vec_type **intBitsToFloat** (ivec_type x) | Int->Float bit copying, no conversion. |
  968. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  969. | vec_type **uintBitsToFloat** (uvec_type x) | UInt->Float bit copying, no conversion. |
  970. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  971. | float **length** (vec_type x) | Vector length. |
  972. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  973. | float **distance** (vec_type a, vec_type b) | Distance between vectors i.e ``length(a - b)``. |
  974. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  975. | float **dot** (vec_type a, vec_type b) | Dot product. |
  976. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  977. | vec3 **cross** (vec3 a, vec3 b) | Cross product. |
  978. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  979. | vec_type **normalize** (vec_type x) | Normalize to unit length. |
  980. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  981. | vec3 **reflect** (vec3 I, vec3 N) | Reflect. |
  982. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  983. | vec3 **refract** (vec3 I, vec3 N, float eta) | Refract. |
  984. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  985. | vec_type **faceforward** (vec_type N, vec_type I, vec_type Nref) | If ``dot(Nref, I)`` < 0, return ``N``, otherwise ``-N``. |
  986. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  987. | mat_type **matrixCompMult** (mat_type x, mat_type y) | Matrix component multiplication. |
  988. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  989. | mat_type **outerProduct** (vec_type column, vec_type row) | Matrix outer product. |
  990. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  991. | mat_type **transpose** (mat_type m) | Transpose matrix. |
  992. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  993. | float **determinant** (mat_type m) | Matrix determinant. |
  994. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  995. | mat_type **inverse** (mat_type m) | Inverse matrix. |
  996. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  997. | bvec_type **lessThan** (vec_type x, vec_type y) | Bool vector comparison on < int/uint/float vectors. |
  998. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  999. | bvec_type **greaterThan** (vec_type x, vec_type y) | Bool vector comparison on > int/uint/float vectors. |
  1000. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1001. | bvec_type **lessThanEqual** (vec_type x, vec_type y) | Bool vector comparison on <= int/uint/float vectors. |
  1002. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1003. | bvec_type **greaterThanEqual** (vec_type x, vec_type y) | Bool vector comparison on >= int/uint/float vectors. |
  1004. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1005. | bvec_type **equal** (vec_type x, vec_type y) | Bool vector comparison on == int/uint/float vectors. |
  1006. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1007. | bvec_type **notEqual** (vec_type x, vec_type y) | Bool vector comparison on != int/uint/float vectors. |
  1008. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1009. | bool **any** (bvec_type x) | ``true`` if any component is ``true``, ``false`` otherwise. |
  1010. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1011. | bool **all** (bvec_type x) | ``true`` if all components are ``true``, ``false`` otherwise. |
  1012. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1013. | bvec_type **not** (bvec_type x) | Invert boolean vector. |
  1014. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1015. | ivec2 **textureSize** (gsampler2D s, int lod) | Get the size of a texture. |
  1016. | | |
  1017. | ivec3 **textureSize** (gsampler2DArray s, int lod) | The LOD defines which mipmap level is used. An LOD value of ``0`` |
  1018. | | will use the full resolution texture. |
  1019. | ivec3 **textureSize** (gsampler3D s, int lod) | |
  1020. | | |
  1021. | ivec2 **textureSize** (samplerCube s, int lod) | |
  1022. | | |
  1023. | ivec2 **textureSize** (samplerCubeArray s, int lod) | |
  1024. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1025. | vec2 **textureQueryLod** (gsampler2D s, vec2 p) | Compute the level-of-detail that would be used to sample from a |
  1026. | | texture. The ``x`` component of the resulted value is the mipmap |
  1027. | vec3 **textureQueryLod** (gsampler2DArray s, vec2 p) | array that would be accessed. The ``y`` component is computed |
  1028. | | level-of-detail relative to the base level (regardless of the |
  1029. | vec2 **textureQueryLod** (gsampler3D s, vec3 p) | mipmap levels of the texture). |
  1030. | | |
  1031. | vec2 **textureQueryLod** (samplerCube s, vec3 p) | |
  1032. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1033. | int **textureQueryLevels** (gsampler2D s) | Get the number of accessible mipmap levels of a texture. |
  1034. | | |
  1035. | int **textureQueryLevels** (gsampler2DArray s) | If the texture is unassigned to a sampler, ``1`` is returned (Godot |
  1036. | | always internally assigns a texture even to an empty sampler). |
  1037. | int **textureQueryLevels** (gsampler3D s) | |
  1038. | | |
  1039. | int **textureQueryLevels** (samplerCube s) | |
  1040. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1041. | gvec4_type **texture** (gsampler2D s, vec2 p [, float bias]) | Perform a texture read. |
  1042. | | |
  1043. | gvec4_type **texture** (gsampler2DArray s, vec3 p [, float bias]) | |
  1044. | | |
  1045. | gvec4_type **texture** (gsampler3D s, vec3 p [, float bias]) | |
  1046. | | |
  1047. | vec4 **texture** (samplerCube s, vec3 p [, float bias]) | |
  1048. | | |
  1049. | vec4 **texture** (samplerCubeArray s, vec4 p [, float bias]) | |
  1050. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1051. | gvec4_type **textureProj** (gsampler2D s, vec3 p [, float bias]) | Perform a texture read with projection. |
  1052. | | |
  1053. | gvec4_type **textureProj** (gsampler2D s, vec4 p [, float bias]) | |
  1054. | | |
  1055. | gvec4_type **textureProj** (gsampler3D s, vec4 p [, float bias]) | |
  1056. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1057. | gvec4_type **textureLod** (gsampler2D s, vec2 p, float lod) | Perform a texture read at custom mipmap. |
  1058. | | |
  1059. | gvec4_type **textureLod** (gsampler2DArray s, vec3 p, float lod) | The LOD defines which mipmap level is used. An LOD value of ``0.0`` |
  1060. | | will use the full resolution texture. If the texture lacks mipmaps, |
  1061. | gvec4_type **textureLod** (gsampler3D s, vec3 p, float lod) | all LOD values will act like ``0.0``. |
  1062. | | |
  1063. | vec4 **textureLod** (samplerCube s, vec3 p, float lod) | |
  1064. | | |
  1065. | vec4 **textureLod** (samplerCubeArray s, vec4 p, float lod) | |
  1066. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1067. | gvec4_type **textureProjLod** (gsampler2D s, vec3 p, float lod) | Performs a texture read with projection/LOD. |
  1068. | | |
  1069. | gvec4_type **textureProjLod** (gsampler2D s, vec4 p, float lod) | The LOD defines which mipmap level is used. An LOD value of ``0.0`` |
  1070. | | will use the full resolution texture. If the texture lacks mipmaps, |
  1071. | gvec4_type **textureProjLod** (gsampler3D s, vec4 p, float lod) | all LOD values will act like ``0.0``. |
  1072. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1073. | gvec4_type **textureGrad** (gsampler2D s, vec2 p, vec2 dPdx, | Performs a texture read with explicit gradients. |
  1074. | vec2 dPdy) | |
  1075. | | |
  1076. | gvec4_type **textureGrad** (gsampler2DArray s, vec3 p, vec2 dPdx, | |
  1077. | vec2 dPdy) | |
  1078. | | |
  1079. | gvec4_type **textureGrad** (gsampler3D s, vec3 p, vec2 dPdx, | |
  1080. | vec2 dPdy) | |
  1081. | | |
  1082. | vec4 **textureGrad** (samplerCube s, vec3 p, vec3 dPdx, vec3 dPdy) | |
  1083. | | |
  1084. | vec4 **textureGrad** (samplerCubeArray s, vec3 p, vec3 dPdx, | |
  1085. | vec3 dPdy) | |
  1086. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1087. | gvec4_type **textureProjGrad** (gsampler2D s, vec3 p, vec2 dPdx, vec2 dPdy) | Performs a texture read with projection/LOD and with explicit |
  1088. | | gradients. |
  1089. | gvec4_type **textureProjGrad** (gsampler2D s, vec4 p, vec2 dPdx, vec2 dPdy) | |
  1090. | | |
  1091. | gvec4_type **textureProjGrad** (gsampler3D s, vec4 p, vec3 dPdx, vec3 dPdy) | |
  1092. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1093. | gvec4_type **texelFetch** (gsampler2D s, ivec2 p, int lod) | Fetches a single texel using integer coordinates. |
  1094. | | |
  1095. | gvec4_type **texelFetch** (gsampler2DArray s, ivec3 p, int lod) | The LOD defines which mipmap level is used. An LOD value of ``0`` |
  1096. | | will use the full resolution texture. |
  1097. | gvec4_type **texelFetch** (gsampler3D s, ivec3 p, int lod) | |
  1098. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1099. | gvec4_type **textureGather** (gsampler2D s, vec2 p [, int comps]) | Gathers four texels from a texture. |
  1100. | | Use ``comps`` within range of 0..3 to |
  1101. | gvec4_type **textureGather** (gsampler2DArray s, vec3 p [, int comps]) | define which component (x, y, z, w) is returned. |
  1102. | | If ``comps`` is not provided: 0 (or x-component) is used. |
  1103. | vec4 **textureGather** (samplerCube s, vec3 p [, int comps]) | |
  1104. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1105. | vec_type **dFdx** (vec_type p) | Derivative in ``x`` using local differencing. |
  1106. | | Internally, can use either ``dFdxCoarse`` or ``dFdxFine``, but the |
  1107. | | decision for which to use is made by the GPU driver. |
  1108. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1109. | vec_type **dFdxCoarse** (vec_type p) | Calculates derivative with respect to ``x`` window coordinate using |
  1110. | | local differencing based on the value of ``p`` for the current |
  1111. | | fragment neighbour(s), and will possibly, but not necessarily, |
  1112. | | include the value for the current fragment. |
  1113. | | This function is not available on ``gl_compatibility`` profile. |
  1114. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1115. | vec_type **dFdxFine** (vec_type p) | Calculates derivative with respect to ``x`` window coordinate using |
  1116. | | local differencing based on the value of ``p`` for the current |
  1117. | | fragment and its immediate neighbour(s). |
  1118. | | This function is not available on ``gl_compatibility`` profile. |
  1119. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1120. | vec_type **dFdy** (vec_type p) | Derivative in ``y`` using local differencing. |
  1121. | | Internally, can use either ``dFdyCoarse`` or ``dFdyFine``, but the |
  1122. | | decision for which to use is made by the GPU driver. |
  1123. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1124. | vec_type **dFdyCoarse** (vec_type p) | Calculates derivative with respect to ``y`` window coordinate using |
  1125. | | local differencing based on the value of ``p`` for the current |
  1126. | | fragment neighbour(s), and will possibly, but not necessarily, |
  1127. | | include the value for the current fragment. |
  1128. | | This function is not available on ``gl_compatibility`` profile. |
  1129. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1130. | vec_type **dFdyFine** (vec_type p) | Calculates derivative with respect to ``y`` window coordinate using |
  1131. | | local differencing based on the value of ``p`` for the current |
  1132. | | fragment and its immediate neighbour(s). |
  1133. | | This function is not available on ``gl_compatibility`` profile. |
  1134. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1135. | vec_type **fwidth** (vec_type p) | Sum of absolute derivative in ``x`` and ``y``. |
  1136. | | This is the equivalent of using ``abs(dFdx(p)) + abs(dFdy(p))``. |
  1137. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1138. | vec_type **fwidthCoarse** (vec_type p) | Sum of absolute derivative in ``x`` and ``y``. |
  1139. | | This is the equivalent of using |
  1140. | | ``abs(dFdxCoarse(p)) + abs(dFdyCoarse(p))``. |
  1141. | | This function is not available on ``gl_compatibility`` profile. |
  1142. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1143. | vec_type **fwidthFine** (vec_type p) | Sum of absolute derivative in ``x`` and ``y``. |
  1144. | | This is the equivalent of using |
  1145. | | ``abs(dFdxFine(p)) + abs(dFdyFine(p))``. |
  1146. | | This function is not available on ``gl_compatibility`` profile. |
  1147. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1148. | uint **packHalf2x16** (vec2 v) | Convert two 32-bit floating-point numbers into 16-bit |
  1149. | | and pack them into a 32-bit unsigned integer and vice-versa. |
  1150. | vec2 **unpackHalf2x16** (uint v) | |
  1151. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1152. | uint **packUnorm2x16** (vec2 v) | Convert two 32-bit floating-point numbers (clamped |
  1153. | | within 0..1 range) into 16-bit and pack them |
  1154. | vec2 **unpackUnorm2x16** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1155. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1156. | uint **packSnorm2x16** (vec2 v) | Convert two 32-bit floating-point numbers (clamped |
  1157. | | within -1..1 range) into 16-bit and pack them |
  1158. | vec2 **unpackSnorm2x16** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1159. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1160. | uint **packUnorm4x8** (vec4 v) | Convert four 32-bit floating-point numbers (clamped |
  1161. | | within 0..1 range) into 8-bit and pack them |
  1162. | vec4 **unpackUnorm4x8** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1163. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1164. | uint **packSnorm4x8** (vec4 v) | Convert four 32-bit floating-point numbers (clamped |
  1165. | | within -1..1 range) into 8-bit and pack them |
  1166. | vec4 **unpackSnorm4x8** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1167. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1168. | ivec_type **bitfieldExtract** (ivec_type value, int offset, int bits) | Extracts a range of bits from an integer. |
  1169. | | |
  1170. | uvec_type **bitfieldExtract** (uvec_type value, int offset, int bits) | |
  1171. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1172. | ivec_type **bitfieldInsert** (ivec_type base, ivec_type insert, | Insert a range of bits into an integer. |
  1173. | int offset, int bits) | |
  1174. | | |
  1175. | uvec_type **bitfieldInsert** (uvec_type base, uvec_type insert, | |
  1176. | int offset, int bits) | |
  1177. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1178. | ivec_type **bitfieldReverse** (ivec_type value) | Reverse the order of bits in an integer. |
  1179. | | |
  1180. | uvec_type **bitfieldReverse** (uvec_type value) | |
  1181. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1182. | ivec_type **bitCount** (ivec_type value) | Counts the number of 1 bits in an integer. |
  1183. | | |
  1184. | uvec_type **bitCount** (uvec_type value) | |
  1185. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1186. | ivec_type **findLSB** (ivec_type value) | Find the index of the least significant bit set to 1 in an integer. |
  1187. | | |
  1188. | uvec_type **findLSB** (uvec_type value) | |
  1189. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1190. | ivec_type **findMSB** (ivec_type value) | Find the index of the most significant bit set to 1 in an integer. |
  1191. | | |
  1192. | uvec_type **findMSB** (uvec_type value) | |
  1193. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1194. | void **imulExtended** (ivec_type x, ivec_type y, out ivec_type msb, | Multiplies two 32-bit numbers and produce a 64-bit result. |
  1195. | out ivec_type lsb) | ``x`` - the first number. |
  1196. | | ``y`` - the second number. |
  1197. | void **umulExtended** (uvec_type x, uvec_type y, out uvec_type msb, | ``msb`` - will contain the most significant bits. |
  1198. | out uvec_type lsb) | ``lsb`` - will contain the least significant bits. |
  1199. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1200. | uvec_type **uaddCarry** (uvec_type x, uvec_type y, out uvec_type carry) | Adds two unsigned integers and generates carry. |
  1201. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1202. | uvec_type **usubBorrow** (uvec_type x, uvec_type y, out uvec_type borrow) | Subtracts two unsigned integers and generates borrow. |
  1203. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1204. | vec_type **ldexp** (vec_type x, out ivec_type exp) | Assemble a floating-point number from a value and exponent. |
  1205. | | |
  1206. | | If this product is too large to be represented in the |
  1207. | | floating-point type the result is undefined. |
  1208. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1209. | vec_type **frexp** (vec_type x, out ivec_type exp) | Splits a floating-point number(``x``) into significand |
  1210. | | (in the range of [0.5, 1.0]) and an integral exponent. |
  1211. | | |
  1212. | | For ``x`` equals zero the significand and exponent are both zero. |
  1213. | | For ``x`` of infinity or NaN, the results are undefined. |
  1214. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+