NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BVHNode.hpp
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 
33 #pragma once
34 #include "base/Array.hpp"
35 #include "bvh/Platform.hpp"
36 #include "Util.hpp"
37 
38 namespace FW
39 {
40 
41 // TODO: remove m_probability. Node needed after all?
42 
47 {
53 };
54 
58 class SplitInfo
59 {
60 public:
61 
65  enum SplitType { SAH, SBVH, OSAH };
66 
71 
72 public:
76  SplitInfo(): m_code(0) {}
77 
84  SplitInfo(S32 axis, SplitType splitType, bool osahTested): m_code(0) { m_code |= (osahTested << 31); m_code |= splitType << 2; m_code |= axis; }
85 
90  SplitInfo(unsigned long bitCode): m_code(bitCode) {}
91 
95  bool getOSAHTested() const { return (m_code & 0x80000000) > 0; }
96 
100  bool getOSAHChosen() const { return getType() == OSAH; }
101 
105  SplitType getType() const { return (SplitType)((m_code & 0xC) >> 2); }
106 
110  String getTypeName() const { return getOSAHTested() ? String(m_typeNames[getType()]) + "+" : String(m_typeNames[getType()]); }
111 
115  SplitAxis getAxis() const { return (SplitAxis)(m_code & 0x3); }
116 
120  String getAxisName() const { return m_axisNames[getAxis()]; }
121 
125  unsigned long getBitCode() const { return m_code; }
126 
127 private:
128  unsigned long m_code;
129  static char* m_axisNames[3];
130  static char* m_typeNames[3];
131 };
132 
136 class BVHNode
137 {
138 public:
139 
144 
148  virtual bool isLeaf() const = 0;
149 
153  virtual S32 getNumChildNodes() const = 0;
154 
160  virtual BVHNode* getChildNode(S32 i) const = 0;
161 
165  virtual S32 getNumTriangles() const { return 0; }
166 
170  float getArea() const { return m_bounds.area(); }
171 
173 
174  // These are somewhat experimental, for some specific test and may be invalid...
177 
178  int m_treelet;
179  int m_index;
180 
181  // Subtree functions
182 
189 
196  void computeSubtreeProbabilities(const Platform& p, float parentProbability, float& sah);
197 
203  float computeSubtreeSAHCost(const Platform& p) const; // NOTE: assumes valid probabilities
204 
208  void deleteSubtree();
209 
215  void assignIndicesDepthFirst (S32 index=0, bool includeLeafNodes=true);
216 
222  void assignIndicesBreadthFirst(S32 index=0, bool includeLeafNodes=true);
223 };
224 
228 class InnerNode : public BVHNode
229 {
230 public:
231 
238  InnerNode(const AABB& bounds, BVHNode* child0, BVHNode* child1) { m_bounds=bounds; m_children[0] = child0; m_children[1] = child1; };
239 
249  InnerNode(const AABB& bounds, BVHNode* child0, BVHNode* child1, S32 axis, SplitInfo::SplitType splitType, bool osahTested): m_splitInfo(axis, splitType, osahTested) { m_bounds=bounds; m_children[0] = child0; m_children[1] = child1; };
250 
254  bool isLeaf() const { return false; }
255 
259  S32 getNumChildNodes() const { return 2; }
260 
266  BVHNode* getChildNode(S32 i) const { FW_ASSERT(i>=0 && i<2); return m_children[i]; }
267 
270 };
271 
275 class LeafNode : public BVHNode
276 {
277 public:
278 
285  LeafNode(const AABB& bounds,int lo,int hi) { m_bounds=bounds; m_lo=lo; m_hi=hi; }
286 
291  LeafNode(const LeafNode& s) { *this = s; }
292 
296  bool isLeaf() const { return true; }
297 
301  S32 getNumChildNodes() const { return 0; }
302 
308  BVHNode* getChildNode(S32) const { return NULL; }
309 
313  S32 getNumTriangles() const { return m_hi-m_lo; }
316 };
317 
318 } //
#define NULL
Definition: Defs.hpp:39
SplitInfo(S32 axis, SplitType splitType, bool osahTested)
Constructor.
Definition: BVHNode.hpp:84
S32 m_lo
Lower index to the BVH's triangle index array.
Definition: BVHNode.hpp:314
BVH_STAT
Available BVH stats.
Definition: BVHNode.hpp:46
LeafNode(const AABB &bounds, int lo, int hi)
Constructor.
Definition: BVHNode.hpp:285
BVH leaf node.
Definition: BVHNode.hpp:275
String getTypeName() const
Definition: BVHNode.hpp:110
BVHNode()
Constructor.
Definition: BVHNode.hpp:143
float computeSubtreeSAHCost(const Platform &p) const
Calculates subtree SAH cost. Requires calculated probabilities.
Definition: BVHNode.cpp:87
BVH inner node.
Definition: BVHNode.hpp:228
S32 getNumChildNodes() const
Definition: BVHNode.hpp:259
bool getOSAHChosen() const
Definition: BVHNode.hpp:100
int m_index
in linearized tree (qmachine uses this).
Definition: BVHNode.hpp:179
BVHNode * getChildNode(S32 i) const
Returns one of the node's child nodes.
Definition: BVHNode.hpp:266
virtual S32 getNumTriangles() const
Definition: BVHNode.hpp:165
SplitAxis getAxis() const
Definition: BVHNode.hpp:115
SplitInfo(unsigned long bitCode)
Constructor.
Definition: BVHNode.hpp:90
S32 getNumChildNodes() const
Definition: BVHNode.hpp:301
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
float getArea() const
Definition: BVHNode.hpp:170
FW_CUDA_FUNC float area(void) const
Definition: Util.hpp:45
String getAxisName() const
Definition: BVHNode.hpp:120
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
virtual bool isLeaf() const =0
SplitType
Available split types.
Definition: BVHNode.hpp:65
SplitInfo m_splitInfo
Split info.
Definition: BVHNode.hpp:269
SplitInfo()
Constructor.
Definition: BVHNode.hpp:76
bool getOSAHTested() const
Definition: BVHNode.hpp:95
Declarations for platform settings.
bool isLeaf() const
Definition: BVHNode.hpp:296
int m_treelet
For queuing tests (qmachine uses this).
Definition: BVHNode.hpp:178
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
Class holding information about a split of a BVH node.
Definition: BVHNode.hpp:58
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
BVHNode * getChildNode(S32) const
Returns one of the node's child nodes.
Definition: BVHNode.hpp:308
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
LeafNode(const LeafNode &s)
Copy constructor.
Definition: BVHNode.hpp:291
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
InnerNode(const AABB &bounds, BVHNode *child0, BVHNode *child1, S32 axis, SplitInfo::SplitType splitType, bool osahTested)
Constructor.
Definition: BVHNode.hpp:249
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
S32 getNumTriangles() const
Definition: BVHNode.hpp:313
bool isLeaf() const
Definition: BVHNode.hpp:254
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
unsigned long getBitCode() const
Definition: BVHNode.hpp:125
S32 m_hi
Higher index to the BVH's triangle index array.
Definition: BVHNode.hpp:315
SplitAxis
Available split axes.
Definition: BVHNode.hpp:70
BVHNode * m_children[2]
Child nodes.
Definition: BVHNode.hpp:268
SplitType getType() const
Definition: BVHNode.hpp:105
InnerNode(const AABB &bounds, BVHNode *child0, BVHNode *child1)
Constructor.
Definition: BVHNode.hpp:238