Added some test_break during the build process.
[blender-staging.git] / source / blender / render / intern / raytrace / rayobject_vbvh.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): AndrĂ© Pinto.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 int tot_pushup   = 0;
30 int tot_pushdown = 0;
31 int tot_hints    = 0;
32
33
34 #include <assert.h>
35 #include "rayobject.h"
36 #include "rayobject_rtbuild.h"
37 #include "RE_raytrace.h"
38 #include "BLI_memarena.h"
39 #include "MEM_guardedalloc.h"
40 #include "BKE_utildefines.h"
41 #include "BLI_arithb.h"
42
43 #include "reorganize.h"
44 #include "bvh.h"
45 #include "vbvh.h"
46 #include "svbvh.h"
47 #include <queue>
48 #include <algorithm>
49
50 #define DFS_STACK_SIZE  256
51
52 struct VBVHTree
53 {
54         RayObject rayobj;
55         VBVHNode *root;
56         MemArena *node_arena;
57         float cost;
58         RTBuilder *builder;
59 };
60
61 /*
62  * Cost to test N childs
63  */
64 struct PackCost
65 {
66         float operator()(int n)
67         {
68                 return n;
69         }
70 };
71
72 template<>
73 void bvh_done<VBVHTree>(VBVHTree *obj)
74 {
75         rtbuild_done(obj->builder, &obj->rayobj.control);
76         
77         //TODO find a away to exactly calculate the needed memory
78         MemArena *arena1 = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
79                                            BLI_memarena_use_malloc(arena1);
80         
81         //Build and optimize the tree
82         if(1)
83         {
84                 VBVHNode *root = BuildBinaryVBVH<VBVHNode>(arena1,&obj->rayobj.control).transform(obj->builder);
85                 if(RE_rayobjectcontrol_test_break(&obj->rayobj.control))
86                 {
87                         BLI_memarena_free(arena1);
88                         return;
89                 }
90
91                 reorganize(root);
92                 remove_useless(root, &root);
93                 bvh_refit(root);
94         
95                 pushup(root);
96                 pushdown(root);
97                 obj->root = root;
98         }
99         else
100         {
101 /*
102         TODO
103                 MemArena *arena2 = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE);
104                                                    BLI_memarena_use_malloc(arena2);
105                                                    
106                 //Finds the optimal packing of this tree using a given cost model
107                 //TODO this uses quite a lot of memory, find ways to reduce memory usage during building
108                 OVBVHNode *root = BuildBinaryVBVH<OVBVHNode>(arena2).transform(obj->builder);                   
109                 VBVH_optimalPackSIMD<OVBVHNode,PackCost>(PackCost()).transform(root);
110                 obj->root = Reorganize_VBVH<OVBVHNode>(arena1).transform(root);
111                 
112                 BLI_memarena_free(arena2);
113  */
114         }
115
116         //Cleanup
117         rtbuild_free( obj->builder );
118         obj->builder = NULL;
119
120         obj->node_arena = arena1;
121         obj->cost = 1.0;        
122 }
123
124 template<int StackSize>
125 int intersect(VBVHTree *obj, Isect* isec)
126 {
127         //TODO renable hint support
128         if(RE_rayobject_isAligned(obj->root))
129                 return bvh_node_stack_raycast<VBVHNode,StackSize,false>( obj->root, isec);
130         else
131                 return RE_rayobject_intersect( (RayObject*) obj->root, isec );
132 }
133
134 template<class Tree>
135 void bvh_hint_bb(Tree *tree, LCTSHint *hint, float *min, float *max)
136 {
137         //TODO renable hint support
138         {
139                 hint->size = 0;
140                 hint->stack[hint->size++] = (RayObject*)tree->root;
141         }
142 }
143
144 void bfree(VBVHTree *tree)
145 {
146         if(tot_pushup + tot_pushdown + tot_hints + tot_moves)
147         {
148                 printf("tot pushups: %d\n", tot_pushup);
149                 printf("tot pushdowns: %d\n", tot_pushdown);
150                 printf("tot moves: %d\n", tot_moves);
151                 printf("tot hints created: %d\n", tot_hints);
152                 tot_pushup = 0;
153                 tot_pushdown = 0;
154                 tot_hints = 0;
155                 tot_moves = 0;
156         }
157         bvh_free(tree);
158 }
159
160 /* the cast to pointer function is needed to workarround gcc bug: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11407 */
161 template<class Tree, int STACK_SIZE>
162 RayObjectAPI make_api()
163 {
164         static RayObjectAPI api = 
165         {
166                 (RE_rayobject_raycast_callback) ((int(*)(Tree*,Isect*)) &intersect<STACK_SIZE>),
167                 (RE_rayobject_add_callback)     ((void(*)(Tree*,RayObject*)) &bvh_add<Tree>),
168                 (RE_rayobject_done_callback)    ((void(*)(Tree*))       &bvh_done<Tree>),
169                 (RE_rayobject_free_callback)    ((void(*)(Tree*))       &bvh_free<Tree>),
170                 (RE_rayobject_merge_bb_callback)((void(*)(Tree*,float*,float*)) &bvh_bb<Tree>),
171                 (RE_rayobject_cost_callback)    ((float(*)(Tree*))      &bvh_cost<Tree>),
172                 (RE_rayobject_hint_bb_callback) ((void(*)(Tree*,LCTSHint*,float*,float*)) &bvh_hint_bb<Tree>)
173         };
174         
175         return api;
176 }
177
178 template<class Tree>
179 RayObjectAPI* bvh_get_api(int maxstacksize)
180 {
181         static RayObjectAPI bvh_api256 = make_api<Tree,1024>();
182         
183         if(maxstacksize <= 1024) return &bvh_api256;
184         assert(maxstacksize <= 256);
185         return 0;
186 }
187
188 RayObject *RE_rayobject_vbvh_create(int size)
189 {
190         return bvh_create_tree<VBVHTree,DFS_STACK_SIZE>(size);
191 }