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
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2009 Blender Foundation.
* All rights reserved.
*/
/** \file blender/render/intern/raytrace/bvh.h
* \ingroup render
*/
#include "MEM_guardedalloc.h"
#include "BLI_math.h"
#include "raycounter.h"
#include "rayintersection.h"
#include "rayobject.h"
#include "rayobject_hint.h"
#include "rayobject_rtbuild.h"
#include <assert.h>
#ifdef __SSE__
#include <xmmintrin.h>
#endif
#ifndef __BVH_H__
#define __BVH_H__
#ifdef __SSE__
inline int test_bb_group4(__m128 *bb_group, const Isect *isec)
{
const __m128 tmin0 = _mm_setzero_ps();
const __m128 tmax0 = _mm_set_ps1(isec->dist);
float start[3], idot_axis[3];
copy_v3_v3(start, isec->start);
copy_v3_v3(idot_axis, isec->idot_axis);
const __m128 tmin1 = _mm_max_ps(tmin0, _mm_mul_ps(_mm_sub_ps(bb_group[isec->bv_index[0]], _mm_set_ps1(start[0]) ), _mm_set_ps1(idot_axis[0])) );
const __m128 tmax1 = _mm_min_ps(tmax0, _mm_mul_ps(_mm_sub_ps(bb_group[isec->bv_index[1]], _mm_set_ps1(start[0]) ), _mm_set_ps1(idot_axis[0])) );
const __m128 tmin2 = _mm_max_ps(tmin1, _mm_mul_ps(_mm_sub_ps(bb_group[isec->bv_index[2]], _mm_set_ps1(start[1]) ), _mm_set_ps1(idot_axis[1])) );
const __m128 tmax2 = _mm_min_ps(tmax1, _mm_mul_ps(_mm_sub_ps(bb_group[isec->bv_index[3]], _mm_set_ps1(start[1]) ), _mm_set_ps1(idot_axis[1])) );
const __m128 tmin3 = _mm_max_ps(tmin2, _mm_mul_ps(_mm_sub_ps(bb_group[isec->bv_index[4]], _mm_set_ps1(start[2]) ), _mm_set_ps1(idot_axis[2])) );
const __m128 tmax3 = _mm_min_ps(tmax2, _mm_mul_ps(_mm_sub_ps(bb_group[isec->bv_index[5]], _mm_set_ps1(start[2]) ), _mm_set_ps1(idot_axis[2])) );
return _mm_movemask_ps(_mm_cmpge_ps(tmax3, tmin3));
}
#endif
/*
* Determines the distance that the ray must travel to hit the bounding volume of the given node
* Based on Tactical Optimization of Ray/Box Intersection, by Graham Fyffe
* [http://tog.acm.org/resources/RTNews/html/rtnv21n1.html#art9]
*/
static inline int rayobject_bb_intersect_test(const Isect *isec, const float *_bb)
{
const float *bb = _bb;
float t1x = (bb[isec->bv_index[0]] - isec->start[0]) * isec->idot_axis[0];
float t2x = (bb[isec->bv_index[1]] - isec->start[0]) * isec->idot_axis[0];
float t1y = (bb[isec->bv_index[2]] - isec->start[1]) * isec->idot_axis[1];
float t2y = (bb[isec->bv_index[3]] - isec->start[1]) * isec->idot_axis[1];
float t1z = (bb[isec->bv_index[4]] - isec->start[2]) * isec->idot_axis[2];
float t2z = (bb[isec->bv_index[5]] - isec->start[2]) * isec->idot_axis[2];
RE_RC_COUNT(isec->raycounter->bb.test);
if (t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return 0;
if (t2x < 0.0f || t2y < 0.0f || t2z < 0.0f) return 0;
if (t1x > isec->dist || t1y > isec->dist || t1z > isec->dist) return 0;
RE_RC_COUNT(isec->raycounter->bb.hit);
return 1;
}
/* bvh tree generics */
template<class Tree> static void bvh_add(Tree *obj, RayObject *ob)
{
rtbuild_add(obj->builder, ob);
}
template<class Node>
inline bool is_leaf(Node *node)
{
return !RE_rayobject_isAligned(node);
}
template<class Tree> static void bvh_done(Tree *obj);
template<class Tree>
static void bvh_free(Tree *obj)
{
if (obj->builder)
rtbuild_free(obj->builder);
if (obj->node_arena)
BLI_memarena_free(obj->node_arena);
MEM_freeN(obj);
}
template<class Tree>
static void bvh_bb(Tree *obj, float *min, float *max)
{
if (obj->root)
bvh_node_merge_bb(obj->root, min, max);
}
template<class Tree>
static float bvh_cost(Tree *obj)
{
assert(obj->cost >= 0.0f);
return obj->cost;
}
/* bvh tree nodes generics */
template<class Node> static inline int bvh_node_hit_test(Node *node, Isect *isec)
{
return rayobject_bb_intersect_test(isec, (const float *)node->bb);
}
template<class Node>
static inline void bvh_node_merge_bb(Node *node, float min[3], float max[3])
{
if (is_leaf(node)) {
RE_rayobject_merge_bb((RayObject *)node, min, max);
}
else {
DO_MIN(node->bb, min);
DO_MAX(node->bb + 3, max);
}
}
/*
* recursively transverse a BVH looking for a rayhit using a local stack
*/
template<class Node> static inline void bvh_node_push_childs(Node *node, Isect *isec, Node **stack, int &stack_pos);
template<class Node, int MAX_STACK_SIZE, bool TEST_ROOT, bool SHADOW>
static int bvh_node_stack_raycast(Node *root, Isect *isec)
{
Node *stack[MAX_STACK_SIZE];
int hit = 0, stack_pos = 0;
if (!TEST_ROOT && !is_leaf(root))
bvh_node_push_childs(root, isec, stack, stack_pos);
else
stack[stack_pos++] = root;
while (stack_pos) {
Node *node = stack[--stack_pos];
if (!is_leaf(node)) {
if (bvh_node_hit_test(node, isec)) {
bvh_node_push_childs(node, isec, stack, stack_pos);
assert(stack_pos <= MAX_STACK_SIZE);
}
}
else {
hit |= RE_rayobject_intersect( (RayObject *)node, isec);
if (SHADOW && hit) return hit;
}
}
return hit;
}
#ifdef __SSE__
/*
* Generic SIMD bvh recursion
* this was created to be able to use any simd (with the cost of some memmoves)
* it can take advantage of any SIMD width and doens't needs any special tree care
*/
template<class Node, int MAX_STACK_SIZE, bool TEST_ROOT>
static int bvh_node_stack_raycast_simd(Node *root, Isect *isec)
{
Node *stack[MAX_STACK_SIZE];
int hit = 0, stack_pos = 0;
if (!TEST_ROOT) {
if (!is_leaf(root)) {
if (!is_leaf(root->child))
bvh_node_push_childs(root, isec, stack, stack_pos);
else
return RE_rayobject_intersect( (RayObject *)root->child, isec);
}
else
return RE_rayobject_intersect( (RayObject *)root, isec);
}
else {
if (!is_leaf(root))
stack[stack_pos++] = root;
else
return RE_rayobject_intersect( (RayObject *)root, isec);
}
while (true) {
//Use SIMD 4
if (stack_pos >= 4) {
__m128 t_bb[6];
Node *t_node[4];
stack_pos -= 4;
/* prepare the 4BB for SIMD */
t_node[0] = stack[stack_pos + 0]->child;
t_node[1] = stack[stack_pos + 1]->child;
t_node[2] = stack[stack_pos + 2]->child;
t_node[3] = stack[stack_pos + 3]->child;
const float *bb0 = stack[stack_pos + 0]->bb;
const float *bb1 = stack[stack_pos + 1]->bb;
const float *bb2 = stack[stack_pos + 2]->bb;
const float *bb3 = stack[stack_pos + 3]->bb;
const __m128 x0y0x1y1 = _mm_shuffle_ps(_mm_load_ps(bb0), _mm_load_ps(bb1), _MM_SHUFFLE(1, 0, 1, 0) );
const __m128 x2y2x3y3 = _mm_shuffle_ps(_mm_load_ps(bb2), _mm_load_ps(bb3), _MM_SHUFFLE(1, 0, 1, 0) );
t_bb[0] = _mm_shuffle_ps(x0y0x1y1, x2y2x3y3, _MM_SHUFFLE(2, 0, 2, 0) );
t_bb[1] = _mm_shuffle_ps(x0y0x1y1, x2y2x3y3, _MM_SHUFFLE(3, 1, 3, 1) );
const __m128 z0X0z1X1 = _mm_shuffle_ps(_mm_load_ps(bb0), _mm_load_ps(bb1), _MM_SHUFFLE(3, 2, 3, 2) );
const __m128 z2X2z3X3 = _mm_shuffle_ps(_mm_load_ps(bb2), _mm_load_ps(bb3), _MM_SHUFFLE(3, 2, 3, 2) );
t_bb[2] = _mm_shuffle_ps(z0X0z1X1, z2X2z3X3, _MM_SHUFFLE(2, 0, 2, 0) );
t_bb[3] = _mm_shuffle_ps(z0X0z1X1, z2X2z3X3, _MM_SHUFFLE(3, 1, 3, 1) );
const __m128 Y0Z0Y1Z1 = _mm_shuffle_ps(_mm_load_ps(bb0 + 4), _mm_load_ps(bb1 + 4), _MM_SHUFFLE(1, 0, 1, 0) );
const __m128 Y2Z2Y3Z3 = _mm_shuffle_ps(_mm_load_ps(bb2 + 4), _mm_load_ps(bb3 + 4), _MM_SHUFFLE(1, 0, 1, 0) );
t_bb[4] = _mm_shuffle_ps(Y0Z0Y1Z1, Y2Z2Y3Z3, _MM_SHUFFLE(2, 0, 2, 0) );
t_bb[5] = _mm_shuffle_ps(Y0Z0Y1Z1, Y2Z2Y3Z3, _MM_SHUFFLE(3, 1, 3, 1) );
#if 0
for (int i = 0; i < 4; i++)
{
Node *t = stack[stack_pos + i];
assert(!is_leaf(t));
float *bb = ((float *)t_bb) + i;
bb[4 * 0] = t->bb[0];
bb[4 * 1] = t->bb[1];
bb[4 * 2] = t->bb[2];
bb[4 * 3] = t->bb[3];
bb[4 * 4] = t->bb[4];
bb[4 * 5] = t->bb[5];
t_node[i] = t->child;
}
#endif
RE_RC_COUNT(isec->raycounter->simd_bb.test);
int res = test_bb_group4(t_bb, isec);
for (int i = 0; i < 4; i++)
if (res & (1 << i)) {
RE_RC_COUNT(isec->raycounter->simd_bb.hit);
if (!is_leaf(t_node[i])) {
for (Node *t = t_node[i]; t; t = t->sibling) {
assert(stack_pos < MAX_STACK_SIZE);
stack[stack_pos++] = t;
}
}
else {
hit |= RE_rayobject_intersect( (RayObject *)t_node[i], isec);
if (hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
}
else if (stack_pos > 0) {
Node *node = stack[--stack_pos];
assert(!is_leaf(node));
if (bvh_node_hit_test(node, isec)) {
if (!is_leaf(node->child)) {
bvh_node_push_childs(node, isec, stack, stack_pos);
assert(stack_pos <= MAX_STACK_SIZE);
}
else {
hit |= RE_rayobject_intersect( (RayObject *)node->child, isec);
if (hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
}
else break;
}
return hit;
}
#endif
/*
* recursively transverse a BVH looking for a rayhit using system stack
*/
#if 0
template<class Node>
static int bvh_node_raycast(Node *node, Isect *isec)
{
int hit = 0;
if (bvh_test_node(node, isec))
{
if (isec->idot_axis[node->split_axis] > 0.0f)
{
int i;
for (i = 0; i < BVH_NCHILDS; i++)
if (!is_leaf(node->child[i]))
{
if (node->child[i] == 0) break;
hit |= bvh_node_raycast(node->child[i], isec);
if (hit && isec->mode == RE_RAY_SHADOW) return hit;
}
else {
hit |= RE_rayobject_intersect( (RayObject *)node->child[i], isec);
if (hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
else {
int i;
for (i = BVH_NCHILDS - 1; i >= 0; i--)
if (!is_leaf(node->child[i]))
{
if (node->child[i])
{
hit |= dfs_raycast(node->child[i], isec);
if (hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
else {
hit |= RE_rayobject_intersect( (RayObject *)node->child[i], isec);
if (hit && isec->mode == RE_RAY_SHADOW) return hit;
}
}
}
return hit;
}
#endif
template<class Node, class HintObject>
static void bvh_dfs_make_hint(Node *node, LCTSHint *hint, int reserve_space, HintObject *hintObject)
{
assert(hint->size + reserve_space + 1 <= RE_RAY_LCTS_MAX_SIZE);
if (is_leaf(node)) {
hint->stack[hint->size++] = (RayObject *)node;
}
else {
int childs = count_childs(node);
if (hint->size + reserve_space + childs <= RE_RAY_LCTS_MAX_SIZE) {
int result = hint_test_bb(hintObject, node->bb, node->bb + 3);
if (result == HINT_RECURSE) {
/* We are 100% sure the ray will be pass inside this node */
bvh_dfs_make_hint_push_siblings(node->child, hint, reserve_space, hintObject);
}
else if (result == HINT_ACCEPT) {
hint->stack[hint->size++] = (RayObject *)node;
}
}
else {
hint->stack[hint->size++] = (RayObject *)node;
}
}
}
template<class Tree>
static RayObjectAPI *bvh_get_api(int maxstacksize);
template<class Tree, int DFS_STACK_SIZE>
static inline RayObject *bvh_create_tree(int size)
{
Tree *obj = (Tree *)MEM_callocN(sizeof(Tree), "BVHTree");
assert(RE_rayobject_isAligned(obj)); /* RayObject API assumes real data to be 4-byte aligned */
obj->rayobj.api = bvh_get_api<Tree>(DFS_STACK_SIZE);
obj->root = NULL;
obj->node_arena = NULL;
obj->builder = rtbuild_create(size);
return RE_rayobject_unalignRayAPI((RayObject *) obj);
}
#endif
|