vertexdecl.cpp 13 KB

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