heuristic_spatial.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/scene.h"
  5. #include "priminfo.h"
  6. namespace embree
  7. {
  8. static const unsigned int RESERVED_NUM_SPATIAL_SPLITS_GEOMID_BITS = 5;
  9. namespace isa
  10. {
  11. /*! mapping into bins */
  12. template<size_t BINS>
  13. struct SpatialBinMapping
  14. {
  15. public:
  16. __forceinline SpatialBinMapping() {}
  17. /*! calculates the mapping */
  18. __forceinline SpatialBinMapping(const CentGeomBBox3fa& pinfo)
  19. {
  20. const vfloat4 lower = (vfloat4) pinfo.geomBounds.lower;
  21. const vfloat4 upper = (vfloat4) pinfo.geomBounds.upper;
  22. const vfloat4 eps = 128.0f*vfloat4(ulp)*max(abs(lower),abs(upper));
  23. const vfloat4 diag = max(eps,(vfloat4) pinfo.geomBounds.size());
  24. scale = select(upper-lower <= eps,vfloat4(0.0f),vfloat4(BINS)/diag);
  25. ofs = (vfloat4) pinfo.geomBounds.lower;
  26. inv_scale = 1.0f / scale;
  27. }
  28. /*! slower but safe binning */
  29. __forceinline vint4 bin(const Vec3fa& p) const
  30. {
  31. const vint4 i = floori((vfloat4(p)-ofs)*scale);
  32. return clamp(i,vint4(0),vint4(BINS-1));
  33. }
  34. __forceinline std::pair<vint4,vint4> bin(const BBox3fa& b) const
  35. {
  36. #if defined(__AVX__)
  37. const vfloat8 ofs8(ofs);
  38. const vfloat8 scale8(scale);
  39. const vint8 lu = floori((vfloat8::loadu(&b)-ofs8)*scale8);
  40. const vint8 c_lu = clamp(lu,vint8(zero),vint8(BINS-1));
  41. return std::pair<vint4,vint4>(extract4<0>(c_lu),extract4<1>(c_lu));
  42. #else
  43. const vint4 lower = floori((vfloat4(b.lower)-ofs)*scale);
  44. const vint4 upper = floori((vfloat4(b.upper)-ofs)*scale);
  45. const vint4 c_lower = clamp(lower,vint4(0),vint4(BINS-1));
  46. const vint4 c_upper = clamp(upper,vint4(0),vint4(BINS-1));
  47. return std::pair<vint4,vint4>(c_lower,c_upper);
  48. #endif
  49. }
  50. /*! calculates left spatial position of bin */
  51. __forceinline float pos(const size_t bin, const size_t dim) const {
  52. return madd(float(bin),inv_scale[dim],ofs[dim]);
  53. }
  54. /*! calculates left spatial position of bin */
  55. template<size_t N>
  56. __forceinline vfloat<N> posN(const vfloat<N> bin, const size_t dim) const {
  57. return madd(bin,vfloat<N>(inv_scale[dim]),vfloat<N>(ofs[dim]));
  58. }
  59. /*! returns true if the mapping is invalid in some dimension */
  60. __forceinline bool invalid(const size_t dim) const {
  61. return scale[dim] == 0.0f;
  62. }
  63. public:
  64. vfloat4 ofs,scale,inv_scale; //!< linear function that maps to bin ID
  65. };
  66. /*! stores all information required to perform some split */
  67. template<size_t BINS>
  68. struct SpatialBinSplit
  69. {
  70. /*! construct an invalid split by default */
  71. __forceinline SpatialBinSplit()
  72. : sah(inf), dim(-1), pos(0), left(-1), right(-1), factor(1.0f) {}
  73. /*! constructs specified split */
  74. __forceinline SpatialBinSplit(float sah, int dim, int pos, const SpatialBinMapping<BINS>& mapping)
  75. : sah(sah), dim(dim), pos(pos), left(-1), right(-1), factor(1.0f), mapping(mapping) {}
  76. /*! constructs specified split */
  77. __forceinline SpatialBinSplit(float sah, int dim, int pos, int left, int right, float factor, const SpatialBinMapping<BINS>& mapping)
  78. : sah(sah), dim(dim), pos(pos), left(left), right(right), factor(factor), mapping(mapping) {}
  79. /*! tests if this split is valid */
  80. __forceinline bool valid() const { return dim != -1; }
  81. /*! calculates surface area heuristic for performing the split */
  82. __forceinline float splitSAH() const { return sah; }
  83. /*! stream output */
  84. friend embree_ostream operator<<(embree_ostream cout, const SpatialBinSplit& split) {
  85. return cout << "SpatialBinSplit { sah = " << split.sah << ", dim = " << split.dim << ", pos = " << split.pos << ", left = " << split.left << ", right = " << split.right << ", factor = " << split.factor << "}";
  86. }
  87. public:
  88. float sah; //!< SAH cost of the split
  89. int dim; //!< split dimension
  90. int pos; //!< split position
  91. int left; //!< number of elements on the left side
  92. int right; //!< number of elements on the right side
  93. float factor; //!< factor splitting the extended range
  94. SpatialBinMapping<BINS> mapping; //!< mapping into bins
  95. };
  96. /*! stores all binning information */
  97. template<size_t BINS, typename PrimRef>
  98. struct __aligned(64) SpatialBinInfo
  99. {
  100. SpatialBinInfo() {
  101. }
  102. __forceinline SpatialBinInfo(EmptyTy) {
  103. clear();
  104. }
  105. /*! clears the bin info */
  106. __forceinline void clear()
  107. {
  108. for (size_t i=0; i<BINS; i++) {
  109. bounds[i][0] = bounds[i][1] = bounds[i][2] = empty;
  110. numBegin[i] = numEnd[i] = 0;
  111. }
  112. }
  113. /*! adds binning data */
  114. __forceinline void add(const size_t dim,
  115. const size_t beginID,
  116. const size_t endID,
  117. const size_t binID,
  118. const BBox3fa &b,
  119. const size_t n = 1)
  120. {
  121. assert(beginID < BINS);
  122. assert(endID < BINS);
  123. assert(binID < BINS);
  124. numBegin[beginID][dim]+=(unsigned int)n;
  125. numEnd [endID][dim]+=(unsigned int)n;
  126. bounds [binID][dim].extend(b);
  127. }
  128. /*! extends binning bounds */
  129. __forceinline void extend(const size_t dim,
  130. const size_t binID,
  131. const BBox3fa &b)
  132. {
  133. assert(binID < BINS);
  134. bounds [binID][dim].extend(b);
  135. }
  136. /*! bins an array of primitives */
  137. template<typename PrimitiveSplitterFactory>
  138. __forceinline void bin2(const PrimitiveSplitterFactory& splitterFactory, const PrimRef* source, size_t begin, size_t end, const SpatialBinMapping<BINS>& mapping)
  139. {
  140. for (size_t i=begin; i<end; i++)
  141. {
  142. const PrimRef& prim = source[i];
  143. unsigned splits = prim.geomID() >> (32-RESERVED_NUM_SPATIAL_SPLITS_GEOMID_BITS);
  144. if (unlikely(splits <= 1))
  145. {
  146. const vint4 bin = mapping.bin(center(prim.bounds()));
  147. for (size_t dim=0; dim<3; dim++)
  148. {
  149. assert(bin[dim] >= (int)0 && bin[dim] < (int)BINS);
  150. add(dim,bin[dim],bin[dim],bin[dim],prim.bounds());
  151. }
  152. }
  153. else
  154. {
  155. const vint4 bin0 = mapping.bin(prim.bounds().lower);
  156. const vint4 bin1 = mapping.bin(prim.bounds().upper);
  157. for (size_t dim=0; dim<3; dim++)
  158. {
  159. if (unlikely(mapping.invalid(dim)))
  160. continue;
  161. size_t bin;
  162. size_t l = bin0[dim];
  163. size_t r = bin1[dim];
  164. // same bin optimization
  165. if (likely(l == r))
  166. {
  167. add(dim,l,l,l,prim.bounds());
  168. continue;
  169. }
  170. size_t bin_start = bin0[dim];
  171. size_t bin_end = bin1[dim];
  172. BBox3fa rest = prim.bounds();
  173. /* assure that split position always overlaps the primitive bounds */
  174. while (bin_start < bin_end && mapping.pos(bin_start+1,dim) <= rest.lower[dim]) bin_start++;
  175. while (bin_start < bin_end && mapping.pos(bin_end ,dim) >= rest.upper[dim]) bin_end--;
  176. const auto splitter = splitterFactory(prim);
  177. for (bin=bin_start; bin<bin_end; bin++)
  178. {
  179. const float pos = mapping.pos(bin+1,dim);
  180. BBox3fa left,right;
  181. splitter(rest,dim,pos,left,right);
  182. if (unlikely(left.empty())) l++;
  183. extend(dim,bin,left);
  184. rest = right;
  185. }
  186. if (unlikely(rest.empty())) r--;
  187. add(dim,l,r,bin,rest);
  188. }
  189. }
  190. }
  191. }
  192. /*! bins an array of primitives */
  193. __forceinline void binSubTreeRefs(const PrimRef* source, size_t begin, size_t end, const SpatialBinMapping<BINS>& mapping)
  194. {
  195. for (size_t i=begin; i<end; i++)
  196. {
  197. const PrimRef &prim = source[i];
  198. const vint4 bin0 = mapping.bin(prim.bounds().lower);
  199. const vint4 bin1 = mapping.bin(prim.bounds().upper);
  200. for (size_t dim=0; dim<3; dim++)
  201. {
  202. if (unlikely(mapping.invalid(dim)))
  203. continue;
  204. const size_t l = bin0[dim];
  205. const size_t r = bin1[dim];
  206. const unsigned int n = prim.primID();
  207. // same bin optimization
  208. if (likely(l == r))
  209. {
  210. add(dim,l,l,l,prim.bounds(),n);
  211. continue;
  212. }
  213. const size_t bin_start = bin0[dim];
  214. const size_t bin_end = bin1[dim];
  215. for (size_t bin=bin_start; bin<bin_end; bin++)
  216. add(dim,l,r,bin,prim.bounds(),n);
  217. }
  218. }
  219. }
  220. /*! merges in other binning information */
  221. void merge (const SpatialBinInfo& other)
  222. {
  223. for (size_t i=0; i<BINS; i++)
  224. {
  225. numBegin[i] += other.numBegin[i];
  226. numEnd [i] += other.numEnd [i];
  227. bounds[i][0].extend(other.bounds[i][0]);
  228. bounds[i][1].extend(other.bounds[i][1]);
  229. bounds[i][2].extend(other.bounds[i][2]);
  230. }
  231. }
  232. /*! merges in other binning information */
  233. static __forceinline const SpatialBinInfo reduce (const SpatialBinInfo& a, const SpatialBinInfo& b)
  234. {
  235. SpatialBinInfo c(empty);
  236. for (size_t i=0; i<BINS; i++)
  237. {
  238. c.numBegin[i] += a.numBegin[i]+b.numBegin[i];
  239. c.numEnd [i] += a.numEnd [i]+b.numEnd [i];
  240. c.bounds[i][0] = embree::merge(a.bounds[i][0],b.bounds[i][0]);
  241. c.bounds[i][1] = embree::merge(a.bounds[i][1],b.bounds[i][1]);
  242. c.bounds[i][2] = embree::merge(a.bounds[i][2],b.bounds[i][2]);
  243. }
  244. return c;
  245. }
  246. /*! finds the best split by scanning binning information */
  247. SpatialBinSplit<BINS> best(const SpatialBinMapping<BINS>& mapping, const size_t blocks_shift) const
  248. {
  249. /* sweep from right to left and compute parallel prefix of merged bounds */
  250. vfloat4 rAreas[BINS];
  251. vuint4 rCounts[BINS];
  252. vuint4 count = 0; BBox3fa bx = empty; BBox3fa by = empty; BBox3fa bz = empty;
  253. for (size_t i=BINS-1; i>0; i--)
  254. {
  255. count += numEnd[i];
  256. rCounts[i] = count;
  257. bx.extend(bounds[i][0]); rAreas[i][0] = halfArea(bx);
  258. by.extend(bounds[i][1]); rAreas[i][1] = halfArea(by);
  259. bz.extend(bounds[i][2]); rAreas[i][2] = halfArea(bz);
  260. rAreas[i][3] = 0.0f;
  261. }
  262. /* sweep from left to right and compute SAH */
  263. vuint4 blocks_add = (1 << blocks_shift)-1;
  264. vuint4 ii = 1; vfloat4 vbestSAH = pos_inf; vuint4 vbestPos = 0; vuint4 vbestlCount = 0; vuint4 vbestrCount = 0;
  265. count = 0; bx = empty; by = empty; bz = empty;
  266. for (size_t i=1; i<BINS; i++, ii+=1)
  267. {
  268. count += numBegin[i-1];
  269. bx.extend(bounds[i-1][0]); float Ax = halfArea(bx);
  270. by.extend(bounds[i-1][1]); float Ay = halfArea(by);
  271. bz.extend(bounds[i-1][2]); float Az = halfArea(bz);
  272. const vfloat4 lArea = vfloat4(Ax,Ay,Az,Az);
  273. const vfloat4 rArea = rAreas[i];
  274. const vuint4 lCount = (count +blocks_add) >> (unsigned int)(blocks_shift);
  275. const vuint4 rCount = (rCounts[i]+blocks_add) >> (unsigned int)(blocks_shift);
  276. const vfloat4 sah = madd(lArea,vfloat4(lCount),rArea*vfloat4(rCount));
  277. // const vfloat4 sah = madd(lArea,vfloat4(vint4(lCount)),rArea*vfloat4(vint4(rCount)));
  278. const vbool4 mask = sah < vbestSAH;
  279. vbestPos = select(mask,ii ,vbestPos);
  280. vbestSAH = select(mask,sah,vbestSAH);
  281. vbestlCount = select(mask,count,vbestlCount);
  282. vbestrCount = select(mask,rCounts[i],vbestrCount);
  283. }
  284. /* find best dimension */
  285. float bestSAH = inf;
  286. int bestDim = -1;
  287. int bestPos = 0;
  288. unsigned int bestlCount = 0;
  289. unsigned int bestrCount = 0;
  290. for (int dim=0; dim<3; dim++)
  291. {
  292. /* ignore zero sized dimensions */
  293. if (unlikely(mapping.invalid(dim)))
  294. continue;
  295. /* test if this is a better dimension */
  296. if (vbestSAH[dim] < bestSAH && vbestPos[dim] != 0) {
  297. bestDim = dim;
  298. bestPos = vbestPos[dim];
  299. bestSAH = vbestSAH[dim];
  300. bestlCount = vbestlCount[dim];
  301. bestrCount = vbestrCount[dim];
  302. }
  303. }
  304. assert(bestSAH >= 0.0f);
  305. /* return invalid split if no split found */
  306. if (bestDim == -1)
  307. return SpatialBinSplit<BINS>(inf,-1,0,mapping);
  308. /* return best found split */
  309. return SpatialBinSplit<BINS>(bestSAH,bestDim,bestPos,bestlCount,bestrCount,1.0f,mapping);
  310. }
  311. private:
  312. BBox3fa bounds[BINS][3]; //!< geometry bounds for each bin in each dimension
  313. vuint4 numBegin[BINS]; //!< number of primitives starting in bin
  314. vuint4 numEnd[BINS]; //!< number of primitives ending in bin
  315. };
  316. }
  317. }