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.

140 lines
3.8 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. #include "basetypes.h"
  10. #include "mathlib/mathlib.h"
  11. #include "mathlib/simdvectormatrix.h"
  12. #include "mathlib/ssemath.h"
  13. #include "tier0/dbg.h"
  14. // NOTE: This has to be the last file included!
  15. #include "tier0/memdbgon.h"
  16. void CSIMDVectorMatrix::CreateFromCSOAAttributes( CSOAContainer const *pSrc,
  17. int nAttrIdx0, int nAttrIdx1, int nAttrIdx2 )
  18. {
  19. SetSize( pSrc->NumCols(), pSrc->NumRows() );
  20. FourVectors *p_write_ptr = m_pData;
  21. int n_vectors_per_source_line = pSrc->NumQuadsPerRow();
  22. for( int y = 0; y < pSrc->NumRows(); y++ )
  23. {
  24. fltx4 const * data_in0 = reinterpret_cast<fltx4 const *>( pSrc->ConstRowPtr( nAttrIdx0, y ) );
  25. fltx4 const * data_in1 = reinterpret_cast<fltx4 const *>( pSrc->ConstRowPtr( nAttrIdx1, y ) );
  26. fltx4 const * data_in2 = reinterpret_cast<fltx4 const *>( pSrc->ConstRowPtr( nAttrIdx2, y ) );
  27. fltx4 *data_out = reinterpret_cast < fltx4 *> ( p_write_ptr );
  28. // copy full input blocks
  29. for( int x = 0; x < n_vectors_per_source_line; x++ )
  30. {
  31. *(data_out++) = (* data_in0++ );
  32. *(data_out++) = (* data_in1++ );
  33. *(data_out++) = (* data_in2++ );
  34. }
  35. // advance ptrs to next line
  36. p_write_ptr += m_nPaddedWidth;
  37. }
  38. }
  39. void CSIMDVectorMatrix::CreateFromRGBA_FloatImageData( int srcwidth, int srcheight,
  40. float const * srcdata )
  41. {
  42. Assert( srcwidth && srcheight && srcdata );
  43. SetSize( srcwidth, srcheight );
  44. FourVectors * p_write_ptr = m_pData;
  45. int n_vectors_per_source_line = ( srcwidth >> 2 );
  46. int ntrailing_pixels_per_source_line = ( srcwidth & 3 );
  47. for( int y = 0; y < srcheight; y++ )
  48. {
  49. float const * data_in = srcdata;
  50. float * data_out = reinterpret_cast < float *> ( p_write_ptr );
  51. // copy full input blocks
  52. for( int x = 0; x < n_vectors_per_source_line; x++ )
  53. {
  54. for( int c = 0; c < 3; c++ )
  55. {
  56. data_out[0]= data_in[c]; // x0
  57. data_out[1]= data_in[4 + c]; // x1
  58. data_out[2]= data_in[8 + c]; // x2
  59. data_out[3]= data_in[12 + c]; // x3
  60. data_out += 4;
  61. }
  62. data_in += 16;
  63. }
  64. // now, copy trailing data and pad with copies
  65. if ( ntrailing_pixels_per_source_line )
  66. {
  67. for( int c = 0; c < 3; c++ )
  68. {
  69. for( int cp = 0; cp < 4; cp++ )
  70. {
  71. int real_cp = MIN( cp, ntrailing_pixels_per_source_line - 1 );
  72. data_out[4 * c + cp]= data_in[c + 4 * real_cp];
  73. }
  74. }
  75. }
  76. // advance ptrs to next line
  77. p_write_ptr += m_nPaddedWidth;
  78. srcdata += 4 * srcwidth;
  79. }
  80. }
  81. void CSIMDVectorMatrix::RaiseToPower( float power )
  82. {
  83. int nv = NVectors();
  84. if ( nv )
  85. {
  86. int fixed_point_exp = ( int ) ( 4.0 * power );
  87. FourVectors * src = m_pData;
  88. do
  89. {
  90. src->x = Pow_FixedPoint_Exponent_SIMD( src->x, fixed_point_exp );
  91. src->y = Pow_FixedPoint_Exponent_SIMD( src->y, fixed_point_exp );
  92. src->z = Pow_FixedPoint_Exponent_SIMD( src->z, fixed_point_exp );
  93. src++;
  94. } while (-- nv );
  95. }
  96. }
  97. CSIMDVectorMatrix & CSIMDVectorMatrix::operator += ( CSIMDVectorMatrix const & src )
  98. {
  99. Assert( m_nWidth == src.m_nWidth );
  100. Assert( m_nHeight == src.m_nHeight );
  101. int nv = NVectors();
  102. if ( nv )
  103. {
  104. FourVectors * srcv = src.m_pData;
  105. FourVectors * destv = m_pData;
  106. do // !! speed !! inline more iters
  107. {
  108. * ( destv++ ) += * ( srcv++ );
  109. } while (-- nv );
  110. }
  111. return * this;
  112. }
  113. CSIMDVectorMatrix & CSIMDVectorMatrix::operator *= ( Vector const & src )
  114. {
  115. int nv = NVectors();
  116. if ( nv )
  117. {
  118. FourVectors scalevalue;
  119. scalevalue.DuplicateVector( src );
  120. FourVectors * destv = m_pData;
  121. do // !! speed !! inline more iters
  122. {
  123. destv->VProduct( scalevalue );
  124. destv++;
  125. } while (-- nv );
  126. }
  127. return * this;
  128. }