From 384a1df6f499f6de32ebf71362c3132e07c1a2c4 Mon Sep 17 00:00:00 2001 From: Lee Kerley Date: Fri, 21 Nov 2025 18:10:17 -0800 Subject: [PATCH] Move Matrix constants to constexpr to avoid global constructor/destructor --- source/MaterialXCore/Types.cpp | 16 ++++++++-------- source/MaterialXCore/Types.h | 32 ++++++++++++++------------------ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/source/MaterialXCore/Types.cpp b/source/MaterialXCore/Types.cpp index 4b34e49295..d54f431c7f 100644 --- a/source/MaterialXCore/Types.cpp +++ b/source/MaterialXCore/Types.cpp @@ -30,14 +30,14 @@ const string NAME_PATH_SEPARATOR = "/"; const string ARRAY_VALID_SEPARATORS = ", "; const string ARRAY_PREFERRED_SEPARATOR = ", "; -const Matrix33 Matrix33::IDENTITY(1, 0, 0, - 0, 1, 0, - 0, 0, 1); - -const Matrix44 Matrix44::IDENTITY(1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - 0, 0, 0, 1); +constexpr Matrix33 Matrix33::IDENTITY = Matrix33(1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f); + +constexpr Matrix44 Matrix44::IDENTITY = Matrix44(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); // // Color3 methods diff --git a/source/MaterialXCore/Types.h b/source/MaterialXCore/Types.h index 4fd88cb127..ecea4a59be 100644 --- a/source/MaterialXCore/Types.h +++ b/source/MaterialXCore/Types.h @@ -400,8 +400,10 @@ template class MatrixN : public MatrixBase using ConstIterator = typename std::array::const_iterator; public: - MatrixN() : _arr{} { } - explicit MatrixN(Uninit) { } + constexpr MatrixN() : _arr{} { } + constexpr explicit MatrixN(Uninit) { } + template + constexpr explicit MatrixN(const T... ts) : _arr{static_cast(ts)...} {} explicit MatrixN(S s) { std::fill_n(&_arr[0][0], N * N, s); } explicit MatrixN(const S* begin, const S* end) { std::copy(begin, end, &_arr[0][0]); } @@ -615,16 +617,13 @@ class MX_CORE_API Matrix33 : public MatrixN { public: using MatrixN::MatrixN; - Matrix33() = default; - Matrix33(float m00, float m01, float m02, + constexpr Matrix33() = default; + constexpr Matrix33(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) : - MatrixN(Uninit{}) - { - _arr = { RowArray{ m00, m01, m02 }, - RowArray{ m10, m11, m12 }, - RowArray{ m20, m21, m22 } }; - } + MatrixN{m00, m01, m02, + m10, m11, m12, + m20, m21, m22} {} /// @name Matrix Operations /// @{ @@ -686,17 +685,14 @@ class MX_CORE_API Matrix44 : public MatrixN public: using MatrixN::MatrixN; Matrix44() = default; - Matrix44(float m00, float m01, float m02, float m03, + constexpr Matrix44(float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33) : - MatrixN(Uninit{}) - { - _arr = { RowArray{ m00, m01, m02, m03 }, - RowArray{ m10, m11, m12, m13 }, - RowArray{ m20, m21, m22, m23 }, - RowArray{ m30, m31, m32, m33 } }; - } + MatrixN{m00, m01, m02, m03, + m10, m11, m12, m13, + m20, m21, m22, m23, + m30, m31, m32, m33} {} /// @name Matrix Operations /// @{