From c3999d708fb61f9a5c8052a31ee63833bfb683e2 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 18 Oct 2021 16:46:03 +0200 Subject: [PATCH] Graphics: Add DistanceAsSortKey function --- include/Nazara/Graphics/Algorithm.hpp | 19 ++++++++++ include/Nazara/Graphics/Algorithm.inl | 37 +++++++++++++++++++ include/Nazara/Graphics/RenderSpriteChain.inl | 16 ++------ include/Nazara/Graphics/RenderSubmesh.inl | 16 ++------ src/Nazara/Graphics/AlgorithmGraphics.cpp | 10 +++++ 5 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 include/Nazara/Graphics/Algorithm.hpp create mode 100644 include/Nazara/Graphics/Algorithm.inl create mode 100644 src/Nazara/Graphics/AlgorithmGraphics.cpp diff --git a/include/Nazara/Graphics/Algorithm.hpp b/include/Nazara/Graphics/Algorithm.hpp new file mode 100644 index 000000000..3bd996b50 --- /dev/null +++ b/include/Nazara/Graphics/Algorithm.hpp @@ -0,0 +1,19 @@ +// Copyright (C) 2020 Jérôme Leclercq +// This file is part of the "Nazara Engine - Graphics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_ALGORITHM_GRAPHICS_HPP +#define NAZARA_ALGORITHM_GRAPHICS_HPP + +#include + +namespace Nz +{ + inline UInt32 DistanceAsSortKey(float distance); +} + +#include + +#endif diff --git a/include/Nazara/Graphics/Algorithm.inl b/include/Nazara/Graphics/Algorithm.inl new file mode 100644 index 000000000..22eee99a8 --- /dev/null +++ b/include/Nazara/Graphics/Algorithm.inl @@ -0,0 +1,37 @@ +// Copyright (C) 2020 Jérôme Leclercq +// This file is part of the "Nazara Engine - Graphics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include +#include + +namespace Nz +{ + inline UInt32 DistanceAsSortKey(float distance) + { +#if defined(arm) && \ + ((defined(__MAVERICK__) && defined(NAZARA_BIG_ENDIAN)) || \ + (!defined(__SOFTFP__) && !defined(__VFP_FP__) && !defined(__MAVERICK__))) +#error The following code relies on native-endian IEEE-754 representation, which your platform does not guarantee +#endif + + static_assert(sizeof(float) == sizeof(UInt32)); + static_assert(std::numeric_limits::is_iec559); + + if (std::isnan(distance)) + return std::numeric_limits::max(); + + if (std::isinf(distance)) + return 0; + + UInt32 distanceInt; + std::memcpy(&distanceInt, &distance, sizeof(UInt32)); + + return ~distanceInt; //< Reverse distance to have back to front + } +} + +#include diff --git a/include/Nazara/Graphics/RenderSpriteChain.inl b/include/Nazara/Graphics/RenderSpriteChain.inl index 5576967a6..b124f6f2d 100644 --- a/include/Nazara/Graphics/RenderSpriteChain.inl +++ b/include/Nazara/Graphics/RenderSpriteChain.inl @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include namespace Nz @@ -32,19 +33,8 @@ namespace Nz { UInt64 matFlags = 1; -#if defined(arm) && \ - ((defined(__MAVERICK__) && defined(NAZARA_BIG_ENDIAN)) || \ - (!defined(__SOFTFP__) && !defined(__VFP_FP__) && !defined(__MAVERICK__))) -#error The following code relies on native-endian IEEE-754 representation, which your platform does not guarantee -#endif - - static_assert(sizeof(float) == sizeof(UInt32)); - - float distanceNear = frustum.GetPlane(Nz::FrustumPlane::Near).Distance(m_worldInstance.GetWorldMatrix().GetTranslation()); - UInt32 distanceInt; - std::memcpy(&distanceInt, &distanceNear, sizeof(UInt32)); - - UInt64 distance = static_cast(~distanceInt); //< Reverse distance to have back to front + float distanceNear = frustum.GetPlane(FrustumPlane::Near).Distance(m_worldInstance.GetWorldMatrix().GetTranslation()); + UInt64 distance = DistanceAsSortKey(distanceNear); // Transparent RQ index: // - Layer (8bits) diff --git a/include/Nazara/Graphics/RenderSubmesh.inl b/include/Nazara/Graphics/RenderSubmesh.inl index 860793541..92e6451a5 100644 --- a/include/Nazara/Graphics/RenderSubmesh.inl +++ b/include/Nazara/Graphics/RenderSubmesh.inl @@ -3,6 +3,7 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include namespace Nz @@ -31,19 +32,8 @@ namespace Nz { UInt64 matFlags = 1; -#if defined(arm) && \ - ((defined(__MAVERICK__) && defined(NAZARA_BIG_ENDIAN)) || \ - (!defined(__SOFTFP__) && !defined(__VFP_FP__) && !defined(__MAVERICK__))) -#error The following code relies on native-endian IEEE-754 representation, which your platform does not guarantee -#endif - - static_assert(sizeof(float) == sizeof(UInt32)); - - float distanceNear = frustum.GetPlane(Nz::FrustumPlane::Near).Distance(m_worldInstance.GetWorldMatrix().GetTranslation()); - UInt32 distanceInt; - std::memcpy(&distanceInt, &distanceNear, sizeof(UInt32)); - - UInt64 distance = ~distanceInt; //< Reverse distance to have back to front + float distanceNear = frustum.GetPlane(FrustumPlane::Near).Distance(m_worldInstance.GetWorldMatrix().GetTranslation()); + UInt64 distance = DistanceAsSortKey(distanceNear); // Transparent RQ index: // - Layer (8bits) diff --git a/src/Nazara/Graphics/AlgorithmGraphics.cpp b/src/Nazara/Graphics/AlgorithmGraphics.cpp new file mode 100644 index 000000000..421dd7e74 --- /dev/null +++ b/src/Nazara/Graphics/AlgorithmGraphics.cpp @@ -0,0 +1,10 @@ +// Copyright (C) 2020 Jérôme Leclercq +// This file is part of the "Nazara Engine - Graphics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +namespace Nz +{ +}