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.8 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, typename vecType>
  17. static void test_vec_mul_mat(matType const& M, std::vector<vecType> const& I, std::vector<vecType>& O)
  18. {
  19. for (std::size_t i = 0, n = I.size(); i < n; ++i)
  20. O[i] = I[i] * M;
  21. }
  22. template <typename matType, typename vecType>
  23. static int launch_vec_mul_mat(std::vector<vecType>& O, matType const& Transform, vecType const& Scale, std::size_t Samples)
  24. {
  25. typedef typename matType::value_type T;
  26. std::vector<vecType> 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_vec_mul_mat<matType, vecType>(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 packedVecType, typename alignedMatType, typename alignedVecType>
  36. static int comp_vec2_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. packedVecType const Scale(0.01, 0.02);
  42. std::vector<packedVecType> SISD;
  43. printf("- SISD: %d us\n", launch_vec_mul_mat<packedMatType, packedVecType>(SISD, Transform, Scale, Samples));
  44. std::vector<alignedVecType> SIMD;
  45. printf("- SIMD: %d us\n", launch_vec_mul_mat<alignedMatType, alignedVecType>(SIMD, Transform, Scale, Samples));
  46. for(std::size_t i = 0; i < Samples; ++i)
  47. {
  48. packedVecType const A = SISD[i];
  49. packedVecType const B = packedVecType(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 packedVecType, typename alignedMatType, typename alignedVecType>
  55. static int comp_vec3_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. packedVecType const Scale(0.01, 0.02, 0.05);
  61. std::vector<packedVecType> SISD;
  62. printf("- SISD: %d us\n", launch_vec_mul_mat<packedMatType, packedVecType>(SISD, Transform, Scale, Samples));
  63. std::vector<alignedVecType> SIMD;
  64. printf("- SIMD: %d us\n", launch_vec_mul_mat<alignedMatType, alignedVecType>(SIMD, Transform, Scale, Samples));
  65. for(std::size_t i = 0; i < Samples; ++i)
  66. {
  67. packedVecType const A = SISD[i];
  68. packedVecType 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 packedVecType, typename alignedMatType, typename alignedVecType>
  74. static int comp_vec4_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. packedVecType const Scale(0.01, 0.02, 0.03, 0.05);
  80. std::vector<packedVecType> SISD;
  81. printf("- SISD: %d us\n", launch_vec_mul_mat<packedMatType, packedVecType>(SISD, Transform, Scale, Samples));
  82. std::vector<alignedVecType> SIMD;
  83. printf("- SIMD: %d us\n", launch_vec_mul_mat<alignedMatType, alignedVecType>(SIMD, Transform, Scale, Samples));
  84. for(std::size_t i = 0; i < Samples; ++i)
  85. {
  86. packedVecType const A = SISD[i];
  87. packedVecType 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("vec2 * mat2:\n");
  97. Error += comp_vec2_mul_mat2<glm::mat2, glm::vec2, glm::aligned_mat2, glm::aligned_vec2>(Samples);
  98. printf("dvec2 * dmat2:\n");
  99. Error += comp_vec2_mul_mat2<glm::dmat2, glm::dvec2,glm::aligned_dmat2, glm::aligned_dvec2>(Samples);
  100. printf("vec3 * mat3:\n");
  101. Error += comp_vec3_mul_mat3<glm::mat3, glm::vec3, glm::aligned_mat3, glm::aligned_vec3>(Samples);
  102. printf("dvec3 * dmat3:\n");
  103. Error += comp_vec3_mul_mat3<glm::dmat3, glm::dvec3, glm::aligned_dmat3, glm::aligned_dvec3>(Samples);
  104. printf("vec4 * mat4:\n");
  105. Error += comp_vec4_mul_mat4<glm::mat4, glm::vec4, glm::aligned_mat4, glm::aligned_vec4>(Samples);
  106. printf("dvec4 * dmat4:\n");
  107. Error += comp_vec4_mul_mat4<glm::dmat4, glm::dvec4, glm::aligned_dmat4, glm::aligned_dvec4>(Samples);
  108. return Error;
  109. }
  110. #else
  111. int main()
  112. {
  113. return 0;
  114. }
  115. #endif