From a68e81279685b08564c43d2b55ed7c2b77317a9a Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 29 Nov 2020 11:02:31 -0800 Subject: TextureLoader: using ComputeMipLevel function --- TextureLoader/src/TextureLoader.cpp | 94 ++----------------------------------- 1 file changed, 4 insertions(+), 90 deletions(-) (limited to 'TextureLoader/src') diff --git a/TextureLoader/src/TextureLoader.cpp b/TextureLoader/src/TextureLoader.cpp index 48519a3..3c12a0f 100644 --- a/TextureLoader/src/TextureLoader.cpp +++ b/TextureLoader/src/TextureLoader.cpp @@ -33,6 +33,7 @@ #include "TextureLoader.h" #include "GraphicsAccessories.hpp" +#include "GraphicsUtilities.h" #include "DDSLoader.h" #include "PNGCodec.h" #include "JPEGCodec.h" @@ -66,80 +67,6 @@ extern "C" namespace Diligent { -template -ChannelType SRGBAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelType c3) -{ - 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) * 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 -ChannelType LinearAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelType c3) -{ - static_assert(std::numeric_limits::is_integer && !std::numeric_limits::is_signed, "Unsigned integers are expected"); - return static_cast((static_cast(c0) + static_cast(c1) + static_cast(c2) + static_cast(c3)) / 4); -} - -template -void ComputeCoarseMip(Uint32 NumChannels, - bool IsSRGB, - const void* pFineMip, - Uint32 FineMipStride, - Uint32 FineMipWidth, - Uint32 FineMipHeight, - void* pCoarseMip, - Uint32 CoarseMipStride, - Uint32 CoarseMipWidth, - Uint32 CoarseMipHeight) -{ - VERIFY_EXPR(FineMipWidth > 0 && FineMipHeight > 0 && FineMipStride > 0); - VERIFY_EXPR(CoarseMipWidth > 0 && CoarseMipHeight > 0 && CoarseMipStride > 0); - - for (Uint32 row = 0; row < CoarseMipHeight; ++row) - { - auto src_row0 = row * 2; - auto src_row1 = std::min(row * 2 + 1, FineMipHeight - 1); - - auto pSrcRow0 = reinterpret_cast(reinterpret_cast(pFineMip) + src_row0 * FineMipStride); - auto pSrcRow1 = reinterpret_cast(reinterpret_cast(pFineMip) + src_row1 * FineMipStride); - - for (Uint32 col = 0; col < CoarseMipWidth; ++col) - { - auto src_col0 = col * 2; - auto src_col1 = std::min(col * 2 + 1, FineMipWidth - 1); - - for (Uint32 c = 0; c < NumChannels; ++c) - { - auto Chnl00 = pSrcRow0[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]; - if (IsSRGB) - DstCol = SRGBAverage(Chnl00, Chnl01, Chnl10, Chnl11); - else - DstCol = LinearAverage(Chnl00, Chnl01, Chnl10, Chnl11); - } - } - } -} - template void RGBToRGBA(const void* pRGBData, Uint32 RGBStride, @@ -259,22 +186,9 @@ void CreateTextureFromImage(Image* pSrcImage, if (TexLoadInfo.GenerateMips) { - if (ChannelDepth == 8) - { - ComputeCoarseMip(NumComponents, IsSRGB, - pSubResources[m - 1].pData, pSubResources[m - 1].Stride, - MipWidth, MipHeight, - Mips[m].data(), CoarseMipStride, - CoarseMipWidth, CoarseMipHeight); - } - else if (ChannelDepth == 16) - { - ComputeCoarseMip(NumComponents, IsSRGB, - pSubResources[m - 1].pData, pSubResources[m - 1].Stride, - MipWidth, MipHeight, - Mips[m].data(), CoarseMipStride, - CoarseMipWidth, CoarseMipHeight); - } + ComputeMipLevel(MipWidth, MipHeight, TexDesc.Format, + pSubResources[m - 1].pData, pSubResources[m - 1].Stride, + Mips[m].data(), CoarseMipStride); } pSubResources[m].pData = Mips[m].data(); -- cgit v1.2.3