2
0

shading_language.rst 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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** | 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** | 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 floats. |
  77. +---------------------+---------------------------------------------------------------------------------+
  78. Casting
  79. ~~~~~~~
  80. Just like GLSL ES 3.0, implicit casting between scalars and vectors of the same
  81. size but different type is not allowed. Casting of types of different size is
  82. also not allowed. Conversion must be done explicitly via constructors.
  83. Example:
  84. .. code-block:: glsl
  85. float a = 2; // invalid
  86. float a = 2.0; // valid
  87. float a = float(2); // valid
  88. Default integer constants are signed, so casting is always needed to convert to
  89. unsigned:
  90. .. code-block:: glsl
  91. int a = 2; // valid
  92. uint a = 2; // invalid
  93. uint a = uint(2); // valid
  94. Members
  95. ~~~~~~~
  96. Individual scalar members of vector types are accessed via the "x", "y", "z" and
  97. "w" members. Alternatively, using "r", "g", "b" and "a" also works and is
  98. equivalent. Use whatever fits best for your needs.
  99. For matrices, use the ``m[row][column]`` indexing syntax to access each scalar,
  100. or ``m[idx]`` to access a vector by row index. For example, for accessing the y
  101. position of an object in a mat4 you use ``m[3][1]``.
  102. Constructing
  103. ~~~~~~~~~~~~
  104. Construction of vector types must always pass:
  105. .. code-block:: glsl
  106. // The required amount of scalars
  107. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  108. // Complementary vectors and/or scalars
  109. vec4 a = vec4(vec2(0.0, 1.0), vec2(2.0, 3.0));
  110. vec4 a = vec4(vec3(0.0, 1.0, 2.0), 3.0);
  111. // A single scalar for the whole vector
  112. vec4 a = vec4(0.0);
  113. Construction of matrix types requires vectors of the same dimension as the
  114. matrix. You can also build a diagonal matrix using ``matx(float)`` syntax.
  115. Accordingly, ``mat4(1.0)`` is an identity matrix.
  116. .. code-block:: glsl
  117. mat2 m2 = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0));
  118. mat3 m3 = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  119. mat4 identity = mat4(1.0);
  120. Matrices can also be built from a matrix of another dimension. There are two
  121. rules:
  122. 1. If a larger matrix is constructed from a smaller matrix, the additional rows
  123. and columns are set to the values they would have in an identity matrix.
  124. 2. If a smaller matrix is constructed from a larger matrix, the top, left
  125. submatrix of the larger matrix is used.
  126. .. code-block:: glsl
  127. mat3 basis = mat3(MODEL_MATRIX);
  128. mat4 m4 = mat4(basis);
  129. mat2 m2 = mat2(m4);
  130. Swizzling
  131. ~~~~~~~~~
  132. It is possible to obtain any combination of components in any order, as long as
  133. the result is another vector type (or scalar). This is easier shown than
  134. explained:
  135. .. code-block:: glsl
  136. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  137. vec3 b = a.rgb; // Creates a vec3 with vec4 components.
  138. vec3 b = a.ggg; // Also valid; creates a vec3 and fills it with a single vec4 component.
  139. vec3 b = a.bgr; // "b" will be vec3(2.0, 1.0, 0.0).
  140. vec3 b = a.xyz; // Also rgba, xyzw are equivalent.
  141. vec3 b = a.stp; // And stpq (for texture coordinates).
  142. float c = b.w; // Invalid, because "w" is not present in vec3 b.
  143. vec3 c = b.xrt; // Invalid, mixing different styles is forbidden.
  144. b.rrr = a.rgb; // Invalid, assignment with duplication.
  145. b.bgr = a.rgb; // Valid assignment. "b"'s "blue" component will be "a"'s "red" and vice versa.
  146. Precision
  147. ~~~~~~~~~
  148. It is possible to add precision modifiers to datatypes; use them for uniforms,
  149. variables, arguments and varyings:
  150. .. code-block:: glsl
  151. lowp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
  152. mediump vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
  153. highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (default)
  154. Using lower precision for some operations can speed up the math involved (at the
  155. cost of less precision). This is rarely needed in the vertex processor function
  156. (where full precision is needed most of the time), but is often useful in the
  157. fragment processor.
  158. Some architectures (mainly mobile) can benefit significantly from this, but
  159. there are downsides such as the additional overhead of conversion between
  160. precisions. Refer to the documentation of the target architecture for further
  161. information. In many cases, mobile drivers cause inconsistent or unexpected
  162. behavior and it is best to avoid specifying precision unless necessary.
  163. Arrays
  164. ------
  165. Arrays are containers for multiple variables of a similar type.
  166. Local arrays
  167. ~~~~~~~~~~~~
  168. Local arrays are declared in functions. They can use all of the allowed
  169. datatypes, except samplers. The array declaration follows a C-style syntax:
  170. ``[const] + [precision] + typename + identifier + [array size]``.
  171. .. code-block:: glsl
  172. void fragment() {
  173. float arr[3];
  174. }
  175. They can be initialized at the beginning like:
  176. .. code-block:: glsl
  177. float float_arr[3] = float[3] (1.0, 0.5, 0.0); // first constructor
  178. int int_arr[3] = int[] (2, 1, 0); // second constructor
  179. vec2 vec2_arr[3] = { vec2(1.0, 1.0), vec2(0.5, 0.5), vec2(0.0, 0.0) }; // third constructor
  180. bool bool_arr[] = { true, true, false }; // fourth constructor - size is defined automatically from the element count
  181. You can declare multiple arrays (even with different sizes) in one expression:
  182. .. code-block:: glsl
  183. float a[3] = float[3] (1.0, 0.5, 0.0),
  184. b[2] = { 1.0, 0.5 },
  185. c[] = { 0.7 },
  186. d = 0.0,
  187. e[5];
  188. To access an array element, use the indexing syntax:
  189. .. code-block:: glsl
  190. float arr[3];
  191. arr[0] = 1.0; // setter
  192. COLOR.r = arr[0]; // getter
  193. Arrays also have a built-in function ``.length()`` (not to be confused with the
  194. built-in ``length()`` function). It doesn't accept any parameters and will
  195. return the array's size.
  196. .. code-block:: glsl
  197. float arr[] = { 0.0, 1.0, 0.5, -1.0 };
  198. for (int i = 0; i < arr.length(); i++) {
  199. // ...
  200. }
  201. .. note::
  202. If you use an index either below 0 or greater than array size - the shader will
  203. crash and break rendering. To prevent this, use ``length()``, ``if``, or
  204. ``clamp()`` functions to ensure the index is between 0 and the array's
  205. length. Always carefully test and check your code. If you pass a constant
  206. expression or a number, the editor will check its bounds to prevent
  207. this crash.
  208. Global arrays
  209. ~~~~~~~~~~~~~
  210. You can declare arrays at global space like:
  211. .. code-block:: glsl
  212. shader_type spatial;
  213. const lowp vec3 v[1] = lowp vec3[1] ( vec3(0, 0, 1) );
  214. void fragment() {
  215. ALBEDO = v[0];
  216. }
  217. .. note::
  218. Global arrays have to be declared as global constants, otherwise they can be
  219. declared the same as local arrays.
  220. Constants
  221. ---------
  222. Use the ``const`` keyword before the variable declaration to make that variable
  223. immutable, which means that it cannot be modified. All basic types, except
  224. samplers can be declared as constants. Accessing and using a constant value is
  225. slightly faster than using a uniform. Constants must be initialized at their
  226. declaration.
  227. .. code-block:: glsl
  228. const vec2 a = vec2(0.0, 1.0);
  229. vec2 b;
  230. a = b; // invalid
  231. b = a; // valid
  232. Constants cannot be modified and additionally cannot have hints, but multiple of
  233. them (if they have the same type) can be declared in a single expression e.g
  234. .. code-block:: glsl
  235. const vec2 V1 = vec2(1, 1), V2 = vec2(2, 2);
  236. Similar to variables, arrays can also be declared with ``const``.
  237. .. code-block:: glsl
  238. const float arr[] = { 1.0, 0.5, 0.0 };
  239. arr[0] = 1.0; // invalid
  240. COLOR.r = arr[0]; // valid
  241. Constants can be declared both globally (outside of any function) or locally
  242. (inside a function). Global constants are useful when you want to have access to
  243. a value throughout your shader that does not need to be modified. Like uniforms,
  244. global constants are shared between all shader stages, but they are not
  245. accessible outside of the shader.
  246. .. code-block:: glsl
  247. shader_type spatial;
  248. const float PI = 3.14159265358979323846;
  249. Constants of the ``float`` type must be initialized using ``.`` notation after the
  250. decimal part or by using the scientific notation. The optional ``f`` post-suffix is
  251. also supported.
  252. .. code-block:: glsl
  253. float a = 1.0;
  254. float b = 1.0f; // same, using suffix for clarity
  255. float c = 1e-1; // gives 0.1 by using the scientific notation
  256. Constants of the ``uint`` (unsigned int) type must have a ``u`` suffix to differentiate them from signed integers.
  257. Alternatively, this can be done by using the ``uint(x)`` built-in conversion function.
  258. .. code-block:: glsl
  259. uint a = 1u;
  260. uint b = uint(1);
  261. Structs
  262. -------
  263. Structs are compound types which can be used for better abstraction of shader
  264. code. You can declare them at the global scope like:
  265. .. code-block:: glsl
  266. struct PointLight {
  267. vec3 position;
  268. vec3 color;
  269. float intensity;
  270. };
  271. After declaration, you can instantiate and initialize them like:
  272. .. code-block:: glsl
  273. void fragment()
  274. {
  275. PointLight light;
  276. light.position = vec3(0.0);
  277. light.color = vec3(1.0, 0.0, 0.0);
  278. light.intensity = 0.5;
  279. }
  280. Or use struct constructor for same purpose:
  281. .. code-block:: glsl
  282. PointLight light = PointLight(vec3(0.0), vec3(1.0, 0.0, 0.0), 0.5);
  283. Structs may contain other struct or array, you can also instance them as global
  284. constant:
  285. .. code-block:: glsl
  286. shader_type spatial;
  287. ...
  288. struct Scene {
  289. PointLight lights[2];
  290. };
  291. 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)));
  292. void fragment()
  293. {
  294. ALBEDO = scene.lights[0].color;
  295. }
  296. You can also pass them to functions:
  297. .. code-block:: glsl
  298. shader_type canvas_item;
  299. ...
  300. Scene construct_scene(PointLight light1, PointLight light2) {
  301. return Scene({light1, light2});
  302. }
  303. void fragment()
  304. {
  305. 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;
  306. }
  307. Operators
  308. ---------
  309. Godot shading language supports the same set of operators as GLSL ES 3.0. Below
  310. is the list of them in precedence order:
  311. +-------------+------------------------+------------------+
  312. | Precedence | Class | Operator |
  313. +-------------+------------------------+------------------+
  314. | 1 (highest) | parenthetical grouping | **()** |
  315. +-------------+------------------------+------------------+
  316. | 2 | unary | **+, -, !, ~** |
  317. +-------------+------------------------+------------------+
  318. | 3 | multiplicative | **/, \*, %** |
  319. +-------------+------------------------+------------------+
  320. | 4 | additive | **+, -** |
  321. +-------------+------------------------+------------------+
  322. | 5 | bit-wise shift | **<<, >>** |
  323. +-------------+------------------------+------------------+
  324. | 6 | relational | **<, >, <=, >=** |
  325. +-------------+------------------------+------------------+
  326. | 7 | equality | **==, !=** |
  327. +-------------+------------------------+------------------+
  328. | 8 | bit-wise AND | **&** |
  329. +-------------+------------------------+------------------+
  330. | 9 | bit-wise exclusive OR | **^** |
  331. +-------------+------------------------+------------------+
  332. | 10 | bit-wise inclusive OR | **|** |
  333. +-------------+------------------------+------------------+
  334. | 11 | logical AND | **&&** |
  335. +-------------+------------------------+------------------+
  336. | 12 (lowest) | logical inclusive OR | **||** |
  337. +-------------+------------------------+------------------+
  338. Flow control
  339. ------------
  340. Godot Shading language supports the most common types of flow control:
  341. .. code-block:: glsl
  342. // `if` and `else`.
  343. if (cond) {
  344. } else {
  345. }
  346. // Ternary operator.
  347. // This is an expression that behaves like `if`/`else` and returns the value.
  348. // If `cond` evaluates to `true`, `result` will be `9`.
  349. // Otherwise, `result` will be `5`.
  350. int result = cond ? 9 : 5;
  351. // `switch`.
  352. switch (i) { // `i` should be a signed integer expression.
  353. case -1:
  354. break;
  355. case 0:
  356. return; // `break` or `return` to avoid running the next `case`.
  357. case 1: // Fallthrough (no `break` or `return`): will run the next `case`.
  358. case 2:
  359. break;
  360. //...
  361. default: // Only run if no `case` above matches. Optional.
  362. break;
  363. }
  364. // `for` loop. Best used when the number of elements to iterate on
  365. // is known in advance.
  366. for (int i = 0; i < 10; i++) {
  367. }
  368. // `while` loop. Best used when the number of elements to iterate on
  369. // is not known in advance.
  370. while (cond) {
  371. }
  372. // `do while`. Like `while`, but always runs at least once even if `cond`
  373. // never evaluates to `true`.
  374. do {
  375. } while (cond);
  376. Keep in mind that, in modern GPUs, an infinite loop can exist and can freeze
  377. your application (including editor). Godot can't protect you from this, so be
  378. careful not to make this mistake!
  379. Also, when comparing floating-point values against a number, make sure to
  380. compare them against a *range* instead of an exact number.
  381. A comparison like ``if (value == 0.3)`` may not evaluate to ``true``.
  382. Floating-point math is often approximate and can defy expectations. It can also
  383. behave differently depending on the hardware.
  384. **Don't** do this.
  385. .. code-block:: glsl
  386. float value = 0.1 + 0.2;
  387. // May not evaluate to `true`!
  388. if (value == 0.3) {
  389. // ...
  390. }
  391. Instead, always perform a range comparison with an epsilon value. The larger the
  392. floating-point number (and the less precise the floating-point number, the
  393. larger the epsilon value should be.
  394. .. code-block:: glsl
  395. const float EPSILON = 0.0001;
  396. if (value >= 0.3 - EPSILON && value <= 0.3 + EPSILON) {
  397. // ...
  398. }
  399. See `floating-point-gui.de <https://floating-point-gui.de/>`__ for more
  400. information.
  401. .. warning::
  402. When exporting a GLES2 project to HTML5, WebGL 1.0 will be used. WebGL 1.0
  403. doesn't support dynamic loops, so shaders using those won't work there.
  404. Discarding
  405. ----------
  406. Fragment and light functions can use the **discard** keyword. If used, the
  407. fragment is discarded and nothing is written.
  408. Functions
  409. ---------
  410. It is possible to define functions in a Godot shader. They use the following
  411. syntax:
  412. .. code-block:: glsl
  413. ret_type func_name(args) {
  414. return ret_type; // if returning a value
  415. }
  416. // a more specific example:
  417. int sum2(int a, int b) {
  418. return a + b;
  419. }
  420. You can only use functions that have been defined above (higher in the editor)
  421. the function from which you are calling them.
  422. Function arguments can have special qualifiers:
  423. * **in**: Means the argument is only for reading (default).
  424. * **out**: Means the argument is only for writing.
  425. * **inout**: Means the argument is fully passed via reference.
  426. * **const**: Means the argument is a constant and cannot be changed, may be
  427. combined with **in** qualifier.
  428. Example below:
  429. .. code-block:: glsl
  430. void sum2(int a, int b, inout int result) {
  431. result = a + b;
  432. }
  433. Varyings
  434. ~~~~~~~~
  435. To send data from the vertex to the fragment (or light) processor function, *varyings* are
  436. used. They are set for every primitive vertex in the *vertex processor*, and the
  437. value is interpolated for every pixel in the *fragment processor*.
  438. .. code-block:: glsl
  439. shader_type spatial;
  440. varying vec3 some_color;
  441. void vertex() {
  442. some_color = NORMAL; // Make the normal the color.
  443. }
  444. void fragment() {
  445. ALBEDO = some_color;
  446. }
  447. void light() {
  448. DIFFUSE_LIGHT = some_color * 100; // optionally
  449. }
  450. Varying can also be an array:
  451. .. code-block:: glsl
  452. shader_type spatial;
  453. varying float var_arr[3];
  454. void vertex() {
  455. var_arr[0] = 1.0;
  456. var_arr[1] = 0.0;
  457. }
  458. void fragment() {
  459. ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
  460. }
  461. 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.
  462. .. code-block:: glsl
  463. shader_type spatial;
  464. varying vec3 some_light;
  465. void fragment() {
  466. some_light = ALBEDO * 100.0; // Make a shining light.
  467. }
  468. void light() {
  469. DIFFUSE_LIGHT = some_light;
  470. }
  471. Note that varying may not be assigned in custom functions or a *light processor* function like:
  472. .. code-block:: glsl
  473. shader_type spatial;
  474. varying float test;
  475. void foo() {
  476. test = 0.0; // Error.
  477. }
  478. void vertex() {
  479. test = 0.0;
  480. }
  481. void light() {
  482. test = 0.0; // Error too.
  483. }
  484. This limitation was introduced to prevent incorrect usage before initialization.
  485. Interpolation qualifiers
  486. ~~~~~~~~~~~~~~~~~~~~~~~~
  487. Certain values are interpolated during the shading pipeline. You can modify how
  488. these interpolations are done by using *interpolation qualifiers*.
  489. .. code-block:: glsl
  490. shader_type spatial;
  491. varying flat vec3 our_color;
  492. void vertex() {
  493. our_color = COLOR.rgb;
  494. }
  495. void fragment() {
  496. ALBEDO = our_color;
  497. }
  498. There are two possible interpolation qualifiers:
  499. +-------------------+---------------------------------------------------------------------------------+
  500. | Qualifier | Description |
  501. +===================+=================================================================================+
  502. | **flat** | The value is not interpolated. |
  503. +-------------------+---------------------------------------------------------------------------------+
  504. | **smooth** | The value is interpolated in a perspective-correct fashion. This is the default.|
  505. +-------------------+---------------------------------------------------------------------------------+
  506. Uniforms
  507. ~~~~~~~~
  508. Passing values to shaders is possible. These are global to the whole shader and
  509. are called *uniforms*. When a shader is later assigned to a material, the
  510. uniforms will appear as editable parameters in it. Uniforms can't be written
  511. from within the shader.
  512. .. note::
  513. Uniform arrays are not implemented yet.
  514. .. code-block:: glsl
  515. shader_type spatial;
  516. uniform float some_value;
  517. You can set uniforms in the editor in the material. Or you can set them through
  518. GDScript:
  519. ::
  520. material.set_shader_param("some_value", some_value)
  521. .. note:: The first argument to ``set_shader_param`` is the name of the uniform
  522. in the shader. It must match *exactly* to the name of the uniform in
  523. the shader or else it will not be recognized.
  524. Any GLSL type except for *void* can be a uniform. Additionally, Godot provides
  525. optional shader hints to make the compiler understand for what the uniform is
  526. used.
  527. .. code-block:: glsl
  528. shader_type spatial;
  529. uniform vec4 color : hint_color;
  530. uniform float amount : hint_range(0, 1);
  531. uniform vec4 other_color : hint_color = vec4(1.0);
  532. It's important to understand that textures that are supplied as color require
  533. hints for proper sRGB->linear conversion (i.e. ``hint_albedo``), as Godot's 3D
  534. engine renders in linear color space.
  535. Full list of hints below:
  536. +----------------------+------------------------------------------------+--------------------------------------+
  537. | Type | Hint | Description |
  538. +======================+================================================+======================================+
  539. | **vec4** | hint_color | Used as color. |
  540. +----------------------+------------------------------------------------+--------------------------------------+
  541. | **int, float** | hint_range(min, max[, step]) | Used as range (with min/max/step). |
  542. +----------------------+------------------------------------------------+--------------------------------------+
  543. | **sampler2D** | hint_albedo | Used as albedo color, default white. |
  544. +----------------------+------------------------------------------------+--------------------------------------+
  545. | **sampler2D** | hint_black_albedo | Used as albedo color, default black. |
  546. +----------------------+------------------------------------------------+--------------------------------------+
  547. | **sampler2D** | hint_normal | Used as normalmap. |
  548. +----------------------+------------------------------------------------+--------------------------------------+
  549. | **sampler2D** | hint_white | As value, default to white. |
  550. +----------------------+------------------------------------------------+--------------------------------------+
  551. | **sampler2D** | hint_black | As value, default to black. |
  552. +----------------------+------------------------------------------------+--------------------------------------+
  553. | **sampler2D** | hint_anisotropy | As flowmap, default to right. |
  554. +----------------------+------------------------------------------------+--------------------------------------+
  555. | **sampler2D** | hint_roughness[_r, _g, _b, _a, _normal, _gray] | |
  556. +----------------------+------------------------------------------------+--------------------------------------+
  557. | **sampler2D** | filter[_nearest, _linear][_mipmap][_aniso] | Enabled specified texture filtering. |
  558. +----------------------+------------------------------------------------+--------------------------------------+
  559. | **sampler2D** | repeat_[enable, disable] | Enabled texture repeating. |
  560. +----------------------+------------------------------------------------+--------------------------------------+
  561. GDScript uses different variable types than GLSL does, so when passing variables
  562. from GDScript to shaders, Godot converts the type automatically. Below is a
  563. table of the corresponding types:
  564. +-----------------+-----------+
  565. | GDScript type | GLSL type |
  566. +=================+===========+
  567. | **bool** | **bool** |
  568. +-----------------+-----------+
  569. | **int** | **int** |
  570. +-----------------+-----------+
  571. | **float** | **float** |
  572. +-----------------+-----------+
  573. | **Vector2** | **vec2** |
  574. +-----------------+-----------+
  575. | **Vector3** | **vec3** |
  576. +-----------------+-----------+
  577. | **Color** | **vec4** |
  578. +-----------------+-----------+
  579. | **Transform3D** | **mat4** |
  580. +-----------------+-----------+
  581. | **Transform2D** | **mat4** |
  582. +-----------------+-----------+
  583. .. note:: Be careful when setting shader uniforms from GDScript, no error will
  584. be thrown if the type does not match. Your shader will just exhibit
  585. undefined behavior.
  586. Uniforms can also be assigned default values:
  587. .. code-block:: glsl
  588. shader_type spatial;
  589. uniform vec4 some_vector = vec4(0.0);
  590. uniform vec4 some_color : hint_color = vec4(1.0);
  591. Built-in variables
  592. ------------------
  593. 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``).
  594. For a list of the built-in variables that are available, please see the corresponding pages:
  595. - :ref:`Spatial shaders <doc_spatial_shader>`
  596. - :ref:`Canvas item shaders <doc_canvas_item_shader>`
  597. - :ref:`Particle shaders <doc_particle_shader>`
  598. Built-in functions
  599. ------------------
  600. A large number of built-in functions are supported, conforming to GLSL ES 3.0.
  601. When vec_type (float), vec_int_type, vec_uint_type, vec_bool_type nomenclature
  602. is used, it can be scalar or vector.
  603. .. note:: For a list of the functions that are not available in the GLES2
  604. backend, please see the :ref:`Differences between GLES2 and GLES3 doc
  605. <doc_gles2_gles3_differences>`.
  606. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  607. | Function | Description / Return value |
  608. +===========================================================================+=====================================================================+
  609. | vec_type **radians** (vec_type degrees) | Convert degrees to radians. |
  610. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  611. | vec_type **degrees** (vec_type radians) | Convert radians to degrees. |
  612. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  613. | vec_type **sin** (vec_type x) | Sine. |
  614. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  615. | vec_type **cos** (vec_type x) | Cosine. |
  616. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  617. | vec_type **tan** (vec_type x) | Tangent. |
  618. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  619. | vec_type **asin** (vec_type x) | Arcsine. |
  620. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  621. | vec_type **acos** (vec_type x) | Arccosine. |
  622. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  623. | vec_type **atan** (vec_type y_over_x) | Arctangent. |
  624. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  625. | vec_type **atan** (vec_type y, vec_type x) | Arctangent. |
  626. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  627. | vec_type **sinh** (vec_type x) | Hyperbolic sine. |
  628. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  629. | vec_type **cosh** (vec_type x) | Hyperbolic cosine. |
  630. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  631. | vec_type **tanh** (vec_type x) | Hyperbolic tangent. |
  632. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  633. | vec_type **asinh** (vec_type x) | Inverse hyperbolic sine. |
  634. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  635. | vec_type **acosh** (vec_type x) | Inverse hyperbolic cosine. |
  636. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  637. | vec_type **atanh** (vec_type x) | Inverse hyperbolic tangent. |
  638. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  639. | vec_type **pow** (vec_type x, vec_type y) | Power (undefined if ``x`` < 0 or if ``x`` == 0 and ``y`` <= 0). |
  640. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  641. | vec_type **exp** (vec_type x) | Base-e exponential. |
  642. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  643. | vec_type **exp2** (vec_type x) | Base-2 exponential. |
  644. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  645. | vec_type **log** (vec_type x) | Natural logarithm. |
  646. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  647. | vec_type **log2** (vec_type x) | Base-2 logarithm. |
  648. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  649. | vec_type **sqrt** (vec_type x) | Square root. |
  650. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  651. | vec_type **inversesqrt** (vec_type x) | Inverse square root. |
  652. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  653. | vec_type **abs** (vec_type x) | Absolute value (returns positive value if negative). |
  654. | | |
  655. | ivec_type **abs** (ivec_type x) | |
  656. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  657. | vec_type **sign** (vec_type x) | Sign (returns ``1.0`` if positive, ``-1.0`` if negative, |
  658. | | ``0.0`` if zero). |
  659. | ivec_type **sign** (ivec_type x) | |
  660. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  661. | vec_type **floor** (vec_type x) | Round to the integer below. |
  662. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  663. | vec_type **round** (vec_type x) | Round to the nearest integer. |
  664. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  665. | vec_type **roundEven** (vec_type x) | Round to the nearest even integer. |
  666. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  667. | vec_type **trunc** (vec_type x) | Truncation. |
  668. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  669. | vec_type **ceil** (vec_type x) | Round to the integer above. |
  670. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  671. | vec_type **fract** (vec_type x) | Fractional (returns ``x - floor(x)``). |
  672. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  673. | vec_type **mod** (vec_type x, vec_type y) | Modulo (division remainder). |
  674. | | |
  675. | vec_type **mod** (vec_type x, float y) | |
  676. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  677. | vec_type **modf** (vec_type x, out vec_type i) | Fractional of ``x``, with ``i`` as integer part. |
  678. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  679. | vec_type **min** (vec_type a, vec_type b) | Lowest value between ``a`` and ``b``. |
  680. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  681. | vec_type **max** (vec_type a, vec_type b) | Highest value between ``a`` and ``b``. |
  682. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  683. | vec_type **clamp** (vec_type x, vec_type min, vec_type max) | Clamp ``x`` between ``min`` and ``max`` (inclusive). |
  684. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  685. | float **mix** (float a, float b, float c) | Linear interpolate between ``a`` and ``b`` by ``c``. |
  686. | | |
  687. | vec_type **mix** (vec_type a, vec_type b, float c) | |
  688. | | |
  689. | vec_type **mix** (vec_type a, vec_type b, bvec_type c) | |
  690. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  691. | vec_type **fma** (vec_type a, vec_type b, vec_type c) | Performs a fused multiply-add operation: ``(a * b + c)`` |
  692. | | (faster than doing it manually). |
  693. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  694. | vec_type **step** (vec_type a, vec_type b) | ``b[i] < a[i] ? 0.0 : 1.0``. |
  695. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  696. | vec_type **step** (float a, vec_type b) | ``b[i] < a ? 0.0 : 1.0``. |
  697. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  698. | vec_type **smoothstep** (vec_type a, vec_type b, vec_type c) | Hermite interpolate between ``a`` and ``b`` by ``c``. |
  699. | | |
  700. | vec_type **smoothstep** (float a, float b, vec_type c) | |
  701. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  702. | bvec_type **isnan** (vec_type x) | Returns ``true`` if scalar or vector component is ``NaN``. |
  703. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  704. | bvec_type **isinf** (vec_type x) | Returns ``true`` if scalar or vector component is ``INF``. |
  705. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  706. | ivec_type **floatBitsToInt** (vec_type x) | Float->Int bit copying, no conversion. |
  707. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  708. | uvec_type **floatBitsToUint** (vec_type x) | Float->UInt bit copying, no conversion. |
  709. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  710. | vec_type **intBitsToFloat** (ivec_type x) | Int->Float bit copying, no conversion. |
  711. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  712. | vec_type **uintBitsToFloat** (uvec_type x) | UInt->Float bit copying, no conversion. |
  713. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  714. | float **length** (vec_type x) | Vector length. |
  715. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  716. | float **distance** (vec_type a, vec_type b) | Distance between vectors i.e ``length(a - b)``. |
  717. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  718. | float **dot** (vec_type a, vec_type b) | Dot product. |
  719. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  720. | vec3 **cross** (vec3 a, vec3 b) | Cross product. |
  721. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  722. | vec_type **normalize** (vec_type x) | Normalize to unit length. |
  723. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  724. | vec3 **reflect** (vec3 I, vec3 N) | Reflect. |
  725. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  726. | vec3 **refract** (vec3 I, vec3 N, float eta) | Refract. |
  727. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  728. | vec_type **faceforward** (vec_type N, vec_type I, vec_type Nref) | If ``dot(Nref, I)`` < 0, return N, otherwise –N. |
  729. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  730. | mat_type **matrixCompMult** (mat_type x, mat_type y) | Matrix component multiplication. |
  731. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  732. | mat_type **outerProduct** (vec_type column, vec_type row) | Matrix outer product. |
  733. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  734. | mat_type **transpose** (mat_type m) | Transpose matrix. |
  735. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  736. | float **determinant** (mat_type m) | Matrix determinant. |
  737. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  738. | mat_type **inverse** (mat_type m) | Inverse matrix. |
  739. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  740. | bvec_type **lessThan** (vec_type x, vec_type y) | Bool vector comparison on < int/uint/float vectors. |
  741. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  742. | bvec_type **greaterThan** (vec_type x, vec_type y) | Bool vector comparison on > int/uint/float vectors. |
  743. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  744. | bvec_type **lessThanEqual** (vec_type x, vec_type y) | Bool vector comparison on <= int/uint/float vectors. |
  745. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  746. | bvec_type **greaterThanEqual** (vec_type x, vec_type y) | Bool vector comparison on >= int/uint/float vectors. |
  747. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  748. | bvec_type **equal** (vec_type x, vec_type y) | Bool vector comparison on == int/uint/float vectors. |
  749. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  750. | bvec_type **notEqual** (vec_type x, vec_type y) | Bool vector comparison on != int/uint/float vectors. |
  751. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  752. | bool **any** (bvec_type x) | ``true`` if any component is ``true``, ``false`` otherwise. |
  753. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  754. | bool **all** (bvec_type x) | ``true`` if all components are ``true``, ``false`` otherwise. |
  755. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  756. | bvec_type **not** (bvec_type x) | Invert boolean vector. |
  757. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  758. | ivec2 **textureSize** (gsampler2D s, int lod) | Get the size of a texture. |
  759. | | |
  760. | ivec3 **textureSize** (gsampler2DArray s, int lod) | |
  761. | | |
  762. | ivec3 **textureSize** (gsampler3D s, int lod) | |
  763. | | |
  764. | ivec2 **textureSize** (samplerCube s, int lod) | |
  765. | | |
  766. | ivec2 **textureSize** (samplerCubeArray s, int lod) | |
  767. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  768. | gvec4_type **texture** (gsampler2D s, vec2 p [, float bias]) | Perform a texture read. |
  769. | | |
  770. | gvec4_type **texture** (gsampler2DArray s, vec3 p [, float bias]) | |
  771. | | |
  772. | gvec4_type **texture** (gsampler3D s, vec3 p [, float bias]) | |
  773. | | |
  774. | vec4 **texture** (samplerCube s, vec3 p [, float bias]) | |
  775. | | |
  776. | vec4 **texture** (samplerCubeArray s, vec4 p [, float bias]) | |
  777. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  778. | gvec4_type **textureProj** (gsampler2D s, vec3 p [, float bias]) | Perform a texture read with projection. |
  779. | | |
  780. | gvec4_type **textureProj** (gsampler2D s, vec4 p [, float bias]) | |
  781. | | |
  782. | gvec4_type **textureProj** (gsampler3D s, vec4 p [, float bias]) | |
  783. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  784. | gvec4_type **textureLod** (gsampler2D s, vec2 p, float lod) | Perform a texture read at custom mipmap. |
  785. | | |
  786. | gvec4_type **textureLod** (gsampler2DArray s, vec3 p, float lod) | |
  787. | | |
  788. | gvec4_type **textureLod** (gsampler3D s, vec3 p, float lod) | |
  789. | | |
  790. | vec4 **textureLod** (samplerCube s, vec3 p, float lod) | |
  791. | | |
  792. | vec4 **textureLod** (samplerCubeArray s, vec4 p, float lod) | |
  793. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  794. | gvec4_type **textureProjLod** (gsampler2D s, vec3 p, float lod) | Performs a texture read with projection/LOD. |
  795. | | |
  796. | gvec4_type **textureProjLod** (gsampler2D s, vec4 p, float lod) | |
  797. | | |
  798. | gvec4_type **textureProjLod** (gsampler3D s, vec4 p, float lod) | |
  799. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  800. | gvec4_type **textureGrad** (gsampler2D s, vec2 p, vec2 dPdx, | Performs a texture read with explicit gradients. |
  801. | vec2 dPdy) | |
  802. | | |
  803. | gvec4_type **textureGrad** (gsampler2DArray s, vec3 p, vec2 dPdx, | |
  804. | vec2 dPdy) | |
  805. | | |
  806. | gvec4_type **textureGrad** (gsampler3D s, vec3 p, vec2 dPdx, | |
  807. | vec2 dPdy) | |
  808. | | |
  809. | vec4 **textureGrad** (samplerCube s, vec3 p, vec3 dPdx, vec3 dPdy) | |
  810. | | |
  811. | vec4 **textureGrad** (samplerCubeArray s, vec3 p, vec3 dPdx, | |
  812. | vec3 dPdy) | |
  813. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  814. | gvec4_type **texelFetch** (gsampler2D s, ivec2 p, int lod) | Fetches a single texel using integer coordinates. |
  815. | | |
  816. | gvec4_type **texelFetch** (gsampler2DArray s, ivec3 p, int lod) | |
  817. | | |
  818. | gvec4_type **texelFetch** (gsampler3D s, ivec3 p, int lod) | |
  819. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  820. | gvec4_type **textureGather** (gsampler2D s, vec2 p [, int comps]) | Gathers four texels from a texture. |
  821. | | Use ``comps`` within range of 0..3 to |
  822. | gvec4_type **textureGather** (gsampler2DArray s, vec3 p [, int comps]) | define which component (x, y, z, w) is returned. |
  823. | | If ``comps`` is not provided: 0 (or x-component) is used. |
  824. | vec4 **textureGather** (samplerCube s, vec3 p [, int comps]) | |
  825. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  826. | vec_type **dFdx** (vec_type p) | Derivative in ``x`` using local differencing. |
  827. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  828. | vec_type **dFdy** (vec_type p) | Derivative in ``y`` using local differencing. |
  829. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  830. | vec_type **fwidth** (vec_type p) | Sum of absolute derivative in ``x`` and ``y``. |
  831. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  832. | uint **packHalf2x16** (vec2 v) | Convert two 32-bit floating-point numbers into 16-bit |
  833. | | and pack them into a 32-bit unsigned integer and vice-versa. |
  834. | vec2 **unpackHalf2x16** (uint v) | |
  835. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  836. | uint **packUnorm2x16** (vec2 v) | Convert two 32-bit floating-point numbers (clamped |
  837. | | within 0..1 range) into 16-bit and pack them |
  838. | vec2 **unpackUnorm2x16** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  839. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  840. | uint **packSnorm2x16** (vec2 v) | Convert two 32-bit floating-point numbers (clamped |
  841. | | within -1..1 range) into 16-bit and pack them |
  842. | vec2 **unpackSnorm2x16** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  843. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  844. | uint **packUnorm4x8** (vec4 v) | Convert four 32-bit floating-point numbers (clamped |
  845. | | within 0..1 range) into 8-bit and pack them |
  846. | vec4 **unpackUnorm4x8** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  847. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  848. | uint **packSnorm4x8** (vec4 v) | Convert four 32-bit floating-point numbers (clamped |
  849. | | within -1..1 range) into 8-bit and pack them |
  850. | vec4 **unpackSnorm4x8** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  851. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  852. | ivec_type **bitfieldExtract** (ivec_type value, int offset, int bits) | Extracts a range of bits from an integer. |
  853. | | |
  854. | uvec_type **bitfieldExtract** (uvec_type value, int offset, int bits) | |
  855. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  856. | ivec_type **bitfieldInsert** (ivec_type base, ivec_type insert, | Insert a range of bits into an integer. |
  857. | int offset, int bits) | |
  858. | | |
  859. | uvec_type **bitfieldInsert** (uvec_type base, uvec_type insert, | |
  860. | int offset, int bits) | |
  861. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  862. | ivec_type **bitfieldReverse** (ivec_type value) | Reverse the order of bits in an integer. |
  863. | | |
  864. | uvec_type **bitfieldReverse** (uvec_type value) | |
  865. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  866. | ivec_type **bitCount** (ivec_type value) | Counts the number of 1 bits in an integer. |
  867. | | |
  868. | uvec_type **bitCount** (uvec_type value) | |
  869. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  870. | ivec_type **findLSB** (ivec_type value) | Find the index of the least significant bit set to 1 in an integer. |
  871. | | |
  872. | uvec_type **findLSB** (uvec_type value) | |
  873. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  874. | ivec_type **findMSB** (ivec_type value) | Find the index of the most significant bit set to 1 in an integer. |
  875. | | |
  876. | uvec_type **findMSB** (uvec_type value) | |
  877. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  878. | void **imulExtended** (ivec_type x, ivec_type y, out ivec_type msb, | Adds two 32-bit numbers and produce a 64-bit result. |
  879. | out ivec_type lsb) | ``x`` - the first number. |
  880. | | ``y`` - the second number. |
  881. | void **umulExtended** (uvec_type x, uvec_type y, out uvec_type msb, | ``msb`` - will contain the most significant bits. |
  882. | out uvec_type lsb) | ``lsb`` - will contain the least significant bits. |
  883. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  884. | uvec_type **uaddCarry** (uvec_type x, uvec_type y, out uvec_type carry) | Adds two unsigned integers and generates carry. |
  885. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  886. | uvec_type **usubBorrow** (uvec_type x, uvec_type y, out uvec_type borrow) | Subtracts two unsigned integers and generates borrow. |
  887. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  888. | vec_type **ldexp** (vec_type x, out ivec_type exp) | Assemble a floating-point number from a value and exponent. |
  889. | | |
  890. | | If this product is too large to be represented in the |
  891. | | floating-point type the result is undefined. |
  892. +---------------------------------------------------------------------------+---------------------------------------------------------------------+
  893. | vec_type **frexp** (vec_type x, out ivec_type exp) | Splits a floating-point number(``x``) into significand |
  894. | | (in the range of [0.5, 1.0]) and an integral exponent. |
  895. | | |
  896. | | For ``x`` equals zero the significand and exponent are both zero. |
  897. | | For ``x`` of infinity or NaN, the results are undefined. |
  898. +---------------------------------------------------------------------------+---------------------------------------------------------------------+