vertexdecl.cpp 21 KB

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