RavEngine
Loading...
Searching...
No Matches
GuBV4.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_BV4_H
30#define GU_BV4_H
31
32#include "foundation/PxBounds3.h"
33#include "GuBV4Settings.h"
34#include "GuCenterExtents.h"
35#include "GuTriangle.h"
36#include "foundation/PxVecMath.h"
37#include "common/PxPhysXCommonConfig.h"
38
39#define V4LoadU_Safe physx::aos::V4LoadU // PT: prefix needed on Linux. Sigh.
40#define V4LoadA_Safe V4LoadA
41#define V4StoreA_Safe V4StoreA
42#define V4StoreU_Safe V4StoreU
43
44namespace physx
45{
46 class PxSerializationContext;
47 class PxDeserializationContext;
48
49namespace Gu
50{
52 {
53 const PxVec3* Vertex[3];
54 };
55
57 {
58 const PxVec3* Vertex[4];
59 };
60
61 // PT: TODO: make this more generic, rename to IndQuad32, refactor with GRB's int4
63 {
64 public:
65 public:
67 PX_FORCE_INLINE IndTetrahedron32(PxU32 r0, PxU32 r1, PxU32 r2, PxU32 r3) { mRef[0] = r0; mRef[1] = r1; mRef[2] = r2; mRef[3] = r3; }
69 {
70 mRef[0] = tetrahedron.mRef[0];
71 mRef[1] = tetrahedron.mRef[1];
72 mRef[2] = tetrahedron.mRef[2];
73 mRef[3] = tetrahedron.mRef[3];
74 }
76 PxU32 mRef[4];
77 };
79
80 // PT: TODO: make this more generic, rename to IndQuad16
82 {
83 public:
84 public:
86 PX_FORCE_INLINE IndTetrahedron16(PxU16 r0, PxU16 r1, PxU16 r2, PxU16 r3) { mRef[0] = r0; mRef[1] = r1; mRef[2] = r2; mRef[3] = r3; }
88 {
89 mRef[0] = tetrahedron.mRef[0];
90 mRef[1] = tetrahedron.mRef[1];
91 mRef[2] = tetrahedron.mRef[2];
92 mRef[3] = tetrahedron.mRef[3];
93 }
95 PxU16 mRef[4];
96 };
98
101
102 PX_FORCE_INLINE void getVertexReferences(PxU32& vref0, PxU32& vref1, PxU32& vref2, PxU32 index, const IndTri32* T32, const IndTri16* T16)
103 {
104 if(T32)
105 {
106 const IndTri32* PX_RESTRICT tri = T32 + index;
107 vref0 = tri->mRef[0];
108 vref1 = tri->mRef[1];
109 vref2 = tri->mRef[2];
110 }
111 else
112 {
113 const IndTri16* PX_RESTRICT tri = T16 + index;
114 vref0 = tri->mRef[0];
115 vref1 = tri->mRef[1];
116 vref2 = tri->mRef[2];
117 }
118 }
119
120 PX_FORCE_INLINE void getVertexReferences(PxU32& vref0, PxU32& vref1, PxU32& vref2, PxU32& vref3, PxU32 index, const IndTetrahedron32* T32, const IndTetrahedron16* T16)
121 {
122 if(T32)
123 {
124 const IndTetrahedron32* PX_RESTRICT tet = T32 + index;
125 vref0 = tet->mRef[0];
126 vref1 = tet->mRef[1];
127 vref2 = tet->mRef[2];
128 vref3 = tet->mRef[3];
129 }
130 else
131 {
132 const IndTetrahedron16* PX_RESTRICT tet = T16 + index;
133 vref0 = tet->mRef[0];
134 vref1 = tet->mRef[1];
135 vref2 = tet->mRef[2];
136 vref3 = tet->mRef[3];
137 }
138 }
139
141 {
142 public:
143 enum MeshType
144 {
145 TRI_MESH,
146 TET_MESH,
147 FORCE_DWORD = 0x7fffffff
148 };
149 SourceMeshBase(MeshType meshType);
150 virtual ~SourceMeshBase();
151
152 SourceMeshBase(const PxEMPTY) {}
153 static void getBinaryMetaData(PxOutputStream& stream);
154
155 PxU32 mNbVerts;
156 const PxVec3* mVerts;
157
158 PX_FORCE_INLINE PxU32 getNbVertices() const { return mNbVerts; }
159 PX_FORCE_INLINE const PxVec3* getVerts() const { return mVerts; }
160
161 PX_FORCE_INLINE void setNbVertices(PxU32 nb) { mNbVerts = nb; }
162
163 PX_FORCE_INLINE void initRemap() { mRemap = NULL; }
164 PX_FORCE_INLINE const PxU32* getRemap() const { return mRemap; }
165 PX_FORCE_INLINE void releaseRemap() { PX_FREE(mRemap); }
166
167 PX_FORCE_INLINE MeshType getMeshType() const { return mType; }
168
169 // PT: TODO: check whether adding these vcalls affected build & runtime performance
170 virtual PxU32 getNbPrimitives() const = 0;
171 virtual void remapTopology(const PxU32* order) = 0;
172 virtual void getPrimitiveBox(const PxU32 primitiveInd, physx::aos::Vec4V& minV, physx::aos::Vec4V& maxV) = 0;
173 virtual void refit(const PxU32 primitiveInd, PxBounds3& refitBox) = 0;
174
175 protected:
176 MeshType mType;
177 PxU32* mRemap;
178 };
179
181 {
182 public:
183 SourceMesh();
184 virtual ~SourceMesh();
185 // PX_SERIALIZATION
186 SourceMesh(const PxEMPTY) : SourceMeshBase(PxEmpty) {}
187 static void getBinaryMetaData(PxOutputStream& stream);
188 //~PX_SERIALIZATION
189
190 void reset();
191 void operator = (SourceMesh& v);
192
193 PxU32 mNbTris;
194 IndTri32* mTriangles32;
195 IndTri16* mTriangles16;
196
197 PX_FORCE_INLINE PxU32 getNbTriangles() const { return mNbTris; }
198 PX_FORCE_INLINE const IndTri32* getTris32() const { return mTriangles32; }
199 PX_FORCE_INLINE const IndTri16* getTris16() const { return mTriangles16; }
200
201 PX_FORCE_INLINE void setNbTriangles(PxU32 nb) { mNbTris = nb; }
202
203 // SourceMeshBase
204 virtual PxU32 getNbPrimitives() const { return getNbTriangles(); }
205 virtual void remapTopology(const PxU32* order);
206 virtual void getPrimitiveBox(const PxU32 primitiveInd, physx::aos::Vec4V& minV, physx::aos::Vec4V& maxV);
207 virtual void refit(const PxU32 primitiveInd, PxBounds3& refitBox);
208 //~SourceMeshBase
209
210 PX_FORCE_INLINE void setPointers(IndTri32* tris32, IndTri16* tris16, const PxVec3* verts)
211 {
212 mTriangles32 = tris32;
213 mTriangles16 = tris16;
214 mVerts = verts;
215 }
216
217 bool isValid() const;
218
219 PX_FORCE_INLINE void getTriangle(VertexPointers& vp, PxU32 index) const
220 {
221 PxU32 VRef0, VRef1, VRef2;
222 getVertexReferences(VRef0, VRef1, VRef2, index, mTriangles32, mTriangles16);
223 vp.Vertex[0] = mVerts + VRef0;
224 vp.Vertex[1] = mVerts + VRef1;
225 vp.Vertex[2] = mVerts + VRef2;
226 }
227 };
228
230 {
231 public:
233 virtual ~TetrahedronSourceMesh();
234 // PX_SERIALIZATION
235 TetrahedronSourceMesh(const PxEMPTY) : SourceMeshBase(TET_MESH) {}
236 static void getBinaryMetaData(PxOutputStream& stream);
237 //~PX_SERIALIZATION
238
239 void reset();
240 void operator = (TetrahedronSourceMesh& v);
241
242 PxU32 mNbTetrahedrons;
243 IndTetrahedron32* mTetrahedrons32;
244 IndTetrahedron16* mTetrahedrons16;
245
246 PX_FORCE_INLINE PxU32 getNbTetrahedrons() const { return mNbTetrahedrons; }
247 PX_FORCE_INLINE const IndTetrahedron32* getTetrahedrons32() const { return mTetrahedrons32; }
248 PX_FORCE_INLINE const IndTetrahedron16* getTetrahedrons16() const { return mTetrahedrons16; }
249
250 PX_FORCE_INLINE void setNbTetrahedrons(PxU32 nb) { mNbTetrahedrons = nb; }
251
252 // SourceMeshBase
253 virtual PxU32 getNbPrimitives() const { return getNbTetrahedrons(); }
254 virtual void remapTopology(const PxU32* order);
255 virtual void getPrimitiveBox(const PxU32 primitiveInd, physx::aos::Vec4V& minV, physx::aos::Vec4V& maxV);
256 virtual void refit(const PxU32 primitiveInd, PxBounds3& refitBox);
257 //~SourceMeshBase
258
259 PX_FORCE_INLINE void setPointers(IndTetrahedron32* tets32, IndTetrahedron16* tets16, const PxVec3* verts)
260 {
261 mTetrahedrons32 = tets32;
262 mTetrahedrons16 = tets16;
263 mVerts = verts;
264 }
265
266 bool isValid() const;
267
268 PX_FORCE_INLINE void getTetrahedron(TetrahedronPointers& vp, PxU32 index) const
269 {
270 PxU32 VRef0, VRef1, VRef2, VRef3;
271 getVertexReferences(VRef0, VRef1, VRef2, VRef3, index, mTetrahedrons32, mTetrahedrons16);
272 vp.Vertex[0] = mVerts + VRef0;
273 vp.Vertex[1] = mVerts + VRef1;
274 vp.Vertex[2] = mVerts + VRef2;
275 vp.Vertex[3] = mVerts + VRef3;
276 }
277 };
278
280 {
281 // PX_SERIALIZATION
282 LocalBounds(const PxEMPTY) {}
283 //~PX_SERIALIZATION
284 LocalBounds() : mCenter(PxVec3(0.0f)), mExtentsMagnitude(0.0f) {}
285
286 PxVec3 mCenter;
287 float mExtentsMagnitude;
288
289 PX_FORCE_INLINE void init(const PxBounds3& bounds)
290 {
291 mCenter = bounds.getCenter();
292 // PT: TODO: compute mag first, then multiplies by 0.5f (TA34704)
293 mExtentsMagnitude = bounds.getExtents().magnitude();
294 }
295 };
296
298 {
299 public:
300
301 struct Data
302 {
303 PxU16 mExtents;
304 PxI16 mCenter;
305 };
306 Data mData[3];
307 };
309
311
312 #define GU_BV4_CHILD_OFFSET_SHIFT_COUNT 11
313 static PX_FORCE_INLINE PxU32 getChildOffset(PxU32 data) { return data>>GU_BV4_CHILD_OFFSET_SHIFT_COUNT; }
314 static PX_FORCE_INLINE PxU32 getChildType(PxU32 data) { return (data>>1)&3; }
315
316 template<class BoxType>
318 {
319 BoxType mAABB;
320 PxU32 mData;
321
322 PX_FORCE_INLINE PxU32 isLeaf() const { return mData&1; }
323 PX_FORCE_INLINE PxU32 getPrimitive() const { return mData>>1; }
324 PX_FORCE_INLINE PxU32 getChildOffset() const { return mData>>GU_BV4_CHILD_OFFSET_SHIFT_COUNT;}
325 PX_FORCE_INLINE PxU32 getChildType() const { return (mData>>1)&3; }
326 PX_FORCE_INLINE PxU32 getChildData() const { return mData; }
327
328 PX_FORCE_INLINE void encodePNS(PxU32 code)
329 {
330 PX_ASSERT(code<256);
331 mData |= code<<3;
332 }
333 PX_FORCE_INLINE PxU32 decodePNSNoShift() const { return mData; }
334 };
335
338
339 // PT: TODO: align class to 16? (TA34704)
341 {
342 public:
343 // PX_SERIALIZATION
344 BV4Tree(const PxEMPTY);
345 void exportExtraData(PxSerializationContext&);
346 void importExtraData(PxDeserializationContext& context);
347 static void getBinaryMetaData(PxOutputStream& stream);
348 //~PX_SERIALIZATION
349 BV4Tree();
350 BV4Tree(SourceMesh* meshInterface, const PxBounds3& localBounds);
351 ~BV4Tree();
352
353 bool refit(PxBounds3& globalBounds, float epsilon);
354
355 bool load(PxInputStream& stream, bool mismatch);
356
357 void reset();
358 void operator = (BV4Tree& v);
359
360 bool init(SourceMeshBase* meshInterface, const PxBounds3& localBounds);
361 void release();
362
363 SourceMeshBase* mMeshInterface;
364
365 LocalBounds mLocalBounds;
366
367 PxU32 mNbNodes;
368 void* mNodes; // PT: BVDataPacked / BVDataSwizzled
369 PxU32 mInitData;
370 // PT: the dequantization coeffs are only used for quantized trees
371 PxVec3 mCenterOrMinCoeff; // PT: dequantization coeff, either for Center or Min (depending on AABB format)
372 PxVec3 mExtentsOrMaxCoeff; // PT: dequantization coeff, either for Extents or Max (depending on AABB format)
373 bool mUserAllocated; // PT: please keep these 4 bytes right after mCenterOrMinCoeff/mExtentsOrMaxCoeff for safe V4 loading
374 bool mQuantized; // PT: true for quantized trees
375 bool mIsEdgeSet; // PT: equivalent to RTree::IS_EDGE_SET
376 bool mPadding;
377 };
378
379} // namespace Gu
380}
381
382#endif // GU_BV4_H
Definition base.h:1940
Definition GuBV4.h:341
Definition GuBV4.h:82
Definition GuBV4.h:63
Definition GuBV4.h:298
Definition GuBV4.h:141
Definition GuBV4.h:181
Definition GuBV4.h:230
Class representing 3D range or axis aligned bounding box.
Definition PxBounds3.h:58
PX_CUDA_CALLABLE PX_FORCE_INLINE PxVec3 getCenter() const
returns the center of this axis aligned box.
Definition PxBounds3.h:396
PX_CUDA_CALLABLE PX_FORCE_INLINE float getExtents(uint32_t axis) const
get component of the box's extents along a given axis
Definition PxBounds3.h:408
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
#define PX_RESTRICT
Definition PxPreprocessor.h:355
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_COMPILE_TIME_ASSERT(exp)
Definition PxPreprocessor.h:428
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 GuBV4.h:318
Definition GuTriangle.h:48
Definition GuBV4.h:280
Definition GuBV4.h:302
PxI16 mCenter
Quantized center.
Definition GuBV4.h:304
PxU16 mExtents
Quantized extents.
Definition GuBV4.h:303
Definition GuBV4.h:57
Definition GuBV4.h:52
Definition PxVecMathAoSScalar.h:65