From 80e44f29d72cdbcacc991b95ed40dc4e57b3b945 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 29 Nov 2020 10:56:53 -0800 Subject: Another minor update to SRGBAverage --- Graphics/GraphicsTools/src/GraphicsUtilities.cpp | 3 ++- .../DiligentCoreTest/src/GraphicsAccessories/GraphicsUtilitiesTest.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp index 93e188fd..664749b2 100644 --- a/Graphics/GraphicsTools/src/GraphicsUtilities.cpp +++ b/Graphics/GraphicsTools/src/GraphicsUtilities.cpp @@ -149,7 +149,8 @@ ChannelType SRGBAverage(ChannelType c0, ChannelType c1, ChannelType c2, ChannelT float fLinearAverage = (FastSRGBToLinear(fc0) + FastSRGBToLinear(fc1) + FastSRGBToLinear(fc2) + FastSRGBToLinear(fc3)) * 0.25f; float fSRGBAverage = FastLinearToSRGB(fLinearAverage) * MaxVal; - // Clamping by MaxVal is essential because fast SRGB math is imprecise + // Clamping on both ends is essential because fast SRGB math is imprecise + fSRGBAverage = std::max(fSRGBAverage, 0.f); fSRGBAverage = std::min(fSRGBAverage, MaxVal); return static_cast(fSRGBAverage); diff --git a/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsUtilitiesTest.cpp b/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsUtilitiesTest.cpp index 50b4cb47..1aabca1c 100644 --- a/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsUtilitiesTest.cpp +++ b/Tests/DiligentCoreTest/src/GraphicsAccessories/GraphicsUtilitiesTest.cpp @@ -488,7 +488,8 @@ TEST(GraphicsTools_CalculateMipLevel, sRGB) FastSRGBToLinear(FineData[((x * 2 + 0) + (y * 2 + 1) * FineWidth) * NumChannels + c] / 255.f) + FastSRGBToLinear(FineData[((x * 2 + 1) + (y * 2 + 1) * FineWidth) * NumChannels + c] / 255.f)) * 0.25f; - float fSRGB = FastLinearToSRGB(fLinearAverage); + fLinearAverage = std::min(std::max(fLinearAverage, 0.f), 255.f); + float fSRGB = FastLinearToSRGB(fLinearAverage); RefCoarseData[(x + y * CoarseWidth) * NumChannels + c] = static_cast(fSRGB * 255.f); } -- cgit v1.2.3