0a5690deb46005eea3b457bc91ec37843b16c300
[blender.git] / source / blender / render / intern / raytrace / svbvh.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2009 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): AndrĂ© Pinto.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/render/intern/raytrace/svbvh.h
29  *  \ingroup render
30  */
31
32 #ifndef __SVBVH_H__
33 #define __SVBVH_H__
34
35 #ifdef __SSE__
36
37 #include "bvh.h"
38 #include "BLI_memarena.h"
39 #include <algorithm>
40
41 struct SVBVHNode {
42         float child_bb[24];
43         SVBVHNode *child[4];
44         int nchilds;
45 };
46
47 static int svbvh_bb_intersect_test_simd4(const Isect *isec, const __m128 *bb_group)
48 {
49         const __m128 tmin0 = _mm_setzero_ps();
50         const __m128 tmax0 = _mm_set_ps1(isec->dist);
51
52         const __m128 start0 = _mm_set_ps1(isec->start[0]);
53         const __m128 start1 = _mm_set_ps1(isec->start[1]);
54         const __m128 start2 = _mm_set_ps1(isec->start[2]);
55         const __m128 sub0 = _mm_sub_ps(bb_group[isec->bv_index[0]], start0);
56         const __m128 sub1 = _mm_sub_ps(bb_group[isec->bv_index[1]], start0);
57         const __m128 sub2 = _mm_sub_ps(bb_group[isec->bv_index[2]], start1);
58         const __m128 sub3 = _mm_sub_ps(bb_group[isec->bv_index[3]], start1);
59         const __m128 sub4 = _mm_sub_ps(bb_group[isec->bv_index[4]], start2);
60         const __m128 sub5 = _mm_sub_ps(bb_group[isec->bv_index[5]], start2);
61         const __m128 idot_axis0 = _mm_set_ps1(isec->idot_axis[0]);
62         const __m128 idot_axis1 = _mm_set_ps1(isec->idot_axis[1]);
63         const __m128 idot_axis2 = _mm_set_ps1(isec->idot_axis[2]);
64         const __m128 mul0 = _mm_mul_ps(sub0, idot_axis0);
65         const __m128 mul1 = _mm_mul_ps(sub1, idot_axis0);
66         const __m128 mul2 = _mm_mul_ps(sub2, idot_axis1);
67         const __m128 mul3 = _mm_mul_ps(sub3, idot_axis1);
68         const __m128 mul4 = _mm_mul_ps(sub4, idot_axis2);
69         const __m128 mul5 = _mm_mul_ps(sub5, idot_axis2);
70         const __m128 tmin1 = _mm_max_ps(tmin0, mul0);
71         const __m128 tmax1 = _mm_min_ps(tmax0, mul1);
72         const __m128 tmin2 = _mm_max_ps(tmin1, mul2);
73         const __m128 tmax2 = _mm_min_ps(tmax1, mul3);
74         const __m128 tmin3 = _mm_max_ps(tmin2, mul4);
75         const __m128 tmax3 = _mm_min_ps(tmax2, mul5);
76
77         return _mm_movemask_ps(_mm_cmpge_ps(tmax3, tmin3));
78 }
79
80 static int svbvh_bb_intersect_test(const Isect *isec, const float *_bb)
81 {
82         const float *bb = _bb;
83
84         float t1x = (bb[isec->bv_index[0]] - isec->start[0]) * isec->idot_axis[0];
85         float t2x = (bb[isec->bv_index[1]] - isec->start[0]) * isec->idot_axis[0];
86         float t1y = (bb[isec->bv_index[2]] - isec->start[1]) * isec->idot_axis[1];
87         float t2y = (bb[isec->bv_index[3]] - isec->start[1]) * isec->idot_axis[1];
88         float t1z = (bb[isec->bv_index[4]] - isec->start[2]) * isec->idot_axis[2];
89         float t2z = (bb[isec->bv_index[5]] - isec->start[2]) * isec->idot_axis[2];
90
91         RE_RC_COUNT(isec->raycounter->bb.test);
92
93         if (t1x > t2y || t2x < t1y || t1x > t2z || t2x < t1z || t1y > t2z || t2y < t1z) return 0;
94         if (t2x < 0.0f || t2y < 0.0f || t2z < 0.0f) return 0;
95         if (t1x > isec->dist || t1y > isec->dist || t1z > isec->dist) return 0;
96
97         RE_RC_COUNT(isec->raycounter->bb.hit);
98
99         return 1;
100 }
101
102 static bool svbvh_node_is_leaf(const SVBVHNode *node)
103 {
104         return !RE_rayobject_isAligned(node);
105 }
106
107 template<int MAX_STACK_SIZE, bool SHADOW>
108 static int svbvh_node_stack_raycast(SVBVHNode *root, Isect *isec)
109 {
110         SVBVHNode *stack[MAX_STACK_SIZE], *node;
111         int hit = 0, stack_pos = 0;
112
113         stack[stack_pos++] = root;
114
115         while (stack_pos) {
116                 node = stack[--stack_pos];
117
118                 if (!svbvh_node_is_leaf(node)) {
119                         int nchilds = node->nchilds;
120
121                         if (nchilds == 4) {
122                                 float *child_bb = node->child_bb;
123                                 int res = svbvh_bb_intersect_test_simd4(isec, ((__m128 *) (child_bb)));
124                                 SVBVHNode **child = node->child;
125
126                                 RE_RC_COUNT(isec->raycounter->simd_bb.test);
127
128                                 if (res & 1) { stack[stack_pos++] = child[0]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); }
129                                 if (res & 2) { stack[stack_pos++] = child[1]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); }
130                                 if (res & 4) { stack[stack_pos++] = child[2]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); }
131                                 if (res & 8) { stack[stack_pos++] = child[3]; RE_RC_COUNT(isec->raycounter->simd_bb.hit); }
132                         }
133                         else {
134                                 float *child_bb = node->child_bb;
135                                 SVBVHNode **child = node->child;
136                                 int i;
137
138                                 for (i = 0; i < nchilds; i++) {
139                                         if (svbvh_bb_intersect_test(isec, (float *)child_bb + 6 * i)) {
140                                                 stack[stack_pos++] = child[i];
141                                         }
142                                 }
143                         }
144                 }
145                 else {
146                         hit |= RE_rayobject_intersect((RayObject *)node, isec);
147                         if (SHADOW && hit) break;
148                 }
149         }
150
151         return hit;
152 }
153
154
155 template<>
156 inline void bvh_node_merge_bb<SVBVHNode>(SVBVHNode *node, float min[3], float max[3])
157 {
158         if (is_leaf(node)) {
159                 RE_rayobject_merge_bb((RayObject *)node, min, max);
160         }
161         else {
162                 int i;
163                 for (i = 0; i + 4 <= node->nchilds; i += 4) {
164                         float *res = node->child_bb + 6 * i;
165                         for (int j = 0; j < 3; j++) {
166                                 min[j] = min_ff(min[j],
167                                                 min_ffff(res[4 * j + 0],
168                                                          res[4 * j + 1],
169                                                          res[4 * j + 2],
170                                                          res[4 * j + 3]));
171                         }
172                         for (int j = 0; j < 3; j++) {
173                                 max[j] = max_ff(max[j],
174                                                 max_ffff(res[4 * (j + 3) + 0],
175                                                          res[4 * (j + 3) + 1],
176                                                          res[4 * (j + 3) + 2],
177                                                          res[4 * (j + 3) + 3]));
178                         }
179                 }
180
181                 for (; i < node->nchilds; i++) {
182                         DO_MIN(node->child_bb + 6 * i, min);
183                         DO_MAX(node->child_bb + 3 + 6 * i, max);
184                 }
185         }
186 }
187
188
189
190 /*
191  * Builds a SVBVH tree form a VBVHTree
192  */
193 template<class OldNode>
194 struct Reorganize_SVBVH {
195         MemArena *arena;
196
197         float childs_per_node;
198         int nodes_with_childs[16];
199         int useless_bb;
200         int nodes;
201
202         Reorganize_SVBVH(MemArena *a)
203         {
204                 arena = a;
205                 nodes = 0;
206                 childs_per_node = 0;
207                 useless_bb = 0;
208
209                 for (int i = 0; i < 16; i++) {
210                         nodes_with_childs[i] = 0;
211                 }
212         }
213
214         ~Reorganize_SVBVH()
215         {
216 #if 0
217                 {
218                         printf("%f childs per node\n", childs_per_node / nodes);
219                         printf("%d childs BB are useless\n", useless_bb);
220                         for (int i = 0; i < 16; i++) {
221                                 printf("%i childs per node: %d/%d = %f\n", i, nodes_with_childs[i], nodes,  nodes_with_childs[i] / float(nodes));
222                         }
223                 }
224 #endif
225         }
226
227         SVBVHNode *create_node(int nchilds)
228         {
229                 SVBVHNode *node = (SVBVHNode *)BLI_memarena_alloc(arena, sizeof(SVBVHNode));
230                 node->nchilds = nchilds;
231
232                 return node;
233         }
234
235         void copy_bb(float bb[6], const float old_bb[6])
236         {
237                 std::copy(old_bb, old_bb + 6, bb);
238         }
239
240         void prepare_for_simd(SVBVHNode *node)
241         {
242                 int i = 0;
243                 while (i + 4 <= node->nchilds) {
244                         float vec_tmp[4 * 6];
245                         float *res = node->child_bb + 6 * i;
246                         std::copy(res, res + 6 * 4, vec_tmp);
247
248                         for (int j = 0; j < 6; j++) {
249                                 res[4 * j + 0] = vec_tmp[6 * 0 + j];
250                                 res[4 * j + 1] = vec_tmp[6 * 1 + j];
251                                 res[4 * j + 2] = vec_tmp[6 * 2 + j];
252                                 res[4 * j + 3] = vec_tmp[6 * 3 + j];
253                         }
254
255                         i += 4;
256                 }
257         }
258
259         /* amt must be power of two */
260         inline int padup(int num, int amt)
261         {
262                 return ((num + (amt - 1)) & ~(amt - 1));
263         }
264
265         SVBVHNode *transform(OldNode *old)
266         {
267                 if (is_leaf(old))
268                         return (SVBVHNode *)old;
269                 if (is_leaf(old->child))
270                         return (SVBVHNode *)old->child;
271
272                 int nchilds = count_childs(old);
273                 int alloc_childs = nchilds;
274                 if (nchilds % 4 > 2)
275                         alloc_childs = padup(nchilds, 4);
276
277                 SVBVHNode *node = create_node(alloc_childs);
278
279                 childs_per_node += nchilds;
280                 nodes++;
281                 if (nchilds < 16)
282                         nodes_with_childs[nchilds]++;
283
284                 useless_bb += alloc_childs - nchilds;
285                 while (alloc_childs > nchilds) {
286                         const static float def_bb[6] = {FLT_MAX,  FLT_MAX,  FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX};
287                         alloc_childs--;
288                         node->child[alloc_childs] = NULL;
289                         copy_bb(node->child_bb + alloc_childs * 6, def_bb);
290                 }
291
292                 int i = nchilds;
293                 for (OldNode *o_child = old->child; o_child; o_child = o_child->sibling) {
294                         i--;
295                         node->child[i] = transform(o_child);
296                         if (is_leaf(o_child)) {
297                                 float bb[6];
298                                 INIT_MINMAX(bb, bb + 3);
299                                 RE_rayobject_merge_bb((RayObject *)o_child, bb, bb + 3);
300                                 copy_bb(node->child_bb + i * 6, bb);
301                                 break;
302                         }
303                         else {
304                                 copy_bb(node->child_bb + i * 6, o_child->bb);
305                         }
306                 }
307                 assert(i == 0);
308
309                 prepare_for_simd(node);
310
311                 return node;
312         }
313 };
314
315 #endif  /* __SSE__ */
316
317 #endif  /* __SVBVH_H__ */