Tidying and renaming...

This commit is contained in:
David Williams 2013-07-16 15:59:06 +02:00
parent 0cfb9f5196
commit 3904c9aa8f
4 changed files with 52 additions and 51 deletions

View File

@ -77,7 +77,7 @@ namespace PolyVox
uint8_t* buffer = new uint8_t[fileSizeInBytes]; uint8_t* buffer = new uint8_t[fileSizeInBytes];
fread(buffer, sizeof(uint8_t), fileSizeInBytes, pFile); fread(buffer, sizeof(uint8_t), fileSizeInBytes, pFile);
pBlockData->setCompressedData(buffer, fileSizeInBytes); pBlockData->setData(buffer, fileSizeInBytes);
delete[] buffer; delete[] buffer;
if(ferror(pFile)) if(ferror(pFile))
@ -115,7 +115,7 @@ namespace PolyVox
POLYVOX_THROW(std::runtime_error, "Unable to open file to write out block data."); POLYVOX_THROW(std::runtime_error, "Unable to open file to write out block data.");
} }
fwrite(pBlockData->getCompressedData(), sizeof(uint8_t), pBlockData->getCompressedDataLength(), pFile); fwrite(pBlockData->getData(), sizeof(uint8_t), pBlockData->getDataSizeInBytes(), pFile);
if(ferror(pFile)) if(ferror(pFile))
{ {

View File

@ -35,26 +35,42 @@ freely, subject to the following restrictions:
namespace PolyVox namespace PolyVox
{ {
template <typename VoxelType> template <typename VoxelType>
class CompressedBlock class Block
{
friend LargeVolume<VoxelType>;
public:
Block()
:m_uBlockLastAccessed(0)
,m_bDataModified(false)
{
}
protected:
uint32_t m_uBlockLastAccessed;
bool m_bDataModified;
};
template <typename VoxelType>
class CompressedBlock : public Block<VoxelType>
{ {
public: public:
CompressedBlock(uint16_t uSideLength, Compressor* pCompressor); CompressedBlock();
const uint8_t* getCompressedData(void) const; const uint8_t* getData(void) const;
uint32_t getCompressedDataLength(void) const; uint32_t getDataSizeInBytes(void) const;
void setCompressedData(const uint8_t* const data, uint32_t dataLength); void setData(const uint8_t* const pData, uint32_t uDataSizeInBytes);
uint32_t calculateSizeInBytes(void); uint32_t calculateSizeInBytes(void);
public: public:
uint8_t* m_pCompressedData; uint8_t* m_pData;
uint32_t m_uCompressedDataLength; uint32_t m_uDataSizeInBytes;
uint32_t timestamp;
}; };
template <typename VoxelType> template <typename VoxelType>
class UncompressedBlock class UncompressedBlock : public Block<VoxelType>
{ {
public: public:
UncompressedBlock(uint16_t uSideLength); UncompressedBlock(uint16_t uSideLength);
@ -70,7 +86,6 @@ namespace PolyVox
VoxelType* m_tUncompressedData; VoxelType* m_tUncompressedData;
uint16_t m_uSideLength; uint16_t m_uSideLength;
uint8_t m_uSideLengthPower; uint8_t m_uSideLengthPower;
bool m_bIsUncompressedDataModified;
}; };
} }

View File

@ -38,52 +38,39 @@ namespace PolyVox
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
CompressedBlock<VoxelType>::CompressedBlock(uint16_t uSideLength, Compressor* pCompressor) CompressedBlock<VoxelType>::CompressedBlock()
:m_pCompressedData(0) :m_pData(0)
,m_uCompressedDataLength(0) ,m_uDataSizeInBytes(0)
{ {
if(uSideLength == 0)
{
POLYVOX_THROW(std::invalid_argument, "Block side length cannot be zero.");
}
if(!isPowerOf2(uSideLength))
{
POLYVOX_THROW(std::invalid_argument, "Block side length must be a power of two.");
}
if(pCompressor == 0)
{
POLYVOX_THROW(std::invalid_argument, "Block must be provided with a valid compressor.");
}
} }
template <typename VoxelType> template <typename VoxelType>
const uint8_t* CompressedBlock<VoxelType>::getCompressedData(void) const const uint8_t* CompressedBlock<VoxelType>::getData(void) const
{ {
POLYVOX_ASSERT(m_pCompressedData, "Compressed data is NULL"); return m_pData;
return m_pCompressedData;
} }
template <typename VoxelType> template <typename VoxelType>
uint32_t CompressedBlock<VoxelType>::getCompressedDataLength(void) const uint32_t CompressedBlock<VoxelType>::getDataSizeInBytes(void) const
{ {
POLYVOX_ASSERT(m_pCompressedData, "Compressed data is NULL"); return m_uDataSizeInBytes;
return m_uCompressedDataLength;
} }
template <typename VoxelType> template <typename VoxelType>
void CompressedBlock<VoxelType>::setCompressedData(const uint8_t* const data, uint32_t dataLength) void CompressedBlock<VoxelType>::setData(const uint8_t* const pData, uint32_t uDataSizeInBytes)
{ {
//POLYVOX_ASSERT(m_pCompressedData, "Compressed data is NULL"); POLYVOX_THROW_IF(pData == 0, std::invalid_argument, "Pointer to data cannot be null");
POLYVOX_ASSERT(m_pCompressedData != data, "Attempting to copy data onto itself"); POLYVOX_THROW_IF(m_pData == pData, std::invalid_argument, "Attempting to copy data onto itself");
delete[] m_pCompressedData; // Delete any existing data
delete[] m_pData;
m_uCompressedDataLength = dataLength; // Allocate new data
m_pCompressedData = new uint8_t[dataLength]; m_uDataSizeInBytes = uDataSizeInBytes;
memcpy(m_pCompressedData, data, dataLength); m_pData = new uint8_t[uDataSizeInBytes];
// Copy the data across
memcpy(m_pData, pData, uDataSizeInBytes);
} }
template <typename VoxelType> template <typename VoxelType>
@ -91,7 +78,7 @@ namespace PolyVox
{ {
// Returns the size of this class plus the size of the compressed data. It // Returns the size of this class plus the size of the compressed data. It
// doesn't include the uncompressed data cache as that is owned by the volume. // doesn't include the uncompressed data cache as that is owned by the volume.
uint32_t uSizeInBytes = sizeof(CompressedBlock<VoxelType>) + m_uCompressedDataLength; uint32_t uSizeInBytes = sizeof(CompressedBlock<VoxelType>) + m_uDataSizeInBytes;
return uSizeInBytes; return uSizeInBytes;
} }
@ -102,7 +89,6 @@ namespace PolyVox
:m_tUncompressedData(0) :m_tUncompressedData(0)
,m_uSideLength(0) ,m_uSideLength(0)
,m_uSideLengthPower(0) ,m_uSideLengthPower(0)
,m_bIsUncompressedDataModified(true)
{ {
// Compute the side length // Compute the side length
m_uSideLength = uSideLength; m_uSideLength = uSideLength;
@ -165,7 +151,7 @@ namespace PolyVox
uZPos * m_uSideLength * m_uSideLength uZPos * m_uSideLength * m_uSideLength
] = tValue; ] = tValue;
m_bIsUncompressedDataModified = true; m_bDataModified = true;
} }
template <typename VoxelType> template <typename VoxelType>

