CTextureDecoder: Make utility functions internally linked

Same behavior, but keeps implementation details internal.
This commit is contained in:
Lioncash 2020-06-14 15:41:29 -04:00
parent 849906c8c4
commit 68455c54e8
2 changed files with 126 additions and 73 deletions

View File

@ -1,46 +1,156 @@
#include "CTextureDecoder.h" #include "CTextureDecoder.h"
#include <Common/Log.h> #include <Common/Log.h>
#include <Common/CColor.h> #include <Common/CColor.h>
#include <array>
// A cleanup is warranted at some point. Trying to support both partial + full decode ended up really messy. // A cleanup is warranted at some point. Trying to support both partial + full decode ended up really messy.
namespace
{
// Number of pixels * this = number of bytes // Number of pixels * this = number of bytes
static const float gskPixelsToBytes[] = { constexpr std::array gskPixelsToBytes{
2.f, 2.f, 2.f, 2.f, 4.f, 4.f, 0.f, 2.f, 4.f, 4.f, 0.5f 2.f,
2.f,
2.f,
2.f,
4.f,
4.f,
0.f,
2.f,
4.f,
4.f,
0.5f,
}; };
// Bits per pixel for each GX texture format // Bits per pixel for each GX texture format
static const uint32 gskSourceBpp[] = { constexpr std::array gskSourceBpp{
4, 8, 8, 16, 4, 8, 16, 16, 16, 32, 4 4U,
8U,
8U,
16U,
4U,
8U,
16U,
16U,
16U,
32U,
4U,
}; };
// Bits per pixel for each GX texture format when decoded // Bits per pixel for each GX texture format when decoded
static const uint32 gskOutputBpp[] = { constexpr std::array gskOutputBpp{
16, 16, 16, 16, 16, 16, 16, 16, 32, 32, 4 16U,
16U,
16U,
16U,
16U,
16U,
16U,
16U,
32U,
32U,
4U,
}; };
// Size of one pixel in output data in bytes // Size of one pixel in output data in bytes
static const uint32 gskOutputPixelStride[] = { constexpr std::array gskOutputPixelStride{
2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 8 2U,
2U,
2U,
2U,
2U,
2U,
2U,
2U,
4U,
4U,
8U,
}; };
// Block width for each GX texture format // Block width for each GX texture format
static const uint32 gskBlockWidth[] = { constexpr std::array gskBlockWidth{
8, 8, 8, 4, 8, 8, 4, 4, 4, 4, 2 8U,
8U,
8U,
4U,
8U,
8U,
4U,
4U,
4U,
4U,
2U,
}; };
// Block height for each GX texture format // Block height for each GX texture format
static const uint32 gskBlockHeight[] = { constexpr std::array gskBlockHeight{
8, 4, 4, 4, 8, 4, 4, 4, 4, 4, 2 8U,
4U,
4U,
4U,
8U,
4U,
4U,
4U,
4U,
4U,
2U,
}; };
constexpr uint8 Extend3to8(uint8 In)
{
In &= 0x7;
return (In << 5) | (In << 2) | (In >> 1);
}
constexpr uint8 Extend4to8(uint8 In)
{
In &= 0xF;
return (In << 4) | In;
}
constexpr uint8 Extend5to8(uint8 In)
{
In &= 0x1F;
return (In << 3) | (In >> 2);
}
constexpr uint8 Extend6to8(uint8 In)
{
In &= 0x3F;
return (In << 2) | (In >> 4);
}
constexpr uint32 CalculateShiftForMask(uint32 BitMask)
{
uint32 Shift = 32;
while (BitMask)
{
BitMask <<= 1;
Shift--;
}
return Shift;
}
constexpr uint32 CalculateMaskBitCount(uint32 BitMask)
{
uint32 Count = 0;
while (BitMask)
{
if (BitMask & 0x1)
Count++;
BitMask >>= 1;
}
return Count;
}
} // Anonymous namespace
CTextureDecoder::CTextureDecoder() CTextureDecoder::CTextureDecoder()
{ {
} }
CTextureDecoder::~CTextureDecoder() CTextureDecoder::~CTextureDecoder() = default;
{
}
std::unique_ptr<CTexture> CTextureDecoder::CreateTexture() std::unique_ptr<CTexture> CTextureDecoder::CreateTexture()
{ {
@ -848,52 +958,3 @@ CColor CTextureDecoder::DecodeDDSPixel(IInputStream& /*rDDS*/)
{ {
return CColor::skWhite; return CColor::skWhite;
} }
// ************ UTILITY ************
uint8 CTextureDecoder::Extend3to8(uint8 In)
{
In &= 0x7;
return (In << 5) | (In << 2) | (In >> 1);
}
uint8 CTextureDecoder::Extend4to8(uint8 In)
{
In &= 0xF;
return (In << 4) | In;
}
uint8 CTextureDecoder::Extend5to8(uint8 In)
{
In &= 0x1F;
return (In << 3) | (In >> 2);
}
uint8 CTextureDecoder::Extend6to8(uint8 In)
{
In &= 0x3F;
return (In << 2) | (In >> 4);
}
uint32 CTextureDecoder::CalculateShiftForMask(uint32 BitMask)
{
uint32 Shift = 32;
while (BitMask)
{
BitMask <<= 1;
Shift--;
}
return Shift;
}
uint32 CTextureDecoder::CalculateMaskBitCount(uint32 BitMask)
{
uint32 Count = 0;
while (BitMask)
{
if (BitMask & 0x1) Count++;
BitMask >>= 1;
}
return Count;
}

View File

@ -83,14 +83,6 @@ public:
static std::unique_ptr<CTexture> LoadDDS(IInputStream& rDDS, CResourceEntry *pEntry); static std::unique_ptr<CTexture> LoadDDS(IInputStream& rDDS, CResourceEntry *pEntry);
static std::unique_ptr<CTexture> DoFullDecode(IInputStream& rTXTR, CResourceEntry *pEntry); static std::unique_ptr<CTexture> DoFullDecode(IInputStream& rTXTR, CResourceEntry *pEntry);
static CTexture* DoFullDecode(CTexture *pTexture); static CTexture* DoFullDecode(CTexture *pTexture);
// Utility
static uint8 Extend3to8(uint8 In);
static uint8 Extend4to8(uint8 In);
static uint8 Extend5to8(uint8 In);
static uint8 Extend6to8(uint8 In);
static uint32 CalculateShiftForMask(uint32 BitMask);
static uint32 CalculateMaskBitCount(uint32 BitMask);
}; };
#endif // CTEXTUREDECODER_H #endif // CTEXTUREDECODER_H