NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
BVH.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 
32 #pragma once
33 #include "Scene.hpp"
34 #include "bvh/BVHNode.hpp"
35 #include "ray/RayBuffer.hpp"
37 #include "Environment.h"
38 
39 namespace FW
40 {
41 
45 struct RayStats
46 {
50  RayStats() { clear(); }
51 
55  void clear() { memset(this,0,sizeof(RayStats)); }
56 
60  void print() const { if(numRays>0) printf("Ray stats: (%s) %d rays, %.1f tris/ray, %.1f nodes/ray (cost=%.2f) %.2f treelets/ray\n", platform.getName().getPtr(), numRays, 1.f*numTriangleTests/numRays, 1.f*numNodeTests/numRays, (platform.getSAHTriangleCost()*numTriangleTests/numRays + platform.getSAHNodeCost()*numNodeTests/numRays), 1.f*numTreelets/numRays ); }
61 
67 
68 };
69 
74 class BVH : public AccelerationStructure
75 {
76 public:
77 
81  struct Stats
82  {
86  Stats() { clear(); }
87 
91  void clear() { memset(this, 0, sizeof(Stats)); }
92 
96  void print() const { printf("Tree stats: [bfactor=%d] %d nodes (%d+%d), %.2f SAHCost, %.1f children/inner, %.1f tris/leaf\n", branchingFactor,numLeafNodes+numInnerNodes, numLeafNodes,numInnerNodes, SAHCost, 1.f*numChildNodes/max(numInnerNodes,1), 1.f*numTris/max(numLeafNodes,1)); }
97 
104  };
105 
109  struct BuildParams
110  {
120  int cameraIdx;
121  bool twoTrees;
122 
127  {
128  stats = NULL;
129  enablePrints = true;
130  splitAlpha = 1.0e-5f;
131  osahWeight = 0.9f;
132  //camera = NULL;
133  cameraIdx = 0;
134  twoTrees = false;
135  //visibility = NULL;
136  }
137 
142  U32 computeHash(void) const
143  {
145  }
146  };
147 
148 public:
149 
156  BVH (Scene* scene, const Platform& platform, const BuildParams& params, Environment* env);
157 
161  ~BVH (void) { if(m_root) m_root->deleteSubtree(); }
162 
167  BVHNode* getRoot (void) const { return m_root; }
168 
174  void trace (RayBuffer& rays, RayStats* stats = NULL) const;
175 
180  Array<S32>& getTriIndices (void) { return m_triIndices; }
181 
186  const Array<S32>& getTriIndices (void) const { return m_triIndices; }
187 
188 private:
189 
198  void traceRecursive (BVHNode* node, Ray& ray, RayResult& result, bool needClosestHit, RayStats* stats) const;
199 
200  BVHNode* m_root;
201  Array<S32> m_triIndices;
202  Environment* m_env;
203 };
204 
205 }
bool enablePrints
Flag whether to enable prints about build progress.
Definition: BVH.hpp:112
const String & getName() const
Definition: Platform.hpp:68
U32 floatToBits(F32 a)
Definition: Math.hpp:95
Stucture holding the BVH build parameters.
Definition: BVH.hpp:109
#define NULL
Definition: Defs.hpp:39
RayStats()
Constructor.
Definition: BVH.hpp:50
Array< AABB > empty_boxes
Information about boxes with no triangles inside.
Definition: BVH.hpp:116
const char * getPtr(void) const
Definition: String.hpp:51
const Array< S32 > & getTriIndices(void) const
Returns an array of triangle indices to which leaf nodes are pointig. These indices point to scene's ...
Definition: BVH.hpp:186
S32 branchingFactor
Number of children nodes per one parent node.
Definition: BVH.hpp:99
Stats * stats
Statistics. If NULL, no statistics are gathered.
Definition: BVH.hpp:111
void print() const
Prints the statistics to the console.
Definition: BVH.hpp:96
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 GLfloat GLfloat GLfloat z GLuint GLint GLenum GLboolean GLsizei const GLvoid pointer GLuint GLuint const GLchar name GLenum GLsizei GLenum GLsizei GLsizei height GLenum GLuint renderbuffer GLenum GLenum GLint * params
Definition: DLLImports.inl:373
S32 numLeafNodes
Total number of leaf nodes.
Definition: BVH.hpp:101
S32 numTreelets
Total number of traversal steps.
Definition: BVH.hpp:65
S32 numTriangleTests
Total number of ray-triangle tests.
Definition: BVH.hpp:63
S32 numRays
Total number of rays.
Definition: BVH.hpp:62
S32 numChildNodes
Total number of children nodes.
Definition: BVH.hpp:102
S32 numInnerNodes
Total number of inner nodes.
Definition: BVH.hpp:100
Definition: Util.hpp:62
Structure holding ray statistics. Also provides print to the console. These statistics are used in a ...
Definition: BVH.hpp:45
float getSAHNodeCost() const
Definition: Platform.hpp:79
F32 splitAlpha
Spatial split area threshold.
Definition: BVH.hpp:113
Sturcture for holding statistics about the BVH.
Definition: BVH.hpp:81
void print() const
Prints ray statistics to the console.
Definition: BVH.hpp:60
Array< S32 > & getTriIndices(void)
Returns an array of triangle indices to which leaf nodes are pointig. These indices point to scene's ...
Definition: BVH.hpp:180
F32 osahWeight
Weighting factor for OSAH construction.
Definition: BVH.hpp:114
float F32
Definition: Defs.hpp:89
~BVH(void)
Destructor.
Definition: BVH.hpp:161
Ray buffer class. Stores rays.
Definition: RayBuffer.hpp:38
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Definition: Math.hpp:462
BVH acceleration structure class.
Definition: BVH.hpp:74
signed int S32
Definition: Defs.hpp:88
String logDirectory
Directory where the log file will be saved.
Definition: BVH.hpp:118
S32 numTris
Total number of triangles.
Definition: BVH.hpp:103
Buffer * visibility
Visibility buffer for the CPU renderer.
Definition: BVH.hpp:117
S32 numNodeTests
Total number of ray-node tests.
Definition: BVH.hpp:64
String accelerator
The name of the acceleration data structure method for ray tracing.
Definition: BVH.hpp:115
unsigned int U32
Definition: Defs.hpp:85
U32 hashBits(U32 a, U32 b=FW_HASH_MAGIC, U32 c=0)
Definition: Hash.hpp:183
Class holding 3d scene.
Definition: Scene.hpp:44
Stats()
Constructor.
Definition: BVH.hpp:86
int cameraIdx
Camera index.
Definition: BVH.hpp:120
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
void deleteSubtree()
Deletes node's subtree.
Definition: BVHNode.cpp:59
void printf(const char *fmt,...)
Definition: Defs.cpp:225
BuildParams(void)
Constructor.
Definition: BVH.hpp:126
Class holding various SAH and batch processing parameters.
Definition: Platform.hpp:46
void clear()
Resets the statistics to the default values.
Definition: BVH.hpp:91
void clear()
Resets the statistics to the default values.
Definition: BVH.hpp:55
String buildName
Build name.
Definition: BVH.hpp:119
U32 computeHash(void) const
Computes hash of the build parameters.
Definition: BVH.hpp:142
BVH virtual node. Parent class of both a leaf node and an inner node.
Definition: BVHNode.hpp:136
F32 SAHCost
Total sah cost of the BVH.
Definition: BVH.hpp:98
Declarations for a BVH node.
float getSAHTriangleCost() const
Definition: Platform.hpp:74
BVHNode * getRoot(void) const
Returns root node of the BVH.
Definition: BVH.hpp:167
void trace(RayBuffer &rays, RayStats *stats=NULL) const
CPU traversal.
Definition: BVH.cpp:82
Platform platform
Platform settings of the BVH. Set by whoever sets the stats.
Definition: BVH.hpp:66
BVH(Scene *scene, const Platform &platform, const BuildParams &params, Environment *env)
Constructor.
Definition: BVH.cpp:36
bool twoTrees
Flag whether to build BVH from two separate trees.
Definition: BVH.hpp:121