diff --git a/include/PolyVox/PagedVolume.h b/include/PolyVox/PagedVolume.h index 852ebe60..105e50df 100644 --- a/include/PolyVox/PagedVolume.h +++ b/include/PolyVox/PagedVolume.h @@ -285,17 +285,36 @@ namespace PolyVox ChunkKey posToChunkKey(int32_t iXPos, int32_t iYPos, int32_t iZPos) const { + int32_t iBlockX = iXPos >> m_uChunkSideLengthPower; + int32_t iBlockY = iYPos >> m_uChunkSideLengthPower; + int32_t iBlockZ = iZPos >> m_uChunkSideLengthPower; + + int16_t iBlockXAsInt16 = static_cast(iBlockX); + int16_t iBlockYAsInt16 = static_cast(iBlockY); + int16_t iBlockZAsInt16 = static_cast(iBlockZ); + + uint16_t uBlockXAsUint16 = reinterpret_cast(iBlockXAsInt16); + uint16_t uBlockYAsUint16 = reinterpret_cast(iBlockYAsInt16); + uint16_t uBlockZAsUint16 = reinterpret_cast(iBlockZAsInt16); + + uint64_t uBlockXAsUint64 = static_cast(uBlockXAsUint16); + uint64_t uBlockYAsUint64 = static_cast(uBlockYAsUint16); + uint64_t uBlockZAsUint64 = static_cast(uBlockZAsUint16); + + uBlockXAsUint64 = uBlockXAsUint64 << 32; + uBlockYAsUint64 = uBlockYAsUint64 << 16; + // Bit-shifting of signed integer values has various issues with undefined or implementation-defined behaviour. // Therefore we cast to unsigned to avoid these (we only care about the bit pattern anyway, not the actual value). - const ChunkKey uXPos = static_cast(iXPos); + /*const ChunkKey uXPos = static_cast(iXPos); const ChunkKey uYPos = static_cast(iYPos); const ChunkKey uZPos = static_cast(iZPos); const ChunkKey xKey = ((uXPos >> m_uChunkSideLengthPower) & 0x3FF) << 20; const ChunkKey yKey = ((uYPos >> m_uChunkSideLengthPower) & 0x3FF) << 10; - const ChunkKey zKey = ((uZPos >> m_uChunkSideLengthPower) & 0x3FF); + const ChunkKey zKey = ((uZPos >> m_uChunkSideLengthPower) & 0x3FF);*/ - const ChunkKey key = 0x80000000 | xKey | yKey | zKey; + const ChunkKey key = 0xFFFF000000000000 | uBlockXAsUint64 | uBlockYAsUint64 | uBlockZAsUint64; return key; } diff --git a/include/PolyVox/PagedVolume.inl b/include/PolyVox/PagedVolume.inl index b93fc2de..8d80c0f1 100644 --- a/include/PolyVox/PagedVolume.inl +++ b/include/PolyVox/PagedVolume.inl @@ -293,14 +293,33 @@ namespace PolyVox // If we still haven't found the chunk then it's time to create a new one and page it in from disk. if (!pChunk) { - const int32_t uChunkX = (iKeyAsInt32 >> 20) & 0x3FF; + uint64_t uBlockXAsUint64 = iKeyAsInt32; + uint64_t uBlockYAsUint64 = iKeyAsInt32; + uint64_t uBlockZAsUint64 = iKeyAsInt32; + + uBlockXAsUint64 = uBlockXAsUint64 >> 32; + uBlockYAsUint64 = uBlockYAsUint64 >> 16; + + uBlockXAsUint64 = uBlockXAsUint64 & 0xFFFF; + uBlockYAsUint64 = uBlockYAsUint64 & 0xFFFF; + uBlockZAsUint64 = uBlockZAsUint64 & 0xFFFF; + + uint16_t uBlockXAsUint16 = static_cast(uBlockXAsUint64); + uint16_t uBlockYAsUint16 = static_cast(uBlockYAsUint64); + uint16_t uBlockZAsUint16 = static_cast(uBlockZAsUint64); + + int16_t uBlockXAsInt16 = reinterpret_cast(uBlockXAsUint16); + int16_t uBlockYAsInt16 = reinterpret_cast(uBlockYAsUint16); + int16_t uBlockZAsInt16 = reinterpret_cast(uBlockZAsUint16); + + /*const int32_t uChunkX = (iKeyAsInt32 >> 20) & 0x3FF; const int32_t uChunkY = (iKeyAsInt32 >> 10) & 0x3FF; - const int32_t uChunkZ = (iKeyAsInt32) & 0x3FF; + const int32_t uChunkZ = (iKeyAsInt32) & 0x3FF;*/ // The chunk was not found so we will create a new one. /*ChunkKeyConverter converter; converter.i = key;*/ //ChunkKey realKey = force_cast(iKeyAsInt32); - Vector3DInt32 v3dChunkPos(uChunkX, uChunkY, uChunkZ); + Vector3DInt32 v3dChunkPos(uBlockXAsInt16, uBlockYAsInt16, uBlockZAsInt16); pChunk = new PagedVolume::Chunk(v3dChunkPos, m_uChunkSideLength, m_pPager); pChunk->m_uChunkLastAccessed = ++m_uTimestamper; // Important, as we may soon delete the oldest chunk m_mapChunks.insert(std::make_pair(iKeyAsInt32, std::unique_ptr(pChunk)));