From 5b59bc2d8c07fe0455eafced9d86462daa91d4c3 Mon Sep 17 00:00:00 2001 From: David Williams Date: Wed, 17 Sep 2014 13:35:43 +0200 Subject: [PATCH] Moved paging code into block constructor/destructor. --- examples/Paging/main.cpp | 4 +-- .../include/PolyVoxCore/FilePager.h | 4 +-- .../include/PolyVoxCore/LargeVolume.inl | 27 ++-------------- .../PolyVoxCore/include/PolyVoxCore/Pager.h | 4 +-- .../include/PolyVoxCore/UncompressedBlock.h | 6 ++-- .../include/PolyVoxCore/UncompressedBlock.inl | 31 +++++++++++++++++-- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/examples/Paging/main.cpp b/examples/Paging/main.cpp index 04d20067..a01779fb 100644 --- a/examples/Paging/main.cpp +++ b/examples/Paging/main.cpp @@ -90,7 +90,7 @@ public: /// Destructor virtual ~PerlinNoisePager() {}; - virtual void pageIn(const PolyVox::Region& region, std::shared_ptr< UncompressedBlock > pBlockData) + virtual void pageIn(const PolyVox::Region& region, UncompressedBlock* pBlockData) { // FIXME - this isn't a great example... it's a shame we have to hard clode the block size and also create/destroy // a compressor each time. These could at least be moved outside somewhere if we can't fix it in a better way... @@ -143,7 +143,7 @@ public: //delete compressor; } - virtual void pageOut(const PolyVox::Region& region, std::shared_ptr< UncompressedBlock > /*pBlockData*/) + virtual void pageOut(const PolyVox::Region& region, UncompressedBlock* /*pBlockData*/) { std::cout << "warning unloading region: " << region.getLowerCorner() << " -> " << region.getUpperCorner() << std::endl; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h index 8da3adcf..8e3e36ed 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/FilePager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/FilePager.h @@ -69,7 +69,7 @@ namespace PolyVox m_vecCreatedFiles.clear(); } - virtual void pageIn(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) + virtual void pageIn(const Region& region, UncompressedBlock* pBlockData) { POLYVOX_ASSERT(pBlockData, "Attempting to page in NULL block"); //POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data"); @@ -113,7 +113,7 @@ namespace PolyVox } } - virtual void pageOut(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) + virtual void pageOut(const Region& region, UncompressedBlock* pBlockData) { POLYVOX_ASSERT(pBlockData, "Attempting to page out NULL block"); //POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data"); diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index 47068b36..218bff10 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -515,21 +515,7 @@ namespace PolyVox // Before deleting the block we may need to recompress its data. We // only do this if the data has been modified since it was decompressed. - if(pUncompressedBlock->m_bDataModified) - { - // Get the compressed block which we will copy the data back in to. - Vector3DInt32 v3dBlockPos = itUncompressedBlock->first; - - // From the coordinates of the block we deduce the coordinates of the contained voxels. - Vector3DInt32 v3dLower(v3dBlockPos.getX() << m_uBlockSideLengthPower, v3dBlockPos.getY() << m_uBlockSideLengthPower, v3dBlockPos.getZ() << m_uBlockSideLengthPower); - Vector3DInt32 v3dUpper = v3dLower + Vector3DInt32(m_uBlockSideLength - 1, m_uBlockSideLength - 1, m_uBlockSideLength - 1); - - // Page the data out - m_pPager->pageOut(Region(v3dLower, v3dUpper), itUncompressedBlock->second); - - // The compressed data has been updated, so the uncompressed data is no longer modified with respect to it. - pUncompressedBlock->m_bDataModified = false; - } + //delete itUncompressedBlock->second; @@ -564,16 +550,7 @@ namespace PolyVox else { // The blcok was not found so we will create a new one. - pUncompressedBlock = std::make_shared< UncompressedBlock >(m_uBlockSideLength); - - // Pass the block to the Pager to give it a chance to initialise it with any data - if (m_pPager) - { - Vector3DInt32 v3dLower(uBlockX << m_uBlockSideLengthPower, uBlockY << m_uBlockSideLengthPower, uBlockZ << m_uBlockSideLengthPower); - Vector3DInt32 v3dUpper = v3dLower + Vector3DInt32(m_uBlockSideLength - 1, m_uBlockSideLength - 1, m_uBlockSideLength - 1); - Region reg(v3dLower, v3dUpper); - m_pPager->pageIn(reg, pUncompressedBlock); - } + pUncompressedBlock = std::make_shared< UncompressedBlock >(v3dBlockPos, m_uBlockSideLength, m_pPager); while (m_pBlocks.size() + 1 > m_uMaxNumberOfUncompressedBlocks) // +1 ready for new block we will add next. { diff --git a/library/PolyVoxCore/include/PolyVoxCore/Pager.h b/library/PolyVoxCore/include/PolyVoxCore/Pager.h index 0970c943..6b670302 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Pager.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Pager.h @@ -43,8 +43,8 @@ namespace PolyVox /// Destructor virtual ~Pager() {}; - virtual void pageIn(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) = 0; - virtual void pageOut(const Region& region, std::shared_ptr< UncompressedBlock > pBlockData) = 0; + virtual void pageIn(const Region& region, UncompressedBlock* pBlockData) = 0; + virtual void pageOut(const Region& region, UncompressedBlock* pBlockData) = 0; }; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.h b/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.h index a0ed3c75..c8479322 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.h +++ b/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.h @@ -35,7 +35,7 @@ namespace PolyVox friend class LargeVolume; public: - UncompressedBlock(uint16_t uSideLength); + UncompressedBlock(Vector3DInt32 v3dPosition, uint16_t uSideLength, Pager* pPager = nullptr); ~UncompressedBlock(); VoxelType* getData(void) const; @@ -67,7 +67,9 @@ namespace PolyVox VoxelType* m_tData; uint16_t m_uSideLength; - uint8_t m_uSideLengthPower; + uint8_t m_uSideLengthPower; + Pager* m_pPager; + Vector3DInt32 m_v3dBlockSpacePosition; }; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.inl b/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.inl index d5252d2c..401ff40e 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/UncompressedBlock.inl @@ -26,12 +26,14 @@ freely, subject to the following restrictions: namespace PolyVox { template - UncompressedBlock::UncompressedBlock(uint16_t uSideLength) + UncompressedBlock::UncompressedBlock(Vector3DInt32 v3dPosition, uint16_t uSideLength, Pager* pPager) :m_uBlockLastAccessed(0) ,m_bDataModified(true) ,m_tData(0) ,m_uSideLength(0) ,m_uSideLengthPower(0) + ,m_pPager(pPager) + ,m_v3dBlockSpacePosition(v3dPosition) { // Compute the side length m_uSideLength = uSideLength; @@ -39,12 +41,37 @@ namespace PolyVox // Allocate the data const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength; - m_tData = new VoxelType[uNoOfVoxels]; + m_tData = new VoxelType[uNoOfVoxels]; + + // Pass the block to the Pager to give it a chance to initialise it with any data + if (m_pPager) + { + // From the coordinates of the block we deduce the coordinates of the contained voxels. + Vector3DInt32 v3dLower = m_v3dBlockSpacePosition * static_cast(m_uSideLength); + Vector3DInt32 v3dUpper = v3dLower + Vector3DInt32(m_uSideLength - 1, m_uSideLength - 1, m_uSideLength - 1); + Region reg(v3dLower, v3dUpper); + + // Page the data in + m_pPager->pageIn(reg, this); + } + + // We'll use this later to decide if data needs to be paged out again. + m_bDataModified = false; } template UncompressedBlock::~UncompressedBlock() { + if (m_pPager && m_bDataModified) + { + // From the coordinates of the block we deduce the coordinates of the contained voxels. + Vector3DInt32 v3dLower = m_v3dBlockSpacePosition * static_cast(m_uSideLength); + Vector3DInt32 v3dUpper = v3dLower + Vector3DInt32(m_uSideLength - 1, m_uSideLength - 1, m_uSideLength - 1); + + // Page the data out + m_pPager->pageOut(Region(v3dLower, v3dUpper), this); + } + delete[] m_tData; m_tData = 0; }