HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CE_BVH.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: CE_BVH.h ( CE Library, C++)
7  *
8  * COMMENTS: BVH class that runs on the GPU.
9  */
10 
11  #ifndef __CE_BVH__
12  #define __CE_BVH__
13 
14 #include "CE_Context.h"
15 #include "CE_Array.h"
16 #include "CE_BufferDevice.h"
17 
18 #include <UT/UT_UniquePtr.h>
19 #include <UT/UT_ValArray.h>
20 
21 namespace tinybvh
22 {
23  class BVH_GPU;
24 }
25 
27 {
28 public:
29  CE_BVH();
30  ~CE_BVH();
31 
32  enum class PrimType
33  {
34  TRI,
35  QUAD,
36  TET,
37  POINT
38  };
39 
40  void build(const CE_Int32Array &trisingrp,
41  const cl::Buffer &primptsidx,
42  const cl::Buffer &primpts,
43  const cl::Buffer &P,
44  PrimType primtype = PrimType::TRI,
45  bool refitTets = true);
46 
47  /// Convenience overload for point BVH — no prim connectivity needed.
48  void buildPoints(const CE_Int32Array &pts, const cl::Buffer &P);
49 
50  /// Build a valid but empty point BVH. All traversal queries return zero
51  /// results immediately (the root node's child AABBs are at infinity so
52  /// the distance check in BVH_TRAVERSE_BEGIN exceeds any finite threshold).
53  /// All four kernel buffers (nodes, verts, idx, pts) are non-null so they
54  /// can be bound to kernel arguments without error.
55  void buildEmpty();
56 
57  void refit(const CE_Int32Array &trisingrp,
58  const cl::Buffer &primptsidx,
59  const cl::Buffer &primpts,
60  const cl::Buffer &P);
61 
62  void clear();
63 
64  bool isBuilt() const;
65 
66  const cl::Buffer &nodeBuffer() const
67  { return myBVHNodes.buffer(); }
68 
69  const cl::Buffer &verticesBuffer() const
70  { return myVertices.buffer(); }
71 
72  const cl::Buffer &primIdxBuffer() const
73  { return myPrimIdx.buffer(); }
74 
75  // For point BVH only: the selected point indices (subset→full mapping).
76  // Empty for non-point BVH types. Passed to traverse_k_nearest_points
77  // so it can translate BVH subset indices to full geometry point indices.
78  const cl::Buffer &ptsBuffer() const
79  { return myPts.buffer(); }
80 
81 private:
82 
83  void updateVertices(const CE_Int32Array &tris,
84  const cl::Buffer &primptsidx,
85  const cl::Buffer &primpts,
86  const cl::Buffer &P);
87 
88  void buildDepthIndex(bool reverse = true);
89 
90  // These are copies of the tinybvh structs
91  // that we need to know the size of, but we
92  // don't want to include tinybvh.h in our
93  // include files.
94 #ifdef _MSC_VER
95  struct __declspec (align(16)) cebvhvec4
96 #else
97  struct __attribute__((aligned(16))) cebvhvec4
98 #endif
99  {
100  union { struct { float x, y, z, w; }; float cell[4]; };
101  };
102 
103  struct cebvhvec3
104  {
105  union { struct { float x, y, z; }; float cell[3]; };
106  };
107 
108  struct ceBVHNode
109  {
110  cebvhvec3 lmin; uint32_t left;
111  cebvhvec3 lmax; uint32_t right;
112  cebvhvec3 rmin; uint32_t triCount;
113  cebvhvec3 rmax; uint32_t firstTri; // total: 64 bytes
114  };
115 
117  CE_BufferDevice<ceBVHNode> myBVHNodes;
118  CE_BufferDevice<cebvhvec4> myVertices;
119  CE_UInt32Array myPrimIdx;
120  CE_Int32Array myPts; // point BVH only
121  UT_IntArray myDepthCounts;
122  CE_Int32Array mySingleDepthCounts;
123  CE_Int32Array mySingleDepthOffsets;
124  CE_Int32Array myDepthIndex;
125  cl::NDRange myRefitRange;
126  PrimType myPrimType;
127 };
128 
129 #endif
#define CE_API
Definition: CE_API.h:13
GLint left
Definition: glcorearb.h:2005
const cl::Buffer & nodeBuffer() const
Definition: CE_BVH.h:66
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLdouble right
Definition: glad.h:2817
PUGI__FN void reverse(I begin, I end)
Definition: pugixml.cpp:7458
GLint y
Definition: glcorearb.h:103
const cl::Buffer & ptsBuffer() const
Definition: CE_BVH.h:78
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
PrimType
Definition: CE_BVH.h:32
GLint GLenum GLint x
Definition: glcorearb.h:409
const cl::Buffer & primIdxBuffer() const
Definition: CE_BVH.h:72
Definition: CE_BVH.h:26
Memory buffer interface.
Definition: cl.hpp:1867
NDRange interface.
Definition: cl.hpp:2466
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
const cl::Buffer & verticesBuffer() const
Definition: CE_BVH.h:69