btSerializer.h 19 KB

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