From ed355b856f99b0f6ebc51e30f5eda36c13821833 Mon Sep 17 00:00:00 2001 From: David Williams Date: Sat, 12 Feb 2011 20:47:01 +0000 Subject: [PATCH] More tidying up. --- examples/Basic/main.cpp | 16 +++- .../PolyVoxCore/include/PolyVoxImpl/Block.h | 1 - .../PolyVoxCore/include/PolyVoxImpl/Block.inl | 10 +-- library/PolyVoxCore/include/Volume.h | 13 +-- library/PolyVoxCore/include/Volume.inl | 86 ++++++++++++------- library/PolyVoxCore/include/VolumeSampler.inl | 8 +- 6 files changed, 80 insertions(+), 54 deletions(-) diff --git a/examples/Basic/main.cpp b/examples/Basic/main.cpp index d9cbaaed..85ef2f9e 100644 --- a/examples/Basic/main.cpp +++ b/examples/Basic/main.cpp @@ -356,8 +356,16 @@ void createPerlinVolumeSlow(Volume& volData) { float perlinVal = perlin.Get3D(x /static_cast(volData.getWidth()-1), (y) / static_cast(volData.getHeight()-1), z / static_cast(volData.getDepth()-1)); + perlinVal += 1.0f; + perlinVal *= 0.5f; + perlinVal *= MaterialDensityPair44::getMaxDensity(); + MaterialDensityPair44 voxel; - if(perlinVal < 0.0f) + + voxel.setMaterial(245); + voxel.setDensity(perlinVal); + + /*if(perlinVal < 0.0f) { voxel.setMaterial(245); voxel.setDensity(MaterialDensityPair44::getMaxDensity()); @@ -366,7 +374,7 @@ void createPerlinVolumeSlow(Volume& volData) { voxel.setMaterial(0); voxel.setDensity(MaterialDensityPair44::getMinDensity()); - } + }*/ volData.setVoxelAt(x, y, z, voxel); } @@ -504,8 +512,8 @@ int main(int argc, char *argv[]) //Create an empty volume and then place a sphere in it Volume volData(256, 256, 256); //createSphereInVolume(volData, 30); - createPerlinTerrain(volData); - //createPerlinVolumeSlow(volData); + //createPerlinTerrain(volData); + createPerlinVolumeSlow(volData); volData.setBlockCacheSize(8); /*srand(12345); diff --git a/library/PolyVoxCore/include/PolyVoxImpl/Block.h b/library/PolyVoxCore/include/PolyVoxImpl/Block.h index bad3a813..78529a0d 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxImpl/Block.h @@ -65,7 +65,6 @@ namespace PolyVox void uncompress(VoxelType* pData); std::vector< RunlengthEntry > m_vecCompressedData; - uint64_t m_uTimestamp; VoxelType* m_tUncompressedData; uint16_t m_uSideLength; uint8_t m_uSideLengthPower; diff --git a/library/PolyVoxCore/include/PolyVoxImpl/Block.inl b/library/PolyVoxCore/include/PolyVoxImpl/Block.inl index a9b1c27f..9a65f63d 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/Block.inl +++ b/library/PolyVoxCore/include/PolyVoxImpl/Block.inl @@ -39,7 +39,6 @@ namespace PolyVox ,m_tUncompressedData(0) ,m_bIsCompressed(true) ,m_bIsUncompressedDataModified(true) - ,m_uTimestamp(0) { if(uSideLength != 0) { @@ -148,7 +147,7 @@ namespace PolyVox uint32_t Block::sizeInBytes(void) { uint32_t uSizeInBytes = sizeof(Block); - uSizeInBytes += m_vecCompressedData.size() * sizeof(RunlengthEntry); + uSizeInBytes += m_vecCompressedData.capacity() * sizeof(RunlengthEntry); return uSizeInBytes; } @@ -207,11 +206,8 @@ namespace PolyVox VoxelType* pUncompressedData = m_tUncompressedData; for(uint32_t ct = 0; ct < m_vecCompressedData.size(); ++ct) { - for(uint32_t i = 0; i < m_vecCompressedData[ct].length; ++i) - { - *pUncompressedData = m_vecCompressedData[ct].value; - ++pUncompressedData; - } + std::fill(pUncompressedData, pUncompressedData + m_vecCompressedData[ct].length, m_vecCompressedData[ct].value); + pUncompressedData += m_vecCompressedData[ct].length; } m_bIsCompressed = false; diff --git a/library/PolyVoxCore/include/Volume.h b/library/PolyVoxCore/include/Volume.h index be01b95b..85a454b0 100644 --- a/library/PolyVoxCore/include/Volume.h +++ b/library/PolyVoxCore/include/Volume.h @@ -120,7 +120,7 @@ namespace PolyVox struct UncompressedBlock { - Block* block; + uint32_t uBlockIndex; VoxelType* data; }; @@ -168,15 +168,16 @@ namespace PolyVox uint32_t sizeInBytes(void); public: - Block* getUncompressedBlock(Block* block) const; + Block* getUncompressedBlock(uint16_t uBlockX, uint16_t uBlockY, uint16_t uBlockZ) const; + + //Block m_pBorderBlock; + VoxelType* m_pUncompressedBorderData; - Block m_pBorderBlock; Block* m_pBlocks; - //mutable std::vector*> m_pUncompressedBlocks; - //mutable std::vector< std::vector > m_pUncompressedBlockData; - //mutable VoxelType* m_pUncompressedBlockData; + uint32_t* m_pUncompressedTimestamps; mutable std::vector< UncompressedBlock > m_vecUncompressedBlockCache; uint16_t m_uMaxUncompressedBlockCacheSize; + uint32_t m_ulastAccessedBlockIndex; uint32_t m_uNoOfBlocksInVolume; diff --git a/library/PolyVoxCore/include/Volume.inl b/library/PolyVoxCore/include/Volume.inl index eb45c8f4..fe20f60a 100644 --- a/library/PolyVoxCore/include/Volume.inl +++ b/library/PolyVoxCore/include/Volume.inl @@ -54,6 +54,9 @@ namespace PolyVox ,m_uUncompressions(0) ,m_uBlockSideLength(uBlockSideLength) ,m_pBlocks(0) + ,m_pUncompressedTimestamps(0) + ,m_pUncompressedBorderData(0) + ,m_ulastAccessedBlockIndex((std::numeric_limits::max)()) //An invalid index { setBlockCacheSize(m_uMaxUncompressedBlockCacheSize); @@ -77,8 +80,9 @@ namespace PolyVox template VoxelType Volume::getBorderValue(void) const { - Block* pUncompressedBorderBlock = getUncompressedBlock(const_cast*>(&m_pBorderBlock)); - return pUncompressedBorderBlock->getVoxelAt(0,0,0); + /*Block* pUncompressedBorderBlock = getUncompressedBlock(const_cast*>(&m_pBorderBlock)); + return pUncompressedBorderBlock->getVoxelAt(0,0,0);*/ + return *m_pUncompressedBorderData; } //////////////////////////////////////////////////////////////////////////////// @@ -178,14 +182,14 @@ namespace PolyVox const uint16_t yOffset = uYPos - (blockY << m_uBlockSideLengthPower); const uint16_t zOffset = uZPos - (blockZ << m_uBlockSideLengthPower); - const Block& block = m_pBlocks + /*const Block& block = m_pBlocks [ blockX + blockY * m_uWidthInBlocks + blockZ * m_uWidthInBlocks * m_uHeightInBlocks - ]; + ];*/ - Block* pUncompressedBlock = getUncompressedBlock(const_cast*>(&block)); + Block* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); return pUncompressedBlock->getVoxelAt(xOffset,yOffset,zOffset); } @@ -211,8 +215,9 @@ namespace PolyVox template void Volume::setBorderValue(const VoxelType& tBorder) { - Block* pUncompressedBorderBlock = getUncompressedBlock(&m_pBorderBlock); - return pUncompressedBorderBlock->fill(tBorder); + /*Block* pUncompressedBorderBlock = getUncompressedBlock(&m_pBorderBlock); + return pUncompressedBorderBlock->fill(tBorder);*/ + std::fill(m_pUncompressedBorderData, m_pUncompressedBorderData + m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength, tBorder); } //////////////////////////////////////////////////////////////////////////////// @@ -237,14 +242,14 @@ namespace PolyVox const uint16_t yOffset = uYPos - (blockY << m_uBlockSideLengthPower); const uint16_t zOffset = uZPos - (blockZ << m_uBlockSideLengthPower); - uint32_t uBlockIndex = + /*uint32_t uBlockIndex = blockX + blockY * m_uWidthInBlocks + blockZ * m_uWidthInBlocks * m_uHeightInBlocks; - Block& block = m_pBlocks[uBlockIndex]; + Block& block = m_pBlocks[uBlockIndex];*/ - Block* pUncompressedBlock = getUncompressedBlock(&block); + Block* pUncompressedBlock = getUncompressedBlock(blockX, blockY, blockZ); pUncompressedBlock->setVoxelAt(xOffset,yOffset,zOffset, tValue); @@ -274,7 +279,7 @@ namespace PolyVox { for(uint32_t ct = 0; ct < m_vecUncompressedBlockCache.size(); ct++) { - m_vecUncompressedBlockCache[ct].block->compress(); + m_pBlocks[m_vecUncompressedBlockCache[ct].uBlockIndex].compress(); delete[] m_vecUncompressedBlockCache[ct].data; m_uCompressions++; } @@ -329,6 +334,9 @@ namespace PolyVox delete[] m_pBlocks; m_pBlocks = 0; + delete[] m_pUncompressedTimestamps; + m_pUncompressedTimestamps = 0; + //Compute the volume side lengths m_uWidth = uWidth; m_uHeight = uHeight; @@ -348,15 +356,19 @@ namespace PolyVox //Create the blocks m_pBlocks = new Block[m_uNoOfBlocksInVolume]; + m_pUncompressedTimestamps = new uint32_t[m_uNoOfBlocksInVolume]; for(uint32_t i = 0; i < m_uNoOfBlocksInVolume; ++i) { m_pBlocks[i].initialise(m_uBlockSideLength); + m_pUncompressedTimestamps[i] = 0; //Use memset/std::fill } //Create the border block - m_pBorderBlock.initialise(uBlockSideLength); + /*m_pBorderBlock.initialise(uBlockSideLength); Block* pUncompressedBorderBlock = getUncompressedBlock(&m_pBorderBlock); - pUncompressedBorderBlock->fill(VoxelType()); + pUncompressedBorderBlock->fill(VoxelType());*/ + m_pUncompressedBorderData = new VoxelType[m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength]; + std::fill(m_pUncompressedBorderData, m_pUncompressedBorderData + m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength, VoxelType()); //Other properties we might find useful later m_uLongestSideLength = (std::max)((std::max)(m_uWidth,m_uHeight),m_uDepth); @@ -370,24 +382,29 @@ namespace PolyVox clearBlockCache(); m_uMaxUncompressedBlockCacheSize = uBlockCacheSize; - - /*m_pUncompressedBlockCache.resize(uBlockCacheSize); - for(uint32_t ct = 0; ct < m_pUncompressedBlockData.size(); ct++) - { - m_pUncompressedBlockData[ct].data - VoxelType empty; - empty.setMaterial(0); - empty.setDensity(0); - std::fill(m_pUncompressedBlockData[ct].begin(), m_pUncompressedBlockData[ct].end(), empty); - } - m_pUncompressedBlockData = new VoxelType[m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength * m_uBlockCacheSize]; - memset(m_pUncompressedBlockData, 0, m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength * m_uBlockCacheSize);*/ } template - Block* Volume::getUncompressedBlock(Block* block) const + Block* Volume::getUncompressedBlock(uint16_t uBlockX, uint16_t uBlockY, uint16_t uBlockZ) const { - block->m_uTimestamp = ++m_uTimestamper; + //Compute the block's index from it's position. + uint32_t uBlockIndex = + uBlockX + + uBlockY * m_uWidthInBlocks + + uBlockZ * m_uWidthInBlocks * m_uHeightInBlocks; + + //Get the block + Block* block = &(m_pBlocks[uBlockIndex]); + + //Check if we have the same block as last time, if so there's no need to even update + //the time stamp. If we updated it everytime then that would be every time we touched + //a voxel, which would overflow a uint32_t and require us to use a uint64_t instead. + if(uBlockIndex == m_ulastAccessedBlockIndex) + { + return block; + } + + m_pUncompressedTimestamps[uBlockIndex] = ++m_uTimestamper; if(block->m_bIsCompressed == false) { @@ -403,23 +420,26 @@ namespace PolyVox uint64_t uLeastRecentTimestamp = 1000000000000000; for(uint32_t ct = 0; ct < m_vecUncompressedBlockCache.size(); ct++) { - if(m_vecUncompressedBlockCache[ct].block->m_uTimestamp < uLeastRecentTimestamp) + if(m_pUncompressedTimestamps[m_vecUncompressedBlockCache[ct].uBlockIndex] < uLeastRecentTimestamp) + //if(m_pBlocks[m_vecUncompressedBlockCache[ct].uBlockIndex].m_uTimestamp < uLeastRecentTimestamp) { - uLeastRecentTimestamp = m_vecUncompressedBlockCache[ct].block->m_uTimestamp; + uLeastRecentTimestamp = m_pUncompressedTimestamps[m_vecUncompressedBlockCache[ct].uBlockIndex]; + //uLeastRecentTimestamp = m_pBlocks[m_vecUncompressedBlockCache[ct].uBlockIndex].m_uTimestamp; leastRecentlyUsedBlockIndex = ct; } } uUncompressedBlockIndex = leastRecentlyUsedBlockIndex; - m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex].block->compress(); - m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex].block->m_tUncompressedData = 0; + m_pBlocks[m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex].uBlockIndex].compress(); + m_pBlocks[m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex].uBlockIndex].m_tUncompressedData = 0; m_uCompressions++; - m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex].block = block; + m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex].uBlockIndex = uBlockIndex; } else { UncompressedBlock uncompressedBlock; - uncompressedBlock.block = block; + //uncompressedBlock.block = block; + uncompressedBlock.uBlockIndex = uBlockIndex; uncompressedBlock.data = new VoxelType[m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength]; m_vecUncompressedBlockCache.push_back(uncompressedBlock); uUncompressedBlockIndex = m_vecUncompressedBlockCache.size() - 1; diff --git a/library/PolyVoxCore/include/VolumeSampler.inl b/library/PolyVoxCore/include/VolumeSampler.inl index df97d81c..881c1d1d 100644 --- a/library/PolyVoxCore/include/VolumeSampler.inl +++ b/library/PolyVoxCore/include/VolumeSampler.inl @@ -155,14 +155,16 @@ namespace PolyVox uZBlock * mVolume->m_uWidthInBlocks * mVolume->m_uHeightInBlocks; const Block& currentBlock = mVolume->m_pBlocks[uBlockIndexInVolume]; - Block* pUncompressedCurrentBlock = mVolume->getUncompressedBlock(const_cast*>(¤tBlock)); + Block* pUncompressedCurrentBlock = mVolume->getUncompressedBlock(uXBlock, uYBlock, uZBlock); mCurrentVoxel = pUncompressedCurrentBlock->m_tUncompressedData + uVoxelIndexInBlock; } else { - Block* pUncompressedBorderBlock = mVolume->getUncompressedBlock(&(mVolume->m_pBorderBlock)); - mCurrentVoxel = pUncompressedBorderBlock->m_tUncompressedData + uVoxelIndexInBlock; + /*Block* pUncompressedBorderBlock = mVolume->getUncompressedBlock(&(mVolume->m_pBorderBlock)); + mCurrentVoxel = pUncompressedBorderBlock->m_tUncompressedData + uVoxelIndexInBlock;*/ + + mCurrentVoxel = mVolume->m_pUncompressedBorderData + uVoxelIndexInBlock; } }