16 #ifndef BT_SERIALIZER_H
17 #define BT_SERIALIZER_H
22 #if !defined( __CELLOS_LV2__) && !defined(__MWERKS__)
81 virtual void finalizeChunk(
btChunk* chunk,
const char* structType,
int chunkCode,
void* oldPtr)= 0;
109 #define BT_HEADER_LENGTH 12
110 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
111 # define BT_MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
113 # define BT_MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
117 #define BT_MULTIBODY_CODE BT_MAKE_ID('M','B','D','Y')
118 #define BT_SOFTBODY_CODE BT_MAKE_ID('S','B','D','Y')
119 #define BT_COLLISIONOBJECT_CODE BT_MAKE_ID('C','O','B','J')
120 #define BT_RIGIDBODY_CODE BT_MAKE_ID('R','B','D','Y')
121 #define BT_CONSTRAINT_CODE BT_MAKE_ID('C','O','N','S')
122 #define BT_BOXSHAPE_CODE BT_MAKE_ID('B','O','X','S')
123 #define BT_QUANTIZED_BVH_CODE BT_MAKE_ID('Q','B','V','H')
124 #define BT_TRIANLGE_INFO_MAP BT_MAKE_ID('T','M','A','P')
125 #define BT_SHAPE_CODE BT_MAKE_ID('S','H','A','P')
126 #define BT_ARRAY_CODE BT_MAKE_ID('A','R','A','Y')
127 #define BT_SBMATERIAL_CODE BT_MAKE_ID('S','B','M','T')
128 #define BT_SBNODE_CODE BT_MAKE_ID('S','B','N','D')
129 #define BT_DYNAMICSWORLD_CODE BT_MAKE_ID('D','W','L','D')
130 #define BT_DNA_CODE BT_MAKE_ID('D','N','A','1')
204 void** ptr = m_chunkP.
find(oldPtr);
217 memcpy(dnaChunk->
m_oldPtr,m_dna,m_dnaLength);
225 const int* valuePtr = mTypeLookup.
find(key);
239 littleEndian= ((
char*)&littleEndian)[0];
243 memcpy(m_dna,bdnaOrg,dnalen);
244 m_dnaLength = dnalen;
248 char *cp = 0;
int dataLen =0;
249 intPtr = (
int*)m_dna;
259 if (strncmp((
const char*)m_dna,
"SDNA", 4)==0)
275 for ( i=0; i<dataLen; i++)
291 btAssert(strncmp(cp,
"TYPE", 4)==0); intPtr++;
301 for (i=0; i<dataLen; i++)
319 btAssert(strncmp(cp,
"TLEN", 4)==0); intPtr++;
321 dataLen = (int)mTypes.
size();
323 shtPtr = (
short*)intPtr;
324 for (i=0; i<dataLen; i++, shtPtr++)
331 if (dataLen & 1) shtPtr++;
344 intPtr = (
int*)shtPtr;
346 btAssert(strncmp(cp,
"STRC", 4)==0); intPtr++;
354 shtPtr = (
short*)intPtr;
355 for (i=0; i<dataLen; i++)
367 for (
int a=0; a<len; a++, shtPtr+=2)
375 shtPtr+= (2*shtPtr[1])+2;
380 for (i=0; i<(int)mStructs.
size(); i++)
382 short *strc = mStructs.
at(i);
383 mStructReverse.
insert(strc[0], i);
394 :m_totalSize(totalSize),
398 m_serializationFlags(0)
402 m_buffer = m_totalSize?(
unsigned char*)
btAlignedAlloc(totalSize,16):0;
407 m_ownsBuffer =
false;
410 const bool VOID_IS_8 = ((
sizeof(
void*)==8));
412 #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
429 #else //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
437 #endif //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
443 if (m_buffer && m_ownsBuffer)
459 #ifdef BT_USE_DOUBLE_PRECISION
460 memcpy(buffer,
"BULLETd", 7);
462 memcpy(buffer,
"BULLETf", 7);
463 #endif //BT_USE_DOUBLE_PRECISION
466 littleEndian= ((
char*)&littleEndian)[0];
468 if (
sizeof(
void*)==8)
493 m_uniqueIdGenerator= 1;
516 unsigned char* currentPtr =
m_buffer;
520 for (
int i=0;i< m_chunkPtrs.
size();i++)
522 int curLength =
sizeof(
btChunk)+m_chunkPtrs[i]->m_length;
523 memcpy(currentPtr,m_chunkPtrs[i], curLength);
525 currentPtr+=curLength;
533 mStructReverse.
clear();
535 m_skipPointers.
clear();
538 m_uniquePointers.
clear();
553 void** ptr2 = m_skipPointers[oldPtr];
559 m_uniqueIdGenerator++;
564 m_uniquePointers.
insert(oldPtr,uid);
592 m_chunkP.
insert(oldPtr,uniquePtr);
600 unsigned char* ptr = 0;
605 m_currentSize += int(size);
606 btAssert(m_currentSize<m_totalSize);
610 m_currentSize += int(size);
622 unsigned char* data = ptr +
sizeof(
btChunk);
626 chunk->m_oldPtr = data;
627 chunk->m_length = int(size)*numElements;
628 chunk->m_number = numElements;
638 const char*
const * namePtr = m_nameMap.
find(ptr);
639 if (namePtr && *namePtr)
647 m_nameMap.
insert(ptr,name);
663 int padding = ((newLen+3)&~3)-newLen;
668 char* destinationName = (
char*)chunk->
m_oldPtr;
669 for (
int i=0;i<len;i++)
671 destinationName[i] = name[i];
673 destinationName[len] = 0;
686 m_serializationFlags = flags;
690 return m_chunkPtrs.
size();
695 return m_chunkPtrs[chunkIndex];
705 #ifdef ENABLE_INMEMORY_SERIALIZER
716 btInMemorySerializer(
int totalSize=0,
unsigned char* buffer=0)
724 m_uid2ChunkPtr.
clear();
731 btChunk* findChunkFromUniquePointer(
void* uniquePointer)
733 btChunk** chkPtr = m_uid2ChunkPtr[uniquePointer];
744 m_names2Ptr.
insert(name,ptr);
757 btChunk* chunk = findChunkFromUniquePointer(oldPtr);
763 const char* n = (
const char*) oldPtr;
764 const void** ptr = m_names2Ptr[n];
770 void** ptr2 = m_skipPointers[oldPtr];
797 m_chunkP.insert(oldPtr,oldPtr);
801 m_uid2ChunkPtr.
insert(uid,chunk);
807 #ifdef BT_USE_DOUBLE_PRECISION
816 #ifdef BT_USE_DOUBLE_PRECISION
818 #else//BT_USE_DOUBLE_PRECISION
820 #endif //BT_USE_DOUBLE_PRECISION
825 #ifdef BT_USE_DOUBLE_PRECISION
829 #endif//BT_USE_DOUBLE_PRECISION
834 #ifdef BT_USE_DOUBLE_PRECISION
843 #ifdef BT_USE_DOUBLE_PRECISION
874 return m_uid2ChunkPtr.
size();
879 return *m_uid2ChunkPtr.
getAtIndex(chunkIndex);
883 #endif //ENABLE_INMEMORY_SERIALIZER
885 #endif //BT_SERIALIZER_H
virtual void finishSerialization()=0
btAlignedObjectArray< struct btRigidBodyDoubleData * > m_rigidBodyDataDouble
virtual int getCurrentBufferSize() const
virtual void serializeName(const char *name)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
btAlignedObjectArray< btChunk * > m_chunkPtrs
void push_back(const T &_Val)
virtual int getSerializationFlags() const
virtual void setSerializationFlags(int flags)
#define BT_CONSTRAINT_CODE
virtual const unsigned char * getBufferPointer() const
virtual void finalizeChunk(btChunk *chunk, const char *structType, int chunkCode, void *oldPtr)
btHashMap< btHashPtr, const char * > m_nameMap
const T & at(int n) const
virtual void registerNameForPointer(const void *ptr, const char *name)
btAlignedObjectArray< struct btDynamicsWorldFloatData * > m_dynamicWorldInfoDataFloat
btAlignedObjectArray< struct btQuantizedBvhFloatData * > m_bvhsFloat
#define BT_QUANTIZED_BVH_CODE
btAlignedObjectArray< struct btQuantizedBvhDoubleData * > m_bvhsDouble
void writeHeader(unsigned char *buffer) const
virtual btChunk * allocate(size_t size, int numElements)
virtual void startSerialization()=0
btAlignedObjectArray< struct btDynamicsWorldDoubleData * > m_dynamicWorldInfoDataDouble
virtual int getCurrentBufferSize() const =0
virtual void * getUniquePointer(void *oldPtr)=0
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
#define BT_DYNAMICSWORLD_CODE
#define SIMD_FORCE_INLINE
virtual void setSerializationFlags(int flags)=0
btAlignedObjectArray< struct btSoftBodyDoubleData * > m_softBodyDoubleData
virtual void registerNameForPointer(const void *ptr, const char *name)=0
virtual void finishSerialization()
btHashMap< btHashString, int > mTypeLookup
virtual int getNumChunks() const =0
virtual void * findPointer(void *oldPtr)
virtual void * getUniquePointer(void *oldPtr)
btHashMap< btHashInt, int > mStructReverse
void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
The btDefaultSerializer is the main Bullet serialization class.
btAlignedObjectArray< struct btTypedConstraintFloatData * > m_constraintDataFloat
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
const btChunk * getChunk(int chunkIndex) const
virtual unsigned char * internalAlloc(size_t size)
int size() const
return the number of elements in the array
virtual int getSerializationFlags() const =0
btAlignedObjectArray< struct btCollisionObjectFloatData * > m_collisionObjectDataFloat
virtual const unsigned char * getBufferPointer() const =0
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
btAlignedObjectArray< struct btCollisionShapeData * > m_colShapeData
btAlignedObjectArray< struct btTypedConstraintData * > m_constraintData
void initDNA(const char *bdnaOrg, int dnalen)
btAlignedObjectArray< short * > mStructs
#define btAlignedFree(ptr)
void insert(const Key &key, const Value &value)
btBulletSerializedArrays()
btAlignedObjectArray< char * > mTypes
btAlignedObjectArray< struct btSoftBodyFloatData * > m_softBodyFloatData
int btStrLen(const char *str)
virtual const btChunk * getChunk(int chunkIndex) const =0
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
#define BT_COLLISIONOBJECT_CODE
btDefaultSerializer(int totalSize=0, unsigned char *buffer=0)
const Value * find(const Key &key) const
const Value * getAtIndex(int index) const
btAlignedObjectArray< struct btCollisionObjectDoubleData * > m_collisionObjectDataDouble
virtual void finalizeChunk(btChunk *chunk, const char *structType, int chunkCode, void *oldPtr)=0
btHashMap< btHashPtr, void * > m_skipPointers
very basic hashable string implementation, compatible with btHashMap
virtual void serializeName(const char *ptr)=0
#define BT_SBMATERIAL_CODE
virtual const char * findNameForPointer(const void *ptr) const
btAlignedObjectArray< struct btRigidBodyFloatData * > m_rigidBodyDataFloat
#define BT_RIGIDBODY_CODE
virtual void startSerialization()
btAlignedObjectArray< short > mTlens
unsigned btSwapEndian(unsigned val)
#define btAlignedAlloc(size, alignment)
int getReverseType(const char *type) const
T * btAlignPointer(T *unalignedPtr, size_t alignment)
align a pointer to the provided alignment, upwards
virtual const char * findNameForPointer(const void *ptr) const =0
#define BT_TRIANLGE_INFO_MAP
btHashMap< btHashPtr, btPointerUid > m_uniquePointers
virtual void * findPointer(void *oldPtr)=0
virtual ~btDefaultSerializer()
btHashMap< btHashPtr, void * > m_chunkP
char sBulletDNAstr[]
only the 32bit versions for now
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
virtual btChunk * allocate(size_t size, int numElements)=0
btAlignedObjectArray< struct btTypedConstraintDoubleData * > m_constraintDataDouble
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...