37 m_platform (kdtree.getPlatform()),
52 m_refStack.
resize(rootSpec.numRef);
54 for (
int i = 0; i < rootSpec.numRef; i++)
56 m_refStack[i].triIdx = i;
57 for (
int j = 0; j < 3; j++)
58 m_refStack[i].bounds.grow(verts[tris[i][j]]);
59 rootSpec.bounds.grow(m_refStack[i].bounds);
62 m_progressTimer.
start();
67 std::printf(
"\rNaiveKDTreeBuilder: progress %.0f%%\n", 100.0
f);
73 KDTreeNode* NaiveKDTreeBuilder::buildNode(NodeSpec spec,
int level,
F32 progressStart,
F32 progressEnd)
77 std::printf(
"NaiveKDTreeBuilder: progress %.0f%%\r", progressStart * 100.0
f);
78 m_progressTimer.
start();
81 if(spec.numRef <= m_platform.
getMaxLeafSize() || level >= MaxDepth)
82 return createLeaf(spec);
86 Split split = findSplit(spec, level);
87 performSplit(left, right, spec, split);
89 F32 progressMid =
lerp(progressStart, progressEnd, (
F32)right.numRef / (
F32)(left.numRef + right.numRef));
90 KDTreeNode* rightNode = buildNode(right, level + 1, progressStart, progressMid);
91 KDTreeNode* leftNode = buildNode(left, level + 1, progressMid, progressEnd);
92 return new KDTInnerNode(split.pos, split.dim, leftNode, rightNode);
96 KDTreeNode* NaiveKDTreeBuilder::createLeaf(
const NodeSpec& spec)
99 for (
int i = 0; i < spec.numRef; i++)
101 return new KDTLeafNode(tris.getSize() - spec.numRef, tris.getSize());
105 NaiveKDTreeBuilder::Split NaiveKDTreeBuilder::findSplit (
const NodeSpec& spec,
S32 level)
107 Array<Reference>& refs = m_refStack;
114 F32 boundsMin = spec.bounds.min()[
dim];
115 F32 boundsMax = spec.bounds.max()[
dim];
117 splitPos = (boundsMin + boundsMax) / 2;
122 sort(
this, refs.getSize() - spec.numRef, refs.getSize(), sortCompare, sortSwap);
124 S32 medIdx = refs.getSize() - (int)(spec.numRef / 2);
125 splitPos = refs.get(medIdx).bounds.min()[
dim];
131 foundSplit.dim =
dim;
132 foundSplit.pos = splitPos;
138 void NaiveKDTreeBuilder::performSplit (NodeSpec& left, NodeSpec& right,
const NodeSpec& spec,
const Split& split)
140 Array<Reference>& refs = m_refStack;
141 int leftStart = refs.
getSize() - spec.numRef;
142 int leftEnd = leftStart;
143 int rightStart = refs.getSize();
145 for (
int i = leftEnd; i < rightStart; i++)
147 if (refs[i].bounds.max()[split.dim] <= split.pos)
149 swap(refs[i], refs[leftEnd++]);
152 else if (refs[i].bounds.min()[split.dim] >= split.pos)
154 swap(refs[i--], refs[--rightStart]);
160 for (
int i = leftEnd; i < rightStart; i++)
162 refs.add(Reference(refs.get(i)));
168 left.numRef = leftEnd - leftStart;
169 right.numRef = refs.getSize() - rightStart;
171 Vec3f leftCut = spec.bounds.max();
172 leftCut[split.dim] = split.pos;
174 Vec3f rightCut = spec.bounds.min();
175 rightCut[split.dim] = split.pos;
177 left.bounds = AABB(spec.bounds.min(), leftCut);
178 right.bounds = AABB(rightCut, spec.bounds.max());
182 bool NaiveKDTreeBuilder::sortCompare(
void*
data,
int idxA,
int idxB)
185 int dim = ptr->m_sortDim;
186 const Reference& ra = ptr->m_refStack[idxA];
187 const Reference& rb = ptr->m_refStack[idxB];
188 F32 ca = ra.bounds.min()[
dim] + ra.bounds.max()[
dim];
189 F32 cb = rb.bounds.min()[
dim] + rb.bounds.max()[
dim];
190 return (ca < cb || (ca == cb && ra.triIdx < rb.triIdx));
193 void NaiveKDTreeBuilder::sortSwap(
void* data,
int idxA,
int idxB)
196 swap(ptr->m_refStack[idxA], ptr->m_refStack[idxB]);
Buffer & getTriVtxIndexBuffer(void)
Returns buffer of triangle's vertex indieces.
KDTreeNode * run(void)
Builds k-d tree.
void sort(void *data, int start, int end, SortCompareFunc compareFunc, SortSwapFunc swapFunc, bool multicore=false)
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
Buffer & getVtxPosBuffer(void)
Returns vertex position buffer.
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
Strucure holding build parameters.
K-d tree acceleration structure class.
const U8 * getPtr(S64 ofs=0)
NaiveKDTreeBuilder(KDTree &kdtree, const KDTree::BuildParams ¶ms)
Constructor.
Scene * getScene(void) const
Gets source scene of the k-d tree.
int getNumTriangles(void) const
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 dim
Array< S32 > & getTriIndices(void)
Returns an array of triangle indices to which leaf nodes are pointig. These indices point to scene's ...
BuilderType builder
Defines which builder type will be used to build the k-d tree.
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
void printf(const char *fmt,...)
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
FW_CUDA_FUNC void swap(T &a, T &b)
bool enablePrints
Flag whether to print information during build phase.
K-d tree virtual parent node class.
FW_CUDA_FUNC A lerp(const A &a, const A &b, const B &t)