From 44d525f5912f2eb232d0b9355f0f058f73c1508b Mon Sep 17 00:00:00 2001 From: Daviw Williams Date: Wed, 26 Jun 2013 17:02:06 +0200 Subject: [PATCH] Tidying up and refactoring LargeVolume. --- examples/Basic/main.cpp | 6 +-- examples/Paging/main.cpp | 2 +- .../include/PolyVoxCore/LargeVolume.h | 35 +++++++------ .../include/PolyVoxCore/LargeVolume.inl | 51 ++++++++++++------- 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/examples/Basic/main.cpp b/examples/Basic/main.cpp index 444e71fb..407d4e8e 100644 --- a/examples/Basic/main.cpp +++ b/examples/Basic/main.cpp @@ -82,11 +82,11 @@ int main(int argc, char *argv[]) //Create an empty volume and then place a sphere in it RLECompressor* pCompressor = new RLECompressor(); - FilePager* pFilePager = new FilePager("D:/temp/voldata/"); + FilePager* pFilePager = new FilePager("C:/temp/voldata/"); LargeVolume volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63)), pCompressor, pFilePager, 32); - volData.setMaxNumberOfUncompressedBlocks(6); - volData.setMaxNumberOfBlocksInMemory(7); + volData.setMaxNumberOfUncompressedBlocks(2); + volData.setMaxNumberOfBlocksInMemory(4); createSphereInVolume(volData, 30); diff --git a/examples/Paging/main.cpp b/examples/Paging/main.cpp index 4622764c..03601f9b 100644 --- a/examples/Paging/main.cpp +++ b/examples/Paging/main.cpp @@ -332,7 +332,7 @@ int main(int argc, char *argv[]) //If these lines don't compile, please try commenting them out and using the two lines after //(you will need Boost for this). If you have to do this then please let us know in the forums as //we rely on community feedback to keep the Boost version running. - LargeVolume volData(compressor, pager, 256); + LargeVolume volData(Region::MaxRegion, compressor, pager, 256); //LargeVolume volData(polyvox_bind(&load, polyvox_placeholder_1, polyvox_placeholder_2), // polyvox_bind(&unload, polyvox_placeholder_1, polyvox_placeholder_2), 256); volData.setMaxNumberOfBlocksInMemory(4096); diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h index daded00b..581f1adf 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.h @@ -233,20 +233,19 @@ namespace PolyVox #endif - public: - /// Constructor for creating a very large paging volume. + public: + /// Constructor for creating a fixed size volume. LargeVolume ( - Compressor* pCompressor, - Pager* pPager, - uint16_t uBlockSideLength = 32 + const Region& regValid, + uint16_t uBlockSideLength = 32 ); /// Constructor for creating a fixed size volume. LargeVolume ( const Region& regValid, - Compressor* pCompressor, - Pager* pPager, + Compressor* pCompressor, + Pager* pPager , uint16_t uBlockSideLength = 32 ); /// Destructor @@ -319,7 +318,7 @@ namespace PolyVox return false; } }; - void initialise(const Region& regValidRegion, uint16_t uBlockSideLength); + void initialise(); // A trick to implement specialization of template member functions in template classes. See http://stackoverflow.com/a/4951057 template @@ -344,13 +343,13 @@ namespace PolyVox /// this function can be called by m_funcDataRequiredHandler without causing any weird effects bool setVoxelAtConst(int32_t uXPos, int32_t uYPos, int32_t uZPos, VoxelType tValue) const; - //The block data + // The block data mutable std::map, BlockPositionCompare> m_pBlocks; - //The cache of uncompressed blocks. The uncompressed block data and the timestamps are stored here rather - //than in the Block class. This is so that in the future each VolumeIterator might to maintain its own cache - //of blocks. However, this could mean the same block data is uncompressed and modified in more than one - //location in memory... could be messy with threading. + // The cache of uncompressed blocks. The uncompressed block data and the timestamps are stored here rather + // than in the Block class. This is so that in the future each VolumeIterator might to maintain its own cache + // of blocks. However, this could mean the same block data is uncompressed and modified in more than one + // location in memory... could be messy with threading. mutable std::vector< Block* > m_vecBlocksWithUncompressedData; mutable uint32_t m_uTimestamper; mutable Vector3DInt32 m_v3dLastAccessedBlockPos; @@ -358,16 +357,20 @@ namespace PolyVox uint32_t m_uMaxNumberOfUncompressedBlocks; uint32_t m_uMaxNumberOfBlocksInMemory; - //The size of the volume + // The size of the volume Region m_regValidRegionInBlocks; - //The size of the blocks + // The size of the blocks uint16_t m_uBlockSideLength; uint8_t m_uBlockSideLengthPower; - //The compressor used by the Blocks to compress their data if required. + // The compressor used by the Blocks to compress their data if required. Compressor* m_pCompressor; Pager* m_pPager; + + // Whether we created the compressor or whether it was provided + // by the user. This controls whether we delete it on destruction. + bool m_bIsOurCompressor; }; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl index dccaa4cd..640ddc3e 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/LargeVolume.inl @@ -23,6 +23,8 @@ freely, subject to the following restrictions: #include "PolyVoxCore/Impl/ErrorHandling.h" +#include "PolyVoxCore/MinizCompressor.h" + //Included here rather than in the .h because it refers to LargeVolume (avoids forward declaration) #include "PolyVoxCore/ConstVolumeProxy.h" @@ -37,16 +39,19 @@ namespace PolyVox template LargeVolume::LargeVolume ( - Compressor* pCompressor, - Pager* pPager, - uint16_t uBlockSideLength + const Region& regValid, + uint16_t uBlockSideLength ) - :BaseVolume(Region::MaxRegion) - ,m_pCompressor(pCompressor) + :BaseVolume(regValid) { - m_pPager = pPager; - //Create a volume of the right size. - initialise(Region::MaxRegion,uBlockSideLength); + m_uBlockSideLength = uBlockSideLength; + + m_pCompressor = new MinizCompressor(); + m_bIsOurCompressor = true; + + m_pPager = 0; + + initialise(); } //////////////////////////////////////////////////////////////////////////////// @@ -67,12 +72,16 @@ namespace PolyVox uint16_t uBlockSideLength ) :BaseVolume(regValid) - ,m_pCompressor(pCompressor) { + + m_uBlockSideLength = uBlockSideLength; + + m_pCompressor = pCompressor; + m_bIsOurCompressor = false; + m_pPager = pPager; - //Create a volume of the right size. - initialise(regValid,uBlockSideLength); + initialise(); } //////////////////////////////////////////////////////////////////////////////// @@ -95,6 +104,12 @@ namespace PolyVox LargeVolume::~LargeVolume() { flushAll(); + + // Only delete the compressor if it was created by us (in the constructor), not by the user. + if(m_bIsOurCompressor) + { + delete m_pCompressor; + } } //////////////////////////////////////////////////////////////////////////////// @@ -480,33 +495,31 @@ namespace PolyVox /// This function should probably be made internal... //////////////////////////////////////////////////////////////////////////////// template - void LargeVolume::initialise(const Region& regValidRegion, uint16_t uBlockSideLength) + void LargeVolume::initialise() { //Validate parameters - if(uBlockSideLength == 0) + if(m_uBlockSideLength == 0) { POLYVOX_THROW(std::invalid_argument, "Block side length cannot be zero."); } - if(!isPowerOf2(uBlockSideLength)) + + if(!isPowerOf2(m_uBlockSideLength)) { POLYVOX_THROW(std::invalid_argument, "Block side length must be a power of two."); } + if(!m_pCompressor) { - POLYVOX_THROW(std::invalid_argument, "You must provide a compressor for the LargeVolume to use."); + POLYVOX_THROW(std::invalid_argument, "You must provide a valid compressor for the LargeVolume to use."); } m_uTimestamper = 0; m_uMaxNumberOfUncompressedBlocks = 16; - m_uBlockSideLength = uBlockSideLength; m_uMaxNumberOfBlocksInMemory = 1024; m_v3dLastAccessedBlockPos = Vector3DInt32(0,0,0); //There are no invalid positions, but initially the m_pLastAccessedBlock pointer will be null; m_pLastAccessedBlock = 0; - this->m_regValidRegion = regValidRegion; - //Compute the block side length - m_uBlockSideLength = uBlockSideLength; m_uBlockSideLengthPower = logBase2(m_uBlockSideLength); m_regValidRegionInBlocks.setLowerX(this->m_regValidRegion.getLowerX() >> m_uBlockSideLengthPower);