func_packing.inl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /// @ref core
  2. /// @file glm/detail/func_packing.inl
  3. #include "../common.hpp"
  4. #include "type_half.hpp"
  5. namespace glm
  6. {
  7. GLM_FUNC_QUALIFIER uint packUnorm2x16(vec2 const& v)
  8. {
  9. union
  10. {
  11. unsigned short in[2];
  12. uint out;
  13. } u;
  14. vec<2, unsigned short, defaultp> result(round(clamp(v, 0.0f, 1.0f) * 65535.0f));
  15. u.in[0] = result[0];
  16. u.in[1] = result[1];
  17. return u.out;
  18. }
  19. GLM_FUNC_QUALIFIER vec2 unpackUnorm2x16(uint p)
  20. {
  21. union
  22. {
  23. uint in;
  24. unsigned short out[2];
  25. } u;
  26. u.in = p;
  27. return vec2(u.out[0], u.out[1]) * 1.5259021896696421759365224689097e-5f;
  28. }
  29. GLM_FUNC_QUALIFIER uint packSnorm2x16(vec2 const& v)
  30. {
  31. union
  32. {
  33. signed short in[2];
  34. uint out;
  35. } u;
  36. vec<2, short, defaultp> result(round(clamp(v, -1.0f, 1.0f) * 32767.0f));
  37. u.in[0] = result[0];
  38. u.in[1] = result[1];
  39. return u.out;
  40. }
  41. GLM_FUNC_QUALIFIER vec2 unpackSnorm2x16(uint p)
  42. {
  43. union
  44. {
  45. uint in;
  46. signed short out[2];
  47. } u;
  48. u.in = p;
  49. return clamp(vec2(u.out[0], u.out[1]) * 3.0518509475997192297128208258309e-5f, -1.0f, 1.0f);
  50. }
  51. GLM_FUNC_QUALIFIER uint packUnorm4x8(vec4 const& v)
  52. {
  53. union
  54. {
  55. unsigned char in[4];
  56. uint out;
  57. } u;
  58. vec<4, unsigned char, defaultp> result(round(clamp(v, 0.0f, 1.0f) * 255.0f));
  59. u.in[0] = result[0];
  60. u.in[1] = result[1];
  61. u.in[2] = result[2];
  62. u.in[3] = result[3];
  63. return u.out;
  64. }
  65. GLM_FUNC_QUALIFIER vec4 unpackUnorm4x8(uint p)
  66. {
  67. union
  68. {
  69. uint in;
  70. unsigned char out[4];
  71. } u;
  72. u.in = p;
  73. return vec4(u.out[0], u.out[1], u.out[2], u.out[3]) * 0.0039215686274509803921568627451f;
  74. }
  75. GLM_FUNC_QUALIFIER uint packSnorm4x8(vec4 const& v)
  76. {
  77. union
  78. {
  79. signed char in[4];
  80. uint out;
  81. } u;
  82. vec<4, signed char, defaultp> result(round(clamp(v, -1.0f, 1.0f) * 127.0f));
  83. u.in[0] = result[0];
  84. u.in[1] = result[1];
  85. u.in[2] = result[2];
  86. u.in[3] = result[3];
  87. return u.out;
  88. }
  89. GLM_FUNC_QUALIFIER glm::vec4 unpackSnorm4x8(uint p)
  90. {
  91. union
  92. {
  93. uint in;
  94. signed char out[4];
  95. } u;
  96. u.in = p;
  97. return clamp(vec4(u.out[0], u.out[1], u.out[2], u.out[3]) * 0.0078740157480315f, -1.0f, 1.0f);
  98. }
  99. GLM_FUNC_QUALIFIER double packDouble2x32(uvec2 const& v)
  100. {
  101. union
  102. {
  103. uint in[2];
  104. double out;
  105. } u;
  106. u.in[0] = v[0];
  107. u.in[1] = v[1];
  108. return u.out;
  109. }
  110. GLM_FUNC_QUALIFIER uvec2 unpackDouble2x32(double v)
  111. {
  112. union
  113. {
  114. double in;
  115. uint out[2];
  116. } u;
  117. u.in = v;
  118. return uvec2(u.out[0], u.out[1]);
  119. }
  120. GLM_FUNC_QUALIFIER uint packHalf2x16(vec2 const& v)
  121. {
  122. union
  123. {
  124. signed short in[2];
  125. uint out;
  126. } u;
  127. u.in[0] = detail::toFloat16(v.x);
  128. u.in[1] = detail::toFloat16(v.y);
  129. return u.out;
  130. }
  131. GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint v)
  132. {
  133. union
  134. {
  135. uint in;
  136. signed short out[2];
  137. } u;
  138. u.in = v;
  139. return vec2(
  140. detail::toFloat32(u.out[0]),
  141. detail::toFloat32(u.out[1]));
  142. }
  143. }//namespace glm
  144. #if GLM_CONFIG_SIMD == GLM_ENABLE
  145. # include "func_packing_simd.inl"
  146. #endif