-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHplocKernel.h
294 lines (247 loc) · 8.62 KB
/
HplocKernel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <src/Common.h>
using namespace BvhConstruction;
template <typename T>
constexpr DEVICE T Log2(T n)
{
return n <= 1 ? 0 : 1 + Log2((n + 1) / 2);
}
template <typename T, typename U>
DEVICE T divideRoundUp(T value, U factor)
{
return (value + factor - 1) / factor;
}
extern "C" __global__ void SetupClusters(Bvh2Node* bvhNodes, PrimRef* __restrict__ primRefs, u32* __restrict__ sortedPrimIdx, Aabb* __restrict__ primitivesAabb, u32* __restrict__ nodeIndices, u32* __restrict__ parentIdx, u32 primCount)
{
u32 gIdx = blockIdx.x * blockDim.x + threadIdx.x;
if (gIdx >= primCount) return;
u32 primIdx = sortedPrimIdx[gIdx];
primRefs[gIdx].m_primIdx = primIdx;
primRefs[gIdx].m_aabb = primitivesAabb[primIdx];
nodeIndices[gIdx] = gIdx + (primCount - 1);
parentIdx[gIdx] = INVALID_NODE_IDX;
if (gIdx < (primCount - 1))
{
bvhNodes[gIdx].m_leftChildIdx = INVALID_NODE_IDX;
bvhNodes[gIdx].m_rightChildIdx = INVALID_NODE_IDX;
bvhNodes[gIdx].m_aabb.reset();
}
}
DEVICE uint64_t findHighestDiffBit(const u32* __restrict__ mortonCodes, int i, int j, int n)
{
if (i < 0 || j >= n) return ~0ull;
const uint64_t a = (static_cast<uint64_t>(mortonCodes[i]) << 32ull) | i;
const uint64_t b = (static_cast<uint64_t>(mortonCodes[j]) << 32ull) | j;
return a ^ b;
}
DEVICE int findParent(
const u32* __restrict__ mortonCodes,
int i,
int j,
int n)
{
if (i == 0 && j == n) return INVALID_NODE_IDX;
if (i == 0 || (j != n && findHighestDiffBit(mortonCodes, j , j + 1, n) < findHighestDiffBit(mortonCodes, i - 1, i, n)))
{
return j;
}
else
{
return i - 1;
}
}
DEVICE void findNearestNeighbours(u32 nPrims, u64* nearestNeighbours, u32* clusterIndices, Aabb* aabbSharedMem, Bvh2Node* bvhNodes, PrimRef* primRefs, u32 nInternalNodes)
{
const int laneIndex = threadIdx.x & (WarpSize - 1);
u64 minAreadAndIndex = u64(-1);
nearestNeighbours[laneIndex] = u64(-1);
__syncthreads();
if (laneIndex < nPrims)
{
Aabb aabb = aabbSharedMem[laneIndex];
for (int r = 1; r <= PlocRadius; r++)
{
if (laneIndex + r < WarpSize)
{
u32 neighbourIdx = (laneIndex + r);
if (neighbourIdx < nPrims)
{
//if (clusterIndices[laneIndex] != INVALID_NODE_IDX && clusterIndices[neighbourIdx] != INVALID_NODE_IDX)
{
Aabb neighbourAabb = aabbSharedMem[neighbourIdx];
neighbourAabb.grow(aabb);
float area = neighbourAabb.area();
u64 encode0 = (u64(__float_as_int(area)) << 32ull) | u64(neighbourIdx);
minAreadAndIndex = min(minAreadAndIndex, encode0);
u64 encode1 = (u64(__float_as_int(area)) << 32ull) | u64(laneIndex);
atomicMin(&nearestNeighbours[neighbourIdx], encode1);
}
}
}
}
}
atomicMin(&nearestNeighbours[laneIndex], minAreadAndIndex);
}
DEVICE u32 ScanWarpBinary(bool x)
{
int laneIdx = threadIdx.x & (WarpSize - 1);
u32 activeMask = __ballot(x);
return __popc(activeMask & ((1u << laneIdx) - 1));
}
DEVICE u32 mergeClusters(u32 nPrims, u64* nearestNeighbours, u32* clusterIndices, Aabb* aabbSharedMem, Bvh2Node* bvhNodes, PrimRef* primRefs, u32* nMergedClusters, u32 nInternalNodes)
{
const int laneIndex = threadIdx.x & (WarpSize - 1);
bool laneActive = laneIndex < nPrims;
u32 nodeIdx = INVALID_NODE_IDX;
bool merge = false;
Aabb aabb;
if (laneActive)
{
int currentClusterIdx = laneIndex;
aabb = aabbSharedMem[currentClusterIdx];
u32 leftChildIdx = clusterIndices[currentClusterIdx];
u32 neighbourIdx = (nearestNeighbours[currentClusterIdx] & 0xffffffff);
u32 rightChildIdx = clusterIndices[neighbourIdx];
u32 neighboursNeighbourIDx = (nearestNeighbours[neighbourIdx] & 0xffffffff);
clusterIndices[currentClusterIdx] = INVALID_NODE_IDX;
if (currentClusterIdx == neighboursNeighbourIDx)
{
if (currentClusterIdx < neighbourIdx)
{
merge = true;
}
else
{
aabb.reset();
nodeIdx = INVALID_NODE_IDX;
}
}
else
{
nodeIdx = leftChildIdx;
}
u32 nodeOffset = 0;
u32 totalNodesCreated = __popc(__ballot(merge));
if (laneIndex == 0) nodeOffset = atomicAdd(nMergedClusters, totalNodesCreated);
nodeOffset = __shfl(nodeOffset, 0);
u32 baseOffset = (nInternalNodes) - nodeOffset - totalNodesCreated;
u32 mergedNodeIdx = baseOffset + ScanWarpBinary(merge);
if (merge)
{
aabb.grow(aabbSharedMem[neighbourIdx]);
bvhNodes[mergedNodeIdx].m_leftChildIdx = leftChildIdx;
bvhNodes[mergedNodeIdx].m_rightChildIdx = rightChildIdx;
bvhNodes[mergedNodeIdx].m_aabb = aabb;
nodeIdx = mergedNodeIdx;
}
}
__syncthreads();
u32 newClusterIdx = ScanWarpBinary(nodeIdx != INVALID_NODE_IDX);
clusterIndices[newClusterIdx] = nodeIdx;
aabbSharedMem[newClusterIdx] = aabb ;
__syncthreads();
return __popc(__ballot(nodeIdx != INVALID_NODE_IDX));
}
DEVICE u32 loadIndices(u32 start, u32 end, u32* nodeIndices, u32* clusterIndices, Aabb* aabbSharedMem, Bvh2Node* bvhNodes, PrimRef* primRefs, u32 nInternalNodes, u32 offset)
{
const int laneIndex = threadIdx.x & (WarpSize - 1);
u32 nClusters = min(end - start, WarpSize / 2);
u32 nodeOffset = start + laneIndex;
bool isLaneActive = (laneIndex) < nClusters;
if (isLaneActive)
{
clusterIndices[laneIndex + offset] = nodeIndices[nodeOffset];
}
__syncthreads();
u32 nValids = (__popc(__ballot(clusterIndices[laneIndex] != INVALID_NODE_IDX)) - offset);
return min(nClusters, nValids);
}
DEVICE void storeIndices(u32 nClusters, u32* clusterIndices, u32* nodeIndices0, u32 Lstart)
{
const int laneIndex = threadIdx.x & (WarpSize - 1);
bool isLaneActive = laneIndex < nClusters;
if (isLaneActive)
{
nodeIndices0[Lstart + laneIndex] = clusterIndices[laneIndex];
}
__threadfence();
}
DEVICE void plocMerge(u32 laneId, u32 L, u32 R, u32 split, bool finalR, u32* nodeIndices0, Bvh2Node* bvhNodes, PrimRef* primRefs, u32* nMergedClusters, u32 nInternalNodes, Aabb* aabbSharedMem, u32* clusterIndices, u64* nearestNeighbours)
{
const int laneIndex = threadIdx.x & (WarpSize - 1);
u32 Lstart = __shfl(L, laneId);
u32 Lend = __shfl(split, laneId);
u32 Rstart = __shfl(split, laneId);
u32 Rend = __shfl(R, laneId) + 1;
nearestNeighbours[laneIndex] = (u64)-1;
clusterIndices[laneIndex] = INVALID_NODE_IDX;
aabbSharedMem[laneIndex].reset();
__syncthreads();
//load CIs
u32 nLeft = loadIndices(Lstart, Lend, nodeIndices0, clusterIndices, aabbSharedMem, bvhNodes, primRefs, nInternalNodes, 0);
u32 nRight = loadIndices(Rstart, Rend, nodeIndices0, clusterIndices, aabbSharedMem, bvhNodes, primRefs, nInternalNodes, nLeft);
u32 nPrims = nLeft + nRight;
u32 threshold = (__shfl(finalR, laneId) == true) ? 1 : (WarpSize / 2);
//load BBs
if (clusterIndices[laneIndex] != INVALID_NODE_IDX)
{
u32 nodeIdx = clusterIndices[laneIndex];
aabbSharedMem[laneIndex] = (nodeIdx >= nInternalNodes) ? primRefs[nodeIdx - nInternalNodes].m_aabb : bvhNodes[nodeIdx].m_aabb;
}
__syncthreads();
while(nPrims > threshold)
{
findNearestNeighbours(nPrims, nearestNeighbours, clusterIndices, aabbSharedMem, bvhNodes, primRefs, nInternalNodes);
nPrims = mergeClusters(nPrims, nearestNeighbours, clusterIndices, aabbSharedMem, bvhNodes, primRefs, nMergedClusters, nInternalNodes);
}
storeIndices(nLeft + nRight, clusterIndices, nodeIndices0, Lstart);
}
extern "C" __global__ void HPloc(Bvh2Node* bvhNodes, PrimRef* primRefs, u32* mortonCodes, u32* nodeIndices0, u32* parentIdx, u32* nMergedClusters, u32 nClusters, u32 nInternalNodes)
{
u32 gIdx = blockIdx.x * blockDim.x + threadIdx.x;
u32 L = gIdx;
u32 R = gIdx;
bool laneIsActive = (gIdx < nClusters);
alignas(alignof(Aabb)) __shared__ u8 aabbCache[sizeof(Aabb) * WarpSize];
__shared__ u32 clusterIndices[WarpSize];
__shared__ u64 nearestNeighbours[WarpSize];
Aabb* aabbSharedMem = reinterpret_cast<Aabb*>(aabbCache);
while (__ballot(laneIsActive))
{
u32 split = INVALID_VALUE;
if (laneIsActive)
{
u32 previousId = INVALID_NODE_IDX;
if (findParent(mortonCodes, L, R, nClusters) == R)
{
previousId = atomicExch(&parentIdx[R], L);
if (previousId != INVALID_NODE_IDX)
{
split = R + 1;
R = previousId;
}
}
else
{
previousId = atomicExch(&parentIdx[L - 1], R);
if (previousId != INVALID_NODE_IDX)
{
split = L;
L = previousId;
}
}
if (previousId == INVALID_NODE_IDX)
{
laneIsActive = false;
}
}//if laneIsActive end
u32 size = R - L + 1;
bool finalR = (laneIsActive && (size == nClusters)); //reached root need to stop
u32 waveMask = __ballot(laneIsActive && (size > WarpSize / 2) || finalR);
while (waveMask)
{
u32 laneId = __ffs(waveMask) - 1;
plocMerge(laneId, L, R, split, finalR, nodeIndices0, bvhNodes, primRefs, nMergedClusters, nInternalNodes, aabbSharedMem, clusterIndices, nearestNeighbours);
waveMask = waveMask & (waveMask - 1u);
}//end while
}//While ballot(laneIsActive) end
}