Browse Source

WIP: move vmatrix functions to header and inline them

mathlib-optimize
nillerusr 2 years ago
parent
commit
7ceba77616
  1. 58
      mathlib/mathlib_base.cpp
  2. 1264
      mathlib/vmatrix.cpp
  3. 12
      public/mathlib/math_pfns.h
  4. 31
      public/mathlib/mathlib.h
  5. 2
      public/mathlib/vector4d.h
  6. 1776
      public/mathlib/vmatrix.h

58
mathlib/mathlib_base.cpp

@ -420,13 +420,6 @@ void MatrixGetColumn( const matrix3x4_t& in, int column, Vector &out )
out.z = in[2][column]; out.z = in[2][column];
} }
void MatrixSetColumn( const Vector &in, int column, matrix3x4_t& out )
{
out[0][column] = in.x;
out[1][column] = in.y;
out[2][column] = in.z;
}
void MatrixScaleBy ( const float flScale, matrix3x4_t &out ) void MatrixScaleBy ( const float flScale, matrix3x4_t &out )
{ {
out[0][0] *= flScale; out[0][0] *= flScale;
@ -1092,57 +1085,6 @@ void SetScaleMatrix( float x, float y, float z, matrix3x4_t &dst )
dst[2][0] = 0.0f; dst[2][1] = 0.0f; dst[2][2] = z; dst[2][3] = 0.0f; dst[2][0] = 0.0f; dst[2][1] = 0.0f; dst[2][2] = z; dst[2][3] = 0.0f;
} }
//-----------------------------------------------------------------------------
// Purpose: Builds the matrix for a counterclockwise rotation about an arbitrary axis.
//
// | ax2 + (1 - ax2)cosQ axay(1 - cosQ) - azsinQ azax(1 - cosQ) + aysinQ |
// Ra(Q) = | axay(1 - cosQ) + azsinQ ay2 + (1 - ay2)cosQ ayaz(1 - cosQ) - axsinQ |
// | azax(1 - cosQ) - aysinQ ayaz(1 - cosQ) + axsinQ az2 + (1 - az2)cosQ |
//
// Input : mat -
// vAxisOrRot -
// angle -
//-----------------------------------------------------------------------------
void MatrixBuildRotationAboutAxis( const Vector &vAxisOfRot, float angleDegrees, matrix3x4_t &dst )
{
float radians;
float axisXSquared;
float axisYSquared;
float axisZSquared;
float fSin;
float fCos;
radians = angleDegrees * ( M_PI / 180.0 );
fSin = sin( radians );
fCos = cos( radians );
axisXSquared = vAxisOfRot[0] * vAxisOfRot[0];
axisYSquared = vAxisOfRot[1] * vAxisOfRot[1];
axisZSquared = vAxisOfRot[2] * vAxisOfRot[2];
// Column 0:
dst[0][0] = axisXSquared + (1 - axisXSquared) * fCos;
dst[1][0] = vAxisOfRot[0] * vAxisOfRot[1] * (1 - fCos) + vAxisOfRot[2] * fSin;
dst[2][0] = vAxisOfRot[2] * vAxisOfRot[0] * (1 - fCos) - vAxisOfRot[1] * fSin;
// Column 1:
dst[0][1] = vAxisOfRot[0] * vAxisOfRot[1] * (1 - fCos) - vAxisOfRot[2] * fSin;
dst[1][1] = axisYSquared + (1 - axisYSquared) * fCos;
dst[2][1] = vAxisOfRot[1] * vAxisOfRot[2] * (1 - fCos) + vAxisOfRot[0] * fSin;
// Column 2:
dst[0][2] = vAxisOfRot[2] * vAxisOfRot[0] * (1 - fCos) + vAxisOfRot[1] * fSin;
dst[1][2] = vAxisOfRot[1] * vAxisOfRot[2] * (1 - fCos) - vAxisOfRot[0] * fSin;
dst[2][2] = axisZSquared + (1 - axisZSquared) * fCos;
// Column 3:
dst[0][3] = 0;
dst[1][3] = 0;
dst[2][3] = 0;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Computes the transpose // Computes the transpose
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

1264
mathlib/vmatrix.cpp

File diff suppressed because it is too large Load Diff

12
public/mathlib/math_pfns.h

@ -22,10 +22,16 @@ extern float (*pfFastCos)(float x);
// The following are not declared as macros because they are often used in limiting situations, // The following are not declared as macros because they are often used in limiting situations,
// and sometimes the compiler simply refuses to inline them for some reason // and sometimes the compiler simply refuses to inline them for some reason
#define FastSqrt(x) (*pfSqrt)(x) #define FastSqrt(x) sqrtf(x)
#define FastRSqrt(x) (*pfRSqrt)(x) #define FastRSqrt(x) (1.f/sqrtf(x))
#define FastRSqrtFast(x) (*pfRSqrtFast)(x) #define FastRSqrtFast(x) (1.f/sqrtf(x))
#ifdef _WIN32
#define FastSinCos(x,s,c) (*pfFastSinCos)(x,s,c) #define FastSinCos(x,s,c) (*pfFastSinCos)(x,s,c)
#else
#define FastSinCos(x,s,c) sincosf(x,s,c)
#endif
#define FastCos(x) (*pfFastCos)(x) #define FastCos(x) (*pfFastCos)(x)
#if defined(__i386__) || defined(_M_IX86) #if defined(__i386__) || defined(_M_IX86)

31
public/mathlib/mathlib.h

@ -237,8 +237,8 @@ bool R_CullBoxSkipNear( const Vector& mins, const Vector& maxs, const Frustum_t
struct matrix3x4_t struct matrix3x4_t
{ {
matrix3x4_t() = default; inline matrix3x4_t() = default;
matrix3x4_t( inline matrix3x4_t(
float m00, float m01, float m02, float m03, float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13, float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23 ) float m20, float m21, float m22, float m23 )
@ -252,7 +252,7 @@ struct matrix3x4_t
// Creates a matrix where the X axis = forward // Creates a matrix where the X axis = forward
// the Y axis = left, and the Z axis = up // the Y axis = left, and the Z axis = up
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Init( const Vector& xAxis, const Vector& yAxis, const Vector& zAxis, const Vector &vecOrigin ) inline void Init( const Vector& xAxis, const Vector& yAxis, const Vector& zAxis, const Vector &vecOrigin )
{ {
m_flMatVal[0][0] = xAxis.x; m_flMatVal[0][1] = yAxis.x; m_flMatVal[0][2] = zAxis.x; m_flMatVal[0][3] = vecOrigin.x; m_flMatVal[0][0] = xAxis.x; m_flMatVal[0][1] = yAxis.x; m_flMatVal[0][2] = zAxis.x; m_flMatVal[0][3] = vecOrigin.x;
m_flMatVal[1][0] = xAxis.y; m_flMatVal[1][1] = yAxis.y; m_flMatVal[1][2] = zAxis.y; m_flMatVal[1][3] = vecOrigin.y; m_flMatVal[1][0] = xAxis.y; m_flMatVal[1][1] = yAxis.y; m_flMatVal[1][2] = zAxis.y; m_flMatVal[1][3] = vecOrigin.y;
@ -263,26 +263,23 @@ struct matrix3x4_t
// Creates a matrix where the X axis = forward // Creates a matrix where the X axis = forward
// the Y axis = left, and the Z axis = up // the Y axis = left, and the Z axis = up
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
matrix3x4_t( const Vector& xAxis, const Vector& yAxis, const Vector& zAxis, const Vector &vecOrigin ) inline matrix3x4_t( const Vector& xAxis, const Vector& yAxis, const Vector& zAxis, const Vector &vecOrigin )
{ {
Init( xAxis, yAxis, zAxis, vecOrigin ); Init( xAxis, yAxis, zAxis, vecOrigin );
} }
inline void Invalidate( void ) inline void Invalidate( void )
{ {
for (int i = 0; i < 3; i++) for( int i=0; i < 12; i++ )
{ {
for (int j = 0; j < 4; j++) ((float*)m_flMatVal)[i] = VEC_T_NAN;
{
m_flMatVal[i][j] = VEC_T_NAN;
}
} }
} }
float *operator[]( int i ) { Assert(( i >= 0 ) && ( i < 3 )); return m_flMatVal[i]; } inline float *operator[]( int i ) { Assert(( i >= 0 ) && ( i < 3 )); return m_flMatVal[i]; }
const float *operator[]( int i ) const { Assert(( i >= 0 ) && ( i < 3 )); return m_flMatVal[i]; } inline const float *operator[]( int i ) const { Assert(( i >= 0 ) && ( i < 3 )); return m_flMatVal[i]; }
float *Base() { return &m_flMatVal[0][0]; } inline float *Base() { return &m_flMatVal[0][0]; }
const float *Base() const { return &m_flMatVal[0][0]; } inline const float *Base() const { return &m_flMatVal[0][0]; }
float m_flMatVal[3][4]; float m_flMatVal[3][4];
}; };
@ -565,7 +562,13 @@ void MatrixInvert( const matrix3x4_t &in, matrix3x4_t &out );
bool MatricesAreEqual( const matrix3x4_t &src1, const matrix3x4_t &src2, float flTolerance = 1e-5 ); bool MatricesAreEqual( const matrix3x4_t &src1, const matrix3x4_t &src2, float flTolerance = 1e-5 );
void MatrixGetColumn( const matrix3x4_t &in, int column, Vector &out ); void MatrixGetColumn( const matrix3x4_t &in, int column, Vector &out );
void MatrixSetColumn( const Vector &in, int column, matrix3x4_t &out );
inline void MatrixSetColumn( const Vector &in, int column, matrix3x4_t& out )
{
out[0][column] = in.x;
out[1][column] = in.y;
out[2][column] = in.z;
}
inline void MatrixGetTranslation( const matrix3x4_t &in, Vector &out ) inline void MatrixGetTranslation( const matrix3x4_t &in, Vector &out )
{ {

2
public/mathlib/vector4d.h

@ -35,7 +35,7 @@ class Vector2D;
// 4D Vector4D // 4D Vector4D
//========================================================= //=========================================================
class Vector4D class alignas(16) Vector4D
{ {
public: public:
// Members // Members

1776
public/mathlib/vmatrix.h

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save