1//
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions
4// are met:
5// * Redistributions of source code must retain the above copyright
6// notice, this list of conditions and the following disclaimer.
7// * Redistributions in binary form must reproduce the above copyright
8// notice, this list of conditions and the following disclaimer in the
9// documentation and/or other materials provided with the distribution.
10// * Neither the name of NVIDIA CORPORATION nor the names of its
11// contributors may be used to endorse or promote products derived
12// from this software without specific prior written permission.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
28// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
29
30#include "common/PxBase.h"
31#include "common/PxSerialFramework.h"
32#include "common/PxSerializer.h"
33#include "PxPhysicsSerialization.h"
34
35#include "GuHeightField.h"
36#include "GuConvexMesh.h"
37#include "GuTriangleMesh.h"
38#include "GuTriangleMeshBV4.h"
39#include "GuTriangleMeshRTree.h"
40#include "GuHeightFieldData.h"
41#include "SqPruningStructure.h"
42#include "NpRigidStatic.h"
43#include "NpRigidDynamic.h"
44#include "NpArticulation.h"
45#include "NpArticulationReducedCoordinate.h"
46#include "NpArticulationLink.h"
47#include "NpArticulationJoint.h"
48#include "NpMaterial.h"
49#include "NpAggregate.h"
50
51namespace physx
52{
53 using namespace physx::Gu;
54
55 template<>
56 void PxSerializerDefaultAdapter<NpMaterial>::registerReferences(PxBase& obj, PxSerializationContext& context) const
57 {
58 NpMaterial& t = static_cast<NpMaterial&>(obj);
59 context.registerReference(base&: obj, PX_SERIAL_REF_KIND_PXBASE, reference: size_t(&obj));
60 context.registerReference(base&: obj, PX_SERIAL_REF_KIND_MATERIAL_IDX, reference: size_t(t.getHandle()));
61 }
62
63 template<>
64 void PxSerializerDefaultAdapter<NpRigidDynamic>::registerReferences(PxBase& obj, PxSerializationContext& context) const
65 {
66 NpRigidDynamic& dynamic = static_cast<NpRigidDynamic&>(obj);
67
68 context.registerReference(base&: obj, PX_SERIAL_REF_KIND_PXBASE, reference: size_t(&obj));
69
70 struct RequiresCallback : public PxProcessPxBaseCallback
71 {
72 RequiresCallback(physx::PxSerializationContext& c) : context(c) {}
73 RequiresCallback& operator=(const RequiresCallback&) { PX_ASSERT(0); return *this; } //PX_NOCOPY doesn't work for local classes
74 void process(PxBase& base)
75 {
76 context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, reference: size_t(&base));
77 }
78 PxSerializationContext& context;
79 };
80
81 RequiresCallback callback(context);
82 dynamic.requiresObjects(c&: callback);
83 }
84
85 template<>
86 bool PxSerializerDefaultAdapter<NpArticulationLink>::isSubordinate() const
87 {
88 return true;
89 }
90
91 template<>
92 void PxSerializerDefaultAdapter<NpShape>::registerReferences(PxBase& obj, PxSerializationContext& context) const
93 {
94 NpShape& shape = static_cast<NpShape&>(obj);
95
96 context.registerReference(base&: obj, PX_SERIAL_REF_KIND_PXBASE, reference: size_t(&obj));
97
98 struct RequiresCallback : public PxProcessPxBaseCallback
99 {
100 RequiresCallback(physx::PxSerializationContext& c) : context(c) {}
101 RequiresCallback &operator=(const RequiresCallback&) { PX_ASSERT(0); return *this; } //PX_NOCOPY doesn't work for local classes
102 void process(PxBase& base)
103 {
104 PxMaterial* pxMaterial = base.is<PxMaterial>();
105 if (!pxMaterial)
106 {
107 context.registerReference(base, PX_SERIAL_REF_KIND_PXBASE, reference: size_t(&base));
108 }
109 else
110 {
111 //ideally we would move this part to ScShapeCore but we don't yet have a MaterialManager available there.
112 PxU16 index = static_cast<NpMaterial*>(pxMaterial)->getHandle();
113 context.registerReference(base, PX_SERIAL_REF_KIND_MATERIAL_IDX, reference: size_t(index));
114 }
115 }
116 PxSerializationContext& context;
117 };
118
119 RequiresCallback callback(context);
120 shape.requiresObjects(c&: callback);
121 }
122
123 template<>
124 bool PxSerializerDefaultAdapter<NpConstraint>::isSubordinate() const
125 {
126 return true;
127 }
128
129 template<>
130 bool PxSerializerDefaultAdapter<NpArticulationJoint>::isSubordinate() const
131 {
132 return true;
133 }
134
135}
136
137using namespace physx;
138
139void PxRegisterPhysicsSerializers(PxSerializationRegistry& sr)
140{
141 sr.registerSerializer(type: PxConcreteType::eCONVEX_MESH, PX_NEW_SERIALIZER_ADAPTER(ConvexMesh));
142 sr.registerSerializer(type: PxConcreteType::eTRIANGLE_MESH_BVH33, PX_NEW_SERIALIZER_ADAPTER(RTreeTriangleMesh));
143 sr.registerSerializer(type: PxConcreteType::eTRIANGLE_MESH_BVH34, PX_NEW_SERIALIZER_ADAPTER(BV4TriangleMesh));
144 sr.registerSerializer(type: PxConcreteType::eHEIGHTFIELD, PX_NEW_SERIALIZER_ADAPTER(HeightField));
145 sr.registerSerializer(type: PxConcreteType::eRIGID_DYNAMIC, PX_NEW_SERIALIZER_ADAPTER(NpRigidDynamic));
146 sr.registerSerializer(type: PxConcreteType::eRIGID_STATIC, PX_NEW_SERIALIZER_ADAPTER(NpRigidStatic));
147 sr.registerSerializer(type: PxConcreteType::eSHAPE, PX_NEW_SERIALIZER_ADAPTER(NpShape));
148 sr.registerSerializer(type: PxConcreteType::eMATERIAL, PX_NEW_SERIALIZER_ADAPTER(NpMaterial));
149 sr.registerSerializer(type: PxConcreteType::eCONSTRAINT, PX_NEW_SERIALIZER_ADAPTER(NpConstraint));
150 sr.registerSerializer(type: PxConcreteType::eAGGREGATE, PX_NEW_SERIALIZER_ADAPTER(NpAggregate));
151 sr.registerSerializer(type: PxConcreteType::eARTICULATION, PX_NEW_SERIALIZER_ADAPTER(NpArticulation));
152 sr.registerSerializer(type: PxConcreteType::eARTICULATION_REDUCED_COORDINATE, PX_NEW_SERIALIZER_ADAPTER(NpArticulationReducedCoordinate));
153 sr.registerSerializer(type: PxConcreteType::eARTICULATION_LINK, PX_NEW_SERIALIZER_ADAPTER(NpArticulationLink));
154 sr.registerSerializer(type: PxConcreteType::eARTICULATION_JOINT, PX_NEW_SERIALIZER_ADAPTER(NpArticulationJoint));
155 sr.registerSerializer(type: PxConcreteType::eARTICULATION_JOINT_REDUCED_COORDINATE, PX_NEW_SERIALIZER_ADAPTER(NpArticulationJointReducedCoordinate));
156 sr.registerSerializer(type: PxConcreteType::ePRUNING_STRUCTURE, PX_NEW_SERIALIZER_ADAPTER(Sq::PruningStructure));
157}
158
159
160void PxUnregisterPhysicsSerializers(PxSerializationRegistry& sr)
161{
162 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eCONVEX_MESH));
163 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eTRIANGLE_MESH_BVH33));
164 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eTRIANGLE_MESH_BVH34));
165 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eHEIGHTFIELD));
166 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eRIGID_DYNAMIC));
167 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eRIGID_STATIC));
168 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eSHAPE));
169 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eMATERIAL));
170 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eCONSTRAINT));
171 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eAGGREGATE));
172 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eARTICULATION));
173 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eARTICULATION_REDUCED_COORDINATE));
174 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eARTICULATION_LINK));
175 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eARTICULATION_JOINT));
176 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::eARTICULATION_JOINT_REDUCED_COORDINATE));
177 PX_DELETE_SERIALIZER_ADAPTER(sr.unregisterSerializer(PxConcreteType::ePRUNING_STRUCTURE));
178}
179

source code of qtquick3dphysics/src/3rdparty/PhysX/source/physx/src/NpSerializerAdapter.cpp