shading_language.rst 106 KB

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