vertexdecl.cpp 20 KB

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