bvh.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #include "bvh.h"
  17. #include "bvh_statistics.h"
  18. namespace embree
  19. {
  20. template<int N>
  21. BVHN<N>::BVHN (const PrimitiveType& primTy, Scene* scene)
  22. : AccelData((N==4) ? AccelData::TY_BVH4 : (N==8) ? AccelData::TY_BVH8 : AccelData::TY_UNKNOWN),
  23. primTy(primTy), device(scene->device), scene(scene),
  24. root(emptyNode), msmblur(false), numTimeSteps(1), alloc(scene->device,scene->isStatic()), numPrimitives(0), numVertices(0) , primrefs(scene->device)
  25. {
  26. }
  27. template<int N>
  28. BVHN<N>::~BVHN ()
  29. {
  30. for (size_t i=0; i<objects.size(); i++)
  31. delete objects[i];
  32. }
  33. template<int N>
  34. void BVHN<N>::clear()
  35. {
  36. set(BVHN::emptyNode,empty,0);
  37. alloc.clear();
  38. }
  39. template<int N>
  40. void BVHN<N>::set (NodeRef root, const LBBox3fa& bounds, size_t numPrimitives)
  41. {
  42. this->root = root;
  43. this->bounds = bounds;
  44. this->numPrimitives = numPrimitives;
  45. }
  46. template<int N>
  47. void BVHN<N>::printStatistics()
  48. {
  49. std::cout << BVHNStatistics<N>(this).str();
  50. }
  51. template<int N>
  52. void BVHN<N>::clearBarrier(NodeRef& node)
  53. {
  54. if (node.isBarrier())
  55. node.clearBarrier();
  56. else if (!node.isLeaf()) {
  57. BaseNode* n = node.baseNode(BVH_FLAG_ALIGNED_NODE); // FIXME: flags should be stored in BVH
  58. for (size_t c=0; c<N; c++)
  59. clearBarrier(n->child(c));
  60. }
  61. }
  62. template<int N>
  63. void BVHN<N>::layoutLargeNodes(size_t num)
  64. {
  65. struct NodeArea
  66. {
  67. __forceinline NodeArea() {}
  68. __forceinline NodeArea(NodeRef& node, const BBox3fa& bounds)
  69. : node(&node), A(node.isLeaf() ? float(neg_inf) : area(bounds)) {}
  70. __forceinline bool operator< (const NodeArea& other) const {
  71. return this->A < other.A;
  72. }
  73. NodeRef* node;
  74. float A;
  75. };
  76. std::vector<NodeArea> lst;
  77. lst.reserve(num);
  78. lst.push_back(NodeArea(root,empty));
  79. while (lst.size() < num)
  80. {
  81. std::pop_heap(lst.begin(), lst.end());
  82. NodeArea n = lst.back(); lst.pop_back();
  83. if (!n.node->isAlignedNode()) break;
  84. AlignedNode* node = n.node->alignedNode();
  85. for (size_t i=0; i<N; i++) {
  86. if (node->child(i) == BVHN::emptyNode) continue;
  87. lst.push_back(NodeArea(node->child(i),node->bounds(i)));
  88. std::push_heap(lst.begin(), lst.end());
  89. }
  90. }
  91. for (size_t i=0; i<lst.size(); i++)
  92. lst[i].node->setBarrier();
  93. root = layoutLargeNodesRecursion(root,*alloc.threadLocal());
  94. }
  95. template<int N>
  96. typename BVHN<N>::NodeRef BVHN<N>::layoutLargeNodesRecursion(NodeRef& node, FastAllocator::ThreadLocal& allocator)
  97. {
  98. if (node.isBarrier()) {
  99. node.clearBarrier();
  100. return node;
  101. }
  102. else if (node.isAlignedNode())
  103. {
  104. AlignedNode* oldnode = node.alignedNode();
  105. AlignedNode* newnode = (BVHN::AlignedNode*) allocator.malloc(sizeof(BVHN::AlignedNode),byteNodeAlignment);
  106. *newnode = *oldnode;
  107. for (size_t c=0; c<N; c++)
  108. newnode->child(c) = layoutLargeNodesRecursion(oldnode->child(c),allocator);
  109. return encodeNode(newnode);
  110. }
  111. else return node;
  112. }
  113. template<int N>
  114. double BVHN<N>::preBuild(const std::string& builderName)
  115. {
  116. if (builderName == "")
  117. return inf;
  118. if (device->verbosity(1))
  119. std::cout << "building BVH" << N << "<" << primTy.name << "> using " << builderName << " ..." << std::flush;
  120. double t0 = 0.0;
  121. if (device->benchmark || device->verbosity(1)) t0 = getSeconds();
  122. return t0;
  123. }
  124. template<int N>
  125. void BVHN<N>::postBuild(double t0)
  126. {
  127. if (t0 == double(inf))
  128. return;
  129. double dt = 0.0;
  130. if (device->benchmark || device->verbosity(1))
  131. dt = getSeconds()-t0;
  132. /* print statistics */
  133. if (device->verbosity(1)) {
  134. const size_t usedBytes = alloc.getUsedBytes();
  135. std::cout << " [DONE]" << " " << 1000.0f*dt << "ms, " << 1E-6*double(numPrimitives)/dt << " Mprim/s, " << 1E-9*double(usedBytes)/dt << " GB/s" << std::endl;
  136. }
  137. if (device->verbosity(2))
  138. printStatistics();
  139. if (device->verbosity(2))
  140. alloc.print_statistics(device->verbosity(3));
  141. /* benchmark mode */
  142. if (device->benchmark) {
  143. BVHNStatistics<N> stat(this);
  144. std::cout << "BENCHMARK_BUILD " << dt << " " << double(numPrimitives)/dt << " " << stat.sah() << " " << stat.bytesUsed() << std::endl;
  145. }
  146. }
  147. #if defined(__AVX__)
  148. template class BVHN<8>;
  149. #else
  150. template class BVHN<4>;
  151. #endif
  152. }