Skip to content

Commit ab56089

Browse files
committed
Additional explanatory comments and cleanup
Signed-off-by: Jonathan Swartz <jonathan@jswartz.info>
1 parent e564a47 commit ab56089

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

nanovdb/nanovdb/tools/cuda/VoxelBlockManager.cuh

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,40 +89,56 @@ struct VoxelBlockManager : nanovdb::tools::VoxelBlockManagerBase<Log2BlockWidth>
8989
NANOVDB_ASSERT(grid->isSequential());
9090
NANOVDB_ASSERT(blockDim.x <= 512);
9191

92-
// Select-based decode: one thread per OUTPUT slot. Here each
92+
// Select-based decode: one thread per output slot. Here each
9393
// slot ranks itself into its leaf via the jumpMap popcount,
9494
// then locates its voxel with the leaf's 9-bit prefix sums
9595
// plus an in-word bit select - O(1) per slot.
96-
int tID = threadIdx.x;
96+
const int tID = threadIdx.x;
9797
const auto *leaf0 = grid->tree().template getFirstNode<0>();
9898
for (int blockOffset = tID; blockOffset < BlockWidth; blockOffset += blockDim.x) {
9999
// rank this slot into its leaf: count leaves beginning at in-block positions
100100
// [1, blockOffset] (bit 0 is never set) via the jumpMap popcount
101101
uint32_t leafRank = 0;
102-
const int jumpWord = blockOffset >> 6;
102+
const int jumpWord = blockOffset >> 6; // index into jumpMap
103+
// count the number of leaves that begin before blockOffset
103104
#pragma unroll
104105
for (int i = 0; i < JumpMapLength; ++i) {
105-
if (i < jumpWord) leafRank += util::countOn(jumpMap[i]);
106-
else if (i == jumpWord) leafRank += util::countOn(jumpMap[i] & ((uint64_t(2) << (blockOffset & 63)) - 1u));
106+
if (i < jumpWord) leafRank += util::countOn(jumpMap[i]); // count leaves before the current jump word
107+
else if (i == jumpWord) {
108+
// count leaves in the current jump word, masking those that are before blockOffset
109+
leafRank += util::countOn(jumpMap[i] & ((uint64_t(2) << (blockOffset & 63)) - 1u));
110+
}
111+
107112
}
108113
const uint32_t leafID = firstLeafID + leafRank;
109114
const auto& leafData = *leaf0[leafID].data();
110-
const uint64_t rankInLeaf = (blockFirstOffset + blockOffset) - leafData.firstOffset();// 0-based rank among the leaf's actives
111-
if (rankInLeaf < leafData.valueCount()) {
115+
// 0-based rank among the leaf's actives
116+
const uint64_t rankInLeaf = (blockFirstOffset + blockOffset) - leafData.firstOffset();
117+
if (rankInLeaf < leafData.valueCount()) { // if the rank is within the leaf's active voxels (bounds check)
112118
// select the rankInLeaf-th active voxel: find its 64-bit mask word via the
113119
// leaf's 9-bit prefix sums, then its bit within that word
114-
uint32_t activesBeforeWord = 0;
115120
int wordID = 0;
121+
uint32_t activesBeforeWord = 0;
116122
#pragma unroll
117123
for (int candidateWord = 1; candidateWord < 8; ++candidateWord) {
124+
// the number of active voxels before the candidateWord's mask word (& 0x1ffu masks to 9 bits)
118125
const uint32_t cumulative = uint32_t(leafData.mPrefixSum >> (9*(candidateWord-1))) & 0x1ffu;
119-
if (cumulative <= rankInLeaf) { wordID = candidateWord; activesBeforeWord = cumulative; }
126+
if (cumulative <= rankInLeaf) {
127+
wordID = candidateWord; // word ID of the mask word that contains the rankInLeaf-th active voxel
128+
activesBeforeWord = cumulative; // the number of active voxels before the wordID's mask word
129+
}
120130
}
121-
uint32_t rankInWord = uint32_t(rankInLeaf) - activesBeforeWord;
131+
132+
uint32_t rankInWord = uint32_t(rankInLeaf) - activesBeforeWord; // active voxel's rank within the mask word (0-based)
133+
// select the in-word bit position of the voxel using __fns (find n-th set bit)
134+
// /__fns(mask, base, k) is the hardware find-nth-set-bit intrinsic - the k-th set bit (1-based) at/after base
135+
// but it's a 32-bit op while `maskWord` is 64-bit, so we need to split it into two 32-bit halves
122136
const uint64_t maskWord = leafData.mValueMask.words()[wordID];
123-
const uint32_t lowHalf = uint32_t(maskWord);
137+
const uint32_t lowHalf = uint32_t(maskWord); // low 32 bits of the mask word
124138
const uint32_t lowHalfCount = util::countOn(uint64_t(lowHalf));
125139
int bit;
140+
// if rank is less than the number of active voxels in the low half, __fns finds the bit in the lower half
141+
// otherwise, shift maskWord by 32 bits and __fns finds the bit in the upper half
126142
if (rankInWord < lowHalfCount) bit = __fns(lowHalf, 0, rankInWord + 1);
127143
else bit = 32 + __fns(uint32_t(maskWord >> 32), 0, rankInWord - lowHalfCount + 1);
128144
smem_leafIndex[blockOffset] = leafID;

0 commit comments

Comments
 (0)