shading_language.rst 79 KB

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