NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BVHNode.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "bvh/BVHNode.hpp"
29 
30 namespace FW
31 {
32 
33 char* SplitInfo::m_axisNames[3] = { "x", "y", "z" };
34 char* SplitInfo::m_typeNames[3] = { "SAH", "SBVH", "OSAH" };
35 
37 {
38  int cnt;
39  switch(stat)
40  {
41  default: FW_ASSERT(0); // unknown mode
42  case BVH_STAT_NODE_COUNT: cnt = 1; break;
43  case BVH_STAT_LEAF_COUNT: cnt = isLeaf() ? 1 : 0; break;
44  case BVH_STAT_INNER_COUNT: cnt = isLeaf() ? 0 : 1; break;
45  case BVH_STAT_TRIANGLE_COUNT: cnt = isLeaf() ? reinterpret_cast<const LeafNode*>(this)->getNumTriangles() : 0; break;
46  case BVH_STAT_CHILDNODE_COUNT: cnt = getNumChildNodes(); break;
47  }
48 
49  if(!isLeaf())
50  {
51  for(int i=0;i<getNumChildNodes();i++)
52  cnt += getChildNode(i)->getSubtreeSize(stat);
53  }
54 
55  return cnt;
56 }
57 
58 
60 {
61  for(int i=0;i<getNumChildNodes();i++)
63 
64  delete this;
65 }
66 
67 
68 void BVHNode::computeSubtreeProbabilities(const Platform& p,float probability, float& sah)
69 {
70  sah += probability * p.getCost(this->getNumChildNodes(),this->getNumTriangles());
71 
72  m_probability = probability;
73 
74  for(int i=0;i<getNumChildNodes();i++)
75  {
76  BVHNode* child = getChildNode(i);
77  child->m_parentProbability = probability;
78  float childProbability = 0.0f;
79  if (probability > 0.0f)
80  childProbability = probability * child->m_bounds.area()/this->m_bounds.area();
81  child->computeSubtreeProbabilities(p, childProbability, sah );
82  }
83 }
84 
85 
86 // TODO: requires valid probabilities...
88 {
90 
91  for(int i=0;i<getNumChildNodes();i++)
92  SAH += getChildNode(i)->computeSubtreeSAHCost(p);
93 
94  return SAH;
95 }
96 
97 //-------------------------------------------------------------
98 
99 void assignIndicesDepthFirstRecursive( BVHNode* node, S32& index, bool includeLeafNodes )
100 {
101  if(node->isLeaf() && !includeLeafNodes)
102  return;
103 
104  node->m_index = index++;
105  for(int i=0;i<node->getNumChildNodes();i++)
106  assignIndicesDepthFirstRecursive(node->getChildNode(i), index, includeLeafNodes);
107 }
108 
109 void BVHNode::assignIndicesDepthFirst( S32 index, bool includeLeafNodes )
110 {
111  assignIndicesDepthFirstRecursive( this, index, includeLeafNodes );
112 }
113 
114 //-------------------------------------------------------------
115 
116 void BVHNode::assignIndicesBreadthFirst( S32 index, bool includeLeafNodes )
117 {
118  Array<BVHNode*> nodes;
119  nodes.add(this);
120  S32 head=0;
121 
122  while(head < nodes.getSize())
123  {
124  // pop
125  BVHNode* node = nodes[head++];
126 
127  // discard
128  if(node->isLeaf() && !includeLeafNodes)
129  continue;
130 
131  // assign
132  node->m_index = index++;
133 
134  // push children
135  for(int i=0;i<node->getNumChildNodes();i++)
136  nodes.add(node->getChildNode(i));
137  }
138 }
139 
140 
141 } //
BVH_STAT
Available BVH stats.
Definition: BVHNode.hpp:46
void assignIndicesDepthFirstRecursive(BVHNode *node, S32 &index, bool includeLeafNodes)
Definition: BVHNode.cpp:99
BVH leaf node.
Definition: BVHNode.hpp:275
float computeSubtreeSAHCost(const Platform &p) const
Calculates subtree SAH cost. Requires calculated probabilities.
Definition: BVHNode.cpp:87
int m_index
in linearized tree (qmachine uses this).
Definition: BVHNode.hpp:179
virtual S32 getNumTriangles() const
Definition: BVHNode.hpp:165
void assignIndicesDepthFirst(S32 index=0, bool includeLeafNodes=true)
Assigns node's sbutree indices in depth first order.
Definition: BVHNode.cpp:109
void assignIndicesBreadthFirst(S32 index=0, bool includeLeafNodes=true)
Assigns node's subtree indices in breadth first order.
Definition: BVHNode.cpp:116
void computeSubtreeProbabilities(const Platform &p, float parentProbability, float &sah)
Calculates node's subtree probabilities and also sah price.
Definition: BVHNode.cpp:68
FW_CUDA_FUNC float area(void) const
Definition: Util.hpp:45
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
virtual bool isLeaf() const =0
AABB m_bounds
Bounding box of the node.
Definition: BVHNode.hpp:172
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint index
Definition: DLLImports.inl:363
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
Definition: DLLImports.inl:88
virtual BVHNode * getChildNode(S32 i) const =0
Returns one of the node's child nodes.
void deleteSubtree()
Deletes node's subtree.
Definition: BVHNode.cpp:59
virtual S32 getNumChildNodes() const =0
int getSubtreeSize(BVH_STAT stat=BVH_STAT_NODE_COUNT) const
Calculates various information about the node's subtree.
Definition: BVHNode.cpp:36
Class holding various SAH and batch processing parameters.
Definition: Platform.hpp:46
float getCost(int numChildNodes, int numTris) const
Calculates cost of a single node.
Definition: Platform.hpp:88
BVH virtual node. Parent class of both a leaf node and an inner node.
Definition: BVHNode.hpp:136
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void * p
Definition: DLLImports.inl:66
Declarations for a BVH node.
float m_parentProbability
Probability of coming to parent (widebvh uses this).
Definition: BVHNode.hpp:176
float m_probability
Probability of coming here (widebvh uses this).
Definition: BVHNode.hpp:175
S32 getSize(void) const