bvh_statistics.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_statistics.h"
  17. namespace embree
  18. {
  19. template<int N>
  20. BVHNStatistics<N>::BVHNStatistics (BVH* bvh) : bvh(bvh)
  21. {
  22. double A = max(0.0f,bvh->getLinearBounds().expectedHalfArea());
  23. if (bvh->msmblur)
  24. {
  25. NodeRef* roots = (NodeRef*)(size_t)bvh->root;
  26. for (size_t i=0; i<bvh->numTimeSteps-1; i++) {
  27. const BBox1f t0t1(float(i+0)/float(bvh->numTimeSteps-1),
  28. float(i+1)/float(bvh->numTimeSteps-1));
  29. stat = stat + statistics(roots[i],A,t0t1);
  30. }
  31. }
  32. else {
  33. stat = statistics(bvh->root,A,BBox1f(0.0f,1.0f));
  34. }
  35. }
  36. template<int N>
  37. std::string BVHNStatistics<N>::str()
  38. {
  39. std::ostringstream stream;
  40. stream.setf(std::ios::fixed, std::ios::floatfield);
  41. stream << " primitives = " << bvh->numPrimitives << ", vertices = " << bvh->numVertices << ", depth = " << stat.depth << std::endl;
  42. size_t totalBytes = stat.bytes(bvh);
  43. double totalSAH = stat.sah(bvh);
  44. stream << " total : sah = " << std::setw(7) << std::setprecision(3) << totalSAH << " (100.00%), ";
  45. stream << "#bytes = " << std::setw(7) << std::setprecision(2) << totalBytes/1E6 << " MB (100.00%), ";
  46. stream << "#nodes = " << std::setw(7) << stat.size() << " (" << std::setw(6) << std::setprecision(2) << 100.0*stat.fillRate(bvh) << "% filled), ";
  47. stream << "#bytes/prim = " << std::setw(6) << std::setprecision(2) << double(totalBytes)/double(bvh->numPrimitives) << std::endl;
  48. if (stat.statAlignedNodes.numNodes ) stream << " alignedNodes : " << stat.statAlignedNodes.toString(bvh,totalSAH,totalBytes) << std::endl;
  49. if (stat.statUnalignedNodes.numNodes ) stream << " unalignedNodes : " << stat.statUnalignedNodes.toString(bvh,totalSAH,totalBytes) << std::endl;
  50. if (stat.statAlignedNodesMB.numNodes ) stream << " alignedNodesMB : " << stat.statAlignedNodesMB.toString(bvh,totalSAH,totalBytes) << std::endl;
  51. if (stat.statUnalignedNodesMB.numNodes) stream << " unalignedNodesMB : " << stat.statUnalignedNodesMB.toString(bvh,totalSAH,totalBytes) << std::endl;
  52. if (stat.statTransformNodes.numNodes ) stream << " transformNodes : " << stat.statTransformNodes.toString(bvh,totalSAH,totalBytes) << std::endl;
  53. if (stat.statQuantizedNodes.numNodes ) stream << " quantizedNodes : " << stat.statQuantizedNodes.toString(bvh,totalSAH,totalBytes) << std::endl;
  54. if (true) stream << " leaves : " << stat.statLeaf.toString(bvh,totalSAH,totalBytes) << std::endl;
  55. if (true) stream << " histogram : " << stat.statLeaf.histToString() << std::endl;
  56. return stream.str();
  57. }
  58. template<int N>
  59. typename BVHNStatistics<N>::Statistics BVHNStatistics<N>::statistics(NodeRef node, const double A, const BBox1f t0t1)
  60. {
  61. Statistics s;
  62. assert(t0t1.size() > 0.0f);
  63. double dt = max(0.0f,t0t1.size());
  64. if (node.isAlignedNode())
  65. {
  66. AlignedNode* n = node.alignedNode();
  67. for (size_t i=0; i<N; i++) {
  68. if (n->child(i) == BVH::emptyNode) continue;
  69. s.statAlignedNodes.numChildren++;
  70. const double Ai = max(0.0f,halfArea(n->extend(i)));
  71. s = s + statistics(n->child(i),Ai,t0t1);
  72. }
  73. s.statAlignedNodes.numNodes++;
  74. s.statAlignedNodes.nodeSAH += dt*A;
  75. s.depth++;
  76. }
  77. else if (node.isUnalignedNode())
  78. {
  79. UnalignedNode* n = node.unalignedNode();
  80. for (size_t i=0; i<N; i++) {
  81. if (n->child(i) == BVH::emptyNode) continue;
  82. s.statUnalignedNodes.numChildren++;
  83. const double Ai = max(0.0f,halfArea(n->extend(i)));
  84. s = s + statistics(n->child(i),Ai,t0t1);
  85. }
  86. s.statUnalignedNodes.numNodes++;
  87. s.statUnalignedNodes.nodeSAH += dt*A;
  88. s.depth++;
  89. }
  90. else if (node.isAlignedNodeMB())
  91. {
  92. AlignedNodeMB* n = node.alignedNodeMB();
  93. for (size_t i=0; i<N; i++) {
  94. if (n->child(i) == BVH::emptyNode) continue;
  95. s.statAlignedNodesMB.numChildren++;
  96. const double Ai = max(0.0f,n->expectedHalfArea(i,t0t1));
  97. s = s + statistics(n->child(i),Ai,t0t1);
  98. }
  99. s.statAlignedNodesMB.numNodes++;
  100. s.statAlignedNodesMB.nodeSAH += dt*A;
  101. s.depth++;
  102. }
  103. else if (node.isUnalignedNodeMB())
  104. {
  105. UnalignedNodeMB* n = node.unalignedNodeMB();
  106. for (size_t i=0; i<N; i++) {
  107. if (n->child(i) == BVH::emptyNode) continue;
  108. s.statUnalignedNodesMB.numChildren++;
  109. const double Ai = max(0.0f,halfArea(n->extend0(i)));
  110. s = s + statistics(n->child(i),Ai,t0t1);
  111. }
  112. s.statUnalignedNodesMB.numNodes++;
  113. s.statUnalignedNodesMB.nodeSAH += dt*A;
  114. s.depth++;
  115. }
  116. else if (node.isTransformNode())
  117. {
  118. s.statTransformNodes.numNodes++;
  119. s.statTransformNodes.nodeSAH += dt*A;
  120. s.depth++;
  121. }
  122. else if (node.isQuantizedNode())
  123. {
  124. QuantizedNode* n = node.quantizedNode();
  125. for (size_t i=0; i<N; i++) {
  126. if (n->child(i) == BVH::emptyNode) continue;
  127. s.statQuantizedNodes.numChildren++;
  128. const double Ai = max(0.0f,halfArea(n->extend(i)));
  129. s = s + statistics(n->child(i),Ai,t0t1);
  130. }
  131. s.statQuantizedNodes.numNodes++;
  132. s.statQuantizedNodes.nodeSAH += dt*A;
  133. s.depth++;
  134. }
  135. else if (node.isLeaf())
  136. {
  137. size_t num; const char* tri = node.leaf(num);
  138. if (num)
  139. {
  140. for (size_t i=0; i<num; i++) {
  141. s.statLeaf.numPrims += bvh->primTy.size(tri+i*bvh->primTy.bytes);
  142. }
  143. s.statLeaf.numLeaves++;
  144. s.statLeaf.numPrimBlocks += num;
  145. s.statLeaf.leafSAH += dt*A*num;
  146. if (num-1 < Statistics::LeafStat::NHIST) {
  147. s.statLeaf.numPrimBlocksHistogram[num-1]++;
  148. }
  149. }
  150. }
  151. else {
  152. throw std::runtime_error("not supported node type in bvh_statistics");
  153. }
  154. return s;
  155. }
  156. #if defined(__AVX__)
  157. template class BVHNStatistics<8>;
  158. #else
  159. template class BVHNStatistics<4>;
  160. #endif
  161. }