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.

150 lines
4.0 KiB

5 years ago
  1. #define GLM_FORCE_INLINE
  2. #include <glm/matrix.hpp>
  3. #include <glm/ext/matrix_float4x4.hpp>
  4. #include <glm/ext/matrix_double4x4.hpp>
  5. #include <glm/ext/matrix_relational.hpp>
  6. #include <glm/ext/vector_float4.hpp>
  7. #if GLM_CONFIG_SIMD == GLM_ENABLE
  8. #include <glm/gtc/type_aligned.hpp>
  9. #include <vector>
  10. #include <chrono>
  11. #include <cstdio>
  12. template <typename matType>
  13. static void test_mat_inverse(std::vector<matType> const& I, std::vector<matType>& O)
  14. {
  15. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  16. O[i] = glm::inverse(I[i]);
  17. }
  18. template <typename matType>
  19. static int launch_mat_inverse(std::vector<matType>& O, matType const& Scale, std::size_t Samples)
  20. {
  21. typedef typename matType::value_type T;
  22. std::vector<matType> I(Samples);
  23. O.resize(Samples);
  24. for(std::size_t i = 0; i < Samples; ++i)
  25. I[i] = Scale * static_cast<T>(i) + Scale;
  26. std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  27. test_mat_inverse<matType>(I, O);
  28. std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  29. return static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  30. }
  31. template <typename packedMatType, typename alignedMatType>
  32. static int comp_mat2_inverse(std::size_t Samples)
  33. {
  34. typedef typename packedMatType::value_type T;
  35. int Error = 0;
  36. packedMatType const Scale(0.01, 0.02, 0.03, 0.05);
  37. std::vector<packedMatType> SISD;
  38. printf("- SISD: %d us\n", launch_mat_inverse<packedMatType>(SISD, Scale, Samples));
  39. std::vector<alignedMatType> SIMD;
  40. printf("- SIMD: %d us\n", launch_mat_inverse<alignedMatType>(SIMD, Scale, Samples));
  41. for(std::size_t i = 0; i < Samples; ++i)
  42. {
  43. packedMatType const A = SISD[i];
  44. packedMatType const B = SIMD[i];
  45. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  46. assert(!Error);
  47. }
  48. return Error;
  49. }
  50. template <typename packedMatType, typename alignedMatType>
  51. static int comp_mat3_inverse(std::size_t Samples)
  52. {
  53. typedef typename packedMatType::value_type T;
  54. int Error = 0;
  55. packedMatType const Scale(0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05, 0.01);
  56. std::vector<packedMatType> SISD;
  57. printf("- SISD: %d us\n", launch_mat_inverse<packedMatType>(SISD, Scale, Samples));
  58. std::vector<alignedMatType> SIMD;
  59. printf("- SIMD: %d us\n", launch_mat_inverse<alignedMatType>(SIMD, Scale, Samples));
  60. for(std::size_t i = 0; i < Samples; ++i)
  61. {
  62. packedMatType const A = SISD[i];
  63. packedMatType const B = SIMD[i];
  64. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  65. assert(!Error);
  66. }
  67. return Error;
  68. }
  69. template <typename packedMatType, typename alignedMatType>
  70. static int comp_mat4_inverse(std::size_t Samples)
  71. {
  72. typedef typename packedMatType::value_type T;
  73. int Error = 0;
  74. packedMatType const Scale(0.01, 0.02, 0.05, 0.04, 0.02, 0.08, 0.05, 0.01, 0.08, 0.03, 0.05, 0.06, 0.02, 0.03, 0.07, 0.05);
  75. std::vector<packedMatType> SISD;
  76. printf("- SISD: %d us\n", launch_mat_inverse<packedMatType>(SISD, Scale, Samples));
  77. std::vector<alignedMatType> SIMD;
  78. printf("- SIMD: %d us\n", launch_mat_inverse<alignedMatType>(SIMD, Scale, Samples));
  79. for(std::size_t i = 0; i < Samples; ++i)
  80. {
  81. packedMatType const A = SISD[i];
  82. packedMatType const B = SIMD[i];
  83. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  84. assert(!Error);
  85. }
  86. return Error;
  87. }
  88. int main()
  89. {
  90. std::size_t const Samples = 100000;
  91. int Error = 0;
  92. printf("glm::inverse(mat2):\n");
  93. Error += comp_mat2_inverse<glm::mat2, glm::aligned_mat2>(Samples);
  94. printf("glm::inverse(dmat2):\n");
  95. Error += comp_mat2_inverse<glm::dmat2, glm::aligned_dmat2>(Samples);
  96. printf("glm::inverse(mat3):\n");
  97. Error += comp_mat3_inverse<glm::mat3, glm::aligned_mat3>(Samples);
  98. printf("glm::inverse(dmat3):\n");
  99. Error += comp_mat3_inverse<glm::dmat3, glm::aligned_dmat3>(Samples);
  100. printf("glm::inverse(mat4):\n");
  101. Error += comp_mat4_inverse<glm::mat4, glm::aligned_mat4>(Samples);
  102. printf("glm::inverse(dmat4):\n");
  103. Error += comp_mat4_inverse<glm::dmat4, glm::aligned_dmat4>(Samples);
  104. return Error;
  105. }
  106. #else
  107. int main()
  108. {
  109. return 0;
  110. }
  111. #endif