Cycles: Move primitive splitting into own functions
[blender.git] / intern / cycles / bvh / bvh_split.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_SPLIT_H__
19 #define __BVH_SPLIT_H__
20
21 #include "bvh_build.h"
22 #include "bvh_params.h"
23
24 CCL_NAMESPACE_BEGIN
25
26 class BVHBuild;
27
28 /* Object Split */
29
30 class BVHObjectSplit
31 {
32 public:
33         float sah;
34         int dim;
35         int num_left;
36         BoundBox left_bounds;
37         BoundBox right_bounds;
38
39         BVHObjectSplit() {}
40         BVHObjectSplit(BVHBuild *builder, const BVHRange& range, float nodeSAH);
41
42         void split(BVHBuild *builder, BVHRange& left, BVHRange& right, const BVHRange& range);
43 };
44
45 /* Spatial Split */
46
47 class BVHSpatialSplit
48 {
49 public:
50         float sah;
51         int dim;
52         float pos;
53
54         BVHSpatialSplit() : sah(FLT_MAX), dim(0), pos(0.0f) {}
55         BVHSpatialSplit(BVHBuild *builder, const BVHRange& range, float nodeSAH);
56
57         void split(BVHBuild *builder, BVHRange& left, BVHRange& right, const BVHRange& range);
58         void split_reference(BVHBuild *builder,
59                              BVHReference& left,
60                              BVHReference& right,
61                              const BVHReference& ref,
62                              int dim,
63                              float pos);
64
65 protected:
66         void split_triangle_reference(const BVHReference& ref,
67                                       const Mesh *mesh,
68                                       int dim,
69                                       float pos,
70                                       BoundBox& left_bounds,
71                                       BoundBox& right_bounds);
72         void split_curve_reference(const BVHReference& ref,
73                                    const Mesh *mesh,
74                                    int dim,
75                                    float pos,
76                                    BoundBox& left_bounds,
77                                    BoundBox& right_bounds);
78 };
79
80 /* Mixed Object-Spatial Split */
81
82 class BVHMixedSplit
83 {
84 public:
85         BVHObjectSplit object;
86         BVHSpatialSplit spatial;
87
88         float leafSAH;
89         float nodeSAH;
90         float minSAH;
91
92         bool no_split;
93
94         __forceinline BVHMixedSplit(BVHBuild *builder, const BVHRange& range, int level)
95         {
96                 /* find split candidates. */
97                 float area = range.bounds().safe_area();
98
99                 leafSAH = area * builder->params.primitive_cost(range.size());
100                 nodeSAH = area * builder->params.node_cost(2);
101
102                 object = BVHObjectSplit(builder, range, nodeSAH);
103
104                 if(builder->params.use_spatial_split && level < BVHParams::MAX_SPATIAL_DEPTH) {
105                         BoundBox overlap = object.left_bounds;
106                         overlap.intersect(object.right_bounds);
107
108                         if(overlap.safe_area() >= builder->spatial_min_overlap)
109                                 spatial = BVHSpatialSplit(builder, range, nodeSAH);
110                 }
111
112                 /* leaf SAH is the lowest => create leaf. */
113                 minSAH = min(min(leafSAH, object.sah), spatial.sah);
114                 no_split = (minSAH == leafSAH && builder->range_within_max_leaf_size(range));
115         }
116
117         __forceinline void split(BVHBuild *builder, BVHRange& left, BVHRange& right, const BVHRange& range)
118         {
119                 if(builder->params.use_spatial_split && minSAH == spatial.sah)
120                         spatial.split(builder, left, right, range);
121                 if(!left.size() || !right.size())
122                         object.split(builder, left, right, range);
123         }
124 };
125
126 CCL_NAMESPACE_END
127
128 #endif /* __BVH_SPLIT_H__ */
129