Skip to content

Commit

Permalink
Math optimizations (axmolengine#2115)
Browse files Browse the repository at this point in the history
* Mat4: make some methods inlineable

* Vec3: make some methods inlineable

* Vec4: make some methods inlineable

* Color: make some methods inlineable

* Quaternion: make some methods inlineable

* Rect: make some methods inlineable

* Vec2: make some methods inlineable

* Math: mark constants inline

* Math: fix DLL build

* Vec2: make constants inlineable

* Color: remove inline file

* Mat4: remove inline file

* Quaternion: remove inline file

* Rect: remove inline file

* Vec2: remove inline file

* Vec3: remove inline file

* Vec4: remove inline file
  • Loading branch information
smilediver authored and xfbird committed Sep 18, 2024
1 parent 11fe50d commit dc26757
Show file tree
Hide file tree
Showing 20 changed files with 668 additions and 1,127 deletions.
28 changes: 0 additions & 28 deletions core/math/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ NS_AX_MATH_BEGIN
* Color3B
*/

Color3B::Color3B() {}

Color3B::Color3B(uint8_t _r, uint8_t _g, uint8_t _b) : r(_r), g(_g), b(_b) {}

Color3B::Color3B(const Color4B& color) : r(color.r), g(color.g), b(color.b) {}

Color3B::Color3B(const Color4F& color) : r(color.r * 255.0f), g(color.g * 255.0f), b(color.b * 255.0f) {}

bool Color3B::operator==(const Color3B& right) const
{
return (r == right.r && g == right.g && b == right.b);
Expand Down Expand Up @@ -78,14 +70,6 @@ bool Color3B::operator!=(const Color4F& right) const
* Color4B
*/

Color4B::Color4B() {}

Color4B::Color4B(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a) : r(_r), g(_g), b(_b), a(_a) {}

Color4B::Color4B(const Color3B& color, uint8_t _a) : r(color.r), g(color.g), b(color.b), a(_a) {}

Color4B::Color4B(const Color4F& color) : r(color.r * 255), g(color.g * 255), b(color.b * 255), a(color.a * 255) {}

bool Color4B::operator==(const Color4B& right) const
{
return (r == right.r && g == right.g && b == right.b && a == right.a);
Expand Down Expand Up @@ -120,18 +104,6 @@ bool Color4B::operator!=(const Color4F& right) const
* Color4F
*/

Color4F::Color4F() {}

Color4F::Color4F(float _r, float _g, float _b, float _a) : Vec4Base(_r, _g, _b, _a) {}

Color4F::Color4F(const Color3B& color, float _a)
: Vec4Base(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, _a)
{}

Color4F::Color4F(const Color4B& color)
: Vec4Base(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f)
{}

bool Color4F::operator==(const Color3B& right) const
{
return (a == 1.0f && Color3B(*this) == right);
Expand Down
28 changes: 19 additions & 9 deletions core/math/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct HSV;
*/
struct AX_DLL Color3B
{
Color3B();
Color3B(uint8_t _r, uint8_t _g, uint8_t _b);
Color3B() {};
Color3B(uint8_t _r, uint8_t _g, uint8_t _b) : r(_r), g(_g), b(_b) {}
explicit Color3B(const Color4B& color);
explicit Color3B(const Color4F& color);

Expand Down Expand Up @@ -79,9 +79,9 @@ struct AX_DLL Color3B
*/
struct AX_DLL Color4B
{
Color4B();
Color4B(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a);
explicit Color4B(const Color3B& color, uint8_t _a = 255);
Color4B() {}
Color4B(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a) : r(_r), g(_g), b(_b), a(_a) {}
explicit Color4B(const Color3B& color, uint8_t _a = 255) : r(color.r), g(color.g), b(color.b), a(_a) {}
Color4B(const Color4F& color);

inline void set(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a)
Expand Down Expand Up @@ -123,10 +123,15 @@ struct AX_DLL Color4B
struct AX_DLL Color4F : public Vec4Base<Color4F>
{
using Vec4Base = Vec4Base<Color4F>;
Color4F();
Color4F(float _r, float _g, float _b, float _a);
explicit Color4F(const Color3B& color, float _a = 1.0f);
explicit Color4F(const Color4B& color);

Color4F() {}
Color4F(float _r, float _g, float _b, float _a) : Vec4Base(_r, _g, _b, _a) {}
explicit Color4F(const Color3B& color, float _a = 1.0f)
: Vec4Base(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, _a)
{}
explicit Color4F(const Color4B& color)
: Vec4Base(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f)
{}

bool operator==(const Color3B& rhs) const;
bool operator==(const Color4B& rhs) const;
Expand Down Expand Up @@ -199,6 +204,11 @@ struct AX_DLL HSL : public Vec4Base<HSL>
Color4F toColor4F() const;
};

inline Color3B::Color3B(const Color4B& color) : r(color.r), g(color.g), b(color.b) {}
inline Color3B::Color3B(const Color4F& color) : r(color.r * 255.0f), g(color.g * 255.0f), b(color.b * 255.0f) {}

inline Color4B::Color4B(const Color4F& color) : r(color.r * 255), g(color.g * 255), b(color.b * 255), a(color.a * 255) {}

NS_AX_MATH_END

#if defined(_WIN32)
Expand Down
120 changes: 15 additions & 105 deletions core/math/Mat4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,13 @@

NS_AX_MATH_BEGIN

Mat4::Mat4()
{
*this = IDENTITY;
}

Mat4::Mat4(float m11,
float m12,
float m13,
float m14,
float m21,
float m22,
float m23,
float m24,
float m31,
float m32,
float m33,
float m34,
float m41,
float m42,
float m43,
float m44)
{
set(m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, m44);
}
#if defined(AX_DLLEXPORT) || defined(AX_DLLIMPORT)
const Mat4 Mat4::IDENTITY =
Mat4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);

Mat4::Mat4(const float* mat)
{
set(mat);
}

Mat4::Mat4(const Mat4& copy)
{
memcpy(m, copy.m, MATRIX_SIZE);
}
const Mat4 Mat4::ZERO = Mat4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
#endif

Mat4::~Mat4() {}

void Mat4::createLookAt(const Vec3& eyePosition, const Vec3& targetPosition, const Vec3& up, Mat4* dst)
{
Expand Down Expand Up @@ -140,7 +111,7 @@ void Mat4::createPerspective(float fieldOfView, float aspectRatio, float zNearPl
GP_ASSERT(divisor);
float factor = 1.0f / divisor;

memset(dst, 0, MATRIX_SIZE);
memset(dst->m, 0, sizeof(dst->m));

GP_ASSERT(aspectRatio);
dst->m[0] = (1.0f / aspectRatio) * factor;
Expand Down Expand Up @@ -176,7 +147,7 @@ void Mat4::createOrthographicOffCenter(float left,
GP_ASSERT(top != bottom);
GP_ASSERT(zFarPlane != zNearPlane);

memset(dst, 0, MATRIX_SIZE);
memset(dst->m, 0, sizeof(dst->m));
dst->m[0] = 2 / (right - left);
dst->m[5] = 2 / (top - bottom);
dst->m[10] = 2 / (zNearPlane - zFarPlane);
Expand Down Expand Up @@ -268,7 +239,7 @@ void Mat4::createScale(const Vec3& scale, Mat4* dst)
{
GP_ASSERT(dst);

memcpy(dst, &IDENTITY, MATRIX_SIZE);
dst->setIdentity();

dst->m[0] = scale.x;
dst->m[5] = scale.y;
Expand All @@ -279,7 +250,7 @@ void Mat4::createScale(float xScale, float yScale, float zScale, Mat4* dst)
{
GP_ASSERT(dst);

memcpy(dst, &IDENTITY, MATRIX_SIZE);
dst->setIdentity();

dst->m[0] = xScale;
dst->m[5] = yScale;
Expand Down Expand Up @@ -388,7 +359,7 @@ void Mat4::createRotationX(float angle, Mat4* dst)
{
GP_ASSERT(dst);

memcpy(dst, &IDENTITY, MATRIX_SIZE);
dst->setIdentity();

float c = std::cos(angle);
float s = std::sin(angle);
Expand All @@ -403,7 +374,7 @@ void Mat4::createRotationY(float angle, Mat4* dst)
{
GP_ASSERT(dst);

memcpy(dst, &IDENTITY, MATRIX_SIZE);
dst->setIdentity();

float c = std::cos(angle);
float s = std::sin(angle);
Expand All @@ -418,7 +389,7 @@ void Mat4::createRotationZ(float angle, Mat4* dst)
{
GP_ASSERT(dst);

memcpy(dst, &IDENTITY, MATRIX_SIZE);
dst->setIdentity();

float c = std::cos(angle);
float s = std::sin(angle);
Expand All @@ -433,7 +404,7 @@ void Mat4::createTranslation(const Vec3& translation, Mat4* dst)
{
GP_ASSERT(dst);

memcpy(dst, &IDENTITY, MATRIX_SIZE);
dst->setIdentity();

dst->m[12] = translation.x;
dst->m[13] = translation.y;
Expand All @@ -444,7 +415,7 @@ void Mat4::createTranslation(float xTranslation, float yTranslation, float zTran
{
GP_ASSERT(dst);

memcpy(dst, &IDENTITY, MATRIX_SIZE);
dst->setIdentity();

dst->m[12] = xTranslation;
dst->m[13] = yTranslation;
Expand Down Expand Up @@ -727,7 +698,7 @@ bool Mat4::inverse()

bool Mat4::isIdentity() const
{
return (memcmp(m, &IDENTITY, MATRIX_SIZE) == 0);
return (memcmp(m, IDENTITY.m, sizeof(m)) == 0);
}

void Mat4::multiply(float scalar)
Expand Down Expand Up @@ -861,62 +832,6 @@ void Mat4::scale(const Vec3& s, Mat4* dst) const
scale(s.x, s.y, s.z, dst);
}

void Mat4::set(float m11,
float m12,
float m13,
float m14,
float m21,
float m22,
float m23,
float m24,
float m31,
float m32,
float m33,
float m34,
float m41,
float m42,
float m43,
float m44)
{
m[0] = m11;
m[1] = m21;
m[2] = m31;
m[3] = m41;
m[4] = m12;
m[5] = m22;
m[6] = m32;
m[7] = m42;
m[8] = m13;
m[9] = m23;
m[10] = m33;
m[11] = m43;
m[12] = m14;
m[13] = m24;
m[14] = m34;
m[15] = m44;
}

void Mat4::set(const float* mat)
{
GP_ASSERT(mat);
memcpy(this->m, mat, MATRIX_SIZE);
}

void Mat4::set(const Mat4& mat)
{
memcpy(this->m, mat.m, MATRIX_SIZE);
}

void Mat4::setIdentity()
{
memcpy(m, &IDENTITY, MATRIX_SIZE);
}

void Mat4::setZero()
{
memset(m, 0, MATRIX_SIZE);
}

void Mat4::subtract(const Mat4& mat)
{
subtract(*this, mat, this);
Expand Down Expand Up @@ -992,9 +907,4 @@ Mat4 Mat4::getTransposed() const
return mat;
}

const Mat4 Mat4::IDENTITY =
Mat4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);

const Mat4 Mat4::ZERO = Mat4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

NS_AX_MATH_END
Loading

0 comments on commit dc26757

Please sign in to comment.