vertexlayout.cpp 21 KB

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