From a247564855878f0acdf26a5a3bcc27f986a5ff4d Mon Sep 17 00:00:00 2001 From: Evgeny Gorodetskiy Date: Mon, 18 Jul 2022 20:06:34 +0300 Subject: [PATCH] Workaround HLSL++ greater and less operators issue in Point wrapper, add indexed component getter to Point --- .../Data/Types/Include/Methane/Data/Point.hpp | 44 +++++++++++++++++-- .../Types/Include/Methane/Data/Vector.hpp | 6 +-- Tests/Data/Types/PointTest.cpp | 4 ++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/Modules/Data/Types/Include/Methane/Data/Point.hpp b/Modules/Data/Types/Include/Methane/Data/Point.hpp index 9bebe8178..46560d482 100644 --- a/Modules/Data/Types/Include/Methane/Data/Point.hpp +++ b/Modules/Data/Types/Include/Methane/Data/Point.hpp @@ -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): @@ -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()); } diff --git a/Modules/Data/Types/Include/Methane/Data/Vector.hpp b/Modules/Data/Types/Include/Methane/Data/Vector.hpp index fa8ac0bcc..41a5d1aa9 100644 --- a/Modules/Data/Types/Include/Methane/Data/Vector.hpp +++ b/Modules/Data/Types/Include/Methane/Data/Vector.hpp @@ -254,10 +254,10 @@ class RawVector return RoundCast(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]; } diff --git a/Tests/Data/Types/PointTest.cpp b/Tests/Data/Types/PointTest.cpp index cfb3171a3..53cec0404 100644 --- a/Tests/Data/Types/PointTest.cpp +++ b/Tests/Data/Types/PointTest.cpp @@ -213,6 +213,7 @@ 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(test_arr).SetX(new_value), new_arr); } @@ -220,6 +221,7 @@ TEMPLATE_TEST_CASE_SIG("Point Coordinate Accessors and Property Getters", "[poin 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(test_arr).SetY(new_value), new_arr); } @@ -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(test_arr).SetZ(new_value), new_arr); } @@ -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(test_arr).SetW(new_value), new_arr); }