vertexdecl.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <string.h>
  6. #include <bx/debug.h>
  7. #include <bx/hash.h>
  8. #include <bx/uint32_t.h>
  9. #include <bx/string.h>
  10. #include "config.h"
  11. #include "vertexdecl.h"
  12. namespace bgfx
  13. {
  14. static const uint8_t s_attribTypeSizeDx9[AttribType::Count][4] =
  15. {
  16. { 4, 4, 4, 4 },
  17. { 4, 4, 8, 8 },
  18. { 4, 4, 8, 8 },
  19. { 4, 8, 12, 16 },
  20. };
  21. static const uint8_t s_attribTypeSizeDx11[AttribType::Count][4] =
  22. {
  23. { 1, 2, 4, 4 },
  24. { 2, 4, 8, 8 },
  25. { 2, 4, 8, 8 },
  26. { 4, 8, 12, 16 },
  27. };
  28. static const uint8_t s_attribTypeSizeGl[AttribType::Count][4] =
  29. {
  30. { 1, 2, 4, 4 },
  31. { 2, 4, 6, 8 },
  32. { 2, 4, 6, 8 },
  33. { 4, 8, 12, 16 },
  34. };
  35. static const uint8_t (*s_attribTypeSize[RendererType::Count])[AttribType::Count][4] =
  36. {
  37. #if BGFX_CONFIG_RENDERER_DIRECT3D9
  38. &s_attribTypeSizeDx9,
  39. #elif BGFX_CONFIG_RENDERER_DIRECT3D11
  40. &s_attribTypeSizeDx11,
  41. #elif BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES
  42. &s_attribTypeSizeGl,
  43. #else
  44. &s_attribTypeSizeDx9,
  45. #endif // BGFX_CONFIG_RENDERER_
  46. &s_attribTypeSizeDx9,
  47. &s_attribTypeSizeDx11,
  48. &s_attribTypeSizeGl,
  49. &s_attribTypeSizeGl,
  50. };
  51. void initAttribTypeSizeTable(RendererType::Enum _type)
  52. {
  53. s_attribTypeSize[0] = s_attribTypeSize[_type];
  54. }
  55. void dbgPrintfVargs(const char* _format, va_list _argList)
  56. {
  57. char temp[8192];
  58. char* out = temp;
  59. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  60. if ( (int32_t)sizeof(temp) < len)
  61. {
  62. out = (char*)alloca(len+1);
  63. len = bx::vsnprintf(out, len, _format, _argList);
  64. }
  65. out[len] = '\0';
  66. bx::debugOutput(out);
  67. }
  68. void dbgPrintf(const char* _format, ...)
  69. {
  70. va_list argList;
  71. va_start(argList, _format);
  72. dbgPrintfVargs(_format, argList);
  73. va_end(argList);
  74. }
  75. VertexDecl& VertexDecl::begin(RendererType::Enum _renderer)
  76. {
  77. m_hash = _renderer; // use hash to store renderer type while building VertexDecl.
  78. m_stride = 0;
  79. memset(m_attributes, 0xff, sizeof(m_attributes) );
  80. memset(m_offset, 0, sizeof(m_offset) );
  81. return *this;
  82. }
  83. void VertexDecl::end()
  84. {
  85. m_hash = bx::hashMurmur2A(m_attributes);
  86. }
  87. VertexDecl& VertexDecl::add(Attrib::Enum _attrib, uint8_t _num, AttribType::Enum _type, bool _normalized, bool _asInt)
  88. {
  89. const uint8_t encodedNorm = (_normalized&1)<<6;
  90. const uint8_t encodedType = (_type&3)<<3;
  91. const uint8_t encodedNum = (_num-1)&3;
  92. const uint8_t encodeAsInt = (_asInt&(!!"\x1\x1\x0\x0"[_type]) )<<7;
  93. m_attributes[_attrib] = encodedNorm|encodedType|encodedNum|encodeAsInt;
  94. m_offset[_attrib] = m_stride;
  95. m_stride += (*s_attribTypeSize[m_hash])[_type][_num-1];
  96. return *this;
  97. }
  98. VertexDecl& VertexDecl::skip(uint8_t _num)
  99. {
  100. m_stride += _num;
  101. return *this;
  102. }
  103. void VertexDecl::decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const
  104. {
  105. uint8_t val = m_attributes[_attrib];
  106. _num = (val&3)+1;
  107. _type = AttribType::Enum((val>>3)&3);
  108. _normalized = !!(val&(1<<6) );
  109. _asInt = !!(val&(1<<7) );
  110. }
  111. static const char* s_attrName[Attrib::Count] =
  112. {
  113. "Attrib::Position",
  114. "Attrib::Normal",
  115. "Attrib::Tangent",
  116. "Attrib::Color0",
  117. "Attrib::Color1",
  118. "Attrib::Indices",
  119. "Attrib::Weights",
  120. "Attrib::TexCoord0",
  121. "Attrib::TexCoord1",
  122. "Attrib::TexCoord2",
  123. "Attrib::TexCoord3",
  124. "Attrib::TexCoord4",
  125. "Attrib::TexCoord5",
  126. "Attrib::TexCoord6",
  127. "Attrib::TexCoord7",
  128. };
  129. const char* getAttribName(Attrib::Enum _attr)
  130. {
  131. return s_attrName[_attr];
  132. }
  133. void dump(const VertexDecl& _decl)
  134. {
  135. if (BX_ENABLED(BGFX_CONFIG_DEBUG) )
  136. {
  137. dbgPrintf("vertexdecl %08x (%08x), stride %d\n"
  138. , _decl.m_hash
  139. , bx::hashMurmur2A(_decl.m_attributes)
  140. , _decl.m_stride
  141. );
  142. for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
  143. {
  144. if (0xff != _decl.m_attributes[attr])
  145. {
  146. uint8_t num;
  147. AttribType::Enum type;
  148. bool normalized;
  149. bool asInt;
  150. _decl.decode(Attrib::Enum(attr), num, type, normalized, asInt);
  151. dbgPrintf("\tattr %d - %s, num %d, type %d, norm %d, asint %d, offset %d\n"
  152. , attr
  153. , getAttribName(Attrib::Enum(attr) )
  154. , num
  155. , type
  156. , normalized
  157. , asInt
  158. , _decl.m_offset[attr]
  159. );
  160. }
  161. }
  162. }
  163. }
  164. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index)
  165. {
  166. if (!_decl.has(_attr) )
  167. {
  168. return;
  169. }
  170. uint32_t stride = _decl.getStride();
  171. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  172. uint8_t num;
  173. AttribType::Enum type;
  174. bool normalized;
  175. bool asInt;
  176. _decl.decode(_attr, num, type, normalized, asInt);
  177. switch (type)
  178. {
  179. default:
  180. case AttribType::Uint8:
  181. {
  182. uint8_t* packed = (uint8_t*)data;
  183. if (_inputNormalized)
  184. {
  185. if (asInt)
  186. {
  187. switch (num)
  188. {
  189. default: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  190. case 3: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  191. case 2: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  192. case 1: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  193. }
  194. }
  195. else
  196. {
  197. switch (num)
  198. {
  199. default: *packed++ = uint8_t(*_input++ * 255.0f);
  200. case 3: *packed++ = uint8_t(*_input++ * 255.0f);
  201. case 2: *packed++ = uint8_t(*_input++ * 255.0f);
  202. case 1: *packed++ = uint8_t(*_input++ * 255.0f);
  203. }
  204. }
  205. }
  206. else
  207. {
  208. switch (num)
  209. {
  210. default: *packed++ = uint8_t(*_input++);
  211. case 3: *packed++ = uint8_t(*_input++);
  212. case 2: *packed++ = uint8_t(*_input++);
  213. case 1: *packed++ = uint8_t(*_input++);
  214. }
  215. }
  216. }
  217. break;
  218. case AttribType::Int16:
  219. {
  220. int16_t* packed = (int16_t*)data;
  221. if (_inputNormalized)
  222. {
  223. if (asInt)
  224. {
  225. switch (num)
  226. {
  227. default: *packed++ = int16_t(*_input++ * 32767.0f);
  228. case 3: *packed++ = int16_t(*_input++ * 32767.0f);
  229. case 2: *packed++ = int16_t(*_input++ * 32767.0f);
  230. case 1: *packed++ = int16_t(*_input++ * 32767.0f);
  231. }
  232. }
  233. else
  234. {
  235. switch (num)
  236. {
  237. default: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  238. case 3: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  239. case 2: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  240. case 1: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  241. }
  242. }
  243. }
  244. else
  245. {
  246. switch (num)
  247. {
  248. default: *packed++ = int16_t(*_input++);
  249. case 3: *packed++ = int16_t(*_input++);
  250. case 2: *packed++ = int16_t(*_input++);
  251. case 1: *packed++ = int16_t(*_input++);
  252. }
  253. }
  254. }
  255. break;
  256. case AttribType::Half:
  257. {
  258. uint16_t* packed = (uint16_t*)data;
  259. switch (num)
  260. {
  261. default: *packed++ = bx::halfFromFloat(*_input++);
  262. case 3: *packed++ = bx::halfFromFloat(*_input++);
  263. case 2: *packed++ = bx::halfFromFloat(*_input++);
  264. case 1: *packed++ = bx::halfFromFloat(*_input++);
  265. }
  266. }
  267. break;
  268. case AttribType::Float:
  269. memcpy(data, _input, num*sizeof(float) );
  270. break;
  271. }
  272. }
  273. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index)
  274. {
  275. if (!_decl.has(_attr) )
  276. {
  277. memset(_output, 0, 4*sizeof(float) );
  278. return;
  279. }
  280. uint32_t stride = _decl.getStride();
  281. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  282. uint8_t num;
  283. AttribType::Enum type;
  284. bool normalized;
  285. bool asInt;
  286. _decl.decode(_attr, num, type, normalized, asInt);
  287. switch (type)
  288. {
  289. default:
  290. case AttribType::Uint8:
  291. {
  292. uint8_t* packed = (uint8_t*)data;
  293. if (asInt)
  294. {
  295. switch (num)
  296. {
  297. default: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  298. case 3: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  299. case 2: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  300. case 1: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  301. }
  302. }
  303. else
  304. {
  305. switch (num)
  306. {
  307. default: *_output++ = float(*packed++)*1.0f/255.0f;
  308. case 3: *_output++ = float(*packed++)*1.0f/255.0f;
  309. case 2: *_output++ = float(*packed++)*1.0f/255.0f;
  310. case 1: *_output++ = float(*packed++)*1.0f/255.0f;
  311. }
  312. }
  313. }
  314. break;
  315. case AttribType::Int16:
  316. {
  317. int16_t* packed = (int16_t*)data;
  318. if (asInt)
  319. {
  320. switch (num)
  321. {
  322. default: *_output++ = float(*packed++)*1.0f/32767.0f;
  323. case 3: *_output++ = float(*packed++)*1.0f/32767.0f;
  324. case 2: *_output++ = float(*packed++)*1.0f/32767.0f;
  325. case 1: *_output++ = float(*packed++)*1.0f/32767.0f;
  326. }
  327. }
  328. else
  329. {
  330. switch (num)
  331. {
  332. default: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  333. case 3: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  334. case 2: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  335. case 1: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  336. }
  337. }
  338. }
  339. break;
  340. case AttribType::Half:
  341. {
  342. uint16_t* packed = (uint16_t*)data;
  343. switch (num)
  344. {
  345. default: *_output++ = bx::halfToFloat(*packed++);
  346. case 3: *_output++ = bx::halfToFloat(*packed++);
  347. case 2: *_output++ = bx::halfToFloat(*packed++);
  348. case 1: *_output++ = bx::halfToFloat(*packed++);
  349. }
  350. }
  351. break;
  352. case AttribType::Float:
  353. memcpy(_output, data, num*sizeof(float) );
  354. _output += num;
  355. break;
  356. }
  357. switch (num)
  358. {
  359. case 1: *_output++ = 0.0f;
  360. case 2: *_output++ = 0.0f;
  361. case 3: *_output++ = 0.0f;
  362. default: break;
  363. }
  364. }
  365. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num)
  366. {
  367. if (_destDecl.m_hash == _srcDecl.m_hash)
  368. {
  369. memcpy(_destData, _srcData, _srcDecl.getSize(_num) );
  370. return;
  371. }
  372. struct ConvertOp
  373. {
  374. enum Enum
  375. {
  376. Set,
  377. Copy,
  378. Convert,
  379. };
  380. Attrib::Enum attr;
  381. Enum op;
  382. uint32_t src;
  383. uint32_t dest;
  384. uint32_t size;
  385. };
  386. ConvertOp convertOp[Attrib::Count];
  387. uint32_t numOps = 0;
  388. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  389. {
  390. Attrib::Enum attr = (Attrib::Enum)ii;
  391. if (_destDecl.has(attr) )
  392. {
  393. ConvertOp& cop = convertOp[numOps];
  394. cop.attr = attr;
  395. cop.dest = _destDecl.getOffset(attr);
  396. uint8_t num;
  397. AttribType::Enum type;
  398. bool normalized;
  399. bool asInt;
  400. _destDecl.decode(attr, num, type, normalized, asInt);
  401. cop.size = (*s_attribTypeSize[0])[type][num-1];
  402. if (_srcDecl.has(attr) )
  403. {
  404. cop.src = _srcDecl.getOffset(attr);
  405. cop.op = _destDecl.m_attributes[attr] == _srcDecl.m_attributes[attr] ? ConvertOp::Copy : ConvertOp::Convert;
  406. }
  407. else
  408. {
  409. cop.op = ConvertOp::Set;
  410. }
  411. ++numOps;
  412. }
  413. }
  414. if (0 < numOps)
  415. {
  416. const uint8_t* src = (const uint8_t*)_srcData;
  417. uint32_t srcStride = _srcDecl.getStride();
  418. uint8_t* dest = (uint8_t*)_destData;
  419. uint32_t destStride = _destDecl.getStride();
  420. float unpacked[4];
  421. for (uint32_t ii = 0; ii < _num; ++ii)
  422. {
  423. for (uint32_t jj = 0; jj < numOps; ++jj)
  424. {
  425. const ConvertOp& cop = convertOp[jj];
  426. switch (cop.op)
  427. {
  428. case ConvertOp::Set:
  429. memset(dest + cop.dest, 0, cop.size);
  430. break;
  431. case ConvertOp::Copy:
  432. memcpy(dest + cop.dest, src + cop.src, cop.size);
  433. break;
  434. case ConvertOp::Convert:
  435. vertexUnpack(unpacked, cop.attr, _srcDecl, src);
  436. vertexPack(unpacked, true, cop.attr, _destDecl, dest);
  437. break;
  438. }
  439. }
  440. src += srcStride;
  441. dest += destStride;
  442. }
  443. }
  444. }
  445. inline float sqLength(const float _a[3], const float _b[3])
  446. {
  447. const float xx = _a[0] - _b[0];
  448. const float yy = _a[1] - _b[1];
  449. const float zz = _a[2] - _b[2];
  450. return xx*xx + yy*yy + zz*zz;
  451. }
  452. uint16_t weldVerticesRef(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon)
  453. {
  454. // Brute force slow vertex welding...
  455. const float epsilonSq = _epsilon*_epsilon;
  456. uint32_t numVertices = 0;
  457. memset(_output, 0xff, _num*sizeof(uint16_t) );
  458. for (uint32_t ii = 0; ii < _num; ++ii)
  459. {
  460. if (UINT16_MAX != _output[ii])
  461. {
  462. continue;
  463. }
  464. _output[ii] = (uint16_t)ii;
  465. ++numVertices;
  466. float pos[4];
  467. vertexUnpack(pos, bgfx::Attrib::Position, _decl, _data, ii);
  468. for (uint32_t jj = 0; jj < _num; ++jj)
  469. {
  470. if (UINT16_MAX != _output[jj])
  471. {
  472. continue;
  473. }
  474. float test[4];
  475. vertexUnpack(test, bgfx::Attrib::Position, _decl, _data, jj);
  476. if (sqLength(test, pos) < epsilonSq)
  477. {
  478. _output[jj] = (uint16_t)ii;
  479. }
  480. }
  481. }
  482. return (uint16_t)numVertices;
  483. }
  484. uint16_t weldVertices(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon)
  485. {
  486. const uint32_t hashSize = bx::uint32_nextpow2(_num);
  487. const uint32_t hashMask = hashSize-1;
  488. const float epsilonSq = _epsilon*_epsilon;
  489. uint32_t numVertices = 0;
  490. const uint32_t size = sizeof(uint16_t)*(hashSize + _num);
  491. uint16_t* hashTable = (uint16_t*)alloca(size);
  492. memset(hashTable, 0xff, size);
  493. uint16_t* next = hashTable + hashSize;
  494. for (uint32_t ii = 0; ii < _num; ++ii)
  495. {
  496. float pos[4];
  497. vertexUnpack(pos, bgfx::Attrib::Position, _decl, _data, ii);
  498. uint32_t hashValue = bx::hashMurmur2A(pos, 3*sizeof(float) ) & hashMask;
  499. uint16_t offset = hashTable[hashValue];
  500. for (; UINT16_MAX != offset; offset = next[offset])
  501. {
  502. float test[4];
  503. vertexUnpack(test, bgfx::Attrib::Position, _decl, _data, _output[offset]);
  504. if (sqLength(test, pos) < epsilonSq)
  505. {
  506. _output[ii] = _output[offset];
  507. break;
  508. }
  509. }
  510. if (UINT16_MAX == offset)
  511. {
  512. _output[ii] = (uint16_t)ii;
  513. next[ii] = hashTable[hashValue];
  514. hashTable[hashValue] = (uint16_t)ii;
  515. numVertices++;
  516. }
  517. }
  518. return (uint16_t)numVertices;
  519. }
  520. } // namespace bgfx