vertexdecl.cpp 18 KB

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