View File

@ -596,7 +596,7 @@ namespace PolyVox
if(itBlock == m_pBlocks.end()) if(itBlock == m_pBlocks.end())
{ {
//The block is not in the map, so we will have to create a new block and add it. //The block is not in the map, so we will have to create a new block and add it.
CompressedBlock<VoxelType> newBlock(m_uBlockSideLength, m_pCompressor); CompressedBlock<VoxelType> newBlock;
itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, newBlock)).first; itBlock = m_pBlocks.insert(std::make_pair(v3dBlockPos, newBlock)).first;
// Now use the pager to fill the block with it's initial data. // Now use the pager to fill the block with it's initial data.
@ -611,7 +611,7 @@ namespace PolyVox
//Get the block and mark that we accessed it //Get the block and mark that we accessed it
CompressedBlock<VoxelType>& block = itBlock->second; CompressedBlock<VoxelType>& block = itBlock->second;
block.timestamp = ++m_uTimestamper; block.m_uBlockLastAccessed = ++m_uTimestamper;
//m_v3dLastAccessedBlockPos = v3dBlockPos; //m_v3dLastAccessedBlockPos = v3dBlockPos;
return &block; return &block;
@ -641,9 +641,9 @@ namespace PolyVox
{ {
UncompressedBlock<VoxelType>* pUncompressedBlock = new UncompressedBlock<VoxelType>(m_uBlockSideLength); UncompressedBlock<VoxelType>* pUncompressedBlock = new UncompressedBlock<VoxelType>(m_uBlockSideLength);
void* pSrcData = reinterpret_cast<void*>(block->m_pCompressedData); void* pSrcData = reinterpret_cast<void*>(block->m_pData);
void* pDstData = reinterpret_cast<void*>(pUncompressedBlock->m_tUncompressedData); void* pDstData = reinterpret_cast<void*>(pUncompressedBlock->m_tUncompressedData);
uint32_t uSrcLength = block->m_uCompressedDataLength; uint32_t uSrcLength = block->m_uDataSizeInBytes;
uint32_t uDstLength = m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength * sizeof(VoxelType); uint32_t uDstLength = m_uBlockSideLength * m_uBlockSideLength * m_uBlockSideLength * sizeof(VoxelType);
//MinizCompressor compressor; //MinizCompressor compressor;
@ -724,7 +724,7 @@ namespace PolyVox
typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itUnloadBlock = m_pBlocks.begin(); typename std::map<Vector3DInt32, CompressedBlock<VoxelType>, BlockPositionCompare>::iterator itUnloadBlock = m_pBlocks.begin();
for(i = m_pBlocks.begin(); i != m_pBlocks.end(); i++) for(i = m_pBlocks.begin(); i != m_pBlocks.end(); i++)
{ {
if(i->second.timestamp < itUnloadBlock->second.timestamp) if(i->second.m_uBlockLastAccessed < itUnloadBlock->second.m_uBlockLastAccessed)
{ {
itUnloadBlock = i; itUnloadBlock = i;
} }