Blender V5.0
binning.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2011 Intel Corporation
2 * SPDX-FileCopyrightText: 2012-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Adapted code from Intel Corporation. */
7
8// #define __KERNEL_SSE__
9
10#include "bvh/binning.h"
11
12#include <cstdlib>
13
14#include "util/algorithm.h"
15#include "util/boundbox.h"
16#include "util/types.h"
17
19
20/* SSE replacements */
21
22__forceinline void prefetch_L1(const void * /*ptr*/) {}
23__forceinline void prefetch_L2(const void * /*ptr*/) {}
24__forceinline void prefetch_L3(const void * /*ptr*/) {}
25__forceinline void prefetch_NTA(const void * /*ptr*/) {}
26
27template<size_t src> __forceinline float extract(const int4 &b)
28{
29 return b[src];
30}
31template<size_t dst> __forceinline const float4 insert(const float4 &a, const float b)
32{
33 float4 r = a;
34 r[dst] = b;
35 return r;
36}
37
39{
40 // return (int)__bsf(movemask(reduce_min(bestSAH) == bestSAH));
41
42 const float minSAH = min(bestSAH.x, min(bestSAH.y, bestSAH.z));
43
44 if (bestSAH.x == minSAH) {
45 return 0;
46 }
47 if (bestSAH.y == minSAH) {
48 return 1;
49 }
50 return 2;
51}
52
53/* BVH Object Binning */
54
56 BVHReference *prims,
57 const BVHUnaligned *unaligned_heuristic,
58 const Transform *aligned_space)
59 : BVHRange(job),
61 dim(0),
62 pos(0),
63 unaligned_heuristic_(unaligned_heuristic),
64 aligned_space_(aligned_space)
65{
66 if (aligned_space_ == nullptr) {
67 bounds_ = bounds();
69 }
70 else {
71 /* TODO(sergey): With some additional storage we can avoid
72 * need in re-calculating this.
73 */
74 bounds_ = unaligned_heuristic->compute_aligned_boundbox(
75 *this, prims, *aligned_space, &cent_bounds_);
76 }
77
78 /* compute number of bins to use and precompute scaling factor for binning */
79 num_bins = min(size_t(MAX_BINS), size_t(4.0f + 0.05f * size()));
81
82 /* initialize binning counter and bounds */
83 BoundBox bin_bounds[MAX_BINS][4]; /* bounds for every bin in every dimension */
84 int4 bin_count[MAX_BINS]; /* number of primitives mapped to bin */
85
86 for (size_t i = 0; i < num_bins; i++) {
87 bin_count[i] = make_int4(0);
88 bin_bounds[i][0] = bin_bounds[i][1] = bin_bounds[i][2] = BoundBox::empty;
89 }
90
91 /* map geometry to bins, unrolled once */
92 {
93 int64_t i;
94
95 for (i = 0; i < int64_t(size()) - 1; i += 2) {
96 prefetch_L2(&prims[start() + i + 8]);
97
98 /* map even and odd primitive to bin */
99 const BVHReference &prim0 = prims[start() + i + 0];
100 const BVHReference &prim1 = prims[start() + i + 1];
101
102 const BoundBox bounds0 = get_prim_bounds(prim0);
103 const BoundBox bounds1 = get_prim_bounds(prim1);
104
105 const int4 bin0 = get_bin(bounds0);
106 const int4 bin1 = get_bin(bounds1);
107
108 /* increase bounds for bins for even primitive */
109 const int b00 = (int)extract<0>(bin0);
110 bin_count[b00][0]++;
111 bin_bounds[b00][0].grow(bounds0);
112 const int b01 = (int)extract<1>(bin0);
113 bin_count[b01][1]++;
114 bin_bounds[b01][1].grow(bounds0);
115 const int b02 = (int)extract<2>(bin0);
116 bin_count[b02][2]++;
117 bin_bounds[b02][2].grow(bounds0);
118
119 /* increase bounds of bins for odd primitive */
120 const int b10 = (int)extract<0>(bin1);
121 bin_count[b10][0]++;
122 bin_bounds[b10][0].grow(bounds1);
123 const int b11 = (int)extract<1>(bin1);
124 bin_count[b11][1]++;
125 bin_bounds[b11][1].grow(bounds1);
126 const int b12 = (int)extract<2>(bin1);
127 bin_count[b12][2]++;
128 bin_bounds[b12][2].grow(bounds1);
129 }
130
131 /* for uneven number of primitives */
132 if (i < int64_t(size())) {
133 /* map primitive to bin */
134 const BVHReference &prim0 = prims[start() + i];
135 const BoundBox bounds0 = get_prim_bounds(prim0);
136 const int4 bin0 = get_bin(bounds0);
137
138 /* increase bounds of bins */
139 const int b00 = (int)extract<0>(bin0);
140 bin_count[b00][0]++;
141 bin_bounds[b00][0].grow(bounds0);
142 const int b01 = (int)extract<1>(bin0);
143 bin_count[b01][1]++;
144 bin_bounds[b01][1].grow(bounds0);
145 const int b02 = (int)extract<2>(bin0);
146 bin_count[b02][2]++;
147 bin_bounds[b02][2].grow(bounds0);
148 }
149 }
150
151 /* sweep from right to left and compute parallel prefix of merged bounds */
152 float4 r_area[MAX_BINS]; /* area of bounds of primitives on the right */
153 float4 r_count[MAX_BINS]; /* number of primitives on the right */
154 int4 count = make_int4(0);
155
159
160 for (size_t i = num_bins - 1; i > 0; i--) {
161 count = count + bin_count[i];
162 r_count[i] = blocks(count);
163
164 bx = merge(bx, bin_bounds[i][0]);
165 r_area[i][0] = bx.half_area();
166 by = merge(by, bin_bounds[i][1]);
167 r_area[i][1] = by.half_area();
168 bz = merge(bz, bin_bounds[i][2]);
169 r_area[i][2] = bz.half_area();
170 r_area[i][3] = r_area[i][2];
171 }
172
173 /* sweep from left to right and compute SAH */
174 int4 ii = make_int4(1);
175 float4 bestSAH = make_float4(FLT_MAX);
176 int4 bestSplit = make_int4(-1);
177
178 count = make_int4(0);
179
180 bx = BoundBox::empty;
181 by = BoundBox::empty;
182 bz = BoundBox::empty;
183
184 for (size_t i = 1; i < num_bins; i++, ii += make_int4(1)) {
185 count = count + bin_count[i - 1];
186
187 bx = merge(bx, bin_bounds[i - 1][0]);
188 const float Ax = bx.half_area();
189 by = merge(by, bin_bounds[i - 1][1]);
190 const float Ay = by.half_area();
191 bz = merge(bz, bin_bounds[i - 1][2]);
192 const float Az = bz.half_area();
193
194 const float4 lCount = blocks(count);
195 const float4 lArea = make_float4(Ax, Ay, Az, Az);
196 const float4 sah = lArea * lCount + r_area[i] * r_count[i];
197
198 bestSplit = select(sah < bestSAH, ii, bestSplit);
199 bestSAH = min(sah, bestSAH);
200 }
201
202 const int4 mask = make_float4(cent_bounds_.size()) <= zero_float4();
203 bestSAH = insert<3>(select(mask, make_float4(FLT_MAX), bestSAH), FLT_MAX);
204
205 /* find best dimension */
206 dim = get_best_dimension(bestSAH);
207 splitSAH = bestSAH[dim];
208 pos = bestSplit[dim];
209 leafSAH = bounds_.half_area() * blocks(size());
210}
211
213 BVHObjectBinning &left_o,
214 BVHObjectBinning &right_o) const
215{
216 const size_t N = size();
217
218 BoundBox lgeom_bounds = BoundBox::empty;
219 BoundBox rgeom_bounds = BoundBox::empty;
220 BoundBox lcent_bounds = BoundBox::empty;
221 BoundBox rcent_bounds = BoundBox::empty;
222
223 int64_t l = 0;
224 int64_t r = N - 1;
225
226 while (l <= r) {
227 prefetch_L2(&prims[start() + l + 8]);
228 prefetch_L2(&prims[start() + r - 8]);
229
230 const BVHReference prim = prims[start() + l];
232 const float3 unaligned_center = unaligned_bounds.center2();
233 const float3 center = prim.bounds().center2();
234
235 if (get_bin(unaligned_center)[dim] < pos) {
236 lgeom_bounds.grow(prim.bounds());
237 lcent_bounds.grow(center);
238 l++;
239 }
240 else {
241 rgeom_bounds.grow(prim.bounds());
242 rcent_bounds.grow(center);
243 swap(prims[start() + l], prims[start() + r]);
244 r--;
245 }
246 }
247 /* finish */
248 if (l != 0 && N - 1 - r != 0) {
249 right_o = BVHObjectBinning(BVHRange(rgeom_bounds, rcent_bounds, start() + l, N - 1 - r),
250 prims);
251 left_o = BVHObjectBinning(BVHRange(lgeom_bounds, lcent_bounds, start(), l), prims);
252 return;
253 }
254
255 /* object medium split if we did not make progress, can happen when all
256 * primitives have same centroid */
257 lgeom_bounds = BoundBox::empty;
258 rgeom_bounds = BoundBox::empty;
259 lcent_bounds = BoundBox::empty;
260 rcent_bounds = BoundBox::empty;
261
262 for (size_t i = 0; i < N / 2; i++) {
263 lgeom_bounds.grow(prims[start() + i].bounds());
264 lcent_bounds.grow(prims[start() + i].bounds().center2());
265 }
266
267 for (size_t i = N / 2; i < N; i++) {
268 rgeom_bounds.grow(prims[start() + i].bounds());
269 rcent_bounds.grow(prims[start() + i].bounds().center2());
270 }
271
272 right_o = BVHObjectBinning(BVHRange(rgeom_bounds, rcent_bounds, start() + N / 2, N / 2 + N % 2),
273 prims);
274 left_o = BVHObjectBinning(BVHRange(lgeom_bounds, lcent_bounds, start(), N / 2), prims);
275}
276
void BLI_kdtree_nd_ insert(KDTree *tree, int index, const float co[KD_DIMS]) ATTR_NONNULL(1
MINLINE float safe_divide(float a, float b)
CCL_NAMESPACE_BEGIN __forceinline void prefetch_L1(const void *)
Definition binning.cpp:22
__forceinline float extract(const int4 &b)
Definition binning.cpp:27
__forceinline void prefetch_L2(const void *)
Definition binning.cpp:23
__forceinline void prefetch_NTA(const void *)
Definition binning.cpp:25
__forceinline void prefetch_L3(const void *)
Definition binning.cpp:24
__forceinline const float4 insert(const float4 &a, const float b)
Definition binning.cpp:31
__forceinline int get_best_dimension(const float4 &bestSAH)
Definition binning.cpp:38
ATTR_WARN_UNUSED_RESULT const BMLoop * l
long long int int64_t
void split(BVHReference *prims, BVHObjectBinning &left_o, BVHObjectBinning &right_o) const
Definition binning.cpp:212
const BVHUnaligned * unaligned_heuristic_
Definition binning.h:55
__forceinline int4 get_bin(const BoundBox &box) const
Definition binning.h:62
__forceinline BVHObjectBinning()
Definition binning.h:28
BoundBox bounds_
Definition binning.h:52
__forceinline BoundBox get_prim_bounds(const BVHReference &prim) const
Definition binning.h:89
__forceinline const BoundBox & unaligned_bounds()
Definition binning.h:37
__forceinline float4 blocks(const int4 &a) const
Definition binning.h:78
const Transform * aligned_space_
Definition binning.h:56
size_t num_bins
Definition binning.h:48
BoundBox cent_bounds_
Definition binning.h:53
__forceinline int size() const
Definition params.h:281
__forceinline int start() const
Definition params.h:277
__forceinline const BoundBox & bounds() const
Definition params.h:269
__forceinline BVHRange()
Definition params.h:245
__forceinline const BoundBox & cent_bounds() const
Definition params.h:273
__forceinline const BoundBox & bounds() const
Definition params.h:204
BoundBox compute_aligned_boundbox(const BVHObjectBinning &range, const BVHReference *references, const Transform &aligned_space, BoundBox *cent_bounds=nullptr) const
#define __forceinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
#define select(A, B, C)
int count
OrientationBounds merge(const OrientationBounds &cone_a, const OrientationBounds &cone_b)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define N
#define make_float4
#define swap(a, b)
Definition sort.cc:59
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
__forceinline float half_area() const
Definition boundbox.h:106
__forceinline void grow(const float3 &pt)
Definition boundbox.h:35
__forceinline float3 center2() const
Definition boundbox.h:117
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
i
Definition text_draw.cc:230