vertexlayout.cpp 21 KB

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