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
427
428
429
430
431
432
433
|
/*
* Copyright 2011-2014, Blender Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
struct QBVHStackItem {
int addr;
float dist;
};
/* TOOD(sergey): Investigate if using intrinsics helps for both
* stack item swap and float comparison.
*/
ccl_device_inline void qbvh_item_swap(QBVHStackItem *__restrict a,
QBVHStackItem *__restrict b)
{
QBVHStackItem tmp = *a;
*a = *b;
*b = tmp;
}
ccl_device_inline void qbvh_stack_sort(QBVHStackItem *__restrict s1,
QBVHStackItem *__restrict s2,
QBVHStackItem *__restrict s3)
{
if(s2->dist < s1->dist) { qbvh_item_swap(s2, s1); }
if(s3->dist < s2->dist) { qbvh_item_swap(s3, s2); }
if(s2->dist < s1->dist) { qbvh_item_swap(s2, s1); }
}
ccl_device_inline void qbvh_stack_sort(QBVHStackItem *__restrict s1,
QBVHStackItem *__restrict s2,
QBVHStackItem *__restrict s3,
QBVHStackItem *__restrict s4)
{
if(s2->dist < s1->dist) { qbvh_item_swap(s2, s1); }
if(s4->dist < s3->dist) { qbvh_item_swap(s4, s3); }
if(s3->dist < s1->dist) { qbvh_item_swap(s3, s1); }
if(s4->dist < s2->dist) { qbvh_item_swap(s4, s2); }
if(s3->dist < s2->dist) { qbvh_item_swap(s3, s2); }
}
/* Axis-aligned nodes intersection */
ccl_device_inline int qbvh_aligned_node_intersect(KernelGlobals *__restrict kg,
const ssef& tnear,
const ssef& tfar,
#ifdef __KERNEL_AVX2__
const sse3f& org_idir,
#else
const sse3f& org,
#endif
const sse3f& idir,
const int near_x,
const int near_y,
const int near_z,
const int far_x,
const int far_y,
const int far_z,
const int node_addr,
ssef *__restrict dist)
{
const int offset = node_addr + 1;
#ifdef __KERNEL_AVX2__
const ssef tnear_x = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+near_x), idir.x, org_idir.x);
const ssef tnear_y = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+near_y), idir.y, org_idir.y);
const ssef tnear_z = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+near_z), idir.z, org_idir.z);
const ssef tfar_x = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+far_x), idir.x, org_idir.x);
const ssef tfar_y = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+far_y), idir.y, org_idir.y);
const ssef tfar_z = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+far_z), idir.z, org_idir.z);
#else
const ssef tnear_x = (kernel_tex_fetch_ssef(__bvh_nodes, offset+near_x) - org.x) * idir.x;
const ssef tnear_y = (kernel_tex_fetch_ssef(__bvh_nodes, offset+near_y) - org.y) * idir.y;
const ssef tnear_z = (kernel_tex_fetch_ssef(__bvh_nodes, offset+near_z) - org.z) * idir.z;
const ssef tfar_x = (kernel_tex_fetch_ssef(__bvh_nodes, offset+far_x) - org.x) * idir.x;
const ssef tfar_y = (kernel_tex_fetch_ssef(__bvh_nodes, offset+far_y) - org.y) * idir.y;
const ssef tfar_z = (kernel_tex_fetch_ssef(__bvh_nodes, offset+far_z) - org.z) * idir.z;
#endif
#ifdef __KERNEL_SSE41__
const ssef near = maxi(maxi(tnear_x, tnear_y), maxi(tnear_z, tnear));
const ssef far = mini(mini(tfar_x, tfar_y), mini(tfar_z, tfar));
const sseb vmask = cast(near) > cast(far);
int mask = (int)movemask(vmask)^0xf;
#else
const ssef near = max4(tnear_x, tnear_y, tnear_z, tnear);
const ssef far = min4(tfar_x, tfar_y, tfar_z, tfar);
const sseb vmask = near <= far;
int mask = (int)movemask(vmask);
#endif
*dist = near;
return mask;
}
ccl_device_inline int qbvh_aligned_node_intersect_robust(
KernelGlobals *__restrict kg,
const ssef& tnear,
const ssef& tfar,
#ifdef __KERNEL_AVX2__
const sse3f& P_idir,
#else
const sse3f& P,
#endif
const sse3f& idir,
const int near_x,
const int near_y,
const int near_z,
const int far_x,
const int far_y,
const int far_z,
const int node_addr,
const float difl,
ssef *__restrict dist)
{
const int offset = node_addr + 1;
#ifdef __KERNEL_AVX2__
const ssef tnear_x = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+near_x), idir.x, P_idir.x);
const ssef tnear_y = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+near_y), idir.y, P_idir.y);
const ssef tnear_z = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+near_z), idir.z, P_idir.z);
const ssef tfar_x = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+far_x), idir.x, P_idir.x);
const ssef tfar_y = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+far_y), idir.y, P_idir.y);
const ssef tfar_z = msub(kernel_tex_fetch_ssef(__bvh_nodes, offset+far_z), idir.z, P_idir.z);
#else
const ssef tnear_x = (kernel_tex_fetch_ssef(__bvh_nodes, offset+near_x) - P.x) * idir.x;
const ssef tnear_y = (kernel_tex_fetch_ssef(__bvh_nodes, offset+near_y) - P.y) * idir.y;
const ssef tnear_z = (kernel_tex_fetch_ssef(__bvh_nodes, offset+near_z) - P.z) * idir.z;
const ssef tfar_x = (kernel_tex_fetch_ssef(__bvh_nodes, offset+far_x) - P.x) * idir.x;
const ssef tfar_y = (kernel_tex_fetch_ssef(__bvh_nodes, offset+far_y) - P.y) * idir.y;
const ssef tfar_z = (kernel_tex_fetch_ssef(__bvh_nodes, offset+far_z) - P.z) * idir.z;
#endif
const float round_down = 1.0f - difl;
const float round_up = 1.0f + difl;
const ssef near = max4(tnear_x, tnear_y, tnear_z, tnear);
const ssef far = min4(tfar_x, tfar_y, tfar_z, tfar);
const sseb vmask = round_down*near <= round_up*far;
*dist = near;
return (int)movemask(vmask);
}
/* Unaligned nodes intersection */
ccl_device_inline int qbvh_unaligned_node_intersect(
KernelGlobals *__restrict kg,
const ssef& tnear,
const ssef& tfar,
#ifdef __KERNEL_AVX2__
const sse3f& org_idir,
#endif
const sse3f& org,
const sse3f& dir,
const sse3f& idir,
const int near_x,
const int near_y,
const int near_z,
const int far_x,
const int far_y,
const int far_z,
const int node_addr,
ssef *__restrict dist)
{
const int offset = node_addr;
const ssef tfm_x_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+1);
const ssef tfm_x_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+2);
const ssef tfm_x_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+3);
const ssef tfm_y_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+4);
const ssef tfm_y_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+5);
const ssef tfm_y_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+6);
const ssef tfm_z_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+7);
const ssef tfm_z_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+8);
const ssef tfm_z_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+9);
const ssef tfm_t_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+10);
const ssef tfm_t_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+11);
const ssef tfm_t_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+12);
const ssef aligned_dir_x = dir.x*tfm_x_x + dir.y*tfm_x_y + dir.z*tfm_x_z,
aligned_dir_y = dir.x*tfm_y_x + dir.y*tfm_y_y + dir.z*tfm_y_z,
aligned_dir_z = dir.x*tfm_z_x + dir.y*tfm_z_y + dir.z*tfm_z_z;
const ssef aligned_P_x = org.x*tfm_x_x + org.y*tfm_x_y + org.z*tfm_x_z + tfm_t_x,
aligned_P_y = org.x*tfm_y_x + org.y*tfm_y_y + org.z*tfm_y_z + tfm_t_y,
aligned_P_z = org.x*tfm_z_x + org.y*tfm_z_y + org.z*tfm_z_z + tfm_t_z;
const ssef neg_one(-1.0f, -1.0f, -1.0f, -1.0f);
const ssef nrdir_x = neg_one / aligned_dir_x,
nrdir_y = neg_one / aligned_dir_y,
nrdir_z = neg_one / aligned_dir_z;
const ssef tlower_x = aligned_P_x * nrdir_x,
tlower_y = aligned_P_y * nrdir_y,
tlower_z = aligned_P_z * nrdir_z;
const ssef tupper_x = tlower_x - nrdir_x,
tupper_y = tlower_y - nrdir_y,
tupper_z = tlower_z - nrdir_z;
#ifdef __KERNEL_SSE41__
const ssef tnear_x = mini(tlower_x, tupper_x);
const ssef tnear_y = mini(tlower_y, tupper_y);
const ssef tnear_z = mini(tlower_z, tupper_z);
const ssef tfar_x = maxi(tlower_x, tupper_x);
const ssef tfar_y = maxi(tlower_y, tupper_y);
const ssef tfar_z = maxi(tlower_z, tupper_z);
const ssef near = max4(tnear, tnear_x, tnear_y, tnear_z);
const ssef far = min4(tfar, tfar_x, tfar_y, tfar_z);
const sseb vmask = near <= far;
*dist = near;
return movemask(vmask);
#else
const ssef tnear_x = min(tlower_x, tupper_x);
const ssef tnear_y = min(tlower_y, tupper_y);
const ssef tnear_z = min(tlower_z, tupper_z);
const ssef tfar_x = max(tlower_x, tupper_x);
const ssef tfar_y = max(tlower_y, tupper_y);
const ssef tfar_z = max(tlower_z, tupper_z);
const ssef near = max4(tnear, tnear_x, tnear_y, tnear_z);
const ssef far = min4(tfar, tfar_x, tfar_y, tfar_z);
const sseb vmask = near <= far;
*dist = near;
return movemask(vmask);
#endif
}
ccl_device_inline int qbvh_unaligned_node_intersect_robust(
KernelGlobals *__restrict kg,
const ssef& tnear,
const ssef& tfar,
#ifdef __KERNEL_AVX2__
const sse3f& P_idir,
#endif
const sse3f& P,
const sse3f& dir,
const sse3f& idir,
const int near_x,
const int near_y,
const int near_z,
const int far_x,
const int far_y,
const int far_z,
const int node_addr,
const float difl,
ssef *__restrict dist)
{
const int offset = node_addr;
const ssef tfm_x_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+1);
const ssef tfm_x_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+2);
const ssef tfm_x_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+3);
const ssef tfm_y_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+4);
const ssef tfm_y_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+5);
const ssef tfm_y_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+6);
const ssef tfm_z_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+7);
const ssef tfm_z_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+8);
const ssef tfm_z_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+9);
const ssef tfm_t_x = kernel_tex_fetch_ssef(__bvh_nodes, offset+10);
const ssef tfm_t_y = kernel_tex_fetch_ssef(__bvh_nodes, offset+11);
const ssef tfm_t_z = kernel_tex_fetch_ssef(__bvh_nodes, offset+12);
const ssef aligned_dir_x = dir.x*tfm_x_x + dir.y*tfm_x_y + dir.z*tfm_x_z,
aligned_dir_y = dir.x*tfm_y_x + dir.y*tfm_y_y + dir.z*tfm_y_z,
aligned_dir_z = dir.x*tfm_z_x + dir.y*tfm_z_y + dir.z*tfm_z_z;
const ssef aligned_P_x = P.x*tfm_x_x + P.y*tfm_x_y + P.z*tfm_x_z + tfm_t_x,
aligned_P_y = P.x*tfm_y_x + P.y*tfm_y_y + P.z*tfm_y_z + tfm_t_y,
aligned_P_z = P.x*tfm_z_x + P.y*tfm_z_y + P.z*tfm_z_z + tfm_t_z;
const ssef neg_one(-1.0f, -1.0f, -1.0f, -1.0f);
const ssef nrdir_x = neg_one / aligned_dir_x,
nrdir_y = neg_one / aligned_dir_y,
nrdir_z = neg_one / aligned_dir_z;
const ssef tlower_x = aligned_P_x * nrdir_x,
tlower_y = aligned_P_y * nrdir_y,
tlower_z = aligned_P_z * nrdir_z;
const ssef tupper_x = tlower_x - nrdir_x,
tupper_y = tlower_y - nrdir_y,
tupper_z = tlower_z - nrdir_z;
const float round_down = 1.0f - difl;
const float round_up = 1.0f + difl;
#ifdef __KERNEL_SSE41__
const ssef tnear_x = mini(tlower_x, tupper_x);
const ssef tnear_y = mini(tlower_y, tupper_y);
const ssef tnear_z = mini(tlower_z, tupper_z);
const ssef tfar_x = maxi(tlower_x, tupper_x);
const ssef tfar_y = maxi(tlower_y, tupper_y);
const ssef tfar_z = maxi(tlower_z, tupper_z);
#else
const ssef tnear_x = min(tlower_x, tupper_x);
const ssef tnear_y = min(tlower_y, tupper_y);
const ssef tnear_z = min(tlower_z, tupper_z);
const ssef tfar_x = max(tlower_x, tupper_x);
const ssef tfar_y = max(tlower_y, tupper_y);
const ssef tfar_z = max(tlower_z, tupper_z);
#endif
const ssef near = max4(tnear, tnear_x, tnear_y, tnear_z);
const ssef far = min4(tfar, tfar_x, tfar_y, tfar_z);
const sseb vmask = round_down*near <= round_up*far;
*dist = near;
return movemask(vmask);
}
/* Intersectors wrappers.
*
* They'll check node type and call appropriate intersection code.
*/
ccl_device_inline int qbvh_node_intersect(
KernelGlobals *__restrict kg,
const ssef& tnear,
const ssef& tfar,
#ifdef __KERNEL_AVX2__
const sse3f& org_idir,
#endif
const sse3f& org,
const sse3f& dir,
const sse3f& idir,
const int near_x,
const int near_y,
const int near_z,
const int far_x,
const int far_y,
const int far_z,
const int node_addr,
ssef *__restrict dist)
{
const int offset = node_addr;
const float4 node = kernel_tex_fetch(__bvh_nodes, offset);
if(__float_as_uint(node.x) & PATH_RAY_NODE_UNALIGNED) {
return qbvh_unaligned_node_intersect(kg,
tnear,
tfar,
#ifdef __KERNEL_AVX2__
org_idir,
#endif
org,
dir,
idir,
near_x, near_y, near_z,
far_x, far_y, far_z,
node_addr,
dist);
}
else {
return qbvh_aligned_node_intersect(kg,
tnear,
tfar,
#ifdef __KERNEL_AVX2__
org_idir,
#else
org,
#endif
idir,
near_x, near_y, near_z,
far_x, far_y, far_z,
node_addr,
dist);
}
}
ccl_device_inline int qbvh_node_intersect_robust(
KernelGlobals *__restrict kg,
const ssef& tnear,
const ssef& tfar,
#ifdef __KERNEL_AVX2__
const sse3f& P_idir,
#endif
const sse3f& P,
const sse3f& dir,
const sse3f& idir,
const int near_x,
const int near_y,
const int near_z,
const int far_x,
const int far_y,
const int far_z,
const int node_addr,
const float difl,
ssef *__restrict dist)
{
const int offset = node_addr;
const float4 node = kernel_tex_fetch(__bvh_nodes, offset);
if(__float_as_uint(node.x) & PATH_RAY_NODE_UNALIGNED) {
return qbvh_unaligned_node_intersect_robust(kg,
tnear,
tfar,
#ifdef __KERNEL_AVX2__
P_idir,
#endif
P,
dir,
idir,
near_x, near_y, near_z,
far_x, far_y, far_z,
node_addr,
difl,
dist);
}
else {
return qbvh_aligned_node_intersect_robust(kg,
tnear,
tfar,
#ifdef __KERNEL_AVX2__
P_idir,
#else
P,
#endif
idir,
near_x, near_y, near_z,
far_x, far_y, far_z,
node_addr,
difl,
dist);
}
}
|