Work on compression interface.

This commit is contained in:
Daviw Williams 2013-01-31 15:54:04 +01:00
parent 924744c5e6
commit 36676433be
10 changed files with 196 additions and 132 deletions

View File

@ -49,6 +49,7 @@ SET(CORE_INC_FILES
include/PolyVoxCore/BaseVolume.h include/PolyVoxCore/BaseVolume.h
include/PolyVoxCore/BaseVolume.inl include/PolyVoxCore/BaseVolume.inl
include/PolyVoxCore/BaseVolumeSampler.inl include/PolyVoxCore/BaseVolumeSampler.inl
include/PolyVoxCore/Compressor.h
include/PolyVoxCore/ConstVolumeProxy.h include/PolyVoxCore/ConstVolumeProxy.h
include/PolyVoxCore/CubicSurfaceExtractor.h include/PolyVoxCore/CubicSurfaceExtractor.h
include/PolyVoxCore/CubicSurfaceExtractor.inl include/PolyVoxCore/CubicSurfaceExtractor.inl

View File

@ -0,0 +1,42 @@
/*******************************************************************************
Copyright (c) 2005-2009 David Williams
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*******************************************************************************/
#ifndef __PolyVox_Compressor_H__
#define __PolyVox_Compressor_H__
#include "PolyVoxCore/Impl/TypeDef.h"
namespace PolyVox
{
class Compressor
{
public:
Compressor() {};
virtual ~Compressor() {};
virtual uint32_t compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) = 0;
virtual uint32_t decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength) = 0;
};
}
#endif //__PolyVox_Compressor_H__

View File

@ -25,6 +25,8 @@ freely, subject to the following restrictions:
#define __PolyVox_Block_H__ #define __PolyVox_Block_H__
#include "PolyVoxCore/Impl/TypeDef.h" #include "PolyVoxCore/Impl/TypeDef.h"
#include "PolyVoxCore/Compressor.h"
#include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Vector.h"
#include <limits> #include <limits>
@ -60,10 +62,9 @@ namespace PolyVox
uint32_t calculateSizeInBytes(void); uint32_t calculateSizeInBytes(void);
public: public:
void compress(void); void compress(Compressor* pCompressor);
void uncompress(void); void uncompress(Compressor* pCompressor);
std::vector< RunlengthEntry<uint16_t> > m_vecCompressedData;
void* m_pCompressedData; void* m_pCompressedData;
uint32_t m_uCompressedDataLength; uint32_t m_uCompressedDataLength;
VoxelType* m_tUncompressedData; VoxelType* m_tUncompressedData;

View File

