diff --git a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h index 80c4f05a..66091d9c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h @@ -77,7 +77,7 @@ namespace PolyVox uint8_t* buffer = new uint8_t[fileSizeInBytes]; fread(buffer, sizeof(uint8_t), fileSizeInBytes, pFile); - pBlockData->setCompressedData(buffer, fileSizeInBytes); + pBlockData->setData(buffer, fileSizeInBytes); delete[] buffer; if(ferror(pFile)) @@ -115,7 +115,7 @@ namespace PolyVox POLYVOX_THROW(std::runtime_error, "Unable to open file to write out block data."); } - fwrite(pBlockData->getCompressedData(), sizeof(uint8_t), pBlockData->getCompressedDataLength(), pFile); + fwrite(pBlockData->getData(), sizeof(uint8_t), pBlockData->getDataSizeInBytes(), pFile); if(ferror(pFile)) { diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h index edd3a893..2b9893b5 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h @@ -35,26 +35,42 @@ freely, subject to the following restrictions: namespace PolyVox { template - class CompressedBlock + class Block + { + friend LargeVolume; + + public: + Block() + :m_uBlockLastAccessed(0) + ,m_bDataModified(false) + { + } + + protected: + uint32_t m_uBlockLastAccessed; + bool m_bDataModified; + }; + + template + class CompressedBlock : public Block { public: - CompressedBlock(uint16_t uSideLength, Compressor* pCompressor); + CompressedBlock(); - const uint8_t* getCompressedData(void) const; - uint32_t getCompressedDataLength(void) const; + const uint8_t* getData(void) const; + uint32_t getDataSizeInBytes(void) const; - void setCompressedData(const uint8_t* const data, uint32_t dataLength); + void setData(const uint8_t* const pData, uint32_t uDataSizeInBytes); uint32_t calculateSizeInBytes(void); public: - uint8_t* m_pCompressedData; - uint32_t m_uCompressedDataLength; - uint32_t timestamp; + uint8_t* m_pData; + uint32_t m_uDataSizeInBytes; }; template - class UncompressedBlock + class UncompressedBlock : public Block { public: UncompressedBlock(uint16_t uSideLength); @@ -70,7 +86,6 @@ namespace PolyVox VoxelType* m_tUncompressedData; uint16_t m_uSideLength; uint8_t m_uSideLengthPower; - bool m_bIsUncompressedDataModified; }; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl index 19f50146..492d829f 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl @@ -38,52 +38,39 @@ namespace PolyVox //////////////////////////////////////////////////////////////////////////////// template - CompressedBlock::CompressedBlock(uint16_t uSideLength, Compressor* pCompressor) - :m_pCompressedData(0) - ,m_uCompressedDataLength(0) + CompressedBlock::CompressedBlock() + :m_pData(0) + ,m_uDataSizeInBytes(0) { - if(uSideLength == 0) - { - POLYVOX_THROW(std::invalid_argument, "Block side length cannot be zero."); - } - - if(!isPowerOf2(uSideLength)) - { - 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."); - } - } template - const uint8_t* CompressedBlock::getCompressedData(void) const + const uint8_t* CompressedBlock::getData(void) const { - POLYVOX_ASSERT(m_pCompressedData, "Compressed data is NULL"); - return m_pCompressedData; + return m_pData; } template - uint32_t CompressedBlock::getCompressedDataLength(void) const + uint32_t CompressedBlock::getDataSizeInBytes(void) const { - POLYVOX_ASSERT(m_pCompressedData, "Compressed data is NULL"); - return m_uCompressedDataLength; + return m_uDataSizeInBytes; } template - void CompressedBlock::setCompressedData(const uint8_t* const data, uint32_t dataLength) + void CompressedBlock::setData(const uint8_t* const pData, uint32_t uDataSizeInBytes) { - //POLYVOX_ASSERT(m_pCompressedData, "Compressed data is NULL"); - POLYVOX_ASSERT(m_pCompressedData != data, "Attempting to copy data onto itself"); + POLYVOX_THROW_IF(pData == 0, std::invalid_argument, "Pointer to data cannot be null"); + POLYVOX_THROW_IF(m_pData == pData, std::invalid_argument, "Attempting to copy data onto itself"); - delete[] m_pCompressedData; + // Delete any existing data + delete[] m_pData; - m_uCompressedDataLength = dataLength; - m_pCompressedData = new uint8_t[dataLength]; - memcpy(m_pCompressedData, data, dataLength); + // Allocate new data + m_uDataSizeInBytes = uDataSizeInBytes; + m_pData = new uint8_t[uDataSizeInBytes]; + + // Copy the data across + memcpy(m_pData, pData, uDataSizeInBytes); } template @@ -91,7 +78,7 @@ namespace PolyVox { // Returns the size of this class plus the size of the compressed data. It // doesn't include the uncompressed data cache as that is owned by the volume. - uint32_t uSizeInBytes = sizeof(CompressedBlock) + m_uCompressedDataLength; + uint32_t uSizeInBytes = sizeof(CompressedBlock) + m_uDataSizeInBytes; return uSizeInBytes; } @@ -102,7 +89,6 @@ namespace PolyVox :m_tUncompressedData(0) ,m_uSideLength(0) ,m_uSideLengthPower(0) - ,m_bIsUncompressedDataModified(true) { // Compute the side length m_uSideLength = uSideLength; @@ -165,7 +151,7 @@ namespace PolyVox uZPos * m_uSideLength * m_uSideLength ] = tValue; - m_bIsUncompressedDataModified = true; + m_bDataModified = true; } template diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index 3357f60c..ce482006 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -596,7 +596,7 @@ namespace PolyVox if(itBlock == m_pBlocks.end()) { //The block is not in the map, so we will have to create a new block and add it. - CompressedBlock newBlock(m_uBlockSideLength, m_pCompressor); + CompressedBlock newBlock; itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, newBlock)).first; // Now use the pager to fill the block with it's initial data. @@ -611,7 +611,7 @@ namespace PolyVox //Get the block and mark that we accessed it CompressedBlock& block = itBlock->second; - block.timestamp = ++m_uTimestamper; + block.m_uBlockLastAccessed = ++m_uTimestamper; //m_v3dLastAccessedBlockPos = v3dBlockPos; return █ @@ -641,9 +641,9 @@ namespace PolyVox { UncompressedBlock* pUncompressedBlock = new UncompressedBlock(m_uBlockSideLength); - void* pSrcData = reinterpret_cast(block->m_pCompressedData); + void* pSrcData = reinterpret_cast(block->m_pData); void* pDstData = reinterpret_cast(pUncompressedBlock->m_tUncompressedData); - uint32_t uSrcLength = block->m_uCompressedDataLength; + uint32_t uSrcLength = block->m_uDataSizeInBytes; uint32_t uDstLength = m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength * sizeof(VoxelType); //MinizCompressor compressor; @@ -724,7 +724,7 @@ namespace PolyVox typename std::map, BlockPositionCompare>::iterator itUnloadBlock = m_pBlocks.begin(); for(i = m_pBlocks.begin(); i != m_pBlocks.end(); i++) { - if(i->second.timestamp < itUnloadBlock->second.timestamp) + if(i->second.m_uBlockLastAccessed < itUnloadBlock->second.m_uBlockLastAccessed) { itUnloadBlock = i; }