Making use of shared_ptr to track blocks.

This commit is contained in:
David Williams 2014-09-14 11:47:17 +02:00
parent 3cf8f38793
commit 47ace554cc
6 changed files with 22 additions and 19 deletions

View File

@ -90,7 +90,7 @@ public:
/// Destructor /// Destructor
virtual ~PerlinNoisePager() {}; virtual ~PerlinNoisePager() {};
virtual void pageIn(const PolyVox::Region& region, UncompressedBlock<MaterialDensityPair44>* pBlockData) virtual void pageIn(const PolyVox::Region& region, std::shared_ptr< UncompressedBlock<MaterialDensityPair44> > pBlockData)
{ {
// FIXME - this isn't a great example... it's a shame we have to hard clode the block size and also create/destroy // 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... // 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; //delete compressor;
} }
virtual void pageOut(const PolyVox::Region& region, UncompressedBlock<MaterialDensityPair44>* /*pBlockData*/) virtual void pageOut(const PolyVox::Region& region, std::shared_ptr< UncompressedBlock<MaterialDensityPair44> > /*pBlockData*/)
{ {
std::cout << "warning unloading region: " << region.getLowerCorner() << " -> " << region.getUpperCorner() << std::endl; std::cout << "warning unloading region: " << region.getLowerCorner() << " -> " << region.getUpperCorner() << std::endl;
} }

View File

@ -69,7 +69,7 @@ namespace PolyVox
m_vecCreatedFiles.clear(); m_vecCreatedFiles.clear();
} }
virtual void pageIn(const Region& region, UncompressedBlock<VoxelType>* pBlockData) virtual void pageIn(const Region& region, std::shared_ptr< UncompressedBlock<VoxelType> > pBlockData)
{ {
POLYVOX_ASSERT(pBlockData, "Attempting to page in NULL block"); POLYVOX_ASSERT(pBlockData, "Attempting to page in NULL block");
//POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data"); //POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data");
@ -113,7 +113,7 @@ namespace PolyVox
} }
} }
virtual void pageOut(const Region& region, UncompressedBlock<VoxelType>* pBlockData) virtual void pageOut(const Region& region, std::shared_ptr< UncompressedBlock<VoxelType> > pBlockData)
{ {
POLYVOX_ASSERT(pBlockData, "Attempting to page out NULL block"); POLYVOX_ASSERT(pBlockData, "Attempting to page out NULL block");
//POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data"); //POLYVOX_ASSERT(pBlockData->hasUncompressedData() == false, "Block should not have uncompressed data");

View File

@ -300,7 +300,7 @@ namespace PolyVox
} }
}; };
typedef std::map<Vector3DInt32, UncompressedBlock<VoxelType>*, BlockPositionCompare> UncompressedBlockMap; typedef std::map<Vector3DInt32, std::shared_ptr< UncompressedBlock<VoxelType> >, BlockPositionCompare> UncompressedBlockMap;
void ensureUncompressedBlockMapHasFreeSpace(void) const; void ensureUncompressedBlockMapHasFreeSpace(void) const;
@ -314,7 +314,7 @@ namespace PolyVox
VoxelType getVoxelImpl(int32_t uXPos, int32_t uYPos, int32_t uZPos, WrapModeType<WrapModes::Border>, VoxelType tBorder) const; VoxelType getVoxelImpl(int32_t uXPos, int32_t uYPos, int32_t uZPos, WrapModeType<WrapModes::Border>, VoxelType tBorder) const;
VoxelType getVoxelImpl(int32_t uXPos, int32_t uYPos, int32_t uZPos, WrapModeType<WrapModes::AssumeValid>, VoxelType tBorder) const; VoxelType getVoxelImpl(int32_t uXPos, int32_t uYPos, int32_t uZPos, WrapModeType<WrapModes::AssumeValid>, VoxelType tBorder) const;
UncompressedBlock<VoxelType>* getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const; std::shared_ptr< UncompressedBlock<VoxelType> > getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const;
void eraseBlock(typename UncompressedBlockMap::iterator itUncompressedBlock) const; void eraseBlock(typename UncompressedBlockMap::iterator itUncompressedBlock) const;
@ -323,7 +323,7 @@ namespace PolyVox
mutable uint32_t m_uTimestamper; mutable uint32_t m_uTimestamper;
mutable Vector3DInt32 m_v3dLastAccessedBlockPos; mutable Vector3DInt32 m_v3dLastAccessedBlockPos;
mutable UncompressedBlock<VoxelType>* m_pLastAccessedBlock; mutable std::shared_ptr< UncompressedBlock<VoxelType> > m_pLastAccessedBlock;
uint32_t m_uMaxNumberOfUncompressedBlocks; uint32_t m_uMaxNumberOfUncompressedBlocks;
// The size of the volume // The size of the volume

View File

