llmatrix4a.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @file llmatrix4a.cpp
  3. * @brief Methods for vectorized matrix/vector operations
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (C) 2018, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llmath.h"
  33. #include "llmatrix4a.h"
  34. #include "llvector4logical.h"
  35. bool LLMatrix4a::isIdentity() const
  36. {
  37. thread_local LLMatrix4a mins;
  38. thread_local LLMatrix4a maxs;
  39. thread_local bool needs_init = true;
  40. if (needs_init)
  41. {
  42. const LLVector4a delta(0.0001f);
  43. mins.setIdentity();
  44. mins.getRow<0>().sub(delta);
  45. mins.getRow<1>().sub(delta);
  46. mins.getRow<2>().sub(delta);
  47. mins.getRow<3>().sub(delta);
  48. maxs.setIdentity();
  49. maxs.getRow<0>().sub(delta);
  50. maxs.getRow<1>().sub(delta);
  51. maxs.getRow<2>().sub(delta);
  52. maxs.getRow<3>().sub(delta);
  53. needs_init = false;
  54. }
  55. LLVector4a mask1 = _mm_and_ps(_mm_cmpgt_ps(mMatrix[0],
  56. mins.getRow<0>()),
  57. _mm_cmplt_ps(mMatrix[0],
  58. maxs.getRow<0>()));
  59. LLVector4a mask2 = _mm_and_ps(_mm_cmpgt_ps(mMatrix[1],
  60. mins.getRow<1>()),
  61. _mm_cmplt_ps(mMatrix[1],
  62. maxs.getRow<1>()));
  63. LLVector4a mask3 = _mm_and_ps(_mm_cmpgt_ps(mMatrix[2],
  64. mins.getRow<2>()),
  65. _mm_cmplt_ps(mMatrix[2],
  66. maxs.getRow<2>()));
  67. LLVector4a mask4 = _mm_and_ps(_mm_cmpgt_ps(mMatrix[3],
  68. mins.getRow<3>()),
  69. _mm_cmplt_ps(mMatrix[3],
  70. maxs.getRow<3>()));
  71. mask1 = _mm_and_ps(mask1, mask2);
  72. mask2 = _mm_and_ps(mask3, mask4);
  73. return _mm_movemask_epi8(_mm_castps_si128(_mm_and_ps(mask1,
  74. mask2))) == 0xFFFF;
  75. }
  76. // Convert a bounding box into other coordinate system. Should give the same
  77. // results as transforming every corner of the bounding box and extracting the
  78. // bounding box of that, although that is not necessarily the fastest way to
  79. // implement.
  80. void LLMatrix4a::matMulBoundBox(const LLVector4a* in_extents,
  81. LLVector4a* out_extents) const
  82. {
  83. thread_local LLVector4Logical mask[6];
  84. thread_local bool needs_init = true;
  85. if (needs_init)
  86. {
  87. needs_init = false;
  88. for (U32 i = 0; i < 6; ++i)
  89. {
  90. mask[i].clear();
  91. }
  92. mask[0].setElement<2>(); // 001
  93. mask[1].setElement<1>(); // 010
  94. mask[2].setElement<1>(); // 011
  95. mask[2].setElement<2>();
  96. mask[3].setElement<0>(); // 100
  97. mask[4].setElement<0>(); // 101
  98. mask[4].setElement<2>();
  99. mask[5].setElement<0>(); // 110
  100. mask[5].setElement<1>();
  101. }
  102. // Get 8 corners of bounding box
  103. LLVector4a v[8];
  104. v[6] = in_extents[0];
  105. v[7] = in_extents[1];
  106. for (U32 i = 0; i < 6; ++i)
  107. {
  108. v[i].setSelectWithMask(mask[i], in_extents[0], in_extents[1]);
  109. }
  110. LLVector4a tv[8];
  111. // Transform bounding box into drawable space
  112. for (U32 i = 0; i < 8; ++i)
  113. {
  114. affineTransform(v[i], tv[i]);
  115. }
  116. // Find bounding box
  117. out_extents[0] = out_extents[1] = tv[0];
  118. for (U32 i = 1; i < 8; ++i)
  119. {
  120. out_extents[0].setMin(out_extents[0], tv[i]);
  121. out_extents[1].setMax(out_extents[1], tv[i]);
  122. }
  123. }