gtc_packing.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #include <glm/gtc/packing.hpp>
  29. #include <cstdio>
  30. #include <vector>
  31. void print_bits(float const & s)
  32. {
  33. union
  34. {
  35. float f;
  36. unsigned int i;
  37. } uif;
  38. uif.f = s;
  39. printf("f32: ");
  40. for(std::size_t j = sizeof(s) * 8; j > 0; --j)
  41. {
  42. if(j == 23 || j == 31)
  43. printf(" ");
  44. printf("%d", (uif.i & (1 << (j - 1))) ? 1 : 0);
  45. }
  46. }
  47. void print_10bits(glm::uint const & s)
  48. {
  49. printf("10b: ");
  50. for(std::size_t j = 10; j > 0; --j)
  51. {
  52. if(j == 5)
  53. printf(" ");
  54. printf("%d", (s & (1 << (j - 1))) ? 1 : 0);
  55. }
  56. }
  57. void print_11bits(glm::uint const & s)
  58. {
  59. printf("11b: ");
  60. for(std::size_t j = 11; j > 0; --j)
  61. {
  62. if(j == 6)
  63. printf(" ");
  64. printf("%d", (s & (1 << (j - 1))) ? 1 : 0);
  65. }
  66. }
  67. void print_value(float const & s)
  68. {
  69. printf("%2.5f, ", s);
  70. print_bits(s);
  71. printf(", ");
  72. // print_11bits(detail::floatTo11bit(s));
  73. // printf(", ");
  74. // print_10bits(detail::floatTo10bit(s));
  75. printf("\n");
  76. }
  77. int test_Half1x16()
  78. {
  79. int Error = 0;
  80. std::vector<float> Tests;
  81. Tests.push_back(0.0f);
  82. Tests.push_back(1.0f);
  83. Tests.push_back(-1.0f);
  84. Tests.push_back(2.0f);
  85. Tests.push_back(-2.0f);
  86. Tests.push_back(1.9f);
  87. for(std::size_t i = 0; i < Tests.size(); ++i)
  88. {
  89. glm::uint32 p0 = glm::packHalf1x16(Tests[i]);
  90. float v0 = glm::unpackHalf1x16(p0);
  91. glm::uint32 p1 = glm::packHalf1x16(v0);
  92. float v1 = glm::unpackHalf1x16(p1);
  93. Error += (v0 == v1) ? 0 : 1;
  94. }
  95. return Error;
  96. }
  97. int test_Half4x16()
  98. {
  99. int Error = 0;
  100. std::vector<glm::vec4> Tests;
  101. Tests.push_back(glm::vec4(1.0f));
  102. Tests.push_back(glm::vec4(0.0f));
  103. Tests.push_back(glm::vec4(2.0f));
  104. Tests.push_back(glm::vec4(0.1f));
  105. Tests.push_back(glm::vec4(0.5f));
  106. Tests.push_back(glm::vec4(-0.9f));
  107. for(std::size_t i = 0; i < Tests.size(); ++i)
  108. {
  109. glm::uint64 p0 = glm::packHalf4x16(Tests[i]);
  110. glm::vec4 v0 = glm::unpackHalf4x16(p0);
  111. glm::uint64 p1 = glm::packHalf4x16(v0);
  112. glm::vec4 v1 = glm::unpackHalf4x16(p1);
  113. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  114. }
  115. return Error;
  116. }
  117. int test_I3x10_1x2()
  118. {
  119. int Error = 0;
  120. std::vector<glm::ivec4> Tests;
  121. Tests.push_back(glm::ivec4(0));
  122. Tests.push_back(glm::ivec4(1));
  123. Tests.push_back(glm::ivec4(-1));
  124. Tests.push_back(glm::ivec4(2));
  125. Tests.push_back(glm::ivec4(-2));
  126. Tests.push_back(glm::ivec4(3));
  127. for(std::size_t i = 0; i < Tests.size(); ++i)
  128. {
  129. glm::uint32 p0 = glm::packI3x10_1x2(Tests[i]);
  130. glm::ivec4 v0 = glm::unpackI3x10_1x2(p0);
  131. glm::uint32 p1 = glm::packI3x10_1x2(v0);
  132. glm::ivec4 v1 = glm::unpackI3x10_1x2(p1);
  133. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  134. }
  135. return Error;
  136. }
  137. int test_U3x10_1x2()
  138. {
  139. int Error = 0;
  140. std::vector<glm::uvec4> Tests;
  141. Tests.push_back(glm::uvec4(0));
  142. Tests.push_back(glm::uvec4(1));
  143. Tests.push_back(glm::uvec4(2));
  144. Tests.push_back(glm::uvec4(3));
  145. Tests.push_back(glm::uvec4(4));
  146. Tests.push_back(glm::uvec4(5));
  147. for(std::size_t i = 0; i < Tests.size(); ++i)
  148. {
  149. glm::uint32 p0 = glm::packU3x10_1x2(Tests[i]);
  150. glm::uvec4 v0 = glm::unpackU3x10_1x2(p0);
  151. glm::uint32 p1 = glm::packU3x10_1x2(v0);
  152. glm::uvec4 v1 = glm::unpackU3x10_1x2(p1);
  153. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  154. }
  155. return Error;
  156. }
  157. int test_Snorm3x10_1x2()
  158. {
  159. int Error = 0;
  160. std::vector<glm::vec4> Tests;
  161. Tests.push_back(glm::vec4(1.0f));
  162. Tests.push_back(glm::vec4(0.0f));
  163. Tests.push_back(glm::vec4(2.0f));
  164. Tests.push_back(glm::vec4(0.1f));
  165. Tests.push_back(glm::vec4(0.5f));
  166. Tests.push_back(glm::vec4(0.9f));
  167. for(std::size_t i = 0; i < Tests.size(); ++i)
  168. {
  169. glm::uint32 p0 = glm::packSnorm3x10_1x2(Tests[i]);
  170. glm::vec4 v0 = glm::unpackSnorm3x10_1x2(p0);
  171. glm::uint32 p1 = glm::packSnorm3x10_1x2(v0);
  172. glm::vec4 v1 = glm::unpackSnorm3x10_1x2(p1);
  173. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  174. }
  175. return Error;
  176. }
  177. int test_Unorm3x10_1x2()
  178. {
  179. int Error = 0;
  180. std::vector<glm::vec4> Tests;
  181. Tests.push_back(glm::vec4(1.0f));
  182. Tests.push_back(glm::vec4(0.0f));
  183. Tests.push_back(glm::vec4(2.0f));
  184. Tests.push_back(glm::vec4(0.1f));
  185. Tests.push_back(glm::vec4(0.5f));
  186. Tests.push_back(glm::vec4(0.9f));
  187. for(std::size_t i = 0; i < Tests.size(); ++i)
  188. {
  189. glm::uint32 p0 = glm::packSnorm3x10_1x2(Tests[i]);
  190. glm::vec4 v0 = glm::unpackSnorm3x10_1x2(p0);
  191. glm::uint32 p1 = glm::packSnorm3x10_1x2(v0);
  192. glm::vec4 v1 = glm::unpackSnorm3x10_1x2(p1);
  193. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  194. }
  195. return Error;
  196. }
  197. int test_F2x11_1x10()
  198. {
  199. int Error = 0;
  200. std::vector<glm::vec3> Tests;
  201. Tests.push_back(glm::vec3(1.0f));
  202. Tests.push_back(glm::vec3(0.0f));
  203. Tests.push_back(glm::vec3(2.0f));
  204. Tests.push_back(glm::vec3(0.1f));
  205. Tests.push_back(glm::vec3(0.5f));
  206. Tests.push_back(glm::vec3(0.9f));
  207. for(std::size_t i = 0; i < Tests.size(); ++i)
  208. {
  209. glm::uint32 p0 = glm::packF2x11_1x10(Tests[i]);
  210. glm::vec3 v0 = glm::unpackF2x11_1x10(p0);
  211. glm::uint32 p1 = glm::packF2x11_1x10(v0);
  212. glm::vec3 v1 = glm::unpackF2x11_1x10(p1);
  213. Error += glm::all(glm::equal(v0, v1)) ? 0 : 1;
  214. }
  215. return Error;
  216. }
  217. int main()
  218. {
  219. int Error(0);
  220. Error += test_F2x11_1x10();
  221. Error += test_Snorm3x10_1x2();
  222. Error += test_Unorm3x10_1x2();
  223. Error += test_I3x10_1x2();
  224. Error += test_U3x10_1x2();
  225. Error += test_Half1x16();
  226. Error += test_U3x10_1x2();
  227. return Error;
  228. }