RavEngine
Loading...
Searching...
No Matches
GuBV32.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef GU_BV32_H
30#define GU_BV32_H
31
32#include "foundation/PxBounds3.h"
33#include "foundation/PxVec4.h"
34#include "common/PxSerialFramework.h"
35#include "foundation/PxUserAllocated.h"
36#include "foundation/PxArray.h"
37#include "GuBV4.h"
38
39namespace physx
40{
41 namespace Gu
42 {
44 {
45 PxVec3 mMin;
46 PxVec3 mMax;
47 PxU32 mNbLeafNodes;
48 PxU32 mDepth;
49 size_t mData;
50
51 PX_FORCE_INLINE BV32Data() : mNbLeafNodes(0), mDepth(0), mData(PX_INVALID_U32)
52 {
53 setEmpty();
54 }
55
56 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 isLeaf() const { return mData & 1; }
57
58 //if the node is leaf,
59 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getNbReferencedPrimitives() const { PX_ASSERT(isLeaf()); return PxU32((mData >>1)&63); }
60 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getPrimitiveStartIndex() const { PX_ASSERT(isLeaf()); return PxU32(mData >> 7); }
61
62 //PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getPrimitive() const { return mData >> 1; }
63 //if the node isn't leaf, we will get the childOffset
64 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getChildOffset() const { PX_ASSERT(!isLeaf()); return PxU32(mData >> GU_BV4_CHILD_OFFSET_SHIFT_COUNT); }
65 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getNbChildren() const { PX_ASSERT(!isLeaf()); return ((mData) & ((1 << GU_BV4_CHILD_OFFSET_SHIFT_COUNT) - 1))>>1; }
66
67 PX_CUDA_CALLABLE PX_FORCE_INLINE void getMinMax(PxVec3& min, PxVec3& max) const
68 {
69 //min = mCenter - mExtents;
70 //max = mCenter + mExtents;
71 min = mMin;
72 max = mMax;
73 }
74
75 PX_FORCE_INLINE void setEmpty()
76 {
77 //mCenter = PxVec3(0.0f, 0.0f, 0.0f);
78 //mExtents = PxVec3(-1.0f, -1.0f, -1.0f);
79
80 mMin = PxVec3(PX_MAX_F32);
81 mMax = PxVec3(-PX_MAX_F32);
82 }
83
84 };
85
86 PX_ALIGN_PREFIX(16)
88 {
89 /*PxVec4 mCenter[32];
90 PxVec4 mExtents[32];*/
91 PxVec4 mMin[32];
92 PxVec4 mMax[32];
93 PxU32 mData[32];
94 PxU32 mNbNodes;
95 PxU32 mDepth;
96 PxU32 padding[2];
97
98 PX_CUDA_CALLABLE PX_FORCE_INLINE BV32DataPacked() : mNbNodes(0), mDepth(0)
99 {
100 }
101
102 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 isLeaf(const PxU32 index) const { return mData[index] & 1; }
103 //if the node is leaf
104 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getNbReferencedPrimitives(const PxU32 index) const { PX_ASSERT(isLeaf(index)); return (mData[index] >> 1) & 63; }
105 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getPrimitiveStartIndex(const PxU32 index) const { PX_ASSERT(isLeaf(index)); return (mData[index] >> 7); }
106 //if the node isn't leaf, we will get the childOffset
107 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getChildOffset(const PxU32 index) const { PX_ASSERT(!isLeaf(index)); return mData[index] >> GU_BV4_CHILD_OFFSET_SHIFT_COUNT; }
108 PX_CUDA_CALLABLE PX_FORCE_INLINE PxU32 getNbChildren(const PxU32 index) const { PX_ASSERT(!isLeaf(index)); return ((mData[index])& ((1 << GU_BV4_CHILD_OFFSET_SHIFT_COUNT) - 1)) >> 1; }
109 }
110 PX_ALIGN_SUFFIX(16);
111
112 //This struct store the start and end index of the packed node at the same depth level in the tree
114 {
115 public:
116 PxU32 offset;
117 PxU32 count;
118 };
119
121 {
122 public:
123 // PX_SERIALIZATION
124 BV32Tree(const PxEMPTY);
125 void exportExtraData(PxSerializationContext&);
126 void importExtraData(PxDeserializationContext& context);
127 static void getBinaryMetaData(PxOutputStream& stream);
128 //~PX_SERIALIZATION
129
130 BV32Tree();
131 BV32Tree(SourceMesh* meshInterface, const PxBounds3& localBounds);
132 ~BV32Tree();
133
134 bool refit(const float epsilon);
135
136 bool load(PxInputStream& stream, bool mismatch);
137
138 void calculateLeafNode(BV32Data& node);
139 void createSOAformatNode(BV32DataPacked& packedData, const BV32Data& node, const PxU32 childOffset, PxU32& currentIndex, PxU32& nbPackedNodes);
140
141 void reset();
142 void operator = (BV32Tree& v);
143
144 bool init(SourceMeshBase* meshInterface, const PxBounds3& localBounds);
145 void release();
146
147 SourceMeshBase* mMeshInterface;
148 LocalBounds mLocalBounds;
149
150 PxU32 mNbNodes;
151 BV32Data* mNodes;
152 BV32DataPacked* mPackedNodes;
153 PxU32 mNbPackedNodes;
154 PxU32* mRemapPackedNodeIndexWithDepth;
155 BV32DataDepthInfo* mTreeDepthInfo;
156 PxU32 mMaxTreeDepth;
157 PxU32 mInitData;
158 bool mUserAllocated; // PT: please keep these 4 bytes right after mCenterOrMinCoeff/mExtentsOrMaxCoeff for safe V4 loading
159 bool mPadding[2];
160 };
161
162 } // namespace Gu
163}
164
165#endif // GU_BV32_H
Definition base.h:1940
Definition GuBV32.h:121
Definition GuBV4.h:141
Definition GuBV4.h:181
Class representing 3D range or axis aligned bounding box.
Definition PxBounds3.h:58
Binary deserialization context class.
Definition PxSerialFramework.h:174
Input stream class for I/O.
Definition PxIO.h:50
Output stream class for I/O.
Definition PxIO.h:114
Binary serialization context class.
Definition PxSerialFramework.h:99
Definition PxUserAllocated.h:43
3 Element vector class.
Definition PxVec3.h:50
Definition PxVec4.h:50
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PxEMPTY
Definition Px.h:87
Definition GuBV32.h:114
Definition GuBV32.h:88
Definition GuBV32.h:44
Definition GuBV4.h:280