-
Notifications
You must be signed in to change notification settings - Fork 8
/
TraversalKernelCWBVH.cu
426 lines (344 loc) · 15.3 KB
/
TraversalKernelCWBVH.cu
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <cuda_profiler_api.h>
#include "helper_math.h"
#include "FastDeviceMinMax.h"
#include "Logger.h"
#include "CUDAAssert.h"
__device__ unsigned __bfind(unsigned i) { unsigned b; asm volatile("bfind.u32 %0, %1; " : "=r"(b) : "r"(i)); return b; }
__device__ __inline__ uint sign_extend_s8x4(uint i) { uint v; asm("prmt.b32 %0, %1, 0x0, 0x0000BA98;" : "=r"(v) : "r"(i)); return v; }
__device__ __inline__ uint extract_byte(uint i, uint n) { return (i >> (n * 8)) & 0xFF; }
__device__ const float4* BVHTreeNodes;
__device__ const float4* TriangleWoopCoordinates;
__device__ const int* MappingFromTriangleAddressToIndex;
#define DYNAMIC_FETCH 1
#define TRIANGLE_POSTPONING 1
#define STACK_POP(X) { --stackPtr; if (stackPtr < SM_STACK_SIZE) X = traversalStackSM[threadIdx.x][threadIdx.y][stackPtr]; else X = traversalStack[stackPtr - SM_STACK_SIZE]; }
#define STACK_PUSH(X) { if (stackPtr < SM_STACK_SIZE) traversalStackSM[threadIdx.x][threadIdx.y][stackPtr] = X; else traversalStack[stackPtr - SM_STACK_SIZE] = X; stackPtr++; }
__global__ void rtTraceCWBVHDynamicFetch(
Ray* rayBuffer,
Hit* rayResultBuffer,
int rayCount,
int* finishedRayCount
)
{
const float ooeps = exp2f(-80.0f);
const int STACK_SIZE = 32;
uint2 traversalStack[STACK_SIZE];
const int SM_STACK_SIZE = 8; // Slightly smaller stack size than the paper (12), as this seems faster on my GTX1080
__shared__ uint2 traversalStackSM[32][2][SM_STACK_SIZE];
int rayidx;
float3 orig, dir;
float tmin, tmax;
float idirx, idiry, idirz;
uint octinv;
uint2 nodeGroup = make_uint2(0);
uint2 triangleGroup = make_uint2(0);
char stackPtr = 0;
int hitAddr = -1;
float2 triangleuv;
__shared__ int nextRayArray[2];
const float4* localBVHTreeNodes = BVHTreeNodes;
const float4* localTriangleWoopCoordinates = TriangleWoopCoordinates;
do
{
int& rayBase = nextRayArray[threadIdx.y];
bool terminated = stackPtr == 0 && nodeGroup.y <= 0x00FFFFFF && triangleGroup.y == 0;
const unsigned int maskTerminated = __ballot_sync(__activemask(), terminated);
const int numTerminated = __popc(maskTerminated);
const int idxTerminated = __popc(maskTerminated & ((1u << threadIdx.x) - 1));
if (terminated)
{
if (idxTerminated == 0)
rayBase = atomicAdd(finishedRayCount, numTerminated);
rayidx = rayBase + idxTerminated;
if (rayidx >= rayCount)
break;
orig = make_float3(rayBuffer[rayidx].origin_tmin);
dir = make_float3(rayBuffer[rayidx].dir_tmax);
tmin = rayBuffer[rayidx].origin_tmin.w;
tmax = rayBuffer[rayidx].dir_tmax.w;
idirx = 1.0f / (fabsf(dir.x) > ooeps ? dir.x : copysignf(ooeps, dir.x)); // inverse ray direction
idiry = 1.0f / (fabsf(dir.y) > ooeps ? dir.y : copysignf(ooeps, dir.y)); // inverse ray direction
idirz = 1.0f / (fabsf(dir.z) > ooeps ? dir.z : copysignf(ooeps, dir.z)); // inverse ray direction
octinv = ((dir.x < 0 ? 1 : 0) << 2) | ((dir.y < 0 ? 1 : 0) << 1) | ((dir.z < 0 ? 1 : 0) << 0);
octinv = 7 - octinv;
nodeGroup = make_uint2(0, 0b10000000000000000000000000000000);
triangleGroup = make_uint2(0);
stackPtr = 0;
hitAddr = -1;
}
#if DYNAMIC_FETCH
int lostLoopIterations = 0;
#endif
do
{
if (nodeGroup.y > 0x00FFFFFF)
{
const unsigned int hits = nodeGroup.y;
const unsigned int imask = nodeGroup.y;
const unsigned int child_bit_index = __bfind(hits);
const unsigned int child_node_base_index = nodeGroup.x;
nodeGroup.y &= ~(1 << child_bit_index);
if (nodeGroup.y > 0x00FFFFFF)
{
STACK_PUSH(nodeGroup);
}
{
const unsigned int slot_index = (child_bit_index - 24) ^ octinv;
const unsigned int octinv4 = octinv * 0x01010101u;
const unsigned int relative_index = __popc(imask & ~(0xFFFFFFFF << slot_index));
const unsigned int child_node_index = child_node_base_index + relative_index;
float4 n0, n1, n2, n3, n4;
n0 = __ldg(localBVHTreeNodes + child_node_index * 5 + 0);
n1 = __ldg(localBVHTreeNodes + child_node_index * 5 + 1);
n2 = __ldg(localBVHTreeNodes + child_node_index * 5 + 2);
n3 = __ldg(localBVHTreeNodes + child_node_index * 5 + 3);
n4 = __ldg(localBVHTreeNodes + child_node_index * 5 + 4);
float3 p = make_float3(n0);
int3 e;
e.x = *((char*)&n0.w + 0);
e.y = *((char*)&n0.w + 1);
e.z = *((char*)&n0.w + 2);
nodeGroup.x = float_as_uint(n1.x);
triangleGroup.x = float_as_uint(n1.y);
triangleGroup.y = 0;
unsigned int hitmask = 0;
const float adjusted_idirx = uint_as_float((e.x + 127) << 23) * idirx;
const float adjusted_idiry = uint_as_float((e.y + 127) << 23) * idiry;
const float adjusted_idirz = uint_as_float((e.z + 127) << 23) * idirz;
const float origx = -(orig.x - p.x) * idirx;
const float origy = -(orig.y - p.y) * idiry;
const float origz = -(orig.z - p.z) * idirz;
{
// First 4
const unsigned int meta4 = float_as_uint(n1.z);
const unsigned int is_inner4 = (meta4 & (meta4 << 1)) & 0x10101010;
const unsigned int inner_mask4 = sign_extend_s8x4(is_inner4 << 3);
const unsigned int bit_index4 = (meta4 ^ (octinv4 & inner_mask4)) & 0x1F1F1F1F;
const unsigned int child_bits4 = (meta4 >> 5) & 0x07070707;
// Potential micro-optimization: use PRMT to do the selection here, as described by the paper
uint swizzledLox = (idirx < 0) ? float_as_uint(n3.z) : float_as_uint(n2.x);
uint swizzledHix = (idirx < 0) ? float_as_uint(n2.x) : float_as_uint(n3.z);
uint swizzledLoy = (idiry < 0) ? float_as_uint(n4.x) : float_as_uint(n2.z);
uint swizzledHiy = (idiry < 0) ? float_as_uint(n2.z) : float_as_uint(n4.x);
uint swizzledLoz = (idirz < 0) ? float_as_uint(n4.z) : float_as_uint(n3.x);
uint swizzledHiz = (idirz < 0) ? float_as_uint(n3.x) : float_as_uint(n4.z);
float tminx[4];
float tminy[4];
float tminz[4];
float tmaxx[4];
float tmaxy[4];
float tmaxz[4];
tminx[0] = ((swizzledLox >> 0) & 0xFF) * adjusted_idirx + origx;
tminx[1] = ((swizzledLox >> 8) & 0xFF) * adjusted_idirx + origx;
tminx[2] = ((swizzledLox >> 16) & 0xFF) * adjusted_idirx + origx;
tminx[3] = ((swizzledLox >> 24) & 0xFF) * adjusted_idirx + origx;
tminy[0] = ((swizzledLoy >> 0) & 0xFF) * adjusted_idiry + origy;
tminy[1] = ((swizzledLoy >> 8) & 0xFF) * adjusted_idiry + origy;
tminy[2] = ((swizzledLoy >> 16) & 0xFF) * adjusted_idiry + origy;
tminy[3] = ((swizzledLoy >> 24) & 0xFF) * adjusted_idiry + origy;
tminz[0] = ((swizzledLoz >> 0) & 0xFF) * adjusted_idirz + origz;
tminz[1] = ((swizzledLoz >> 8) & 0xFF) * adjusted_idirz + origz;
tminz[2] = ((swizzledLoz >> 16) & 0xFF) * adjusted_idirz + origz;
tminz[3] = ((swizzledLoz >> 24) & 0xFF) * adjusted_idirz + origz;
tmaxx[0] = ((swizzledHix >> 0) & 0xFF) * adjusted_idirx + origx;
tmaxx[1] = ((swizzledHix >> 8) & 0xFF) * adjusted_idirx + origx;
tmaxx[2] = ((swizzledHix >> 16) & 0xFF) * adjusted_idirx + origx;
tmaxx[3] = ((swizzledHix >> 24) & 0xFF) * adjusted_idirx + origx;
tmaxy[0] = ((swizzledHiy >> 0) & 0xFF) * adjusted_idiry + origy;
tmaxy[1] = ((swizzledHiy >> 8) & 0xFF) * adjusted_idiry + origy;
tmaxy[2] = ((swizzledHiy >> 16) & 0xFF) * adjusted_idiry + origy;
tmaxy[3] = ((swizzledHiy >> 24) & 0xFF) * adjusted_idiry + origy;
tmaxz[0] = ((swizzledHiz >> 0) & 0xFF) * adjusted_idirz + origz;
tmaxz[1] = ((swizzledHiz >> 8) & 0xFF) * adjusted_idirz + origz;
tmaxz[2] = ((swizzledHiz >> 16) & 0xFF) * adjusted_idirz + origz;
tmaxz[3] = ((swizzledHiz >> 24) & 0xFF) * adjusted_idirz + origz;
for (int childIndex = 0; childIndex < 4; childIndex++)
{
// Use VMIN, VMAX to compute the slabs
const float cmin = fmaxf(fmax_fmax(tminx[childIndex], tminy[childIndex], tminz[childIndex]), tmin);
const float cmax = fminf(fmin_fmin(tmaxx[childIndex], tmaxy[childIndex], tmaxz[childIndex]), tmax);
bool intersected = cmin <= cmax;
// Potential micro-optimization: use VSHL to implement this part, as described by the paper
if (intersected)
{
const unsigned int child_bits = extract_byte(child_bits4, childIndex);
const unsigned int bit_index = extract_byte(bit_index4, childIndex);
hitmask |= child_bits << bit_index;
}
}
}
{
// Second 4
const unsigned int meta4 = float_as_uint(n1.w);
const unsigned int is_inner4 = (meta4 & (meta4 << 1)) & 0x10101010;
const unsigned int inner_mask4 = sign_extend_s8x4(is_inner4 << 3);
const unsigned int bit_index4 = (meta4 ^ (octinv4 & inner_mask4)) & 0x1F1F1F1F;
const unsigned int child_bits4 = (meta4 >> 5) & 0x07070707;
// Potential micro-optimization: use PRMT to do the selection here, as described by the paper
uint swizzledLox = (idirx < 0) ? float_as_uint(n3.w) : float_as_uint(n2.y);
uint swizzledHix = (idirx < 0) ? float_as_uint(n2.y) : float_as_uint(n3.w);
uint swizzledLoy = (idiry < 0) ? float_as_uint(n4.y) : float_as_uint(n2.w);
uint swizzledHiy = (idiry < 0) ? float_as_uint(n2.w) : float_as_uint(n4.y);
uint swizzledLoz = (idirz < 0) ? float_as_uint(n4.w) : float_as_uint(n3.y);
uint swizzledHiz = (idirz < 0) ? float_as_uint(n3.y) : float_as_uint(n4.w);
float tminx[4];
float tminy[4];
float tminz[4];
float tmaxx[4];
float tmaxy[4];
float tmaxz[4];
tminx[0] = ((swizzledLox >> 0) & 0xFF) * adjusted_idirx + origx;
tminx[1] = ((swizzledLox >> 8) & 0xFF) * adjusted_idirx + origx;
tminx[2] = ((swizzledLox >> 16) & 0xFF) * adjusted_idirx + origx;
tminx[3] = ((swizzledLox >> 24) & 0xFF) * adjusted_idirx + origx;
tminy[0] = ((swizzledLoy >> 0) & 0xFF) * adjusted_idiry + origy;
tminy[1] = ((swizzledLoy >> 8) & 0xFF) * adjusted_idiry + origy;
tminy[2] = ((swizzledLoy >> 16) & 0xFF) * adjusted_idiry + origy;
tminy[3] = ((swizzledLoy >> 24) & 0xFF) * adjusted_idiry + origy;
tminz[0] = ((swizzledLoz >> 0) & 0xFF) * adjusted_idirz + origz;
tminz[1] = ((swizzledLoz >> 8) & 0xFF) * adjusted_idirz + origz;
tminz[2] = ((swizzledLoz >> 16) & 0xFF) * adjusted_idirz + origz;
tminz[3] = ((swizzledLoz >> 24) & 0xFF) * adjusted_idirz + origz;
tmaxx[0] = ((swizzledHix >> 0) & 0xFF) * adjusted_idirx + origx;
tmaxx[1] = ((swizzledHix >> 8) & 0xFF) * adjusted_idirx + origx;
tmaxx[2] = ((swizzledHix >> 16) & 0xFF) * adjusted_idirx + origx;
tmaxx[3] = ((swizzledHix >> 24) & 0xFF) * adjusted_idirx + origx;
tmaxy[0] = ((swizzledHiy >> 0) & 0xFF) * adjusted_idiry + origy;
tmaxy[1] = ((swizzledHiy >> 8) & 0xFF) * adjusted_idiry + origy;
tmaxy[2] = ((swizzledHiy >> 16) & 0xFF) * adjusted_idiry + origy;
tmaxy[3] = ((swizzledHiy >> 24) & 0xFF) * adjusted_idiry + origy;
tmaxz[0] = ((swizzledHiz >> 0) & 0xFF) * adjusted_idirz + origz;
tmaxz[1] = ((swizzledHiz >> 8) & 0xFF) * adjusted_idirz + origz;
tmaxz[2] = ((swizzledHiz >> 16) & 0xFF) * adjusted_idirz + origz;
tmaxz[3] = ((swizzledHiz >> 24) & 0xFF) * adjusted_idirz + origz;
for (int childIndex = 0; childIndex < 4; childIndex++)
{
// Use VMIN, VMAX to compute the slabs
const float cmin = fmaxf(fmax_fmax(tminx[childIndex], tminy[childIndex], tminz[childIndex]), tmin);
const float cmax = fminf(fmin_fmin(tmaxx[childIndex], tmaxy[childIndex], tmaxz[childIndex]), tmax);
bool intersected = cmin <= cmax;
// Potential micro-optimization: use VSHL to implement this part, as described by the paper
if (intersected)
{
const unsigned int child_bits = extract_byte(child_bits4, childIndex);
const unsigned int bit_index = extract_byte(bit_index4, childIndex);
hitmask |= child_bits << bit_index;
}
}
}
nodeGroup.y = (hitmask & 0xFF000000) | (*((byte*)&n0.w + 3));
triangleGroup.y = hitmask & 0x00FFFFFF;
}
}
else
{
triangleGroup = nodeGroup;
nodeGroup = make_uint2(0);
}
#if TRIANGLE_POSTPONING
const int totalThreads = __popc(__activemask());
#endif
while (triangleGroup.y != 0)
{
#if TRIANGLE_POSTPONING
const float Rt = 0.2;
const int threshold = totalThreads * Rt;
const int numActiveThreads = __popc(__activemask());
if (numActiveThreads < threshold)
{
STACK_PUSH(triangleGroup);
break;
}
#endif
int triangleIndex = __bfind(triangleGroup.y);
int triAddr = triangleGroup.x * 3 + triangleIndex * 3;
float4 v00 = __ldg(localTriangleWoopCoordinates + triAddr + 0);
float4 v11 = __ldg(localTriangleWoopCoordinates + triAddr + 1);
float4 v22 = __ldg(localTriangleWoopCoordinates + triAddr + 2);
float Oz = v00.w - orig.x*v00.x - orig.y*v00.y - orig.z*v00.z;
float invDz = 1.0f / (dir.x*v00.x + dir.y*v00.y + dir.z*v00.z);
float t = Oz * invDz;
float Ox = v11.w + orig.x*v11.x + orig.y*v11.y + orig.z*v11.z;
float Dx = dir.x * v11.x + dir.y * v11.y + dir.z * v11.z;
float u = Ox + t * Dx;
float Oy = v22.w + orig.x*v22.x + orig.y*v22.y + orig.z*v22.z;
float Dy = dir.x*v22.x + dir.y*v22.y + dir.z*v22.z;
float v = Oy + t*Dy;
if (t > tmin && t < tmax)
{
if (u >= 0.0f && u <= 1.0f)
{
if (v >= 0.0f && u + v <= 1.0f)
{
triangleuv.x = u;
triangleuv.y = v;
tmax = t;
hitAddr = triAddr;
}
}
}
triangleGroup.y &= ~(1 << triangleIndex);
}
if (nodeGroup.y <= 0x00FFFFFF)
{
if (stackPtr > 0)
{
STACK_POP(nodeGroup);
}
else
{
rayResultBuffer[rayidx].t_triId_u_v = make_float4(tmax, int_as_float(hitAddr), triangleuv.x, triangleuv.y);
break;
}
}
#if DYNAMIC_FETCH
const int Nd = 4;
const int Nw = 16;
lostLoopIterations += __popc(__activemask()) - Nd;
if (lostLoopIterations >= Nw)
break;
#endif
} while (true);
} while (true);
}
__host__ void rtBindCWBVHData(
const float4* InBVHTreeNodes,
const float4* InTriangleWoopCoordinates,
const int* InMappingFromTriangleAddressToIndex)
{
cudaCheck(cudaMemcpyToSymbol(MappingFromTriangleAddressToIndex, &InMappingFromTriangleAddressToIndex, 1 * sizeof(InMappingFromTriangleAddressToIndex)));
cudaCheck(cudaMemcpyToSymbol(TriangleWoopCoordinates, &InTriangleWoopCoordinates, 1 * sizeof(InTriangleWoopCoordinates)));
cudaCheck(cudaMemcpyToSymbol(BVHTreeNodes, &InBVHTreeNodes, 1 * sizeof(InBVHTreeNodes)));
}
__host__ void rtTraceCWBVH(
Ray* rayBuffer,
Hit* rayResultBuffer,
int rayCount
)
{
float elapsedTime;
cudaEvent_t startEvent, stopEvent;
cudaCheck(cudaEventCreate(&startEvent));
cudaCheck(cudaEventCreate(&stopEvent));
int* cudaFinishedRayCount;
cudaCheck(cudaMalloc(&cudaFinishedRayCount, sizeof(int)));
dim3 blockDim(32, 2);
dim3 gridDim(32, 32);
cudaProfilerStart();
cudaCheck(cudaEventRecord(startEvent, 0));
{
cudaMemset(cudaFinishedRayCount, 0, sizeof(int));
rtTraceCWBVHDynamicFetch <<< gridDim, blockDim >>> (
rayBuffer,
rayResultBuffer,
rayCount,
cudaFinishedRayCount
);
}
cudaCheck(cudaEventRecord(stopEvent, 0));
cudaCheck(cudaEventSynchronize(stopEvent));
cudaCheck(cudaEventElapsedTime(&elapsedTime, startEvent, stopEvent));
Log("%.3fMS, %.2fMRays/s (rtTraceCWBVH Dynamic Fetch)", elapsedTime, (float)rayCount / 1000000.0f / (elapsedTime / 1000.0f));
cudaProfilerStop();
cudaFree(cudaFinishedRayCount);
}