vertexdecl.cpp 20 KB

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