packing.inl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. /// @ref gtc_packing
  2. #include "../ext/scalar_relational.hpp"
  3. #include "../ext/vector_relational.hpp"
  4. #include "../common.hpp"
  5. #include "../vec2.hpp"
  6. #include "../vec3.hpp"
  7. #include "../vec4.hpp"
  8. #include "../detail/type_half.hpp"
  9. #include <cstring>
  10. #include <limits>
  11. namespace glm{
  12. namespace detail
  13. {
  14. GLM_FUNC_QUALIFIER glm::uint16 float2half(glm::uint32 f)
  15. {
  16. // 10 bits => EE EEEFFFFF
  17. // 11 bits => EEE EEFFFFFF
  18. // Half bits => SEEEEEFF FFFFFFFF
  19. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  20. // 0x00007c00 => 00000000 00000000 01111100 00000000
  21. // 0x000003ff => 00000000 00000000 00000011 11111111
  22. // 0x38000000 => 00111000 00000000 00000000 00000000
  23. // 0x7f800000 => 01111111 10000000 00000000 00000000
  24. // 0x00008000 => 00000000 00000000 10000000 00000000
  25. return
  26. ((f >> 16) & 0x8000) | // sign
  27. ((((f & 0x7f800000) - 0x38000000) >> 13) & 0x7c00) | // exponential
  28. ((f >> 13) & 0x03ff); // Mantissa
  29. }
  30. GLM_FUNC_QUALIFIER glm::uint32 float2packed11(glm::uint32 f)
  31. {
  32. // 10 bits => EE EEEFFFFF
  33. // 11 bits => EEE EEFFFFFF
  34. // Half bits => SEEEEEFF FFFFFFFF
  35. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  36. // 0x000007c0 => 00000000 00000000 00000111 11000000
  37. // 0x00007c00 => 00000000 00000000 01111100 00000000
  38. // 0x000003ff => 00000000 00000000 00000011 11111111
  39. // 0x38000000 => 00111000 00000000 00000000 00000000
  40. // 0x7f800000 => 01111111 10000000 00000000 00000000
  41. // 0x00008000 => 00000000 00000000 10000000 00000000
  42. return
  43. ((((f & 0x7f800000) - 0x38000000) >> 17) & 0x07c0) | // exponential
  44. ((f >> 17) & 0x003f); // Mantissa
  45. }
  46. GLM_FUNC_QUALIFIER glm::uint32 packed11ToFloat(glm::uint32 p)
  47. {
  48. // 10 bits => EE EEEFFFFF
  49. // 11 bits => EEE EEFFFFFF
  50. // Half bits => SEEEEEFF FFFFFFFF
  51. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  52. // 0x000007c0 => 00000000 00000000 00000111 11000000
  53. // 0x00007c00 => 00000000 00000000 01111100 00000000
  54. // 0x000003ff => 00000000 00000000 00000011 11111111
  55. // 0x38000000 => 00111000 00000000 00000000 00000000
  56. // 0x7f800000 => 01111111 10000000 00000000 00000000
  57. // 0x00008000 => 00000000 00000000 10000000 00000000
  58. return
  59. ((((p & 0x07c0) << 17) + 0x38000000) & 0x7f800000) | // exponential
  60. ((p & 0x003f) << 17); // Mantissa
  61. }
  62. GLM_FUNC_QUALIFIER glm::uint32 float2packed10(glm::uint32 f)
  63. {
  64. // 10 bits => EE EEEFFFFF
  65. // 11 bits => EEE EEFFFFFF
  66. // Half bits => SEEEEEFF FFFFFFFF
  67. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  68. // 0x0000001F => 00000000 00000000 00000000 00011111
  69. // 0x0000003F => 00000000 00000000 00000000 00111111
  70. // 0x000003E0 => 00000000 00000000 00000011 11100000
  71. // 0x000007C0 => 00000000 00000000 00000111 11000000
  72. // 0x00007C00 => 00000000 00000000 01111100 00000000
  73. // 0x000003FF => 00000000 00000000 00000011 11111111
  74. // 0x38000000 => 00111000 00000000 00000000 00000000
  75. // 0x7f800000 => 01111111 10000000 00000000 00000000
  76. // 0x00008000 => 00000000 00000000 10000000 00000000
  77. return
  78. ((((f & 0x7f800000) - 0x38000000) >> 18) & 0x03E0) | // exponential
  79. ((f >> 18) & 0x001f); // Mantissa
  80. }
  81. GLM_FUNC_QUALIFIER glm::uint32 packed10ToFloat(glm::uint32 p)
  82. {
  83. // 10 bits => EE EEEFFFFF
  84. // 11 bits => EEE EEFFFFFF
  85. // Half bits => SEEEEEFF FFFFFFFF
  86. // Float bits => SEEEEEEE EFFFFFFF FFFFFFFF FFFFFFFF
  87. // 0x0000001F => 00000000 00000000 00000000 00011111
  88. // 0x0000003F => 00000000 00000000 00000000 00111111
  89. // 0x000003E0 => 00000000 00000000 00000011 11100000
  90. // 0x000007C0 => 00000000 00000000 00000111 11000000
  91. // 0x00007C00 => 00000000 00000000 01111100 00000000
  92. // 0x000003FF => 00000000 00000000 00000011 11111111
  93. // 0x38000000 => 00111000 00000000 00000000 00000000
  94. // 0x7f800000 => 01111111 10000000 00000000 00000000
  95. // 0x00008000 => 00000000 00000000 10000000 00000000
  96. return
  97. ((((p & 0x03E0) << 18) + 0x38000000) & 0x7f800000) | // exponential
  98. ((p & 0x001f) << 18); // Mantissa
  99. }
  100. GLM_FUNC_QUALIFIER glm::uint half2float(glm::uint h)
  101. {
  102. return ((h & 0x8000) << 16) | ((( h & 0x7c00) + 0x1C000) << 13) | ((h & 0x03FF) << 13);
  103. }
  104. GLM_FUNC_QUALIFIER glm::uint floatTo11bit(float x)
  105. {
  106. if(x == 0.0f)
  107. return 0u;
  108. else if(glm::isnan(x))
  109. return ~0u;
  110. else if(glm::isinf(x))
  111. return 0x1Fu << 6u;
  112. uint Pack = 0u;
  113. memcpy(&Pack, &x, sizeof(Pack));
  114. return float2packed11(Pack);
  115. }
  116. GLM_FUNC_QUALIFIER float packed11bitToFloat(glm::uint x)
  117. {
  118. if(x == 0)
  119. return 0.0f;
  120. else if(x == ((1 << 11) - 1))
  121. return ~0;//NaN
  122. else if(x == (0x1f << 6))
  123. return ~0;//Inf
  124. uint Result = packed11ToFloat(x);
  125. float Temp = 0;
  126. memcpy(&Temp, &Result, sizeof(Temp));
  127. return Temp;
  128. }
  129. GLM_FUNC_QUALIFIER glm::uint floatTo10bit(float x)
  130. {
  131. if(x == 0.0f)
  132. return 0u;
  133. else if(glm::isnan(x))
  134. return ~0u;
  135. else if(glm::isinf(x))
  136. return 0x1Fu << 5u;
  137. uint Pack = 0;
  138. memcpy(&Pack, &x, sizeof(Pack));
  139. return float2packed10(Pack);
  140. }
  141. GLM_FUNC_QUALIFIER float packed10bitToFloat(glm::uint x)
  142. {
  143. if(x == 0)
  144. return 0.0f;
  145. else if(x == ((1 << 10) - 1))
  146. return ~0;//NaN
  147. else if(x == (0x1f << 5))
  148. return ~0;//Inf
  149. uint Result = packed10ToFloat(x);
  150. float Temp = 0;
  151. memcpy(&Temp, &Result, sizeof(Temp));
  152. return Temp;
  153. }
  154. // GLM_FUNC_QUALIFIER glm::uint f11_f11_f10(float x, float y, float z)
  155. // {
  156. // return ((floatTo11bit(x) & ((1 << 11) - 1)) << 0) | ((floatTo11bit(y) & ((1 << 11) - 1)) << 11) | ((floatTo10bit(z) & ((1 << 10) - 1)) << 22);
  157. // }
  158. union u3u3u2
  159. {
  160. struct
  161. {
  162. uint x : 3;
  163. uint y : 3;
  164. uint z : 2;
  165. } data;
  166. uint8 pack;
  167. };
  168. union u4u4
  169. {
  170. struct
  171. {
  172. uint x : 4;
  173. uint y : 4;
  174. } data;
  175. uint8 pack;
  176. };
  177. union u4u4u4u4
  178. {
  179. struct
  180. {
  181. uint x : 4;
  182. uint y : 4;
  183. uint z : 4;
  184. uint w : 4;
  185. } data;
  186. uint16 pack;
  187. };
  188. union u5u6u5
  189. {
  190. struct
  191. {
  192. uint x : 5;
  193. uint y : 6;
  194. uint z : 5;
  195. } data;
  196. uint16 pack;
  197. };
  198. union u5u5u5u1
  199. {
  200. struct
  201. {
  202. uint x : 5;
  203. uint y : 5;
  204. uint z : 5;
  205. uint w : 1;
  206. } data;
  207. uint16 pack;
  208. };
  209. union u10u10u10u2
  210. {
  211. struct
  212. {
  213. uint x : 10;
  214. uint y : 10;
  215. uint z : 10;
  216. uint w : 2;
  217. } data;
  218. uint32 pack;
  219. };
  220. union i10i10i10i2
  221. {
  222. struct
  223. {
  224. int x : 10;
  225. int y : 10;
  226. int z : 10;
  227. int w : 2;
  228. } data;
  229. uint32 pack;
  230. };
  231. union u9u9u9e5
  232. {
  233. struct
  234. {
  235. uint x : 9;
  236. uint y : 9;
  237. uint z : 9;
  238. uint w : 5;
  239. } data;
  240. uint32 pack;
  241. };
  242. template<length_t L, qualifier Q>
  243. struct compute_half
  244. {};
  245. template<qualifier Q>
  246. struct compute_half<1, Q>
  247. {
  248. GLM_FUNC_QUALIFIER static vec<1, uint16, Q> pack(vec<1, float, Q> const& v)
  249. {
  250. int16 const Unpack(detail::toFloat16(v.x));
  251. u16vec1 Packed;
  252. memcpy(&Packed, &Unpack, sizeof(Packed));
  253. return Packed;
  254. }
  255. GLM_FUNC_QUALIFIER static vec<1, float, Q> unpack(vec<1, uint16, Q> const& v)
  256. {
  257. i16vec1 Unpack;
  258. memcpy(&Unpack, &v, sizeof(Unpack));
  259. return vec<1, float, Q>(detail::toFloat32(v.x));
  260. }
  261. };
  262. template<qualifier Q>
  263. struct compute_half<2, Q>
  264. {
  265. GLM_FUNC_QUALIFIER static vec<2, uint16, Q> pack(vec<2, float, Q> const& v)
  266. {
  267. vec<2, int16, Q> const Unpack(detail::toFloat16(v.x), detail::toFloat16(v.y));
  268. u16vec2 Packed;
  269. memcpy(&Packed, &Unpack, sizeof(Packed));
  270. return Packed;
  271. }
  272. GLM_FUNC_QUALIFIER static vec<2, float, Q> unpack(vec<2, uint16, Q> const& v)
  273. {
  274. i16vec2 Unpack;
  275. memcpy(&Unpack, &v, sizeof(Unpack));
  276. return vec<2, float, Q>(detail::toFloat32(v.x), detail::toFloat32(v.y));
  277. }
  278. };
  279. template<qualifier Q>
  280. struct compute_half<3, Q>
  281. {
  282. GLM_FUNC_QUALIFIER static vec<3, uint16, Q> pack(vec<3, float, Q> const& v)
  283. {
  284. vec<3, int16, Q> const Unpack(detail::toFloat16(v.x), detail::toFloat16(v.y), detail::toFloat16(v.z));
  285. u16vec3 Packed;
  286. memcpy(&Packed, &Unpack, sizeof(Packed));
  287. return Packed;
  288. }
  289. GLM_FUNC_QUALIFIER static vec<3, float, Q> unpack(vec<3, uint16, Q> const& v)
  290. {
  291. i16vec3 Unpack;
  292. memcpy(&Unpack, &v, sizeof(Unpack));
  293. return vec<3, float, Q>(detail::toFloat32(v.x), detail::toFloat32(v.y), detail::toFloat32(v.z));
  294. }
  295. };
  296. template<qualifier Q>
  297. struct compute_half<4, Q>
  298. {
  299. GLM_FUNC_QUALIFIER static vec<4, uint16, Q> pack(vec<4, float, Q> const& v)
  300. {
  301. vec<4, int16, Q> const Unpack(detail::toFloat16(v.x), detail::toFloat16(v.y), detail::toFloat16(v.z), detail::toFloat16(v.w));
  302. u16vec4 Packed;
  303. memcpy(&Packed, &Unpack, sizeof(Packed));
  304. return Packed;
  305. }
  306. GLM_FUNC_QUALIFIER static vec<4, float, Q> unpack(vec<4, uint16, Q> const& v)
  307. {
  308. i16vec4 Unpack;
  309. memcpy(&Unpack, &v, sizeof(Unpack));
  310. return vec<4, float, Q>(detail::toFloat32(v.x), detail::toFloat32(v.y), detail::toFloat32(v.z), detail::toFloat32(v.w));
  311. }
  312. };
  313. }//namespace detail
  314. GLM_FUNC_QUALIFIER uint8 packUnorm1x8(float v)
  315. {
  316. return static_cast<uint8>(round(clamp(v, 0.0f, 1.0f) * 255.0f));
  317. }
  318. GLM_FUNC_QUALIFIER float unpackUnorm1x8(uint8 p)
  319. {
  320. float const Unpack(p);
  321. return Unpack * static_cast<float>(0.0039215686274509803921568627451); // 1 / 255
  322. }
  323. GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const& v)
  324. {
  325. u8vec2 const Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f));
  326. uint16 Unpack = 0;
  327. memcpy(&Unpack, &Topack, sizeof(Unpack));
  328. return Unpack;
  329. }
  330. GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 p)
  331. {
  332. u8vec2 Unpack;
  333. memcpy(&Unpack, &p, sizeof(Unpack));
  334. return vec2(Unpack) * float(0.0039215686274509803921568627451); // 1 / 255
  335. }
  336. GLM_FUNC_QUALIFIER uint8 packSnorm1x8(float v)
  337. {
  338. int8 const Topack(static_cast<int8>(round(clamp(v ,-1.0f, 1.0f) * 127.0f)));
  339. uint8 Packed = 0;
  340. memcpy(&Packed, &Topack, sizeof(Packed));
  341. return Packed;
  342. }
  343. GLM_FUNC_QUALIFIER float unpackSnorm1x8(uint8 p)
  344. {
  345. int8 Unpack = 0;
  346. memcpy(&Unpack, &p, sizeof(Unpack));
  347. return clamp(
  348. static_cast<float>(Unpack) * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
  349. -1.0f, 1.0f);
  350. }
  351. GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const& v)
  352. {
  353. i8vec2 const Topack(round(clamp(v, -1.0f, 1.0f) * 127.0f));
  354. uint16 Packed = 0;
  355. memcpy(&Packed, &Topack, sizeof(Packed));
  356. return Packed;
  357. }
  358. GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 p)
  359. {
  360. i8vec2 Unpack;
  361. memcpy(&Unpack, &p, sizeof(Unpack));
  362. return clamp(
  363. vec2(Unpack) * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
  364. -1.0f, 1.0f);
  365. }
  366. GLM_FUNC_QUALIFIER uint16 packUnorm1x16(float s)
  367. {
  368. return static_cast<uint16>(round(clamp(s, 0.0f, 1.0f) * 65535.0f));
  369. }
  370. GLM_FUNC_QUALIFIER float unpackUnorm1x16(uint16 p)
  371. {
  372. float const Unpack(p);
  373. return Unpack * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0
  374. }
  375. GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const& v)
  376. {
  377. u16vec4 const Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f));
  378. uint64 Packed = 0;
  379. memcpy(&Packed, &Topack, sizeof(Packed));
  380. return Packed;
  381. }
  382. GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 p)
  383. {
  384. u16vec4 Unpack;
  385. memcpy(&Unpack, &p, sizeof(Unpack));
  386. return vec4(Unpack) * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0
  387. }
  388. GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float v)
  389. {
  390. int16 const Topack = static_cast<int16>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
  391. uint16 Packed = 0;
  392. memcpy(&Packed, &Topack, sizeof(Packed));
  393. return Packed;
  394. }
  395. GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 p)
  396. {
  397. int16 Unpack = 0;
  398. memcpy(&Unpack, &p, sizeof(Unpack));
  399. return clamp(
  400. static_cast<float>(Unpack) * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f,
  401. -1.0f, 1.0f);
  402. }
  403. GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const& v)
  404. {
  405. i16vec4 const Topack(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
  406. uint64 Packed = 0;
  407. memcpy(&Packed, &Topack, sizeof(Packed));
  408. return Packed;
  409. }
  410. GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 p)
  411. {
  412. i16vec4 Unpack;
  413. memcpy(&Unpack, &p, sizeof(Unpack));
  414. return clamp(
  415. vec4(Unpack) * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f,
  416. -1.0f, 1.0f);
  417. }
  418. GLM_FUNC_QUALIFIER uint16 packHalf1x16(float v)
  419. {
  420. int16 const Topack(detail::toFloat16(v));
  421. uint16 Packed = 0;
  422. memcpy(&Packed, &Topack, sizeof(Packed));
  423. return Packed;
  424. }
  425. GLM_FUNC_QUALIFIER float unpackHalf1x16(uint16 v)
  426. {
  427. int16 Unpack = 0;
  428. memcpy(&Unpack, &v, sizeof(Unpack));
  429. return detail::toFloat32(Unpack);
  430. }
  431. GLM_FUNC_QUALIFIER uint64 packHalf4x16(glm::vec4 const& v)
  432. {
  433. i16vec4 const Unpack(
  434. detail::toFloat16(v.x),
  435. detail::toFloat16(v.y),
  436. detail::toFloat16(v.z),
  437. detail::toFloat16(v.w));
  438. uint64 Packed = 0;
  439. memcpy(&Packed, &Unpack, sizeof(Packed));
  440. return Packed;
  441. }
  442. GLM_FUNC_QUALIFIER glm::vec4 unpackHalf4x16(uint64 v)
  443. {
  444. i16vec4 Unpack;
  445. memcpy(&Unpack, &v, sizeof(Unpack));
  446. return vec4(
  447. detail::toFloat32(Unpack.x),
  448. detail::toFloat32(Unpack.y),
  449. detail::toFloat32(Unpack.z),
  450. detail::toFloat32(Unpack.w));
  451. }
  452. GLM_FUNC_QUALIFIER uint32 packI3x10_1x2(ivec4 const& v)
  453. {
  454. detail::i10i10i10i2 Result;
  455. Result.data.x = v.x;
  456. Result.data.y = v.y;
  457. Result.data.z = v.z;
  458. Result.data.w = v.w;
  459. return Result.pack;
  460. }
  461. GLM_FUNC_QUALIFIER ivec4 unpackI3x10_1x2(uint32 v)
  462. {
  463. detail::i10i10i10i2 Unpack;
  464. Unpack.pack = v;
  465. return ivec4(
  466. Unpack.data.x,
  467. Unpack.data.y,
  468. Unpack.data.z,
  469. Unpack.data.w);
  470. }
  471. GLM_FUNC_QUALIFIER uint32 packU3x10_1x2(uvec4 const& v)
  472. {
  473. detail::u10u10u10u2 Result;
  474. Result.data.x = v.x;
  475. Result.data.y = v.y;
  476. Result.data.z = v.z;
  477. Result.data.w = v.w;
  478. return Result.pack;
  479. }
  480. GLM_FUNC_QUALIFIER uvec4 unpackU3x10_1x2(uint32 v)
  481. {
  482. detail::u10u10u10u2 Unpack;
  483. Unpack.pack = v;
  484. return uvec4(
  485. Unpack.data.x,
  486. Unpack.data.y,
  487. Unpack.data.z,
  488. Unpack.data.w);
  489. }
  490. GLM_FUNC_QUALIFIER uint32 packSnorm3x10_1x2(vec4 const& v)
  491. {
  492. ivec4 const Pack(round(clamp(v,-1.0f, 1.0f) * vec4(511.f, 511.f, 511.f, 1.f)));
  493. detail::i10i10i10i2 Result;
  494. Result.data.x = Pack.x;
  495. Result.data.y = Pack.y;
  496. Result.data.z = Pack.z;
  497. Result.data.w = Pack.w;
  498. return Result.pack;
  499. }
  500. GLM_FUNC_QUALIFIER vec4 unpackSnorm3x10_1x2(uint32 v)
  501. {
  502. detail::i10i10i10i2 Unpack;
  503. Unpack.pack = v;
  504. vec4 const Result(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w);
  505. return clamp(Result * vec4(1.f / 511.f, 1.f / 511.f, 1.f / 511.f, 1.f), -1.0f, 1.0f);
  506. }
  507. GLM_FUNC_QUALIFIER uint32 packUnorm3x10_1x2(vec4 const& v)
  508. {
  509. uvec4 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec4(1023.f, 1023.f, 1023.f, 3.f)));
  510. detail::u10u10u10u2 Result;
  511. Result.data.x = Unpack.x;
  512. Result.data.y = Unpack.y;
  513. Result.data.z = Unpack.z;
  514. Result.data.w = Unpack.w;
  515. return Result.pack;
  516. }
  517. GLM_FUNC_QUALIFIER vec4 unpackUnorm3x10_1x2(uint32 v)
  518. {
  519. vec4 const ScaleFactors(1.0f / 1023.f, 1.0f / 1023.f, 1.0f / 1023.f, 1.0f / 3.f);
  520. detail::u10u10u10u2 Unpack;
  521. Unpack.pack = v;
  522. return vec4(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w) * ScaleFactors;
  523. }
  524. GLM_FUNC_QUALIFIER uint32 packF2x11_1x10(vec3 const& v)
  525. {
  526. return
  527. ((detail::floatTo11bit(v.x) & ((1 << 11) - 1)) << 0) |
  528. ((detail::floatTo11bit(v.y) & ((1 << 11) - 1)) << 11) |
  529. ((detail::floatTo10bit(v.z) & ((1 << 10) - 1)) << 22);
  530. }
  531. GLM_FUNC_QUALIFIER vec3 unpackF2x11_1x10(uint32 v)
  532. {
  533. return vec3(
  534. detail::packed11bitToFloat(v >> 0),
  535. detail::packed11bitToFloat(v >> 11),
  536. detail::packed10bitToFloat(v >> 22));
  537. }
  538. GLM_FUNC_QUALIFIER uint32 packF3x9_E1x5(vec3 const& v)
  539. {
  540. float const SharedExpMax = (pow(2.0f, 9.0f - 1.0f) / pow(2.0f, 9.0f)) * pow(2.0f, 31.f - 15.f);
  541. vec3 const Color = clamp(v, 0.0f, SharedExpMax);
  542. float const MaxColor = max(Color.x, max(Color.y, Color.z));
  543. float const ExpSharedP = max(-15.f - 1.f, floor(log2(MaxColor))) + 1.0f + 15.f;
  544. float const MaxShared = floor(MaxColor / pow(2.0f, (ExpSharedP - 15.f - 9.f)) + 0.5f);
  545. float const ExpShared = equal(MaxShared, pow(2.0f, 9.0f), epsilon<float>()) ? ExpSharedP + 1.0f : ExpSharedP;
  546. uvec3 const ColorComp(floor(Color / pow(2.f, (ExpShared - 15.f - 9.f)) + 0.5f));
  547. detail::u9u9u9e5 Unpack;
  548. Unpack.data.x = ColorComp.x;
  549. Unpack.data.y = ColorComp.y;
  550. Unpack.data.z = ColorComp.z;
  551. Unpack.data.w = uint(ExpShared);
  552. return Unpack.pack;
  553. }
  554. GLM_FUNC_QUALIFIER vec3 unpackF3x9_E1x5(uint32 v)
  555. {
  556. detail::u9u9u9e5 Unpack;
  557. Unpack.pack = v;
  558. return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * pow(2.0f, Unpack.data.w - 15.f - 9.f);
  559. }
  560. // Based on Brian Karis http://graphicrants.blogspot.fr/2009/04/rgbm-color-encoding.html
  561. template<typename T, qualifier Q>
  562. GLM_FUNC_QUALIFIER vec<4, T, Q> packRGBM(vec<3, T, Q> const& rgb)
  563. {
  564. vec<3, T, Q> const Color(rgb * static_cast<T>(1.0 / 6.0));
  565. T Alpha = clamp(max(max(Color.x, Color.y), max(Color.z, static_cast<T>(1e-6))), static_cast<T>(0), static_cast<T>(1));
  566. Alpha = ceil(Alpha * static_cast<T>(255.0)) / static_cast<T>(255.0);
  567. return vec<4, T, Q>(Color / Alpha, Alpha);
  568. }
  569. template<typename T, qualifier Q>
  570. GLM_FUNC_QUALIFIER vec<3, T, Q> unpackRGBM(vec<4, T, Q> const& rgbm)
  571. {
  572. return vec<3, T, Q>(rgbm.x, rgbm.y, rgbm.z) * rgbm.w * static_cast<T>(6);
  573. }
  574. template<length_t L, qualifier Q>
  575. GLM_FUNC_QUALIFIER vec<L, uint16, Q> packHalf(vec<L, float, Q> const& v)
  576. {
  577. return detail::compute_half<L, Q>::pack(v);
  578. }
  579. template<length_t L, qualifier Q>
  580. GLM_FUNC_QUALIFIER vec<L, float, Q> unpackHalf(vec<L, uint16, Q> const& v)
  581. {
  582. return detail::compute_half<L, Q>::unpack(v);
  583. }
  584. template<typename uintType, length_t L, typename floatType, qualifier Q>
  585. GLM_FUNC_QUALIFIER vec<L, uintType, Q> packUnorm(vec<L, floatType, Q> const& v)
  586. {
  587. GLM_STATIC_ASSERT(std::numeric_limits<uintType>::is_integer, "uintType must be an integer type");
  588. GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
  589. return vec<L, uintType, Q>(round(clamp(v, static_cast<floatType>(0), static_cast<floatType>(1)) * static_cast<floatType>(std::numeric_limits<uintType>::max())));
  590. }
  591. template<typename floatType, length_t L, typename uintType, qualifier Q>
  592. GLM_FUNC_QUALIFIER vec<L, floatType, Q> unpackUnorm(vec<L, uintType, Q> const& v)
  593. {
  594. GLM_STATIC_ASSERT(std::numeric_limits<uintType>::is_integer, "uintType must be an integer type");
  595. GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
  596. return vec<L, float, Q>(v) * (static_cast<floatType>(1) / static_cast<floatType>(std::numeric_limits<uintType>::max()));
  597. }
  598. template<typename intType, length_t L, typename floatType, qualifier Q>
  599. GLM_FUNC_QUALIFIER vec<L, intType, Q> packSnorm(vec<L, floatType, Q> const& v)
  600. {
  601. GLM_STATIC_ASSERT(std::numeric_limits<intType>::is_integer, "uintType must be an integer type");
  602. GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
  603. return vec<L, intType, Q>(round(clamp(v , static_cast<floatType>(-1), static_cast<floatType>(1)) * static_cast<floatType>(std::numeric_limits<intType>::max())));
  604. }
  605. template<typename floatType, length_t L, typename intType, qualifier Q>
  606. GLM_FUNC_QUALIFIER vec<L, floatType, Q> unpackSnorm(vec<L, intType, Q> const& v)
  607. {
  608. GLM_STATIC_ASSERT(std::numeric_limits<intType>::is_integer, "uintType must be an integer type");
  609. GLM_STATIC_ASSERT(std::numeric_limits<floatType>::is_iec559, "floatType must be a floating point type");
  610. return clamp(vec<L, floatType, Q>(v) * (static_cast<floatType>(1) / static_cast<floatType>(std::numeric_limits<intType>::max())), static_cast<floatType>(-1), static_cast<floatType>(1));
  611. }
  612. GLM_FUNC_QUALIFIER uint8 packUnorm2x4(vec2 const& v)
  613. {
  614. u32vec2 const Unpack(round(clamp(v, 0.0f, 1.0f) * 15.0f));
  615. detail::u4u4 Result;
  616. Result.data.x = Unpack.x;
  617. Result.data.y = Unpack.y;
  618. return Result.pack;
  619. }
  620. GLM_FUNC_QUALIFIER vec2 unpackUnorm2x4(uint8 v)
  621. {
  622. float const ScaleFactor(1.f / 15.f);
  623. detail::u4u4 Unpack;
  624. Unpack.pack = v;
  625. return vec2(Unpack.data.x, Unpack.data.y) * ScaleFactor;
  626. }
  627. GLM_FUNC_QUALIFIER uint16 packUnorm4x4(vec4 const& v)
  628. {
  629. u32vec4 const Unpack(round(clamp(v, 0.0f, 1.0f) * 15.0f));
  630. detail::u4u4u4u4 Result;
  631. Result.data.x = Unpack.x;
  632. Result.data.y = Unpack.y;
  633. Result.data.z = Unpack.z;
  634. Result.data.w = Unpack.w;
  635. return Result.pack;
  636. }
  637. GLM_FUNC_QUALIFIER vec4 unpackUnorm4x4(uint16 v)
  638. {
  639. float const ScaleFactor(1.f / 15.f);
  640. detail::u4u4u4u4 Unpack;
  641. Unpack.pack = v;
  642. return vec4(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w) * ScaleFactor;
  643. }
  644. GLM_FUNC_QUALIFIER uint16 packUnorm1x5_1x6_1x5(vec3 const& v)
  645. {
  646. u32vec3 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec3(31.f, 63.f, 31.f)));
  647. detail::u5u6u5 Result;
  648. Result.data.x = Unpack.x;
  649. Result.data.y = Unpack.y;
  650. Result.data.z = Unpack.z;
  651. return Result.pack;
  652. }
  653. GLM_FUNC_QUALIFIER vec3 unpackUnorm1x5_1x6_1x5(uint16 v)
  654. {
  655. vec3 const ScaleFactor(1.f / 31.f, 1.f / 63.f, 1.f / 31.f);
  656. detail::u5u6u5 Unpack;
  657. Unpack.pack = v;
  658. return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * ScaleFactor;
  659. }
  660. GLM_FUNC_QUALIFIER uint16 packUnorm3x5_1x1(vec4 const& v)
  661. {
  662. u32vec4 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec4(31.f, 31.f, 31.f, 1.f)));
  663. detail::u5u5u5u1 Result;
  664. Result.data.x = Unpack.x;
  665. Result.data.y = Unpack.y;
  666. Result.data.z = Unpack.z;
  667. Result.data.w = Unpack.w;
  668. return Result.pack;
  669. }
  670. GLM_FUNC_QUALIFIER vec4 unpackUnorm3x5_1x1(uint16 v)
  671. {
  672. vec4 const ScaleFactor(1.f / 31.f, 1.f / 31.f, 1.f / 31.f, 1.f);
  673. detail::u5u5u5u1 Unpack;
  674. Unpack.pack = v;
  675. return vec4(Unpack.data.x, Unpack.data.y, Unpack.data.z, Unpack.data.w) * ScaleFactor;
  676. }
  677. GLM_FUNC_QUALIFIER uint8 packUnorm2x3_1x2(vec3 const& v)
  678. {
  679. u32vec3 const Unpack(round(clamp(v, 0.0f, 1.0f) * vec3(7.f, 7.f, 3.f)));
  680. detail::u3u3u2 Result;
  681. Result.data.x = Unpack.x;
  682. Result.data.y = Unpack.y;
  683. Result.data.z = Unpack.z;
  684. return Result.pack;
  685. }
  686. GLM_FUNC_QUALIFIER vec3 unpackUnorm2x3_1x2(uint8 v)
  687. {
  688. vec3 const ScaleFactor(1.f / 7.f, 1.f / 7.f, 1.f / 3.f);
  689. detail::u3u3u2 Unpack;
  690. Unpack.pack = v;
  691. return vec3(Unpack.data.x, Unpack.data.y, Unpack.data.z) * ScaleFactor;
  692. }
  693. GLM_FUNC_QUALIFIER int16 packInt2x8(i8vec2 const& v)
  694. {
  695. int16 Pack = 0;
  696. memcpy(&Pack, &v, sizeof(Pack));
  697. return Pack;
  698. }
  699. GLM_FUNC_QUALIFIER i8vec2 unpackInt2x8(int16 p)
  700. {
  701. i8vec2 Unpack;
  702. memcpy(&Unpack, &p, sizeof(Unpack));
  703. return Unpack;
  704. }
  705. GLM_FUNC_QUALIFIER uint16 packUint2x8(u8vec2 const& v)
  706. {
  707. uint16 Pack = 0;
  708. memcpy(&Pack, &v, sizeof(Pack));
  709. return Pack;
  710. }
  711. GLM_FUNC_QUALIFIER u8vec2 unpackUint2x8(uint16 p)
  712. {
  713. u8vec2 Unpack;
  714. memcpy(&Unpack, &p, sizeof(Unpack));
  715. return Unpack;
  716. }
  717. GLM_FUNC_QUALIFIER int32 packInt4x8(i8vec4 const& v)
  718. {
  719. int32 Pack = 0;
  720. memcpy(&Pack, &v, sizeof(Pack));
  721. return Pack;
  722. }
  723. GLM_FUNC_QUALIFIER i8vec4 unpackInt4x8(int32 p)
  724. {
  725. i8vec4 Unpack;
  726. memcpy(&Unpack, &p, sizeof(Unpack));
  727. return Unpack;
  728. }
  729. GLM_FUNC_QUALIFIER uint32 packUint4x8(u8vec4 const& v)
  730. {
  731. uint32 Pack = 0;
  732. memcpy(&Pack, &v, sizeof(Pack));
  733. return Pack;
  734. }
  735. GLM_FUNC_QUALIFIER u8vec4 unpackUint4x8(uint32 p)
  736. {
  737. u8vec4 Unpack;
  738. memcpy(&Unpack, &p, sizeof(Unpack));
  739. return Unpack;
  740. }
  741. GLM_FUNC_QUALIFIER int packInt2x16(i16vec2 const& v)
  742. {
  743. int Pack = 0;
  744. memcpy(&Pack, &v, sizeof(Pack));
  745. return Pack;
  746. }
  747. GLM_FUNC_QUALIFIER i16vec2 unpackInt2x16(int p)
  748. {
  749. i16vec2 Unpack;
  750. memcpy(&Unpack, &p, sizeof(Unpack));
  751. return Unpack;
  752. }
  753. GLM_FUNC_QUALIFIER int64 packInt4x16(i16vec4 const& v)
  754. {
  755. int64 Pack = 0;
  756. memcpy(&Pack, &v, sizeof(Pack));
  757. return Pack;
  758. }
  759. GLM_FUNC_QUALIFIER i16vec4 unpackInt4x16(int64 p)
  760. {
  761. i16vec4 Unpack;
  762. memcpy(&Unpack, &p, sizeof(Unpack));
  763. return Unpack;
  764. }
  765. GLM_FUNC_QUALIFIER uint packUint2x16(u16vec2 const& v)
  766. {
  767. uint Pack = 0;
  768. memcpy(&Pack, &v, sizeof(Pack));
  769. return Pack;
  770. }
  771. GLM_FUNC_QUALIFIER u16vec2 unpackUint2x16(uint p)
  772. {
  773. u16vec2 Unpack;
  774. memcpy(&Unpack, &p, sizeof(Unpack));
  775. return Unpack;
  776. }
  777. GLM_FUNC_QUALIFIER uint64 packUint4x16(u16vec4 const& v)
  778. {
  779. uint64 Pack = 0;
  780. memcpy(&Pack, &v, sizeof(Pack));
  781. return Pack;
  782. }
  783. GLM_FUNC_QUALIFIER u16vec4 unpackUint4x16(uint64 p)
  784. {
  785. u16vec4 Unpack;
  786. memcpy(&Unpack, &p, sizeof(Unpack));
  787. return Unpack;
  788. }
  789. GLM_FUNC_QUALIFIER int64 packInt2x32(i32vec2 const& v)
  790. {
  791. int64 Pack = 0;
  792. memcpy(&Pack, &v, sizeof(Pack));
  793. return Pack;
  794. }
  795. GLM_FUNC_QUALIFIER i32vec2 unpackInt2x32(int64 p)
  796. {
  797. i32vec2 Unpack;
  798. memcpy(&Unpack, &p, sizeof(Unpack));
  799. return Unpack;
  800. }
  801. GLM_FUNC_QUALIFIER uint64 packUint2x32(u32vec2 const& v)
  802. {
  803. uint64 Pack = 0;
  804. memcpy(&Pack, &v, sizeof(Pack));
  805. return Pack;
  806. }
  807. GLM_FUNC_QUALIFIER u32vec2 unpackUint2x32(uint64 p)
  808. {
  809. u32vec2 Unpack;
  810. memcpy(&Unpack, &p, sizeof(Unpack));
  811. return Unpack;
  812. }
  813. }//namespace glm