Cycles: Make primitive split code easier for re-use by reference splitting function
[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_primitive(const Mesh *mesh,
67                                       int prim_index,
68                                       int dim,
69                                       float pos,
70                                       BoundBox& left_bounds,
71                                       BoundBox& right_bounds);
72         void split_curve_primitive(const Mesh *mesh,
73                                    int prim_index,
74                                    int segment_index,
75                                    int dim,
76                                    float pos,
77                                    BoundBox& left_bounds,
78                                    BoundBox& right_bounds);
79
80         void split_triangle_reference(const BVHReference& ref,
81                                       const Mesh *mesh,
82                                       int dim,
83                                       float pos,
84                                       BoundBox& left_bounds,
85                                       BoundBox& right_bounds);
86         void split_curve_reference(const BVHReference& ref,
87                                    const Mesh *mesh,
88                                    int dim,
89                                    float pos,
90                                    BoundBox& left_bounds,
91                                    BoundBox& right_bounds);
92 };
93
94 /* Mixed Object-Spatial Split */
95
96 class BVHMixedSplit
97 {
98 public:
99         BVHObjectSplit object;
100         BVHSpatialSplit spatial;
101
102         float leafSAH;
103         float nodeSAH;
104         float minSAH;
105
106         bool no_split;
107
108         __forceinline BVHMixedSplit(BVHBuild *builder, const BVHRange& range, int level)
109         {
110                 /* find split candidates. */
111                 float area = range.bounds().safe_area();
112
113                 leafSAH = area * builder->params.primitive_cost(range.size());
114                 nodeSAH = area * builder->params.node_cost(2);
115
116                 object = BVHObjectSplit(builder, range, nodeSAH);
117
118                 if(builder->params.use_spatial_split && level < BVHParams::MAX_SPATIAL_DEPTH) {
119                         BoundBox overlap = object.left_bounds;
120                         overlap.intersect(object.right_bounds);
121
122                         if(overlap.safe_area() >= builder->spatial_min_overlap)
123                                 spatial = BVHSpatialSplit(builder, range, nodeSAH);
124                 }
125
126                 /* leaf SAH is the lowest => create leaf. */
127                 minSAH = min(min(leafSAH, object.sah), spatial.sah);
128                 no_split = (minSAH == leafSAH && builder->range_within_max_leaf_size(range));
129         }
130
131         __forceinline void split(BVHBuild *builder, BVHRange& left, BVHRange& right, const BVHRange& range)
132         {
133                 if(builder->params.use_spatial_split && minSAH == spatial.sah)
134                         spatial.split(builder, left, right, range);
135                 if(!left.size() || !right.size())
136                         object.split(builder, left, right, range);
137         }
138 };
139
140 CCL_NAMESPACE_END
141
142 #endif /* __BVH_SPLIT_H__ */
143