diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2020-10-21 19:18:28 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2020-10-21 19:18:28 +0000 |
| commit | 27c304412a64125516d15dac82d19b73e96668f3 (patch) | |
| tree | 9fd395196c5a88237ebf02f7008da9a7e4622fca /TextureLoader/src/TextureLoader.cpp | |
| parent | Another minor fix to mipmap generation (diff) | |
| download | DiligentTools-27c304412a64125516d15dac82d19b73e96668f3.tar.gz DiligentTools-27c304412a64125516d15dac82d19b73e96668f3.zip | |
Mipmap generation: few updates to fix floating point incosistencies that were causing tests to fail
Diffstat (limited to 'TextureLoader/src/TextureLoader.cpp')
| -rw-r--r-- | TextureLoader/src/TextureLoader.cpp | 29 |
1 files changed, 17 insertions, 12 deletions
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 <typename ChannelType> ChannelType SRGBAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelType c3) { - constexpr float NormVal = static_cast<float>(std::numeric_limits<ChannelType>::max()); - float fc0 = static_cast<float>(c0) / NormVal; - float fc1 = static_cast<float>(c1) / NormVal; - float fc2 = static_cast<float>(c2) / NormVal; - float fc3 = static_cast<float>(c3) / NormVal; + static constexpr float NormVal = static_cast<float>(std::numeric_limits<ChannelType>::max()); + + float fc0 = static_cast<float>(c0) / NormVal; + float fc1 = static_cast<float>(c1) / NormVal; + float fc2 = static_cast<float>(c2) / NormVal; + float fc3 = static_cast<float>(c3) / NormVal; float fLinearAverage = (SRGBToLinear(fc0) + SRGBToLinear(fc1) + SRGBToLinear(fc2) + SRGBToLinear(fc3)) / 4.f; - float fSRGBAverage = LinearToSRGB(fLinearAverage); - Int32 SRGBAverage = static_cast<Int32>(fSRGBAverage * NormVal); - SRGBAverage = std::min(SRGBAverage, static_cast<Int32>(std::numeric_limits<ChannelType>::max())); - SRGBAverage = std::max(SRGBAverage, static_cast<Int32>(std::numeric_limits<ChannelType>::min())); - return static_cast<ChannelType>(SRGBAverage); + float fSRGBAverage = LinearToSRGB(fLinearAverage) * NormVal; + + static constexpr float MinVal = static_cast<float>(std::numeric_limits<ChannelType>::min()); + static constexpr float MaxVal = static_cast<float>(std::numeric_limits<ChannelType>::max()); + + fSRGBAverage = std::max(fSRGBAverage, MinVal); + fSRGBAverage = std::min(fSRGBAverage, MaxVal); + + return static_cast<ChannelType>(fSRGBAverage); } template <typename ChannelType> @@ -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<ChannelType*>(reinterpret_cast<Uint8*>(pCoarseMip) + row * CoarseMipStride)[col * NumChannels + c]; |
