vertexdecl.cpp 20 KB

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