vertexdecl.cpp 11 KB

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