common.shader 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. bgfx_shaders = {
  2. common = {
  3. code = "
  4. /*
  5. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  6. * License: http://www.opensource.org/licenses/BSD-2-Clause
  7. */
  8. #if !defined(BGFX_CONFIG_MAX_BONES)
  9. # define BGFX_CONFIG_MAX_BONES 32
  10. #endif // !defined(BGFX_CONFIG_MAX_BONES)
  11. #ifndef __cplusplus
  12. #if BGFX_SHADER_LANGUAGE_HLSL
  13. # define dFdx(_x) ddx(_x)
  14. # define dFdy(_y) ddy(-_y)
  15. # define inversesqrt(_x) rsqrt(_x)
  16. # define fract(_x) frac(_x)
  17. # define bvec2 bool2
  18. # define bvec3 bool3
  19. # define bvec4 bool4
  20. # if BGFX_SHADER_LANGUAGE_HLSL > 3
  21. struct BgfxSampler2D
  22. {
  23. SamplerState m_sampler;
  24. Texture2D m_texture;
  25. };
  26. vec4 bgfxTexture2D(BgfxSampler2D _sampler, vec2 _coord)
  27. {
  28. return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
  29. }
  30. vec4 bgfxTexture2DLod(BgfxSampler2D _sampler, vec2 _coord, float _level)
  31. {
  32. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level);
  33. }
  34. vec4 bgfxTexture2DProj(BgfxSampler2D _sampler, vec3 _coord)
  35. {
  36. vec2 coord = _coord.xy * rcp(_coord.z);
  37. return _sampler.m_texture.Sample(_sampler.m_sampler, coord);
  38. }
  39. vec4 bgfxTexture2DProj(BgfxSampler2D _sampler, vec4 _coord)
  40. {
  41. vec2 coord = _coord.xy * rcp(_coord.w);
  42. return _sampler.m_texture.Sample(_sampler.m_sampler, coord);
  43. }
  44. struct BgfxSampler2DShadow
  45. {
  46. SamplerComparisonState m_sampler;
  47. Texture2D m_texture;
  48. };
  49. float bgfxShadow2D(BgfxSampler2DShadow _sampler, vec3 _coord)
  50. {
  51. return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xy, _coord.z * 2.0 - 1.0);
  52. }
  53. float bgfxShadow2DProj(BgfxSampler2DShadow _sampler, vec4 _coord)
  54. {
  55. vec3 coord = _coord.xyz * rcp(_coord.w);
  56. return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, coord.xy, coord.z * 2.0 - 1.0);
  57. }
  58. struct BgfxSampler3D
  59. {
  60. SamplerState m_sampler;
  61. Texture3D m_texture;
  62. };
  63. struct BgfxISampler3D
  64. {
  65. Texture3D<ivec4> m_texture;
  66. };
  67. struct BgfxUSampler3D
  68. {
  69. Texture3D<uvec4> m_texture;
  70. };
  71. vec4 bgfxTexture3D(BgfxSampler3D _sampler, vec3 _coord)
  72. {
  73. return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
  74. }
  75. vec4 bgfxTexture3DLod(BgfxSampler3D _sampler, vec3 _coord, float _level)
  76. {
  77. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level);
  78. }
  79. ivec4 bgfxTexture3D(BgfxISampler3D _sampler, vec3 _coord)
  80. {
  81. ivec3 size;
  82. _sampler.m_texture.GetDimensions(size.x, size.y, size.z);
  83. return _sampler.m_texture.Load(ivec4(_coord * size, 0) );
  84. }
  85. uvec4 bgfxTexture3D(BgfxUSampler3D _sampler, vec3 _coord)
  86. {
  87. uvec3 size;
  88. _sampler.m_texture.GetDimensions(size.x, size.y, size.z);
  89. return _sampler.m_texture.Load(uvec4(_coord * size, 0) );
  90. }
  91. struct BgfxSamplerCube
  92. {
  93. SamplerState m_sampler;
  94. TextureCube m_texture;
  95. };
  96. vec4 bgfxTextureCube(BgfxSamplerCube _sampler, vec3 _coord)
  97. {
  98. return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
  99. }
  100. vec4 bgfxTextureCubeLod(BgfxSamplerCube _sampler, vec3 _coord, float _level)
  101. {
  102. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level);
  103. }
  104. # define SAMPLER2D(_name, _reg) \\
  105. uniform SamplerState _name ## Sampler : register(s[_reg]); \\
  106. uniform Texture2D _name ## Texture : register(t[_reg]); \\
  107. static BgfxSampler2D _name = { _name ## Sampler, _name ## Texture }
  108. # define sampler2D BgfxSampler2D
  109. # define texture2D(_sampler, _coord) bgfxTexture2D(_sampler, _coord)
  110. # define texture2DLod(_sampler, _coord, _level) bgfxTexture2DLod(_sampler, _coord, _level)
  111. # define texture2DProj(_sampler, _coord) bgfxTexture2DProj(_sampler, _coord)
  112. # define SAMPLER2DSHADOW(_name, _reg) \\
  113. uniform SamplerComparisonState _name ## Sampler : register(s[_reg]); \\
  114. uniform Texture2D _name ## Texture : register(t[_reg]); \\
  115. static BgfxSampler2DShadow _name = { _name ## Sampler, _name ## Texture }
  116. # define sampler2DShadow BgfxSampler2DShadow
  117. # define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord)
  118. # define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord)
  119. # define SAMPLER3D(_name, _reg) \\
  120. uniform SamplerState _name ## Sampler : register(s[_reg]); \\
  121. uniform Texture3D _name ## Texture : register(t[_reg]); \\
  122. static BgfxSampler3D _name = { _name ## Sampler, _name ## Texture }
  123. # define ISAMPLER3D(_name, _reg) \\
  124. uniform Texture3D<ivec4> _name ## Texture : register(t[_reg]); \\
  125. static BgfxISampler3D _name = { _name ## Texture }
  126. # define USAMPLER3D(_name, _reg) \\
  127. uniform Texture3D<uvec4> _name ## Texture : register(t[_reg]); \\
  128. static BgfxUSampler3D _name = { _name ## Texture }
  129. # define sampler3D BgfxSampler3D
  130. # define texture3D(_sampler, _coord) bgfxTexture3D(_sampler, _coord)
  131. # define texture3DLod(_sampler, _coord, _level) bgfxTexture3DLod(_sampler, _coord, _level)
  132. # define SAMPLERCUBE(_name, _reg) \\
  133. uniform SamplerState _name ## Sampler : register(s[_reg]); \\
  134. uniform TextureCube _name ## Texture : register(t[_reg]); \\
  135. static BgfxSamplerCube _name = { _name ## Sampler, _name ## Texture }
  136. # define samplerCube BgfxSamplerCube
  137. # define textureCube(_sampler, _coord) bgfxTextureCube(_sampler, _coord)
  138. # define textureCubeLod(_sampler, _coord, _level) bgfxTextureCubeLod(_sampler, _coord, _level)
  139. # else
  140. # define sampler2DShadow sampler2D
  141. vec4 bgfxTexture2DProj(sampler2D _sampler, vec3 _coord)
  142. {
  143. return tex2Dproj(_sampler, vec4(_coord.xy, 0.0, _coord.z) );
  144. }
  145. vec4 bgfxTexture2DProj(sampler2D _sampler, vec4 _coord)
  146. {
  147. return tex2Dproj(_sampler, _coord);
  148. }
  149. float bgfxShadow2D(sampler2DShadow _sampler, vec3 _coord)
  150. {
  151. #if 0
  152. float occluder = tex2D(_sampler, _coord.xy).x;
  153. return step(_coord.z * 2.0 - 1.0, occluder);
  154. #else
  155. return tex2Dproj(_sampler, vec4(_coord.xy, _coord.z * 2.0 - 1.0, 1.0) ).x;
  156. #endif // 0
  157. }
  158. float bgfxShadow2DProj(sampler2DShadow _sampler, vec4 _coord)
  159. {
  160. #if 0
  161. vec3 coord = _coord.xyz * rcp(_coord.w);
  162. float occluder = tex2D(_sampler, coord.xy).x;
  163. return step(coord.z * 2.0 - 1.0, occluder);
  164. #else
  165. return tex2Dproj(_sampler, _coord).x;
  166. #endif // 0
  167. }
  168. # define SAMPLER2D(_name, _reg) uniform sampler2D _name : register(s ## _reg)
  169. # define texture2D(_sampler, _coord) tex2D(_sampler, _coord)
  170. # define texture2DProj(_sampler, _coord) bgfxTexture2DProj(_sampler, _coord)
  171. # define SAMPLER2DSHADOW(_name, _reg) uniform sampler2DShadow _name : register(s ## _reg)
  172. # define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord)
  173. # define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord)
  174. # define SAMPLER3D(_name, _reg) uniform sampler3D _name : register(s ## _reg)
  175. # define texture3D(_sampler, _coord) tex3D(_sampler, _coord)
  176. # define SAMPLERCUBE(_name, _reg) uniform samplerCUBE _name : register(s[_reg])
  177. # define textureCube(_sampler, _coord) texCUBE(_sampler, _coord)
  178. # if BGFX_SHADER_LANGUAGE_HLSL == 2
  179. # define texture2DLod(_sampler, _coord, _level) tex2D(_sampler, (_coord).xy)
  180. # define texture3DLod(_sampler, _coord, _level) tex3D(_sampler, (_coord).xyz)
  181. # define textureCubeLod(_sampler, _coord, _level) texCUBE(_sampler, (_coord).xyz)
  182. # else
  183. # define texture2DLod(_sampler, _coord, _level) tex2Dlod(_sampler, vec4( (_coord).xy, 0.0, _level) )
  184. # define texture3DLod(_sampler, _coord, _level) tex3Dlod(_sampler, vec4( (_coord).xyz, _level) )
  185. # define textureCubeLod(_sampler, _coord, _level) texCUBElod(_sampler, vec4( (_coord).xyz, _level) )
  186. # endif // BGFX_SHADER_LANGUAGE_HLSL == 2
  187. # endif // BGFX_SHADER_LANGUAGE_HLSL > 3
  188. vec2 vec2_splat(float _x) { return vec2(_x, _x); }
  189. vec3 vec3_splat(float _x) { return vec3(_x, _x, _x); }
  190. vec4 vec4_splat(float _x) { return vec4(_x, _x, _x, _x); }
  191. uvec2 uvec2_splat(uint _x) { return uvec2(_x, _x); }
  192. uvec3 uvec3_splat(uint _x) { return uvec3(_x, _x, _x); }
  193. uvec4 uvec4_splat(uint _x) { return uvec4(_x, _x, _x, _x); }
  194. vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_mtx, _vec); }
  195. vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_vec, _mtx); }
  196. vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_mtx, _vec); }
  197. vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_vec, _mtx); }
  198. bvec2 lessThan(vec2 _a, vec2 _b) { return _a < _b; }
  199. bvec3 lessThan(vec3 _a, vec3 _b) { return _a < _b; }
  200. bvec4 lessThan(vec4 _a, vec4 _b) { return _a < _b; }
  201. bvec2 lessThanEqual(vec2 _a, vec2 _b) { return _a <= _b; }
  202. bvec3 lessThanEqual(vec3 _a, vec3 _b) { return _a <= _b; }
  203. bvec4 lessThanEqual(vec4 _a, vec4 _b) { return _a <= _b; }
  204. bvec2 greaterThan(vec2 _a, vec2 _b) { return _a > _b; }
  205. bvec3 greaterThan(vec3 _a, vec3 _b) { return _a > _b; }
  206. bvec4 greaterThan(vec4 _a, vec4 _b) { return _a > _b; }
  207. bvec2 greaterThanEqual(vec2 _a, vec2 _b) { return _a >= _b; }
  208. bvec3 greaterThanEqual(vec3 _a, vec3 _b) { return _a >= _b; }
  209. bvec4 greaterThanEqual(vec4 _a, vec4 _b) { return _a >= _b; }
  210. bvec2 notEqual(vec2 _a, vec2 _b) { return _a != _b; }
  211. bvec3 notEqual(vec3 _a, vec3 _b) { return _a != _b; }
  212. bvec4 notEqual(vec4 _a, vec4 _b) { return _a != _b; }
  213. bvec2 equal(vec2 _a, vec2 _b) { return _a == _b; }
  214. bvec3 equal(vec3 _a, vec3 _b) { return _a == _b; }
  215. bvec4 equal(vec4 _a, vec4 _b) { return _a == _b; }
  216. float mix(float _a, float _b, float _t) { return lerp(_a, _b, _t); }
  217. vec2 mix(vec2 _a, vec2 _b, vec2 _t) { return lerp(_a, _b, _t); }
  218. vec3 mix(vec3 _a, vec3 _b, vec3 _t) { return lerp(_a, _b, _t); }
  219. vec4 mix(vec4 _a, vec4 _b, vec4 _t) { return lerp(_a, _b, _t); }
  220. float mod(float _a, float _b) { return _a - _b * floor(_a / _b); }
  221. vec2 mod(vec2 _a, vec2 _b) { return _a - _b * floor(_a / _b); }
  222. vec3 mod(vec3 _a, vec3 _b) { return _a - _b * floor(_a / _b); }
  223. vec4 mod(vec4 _a, vec4 _b) { return _a - _b * floor(_a / _b); }
  224. #else
  225. # define atan2(_x, _y) atan(_x, _y)
  226. # define mul(_a, _b) ( (_a) * (_b) )
  227. # define saturate(_x) clamp(_x, 0.0, 1.0)
  228. # define SAMPLER2D(_name, _reg) uniform sampler2D _name
  229. # define SAMPLER3D(_name, _reg) uniform sampler3D _name
  230. # define SAMPLERCUBE(_name, _reg) uniform samplerCube _name
  231. # define SAMPLER2DSHADOW(_name, _reg) uniform sampler2DShadow _name
  232. # define vec2_splat(_x) vec2(_x)
  233. # define vec3_splat(_x) vec3(_x)
  234. # define vec4_splat(_x) vec4(_x)
  235. # define uvec2_splat(_x) uvec2(_x)
  236. # define uvec3_splat(_x) uvec3(_x)
  237. # define uvec4_splat(_x) uvec4(_x)
  238. # if BGFX_SHADER_LANGUAGE_GLSL >= 130
  239. # define ISAMPLER3D(_name, _reg) uniform isampler3D _name
  240. # define USAMPLER3D(_name, _reg) uniform usampler3D _name
  241. ivec4 texture3D(isampler3D _sampler, vec3 _coord) { return texture(_sampler, _coord); }
  242. uvec4 texture3D(usampler3D _sampler, vec3 _coord) { return texture(_sampler, _coord); }
  243. # endif // BGFX_SHADER_LANGUAGE_GLSL >= 130
  244. vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_vec, _mtx); }
  245. vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_mtx, _vec); }
  246. vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_vec, _mtx); }
  247. vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_mtx, _vec); }
  248. float rcp(float _a) { return 1.0/_a; }
  249. vec2 rcp(vec2 _a) { return vec2(1.0)/_a; }
  250. vec3 rcp(vec3 _a) { return vec3(1.0)/_a; }
  251. vec4 rcp(vec4 _a) { return vec4(1.0)/_a; }
  252. #endif // BGFX_SHADER_LANGUAGE_*
  253. uniform vec4 u_viewRect;
  254. uniform vec4 u_viewTexel;
  255. uniform mat4 u_view;
  256. uniform mat4 u_invView;
  257. uniform mat4 u_proj;
  258. uniform mat4 u_invProj;
  259. uniform mat4 u_viewProj;
  260. uniform mat4 u_invViewProj;
  261. uniform mat4 u_model[BGFX_CONFIG_MAX_BONES];
  262. uniform mat4 u_modelView;
  263. uniform mat4 u_modelViewProj;
  264. uniform vec4 u_alphaRef4;
  265. #define u_alphaRef u_alphaRef4.x
  266. #endif // __cplusplus
  267. vec4 encodeRE8(float _r)
  268. {
  269. float exponent = ceil(log2(_r) );
  270. return vec4(_r / exp2(exponent)
  271. , 0.0
  272. , 0.0
  273. , (exponent + 128.0) / 255.0
  274. );
  275. }
  276. float decodeRE8(vec4 _re8)
  277. {
  278. float exponent = _re8.w * 255.0 - 128.0;
  279. return _re8.x * exp2(exponent);
  280. }
  281. vec4 encodeRGBE8(vec3 _rgb)
  282. {
  283. vec4 rgbe8;
  284. float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z);
  285. float exponent = ceil(log2(maxComponent) );
  286. rgbe8.xyz = _rgb / exp2(exponent);
  287. rgbe8.w = (exponent + 128.0) / 255.0;
  288. return rgbe8;
  289. }
  290. vec3 decodeRGBE8(vec4 _rgbe8)
  291. {
  292. float exponent = _rgbe8.w * 255.0 - 128.0;
  293. vec3 rgb = _rgbe8.xyz * exp2(exponent);
  294. return rgb;
  295. }
  296. vec3 encodeNormalUint(vec3 _normal)
  297. {
  298. return _normal * 0.5 + 0.5;
  299. }
  300. vec3 decodeNormalUint(vec3 _encodedNormal)
  301. {
  302. return _encodedNormal * 2.0 - 1.0;
  303. }
  304. vec2 encodeNormalSphereMap(vec3 _normal)
  305. {
  306. return normalize(_normal.xy) * sqrt(_normal.z * 0.5 + 0.5);
  307. }
  308. vec3 decodeNormalSphereMap(vec2 _encodedNormal)
  309. {
  310. float zz = dot(_encodedNormal, _encodedNormal) * 2.0 - 1.0;
  311. return vec3(normalize(_encodedNormal.xy) * sqrt(1.0 - zz*zz), zz);
  312. }
  313. // Reference:
  314. // Octahedron normal vector encoding
  315. // http://kriscg.blogspot.com/2014/04/octahedron-normal-vector-encoding.html
  316. vec2 octahedronWrap(vec2 _val)
  317. {
  318. return (1.0 - abs(_val.yx) )
  319. * mix(vec2_splat(-1.0), vec2_splat(1.0), vec2(greaterThanEqual(_val.xy, vec2_splat(0.0) ) ) );
  320. }
  321. vec2 encodeNormalOctahedron(vec3 _normal)
  322. {
  323. _normal /= abs(_normal.x) + abs(_normal.y) + abs(_normal.z);
  324. _normal.xy = _normal.z >= 0.0 ? _normal.xy : octahedronWrap(_normal.xy);
  325. _normal.xy = _normal.xy * 0.5 + 0.5;
  326. return _normal.xy;
  327. }
  328. vec3 decodeNormalOctahedron(vec2 _encodedNormal)
  329. {
  330. _encodedNormal = _encodedNormal * 2.0 - 1.0;
  331. vec3 normal;
  332. normal.z = 1.0 - abs(_encodedNormal.x) - abs(_encodedNormal.y);
  333. normal.xy = normal.z >= 0.0 ? _encodedNormal.xy : octahedronWrap(_encodedNormal.xy);
  334. return normalize(normal);
  335. }
  336. // Reference:
  337. // RGB/XYZ Matrices
  338. // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  339. vec3 convertRGB2XYZ(vec3 _rgb)
  340. {
  341. vec3 xyz;
  342. xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb);
  343. xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb);
  344. xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb);
  345. return xyz;
  346. }
  347. vec3 convertXYZ2RGB(vec3 _xyz)
  348. {
  349. vec3 rgb;
  350. rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz);
  351. rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz);
  352. rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz);
  353. return rgb;
  354. }
  355. vec3 convertXYZ2Yxy(vec3 _xyz)
  356. {
  357. // Reference:
  358. // http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html
  359. float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) );
  360. return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv);
  361. }
  362. vec3 convertYxy2XYZ(vec3 _Yxy)
  363. {
  364. // Reference:
  365. // http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html
  366. vec3 xyz;
  367. xyz.x = _Yxy.x*_Yxy.y/_Yxy.z;
  368. xyz.y = _Yxy.x;
  369. xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z;
  370. return xyz;
  371. }
  372. vec3 convertRGB2Yxy(vec3 _rgb)
  373. {
  374. return convertXYZ2Yxy(convertRGB2XYZ(_rgb) );
  375. }
  376. vec3 convertYxy2RGB(vec3 _Yxy)
  377. {
  378. return convertXYZ2RGB(convertYxy2XYZ(_Yxy) );
  379. }
  380. vec3 convertRGB2Yuv(vec3 _rgb)
  381. {
  382. vec3 yuv;
  383. yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) );
  384. yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5;
  385. yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5;
  386. return yuv;
  387. }
  388. vec3 convertYuv2RGB(vec3 _yuv)
  389. {
  390. vec3 rgb;
  391. rgb.x = _yuv.x + 1.403*(_yuv.y-0.5);
  392. rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5);
  393. rgb.z = _yuv.x + 1.773*(_yuv.z-0.5);
  394. return rgb;
  395. }
  396. vec3 convertRGB2YIQ(vec3 _rgb)
  397. {
  398. vec3 yiq;
  399. yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb);
  400. yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb);
  401. yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb);
  402. return yiq;
  403. }
  404. vec3 convertYIQ2RGB(vec3 _yiq)
  405. {
  406. vec3 rgb;
  407. rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq);
  408. rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq);
  409. rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq);
  410. return rgb;
  411. }
  412. vec3 toLinear(vec3 _rgb)
  413. {
  414. return pow(abs(_rgb), vec3_splat(2.2) );
  415. }
  416. vec4 toLinear(vec4 _rgba)
  417. {
  418. return vec4(toLinear(_rgba.xyz), _rgba.w);
  419. }
  420. vec3 toLinearAccurate(vec3 _rgb)
  421. {
  422. vec3 lo = _rgb / 12.92;
  423. vec3 hi = pow( (_rgb + 0.055) / 1.055, vec3_splat(2.4) );
  424. vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.04045) ) ) );
  425. return rgb;
  426. }
  427. vec4 toLinearAccurate(vec4 _rgba)
  428. {
  429. return vec4(toLinearAccurate(_rgba.xyz), _rgba.w);
  430. }
  431. float toGamma(float _r)
  432. {
  433. return pow(abs(_r), 1.0/2.2);
  434. }
  435. vec3 toGamma(vec3 _rgb)
  436. {
  437. return pow(abs(_rgb), vec3_splat(1.0/2.2) );
  438. }
  439. vec4 toGamma(vec4 _rgba)
  440. {
  441. return vec4(toGamma(_rgba.xyz), _rgba.w);
  442. }
  443. vec3 toGammaAccurate(vec3 _rgb)
  444. {
  445. vec3 lo = _rgb * 12.92;
  446. vec3 hi = pow(abs(_rgb), vec3_splat(1.0/2.4) ) * 1.055 - 0.055;
  447. vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.0031308) ) ) );
  448. return rgb;
  449. }
  450. vec4 toGammaAccurate(vec4 _rgba)
  451. {
  452. return vec4(toGammaAccurate(_rgba.xyz), _rgba.w);
  453. }
  454. vec3 toReinhard(vec3 _rgb)
  455. {
  456. return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) );
  457. }
  458. vec4 toReinhard(vec4 _rgba)
  459. {
  460. return vec4(toReinhard(_rgba.xyz), _rgba.w);
  461. }
  462. vec3 toFilmic(vec3 _rgb)
  463. {
  464. _rgb = max(vec3_splat(0.0), _rgb - 0.004);
  465. _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06);
  466. return _rgb;
  467. }
  468. vec4 toFilmic(vec4 _rgba)
  469. {
  470. return vec4(toFilmic(_rgba.xyz), _rgba.w);
  471. }
  472. vec3 luma(vec3 _rgb)
  473. {
  474. float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb);
  475. return vec3_splat(yy);
  476. }
  477. vec4 luma(vec4 _rgba)
  478. {
  479. return vec4(luma(_rgba.xyz), _rgba.w);
  480. }
  481. vec3 conSatBri(vec3 _rgb, vec3 _csb)
  482. {
  483. vec3 rgb = _rgb * _csb.z;
  484. rgb = mix(luma(rgb), rgb, _csb.y);
  485. rgb = mix(vec3_splat(0.5), rgb, _csb.x);
  486. return rgb;
  487. }
  488. vec4 conSatBri(vec4 _rgba, vec3 _csb)
  489. {
  490. return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w);
  491. }
  492. vec3 posterize(vec3 _rgb, float _numColors)
  493. {
  494. return floor(_rgb*_numColors) / _numColors;
  495. }
  496. vec4 posterize(vec4 _rgba, float _numColors)
  497. {
  498. return vec4(posterize(_rgba.xyz, _numColors), _rgba.w);
  499. }
  500. vec3 sepia(vec3 _rgb)
  501. {
  502. vec3 color;
  503. color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) );
  504. color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) );
  505. color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) );
  506. return color;
  507. }
  508. vec4 sepia(vec4 _rgba)
  509. {
  510. return vec4(sepia(_rgba.xyz), _rgba.w);
  511. }
  512. vec3 blendOverlay(vec3 _base, vec3 _blend)
  513. {
  514. vec3 lt = 2.0 * _base * _blend;
  515. vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend);
  516. return mix(lt, gte, step(vec3_splat(0.5), _base) );
  517. }
  518. vec4 blendOverlay(vec4 _base, vec4 _blend)
  519. {
  520. return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w);
  521. }
  522. vec3 adjustHue(vec3 _rgb, float _hue)
  523. {
  524. vec3 yiq = convertRGB2YIQ(_rgb);
  525. float angle = _hue + atan2(yiq.z, yiq.y);
  526. float len = length(yiq.yz);
  527. return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) );
  528. }
  529. vec4 packFloatToRgba(float _value)
  530. {
  531. const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0);
  532. const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
  533. vec4 comp = fract(_value * shift);
  534. comp -= comp.xxyz * mask;
  535. return comp;
  536. }
  537. float unpackRgbaToFloat(vec4 _rgba)
  538. {
  539. const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
  540. return dot(_rgba, shift);
  541. }
  542. vec2 packHalfFloat(float _value)
  543. {
  544. const vec2 shift = vec2(256, 1.0);
  545. const vec2 mask = vec2(0, 1.0 / 256.0);
  546. vec2 comp = fract(_value * shift);
  547. comp -= comp.xx * mask;
  548. return comp;
  549. }
  550. float unpackHalfFloat(vec2 _rg)
  551. {
  552. const vec2 shift = vec2(1.0 / 256.0, 1.0);
  553. return dot(_rg, shift);
  554. }
  555. float random(vec2 _uv)
  556. {
  557. return fract(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453);
  558. }
  559. "
  560. }
  561. }