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.

154 lines
4.4 KiB

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