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 BT_SERIALIZER_H
17 #define BT_SERIALIZER_H
19 #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
20 #include "btStackAlloc.h"
21 #include "btHashMap.h"
23 #if !defined( __CELLOS_LV2__) && !defined(__MWERKS__)
30 ///only the 32bit versions for now
31 extern char sBulletDNAstr[];
32 extern int sBulletDNAlen;
33 extern char sBulletDNAstr64[];
34 extern int sBulletDNAlen64;
36 SIMD_FORCE_INLINE int btStrLen(const char* str)
62 enum btSerializationFlags
64 BT_SERIALIZE_NO_BVH = 1,
65 BT_SERIALIZE_NO_TRIANGLEINFOMAP = 2,
66 BT_SERIALIZE_NO_DUPLICATE_ASSERT = 4
74 virtual ~btSerializer() {}
76 virtual const unsigned char* getBufferPointer() const = 0;
78 virtual int getCurrentBufferSize() const = 0;
80 virtual btChunk* allocate(size_t size, int numElements) = 0;
82 virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)= 0;
84 virtual void* findPointer(void* oldPtr) = 0;
86 virtual void* getUniquePointer(void*oldPtr) = 0;
88 virtual void startSerialization() = 0;
90 virtual void finishSerialization() = 0;
92 virtual const char* findNameForPointer(const void* ptr) const = 0;
94 virtual void registerNameForPointer(const void* ptr, const char* name) = 0;
96 virtual void serializeName(const char* ptr) = 0;
98 virtual int getSerializationFlags() const = 0;
100 virtual void setSerializationFlags(int flags) = 0;
107 #define BT_HEADER_LENGTH 12
108 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
109 # define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
111 # define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
114 #define BT_SOFTBODY_CODE MAKE_ID('S','B','D','Y')
115 #define BT_COLLISIONOBJECT_CODE MAKE_ID('C','O','B','J')
116 #define BT_RIGIDBODY_CODE MAKE_ID('R','B','D','Y')
117 #define BT_CONSTRAINT_CODE MAKE_ID('C','O','N','S')
118 #define BT_BOXSHAPE_CODE MAKE_ID('B','O','X','S')
119 #define BT_QUANTIZED_BVH_CODE MAKE_ID('Q','B','V','H')
120 #define BT_TRIANLGE_INFO_MAP MAKE_ID('T','M','A','P')
121 #define BT_SHAPE_CODE MAKE_ID('S','H','A','P')
122 #define BT_ARRAY_CODE MAKE_ID('A','R','A','Y')
123 #define BT_SBMATERIAL_CODE MAKE_ID('S','B','M','T')
124 #define BT_SBNODE_CODE MAKE_ID('S','B','N','D')
125 #define BT_DNA_CODE MAKE_ID('D','N','A','1')
137 ///The btDefaultSerializer is the main Bullet serialization class.
138 ///The constructor takes an optional argument for backwards compatibility, it is recommended to leave this empty/zero.
139 class btDefaultSerializer : public btSerializer
143 btAlignedObjectArray<char*> mTypes;
144 btAlignedObjectArray<short*> mStructs;
145 btAlignedObjectArray<short> mTlens;
146 btHashMap<btHashInt, int> mStructReverse;
147 btHashMap<btHashString,int> mTypeLookup;
150 btHashMap<btHashPtr,void*> m_chunkP;
152 btHashMap<btHashPtr,const char*> m_nameMap;
154 btHashMap<btHashPtr,btPointerUid> m_uniquePointers;
155 int m_uniqueIdGenerator;
158 unsigned char* m_buffer;
163 int m_serializationFlags;
166 btAlignedObjectArray<btChunk*> m_chunkPtrs;
170 virtual void* findPointer(void* oldPtr)
172 void** ptr = m_chunkP.find(oldPtr);
184 btChunk* dnaChunk = allocate(m_dnaLength,1);
185 memcpy(dnaChunk->m_oldPtr,m_dna,m_dnaLength);
186 finalizeChunk(dnaChunk,"DNA1",BT_DNA_CODE, m_dna);
189 int getReverseType(const char *type) const
192 btHashString key(type);
193 const int* valuePtr = mTypeLookup.find(key);
200 void initDNA(const char* bdnaOrg,int dnalen)
202 ///was already initialized
207 littleEndian= ((char*)&littleEndian)[0];
210 m_dna = btAlignedAlloc(dnalen,16);
211 memcpy(m_dna,bdnaOrg,dnalen);
212 m_dnaLength = dnalen;
216 char *cp = 0;int dataLen =0;
217 intPtr = (int*)m_dna;
220 SDNA (4 bytes) (magic number)
222 <nr> (4 bytes) amount of names (int)
227 if (strncmp((const char*)m_dna, "SDNA", 4)==0)
235 *intPtr = btSwapEndian(*intPtr);
243 for ( i=0; i<dataLen; i++)
249 cp = btAlignPointer(cp,4);
253 <nr> amount of types (int)
259 assert(strncmp(cp, "TYPE", 4)==0); intPtr++;
262 *intPtr = btSwapEndian(*intPtr);
269 for (i=0; i<dataLen; i++)
271 mTypes.push_back(cp);
276 cp = btAlignPointer(cp,4);
281 <len> (short) the lengths of types
287 assert(strncmp(cp, "TLEN", 4)==0); intPtr++;
289 dataLen = (int)mTypes.size();
291 shtPtr = (short*)intPtr;
292 for (i=0; i<dataLen; i++, shtPtr++)
295 shtPtr[0] = btSwapEndian(shtPtr[0]);
296 mTlens.push_back(shtPtr[0]);
299 if (dataLen & 1) shtPtr++;
303 <nr> amount of structs (int)
312 intPtr = (int*)shtPtr;
314 assert(strncmp(cp, "STRC", 4)==0); intPtr++;
317 *intPtr = btSwapEndian(*intPtr);
322 shtPtr = (short*)intPtr;
323 for (i=0; i<dataLen; i++)
325 mStructs.push_back (shtPtr);
329 shtPtr[0]= btSwapEndian(shtPtr[0]);
330 shtPtr[1]= btSwapEndian(shtPtr[1]);
335 for (int a=0; a<len; a++, shtPtr+=2)
337 shtPtr[0]= btSwapEndian(shtPtr[0]);
338 shtPtr[1]= btSwapEndian(shtPtr[1]);
343 shtPtr+= (2*shtPtr[1])+2;
347 // build reverse lookups
348 for (i=0; i<(int)mStructs.size(); i++)
350 short *strc = mStructs.at(i);
351 mStructReverse.insert(strc[0], i);
352 mTypeLookup.insert(btHashString(mTypes[strc[0]]),i);
361 btDefaultSerializer(int totalSize=0)
362 :m_totalSize(totalSize),
366 m_serializationFlags(0)
368 m_buffer = m_totalSize?(unsigned char*)btAlignedAlloc(totalSize,16):0;
370 const bool VOID_IS_8 = ((sizeof(void*)==8));
372 #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
376 initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
383 initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
389 #else //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
392 initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
395 initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
397 #endif //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
401 virtual ~btDefaultSerializer()
404 btAlignedFree(m_buffer);
406 btAlignedFree(m_dna);
409 void writeHeader(unsigned char* buffer) const
413 #ifdef BT_USE_DOUBLE_PRECISION
414 memcpy(buffer, "BULLETd", 7);
416 memcpy(buffer, "BULLETf", 7);
417 #endif //BT_USE_DOUBLE_PRECISION
420 littleEndian= ((char*)&littleEndian)[0];
422 if (sizeof(void*)==8)
445 virtual void startSerialization()
447 m_uniqueIdGenerator= 1;
450 unsigned char* buffer = internalAlloc(BT_HEADER_LENGTH);
456 virtual void finishSerialization()
460 //if we didn't pre-allocate a buffer, we need to create a contiguous buffer now
465 btAlignedFree(m_buffer);
467 m_currentSize += BT_HEADER_LENGTH;
468 m_buffer = (unsigned char*)btAlignedAlloc(m_currentSize,16);
470 unsigned char* currentPtr = m_buffer;
471 writeHeader(m_buffer);
472 currentPtr += BT_HEADER_LENGTH;
473 mysize+=BT_HEADER_LENGTH;
474 for (int i=0;i< m_chunkPtrs.size();i++)
476 int curLength = sizeof(btChunk)+m_chunkPtrs[i]->m_length;
477 memcpy(currentPtr,m_chunkPtrs[i], curLength);
478 btAlignedFree(m_chunkPtrs[i]);
479 currentPtr+=curLength;
487 mStructReverse.clear();
491 m_uniquePointers.clear();
495 virtual void* getUniquePointer(void*oldPtr)
500 btPointerUid* uptr = (btPointerUid*)m_uniquePointers.find(oldPtr);
505 m_uniqueIdGenerator++;
508 uid.m_uniqueIds[0] = m_uniqueIdGenerator;
509 uid.m_uniqueIds[1] = m_uniqueIdGenerator;
510 m_uniquePointers.insert(oldPtr,uid);
515 virtual const unsigned char* getBufferPointer() const
520 virtual int getCurrentBufferSize() const
522 return m_currentSize;
525 virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
527 if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
529 btAssert(!findPointer(oldPtr));
532 chunk->m_dna_nr = getReverseType(structType);
534 chunk->m_chunkCode = chunkCode;
536 void* uniquePtr = getUniquePointer(oldPtr);
538 m_chunkP.insert(oldPtr,uniquePtr);//chunk->m_oldPtr);
539 chunk->m_oldPtr = uniquePtr;//oldPtr;
544 virtual unsigned char* internalAlloc(size_t size)
546 unsigned char* ptr = 0;
550 ptr = m_buffer+m_currentSize;
551 m_currentSize += int(size);
552 btAssert(m_currentSize<m_totalSize);
555 ptr = (unsigned char*)btAlignedAlloc(size,16);
556 m_currentSize += int(size);
563 virtual btChunk* allocate(size_t size, int numElements)
566 unsigned char* ptr = internalAlloc(int(size)*numElements+sizeof(btChunk));
568 unsigned char* data = ptr + sizeof(btChunk);
570 btChunk* chunk = (btChunk*)ptr;
571 chunk->m_chunkCode = 0;
572 chunk->m_oldPtr = data;
573 chunk->m_length = int(size)*numElements;
574 chunk->m_number = numElements;
576 m_chunkPtrs.push_back(chunk);
582 virtual const char* findNameForPointer(const void* ptr) const
584 const char*const * namePtr = m_nameMap.find(ptr);
585 if (namePtr && *namePtr)
591 virtual void registerNameForPointer(const void* ptr, const char* name)
593 m_nameMap.insert(ptr,name);
596 virtual void serializeName(const char* name)
600 //don't serialize name twice
601 if (findPointer((void*)name))
604 int len = btStrLen(name);
609 int padding = ((newLen+3)&~3)-newLen;
612 //serialize name string now
613 btChunk* chunk = allocate(sizeof(char),newLen);
614 char* destinationName = (char*)chunk->m_oldPtr;
615 for (int i=0;i<len;i++)
617 destinationName[i] = name[i];
619 destinationName[len] = 0;
620 finalizeChunk(chunk,"char",BT_ARRAY_CODE,(void*)name);
625 virtual int getSerializationFlags() const
627 return m_serializationFlags;
630 virtual void setSerializationFlags(int flags)
632 m_serializationFlags = flags;
638 #endif //BT_SERIALIZER_H