Tidying up and refactoring of block class.

This commit is contained in:
Daviw Williams 2013-06-25 16:45:53 +02:00
parent c346d19d77
commit baed7ddccc
4 changed files with 29 additions and 30 deletions

View File

@ -38,7 +38,7 @@ namespace PolyVox
class Block
{
public:
Block(uint16_t uSideLength = 0);
Block(uint16_t uSideLength, Compressor* pCompressor);
const uint8_t* const getCompressedData(void) const;
const uint32_t getCompressedDataLength(void) const;
@ -56,9 +56,10 @@ namespace PolyVox
uint32_t calculateSizeInBytes(void);
public:
void compress(Compressor* pCompressor);
void uncompress(Compressor* pCompressor);
void compress();
void uncompress();
Compressor* m_pCompressor;
uint8_t* m_pCompressedData;
uint32_t m_uCompressedDataLength;
VoxelType* m_tUncompressedData;

View File

@ -36,8 +36,9 @@ freely, subject to the following restrictions:
namespace PolyVox
{
template <typename VoxelType>
Block<VoxelType>::Block(uint16_t uSideLength)
:m_pCompressedData(0)
Block<VoxelType>::Block(uint16_t uSideLength, Compressor* pCompressor)
:m_pCompressor(pCompressor)
,m_pCompressedData(0)
,m_uCompressedDataLength(0)
,m_tUncompressedData(0)
,m_uSideLength(0)
@ -47,7 +48,7 @@ namespace PolyVox
{
if(uSideLength == 0)
{
POLYVOX_THROW(std::invalid_argument, "Block side cannot be zero.");
POLYVOX_THROW(std::invalid_argument, "Block side length cannot be zero.");
}
if(!isPowerOf2(uSideLength))
@ -55,6 +56,11 @@ namespace PolyVox
POLYVOX_THROW(std::invalid_argument, "Block side length must be a power of two.");
}
if(pCompressor == 0)
{
POLYVOX_THROW(std::invalid_argument, "Block must be provided with a valid compressor.");
}
//Compute the side length
m_uSideLength = uSideLength;
m_uSideLengthPower = logBase2(uSideLength);
@ -169,18 +175,13 @@ namespace PolyVox
}
template <typename VoxelType>
void Block<VoxelType>::compress(Compressor* pCompressor)
void Block<VoxelType>::compress()
{
if(m_bIsCompressed)
{
POLYVOX_THROW(invalid_operation, "Attempted to compress block which is already flagged as compressed.");
}
if(!pCompressor)
{
POLYVOX_THROW(std::invalid_argument, "A valid compressor must be provided");
}
POLYVOX_ASSERT(m_tUncompressedData != 0, "No uncompressed data is present.");
//If the uncompressed data hasn't actually been
@ -202,7 +203,7 @@ namespace PolyVox
try
{
uCompressedLength = pCompressor->compress(pSrcData, uSrcLength, pDstData, uDstLength);
uCompressedLength = m_pCompressor->compress(pSrcData, uSrcLength, pDstData, uDstLength);
// Create new compressed data and copy across
m_pCompressedData = new uint8_t[uCompressedLength];
@ -214,7 +215,9 @@ namespace PolyVox
// It is possible for the compression to fail. A common cause for this would be if the destination
// buffer is not big enough. So now we try again using a buffer that is definitely big enough.
// Note that ideally we will choose our earlier buffer size so that this almost never happens.
uint32_t uMaxCompressedSize = pCompressor->getMaxCompressedSize(uSrcLength);
logWarning() << "The compressor failed to compress the block, proabaly due to the buffer being too small.";
logWarning() << "The compression will be tried again with a larger buffer";
uint32_t uMaxCompressedSize = m_pCompressor->getMaxCompressedSize(uSrcLength);
uint8_t* buffer = new uint8_t[ uMaxCompressedSize ];
pDstData = reinterpret_cast<void*>( buffer );
@ -222,7 +225,7 @@ namespace PolyVox
try
{
uCompressedLength = pCompressor->compress(pSrcData, uSrcLength, pDstData, uDstLength);
uCompressedLength = m_pCompressor->compress(pSrcData, uSrcLength, pDstData, uDstLength);
// Create new compressed data and copy across
m_pCompressedData = new uint8_t[uCompressedLength];
@ -248,18 +251,13 @@ namespace PolyVox
}
template <typename VoxelType>
void Block<VoxelType>::uncompress(Compressor* pCompressor)
void Block<VoxelType>::uncompress()
{
if(!m_bIsCompressed)
{
POLYVOX_THROW(invalid_operation, "Attempted to uncompress block which is not flagged as compressed.");
}
if(!pCompressor)
{
POLYVOX_THROW(std::invalid_argument, "A valid compressor must be provided");
}
POLYVOX_ASSERT(m_tUncompressedData == 0, "Uncompressed data already exists.");
m_tUncompressedData = new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength];
@ -271,7 +269,7 @@ namespace PolyVox
//MinizCompressor compressor;
//RLECompressor<VoxelType, uint16_t> compressor;
uint32_t uUncompressedLength = pCompressor->decompress(pSrcData, uSrcLength, pDstData, uDstLength);
uint32_t uUncompressedLength = m_pCompressor->decompress(pSrcData, uSrcLength, pDstData, uDstLength);
POLYVOX_ASSERT(uUncompressedLength == m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType), "Destination length has changed.");

View File

@ -234,8 +234,8 @@ namespace PolyVox
struct LoadedBlock
{
public:
LoadedBlock(uint16_t uSideLength = 0)
:block(uSideLength)
LoadedBlock(uint16_t uSideLength, Compressor* pCompressor)
:block(uSideLength, pCompressor)
,timestamp(0)
{
}

View File

@ -471,7 +471,7 @@ namespace PolyVox
{
for(uint32_t ct = 0; ct < m_vecUncompressedBlockCache.size(); ct++)
{
m_vecUncompressedBlockCache[ct]->block.compress(m_pCompressor);
m_vecUncompressedBlockCache[ct]->block.compress();
}
m_vecUncompressedBlockCache.clear();
}
@ -552,7 +552,7 @@ namespace PolyVox
if(m_vecUncompressedBlockCache[ct] == &(itBlock->second))
{
// TODO: compression is unneccessary? or will not compressing this cause a memleak?
itBlock->second.block.compress(m_pCompressor);
itBlock->second.block.compress();
// put last object in cache here
m_vecUncompressedBlockCache[ct] = m_vecUncompressedBlockCache.back();
// decrease cache size by one since last element is now in here twice
@ -630,12 +630,12 @@ namespace PolyVox
}
// create the new block
LoadedBlock newBlock(m_uBlockSideLength);
LoadedBlock newBlock(m_uBlockSideLength, m_pCompressor);
// Blocks start out compressed - should we change this?
// Or maybe we should just 'seed' them with compressed data,
// rather than creating an empty block and then compressing?
newBlock.block.compress(m_pCompressor);
newBlock.block.compress();
itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, newBlock)).first;
@ -688,7 +688,7 @@ namespace PolyVox
}
//Compress the least recently used block.
m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex]->block.compress(m_pCompressor);
m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex]->block.compress();
//We don't actually remove any elements from this vector, we
//simply change the pointer to point at the new uncompressed bloack.
@ -699,7 +699,7 @@ namespace PolyVox
m_vecUncompressedBlockCache.push_back(&loadedBlock);
}
loadedBlock.block.uncompress(m_pCompressor);
loadedBlock.block.uncompress();
m_pLastAccessedBlock = &(loadedBlock.block);
POLYVOX_ASSERT(m_pLastAccessedBlock->m_tUncompressedData, "Block has no uncompressed data");