Add SIMD to Pmath::Plane.

This commit is contained in:
THoehne 2024-10-13 17:54:58 +02:00
parent 2f84387b1a
commit 89d96bbdf9
6 changed files with 93 additions and 40 deletions

View File

@ -24,15 +24,6 @@ namespace Phanes::Core::Math::Detail
template<RealType T> template<RealType T>
struct construct_plane<T, false> struct construct_plane<T, false>
{ {
static constexpr void map(TPlane<T, false>& r, const TPlane<T, false>& pl1)
{
r.comp = std::copy(pl1.comp);
}
static constexpr void map(TPlane<T, false>& r, TPlane<T, false>&& pl1)
{
r.comp = std::move(pl1.comp);
}
static constexpr void map(TPlane<T, false>& r, const TVector3<T, false>& normal, T d) static constexpr void map(TPlane<T, false>& r, const TVector3<T, false>& normal, T d)
{ {

View File

@ -93,6 +93,13 @@ namespace Phanes::Core::Math::Detail
r.w = v3.y; r.w = v3.y;
} }
static constexpr void map(Phanes::Core::Math::TVector4<T, false>& r, const Phanes::Core::Math::TVector3<T, false>& v, T w)
{
r.x = v.x;
r.y = v.y;
r.z = v.z;
r.w = w;
}
static constexpr void map(Phanes::Core::Math::TVector4<T, false>& r, const T* comp) static constexpr void map(Phanes::Core::Math::TVector4<T, false>& r, const T* comp)
{ {

View File

@ -62,19 +62,6 @@ namespace Phanes::Core::Math {
TPlane() = default; TPlane() = default;
/**
* Copy constructor
*/
TPlane(const TPlane<Real, S>& plane);
/**
* Move constructor
*/
TPlane(TPlane<Real, S>&& plane);
/** /**
* Construct plane from normal and d * Construct plane from normal and d
* *
@ -96,7 +83,7 @@ namespace Phanes::Core::Math {
TPlane(const TVector3<Real, S>& normal, const TVector3<Real, S>& base); TPlane(const TVector3<Real, S>& normal, const TVector3<Real, S>& base);
/** /**
* Construct plane from coefficients * Construct plane from coefficients. The components should be normalized.
* *
* @param(x) X coefficient * @param(x) X coefficient
* @param(y) Y coefficient * @param(y) Y coefficient
@ -911,7 +898,7 @@ namespace Phanes::Core::Math {
template<RealType T> template<RealType T>
TVector3<T, false> PointProjectOntoPlane(const TVector3<T, false>& p1, const TPlane<T, false>& pl1) TVector3<T, false> PointProjectOntoPlane(const TVector3<T, false>& p1, const TPlane<T, false>& pl1)
{ {
p1 - PointDistance(pl1, p1) * pl1.normal; return p1 - PointDistance(pl1, p1) * pl1.normal;
} }
/** /**

View File

@ -11,18 +11,6 @@
namespace Phanes::Core::Math namespace Phanes::Core::Math
{ {
template<RealType T, bool S>
TPlane<T, S>::TPlane(const TPlane<T, S>& plane)
{
Detail::construct_plane<T, S>::map(*this, plane);
}
template<RealType T, bool S>
TPlane<T, S>::TPlane(TPlane<T, S>&& plane)
{
Detail::construct_plane<T, S>::map(*this, plane);
}
template<RealType T, bool S> template<RealType T, bool S>
TPlane<T, S>::TPlane(const TVector3<T, S>& normal, Real d) TPlane<T, S>::TPlane(const TVector3<T, S>& normal, Real d)
{ {

View File

@ -11,6 +11,8 @@
#include "Core/public/Math/Vector3.hpp" #include "Core/public/Math/Vector3.hpp"
#include "Core/public/Math/Vector4.hpp" #include "Core/public/Math/Vector4.hpp"
#include "Core/public/Math/Plane.hpp"
#include "Core/public/Math/IntVector2.hpp" #include "Core/public/Math/IntVector2.hpp"
#include "Core/public/Math/IntVector3.hpp" #include "Core/public/Math/IntVector3.hpp"
#include "Core/public/Math/IntVector4.hpp" #include "Core/public/Math/IntVector4.hpp"
@ -19,6 +21,7 @@
#include "Core/public/Math/Matrix4.hpp" #include "Core/public/Math/Matrix4.hpp"
// ========== // // ========== //
// Common // // Common //
// ========== // // ========== //
@ -110,7 +113,7 @@ namespace Phanes::Core::Math::SIMD
/// <param name="v1"></param> /// <param name="v1"></param>
void vec3_fix(Phanes::Core::Types::Vec4f32Reg v1) void vec3_fix(Phanes::Core::Types::Vec4f32Reg v1)
{ {
v1 = _mm_blend_ps(v1, _mm_setzero_ps(), 0x1); v1.m128_f32[3] = 0.0f;
} }
} }
@ -155,6 +158,11 @@ namespace Phanes::Core::Math::Detail
{ {
v1.comp = _mm_loadu_ps(s); v1.comp = _mm_loadu_ps(s);
} }
static FORCEINLINE void map(Phanes::Core::Math::TVector4<float, true>& r, const Phanes::Core::Math::TVector3<float, true>& v, float w)
{
r.comp = _mm_set_ps(w, v.z, v.y, v.x);
}
}; };
template<> template<>
@ -302,8 +310,8 @@ namespace Phanes::Core::Math::Detail
{ {
static FORCEINLINE void map(Phanes::Core::Math::TVector4<float, true>& r, const Phanes::Core::Math::TVector4<float, true>& v1) static FORCEINLINE void map(Phanes::Core::Math::TVector4<float, true>& r, const Phanes::Core::Math::TVector4<float, true>& v1)
{ {
__m128 tmp = _mm_div_ps(v1.data, _mm_set_ps1(v1.w)); r.data = _mm_div_ps(v1.data, _mm_set_ps1(v1.w));
r.data = _mm_blend_ps(tmp, _mm_setzero_ps(), 0x1); r.w = 0.0f;
} }
}; };
@ -553,6 +561,76 @@ namespace Phanes::Core::Math::Detail
}; };
// ========= //
// Plane //
// ========= //
template<>
struct construct_plane<float, true>
{
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& pl, const TVector3<float, true>& v1, float d)
{
pl.comp.data = v1.data;
pl.comp.w = d;
}
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& pl, const TVector3<float, true>& normal, const TVector3<float, true>& base)
{
pl.comp.data = normal.data;
pl.comp.w = DotP(normal, base);
}
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& pl, float x, float y, float z, float d)
{
pl.comp.data = _mm_set_ps(x, y, z, d);
}
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& pl, const TVector3<float, true>& v1, const TVector3<float, true>& v2, const TVector3<float, true>& v3)
{
TVector4<float, false> tmp;
}
};
template<>
struct compute_plane_add<float, true>
{
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& r, Phanes::Core::Math::TPlane<float, true>& pl1, Phanes::Core::Math::TPlane<float, true>& pl2)
{
r.comp.data = _mm_add_ps(pl1.comp.data, pl2.comp.data);
}
};
template<>
struct compute_plane_sub<float, true>
{
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& r, Phanes::Core::Math::TPlane<float, true>& pl1, Phanes::Core::Math::TPlane<float, true>& pl2)
{
r.comp.data = _mm_sub_ps(pl1.comp.data, pl2.comp.data);
}
};
template<>
struct compute_plane_mul<float, true>
{
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& r, Phanes::Core::Math::TPlane<float, true>& pl1, Phanes::Core::Math::TPlane<float, true>& pl2)
{
r.comp.data = _mm_mul_ps(pl1.comp.data, pl2.comp.data);
}
};
template<>
struct compute_plane_div<float, true>
{
static FORCEINLINE void map(Phanes::Core::Math::TPlane<float, true>& r, Phanes::Core::Math::TPlane<float, true>& pl1, Phanes::Core::Math::TPlane<float, true>& pl2)
{
r.comp.data = _mm_div_ps(pl1.comp.data, pl2.comp.data);
}
};
// =============== // // =============== //
// TIntVector4 // // TIntVector4 //
// =============== // // =============== //

View File

@ -1262,6 +1262,8 @@ namespace Plane
{ {
TEST(Plane, OperatorTests) TEST(Plane, OperatorTests)
{ {
PMath::Plane pl1(3.0f / 5.4772255750f, -2.0f / 5.4772255750f, -3.0f / 5.4772255750f, 4.0f);
PMath::Plane pl2(-0.526316f, -0.442105f, -0.726316f, 6.0f);
} }
} }