@ -186,7 +186,7 @@ namespace PolyVox
const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower));
const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower));
const UncompressedBlock<VoxelType>* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ);
return pUncompressedBlock->getVoxel(xOffset, yOffset, zOffset); return pUncompressedBlock->getVoxel(xOffset, yOffset, zOffset);
} }
@ -262,7 +262,7 @@ namespace PolyVox
const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower));
const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower));
UncompressedBlock<VoxelType>* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ);
pUncompressedBlock->setVoxelAt(xOffset, yOffset, zOffset, tValue); pUncompressedBlock->setVoxelAt(xOffset, yOffset, zOffset, tValue);
} }
@ -299,7 +299,7 @@ namespace PolyVox
const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower));
const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower));
UncompressedBlock<VoxelType>* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ);
pUncompressedBlock->setVoxelAt(xOffset, yOffset, zOffset, tValue); pUncompressedBlock->setVoxelAt(xOffset, yOffset, zOffset, tValue);
@ -486,7 +486,7 @@ namespace PolyVox
template <typename VoxelType> template <typename VoxelType>
void LargeVolume<VoxelType>::eraseBlock(typename UncompressedBlockMap::iterator itUncompressedBlock) const void LargeVolume<VoxelType>::eraseBlock(typename UncompressedBlockMap::iterator itUncompressedBlock) const
{ {
UncompressedBlock<VoxelType>* pUncompressedBlock = itUncompressedBlock->second; std::shared_ptr< UncompressedBlock<VoxelType> > pUncompressedBlock = itUncompressedBlock->second;
// This should never happen as blocks are deleted based on being least recently used. // This should never happen as blocks are deleted based on being least recently used.
// I the case that we are flushing we delete all blocks, but the flush function will // I the case that we are flushing we delete all blocks, but the flush function will
@ -511,14 +511,14 @@ namespace PolyVox
pUncompressedBlock->m_bDataModified = false; pUncompressedBlock->m_bDataModified = false;
} }
delete itUncompressedBlock->second; //delete itUncompressedBlock->second;
// We can now remove the block data from memory. // We can now remove the block data from memory.
m_pBlocks.erase(itUncompressedBlock); m_pBlocks.erase(itUncompressedBlock);
} }
template <typename VoxelType> template <typename VoxelType>
UncompressedBlock<VoxelType>* LargeVolume<VoxelType>::getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const std::shared_ptr< UncompressedBlock<VoxelType> > LargeVolume<VoxelType>::getUncompressedBlock(int32_t uBlockX, int32_t uBlockY, int32_t uBlockZ) const
{ {
Vector3DInt32 v3dBlockPos(uBlockX, uBlockY, uBlockZ); Vector3DInt32 v3dBlockPos(uBlockX, uBlockY, uBlockZ);
@ -531,7 +531,7 @@ namespace PolyVox
return m_pLastAccessedBlock; return m_pLastAccessedBlock;
} }
UncompressedBlock<VoxelType>* pUncompressedBlock = 0; std::shared_ptr< UncompressedBlock<VoxelType> > pUncompressedBlock = nullptr;
typename UncompressedBlockMap::iterator itUncompressedBlock = m_pBlocks.find(v3dBlockPos); typename UncompressedBlockMap::iterator itUncompressedBlock = m_pBlocks.find(v3dBlockPos);
// check whether the block is already loaded // check whether the block is already loaded
@ -546,7 +546,8 @@ namespace PolyVox
ensureUncompressedBlockMapHasFreeSpace(); ensureUncompressedBlockMapHasFreeSpace();
// We can now create a new block. // We can now create a new block.
pUncompressedBlock = new UncompressedBlock<VoxelType>(m_uBlockSideLength); //pUncompressedBlock = new UncompressedBlock<VoxelType>(m_uBlockSideLength);
pUncompressedBlock = std::make_shared< UncompressedBlock<VoxelType> >(m_uBlockSideLength);
// An uncompressed bock is always backed by a compressed one, and this is created by getCompressedBlock() if it doesn't // An uncompressed bock is always backed by a compressed one, and this is created by getCompressedBlock() if it doesn't
// already exist. If it does already exist and has data then we bring this across into the ucompressed version. // already exist. If it does already exist and has data then we bring this across into the ucompressed version.
@ -677,7 +678,7 @@ namespace PolyVox
const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower)); const uint16_t yOffset = static_cast<uint16_t>(uYPos - (blockY << m_uBlockSideLengthPower));
const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower)); const uint16_t zOffset = static_cast<uint16_t>(uZPos - (blockZ << m_uBlockSideLengthPower));
UncompressedBlock<VoxelType>* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); auto pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ);
return pUncompressedBlock->getVoxel(xOffset, yOffset, zOffset); return pUncompressedBlock->getVoxel(xOffset, yOffset, zOffset);
} }
} }

View File

@ -119,7 +119,7 @@ namespace PolyVox
uYPosInBlock * this->mVolume->m_uBlockSideLength + uYPosInBlock * this->mVolume->m_uBlockSideLength +
uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength; uZPosInBlock * this->mVolume->m_uBlockSideLength * this->mVolume->m_uBlockSideLength;
UncompressedBlock<VoxelType>* pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); auto pUncompressedCurrentBlock = this->mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock);
mCurrentVoxel = pUncompressedCurrentBlock->m_tData + uVoxelIndexInBlock; mCurrentVoxel = pUncompressedCurrentBlock->m_tData + uVoxelIndexInBlock;
} }

View File

@ -27,6 +27,8 @@ freely, subject to the following restrictions:
#include "PolyVoxCore/UncompressedBlock.h" #include "PolyVoxCore/UncompressedBlock.h"
#include "PolyVoxCore/Impl/TypeDef.h" #include "PolyVoxCore/Impl/TypeDef.h"
#include <memory>
namespace PolyVox namespace PolyVox
{ {
/** /**
@ -41,8 +43,8 @@ namespace PolyVox
/// Destructor /// Destructor
virtual ~Pager() {}; virtual ~Pager() {};
virtual void pageIn(const Region& region, UncompressedBlock<VoxelType>* pBlockData) = 0; virtual void pageIn(const Region& region, std::shared_ptr< UncompressedBlock<VoxelType> > pBlockData) = 0;
virtual void pageOut(const Region& region, UncompressedBlock<VoxelType>* pBlockData) = 0; virtual void pageOut(const Region& region, std::shared_ptr< UncompressedBlock<VoxelType> > pBlockData) = 0;
}; };
} }