shaderlib.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #ifndef __SHADERLIB_SH__
  6. #define __SHADERLIB_SH__
  7. vec4 encodeRE8(float _r)
  8. {
  9. float exponent = ceil(log2(_r) );
  10. return vec4(_r / exp2(exponent)
  11. , 0.0
  12. , 0.0
  13. , (exponent + 128.0) / 255.0
  14. );
  15. }
  16. float decodeRE8(vec4 _re8)
  17. {
  18. float exponent = _re8.w * 255.0 - 128.0;
  19. return _re8.x * exp2(exponent);
  20. }
  21. vec4 encodeRGBE8(vec3 _rgb)
  22. {
  23. vec4 rgbe8;
  24. float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z);
  25. float exponent = ceil(log2(maxComponent) );
  26. rgbe8.xyz = _rgb / exp2(exponent);
  27. rgbe8.w = (exponent + 128.0) / 255.0;
  28. return rgbe8;
  29. }
  30. vec3 decodeRGBE8(vec4 _rgbe8)
  31. {
  32. float exponent = _rgbe8.w * 255.0 - 128.0;
  33. vec3 rgb = _rgbe8.xyz * exp2(exponent);
  34. return rgb;
  35. }
  36. vec3 encodeNormalUint(vec3 _normal)
  37. {
  38. return _normal * 0.5 + 0.5;
  39. }
  40. vec3 decodeNormalUint(vec3 _encodedNormal)
  41. {
  42. return _encodedNormal * 2.0 - 1.0;
  43. }
  44. vec2 encodeNormalSphereMap(vec3 _normal)
  45. {
  46. return normalize(_normal.xy) * sqrt(_normal.z * 0.5 + 0.5);
  47. }
  48. vec3 decodeNormalSphereMap(vec2 _encodedNormal)
  49. {
  50. float zz = dot(_encodedNormal, _encodedNormal) * 2.0 - 1.0;
  51. return vec3(normalize(_encodedNormal.xy) * sqrt(1.0 - zz*zz), zz);
  52. }
  53. vec2 octahedronWrap(vec2 _val)
  54. {
  55. // Reference(s):
  56. // - Octahedron normal vector encoding
  57. // https://web.archive.org/web/20191027010600/https://knarkowicz.wordpress.com/2014/04/16/octahedron-normal-vector-encoding/comment-page-1/
  58. return (1.0 - abs(_val.yx) )
  59. * mix(vec2_splat(-1.0), vec2_splat(1.0), vec2(greaterThanEqual(_val.xy, vec2_splat(0.0) ) ) );
  60. }
  61. vec2 encodeNormalOctahedron(vec3 _normal)
  62. {
  63. _normal /= abs(_normal.x) + abs(_normal.y) + abs(_normal.z);
  64. _normal.xy = _normal.z >= 0.0 ? _normal.xy : octahedronWrap(_normal.xy);
  65. _normal.xy = _normal.xy * 0.5 + 0.5;
  66. return _normal.xy;
  67. }
  68. vec3 decodeNormalOctahedron(vec2 _encodedNormal)
  69. {
  70. _encodedNormal = _encodedNormal * 2.0 - 1.0;
  71. vec3 normal;
  72. normal.z = 1.0 - abs(_encodedNormal.x) - abs(_encodedNormal.y);
  73. normal.xy = normal.z >= 0.0 ? _encodedNormal.xy : octahedronWrap(_encodedNormal.xy);
  74. return normalize(normal);
  75. }
  76. vec3 convertRGB2XYZ(vec3 _rgb)
  77. {
  78. // Reference(s):
  79. // - RGB/XYZ Matrices
  80. // https://web.archive.org/web/20191027010220/http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  81. vec3 xyz;
  82. xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb);
  83. xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb);
  84. xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb);
  85. return xyz;
  86. }
  87. vec3 convertXYZ2RGB(vec3 _xyz)
  88. {
  89. vec3 rgb;
  90. rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz);
  91. rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz);
  92. rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz);
  93. return rgb;
  94. }
  95. vec3 convertXYZ2Yxy(vec3 _xyz)
  96. {
  97. // Reference(s):
  98. // - XYZ to xyY
  99. // https://web.archive.org/web/20191027010144/http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html
  100. float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) );
  101. return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv);
  102. }
  103. vec3 convertYxy2XYZ(vec3 _Yxy)
  104. {
  105. // Reference(s):
  106. // - xyY to XYZ
  107. // https://web.archive.org/web/20191027010036/http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html
  108. vec3 xyz;
  109. xyz.x = _Yxy.x*_Yxy.y/_Yxy.z;
  110. xyz.y = _Yxy.x;
  111. xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z;
  112. return xyz;
  113. }
  114. vec3 convertRGB2Yxy(vec3 _rgb)
  115. {
  116. return convertXYZ2Yxy(convertRGB2XYZ(_rgb) );
  117. }
  118. vec3 convertYxy2RGB(vec3 _Yxy)
  119. {
  120. return convertXYZ2RGB(convertYxy2XYZ(_Yxy) );
  121. }
  122. vec3 convertRGB2Yuv(vec3 _rgb)
  123. {
  124. vec3 yuv;
  125. yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) );
  126. yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5;
  127. yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5;
  128. return yuv;
  129. }
  130. vec3 convertYuv2RGB(vec3 _yuv)
  131. {
  132. vec3 rgb;
  133. rgb.x = _yuv.x + 1.403*(_yuv.y-0.5);
  134. rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5);
  135. rgb.z = _yuv.x + 1.773*(_yuv.z-0.5);
  136. return rgb;
  137. }
  138. vec3 convertRGB2YIQ(vec3 _rgb)
  139. {
  140. vec3 yiq;
  141. yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb);
  142. yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb);
  143. yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb);
  144. return yiq;
  145. }
  146. vec3 convertYIQ2RGB(vec3 _yiq)
  147. {
  148. vec3 rgb;
  149. rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq);
  150. rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq);
  151. rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq);
  152. return rgb;
  153. }
  154. vec3 toLinear(vec3 _rgb)
  155. {
  156. return pow(abs(_rgb), vec3_splat(2.2) );
  157. }
  158. vec4 toLinear(vec4 _rgba)
  159. {
  160. return vec4(toLinear(_rgba.xyz), _rgba.w);
  161. }
  162. vec3 toLinearAccurate(vec3 _rgb)
  163. {
  164. vec3 lo = _rgb / 12.92;
  165. vec3 hi = pow( (_rgb + 0.055) / 1.055, vec3_splat(2.4) );
  166. vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.04045) ) ) );
  167. return rgb;
  168. }
  169. vec4 toLinearAccurate(vec4 _rgba)
  170. {
  171. return vec4(toLinearAccurate(_rgba.xyz), _rgba.w);
  172. }
  173. float toGamma(float _r)
  174. {
  175. return pow(abs(_r), 1.0/2.2);
  176. }
  177. vec3 toGamma(vec3 _rgb)
  178. {
  179. return pow(abs(_rgb), vec3_splat(1.0/2.2) );
  180. }
  181. vec4 toGamma(vec4 _rgba)
  182. {
  183. return vec4(toGamma(_rgba.xyz), _rgba.w);
  184. }
  185. vec3 toGammaAccurate(vec3 _rgb)
  186. {
  187. vec3 lo = _rgb * 12.92;
  188. vec3 hi = pow(abs(_rgb), vec3_splat(1.0/2.4) ) * 1.055 - 0.055;
  189. vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.0031308) ) ) );
  190. return rgb;
  191. }
  192. vec4 toGammaAccurate(vec4 _rgba)
  193. {
  194. return vec4(toGammaAccurate(_rgba.xyz), _rgba.w);
  195. }
  196. vec3 toReinhard(vec3 _rgb)
  197. {
  198. return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) );
  199. }
  200. vec4 toReinhard(vec4 _rgba)
  201. {
  202. return vec4(toReinhard(_rgba.xyz), _rgba.w);
  203. }
  204. vec3 toFilmic(vec3 _rgb)
  205. {
  206. _rgb = max(vec3_splat(0.0), _rgb - 0.004);
  207. _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06);
  208. return _rgb;
  209. }
  210. vec4 toFilmic(vec4 _rgba)
  211. {
  212. return vec4(toFilmic(_rgba.xyz), _rgba.w);
  213. }
  214. vec3 toAcesFilmic(vec3 _rgb)
  215. {
  216. // Reference(s):
  217. // - ACES Filmic Tone Mapping Curve
  218. // https://web.archive.org/web/20191027010704/https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
  219. float aa = 2.51f;
  220. float bb = 0.03f;
  221. float cc = 2.43f;
  222. float dd = 0.59f;
  223. float ee = 0.14f;
  224. return saturate( (_rgb*(aa*_rgb + bb) )/(_rgb*(cc*_rgb + dd) + ee) );
  225. }
  226. vec4 toAcesFilmic(vec4 _rgba)
  227. {
  228. return vec4(toAcesFilmic(_rgba.xyz), _rgba.w);
  229. }
  230. vec3 luma(vec3 _rgb)
  231. {
  232. float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb);
  233. return vec3_splat(yy);
  234. }
  235. vec4 luma(vec4 _rgba)
  236. {
  237. return vec4(luma(_rgba.xyz), _rgba.w);
  238. }
  239. vec3 conSatBri(vec3 _rgb, vec3 _csb)
  240. {
  241. vec3 rgb = _rgb * _csb.z;
  242. rgb = mix(luma(rgb), rgb, _csb.y);
  243. rgb = mix(vec3_splat(0.5), rgb, _csb.x);
  244. return rgb;
  245. }
  246. vec4 conSatBri(vec4 _rgba, vec3 _csb)
  247. {
  248. return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w);
  249. }
  250. vec3 posterize(vec3 _rgb, float _numColors)
  251. {
  252. return floor(_rgb*_numColors) / _numColors;
  253. }
  254. vec4 posterize(vec4 _rgba, float _numColors)
  255. {
  256. return vec4(posterize(_rgba.xyz, _numColors), _rgba.w);
  257. }
  258. vec3 sepia(vec3 _rgb)
  259. {
  260. vec3 color;
  261. color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) );
  262. color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) );
  263. color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) );
  264. return color;
  265. }
  266. vec4 sepia(vec4 _rgba)
  267. {
  268. return vec4(sepia(_rgba.xyz), _rgba.w);
  269. }
  270. vec3 blendOverlay(vec3 _base, vec3 _blend)
  271. {
  272. vec3 lt = 2.0 * _base * _blend;
  273. vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend);
  274. return mix(lt, gte, step(vec3_splat(0.5), _base) );
  275. }
  276. vec4 blendOverlay(vec4 _base, vec4 _blend)
  277. {
  278. return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w);
  279. }
  280. vec3 adjustHue(vec3 _rgb, float _hue)
  281. {
  282. vec3 yiq = convertRGB2YIQ(_rgb);
  283. float angle = _hue + atan2(yiq.z, yiq.y);
  284. float len = length(yiq.yz);
  285. return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) );
  286. }
  287. vec4 packFloatToRgba(float _value)
  288. {
  289. const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0);
  290. const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
  291. vec4 comp = fract(_value * shift);
  292. comp -= comp.xxyz * mask;
  293. return comp;
  294. }
  295. float unpackRgbaToFloat(vec4 _rgba)
  296. {
  297. const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
  298. return dot(_rgba, shift);
  299. }
  300. vec2 packHalfFloat(float _value)
  301. {
  302. const vec2 shift = vec2(256, 1.0);
  303. const vec2 mask = vec2(0, 1.0 / 256.0);
  304. vec2 comp = fract(_value * shift);
  305. comp -= comp.xx * mask;
  306. return comp;
  307. }
  308. float unpackHalfFloat(vec2 _rg)
  309. {
  310. const vec2 shift = vec2(1.0 / 256.0, 1.0);
  311. return dot(_rg, shift);
  312. }
  313. float random(vec2 _uv)
  314. {
  315. return fract(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453);
  316. }
  317. vec3 fixCubeLookup(vec3 _v, float _lod, float _topLevelCubeSize)
  318. {
  319. // Reference(s):
  320. // - Seamless cube-map filtering
  321. // https://web.archive.org/web/20190411181934/http://the-witness.net/news/2012/02/seamless-cube-map-filtering/
  322. float ax = abs(_v.x);
  323. float ay = abs(_v.y);
  324. float az = abs(_v.z);
  325. float vmax = max(max(ax, ay), az);
  326. float scale = 1.0 - exp2(_lod) / _topLevelCubeSize;
  327. if (ax != vmax) { _v.x *= scale; }
  328. if (ay != vmax) { _v.y *= scale; }
  329. if (az != vmax) { _v.z *= scale; }
  330. return _v;
  331. }
  332. vec2 texture2DBc5(sampler2D _sampler, vec2 _uv)
  333. {
  334. #if BGFX_SHADER_LANGUAGE_HLSL && BGFX_SHADER_LANGUAGE_HLSL <= 300
  335. return texture2D(_sampler, _uv).yx;
  336. #else
  337. return texture2D(_sampler, _uv).xy;
  338. #endif
  339. }
  340. mat3 cofactor(mat4 _m)
  341. {
  342. // Reference:
  343. // Cofactor of matrix. Use to transform normals. The code assumes the last column of _m is [0,0,0,1].
  344. // https://www.shadertoy.com/view/3s33zj
  345. // https://github.com/graphitemaster/normals_revisited
  346. return mat3(
  347. _m[1][1]*_m[2][2]-_m[1][2]*_m[2][1],
  348. _m[1][2]*_m[2][0]-_m[1][0]*_m[2][2],
  349. _m[1][0]*_m[2][1]-_m[1][1]*_m[2][0],
  350. _m[0][2]*_m[2][1]-_m[0][1]*_m[2][2],
  351. _m[0][0]*_m[2][2]-_m[0][2]*_m[2][0],
  352. _m[0][1]*_m[2][0]-_m[0][0]*_m[2][1],
  353. _m[0][1]*_m[1][2]-_m[0][2]*_m[1][1],
  354. _m[0][2]*_m[1][0]-_m[0][0]*_m[1][2],
  355. _m[0][0]*_m[1][1]-_m[0][1]*_m[1][0]
  356. );
  357. }
  358. float toClipSpaceDepth(float _depthTextureZ)
  359. {
  360. #if BGFX_SHADER_LANGUAGE_GLSL
  361. return _depthTextureZ * 2.0 - 1.0;
  362. #else
  363. return _depthTextureZ;
  364. #endif // BGFX_SHADER_LANGUAGE_GLSL
  365. }
  366. vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos)
  367. {
  368. vec4 wpos = mul(_invViewProj, vec4(_clipPos, 1.0) );
  369. return wpos.xyz / wpos.w;
  370. }
  371. #endif // __SHADERLIB_SH__