vertexdecl.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <string.h>
  6. #include <bx/debug.h>
  7. #include <bx/hash.h>
  8. #include <bx/uint32_t.h>
  9. #include <bx/string.h>
  10. #include <bx/readerwriter.h>
  11. #include "config.h"
  12. #include "vertexdecl.h"
  13. namespace bgfx
  14. {
  15. static const uint8_t s_attribTypeSizeDx9[AttribType::Count][4] =
  16. {
  17. { 4, 4, 4, 4 }, // Uint8
  18. { 4, 4, 4, 4 }, // Uint10
  19. { 4, 4, 8, 8 }, // Int16
  20. { 4, 4, 8, 8 }, // Half
  21. { 4, 8, 12, 16 }, // Float
  22. };
  23. static const uint8_t s_attribTypeSizeDx1x[AttribType::Count][4] =
  24. {
  25. { 1, 2, 4, 4 }, // Uint8
  26. { 4, 4, 4, 4 }, // Uint10
  27. { 2, 4, 8, 8 }, // Int16
  28. { 2, 4, 8, 8 }, // Half
  29. { 4, 8, 12, 16 }, // Float
  30. };
  31. static const uint8_t s_attribTypeSizeGl[AttribType::Count][4] =
  32. {
  33. { 1, 2, 4, 4 }, // Uint8
  34. { 4, 4, 4, 4 }, // Uint10
  35. { 2, 4, 6, 8 }, // Int16
  36. { 2, 4, 6, 8 }, // Half
  37. { 4, 8, 12, 16 }, // Float
  38. };
  39. static const uint8_t (*s_attribTypeSize[])[AttribType::Count][4] =
  40. {
  41. &s_attribTypeSizeDx9, // Null
  42. &s_attribTypeSizeDx9, // Direct3D9
  43. &s_attribTypeSizeDx1x, // Direct3D11
  44. &s_attribTypeSizeDx1x, // Direct3D12
  45. &s_attribTypeSizeGl, // Metal
  46. &s_attribTypeSizeGl, // OpenGLES
  47. &s_attribTypeSizeGl, // OpenGL
  48. &s_attribTypeSizeGl, // Vulkan
  49. &s_attribTypeSizeDx9, // Count
  50. };
  51. BX_STATIC_ASSERT(BX_COUNTOF(s_attribTypeSize) == RendererType::Count+1);
  52. void initAttribTypeSizeTable(RendererType::Enum _type)
  53. {
  54. s_attribTypeSize[0] = s_attribTypeSize[_type];
  55. s_attribTypeSize[RendererType::Count] = s_attribTypeSize[_type];
  56. }
  57. void dbgPrintfVargs(const char* _format, va_list _argList)
  58. {
  59. char temp[8192];
  60. char* out = temp;
  61. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  62. if ( (int32_t)sizeof(temp) < len)
  63. {
  64. out = (char*)alloca(len+1);
  65. len = bx::vsnprintf(out, len, _format, _argList);
  66. }
  67. out[len] = '\0';
  68. bx::debugOutput(out);
  69. }
  70. void dbgPrintf(const char* _format, ...)
  71. {
  72. va_list argList;
  73. va_start(argList, _format);
  74. dbgPrintfVargs(_format, argList);
  75. va_end(argList);
  76. }
  77. VertexDecl::VertexDecl()
  78. {
  79. // BK - struct need to have ctor to qualify as non-POD data.
  80. // Need this to catch programming errors when serializing struct.
  81. }
  82. VertexDecl& VertexDecl::begin(RendererType::Enum _renderer)
  83. {
  84. m_hash = _renderer; // use hash to store renderer type while building VertexDecl.
  85. m_stride = 0;
  86. memset(m_attributes, 0xff, sizeof(m_attributes) );
  87. memset(m_offset, 0, sizeof(m_offset) );
  88. return *this;
  89. }
  90. void VertexDecl::end()
  91. {
  92. bx::HashMurmur2A murmur;
  93. murmur.begin();
  94. murmur.add(m_attributes, sizeof(m_attributes) );
  95. murmur.add(m_offset, sizeof(m_offset) );
  96. m_hash = murmur.end();
  97. }
  98. VertexDecl& VertexDecl::add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized, bool _asInt)
  99. {
  100. const uint32_t encodedNorm = (_normalized&1)<<7;
  101. const uint32_t encodedType = (_type&7)<<3;
  102. const uint32_t encodedNum = (_num-1)&3;
  103. const uint32_t encodeAsInt = (_asInt&(!!"\x1\x1\x1\x0\x0"[_type]) )<<8;
  104. m_attributes[_attrib] = encodedNorm|encodedType|encodedNum|encodeAsInt;
  105. m_offset[_attrib] = m_stride;
  106. m_stride += (*s_attribTypeSize[m_hash])[_type][_num-1];
  107. return *this;
  108. }
  109. VertexDecl& VertexDecl::skip(uint8_t _num)
  110. {
  111. m_stride += _num;
  112. return *this;
  113. }
  114. void VertexDecl::decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const
  115. {
  116. uint16_t val = m_attributes[_attrib];
  117. _num = (val&3)+1;
  118. _type = AttribType::Enum( (val>>3)&7);
  119. _normalized = !!(val&(1<<7) );
  120. _asInt = !!(val&(1<<8) );
  121. }
  122. static const char* s_attrName[] =
  123. {
  124. "Attrib::Position",
  125. "Attrib::Normal",
  126. "Attrib::Tangent",
  127. "Attrib::Bitangent",
  128. "Attrib::Color0",
  129. "Attrib::Color1",
  130. "Attrib::Indices",
  131. "Attrib::Weights",
  132. "Attrib::TexCoord0",
  133. "Attrib::TexCoord1",
  134. "Attrib::TexCoord2",
  135. "Attrib::TexCoord3",
  136. "Attrib::TexCoord4",
  137. "Attrib::TexCoord5",
  138. "Attrib::TexCoord6",
  139. "Attrib::TexCoord7",
  140. };
  141. BX_STATIC_ASSERT(BX_COUNTOF(s_attrName) == Attrib::Count);
  142. const char* getAttribName(Attrib::Enum _attr)
  143. {
  144. return s_attrName[_attr];
  145. }
  146. void dump(const VertexDecl& _decl)
  147. {
  148. if (BX_ENABLED(BGFX_CONFIG_DEBUG) )
  149. {
  150. dbgPrintf("vertexdecl %08x (%08x), stride %d\n"
  151. , _decl.m_hash
  152. , bx::hashMurmur2A(_decl.m_attributes)
  153. , _decl.m_stride
  154. );
  155. for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
  156. {
  157. if (UINT16_MAX != _decl.m_attributes[attr])
  158. {
  159. uint8_t num;
  160. AttribType::Enum type;
  161. bool normalized;
  162. bool asInt;
  163. _decl.decode(Attrib::Enum(attr), num, type, normalized, asInt);
  164. dbgPrintf("\tattr %d - %s, num %d, type %d, norm %d, asint %d, offset %d\n"
  165. , attr
  166. , getAttribName(Attrib::Enum(attr) )
  167. , num
  168. , type
  169. , normalized
  170. , asInt
  171. , _decl.m_offset[attr]
  172. );
  173. }
  174. }
  175. }
  176. }
  177. struct AttribToId
  178. {
  179. Attrib::Enum attr;
  180. uint16_t id;
  181. };
  182. static AttribToId s_attribToId[] =
  183. {
  184. // NOTICE:
  185. // Attrib must be in order how it appears in Attrib::Enum! id is
  186. // unique and should not be changed if new Attribs are added.
  187. { Attrib::Position, 0x0001 },
  188. { Attrib::Normal, 0x0002 },
  189. { Attrib::Tangent, 0x0003 },
  190. { Attrib::Bitangent, 0x0004 },
  191. { Attrib::Color0, 0x0005 },
  192. { Attrib::Color1, 0x0006 },
  193. { Attrib::Indices, 0x000e },
  194. { Attrib::Weight, 0x000f },
  195. { Attrib::TexCoord0, 0x0010 },
  196. { Attrib::TexCoord1, 0x0011 },
  197. { Attrib::TexCoord2, 0x0012 },
  198. { Attrib::TexCoord3, 0x0013 },
  199. { Attrib::TexCoord4, 0x0014 },
  200. { Attrib::TexCoord5, 0x0015 },
  201. { Attrib::TexCoord6, 0x0016 },
  202. { Attrib::TexCoord7, 0x0017 },
  203. };
  204. BX_STATIC_ASSERT(BX_COUNTOF(s_attribToId) == Attrib::Count);
  205. Attrib::Enum idToAttrib(uint16_t id)
  206. {
  207. for (uint32_t ii = 0; ii < BX_COUNTOF(s_attribToId); ++ii)
  208. {
  209. if (s_attribToId[ii].id == id)
  210. {
  211. return s_attribToId[ii].attr;
  212. }
  213. }
  214. return Attrib::Count;
  215. }
  216. uint16_t attribToId(Attrib::Enum _attr)
  217. {
  218. return s_attribToId[_attr].id;
  219. }
  220. struct AttribTypeToId
  221. {
  222. AttribType::Enum type;
  223. uint16_t id;
  224. };
  225. static AttribTypeToId s_attribTypeToId[] =
  226. {
  227. // NOTICE:
  228. // AttribType must be in order how it appears in AttribType::Enum!
  229. // id is unique and should not be changed if new AttribTypes are
  230. // added.
  231. { AttribType::Uint8, 0x0001 },
  232. { AttribType::Uint10, 0x0005 },
  233. { AttribType::Int16, 0x0002 },
  234. { AttribType::Half, 0x0003 },
  235. { AttribType::Float, 0x0004 },
  236. };
  237. BX_STATIC_ASSERT(BX_COUNTOF(s_attribTypeToId) == AttribType::Count);
  238. AttribType::Enum idToAttribType(uint16_t id)
  239. {
  240. for (uint32_t ii = 0; ii < BX_COUNTOF(s_attribTypeToId); ++ii)
  241. {
  242. if (s_attribTypeToId[ii].id == id)
  243. {
  244. return s_attribTypeToId[ii].type;
  245. }
  246. }
  247. return AttribType::Count;
  248. }
  249. uint16_t attribTypeToId(AttribType::Enum _attr)
  250. {
  251. return s_attribTypeToId[_attr].id;
  252. }
  253. int32_t write(bx::WriterI* _writer, const VertexDecl& _decl)
  254. {
  255. int32_t total = 0;
  256. uint8_t numAttrs = 0;
  257. for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
  258. {
  259. numAttrs += UINT16_MAX == _decl.m_attributes[attr] ? 0 : 1;
  260. }
  261. total += bx::write(_writer, numAttrs);
  262. total += bx::write(_writer, _decl.m_stride);
  263. for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
  264. {
  265. if (UINT16_MAX != _decl.m_attributes[attr])
  266. {
  267. uint8_t num;
  268. AttribType::Enum type;
  269. bool normalized;
  270. bool asInt;
  271. _decl.decode(Attrib::Enum(attr), num, type, normalized, asInt);
  272. total += bx::write(_writer, _decl.m_offset[attr]);
  273. total += bx::write(_writer, s_attribToId[attr].id);
  274. total += bx::write(_writer, num);
  275. total += bx::write(_writer, s_attribTypeToId[type].id);
  276. total += bx::write(_writer, normalized);
  277. total += bx::write(_writer, asInt);
  278. }
  279. }
  280. return total;
  281. }
  282. int32_t read(bx::ReaderI* _reader, VertexDecl& _decl)
  283. {
  284. int32_t total = 0;
  285. uint8_t numAttrs;
  286. total += bx::read(_reader, numAttrs);
  287. uint16_t stride;
  288. total += bx::read(_reader, stride);
  289. _decl.begin();
  290. for (uint32_t ii = 0; ii < numAttrs; ++ii)
  291. {
  292. uint16_t offset;
  293. total += bx::read(_reader, offset);
  294. uint16_t attribId = 0;
  295. total += bx::read(_reader, attribId);
  296. uint8_t num;
  297. total += bx::read(_reader, num);
  298. uint16_t attribTypeId;
  299. total += bx::read(_reader, attribTypeId);
  300. bool normalized;
  301. total += bx::read(_reader, normalized);
  302. bool asInt;
  303. total += bx::read(_reader, asInt);
  304. Attrib::Enum attr = idToAttrib(attribId);
  305. AttribType::Enum type = idToAttribType(attribTypeId);
  306. if (Attrib::Count != attr
  307. && AttribType::Count != type)
  308. {
  309. _decl.add(attr, num, type, normalized, asInt);
  310. _decl.m_offset[attr] = offset;
  311. }
  312. }
  313. _decl.end();
  314. _decl.m_stride = stride;
  315. return total;
  316. }
  317. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index)
  318. {
  319. if (!_decl.has(_attr) )
  320. {
  321. return;
  322. }
  323. uint32_t stride = _decl.getStride();
  324. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  325. uint8_t num;
  326. AttribType::Enum type;
  327. bool normalized;
  328. bool asInt;
  329. _decl.decode(_attr, num, type, normalized, asInt);
  330. switch (type)
  331. {
  332. default:
  333. case AttribType::Uint8:
  334. {
  335. uint8_t* packed = (uint8_t*)data;
  336. if (_inputNormalized)
  337. {
  338. if (asInt)
  339. {
  340. switch (num)
  341. {
  342. default: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  343. case 3: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  344. case 2: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  345. case 1: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  346. }
  347. }
  348. else
  349. {
  350. switch (num)
  351. {
  352. default: *packed++ = uint8_t(*_input++ * 255.0f);
  353. case 3: *packed++ = uint8_t(*_input++ * 255.0f);
  354. case 2: *packed++ = uint8_t(*_input++ * 255.0f);
  355. case 1: *packed++ = uint8_t(*_input++ * 255.0f);
  356. }
  357. }
  358. }
  359. else
  360. {
  361. switch (num)
  362. {
  363. default: *packed++ = uint8_t(*_input++);
  364. case 3: *packed++ = uint8_t(*_input++);
  365. case 2: *packed++ = uint8_t(*_input++);
  366. case 1: *packed++ = uint8_t(*_input++);
  367. }
  368. }
  369. }
  370. break;
  371. case AttribType::Uint10:
  372. {
  373. uint32_t packed = 0;
  374. if (_inputNormalized)
  375. {
  376. if (asInt)
  377. {
  378. switch (num)
  379. {
  380. default:
  381. case 3: packed |= uint32_t(*_input++ * 511.0f + 512.0f);
  382. case 2: packed <<= 10; packed |= uint32_t(*_input++ * 511.0f + 512.0f);
  383. case 1: packed <<= 10; packed |= uint32_t(*_input++ * 511.0f + 512.0f);
  384. }
  385. }
  386. else
  387. {
  388. switch (num)
  389. {
  390. default:
  391. case 3: packed |= uint32_t(*_input++ * 1023.0f);
  392. case 2: packed <<= 10; packed |= uint32_t(*_input++ * 1023.0f);
  393. case 1: packed <<= 10; packed |= uint32_t(*_input++ * 1023.0f);
  394. }
  395. }
  396. }
  397. else
  398. {
  399. switch (num)
  400. {
  401. default:
  402. case 3: packed |= uint32_t(*_input++);
  403. case 2: packed <<= 10; packed |= uint32_t(*_input++);
  404. case 1: packed <<= 10; packed |= uint32_t(*_input++);
  405. }
  406. }
  407. *(uint32_t*)data = packed;
  408. }
  409. break;
  410. case AttribType::Int16:
  411. {
  412. int16_t* packed = (int16_t*)data;
  413. if (_inputNormalized)
  414. {
  415. if (asInt)
  416. {
  417. switch (num)
  418. {
  419. default: *packed++ = int16_t(*_input++ * 32767.0f);
  420. case 3: *packed++ = int16_t(*_input++ * 32767.0f);
  421. case 2: *packed++ = int16_t(*_input++ * 32767.0f);
  422. case 1: *packed++ = int16_t(*_input++ * 32767.0f);
  423. }
  424. }
  425. else
  426. {
  427. switch (num)
  428. {
  429. default: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  430. case 3: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  431. case 2: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  432. case 1: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  433. }
  434. }
  435. }
  436. else
  437. {
  438. switch (num)
  439. {
  440. default: *packed++ = int16_t(*_input++);
  441. case 3: *packed++ = int16_t(*_input++);
  442. case 2: *packed++ = int16_t(*_input++);
  443. case 1: *packed++ = int16_t(*_input++);
  444. }
  445. }
  446. }
  447. break;
  448. case AttribType::Half:
  449. {
  450. uint16_t* packed = (uint16_t*)data;
  451. switch (num)
  452. {
  453. default: *packed++ = bx::halfFromFloat(*_input++);
  454. case 3: *packed++ = bx::halfFromFloat(*_input++);
  455. case 2: *packed++ = bx::halfFromFloat(*_input++);
  456. case 1: *packed++ = bx::halfFromFloat(*_input++);
  457. }
  458. }
  459. break;
  460. case AttribType::Float:
  461. memcpy(data, _input, num*sizeof(float) );
  462. break;
  463. }
  464. }
  465. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index)
  466. {
  467. if (!_decl.has(_attr) )
  468. {
  469. memset(_output, 0, 4*sizeof(float) );
  470. return;
  471. }
  472. uint32_t stride = _decl.getStride();
  473. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  474. uint8_t num;
  475. AttribType::Enum type;
  476. bool normalized;
  477. bool asInt;
  478. _decl.decode(_attr, num, type, normalized, asInt);
  479. switch (type)
  480. {
  481. default:
  482. case AttribType::Uint8:
  483. {
  484. uint8_t* packed = (uint8_t*)data;
  485. if (asInt)
  486. {
  487. switch (num)
  488. {
  489. default: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  490. case 3: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  491. case 2: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  492. case 1: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  493. }
  494. }
  495. else
  496. {
  497. switch (num)
  498. {
  499. default: *_output++ = float(*packed++)*1.0f/255.0f;
  500. case 3: *_output++ = float(*packed++)*1.0f/255.0f;
  501. case 2: *_output++ = float(*packed++)*1.0f/255.0f;
  502. case 1: *_output++ = float(*packed++)*1.0f/255.0f;
  503. }
  504. }
  505. }
  506. break;
  507. case AttribType::Uint10:
  508. {
  509. uint32_t packed = *(uint32_t*)data;
  510. if (asInt)
  511. {
  512. switch (num)
  513. {
  514. default:
  515. case 3: *_output++ = (float(packed & 0x3ff) - 512.0f)*1.0f/511.0f; packed >>= 10;
  516. case 2: *_output++ = (float(packed & 0x3ff) - 512.0f)*1.0f/511.0f; packed >>= 10;
  517. case 1: *_output++ = (float(packed & 0x3ff) - 512.0f)*1.0f/511.0f;
  518. }
  519. }
  520. else
  521. {
  522. switch (num)
  523. {
  524. default:
  525. case 3: *_output++ = float(packed & 0x3ff)*1.0f/1023.0f; packed >>= 10;
  526. case 2: *_output++ = float(packed & 0x3ff)*1.0f/1023.0f; packed >>= 10;
  527. case 1: *_output++ = float(packed & 0x3ff)*1.0f/1023.0f;
  528. }
  529. }
  530. }
  531. break;
  532. case AttribType::Int16:
  533. {
  534. int16_t* packed = (int16_t*)data;
  535. if (asInt)
  536. {
  537. switch (num)
  538. {
  539. default: *_output++ = float(*packed++)*1.0f/32767.0f;
  540. case 3: *_output++ = float(*packed++)*1.0f/32767.0f;
  541. case 2: *_output++ = float(*packed++)*1.0f/32767.0f;
  542. case 1: *_output++ = float(*packed++)*1.0f/32767.0f;
  543. }
  544. }
  545. else
  546. {
  547. switch (num)
  548. {
  549. default: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  550. case 3: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  551. case 2: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  552. case 1: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  553. }
  554. }
  555. }
  556. break;
  557. case AttribType::Half:
  558. {
  559. uint16_t* packed = (uint16_t*)data;
  560. switch (num)
  561. {
  562. default: *_output++ = bx::halfToFloat(*packed++);
  563. case 3: *_output++ = bx::halfToFloat(*packed++);
  564. case 2: *_output++ = bx::halfToFloat(*packed++);
  565. case 1: *_output++ = bx::halfToFloat(*packed++);
  566. }
  567. }
  568. break;
  569. case AttribType::Float:
  570. memcpy(_output, data, num*sizeof(float) );
  571. _output += num;
  572. break;
  573. }
  574. switch (num)
  575. {
  576. case 1: *_output++ = 0.0f;
  577. case 2: *_output++ = 0.0f;
  578. case 3: *_output++ = 0.0f;
  579. default: break;
  580. }
  581. }
  582. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num)
  583. {
  584. if (_destDecl.m_hash == _srcDecl.m_hash)
  585. {
  586. memcpy(_destData, _srcData, _srcDecl.getSize(_num) );
  587. return;
  588. }
  589. struct ConvertOp
  590. {
  591. enum Enum
  592. {
  593. Set,
  594. Copy,
  595. Convert,
  596. };
  597. Attrib::Enum attr;
  598. Enum op;
  599. uint32_t src;
  600. uint32_t dest;
  601. uint32_t size;
  602. };
  603. ConvertOp convertOp[Attrib::Count];
  604. uint32_t numOps = 0;
  605. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  606. {
  607. Attrib::Enum attr = (Attrib::Enum)ii;
  608. if (_destDecl.has(attr) )
  609. {
  610. ConvertOp& cop = convertOp[numOps];
  611. cop.attr = attr;
  612. cop.dest = _destDecl.getOffset(attr);
  613. uint8_t num;
  614. AttribType::Enum type;
  615. bool normalized;
  616. bool asInt;
  617. _destDecl.decode(attr, num, type, normalized, asInt);
  618. cop.size = (*s_attribTypeSize[0])[type][num-1];
  619. if (_srcDecl.has(attr) )
  620. {
  621. cop.src = _srcDecl.getOffset(attr);
  622. cop.op = _destDecl.m_attributes[attr] == _srcDecl.m_attributes[attr] ? ConvertOp::Copy : ConvertOp::Convert;
  623. }
  624. else
  625. {
  626. cop.op = ConvertOp::Set;
  627. }
  628. ++numOps;
  629. }
  630. }
  631. if (0 < numOps)
  632. {
  633. const uint8_t* src = (const uint8_t*)_srcData;
  634. uint32_t srcStride = _srcDecl.getStride();
  635. uint8_t* dest = (uint8_t*)_destData;
  636. uint32_t destStride = _destDecl.getStride();
  637. float unpacked[4];
  638. for (uint32_t ii = 0; ii < _num; ++ii)
  639. {
  640. for (uint32_t jj = 0; jj < numOps; ++jj)
  641. {
  642. const ConvertOp& cop = convertOp[jj];
  643. switch (cop.op)
  644. {
  645. case ConvertOp::Set:
  646. memset(dest + cop.dest, 0, cop.size);
  647. break;
  648. case ConvertOp::Copy:
  649. memcpy(dest + cop.dest, src + cop.src, cop.size);
  650. break;
  651. case ConvertOp::Convert:
  652. vertexUnpack(unpacked, cop.attr, _srcDecl, src);
  653. vertexPack(unpacked, true, cop.attr, _destDecl, dest);
  654. break;
  655. }
  656. }
  657. src += srcStride;
  658. dest += destStride;
  659. }
  660. }
  661. }
  662. inline float sqLength(const float _a[3], const float _b[3])
  663. {
  664. const float xx = _a[0] - _b[0];
  665. const float yy = _a[1] - _b[1];
  666. const float zz = _a[2] - _b[2];
  667. return xx*xx + yy*yy + zz*zz;
  668. }
  669. uint16_t weldVerticesRef(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon)
  670. {
  671. // Brute force slow vertex welding...
  672. const float epsilonSq = _epsilon*_epsilon;
  673. uint32_t numVertices = 0;
  674. memset(_output, 0xff, _num*sizeof(uint16_t) );
  675. for (uint32_t ii = 0; ii < _num; ++ii)
  676. {
  677. if (UINT16_MAX != _output[ii])
  678. {
  679. continue;
  680. }
  681. _output[ii] = (uint16_t)ii;
  682. ++numVertices;
  683. float pos[4];
  684. vertexUnpack(pos, Attrib::Position, _decl, _data, ii);
  685. for (uint32_t jj = 0; jj < _num; ++jj)
  686. {
  687. if (UINT16_MAX != _output[jj])
  688. {
  689. continue;
  690. }
  691. float test[4];
  692. vertexUnpack(test, Attrib::Position, _decl, _data, jj);
  693. if (sqLength(test, pos) < epsilonSq)
  694. {
  695. _output[jj] = (uint16_t)ii;
  696. }
  697. }
  698. }
  699. return (uint16_t)numVertices;
  700. }
  701. uint16_t weldVertices(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon)
  702. {
  703. const uint32_t hashSize = bx::uint32_nextpow2(_num);
  704. const uint32_t hashMask = hashSize-1;
  705. const float epsilonSq = _epsilon*_epsilon;
  706. uint32_t numVertices = 0;
  707. const uint32_t size = sizeof(uint16_t)*(hashSize + _num);
  708. uint16_t* hashTable = (uint16_t*)alloca(size);
  709. memset(hashTable, 0xff, size);
  710. uint16_t* next = hashTable + hashSize;
  711. for (uint32_t ii = 0; ii < _num; ++ii)
  712. {
  713. float pos[4];
  714. vertexUnpack(pos, Attrib::Position, _decl, _data, ii);
  715. uint32_t hashValue = bx::hashMurmur2A(pos, 3*sizeof(float) ) & hashMask;
  716. uint16_t offset = hashTable[hashValue];
  717. for (; UINT16_MAX != offset; offset = next[offset])
  718. {
  719. float test[4];
  720. vertexUnpack(test, Attrib::Position, _decl, _data, _output[offset]);
  721. if (sqLength(test, pos) < epsilonSq)
  722. {
  723. _output[ii] = _output[offset];
  724. break;
  725. }
  726. }
  727. if (UINT16_MAX == offset)
  728. {
  729. _output[ii] = (uint16_t)ii;
  730. next[ii] = hashTable[hashValue];
  731. hashTable[hashValue] = (uint16_t)ii;
  732. numVertices++;
  733. }
  734. }
  735. return (uint16_t)numVertices;
  736. }
  737. } // namespace bgfx