From 69f6f4ac3713e7a9eee804773a516cc5c3412183 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 20 Mar 2015 23:09:38 +0100 Subject: [PATCH] Decided to always use a 64-bit chunk key, rather than trying to make it configurable. --- include/PolyVox/Impl/Config.h | 2 -- include/PolyVox/PagedVolume.h | 24 ++++++++++++------------ include/PolyVox/PagedVolume.inl | 10 +++++----- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/include/PolyVox/Impl/Config.h b/include/PolyVox/Impl/Config.h index 388f25d6..1a46222e 100644 --- a/include/PolyVox/Impl/Config.h +++ b/include/PolyVox/Impl/Config.h @@ -36,6 +36,4 @@ freely, subject to the following restrictions: //#define POLYVOX_ASSERTS_ENABLED #define POLYVOX_THROW_ENABLED -typedef int32_t PagedVolumeChunkKeyIntType; - #endif diff --git a/include/PolyVox/PagedVolume.h b/include/PolyVox/PagedVolume.h index 5477b6d1..066ff927 100644 --- a/include/PolyVox/PagedVolume.h +++ b/include/PolyVox/PagedVolume.h @@ -284,17 +284,17 @@ namespace PolyVox struct ChunkKey { ChunkKey(int32_t x, int32_t y, int32_t z) : iZPos(z), iYPos(y), iXPos(x), iValid(~0) {} - PagedVolumeChunkKeyIntType iZPos : 10; - PagedVolumeChunkKeyIntType iYPos : 10; - PagedVolumeChunkKeyIntType iXPos : 10; + int64_t iZPos : 10; + int64_t iYPos : 10; + int64_t iXPos : 10; // Should only be true when the last access chunk pointer is valid, // so we don't need to have a seperate check for that before using it. - PagedVolumeChunkKeyIntType iValid : 2; + int64_t iValid : 2; }; - static_assert(sizeof(ChunkKey) == sizeof(PagedVolumeChunkKeyIntType), ""); + static_assert(sizeof(ChunkKey) == sizeof(int64_t), ""); - PagedVolumeChunkKeyIntType posToChunkKey(int32_t iXPos, int32_t iYPos, int32_t iZPos) const + int64_t posToChunkKey(int32_t iXPos, int32_t iYPos, int32_t iZPos) const { iXPos = iXPos >> m_uChunkSideLengthPower; iYPos = iYPos >> m_uChunkSideLengthPower; @@ -306,22 +306,22 @@ namespace PolyVox // In general, bit shifting signed integers is dubious because of potential undefined or implementation defied behaviour. // However, it seems that in practice most compilers and architectures work in the same way (http://stackoverflow.com/a/6488645). - static_assert((int32_t(-3) / 4) == 0, "fdfs"); - static_assert((int32_t(-3) >> 2) == -1, "fdfs"); + static_assert((int64_t(-3) / 4) == 0, "fdfs"); + static_assert((int64_t(-3) >> 2) == -1, "fdfs"); /*chunkKey.iXPos = iXPos; chunkKey.iYPos = iYPos; chunkKey.iZPos = iZPos;*/ // If this kind of casting ever causes problems there are // other solutions here: http://stackoverflow.com/a/2468738 - PagedVolumeChunkKeyIntType iKeyAsInt32 = force_cast(chunkKey); + int64_t iKeyAsInt64 = force_cast(chunkKey); - return iKeyAsInt32; + return iKeyAsInt64; } - Chunk* getChunk(PagedVolumeChunkKeyIntType iKeyAsInt32) const; + Chunk* getChunk(int64_t iKeyAsInt32) const; - mutable PagedVolumeChunkKeyIntType m_v3dLastAccessedChunkKey = 0; + mutable int64_t m_v3dLastAccessedChunkKey = 0; mutable Chunk* m_pLastAccessedChunk = nullptr; mutable std::unordered_map > m_mapChunks; diff --git a/include/PolyVox/PagedVolume.inl b/include/PolyVox/PagedVolume.inl index 15c831fd..5e2a8152 100644 --- a/include/PolyVox/PagedVolume.inl +++ b/include/PolyVox/PagedVolume.inl @@ -115,7 +115,7 @@ namespace PolyVox template VoxelType PagedVolume::getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const { - const PagedVolumeChunkKeyIntType key = posToChunkKey(uXPos, uYPos, uZPos); + const int64_t key = posToChunkKey(uXPos, uYPos, uZPos); // Only call get chunk if we can't reuse the chunk pointer from the last voxel access. auto pChunk = (key == m_v3dLastAccessedChunkKey) ? m_pLastAccessedChunk : getChunk(key); @@ -147,7 +147,7 @@ namespace PolyVox template void PagedVolume::setVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, VoxelType tValue) { - const PagedVolumeChunkKeyIntType key = posToChunkKey(uXPos, uYPos, uZPos); + const int64_t key = posToChunkKey(uXPos, uYPos, uZPos); // Only call get chunk if we can't reuse the chunk pointer from the last voxel access. auto pChunk = (key == m_v3dLastAccessedChunkKey) ? m_pLastAccessedChunk : getChunk(key); @@ -202,7 +202,7 @@ namespace PolyVox { for(int32_t z = v3dStart.getZ(); z <= v3dEnd.getZ(); z++) { - const PagedVolumeChunkKeyIntType key = posToChunkKey(x, y, z); + const int64_t key = posToChunkKey(x, y, z); // Note that we don't check against the last chunk here. We're // not accessing the voxels, we just want to pull them into memory. @@ -256,7 +256,7 @@ namespace PolyVox { for(int32_t z = v3dStart.getZ(); z <= v3dEnd.getZ(); z++) { - const PagedVolumeChunkKeyIntType key = posToChunkKey(x, y, z); + const int64_t key = posToChunkKey(x, y, z); m_mapChunks.erase(key); } } @@ -264,7 +264,7 @@ namespace PolyVox } template - typename PagedVolume::Chunk* PagedVolume::getChunk(PagedVolumeChunkKeyIntType iKeyAsInt32) const + typename PagedVolume::Chunk* PagedVolume::getChunk(int64_t iKeyAsInt32) const { // This function is relatively large and slow because it involves searching for a chunk and creating it if it is not found. A natural // optimization is to only do this work if the chunk we are accessing is not the same as the last chunk we accessed (which it usually