vertexdecl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 "vertexdecl.h"
  11. namespace bgfx
  12. {
  13. static const uint8_t s_attribTypeSizeDx9[AttribType::Count][4] =
  14. {
  15. { 4, 4, 4, 4 },
  16. { 4, 4, 8, 8 },
  17. { 4, 4, 8, 8 },
  18. { 4, 8, 12, 16 },
  19. };
  20. static const uint8_t s_attribTypeSizeDx11[AttribType::Count][4] =
  21. {
  22. { 1, 2, 4, 4 },
  23. { 2, 4, 8, 8 },
  24. { 2, 4, 8, 8 },
  25. { 4, 8, 12, 16 },
  26. };
  27. static const uint8_t s_attribTypeSizeGl[AttribType::Count][4] =
  28. {
  29. { 1, 2, 4, 4 },
  30. { 2, 4, 6, 8 },
  31. { 2, 4, 6, 8 },
  32. { 4, 8, 12, 16 },
  33. };
  34. static const uint8_t (*s_attribTypeSize[RendererType::Count])[AttribType::Count][4] =
  35. {
  36. #if BGFX_CONFIG_RENDERER_DIRECT3D9
  37. &s_attribTypeSizeDx9,
  38. #elif BGFX_CONFIG_RENDERER_DIRECT3D11
  39. &s_attribTypeSizeDx11,
  40. #elif BGFX_CONFIG_RENDERER_OPENGL|BGFX_CONFIG_RENDERER_OPENGLES2|BGFX_CONFIG_RENDERER_OPENGLES3
  41. &s_attribTypeSizeGl,
  42. #else
  43. &s_attribTypeSizeDx9,
  44. #endif // BGFX_CONFIG_RENDERER_
  45. &s_attribTypeSizeDx9,
  46. &s_attribTypeSizeDx11,
  47. &s_attribTypeSizeGl,
  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 BGFX_CONFIG_DEBUG
  129. dbgPrintf("vertexdecl %08x (%08x), stride %d\n"
  130. , _decl.m_hash
  131. , bx::hashMurmur2A(_decl.m_attributes)
  132. , _decl.m_stride
  133. );
  134. for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
  135. {
  136. if (0xff != _decl.m_attributes[attr])
  137. {
  138. uint8_t num;
  139. AttribType::Enum type;
  140. bool normalized;
  141. bool asInt;
  142. _decl.decode(Attrib::Enum(attr), num, type, normalized, asInt);
  143. dbgPrintf("\tattr %d - %s, num %d, type %d, norm %d, asint %d, offset %d\n"
  144. , attr
  145. , getAttribName(Attrib::Enum(attr) )
  146. , num
  147. , type
  148. , normalized
  149. , asInt
  150. , _decl.m_offset[attr]
  151. );
  152. }
  153. }
  154. #else
  155. BX_UNUSED(_decl);
  156. #endif // BGFX_CONFIG_DEBUG
  157. }
  158. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index)
  159. {
  160. if (!_decl.has(_attr) )
  161. {
  162. return;
  163. }
  164. uint32_t stride = _decl.getStride();
  165. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  166. uint8_t num;
  167. AttribType::Enum type;
  168. bool normalized;
  169. bool asInt;
  170. _decl.decode(_attr, num, type, normalized, asInt);
  171. switch (type)
  172. {
  173. default:
  174. case AttribType::Uint8:
  175. {
  176. uint8_t* packed = (uint8_t*)data;
  177. if (_inputNormalized)
  178. {
  179. if (asInt)
  180. {
  181. switch (num)
  182. {
  183. default: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  184. case 3: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  185. case 2: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  186. case 1: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  187. }
  188. }
  189. else
  190. {
  191. switch (num)
  192. {
  193. default: *packed++ = uint8_t(*_input++ * 255.0f);
  194. case 3: *packed++ = uint8_t(*_input++ * 255.0f);
  195. case 2: *packed++ = uint8_t(*_input++ * 255.0f);
  196. case 1: *packed++ = uint8_t(*_input++ * 255.0f);
  197. }
  198. }
  199. }
  200. else
  201. {
  202. switch (num)
  203. {
  204. default: *packed++ = uint8_t(*_input++);
  205. case 3: *packed++ = uint8_t(*_input++);
  206. case 2: *packed++ = uint8_t(*_input++);
  207. case 1: *packed++ = uint8_t(*_input++);
  208. }
  209. }
  210. }
  211. break;
  212. case AttribType::Int16:
  213. {
  214. int16_t* packed = (int16_t*)data;
  215. if (_inputNormalized)
  216. {
  217. if (asInt)
  218. {
  219. switch (num)
  220. {
  221. default: *packed++ = int16_t(*_input++ * 32767.0f);
  222. case 3: *packed++ = int16_t(*_input++ * 32767.0f);
  223. case 2: *packed++ = int16_t(*_input++ * 32767.0f);
  224. case 1: *packed++ = int16_t(*_input++ * 32767.0f);
  225. }
  226. }
  227. else
  228. {
  229. switch (num)
  230. {
  231. default: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  232. case 3: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  233. case 2: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  234. case 1: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  235. }
  236. }
  237. }
  238. else
  239. {
  240. switch (num)
  241. {
  242. default: *packed++ = int16_t(*_input++);
  243. case 3: *packed++ = int16_t(*_input++);
  244. case 2: *packed++ = int16_t(*_input++);
  245. case 1: *packed++ = int16_t(*_input++);
  246. }
  247. }
  248. }
  249. break;
  250. case AttribType::Half:
  251. {
  252. uint16_t* packed = (uint16_t*)data;
  253. switch (num)
  254. {
  255. default: *packed++ = bx::halfFromFloat(*_input++);
  256. case 3: *packed++ = bx::halfFromFloat(*_input++);
  257. case 2: *packed++ = bx::halfFromFloat(*_input++);
  258. case 1: *packed++ = bx::halfFromFloat(*_input++);
  259. }
  260. }
  261. break;
  262. case AttribType::Float:
  263. memcpy(data, _input, num*sizeof(float) );
  264. break;
  265. }
  266. }
  267. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index)
  268. {
  269. if (!_decl.has(_attr) )
  270. {
  271. memset(_output, 0, 4*sizeof(float) );
  272. return;
  273. }
  274. uint32_t stride = _decl.getStride();
  275. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  276. uint8_t num;
  277. AttribType::Enum type;
  278. bool normalized;
  279. bool asInt;
  280. _decl.decode(_attr, num, type, normalized, asInt);
  281. switch (type)
  282. {
  283. default:
  284. case AttribType::Uint8:
  285. {
  286. uint8_t* packed = (uint8_t*)data;
  287. if (asInt)
  288. {
  289. switch (num)
  290. {
  291. default: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  292. case 3: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  293. case 2: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  294. case 1: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  295. }
  296. }
  297. else
  298. {
  299. switch (num)
  300. {
  301. default: *_output++ = float(*packed++)*1.0f/255.0f;
  302. case 3: *_output++ = float(*packed++)*1.0f/255.0f;
  303. case 2: *_output++ = float(*packed++)*1.0f/255.0f;
  304. case 1: *_output++ = float(*packed++)*1.0f/255.0f;
  305. }
  306. }
  307. }
  308. break;
  309. case AttribType::Int16:
  310. {
  311. int16_t* packed = (int16_t*)data;
  312. if (asInt)
  313. {
  314. switch (num)
  315. {
  316. default: *_output++ = float(*packed++)*1.0f/32767.0f;
  317. case 3: *_output++ = float(*packed++)*1.0f/32767.0f;
  318. case 2: *_output++ = float(*packed++)*1.0f/32767.0f;
  319. case 1: *_output++ = float(*packed++)*1.0f/32767.0f;
  320. }
  321. }
  322. else
  323. {
  324. switch (num)
  325. {
  326. default: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  327. case 3: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  328. case 2: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  329. case 1: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  330. }
  331. }
  332. }
  333. break;
  334. case AttribType::Half:
  335. {
  336. uint16_t* packed = (uint16_t*)data;
  337. switch (num)
  338. {
  339. default: *_output++ = bx::halfToFloat(*packed++);
  340. case 3: *_output++ = bx::halfToFloat(*packed++);
  341. case 2: *_output++ = bx::halfToFloat(*packed++);
  342. case 1: *_output++ = bx::halfToFloat(*packed++);
  343. }
  344. }
  345. break;
  346. case AttribType::Float:
  347. memcpy(_output, data, num*sizeof(float) );
  348. _output += num;
  349. break;
  350. }
  351. switch (num)
  352. {
  353. case 1: *_output++ = 0.0f;
  354. case 2: *_output++ = 0.0f;
  355. case 3: *_output++ = 0.0f;
  356. default: break;
  357. }
  358. }
  359. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num)
  360. {
  361. if (_destDecl.m_hash == _srcDecl.m_hash)
  362. {
  363. memcpy(_destData, _srcData, _srcDecl.getSize(_num) );
  364. return;
  365. }
  366. struct ConvertOp
  367. {
  368. enum Enum
  369. {
  370. Set,
  371. Copy,
  372. Convert,
  373. };
  374. Attrib::Enum attr;
  375. Enum op;
  376. uint32_t src;
  377. uint32_t dest;
  378. uint32_t size;
  379. };
  380. ConvertOp convertOp[Attrib::Count];
  381. uint32_t numOps = 0;
  382. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  383. {
  384. Attrib::Enum attr = (Attrib::Enum)ii;
  385. if (_destDecl.has(attr) )
  386. {
  387. ConvertOp& cop = convertOp[numOps];
  388. cop.attr = attr;
  389. cop.dest = _destDecl.getOffset(attr);
  390. uint8_t num;
  391. AttribType::Enum type;
  392. bool normalized;
  393. bool asInt;
  394. _destDecl.decode(attr, num, type, normalized, asInt);
  395. cop.size = (*s_attribTypeSize[0])[type][num-1];
  396. if (_srcDecl.has(attr) )
  397. {
  398. cop.src = _srcDecl.getOffset(attr);
  399. cop.op = _destDecl.m_attributes[attr] == _srcDecl.m_attributes[attr] ? ConvertOp::Copy : ConvertOp::Convert;
  400. }
  401. else
  402. {
  403. cop.op = ConvertOp::Set;
  404. }
  405. ++numOps;
  406. }
  407. }
  408. if (0 < numOps)
  409. {
  410. const uint8_t* src = (const uint8_t*)_srcData;
  411. uint32_t srcStride = _srcDecl.getStride();
  412. uint8_t* dest = (uint8_t*)_destData;
  413. uint32_t destStride = _destDecl.getStride();
  414. float unpacked[4];
  415. for (uint32_t ii = 0; ii < _num; ++ii)
  416. {
  417. for (uint32_t jj = 0; jj < numOps; ++jj)
  418. {
  419. const ConvertOp& cop = convertOp[jj];
  420. switch (cop.op)
  421. {
  422. case ConvertOp::Set:
  423. memset(dest + cop.dest, 0, cop.size);
  424. break;
  425. case ConvertOp::Copy:
  426. memcpy(dest + cop.dest, src + cop.src, cop.size);
  427. break;
  428. case ConvertOp::Convert:
  429. vertexUnpack(unpacked, cop.attr, _srcDecl, src);
  430. vertexPack(unpacked, true, cop.attr, _destDecl, dest);
  431. break;
  432. }
  433. }
  434. src += srcStride;
  435. dest += destStride;
  436. }
  437. }
  438. }
  439. inline float sqLength(const float _a[3], const float _b[3])
  440. {
  441. const float xx = _a[0] - _b[0];
  442. const float yy = _a[1] - _b[1];
  443. const float zz = _a[2] - _b[2];
  444. return xx*xx + yy*yy + zz*zz;
  445. }
  446. uint16_t weldVerticesRef(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon)
  447. {
  448. // Brute force slow vertex welding...
  449. const float epsilonSq = _epsilon*_epsilon;
  450. uint32_t numVertices = 0;
  451. memset(_output, 0xff, _num*sizeof(uint16_t) );
  452. for (uint32_t ii = 0; ii < _num; ++ii)
  453. {
  454. if (UINT16_MAX != _output[ii])
  455. {
  456. continue;
  457. }
  458. _output[ii] = (uint16_t)ii;
  459. ++numVertices;
  460. float pos[4];
  461. vertexUnpack(pos, bgfx::Attrib::Position, _decl, _data, ii);
  462. for (uint32_t jj = 0; jj < _num; ++jj)
  463. {
  464. if (UINT16_MAX != _output[jj])
  465. {
  466. continue;
  467. }
  468. float test[4];
  469. vertexUnpack(test, bgfx::Attrib::Position, _decl, _data, jj);
  470. if (sqLength(test, pos) < epsilonSq)
  471. {
  472. _output[jj] = (uint16_t)ii;
  473. }
  474. }
  475. }
  476. return (uint16_t)numVertices;
  477. }
  478. uint16_t weldVertices(uint16_t* _output, const VertexDecl& _decl, const void* _data, uint16_t _num, float _epsilon)
  479. {
  480. const uint32_t hashSize = bx::uint32_nextpow2(_num);
  481. const uint32_t hashMask = hashSize-1;
  482. const float epsilonSq = _epsilon*_epsilon;
  483. uint32_t numVertices = 0;
  484. const uint32_t size = sizeof(uint16_t)*(hashSize + _num);
  485. uint16_t* hashTable = (uint16_t*)alloca(size);
  486. memset(hashTable, 0xff, size);
  487. uint16_t* next = hashTable + hashSize;
  488. for (uint32_t ii = 0; ii < _num; ++ii)
  489. {
  490. float pos[4];
  491. vertexUnpack(pos, bgfx::Attrib::Position, _decl, _data, ii);
  492. uint32_t hashValue = bx::hashMurmur2A(pos, 3*sizeof(float) ) & hashMask;
  493. uint16_t offset = hashTable[hashValue];
  494. for (; UINT16_MAX != offset; offset = next[offset])
  495. {
  496. float test[4];
  497. vertexUnpack(test, bgfx::Attrib::Position, _decl, _data, _output[offset]);
  498. if (sqLength(test, pos) < epsilonSq)
  499. {
  500. _output[ii] = _output[offset];
  501. break;
  502. }
  503. }
  504. if (UINT16_MAX == offset)
  505. {
  506. _output[ii] = (uint16_t)ii;
  507. next[ii] = hashTable[hashValue];
  508. hashTable[hashValue] = (uint16_t)ii;
  509. numVertices++;
  510. }
  511. }
  512. return (uint16_t)numVertices;
  513. }
  514. } // namespace bgfx