From dea7e6a4e9055904a99fb7613b5a882ad1daab9c Mon Sep 17 00:00:00 2001 From: David Williams Date: Tue, 16 Jul 2013 16:50:04 +0200 Subject: [PATCH] Tidying up block classes. --- .../include/PolyVoxCore/Impl/Block.h | 20 ++++++++-- .../include/PolyVoxCore/Impl/Block.inl | 39 ++++++++++--------- .../include/PolyVoxCore/LargeVolume.inl | 2 +- .../PolyVoxCore/LargeVolumeSampler.inl | 2 +- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h index 32cb9857..e78ecb48 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.h @@ -47,13 +47,19 @@ namespace PolyVox } protected: + // This is updated by the LargeVolume and used to discard the least recently used blocks. uint32_t m_uBlockLastAccessed; + + // This is so we can tell whether a uncompressed block has to be recompressed and whether + // a compressed block has to be paged back to disk, or whether they can just be discarded. bool m_bDataModified; }; template class CompressedBlock : public Block { + friend LargeVolume; + public: CompressedBlock(); ~CompressedBlock(); @@ -63,9 +69,11 @@ namespace PolyVox void setData(const uint8_t* const pData, uint32_t uDataSizeInBytes); + private: + // Made this private to avoid any confusion with getDataSizeInBytes(). + // Users shouldn't really need this for CompressedBlock anyway. uint32_t calculateSizeInBytes(void); - private: uint8_t* m_pData; uint32_t m_uDataSizeInBytes; }; @@ -73,18 +81,24 @@ namespace PolyVox template class UncompressedBlock : public Block { + friend LargeVolume; + public: UncompressedBlock(uint16_t uSideLength); ~UncompressedBlock(); - uint16_t getSideLength(void) const; VoxelType getVoxel(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const; VoxelType getVoxel(const Vector3DUint16& v3dPos) const; void setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue); void setVoxelAt(const Vector3DUint16& v3dPos, VoxelType tValue); - VoxelType* m_tUncompressedData; + private: + // Made this private for consistancy with CompressedBlock. + // Users shouldn't really need this for UncompressedBlock anyway. + uint32_t calculateSizeInBytes(void); + + VoxelType* m_tData; uint16_t m_uSideLength; uint8_t m_uSideLengthPower; }; diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl index b59d6f0a..dab2f35d 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Block.inl @@ -86,8 +86,7 @@ namespace PolyVox template uint32_t CompressedBlock::calculateSizeInBytes(void) { - // 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. + // Returns the size of this class plus the size of the compressed data. uint32_t uSizeInBytes = sizeof(CompressedBlock) + m_uDataSizeInBytes; return uSizeInBytes; } @@ -96,7 +95,7 @@ namespace PolyVox template UncompressedBlock::UncompressedBlock(uint16_t uSideLength) - :m_tUncompressedData(0) + :m_tData(0) ,m_uSideLength(0) ,m_uSideLengthPower(0) { @@ -106,32 +105,27 @@ namespace PolyVox // Allocate the data const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength; - m_tUncompressedData = new VoxelType[uNoOfVoxels]; + m_tData = new VoxelType[uNoOfVoxels]; } template UncompressedBlock::~UncompressedBlock() { - delete m_tUncompressedData; - m_tUncompressedData = 0; - } - - template - uint16_t UncompressedBlock::getSideLength(void) const - { - return m_uSideLength; + delete m_tData; + m_tData = 0; } template VoxelType UncompressedBlock::getVoxel(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos) const { - // This is internal code not directly called by the user. For efficiency we assert rather than throwing. + // This code is not usually expected to be called by the user, with the exception of when implementing paging + // of uncompressed data. It's a performance critical code path so we use asserts rather than exceptions. POLYVOX_ASSERT(uXPos < m_uSideLength, "Supplied position is outside of the block"); POLYVOX_ASSERT(uYPos < m_uSideLength, "Supplied position is outside of the block"); POLYVOX_ASSERT(uZPos < m_uSideLength, "Supplied position is outside of the block"); - POLYVOX_ASSERT(m_tUncompressedData, "No uncompressed data - block must be decompressed before accessing voxels."); + POLYVOX_ASSERT(m_tData, "No uncompressed data - block must be decompressed before accessing voxels."); - return m_tUncompressedData + return m_tData [ uXPos + uYPos * m_uSideLength + @@ -148,13 +142,14 @@ namespace PolyVox template void UncompressedBlock::setVoxelAt(uint16_t uXPos, uint16_t uYPos, uint16_t uZPos, VoxelType tValue) { - // This is internal code not directly called by the user. For efficiency we assert rather than throwing. + // This code is not usually expected to be called by the user, with the exception of when implementing paging + // of uncompressed data. It's a performance critical code path so we use asserts rather than exceptions. POLYVOX_ASSERT(uXPos < m_uSideLength, "Supplied position is outside of the block"); POLYVOX_ASSERT(uYPos < m_uSideLength, "Supplied position is outside of the block"); POLYVOX_ASSERT(uZPos < m_uSideLength, "Supplied position is outside of the block"); - POLYVOX_ASSERT(m_tUncompressedData, "No uncompressed data - block must be decompressed before accessing voxels."); + POLYVOX_ASSERT(m_tData, "No uncompressed data - block must be decompressed before accessing voxels."); - m_tUncompressedData + m_tData [ uXPos + uYPos * m_uSideLength + @@ -169,4 +164,12 @@ namespace PolyVox { setVoxelAt(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), tValue); } + + template + uint32_t UncompressedBlock::calculateSizeInBytes(void) + { + // Returns the size of this class plus the size of the uncompressed data. + uint32_t uSizeInBytes = sizeof(UncompressedBlock) + (m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType)); + return uSizeInBytes; + } } diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index fdf4bcac..731a6c01 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -642,7 +642,7 @@ namespace PolyVox UncompressedBlock* pUncompressedBlock = new UncompressedBlock(m_uBlockSideLength); const void* pSrcData = reinterpret_cast(block->getData()); - void* pDstData = reinterpret_cast(pUncompressedBlock->m_tUncompressedData); + void* pDstData = reinterpret_cast(pUncompressedBlock->m_tData); uint32_t uSrcLength = block->getDataSizeInBytes(); uint32_t uDstLength = m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength * sizeof(VoxelType); diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl index 8bbad0f4..c5e4e12c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolumeSampler.inl @@ -121,7 +121,7 @@ namespace PolyVox UncompressedBlock* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); - mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; + mCurrentVoxel = pUncompressedCurrentBlock->m_tData + uVoxelIndexInBlock; } else {