Merge branch 'develop' into feature/cubiquity-version

This commit is contained in:
Daviw Williams 2013-11-13 15:24:21 +01:00
commit f2ecb667e8
4 changed files with 34 additions and 86 deletions

View File

@ -48,12 +48,7 @@ freely, subject to the following restrictions:
// Macros cannot contain #ifdefs, but some of our macros need to disable warnings and such warning supression is // Macros cannot contain #ifdefs, but some of our macros need to disable warnings and such warning supression is
// platform specific. But macros can contain other macros, so we create macros to control the warnings and use // platform specific. But macros can contain other macros, so we create macros to control the warnings and use
// those instead. // those instead. This set of warning supression macros can be extended to GCC/Clang when required.
//
// Note that we have seperate macros for POLYVOX_MSC_..., POLYVOX_GCC_..., etc. In princpiple we could have just one
// as compilers should ignore pragmas they don't recognise, but in practice at least MSVC complains about this as
// well. So in practice it's just eaier to have seperate macros. We could look into the compiler switch to not warn
// on unrecognised pragmas though.
#if defined(_MSC_VER) #if defined(_MSC_VER)
#define POLYVOX_MSC_WARNING_PUSH __pragma(warning(push)) #define POLYVOX_MSC_WARNING_PUSH __pragma(warning(push))
#define POLYVOX_DISABLE_MSC_WARNING(x) __pragma(warning(disable:x)) #define POLYVOX_DISABLE_MSC_WARNING(x) __pragma(warning(disable:x))
@ -64,16 +59,6 @@ freely, subject to the following restrictions:
#define POLYVOX_MSC_WARNING_POP #define POLYVOX_MSC_WARNING_POP
#endif #endif
#if defined(__GNUC__)
#define POLYVOX_GCC_WARNING_PUSH #pragma GCC diagnostic push
#define POLYVOX_DISABLE_GCC_WARNING(x) #pragma GCC diagnostic ignored x
#define POLYVOX_GCC_WARNING_POP #pragma GCC diagnostic pop
#else
#define POLYVOX_GCC_WARNING_PUSH
#define POLYVOX_DISABLE_GCC_WARNING(x)
#define POLYVOX_GCC_WARNING_POP
#endif
#define POLYVOX_UNUSED(x) do { (void)sizeof(x); } while(0) #define POLYVOX_UNUSED(x) do { (void)sizeof(x); } while(0)
/* /*

View File

@ -319,8 +319,8 @@ namespace PolyVox
uint32_t calculateBlockMemoryUsage(void) const; uint32_t calculateBlockMemoryUsage(void) const;
void flushOldestExcessiveBlocks(void) const; void ensureCompressedBlockMapHasFreeSpace(void) const;
void flushExcessiveCacheEntries(void) const; void ensureUncompressedBlockMapHasFreeSpace(void) const;
void initialise(); void initialise();

View File

@ -436,6 +436,9 @@ namespace PolyVox
{ {
typename CompressedBlockMap::iterator i; typename CompressedBlockMap::iterator i;
// Flushing will remove the most accessed block, so invalidate the pointer.
m_pLastAccessedBlock = 0;
//Replaced the for loop here as the call to //Replaced the for loop here as the call to
//eraseBlock was invalidating the iterator. //eraseBlock was invalidating the iterator.
while(m_pUncompressedBlockCache.size() > 0) while(m_pUncompressedBlockCache.size() > 0)
@ -585,14 +588,10 @@ namespace PolyVox
{ {
UncompressedBlock<VoxelType>* pUncompressedBlock = itUncompressedBlock->second; UncompressedBlock<VoxelType>* pUncompressedBlock = itUncompressedBlock->second;
// This should not often happen as blocks are normally deleted based on being least recently used. // This should never happen as blocks are deleted based on being least recently used.
// However, I can imagine that flushing a large number of blocks could cause this to occur. Just // I the case that we are flushing we delete all blocks, but the flush function will
// to be safe we handle it by invalidating the last accessed block pointer. // reset the 'm_pLastAccessedBlock' anyway to prevent it being accidentally reused.
if(pUncompressedBlock == m_pLastAccessedBlock) POLYVOX_ASSERT(pUncompressedBlock != m_pLastAccessedBlock, "Attempted to delete last accessed block.");
{
logWarning() << "The last accessed block is being erased from the uncompressed cache.";
m_pLastAccessedBlock = 0;
}
// Before deleting the block we may need to recompress its data. We // 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. // only do this if the data has been modified since it was decompressed.
@ -625,17 +624,14 @@ namespace PolyVox
if(itBlock != m_pBlocks.end()) if(itBlock != m_pBlocks.end())
{ {
pBlock = itBlock->second; pBlock = itBlock->second;
pBlock->m_uBlockLastAccessed = ++m_uTimestamper;
return pBlock;
} }
else else
{ {
ensureCompressedBlockMapHasFreeSpace();
// The block wasn't found so we create a new one // The block wasn't found so we create a new one
pBlock = new CompressedBlock<VoxelType>; pBlock = new CompressedBlock<VoxelType>;
// It's important to set the timestamp before we flush later.
pBlock->m_uBlockLastAccessed = ++m_uTimestamper;
// Pass the block to the Pager to give it a chance to initialise it with any data // Pass the block to the Pager to give it a chance to initialise it with any data
Vector3DInt32 v3dLower(v3dBlockPos.getX() << m_uBlockSideLengthPower, v3dBlockPos.getY() << m_uBlockSideLengthPower, v3dBlockPos.getZ() << m_uBlockSideLengthPower); 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); Vector3DInt32 v3dUpper = v3dLower + Vector3DInt32(m_uBlockSideLength-1, m_uBlockSideLength-1, m_uBlockSideLength-1);
@ -644,12 +640,10 @@ namespace PolyVox
// Add the block to the map // Add the block to the map
itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, pBlock)).first; itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, pBlock)).first;
// Paging in this new block may mean we are now using too much memory. If necessary, flush some old blocks.
flushOldestExcessiveBlocks();
return pBlock;
} }
pBlock->m_uBlockLastAccessed = ++m_uTimestamper;
return pBlock;
} }
template <typename VoxelType> template <typename VoxelType>
@ -666,30 +660,22 @@ namespace PolyVox
return m_pLastAccessedBlock; return m_pLastAccessedBlock;
} }
UncompressedBlock<VoxelType>* pUncompressedBlock = 0;
typename UncompressedBlockMap::iterator itUncompressedBlock = m_pUncompressedBlockCache.find(v3dBlockPos); typename UncompressedBlockMap::iterator itUncompressedBlock = m_pUncompressedBlockCache.find(v3dBlockPos);
// check whether the block is already loaded // check whether the block is already loaded
if(itUncompressedBlock != m_pUncompressedBlockCache.end()) if(itUncompressedBlock != m_pUncompressedBlockCache.end())
{ {
UncompressedBlock<VoxelType>* pUncompressedBlock = itUncompressedBlock->second; pUncompressedBlock = itUncompressedBlock->second;
pUncompressedBlock->m_uBlockLastAccessed = ++m_uTimestamper;
m_pLastAccessedBlock = pUncompressedBlock;
m_v3dLastAccessedBlockPos = v3dBlockPos;
return pUncompressedBlock;
} }
else else
{ {
// At this point we just create a new block. // At this point we know that the uncompresed block did not exist in the cache. We will
UncompressedBlock<VoxelType>* pUncompressedBlock = new UncompressedBlock<VoxelType>(m_uBlockSideLength); // create it and add it to the cache, which means we need to make sure there is space.
ensureUncompressedBlockMapHasFreeSpace();
// It's important to set the timestamp before we flush later.
pUncompressedBlock->m_uBlockLastAccessed = ++m_uTimestamper;
// We set these before flushing because the flush can cause block to be erased, and there // We can now create a new block.
// is a test to make sure the block which is being erase is not the last accessed block. pUncompressedBlock = new UncompressedBlock<VoxelType>(m_uBlockSideLength);
m_pLastAccessedBlock = pUncompressedBlock;
m_v3dLastAccessedBlockPos = v3dBlockPos;
// 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.
@ -702,14 +688,14 @@ namespace PolyVox
} }
// Add our new block to the map. // Add our new block to the map.
m_pUncompressedBlockCache.insert(std::make_pair(v3dBlockPos, pUncompressedBlock)); m_pUncompressedBlockCache.insert(std::make_pair(v3dBlockPos, pUncompressedBlock));
// Our block cache may now have grown too large. Flush some entries if necessary.
// FIXME - Watch out for flushing the block we just created!
flushExcessiveCacheEntries();
return pUncompressedBlock;
} }
pUncompressedBlock->m_uBlockLastAccessed = ++m_uTimestamper;
m_pLastAccessedBlock = pUncompressedBlock;
m_v3dLastAccessedBlockPos = v3dBlockPos;
return pUncompressedBlock;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -762,7 +748,7 @@ namespace PolyVox
} }
template <typename VoxelType> template <typename VoxelType>
void LargeVolume<VoxelType>::flushOldestExcessiveBlocks(void) const void LargeVolume<VoxelType>::ensureCompressedBlockMapHasFreeSpace(void) const
{ {
while(m_pBlocks.size() > m_uMaxNumberOfBlocksInMemory) while(m_pBlocks.size() > m_uMaxNumberOfBlocksInMemory)
{ {
@ -784,7 +770,7 @@ namespace PolyVox
} }
template <typename VoxelType> template <typename VoxelType>
void LargeVolume<VoxelType>::flushExcessiveCacheEntries(void) const void LargeVolume<VoxelType>::ensureUncompressedBlockMapHasFreeSpace(void) const
{ {
while(m_pUncompressedBlockCache.size() > m_uMaxNumberOfUncompressedBlocks) while(m_pUncompressedBlockCache.size() > m_uMaxNumberOfUncompressedBlocks)
{ {

View File

@ -75,31 +75,8 @@ namespace PolyVox
Type getDensity() const { return m_uDensity; } Type getDensity() const { return m_uDensity; }
Type getMaterial() const { return m_uMaterial; } Type getMaterial() const { return m_uMaterial; }
void setDensity(Type uDensity) void setDensity(Type uDensity) { m_uDensity = uDensity; }
{ void setMaterial(Type uMaterial) { m_uMaterial = uMaterial; }
// Depending on our underlying type it may be impossible for the assert below to be triggered (i.e. if density is stored as
// Type, rather than just using a few bits of Type). GCC will warn about this but it's redundant so we diable the warning.
POLYVOX_GCC_WARNING_PUSH
POLYVOX_DISABLE_GCC_WARNING("-Wtype-limits")
POLYVOX_ASSERT(uDensity >= getMinDensity(), "Density out of range");
POLYVOX_ASSERT(uDensity <= getMaxDensity(), "Density out of range");
POLYVOX_GCC_WARNING_POP
m_uDensity = uDensity;
}
void setMaterial(Type uMaterial)
{
// Depending on our underlying type it may be impossible for the assert below to be triggered (i.e. if material is stored as
// Type, rather than just using a few bits of Type). GCC will warn about this but it's redundant so we diable the warning.
POLYVOX_GCC_WARNING_PUSH
POLYVOX_DISABLE_GCC_WARNING("-Wtype-limits")
POLYVOX_ASSERT(uMaterial >= 0, "Material out of range");
POLYVOX_ASSERT(uMaterial <= (0x01 << NoOfMaterialBits) - 1, "Material out of range");
POLYVOX_GCC_WARNING_POP
m_uMaterial = uMaterial;
}
static Type getMaxDensity() { return (0x01 << NoOfDensityBits) - 1; } static Type getMaxDensity() { return (0x01 << NoOfDensityBits) - 1; }
static Type getMinDensity() { return 0; } static Type getMinDensity() { return 0; }