NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CudaKDTree.cpp
Go to the documentation of this file.
1 
2 #include "CudaKDTree.hpp"
3 
4 using namespace FW;
5 
6 //------------------------------------------------------------------------
7 
9 {
10  createNodeTriIdx(kdtree);
11  createWoopTri(kdtree);
12 }
13 
14 //------------------------------------------------------------------------
15 
17 {
18  Vec3f min, max;
19  in >> min >> max >> m_nodes >> m_triWoop >> m_triIndex;
20  m_bbox = AABB(min, max);
21 }
22 
23 //------------------------------------------------------------------------
24 
26 {
27 }
28 
29 //------------------------------------------------------------------------
30 
32 {
33  out << m_bbox.min() << m_bbox.max() << m_nodes << m_triWoop << m_triIndex;
34 }
35 
36 //------------------------------------------------------------------------
37 
38 //CudaKDTree& CudaKDTree::operator=(CudaKDTree& other)
39 //{
40 // if (&other != this)
41 // {
42 // m_nodes = other.m_nodes;
43 // m_triWoop = other.m_triWoop;
44 // m_triIndex = other.m_triIndex;
45 // }
46 // return *this;
47 //}
48 
49 //------------------------------------------------------------------------
50 
51 void CudaKDTree::createWoopTri(const KDTree& kdtree)
52 {
53  const int triangleCount = kdtree.getScene()->getNumTriangles();
54 
55  Vec4f woop[3];
56  m_triWoop.resizeDiscard((triangleCount * 48 + 4096 - 1) & -4096);
57 
58  const Vec3i* triVtxIndex = (const Vec3i*)kdtree.getScene()->getTriVtxIndexBuffer().getPtr();
59  const Vec3f* vtxPos = (const Vec3f*)kdtree.getScene()->getVtxPosBuffer().getPtr();
60 
61  for (int i = 0; i < triangleCount; i++)
62  {
63  //const Vec3i& inds = triVtxIndex[kdtree.getTriIndices()[i]];
64  const Vec3i& inds = triVtxIndex[i];
65  const Vec3f& v0 = vtxPos[inds.x];
66  const Vec3f& v1 = vtxPos[inds.y];
67  const Vec3f& v2 = vtxPos[inds.z];
68 
69  Mat4f mtx;
70  mtx.setCol(0, Vec4f(v0 - v2, 0.0f));
71  mtx.setCol(1, Vec4f(v1 - v2, 0.0f));
72  mtx.setCol(2, Vec4f(cross(v0 - v2, v1 - v2), 0.0f));
73  mtx.setCol(3, Vec4f(v2, 1.0f));
74  mtx = invert(mtx);
75 
76  woop[0] = Vec4f(mtx(2,0), mtx(2,1), mtx(2,2), -mtx(2,3));
77  woop[1] = mtx.getRow(0);
78  woop[2] = mtx.getRow(1);
79 
80  memcpy(m_triWoop.getMutablePtr(i * 48), woop, 48);
81  }
82 }
83 
84 //------------------------------------------------------------------------
85 
86 void CudaKDTree::createNodeTriIdx(const KDTree& kdtree)
87 {
88  const Vec3i* tris = (const Vec3i*)kdtree.getScene()->getTriVtxIndexBuffer().getPtr(); // Necessary for bbox construction.
89  const Vec3f* verts = (const Vec3f*)kdtree.getScene()->getVtxPosBuffer().getPtr();
90 
91  Array<Vec4i> nodeData;
92  Array<int> triIndexData;
93 
94  const KDTreeNode* root = kdtree.getRoot();
95  const int nodeCount = root->getSubtreeSize(KDTREE_STAT_NODE_COUNT);
96 
97  nodeData.resize(nodeCount);
98 
99  int nextNodeIdx = 0;
100  int nextLeafIdx = 0;
101  Array<StackEntry> stack(StackEntry(root, nextNodeIdx++));
102 
103  while (stack.getSize())
104  {
105  StackEntry e = stack.removeLast(); // Pop stack. 'e' is always an inner node; leaves are not stored on the stack, they are processed immediately.
106 
107  const KDTreeNode* children[] = { e.node->getChildNode(0), e.node->getChildNode(1) };
108  StackEntry childrenEntries[2];
109 
110  for (int c = 0; c < 2; c++) // Process both child nodes.
111  {
112  if(children[c]->isLeaf()) // Write to triIdx array.
113  {
114  childrenEntries[c] = StackEntry(children[c], nextLeafIdx);
115 
116  const Array<S32>& tidx = kdtree.getTriIndices();
117  const KDTLeafNode* leaf = reinterpret_cast<const KDTLeafNode*>(childrenEntries[c].node);
118  // Write leaf's triangle indexes followed by 0x8000000 and then increment nextLeafIdx to point to next free space in triIdx array.
119  for(int i = leaf->m_lo; i < leaf->m_hi; i++)
120  {
121  triIndexData.add(tidx.get(i));
122  for (int j = 0; j < 3; j++)
123  {
124  m_bbox.grow(verts[tris[tidx.get(i)][j]]);
125  }
126  }
127 
128  if (leaf->getNumTriangles() == 0)
129  {
130  childrenEntries[c].idx = ~KDTREE_EMPTYLEAF;
131  }
132  else
133  {
134  triIndexData.add(KDTREE_EMPTYLEAF);
135  nextLeafIdx += leaf->m_hi - leaf->m_lo + 1;
136  }
137  }
138  else
139  childrenEntries[c] = stack.add(StackEntry(children[c], nextNodeIdx++)); // Add node on top of the node stack.
140  }
141 
142  const KDTInnerNode* node = reinterpret_cast<const KDTInnerNode*>(e.node);
143 
144  S32 leftOffset = childrenEntries[0].encodeIdx();
145  S32 rightOffset = childrenEntries[1].encodeIdx();
146  F32 splitPos = node->m_pos;
147  S32 flags = node->m_axis << 28;
148  flags = flags & KDTREE_MASK;
149 
150  Vec4i data(leftOffset, rightOffset, floatToBits(splitPos), flags);
151  nodeData.set(e.idx, data);
152  }
153 
154  m_nodes.resizeDiscard(nodeData.getNumBytes());
155  m_nodes.set(nodeData.getPtr(), nodeData.getNumBytes());
156 
157  m_triIndex.resizeDiscard(triIndexData.getNumBytes());
158  m_triIndex.set(triIndexData.getPtr(), triIndexData.getNumBytes());
159 }
U32 floatToBits(F32 a)
Definition: Math.hpp:95
F32 m_pos
Split position.
Definition: KDTreeNode.hpp:127
void set(const void *ptr)
Definition: Buffer.hpp:92
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
Definition: DLLImports.inl:353
Buffer & getTriVtxIndexBuffer(void)
Returns buffer of triangle's vertex indieces.
Definition: Scene.hpp:75
KDTreeNode * getRoot(void) const
Gets root node of the k-d tree.
Definition: KDTree.hpp:131
S32 m_hi
Higher index to the tree's triangle references array.
Definition: KDTreeNode.hpp:170
FW_CUDA_FUNC const Vec3f & max(void) const
Definition: Util.hpp:49
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 * data
Definition: DLLImports.inl:319
Buffer & getVtxPosBuffer(void)
Returns vertex position buffer.
Definition: Scene.hpp:103
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
Definition: DLLImports.inl:355
K-d tree acceleration structure class.
Definition: KDTree.hpp:41
int getSubtreeSize(KDTREE_STAT stat=KDTREE_STAT_NODE_COUNT) const
Computes given statistics about node's subtree.
Definition: KDTreeNode.cpp:43
S32 m_lo
Lower index to the tree's triangle references array.
Definition: KDTreeNode.hpp:169
const U8 * getPtr(S64 ofs=0)
Definition: Buffer.hpp:106
S32 getNumBytes(void) const
#define KDTREE_EMPTYLEAF
CudaKDTree(const KDTree &kdtree)
Constructor. [in] kdtree KDTree to convert.
Definition: CudaKDTree.cpp:8
S32 getNumTriangles() const
Returns number of triangles this node references.
Definition: KDTreeNode.hpp:167
const T & get(S idx) const
Definition: Array.hpp:232
float F32
Definition: Defs.hpp:89
Scene * getScene(void) const
Gets source scene of the k-d tree.
Definition: KDTree.hpp:119
void serialize(OutputStream &out)
Writes CudaKDTree to a given output stream.
Definition: CudaKDTree.cpp:31
FW_CUDA_FUNC S invert(const MatrixBase< T, L, S > &v)
Definition: Math.hpp:784
K-d tree's leaf node class.
Definition: KDTreeNode.hpp:134
U8 * getMutablePtr(S64 ofs=0)
Definition: Buffer.hpp:110
FW_CUDA_FUNC T min(const VectorBase< T, L, S > &v)
Definition: Math.hpp:461
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Definition: Math.hpp:462
int getNumTriangles(void) const
Definition: Scene.hpp:61
K-d tree's inner node class.
Definition: KDTreeNode.hpp:94
signed int S32
Definition: Defs.hpp:88
~CudaKDTree(void)
Destructor.
Definition: CudaKDTree.cpp:25
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 v0
Definition: DLLImports.inl:353
T & add(void)
Definition: Array.hpp:384
FW_CUDA_FUNC const Vec3f & min(void) const
Definition: Util.hpp:48
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
FW_CUDA_FUNC Vector< T, L > getRow(int r) const
Definition: Math.hpp:881
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
FW_CUDA_FUNC F32 cross(const Vec2f &a, const Vec2f &b)
Definition: Math.hpp:481
FW_CUDA_FUNC void grow(const Vec3f &pt)
Definition: Util.hpp:41
Definitions for Cuda KDTree.
S32 m_axis
Split dimension.
Definition: KDTreeNode.hpp:128
virtual KDTreeNode * getChildNode(S32 i) const =0
Returns node's child node (left or right).
T set(S32idx, const T &item)
#define KDTREE_MASK
const T * getPtr(S32idx=0) const
void resizeDiscard(S64 size)
Definition: Buffer.hpp:83
FW_CUDA_FUNC void setCol(int c, const VectorBase< T, L, V > &v)
Definition: Math.hpp:576
K-d tree virtual parent node class.
Definition: KDTreeNode.hpp:51