Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
5.3 KiB

5 years ago
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #define GLM_FORCE_INLINE
  3. #include <glm/gtc/epsilon.hpp>
  4. #include <glm/gtc/integer.hpp>
  5. #include <glm/gtc/type_precision.hpp>
  6. #include <glm/gtc/vec1.hpp>
  7. #include <glm/gtx/type_aligned.hpp>
  8. #include <glm/vector_relational.hpp>
  9. #include <glm/vec2.hpp>
  10. #include <glm/vec3.hpp>
  11. #include <glm/vec4.hpp>
  12. #include <ctime>
  13. #include <cstdio>
  14. #include <vector>
  15. #include <cmath>
  16. namespace log2_
  17. {
  18. int test()
  19. {
  20. int Error = 0;
  21. int A0 = static_cast<int>(glm::log2(16.f));
  22. glm::ivec1 B0(glm::log2(glm::vec1(16.f)));
  23. glm::ivec2 C0(glm::log2(glm::vec2(16.f)));
  24. glm::ivec3 D0(glm::log2(glm::vec3(16.f)));
  25. glm::ivec4 E0(glm::log2(glm::vec4(16.f)));
  26. int A1 = glm::log2(int(16));
  27. glm::ivec1 B1 = glm::log2(glm::ivec1(16));
  28. glm::ivec2 C1 = glm::log2(glm::ivec2(16));
  29. glm::ivec3 D1 = glm::log2(glm::ivec3(16));
  30. glm::ivec4 E1 = glm::log2(glm::ivec4(16));
  31. Error += A0 == A1 ? 0 : 1;
  32. Error += glm::all(glm::equal(B0, B1)) ? 0 : 1;
  33. Error += glm::all(glm::equal(C0, C1)) ? 0 : 1;
  34. Error += glm::all(glm::equal(D0, D1)) ? 0 : 1;
  35. Error += glm::all(glm::equal(E0, E1)) ? 0 : 1;
  36. glm::uint64 A2 = glm::log2(glm::uint64(16));
  37. glm::u64vec1 B2 = glm::log2(glm::u64vec1(16));
  38. glm::u64vec2 C2 = glm::log2(glm::u64vec2(16));
  39. glm::u64vec3 D2 = glm::log2(glm::u64vec3(16));
  40. glm::u64vec4 E2 = glm::log2(glm::u64vec4(16));
  41. Error += A2 == glm::uint64(4) ? 0 : 1;
  42. Error += glm::all(glm::equal(B2, glm::u64vec1(4))) ? 0 : 1;
  43. Error += glm::all(glm::equal(C2, glm::u64vec2(4))) ? 0 : 1;
  44. Error += glm::all(glm::equal(D2, glm::u64vec3(4))) ? 0 : 1;
  45. Error += glm::all(glm::equal(E2, glm::u64vec4(4))) ? 0 : 1;
  46. return Error;
  47. }
  48. int perf(std::size_t Count)
  49. {
  50. int Error = 0;
  51. {
  52. std::vector<int> Result;
  53. Result.resize(Count);
  54. std::clock_t Begin = clock();
  55. for(int i = 0; i < static_cast<int>(Count); ++i)
  56. Result[i] = glm::log2(static_cast<int>(i));
  57. std::clock_t End = clock();
  58. printf("glm::log2<int>: %d clocks\n", static_cast<int>(End - Begin));
  59. }
  60. {
  61. std::vector<glm::ivec4> Result;
  62. Result.resize(Count);
  63. std::clock_t Begin = clock();
  64. for(int i = 0; i < static_cast<int>(Count); ++i)
  65. Result[i] = glm::log2(glm::ivec4(i));
  66. std::clock_t End = clock();
  67. printf("glm::log2<ivec4>: %d clocks\n", static_cast<int>(End - Begin));
  68. }
  69. # if GLM_HAS_BITSCAN_WINDOWS
  70. {
  71. std::vector<glm::ivec4> Result;
  72. Result.resize(Count);
  73. std::clock_t Begin = clock();
  74. for(std::size_t i = 0; i < Count; ++i)
  75. {
  76. glm::vec<4, unsigned long, glm::defaultp> Tmp;
  77. _BitScanReverse(&Tmp.x, i);
  78. _BitScanReverse(&Tmp.y, i);
  79. _BitScanReverse(&Tmp.z, i);
  80. _BitScanReverse(&Tmp.w, i);
  81. Result[i] = glm::ivec4(Tmp);
  82. }
  83. std::clock_t End = clock();
  84. printf("glm::log2<ivec4> inlined: %d clocks\n", static_cast<int>(End - Begin));
  85. }
  86. {
  87. std::vector<glm::vec<4, unsigned long, glm::defaultp> > Result;
  88. Result.resize(Count);
  89. std::clock_t Begin = clock();
  90. for(std::size_t i = 0; i < Count; ++i)
  91. {
  92. _BitScanReverse(&Result[i].x, i);
  93. _BitScanReverse(&Result[i].y, i);
  94. _BitScanReverse(&Result[i].z, i);
  95. _BitScanReverse(&Result[i].w, i);
  96. }
  97. std::clock_t End = clock();
  98. printf("glm::log2<ivec4> inlined no cast: %d clocks\n", static_cast<int>(End - Begin));
  99. }
  100. {
  101. std::vector<glm::ivec4> Result;
  102. Result.resize(Count);
  103. std::clock_t Begin = clock();
  104. for(std::size_t i = 0; i < Count; ++i)
  105. {
  106. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].x), i);
  107. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].y), i);
  108. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].z), i);
  109. _BitScanReverse(reinterpret_cast<unsigned long*>(&Result[i].w), i);
  110. }
  111. std::clock_t End = clock();
  112. printf("glm::log2<ivec4> reinterpret: %d clocks\n", static_cast<int>(End - Begin));
  113. }
  114. # endif//GLM_HAS_BITSCAN_WINDOWS
  115. {
  116. std::vector<float> Result;
  117. Result.resize(Count);
  118. std::clock_t Begin = clock();
  119. for(std::size_t i = 0; i < Count; ++i)
  120. Result[i] = glm::log2(static_cast<float>(i));
  121. std::clock_t End = clock();
  122. printf("glm::log2<float>: %d clocks\n", static_cast<int>(End - Begin));
  123. }
  124. {
  125. std::vector<glm::vec4> Result;
  126. Result.resize(Count);
  127. std::clock_t Begin = clock();
  128. for(int i = 0; i < static_cast<int>(Count); ++i)
  129. Result[i] = glm::log2(glm::vec4(static_cast<float>(i)));
  130. std::clock_t End = clock();
  131. printf("glm::log2<vec4>: %d clocks\n", static_cast<int>(End - Begin));
  132. }
  133. return Error;
  134. }
  135. }//namespace log2_
  136. namespace iround
  137. {
  138. int test()
  139. {
  140. int Error = 0;
  141. for(float f = 0.0f; f < 3.1f; f += 0.05f)
  142. {
  143. int RoundFast = static_cast<int>(glm::iround(f));
  144. int RoundSTD = static_cast<int>(glm::round(f));
  145. Error += RoundFast == RoundSTD ? 0 : 1;
  146. assert(!Error);
  147. }
  148. return Error;
  149. }
  150. }//namespace iround
  151. namespace uround
  152. {
  153. int test()
  154. {
  155. int Error = 0;
  156. for(float f = 0.0f; f < 3.1f; f += 0.05f)
  157. {
  158. int RoundFast = static_cast<int>(glm::uround(f));
  159. int RoundSTD = static_cast<int>(glm::round(f));
  160. Error += RoundFast == RoundSTD ? 0 : 1;
  161. assert(!Error);
  162. }
  163. return Error;
  164. }
  165. }//namespace uround
  166. int main()
  167. {
  168. int Error(0);
  169. Error += ::log2_::test();
  170. Error += ::iround::test();
  171. Error += ::uround::test();
  172. # ifdef NDEBUG
  173. std::size_t const Samples(1000);
  174. Error += ::log2_::perf(Samples);
  175. # endif//NDEBUG
  176. return Error;
  177. }