RavEngine
Loading...
Searching...
No Matches
SnSerializationContext.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 SN_SERIALIZATION_CONTEXT_H
30#define SN_SERIALIZATION_CONTEXT_H
31
32#include "foundation/PxAssert.h"
33#include "foundation/PxMemory.h"
34#include "foundation/PxHash.h"
35#include "common/PxSerialFramework.h"
36#include "extensions/PxDefaultStreams.h"
37
38#include "foundation/PxUserAllocated.h"
39#include "CmCollection.h"
40#include "CmUtils.h"
41#include "SnConvX_Align.h"
42
43namespace physx
44{
45 namespace Sn
46 {
47
49 {
50 //= ATTENTION! =====================================================================================
51 // Changing the data layout of this class breaks the binary serialization format. See comments for
52 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
53 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
54 // accordingly.
55 //==================================================================================================
56
57 PX_FORCE_INLINE ManifestEntry(PxU32 _offset, PxType _type)
58 {
60 offset = _offset;
61 type = _type;
62 }
64 PX_FORCE_INLINE void operator =(const ManifestEntry& m)
65 {
66 PxMemCopy(this, &m, sizeof(ManifestEntry));
67 }
68
69 PxU32 offset;
70 PxType type;
71 };
72
74 {
75 //= ATTENTION! =====================================================================================
76 // Changing the data layout of this class breaks the binary serialization format. See comments for
77 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
78 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
79 // accordingly.
80 //==================================================================================================
81
83 {
85 id = _id;
86 type = _type;
87 }
89 PX_FORCE_INLINE void operator =(const ImportReference& m)
90 {
91 PxMemCopy(this, &m, sizeof(ImportReference));
92 }
94 PxType type;
95 };
96
97#define SERIAL_OBJECT_INDEX_TYPE_BIT (1u<<31)
99 {
100 //= ATTENTION! =====================================================================================
101 // Changing the data layout of this class breaks the binary serialization format. See comments for
102 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
103 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
104 // accordingly.
105 //==================================================================================================
106
107 PX_FORCE_INLINE SerialObjectIndex(PxU32 index, bool external) { setIndex(index, external); }
108 PX_FORCE_INLINE SerialObjectIndex(const SerialObjectIndex& objIndex) : mObjIndex(objIndex.mObjIndex) {}
109 PX_FORCE_INLINE SerialObjectIndex() : mObjIndex(PX_INVALID_U32) {}
110
111 PX_FORCE_INLINE void setIndex(PxU32 index, bool external)
112 {
113 PX_ASSERT((index & SERIAL_OBJECT_INDEX_TYPE_BIT) == 0);
114 mObjIndex = index | (external ? SERIAL_OBJECT_INDEX_TYPE_BIT : 0);
115 }
116
117 PX_FORCE_INLINE PxU32 getIndex(bool& isExternal)
118 {
119 PX_ASSERT(mObjIndex != PX_INVALID_U32);
120 isExternal = (mObjIndex & SERIAL_OBJECT_INDEX_TYPE_BIT) > 0;
121 return mObjIndex & ~SERIAL_OBJECT_INDEX_TYPE_BIT;
122 }
123
124 PX_FORCE_INLINE bool operator < (const SerialObjectIndex& so) const
125 {
126 return mObjIndex < so.mObjIndex;
127 }
128
129 private:
130 PxU32 mObjIndex;
131 };
132
134 {
135 //= ATTENTION! =====================================================================================
136 // Changing the data layout of this class breaks the binary serialization format. See comments for
137 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
138 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
139 // accordingly.
140 //==================================================================================================
141
143 {
145 id = _id;
146 objIndex = _objIndex;
147 }
149 PX_FORCE_INLINE void operator =(const ExportReference& m)
150 {
151 PxMemCopy(this, &m, sizeof(ExportReference));
152 }
154 SerialObjectIndex objIndex;
155 };
156
158 {
159 //= ATTENTION! =====================================================================================
160 // Changing the data layout of this class breaks the binary serialization format. See comments for
161 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
162 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
163 // accordingly.
164 //==================================================================================================
165
167
168 PX_FORCE_INLINE InternalReferencePtr(size_t _reference, SerialObjectIndex _objIndex) :
169 reference(_reference),
170 objIndex(_objIndex)
171#if PX_P64_FAMILY
172 ,pad(PX_PADDING_32)
173#endif
174 {
175 }
176
177 size_t reference;
178 SerialObjectIndex objIndex;
179#if PX_P64_FAMILY
180 PxU32 pad;
181#endif
182 };
183
185 {
186 //= ATTENTION! =====================================================================================
187 // Changing the data layout of this class breaks the binary serialization format. See comments for
188 // PX_BINARY_SERIAL_VERSION. If a modification is required, please adjust the getBinaryMetaData
189 // function. If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
190 // accordingly.
191 //==================================================================================================
192
194
195 PX_FORCE_INLINE InternalReferenceHandle16(PxU16 _reference, SerialObjectIndex _objIndex) :
196 reference(_reference),
197 pad(PX_PADDING_16),
198 objIndex(_objIndex)
199 {
200 }
201
202 PxU16 reference;
203 PxU16 pad;
204 SerialObjectIndex objIndex;
205 };
206
209
211 {
212 PX_NOCOPY(DeserializationContext)
213
214 public:
215 DeserializationContext(const ManifestEntry* manifestTable,
216 const ImportReference* importReferences,
217 PxU8* objectDataAddress,
218 const InternalPtrRefMap& internalPtrReferencesMap,
219 const InternalHandle16RefMap& internalHandle16ReferencesMap,
220 const Cm::Collection* externalRefs,
221 PxU8* extraData)
222 : mManifestTable(manifestTable)
223 , mImportReferences(importReferences)
224 , mObjectDataAddress(objectDataAddress)
225 , mInternalPtrReferencesMap(internalPtrReferencesMap)
226 , mInternalHandle16ReferencesMap(internalHandle16ReferencesMap)
227 , mExternalRefs(externalRefs)
228 {
229 mExtraDataAddress = extraData;
230 }
231
232 virtual PxBase* resolveReference(PxU32 kind, size_t reference) const;
233
234 private:
235 //various pointers to deserialized data
236 const ManifestEntry* mManifestTable;
237 const ImportReference* mImportReferences;
238 PxU8* mObjectDataAddress;
239
240 //internal references maps for resolving references.
241 const InternalPtrRefMap& mInternalPtrReferencesMap;
242 const InternalHandle16RefMap& mInternalHandle16ReferencesMap;
243
244 //external collection for resolving import references.
245 const Cm::Collection* mExternalRefs;
246 //const PxU32 mPhysXVersion;
247 };
248
250 {
251 PX_NOCOPY(SerializationContext)
252 public:
253 SerializationContext(const Cm::Collection& collection, const Cm::Collection* externalRefs)
254 : mCollection(collection)
255 , mExternalRefs(externalRefs)
256 {
257 // fill object to collection index map (same ordering as manifest)
258 for (PxU32 i=0;i<mCollection.internalGetNbObjects();i++)
259 {
260 mObjToCollectionIndexMap[mCollection.internalGetObject(i)] = i;
261 }
262 }
263
264 virtual void writeData(const void* buffer, PxU32 size) { mMemStream.write(buffer, size); }
265 virtual PxU32 getTotalStoredSize() { return mMemStream.getSize(); }
266 virtual void alignData(PxU32 alignment = PX_SERIAL_ALIGN)
267 {
268 if(!alignment)
269 return;
270
271 PxI32 bytesToPad = PxI32(getPadding(mMemStream.getSize(), alignment));
272 static const PxI32 BUFSIZE = 64;
273 char buf[BUFSIZE];
274 PxMemSet(buf, 0, bytesToPad < BUFSIZE ? PxU32(bytesToPad) : PxU32(BUFSIZE));
275 while(bytesToPad > 0)
276 {
277 mMemStream.write(buf, bytesToPad < BUFSIZE ? PxU32(bytesToPad) : PxU32(BUFSIZE));
278 bytesToPad -= BUFSIZE;
279 }
280 PX_ASSERT(!getPadding(getTotalStoredSize(), alignment));
281 }
282
283 virtual void writeName(const char*)
284 {
285 PxGetFoundation().error(physx::PxErrorCode::eINVALID_OPERATION, __FILE__, __LINE__,
286 "Cannot export names during exportData.");
287 }
288
289 const PxCollection& getCollection() const { return mCollection; }
290
291 virtual void registerReference(PxBase& serializable, PxU32 kind, size_t reference);
292
293 const PxArray<ImportReference>& getImportReferences() { return mImportReferences; }
294 InternalPtrRefMap& getInternalPtrReferencesMap() { return mInternalPtrReferencesMap; }
295 InternalHandle16RefMap& getInternalHandle16ReferencesMap() { return mInternalHandle16ReferencesMap; }
296
297 PxU32 getSize() const { return mMemStream.getSize(); }
298 PxU8* getData() const { return mMemStream.getData(); }
299
300
301
302 private:
303 //import reference map for unique registration of import references and corresponding buffer.
304 PxHashMap<PxSerialObjectId, PxU32> mImportReferencesMap;
305 PxArray<ImportReference> mImportReferences;
306
307 //maps for unique registration of internal references
308 InternalPtrRefMap mInternalPtrReferencesMap;
309 InternalHandle16RefMap mInternalHandle16ReferencesMap;
310
311 //map for quick lookup of manifest index.
312 PxHashMap<const PxBase*, PxU32> mObjToCollectionIndexMap;
313
314 //collection and externalRefs collection for assigning references.
315 const Cm::Collection& mCollection;
316 const Cm::Collection* mExternalRefs;
317
318 PxDefaultMemoryOutputStream mMemStream;
319
320 };
321
322 } // namespace Sn
323}
324
325#endif
Definition CmCollection.h:46
Definition CmCollection.h:61
Definition PxArray.h:53
Base class for objects that can be members of a PxCollection.
Definition PxBase.h:73
Collection class for serialization.
Definition PxCollection.h:104
virtual PxU32 write(const void *src, PxU32 count)
write to the stream. The number of bytes written may be less than the number sent.
Definition ExtDefaultStreams.cpp:57
Binary deserialization context class.
Definition PxSerialFramework.h:174
Binary serialization context class.
Definition PxSerialFramework.h:99
Definition PxUserAllocated.h:43
Definition SnSerializationContext.h:211
virtual PxBase * resolveReference(PxU32 kind, size_t reference) const
Retrieves a pointer to a deserialized PxBase object given a corresponding deserialized reference valu...
Definition SnSerializationContext.cpp:35
Definition SnSerializationContext.h:250
virtual void writeData(const void *buffer, PxU32 size)
Serializes object data and object extra data.
Definition SnSerializationContext.h:264
const PxCollection & getCollection() const
Returns the collection that is being serialized.
Definition SnSerializationContext.h:289
virtual void registerReference(PxBase &serializable, PxU32 kind, size_t reference)
Registers a reference value corresponding to a PxBase object.
Definition SnSerializationContext.cpp:72
virtual void alignData(PxU32 alignment=PX_SERIAL_ALIGN)
Aligns the serialized data.
Definition SnSerializationContext.h:266
virtual void writeName(const char *)
Helper function to write a name to the extraData if serialization is configured to save names.
Definition SnSerializationContext.h:283
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
PX_C_EXPORT PX_FOUNDATION_API physx::PxFoundation &PX_CALL_CONV PxGetFoundation()
Retrieves the Foundation SDK after it has been created.
Definition FdFoundation.cpp:279
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PX_FORCE_INLINE void * PxMemSet(void *dest, PxI32 c, PxU32 count)
Sets the bytes of the provided buffer to the specified value.
Definition PxMemory.h:67
PX_FORCE_INLINE void * PxMemCopy(void *dest, const void *src, PxU32 count)
Copies the bytes of one memory block to another. The memory blocks must not overlap.
Definition PxMemory.h:83
PX_INLINE void PxMarkSerializedMemory(void *ptr, PxU32 byteSize)
Definition PxMemory.h:111
PxU64 PxSerialObjectId
ID type for PxBase objects in a PxCollection.
Definition PxSerialFramework.h:61
@ eINVALID_OPERATION
method was called at a time when an operation is not possible
Definition PxErrors.h:65
Definition SnSerializationContext.h:134
Definition SnSerializationContext.h:74
Definition SnSerializationContext.h:185
Definition SnSerializationContext.h:158
Definition SnSerializationContext.h:49
Definition SnSerializationContext.h:99