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.

153 lines
4.3 KiB

5 years ago
  1. #define GLM_FORCE_INLINE
  2. #include <glm/ext/matrix_float4x4.hpp>
  3. #include <glm/ext/matrix_double4x4.hpp>
  4. #include <glm/ext/matrix_transform.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_div_mat(matType const& M, 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] = M / I[i];
  17. }
  18. template <typename matType>
  19. static int launch_mat_div_mat(std::vector<matType>& O, matType const& Transform, 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_div_mat<matType>(Transform, 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_div_mat2(std::size_t Samples)
  33. {
  34. typedef typename packedMatType::value_type T;
  35. int Error = 0;
  36. packedMatType const Transform(1, 2, 3, 4);
  37. packedMatType const Scale(0.01, 0.02, 0.03, 0.05);
  38. std::vector<packedMatType> SISD;
  39. printf("- SISD: %d us\n", launch_mat_div_mat<packedMatType>(SISD, Transform, Scale, Samples));
  40. std::vector<alignedMatType> SIMD;
  41. printf("- SIMD: %d us\n", launch_mat_div_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  42. for(std::size_t i = 0; i < Samples; ++i)
  43. {
  44. packedMatType const A = SISD[i];
  45. packedMatType const B = SIMD[i];
  46. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  47. assert(!Error);
  48. }
  49. return Error;
  50. }
  51. template <typename packedMatType, typename alignedMatType>
  52. static int comp_mat3_div_mat3(std::size_t Samples)
  53. {
  54. typedef typename packedMatType::value_type T;
  55. int Error = 0;
  56. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9);
  57. packedMatType const Scale(0.01, 0.02, 0.03, 0.05, 0.01, 0.02, 0.03, 0.05, 0.01);
  58. std::vector<packedMatType> SISD;
  59. printf("- SISD: %d us\n", launch_mat_div_mat<packedMatType>(SISD, Transform, Scale, Samples));
  60. std::vector<alignedMatType> SIMD;
  61. printf("- SIMD: %d us\n", launch_mat_div_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  62. for(std::size_t i = 0; i < Samples; ++i)
  63. {
  64. packedMatType const A = SISD[i];
  65. packedMatType const B = SIMD[i];
  66. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  67. assert(!Error);
  68. }
  69. return Error;
  70. }
  71. template <typename packedMatType, typename alignedMatType>
  72. static int comp_mat4_div_mat4(std::size_t Samples)
  73. {
  74. typedef typename packedMatType::value_type T;
  75. int Error = 0;
  76. packedMatType const Transform(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
  77. 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);
  78. std::vector<packedMatType> SISD;
  79. printf("- SISD: %d us\n", launch_mat_div_mat<packedMatType>(SISD, Transform, Scale, Samples));
  80. std::vector<alignedMatType> SIMD;
  81. printf("- SIMD: %d us\n", launch_mat_div_mat<alignedMatType>(SIMD, Transform, Scale, Samples));
  82. for(std::size_t i = 0; i < Samples; ++i)
  83. {
  84. packedMatType const A = SISD[i];
  85. packedMatType const B = SIMD[i];
  86. Error += glm::all(glm::equal(A, B, static_cast<T>(0.001))) ? 0 : 1;
  87. assert(!Error);
  88. }
  89. return Error;
  90. }
  91. int main()
  92. {
  93. std::size_t const Samples = 100000;
  94. int Error = 0;
  95. printf("mat2 / mat2:\n");
  96. Error += comp_mat2_div_mat2<glm::mat2, glm::aligned_mat2>(Samples);
  97. printf("dmat2 / dmat2:\n");
  98. Error += comp_mat2_div_mat2<glm::dmat2, glm::aligned_dmat2>(Samples);
  99. printf("mat3 / mat3:\n");
  100. Error += comp_mat3_div_mat3<glm::mat3, glm::aligned_mat3>(Samples);
  101. printf("dmat3 / dmat3:\n");
  102. Error += comp_mat3_div_mat3<glm::dmat3, glm::aligned_dmat3>(Samples);
  103. printf("mat4 / mat4:\n");
  104. Error += comp_mat4_div_mat4<glm::mat4, glm::aligned_mat4>(Samples);
  105. printf("dmat4 / dmat4:\n");
  106. Error += comp_mat4_div_mat4<glm::dmat4, glm::aligned_dmat4>(Samples);
  107. return Error;
  108. }
  109. #else
  110. int main()
  111. {
  112. return 0;
  113. }
  114. #endif