From 5a5b2b3875f2dc0c1714fe2da5a6be4e2b750bea Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 6 Mar 2015 23:03:38 +0100 Subject: [PATCH] Spotted an optimization for computing the voxel position. --- include/PolyVox/PagedVolume.h | 1 + include/PolyVox/PagedVolume.inl | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/PolyVox/PagedVolume.h b/include/PolyVox/PagedVolume.h index f000bbb6..0f0df0c1 100644 --- a/include/PolyVox/PagedVolume.h +++ b/include/PolyVox/PagedVolume.h @@ -317,6 +317,7 @@ namespace PolyVox // The size of the chunks uint16_t m_uChunkSideLength; uint8_t m_uChunkSideLengthPower; + int32_t m_iChunkMask; Pager* m_pPager; }; diff --git a/include/PolyVox/PagedVolume.inl b/include/PolyVox/PagedVolume.inl index f87d347b..d3eb8764 100644 --- a/include/PolyVox/PagedVolume.inl +++ b/include/PolyVox/PagedVolume.inl @@ -48,6 +48,8 @@ namespace PolyVox // Used to perform multiplications and divisions by bit shifting. m_uChunkSideLengthPower = logBase2(m_uChunkSideLength); + // Use to perform modulo by bit operations + m_iChunkMask = m_uChunkSideLength - 1; // Calculate the number of chunks based on the memory limit and the size of each chunk. uint32_t uChunkSizeInBytes = PagedVolume::Chunk::calculateSizeInBytes(m_uChunkSideLength); @@ -98,7 +100,7 @@ namespace PolyVox template PagedVolume& PagedVolume::operator=(const PagedVolume& /*rhs*/) { - POLYVOX_THROW(not_implemented, "Volume assignment operator not implemented for performance reasons."); + POLYVOX_THROW(not_implemented, "Volume assignment operator not implemented to prevent accidental copying."); } //////////////////////////////////////////////////////////////////////////////// @@ -116,9 +118,9 @@ namespace PolyVox const int32_t chunkY = uYPos >> m_uChunkSideLengthPower; const int32_t chunkZ = uZPos >> m_uChunkSideLengthPower; - const uint16_t xOffset = static_cast(uXPos - (chunkX << m_uChunkSideLengthPower)); - const uint16_t yOffset = static_cast(uYPos - (chunkY << m_uChunkSideLengthPower)); - const uint16_t zOffset = static_cast(uZPos - (chunkZ << m_uChunkSideLengthPower)); + const uint16_t xOffset = static_cast(uXPos & m_iChunkMask); + const uint16_t yOffset = static_cast(uYPos & m_iChunkMask); + const uint16_t zOffset = static_cast(uZPos & m_iChunkMask); auto pChunk = getChunk(chunkX, chunkY, chunkZ); return pChunk->getVoxel(xOffset, yOffset, zOffset);