vertexdecl.cpp 11 KB

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