vertexdecl.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Copyright 2011-2013 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::decode(Attrib::Enum _attrib, uint8_t& _num, AttribType::Enum& _type, bool& _normalized, bool& _asInt) const
  93. {
  94. uint8_t val = m_attributes[_attrib];
  95. _num = (val&3)+1;
  96. _type = AttribType::Enum((val>>3)&3);
  97. _normalized = !!(val&(1<<6) );
  98. _asInt = !!(val&(1<<7) );
  99. }
  100. static const char* s_attrName[Attrib::Count] =
  101. {
  102. "Attrib::Position",
  103. "Attrib::Normal",
  104. "Attrib::Tangent",
  105. "Attrib::Color0",
  106. "Attrib::Color1",
  107. "Attrib::Indices",
  108. "Attrib::Weights",
  109. "Attrib::TexCoord0",
  110. "Attrib::TexCoord1",
  111. "Attrib::TexCoord2",
  112. "Attrib::TexCoord3",
  113. "Attrib::TexCoord4",
  114. "Attrib::TexCoord5",
  115. "Attrib::TexCoord6",
  116. "Attrib::TexCoord7",
  117. };
  118. const char* getAttribName(Attrib::Enum _attr)
  119. {
  120. return s_attrName[_attr];
  121. }
  122. void dump(const VertexDecl& _decl)
  123. {
  124. #if BGFX_CONFIG_DEBUG
  125. dbgPrintf("vertexdecl %08x (%08x), stride %d\n"
  126. , _decl.m_hash
  127. , bx::hashMurmur2A(_decl.m_attributes)
  128. , _decl.m_stride
  129. );
  130. for (uint32_t attr = 0; attr < Attrib::Count; ++attr)
  131. {
  132. if (0xff != _decl.m_attributes[attr])
  133. {
  134. uint8_t num;
  135. AttribType::Enum type;
  136. bool normalized;
  137. bool asInt;
  138. _decl.decode(Attrib::Enum(attr), num, type, normalized, asInt);
  139. dbgPrintf("\tattr %d - %s, num %d, type %d, norm %d, asint %d, offset %d\n"
  140. , attr
  141. , getAttribName(Attrib::Enum(attr) )
  142. , num
  143. , type
  144. , normalized
  145. , asInt
  146. , _decl.m_offset[attr]
  147. );
  148. }
  149. }
  150. #else
  151. BX_UNUSED(_decl);
  152. #endif // BGFX_CONFIG_DEBUG
  153. }
  154. void vertexPack(const float _input[4], bool _inputNormalized, Attrib::Enum _attr, const VertexDecl& _decl, void* _data, uint32_t _index)
  155. {
  156. if (!_decl.has(_attr) )
  157. {
  158. return;
  159. }
  160. uint32_t stride = _decl.getStride();
  161. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  162. uint8_t num;
  163. AttribType::Enum type;
  164. bool normalized;
  165. bool asInt;
  166. _decl.decode(_attr, num, type, normalized, asInt);
  167. switch (type)
  168. {
  169. default:
  170. case AttribType::Uint8:
  171. {
  172. uint8_t* packed = (uint8_t*)data;
  173. if (_inputNormalized)
  174. {
  175. if (asInt)
  176. {
  177. switch (num)
  178. {
  179. default: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  180. case 3: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  181. case 2: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  182. case 1: *packed++ = uint8_t(*_input++ * 127.0f + 128.0f);
  183. }
  184. }
  185. else
  186. {
  187. switch (num)
  188. {
  189. default: *packed++ = uint8_t(*_input++ * 255.0f);
  190. case 3: *packed++ = uint8_t(*_input++ * 255.0f);
  191. case 2: *packed++ = uint8_t(*_input++ * 255.0f);
  192. case 1: *packed++ = uint8_t(*_input++ * 255.0f);
  193. }
  194. }
  195. }
  196. else
  197. {
  198. switch (num)
  199. {
  200. default: *packed++ = uint8_t(*_input++);
  201. case 3: *packed++ = uint8_t(*_input++);
  202. case 2: *packed++ = uint8_t(*_input++);
  203. case 1: *packed++ = uint8_t(*_input++);
  204. }
  205. }
  206. }
  207. break;
  208. case AttribType::Int16:
  209. {
  210. int16_t* packed = (int16_t*)data;
  211. if (_inputNormalized)
  212. {
  213. if (asInt)
  214. {
  215. switch (num)
  216. {
  217. default: *packed++ = int16_t(*_input++ * 32767.0f);
  218. case 3: *packed++ = int16_t(*_input++ * 32767.0f);
  219. case 2: *packed++ = int16_t(*_input++ * 32767.0f);
  220. case 1: *packed++ = int16_t(*_input++ * 32767.0f);
  221. }
  222. }
  223. else
  224. {
  225. switch (num)
  226. {
  227. default: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  228. case 3: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  229. case 2: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  230. case 1: *packed++ = int16_t(*_input++ * 65535.0f - 32768.0f);
  231. }
  232. }
  233. }
  234. else
  235. {
  236. switch (num)
  237. {
  238. default: *packed++ = int16_t(*_input++);
  239. case 3: *packed++ = int16_t(*_input++);
  240. case 2: *packed++ = int16_t(*_input++);
  241. case 1: *packed++ = int16_t(*_input++);
  242. }
  243. }
  244. }
  245. break;
  246. case AttribType::Half:
  247. {
  248. uint16_t* packed = (uint16_t*)data;
  249. switch (num)
  250. {
  251. default: *packed++ = bx::halfFromFloat(*_input++);
  252. case 3: *packed++ = bx::halfFromFloat(*_input++);
  253. case 2: *packed++ = bx::halfFromFloat(*_input++);
  254. case 1: *packed++ = bx::halfFromFloat(*_input++);
  255. }
  256. }
  257. break;
  258. case AttribType::Float:
  259. memcpy(data, _input, num*sizeof(float) );
  260. break;
  261. }
  262. }
  263. void vertexUnpack(float _output[4], Attrib::Enum _attr, const VertexDecl& _decl, const void* _data, uint32_t _index)
  264. {
  265. if (!_decl.has(_attr) )
  266. {
  267. memset(_output, 0, 4*sizeof(float) );
  268. return;
  269. }
  270. uint32_t stride = _decl.getStride();
  271. uint8_t* data = (uint8_t*)_data + _index*stride + _decl.getOffset(_attr);
  272. uint8_t num;
  273. AttribType::Enum type;
  274. bool normalized;
  275. bool asInt;
  276. _decl.decode(_attr, num, type, normalized, asInt);
  277. switch (type)
  278. {
  279. default:
  280. case AttribType::Uint8:
  281. {
  282. uint8_t* packed = (uint8_t*)data;
  283. if (asInt)
  284. {
  285. switch (num)
  286. {
  287. default: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  288. case 3: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  289. case 2: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  290. case 1: *_output++ = (float(*packed++) - 128.0f)*1.0f/127.0f;
  291. }
  292. }
  293. else
  294. {
  295. switch (num)
  296. {
  297. default: *_output++ = float(*packed++)*1.0f/255.0f;
  298. case 3: *_output++ = float(*packed++)*1.0f/255.0f;
  299. case 2: *_output++ = float(*packed++)*1.0f/255.0f;
  300. case 1: *_output++ = float(*packed++)*1.0f/255.0f;
  301. }
  302. }
  303. }
  304. break;
  305. case AttribType::Int16:
  306. {
  307. int16_t* packed = (int16_t*)data;
  308. if (asInt)
  309. {
  310. switch (num)
  311. {
  312. default: *_output++ = float(*packed++)*1.0f/32767.0f;
  313. case 3: *_output++ = float(*packed++)*1.0f/32767.0f;
  314. case 2: *_output++ = float(*packed++)*1.0f/32767.0f;
  315. case 1: *_output++ = float(*packed++)*1.0f/32767.0f;
  316. }
  317. }
  318. else
  319. {
  320. switch (num)
  321. {
  322. default: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  323. case 3: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  324. case 2: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  325. case 1: *_output++ = (float(*packed++) + 32768.0f)*1.0f/65535.0f;
  326. }
  327. }
  328. }
  329. break;
  330. case AttribType::Half:
  331. {
  332. uint16_t* packed = (uint16_t*)data;
  333. switch (num)
  334. {
  335. default: *_output++ = bx::halfToFloat(*packed++);
  336. case 3: *_output++ = bx::halfToFloat(*packed++);
  337. case 2: *_output++ = bx::halfToFloat(*packed++);
  338. case 1: *_output++ = bx::halfToFloat(*packed++);
  339. }
  340. }
  341. break;
  342. case AttribType::Float:
  343. memcpy(_output, data, num*sizeof(float) );
  344. _output += num;
  345. break;
  346. }
  347. switch (num)
  348. {
  349. case 1: *_output++ = 0.0f;
  350. case 2: *_output++ = 0.0f;
  351. case 3: *_output++ = 0.0f;
  352. default: break;
  353. }
  354. }
  355. void vertexConvert(const VertexDecl& _destDecl, void* _destData, const VertexDecl& _srcDecl, const void* _srcData, uint32_t _num)
  356. {
  357. if (_destDecl.m_hash == _srcDecl.m_hash)
  358. {
  359. memcpy(_destData, _srcData, _srcDecl.getSize(_num) );
  360. return;
  361. }
  362. struct ConvertOp
  363. {
  364. enum Enum
  365. {
  366. Set,
  367. Copy,
  368. Convert,
  369. };
  370. Attrib::Enum attr;
  371. Enum op;
  372. uint32_t src;
  373. uint32_t dest;
  374. uint32_t size;
  375. };
  376. ConvertOp convertOp[Attrib::Count];
  377. uint32_t numOps = 0;
  378. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  379. {
  380. Attrib::Enum attr = (Attrib::Enum)ii;
  381. if (_destDecl.has(attr) )
  382. {
  383. ConvertOp& cop = convertOp[numOps];
  384. cop.attr = attr;
  385. cop.dest = _destDecl.getOffset(attr);
  386. uint8_t num;
  387. AttribType::Enum type;
  388. bool normalized;
  389. bool asInt;
  390. _destDecl.decode(attr, num, type, normalized, asInt);
  391. cop.size = (*s_attribTypeSize[0])[type][num-1];
  392. if (_srcDecl.has(attr) )
  393. {
  394. cop.src = _srcDecl.getOffset(attr);
  395. cop.op = _destDecl.m_attributes[attr] == _srcDecl.m_attributes[attr] ? ConvertOp::Copy : ConvertOp::Convert;
  396. }
  397. else
  398. {
  399. cop.op = ConvertOp::Set;
  400. }
  401. ++numOps;
  402. }
  403. }
  404. if (0 < numOps)
  405. {
  406. const uint8_t* src = (const uint8_t*)_srcData;
  407. uint32_t srcStride = _srcDecl.getStride();
  408. uint8_t* dest = (uint8_t*)_destData;
  409. uint32_t destStride = _destDecl.getStride();
  410. float unpacked[4];
  411. for (uint32_t ii = 0; ii < _num; ++ii)
  412. {
  413. for (uint32_t jj = 0; jj < numOps; ++jj)
  414. {
  415. const ConvertOp& cop = convertOp[jj];
  416. switch (cop.op)
  417. {
  418. case ConvertOp::Set:
  419. memset(dest + cop.dest, 0, cop.size);
  420. break;
  421. case ConvertOp::Copy:
  422. memcpy(dest + cop.dest, src + cop.src, cop.size);
  423. break;
  424. case ConvertOp::Convert:
  425. vertexUnpack(unpacked, cop.attr, _srcDecl, src);
  426. vertexPack(unpacked, true, cop.attr, _destDecl, dest);
  427. break;
  428. }
  429. }
  430. src += srcStride;
  431. dest += destStride;
  432. }
  433. }
  434. }
  435. } // namespace bgfx