btSerializer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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 "btStackAlloc.h"
  17. #include "btHashMap.h"
  18. #if !defined( __CELLOS_LV2__) && !defined(__MWERKS__)
  19. #include <memory.h>
  20. #endif
  21. #include <string.h>
  22. ///only the 32bit versions for now
  23. extern unsigned char sBulletDNAstr[];
  24. extern int sBulletDNAlen;
  25. extern unsigned char sBulletDNAstr64[];
  26. extern int sBulletDNAlen64;
  27. SIMD_FORCE_INLINE int btStrLen(const char* str)
  28. {
  29. if (!str)
  30. return(0);
  31. int len = 0;
  32. while (*str != 0)
  33. {
  34. str++;
  35. len++;
  36. }
  37. return len;
  38. }
  39. class btChunk
  40. {
  41. public:
  42. int m_chunkCode;
  43. int m_length;
  44. void *m_oldPtr;
  45. int m_dna_nr;
  46. int m_number;
  47. };
  48. enum btSerializationFlags
  49. {
  50. BT_SERIALIZE_NO_BVH = 1,
  51. BT_SERIALIZE_NO_TRIANGLEINFOMAP = 2,
  52. BT_SERIALIZE_NO_DUPLICATE_ASSERT = 4
  53. };
  54. class btSerializer
  55. {
  56. public:
  57. virtual ~btSerializer() {}
  58. virtual const unsigned char* getBufferPointer() const = 0;
  59. virtual int getCurrentBufferSize() const = 0;
  60. virtual btChunk* allocate(size_t size, int numElements) = 0;
  61. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)= 0;
  62. virtual void* findPointer(void* oldPtr) = 0;
  63. virtual void* getUniquePointer(void*oldPtr) = 0;
  64. virtual void startSerialization() = 0;
  65. virtual void finishSerialization() = 0;
  66. virtual const char* findNameForPointer(const void* ptr) const = 0;
  67. virtual void registerNameForPointer(const void* ptr, const char* name) = 0;
  68. virtual void serializeName(const char* ptr) = 0;
  69. virtual int getSerializationFlags() const = 0;
  70. virtual void setSerializationFlags(int flags) = 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 MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) )
  75. #else
  76. # define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) )
  77. #endif
  78. #define BT_COLLISIONOBJECT_CODE MAKE_ID('C','O','B','J')
  79. #define BT_RIGIDBODY_CODE MAKE_ID('R','B','D','Y')
  80. #define BT_CONSTRAINT_CODE MAKE_ID('C','O','N','S')
  81. #define BT_BOXSHAPE_CODE MAKE_ID('B','O','X','S')
  82. #define BT_QUANTIZED_BVH_CODE MAKE_ID('Q','B','V','H')
  83. #define BT_TRIANLGE_INFO_MAP MAKE_ID('T','M','A','P')
  84. #define BT_SHAPE_CODE MAKE_ID('S','H','A','P')
  85. #define BT_ARRAY_CODE MAKE_ID('A','R','A','Y')
  86. struct btPointerUid
  87. {
  88. union
  89. {
  90. void* m_ptr;
  91. int m_uniqueIds[2];
  92. };
  93. };
  94. class btDefaultSerializer : public btSerializer
  95. {
  96. btAlignedObjectArray<char*> mTypes;
  97. btAlignedObjectArray<short*> mStructs;
  98. btAlignedObjectArray<short> mTlens;
  99. btHashMap<btHashInt, int> mStructReverse;
  100. btHashMap<btHashString,int> mTypeLookup;
  101. btHashMap<btHashPtr,void*> m_chunkP;
  102. btHashMap<btHashPtr,const char*> m_nameMap;
  103. btHashMap<btHashPtr,btPointerUid> m_uniquePointers;
  104. int m_uniqueIdGenerator;
  105. int m_totalSize;
  106. unsigned char* m_buffer;
  107. int m_currentSize;
  108. void* m_dna;
  109. int m_dnaLength;
  110. int m_serializationFlags;
  111. btAlignedObjectArray<btChunk*> m_chunkPtrs;
  112. protected:
  113. virtual void* findPointer(void* oldPtr)
  114. {
  115. void** ptr = m_chunkP.find(oldPtr);
  116. if (ptr && *ptr)
  117. return *ptr;
  118. return 0;
  119. }
  120. void writeDNA()
  121. {
  122. unsigned char* dnaTarget = m_buffer+m_currentSize;
  123. memcpy(dnaTarget,m_dna,m_dnaLength);
  124. m_currentSize += m_dnaLength;
  125. }
  126. int getReverseType(const char *type) const
  127. {
  128. btHashString key(type);
  129. const int* valuePtr = mTypeLookup.find(key);
  130. if (valuePtr)
  131. return *valuePtr;
  132. return -1;
  133. }
  134. void initDNA(const char* bdnaOrg,int dnalen)
  135. {
  136. ///was already initialized
  137. if (m_dna)
  138. return;
  139. int littleEndian= 1;
  140. littleEndian= ((char*)&littleEndian)[0];
  141. m_dna = btAlignedAlloc(dnalen,16);
  142. memcpy(m_dna,bdnaOrg,dnalen);
  143. m_dnaLength = dnalen;
  144. int *intPtr=0;
  145. short *shtPtr=0;
  146. char *cp = 0;int dataLen =0;long nr=0;
  147. intPtr = (int*)m_dna;
  148. /*
  149. SDNA (4 bytes) (magic number)
  150. NAME (4 bytes)
  151. <nr> (4 bytes) amount of names (int)
  152. <string>
  153. <string>
  154. */
  155. if (strncmp((const char*)m_dna, "SDNA", 4)==0)
  156. {
  157. // skip ++ NAME
  158. intPtr++; intPtr++;
  159. }
  160. // Parse names
  161. if (!littleEndian)
  162. *intPtr = btSwapEndian(*intPtr);
  163. dataLen = *intPtr;
  164. intPtr++;
  165. cp = (char*)intPtr;
  166. int i;
  167. for ( i=0; i<dataLen; i++)
  168. {
  169. while (*cp)cp++;
  170. cp++;
  171. }
  172. {
  173. nr= (long)cp;
  174. // long mask=3;
  175. nr= ((nr+3)&~3)-nr;
  176. while (nr--)
  177. {
  178. cp++;
  179. }
  180. }
  181. /*
  182. TYPE (4 bytes)
  183. <nr> amount of types (int)
  184. <string>
  185. <string>
  186. */
  187. intPtr = (int*)cp;
  188. assert(strncmp(cp, "TYPE", 4)==0); intPtr++;
  189. if (!littleEndian)
  190. *intPtr = btSwapEndian(*intPtr);
  191. dataLen = *intPtr;
  192. intPtr++;
  193. cp = (char*)intPtr;
  194. for (i=0; i<dataLen; i++)
  195. {
  196. mTypes.push_back(cp);
  197. while (*cp)cp++;
  198. cp++;
  199. }
  200. {
  201. nr= (long)cp;
  202. // long mask=3;
  203. nr= ((nr+3)&~3)-nr;
  204. while (nr--)
  205. {
  206. cp++;
  207. }
  208. }
  209. /*
  210. TLEN (4 bytes)
  211. <len> (short) the lengths of types
  212. <len>
  213. */
  214. // Parse type lens
  215. intPtr = (int*)cp;
  216. assert(strncmp(cp, "TLEN", 4)==0); intPtr++;
  217. dataLen = (int)mTypes.size();
  218. shtPtr = (short*)intPtr;
  219. for (i=0; i<dataLen; i++, shtPtr++)
  220. {
  221. if (!littleEndian)
  222. shtPtr[0] = btSwapEndian(shtPtr[0]);
  223. mTlens.push_back(shtPtr[0]);
  224. }
  225. if (dataLen & 1) shtPtr++;
  226. /*
  227. STRC (4 bytes)
  228. <nr> amount of structs (int)
  229. <typenr>
  230. <nr_of_elems>
  231. <typenr>
  232. <namenr>
  233. <typenr>
  234. <namenr>
  235. */
  236. intPtr = (int*)shtPtr;
  237. cp = (char*)intPtr;
  238. assert(strncmp(cp, "STRC", 4)==0); intPtr++;
  239. if (!littleEndian)
  240. *intPtr = btSwapEndian(*intPtr);
  241. dataLen = *intPtr ;
  242. intPtr++;
  243. shtPtr = (short*)intPtr;
  244. for (i=0; i<dataLen; i++)
  245. {
  246. mStructs.push_back (shtPtr);
  247. if (!littleEndian)
  248. {
  249. shtPtr[0]= btSwapEndian(shtPtr[0]);
  250. shtPtr[1]= btSwapEndian(shtPtr[1]);
  251. int len = shtPtr[1];
  252. shtPtr+= 2;
  253. for (int a=0; a<len; a++, shtPtr+=2)
  254. {
  255. shtPtr[0]= btSwapEndian(shtPtr[0]);
  256. shtPtr[1]= btSwapEndian(shtPtr[1]);
  257. }
  258. } else
  259. {
  260. shtPtr+= (2*shtPtr[1])+2;
  261. }
  262. }
  263. // build reverse lookups
  264. for (i=0; i<(int)mStructs.size(); i++)
  265. {
  266. short *strc = mStructs.at(i);
  267. mStructReverse.insert(strc[0], i);
  268. mTypeLookup.insert(btHashString(mTypes[strc[0]]),i);
  269. }
  270. }
  271. public:
  272. btDefaultSerializer(int totalSize)
  273. :m_totalSize(totalSize),
  274. m_currentSize(0),
  275. m_dna(0),
  276. m_dnaLength(0),
  277. m_serializationFlags(0)
  278. {
  279. m_buffer = (unsigned char*)btAlignedAlloc(totalSize, 16);
  280. const bool VOID_IS_8 = ((sizeof(void*)==8));
  281. #ifdef BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  282. if (VOID_IS_8)
  283. {
  284. #if _WIN64
  285. initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
  286. #else
  287. btAssert(0);
  288. #endif
  289. } else
  290. {
  291. #ifndef _WIN64
  292. initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
  293. #else
  294. btAssert(0);
  295. #endif
  296. }
  297. #else //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  298. if (VOID_IS_8)
  299. {
  300. initDNA((const char*)sBulletDNAstr64,sBulletDNAlen64);
  301. } else
  302. {
  303. initDNA((const char*)sBulletDNAstr,sBulletDNAlen);
  304. }
  305. #endif //BT_INTERNAL_UPDATE_SERIALIZATION_STRUCTURES
  306. }
  307. virtual ~btDefaultSerializer()
  308. {
  309. if (m_buffer)
  310. btAlignedFree(m_buffer);
  311. if (m_dna)
  312. btAlignedFree(m_dna);
  313. }
  314. virtual void startSerialization()
  315. {
  316. m_uniqueIdGenerator= 1;
  317. m_currentSize = BT_HEADER_LENGTH;
  318. #ifdef BT_USE_DOUBLE_PRECISION
  319. memcpy(m_buffer, "BULLETd", 7);
  320. #else
  321. memcpy(m_buffer, "BULLETf", 7);
  322. #endif //BT_USE_DOUBLE_PRECISION
  323. int littleEndian= 1;
  324. littleEndian= ((char*)&littleEndian)[0];
  325. if (sizeof(void*)==8)
  326. {
  327. m_buffer[7] = '-';
  328. } else
  329. {
  330. m_buffer[7] = '_';
  331. }
  332. if (littleEndian)
  333. {
  334. m_buffer[8]='v';
  335. } else
  336. {
  337. m_buffer[8]='V';
  338. }
  339. m_buffer[9] = '2';
  340. m_buffer[10] = '7';
  341. m_buffer[11] = '6';
  342. }
  343. virtual void finishSerialization()
  344. {
  345. writeDNA();
  346. mTypes.clear();
  347. mStructs.clear();
  348. mTlens.clear();
  349. mStructReverse.clear();
  350. mTypeLookup.clear();
  351. m_chunkP.clear();
  352. m_nameMap.clear();
  353. m_uniquePointers.clear();
  354. }
  355. virtual void* getUniquePointer(void*oldPtr)
  356. {
  357. if (!oldPtr)
  358. return 0;
  359. btPointerUid* uptr = (btPointerUid*)m_uniquePointers.find(oldPtr);
  360. if (uptr)
  361. {
  362. return uptr->m_ptr;
  363. }
  364. m_uniqueIdGenerator++;
  365. btPointerUid uid;
  366. uid.m_uniqueIds[0] = m_uniqueIdGenerator;
  367. uid.m_uniqueIds[1] = m_uniqueIdGenerator;
  368. m_uniquePointers.insert(oldPtr,uid);
  369. return uid.m_ptr;
  370. }
  371. virtual const unsigned char* getBufferPointer() const
  372. {
  373. return m_buffer;
  374. }
  375. virtual int getCurrentBufferSize() const
  376. {
  377. return m_currentSize;
  378. }
  379. virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
  380. {
  381. if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
  382. {
  383. btAssert(!findPointer(oldPtr));
  384. }
  385. chunk->m_dna_nr = getReverseType(structType);
  386. chunk->m_chunkCode = chunkCode;
  387. void* uniquePtr = getUniquePointer(oldPtr);
  388. m_chunkP.insert(oldPtr,uniquePtr);//chunk->m_oldPtr);
  389. chunk->m_oldPtr = uniquePtr;//oldPtr;
  390. }
  391. virtual btChunk* allocate(size_t size, int numElements)
  392. {
  393. unsigned char* ptr = m_buffer+m_currentSize;
  394. m_currentSize += int(size)*numElements+sizeof(btChunk);
  395. btAssert(m_currentSize<m_totalSize);
  396. unsigned char* data = ptr + sizeof(btChunk);
  397. btChunk* chunk = (btChunk*)ptr;
  398. chunk->m_chunkCode = 0;
  399. chunk->m_oldPtr = data;
  400. chunk->m_length = int(size)*numElements;
  401. chunk->m_number = numElements;
  402. m_chunkPtrs.push_back(chunk);
  403. return chunk;
  404. }
  405. virtual const char* findNameForPointer(const void* ptr) const
  406. {
  407. const char*const * namePtr = m_nameMap.find(ptr);
  408. if (namePtr && *namePtr)
  409. return *namePtr;
  410. return 0;
  411. }
  412. virtual void registerNameForPointer(const void* ptr, const char* name)
  413. {
  414. m_nameMap.insert(ptr,name);
  415. }
  416. virtual void serializeName(const char* name)
  417. {
  418. if (name)
  419. {
  420. //don't serialize name twice
  421. if (findPointer((void*)name))
  422. return;
  423. int len = btStrLen(name);
  424. if (len)
  425. {
  426. int newLen = len+1;
  427. int padding = ((newLen+3)&~3)-newLen;
  428. newLen += padding;
  429. //serialize name string now
  430. btChunk* chunk = allocate(sizeof(char),newLen);
  431. char* destinationName = (char*)chunk->m_oldPtr;
  432. for (int i=0;i<len;i++)
  433. {
  434. destinationName[i] = name[i];
  435. }
  436. destinationName[len] = 0;
  437. finalizeChunk(chunk,"char",BT_ARRAY_CODE,(void*)name);
  438. }
  439. }
  440. }
  441. virtual int getSerializationFlags() const
  442. {
  443. return m_serializationFlags;
  444. }
  445. virtual void setSerializationFlags(int flags)
  446. {
  447. m_serializationFlags = flags;
  448. }
  449. };
  450. #endif //BT_SERIALIZER_H