39
39
#include "MEM_guardedalloc.h"
41
#include "BKE_global.h"
43
41
#include "BLI_math.h"
44
42
#include "BLI_memarena.h"
45
43
#include "BLI_utildefines.h"
45
#include "BKE_global.h"
47
47
#include "rayintersection.h"
48
48
#include "rayobject.h"
49
49
#include "rayobject_rtbuild.h"
85
83
//TODO find a away to exactly calculate the needed memory
86
84
MemArena *arena1 = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "vbvh arena");
87
BLI_memarena_use_malloc(arena1);
85
BLI_memarena_use_malloc(arena1);
89
87
//Build and optimize the tree
92
VBVHNode *root = BuildBinaryVBVH<VBVHNode>(arena1,&obj->rayobj.control).transform(obj->builder);
93
if (RE_rayobjectcontrol_test_break(&obj->rayobj.control))
89
VBVHNode *root = BuildBinaryVBVH<VBVHNode>(arena1, &obj->rayobj.control).transform(obj->builder);
90
if (RE_rayobjectcontrol_test_break(&obj->rayobj.control)) {
95
91
BLI_memarena_free(arena1);
109
105
obj->root = NULL;
115
110
MemArena *arena2 = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, "vbvh arena2");
116
BLI_memarena_use_malloc(arena2);
111
BLI_memarena_use_malloc(arena2);
118
113
//Finds the optimal packing of this tree using a given cost model
119
114
//TODO this uses quite a lot of memory, find ways to reduce memory usage during building
120
OVBVHNode *root = BuildBinaryVBVH<OVBVHNode>(arena2).transform(obj->builder);
121
VBVH_optimalPackSIMD<OVBVHNode,PackCost>(PackCost()).transform(root);
115
OVBVHNode *root = BuildBinaryVBVH<OVBVHNode>(arena2).transform(obj->builder);
116
VBVH_optimalPackSIMD<OVBVHNode, PackCost>(PackCost()).transform(root);
122
117
obj->root = Reorganize_VBVH<OVBVHNode>(arena1).transform(root);
124
119
BLI_memarena_free(arena2);
129
rtbuild_free( obj->builder );
124
rtbuild_free(obj->builder);
130
125
obj->builder = NULL;
132
127
obj->node_arena = arena1;
136
131
template<int StackSize>
137
int intersect(VBVHTree *obj, Isect* isec)
132
int intersect(VBVHTree *obj, Isect *isec)
139
134
//TODO renable hint support
140
135
if (RE_rayobject_isAligned(obj->root)) {
141
136
if (isec->mode == RE_RAY_SHADOW)
142
return bvh_node_stack_raycast<VBVHNode,StackSize,false,true>( obj->root, isec);
137
return bvh_node_stack_raycast<VBVHNode, StackSize, false, true>(obj->root, isec);
144
return bvh_node_stack_raycast<VBVHNode,StackSize,false,false>( obj->root, isec);
139
return bvh_node_stack_raycast<VBVHNode, StackSize, false, false>(obj->root, isec);
147
return RE_rayobject_intersect( (RayObject*) obj->root, isec );
142
return RE_rayobject_intersect( (RayObject *) obj->root, isec);
150
145
template<class Tree>
153
148
//TODO renable hint support
156
hint->stack[hint->size++] = (RayObject*)tree->root;
151
hint->stack[hint->size++] = (RayObject *)tree->root;
160
void bfree(VBVHTree *tree)
156
static void bfree(VBVHTree *tree)
162
if (tot_pushup + tot_pushdown + tot_hints + tot_moves)
158
if (tot_pushup + tot_pushdown + tot_hints + tot_moves) {
164
159
if (G.debug & G_DEBUG) {
165
160
printf("tot pushups: %d\n", tot_pushup);
166
161
printf("tot pushdowns: %d\n", tot_pushdown);
183
179
static RayObjectAPI api =
185
(RE_rayobject_raycast_callback) ((int(*)(Tree*,Isect*)) &intersect<STACK_SIZE>),
186
(RE_rayobject_add_callback) ((void(*)(Tree*,RayObject*)) &bvh_add<Tree>),
187
(RE_rayobject_done_callback) ((void(*)(Tree*)) &bvh_done<Tree>),
188
(RE_rayobject_free_callback) ((void(*)(Tree*)) &bvh_free<Tree>),
189
(RE_rayobject_merge_bb_callback)((void(*)(Tree*,float*,float*)) &bvh_bb<Tree>),
190
(RE_rayobject_cost_callback) ((float(*)(Tree*)) &bvh_cost<Tree>),
191
(RE_rayobject_hint_bb_callback) ((void(*)(Tree*,LCTSHint*,float*,float*)) &bvh_hint_bb<Tree>)
181
(RE_rayobject_raycast_callback) ((int (*)(Tree *, Isect *)) & intersect<STACK_SIZE>),
182
(RE_rayobject_add_callback) ((void (*)(Tree *, RayObject *)) & bvh_add<Tree>),
183
(RE_rayobject_done_callback) ((void (*)(Tree *)) & bvh_done<Tree>),
184
(RE_rayobject_free_callback) ((void (*)(Tree *)) & bvh_free<Tree>),
185
(RE_rayobject_merge_bb_callback)((void (*)(Tree *, float *, float *)) & bvh_bb<Tree>),
186
(RE_rayobject_cost_callback) ((float (*)(Tree *)) & bvh_cost<Tree>),
187
(RE_rayobject_hint_bb_callback) ((void (*)(Tree *, LCTSHint *, float *, float *)) & bvh_hint_bb<Tree>)
197
193
template<class Tree>
198
RayObjectAPI* bvh_get_api(int maxstacksize)
194
RayObjectAPI *bvh_get_api(int maxstacksize)
200
static RayObjectAPI bvh_api256 = make_api<Tree,1024>();
196
static RayObjectAPI bvh_api256 = make_api<Tree, 1024>();
202
198
if (maxstacksize <= 1024) return &bvh_api256;
203
199
assert(maxstacksize <= 256);