Cycles: optimization for hair BVH build, allow max 2 hair curves per leaf.
[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, BVHReference& left, BVHReference& right, const BVHReference& ref, int dim, float pos);
59 };
60
61 /* Mixed Object-Spatial Split */
62
63 class BVHMixedSplit
64 {
65 public:
66         BVHObjectSplit object;
67         BVHSpatialSplit spatial;
68
69         float leafSAH;
70         float nodeSAH;
71         float minSAH;
72
73         bool no_split;
74
75         __forceinline BVHMixedSplit(BVHBuild *builder, const BVHRange& range, int level)
76         {
77                 /* find split candidates. */
78                 float area = range.bounds().safe_area();
79
80                 leafSAH = area * builder->params.primitive_cost(range.size());
81                 nodeSAH = area * builder->params.node_cost(2);
82
83                 object = BVHObjectSplit(builder, range, nodeSAH);
84
85                 if(builder->params.use_spatial_split && level < BVHParams::MAX_SPATIAL_DEPTH) {
86                         BoundBox overlap = object.left_bounds;
87                         overlap.intersect(object.right_bounds);
88
89                         if(overlap.safe_area() >= builder->spatial_min_overlap)
90                                 spatial = BVHSpatialSplit(builder, range, nodeSAH);
91                 }
92
93                 /* leaf SAH is the lowest => create leaf. */
94                 minSAH = min(min(leafSAH, object.sah), spatial.sah);
95                 no_split = (minSAH == leafSAH && builder->range_within_max_leaf_size(range));
96         }
97
98         __forceinline void split(BVHBuild *builder, BVHRange& left, BVHRange& right, const BVHRange& range)
99         {
100                 if(builder->params.use_spatial_split && minSAH == spatial.sah)
101                         spatial.split(builder, left, right, range);
102                 if(!left.size() || !right.size())
103                         object.split(builder, left, right, range);
104         }
105 };
106
107 CCL_NAMESPACE_END
108
109 #endif /* __BVH_SPLIT_H__ */
110