bvh.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include "bvh.h"
  4. #include "bvh_statistics.h"
  5. namespace embree
  6. {
  7. template<int N>
  8. BVHN<N>::BVHN (const PrimitiveType& primTy, Scene* scene)
  9. : AccelData((N==4) ? AccelData::TY_BVH4 : (N==8) ? AccelData::TY_BVH8 : AccelData::TY_UNKNOWN),
  10. primTy(&primTy), device(scene->device), scene(scene),
  11. root(emptyNode), alloc(scene->device,scene->isStaticAccel()), numPrimitives(0), numVertices(0)
  12. {
  13. }
  14. template<int N>
  15. BVHN<N>::~BVHN ()
  16. {
  17. for (size_t i=0; i<objects.size(); i++)
  18. delete objects[i];
  19. }
  20. template<int N>
  21. void BVHN<N>::clear()
  22. {
  23. set(BVHN::emptyNode,empty,0);
  24. alloc.clear();
  25. }
  26. template<int N>
  27. void BVHN<N>::set (NodeRef root, const LBBox3fa& bounds, size_t numPrimitives)
  28. {
  29. this->root = root;
  30. this->bounds = bounds;
  31. this->numPrimitives = numPrimitives;
  32. }
  33. template<int N>
  34. void BVHN<N>::clearBarrier(NodeRef& node)
  35. {
  36. if (node.isBarrier())
  37. node.clearBarrier();
  38. else if (!node.isLeaf()) {
  39. BaseNode* n = node.baseNode(); // FIXME: flags should be stored in BVH
  40. for (size_t c=0; c<N; c++)
  41. clearBarrier(n->child(c));
  42. }
  43. }
  44. template<int N>
  45. void BVHN<N>::layoutLargeNodes(size_t num)
  46. {
  47. #if defined(__64BIT__) // do not use tree rotations on 32 bit platforms, barrier bit in NodeRef will cause issues
  48. struct NodeArea
  49. {
  50. __forceinline NodeArea() {}
  51. __forceinline NodeArea(NodeRef& node, const BBox3fa& bounds)
  52. : node(&node), A(node.isLeaf() ? float(neg_inf) : area(bounds)) {}
  53. __forceinline bool operator< (const NodeArea& other) const {
  54. return this->A < other.A;
  55. }
  56. NodeRef* node;
  57. float A;
  58. };
  59. std::vector<NodeArea> lst;
  60. lst.reserve(num);
  61. lst.push_back(NodeArea(root,empty));
  62. while (lst.size() < num)
  63. {
  64. std::pop_heap(lst.begin(), lst.end());
  65. NodeArea n = lst.back(); lst.pop_back();
  66. if (!n.node->isAABBNode()) break;
  67. AABBNode* node = n.node->getAABBNode();
  68. for (size_t i=0; i<N; i++) {
  69. if (node->child(i) == BVHN::emptyNode) continue;
  70. lst.push_back(NodeArea(node->child(i),node->bounds(i)));
  71. std::push_heap(lst.begin(), lst.end());
  72. }
  73. }
  74. for (size_t i=0; i<lst.size(); i++)
  75. lst[i].node->setBarrier();
  76. root = layoutLargeNodesRecursion(root,alloc.getCachedAllocator());
  77. #endif
  78. }
  79. template<int N>
  80. typename BVHN<N>::NodeRef BVHN<N>::layoutLargeNodesRecursion(NodeRef& node, const FastAllocator::CachedAllocator& allocator)
  81. {
  82. if (node.isBarrier()) {
  83. node.clearBarrier();
  84. return node;
  85. }
  86. else if (node.isAABBNode())
  87. {
  88. AABBNode* oldnode = node.getAABBNode();
  89. AABBNode* newnode = (BVHN::AABBNode*) allocator.malloc0(sizeof(BVHN::AABBNode),byteNodeAlignment);
  90. *newnode = *oldnode;
  91. for (size_t c=0; c<N; c++)
  92. newnode->child(c) = layoutLargeNodesRecursion(oldnode->child(c),allocator);
  93. return encodeNode(newnode);
  94. }
  95. else return node;
  96. }
  97. template<int N>
  98. double BVHN<N>::preBuild(const std::string& builderName)
  99. {
  100. if (builderName == "")
  101. return inf;
  102. if (device->verbosity(2))
  103. {
  104. Lock<MutexSys> lock(g_printMutex);
  105. std::cout << "building BVH" << N << (builderName.find("MBlur") != std::string::npos ? "MB" : "") << "<" << primTy->name() << "> using " << builderName << " ..." << std::endl << std::flush;
  106. }
  107. double t0 = 0.0;
  108. if (device->benchmark || device->verbosity(2)) t0 = getSeconds();
  109. return t0;
  110. }
  111. template<int N>
  112. void BVHN<N>::postBuild(double t0)
  113. {
  114. if (t0 == double(inf))
  115. return;
  116. double dt = 0.0;
  117. if (device->benchmark || device->verbosity(2))
  118. dt = getSeconds()-t0;
  119. std::unique_ptr<BVHNStatistics<N>> stat;
  120. /* print statistics */
  121. if (device->verbosity(2))
  122. {
  123. if (!stat) stat.reset(new BVHNStatistics<N>(this));
  124. const size_t usedBytes = alloc.getUsedBytes();
  125. Lock<MutexSys> lock(g_printMutex);
  126. std::cout << "finished BVH" << N << "<" << primTy->name() << "> : " << 1000.0f*dt << "ms, " << 1E-6*double(numPrimitives)/dt << " Mprim/s, " << 1E-9*double(usedBytes)/dt << " GB/s" << std::endl;
  127. if (device->verbosity(2))
  128. std::cout << stat->str();
  129. if (device->verbosity(2))
  130. {
  131. FastAllocator::AllStatistics stat(&alloc);
  132. for (size_t i=0; i<objects.size(); i++)
  133. if (objects[i])
  134. stat = stat + FastAllocator::AllStatistics(&objects[i]->alloc);
  135. stat.print(numPrimitives);
  136. }
  137. if (device->verbosity(3))
  138. {
  139. alloc.print_blocks();
  140. for (size_t i=0; i<objects.size(); i++)
  141. if (objects[i])
  142. objects[i]->alloc.print_blocks();
  143. }
  144. std::cout << std::flush;
  145. }
  146. /* benchmark mode */
  147. if (device->benchmark)
  148. {
  149. if (!stat) stat.reset(new BVHNStatistics<N>(this));
  150. Lock<MutexSys> lock(g_printMutex);
  151. std::cout << "BENCHMARK_BUILD " << dt << " " << double(numPrimitives)/dt << " " << stat->sah() << " " << stat->bytesUsed() << " BVH" << N << "<" << primTy->name() << ">" << std::endl << std::flush;
  152. }
  153. }
  154. #if defined(__AVX__)
  155. template class BVHN<8>;
  156. #endif
  157. #if !defined(__AVX__) || !defined(EMBREE_TARGET_SSE2) && !defined(EMBREE_TARGET_SSE42) || defined(__aarch64__)
  158. template class BVHN<4>;
  159. #endif
  160. }