@ -131,20 +131,21 @@ namespace PolyVox
m_bIsUncompressedDataModified = true; m_bIsUncompressedDataModified = true;
//For some reason blocks start out compressed. We should probably change this. //For some reason blocks start out compressed. We should probably change this.
compress(); compress(0);
} }
template <typename VoxelType> template <typename VoxelType>
uint32_t Block<VoxelType>::calculateSizeInBytes(void) uint32_t Block<VoxelType>::calculateSizeInBytes(void)
{ {
//FIXME - This function is incomplete.
uint32_t uSizeInBytes = sizeof(Block<VoxelType>); uint32_t uSizeInBytes = sizeof(Block<VoxelType>);
uSizeInBytes += m_vecCompressedData.capacity() * sizeof(RunlengthEntry<uint16_t>);
return uSizeInBytes; return uSizeInBytes;
} }
template <typename VoxelType> template <typename VoxelType>
void Block<VoxelType>::compress(void) void Block<VoxelType>::compress(Compressor* pCompressor)
{ {
//POLYVOX_ASSERT(pCompressor, "Compressor is not valid");
POLYVOX_ASSERT(m_bIsCompressed == false, "Attempted to compress block which is already flagged as compressed."); POLYVOX_ASSERT(m_bIsCompressed == false, "Attempted to compress block which is already flagged as compressed.");
POLYVOX_ASSERT(m_tUncompressedData != 0, "No uncompressed data is present."); POLYVOX_ASSERT(m_tUncompressedData != 0, "No uncompressed data is present.");
@ -175,8 +176,9 @@ namespace PolyVox
} }
template <typename VoxelType> template <typename VoxelType>
void Block<VoxelType>::uncompress(void) void Block<VoxelType>::uncompress(Compressor* pCompressor)
{ {
//POLYVOX_ASSERT(pCompressor, "Compressor is not valid");
POLYVOX_ASSERT(m_bIsCompressed == true, "Attempted to uncompress block which is not flagged as compressed."); POLYVOX_ASSERT(m_bIsCompressed == true, "Attempted to uncompress block which is not flagged as compressed.");
POLYVOX_ASSERT(m_tUncompressedData == 0, "Uncompressed data already exists."); POLYVOX_ASSERT(m_tUncompressedData == 0, "Uncompressed data already exists.");

View File

@ -26,6 +26,7 @@ freely, subject to the following restrictions:
#include "PolyVoxCore/BaseVolume.h" #include "PolyVoxCore/BaseVolume.h"
#include "Impl/Block.h" #include "Impl/Block.h"
#include "PolyVoxCore/Compressor.h"
#include "PolyVoxCore/Log.h" #include "PolyVoxCore/Log.h"
#include "PolyVoxCore/Region.h" #include "PolyVoxCore/Region.h"
#include "PolyVoxCore/Vector.h" #include "PolyVoxCore/Vector.h"
@ -256,6 +257,7 @@ namespace PolyVox
LargeVolume LargeVolume
( (
const Region& regValid, const Region& regValid,
Compressor* pCompressor = 0,
polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataRequiredHandler = 0, polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataRequiredHandler = 0,
polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataOverflowHandler = 0, polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataOverflowHandler = 0,
bool bPagingEnabled = false, bool bPagingEnabled = false,
@ -363,6 +365,9 @@ namespace PolyVox
uint16_t m_uBlockSideLength; uint16_t m_uBlockSideLength;
uint8_t m_uBlockSideLengthPower; uint8_t m_uBlockSideLengthPower;
//The compressor used by the Blocks to compress their data if required.
Compressor* m_pCompressor;
bool m_bCompressionEnabled; bool m_bCompressionEnabled;
bool m_bPagingEnabled; bool m_bPagingEnabled;
}; };

View File

@ -62,12 +62,14 @@ namespace PolyVox
LargeVolume<VoxelType>::LargeVolume LargeVolume<VoxelType>::LargeVolume
( (
const Region& regValid, const Region& regValid,
Compressor* pCompressor,
polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataRequiredHandler, polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataRequiredHandler,
polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataOverflowHandler, polyvox_function<void(const ConstVolumeProxy<VoxelType>&, const Region&)> dataOverflowHandler,
bool bPagingEnabled, bool bPagingEnabled,
uint16_t uBlockSideLength uint16_t uBlockSideLength
) )
:BaseVolume<VoxelType>(regValid) :BaseVolume<VoxelType>(regValid)
,m_pCompressor(pCompressor)
{ {
m_funcDataRequiredHandler = dataRequiredHandler; m_funcDataRequiredHandler = dataRequiredHandler;
m_funcDataOverflowHandler = dataOverflowHandler; m_funcDataOverflowHandler = dataOverflowHandler;
@ -456,7 +458,7 @@ namespace PolyVox
{ {
for(uint32_t ct = 0; ct < m_vecUncompressedBlockCache.size(); ct++) for(uint32_t ct = 0; ct < m_vecUncompressedBlockCache.size(); ct++)
{ {
m_vecUncompressedBlockCache[ct]->block.compress(); m_vecUncompressedBlockCache[ct]->block.compress(m_pCompressor);
} }
m_vecUncompressedBlockCache.clear(); m_vecUncompressedBlockCache.clear();
} }
@ -537,7 +539,7 @@ namespace PolyVox
if(m_vecUncompressedBlockCache[ct] == &(itBlock->second)) if(m_vecUncompressedBlockCache[ct] == &(itBlock->second))
{ {
// TODO: compression is unneccessary? or will not compressing this cause a memleak? // TODO: compression is unneccessary? or will not compressing this cause a memleak?
itBlock->second.block.compress(); itBlock->second.block.compress(m_pCompressor);
// put last object in cache here // put last object in cache here
m_vecUncompressedBlockCache[ct] = m_vecUncompressedBlockCache.back(); m_vecUncompressedBlockCache[ct] = m_vecUncompressedBlockCache.back();
// decrease cache size by one since last element is now in here twice // decrease cache size by one since last element is now in here twice
@ -667,7 +669,7 @@ namespace PolyVox
} }
//Compress the least recently used block. //Compress the least recently used block.
m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex]->block.compress(); m_vecUncompressedBlockCache[leastRecentlyUsedBlockIndex]->block.compress(m_pCompressor);
//We don't actually remove any elements from this vector, we //We don't actually remove any elements from this vector, we
//simply change the pointer to point at the new uncompressed bloack. //simply change the pointer to point at the new uncompressed bloack.
@ -678,7 +680,7 @@ namespace PolyVox
m_vecUncompressedBlockCache.push_back(&loadedBlock); m_vecUncompressedBlockCache.push_back(&loadedBlock);
} }
loadedBlock.block.uncompress(); loadedBlock.block.uncompress(m_pCompressor);
m_pLastAccessedBlock = &(loadedBlock.block); m_pLastAccessedBlock = &(loadedBlock.block);
POLYVOX_ASSERT(m_pLastAccessedBlock->m_tUncompressedData, "Block has no uncompressed data"); POLYVOX_ASSERT(m_pLastAccessedBlock->m_tUncompressedData, "Block has no uncompressed data");

View File

@ -1,16 +1,19 @@
#ifndef __PolyVox_MinizCompressor_H__ #ifndef __PolyVox_MinizCompressor_H__
#define __PolyVox_MinizCompressor_H__ #define __PolyVox_MinizCompressor_H__
#include "PolyVoxCore/Impl/TypeDef.h" #include "PolyVoxCore/Compressor.h"
class MinizCompressor namespace PolyVox
{ {
public: class MinizCompressor : public Compressor
MinizCompressor(); {
~MinizCompressor(); public:
MinizCompressor();
~MinizCompressor();
uint32_t compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); uint32_t compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength);
uint32_t decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); uint32_t decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength);
}; };
}
#endif //__PolyVox_MinizCompressor_H__ #endif //__PolyVox_MinizCompressor_H__

