Added rgb565 test file, clamping Make5/6, proper integral SFINAE condition, removed operators =

This commit is contained in:
Vladyslav Baranovskyi
2024-10-02 16:03:17 +03:00
parent 1872a03ff5
commit 996ec4c168
2 changed files with 224 additions and 34 deletions

View File

@@ -13,8 +13,16 @@ namespace OpenVulkano::Math
{
class RGB565
{
static uint16_t Make5(uint8_t val) { return val * 31.0f / 255.0f; }
static uint16_t Make6(uint8_t val) { return val * 63.0f / 255.0f; }
static uint16_t Make5(int16_t val)
{
val = std::clamp<int16_t>(val, 0, 255);
return val * 31.0f / 255.0f;
}
static uint16_t Make6(int16_t val)
{
val = std::clamp<int16_t>(val, 0, 255);
return val * 63.0f / 255.0f;
}
static uint8_t Unmake5(uint16_t val) { return static_cast<uint8_t>(val * 255.0f / 31.0f); }
static uint8_t Unmake6(uint16_t val) { return static_cast<uint8_t>(val * 255.0f / 63.0f); }
static float Unmake5ToFloat(uint16_t val) { return std::clamp(val / 31.0f, 0.0f, 1.0f); }
@@ -36,15 +44,15 @@ namespace OpenVulkano::Math
RGB565() : r(0), g(0), b(0) {}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565(const Math::Vector3<T>& color) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r))
{
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565(const Math::Vector4<T>& color) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r))
{
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565(const Math::Vector3_SIMD<T>& color) : b(Make5(color.b)), g(Make6(color.g)), r(Make5(color.r))
{
}
@@ -68,17 +76,17 @@ namespace OpenVulkano::Math
uint8_t GetG() { return Unmake6(g); }
uint8_t GetB() { return Unmake5(b); }
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
Math::Vector3<T> Get3()
{
return { GetR(), GetG(), GetB() };
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
Math::Vector4<T> Get4()
{
return { GetR(), GetG(), GetB(), 1 };
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
Math::Vector3_SIMD<T> GetSIMD()
{
return { GetR(), GetG(), GetB() };
@@ -103,7 +111,7 @@ namespace OpenVulkano::Math
void SetG(uint8_t green) { g = Make6(green); }
void SetB(uint8_t blue) { b = Make5(blue); }
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
void Set(const Math::Vector3<T>& color)
{
r = Make5(color.r);
@@ -111,7 +119,7 @@ namespace OpenVulkano::Math
b = Make5(color.b);
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
void Set(const Math::Vector4<T>& color)
{
r = Make5(color.r);
@@ -143,33 +151,21 @@ namespace OpenVulkano::Math
void SetG_Normalized(float green) { g = Make6FromFloat(green); }
void SetB_Normalized(float blue) { b = Make5FromFloat(blue); }
RGB565& operator=(const RGB565& other)
{
value = other.value;
return *this;
}
RGB565& operator=(RGB565&& other) noexcept
{
value = other.value;
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator=(const Vector3<T>& color)
{
Set(color);
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator=(const Vector3_SIMD<T>& color)
{
Set(color);
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator=(const Vector4_SIMD<T>& color)
{
Set(color);
@@ -197,21 +193,21 @@ namespace OpenVulkano::Math
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator+=(const Vector3<T>& color)
{
Set(Get3<T>() + color);
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator+=(const Vector4<T>& color)
{
Set(Get4<T>() + color);
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator+=(const Vector3_SIMD<T>& color)
{
Set(GetSIMD<T>() + color);
@@ -239,21 +235,21 @@ namespace OpenVulkano::Math
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator-=(const Vector3<T>& color)
{
Set(Get3<T>() - color);
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator-=(const Vector4<T>& color)
{
Set(Get4<T>() - color);
return *this;
}
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
RGB565& operator-=(const Vector3_SIMD<T>& color)
{
Set(GetSIMD<T>() - color);
@@ -284,11 +280,11 @@ namespace OpenVulkano::Math
bool operator==(const RGB565& other) const { return value == other.value; }
bool operator!=(const RGB565& other) const { return value != other.value; }
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
operator Math::Vector4<T>() { return Get4<T>(); }
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
operator Math::Vector3<T>() { return Get3<T>(); }
template<typename T, std::enable_if_t<std::is_unsigned_v<T> || std::is_signed_v<T>, bool> = true>
template<typename T, std::enable_if_t<std::is_integral_v<T>, bool> = true>
operator Math::Vector3_SIMD<T>() { return GetSIMD<T>(); }
template<typename T, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
operator Math::Vector4<T>() { return Get4Normalized<T>(); }