Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math optimizations #2115

Merged
merged 17 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions core/math/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ struct AX_DLL HSL : public Vec4Base<HSL>

NS_AX_MATH_END

#include "math/Color.inl"

#if defined(_WIN32)
# pragma pop_macro("TRANSPARENT")
#endif
61 changes: 61 additions & 0 deletions core/math/Color.inl
smilediver marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/****************************************************************************

Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).

https://axmol.dev/

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

#include "math/Color.h"

namespace ax
{

inline Color3B::Color3B() {}

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

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() {}

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

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

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


inline Color4F::Color4F() {}

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

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

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

}
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
25 changes: 4 additions & 21 deletions core/math/Mat4.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,6 @@ class AX_DLL Mat4
#endif
{
public:
// //temp add conversion
// operator kmMat4() const
// {
// kmMat4 result;
// kmMat4Fill(&result, m);
// return result;
// }

// Mat4(const kmMat4& mat)
// {
// set(mat.mat);
// }
/**
* Stores the columns of this 4x4 matrix.
* */
Expand All @@ -110,7 +98,7 @@ class AX_DLL Mat4
* 0 0 1 0
* 0 0 0 1
*/
Mat4();
constexpr Mat4();

/**
* Constructs a matrix initialized to the specified value.
Expand All @@ -132,7 +120,7 @@ class AX_DLL Mat4
* @param m43 The third element of the fourth row.
* @param m44 The fourth element of the fourth row.
*/
Mat4(float m11,
constexpr Mat4(float m11,
float m12,
float m13,
float m14,
Expand Down Expand Up @@ -170,11 +158,6 @@ class AX_DLL Mat4
*/
Mat4(const Mat4& copy);

/**
* Destructor.
*/
~Mat4();

/**
* Creates a view matrix based on the specified input parameters.
*
Expand Down Expand Up @@ -744,7 +727,7 @@ class AX_DLL Mat4
* @param m43 The third element of the fourth row.
* @param m44 The fourth element of the fourth row.
*/
void set(float m11,
constexpr void set(float m11,
float m12,
float m13,
float m14,
Expand Down Expand Up @@ -778,7 +761,7 @@ class AX_DLL Mat4
/**
* Sets this matrix to the identity matrix.
*/
void setIdentity();
constexpr void setIdentity();

/**
* Sets all elements of the current matrix to zero.
Expand Down
Loading