View File

@ -1,23 +1,26 @@
#ifndef __PolyVox_RLECompressor_H__ #ifndef __PolyVox_RLECompressor_H__
#define __PolyVox_RLECompressor_H__ #define __PolyVox_RLECompressor_H__
#include "PolyVoxCore/Impl/TypeDef.h" #include "PolyVoxCore/Compressor.h"
template<typename ValueType, typename LengthType> namespace PolyVox
class RLECompressor
{ {
struct Run template<typename ValueType, typename LengthType>
class RLECompressor : public Compressor
{ {
ValueType value; struct Run
LengthType length; {
}; ValueType value;
public: LengthType length;
RLECompressor(); };
~RLECompressor(); public:
RLECompressor();
~RLECompressor();
uint32_t compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); uint32_t compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength);
uint32_t decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength); uint32_t decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength);
}; };
}
#include "RLECompressor.inl" #include "RLECompressor.inl"

View File

@ -2,85 +2,87 @@
#include <cassert> #include <cassert>
#include <limits> #include <limits>
template<typename ValueType, typename LengthType> namespace PolyVox
RLECompressor<ValueType, LengthType>::RLECompressor()
{ {
} template<typename ValueType, typename LengthType>
RLECompressor<ValueType, LengthType>::RLECompressor()
template<typename ValueType, typename LengthType>
RLECompressor<ValueType, LengthType>::~RLECompressor()
{
}
template<typename ValueType, typename LengthType>
uint32_t RLECompressor<ValueType, LengthType>::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
{
assert(uSrcLength % sizeof(ValueType) == 0);
uSrcLength /= sizeof(ValueType);
uDstLength /= sizeof(Run);
ValueType* pSrcDataAsInt = reinterpret_cast<ValueType*>(pSrcData);
Run* pDstDataAsRun = reinterpret_cast<Run*>(pDstData);
ValueType* pSrcDataEnd = pSrcDataAsInt + uSrcLength;
Run* pDstDataEnd = pDstDataAsRun + uDstLength;
uint32_t uDstLengthInBytes = 0;
pDstDataAsRun->value = *pSrcDataAsInt;
pSrcDataAsInt++;
pDstDataAsRun->length = 1;
uDstLengthInBytes += sizeof(Run);
while(pSrcDataAsInt < pSrcDataEnd)
{ {
if((*pSrcDataAsInt == pDstDataAsRun->value) && (pDstDataAsRun->length < (std::numeric_limits<LengthType>::max)())) }
{
pDstDataAsRun->length++; template<typename ValueType, typename LengthType>
} RLECompressor<ValueType, LengthType>::~RLECompressor()
else {
{ }
pDstDataAsRun++;
assert(pDstDataAsRun < pDstDataEnd); template<typename ValueType, typename LengthType>
pDstDataAsRun->value = *pSrcDataAsInt; uint32_t RLECompressor<ValueType, LengthType>::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
pDstDataAsRun->length = 1; {
uDstLengthInBytes += sizeof(Run); assert(uSrcLength % sizeof(ValueType) == 0);
}
uSrcLength /= sizeof(ValueType);
uDstLength /= sizeof(Run);
ValueType* pSrcDataAsInt = reinterpret_cast<ValueType*>(pSrcData);
Run* pDstDataAsRun = reinterpret_cast<Run*>(pDstData);
ValueType* pSrcDataEnd = pSrcDataAsInt + uSrcLength;
Run* pDstDataEnd = pDstDataAsRun + uDstLength;
uint32_t uDstLengthInBytes = 0;
pDstDataAsRun->value = *pSrcDataAsInt;
pSrcDataAsInt++; pSrcDataAsInt++;
pDstDataAsRun->length = 1;
uDstLengthInBytes += sizeof(Run);
while(pSrcDataAsInt < pSrcDataEnd)
{
if((*pSrcDataAsInt == pDstDataAsRun->value) && (pDstDataAsRun->length < (std::numeric_limits<LengthType>::max)()))
{
pDstDataAsRun->length++;
}
else
{
pDstDataAsRun++;
assert(pDstDataAsRun < pDstDataEnd);
pDstDataAsRun->value = *pSrcDataAsInt;
pDstDataAsRun->length = 1;
uDstLengthInBytes += sizeof(Run);
}
pSrcDataAsInt++;
}
return uDstLengthInBytes;
} }
return uDstLengthInBytes; template<typename ValueType, typename LengthType>
} uint32_t RLECompressor<ValueType, LengthType>::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
template<typename ValueType, typename LengthType>
uint32_t RLECompressor<ValueType, LengthType>::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
{
assert(uSrcLength % sizeof(Run) == 0);
uSrcLength /= sizeof(Run);
uDstLength /= sizeof(ValueType);
Run* pSrcDataAsRun = reinterpret_cast<Run*>(pSrcData);
ValueType* pDstDataAsInt = reinterpret_cast<ValueType*>(pDstData);
Run* pSrcDataEnd = pSrcDataAsRun + uSrcLength;
ValueType* pDstDataEnd = pDstDataAsInt + uDstLength;
uint32_t uDstLengthInBytes = 0;
while(pSrcDataAsRun < pSrcDataEnd)
{ {
std::fill(pDstDataAsInt, pDstDataAsInt + pSrcDataAsRun->length, pSrcDataAsRun->value); assert(uSrcLength % sizeof(Run) == 0);
pDstDataAsInt += pSrcDataAsRun->length;
uDstLengthInBytes += pSrcDataAsRun->length * sizeof(ValueType); uSrcLength /= sizeof(Run);
uDstLength /= sizeof(ValueType);
pSrcDataAsRun++;
Run* pSrcDataAsRun = reinterpret_cast<Run*>(pSrcData);
ValueType* pDstDataAsInt = reinterpret_cast<ValueType*>(pDstData);
Run* pSrcDataEnd = pSrcDataAsRun + uSrcLength;
ValueType* pDstDataEnd = pDstDataAsInt + uDstLength;
uint32_t uDstLengthInBytes = 0;
while(pSrcDataAsRun < pSrcDataEnd)
{
std::fill(pDstDataAsInt, pDstDataAsInt + pSrcDataAsRun->length, pSrcDataAsRun->value);
pDstDataAsInt += pSrcDataAsRun->length;
uDstLengthInBytes += pSrcDataAsRun->length * sizeof(ValueType);
pSrcDataAsRun++;
}
return uDstLengthInBytes;
} }
return uDstLengthInBytes;
} }

