vertexdecl.cpp 20 KB

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