shading_language.rst 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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. Discarding
  404. ----------
  405. Fragment and light functions can use the ``discard`` keyword. If used, the
  406. fragment is discarded and nothing is written.
  407. Beware that ``discard`` has a performance cost when used, as it will prevent the
  408. depth prepass from being effective on any surfaces using the shader. Also, a
  409. discarded pixel still needs to be rendered in the vertex shader, which means a
  410. shader that uses ``discard`` on all of its pixels is still more expensive to
  411. render compared to not rendering any object in the first place.
  412. Functions
  413. ---------
  414. It is possible to define functions in a Godot shader. They use the following
  415. syntax:
  416. .. code-block:: glsl
  417. ret_type func_name(args) {
  418. return ret_type; // if returning a value
  419. }
  420. // a more specific example:
  421. int sum2(int a, int b) {
  422. return a + b;
  423. }
  424. You can only use functions that have been defined above (higher in the editor)
  425. the function from which you are calling them. Redefining a function that has
  426. already been defined above (or is a built-in function name) will cause an error.
  427. Function arguments can have special qualifiers:
  428. * **in**: Means the argument is only for reading (default).
  429. * **out**: Means the argument is only for writing.
  430. * **inout**: Means the argument is fully passed via reference.
  431. * **const**: Means the argument is a constant and cannot be changed, may be
  432. combined with **in** qualifier.
  433. Example below:
  434. .. code-block:: glsl
  435. void sum2(int a, int b, inout int result) {
  436. result = a + b;
  437. }
  438. .. note::
  439. Unlike GLSL, Godot's shader language does **not** support function
  440. overloading. This means that a function cannot be defined several times with
  441. different argument types or numbers of arguments. As a workaround, use
  442. different names for functions that accept a different number of arguments or
  443. arguments of different types.
  444. Varyings
  445. --------
  446. To send data from the vertex to the fragment (or light) processor function, *varyings* are
  447. used. They are set for every primitive vertex in the *vertex processor*, and the
  448. value is interpolated for every pixel in the *fragment processor*.
  449. .. code-block:: glsl
  450. shader_type spatial;
  451. varying vec3 some_color;
  452. void vertex() {
  453. some_color = NORMAL; // Make the normal the color.
  454. }
  455. void fragment() {
  456. ALBEDO = some_color;
  457. }
  458. void light() {
  459. DIFFUSE_LIGHT = some_color * 100; // optionally
  460. }
  461. Varying can also be an array:
  462. .. code-block:: glsl
  463. shader_type spatial;
  464. varying float var_arr[3];
  465. void vertex() {
  466. var_arr[0] = 1.0;
  467. var_arr[1] = 0.0;
  468. }
  469. void fragment() {
  470. ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
  471. }
  472. 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.
  473. .. code-block:: glsl
  474. shader_type spatial;
  475. varying vec3 some_light;
  476. void fragment() {
  477. some_light = ALBEDO * 100.0; // Make a shining light.
  478. }
  479. void light() {
  480. DIFFUSE_LIGHT = some_light;
  481. }
  482. Note that varying may not be assigned in custom functions or a *light processor* function like:
  483. .. code-block:: glsl
  484. shader_type spatial;
  485. varying float test;
  486. void foo() {
  487. test = 0.0; // Error.
  488. }
  489. void vertex() {
  490. test = 0.0;
  491. }
  492. void light() {
  493. test = 0.0; // Error too.
  494. }
  495. This limitation was introduced to prevent incorrect usage before initialization.
  496. Interpolation qualifiers
  497. ------------------------
  498. Certain values are interpolated during the shading pipeline. You can modify how
  499. these interpolations are done by using *interpolation qualifiers*.
  500. .. code-block:: glsl
  501. shader_type spatial;
  502. varying flat vec3 our_color;
  503. void vertex() {
  504. our_color = COLOR.rgb;
  505. }
  506. void fragment() {
  507. ALBEDO = our_color;
  508. }
  509. There are two possible interpolation qualifiers:
  510. +-------------------+---------------------------------------------------------------------------------+
  511. | Qualifier | Description |
  512. +===================+=================================================================================+
  513. | **flat** | The value is not interpolated. |
  514. +-------------------+---------------------------------------------------------------------------------+
  515. | **smooth** | The value is interpolated in a perspective-correct fashion. This is the default.|
  516. +-------------------+---------------------------------------------------------------------------------+
  517. Uniforms
  518. --------
  519. Passing values to shaders is possible. These are global to the whole shader and
  520. are called *uniforms*. When a shader is later assigned to a material, the
  521. uniforms will appear as editable parameters in it. Uniforms can't be written
  522. from within the shader.
  523. .. code-block:: glsl
  524. shader_type spatial;
  525. uniform float some_value;
  526. uniform vec3 colors[3];
  527. You can set uniforms in the editor in the material. Or you can set them through
  528. GDScript:
  529. .. code-block:: gdscript
  530. material.set_shader_parameter("some_value", some_value)
  531. material.set_shader_parameter("colors", [Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1)])
  532. .. note:: The first argument to ``set_shader_parameter`` is the name of the uniform
  533. in the shader. It must match *exactly* to the name of the uniform in
  534. the shader or else it will not be recognized.
  535. Any GLSL type except for *void* can be a uniform. Additionally, Godot provides
  536. optional shader hints to make the compiler understand for what the uniform is
  537. used, and how the editor should allow users to modify it.
  538. .. code-block:: glsl
  539. shader_type spatial;
  540. uniform vec4 color : source_color;
  541. uniform float amount : hint_range(0, 1);
  542. uniform vec4 other_color : source_color = vec4(1.0);
  543. uniform sampler2D image : source_color;
  544. It's important to understand that textures that are supplied as color require
  545. hints for proper sRGB->linear conversion (i.e. ``source_color``), as Godot's 3D
  546. engine renders in linear color space.
  547. Full list of hints below:
  548. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  549. | Type | Hint | Description |
  550. +======================+==================================================+=============================================================================+
  551. | **vec3, vec4** | source_color | Used as color. |
  552. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  553. | **int, float** | hint_range(min, max[, step]) | Restricted to values in a range (with min/max/step). |
  554. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  555. | **sampler2D** | source_color | Used as albedo color. |
  556. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  557. | **sampler2D** | hint_normal | Used as normalmap. |
  558. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  559. | **sampler2D** | hint_default_white | As value or albedo color, default to opaque white. |
  560. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  561. | **sampler2D** | hint_default_black | As value or albedo color, default to opaque black. |
  562. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  563. | **sampler2D** | hint_default_transparent | As value or albedo color, default to transparent black. |
  564. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  565. | **sampler2D** | hint_anisotropy | As flowmap, default to right. |
  566. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  567. | **sampler2D** | hint_roughness[_r, _g, _b, _a, _normal, _gray] | Used for roughness limiter on import (attempts reducing specular aliasing). |
  568. | | | ``_normal`` is a normal map that guides the roughness limiter, |
  569. | | | with roughness increasing in areas that have high-frequency detail. |
  570. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  571. | **sampler2D** | filter[_nearest, _linear][_mipmap][_anisotropic] | Enabled specified texture filtering. |
  572. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  573. | **sampler2D** | repeat[_enable, _disable] | Enabled texture repeating. |
  574. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  575. | **sampler2D** | hint_screen_texture | Texture is the screen texture. |
  576. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  577. | **sampler2D** | hint_depth_texture | Texture is the depth texture. |
  578. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  579. | **sampler2D** | hint_normal_roughness_texture | Texture is the normal roughness texture. |
  580. +----------------------+--------------------------------------------------+-----------------------------------------------------------------------------+
  581. GDScript uses different variable types than GLSL does, so when passing variables
  582. from GDScript to shaders, Godot converts the type automatically. Below is a
  583. table of the corresponding types:
  584. +----------------------+-------------------------+------------------------------------------------------------+
  585. | GLSL type | GDScript type | Notes |
  586. +======================+=========================+============================================================+
  587. | **bool** | **bool** | |
  588. +----------------------+-------------------------+------------------------------------------------------------+
  589. | **bvec2** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  590. | | | |
  591. | | | For example, a bvec2 of (bx, by) could be created in |
  592. | | | the following way: |
  593. | | | |
  594. | | | .. code-block:: gdscript |
  595. | | | |
  596. | | | bvec2_input: int = (int(bx)) | (int(by) << 1) |
  597. | | | |
  598. +----------------------+-------------------------+------------------------------------------------------------+
  599. | **bvec3** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  600. +----------------------+-------------------------+------------------------------------------------------------+
  601. | **bvec4** | **int** | Bitwise packed int where bit 0 (LSB) corresponds to x. |
  602. +----------------------+-------------------------+------------------------------------------------------------+
  603. | **int** | **int** | |
  604. +----------------------+-------------------------+------------------------------------------------------------+
  605. | **ivec2** | **Vector2i** | |
  606. +----------------------+-------------------------+------------------------------------------------------------+
  607. | **ivec3** | **Vector3i** | |
  608. +----------------------+-------------------------+------------------------------------------------------------+
  609. | **ivec4** | **Vector4i** | |
  610. +----------------------+-------------------------+------------------------------------------------------------+
  611. | **uint** | **int** | |
  612. +----------------------+-------------------------+------------------------------------------------------------+
  613. | **uvec2** | **Vector2i** | |
  614. +----------------------+-------------------------+------------------------------------------------------------+
  615. | **uvec3** | **Vector3i** | |
  616. +----------------------+-------------------------+------------------------------------------------------------+
  617. | **uvec4** | **Vector4i** | |
  618. +----------------------+-------------------------+------------------------------------------------------------+
  619. | **float** | **float** | |
  620. +----------------------+-------------------------+------------------------------------------------------------+
  621. | **vec2** | **Vector2** | |
  622. +----------------------+-------------------------+------------------------------------------------------------+
  623. | **vec3** | **Vector3**, **Color** | When Color is used, it will be interpreted as (r, g, b). |
  624. +----------------------+-------------------------+------------------------------------------------------------+
  625. | **vec4** | **Vector4**, **Color**, | When Color is used, it will be interpreted as (r, g, b, a).|
  626. | | **Rect2**, **Plane**, | |
  627. | | **Quaternion** | When Rect2 is used, it will be interpreted as |
  628. | | | (position.x, position.y, size.x, size.y). |
  629. | | | |
  630. | | | When Plane is used it will be interpreted as |
  631. | | | (normal.x, normal.y, normal.z, d). |
  632. | | | |
  633. | | | |
  634. +----------------------+-------------------------+------------------------------------------------------------+
  635. | **mat2** | **Transform2D** | |
  636. | | | |
  637. +----------------------+-------------------------+------------------------------------------------------------+
  638. | **mat3** | **Basis** | |
  639. +----------------------+-------------------------+------------------------------------------------------------+
  640. | **mat4** | **Projection**, | When a Transform3D is used, the w Vector is set to the |
  641. | **mat4** | **Transform3D** | identity. |
  642. +----------------------+-------------------------+------------------------------------------------------------+
  643. | **sampler2D** | **Texture2D** | |
  644. +----------------------+-------------------------+------------------------------------------------------------+
  645. | **isampler2D** | **Texture2D** | |
  646. +----------------------+-------------------------+------------------------------------------------------------+
  647. | **usampler2D** | **Texture2D** | |
  648. +----------------------+-------------------------+------------------------------------------------------------+
  649. | **sampler2DArray** | **Texture2DArray** | |
  650. +----------------------+-------------------------+------------------------------------------------------------+
  651. | **isampler2DArray** | **Texture2DArray** | |
  652. +----------------------+-------------------------+------------------------------------------------------------+
  653. | **usampler2DArray** | **Texture2DArray** | |
  654. +----------------------+-------------------------+------------------------------------------------------------+
  655. | **sampler3D** | **Texture3D** | |
  656. +----------------------+-------------------------+------------------------------------------------------------+
  657. | **isampler3D** | **Texture3D** | |
  658. +----------------------+-------------------------+------------------------------------------------------------+
  659. | **usampler3D** | **Texture3D** | |
  660. +----------------------+-------------------------+------------------------------------------------------------+
  661. | **samplerCube** | **Cubemap** | |
  662. +----------------------+-------------------------+------------------------------------------------------------+
  663. | **samplerCubeArray** | **CubemapArray** | |
  664. +----------------------+-------------------------+------------------------------------------------------------+
  665. .. note:: Be careful when setting shader uniforms from GDScript, no error will
  666. be thrown if the type does not match. Your shader will just exhibit
  667. undefined behavior.
  668. Uniforms can also be assigned default values:
  669. .. code-block:: glsl
  670. shader_type spatial;
  671. uniform vec4 some_vector = vec4(0.0);
  672. uniform vec4 some_color : source_color = vec4(1.0);
  673. 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:
  674. .. code-block:: glsl
  675. group_uniforms MyGroup;
  676. uniform sampler2D test;
  677. You can close the group by using:
  678. .. code-block:: glsl
  679. group_uniforms;
  680. The syntax also supports subgroups (it's not mandatory to declare the base group before this):
  681. .. code-block:: glsl
  682. group_uniforms MyGroup.MySubgroup;
  683. Global uniforms
  684. ~~~~~~~~~~~~~~~
  685. Sometimes, you want to modify a parameter in many different shaders at once.
  686. With a regular uniform, this takes a lot of work as all these shaders need to be
  687. tracked and the uniform needs to be set for each of them. Global uniforms allow
  688. you to create and update uniforms that will be available in all shaders, in
  689. every shader type (``canvas_item``, ``spatial``, ``particles``, ``sky`` and
  690. ``fog``).
  691. Global uniforms are especially useful for environmental effects that affect many
  692. objects in a scene, like having foliage bend when the player is nearby, or having
  693. objects move with the wind.
  694. To create a global uniform, open the **Project Settings** then go to the
  695. **Shader Globals** tab. Specify a name for the uniform (case-sensitive) and a
  696. type, then click **Add** in the top-right corner of the dialog. You can then
  697. edit the value assigned to the uniform by clicking the value in the list of
  698. uniforms:
  699. .. figure:: img/shading_language_adding_global_uniforms.webp
  700. :align: center
  701. :alt: Adding a global uniform in the Shader Globals tab of the Project Settings
  702. Adding a global uniform in the Shader Globals tab of the Project Settings
  703. After creating a global uniform, you can use it in a shader as follows:
  704. .. code-block:: glsl
  705. shader_type canvas_item;
  706. global uniform vec4 my_color;
  707. void fragment() {
  708. COLOR = my_color.rgb;
  709. }
  710. Note that the global uniform *must* exist in the Project Settings at the time
  711. the shader is saved, or compilation will fail. While you can assign a default
  712. value using ``global uniform vec4 my_color = ...`` in the shader code, it will
  713. be ignored as the global uniform must always be defined in the Project Settings
  714. anyway.
  715. To change the value of a global uniform at run-time, use the
  716. :ref:`RenderingServer.global_shader_parameter_set <class_RenderingServer_method_global_shader_parameter_set>`
  717. method in a script:
  718. .. code-block:: gdscript
  719. RenderingServer.global_shader_parameter_set("my_color", Color(0.3, 0.6, 1.0))
  720. Assigning global uniform values can be done as many times as desired without
  721. impacting performance, as setting data doesn't require synchronization between
  722. the CPU and GPU.
  723. You can also add or remove global uniforms at run-time:
  724. .. code-block:: gdscript
  725. RenderingServer.global_shader_parameter_add("my_color", RenderingServer.GLOBAL_VAR_TYPE_COLOR, Color(0.3, 0.6, 1.0))
  726. RenderingServer.global_shader_parameter_remove("my_color")
  727. Adding or removing global uniforms at run-time has a performance cost, although
  728. it's not as pronounced compared to getting global uniform values from a script
  729. (see the warning below).
  730. .. warning::
  731. While you *can* query the value of a global uniform at run-time in a script
  732. using ``RenderingServer.global_shader_parameter_get("uniform_name")``, this
  733. has a large performance penalty as the rendering thread needs to synchronize
  734. with the calling thread.
  735. Therefore, it's not recommended to read global shader uniform values
  736. continuously in a script. If you need to read values in a script after
  737. setting them, consider creating an :ref:`autoload <doc_singletons_autoload>`
  738. where you store the values you need to query at the same time you're setting
  739. them as global uniforms.
  740. .. _doc_shading_language_per_instance_uniforms:
  741. Per-instance uniforms
  742. ~~~~~~~~~~~~~~~~~~~~~
  743. .. note::
  744. Per-instance uniforms are only available in ``spatial`` (3D) shaders.
  745. Sometimes, you want to modify a parameter on each node using the material. As an
  746. example, in a forest full of trees, when you want each tree to have a slightly
  747. different color that is editable by hand. Without per-instance uniforms, this
  748. requires creating a unique material for each tree (each with a slightly
  749. different hue). This makes material management more complex, and also has a
  750. performance overhead due to the scene requiring more unique material instances.
  751. Vertex colors could also be used here, but they'd require creating unique copies
  752. of the mesh for each different color, which also has a performance overhead.
  753. Per-instance uniforms are set on each GeometryInstance3D, rather than on each
  754. Material instance. Take this into account when working with meshes that have
  755. multiple materials assigned to them, or MultiMesh setups.
  756. .. code-block:: glsl
  757. shader_type spatial;
  758. // Provide a hint to edit as a color. Optionally, a default value can be provided.
  759. // If no default value is provided, the type's default is used (e.g. opaque black for colors).
  760. instance uniform vec4 my_color : source_color = vec4(1.0, 0.5, 0.0, 1.0);
  761. void fragment() {
  762. ALBEDO = my_color.rgb;
  763. }
  764. After saving the shader, you can change the per-instance uniform's value using
  765. the inspector:
  766. .. figure:: img/shading_language_per_instance_uniforms_inspector.webp
  767. :align: center
  768. :alt: Setting a per-instance uniform's value in the GeometryInstance3D section of the inspector
  769. Setting a per-instance uniform's value in the GeometryInstance3D section of the inspector
  770. Per-instance uniform values can also be set at run-time using
  771. `set_instance_shader_parameter<class_GeometryInstance3D_method_set_instance_shader_parameter>`
  772. method on a node that inherits from :ref:`class_GeometryInstance3D`:
  773. .. code-block:: gdscript
  774. $MeshInstance3D.set_instance_shader_parameter("my_color", Color(0.3, 0.6, 1.0))
  775. When using per-instance uniforms, there are some restrictions you should be aware of:
  776. - **Per-instance uniforms do not support textures**, only regular scalar and
  777. vector types. As a workaround, you can pass a texture array as a regular
  778. uniform, then pass the index of the texture to be drawn using a per-instance
  779. uniform.
  780. - There is a practical maximum limit of 16 instance uniforms per shader.
  781. - If your mesh uses multiple materials, the parameters for the first mesh
  782. material found will "win" over the subsequent ones, unless they have the same
  783. name, index *and* type. In this case, all parameters are affected correctly.
  784. - If you run into the above situation, you can avoid clashes by manually
  785. specifying the index (0-15) of the instance uniform by using the
  786. ``instance_index`` hint:
  787. .. code-block:: glsl
  788. instance uniform vec4 my_color : source_color, instance_index(5);
  789. Built-in variables
  790. ------------------
  791. 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``).
  792. For a list of the built-in variables that are available, please see the corresponding pages:
  793. - :ref:`Spatial shaders <doc_spatial_shader>`
  794. - :ref:`Canvas item shaders <doc_canvas_item_shader>`
  795. - :ref:`Particle shaders <doc_particle_shader>`
  796. - :ref:`Sky shaders <doc_sky_shader>`
  797. - :ref:`Fog shaders <doc_fog_shader>`
  798. Built-in functions
  799. ------------------
  800. A large number of built-in functions are supported, conforming to GLSL ES 3.0.
  801. When vec_type (float), vec_int_type, vec_uint_type, vec_bool_type nomenclature
  802. is used, it can be scalar or vector.
  803. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  804. | Function | Description / Return value |
  805. +=============================================================================+=====================================================================+
  806. | vec_type **radians** (vec_type degrees) | Convert degrees to radians. |
  807. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  808. | vec_type **degrees** (vec_type radians) | Convert radians to degrees. |
  809. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  810. | vec_type **sin** (vec_type x) | Sine. |
  811. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  812. | vec_type **cos** (vec_type x) | Cosine. |
  813. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  814. | vec_type **tan** (vec_type x) | Tangent. |
  815. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  816. | vec_type **asin** (vec_type x) | Arcsine. |
  817. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  818. | vec_type **acos** (vec_type x) | Arccosine. |
  819. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  820. | vec_type **atan** (vec_type y_over_x) | Arctangent. |
  821. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  822. | vec_type **atan** (vec_type y, vec_type x) | Arctangent. |
  823. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  824. | vec_type **sinh** (vec_type x) | Hyperbolic sine. |
  825. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  826. | vec_type **cosh** (vec_type x) | Hyperbolic cosine. |
  827. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  828. | vec_type **tanh** (vec_type x) | Hyperbolic tangent. |
  829. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  830. | vec_type **asinh** (vec_type x) | Inverse hyperbolic sine. |
  831. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  832. | vec_type **acosh** (vec_type x) | Inverse hyperbolic cosine. |
  833. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  834. | vec_type **atanh** (vec_type x) | Inverse hyperbolic tangent. |
  835. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  836. | vec_type **pow** (vec_type x, vec_type y) | Power (undefined if ``x`` < 0 or if ``x`` == 0 and ``y`` <= 0). |
  837. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  838. | vec_type **exp** (vec_type x) | Base-e exponential. |
  839. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  840. | vec_type **exp2** (vec_type x) | Base-2 exponential. |
  841. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  842. | vec_type **log** (vec_type x) | Natural logarithm. |
  843. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  844. | vec_type **log2** (vec_type x) | Base-2 logarithm. |
  845. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  846. | vec_type **sqrt** (vec_type x) | Square root. |
  847. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  848. | vec_type **inversesqrt** (vec_type x) | Inverse square root. |
  849. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  850. | vec_type **abs** (vec_type x) | Absolute value (returns positive value if negative). |
  851. | | |
  852. | ivec_type **abs** (ivec_type x) | |
  853. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  854. | vec_type **sign** (vec_type x) | Sign (returns ``1.0`` if positive, ``-1.0`` if negative, |
  855. | | ``0.0`` if zero). |
  856. | ivec_type **sign** (ivec_type x) | |
  857. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  858. | vec_type **floor** (vec_type x) | Round to the integer below. |
  859. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  860. | vec_type **round** (vec_type x) | Round to the nearest integer. |
  861. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  862. | vec_type **roundEven** (vec_type x) | Round to the nearest even integer. |
  863. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  864. | vec_type **trunc** (vec_type x) | Truncation. |
  865. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  866. | vec_type **ceil** (vec_type x) | Round to the integer above. |
  867. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  868. | vec_type **fract** (vec_type x) | Fractional (returns ``x - floor(x)``). |
  869. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  870. | vec_type **mod** (vec_type x, vec_type y) | Modulo (division remainder). |
  871. | | |
  872. | vec_type **mod** (vec_type x, float y) | |
  873. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  874. | vec_type **modf** (vec_type x, out vec_type i) | Fractional of ``x``, with ``i`` as integer part. |
  875. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  876. | vec_type **min** (vec_type a, vec_type b) | Lowest value between ``a`` and ``b``. |
  877. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  878. | vec_type **max** (vec_type a, vec_type b) | Highest value between ``a`` and ``b``. |
  879. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  880. | vec_type **clamp** (vec_type x, vec_type min, vec_type max) | Clamp ``x`` between ``min`` and ``max`` (inclusive). |
  881. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  882. | float **mix** (float a, float b, float c) | Linear interpolate between ``a`` and ``b`` by ``c``. |
  883. | | |
  884. | vec_type **mix** (vec_type a, vec_type b, float c) | |
  885. | | |
  886. | vec_type **mix** (vec_type a, vec_type b, bvec_type c) | |
  887. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  888. | vec_type **fma** (vec_type a, vec_type b, vec_type c) | Performs a fused multiply-add operation: ``(a * b + c)`` |
  889. | | (faster than doing it manually). |
  890. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  891. | vec_type **step** (vec_type a, vec_type b) | ``b[i] < a[i] ? 0.0 : 1.0``. |
  892. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  893. | vec_type **step** (float a, vec_type b) | ``b[i] < a ? 0.0 : 1.0``. |
  894. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  895. | vec_type **smoothstep** (vec_type a, vec_type b, vec_type c) | Hermite interpolate between ``a`` and ``b`` by ``c``. |
  896. | | |
  897. | vec_type **smoothstep** (float a, float b, vec_type c) | |
  898. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  899. | bvec_type **isnan** (vec_type x) | Returns ``true`` if scalar or vector component is ``NaN``. |
  900. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  901. | bvec_type **isinf** (vec_type x) | Returns ``true`` if scalar or vector component is ``INF``. |
  902. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  903. | ivec_type **floatBitsToInt** (vec_type x) | Float->Int bit copying, no conversion. |
  904. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  905. | uvec_type **floatBitsToUint** (vec_type x) | Float->UInt bit copying, no conversion. |
  906. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  907. | vec_type **intBitsToFloat** (ivec_type x) | Int->Float bit copying, no conversion. |
  908. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  909. | vec_type **uintBitsToFloat** (uvec_type x) | UInt->Float bit copying, no conversion. |
  910. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  911. | float **length** (vec_type x) | Vector length. |
  912. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  913. | float **distance** (vec_type a, vec_type b) | Distance between vectors i.e ``length(a - b)``. |
  914. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  915. | float **dot** (vec_type a, vec_type b) | Dot product. |
  916. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  917. | vec3 **cross** (vec3 a, vec3 b) | Cross product. |
  918. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  919. | vec_type **normalize** (vec_type x) | Normalize to unit length. |
  920. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  921. | vec3 **reflect** (vec3 I, vec3 N) | Reflect. |
  922. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  923. | vec3 **refract** (vec3 I, vec3 N, float eta) | Refract. |
  924. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  925. | vec_type **faceforward** (vec_type N, vec_type I, vec_type Nref) | If ``dot(Nref, I)`` < 0, return ``N``, otherwise ``-N``. |
  926. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  927. | mat_type **matrixCompMult** (mat_type x, mat_type y) | Matrix component multiplication. |
  928. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  929. | mat_type **outerProduct** (vec_type column, vec_type row) | Matrix outer product. |
  930. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  931. | mat_type **transpose** (mat_type m) | Transpose matrix. |
  932. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  933. | float **determinant** (mat_type m) | Matrix determinant. |
  934. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  935. | mat_type **inverse** (mat_type m) | Inverse matrix. |
  936. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  937. | bvec_type **lessThan** (vec_type x, vec_type y) | Bool vector comparison on < int/uint/float vectors. |
  938. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  939. | bvec_type **greaterThan** (vec_type x, vec_type y) | Bool vector comparison on > int/uint/float vectors. |
  940. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  941. | bvec_type **lessThanEqual** (vec_type x, vec_type y) | Bool vector comparison on <= int/uint/float vectors. |
  942. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  943. | bvec_type **greaterThanEqual** (vec_type x, vec_type y) | Bool vector comparison on >= int/uint/float vectors. |
  944. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  945. | bvec_type **equal** (vec_type x, vec_type y) | Bool vector comparison on == int/uint/float vectors. |
  946. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  947. | bvec_type **notEqual** (vec_type x, vec_type y) | Bool vector comparison on != int/uint/float vectors. |
  948. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  949. | bool **any** (bvec_type x) | ``true`` if any component is ``true``, ``false`` otherwise. |
  950. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  951. | bool **all** (bvec_type x) | ``true`` if all components are ``true``, ``false`` otherwise. |
  952. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  953. | bvec_type **not** (bvec_type x) | Invert boolean vector. |
  954. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  955. | ivec2 **textureSize** (gsampler2D s, int lod) | Get the size of a texture. |
  956. | | |
  957. | ivec3 **textureSize** (gsampler2DArray s, int lod) | The LOD defines which mipmap level is used. An LOD value of ``0`` |
  958. | | will use the full resolution texture. |
  959. | ivec3 **textureSize** (gsampler3D s, int lod) | |
  960. | | |
  961. | ivec2 **textureSize** (samplerCube s, int lod) | |
  962. | | |
  963. | ivec2 **textureSize** (samplerCubeArray s, int lod) | |
  964. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  965. | vec2 **textureQueryLod** (gsampler2D s, vec2 p) | Compute the level-of-detail that would be used to sample from a |
  966. | | texture. The ``x`` component of the resulted value is the mipmap |
  967. | vec3 **textureQueryLod** (gsampler2DArray s, vec2 p) | array that would be accessed. The ``y`` component is computed |
  968. | | level-of-detail relative to the base level (regardless of the |
  969. | vec2 **textureQueryLod** (gsampler3D s, vec3 p) | mipmap levels of the texture). |
  970. | | |
  971. | vec2 **textureQueryLod** (samplerCube s, vec3 p) | |
  972. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  973. | int **textureQueryLevels** (gsampler2D s) | Get the number of accessible mipmap levels of a texture. |
  974. | | |
  975. | int **textureQueryLevels** (gsampler2DArray s) | If the texture is unassigned to a sampler, ``0`` is returned. |
  976. | | |
  977. | int **textureQueryLevels** (gsampler3D s) | |
  978. | | |
  979. | int **textureQueryLevels** (samplerCube s) | |
  980. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  981. | gvec4_type **texture** (gsampler2D s, vec2 p [, float bias]) | Perform a texture read. |
  982. | | |
  983. | gvec4_type **texture** (gsampler2DArray s, vec3 p [, float bias]) | |
  984. | | |
  985. | gvec4_type **texture** (gsampler3D s, vec3 p [, float bias]) | |
  986. | | |
  987. | vec4 **texture** (samplerCube s, vec3 p [, float bias]) | |
  988. | | |
  989. | vec4 **texture** (samplerCubeArray s, vec4 p [, float bias]) | |
  990. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  991. | gvec4_type **textureProj** (gsampler2D s, vec3 p [, float bias]) | Perform a texture read with projection. |
  992. | | |
  993. | gvec4_type **textureProj** (gsampler2D s, vec4 p [, float bias]) | |
  994. | | |
  995. | gvec4_type **textureProj** (gsampler3D s, vec4 p [, float bias]) | |
  996. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  997. | gvec4_type **textureLod** (gsampler2D s, vec2 p, float lod) | Perform a texture read at custom mipmap. |
  998. | | |
  999. | gvec4_type **textureLod** (gsampler2DArray s, vec3 p, float lod) | The LOD defines which mipmap level is used. An LOD value of ``0.0`` |
  1000. | | will use the full resolution texture. |
  1001. | gvec4_type **textureLod** (gsampler3D s, vec3 p, float lod) | |
  1002. | | |
  1003. | vec4 **textureLod** (samplerCube s, vec3 p, float lod) | |
  1004. | | |
  1005. | vec4 **textureLod** (samplerCubeArray s, vec4 p, float lod) | |
  1006. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1007. | gvec4_type **textureProjLod** (gsampler2D s, vec3 p, float lod) | Performs a texture read with projection/LOD. |
  1008. | | |
  1009. | gvec4_type **textureProjLod** (gsampler2D s, vec4 p, float lod) | The LOD defines which mipmap level is used. An LOD value of ``0.0`` |
  1010. | | will use the full resolution texture. |
  1011. | gvec4_type **textureProjLod** (gsampler3D s, vec4 p, float lod) | |
  1012. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1013. | gvec4_type **textureGrad** (gsampler2D s, vec2 p, vec2 dPdx, | Performs a texture read with explicit gradients. |
  1014. | vec2 dPdy) | |
  1015. | | |
  1016. | gvec4_type **textureGrad** (gsampler2DArray s, vec3 p, vec2 dPdx, | |
  1017. | vec2 dPdy) | |
  1018. | | |
  1019. | gvec4_type **textureGrad** (gsampler3D s, vec3 p, vec2 dPdx, | |
  1020. | vec2 dPdy) | |
  1021. | | |
  1022. | vec4 **textureGrad** (samplerCube s, vec3 p, vec3 dPdx, vec3 dPdy) | |
  1023. | | |
  1024. | vec4 **textureGrad** (samplerCubeArray s, vec3 p, vec3 dPdx, | |
  1025. | vec3 dPdy) | |
  1026. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1027. | gvec4_type **textureProjGrad** (gsampler2D s, vec3 p, vec2 dPdx, vec2 dPdy) | Performs a texture read with projection/LOD and with explcit |
  1028. | | gradients. |
  1029. | gvec4_type **textureProjGrad** (gsampler2D s, vec4 p, vec2 dPdx, vec2 dPdy) | |
  1030. | | |
  1031. | gvec4_type **textureProjGrad** (gsampler3D s, vec4 p, vec3 dPdx, vec3 dPdy) | |
  1032. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1033. | gvec4_type **texelFetch** (gsampler2D s, ivec2 p, int lod) | Fetches a single texel using integer coordinates. |
  1034. | | |
  1035. | gvec4_type **texelFetch** (gsampler2DArray s, ivec3 p, int lod) | The LOD defines which mipmap level is used. An LOD value of ``0`` |
  1036. | | will use the full resolution texture. |
  1037. | gvec4_type **texelFetch** (gsampler3D s, ivec3 p, int lod) | |
  1038. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1039. | gvec4_type **textureGather** (gsampler2D s, vec2 p [, int comps]) | Gathers four texels from a texture. |
  1040. | | Use ``comps`` within range of 0..3 to |
  1041. | gvec4_type **textureGather** (gsampler2DArray s, vec3 p [, int comps]) | define which component (x, y, z, w) is returned. |
  1042. | | If ``comps`` is not provided: 0 (or x-component) is used. |
  1043. | vec4 **textureGather** (samplerCube s, vec3 p [, int comps]) | |
  1044. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1045. | vec_type **dFdx** (vec_type p) | Derivative in ``x`` using local differencing. |
  1046. | | Internally, can use either ``dFdxCoarse`` or ``dFdxFine``, but the |
  1047. | | decision for which to use is made by the GPU driver. |
  1048. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1049. | vec_type **dFdxCoarse** (vec_type p) | Calculates derivative with respect to ``x`` window coordinate using |
  1050. | | local differencing based on the value of ``p`` for the current |
  1051. | | fragment neighbour(s), and will possibly, but not necessarily, |
  1052. | | include the value for the current fragment. |
  1053. | | This function is not available on ``gl_compatibility`` profile. |
  1054. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1055. | vec_type **dFdxFine** (vec_type p) | Calculates derivative with respect to ``x`` window coordinate using |
  1056. | | local differencing based on the value of ``p`` for the current |
  1057. | | fragment and its immediate neighbour(s). |
  1058. | | This function is not available on ``gl_compatibility`` profile. |
  1059. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1060. | vec_type **dFdy** (vec_type p) | Derivative in ``y`` using local differencing. |
  1061. | | Internally, can use either ``dFdyCoarse`` or ``dFdyFine``, but the |
  1062. | | decision for which to use is made by the GPU driver. |
  1063. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1064. | vec_type **dFdyCoarse** (vec_type p) | Calculates derivative with respect to ``y`` window coordinate using |
  1065. | | local differencing based on the value of ``p`` for the current |
  1066. | | fragment neighbour(s), and will possibly, but not necessarily, |
  1067. | | include the value for the current fragment. |
  1068. | | This function is not available on ``gl_compatibility`` profile. |
  1069. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1070. | vec_type **dFdyFine** (vec_type p) | Calculates derivative with respect to ``y`` window coordinate using |
  1071. | | local differencing based on the value of ``p`` for the current |
  1072. | | fragment and its immediate neighbour(s). |
  1073. | | This function is not available on ``gl_compatibility`` profile. |
  1074. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1075. | vec_type **fwidth** (vec_type p) | Sum of absolute derivative in ``x`` and ``y``. |
  1076. | | This is the equivalent of using ``abs(dFdx(p)) + abs(dFdy(p))``. |
  1077. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1078. | vec_type **fwidthCoarse** (vec_type p) | Sum of absolute derivative in ``x`` and ``y``. |
  1079. | | This is the equivalent of using |
  1080. | | ``abs(dFdxCoarse(p)) + abs(dFdyCoarse(p))``. |
  1081. | | This function is not available on ``gl_compatibility`` profile. |
  1082. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1083. | vec_type **fwidthFine** (vec_type p) | Sum of absolute derivative in ``x`` and ``y``. |
  1084. | | This is the equivalent of using |
  1085. | | ``abs(dFdxFine(p)) + abs(dFdyFine(p))``. |
  1086. | | This function is not available on ``gl_compatibility`` profile. |
  1087. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1088. | uint **packHalf2x16** (vec2 v) | Convert two 32-bit floating-point numbers into 16-bit |
  1089. | | and pack them into a 32-bit unsigned integer and vice-versa. |
  1090. | vec2 **unpackHalf2x16** (uint v) | |
  1091. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1092. | uint **packUnorm2x16** (vec2 v) | Convert two 32-bit floating-point numbers (clamped |
  1093. | | within 0..1 range) into 16-bit and pack them |
  1094. | vec2 **unpackUnorm2x16** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1095. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1096. | uint **packSnorm2x16** (vec2 v) | Convert two 32-bit floating-point numbers (clamped |
  1097. | | within -1..1 range) into 16-bit and pack them |
  1098. | vec2 **unpackSnorm2x16** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1099. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1100. | uint **packUnorm4x8** (vec4 v) | Convert four 32-bit floating-point numbers (clamped |
  1101. | | within 0..1 range) into 8-bit and pack them |
  1102. | vec4 **unpackUnorm4x8** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1103. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1104. | uint **packSnorm4x8** (vec4 v) | Convert four 32-bit floating-point numbers (clamped |
  1105. | | within -1..1 range) into 8-bit and pack them |
  1106. | vec4 **unpackSnorm4x8** (uint v) | into a 32-bit unsigned integer and vice-versa. |
  1107. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1108. | ivec_type **bitfieldExtract** (ivec_type value, int offset, int bits) | Extracts a range of bits from an integer. |
  1109. | | |
  1110. | uvec_type **bitfieldExtract** (uvec_type value, int offset, int bits) | |
  1111. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1112. | ivec_type **bitfieldInsert** (ivec_type base, ivec_type insert, | Insert a range of bits into an integer. |
  1113. | int offset, int bits) | |
  1114. | | |
  1115. | uvec_type **bitfieldInsert** (uvec_type base, uvec_type insert, | |
  1116. | int offset, int bits) | |
  1117. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1118. | ivec_type **bitfieldReverse** (ivec_type value) | Reverse the order of bits in an integer. |
  1119. | | |
  1120. | uvec_type **bitfieldReverse** (uvec_type value) | |
  1121. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1122. | ivec_type **bitCount** (ivec_type value) | Counts the number of 1 bits in an integer. |
  1123. | | |
  1124. | uvec_type **bitCount** (uvec_type value) | |
  1125. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1126. | ivec_type **findLSB** (ivec_type value) | Find the index of the least significant bit set to 1 in an integer. |
  1127. | | |
  1128. | uvec_type **findLSB** (uvec_type value) | |
  1129. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1130. | ivec_type **findMSB** (ivec_type value) | Find the index of the most significant bit set to 1 in an integer. |
  1131. | | |
  1132. | uvec_type **findMSB** (uvec_type value) | |
  1133. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1134. | void **imulExtended** (ivec_type x, ivec_type y, out ivec_type msb, | Multiplies two 32-bit numbers and produce a 64-bit result. |
  1135. | out ivec_type lsb) | ``x`` - the first number. |
  1136. | | ``y`` - the second number. |
  1137. | void **umulExtended** (uvec_type x, uvec_type y, out uvec_type msb, | ``msb`` - will contain the most significant bits. |
  1138. | out uvec_type lsb) | ``lsb`` - will contain the least significant bits. |
  1139. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1140. | uvec_type **uaddCarry** (uvec_type x, uvec_type y, out uvec_type carry) | Adds two unsigned integers and generates carry. |
  1141. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1142. | uvec_type **usubBorrow** (uvec_type x, uvec_type y, out uvec_type borrow) | Subtracts two unsigned integers and generates borrow. |
  1143. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1144. | vec_type **ldexp** (vec_type x, out ivec_type exp) | Assemble a floating-point number from a value and exponent. |
  1145. | | |
  1146. | | If this product is too large to be represented in the |
  1147. | | floating-point type the result is undefined. |
  1148. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+
  1149. | vec_type **frexp** (vec_type x, out ivec_type exp) | Splits a floating-point number(``x``) into significand |
  1150. | | (in the range of [0.5, 1.0]) and an integral exponent. |
  1151. | | |
  1152. | | For ``x`` equals zero the significand and exponent are both zero. |
  1153. | | For ``x`` of infinity or NaN, the results are undefined. |
  1154. +-----------------------------------------------------------------------------+---------------------------------------------------------------------+