vertexdecl.cpp 20 KB

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