From 27c304412a64125516d15dac82d19b73e96668f3 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 21 Oct 2020 12:18:28 -0700 Subject: Mipmap generation: few updates to fix floating point incosistencies that were causing tests to fail --- TextureLoader/src/TextureLoader.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'TextureLoader/src/TextureLoader.cpp') diff --git a/TextureLoader/src/TextureLoader.cpp b/TextureLoader/src/TextureLoader.cpp index 999070a..48519a3 100644 --- a/TextureLoader/src/TextureLoader.cpp +++ b/TextureLoader/src/TextureLoader.cpp @@ -69,18 +69,23 @@ namespace Diligent template ChannelType SRGBAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelType c3) { - constexpr float NormVal = static_cast(std::numeric_limits::max()); - float fc0 = static_cast(c0) / NormVal; - float fc1 = static_cast(c1) / NormVal; - float fc2 = static_cast(c2) / NormVal; - float fc3 = static_cast(c3) / NormVal; + static constexpr float NormVal = static_cast(std::numeric_limits::max()); + + float fc0 = static_cast(c0) / NormVal; + float fc1 = static_cast(c1) / NormVal; + float fc2 = static_cast(c2) / NormVal; + float fc3 = static_cast(c3) / NormVal; float fLinearAverage = (SRGBToLinear(fc0) + SRGBToLinear(fc1) + SRGBToLinear(fc2) + SRGBToLinear(fc3)) / 4.f; - float fSRGBAverage = LinearToSRGB(fLinearAverage); - Int32 SRGBAverage = static_cast(fSRGBAverage * NormVal); - SRGBAverage = std::min(SRGBAverage, static_cast(std::numeric_limits::max())); - SRGBAverage = std::max(SRGBAverage, static_cast(std::numeric_limits::min())); - return static_cast(SRGBAverage); + float fSRGBAverage = LinearToSRGB(fLinearAverage) * NormVal; + + static constexpr float MinVal = static_cast(std::numeric_limits::min()); + static constexpr float MaxVal = static_cast(std::numeric_limits::max()); + + fSRGBAverage = std::max(fSRGBAverage, MinVal); + fSRGBAverage = std::min(fSRGBAverage, MaxVal); + + return static_cast(fSRGBAverage); } template @@ -121,8 +126,8 @@ void ComputeCoarseMip(Uint32 NumChannels, for (Uint32 c = 0; c < NumChannels; ++c) { auto Chnl00 = pSrcRow0[src_col0 * NumChannels + c]; - auto Chnl10 = pSrcRow0[src_col1 * NumChannels + c]; - auto Chnl01 = pSrcRow1[src_col0 * NumChannels + c]; + auto Chnl01 = pSrcRow0[src_col1 * NumChannels + c]; + auto Chnl10 = pSrcRow1[src_col0 * NumChannels + c]; auto Chnl11 = pSrcRow1[src_col1 * NumChannels + c]; auto& DstCol = reinterpret_cast(reinterpret_cast(pCoarseMip) + row * CoarseMipStride)[col * NumChannels + c]; -- cgit v1.2.3