diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h index 8bc6c5fd..2d4cfeee 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h @@ -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; diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl index edba9045..8a75beab 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl @@ -36,8 +36,9 @@ freely, subject to the following restrictions: namespace PolyVox { template - Block::Block(uint16_t uSideLength) - :m_pCompressedData(0) + Block::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 - void Block::compress(Compressor* pCompressor) + void Block::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( 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 - void Block::uncompress(Compressor* pCompressor) + void Block::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 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."); diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h index 6de905b3..8e46cb79 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h @@ -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) { } diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index 6322135b..9fc1d7c1 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -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");