2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #ifndef B3_SERIALIZER_H
17 #define B3_SERIALIZER_H
19 #include "Bullet3Common/b3Scalar.h" // has definitions like B3_FORCE_INLINE
20 #include "Bullet3Common/b3StackAlloc.h"
21 #include "Bullet3Common/b3HashMap.h"
23 #if !defined(__CELLOS_LV2__) && !defined(__MWERKS__)
28 extern char b3s_bulletDNAstr[];
29 extern int b3s_bulletDNAlen;
30 extern char b3s_bulletDNAstr64[];
31 extern int b3s_bulletDNAlen64;
33 B3_FORCE_INLINE int b3StrLen(const char* str)
58 enum b3SerializationFlags
60 B3_SERIALIZE_NO_BVH = 1,
61 B3_SERIALIZE_NO_TRIANGLEINFOMAP = 2,
62 B3_SERIALIZE_NO_DUPLICATE_ASSERT = 4
68 virtual ~b3Serializer() {}
70 virtual const unsigned char* getBufferPointer() const = 0;
72 virtual int getCurrentBufferSize() const = 0;
74 virtual b3Chunk* allocate(size_t size, int numElements) = 0;
76 virtual void finalizeChunk(b3Chunk* chunk, const char* structType, int chunkCode, void* oldPtr) = 0;
78 virtual void* findPointer(void* oldPtr) = 0;
80 virtual void* getUniquePointer(void* oldPtr) = 0;
82 virtual void startSerialization() = 0;
84 virtual void finishSerialization() = 0;
86 virtual const char* findNameForPointer(const void* ptr) const = 0;
88 virtual void registerNameForPointer(const void* ptr, const char* name) = 0;
90 virtual void serializeName(const char* ptr) = 0;
92 virtual int getSerializationFlags() const = 0;
94 virtual void setSerializationFlags(int flags) = 0;
97 #define B3_HEADER_LENGTH 12
98 #if defined(__sgi) || defined(__sparc) || defined(__sparc__) || defined(__PPC__) || defined(__ppc__) || defined(__BIG_ENDIAN__)
99 #define B3_MAKE_ID(a, b, c, d) ((int)(a) << 24 | (int)(b) << 16 | (c) << 8 | (d))
101 #define B3_MAKE_ID(a, b, c, d) ((int)(d) << 24 | (int)(c) << 16 | (b) << 8 | (a))
104 #define B3_SOFTBODY_CODE B3_MAKE_ID('S', 'B', 'D', 'Y')
105 #define B3_COLLISIONOBJECT_CODE B3_MAKE_ID('C', 'O', 'B', 'J')
106 #define B3_RIGIDBODY_CODE B3_MAKE_ID('R', 'B', 'D', 'Y')
107 #define B3_CONSTRAINT_CODE B3_MAKE_ID('C', 'O', 'N', 'S')
108 #define B3_BOXSHAPE_CODE B3_MAKE_ID('B', 'O', 'X', 'S')
109 #define B3_QUANTIZED_BVH_CODE B3_MAKE_ID('Q', 'B', 'V', 'H')
110 #define B3_TRIANLGE_INFO_MAP B3_MAKE_ID('T', 'M', 'A', 'P')
111 #define B3_SHAPE_CODE B3_MAKE_ID('S', 'H', 'A', 'P')
112 #define B3_ARRAY_CODE B3_MAKE_ID('A', 'R', 'A', 'Y')
113 #define B3_SBMATERIAL_CODE B3_MAKE_ID('S', 'B', 'M', 'T')
114 #define B3_SBNODE_CODE B3_MAKE_ID('S', 'B', 'N', 'D')
115 #define B3_DYNAMICSWORLD_CODE B3_MAKE_ID('D', 'W', 'L', 'D')
116 #define B3_DNA_CODE B3_MAKE_ID('D', 'N', 'A', '1')
126 ///The b3DefaultSerializer is the main Bullet serialization class.
127 ///The constructor takes an optional argument for backwards compatibility, it is recommended to leave this empty/zero.
128 class b3DefaultSerializer : public b3Serializer
130 b3AlignedObjectArray<char*> mTypes;
131 b3AlignedObjectArray<short*> mStructs;
132 b3AlignedObjectArray<short> mTlens;
133 b3HashMap<b3HashInt, int> mStructReverse;
134 b3HashMap<b3HashString, int> mTypeLookup;
136 b3HashMap<b3HashPtr, void*> m_chunkP;
138 b3HashMap<b3HashPtr, const char*> m_nameMap;
140 b3HashMap<b3HashPtr, b3PointerUid> m_uniquePointers;
141 int m_uniqueIdGenerator;
144 unsigned char* m_buffer;
149 int m_serializationFlags;
151 b3AlignedObjectArray<b3Chunk*> m_chunkPtrs;
154 virtual void* findPointer(void* oldPtr)
156 void** ptr = m_chunkP.find(oldPtr);
164 b3Chunk* dnaChunk = allocate(m_dnaLength, 1);
165 memcpy(dnaChunk->m_oldPtr, m_dna, m_dnaLength);
166 finalizeChunk(dnaChunk, "DNA1", B3_DNA_CODE, m_dna);
169 int getReverseType(const char* type) const
171 b3HashString key(type);
172 const int* valuePtr = mTypeLookup.find(key);
179 void initDNA(const char* bdnaOrg, int dnalen)
181 ///was already initialized
185 int littleEndian = 1;
186 littleEndian = ((char*)&littleEndian)[0];
188 m_dna = b3AlignedAlloc(dnalen, 16);
189 memcpy(m_dna, bdnaOrg, dnalen);
190 m_dnaLength = dnalen;
196 intPtr = (int*)m_dna;
199 SDNA (4 bytes) (magic number)
201 <nr> (4 bytes) amount of names (int)
206 if (strncmp((const char*)m_dna, "SDNA", 4) == 0)
215 *intPtr = b3SwapEndian(*intPtr);
223 for (i = 0; i < dataLen; i++)
228 cp = b3AlignPointer(cp, 4);
232 <nr> amount of types (int)
238 b3Assert(strncmp(cp, "TYPE", 4) == 0);
242 *intPtr = b3SwapEndian(*intPtr);
248 for (i = 0; i < dataLen; i++)
250 mTypes.push_back(cp);
255 cp = b3AlignPointer(cp, 4);
259 <len> (short) the lengths of types
265 b3Assert(strncmp(cp, "TLEN", 4) == 0);
268 dataLen = (int)mTypes.size();
270 shtPtr = (short*)intPtr;
271 for (i = 0; i < dataLen; i++, shtPtr++)
274 shtPtr[0] = b3SwapEndian(shtPtr[0]);
275 mTlens.push_back(shtPtr[0]);
278 if (dataLen & 1) shtPtr++;
282 <nr> amount of structs (int)
291 intPtr = (int*)shtPtr;
293 b3Assert(strncmp(cp, "STRC", 4) == 0);
297 *intPtr = b3SwapEndian(*intPtr);
301 shtPtr = (short*)intPtr;
302 for (i = 0; i < dataLen; i++)
304 mStructs.push_back(shtPtr);
308 shtPtr[0] = b3SwapEndian(shtPtr[0]);
309 shtPtr[1] = b3SwapEndian(shtPtr[1]);
314 for (int a = 0; a < len; a++, shtPtr += 2)
316 shtPtr[0] = b3SwapEndian(shtPtr[0]);
317 shtPtr[1] = b3SwapEndian(shtPtr[1]);
322 shtPtr += (2 * shtPtr[1]) + 2;
326 // build reverse lookups
327 for (i = 0; i < (int)mStructs.size(); i++)
329 short* strc = mStructs.at(i);
330 mStructReverse.insert(strc[0], i);
331 mTypeLookup.insert(b3HashString(mTypes[strc[0]]), i);
336 b3DefaultSerializer(int totalSize = 0)
337 : m_totalSize(totalSize),
341 m_serializationFlags(0)
343 m_buffer = m_totalSize ? (unsigned char*)b3AlignedAlloc(totalSize, 16) : 0;
345 const bool VOID_IS_8 = ((sizeof(void*) == 8));
347 #ifdef B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
351 initDNA((const char*)b3s_bulletDNAstr64, b3s_bulletDNAlen64);
359 initDNA((const char*)b3s_bulletDNAstr, b3s_bulletDNAlen);
365 #else //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
368 initDNA((const char*)b3s_bulletDNAstr64, b3s_bulletDNAlen64);
372 initDNA((const char*)b3s_bulletDNAstr, b3s_bulletDNAlen);
374 #endif //B3_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
377 virtual ~b3DefaultSerializer()
380 b3AlignedFree(m_buffer);
382 b3AlignedFree(m_dna);
385 void writeHeader(unsigned char* buffer) const
387 #ifdef B3_USE_DOUBLE_PRECISION
388 memcpy(buffer, "BULLETd", 7);
390 memcpy(buffer, "BULLETf", 7);
391 #endif //B3_USE_DOUBLE_PRECISION
393 int littleEndian = 1;
394 littleEndian = ((char*)&littleEndian)[0];
396 if (sizeof(void*) == 8)
419 virtual void startSerialization()
421 m_uniqueIdGenerator = 1;
424 unsigned char* buffer = internalAlloc(B3_HEADER_LENGTH);
429 virtual void finishSerialization()
433 //if we didn't pre-allocate a buffer, we need to create a contiguous buffer now
438 b3AlignedFree(m_buffer);
440 m_currentSize += B3_HEADER_LENGTH;
441 m_buffer = (unsigned char*)b3AlignedAlloc(m_currentSize, 16);
443 unsigned char* currentPtr = m_buffer;
444 writeHeader(m_buffer);
445 currentPtr += B3_HEADER_LENGTH;
446 mysize += B3_HEADER_LENGTH;
447 for (int i = 0; i < m_chunkPtrs.size(); i++)
449 int curLength = sizeof(b3Chunk) + m_chunkPtrs[i]->m_length;
450 memcpy(currentPtr, m_chunkPtrs[i], curLength);
451 b3AlignedFree(m_chunkPtrs[i]);
452 currentPtr += curLength;
460 mStructReverse.clear();
464 m_uniquePointers.clear();
468 virtual void* getUniquePointer(void* oldPtr)
473 b3PointerUid* uptr = (b3PointerUid*)m_uniquePointers.find(oldPtr);
478 m_uniqueIdGenerator++;
481 uid.m_uniqueIds[0] = m_uniqueIdGenerator;
482 uid.m_uniqueIds[1] = m_uniqueIdGenerator;
483 m_uniquePointers.insert(oldPtr, uid);
487 virtual const unsigned char* getBufferPointer() const
492 virtual int getCurrentBufferSize() const
494 return m_currentSize;
497 virtual void finalizeChunk(b3Chunk* chunk, const char* structType, int chunkCode, void* oldPtr)
499 if (!(m_serializationFlags & B3_SERIALIZE_NO_DUPLICATE_ASSERT))
501 b3Assert(!findPointer(oldPtr));
504 chunk->m_dna_nr = getReverseType(structType);
506 chunk->m_chunkCode = chunkCode;
508 void* uniquePtr = getUniquePointer(oldPtr);
510 m_chunkP.insert(oldPtr, uniquePtr); //chunk->m_oldPtr);
511 chunk->m_oldPtr = uniquePtr; //oldPtr;
514 virtual unsigned char* internalAlloc(size_t size)
516 unsigned char* ptr = 0;
520 ptr = m_buffer + m_currentSize;
521 m_currentSize += int(size);
522 b3Assert(m_currentSize < m_totalSize);
526 ptr = (unsigned char*)b3AlignedAlloc(size, 16);
527 m_currentSize += int(size);
532 virtual b3Chunk* allocate(size_t size, int numElements)
534 unsigned char* ptr = internalAlloc(int(size) * numElements + sizeof(b3Chunk));
536 unsigned char* data = ptr + sizeof(b3Chunk);
538 b3Chunk* chunk = (b3Chunk*)ptr;
539 chunk->m_chunkCode = 0;
540 chunk->m_oldPtr = data;
541 chunk->m_length = int(size) * numElements;
542 chunk->m_number = numElements;
544 m_chunkPtrs.push_back(chunk);
549 virtual const char* findNameForPointer(const void* ptr) const
551 const char* const* namePtr = m_nameMap.find(ptr);
552 if (namePtr && *namePtr)
557 virtual void registerNameForPointer(const void* ptr, const char* name)
559 m_nameMap.insert(ptr, name);
562 virtual void serializeName(const char* name)
566 //don't serialize name twice
567 if (findPointer((void*)name))
570 int len = b3StrLen(name);
573 int newLen = len + 1;
574 int padding = ((newLen + 3) & ~3) - newLen;
577 //serialize name string now
578 b3Chunk* chunk = allocate(sizeof(char), newLen);
579 char* destinationName = (char*)chunk->m_oldPtr;
580 for (int i = 0; i < len; i++)
582 destinationName[i] = name[i];
584 destinationName[len] = 0;
585 finalizeChunk(chunk, "char", B3_ARRAY_CODE, (void*)name);
590 virtual int getSerializationFlags() const
592 return m_serializationFlags;
595 virtual void setSerializationFlags(int flags)
597 m_serializationFlags = flags;
601 #endif //B3_SERIALIZER_H