Counter Strike : Global Offensive Source Code
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.

142 lines
3.0 KiB

  1. //====== Copyright � 1996-2006, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose: Provide a class (SSE/SIMD only) holding a 2d matrix of class FourVectors,
  4. // for high speed processing in tools.
  5. //
  6. // $NoKeywords: $
  7. //
  8. //=============================================================================//
  9. #ifndef SIMDVECTORMATRIX_H
  10. #define SIMDVECTORMATRIX_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include <string.h>
  15. #include "tier0/platform.h"
  16. #include "tier0/dbg.h"
  17. #include "tier1/utlsoacontainer.h"
  18. #include "mathlib/ssemath.h"
  19. class CSIMDVectorMatrix
  20. {
  21. public:
  22. int m_nWidth; // in actual vectors
  23. int m_nHeight;
  24. int m_nPaddedWidth; // # of 4x wide elements
  25. FourVectors *m_pData;
  26. protected:
  27. void Init( void )
  28. {
  29. m_pData = NULL;
  30. m_nWidth = 0;
  31. m_nHeight = 0;
  32. m_nPaddedWidth = 0;
  33. }
  34. int NVectors( void ) const
  35. {
  36. return m_nHeight * m_nPaddedWidth;
  37. }
  38. public:
  39. // constructors and destructors
  40. CSIMDVectorMatrix( void )
  41. {
  42. Init();
  43. }
  44. ~CSIMDVectorMatrix( void )
  45. {
  46. if ( m_pData )
  47. delete[] m_pData;
  48. }
  49. // set up storage and fields for m x n matrix. destroys old data
  50. void SetSize( int width, int height )
  51. {
  52. if ( ( ! m_pData ) || ( width != m_nWidth ) || ( height != m_nHeight ) )
  53. {
  54. if ( m_pData )
  55. delete[] m_pData;
  56. m_nWidth = width;
  57. m_nHeight = height;
  58. m_nPaddedWidth = ( m_nWidth + 3) >> 2;
  59. m_pData = NULL;
  60. if ( width && height )
  61. m_pData = new FourVectors[ m_nPaddedWidth * m_nHeight ];
  62. }
  63. }
  64. CSIMDVectorMatrix( int width, int height )
  65. {
  66. Init();
  67. SetSize( width, height );
  68. }
  69. CSIMDVectorMatrix &operator=( CSIMDVectorMatrix const &src )
  70. {
  71. SetSize( src.m_nWidth, src.m_nHeight );
  72. if ( m_pData )
  73. memcpy( m_pData, src.m_pData, m_nHeight*m_nPaddedWidth*sizeof(m_pData[0]) );
  74. return *this;
  75. }
  76. CSIMDVectorMatrix &operator+=( CSIMDVectorMatrix const &src );
  77. CSIMDVectorMatrix &operator*=( Vector const &src );
  78. // create from an RGBA float bitmap. alpha ignored.
  79. void CreateFromRGBA_FloatImageData(int srcwidth, int srcheight, float const *srcdata );
  80. // create from 3 fields in a csoa
  81. void CreateFromCSOAAttributes( CSOAContainer const *pSrc,
  82. int nAttrIdx0, int nAttrIdx1, int nAttrIdx2 );
  83. // Element access. If you are calling this a lot, you don't want to use this class, because
  84. // you're not getting the sse advantage
  85. Vector Element(int x, int y) const
  86. {
  87. Assert( m_pData );
  88. Assert( x < m_nWidth );
  89. Assert( y < m_nHeight );
  90. Vector ret;
  91. FourVectors const *pData=m_pData+y*m_nPaddedWidth+(x >> 2);
  92. int xo=(x & 3);
  93. ret.x=pData->X( xo );
  94. ret.y=pData->Y( xo );
  95. ret.z=pData->Z( xo );
  96. return ret;
  97. }
  98. //addressing the individual fourvectors elements
  99. FourVectors &CompoundElement(int x, int y)
  100. {
  101. Assert( m_pData );
  102. Assert( y < m_nHeight );
  103. Assert( x < m_nPaddedWidth );
  104. return m_pData[x + m_nPaddedWidth*y ];
  105. }
  106. // math operations on the whole image
  107. void Clear( void )
  108. {
  109. Assert( m_pData );
  110. memset( m_pData, 0, m_nHeight*m_nPaddedWidth*sizeof(m_pData[0]) );
  111. }
  112. void RaiseToPower( float power );
  113. };
  114. #endif