Team Fortress 2 Source Code as on 22/4/2020
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.

112 lines
2.7 KiB

  1. //========= Copyright 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. void CSIMDVectorMatrix::CreateFromRGBA_FloatImageData(int srcwidth, int srcheight,
  15. float const *srcdata )
  16. {
  17. Assert( srcwidth && srcheight && srcdata );
  18. SetSize( srcwidth, srcheight );
  19. FourVectors *p_write_ptr=m_pData;
  20. int n_vectors_per_source_line=(srcwidth >> 2);
  21. int ntrailing_pixels_per_source_line=(srcwidth & 3);
  22. for(int y=0;y<srcheight;y++)
  23. {
  24. float const *data_in=srcdata;
  25. float *data_out=reinterpret_cast<float *>( p_write_ptr );
  26. // copy full input blocks
  27. for(int x=0;x<n_vectors_per_source_line;x++)
  28. {
  29. for(int c=0;c<3;c++)
  30. {
  31. data_out[0]=data_in[c]; // x0
  32. data_out[1]=data_in[4+c]; // x1
  33. data_out[2]=data_in[8+c]; // x2
  34. data_out[3]=data_in[12+c]; // x3
  35. data_out+=4;
  36. }
  37. data_in += 16;
  38. }
  39. // now, copy trailing data and pad with copies
  40. if (ntrailing_pixels_per_source_line )
  41. {
  42. for(int c=0;c<3;c++)
  43. {
  44. for(int cp=0;cp<4; cp++)
  45. {
  46. int real_cp=min( cp, ntrailing_pixels_per_source_line-1 );
  47. data_out[4*c+cp]= data_in[c+4*real_cp];
  48. }
  49. }
  50. }
  51. // advance ptrs to next line
  52. p_write_ptr += m_nPaddedWidth;
  53. srcdata += 4 * srcwidth;
  54. }
  55. }
  56. void CSIMDVectorMatrix::RaiseToPower( float power )
  57. {
  58. int nv=NVectors();
  59. if ( nv )
  60. {
  61. int fixed_point_exp=(int) ( 4.0*power );
  62. FourVectors *src=m_pData;
  63. do
  64. {
  65. src->x=Pow_FixedPoint_Exponent_SIMD( src->x, fixed_point_exp );
  66. src->y=Pow_FixedPoint_Exponent_SIMD( src->y, fixed_point_exp );
  67. src->z=Pow_FixedPoint_Exponent_SIMD( src->z, fixed_point_exp );
  68. src++;
  69. } while (--nv);
  70. }
  71. }
  72. CSIMDVectorMatrix & CSIMDVectorMatrix::operator+=( CSIMDVectorMatrix const &src )
  73. {
  74. Assert( m_nWidth == src.m_nWidth );
  75. Assert( m_nHeight == src.m_nHeight );
  76. int nv=NVectors();
  77. if ( nv )
  78. {
  79. FourVectors *srcv=src.m_pData;
  80. FourVectors *destv=m_pData;
  81. do // !! speed !! inline more iters
  82. {
  83. *( destv++ ) += *( srcv++ );
  84. } while ( --nv );
  85. }
  86. return *this;
  87. }
  88. CSIMDVectorMatrix & CSIMDVectorMatrix::operator*=( Vector const &src )
  89. {
  90. int nv=NVectors();
  91. if ( nv )
  92. {
  93. FourVectors scalevalue;
  94. scalevalue.DuplicateVector( src );
  95. FourVectors *destv=m_pData;
  96. do // !! speed !! inline more iters
  97. {
  98. destv->VProduct( scalevalue );
  99. destv++;
  100. } while ( --nv );
  101. }
  102. return *this;
  103. }