stdlib.glsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. vec2 select2(vec2 a, vec2 b, bvec2 c) {
  2. vec2 ret;
  3. ret.x = c.x ? b.x : a.x;
  4. ret.y = c.y ? b.y : a.y;
  5. return ret;
  6. }
  7. vec3 select3(vec3 a, vec3 b, bvec3 c) {
  8. vec3 ret;
  9. ret.x = c.x ? b.x : a.x;
  10. ret.y = c.y ? b.y : a.y;
  11. ret.z = c.z ? b.z : a.z;
  12. return ret;
  13. }
  14. vec4 select4(vec4 a, vec4 b, bvec4 c) {
  15. vec4 ret;
  16. ret.x = c.x ? b.x : a.x;
  17. ret.y = c.y ? b.y : a.y;
  18. ret.z = c.z ? b.z : a.z;
  19. ret.w = c.w ? b.w : a.w;
  20. return ret;
  21. }
  22. highp vec4 texel2DFetch(highp sampler2D tex, ivec2 size, ivec2 coord) {
  23. float x_coord = float(2 * coord.x + 1) / float(size.x * 2);
  24. float y_coord = float(2 * coord.y + 1) / float(size.y * 2);
  25. return texture2DLod(tex, vec2(x_coord, y_coord), 0.0);
  26. }
  27. #if defined(SINH_USED)
  28. highp float sinh(highp float x) {
  29. return 0.5 * (exp(x) - exp(-x));
  30. }
  31. highp vec2 sinh(highp vec2 x) {
  32. return 0.5 * vec2(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y));
  33. }
  34. highp vec3 sinh(highp vec3 x) {
  35. return 0.5 * vec3(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y), exp(x.z) - exp(-x.z));
  36. }
  37. highp vec4 sinh(highp vec4 x) {
  38. return 0.5 * vec4(exp(x.x) - exp(-x.x), exp(x.y) - exp(-x.y), exp(x.z) - exp(-x.z), exp(x.w) - exp(-x.w));
  39. }
  40. #endif
  41. #if defined(COSH_USED)
  42. highp float cosh(highp float x) {
  43. return 0.5 * (exp(x) + exp(-x));
  44. }
  45. highp vec2 cosh(highp vec2 x) {
  46. return 0.5 * vec2(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y));
  47. }
  48. highp vec3 cosh(highp vec3 x) {
  49. return 0.5 * vec3(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y), exp(x.z) + exp(-x.z));
  50. }
  51. highp vec4 cosh(highp vec4 x) {
  52. return 0.5 * vec4(exp(x.x) + exp(-x.x), exp(x.y) + exp(-x.y), exp(x.z) + exp(-x.z), exp(x.w) + exp(-x.w));
  53. }
  54. #endif
  55. #if defined(TANH_USED)
  56. highp float tanh(highp float x) {
  57. highp float exp2x = exp(2.0 * x);
  58. return (exp2x - 1.0) / (exp2x + 1.0);
  59. }
  60. highp vec2 tanh(highp vec2 x) {
  61. highp float exp2x = exp(2.0 * x.x);
  62. highp float exp2y = exp(2.0 * x.y);
  63. return vec2((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0));
  64. }
  65. highp vec3 tanh(highp vec3 x) {
  66. highp float exp2x = exp(2.0 * x.x);
  67. highp float exp2y = exp(2.0 * x.y);
  68. highp float exp2z = exp(2.0 * x.z);
  69. return vec3((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0), (exp2z - 1.0) / (exp2z + 1.0));
  70. }
  71. highp vec4 tanh(highp vec4 x) {
  72. highp float exp2x = exp(2.0 * x.x);
  73. highp float exp2y = exp(2.0 * x.y);
  74. highp float exp2z = exp(2.0 * x.z);
  75. highp float exp2w = exp(2.0 * x.w);
  76. return vec4((exp2x - 1.0) / (exp2x + 1.0), (exp2y - 1.0) / (exp2y + 1.0), (exp2z - 1.0) / (exp2z + 1.0), (exp2w - 1.0) / (exp2w + 1.0));
  77. }
  78. #endif
  79. #if defined(ASINH_USED)
  80. highp float asinh(highp float x) {
  81. return sign(x) * log(abs(x) + sqrt(1.0 + x * x));
  82. }
  83. highp vec2 asinh(highp vec2 x) {
  84. return vec2(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)));
  85. }
  86. highp vec3 asinh(highp vec3 x) {
  87. return vec3(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)), sign(x.z) * log(abs(x.z) + sqrt(1.0 + x.z * x.z)));
  88. }
  89. highp vec4 asinh(highp vec4 x) {
  90. return vec4(sign(x.x) * log(abs(x.x) + sqrt(1.0 + x.x * x.x)), sign(x.y) * log(abs(x.y) + sqrt(1.0 + x.y * x.y)), sign(x.z) * log(abs(x.z) + sqrt(1.0 + x.z * x.z)), sign(x.w) * log(abs(x.w) + sqrt(1.0 + x.w * x.w)));
  91. }
  92. #endif
  93. #if defined(ACOSH_USED)
  94. highp float acosh(highp float x) {
  95. return log(x + sqrt(x * x - 1.0));
  96. }
  97. highp vec2 acosh(highp vec2 x) {
  98. return vec2(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)));
  99. }
  100. highp vec3 acosh(highp vec3 x) {
  101. return vec3(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)), log(x.z + sqrt(x.z * x.z - 1.0)));
  102. }
  103. highp vec4 acosh(highp vec4 x) {
  104. return vec4(log(x.x + sqrt(x.x * x.x - 1.0)), log(x.y + sqrt(x.y * x.y - 1.0)), log(x.z + sqrt(x.z * x.z - 1.0)), log(x.w + sqrt(x.w * x.w - 1.0)));
  105. }
  106. #endif
  107. #if defined(ATANH_USED)
  108. highp float atanh(highp float x) {
  109. return 0.5 * log((1.0 + x) / (1.0 - x));
  110. }
  111. highp vec2 atanh(highp vec2 x) {
  112. return 0.5 * vec2(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)));
  113. }
  114. highp vec3 atanh(highp vec3 x) {
  115. return 0.5 * vec3(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)), log((1.0 + x.z) / (1.0 - x.z)));
  116. }
  117. highp vec4 atanh(highp vec4 x) {
  118. return 0.5 * vec4(log((1.0 + x.x) / (1.0 - x.x)), log((1.0 + x.y) / (1.0 - x.y)), log((1.0 + x.z) / (1.0 - x.z)), log((1.0 + x.w) / (1.0 - x.w)));
  119. }
  120. #endif
  121. #if defined(ROUND_USED)
  122. highp float round(highp float x) {
  123. return floor(x + 0.5);
  124. }
  125. highp vec2 round(highp vec2 x) {
  126. return floor(x + vec2(0.5));
  127. }
  128. highp vec3 round(highp vec3 x) {
  129. return floor(x + vec3(0.5));
  130. }
  131. highp vec4 round(highp vec4 x) {
  132. return floor(x + vec4(0.5));
  133. }
  134. #endif
  135. #if defined(ROUND_EVEN_USED)
  136. highp float roundEven(highp float x) {
  137. highp float t = x + 0.5;
  138. highp float f = floor(t);
  139. highp float r;
  140. if (t == f) {
  141. if (x > 0)
  142. r = f - mod(f, 2);
  143. else
  144. r = f + mod(f, 2);
  145. } else
  146. r = f;
  147. return r;
  148. }
  149. highp vec2 roundEven(highp vec2 x) {
  150. return vec2(roundEven(x.x), roundEven(x.y));
  151. }
  152. highp vec3 roundEven(highp vec3 x) {
  153. return vec3(roundEven(x.x), roundEven(x.y), roundEven(x.z));
  154. }
  155. highp vec4 roundEven(highp vec4 x) {
  156. return vec4(roundEven(x.x), roundEven(x.y), roundEven(x.z), roundEven(x.w));
  157. }
  158. #endif
  159. #if defined(IS_INF_USED)
  160. bool isinf(highp float x) {
  161. return (2 * x == x) && (x != 0);
  162. }
  163. bvec2 isinf(highp vec2 x) {
  164. return bvec2((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0));
  165. }
  166. bvec3 isinf(highp vec3 x) {
  167. return bvec3((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0), (2 * x.z == x.z) && (x.z != 0));
  168. }
  169. bvec4 isinf(highp vec4 x) {
  170. return bvec4((2 * x.x == x.x) && (x.x != 0), (2 * x.y == x.y) && (x.y != 0), (2 * x.z == x.z) && (x.z != 0), (2 * x.w == x.w) && (x.w != 0));
  171. }
  172. #endif
  173. #if defined(IS_NAN_USED)
  174. bool isnan(highp float x) {
  175. return x != x;
  176. }
  177. bvec2 isnan(highp vec2 x) {
  178. return bvec2(x.x != x.x, x.y != x.y);
  179. }
  180. bvec3 isnan(highp vec3 x) {
  181. return bvec3(x.x != x.x, x.y != x.y, x.z != x.z);
  182. }
  183. bvec4 isnan(highp vec4 x) {
  184. return bvec4(x.x != x.x, x.y != x.y, x.z != x.z, x.w != x.w);
  185. }
  186. #endif
  187. #if defined(TRUNC_USED)
  188. highp float trunc(highp float x) {
  189. return x < 0.0 ? -floor(-x) : floor(x);
  190. }
  191. highp vec2 trunc(highp vec2 x) {
  192. return vec2(x.x < 0.0 ? -floor(-x.x) : floor(x.x), x.y < 0.0 ? -floor(-x.y) : floor(x.y));
  193. }
  194. highp vec3 trunc(highp vec3 x) {
  195. return vec3(x.x < 0.0 ? -floor(-x.x) : floor(x.x), x.y < 0.0 ? -floor(-x.y) : floor(x.y), x.z < 0.0 ? -floor(-x.z) : floor(x.z));
  196. }
  197. highp vec4 trunc(highp vec4 x) {
  198. return vec4(x.x < 0.0 ? -floor(-x.x) : floor(x.x), x.y < 0.0 ? -floor(-x.y) : floor(x.y), x.z < 0.0 ? -floor(-x.z) : floor(x.z), x.w < 0.0 ? -floor(-x.w) : floor(x.w));
  199. }
  200. #endif
  201. #if defined(DETERMINANT_USED)
  202. highp float determinant(highp mat2 m) {
  203. return m[0].x * m[1].y - m[1].x * m[0].y;
  204. }
  205. highp float determinant(highp mat3 m) {
  206. return m[0].x * (m[1].y * m[2].z - m[2].y * m[1].z) - m[1].x * (m[0].y * m[2].z - m[2].y * m[0].z) + m[2].x * (m[0].y * m[1].z - m[1].y * m[0].z);
  207. }
  208. highp float determinant(highp mat4 m) {
  209. highp float s00 = m[2].z * m[3].w - m[3].z * m[2].w;
  210. highp float s01 = m[2].y * m[3].w - m[3].y * m[2].w;
  211. highp float s02 = m[2].y * m[3].z - m[3].y * m[2].z;
  212. highp float s03 = m[2].x * m[3].w - m[3].x * m[2].w;
  213. highp float s04 = m[2].x * m[3].z - m[3].x * m[2].z;
  214. highp float s05 = m[2].x * m[3].y - m[3].x * m[2].y;
  215. highp vec4 c = vec4((m[1].y * s00 - m[1].z * s01 + m[1].w * s02), -(m[1].x * s00 - m[1].z * s03 + m[1].w * s04), (m[1].x * s01 - m[1].y * s03 + m[1].w * s05), -(m[1].x * s02 - m[1].y * s04 + m[1].z * s05));
  216. return m[0].x * c.x + m[0].y * c.y + m[0].z * c.z + m[0].w * c.w;
  217. }
  218. #endif
  219. #if defined(INVERSE_USED)
  220. highp mat2 inverse(highp mat2 m) {
  221. highp float d = 1.0 / (m[0].x * m[1].y - m[1].x * m[0].y);
  222. return mat2(
  223. vec2(m[1].y * d, -m[0].y * d),
  224. vec2(-m[1].x * d, m[0].x * d));
  225. }
  226. highp mat3 inverse(highp mat3 m) {
  227. highp float c01 = m[2].z * m[1].y - m[1].z * m[2].y;
  228. highp float c11 = -m[2].z * m[1].x + m[1].z * m[2].x;
  229. highp float c21 = m[2].y * m[1].x - m[1].y * m[2].x;
  230. highp float d = 1.0 / (m[0].x * c01 + m[0].y * c11 + m[0].z * c21);
  231. return mat3(c01, (-m[2].z * m[0].y + m[0].z * m[2].y), (m[1].z * m[0].y - m[0].z * m[1].y),
  232. c11, (m[2].z * m[0].x - m[0].z * m[2].x), (-m[1].z * m[0].x + m[0].z * m[1].x),
  233. c21, (-m[2].y * m[0].x + m[0].y * m[2].x), (m[1].y * m[0].x - m[0].y * m[1].x)) *
  234. d;
  235. }
  236. highp mat4 inverse(highp mat4 m) {
  237. highp float c00 = m[2].z * m[3].w - m[3].z * m[2].w;
  238. highp float c02 = m[1].z * m[3].w - m[3].z * m[1].w;
  239. highp float c03 = m[1].z * m[2].w - m[2].z * m[1].w;
  240. highp float c04 = m[2].y * m[3].w - m[3].y * m[2].w;
  241. highp float c06 = m[1].y * m[3].w - m[3].y * m[1].w;
  242. highp float c07 = m[1].y * m[2].w - m[2].y * m[1].w;
  243. highp float c08 = m[2].y * m[3].z - m[3].y * m[2].z;
  244. highp float c10 = m[1].y * m[3].z - m[3].y * m[1].z;
  245. highp float c11 = m[1].y * m[2].z - m[2].y * m[1].z;
  246. highp float c12 = m[2].x * m[3].w - m[3].x * m[2].w;
  247. highp float c14 = m[1].x * m[3].w - m[3].x * m[1].w;
  248. highp float c15 = m[1].x * m[2].w - m[2].x * m[1].w;
  249. highp float c16 = m[2].x * m[3].z - m[3].x * m[2].z;
  250. highp float c18 = m[1].x * m[3].z - m[3].x * m[1].z;
  251. highp float c19 = m[1].x * m[2].z - m[2].x * m[1].z;
  252. highp float c20 = m[2].x * m[3].y - m[3].x * m[2].y;
  253. highp float c22 = m[1].x * m[3].y - m[3].x * m[1].y;
  254. highp float c23 = m[1].x * m[2].y - m[2].x * m[1].y;
  255. vec4 f0 = vec4(c00, c00, c02, c03);
  256. vec4 f1 = vec4(c04, c04, c06, c07);
  257. vec4 f2 = vec4(c08, c08, c10, c11);
  258. vec4 f3 = vec4(c12, c12, c14, c15);
  259. vec4 f4 = vec4(c16, c16, c18, c19);
  260. vec4 f5 = vec4(c20, c20, c22, c23);
  261. vec4 v0 = vec4(m[1].x, m[0].x, m[0].x, m[0].x);
  262. vec4 v1 = vec4(m[1].y, m[0].y, m[0].y, m[0].y);
  263. vec4 v2 = vec4(m[1].z, m[0].z, m[0].z, m[0].z);
  264. vec4 v3 = vec4(m[1].w, m[0].w, m[0].w, m[0].w);
  265. vec4 inv0 = vec4(v1 * f0 - v2 * f1 + v3 * f2);
  266. vec4 inv1 = vec4(v0 * f0 - v2 * f3 + v3 * f4);
  267. vec4 inv2 = vec4(v0 * f1 - v1 * f3 + v3 * f5);
  268. vec4 inv3 = vec4(v0 * f2 - v1 * f4 + v2 * f5);
  269. vec4 sa = vec4(+1, -1, +1, -1);
  270. vec4 sb = vec4(-1, +1, -1, +1);
  271. mat4 inv = mat4(inv0 * sa, inv1 * sb, inv2 * sa, inv3 * sb);
  272. vec4 r0 = vec4(inv[0].x, inv[1].x, inv[2].x, inv[3].x);
  273. vec4 d0 = vec4(m[0] * r0);
  274. highp float d1 = (d0.x + d0.y) + (d0.z + d0.w);
  275. highp float d = 1.0 / d1;
  276. return inv * d;
  277. }
  278. #endif
  279. #ifndef USE_GLES_OVER_GL
  280. #if defined(TRANSPOSE_USED)
  281. highp mat2 transpose(highp mat2 m) {
  282. return mat2(
  283. vec2(m[0].x, m[1].x),
  284. vec2(m[0].y, m[1].y));
  285. }
  286. highp mat3 transpose(highp mat3 m) {
  287. return mat3(
  288. vec3(m[0].x, m[1].x, m[2].x),
  289. vec3(m[0].y, m[1].y, m[2].y),
  290. vec3(m[0].z, m[1].z, m[2].z));
  291. }
  292. #endif
  293. highp mat4 transpose(highp mat4 m) {
  294. return mat4(
  295. vec4(m[0].x, m[1].x, m[2].x, m[3].x),
  296. vec4(m[0].y, m[1].y, m[2].y, m[3].y),
  297. vec4(m[0].z, m[1].z, m[2].z, m[3].z),
  298. vec4(m[0].w, m[1].w, m[2].w, m[3].w));
  299. }
  300. #if defined(OUTER_PRODUCT_USED)
  301. highp mat2 outerProduct(highp vec2 c, highp vec2 r) {
  302. return mat2(c * r.x, c * r.y);
  303. }
  304. highp mat3 outerProduct(highp vec3 c, highp vec3 r) {
  305. return mat3(c * r.x, c * r.y, c * r.z);
  306. }
  307. highp mat4 outerProduct(highp vec4 c, highp vec4 r) {
  308. return mat4(c * r.x, c * r.y, c * r.z, c * r.w);
  309. }
  310. #endif
  311. #endif