From ba30da0945c266fa8e765ba812d8d90b9d29796a Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Tue, 3 Jul 2018 19:33:07 -0700 Subject: [PATCH] BC7 CPU codec optimization to skip mode 7 for opaque blocks --- DirectXTex/BC6HBC7.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/DirectXTex/BC6HBC7.cpp b/DirectXTex/BC6HBC7.cpp index ef752e9..594d22a 100644 --- a/DirectXTex/BC6HBC7.cpp +++ b/DirectXTex/BC6HBC7.cpp @@ -2733,6 +2733,7 @@ void D3DX_BC7::Encode(DWORD flags, const HDRColorA* const pIn) D3DX_BC7 final = *this; EncodeParams EP(pIn); float fMSEBest = FLT_MAX; + uint32_t alphaMask = 0xFF; for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i) { @@ -2740,8 +2741,11 @@ void D3DX_BC7::Encode(DWORD flags, const HDRColorA* const pIn) EP.aLDRPixels[i].g = uint8_t(std::max(0.0f, std::min(255.0f, pIn[i].g * 255.0f + 0.01f))); EP.aLDRPixels[i].b = uint8_t(std::max(0.0f, std::min(255.0f, pIn[i].b * 255.0f + 0.01f))); EP.aLDRPixels[i].a = uint8_t(std::max(0.0f, std::min(255.0f, pIn[i].a * 255.0f + 0.01f))); + alphaMask &= EP.aLDRPixels[i].a; } + const bool bHasAlpha = (alphaMask != 0xFF); + for (EP.uMode = 0; EP.uMode < 8 && fMSEBest > 0; ++EP.uMode) { if (!(flags & BC_FLAGS_USE_3SUBSETS) && (EP.uMode == 0 || EP.uMode == 2)) @@ -2756,6 +2760,12 @@ void D3DX_BC7::Encode(DWORD flags, const HDRColorA* const pIn) continue; } + if ((!bHasAlpha) && (EP.uMode == 7)) + { + // There is no value in using mode 7 for completely opaque blocks (the other 2 subset modes handle this case for opaque blocks), so skip it for a small perf win. + continue; + } + const size_t uShapes = size_t(1) << ms_aInfo[EP.uMode].uPartitionBits; assert(uShapes <= BC7_MAX_SHAPES); _Analysis_assume_(uShapes <= BC7_MAX_SHAPES);