Replaced usage of our 'Array' class with native C array and got significant performance increase.

This commit is contained in:
David Williams 2014-08-21 00:06:20 +02:00
parent 458a534bf3
commit d0aa7cd60f
2 changed files with 22 additions and 12 deletions

View File

@ -156,6 +156,8 @@ namespace PolyVox
template< typename VolumeType, typename MeshType, typename ControllerType> template< typename VolumeType, typename MeshType, typename ControllerType>
class MarchingCubesSurfaceExtractor class MarchingCubesSurfaceExtractor
{ {
private:
const static int BitmaskSize = 128;
public: public:
MarchingCubesSurfaceExtractor(VolumeType* volData, Region region, MeshType* result, ControllerType controller, WrapMode eWrapMode = WrapModes::Border, typename VolumeType::VoxelType tBorderValue = typename VolumeType::VoxelType()); MarchingCubesSurfaceExtractor(VolumeType* volData, Region region, MeshType* result, ControllerType controller, WrapMode eWrapMode = WrapModes::Border, typename VolumeType::VoxelType tBorderValue = typename VolumeType::VoxelType());
@ -164,14 +166,14 @@ namespace PolyVox
private: private:
//Compute the cell bitmask for a particular slice in z. //Compute the cell bitmask for a particular slice in z.
template<bool isPrevZAvail> template<bool isPrevZAvail>
uint32_t computeBitmaskForSlice(const Array2DUint8& pPreviousBitmask, Array2DUint8& pCurrentBitmask); uint32_t computeBitmaskForSlice(uint8_t pPreviousBitmask[BitmaskSize][BitmaskSize], uint8_t pCurrentBitmask[BitmaskSize][BitmaskSize]);
//Compute the cell bitmask for a given cell. //Compute the cell bitmask for a given cell.
template<bool isPrevXAvail, bool isPrevYAvail, bool isPrevZAvail> template<bool isPrevXAvail, bool isPrevYAvail, bool isPrevZAvail>
void computeBitmaskForCell(const Array2DUint8& pPreviousBitmask, Array2DUint8& pCurrentBitmask, uint32_t uXRegSpace, uint32_t uYRegSpace); void computeBitmaskForCell(uint8_t pPreviousBitmask[BitmaskSize][BitmaskSize], uint8_t pCurrentBitmask[BitmaskSize][BitmaskSize], uint32_t uXRegSpace, uint32_t uYRegSpace);
//Use the cell bitmasks to generate all the vertices needed for that slice //Use the cell bitmasks to generate all the vertices needed for that slice
void generateVerticesForSlice(const Array2DUint8& pCurrentBitmask, void generateVerticesForSlice(uint8_t pCurrentBitmask[BitmaskSize][BitmaskSize],
Array2DInt32& m_pCurrentVertexIndicesX, Array2DInt32& m_pCurrentVertexIndicesX,
Array2DInt32& m_pCurrentVertexIndicesY, Array2DInt32& m_pCurrentVertexIndicesY,
Array2DInt32& m_pCurrentVertexIndicesZ); Array2DInt32& m_pCurrentVertexIndicesZ);
@ -291,7 +293,7 @@ namespace PolyVox
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
//Use the cell bitmasks to generate all the indices needed for that slice //Use the cell bitmasks to generate all the indices needed for that slice
void generateIndicesForSlice(const Array2DUint8& pPreviousBitmask, void generateIndicesForSlice(uint8_t pPreviousBitmask[BitmaskSize][BitmaskSize],
const Array2DInt32& m_pPreviousVertexIndicesX, const Array2DInt32& m_pPreviousVertexIndicesX,
const Array2DInt32& m_pPreviousVertexIndicesY, const Array2DInt32& m_pPreviousVertexIndicesY,
const Array2DInt32& m_pPreviousVertexIndicesZ, const Array2DInt32& m_pPreviousVertexIndicesZ,

View File

@ -52,6 +52,12 @@ namespace PolyVox
const uint32_t uArrayHeight = m_regSizeInVoxels.getUpperY() - m_regSizeInVoxels.getLowerY() + 1; const uint32_t uArrayHeight = m_regSizeInVoxels.getUpperY() - m_regSizeInVoxels.getLowerY() + 1;
const uint32_t arraySizes[2]= {uArrayWidth, uArrayHeight}; // Array dimensions const uint32_t arraySizes[2]= {uArrayWidth, uArrayHeight}; // Array dimensions
// PolyVox has a hardcoded limit on how big the regions it can extract are. This lets us allocate various
// arrays at compile time rather than run time. If you *really* need to you can increase this by changing
// the 'BitmaskSize' constant, but you should be sure you really know what you are doing.
POLYVOX_THROW_IF(uArrayWidth > BitmaskSize, std::invalid_argument, "Requested extraction region is too large");
POLYVOX_THROW_IF(uArrayHeight > BitmaskSize, std::invalid_argument, "Requested extraction region is too large");
//For edge indices //For edge indices
Array2DInt32 m_pPreviousVertexIndicesX(arraySizes); Array2DInt32 m_pPreviousVertexIndicesX(arraySizes);
Array2DInt32 m_pPreviousVertexIndicesY(arraySizes); Array2DInt32 m_pPreviousVertexIndicesY(arraySizes);
@ -60,8 +66,8 @@ namespace PolyVox
Array2DInt32 m_pCurrentVertexIndicesY(arraySizes); Array2DInt32 m_pCurrentVertexIndicesY(arraySizes);
Array2DInt32 m_pCurrentVertexIndicesZ(arraySizes); Array2DInt32 m_pCurrentVertexIndicesZ(arraySizes);
Array2DUint8 pPreviousBitmask(arraySizes); uint8_t pPreviousBitmask[BitmaskSize][BitmaskSize];
Array2DUint8 pCurrentBitmask(arraySizes); uint8_t pCurrentBitmask[BitmaskSize][BitmaskSize];
//Create a region corresponding to the first slice //Create a region corresponding to the first slice
m_regSlicePrevious = m_regSizeInVoxels; m_regSlicePrevious = m_regSizeInVoxels;
@ -86,7 +92,8 @@ namespace PolyVox
} }
std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1); std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1);
pPreviousBitmask.swap(pCurrentBitmask); //pPreviousBitmask.swap(pCurrentBitmask);
memcpy(pPreviousBitmask, pCurrentBitmask, BitmaskSize * BitmaskSize);
m_pPreviousVertexIndicesX.swap(m_pCurrentVertexIndicesX); m_pPreviousVertexIndicesX.swap(m_pCurrentVertexIndicesX);
m_pPreviousVertexIndicesY.swap(m_pCurrentVertexIndicesY); m_pPreviousVertexIndicesY.swap(m_pCurrentVertexIndicesY);
m_pPreviousVertexIndicesZ.swap(m_pCurrentVertexIndicesZ); m_pPreviousVertexIndicesZ.swap(m_pCurrentVertexIndicesZ);
@ -114,7 +121,8 @@ namespace PolyVox
} }
std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1); std::swap(uNoOfNonEmptyCellsForSlice0, uNoOfNonEmptyCellsForSlice1);
pPreviousBitmask.swap(pCurrentBitmask); //pPreviousBitmask.swap(pCurrentBitmask);
memcpy(pPreviousBitmask, pCurrentBitmask, BitmaskSize * BitmaskSize);
m_pPreviousVertexIndicesX.swap(m_pCurrentVertexIndicesX); m_pPreviousVertexIndicesX.swap(m_pCurrentVertexIndicesX);
m_pPreviousVertexIndicesY.swap(m_pCurrentVertexIndicesY); m_pPreviousVertexIndicesY.swap(m_pCurrentVertexIndicesY);
m_pPreviousVertexIndicesZ.swap(m_pCurrentVertexIndicesZ); m_pPreviousVertexIndicesZ.swap(m_pCurrentVertexIndicesZ);
@ -132,7 +140,7 @@ namespace PolyVox
template<typename VolumeType, typename MeshType, typename ControllerType> template<typename VolumeType, typename MeshType, typename ControllerType>
template<bool isPrevZAvail> template<bool isPrevZAvail>
uint32_t MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::computeBitmaskForSlice(const Array2DUint8& pPreviousBitmask, Array2DUint8& pCurrentBitmask) uint32_t MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::computeBitmaskForSlice(uint8_t pPreviousBitmask[BitmaskSize][BitmaskSize], uint8_t pCurrentBitmask[BitmaskSize][BitmaskSize])
{ {
m_uNoOfOccupiedCells = 0; m_uNoOfOccupiedCells = 0;
@ -198,7 +206,7 @@ namespace PolyVox
template<typename VolumeType, typename MeshType, typename ControllerType> template<typename VolumeType, typename MeshType, typename ControllerType>
template<bool isPrevXAvail, bool isPrevYAvail, bool isPrevZAvail> template<bool isPrevXAvail, bool isPrevYAvail, bool isPrevZAvail>
void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::computeBitmaskForCell(const Array2DUint8& pPreviousBitmask, Array2DUint8& pCurrentBitmask, uint32_t uXRegSpace, uint32_t uYRegSpace) void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::computeBitmaskForCell(uint8_t pPreviousBitmask[BitmaskSize][BitmaskSize], uint8_t pCurrentBitmask[BitmaskSize][BitmaskSize], uint32_t uXRegSpace, uint32_t uYRegSpace)
{ {
uint8_t iCubeIndex = 0; uint8_t iCubeIndex = 0;
@ -397,7 +405,7 @@ namespace PolyVox
} }
template<typename VolumeType, typename MeshType, typename ControllerType> template<typename VolumeType, typename MeshType, typename ControllerType>
void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::generateVerticesForSlice(const Array2DUint8& pCurrentBitmask, void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::generateVerticesForSlice(uint8_t pCurrentBitmask[BitmaskSize][BitmaskSize],
Array2DInt32& m_pCurrentVertexIndicesX, Array2DInt32& m_pCurrentVertexIndicesX,
Array2DInt32& m_pCurrentVertexIndicesY, Array2DInt32& m_pCurrentVertexIndicesY,
Array2DInt32& m_pCurrentVertexIndicesZ) Array2DInt32& m_pCurrentVertexIndicesZ)
@ -537,7 +545,7 @@ namespace PolyVox
} }
template<typename VolumeType, typename MeshType, typename ControllerType> template<typename VolumeType, typename MeshType, typename ControllerType>
void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::generateIndicesForSlice(const Array2DUint8& pPreviousBitmask, void MarchingCubesSurfaceExtractor<VolumeType, MeshType, ControllerType>::generateIndicesForSlice(uint8_t pPreviousBitmask[BitmaskSize][BitmaskSize],
const Array2DInt32& m_pPreviousVertexIndicesX, const Array2DInt32& m_pPreviousVertexIndicesX,
const Array2DInt32& m_pPreviousVertexIndicesY, const Array2DInt32& m_pPreviousVertexIndicesY,
const Array2DInt32& m_pPreviousVertexIndicesZ, const Array2DInt32& m_pPreviousVertexIndicesZ,