Blender  V2.93
bvh_split.h
Go to the documentation of this file.
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/bvh_build.h"
22 #include "bvh/bvh_params.h"
23 
25 
26 class BVHBuild;
27 class Hair;
28 class Mesh;
29 struct Transform;
30 
31 /* Object Split */
32 
34  public:
35  float sah;
36  int dim;
37  int num_left;
40 
42  {
43  }
44  BVHObjectSplit(BVHBuild *builder,
45  BVHSpatialStorage *storage,
46  const BVHRange &range,
47  vector<BVHReference> &references,
48  float nodeSAH,
49  const BVHUnaligned *unaligned_heuristic = NULL,
50  const Transform *aligned_space = NULL);
51 
52  void split(BVHRange &left, BVHRange &right, const BVHRange &range);
53 
54  protected:
59 
61  {
62  if (aligned_space_ == NULL) {
63  return prim.bounds();
64  }
65  else {
67  }
68  }
69 };
70 
71 /* Spatial Split */
72 
74  public:
75  float sah;
76  int dim;
77  float pos;
78 
79  BVHSpatialSplit() : sah(FLT_MAX), dim(0), pos(0.0f), storage_(NULL), references_(NULL)
80  {
81  }
82  BVHSpatialSplit(const BVHBuild &builder,
83  BVHSpatialStorage *storage,
84  const BVHRange &range,
85  vector<BVHReference> &references,
86  float nodeSAH,
87  const BVHUnaligned *unaligned_heuristic = NULL,
88  const Transform *aligned_space = NULL);
89 
90  void split(BVHBuild *builder, BVHRange &left, BVHRange &right, const BVHRange &range);
91 
92  void split_reference(const BVHBuild &builder,
95  const BVHReference &ref,
96  int dim,
97  float pos);
98 
99  protected:
104 
105  /* Lower-level functions which calculates boundaries of left and right nodes
106  * needed for spatial split.
107  *
108  * Operates directly with primitive specified by its index, reused by higher
109  * level splitting functions.
110  */
111  void split_triangle_primitive(const Mesh *mesh,
112  const Transform *tfm,
113  int prim_index,
114  int dim,
115  float pos,
116  BoundBox &left_bounds,
117  BoundBox &right_bounds);
118  void split_curve_primitive(const Hair *hair,
119  const Transform *tfm,
120  int prim_index,
121  int segment_index,
122  int dim,
123  float pos,
124  BoundBox &left_bounds,
125  BoundBox &right_bounds);
126 
127  /* Lower-level functions which calculates boundaries of left and right nodes
128  * needed for spatial split.
129  *
130  * Operates with BVHReference, internally uses lower level API functions.
131  */
132  void split_triangle_reference(const BVHReference &ref,
133  const Mesh *mesh,
134  int dim,
135  float pos,
136  BoundBox &left_bounds,
137  BoundBox &right_bounds);
138  void split_curve_reference(const BVHReference &ref,
139  const Hair *hair,
140  int dim,
141  float pos,
142  BoundBox &left_bounds,
143  BoundBox &right_bounds);
145  const Object *object, int dim, float pos, BoundBox &left_bounds, BoundBox &right_bounds);
146 
148  {
149  if (aligned_space_ == NULL) {
150  return prim.bounds();
151  }
152  else {
154  }
155  }
156 
158  {
159  if (aligned_space_ == NULL) {
160  return point;
161  }
162  else {
163  return transform_point(aligned_space_, point);
164  }
165  }
166 };
167 
168 /* Mixed Object-Spatial Split */
169 
171  public:
174 
175  float leafSAH;
176  float nodeSAH;
177  float minSAH;
178 
179  bool no_split;
180 
182 
184  {
185  }
186 
188  BVHSpatialStorage *storage,
189  const BVHRange &range,
190  vector<BVHReference> &references,
191  int level,
192  const BVHUnaligned *unaligned_heuristic = NULL,
193  const Transform *aligned_space = NULL)
194  {
195  if (aligned_space == NULL) {
196  bounds = range.bounds();
197  }
198  else {
199  bounds = unaligned_heuristic->compute_aligned_boundbox(
200  range, &references.at(0), *aligned_space);
201  }
202  /* find split candidates. */
203  float area = bounds.safe_area();
204 
205  leafSAH = area * builder->params.primitive_cost(range.size());
206  nodeSAH = area * builder->params.node_cost(2);
207 
208  object = BVHObjectSplit(
209  builder, storage, range, references, nodeSAH, unaligned_heuristic, aligned_space);
210 
211  if (builder->params.use_spatial_split && level < BVHParams::MAX_SPATIAL_DEPTH) {
212  BoundBox overlap = object.left_bounds;
213  overlap.intersect(object.right_bounds);
214 
215  if (overlap.safe_area() >= builder->spatial_min_overlap) {
217  *builder, storage, range, references, nodeSAH, unaligned_heuristic, aligned_space);
218  }
219  }
220 
221  /* leaf SAH is the lowest => create leaf. */
222  minSAH = min(min(leafSAH, object.sah), spatial.sah);
223  no_split = (minSAH == leafSAH && builder->range_within_max_leaf_size(range, references));
224  }
225 
226  __forceinline void split(BVHBuild *builder,
227  BVHRange &left,
228  BVHRange &right,
229  const BVHRange &range)
230  {
231  if (builder->params.use_spatial_split && minSAH == spatial.sah)
232  spatial.split(builder, left, right, range);
233  if (!left.size() || !right.size())
234  object.split(left, right, range);
235  }
236 };
237 
239 
240 #endif /* __BVH_SPLIT_H__ */
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble right
bool range_within_max_leaf_size(const BVHRange &range, const vector< BVHReference > &references) const
Definition: bvh_build.cpp:530
BVHParams params
Definition: bvh_build.h:118
float spatial_min_overlap
Definition: bvh_build.h:128
BVHSpatialSplit spatial
Definition: bvh_split.h:173
__forceinline void split(BVHBuild *builder, BVHRange &left, BVHRange &right, const BVHRange &range)
Definition: bvh_split.h:226
float nodeSAH
Definition: bvh_split.h:176
__forceinline BVHMixedSplit(BVHBuild *builder, BVHSpatialStorage *storage, const BVHRange &range, vector< BVHReference > &references, int level, const BVHUnaligned *unaligned_heuristic=NULL, const Transform *aligned_space=NULL)
Definition: bvh_split.h:187
BVHObjectSplit object
Definition: bvh_split.h:172
BoundBox bounds
Definition: bvh_split.h:181
float leafSAH
Definition: bvh_split.h:175
float minSAH
Definition: bvh_split.h:177
BVHSpatialStorage * storage_
Definition: bvh_split.h:55
vector< BVHReference > * references_
Definition: bvh_split.h:56
BoundBox left_bounds
Definition: bvh_split.h:38
BoundBox right_bounds
Definition: bvh_split.h:39
__forceinline BoundBox get_prim_bounds(const BVHReference &prim) const
Definition: bvh_split.h:60
void split(BVHRange &left, BVHRange &right, const BVHRange &range)
Definition: bvh_split.cpp:96
const Transform * aligned_space_
Definition: bvh_split.h:58
const BVHUnaligned * unaligned_heuristic_
Definition: bvh_split.h:57
__forceinline float node_cost(int n) const
Definition: bvh_params.h:138
bool use_spatial_split
Definition: bvh_params.h:49
@ MAX_SPATIAL_DEPTH
Definition: bvh_params.h:95
__forceinline float primitive_cost(int n) const
Definition: bvh_params.h:133
__forceinline int size() const
Definition: bvh_params.h:266
__forceinline const BoundBox & bounds() const
Definition: bvh_params.h:254
__forceinline const BoundBox & bounds() const
Definition: bvh_params.h:180
void split_curve_primitive(const Hair *hair, const Transform *tfm, int prim_index, int segment_index, int dim, float pos, BoundBox &left_bounds, BoundBox &right_bounds)
Definition: bvh_split.cpp:382
const BVHUnaligned * unaligned_heuristic_
Definition: bvh_split.h:102
void split_curve_reference(const BVHReference &ref, const Hair *hair, int dim, float pos, BoundBox &left_bounds, BoundBox &right_bounds)
Definition: bvh_split.cpp:439
void split_triangle_primitive(const Mesh *mesh, const Transform *tfm, int prim_index, int dim, float pos, BoundBox &left_bounds, BoundBox &right_bounds)
Definition: bvh_split.cpp:345
__forceinline float3 get_unaligned_point(const float3 &point) const
Definition: bvh_split.h:157
__forceinline BoundBox get_prim_bounds(const BVHReference &prim) const
Definition: bvh_split.h:147
BVHSpatialStorage * storage_
Definition: bvh_split.h:100
void split_object_reference(const Object *object, int dim, float pos, BoundBox &left_bounds, BoundBox &right_bounds)
Definition: bvh_split.cpp:456
const Transform * aligned_space_
Definition: bvh_split.h:103
vector< BVHReference > * references_
Definition: bvh_split.h:101
void split_triangle_reference(const BVHReference &ref, const Mesh *mesh, int dim, float pos, BoundBox &left_bounds, BoundBox &right_bounds)
Definition: bvh_split.cpp:429
void split_reference(const BVHBuild &builder, BVHReference &left, BVHReference &right, const BVHReference &ref, int dim, float pos)
Definition: bvh_split.cpp:480
void split(BVHBuild *builder, BVHRange &left, BVHRange &right, const BVHRange &range)
Definition: bvh_split.cpp:236
BoundBox compute_aligned_prim_boundbox(const BVHReference &prim, const Transform &aligned_space) const
#define CCL_NAMESPACE_END
static int left
static void area(int d1, int d2, int e1, int e2, float weights[2])
#define min(a, b)
Definition: sort.c:51
__forceinline void intersect(const BoundBox &bbox)
__forceinline float safe_area() const
#define __forceinline
Definition: util_defines.h:71
ccl_device_inline float3 transform_point(const Transform *t, const float3 a)