Skip to content

Commit ad26699

Browse files
committed
Fix the histogram loop
1 parent f4d7249 commit ad26699

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

include/nbl/builtin/hlsl/sort/counting.hlsl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,20 @@ struct counting
8484
histogram.atomicAdd(tid, sum);
8585

8686
const bool is_last_wg_invocation = tid == (GroupSize - 1);
87-
const uint16_t adjusted_key_bucket_count = ((KeyBucketCount - 1) / GroupSize + 1) * GroupSize;
87+
const static uint16_t RoundedKeyBucketCount = (KeyBucketCount - 1) / GroupSize + 1;
8888

89-
for (uint32_t vid = tid + GroupSize; vid < adjusted_key_bucket_count; vid += GroupSize)
89+
for (int i = 1; i < RoundedKeyBucketCount; i++)
9090
{
91+
uint32_t keyBucketStart = GroupSize * i;
92+
uint32_t vid = tid + keyBucketStart;
93+
9194
if (is_last_wg_invocation)
9295
{
93-
uint32_t startIndex = vid - tid;
94-
sdata.set(startIndex, sdata.get(startIndex) + sum);
96+
sdata.set(keyBucketStart, sdata.get(keyBucketStart) + sum);
9597
}
9698

97-
sum = inclusive_scan(sdata.get(vid), sdata);
99+
const uint32_t val = vid < KeyBucketCount ? sdata.get(vid) : 0;
100+
sum = inclusive_scan(val, sdata);
98101
histogram.atomicAdd(vid, sum);
99102
}
100103
}

0 commit comments

Comments
 (0)