Skip to content

Commit

Permalink
Workaround HLSL++ greater and less operators issue in Point wrapper, …
Browse files Browse the repository at this point in the history
…add indexed component getter to Point
  • Loading branch information
egorodet committed Jul 18, 2022
1 parent 1c5df90 commit a247564
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
44 changes: 41 additions & 3 deletions Modules/Data/Types/Include/Methane/Data/Point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,19 @@ class Point
return *this;
}

bool operator==(const PointType& other) const noexcept
[[nodiscard]] T operator[](size_t index) const
{
switch(index)
{
case 0: return m_vector.x;
case 1: return m_vector.y;
case 2: if constexpr(size > 2) return m_vector.z;
case 3: if constexpr(size > 3) return m_vector.w;
default: META_UNEXPECTED_ARG_RETURN(index, T{});
}
}

[[nodiscard]] bool operator==(const PointType& other) const noexcept
{
#if defined(__APPLE__) && defined(__x86_64__)
// FIXME: workaround for HLSL++ issue (https://github.com/redorav/hlslpp/issues/61):
Expand All @@ -132,10 +144,36 @@ class Point
#endif
}

[[nodiscard]] bool operator<(const PointType& other) const noexcept
{
#if defined(__APPLE__) && defined(__x86_64__)
// FIXME: workaround for HLSL++ issue (https://github.com/redorav/hlslpp/issues/61):
// Integer vector comparison is working incorrectly on Intel based Macs with MacOS >= 11
for(size_t i = 0; i < size; ++i)
if ((*this)[i] >= other[i])
return false;
return true;
#else
return hlslpp::all(m_vector < other.AsVector());
#endif
}

[[nodiscard]] bool operator>(const PointType& other) const noexcept
{
#if defined(__APPLE__) && defined(__x86_64__)
// FIXME: workaround for HLSL++ issue (https://github.com/redorav/hlslpp/issues/61):
// Integer vector comparison is working incorrectly on Intel based Macs with MacOS >= 11
for(size_t i = 0; i < size; ++i)
if ((*this)[i] <= other[i])
return false;
return true;
#else
return hlslpp::all(m_vector > other.AsVector());
#endif
}

bool operator!=(const PointType& other) const noexcept { return !operator==(other); }
bool operator<(const PointType& other) const noexcept { return hlslpp::all(m_vector < other.AsVector()); }
bool operator<=(const PointType& other) const noexcept { return hlslpp::all(m_vector <= other.AsVector()); }
bool operator>(const PointType& other) const noexcept { return hlslpp::all(m_vector > other.AsVector()); }
bool operator>=(const PointType& other) const noexcept { return hlslpp::all(m_vector >= other.AsVector()); }

PointType operator+(const PointType& other) const noexcept { return PointType(m_vector + other.AsVector()); }
Expand Down
6 changes: 3 additions & 3 deletions Modules/Data/Types/Include/Methane/Data/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ class RawVector
return RoundCast<T>(std::sqrt(square_sum));
}

[[nodiscard]] T operator[](size_t index) const noexcept { return m_components[index]; }
[[nodiscard]] T& operator[](size_t index) noexcept { return m_components[index]; }
[[nodiscard]] T operator[](size_t index) const { META_CHECK_ARG_LESS(index, size); return m_components[index]; }
[[nodiscard]] T& operator[](size_t index) { META_CHECK_ARG_LESS(index, size); return m_components[index]; }

[[nodiscard]] T Get(size_t index) const { META_CHECK_ARG_LESS(index, size); return m_components[index]; }
[[nodiscard]] T Get(size_t index) const { META_CHECK_ARG_LESS(index, size); return m_components[index]; }

[[nodiscard]] T GetX() const noexcept { return m_components[0]; }
[[nodiscard]] T GetY() const noexcept { return m_components[1]; }
Expand Down
4 changes: 4 additions & 0 deletions Tests/Data/Types/PointTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,15 @@ TEMPLATE_TEST_CASE_SIG("Point Coordinate Accessors and Property Getters", "[poin
SECTION("X-coordinate getter and setter")
{
CHECK(test_point.GetX() == Approx(test_arr[0]));
CHECK(test_point[0] == Approx(test_arr[0]));
auto new_arr = test_arr; new_arr[0] = new_value;
CheckPoint(Point<T, size>(test_arr).SetX(new_value), new_arr);
}

SECTION("Y-coordinate getter and setter")
{
CHECK(test_point.GetY() == Approx(test_arr[1]));
CHECK(test_point[1] == Approx(test_arr[1]));
auto new_arr = test_arr; new_arr[1] = new_value;
CheckPoint(Point<T, size>(test_arr).SetY(new_value), new_arr);
}
Expand All @@ -229,6 +231,7 @@ TEMPLATE_TEST_CASE_SIG("Point Coordinate Accessors and Property Getters", "[poin
SECTION("Z-coordinate getter and setter")
{
CHECK(test_point.GetZ() == Approx(test_arr[2]));
CHECK(test_point[2] == Approx(test_arr[2]));
auto new_arr = test_arr; new_arr[2] = new_value;
CheckPoint(Point<T, size>(test_arr).SetZ(new_value), new_arr);
}
Expand All @@ -239,6 +242,7 @@ TEMPLATE_TEST_CASE_SIG("Point Coordinate Accessors and Property Getters", "[poin
SECTION("W-coordinate getter and setter")
{
CHECK(test_point.GetW() == Approx(test_arr[3]));
CHECK(test_point[3] == Approx(test_arr[3]));
auto new_arr = test_arr; new_arr[3] = new_value;
CheckPoint(Point<T, size>(test_arr).SetW(new_value), new_arr);
}
Expand Down

0 comments on commit a247564

Please sign in to comment.