heuristic_timesplit_array.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "../common/primref_mb.h"
  5. #include "../../common/algorithms/parallel_filter.h"
  6. #define MBLUR_TIME_SPLIT_THRESHOLD 1.25f
  7. namespace embree
  8. {
  9. namespace isa
  10. {
  11. /*! Performs standard object binning */
  12. template<typename PrimRefMB, typename RecalculatePrimRef, size_t BINS>
  13. struct HeuristicMBlurTemporalSplit
  14. {
  15. typedef BinSplit<MBLUR_NUM_OBJECT_BINS> Split;
  16. typedef mvector<PrimRefMB>* PrimRefVector;
  17. typedef typename PrimRefMB::BBox BBox;
  18. static const size_t PARALLEL_THRESHOLD = 3 * 1024;
  19. static const size_t PARALLEL_FIND_BLOCK_SIZE = 1024;
  20. static const size_t PARALLEL_PARTITION_BLOCK_SIZE = 128;
  21. HeuristicMBlurTemporalSplit (MemoryMonitorInterface* device, const RecalculatePrimRef& recalculatePrimRef)
  22. : device(device), recalculatePrimRef(recalculatePrimRef) {}
  23. struct TemporalBinInfo
  24. {
  25. __forceinline TemporalBinInfo () {
  26. }
  27. __forceinline TemporalBinInfo (EmptyTy)
  28. {
  29. for (size_t i=0; i<BINS-1; i++)
  30. {
  31. count0[i] = count1[i] = 0;
  32. bounds0[i] = bounds1[i] = empty;
  33. }
  34. }
  35. void bin(const PrimRefMB* prims, size_t begin, size_t end, BBox1f time_range, const SetMB& set, const RecalculatePrimRef& recalculatePrimRef)
  36. {
  37. for (int b=0; b<BINS-1; b++)
  38. {
  39. const float t = float(b+1)/float(BINS);
  40. const float ct = lerp(time_range.lower,time_range.upper,t);
  41. const float center_time = set.align_time(ct);
  42. if (center_time <= time_range.lower) continue;
  43. if (center_time >= time_range.upper) continue;
  44. const BBox1f dt0(time_range.lower,center_time);
  45. const BBox1f dt1(center_time,time_range.upper);
  46. /* find linear bounds for both time segments */
  47. for (size_t i=begin; i<end; i++)
  48. {
  49. if (prims[i].time_range_overlap(dt0))
  50. {
  51. const LBBox3fa bn0 = recalculatePrimRef.linearBounds(prims[i],dt0);
  52. #if MBLUR_BIN_LBBOX
  53. bounds0[b].extend(bn0);
  54. #else
  55. bounds0[b].extend(bn0.interpolate(0.5f));
  56. #endif
  57. count0[b] += prims[i].timeSegmentRange(dt0).size();
  58. }
  59. if (prims[i].time_range_overlap(dt1))
  60. {
  61. const LBBox3fa bn1 = recalculatePrimRef.linearBounds(prims[i],dt1);
  62. #if MBLUR_BIN_LBBOX
  63. bounds1[b].extend(bn1);
  64. #else
  65. bounds1[b].extend(bn1.interpolate(0.5f));
  66. #endif
  67. count1[b] += prims[i].timeSegmentRange(dt1).size();
  68. }
  69. }
  70. }
  71. }
  72. __forceinline void bin_parallel(const PrimRefMB* prims, size_t begin, size_t end, size_t blockSize, size_t parallelThreshold, BBox1f time_range, const SetMB& set, const RecalculatePrimRef& recalculatePrimRef)
  73. {
  74. if (likely(end-begin < parallelThreshold)) {
  75. bin(prims,begin,end,time_range,set,recalculatePrimRef);
  76. }
  77. else
  78. {
  79. auto bin = [&](const range<size_t>& r) -> TemporalBinInfo {
  80. TemporalBinInfo binner(empty); binner.bin(prims, r.begin(), r.end(), time_range, set, recalculatePrimRef); return binner;
  81. };
  82. *this = parallel_reduce(begin,end,blockSize,TemporalBinInfo(empty),bin,merge2);
  83. }
  84. }
  85. /*! merges in other binning information */
  86. __forceinline void merge (const TemporalBinInfo& other)
  87. {
  88. for (size_t i=0; i<BINS-1; i++)
  89. {
  90. count0[i] += other.count0[i];
  91. count1[i] += other.count1[i];
  92. bounds0[i].extend(other.bounds0[i]);
  93. bounds1[i].extend(other.bounds1[i]);
  94. }
  95. }
  96. static __forceinline const TemporalBinInfo merge2(const TemporalBinInfo& a, const TemporalBinInfo& b) {
  97. TemporalBinInfo r = a; r.merge(b); return r;
  98. }
  99. Split best(int logBlockSize, BBox1f time_range, const SetMB& set)
  100. {
  101. float bestSAH = inf;
  102. float bestPos = 0.0f;
  103. for (int b=0; b<BINS-1; b++)
  104. {
  105. float t = float(b+1)/float(BINS);
  106. float ct = lerp(time_range.lower,time_range.upper,t);
  107. const float center_time = set.align_time(ct);
  108. if (center_time <= time_range.lower) continue;
  109. if (center_time >= time_range.upper) continue;
  110. const BBox1f dt0(time_range.lower,center_time);
  111. const BBox1f dt1(center_time,time_range.upper);
  112. /* calculate sah */
  113. const size_t lCount = (count0[b]+(size_t(1) << logBlockSize)-1) >> int(logBlockSize);
  114. const size_t rCount = (count1[b]+(size_t(1) << logBlockSize)-1) >> int(logBlockSize);
  115. float sah0 = expectedApproxHalfArea(bounds0[b])*float(lCount)*dt0.size();
  116. float sah1 = expectedApproxHalfArea(bounds1[b])*float(rCount)*dt1.size();
  117. if (unlikely(lCount == 0)) sah0 = 0.0f; // happens for initial splits when objects not alive over entire shutter time
  118. if (unlikely(rCount == 0)) sah1 = 0.0f;
  119. const float sah = sah0+sah1;
  120. if (sah < bestSAH) {
  121. bestSAH = sah;
  122. bestPos = center_time;
  123. }
  124. }
  125. return Split(bestSAH*MBLUR_TIME_SPLIT_THRESHOLD,(unsigned)Split::SPLIT_TEMPORAL,0,bestPos);
  126. }
  127. public:
  128. size_t count0[BINS-1];
  129. size_t count1[BINS-1];
  130. BBox bounds0[BINS-1];
  131. BBox bounds1[BINS-1];
  132. };
  133. /*! finds the best split */
  134. const Split find(const SetMB& set, const size_t logBlockSize)
  135. {
  136. assert(set.size() > 0);
  137. TemporalBinInfo binner(empty);
  138. binner.bin_parallel(set.prims->data(),set.begin(),set.end(),PARALLEL_FIND_BLOCK_SIZE,PARALLEL_THRESHOLD,set.time_range,set,recalculatePrimRef);
  139. Split tsplit = binner.best((int)logBlockSize,set.time_range,set);
  140. if (!tsplit.valid()) tsplit.data = Split::SPLIT_FALLBACK; // use fallback split
  141. return tsplit;
  142. }
  143. __forceinline std::unique_ptr<mvector<PrimRefMB>> split(const Split& tsplit, const SetMB& set, SetMB& lset, SetMB& rset)
  144. {
  145. assert(tsplit.sah != float(inf));
  146. assert(tsplit.fpos > set.time_range.lower);
  147. assert(tsplit.fpos < set.time_range.upper);
  148. float center_time = tsplit.fpos;
  149. const BBox1f time_range0(set.time_range.lower,center_time);
  150. const BBox1f time_range1(center_time,set.time_range.upper);
  151. mvector<PrimRefMB>& prims = *set.prims;
  152. /* calculate primrefs for first time range */
  153. std::unique_ptr<mvector<PrimRefMB>> new_vector(new mvector<PrimRefMB>(device, set.size()));
  154. PrimRefVector lprims = new_vector.get();
  155. auto reduction_func0 = [&] (const range<size_t>& r) {
  156. PrimInfoMB pinfo = empty;
  157. for (size_t i=r.begin(); i<r.end(); i++)
  158. {
  159. if (likely(prims[i].time_range_overlap(time_range0)))
  160. {
  161. const PrimRefMB& prim = recalculatePrimRef(prims[i],time_range0);
  162. (*lprims)[i-set.begin()] = prim;
  163. pinfo.add_primref(prim);
  164. }
  165. else
  166. {
  167. (*lprims)[i-set.begin()] = prims[i];
  168. }
  169. }
  170. return pinfo;
  171. };
  172. PrimInfoMB linfo = parallel_reduce(set.object_range,PARALLEL_PARTITION_BLOCK_SIZE,PARALLEL_THRESHOLD,PrimInfoMB(empty),reduction_func0,PrimInfoMB::merge2);
  173. /* primrefs for first time range are in lprims[0 .. set.size()) */
  174. /* some primitives may need to be filtered out */
  175. if (linfo.size() != set.size())
  176. linfo.object_range._end = parallel_filter(lprims->data(), size_t(0), set.size(), size_t(1024),
  177. [&](const PrimRefMB& prim) { return prim.time_range_overlap(time_range0); });
  178. lset = SetMB(linfo,lprims,time_range0);
  179. /* calculate primrefs for second time range */
  180. auto reduction_func1 = [&] (const range<size_t>& r) {
  181. PrimInfoMB pinfo = empty;
  182. for (size_t i=r.begin(); i<r.end(); i++)
  183. {
  184. if (likely(prims[i].time_range_overlap(time_range1)))
  185. {
  186. const PrimRefMB& prim = recalculatePrimRef(prims[i],time_range1);
  187. prims[i] = prim;
  188. pinfo.add_primref(prim);
  189. }
  190. }
  191. return pinfo;
  192. };
  193. PrimInfoMB rinfo = parallel_reduce(set.object_range,PARALLEL_PARTITION_BLOCK_SIZE,PARALLEL_THRESHOLD,PrimInfoMB(empty),reduction_func1,PrimInfoMB::merge2);
  194. rinfo.object_range = range<size_t>(set.begin(), set.begin() + rinfo.size());
  195. /* primrefs for second time range are in prims[set.begin() .. set.end()) */
  196. /* some primitives may need to be filtered out */
  197. if (rinfo.size() != set.size())
  198. rinfo.object_range._end = parallel_filter(prims.data(), set.begin(), set.end(), size_t(1024),
  199. [&](const PrimRefMB& prim) { return prim.time_range_overlap(time_range1); });
  200. rset = SetMB(rinfo,&prims,time_range1);
  201. return new_vector;
  202. }
  203. private:
  204. MemoryMonitorInterface* device; // device to report memory usage to
  205. const RecalculatePrimRef recalculatePrimRef;
  206. };
  207. }
  208. }