From f13e9161f86ea2d88aba3ad4497203be3524333b Mon Sep 17 00:00:00 2001 From: David Williams Date: Sun, 6 Feb 2011 23:23:01 +0000 Subject: [PATCH] More work on compression and bug fixes. --- examples/Basic/main.cpp | 2 +- .../PolyVoxCore/include/PolyVoxImpl/Block.h | 3 +- .../PolyVoxCore/include/PolyVoxImpl/Block.inl | 46 ++++++++++--------- library/PolyVoxCore/include/Volume.h | 2 +- library/PolyVoxCore/include/Volume.inl | 4 +- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/examples/Basic/main.cpp b/examples/Basic/main.cpp index beb314fc..f591d8ed 100644 --- a/examples/Basic/main.cpp +++ b/examples/Basic/main.cpp @@ -421,7 +421,7 @@ int main(int argc, char *argv[]) openGLWidget.show(); //Create an empty volume and then place a sphere in it - Volume volData(256, 256, 256); + Volume volData(1024, 1024, 1024); //createSphereInVolume(volData, 30); createPerlinVolume(volData); diff --git a/library/PolyVoxCore/include/PolyVoxImpl/Block.h b/library/PolyVoxCore/include/PolyVoxImpl/Block.h index 1411c7c6..90df1e76 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/Block.h +++ b/library/PolyVoxCore/include/PolyVoxImpl/Block.h @@ -59,10 +59,9 @@ namespace PolyVox uint16_t m_uSideLength; uint8_t m_uSideLengthPower; - VoxelType* m_tCompressedData; VoxelType* m_tUncompressedData; bool m_bIsCompressed; - uint32_t m_uTimestamp; + uint64_t m_uTimestamp; std::vector runlengths; std::vector values; diff --git a/library/PolyVoxCore/include/PolyVoxImpl/Block.inl b/library/PolyVoxCore/include/PolyVoxImpl/Block.inl index 17d7f326..27f6c862 100644 --- a/library/PolyVoxCore/include/PolyVoxImpl/Block.inl +++ b/library/PolyVoxCore/include/PolyVoxImpl/Block.inl @@ -35,7 +35,6 @@ namespace PolyVox Block::Block(uint16_t uSideLength) :m_uSideLength(0) ,m_uSideLengthPower(0) - ,m_tCompressedData(0) ,m_tUncompressedData(0) ,m_bIsCompressed(true) ,m_uTimestamp(0) @@ -55,33 +54,37 @@ namespace PolyVox template Block::~Block() { - delete[] m_tCompressedData; - m_tCompressedData = 0; + delete[] m_tUncompressedData; + m_tUncompressedData = 0; } template Block& Block::operator=(const Block& rhs) { + //We don't often need to assign blocks as they should be passed around by pointer. + //And I'm pretty sure we don't want to be passing around uncompressed ones becauses + //it means duplicating the uncompressed data which is expensive. This assert is to + //make sure that uncompressed blocks don't get assigned by accident. + assert(rhs.m_bIsCompressed == true); + if (this == &rhs) { return *this; } - //If this fails an exception will be thrown. Memory is not - //allocated and there is nothing else in this class to clean up - m_tCompressedData = new VoxelType[rhs.m_uSideLength * rhs.m_uSideLength * rhs.m_uSideLength]; - //Copy the data m_uSideLength = rhs.m_uSideLength; - m_uSideLengthPower = rhs.m_uSideLengthPower; - memcpy(m_tCompressedData, rhs.m_tCompressedData, m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType)); - + m_uSideLengthPower = rhs.m_uSideLengthPower; m_bIsCompressed = rhs.m_bIsCompressed; + m_uTimestamp = rhs.m_uTimestamp; + runlengths = rhs.runlengths; + values = rhs.values; + if(m_bIsCompressed == false) { + m_tUncompressedData = new VoxelType[rhs.m_uSideLength * rhs.m_uSideLength * rhs.m_uSideLength]; memcpy(m_tUncompressedData, rhs.m_tUncompressedData, m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType)); } - m_uTimestamp = rhs.m_uTimestamp; return *this; } @@ -146,7 +149,6 @@ namespace PolyVox assert(m_tUncompressedData); - //memset(m_tCompressedData, (int)tValue, m_uSideLength * m_uSideLength * m_uSideLength * sizeof(VoxelType)); const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength; std::fill(m_tUncompressedData, m_tUncompressedData + uNoOfVoxels, tValue); } @@ -167,13 +169,17 @@ namespace PolyVox m_uSideLength = uSideLength; m_uSideLengthPower = logBase2(uSideLength); - //Delete the old data - delete[] m_tCompressedData; - m_tCompressedData = 0; - //If this fails an exception will be thrown. Memory is not - //allocated and there is nothing else in this class to clean up - m_tCompressedData = new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength]; + if(m_bIsCompressed == false) + { + //Delete the old data + delete[] m_tUncompressedData; + m_tUncompressedData = 0; + + //If this fails an exception will be thrown. Memory is not + //allocated and there is nothing else in this class to clean up + m_tUncompressedData = new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength]; + } } template @@ -181,7 +187,7 @@ namespace PolyVox { uint32_t uSizeInChars = sizeof(Block); - if(m_tCompressedData != 0) + if(m_tUncompressedData != 0) { const uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength; uSizeInChars += uNoOfVoxels * sizeof(VoxelType); @@ -193,7 +199,6 @@ namespace PolyVox template void Block::compress(void) { - //memcpy(m_tCompressedData, m_tUncompressedData, sizeof(VoxelType) * m_uSideLength * m_uSideLength * m_uSideLength); VoxelType current; uint32_t runLength = 0; uint32_t uNoOfVoxels = m_uSideLength * m_uSideLength * m_uSideLength; @@ -241,7 +246,6 @@ namespace PolyVox m_tUncompressedData = new VoxelType[m_uSideLength * m_uSideLength * m_uSideLength]; - //memcpy(m_tUncompressedData, m_tCompressedData, sizeof(VoxelType) * m_uSideLength * m_uSideLength * m_uSideLength); VoxelType* pUncompressedData = m_tUncompressedData; for(uint32_t ct = 0; ct < runlengths.size(); ++ct) { diff --git a/library/PolyVoxCore/include/Volume.h b/library/PolyVoxCore/include/Volume.h index 36b24e9c..3c94d443 100644 --- a/library/PolyVoxCore/include/Volume.h +++ b/library/PolyVoxCore/include/Volume.h @@ -179,7 +179,7 @@ namespace PolyVox uint16_t m_uLongestSideLength; uint16_t m_uShortestSideLength; float m_fDiagonalLength; - mutable uint32_t m_uTimestamper; + mutable uint64_t m_uTimestamper; }; //Some handy typedefs diff --git a/library/PolyVoxCore/include/Volume.inl b/library/PolyVoxCore/include/Volume.inl index dd3f3dba..c5905e48 100644 --- a/library/PolyVoxCore/include/Volume.inl +++ b/library/PolyVoxCore/include/Volume.inl @@ -388,11 +388,11 @@ namespace PolyVox return block; } - const uint32_t MaxUncompressedBlocks = 10; + const uint32_t MaxUncompressedBlocks = 1000; if(m_pUncompressedBlocks.size() == MaxUncompressedBlocks) { Block* pLeastRecentlyUsedBlock = 0; - uint32_t uLeastRecentTimestamp = 100000000; + uint32_t uLeastRecentTimestamp = 1000000000000000; for(std::set*>::iterator iter = m_pUncompressedBlocks.begin(); iter != m_pUncompressedBlocks.end(); iter++) { if((*iter)->m_uTimestamp < uLeastRecentTimestamp)