View File

@ -14,32 +14,35 @@
// it is then to #include it directly which is what the examples do. // it is then to #include it directly which is what the examples do.
#include "PolyVoxCore/Impl/miniz.c" #include "PolyVoxCore/Impl/miniz.c"
MinizCompressor::MinizCompressor() namespace PolyVox
{ {
MinizCompressor::MinizCompressor()
{
}
MinizCompressor::~MinizCompressor()
{
}
uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
{
mz_ulong ulDstLength = uDstLength;
// Do the compression
int result = mz_compress((unsigned char*)pDstData, &ulDstLength, (const unsigned char*) pSrcData, uSrcLength);
assert(result == MZ_OK);
// Return the number of bytes written to the output.
return ulDstLength;
}
uint32_t MinizCompressor::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
{
mz_ulong ulDstLength = uDstLength;
int result = mz_uncompress((unsigned char*) pDstData, &ulDstLength, (const unsigned char*) pSrcData, uSrcLength);
assert(result == MZ_OK);
return ulDstLength;
}
} }
MinizCompressor::~MinizCompressor()
{
}
uint32_t MinizCompressor::compress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
{
mz_ulong ulDstLength = uDstLength;
// Do the compression
int result = mz_compress((unsigned char*)pDstData, &ulDstLength, (const unsigned char*) pSrcData, uSrcLength);
assert(result == MZ_OK);
// Return the number of bytes written to the output.
return ulDstLength;
}
uint32_t MinizCompressor::decompress(void* pSrcData, uint32_t uSrcLength, void* pDstData, uint32_t uDstLength)
{
mz_ulong ulDstLength = uDstLength;
int result = mz_uncompress((unsigned char*) pDstData, &ulDstLength, (const unsigned char*) pSrcData, uSrcLength);
assert(result == MZ_OK);
return ulDstLength;
}