common.shader 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. sampler_states = {
  2. clamp_point = {
  3. wrap_u = "clamp"
  4. wrap_v = "clamp"
  5. wrap_w = "clamp"
  6. filter_min = "point"
  7. filter_mag = "point"
  8. }
  9. clamp_anisotropic = {
  10. wrap_u = "clamp"
  11. wrap_v = "clamp"
  12. wrap_w = "clamp"
  13. filter_min = "anisotropic"
  14. filter_mag = "anisotropic"
  15. }
  16. mirror_point = {
  17. wrap_u = "mirror"
  18. wrap_v = "mirror"
  19. wrap_w = "mirror"
  20. filter_min = "point"
  21. filter_mag = "point"
  22. }
  23. mirror_anisotropic = {
  24. wrap_u = "mirror"
  25. wrap_v = "mirror"
  26. wrap_w = "mirror"
  27. filter_min = "anisotropic"
  28. filter_mag = "anisotropic"
  29. }
  30. }
  31. bgfx_shaders = {
  32. common = {
  33. code = """
  34. /*
  35. * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
  36. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  37. */
  38. #ifndef BGFX_SHADER_H_HEADER_GUARD
  39. #define BGFX_SHADER_H_HEADER_GUARD
  40. #if !defined(BGFX_CONFIG_MAX_BONES)
  41. # define BGFX_CONFIG_MAX_BONES 192
  42. #endif // !defined(BGFX_CONFIG_MAX_BONES)
  43. #ifndef __cplusplus
  44. #if BGFX_SHADER_LANGUAGE_HLSL > 300
  45. # define BRANCH [branch]
  46. # define LOOP [loop]
  47. # define UNROLL [unroll]
  48. #else
  49. # define BRANCH
  50. # define LOOP
  51. # define UNROLL
  52. #endif // BGFX_SHADER_LANGUAGE_HLSL > 300
  53. #if (BGFX_SHADER_LANGUAGE_HLSL > 300 || BGFX_SHADER_LANGUAGE_METAL || BGFX_SHADER_LANGUAGE_SPIRV) && BGFX_SHADER_TYPE_FRAGMENT
  54. # define EARLY_DEPTH_STENCIL [earlydepthstencil]
  55. #else
  56. # define EARLY_DEPTH_STENCIL
  57. #endif // BGFX_SHADER_LANGUAGE_HLSL > 300 && BGFX_SHADER_TYPE_FRAGMENT
  58. #if BGFX_SHADER_LANGUAGE_GLSL
  59. # define ARRAY_BEGIN(_type, _name, _count) _type _name[_count] = _type[](
  60. # define ARRAY_END() )
  61. #else
  62. # define ARRAY_BEGIN(_type, _name, _count) _type _name[_count] = {
  63. # define ARRAY_END() }
  64. #endif // BGFX_SHADER_LANGUAGE_GLSL
  65. #if BGFX_SHADER_LANGUAGE_HLSL \
  66. || BGFX_SHADER_LANGUAGE_PSSL \
  67. || BGFX_SHADER_LANGUAGE_SPIRV \
  68. || BGFX_SHADER_LANGUAGE_METAL
  69. # define CONST(_x) static const _x
  70. # define dFdx(_x) ddx(_x)
  71. # define dFdy(_y) ddy(-(_y))
  72. # define inversesqrt(_x) rsqrt(_x)
  73. # define fract(_x) frac(_x)
  74. # define bvec2 bool2
  75. # define bvec3 bool3
  76. # define bvec4 bool4
  77. // To be able to patch the uav registers on the DXBC SPDB Chunk (D3D11 renderer) the whitespaces around
  78. // '_type[_reg]' are necessary. This only affects shaders with debug info (i.e., those that have the SPDB Chunk).
  79. # if BGFX_SHADER_LANGUAGE_HLSL > 400 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
  80. # define REGISTER(_type, _reg) register( _type[_reg] )
  81. # else
  82. # define REGISTER(_type, _reg) register(_type ## _reg)
  83. # endif // BGFX_SHADER_LANGUAGE_HLSL
  84. # if BGFX_SHADER_LANGUAGE_HLSL > 300 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
  85. # if BGFX_SHADER_LANGUAGE_HLSL > 400 || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
  86. # define dFdxCoarse(_x) ddx_coarse(_x)
  87. # define dFdxFine(_x) ddx_fine(_x)
  88. # define dFdyCoarse(_y) ddy_coarse(-(_y))
  89. # define dFdyFine(_y) ddy_fine(-(_y))
  90. # endif // BGFX_SHADER_LANGUAGE_HLSL > 400
  91. # if BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
  92. float intBitsToFloat(int _x) { return asfloat(_x); }
  93. vec2 intBitsToFloat(uint2 _x) { return asfloat(_x); }
  94. vec3 intBitsToFloat(uint3 _x) { return asfloat(_x); }
  95. vec4 intBitsToFloat(uint4 _x) { return asfloat(_x); }
  96. # endif // BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
  97. float uintBitsToFloat(uint _x) { return asfloat(_x); }
  98. vec2 uintBitsToFloat(uint2 _x) { return asfloat(_x); }
  99. vec3 uintBitsToFloat(uint3 _x) { return asfloat(_x); }
  100. vec4 uintBitsToFloat(uint4 _x) { return asfloat(_x); }
  101. uint floatBitsToUint(float _x) { return asuint(_x); }
  102. uvec2 floatBitsToUint(vec2 _x) { return asuint(_x); }
  103. uvec3 floatBitsToUint(vec3 _x) { return asuint(_x); }
  104. uvec4 floatBitsToUint(vec4 _x) { return asuint(_x); }
  105. int floatBitsToInt(float _x) { return asint(_x); }
  106. ivec2 floatBitsToInt(vec2 _x) { return asint(_x); }
  107. ivec3 floatBitsToInt(vec3 _x) { return asint(_x); }
  108. ivec4 floatBitsToInt(vec4 _x) { return asint(_x); }
  109. uint bitfieldReverse(uint _x) { return reversebits(_x); }
  110. uint2 bitfieldReverse(uint2 _x) { return reversebits(_x); }
  111. uint3 bitfieldReverse(uint3 _x) { return reversebits(_x); }
  112. uint4 bitfieldReverse(uint4 _x) { return reversebits(_x); }
  113. # if !BGFX_SHADER_LANGUAGE_SPIRV
  114. uint packHalf2x16(vec2 _x)
  115. {
  116. return (f32tof16(_x.y)<<16) | f32tof16(_x.x);
  117. }
  118. vec2 unpackHalf2x16(uint _x)
  119. {
  120. return vec2(f16tof32(_x & 0xffff), f16tof32(_x >> 16) );
  121. }
  122. # endif // !BGFX_SHADER_LANGUAGE_SPIRV
  123. struct BgfxSampler2D
  124. {
  125. SamplerState m_sampler;
  126. Texture2D m_texture;
  127. };
  128. struct BgfxISampler2D
  129. {
  130. Texture2D<ivec4> m_texture;
  131. };
  132. struct BgfxUSampler2D
  133. {
  134. Texture2D<uvec4> m_texture;
  135. };
  136. struct BgfxSampler2DArray
  137. {
  138. SamplerState m_sampler;
  139. Texture2DArray m_texture;
  140. };
  141. struct BgfxSampler2DShadow
  142. {
  143. SamplerComparisonState m_sampler;
  144. Texture2D m_texture;
  145. };
  146. struct BgfxSampler2DArrayShadow
  147. {
  148. SamplerComparisonState m_sampler;
  149. Texture2DArray m_texture;
  150. };
  151. struct BgfxSampler3D
  152. {
  153. SamplerState m_sampler;
  154. Texture3D m_texture;
  155. };
  156. struct BgfxISampler3D
  157. {
  158. Texture3D<ivec4> m_texture;
  159. };
  160. struct BgfxUSampler3D
  161. {
  162. Texture3D<uvec4> m_texture;
  163. };
  164. struct BgfxSamplerCube
  165. {
  166. SamplerState m_sampler;
  167. TextureCube m_texture;
  168. };
  169. struct BgfxSamplerCubeShadow
  170. {
  171. SamplerComparisonState m_sampler;
  172. TextureCube m_texture;
  173. };
  174. struct BgfxSampler2DMS
  175. {
  176. Texture2DMS<vec4> m_texture;
  177. };
  178. vec4 bgfxTexture2D(BgfxSampler2D _sampler, vec2 _coord)
  179. {
  180. return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
  181. }
  182. vec4 bgfxTexture2DBias(BgfxSampler2D _sampler, vec2 _coord, float _bias)
  183. {
  184. return _sampler.m_texture.SampleBias(_sampler.m_sampler, _coord, _bias);
  185. }
  186. vec4 bgfxTexture2DLod(BgfxSampler2D _sampler, vec2 _coord, float _level)
  187. {
  188. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level);
  189. }
  190. vec4 bgfxTexture2DLodOffset(BgfxSampler2D _sampler, vec2 _coord, float _level, ivec2 _offset)
  191. {
  192. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level, _offset);
  193. }
  194. vec4 bgfxTexture2DProj(BgfxSampler2D _sampler, vec3 _coord)
  195. {
  196. vec2 coord = _coord.xy * rcp(_coord.z);
  197. return _sampler.m_texture.Sample(_sampler.m_sampler, coord);
  198. }
  199. vec4 bgfxTexture2DProj(BgfxSampler2D _sampler, vec4 _coord)
  200. {
  201. vec2 coord = _coord.xy * rcp(_coord.w);
  202. return _sampler.m_texture.Sample(_sampler.m_sampler, coord);
  203. }
  204. vec4 bgfxTexture2DGrad(BgfxSampler2D _sampler, vec2 _coord, vec2 _dPdx, vec2 _dPdy)
  205. {
  206. return _sampler.m_texture.SampleGrad(_sampler.m_sampler, _coord, _dPdx, _dPdy);
  207. }
  208. vec4 bgfxTexture2DArray(BgfxSampler2DArray _sampler, vec3 _coord)
  209. {
  210. return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
  211. }
  212. vec4 bgfxTexture2DArrayLod(BgfxSampler2DArray _sampler, vec3 _coord, float _lod)
  213. {
  214. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _lod);
  215. }
  216. vec4 bgfxTexture2DArrayLodOffset(BgfxSampler2DArray _sampler, vec3 _coord, float _level, ivec2 _offset)
  217. {
  218. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level, _offset);
  219. }
  220. float bgfxShadow2D(BgfxSampler2DShadow _sampler, vec3 _coord)
  221. {
  222. return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xy, _coord.z);
  223. }
  224. float bgfxShadow2DProj(BgfxSampler2DShadow _sampler, vec4 _coord)
  225. {
  226. vec3 coord = _coord.xyz * rcp(_coord.w);
  227. return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, coord.xy, coord.z);
  228. }
  229. vec2 bgfxTextureSize(BgfxSampler2DShadow _sampler, int _lod)
  230. {
  231. vec2 result;
  232. float numberOfMipMapLevels;
  233. _sampler.m_texture.GetDimensions(_lod, result.x, result.y, numberOfMipMapLevels);
  234. return result;
  235. }
  236. vec4 bgfxShadow2DArray(BgfxSampler2DArrayShadow _sampler, vec4 _coord)
  237. {
  238. return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xyz, _coord.w);
  239. }
  240. vec2 bgfxTextureSize(BgfxSampler2DArrayShadow _sampler, int _lod)
  241. {
  242. vec2 result;
  243. float numberOfMipMapLevels;
  244. float numberOfElements;
  245. _sampler.m_texture.GetDimensions(_lod, result.x, result.y, numberOfElements, numberOfMipMapLevels);
  246. return result;
  247. }
  248. vec4 bgfxTexture3D(BgfxSampler3D _sampler, vec3 _coord)
  249. {
  250. return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
  251. }
  252. vec4 bgfxTexture3DLod(BgfxSampler3D _sampler, vec3 _coord, float _level)
  253. {
  254. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level);
  255. }
  256. ivec4 bgfxTexture3D(BgfxISampler3D _sampler, vec3 _coord)
  257. {
  258. uvec3 size;
  259. _sampler.m_texture.GetDimensions(size.x, size.y, size.z);
  260. return _sampler.m_texture.Load(ivec4(_coord * size, 0) );
  261. }
  262. uvec4 bgfxTexture3D(BgfxUSampler3D _sampler, vec3 _coord)
  263. {
  264. uvec3 size;
  265. _sampler.m_texture.GetDimensions(size.x, size.y, size.z);
  266. return _sampler.m_texture.Load(ivec4(_coord * size, 0) );
  267. }
  268. vec4 bgfxTextureCube(BgfxSamplerCube _sampler, vec3 _coord)
  269. {
  270. return _sampler.m_texture.Sample(_sampler.m_sampler, _coord);
  271. }
  272. vec4 bgfxTextureCubeBias(BgfxSamplerCube _sampler, vec3 _coord, float _bias)
  273. {
  274. return _sampler.m_texture.SampleBias(_sampler.m_sampler, _coord, _bias);
  275. }
  276. vec4 bgfxTextureCubeLod(BgfxSamplerCube _sampler, vec3 _coord, float _level)
  277. {
  278. return _sampler.m_texture.SampleLevel(_sampler.m_sampler, _coord, _level);
  279. }
  280. float bgfxShadowCube(BgfxSamplerCubeShadow _sampler, vec4 _coord)
  281. {
  282. return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xyz, _coord.w);
  283. }
  284. vec4 bgfxTexelFetch(BgfxSampler2D _sampler, ivec2 _coord, int _lod)
  285. {
  286. return _sampler.m_texture.Load(ivec3(_coord, _lod) );
  287. }
  288. vec4 bgfxTexelFetchOffset(BgfxSampler2D _sampler, ivec2 _coord, int _lod, ivec2 _offset)
  289. {
  290. return _sampler.m_texture.Load(ivec3(_coord, _lod), _offset );
  291. }
  292. vec2 bgfxTextureSize(BgfxSampler2D _sampler, int _lod)
  293. {
  294. vec2 result;
  295. float numberOfMipMapLevels;
  296. _sampler.m_texture.GetDimensions(_lod, result.x, result.y, numberOfMipMapLevels);
  297. return result;
  298. }
  299. vec2 bgfxTextureSize(BgfxISampler2D _sampler, int _lod)
  300. {
  301. vec2 result;
  302. float numberOfMipMapLevels;
  303. _sampler.m_texture.GetDimensions(_lod, result.x, result.y, numberOfMipMapLevels);
  304. return result;
  305. }
  306. vec2 bgfxTextureSize(BgfxUSampler2D _sampler, int _lod)
  307. {
  308. vec2 result;
  309. float numberOfMipMapLevels;
  310. _sampler.m_texture.GetDimensions(_lod, result.x, result.y, numberOfMipMapLevels);
  311. return result;
  312. }
  313. vec4 bgfxTextureGather0(BgfxSampler2D _sampler, vec2 _coord)
  314. {
  315. return _sampler.m_texture.GatherRed(_sampler.m_sampler, _coord);
  316. }
  317. vec4 bgfxTextureGather1(BgfxSampler2D _sampler, vec2 _coord)
  318. {
  319. return _sampler.m_texture.GatherGreen(_sampler.m_sampler, _coord);
  320. }
  321. vec4 bgfxTextureGather2(BgfxSampler2D _sampler, vec2 _coord)
  322. {
  323. return _sampler.m_texture.GatherBlue(_sampler.m_sampler, _coord);
  324. }
  325. vec4 bgfxTextureGather3(BgfxSampler2D _sampler, vec2 _coord)
  326. {
  327. return _sampler.m_texture.GatherAlpha(_sampler.m_sampler, _coord);
  328. }
  329. vec4 bgfxTextureGatherOffset0(BgfxSampler2D _sampler, vec2 _coord, ivec2 _offset)
  330. {
  331. return _sampler.m_texture.GatherRed(_sampler.m_sampler, _coord, _offset);
  332. }
  333. vec4 bgfxTextureGatherOffset1(BgfxSampler2D _sampler, vec2 _coord, ivec2 _offset)
  334. {
  335. return _sampler.m_texture.GatherGreen(_sampler.m_sampler, _coord, _offset);
  336. }
  337. vec4 bgfxTextureGatherOffset2(BgfxSampler2D _sampler, vec2 _coord, ivec2 _offset)
  338. {
  339. return _sampler.m_texture.GatherBlue(_sampler.m_sampler, _coord, _offset);
  340. }
  341. vec4 bgfxTextureGatherOffset3(BgfxSampler2D _sampler, vec2 _coord, ivec2 _offset)
  342. {
  343. return _sampler.m_texture.GatherAlpha(_sampler.m_sampler, _coord, _offset);
  344. }
  345. vec4 bgfxTextureGather0(BgfxSampler2DArray _sampler, vec3 _coord)
  346. {
  347. return _sampler.m_texture.GatherRed(_sampler.m_sampler, _coord);
  348. }
  349. vec4 bgfxTextureGather1(BgfxSampler2DArray _sampler, vec3 _coord)
  350. {
  351. return _sampler.m_texture.GatherGreen(_sampler.m_sampler, _coord);
  352. }
  353. vec4 bgfxTextureGather2(BgfxSampler2DArray _sampler, vec3 _coord)
  354. {
  355. return _sampler.m_texture.GatherBlue(_sampler.m_sampler, _coord);
  356. }
  357. vec4 bgfxTextureGather3(BgfxSampler2DArray _sampler, vec3 _coord)
  358. {
  359. return _sampler.m_texture.GatherAlpha(_sampler.m_sampler, _coord);
  360. }
  361. ivec4 bgfxTexelFetch(BgfxISampler2D _sampler, ivec2 _coord, int _lod)
  362. {
  363. return _sampler.m_texture.Load(ivec3(_coord, _lod) );
  364. }
  365. uvec4 bgfxTexelFetch(BgfxUSampler2D _sampler, ivec2 _coord, int _lod)
  366. {
  367. return _sampler.m_texture.Load(ivec3(_coord, _lod) );
  368. }
  369. vec4 bgfxTexelFetch(BgfxSampler2DMS _sampler, ivec2 _coord, int _sampleIdx)
  370. {
  371. return _sampler.m_texture.Load(_coord, _sampleIdx);
  372. }
  373. vec4 bgfxTexelFetch(BgfxSampler2DArray _sampler, ivec3 _coord, int _lod)
  374. {
  375. return _sampler.m_texture.Load(ivec4(_coord, _lod) );
  376. }
  377. vec4 bgfxTexelFetch(BgfxSampler3D _sampler, ivec3 _coord, int _lod)
  378. {
  379. return _sampler.m_texture.Load(ivec4(_coord, _lod) );
  380. }
  381. vec3 bgfxTextureSize(BgfxSampler3D _sampler, int _lod)
  382. {
  383. vec3 result;
  384. float numberOfMipMapLevels;
  385. _sampler.m_texture.GetDimensions(_lod, result.x, result.y, result.z, numberOfMipMapLevels);
  386. return result;
  387. }
  388. # define SAMPLER2D(_name, _reg) \
  389. uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \
  390. uniform Texture2D _name ## Texture : REGISTER(t, _reg); \
  391. static BgfxSampler2D _name = { _name ## Sampler, _name ## Texture }
  392. # define ISAMPLER2D(_name, _reg) \
  393. uniform Texture2D<ivec4> _name ## Texture : REGISTER(t, _reg); \
  394. static BgfxISampler2D _name = { _name ## Texture }
  395. # define USAMPLER2D(_name, _reg) \
  396. uniform Texture2D<uvec4> _name ## Texture : REGISTER(t, _reg); \
  397. static BgfxUSampler2D _name = { _name ## Texture }
  398. # define sampler2D BgfxSampler2D
  399. # define texture2D(_sampler, _coord) bgfxTexture2D(_sampler, _coord)
  400. # define texture2DBias(_sampler, _coord, _bias) bgfxTexture2DBias(_sampler, _coord, _bias)
  401. # define texture2DLod(_sampler, _coord, _level) bgfxTexture2DLod(_sampler, _coord, _level)
  402. # define texture2DLodOffset(_sampler, _coord, _level, _offset) bgfxTexture2DLodOffset(_sampler, _coord, _level, _offset)
  403. # define texture2DProj(_sampler, _coord) bgfxTexture2DProj(_sampler, _coord)
  404. # define texture2DGrad(_sampler, _coord, _dPdx, _dPdy) bgfxTexture2DGrad(_sampler, _coord, _dPdx, _dPdy)
  405. # define SAMPLER2DARRAY(_name, _reg) \
  406. uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \
  407. uniform Texture2DArray _name ## Texture : REGISTER(t, _reg); \
  408. static BgfxSampler2DArray _name = { _name ## Sampler, _name ## Texture }
  409. # define sampler2DArray BgfxSampler2DArray
  410. # define texture2DArray(_sampler, _coord) bgfxTexture2DArray(_sampler, _coord)
  411. # define texture2DArrayLod(_sampler, _coord, _lod) bgfxTexture2DArrayLod(_sampler, _coord, _lod)
  412. # define texture2DArrayLodOffset(_sampler, _coord, _level, _offset) bgfxTexture2DArrayLodOffset(_sampler, _coord, _level, _offset)
  413. # define SAMPLER2DMS(_name, _reg) \
  414. uniform Texture2DMS<vec4> _name ## Texture : REGISTER(t, _reg); \
  415. static BgfxSampler2DMS _name = { _name ## Texture }
  416. # define sampler2DMS BgfxSampler2DMS
  417. # define SAMPLER2DSHADOW(_name, _reg) \
  418. uniform SamplerComparisonState _name ## SamplerComparison : REGISTER(s, _reg); \
  419. uniform Texture2D _name ## Texture : REGISTER(t, _reg); \
  420. static BgfxSampler2DShadow _name = { _name ## SamplerComparison, _name ## Texture }
  421. # define sampler2DShadow BgfxSampler2DShadow
  422. # define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord)
  423. # define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord)
  424. # define SAMPLER2DARRAYSHADOW(_name, _reg) \
  425. uniform SamplerComparisonState _name ## SamplerComparison : REGISTER(s, _reg); \
  426. uniform Texture2DArray _name ## Texture : REGISTER(t, _reg); \
  427. static BgfxSampler2DArrayShadow _name = { _name ## SamplerComparison, _name ## Texture }
  428. # define sampler2DArrayShadow BgfxSampler2DArrayShadow
  429. # define shadow2DArray(_sampler, _coord) bgfxShadow2DArray(_sampler, _coord)
  430. # define SAMPLER3D(_name, _reg) \
  431. uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \
  432. uniform Texture3D _name ## Texture : REGISTER(t, _reg); \
  433. static BgfxSampler3D _name = { _name ## Sampler, _name ## Texture }
  434. # define ISAMPLER3D(_name, _reg) \
  435. uniform Texture3D<ivec4> _name ## Texture : REGISTER(t, _reg); \
  436. static BgfxISampler3D _name = { _name ## Texture }
  437. # define USAMPLER3D(_name, _reg) \
  438. uniform Texture3D<uvec4> _name ## Texture : REGISTER(t, _reg); \
  439. static BgfxUSampler3D _name = { _name ## Texture }
  440. # define sampler3D BgfxSampler3D
  441. # define texture3D(_sampler, _coord) bgfxTexture3D(_sampler, _coord)
  442. # define texture3DLod(_sampler, _coord, _level) bgfxTexture3DLod(_sampler, _coord, _level)
  443. # define SAMPLERCUBE(_name, _reg) \
  444. uniform SamplerState _name ## Sampler : REGISTER(s, _reg); \
  445. uniform TextureCube _name ## Texture : REGISTER(t, _reg); \
  446. static BgfxSamplerCube _name = { _name ## Sampler, _name ## Texture }
  447. # define samplerCube BgfxSamplerCube
  448. # define textureCube(_sampler, _coord) bgfxTextureCube(_sampler, _coord)
  449. # define textureCubeBias(_sampler, _coord, _bias) bgfxTextureCubeBias(_sampler, _coord, _bias)
  450. # define textureCubeLod(_sampler, _coord, _level) bgfxTextureCubeLod(_sampler, _coord, _level)
  451. # define SAMPLERCUBESHADOW(_name, _reg) \
  452. uniform SamplerComparisonState _name ## SamplerComparison : REGISTER(s, _reg); \
  453. uniform TextureCube _name ## Texture : REGISTER(t, _reg); \
  454. static BgfxSamplerCubeShadow _name = { _name ## SamplerComparison, _name ## Texture }
  455. # define samplerCubeShadow BgfxSamplerCubeShadow
  456. # define shadowCube(_sampler, _coord) bgfxShadowCube(_sampler, _coord)
  457. # define texelFetch(_sampler, _coord, _lod) bgfxTexelFetch(_sampler, _coord, _lod)
  458. # define texelFetchOffset(_sampler, _coord, _lod, _offset) bgfxTexelFetchOffset(_sampler, _coord, _lod, _offset)
  459. # define textureSize(_sampler, _lod) bgfxTextureSize(_sampler, _lod)
  460. # define textureGather(_sampler, _coord, _comp) bgfxTextureGather ## _comp(_sampler, _coord)
  461. # define textureGatherOffset(_sampler, _coord, _offset, _comp) bgfxTextureGatherOffset ## _comp(_sampler, _coord, _offset)
  462. # else
  463. # define sampler2DShadow sampler2D
  464. vec4 bgfxTexture2DProj(sampler2D _sampler, vec3 _coord)
  465. {
  466. return tex2Dproj(_sampler, vec4(_coord.xy, 0.0, _coord.z) );
  467. }
  468. vec4 bgfxTexture2DProj(sampler2D _sampler, vec4 _coord)
  469. {
  470. return tex2Dproj(_sampler, _coord);
  471. }
  472. float bgfxShadow2D(sampler2DShadow _sampler, vec3 _coord)
  473. {
  474. #if 0
  475. float occluder = tex2D(_sampler, _coord.xy).x;
  476. return step(_coord.z, occluder);
  477. #else
  478. return tex2Dproj(_sampler, vec4(_coord.xy, _coord.z, 1.0) ).x;
  479. #endif // 0
  480. }
  481. float bgfxShadow2DProj(sampler2DShadow _sampler, vec4 _coord)
  482. {
  483. #if 0
  484. vec3 coord = _coord.xyz * rcp(_coord.w);
  485. float occluder = tex2D(_sampler, coord.xy).x;
  486. return step(coord.z, occluder);
  487. #else
  488. return tex2Dproj(_sampler, _coord).x;
  489. #endif // 0
  490. }
  491. # define SAMPLER2D(_name, _reg) uniform sampler2D _name : REGISTER(s, _reg)
  492. # define SAMPLER2DMS(_name, _reg) uniform sampler2DMS _name : REGISTER(s, _reg)
  493. # define texture2D(_sampler, _coord) tex2D(_sampler, _coord)
  494. # define texture2DProj(_sampler, _coord) bgfxTexture2DProj(_sampler, _coord)
  495. # define SAMPLER2DARRAY(_name, _reg) SAMPLER2D(_name, _reg)
  496. # define texture2DArray(_sampler, _coord) texture2D(_sampler, (_coord).xy)
  497. # define texture2DArrayLod(_sampler, _coord, _lod) texture2DLod(_sampler, _coord, _lod)
  498. # define SAMPLER2DSHADOW(_name, _reg) uniform sampler2DShadow _name : REGISTER(s, _reg)
  499. # define shadow2D(_sampler, _coord) bgfxShadow2D(_sampler, _coord)
  500. # define shadow2DProj(_sampler, _coord) bgfxShadow2DProj(_sampler, _coord)
  501. # define SAMPLER3D(_name, _reg) uniform sampler3D _name : REGISTER(s, _reg)
  502. # define texture3D(_sampler, _coord) tex3D(_sampler, _coord)
  503. # define SAMPLERCUBE(_name, _reg) uniform samplerCUBE _name : REGISTER(s, _reg)
  504. # define textureCube(_sampler, _coord) texCUBE(_sampler, _coord)
  505. # define texture2DLod(_sampler, _coord, _level) tex2Dlod(_sampler, vec4( (_coord).xy, 0.0, _level) )
  506. # define texture2DGrad(_sampler, _coord, _dPdx, _dPdy) tex2Dgrad(_sampler, _coord, _dPdx, _dPdy)
  507. # define texture3DLod(_sampler, _coord, _level) tex3Dlod(_sampler, vec4( (_coord).xyz, _level) )
  508. # define textureCubeLod(_sampler, _coord, _level) texCUBElod(_sampler, vec4( (_coord).xyz, _level) )
  509. # endif // BGFX_SHADER_LANGUAGE_HLSL > 300
  510. vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_mtx, _vec); }
  511. vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_vec, _mtx); }
  512. vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_mtx, _vec); }
  513. vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_vec, _mtx); }
  514. bvec2 lessThan(vec2 _a, vec2 _b) { return _a < _b; }
  515. bvec3 lessThan(vec3 _a, vec3 _b) { return _a < _b; }
  516. bvec4 lessThan(vec4 _a, vec4 _b) { return _a < _b; }
  517. bvec2 lessThanEqual(vec2 _a, vec2 _b) { return _a <= _b; }
  518. bvec3 lessThanEqual(vec3 _a, vec3 _b) { return _a <= _b; }
  519. bvec4 lessThanEqual(vec4 _a, vec4 _b) { return _a <= _b; }
  520. bvec2 greaterThan(vec2 _a, vec2 _b) { return _a > _b; }
  521. bvec3 greaterThan(vec3 _a, vec3 _b) { return _a > _b; }
  522. bvec4 greaterThan(vec4 _a, vec4 _b) { return _a > _b; }
  523. bvec2 greaterThanEqual(vec2 _a, vec2 _b) { return _a >= _b; }
  524. bvec3 greaterThanEqual(vec3 _a, vec3 _b) { return _a >= _b; }
  525. bvec4 greaterThanEqual(vec4 _a, vec4 _b) { return _a >= _b; }
  526. bvec2 notEqual(vec2 _a, vec2 _b) { return _a != _b; }
  527. bvec3 notEqual(vec3 _a, vec3 _b) { return _a != _b; }
  528. bvec4 notEqual(vec4 _a, vec4 _b) { return _a != _b; }
  529. bvec2 equal(vec2 _a, vec2 _b) { return _a == _b; }
  530. bvec3 equal(vec3 _a, vec3 _b) { return _a == _b; }
  531. bvec4 equal(vec4 _a, vec4 _b) { return _a == _b; }
  532. float mix(float _a, float _b, float _t) { return lerp(_a, _b, _t); }
  533. vec2 mix(vec2 _a, vec2 _b, vec2 _t) { return lerp(_a, _b, _t); }
  534. vec3 mix(vec3 _a, vec3 _b, vec3 _t) { return lerp(_a, _b, _t); }
  535. vec4 mix(vec4 _a, vec4 _b, vec4 _t) { return lerp(_a, _b, _t); }
  536. float mod(float _a, float _b) { return _a - _b * floor(_a / _b); }
  537. vec2 mod(vec2 _a, vec2 _b) { return _a - _b * floor(_a / _b); }
  538. vec3 mod(vec3 _a, vec3 _b) { return _a - _b * floor(_a / _b); }
  539. vec4 mod(vec4 _a, vec4 _b) { return _a - _b * floor(_a / _b); }
  540. #else
  541. # define CONST(_x) const _x
  542. # define atan2(_x, _y) atan(_x, _y)
  543. # define mul(_a, _b) ( (_a) * (_b) )
  544. # define saturate(_x) clamp(_x, 0.0, 1.0)
  545. # define SAMPLER2D(_name, _reg) uniform sampler2D _name
  546. # define SAMPLER2DMS(_name, _reg) uniform sampler2DMS _name
  547. # define SAMPLER3D(_name, _reg) uniform sampler3D _name
  548. # define SAMPLERCUBE(_name, _reg) uniform samplerCube _name
  549. # define SAMPLER2DSHADOW(_name, _reg) uniform sampler2DShadow _name
  550. # define SAMPLER2DARRAY(_name, _reg) uniform sampler2DArray _name
  551. # define SAMPLER2DMSARRAY(_name, _reg) uniform sampler2DMSArray _name
  552. # define SAMPLERCUBEARRAY(_name, _reg) uniform samplerCubeArray _name
  553. # define SAMPLER2DARRAYSHADOW(_name, _reg) uniform sampler2DArrayShadow _name
  554. # define ISAMPLER2D(_name, _reg) uniform isampler2D _name
  555. # define USAMPLER2D(_name, _reg) uniform usampler2D _name
  556. # define ISAMPLER3D(_name, _reg) uniform isampler3D _name
  557. # define USAMPLER3D(_name, _reg) uniform usampler3D _name
  558. # if BGFX_SHADER_LANGUAGE_GLSL >= 130
  559. # define texture2D(_sampler, _coord) texture(_sampler, _coord)
  560. # define texture2DArray(_sampler, _coord) texture(_sampler, _coord)
  561. # define texture3D(_sampler, _coord) texture(_sampler, _coord)
  562. # define textureCube(_sampler, _coord) texture(_sampler, _coord)
  563. # define texture2DLod(_sampler, _coord, _lod) textureLod(_sampler, _coord, _lod)
  564. # define texture2DLodOffset(_sampler, _coord, _lod, _offset) textureLodOffset(_sampler, _coord, _lod, _offset)
  565. # define texture2DBias(_sampler, _coord, _bias) texture(_sampler, _coord, _bias)
  566. # define textureCubeBias(_sampler, _coord, _bias) texture(_sampler, _coord, _bias)
  567. # else
  568. # define texture2DBias(_sampler, _coord, _bias) texture2D(_sampler, _coord, _bias)
  569. # define textureCubeBias(_sampler, _coord, _bias) textureCube(_sampler, _coord, _bias)
  570. # endif // BGFX_SHADER_LANGUAGE_GLSL >= 130
  571. vec3 instMul(vec3 _vec, mat3 _mtx) { return mul(_vec, _mtx); }
  572. vec3 instMul(mat3 _mtx, vec3 _vec) { return mul(_mtx, _vec); }
  573. vec4 instMul(vec4 _vec, mat4 _mtx) { return mul(_vec, _mtx); }
  574. vec4 instMul(mat4 _mtx, vec4 _vec) { return mul(_mtx, _vec); }
  575. float rcp(float _a) { return 1.0/_a; }
  576. vec2 rcp(vec2 _a) { return vec2(1.0)/_a; }
  577. vec3 rcp(vec3 _a) { return vec3(1.0)/_a; }
  578. vec4 rcp(vec4 _a) { return vec4(1.0)/_a; }
  579. #endif // BGFX_SHADER_LANGUAGE_*
  580. vec2 vec2_splat(float _x) { return vec2(_x, _x); }
  581. vec3 vec3_splat(float _x) { return vec3(_x, _x, _x); }
  582. vec4 vec4_splat(float _x) { return vec4(_x, _x, _x, _x); }
  583. #if BGFX_SHADER_LANGUAGE_GLSL >= 130 || BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
  584. uvec2 uvec2_splat(uint _x) { return uvec2(_x, _x); }
  585. uvec3 uvec3_splat(uint _x) { return uvec3(_x, _x, _x); }
  586. uvec4 uvec4_splat(uint _x) { return uvec4(_x, _x, _x, _x); }
  587. #endif // BGFX_SHADER_LANGUAGE_GLSL >= 130 || BGFX_SHADER_LANGUAGE_HLSL || BGFX_SHADER_LANGUAGE_PSSL || BGFX_SHADER_LANGUAGE_SPIRV || BGFX_SHADER_LANGUAGE_METAL
  588. mat4 mtxFromRows(vec4 _0, vec4 _1, vec4 _2, vec4 _3)
  589. {
  590. #if BGFX_SHADER_LANGUAGE_GLSL
  591. return transpose(mat4(_0, _1, _2, _3) );
  592. #else
  593. return mat4(_0, _1, _2, _3);
  594. #endif // BGFX_SHADER_LANGUAGE_GLSL
  595. }
  596. mat4 mtxFromCols(vec4 _0, vec4 _1, vec4 _2, vec4 _3)
  597. {
  598. #if BGFX_SHADER_LANGUAGE_GLSL
  599. return mat4(_0, _1, _2, _3);
  600. #else
  601. return transpose(mat4(_0, _1, _2, _3) );
  602. #endif // BGFX_SHADER_LANGUAGE_GLSL
  603. }
  604. mat3 mtxFromRows(vec3 _0, vec3 _1, vec3 _2)
  605. {
  606. #if BGFX_SHADER_LANGUAGE_GLSL
  607. return transpose(mat3(_0, _1, _2) );
  608. #else
  609. return mat3(_0, _1, _2);
  610. #endif // BGFX_SHADER_LANGUAGE_GLSL
  611. }
  612. mat3 mtxFromCols(vec3 _0, vec3 _1, vec3 _2)
  613. {
  614. #if BGFX_SHADER_LANGUAGE_GLSL
  615. return mat3(_0, _1, _2);
  616. #else
  617. return transpose(mat3(_0, _1, _2) );
  618. #endif // BGFX_SHADER_LANGUAGE_GLSL
  619. }
  620. #if BGFX_SHADER_LANGUAGE_GLSL
  621. #define mtxFromRows3(_0, _1, _2) transpose(mat3(_0, _1, _2) )
  622. #define mtxFromRows4(_0, _1, _2, _3) transpose(mat4(_0, _1, _2, _3) )
  623. #define mtxFromCols3(_0, _1, _2) mat3(_0, _1, _2)
  624. #define mtxFromCols4(_0, _1, _2, _3) mat4(_0, _1, _2, _3)
  625. #else
  626. #define mtxFromRows3(_0, _1, _2) mat3(_0, _1, _2)
  627. #define mtxFromRows4(_0, _1, _2, _3) mat4(_0, _1, _2, _3)
  628. #define mtxFromCols3(_0, _1, _2) transpose(mat3(_0, _1, _2) )
  629. #define mtxFromCols4(_0, _1, _2, _3) transpose(mat4(_0, _1, _2, _3) )
  630. #endif // BGFX_SHADER_LANGUAGE_GLSL
  631. uniform vec4 u_viewRect;
  632. uniform vec4 u_viewTexel;
  633. uniform mat4 u_view;
  634. uniform mat4 u_invView;
  635. uniform mat4 u_proj;
  636. uniform mat4 u_invProj;
  637. uniform mat4 u_viewProj;
  638. uniform mat4 u_invViewProj;
  639. uniform mat4 u_model[BGFX_CONFIG_MAX_BONES];
  640. uniform mat4 u_modelView;
  641. uniform mat4 u_modelViewProj;
  642. uniform vec4 u_alphaRef4;
  643. #define u_alphaRef u_alphaRef4.x
  644. #endif // __cplusplus
  645. #endif // BGFX_SHADER_H_HEADER_GUARD
  646. /*
  647. * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
  648. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  649. */
  650. #ifndef __SHADERLIB_SH__
  651. #define __SHADERLIB_SH__
  652. vec4 encodeRE8(float _r)
  653. {
  654. float exponent = ceil(log2(_r) );
  655. return vec4(_r / exp2(exponent)
  656. , 0.0
  657. , 0.0
  658. , (exponent + 128.0) / 255.0
  659. );
  660. }
  661. float decodeRE8(vec4 _re8)
  662. {
  663. float exponent = _re8.w * 255.0 - 128.0;
  664. return _re8.x * exp2(exponent);
  665. }
  666. vec4 encodeRGBE8(vec3 _rgb)
  667. {
  668. vec4 rgbe8;
  669. float maxComponent = max(max(_rgb.x, _rgb.y), _rgb.z);
  670. float exponent = ceil(log2(maxComponent) );
  671. rgbe8.xyz = _rgb / exp2(exponent);
  672. rgbe8.w = (exponent + 128.0) / 255.0;
  673. return rgbe8;
  674. }
  675. vec3 decodeRGBE8(vec4 _rgbe8)
  676. {
  677. float exponent = _rgbe8.w * 255.0 - 128.0;
  678. vec3 rgb = _rgbe8.xyz * exp2(exponent);
  679. return rgb;
  680. }
  681. vec3 encodeNormalUint(vec3 _normal)
  682. {
  683. return _normal * 0.5 + 0.5;
  684. }
  685. vec3 decodeNormalUint(vec3 _encodedNormal)
  686. {
  687. return _encodedNormal * 2.0 - 1.0;
  688. }
  689. vec2 encodeNormalSphereMap(vec3 _normal)
  690. {
  691. return normalize(_normal.xy) * sqrt(_normal.z * 0.5 + 0.5);
  692. }
  693. vec3 decodeNormalSphereMap(vec2 _encodedNormal)
  694. {
  695. float zz = dot(_encodedNormal, _encodedNormal) * 2.0 - 1.0;
  696. return vec3(normalize(_encodedNormal.xy) * sqrt(1.0 - zz*zz), zz);
  697. }
  698. vec2 octahedronWrap(vec2 _val)
  699. {
  700. // Reference(s):
  701. // - Octahedron normal vector encoding
  702. // https://web.archive.org/web/20191027010600/https://knarkowicz.wordpress.com/2014/04/16/octahedron-normal-vector-encoding/comment-page-1/
  703. return (1.0 - abs(_val.yx) )
  704. * mix(vec2_splat(-1.0), vec2_splat(1.0), vec2(greaterThanEqual(_val.xy, vec2_splat(0.0) ) ) );
  705. }
  706. vec2 encodeNormalOctahedron(vec3 _normal)
  707. {
  708. _normal /= abs(_normal.x) + abs(_normal.y) + abs(_normal.z);
  709. _normal.xy = _normal.z >= 0.0 ? _normal.xy : octahedronWrap(_normal.xy);
  710. _normal.xy = _normal.xy * 0.5 + 0.5;
  711. return _normal.xy;
  712. }
  713. vec3 decodeNormalOctahedron(vec2 _encodedNormal)
  714. {
  715. _encodedNormal = _encodedNormal * 2.0 - 1.0;
  716. vec3 normal;
  717. normal.z = 1.0 - abs(_encodedNormal.x) - abs(_encodedNormal.y);
  718. normal.xy = normal.z >= 0.0 ? _encodedNormal.xy : octahedronWrap(_encodedNormal.xy);
  719. return normalize(normal);
  720. }
  721. vec3 convertRGB2XYZ(vec3 _rgb)
  722. {
  723. // Reference(s):
  724. // - RGB/XYZ Matrices
  725. // https://web.archive.org/web/20191027010220/http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
  726. vec3 xyz;
  727. xyz.x = dot(vec3(0.4124564, 0.3575761, 0.1804375), _rgb);
  728. xyz.y = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb);
  729. xyz.z = dot(vec3(0.0193339, 0.1191920, 0.9503041), _rgb);
  730. return xyz;
  731. }
  732. vec3 convertXYZ2RGB(vec3 _xyz)
  733. {
  734. vec3 rgb;
  735. rgb.x = dot(vec3( 3.2404542, -1.5371385, -0.4985314), _xyz);
  736. rgb.y = dot(vec3(-0.9692660, 1.8760108, 0.0415560), _xyz);
  737. rgb.z = dot(vec3( 0.0556434, -0.2040259, 1.0572252), _xyz);
  738. return rgb;
  739. }
  740. vec3 convertXYZ2Yxy(vec3 _xyz)
  741. {
  742. // Reference(s):
  743. // - XYZ to xyY
  744. // https://web.archive.org/web/20191027010144/http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_xyY.html
  745. float inv = 1.0/dot(_xyz, vec3(1.0, 1.0, 1.0) );
  746. return vec3(_xyz.y, _xyz.x*inv, _xyz.y*inv);
  747. }
  748. vec3 convertYxy2XYZ(vec3 _Yxy)
  749. {
  750. // Reference(s):
  751. // - xyY to XYZ
  752. // https://web.archive.org/web/20191027010036/http://www.brucelindbloom.com/index.html?Eqn_xyY_to_XYZ.html
  753. vec3 xyz;
  754. xyz.x = _Yxy.x*_Yxy.y/_Yxy.z;
  755. xyz.y = _Yxy.x;
  756. xyz.z = _Yxy.x*(1.0 - _Yxy.y - _Yxy.z)/_Yxy.z;
  757. return xyz;
  758. }
  759. vec3 convertRGB2Yxy(vec3 _rgb)
  760. {
  761. return convertXYZ2Yxy(convertRGB2XYZ(_rgb) );
  762. }
  763. vec3 convertYxy2RGB(vec3 _Yxy)
  764. {
  765. return convertXYZ2RGB(convertYxy2XYZ(_Yxy) );
  766. }
  767. vec3 convertRGB2Yuv(vec3 _rgb)
  768. {
  769. vec3 yuv;
  770. yuv.x = dot(_rgb, vec3(0.299, 0.587, 0.114) );
  771. yuv.y = (_rgb.x - yuv.x)*0.713 + 0.5;
  772. yuv.z = (_rgb.z - yuv.x)*0.564 + 0.5;
  773. return yuv;
  774. }
  775. vec3 convertYuv2RGB(vec3 _yuv)
  776. {
  777. vec3 rgb;
  778. rgb.x = _yuv.x + 1.403*(_yuv.y-0.5);
  779. rgb.y = _yuv.x - 0.344*(_yuv.y-0.5) - 0.714*(_yuv.z-0.5);
  780. rgb.z = _yuv.x + 1.773*(_yuv.z-0.5);
  781. return rgb;
  782. }
  783. vec3 convertRGB2YIQ(vec3 _rgb)
  784. {
  785. vec3 yiq;
  786. yiq.x = dot(vec3(0.299, 0.587, 0.114 ), _rgb);
  787. yiq.y = dot(vec3(0.595716, -0.274453, -0.321263), _rgb);
  788. yiq.z = dot(vec3(0.211456, -0.522591, 0.311135), _rgb);
  789. return yiq;
  790. }
  791. vec3 convertYIQ2RGB(vec3 _yiq)
  792. {
  793. vec3 rgb;
  794. rgb.x = dot(vec3(1.0, 0.9563, 0.6210), _yiq);
  795. rgb.y = dot(vec3(1.0, -0.2721, -0.6474), _yiq);
  796. rgb.z = dot(vec3(1.0, -1.1070, 1.7046), _yiq);
  797. return rgb;
  798. }
  799. vec3 toLinear(vec3 _rgb)
  800. {
  801. return pow(abs(_rgb), vec3_splat(2.2) );
  802. }
  803. vec4 toLinear(vec4 _rgba)
  804. {
  805. return vec4(toLinear(_rgba.xyz), _rgba.w);
  806. }
  807. vec3 toLinearAccurate(vec3 _rgb)
  808. {
  809. vec3 lo = _rgb / 12.92;
  810. vec3 hi = pow( (_rgb + 0.055) / 1.055, vec3_splat(2.4) );
  811. vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.04045) ) ) );
  812. return rgb;
  813. }
  814. vec4 toLinearAccurate(vec4 _rgba)
  815. {
  816. return vec4(toLinearAccurate(_rgba.xyz), _rgba.w);
  817. }
  818. float toGamma(float _r)
  819. {
  820. return pow(abs(_r), 1.0/2.2);
  821. }
  822. vec3 toGamma(vec3 _rgb)
  823. {
  824. return pow(abs(_rgb), vec3_splat(1.0/2.2) );
  825. }
  826. vec4 toGamma(vec4 _rgba)
  827. {
  828. return vec4(toGamma(_rgba.xyz), _rgba.w);
  829. }
  830. vec3 toGammaAccurate(vec3 _rgb)
  831. {
  832. vec3 lo = _rgb * 12.92;
  833. vec3 hi = pow(abs(_rgb), vec3_splat(1.0/2.4) ) * 1.055 - 0.055;
  834. vec3 rgb = mix(hi, lo, vec3(lessThanEqual(_rgb, vec3_splat(0.0031308) ) ) );
  835. return rgb;
  836. }
  837. vec4 toGammaAccurate(vec4 _rgba)
  838. {
  839. return vec4(toGammaAccurate(_rgba.xyz), _rgba.w);
  840. }
  841. vec3 toReinhard(vec3 _rgb)
  842. {
  843. return toGamma(_rgb/(_rgb+vec3_splat(1.0) ) );
  844. }
  845. vec4 toReinhard(vec4 _rgba)
  846. {
  847. return vec4(toReinhard(_rgba.xyz), _rgba.w);
  848. }
  849. vec3 toFilmic(vec3 _rgb)
  850. {
  851. _rgb = max(vec3_splat(0.0), _rgb - 0.004);
  852. _rgb = (_rgb*(6.2*_rgb + 0.5) ) / (_rgb*(6.2*_rgb + 1.7) + 0.06);
  853. return _rgb;
  854. }
  855. vec4 toFilmic(vec4 _rgba)
  856. {
  857. return vec4(toFilmic(_rgba.xyz), _rgba.w);
  858. }
  859. vec3 toAcesFilmic(vec3 _rgb)
  860. {
  861. // Reference(s):
  862. // - ACES Filmic Tone Mapping Curve
  863. // https://web.archive.org/web/20191027010704/https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
  864. float aa = 2.51f;
  865. float bb = 0.03f;
  866. float cc = 2.43f;
  867. float dd = 0.59f;
  868. float ee = 0.14f;
  869. return saturate( (_rgb*(aa*_rgb + bb) )/(_rgb*(cc*_rgb + dd) + ee) );
  870. }
  871. vec4 toAcesFilmic(vec4 _rgba)
  872. {
  873. return vec4(toAcesFilmic(_rgba.xyz), _rgba.w);
  874. }
  875. vec3 luma(vec3 _rgb)
  876. {
  877. float yy = dot(vec3(0.2126729, 0.7151522, 0.0721750), _rgb);
  878. return vec3_splat(yy);
  879. }
  880. vec4 luma(vec4 _rgba)
  881. {
  882. return vec4(luma(_rgba.xyz), _rgba.w);
  883. }
  884. vec3 conSatBri(vec3 _rgb, vec3 _csb)
  885. {
  886. vec3 rgb = _rgb * _csb.z;
  887. rgb = mix(luma(rgb), rgb, _csb.y);
  888. rgb = mix(vec3_splat(0.5), rgb, _csb.x);
  889. return rgb;
  890. }
  891. vec4 conSatBri(vec4 _rgba, vec3 _csb)
  892. {
  893. return vec4(conSatBri(_rgba.xyz, _csb), _rgba.w);
  894. }
  895. vec3 posterize(vec3 _rgb, float _numColors)
  896. {
  897. return floor(_rgb*_numColors) / _numColors;
  898. }
  899. vec4 posterize(vec4 _rgba, float _numColors)
  900. {
  901. return vec4(posterize(_rgba.xyz, _numColors), _rgba.w);
  902. }
  903. vec3 sepia(vec3 _rgb)
  904. {
  905. vec3 color;
  906. color.x = dot(_rgb, vec3(0.393, 0.769, 0.189) );
  907. color.y = dot(_rgb, vec3(0.349, 0.686, 0.168) );
  908. color.z = dot(_rgb, vec3(0.272, 0.534, 0.131) );
  909. return color;
  910. }
  911. vec4 sepia(vec4 _rgba)
  912. {
  913. return vec4(sepia(_rgba.xyz), _rgba.w);
  914. }
  915. vec3 blendOverlay(vec3 _base, vec3 _blend)
  916. {
  917. vec3 lt = 2.0 * _base * _blend;
  918. vec3 gte = 1.0 - 2.0 * (1.0 - _base) * (1.0 - _blend);
  919. return mix(lt, gte, step(vec3_splat(0.5), _base) );
  920. }
  921. vec4 blendOverlay(vec4 _base, vec4 _blend)
  922. {
  923. return vec4(blendOverlay(_base.xyz, _blend.xyz), _base.w);
  924. }
  925. vec3 adjustHue(vec3 _rgb, float _hue)
  926. {
  927. vec3 yiq = convertRGB2YIQ(_rgb);
  928. float angle = _hue + atan2(yiq.z, yiq.y);
  929. float len = length(yiq.yz);
  930. return convertYIQ2RGB(vec3(yiq.x, len*cos(angle), len*sin(angle) ) );
  931. }
  932. vec4 packFloatToRgba(float _value)
  933. {
  934. const vec4 shift = vec4(256 * 256 * 256, 256 * 256, 256, 1.0);
  935. const vec4 mask = vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);
  936. vec4 comp = fract(_value * shift);
  937. comp -= comp.xxyz * mask;
  938. return comp;
  939. }
  940. float unpackRgbaToFloat(vec4 _rgba)
  941. {
  942. const vec4 shift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
  943. return dot(_rgba, shift);
  944. }
  945. vec2 packHalfFloat(float _value)
  946. {
  947. const vec2 shift = vec2(256, 1.0);
  948. const vec2 mask = vec2(0, 1.0 / 256.0);
  949. vec2 comp = fract(_value * shift);
  950. comp -= comp.xx * mask;
  951. return comp;
  952. }
  953. float unpackHalfFloat(vec2 _rg)
  954. {
  955. const vec2 shift = vec2(1.0 / 256.0, 1.0);
  956. return dot(_rg, shift);
  957. }
  958. float random(vec2 _uv)
  959. {
  960. return fract(sin(dot(_uv.xy, vec2(12.9898, 78.233) ) ) * 43758.5453);
  961. }
  962. vec3 fixCubeLookup(vec3 _v, float _lod, float _topLevelCubeSize)
  963. {
  964. // Reference(s):
  965. // - Seamless cube-map filtering
  966. // https://web.archive.org/web/20190411181934/http://the-witness.net/news/2012/02/seamless-cube-map-filtering/
  967. float ax = abs(_v.x);
  968. float ay = abs(_v.y);
  969. float az = abs(_v.z);
  970. float vmax = max(max(ax, ay), az);
  971. float scale = 1.0 - exp2(_lod) / _topLevelCubeSize;
  972. if (ax != vmax) { _v.x *= scale; }
  973. if (ay != vmax) { _v.y *= scale; }
  974. if (az != vmax) { _v.z *= scale; }
  975. return _v;
  976. }
  977. vec2 texture2DBc5(sampler2D _sampler, vec2 _uv)
  978. {
  979. #if BGFX_SHADER_LANGUAGE_HLSL && BGFX_SHADER_LANGUAGE_HLSL <= 300
  980. return texture2D(_sampler, _uv).yx;
  981. #else
  982. return texture2D(_sampler, _uv).xy;
  983. #endif
  984. }
  985. mat3 cofactor(mat4 _m)
  986. {
  987. // Reference:
  988. // Cofactor of matrix. Use to transform normals. The code assumes the last column of _m is [0,0,0,1].
  989. // https://www.shadertoy.com/view/3s33zj
  990. // https://github.com/graphitemaster/normals_revisited
  991. return mat3(
  992. _m[1][1]*_m[2][2]-_m[1][2]*_m[2][1],
  993. _m[1][2]*_m[2][0]-_m[1][0]*_m[2][2],
  994. _m[1][0]*_m[2][1]-_m[1][1]*_m[2][0],
  995. _m[0][2]*_m[2][1]-_m[0][1]*_m[2][2],
  996. _m[0][0]*_m[2][2]-_m[0][2]*_m[2][0],
  997. _m[0][1]*_m[2][0]-_m[0][0]*_m[2][1],
  998. _m[0][1]*_m[1][2]-_m[0][2]*_m[1][1],
  999. _m[0][2]*_m[1][0]-_m[0][0]*_m[1][2],
  1000. _m[0][0]*_m[1][1]-_m[0][1]*_m[1][0]
  1001. );
  1002. }
  1003. float toClipSpaceDepth(float _depthTextureZ)
  1004. {
  1005. #if BGFX_SHADER_LANGUAGE_GLSL
  1006. return _depthTextureZ * 2.0 - 1.0;
  1007. #else
  1008. return _depthTextureZ;
  1009. #endif // BGFX_SHADER_LANGUAGE_GLSL
  1010. }
  1011. vec3 clipToWorld(mat4 _invViewProj, vec3 _clipPos)
  1012. {
  1013. vec4 wpos = mul(_invViewProj, vec4(_clipPos, 1.0) );
  1014. return wpos.xyz / wpos.w;
  1015. }
  1016. #endif // __SHADERLIB_SH__
  1017. """
  1018. }
  1019. }