2
0

gtc_packing.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref test
  24. /// @file test/gtc/packing.cpp
  25. /// @date 2013-08-09 / 2013-08-09
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. #define GLM_FORCE_RADIANS
  29. #include <glm/gtc/packing.hpp>
  30. #include <cstdio>
  31. #include <vector>
  32. void print_bits(float const & s)
  33. {
  34. union
  35. {
  36. float f;
  37. unsigned int i;
  38. } uif;
  39. uif.f = s;
  40. printf("f32: ");
  41. for(std::size_t j = sizeof(s) * 8; j > 0; --j)
  42. {
  43. if(j == 23 || j == 31)
  44. printf(" ");
  45. printf("%d", (uif.i & (1 << (j - 1))) ? 1 : 0);
  46. }
  47. }
  48. void print_10bits(glm::uint const & s)
  49. {
  50. printf("10b: ");
  51. for(std::size_t j = 10; j > 0; --j)
  52. {
  53. if(j == 5)
  54. printf(" ");
  55. printf("%d", (s & (1 << (j - 1))) ? 1 : 0);
  56. }
  57. }
  58. void print_11bits(glm::uint const & s)
  59. {
  60. printf("11b: ");
  61. for(std::size_t j = 11; j > 0; --j)
  62. {
  63. if(j == 6)
  64. printf(" ");
  65. printf("%d", (s & (1 << (j - 1))) ? 1 : 0);
  66. }
  67. }
  68. void print_value(float const & s)
  69. {
  70. printf("%2.5f, ", s);
  71. print_bits(s);
  72. printf(", ");
  73. // print_11bits(detail::floatTo11bit(s));
  74. // printf(", ");
  75. // print_10bits(detail::floatTo10bit(s));
  76. printf("\n");
  77. }
  78. int test_Half1x16()
  79. {
  80. int Error = 0;
  81. std::vector<float> Tests;
  82. Tests.push_back(0.0f);
  83. Tests.push_back(1.0f);
  84. Tests.push_back(-1.0f);
  85. Tests.push_back(2.0f);
  86. Tests.push_back(-2.0f);
  87. Tests.push_back(1.9f);
  88. for(std::size_t i = 0; i < Tests.size(); ++i)
  89. {
  90. glm::uint32 p0 = glm::packHalf1x16(Tests[i]);
  91. float v0 = glm::unpackHalf1x16(p0);
  92. glm::uint32 p1 = glm::packHalf1x16(v0);
  93. float v1 = glm::unpackHalf1x16(p1);
  94. Error += (v0 == v1) ? 0 : 1;
  95. }
  96. return Error;
  97. }
  98. int test_Half4x16()
  99. {
  100. int Error = 0;
  101. std::vector<glm::vec4> Tests;
  102. Tests.push_back(glm::vec4(1.0f));
  103. Tests.push_back(glm::vec4(0.0f));
  104. Tests.push_back(glm::vec4(2.0f));
  105. Tests.push_back(glm::vec4(0.1f));
  106. Tests.push_back(glm::vec4(0.5f));
  107. Tests.push_back(glm::vec4(-0.9f));
  108. for(std::size_t i = 0; i < Tests.size(); ++i)
  109. {
  110. glm::uint64 p0 = glm::packHalf4x16(Tests[i]);
  111. glm::vec4 v0 = glm::unpackHalf4x16(p0);
  112. glm::uint64 p1 = glm::packHalf4x16(v0);
  113. glm::vec4 v1 = glm::unpackHalf4x16(p1);
  114. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  115. }
  116. return Error;
  117. }
  118. int test_I3x10_1x2()
  119. {
  120. int Error = 0;
  121. std::vector<glm::ivec4> Tests;
  122. Tests.push_back(glm::ivec4(0));
  123. Tests.push_back(glm::ivec4(1));
  124. Tests.push_back(glm::ivec4(-1));
  125. Tests.push_back(glm::ivec4(2));
  126. Tests.push_back(glm::ivec4(-2));
  127. Tests.push_back(glm::ivec4(3));
  128. for(std::size_t i = 0; i < Tests.size(); ++i)
  129. {
  130. glm::uint32 p0 = glm::packI3x10_1x2(Tests[i]);
  131. glm::ivec4 v0 = glm::unpackI3x10_1x2(p0);
  132. glm::uint32 p1 = glm::packI3x10_1x2(v0);
  133. glm::ivec4 v1 = glm::unpackI3x10_1x2(p1);
  134. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  135. }
  136. return Error;
  137. }
  138. int test_U3x10_1x2()
  139. {
  140. int Error = 0;
  141. std::vector<glm::uvec4> Tests;
  142. Tests.push_back(glm::uvec4(0));
  143. Tests.push_back(glm::uvec4(1));
  144. Tests.push_back(glm::uvec4(2));
  145. Tests.push_back(glm::uvec4(3));
  146. Tests.push_back(glm::uvec4(4));
  147. Tests.push_back(glm::uvec4(5));
  148. for(std::size_t i = 0; i < Tests.size(); ++i)
  149. {
  150. glm::uint32 p0 = glm::packU3x10_1x2(Tests[i]);
  151. glm::uvec4 v0 = glm::unpackU3x10_1x2(p0);
  152. glm::uint32 p1 = glm::packU3x10_1x2(v0);
  153. glm::uvec4 v1 = glm::unpackU3x10_1x2(p1);
  154. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  155. }
  156. return Error;
  157. }
  158. int test_Snorm3x10_1x2()
  159. {
  160. int Error = 0;
  161. std::vector<glm::vec4> Tests;
  162. Tests.push_back(glm::vec4(1.0f));
  163. Tests.push_back(glm::vec4(0.0f));
  164. Tests.push_back(glm::vec4(2.0f));
  165. Tests.push_back(glm::vec4(0.1f));
  166. Tests.push_back(glm::vec4(0.5f));
  167. Tests.push_back(glm::vec4(0.9f));
  168. for(std::size_t i = 0; i < Tests.size(); ++i)
  169. {
  170. glm::uint32 p0 = glm::packSnorm3x10_1x2(Tests[i]);
  171. glm::vec4 v0 = glm::unpackSnorm3x10_1x2(p0);
  172. glm::uint32 p1 = glm::packSnorm3x10_1x2(v0);
  173. glm::vec4 v1 = glm::unpackSnorm3x10_1x2(p1);
  174. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  175. }
  176. return Error;
  177. }
  178. int test_Unorm3x10_1x2()
  179. {
  180. int Error = 0;
  181. std::vector<glm::vec4> Tests;
  182. Tests.push_back(glm::vec4(1.0f));
  183. Tests.push_back(glm::vec4(0.0f));
  184. Tests.push_back(glm::vec4(2.0f));
  185. Tests.push_back(glm::vec4(0.1f));
  186. Tests.push_back(glm::vec4(0.5f));
  187. Tests.push_back(glm::vec4(0.9f));
  188. for(std::size_t i = 0; i < Tests.size(); ++i)
  189. {
  190. glm::uint32 p0 = glm::packSnorm3x10_1x2(Tests[i]);
  191. glm::vec4 v0 = glm::unpackSnorm3x10_1x2(p0);
  192. glm::uint32 p1 = glm::packSnorm3x10_1x2(v0);
  193. glm::vec4 v1 = glm::unpackSnorm3x10_1x2(p1);
  194. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  195. }
  196. return Error;
  197. }
  198. int test_F2x11_1x10()
  199. {
  200. int Error = 0;
  201. std::vector<glm::vec3> Tests;
  202. Tests.push_back(glm::vec3(1.0f));
  203. Tests.push_back(glm::vec3(0.0f));
  204. Tests.push_back(glm::vec3(2.0f));
  205. Tests.push_back(glm::vec3(0.1f));
  206. Tests.push_back(glm::vec3(0.5f));
  207. Tests.push_back(glm::vec3(0.9f));
  208. for(std::size_t i = 0; i < Tests.size(); ++i)
  209. {
  210. glm::uint32 p0 = glm::packF2x11_1x10(Tests[i]);
  211. glm::vec3 v0 = glm::unpackF2x11_1x10(p0);
  212. glm::uint32 p1 = glm::packF2x11_1x10(v0);
  213. glm::vec3 v1 = glm::unpackF2x11_1x10(p1);
  214. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  215. }
  216. return Error;
  217. }
  218. int main()
  219. {
  220. int Error(0);
  221. Error += test_F2x11_1x10();
  222. Error += test_Snorm3x10_1x2();
  223. Error += test_Unorm3x10_1x2();
  224. Error += test_I3x10_1x2();
  225. Error += test_U3x10_1x2();
  226. Error += test_Half1x16();
  227. Error += test_U3x10_1x2();
  228. return Error;
  229. }