btSerializer.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef BT_SERIALIZER_H
  14. #define BT_SERIALIZER_H
  15. #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
  16. #include "btHashMap.h"
  17. #if !defined( __CELLOS_LV2__) && !defined(__MWERKS__)
  18. #include <memory.h>
  19. #endif
  20. #include <string.h>
  21. extern char sBulletDNAstr[];
  22. extern int sBulletDNAlen;
  23. extern char sBulletDNAstr64[];
  24. extern int sBulletDNAlen64;
  25. SIMD_FORCE_INLINE int btStrLen(const char* str)
  26. {
  27. if (!str)
  28. return(0);
  29. int len = 0;
  30. while (*str != 0)
  31. {
  32. str++;
  33. len++;
  34. }
  35. return len;
  36. }
  37. class btChunk
  38. {
  39. public:
  40. int m_chunkCode;
  41. int m_length;
  42. void *m_oldPtr;
  43. int m_dna_nr;
  44. int m_number;
  45. };
  46. enum btSerializationFlags
  47. {
  48. BT_SERIALIZE_NO_BVH = 1,
  49. BT_SERIALIZE_NO_TRIANGLEINFOMAP = 2,
  50. BT_SERIALIZE_NO_DUPLICATE_ASSERT = 4
  51. };
  52. class btSerializer
  53. {
  54. public:
  55. virtual ~btSerializer() {}
  56. virtual const unsigned char* getBufferPointer() const = 0;
  57. virtual int getCurrentBufferSize() const = 0;
  58. virtual btChunk* allocate(size_t size, int numElements) = 0;
  59. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)= 0;
  60. virtual void* findPointer(void* oldPtr) = 0;
  61. virtual void* getUniquePointer(void*oldPtr) = 0;
  62. virtual void startSerialization() = 0;
  63. virtual void finishSerialization() = 0;
  64. virtual const char* findNameForPointer(const void* ptr) const = 0;
  65. virtual void registerNameForPointer(const void* ptr, const char* name) = 0;
  66. virtual void serializeName(const char* ptr) = 0;
  67. virtual int getSerializationFlags() const = 0;
  68. virtual void setSerializationFlags(int flags) = 0;
  69. virtual int getNumChunks() const = 0;
  70. virtual const btChunk* getChunk(int chunkIndex) const = 0;
  71. };
  72. #define BT_HEADER_LENGTH 12
  73. #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
  74. # define BT_MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
  75. #else
  76. # define BT_MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
  77. #endif
  78. #define BT_MULTIBODY_CODE BT_MAKE_ID('M','B','D','Y')
  79. #define BT_SOFTBODY_CODE BT_MAKE_ID('S','B','D','Y')
  80. #define BT_COLLISIONOBJECT_CODE BT_MAKE_ID('C','O','B','J')
  81. #define BT_RIGIDBODY_CODE BT_MAKE_ID('R','B','D','Y')
  82. #define BT_CONSTRAINT_CODE BT_MAKE_ID('C','O','N','S')
  83. #define BT_BOXSHAPE_CODE BT_MAKE_ID('B','O','X','S')
  84. #define BT_QUANTIZED_BVH_CODE BT_MAKE_ID('Q','B','V','H')
  85. #define BT_TRIANLGE_INFO_MAP BT_MAKE_ID('T','M','A','P')
  86. #define BT_SHAPE_CODE BT_MAKE_ID('S','H','A','P')
  87. #define BT_ARRAY_CODE BT_MAKE_ID('A','R','A','Y')
  88. #define BT_SBMATERIAL_CODE BT_MAKE_ID('S','B','M','T')
  89. #define BT_SBNODE_CODE BT_MAKE_ID('S','B','N','D')
  90. #define BT_DYNAMICSWORLD_CODE BT_MAKE_ID('D','W','L','D')
  91. #define BT_DNA_CODE BT_MAKE_ID('D','N','A','1')
  92. struct btPointerUid
  93. {
  94. union
  95. {
  96. void* m_ptr;
  97. int m_uniqueIds[2];
  98. };
  99. };
  100. struct btBulletSerializedArrays
  101. {
  102. btBulletSerializedArrays()
  103. {
  104. }
  105. btAlignedObjectArray<struct btQuantizedBvhDoubleData*> m_bvhsDouble;
  106. btAlignedObjectArray<struct btQuantizedBvhFloatData*> m_bvhsFloat;
  107. btAlignedObjectArray<struct btCollisionShapeData*> m_colShapeData;
  108. btAlignedObjectArray<struct btDynamicsWorldDoubleData*> m_dynamicWorldInfoDataDouble;
  109. btAlignedObjectArray<struct btDynamicsWorldFloatData*> m_dynamicWorldInfoDataFloat;
  110. btAlignedObjectArray<struct btRigidBodyDoubleData*> m_rigidBodyDataDouble;
  111. btAlignedObjectArray<struct btRigidBodyFloatData*> m_rigidBodyDataFloat;
  112. btAlignedObjectArray<struct btCollisionObjectDoubleData*> m_collisionObjectDataDouble;
  113. btAlignedObjectArray<struct btCollisionObjectFloatData*> m_collisionObjectDataFloat;
  114. btAlignedObjectArray<struct btTypedConstraintFloatData*> m_constraintDataFloat;
  115. btAlignedObjectArray<struct btTypedConstraintDoubleData*> m_constraintDataDouble;
  116. btAlignedObjectArray<struct btTypedConstraintData*> m_constraintData;//for backwards compatibility
  117. btAlignedObjectArray<struct btSoftBodyFloatData*> m_softBodyFloatData;
  118. btAlignedObjectArray<struct btSoftBodyDoubleData*> m_softBodyDoubleData;
  119. };
  120. ///The btDefaultSerializer is the main Bullet serialization class.
  121. ///The constructor takes an optional argument for backwards compatibility, it is recommended to leave this empty/zero.
  122. class btDefaultSerializer : public btSerializer
  123. {
  124. protected:
  125. btAlignedObjectArray<char*> mTypes;
  126. btAlignedObjectArray<short*> mStructs;
  127. btAlignedObjectArray<short> mTlens;
  128. btHashMap<btHashInt, int> mStructReverse;
  129. btHashMap<btHashString,int> mTypeLookup;
  130. btHashMap<btHashPtr,void*> m_chunkP;
  131. btHashMap<btHashPtr,const char*> m_nameMap;
  132. btHashMap<btHashPtr,btPointerUid> m_uniquePointers;
  133. int m_uniqueIdGenerator;
  134. int m_totalSize;
  135. unsigned char* m_buffer;
  136. bool m_ownsBuffer;
  137. int m_currentSize;
  138. void* m_dna;
  139. int m_dnaLength;
  140. int m_serializationFlags;
  141. btAlignedObjectArray<btChunk*> m_chunkPtrs;
  142. protected:
  143. virtual void* findPointer(void* oldPtr)
  144. {
  145. void** ptr = m_chunkP.find(oldPtr);
  146. if (ptr && *ptr)
  147. return *ptr;
  148. return 0;
  149. }
  150. virtual void writeDNA()
  151. {
  152. btChunk* dnaChunk = allocate(m_dnaLength,1);
  153. memcpy(dnaChunk->m_oldPtr,m_dna,m_dnaLength);
  154. finalizeChunk(dnaChunk,"DNA1",BT_DNA_CODE, m_dna);
  155. }
  156. int getReverseType(const char *type) const
  157. {
  158. btHashString key(type);
  159. const int* valuePtr = mTypeLookup.find(key);
  160. if (valuePtr)
  161. return *valuePtr;
  162. return -1;
  163. }
  164. void initDNA(const char* bdnaOrg,int dnalen)
  165. {
  166. ///was already initialized
  167. if (m_dna)
  168. return;
  169. int littleEndian= 1;
  170. littleEndian= ((char*)&littleEndian)[0];
  171. m_dna = btAlignedAlloc(dnalen,16);
  172. memcpy(m_dna,bdnaOrg,dnalen);
  173. m_dnaLength = dnalen;
  174. int *intPtr=0;
  175. short *shtPtr=0;
  176. char *cp = 0;int dataLen =0;
  177. intPtr = (int*)m_dna;
  178. /*
  179. SDNA (4 bytes) (magic number)
  180. NAME (4 bytes)
  181. <nr> (4 bytes) amount of names (int)
  182. <string>
  183. <string>
  184. */
  185. if (strncmp((const char*)m_dna, "SDNA", 4)==0)
  186. {
  187. // skip ++ NAME
  188. intPtr++; intPtr++;
  189. }
  190. // Parse names
  191. if (!littleEndian)
  192. *intPtr = btSwapEndian(*intPtr);
  193. dataLen = *intPtr;
  194. intPtr++;
  195. cp = (char*)intPtr;
  196. int i;
  197. for ( i=0; i<dataLen; i++)
  198. {
  199. while (*cp)cp++;
  200. cp++;
  201. }
  202. cp = btAlignPointer(cp,4);
  203. /*
  204. TYPE (4 bytes)
  205. <nr> amount of types (int)
  206. <string>
  207. <string>
  208. */
  209. intPtr = (int*)cp;
  210. btAssert(strncmp(cp, "TYPE", 4)==0); intPtr++;
  211. if (!littleEndian)
  212. *intPtr = btSwapEndian(*intPtr);
  213. dataLen = *intPtr;
  214. intPtr++;
  215. cp = (char*)intPtr;
  216. for (i=0; i<dataLen; i++)
  217. {
  218. mTypes.push_back(cp);
  219. while (*cp)cp++;
  220. cp++;
  221. }
  222. cp = btAlignPointer(cp,4);
  223. /*
  224. TLEN (4 bytes)
  225. <len> (short) the lengths of types
  226. <len>
  227. */
  228. // Parse type lens
  229. intPtr = (int*)cp;
  230. btAssert(strncmp(cp, "TLEN", 4)==0); intPtr++;
  231. dataLen = (int)mTypes.size();
  232. shtPtr = (short*)intPtr;
  233. for (i=0; i<dataLen; i++, shtPtr++)
  234. {
  235. if (!littleEndian)
  236. shtPtr[0] = btSwapEndian(shtPtr[0]);
  237. mTlens.push_back(shtPtr[0]);
  238. }
  239. if (dataLen & 1) shtPtr++;
  240. /*
  241. STRC (4 bytes)
  242. <nr> amount of structs (int)
  243. <typenr>
  244. <nr_of_elems>
  245. <typenr>
  246. <namenr>
  247. <typenr>
  248. <namenr>
  249. */
  250. intPtr = (int*)shtPtr;
  251. cp = (char*)intPtr;
  252. btAssert(strncmp(cp, "STRC", 4)==0); intPtr++;
  253. if (!littleEndian)
  254. *intPtr = btSwapEndian(*intPtr);
  255. dataLen = *intPtr ;
  256. intPtr++;
  257. shtPtr = (short*)intPtr;
  258. for (i=0; i<dataLen; i++)
  259. {
  260. mStructs.push_back (shtPtr);
  261. if (!littleEndian)
  262. {
  263. shtPtr[0]= btSwapEndian(shtPtr[0]);
  264. shtPtr[1]= btSwapEndian(shtPtr[1]);
  265. int len = shtPtr[1];
  266. shtPtr+= 2;
  267. for (int a=0; a<len; a++, shtPtr+=2)
  268. {
  269. shtPtr[0]= btSwapEndian(shtPtr[0]);
  270. shtPtr[1]= btSwapEndian(shtPtr[1]);
  271. }
  272. } else
  273. {
  274. shtPtr+= (2*shtPtr[1])+2;
  275. }
  276. }
  277. // build reverse lookups
  278. for (i=0; i<(int)mStructs.size(); i++)
  279. {
  280. short *strc = mStructs.at(i);
  281. mStructReverse.insert(strc[0], i);
  282. mTypeLookup.insert(btHashString(mTypes[strc[0]]),i);
  283. }
  284. }
  285. public:
  286. btHashMap<btHashPtr,void*> m_skipPointers;
  287. btDefaultSerializer(int totalSize=0, unsigned char* buffer=0)
  288. :m_uniqueIdGenerator(0),
  289. m_totalSize(totalSize),
  290. m_currentSize(0),
  291. m_dna(0),
  292. m_dnaLength(0),
  293. m_serializationFlags(0)
  294. {
  295. if (buffer==0)
  296. {
  297. m_buffer = m_totalSize?(unsigned char*)btAlignedAlloc(totalSize,16):0;
  298. m_ownsBuffer = true;
  299. } else
  300. {
  301. m_buffer = buffer;
  302. m_ownsBuffer = false;
  303. }
  304. const bool VOID_IS_8 = ((sizeof(void*)==8));
  305. #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  306. if (VOID_IS_8)
  307. {
  308. #if _WIN64
  309. initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
  310. #else
  311. btAssert(0);
  312. #endif
  313. } else
  314. {
  315. #ifndef _WIN64
  316. initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
  317. #else
  318. btAssert(0);
  319. #endif
  320. }
  321. #else //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  322. if (VOID_IS_8)
  323. {
  324. initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
  325. } else
  326. {
  327. initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
  328. }
  329. #endif //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  330. }
  331. virtual ~btDefaultSerializer()
  332. {
  333. if (m_buffer && m_ownsBuffer)
  334. btAlignedFree(m_buffer);
  335. if (m_dna)
  336. btAlignedFree(m_dna);
  337. }
  338. static int getMemoryDnaSizeInBytes()
  339. {
  340. const bool VOID_IS_8 = ((sizeof(void*) == 8));
  341. if (VOID_IS_8)
  342. {
  343. return sBulletDNAlen64;
  344. }
  345. return sBulletDNAlen;
  346. }
  347. static const char* getMemoryDna()
  348. {
  349. const bool VOID_IS_8 = ((sizeof(void*) == 8));
  350. if (VOID_IS_8)
  351. {
  352. return (const char*)sBulletDNAstr64;
  353. }
  354. return (const char*)sBulletDNAstr;
  355. }
  356. void insertHeader()
  357. {
  358. writeHeader(m_buffer);
  359. m_currentSize += BT_HEADER_LENGTH;
  360. }
  361. void writeHeader(unsigned char* buffer) const
  362. {
  363. #ifdef BT_USE_DOUBLE_PRECISION
  364. memcpy(buffer, "BULLETd", 7);
  365. #else
  366. memcpy(buffer, "BULLETf", 7);
  367. #endif //BT_USE_DOUBLE_PRECISION
  368. int littleEndian= 1;
  369. littleEndian= ((char*)&littleEndian)[0];
  370. if (sizeof(void*)==8)
  371. {
  372. buffer[7] = '-';
  373. } else
  374. {
  375. buffer[7] = '_';
  376. }
  377. if (littleEndian)
  378. {
  379. buffer[8]='v';
  380. } else
  381. {
  382. buffer[8]='V';
  383. }
  384. buffer[9] = '2';
  385. buffer[10] = '8';
  386. buffer[11] = '7';
  387. }
  388. virtual void startSerialization()
  389. {
  390. m_uniqueIdGenerator= 1;
  391. if (m_totalSize)
  392. {
  393. unsigned char* buffer = internalAlloc(BT_HEADER_LENGTH);
  394. writeHeader(buffer);
  395. }
  396. }
  397. virtual void finishSerialization()
  398. {
  399. writeDNA();
  400. //if we didn't pre-allocate a buffer, we need to create a contiguous buffer now
  401. int mysize = 0;
  402. if (!m_totalSize)
  403. {
  404. if (m_buffer)
  405. btAlignedFree(m_buffer);
  406. m_currentSize += BT_HEADER_LENGTH;
  407. m_buffer = (unsigned char*)btAlignedAlloc(m_currentSize,16);
  408. unsigned char* currentPtr = m_buffer;
  409. writeHeader(m_buffer);
  410. currentPtr += BT_HEADER_LENGTH;
  411. mysize+=BT_HEADER_LENGTH;
  412. for (int i=0;i< m_chunkPtrs.size();i++)
  413. {
  414. int curLength = sizeof(btChunk)+m_chunkPtrs[i]->m_length;
  415. memcpy(currentPtr,m_chunkPtrs[i], curLength);
  416. btAlignedFree(m_chunkPtrs[i]);
  417. currentPtr+=curLength;
  418. mysize+=curLength;
  419. }
  420. }
  421. mTypes.clear();
  422. mStructs.clear();
  423. mTlens.clear();
  424. mStructReverse.clear();
  425. mTypeLookup.clear();
  426. m_skipPointers.clear();
  427. m_chunkP.clear();
  428. m_nameMap.clear();
  429. m_uniquePointers.clear();
  430. m_chunkPtrs.clear();
  431. }
  432. virtual void* getUniquePointer(void*oldPtr)
  433. {
  434. btAssert(m_uniqueIdGenerator >= 0);
  435. if (!oldPtr)
  436. return 0;
  437. btPointerUid* uptr = (btPointerUid*)m_uniquePointers.find(oldPtr);
  438. if (uptr)
  439. {
  440. return uptr->m_ptr;
  441. }
  442. void** ptr2 = m_skipPointers[oldPtr];
  443. if (ptr2)
  444. {
  445. return 0;
  446. }
  447. m_uniqueIdGenerator++;
  448. btPointerUid uid;
  449. uid.m_uniqueIds[0] = m_uniqueIdGenerator;
  450. uid.m_uniqueIds[1] = m_uniqueIdGenerator;
  451. m_uniquePointers.insert(oldPtr,uid);
  452. return uid.m_ptr;
  453. }
  454. virtual const unsigned char* getBufferPointer() const
  455. {
  456. return m_buffer;
  457. }
  458. virtual int getCurrentBufferSize() const
  459. {
  460. return m_currentSize;
  461. }
  462. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
  463. {
  464. if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
  465. {
  466. btAssert(!findPointer(oldPtr));
  467. }
  468. chunk->m_dna_nr = getReverseType(structType);
  469. chunk->m_chunkCode = chunkCode;
  470. void* uniquePtr = getUniquePointer(oldPtr);
  471. m_chunkP.insert(oldPtr,uniquePtr);//chunk->m_oldPtr);
  472. chunk->m_oldPtr = uniquePtr;//oldPtr;
  473. }
  474. virtual unsigned char* internalAlloc(size_t size)
  475. {
  476. unsigned char* ptr = 0;
  477. if (m_totalSize)
  478. {
  479. ptr = m_buffer+m_currentSize;
  480. m_currentSize += int(size);
  481. btAssert(m_currentSize<m_totalSize);
  482. } else
  483. {
  484. ptr = (unsigned char*)btAlignedAlloc(size,16);
  485. m_currentSize += int(size);
  486. }
  487. return ptr;
  488. }
  489. virtual btChunk* allocate(size_t size, int numElements)
  490. {
  491. unsigned char* ptr = internalAlloc(int(size)*numElements+sizeof(btChunk));
  492. unsigned char* data = ptr + sizeof(btChunk);
  493. btChunk* chunk = (btChunk*)ptr;
  494. chunk->m_chunkCode = 0;
  495. chunk->m_oldPtr = data;
  496. chunk->m_length = int(size)*numElements;
  497. chunk->m_number = numElements;
  498. m_chunkPtrs.push_back(chunk);
  499. return chunk;
  500. }
  501. virtual const char* findNameForPointer(const void* ptr) const
  502. {
  503. const char*const * namePtr = m_nameMap.find(ptr);
  504. if (namePtr && *namePtr)
  505. return *namePtr;
  506. return 0;
  507. }
  508. virtual void registerNameForPointer(const void* ptr, const char* name)
  509. {
  510. m_nameMap.insert(ptr,name);
  511. }
  512. virtual void serializeName(const char* name)
  513. {
  514. if (name)
  515. {
  516. //don't serialize name twice
  517. if (findPointer((void*)name))
  518. return;
  519. int len = btStrLen(name);
  520. if (len)
  521. {
  522. int newLen = len+1;
  523. int padding = ((newLen+3)&~3)-newLen;
  524. newLen += padding;
  525. //serialize name string now
  526. btChunk* chunk = allocate(sizeof(char),newLen);
  527. char* destinationName = (char*)chunk->m_oldPtr;
  528. for (int i=0;i<len;i++)
  529. {
  530. destinationName[i] = name[i];
  531. }
  532. destinationName[len] = 0;
  533. finalizeChunk(chunk,"char",BT_ARRAY_CODE,(void*)name);
  534. }
  535. }
  536. }
  537. virtual int getSerializationFlags() const
  538. {
  539. return m_serializationFlags;
  540. }
  541. virtual void setSerializationFlags(int flags)
  542. {
  543. m_serializationFlags = flags;
  544. }
  545. int getNumChunks() const
  546. {
  547. return m_chunkPtrs.size();
  548. }
  549. const btChunk* getChunk(int chunkIndex) const
  550. {
  551. return m_chunkPtrs[chunkIndex];
  552. }
  553. };
  554. ///In general it is best to use btDefaultSerializer,
  555. ///in particular when writing the data to disk or sending it over the network.
  556. ///The btInMemorySerializer is experimental and only suitable in a few cases.
  557. ///The btInMemorySerializer takes a shortcut and can be useful to create a deep-copy
  558. ///of objects. There will be a demo on how to use the btInMemorySerializer.
  559. #ifdef ENABLE_INMEMORY_SERIALIZER
  560. struct btInMemorySerializer : public btDefaultSerializer
  561. {
  562. btHashMap<btHashPtr,btChunk*> m_uid2ChunkPtr;
  563. btHashMap<btHashPtr,void*> m_orgPtr2UniqueDataPtr;
  564. btHashMap<btHashString,const void*> m_names2Ptr;
  565. btBulletSerializedArrays m_arrays;
  566. btInMemorySerializer(int totalSize=0, unsigned char* buffer=0)
  567. :btDefaultSerializer(totalSize,buffer)
  568. {
  569. }
  570. virtual void startSerialization()
  571. {
  572. m_uid2ChunkPtr.clear();
  573. //todo: m_arrays.clear();
  574. btDefaultSerializer::startSerialization();
  575. }
  576. btChunk* findChunkFromUniquePointer(void* uniquePointer)
  577. {
  578. btChunk** chkPtr = m_uid2ChunkPtr[uniquePointer];
  579. if (chkPtr)
  580. {
  581. return *chkPtr;
  582. }
  583. return 0;
  584. }
  585. virtual void registerNameForPointer(const void* ptr, const char* name)
  586. {
  587. btDefaultSerializer::registerNameForPointer(ptr,name);
  588. m_names2Ptr.insert(name,ptr);
  589. }
  590. virtual void finishSerialization()
  591. {
  592. }
  593. virtual void* getUniquePointer(void*oldPtr)
  594. {
  595. if (oldPtr==0)
  596. return 0;
  597. // void* uniquePtr = getUniquePointer(oldPtr);
  598. btChunk* chunk = findChunkFromUniquePointer(oldPtr);
  599. if (chunk)
  600. {
  601. return chunk->m_oldPtr;
  602. } else
  603. {
  604. const char* n = (const char*) oldPtr;
  605. const void** ptr = m_names2Ptr[n];
  606. if (ptr)
  607. {
  608. return oldPtr;
  609. } else
  610. {
  611. void** ptr2 = m_skipPointers[oldPtr];
  612. if (ptr2)
  613. {
  614. return 0;
  615. } else
  616. {
  617. //If this assert hit, serialization happened in the wrong order
  618. // 'getUniquePointer'
  619. btAssert(0);
  620. }
  621. }
  622. return 0;
  623. }
  624. return oldPtr;
  625. }
  626. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
  627. {
  628. if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
  629. {
  630. btAssert(!findPointer(oldPtr));
  631. }
  632. chunk->m_dna_nr = getReverseType(structType);
  633. chunk->m_chunkCode = chunkCode;
  634. //void* uniquePtr = getUniquePointer(oldPtr);
  635. m_chunkP.insert(oldPtr,oldPtr);//chunk->m_oldPtr);
  636. // chunk->m_oldPtr = uniquePtr;//oldPtr;
  637. void* uid = findPointer(oldPtr);
  638. m_uid2ChunkPtr.insert(uid,chunk);
  639. switch (chunk->m_chunkCode)
  640. {
  641. case BT_SOFTBODY_CODE:
  642. {
  643. #ifdef BT_USE_DOUBLE_PRECISION
  644. m_arrays.m_softBodyDoubleData.push_back((btSoftBodyDoubleData*) chunk->m_oldPtr);
  645. #else
  646. m_arrays.m_softBodyFloatData.push_back((btSoftBodyFloatData*) chunk->m_oldPtr);
  647. #endif
  648. break;
  649. }
  650. case BT_COLLISIONOBJECT_CODE:
  651. {
  652. #ifdef BT_USE_DOUBLE_PRECISION
  653. m_arrays.m_collisionObjectDataDouble.push_back((btCollisionObjectDoubleData*)chunk->m_oldPtr);
  654. #else//BT_USE_DOUBLE_PRECISION
  655. m_arrays.m_collisionObjectDataFloat.push_back((btCollisionObjectFloatData*)chunk->m_oldPtr);
  656. #endif //BT_USE_DOUBLE_PRECISION
  657. break;
  658. }
  659. case BT_RIGIDBODY_CODE:
  660. {
  661. #ifdef BT_USE_DOUBLE_PRECISION
  662. m_arrays.m_rigidBodyDataDouble.push_back((btRigidBodyDoubleData*)chunk->m_oldPtr);
  663. #else
  664. m_arrays.m_rigidBodyDataFloat.push_back((btRigidBodyFloatData*)chunk->m_oldPtr);
  665. #endif//BT_USE_DOUBLE_PRECISION
  666. break;
  667. };
  668. case BT_CONSTRAINT_CODE:
  669. {
  670. #ifdef BT_USE_DOUBLE_PRECISION
  671. m_arrays.m_constraintDataDouble.push_back((btTypedConstraintDoubleData*)chunk->m_oldPtr);
  672. #else
  673. m_arrays.m_constraintDataFloat.push_back((btTypedConstraintFloatData*)chunk->m_oldPtr);
  674. #endif
  675. break;
  676. }
  677. case BT_QUANTIZED_BVH_CODE:
  678. {
  679. #ifdef BT_USE_DOUBLE_PRECISION
  680. m_arrays.m_bvhsDouble.push_back((btQuantizedBvhDoubleData*) chunk->m_oldPtr);
  681. #else
  682. m_arrays.m_bvhsFloat.push_back((btQuantizedBvhFloatData*) chunk->m_oldPtr);
  683. #endif
  684. break;
  685. }
  686. case BT_SHAPE_CODE:
  687. {
  688. btCollisionShapeData* shapeData = (btCollisionShapeData*) chunk->m_oldPtr;
  689. m_arrays.m_colShapeData.push_back(shapeData);
  690. break;
  691. }
  692. case BT_TRIANLGE_INFO_MAP:
  693. case BT_ARRAY_CODE:
  694. case BT_SBMATERIAL_CODE:
  695. case BT_SBNODE_CODE:
  696. case BT_DYNAMICSWORLD_CODE:
  697. case BT_DNA_CODE:
  698. {
  699. break;
  700. }
  701. default:
  702. {
  703. }
  704. };
  705. }
  706. int getNumChunks() const
  707. {
  708. return m_uid2ChunkPtr.size();
  709. }
  710. const btChunk* getChunk(int chunkIndex) const
  711. {
  712. return *m_uid2ChunkPtr.getAtIndex(chunkIndex);
  713. }
  714. };
  715. #endif //ENABLE_INMEMORY_SERIALIZER
  716. #endif //BT_SERIALIZER_H