Update bundled version of minilzo
[blender-staging.git] / intern / cycles / bvh / bvh_build.h
1 /*
2  * Adapted from code copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011, Blender Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef __BVH_BUILD_H__
19 #define __BVH_BUILD_H__
20
21 #include <float.h>
22
23 #include "bvh.h"
24 #include "bvh_binning.h"
25
26 #include "util_boundbox.h"
27 #include "util_task.h"
28 #include "util_vector.h"
29
30 CCL_NAMESPACE_BEGIN
31
32 class BVHBuildTask;
33 class BVHParams;
34 class InnerNode;
35 class Mesh;
36 class Object;
37 class Progress;
38
39 /* BVH Builder */
40
41 class BVHBuild
42 {
43 public:
44         /* Constructor/Destructor */
45         BVHBuild(
46                 const vector<Object*>& objects,
47                 vector<int>& prim_segment,
48                 vector<int>& prim_index,
49                 vector<int>& prim_object,
50                 const BVHParams& params,
51                 Progress& progress);
52         ~BVHBuild();
53
54         BVHNode *run();
55
56 protected:
57         friend class BVHMixedSplit;
58         friend class BVHObjectSplit;
59         friend class BVHSpatialSplit;
60         friend class BVHBuildTask;
61
62         /* adding references */
63         void add_reference_mesh(BoundBox& root, BoundBox& center, Mesh *mesh, int i);
64         void add_reference_object(BoundBox& root, BoundBox& center, Object *ob, int i);
65         void add_references(BVHRange& root);
66
67         /* building */
68         BVHNode *build_node(const BVHRange& range, int level);
69         BVHNode *build_node(const BVHObjectBinning& range, int level);
70         BVHNode *create_leaf_node(const BVHRange& range);
71         BVHNode *create_object_leaf_nodes(const BVHReference *ref, int start, int num);
72
73         /* threads */
74         enum { THREAD_TASK_SIZE = 4096 };
75         void thread_build_node(InnerNode *node, int child, BVHObjectBinning *range, int level);
76         thread_mutex build_mutex;
77
78         /* progress */
79         void progress_update();
80
81         /* tree rotations */
82         void rotate(BVHNode *node, int max_depth);
83         void rotate(BVHNode *node, int max_depth, int iterations);
84
85         /* objects and primitive references */
86         vector<Object*> objects;
87         vector<BVHReference> references;
88         int num_original_references;
89
90         /* output primitive indexes and objects */
91         vector<int>& prim_segment;
92         vector<int>& prim_index;
93         vector<int>& prim_object;
94
95         /* build parameters */
96         BVHParams params;
97
98         /* progress reporting */
99         Progress& progress;
100         double progress_start_time;
101         size_t progress_count;
102         size_t progress_total;
103         size_t progress_original_total;
104
105         /* spatial splitting */
106         float spatial_min_overlap;
107         vector<BoundBox> spatial_right_bounds;
108         BVHSpatialBin spatial_bins[3][BVHParams::NUM_SPATIAL_BINS];
109
110         /* threads */
111         TaskPool task_pool;
112 };
113
114 CCL_NAMESPACE_END
115
116 #endif /* __BVH_BUILD_H__ */
117