tsMeshIntrinsics.sse4.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "ts/tsMesh.h"
  23. #if defined(TORQUE_CPU_X86) && (_MSC_VER >= 1500)
  24. #include "ts/tsMeshIntrinsics.h"
  25. #include <smmintrin.h>
  26. void m_matF_x_BatchedVertWeightList_SSE4(const MatrixF &mat,
  27. const dsize_t count,
  28. const TSSkinMesh::BatchData::BatchedVertWeight * __restrict batch,
  29. U8 * const __restrict outPtr,
  30. const dsize_t outStride)
  31. {
  32. const char * __restrict iPtr = reinterpret_cast<const char *>(batch);
  33. const dsize_t inStride = sizeof(TSSkinMesh::BatchData::BatchedVertWeight);
  34. __m128 sseMat[3];
  35. sseMat[0] = _mm_loadu_ps(&mat[0]);
  36. sseMat[1] = _mm_loadu_ps(&mat[4]);
  37. sseMat[2] = _mm_loadu_ps(&mat[8]);
  38. // temp registers
  39. __m128 inPos, tempPos;
  40. __m128 inNrm, tempNrm;
  41. __m128 temp0, temp1, temp2, temp3;
  42. // pre-populate cache
  43. const TSSkinMesh::BatchData::BatchedVertWeight &firstElem = batch[0];
  44. for(S32 i = 0; i < 8; i++)
  45. {
  46. _mm_prefetch(reinterpret_cast<const char *>(iPtr + inStride * i), _MM_HINT_T0);
  47. _mm_prefetch(reinterpret_cast<const char *>(outPtr + outStride * (i + firstElem.vidx)), _MM_HINT_T0);
  48. }
  49. for(S32 i = 0; i < count; i++)
  50. {
  51. const TSSkinMesh::BatchData::BatchedVertWeight &inElem = batch[i];
  52. TSMesh::__TSMeshVertexBase *outElem = reinterpret_cast<TSMesh::__TSMeshVertexBase *>(outPtr + inElem.vidx * outStride);
  53. // process x (hiding the prefetches in the delays)
  54. inPos = _mm_load_ps(inElem.vert);
  55. inNrm = _mm_load_ps(inElem.normal);
  56. // prefetch input
  57. #define INPUT_PREFETCH_LOOKAHEAD 64
  58. const char *prefetchInput = reinterpret_cast<const char *>(batch) + inStride * (i + INPUT_PREFETCH_LOOKAHEAD);
  59. _mm_prefetch(prefetchInput, _MM_HINT_T0);
  60. // prefetch ouput with half the lookahead distance of the input
  61. #define OUTPUT_PREFETCH_LOOKAHEAD (INPUT_PREFETCH_LOOKAHEAD >> 1)
  62. const char *outPrefetch = reinterpret_cast<const char*>(outPtr) + outStride * (inElem.vidx + OUTPUT_PREFETCH_LOOKAHEAD);
  63. _mm_prefetch(outPrefetch, _MM_HINT_T0);
  64. // Multiply position
  65. tempPos = _mm_dp_ps(inPos, sseMat[0], 0xF1);
  66. temp0 = _mm_dp_ps(inPos, sseMat[1], 0xF2);
  67. temp1 = _mm_dp_ps(inPos, sseMat[2], 0xF4);
  68. temp0 = _mm_or_ps(temp0, temp1);
  69. tempPos = _mm_or_ps(tempPos, temp0);
  70. // Multiply normal
  71. tempNrm = _mm_dp_ps(inNrm, sseMat[0], 0x71);
  72. temp2 = _mm_dp_ps(inNrm, sseMat[1], 0x72);
  73. temp3 = _mm_dp_ps(inNrm, sseMat[2], 0x74);
  74. temp2 = _mm_or_ps(temp2, temp3);
  75. tempNrm = _mm_or_ps(tempNrm, temp2);
  76. // Load bone weight and multiply
  77. temp3 = _mm_shuffle_ps(inPos, inPos, _MM_SHUFFLE(3, 3, 3, 3));
  78. tempPos = _mm_mul_ps(tempPos, temp3);
  79. tempNrm = _mm_mul_ps(tempNrm, temp3);
  80. inPos = _mm_load_ps(outElem->_vert); //< load position for accumulation
  81. inNrm = _mm_load_ps(outElem->_normal); //< load normal for accumulation
  82. // accumulate with previous values
  83. tempNrm = _mm_add_ps(tempNrm, inNrm);
  84. tempPos = _mm_add_ps(tempPos, inPos);
  85. _mm_store_ps(outElem->_vert, tempPos); //< output position
  86. _mm_store_ps(outElem->_normal, tempNrm); //< output normal
  87. }
  88. }
  89. #endif // TORQUE_CPU_X86