Moved paging code into block constructor/destructor.

This commit is contained in:
David Williams 2014-09-17 13:35:43 +02:00
parent 2b47c959a5
commit 5b59bc2d8c
6 changed files with 41 additions and 35 deletions

View File

@ -90,7 +90,7 @@ public:
/// Destructor
virtual ~PerlinNoisePager() {};
virtual void pageIn(const PolyVox::Region& region, std::shared_ptr< UncompressedBlock<MaterialDensityPair44> > pBlockData)
virtual void pageIn(const PolyVox::Region& region, 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
// 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<MaterialDensityPair44> > /*pBlockData*/)
virtual void pageOut(const PolyVox::Region& region, UncompressedBlock<MaterialDensityPair44>* /*pBlockData*/)
{
std::cout << "warning unloading region: " << region.getLowerCorner() << " -> " << region.getUpperCorner() << std::endl;
}

View File

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

View File

@ -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<VoxelType> >(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<VoxelType> >(v3dBlockPos, m_uBlockSideLength, m_pPager);
while (m_pBlocks.size() + 1 > m_uMaxNumberOfUncompressedBlocks) // +1 ready for new block we will add next.
{

View File

@ -43,8 +43,8 @@ namespace PolyVox
/// Destructor
virtual ~Pager() {};
virtual void pageIn(const Region& region, std::shared_ptr< UncompressedBlock<VoxelType> > pBlockData) = 0;
virtual void pageOut(const Region& region, std::shared_ptr< UncompressedBlock<VoxelType> > pBlockData) = 0;
virtual void pageIn(const Region& region, UncompressedBlock<VoxelType>* pBlockData) = 0;
virtual void pageOut(const Region& region, UncompressedBlock<VoxelType>* pBlockData) = 0;
};
}

View File

@ -35,7 +35,7 @@ namespace PolyVox
friend class LargeVolume<VoxelType>;
public:
UncompressedBlock(uint16_t uSideLength);
UncompressedBlock(Vector3DInt32 v3dPosition, uint16_t uSideLength, Pager<VoxelType>* 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<VoxelType>* m_pPager;
Vector3DInt32 m_v3dBlockSpacePosition;
};
}

View File

@ -26,12 +26,14 @@ freely, subject to the following restrictions:
namespace PolyVox
{
template <typename VoxelType>
UncompressedBlock<VoxelType>::UncompressedBlock(uint16_t uSideLength)
UncompressedBlock<VoxelType>::UncompressedBlock(Vector3DInt32 v3dPosition, uint16_t uSideLength, Pager<VoxelType>* 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<int32_t>(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 <typename VoxelType>
UncompressedBlock<VoxelType>::~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<int32_t>(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;
}