NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KDTree.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2013, Radek Stibora
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 the <organization> 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 #pragma once
29 #include <cstdio>
30 #include "Scene.hpp"
31 #include "KDTreeNode.hpp"
32 #include "ray/RayBuffer.hpp"
34 
35 namespace FW
36 {
37 
41 class KDTree
42 {
43 public:
48  {
52  };
53 
57  struct Stats
58  {
62  Stats() { clear(); }
63 
67  void clear() { memset(this, 0, sizeof(Stats)); }
68 
72  void print() const { std::printf("Tree stats: [bfactor=2] %d nodes (%d+%d), ?.2f SAHCost, %.1f children/inner, %.1f tris/leaf, %.1f%% duplicates, %d empty leaves\n",numLeafNodes+numInnerNodes, numLeafNodes,numInnerNodes, 1.f*numChildNodes/max(numInnerNodes,1), 1.f*numTris/max(numLeafNodes,1), 1.f*percentDuplicates, numEmptyLeaves); }
73 
80  };
81 
85  struct BuildParams
86  {
91  {
92  stats = nullptr;
93  enablePrints = true;
95  }
96 
98  bool enablePrints;
100  };
101 
108  KDTree (Scene* scene, const Platform& platform, const BuildParams& params);
109 
113  ~KDTree (void) { if(m_root != nullptr) m_root->deleteSubtree(); }
114 
119  Scene* getScene (void) const { return m_scene; }
120 
125  const Platform& getPlatform (void) const { return m_platform; }
126 
131  KDTreeNode* getRoot (void) const { return m_root; }
132 
137  Array<S32>& getTriIndices (void) { return m_triIndices; }
138 
143  const Array<S32>& getTriIndices (void) const { return m_triIndices; }
144 
145 private:
146  Scene* m_scene;
147  Platform m_platform;
148 
149  KDTreeNode* m_root;
150  Array<S32> m_triIndices;
151 };
152 
153 
154 }
Stats * stats
Statistics collected during build phase. Set to NULL if no stats should be collected.
Definition: KDTree.hpp:97
KDTreeNode * getRoot(void) const
Gets root node of the k-d tree.
Definition: KDTree.hpp:131
S32 numTris
Triangle count of the source scene.
Definition: KDTree.hpp:77
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
KDTree(Scene *scene, const Platform &platform, const BuildParams &params)
Constructor.
Definition: KDTree.cpp:35
Strucure holding build parameters.
Definition: KDTree.hpp:85
K-d tree acceleration structure class.
Definition: KDTree.hpp:41
BuildParams(void)
Constructor.
Definition: KDTree.hpp:90
Structure holding statistics about k-d tree.
Definition: KDTree.hpp:57
S32 numLeafNodes
Number of leaf nodes.
Definition: KDTree.hpp:75
~KDTree(void)
Destructor.
Definition: KDTree.hpp:113
float F32
Definition: Defs.hpp:89
Scene * getScene(void) const
Gets source scene of the k-d tree.
Definition: KDTree.hpp:119
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Definition: Math.hpp:462
signed int S32
Definition: Defs.hpp:88
Stats()
Constructor.
Definition: KDTree.hpp:62
void clear()
Resets data to default values.
Definition: KDTree.hpp:67
S32 numChildNodes
Number of child nodes.
Definition: KDTree.hpp:76
void deleteSubtree()
Deletes node's subtree.
Definition: KDTreeNode.cpp:34
Array< S32 > & getTriIndices(void)
Returns an array of triangle indices to which leaf nodes are pointig. These indices point to scene's ...
Definition: KDTree.hpp:137
void print() const
Prints statistics to stdout.
Definition: KDTree.hpp:72
BuilderType builder
Defines which builder type will be used to build the k-d tree.
Definition: KDTree.hpp:99
Class holding 3d scene.
Definition: Scene.hpp:44
S32 numInnerNodes
Number of inner nodes.
Definition: KDTree.hpp:74
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
BuilderType
Supported k-d tree builder types.
Definition: KDTree.hpp:47
S32 numEmptyLeaves
Number of empty leaves.
Definition: KDTree.hpp:78
void printf(const char *fmt,...)
Definition: Defs.cpp:225
Class holding various SAH and batch processing parameters.
Definition: Platform.hpp:46
bool enablePrints
Flag whether to print information during build phase.
Definition: KDTree.hpp:98
const Array< S32 > & getTriIndices(void) const
Returns an array of triangle indices reffered to by the leaf nodes. These indices point to the scene'...
Definition: KDTree.hpp:143
const Platform & getPlatform(void) const
Gets platform settings of the k-d tree.
Definition: KDTree.hpp:125
F32 percentDuplicates
Percentage ratio of duplicated references to initial number of references (which is same as triangle ...
Definition: KDTree.hpp:79
K-d tree virtual parent node class.
Definition: KDTreeNode.hpp:51