vertexdecl.cpp 20 KB

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