Cycles: Make all #include statements relative to cycles source directory
[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/bvh.h"
24 #include "bvh/bvh_binning.h"
25 #include "bvh/bvh_unaligned.h"
26
27 #include "util/util_boundbox.h"
28 #include "util/util_task.h"
29 #include "util/util_vector.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 class BVHBuildTask;
34 class BVHSpatialSplitBuildTask;
35 class BVHParams;
36 class InnerNode;
37 class Mesh;
38 class Object;
39 class Progress;
40
41 /* BVH Builder */
42
43 class BVHBuild
44 {
45 public:
46         /* Constructor/Destructor */
47         BVHBuild(const vector<Object*>& objects,
48                  array<int>& prim_type,
49                  array<int>& prim_index,
50                  array<int>& prim_object,
51                  array<float2>& prim_time,
52                  const BVHParams& params,
53                  Progress& progress);
54         ~BVHBuild();
55
56         BVHNode *run();
57
58 protected:
59         friend class BVHMixedSplit;
60         friend class BVHObjectSplit;
61         friend class BVHSpatialSplit;
62         friend class BVHBuildTask;
63         friend class BVHSpatialSplitBuildTask;
64         friend class BVHObjectBinning;
65
66         /* Adding references. */
67         void add_reference_triangles(BoundBox& root, BoundBox& center, Mesh *mesh, int i);
68         void add_reference_curves(BoundBox& root, BoundBox& center, Mesh *mesh, int i);
69         void add_reference_mesh(BoundBox& root, BoundBox& center, Mesh *mesh, int i);
70         void add_reference_object(BoundBox& root, BoundBox& center, Object *ob, int i);
71         void add_references(BVHRange& root);
72
73         /* Building. */
74         BVHNode *build_node(const BVHRange& range,
75                             vector<BVHReference> *references,
76                             int level,
77                             int thread_id);
78         BVHNode *build_node(const BVHObjectBinning& range, int level);
79         BVHNode *create_leaf_node(const BVHRange& range,
80                                   const vector<BVHReference>& references);
81         BVHNode *create_object_leaf_nodes(const BVHReference *ref, int start, int num);
82
83         bool range_within_max_leaf_size(const BVHRange& range,
84                                         const vector<BVHReference>& references) const;
85
86         /* Threads. */
87         enum { THREAD_TASK_SIZE = 4096 };
88         void thread_build_node(InnerNode *node,
89                                int child,
90                                BVHObjectBinning *range,
91                                int level);
92         void thread_build_spatial_split_node(InnerNode *node,
93                                              int child,
94                                              BVHRange *range,
95                                              vector<BVHReference> *references,
96                                              int level,
97                                              int thread_id);
98         thread_mutex build_mutex;
99
100         /* Progress. */
101         void progress_update();
102
103         /* Tree rotations. */
104         void rotate(BVHNode *node, int max_depth);
105         void rotate(BVHNode *node, int max_depth, int iterations);
106
107         /* Objects and primitive references. */
108         vector<Object*> objects;
109         vector<BVHReference> references;
110         int num_original_references;
111
112         /* Output primitive indexes and objects. */
113         array<int>& prim_type;
114         array<int>& prim_index;
115         array<int>& prim_object;
116         array<float2>& prim_time;
117
118         bool need_prim_time;
119
120         /* Build parameters. */
121         BVHParams params;
122
123         /* Progress reporting. */
124         Progress& progress;
125         double progress_start_time;
126         size_t progress_count;
127         size_t progress_total;
128         size_t progress_original_total;
129
130         /* Spatial splitting. */
131         float spatial_min_overlap;
132         vector<BVHSpatialStorage> spatial_storage;
133         size_t spatial_free_index;
134         thread_spin_lock spatial_spin_lock;
135
136         /* Threads. */
137         TaskPool task_pool;
138
139         /* Unaligned building. */
140         BVHUnaligned unaligned_heuristic;
141 };
142
143 CCL_NAMESPACE_END
144
145 #endif /* __BVH_BUILD_H__ */