Cycles: threading optimizations
[blender.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_index,
48                 vector<int>& prim_object,
49                 const BVHParams& params,
50                 Progress& progress);
51         ~BVHBuild();
52
53         BVHNode *run();
54
55 protected:
56         friend class BVHMixedSplit;
57         friend class BVHObjectSplit;
58         friend class BVHSpatialSplit;
59         friend class BVHBuildTask;
60
61         /* adding references */
62         void add_reference_mesh(BoundBox& root, BoundBox& center, Mesh *mesh, int i);
63         void add_reference_object(BoundBox& root, BoundBox& center, Object *ob, int i);
64         void add_references(BVHRange& root);
65
66         /* building */
67         BVHNode *build_node(const BVHRange& range, int level);
68         BVHNode *build_node(const BVHObjectBinning& range, int level);
69         BVHNode *create_leaf_node(const BVHRange& range);
70         BVHNode *create_object_leaf_nodes(const BVHReference *ref, int start, int num);
71
72         /* threads */
73         enum { THREAD_TASK_SIZE = 4096 };
74         void thread_build_node(InnerNode *node, int child, BVHObjectBinning *range, int level);
75         thread_mutex build_mutex;
76
77         /* progress */
78         void progress_update();
79
80         /* tree rotations */
81         void rotate(BVHNode *node, int max_depth);
82         void rotate(BVHNode *node, int max_depth, int iterations);
83
84         /* objects and primitive references */
85         vector<Object*> objects;
86         vector<BVHReference> references;
87         int num_original_references;
88
89         /* output primitive indexes and objects */
90         vector<int>& prim_index;
91         vector<int>& prim_object;
92
93         /* build parameters */
94         BVHParams params;
95
96         /* progress reporting */
97         Progress& progress;
98         double progress_start_time;
99         size_t progress_count;
100         size_t progress_total;
101         size_t progress_original_total;
102
103         /* spatial splitting */
104         float spatial_min_overlap;
105         vector<BoundBox> spatial_right_bounds;
106         BVHSpatialBin spatial_bins[3][BVHParams::NUM_SPATIAL_BINS];
107
108         /* threads */
109         TaskPool task_pool;
110 };
111
112 CCL_NAMESPACE_END
113
114 #endif /* __BVH_BUILD_H__ */
115