debugdraw.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include "debugdraw.h"
  7. #include <bx/fpumath.h>
  8. #include <bx/radixsort.h>
  9. #include <bx/uint32_t.h>
  10. #include <bx/crtimpl.h>
  11. struct DebugVertex
  12. {
  13. float m_x;
  14. float m_y;
  15. float m_z;
  16. float m_len;
  17. uint32_t m_abgr;
  18. static void init()
  19. {
  20. ms_decl
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::TexCoord0, 1, bgfx::AttribType::Float)
  24. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  25. .end();
  26. }
  27. static bgfx::VertexDecl ms_decl;
  28. };
  29. bgfx::VertexDecl DebugVertex::ms_decl;
  30. struct DebugShapeVertex
  31. {
  32. float m_x;
  33. float m_y;
  34. float m_z;
  35. float m_mask;
  36. static void init()
  37. {
  38. ms_decl
  39. .begin()
  40. .add(bgfx::Attrib::Position, 4, bgfx::AttribType::Float)
  41. .end();
  42. }
  43. static bgfx::VertexDecl ms_decl;
  44. };
  45. bgfx::VertexDecl DebugShapeVertex::ms_decl;
  46. static DebugShapeVertex s_cubeVertices[8] =
  47. {
  48. {-1.0f, 1.0f, 1.0f, 0.0f },
  49. { 1.0f, 1.0f, 1.0f, 0.0f },
  50. {-1.0f, -1.0f, 1.0f, 0.0f },
  51. { 1.0f, -1.0f, 1.0f, 0.0f },
  52. {-1.0f, 1.0f, -1.0f, 0.0f },
  53. { 1.0f, 1.0f, -1.0f, 0.0f },
  54. {-1.0f, -1.0f, -1.0f, 0.0f },
  55. { 1.0f, -1.0f, -1.0f, 0.0f },
  56. };
  57. static const uint16_t s_cubeIndices[36] =
  58. {
  59. 0, 1, 2, // 0
  60. 1, 3, 2,
  61. 4, 6, 5, // 2
  62. 5, 6, 7,
  63. 0, 2, 4, // 4
  64. 4, 2, 6,
  65. 1, 5, 3, // 6
  66. 5, 7, 3,
  67. 0, 4, 1, // 8
  68. 4, 5, 1,
  69. 2, 3, 6, // 10
  70. 6, 3, 7,
  71. };
  72. static const uint8_t s_circleLod[] =
  73. {
  74. 37,
  75. 29,
  76. 23,
  77. 17,
  78. 11,
  79. };
  80. static uint8_t getCircleLod(uint8_t _lod)
  81. {
  82. _lod = _lod > BX_COUNTOF(s_circleLod)-1 ? BX_COUNTOF(s_circleLod)-1 : _lod;
  83. return s_circleLod[_lod];
  84. }
  85. static void circle(float* _out, float _angle)
  86. {
  87. float sa = bx::fsin(_angle);
  88. float ca = bx::fcos(_angle);
  89. _out[0] = sa;
  90. _out[1] = ca;
  91. }
  92. static void squircle(float* _out, float _angle)
  93. {
  94. float sa = bx::fsin(_angle);
  95. float ca = bx::fcos(_angle);
  96. _out[0] = bx::fsqrt(bx::fabsolute(sa) ) * bx::fsign(sa);
  97. _out[1] = bx::fsqrt(bx::fabsolute(ca) ) * bx::fsign(ca);
  98. }
  99. uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0)
  100. {
  101. if (NULL != _pos0)
  102. {
  103. struct Gen
  104. {
  105. Gen(void* _pos, uint16_t _posStride, void* _normals, uint16_t _normalStride, uint8_t _subdiv)
  106. : m_pos( (uint8_t*)_pos)
  107. , m_normals( (uint8_t*)_normals)
  108. , m_posStride(_posStride)
  109. , m_normalStride(_normalStride)
  110. {
  111. static const float scale = 1.0f;
  112. static const float golden = 1.6180339887f;
  113. static const float len = bx::fsqrt(golden*golden + 1.0f);
  114. static const float ss = 1.0f/len * scale;
  115. static const float ll = ss*golden;
  116. static const float vv[12][4] =
  117. {
  118. { -ll, 0.0f, -ss, 0.0f },
  119. { ll, 0.0f, -ss, 0.0f },
  120. { ll, 0.0f, ss, 0.0f },
  121. { -ll, 0.0f, ss, 0.0f },
  122. { -ss, ll, 0.0f, 0.0f },
  123. { ss, ll, 0.0f, 0.0f },
  124. { ss, -ll, 0.0f, 0.0f },
  125. { -ss, -ll, 0.0f, 0.0f },
  126. { 0.0f, -ss, ll, 0.0f },
  127. { 0.0f, ss, ll, 0.0f },
  128. { 0.0f, ss, -ll, 0.0f },
  129. { 0.0f, -ss, -ll, 0.0f },
  130. };
  131. m_numVertices = 0;
  132. triangle(vv[ 0], vv[ 4], vv[ 3], scale, _subdiv);
  133. triangle(vv[ 0], vv[10], vv[ 4], scale, _subdiv);
  134. triangle(vv[ 4], vv[10], vv[ 5], scale, _subdiv);
  135. triangle(vv[ 5], vv[10], vv[ 1], scale, _subdiv);
  136. triangle(vv[ 5], vv[ 1], vv[ 2], scale, _subdiv);
  137. triangle(vv[ 5], vv[ 2], vv[ 9], scale, _subdiv);
  138. triangle(vv[ 5], vv[ 9], vv[ 4], scale, _subdiv);
  139. triangle(vv[ 3], vv[ 4], vv[ 9], scale, _subdiv);
  140. triangle(vv[ 0], vv[ 3], vv[ 7], scale, _subdiv);
  141. triangle(vv[ 0], vv[ 7], vv[11], scale, _subdiv);
  142. triangle(vv[11], vv[ 7], vv[ 6], scale, _subdiv);
  143. triangle(vv[11], vv[ 6], vv[ 1], scale, _subdiv);
  144. triangle(vv[ 1], vv[ 6], vv[ 2], scale, _subdiv);
  145. triangle(vv[ 2], vv[ 6], vv[ 8], scale, _subdiv);
  146. triangle(vv[ 8], vv[ 6], vv[ 7], scale, _subdiv);
  147. triangle(vv[ 8], vv[ 7], vv[ 3], scale, _subdiv);
  148. triangle(vv[ 0], vv[11], vv[10], scale, _subdiv);
  149. triangle(vv[ 1], vv[10], vv[11], scale, _subdiv);
  150. triangle(vv[ 2], vv[ 8], vv[ 9], scale, _subdiv);
  151. triangle(vv[ 3], vv[ 9], vv[ 8], scale, _subdiv);
  152. }
  153. void addVert(const float* _v)
  154. {
  155. float* verts = (float*)m_pos;
  156. verts[0] = _v[0];
  157. verts[1] = _v[1];
  158. verts[2] = _v[2];
  159. m_pos += m_posStride;
  160. if (NULL != m_normals)
  161. {
  162. float* normals = (float*)m_normals;
  163. bx::vec3Norm(normals, _v);
  164. m_normals += m_normalStride;
  165. }
  166. m_numVertices++;
  167. }
  168. void triangle(const float* _v0, const float* _v1, const float* _v2, float _scale, uint8_t _subdiv)
  169. {
  170. if (0 == _subdiv)
  171. {
  172. addVert(_v0);
  173. addVert(_v1);
  174. addVert(_v2);
  175. }
  176. else
  177. {
  178. float tmp0[4];
  179. float tmp1[4];
  180. float v01[4];
  181. bx::vec3Add(tmp0, _v0, _v1);
  182. bx::vec3Norm(tmp1, tmp0);
  183. bx::vec3Mul(v01, tmp1, _scale);
  184. float v12[4];
  185. bx::vec3Add(tmp0, _v1, _v2);
  186. bx::vec3Norm(tmp1, tmp0);
  187. bx::vec3Mul(v12, tmp1, _scale);
  188. float v20[4];
  189. bx::vec3Add(tmp0, _v2, _v0);
  190. bx::vec3Norm(tmp1, tmp0);
  191. bx::vec3Mul(v20, tmp1, _scale);
  192. --_subdiv;
  193. triangle(_v0, v01, v20, _scale, _subdiv);
  194. triangle(_v1, v12, v01, _scale, _subdiv);
  195. triangle(_v2, v20, v12, _scale, _subdiv);
  196. triangle(v01, v12, v20, _scale, _subdiv);
  197. }
  198. }
  199. uint8_t* m_pos;
  200. uint8_t* m_normals;
  201. uint16_t m_posStride;
  202. uint16_t m_normalStride;
  203. uint32_t m_numVertices;
  204. } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0);
  205. }
  206. uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::fpow(4.0f, _subdiv0) );
  207. return numVertices;
  208. }
  209. void getPoint(float* _result, Axis::Enum _axis, float _x, float _y)
  210. {
  211. switch (_axis)
  212. {
  213. case Axis::X:
  214. _result[0] = 0.0f;
  215. _result[1] = _x;
  216. _result[2] = _y;
  217. break;
  218. case Axis::Y:
  219. _result[0] = _y;
  220. _result[1] = 0.0f;
  221. _result[2] = _x;
  222. break;
  223. default:
  224. _result[0] = _x;
  225. _result[1] = _y;
  226. _result[2] = 0.0f;
  227. break;
  228. }
  229. }
  230. #include "vs_debugdraw_lines.bin.h"
  231. #include "fs_debugdraw_lines.bin.h"
  232. #include "vs_debugdraw_lines_stipple.bin.h"
  233. #include "fs_debugdraw_lines_stipple.bin.h"
  234. #include "vs_debugdraw_fill.bin.h"
  235. #include "fs_debugdraw_fill.bin.h"
  236. #include "vs_debugdraw_fill_lit.bin.h"
  237. #include "fs_debugdraw_fill_lit.bin.h"
  238. struct EmbeddedShader
  239. {
  240. bgfx::RendererType::Enum type;
  241. const uint8_t* data;
  242. uint32_t size;
  243. };
  244. #define BGFX_DECLARE_SHADER_EMBEDDED(_name) \
  245. { \
  246. { bgfx::RendererType::Direct3D9, BX_CONCATENATE(_name, _dx9 ), sizeof(BX_CONCATENATE(_name, _dx9 ) ) }, \
  247. { bgfx::RendererType::Direct3D11, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  248. { bgfx::RendererType::Direct3D12, BX_CONCATENATE(_name, _dx11), sizeof(BX_CONCATENATE(_name, _dx11) ) }, \
  249. { bgfx::RendererType::OpenGL, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  250. { bgfx::RendererType::OpenGLES, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  251. { bgfx::RendererType::Vulkan, BX_CONCATENATE(_name, _glsl), sizeof(BX_CONCATENATE(_name, _glsl) ) }, \
  252. { bgfx::RendererType::Metal, BX_CONCATENATE(_name, _mtl ), sizeof(BX_CONCATENATE(_name, _mtl ) ) }, \
  253. { bgfx::RendererType::Count, NULL, 0 }, \
  254. }
  255. static const EmbeddedShader s_embeddedShaders[][8] =
  256. {
  257. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines),
  258. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines),
  259. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_lines_stipple),
  260. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_lines_stipple),
  261. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill),
  262. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill),
  263. BGFX_DECLARE_SHADER_EMBEDDED(vs_debugdraw_fill_lit),
  264. BGFX_DECLARE_SHADER_EMBEDDED(fs_debugdraw_fill_lit),
  265. };
  266. static bgfx::ShaderHandle createEmbeddedShader(bgfx::RendererType::Enum _type, uint32_t _index)
  267. {
  268. for (const EmbeddedShader* es = s_embeddedShaders[_index]; bgfx::RendererType::Count != es->type; ++es)
  269. {
  270. if (_type == es->type)
  271. {
  272. return bgfx::createShader(bgfx::makeRef(es->data, es->size) );
  273. }
  274. }
  275. bgfx::ShaderHandle handle = BGFX_INVALID_HANDLE;
  276. return handle;
  277. }
  278. struct DebugDraw
  279. {
  280. DebugDraw()
  281. : m_state(State::Count)
  282. {
  283. }
  284. void init(bx::AllocatorI* _allocator)
  285. {
  286. m_allocator = _allocator;
  287. #if BX_CONFIG_ALLOCATOR_CRT
  288. if (NULL == _allocator)
  289. {
  290. static bx::CrtAllocator allocator;
  291. m_allocator = &allocator;
  292. }
  293. #endif // BX_CONFIG_ALLOCATOR_CRT
  294. DebugVertex::init();
  295. DebugShapeVertex::init();
  296. bgfx::RendererType::Enum type = bgfx::getRendererType();
  297. m_program[Program::Lines] =
  298. bgfx::createProgram(createEmbeddedShader(type, 0)
  299. , createEmbeddedShader(type, 1)
  300. , true
  301. );
  302. m_program[Program::LinesStipple] =
  303. bgfx::createProgram(createEmbeddedShader(type, 2)
  304. , createEmbeddedShader(type, 3)
  305. , true
  306. );
  307. m_program[Program::Fill] =
  308. bgfx::createProgram(createEmbeddedShader(type, 4)
  309. , createEmbeddedShader(type, 5)
  310. , true
  311. );
  312. m_program[Program::FillLit] =
  313. bgfx::createProgram(createEmbeddedShader(type, 6)
  314. , createEmbeddedShader(type, 7)
  315. , true
  316. );
  317. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
  318. void* vertices[Mesh::Count] = {};
  319. uint16_t* indices[Mesh::Count] = {};
  320. uint16_t stride = DebugShapeVertex::ms_decl.getStride();
  321. uint32_t startVertex = 0;
  322. uint32_t startIndex = 0;
  323. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  324. {
  325. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  326. const uint8_t tess = uint8_t(3-mesh);
  327. const uint32_t numVertices = genSphere(tess);
  328. const uint32_t numIndices = numVertices;
  329. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  330. genSphere(tess, vertices[id], stride);
  331. uint16_t* trilist = (uint16_t*)BX_ALLOC(m_allocator, numIndices*sizeof(uint16_t) );
  332. for (uint32_t ii = 0; ii < numIndices; ++ii)
  333. {
  334. trilist[ii] = uint16_t(ii);
  335. }
  336. uint32_t numLineListIndices = bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  337. , NULL
  338. , 0
  339. , trilist
  340. , numIndices
  341. , false
  342. );
  343. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  344. uint16_t* indicesOut = indices[id];
  345. memcpy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
  346. bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  347. , &indicesOut[numIndices]
  348. , numLineListIndices*sizeof(uint16_t)
  349. , trilist
  350. , numIndices
  351. , false
  352. );
  353. m_mesh[id].m_startVertex = startVertex;
  354. m_mesh[id].m_numVertices = numVertices;
  355. m_mesh[id].m_startIndex[0] = startIndex;
  356. m_mesh[id].m_numIndices[0] = numIndices;
  357. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  358. m_mesh[id].m_numIndices[1] = numLineListIndices;
  359. startVertex += numVertices;
  360. startIndex += numIndices + numLineListIndices;
  361. BX_FREE(m_allocator, trilist);
  362. }
  363. m_mesh[Mesh::Cube].m_startVertex = startVertex;
  364. m_mesh[Mesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
  365. m_mesh[Mesh::Cube].m_startIndex[0] = startIndex;
  366. m_mesh[Mesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
  367. m_mesh[Mesh::Cube].m_startIndex[1] = 0;
  368. m_mesh[Mesh::Cube].m_numIndices[1] = 0;
  369. startVertex += m_mesh[Mesh::Cube].m_numVertices;
  370. startIndex += m_mesh[Mesh::Cube].m_numIndices[0];
  371. const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
  372. const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
  373. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  374. {
  375. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  376. memcpy(&vb->data[m_mesh[id].m_startVertex * stride]
  377. , vertices[id]
  378. , m_mesh[id].m_numVertices*stride
  379. );
  380. memcpy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
  381. , indices[id]
  382. , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
  383. );
  384. BX_FREE(m_allocator, vertices[id]);
  385. BX_FREE(m_allocator, indices[id]);
  386. }
  387. memcpy(&vb->data[m_mesh[Mesh::Cube].m_startVertex * stride]
  388. , s_cubeVertices
  389. , sizeof(s_cubeVertices)
  390. );
  391. memcpy(&ib->data[m_mesh[Mesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
  392. , s_cubeIndices
  393. , sizeof(s_cubeIndices)
  394. );
  395. m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_decl);
  396. m_ibh = bgfx::createIndexBuffer(ib);
  397. m_mtx = 0;
  398. m_viewId = 0;
  399. m_pos = 0;
  400. m_indexPos = 0;
  401. m_vertexPos = 0;
  402. }
  403. void shutdown()
  404. {
  405. bgfx::destroyIndexBuffer(m_ibh);
  406. bgfx::destroyVertexBuffer(m_vbh);
  407. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  408. {
  409. bgfx::destroyProgram(m_program[ii]);
  410. }
  411. bgfx::destroyUniform(u_params);
  412. }
  413. void begin(uint8_t _viewId)
  414. {
  415. BX_CHECK(State::Count == m_state);
  416. m_viewId = _viewId;
  417. m_mtx = 0;
  418. m_state = State::None;
  419. m_stack = 0;
  420. Attrib& attrib = m_attrib[0];
  421. attrib.m_state = 0
  422. | BGFX_STATE_RGB_WRITE
  423. | BGFX_STATE_DEPTH_TEST_LESS
  424. | BGFX_STATE_DEPTH_WRITE
  425. | BGFX_STATE_CULL_CW
  426. ;
  427. attrib.m_scale = 1.0f;
  428. attrib.m_offset = 0.0f;
  429. attrib.m_abgr = UINT32_MAX;
  430. attrib.m_stipple = false;
  431. attrib.m_wireframe = false;
  432. attrib.m_lod = 0;
  433. }
  434. void end()
  435. {
  436. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  437. flush();
  438. m_state = State::Count;
  439. }
  440. void push()
  441. {
  442. BX_CHECK(State::Count != m_state);
  443. ++m_stack;
  444. m_attrib[m_stack] = m_attrib[m_stack-1];
  445. }
  446. void pop()
  447. {
  448. BX_CHECK(State::Count != m_state);
  449. if (m_attrib[m_stack].m_stipple != m_attrib[m_stack-1].m_stipple)
  450. {
  451. flush();
  452. }
  453. --m_stack;
  454. }
  455. void setTransform(const void* _mtx)
  456. {
  457. BX_CHECK(State::Count != m_state);
  458. flush();
  459. if (NULL == _mtx)
  460. {
  461. m_mtx = 0;
  462. return;
  463. }
  464. bgfx::Transform transform;
  465. m_mtx = bgfx::allocTransform(&transform, 1);
  466. memcpy(transform.data, _mtx, 64);
  467. }
  468. void setTranslate(float _x, float _y, float _z)
  469. {
  470. float mtx[16];
  471. bx::mtxTranslate(mtx, _x, _y, _z);
  472. setTransform(mtx);
  473. }
  474. void setTranslate(const float* _pos)
  475. {
  476. setTranslate(_pos[0], _pos[1], _pos[2]);
  477. }
  478. void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  479. {
  480. m_attrib[m_stack].m_state &= ~(0
  481. | BGFX_STATE_DEPTH_TEST_LESS
  482. | BGFX_STATE_DEPTH_WRITE
  483. | BGFX_STATE_CULL_CW
  484. | BGFX_STATE_CULL_CCW
  485. );
  486. m_attrib[m_stack].m_state |= _depthTest
  487. ? BGFX_STATE_DEPTH_TEST_LESS
  488. : 0
  489. ;
  490. m_attrib[m_stack].m_state |= _depthWrite
  491. ? BGFX_STATE_DEPTH_WRITE
  492. : 0
  493. ;
  494. m_attrib[m_stack].m_state |= _clockwise
  495. ? BGFX_STATE_CULL_CW
  496. : BGFX_STATE_CULL_CCW
  497. ;
  498. }
  499. void setColor(uint32_t _abgr)
  500. {
  501. BX_CHECK(State::Count != m_state);
  502. m_attrib[m_stack].m_abgr = _abgr;
  503. }
  504. void setLod(uint8_t _lod)
  505. {
  506. BX_CHECK(State::Count != m_state);
  507. m_attrib[m_stack].m_lod = _lod;
  508. }
  509. void setWireframe(bool _wireframe)
  510. {
  511. BX_CHECK(State::Count != m_state);
  512. m_attrib[m_stack].m_wireframe = _wireframe;
  513. }
  514. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  515. {
  516. BX_CHECK(State::Count != m_state);
  517. Attrib& attrib = m_attrib[m_stack];
  518. if (attrib.m_stipple != _stipple)
  519. {
  520. flush();
  521. }
  522. attrib.m_stipple = _stipple;
  523. attrib.m_offset = _offset;
  524. attrib.m_scale = _scale;
  525. }
  526. void moveTo(float _x, float _y, float _z = 0.0f)
  527. {
  528. BX_CHECK(State::Count != m_state);
  529. softFlush();
  530. m_state = State::MoveTo;
  531. DebugVertex& vertex = m_cache[m_pos];
  532. vertex.m_x = _x;
  533. vertex.m_y = _y;
  534. vertex.m_z = _z;
  535. Attrib& attrib = m_attrib[m_stack];
  536. vertex.m_abgr = attrib.m_abgr;
  537. vertex.m_len = attrib.m_offset;
  538. m_vertexPos = m_pos;
  539. }
  540. void moveTo(const void* _pos)
  541. {
  542. BX_CHECK(State::Count != m_state);
  543. const float* pos = (const float*)_pos;
  544. moveTo(pos[0], pos[1], pos[2]);
  545. }
  546. void moveTo(Axis::Enum _axis, float _x, float _y)
  547. {
  548. float pos[3];
  549. getPoint(pos, _axis, _x, _y);
  550. moveTo(pos);
  551. }
  552. void lineTo(float _x, float _y, float _z = 0.0f)
  553. {
  554. BX_CHECK(State::Count != m_state);
  555. if (State::None == m_state)
  556. {
  557. moveTo(_x, _y, _z);
  558. return;
  559. }
  560. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  561. {
  562. uint32_t pos = m_pos;
  563. uint32_t vertexPos = m_vertexPos;
  564. flush();
  565. memcpy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  566. if (vertexPos == pos)
  567. {
  568. m_pos = 1;
  569. }
  570. else
  571. {
  572. memcpy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  573. m_pos = 2;
  574. }
  575. m_state = State::LineTo;
  576. }
  577. else if (State::MoveTo == m_state)
  578. {
  579. ++m_pos;
  580. m_state = State::LineTo;
  581. }
  582. uint16_t prev = m_pos-1;
  583. uint16_t curr = m_pos++;
  584. DebugVertex& vertex = m_cache[curr];
  585. vertex.m_x = _x;
  586. vertex.m_y = _y;
  587. vertex.m_z = _z;
  588. Attrib& attrib = m_attrib[m_stack];
  589. vertex.m_abgr = attrib.m_abgr;
  590. vertex.m_len = attrib.m_offset;
  591. float tmp[3];
  592. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  593. float len = bx::vec3Length(tmp) * attrib.m_scale;
  594. vertex.m_len = m_cache[prev].m_len + len;
  595. m_indices[m_indexPos++] = prev;
  596. m_indices[m_indexPos++] = curr;
  597. }
  598. void lineTo(const void* _pos)
  599. {
  600. BX_CHECK(State::Count != m_state);
  601. const float* pos = (const float*)_pos;
  602. lineTo(pos[0], pos[1], pos[2]);
  603. }
  604. void lineTo(Axis::Enum _axis, float _x, float _y)
  605. {
  606. float pos[3];
  607. getPoint(pos, _axis, _x, _y);
  608. lineTo(pos);
  609. }
  610. void close()
  611. {
  612. BX_CHECK(State::Count != m_state);
  613. DebugVertex& vertex = m_cache[m_vertexPos];
  614. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  615. m_state = State::None;
  616. }
  617. void draw(const Aabb& _aabb)
  618. {
  619. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  620. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  621. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  622. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  623. close();
  624. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  625. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  626. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  627. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  628. close();
  629. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  630. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  631. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  632. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  633. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  634. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  635. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  636. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  637. }
  638. void draw(const Cylinder& _cylinder, bool _capsule)
  639. {
  640. BX_UNUSED(_cylinder, _capsule);
  641. }
  642. void draw(const Disk& _disk)
  643. {
  644. BX_UNUSED(_disk);
  645. }
  646. void draw(const Obb& _obb)
  647. {
  648. const Attrib& attrib = m_attrib[m_stack];
  649. if (attrib.m_wireframe)
  650. {
  651. setTransform(_obb.m_mtx);
  652. moveTo(-1.0f, -1.0f, -1.0f);
  653. lineTo( 1.0f, -1.0f, -1.0f);
  654. lineTo( 1.0f, 1.0f, -1.0f);
  655. lineTo(-1.0f, 1.0f, -1.0f);
  656. close();
  657. moveTo(-1.0f, 1.0f, 1.0f);
  658. lineTo( 1.0f, 1.0f, 1.0f);
  659. lineTo( 1.0f, -1.0f, 1.0f);
  660. lineTo(-1.0f, -1.0f, 1.0f);
  661. close();
  662. moveTo( 1.0f, -1.0f, -1.0f);
  663. lineTo( 1.0f, -1.0f, 1.0f);
  664. moveTo( 1.0f, 1.0f, -1.0f);
  665. lineTo( 1.0f, 1.0f, 1.0f);
  666. moveTo(-1.0f, 1.0f, -1.0f);
  667. lineTo(-1.0f, 1.0f, 1.0f);
  668. moveTo(-1.0f, -1.0f, -1.0f);
  669. lineTo(-1.0f, -1.0f, 1.0f);
  670. setTransform(NULL);
  671. }
  672. else
  673. {
  674. draw(Mesh::Cube, _obb.m_mtx, false);
  675. }
  676. }
  677. void draw(const Sphere& _sphere)
  678. {
  679. const Attrib& attrib = m_attrib[m_stack];
  680. float mtx[16];
  681. bx::mtxSRT(mtx
  682. , _sphere.m_radius
  683. , _sphere.m_radius
  684. , _sphere.m_radius
  685. , 0.0f
  686. , 0.0f
  687. , 0.0f
  688. , _sphere.m_center[0]
  689. , _sphere.m_center[1]
  690. , _sphere.m_center[2]
  691. );
  692. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  693. ? uint8_t(Mesh::SphereMaxLod)
  694. : attrib.m_lod
  695. ;
  696. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, attrib.m_wireframe);
  697. }
  698. void drawFrustum(const float* _viewProj)
  699. {
  700. Plane planes[6];
  701. buildFrustumPlanes(planes, _viewProj);
  702. float points[24];
  703. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  704. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  705. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  706. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  707. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  708. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  709. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  710. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  711. moveTo(&points[ 0]);
  712. lineTo(&points[ 3]);
  713. lineTo(&points[ 6]);
  714. lineTo(&points[ 9]);
  715. close();
  716. moveTo(&points[12]);
  717. lineTo(&points[15]);
  718. lineTo(&points[18]);
  719. lineTo(&points[21]);
  720. close();
  721. moveTo(&points[ 0]);
  722. lineTo(&points[12]);
  723. moveTo(&points[ 3]);
  724. lineTo(&points[15]);
  725. moveTo(&points[ 6]);
  726. lineTo(&points[18]);
  727. moveTo(&points[ 9]);
  728. lineTo(&points[21]);
  729. }
  730. void drawFrustum(const void* _viewProj)
  731. {
  732. drawFrustum( (const float*)_viewProj);
  733. }
  734. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  735. {
  736. const Attrib& attrib = m_attrib[m_stack];
  737. const uint32_t num = getCircleLod(attrib.m_lod);
  738. const float step = bx::pi * 2.0f / num;
  739. _degrees = bx::fwrap(_degrees, 360.0f);
  740. float pos[3];
  741. getPoint(pos, _axis
  742. , bx::fsin(step * 0)*_radius
  743. , bx::fcos(step * 0)*_radius
  744. );
  745. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  746. uint32_t n = uint32_t(num*_degrees/360.0f);
  747. for (uint32_t ii = 1; ii < n+1; ++ii)
  748. {
  749. getPoint(pos, _axis
  750. , bx::fsin(step * ii)*_radius
  751. , bx::fcos(step * ii)*_radius
  752. );
  753. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  754. }
  755. moveTo(_x, _y, _z);
  756. getPoint(pos, _axis
  757. , bx::fsin(step * 0)*_radius
  758. , bx::fcos(step * 0)*_radius
  759. );
  760. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  761. getPoint(pos, _axis
  762. , bx::fsin(step * n)*_radius
  763. , bx::fcos(step * n)*_radius
  764. );
  765. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  766. lineTo(_x, _y, _z);
  767. }
  768. void drawCircle(const float* _normal, const float* _center, float _radius, float _weight = 0.0f)
  769. {
  770. const Attrib& attrib = m_attrib[m_stack];
  771. const uint32_t num = getCircleLod(attrib.m_lod);
  772. const float step = bx::pi * 2.0f / num;
  773. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  774. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  775. float udir[3];
  776. float vdir[3];
  777. calcPlaneUv(plane, udir, vdir);
  778. float pos[3];
  779. float tmp0[3];
  780. float tmp1[3];
  781. float xy0[2];
  782. float xy1[2];
  783. circle(xy0, 0.0f);
  784. squircle(xy1, 0.0f);
  785. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  786. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  787. bx::vec3Add(tmp1, pos, tmp0);
  788. bx::vec3Add(pos, tmp1, _center);
  789. moveTo(pos);
  790. for (uint32_t ii = 1; ii < num; ++ii)
  791. {
  792. float angle = step * ii;
  793. circle(xy0, angle);
  794. squircle(xy1, angle);
  795. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  796. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  797. bx::vec3Add(tmp1, pos, tmp0);
  798. bx::vec3Add(pos, tmp1, _center);
  799. lineTo(pos);
  800. }
  801. close();
  802. }
  803. void drawCircle(const void* _normal, const void* _center, float _radius, float _weight = 0.0f)
  804. {
  805. drawCircle( (const float*)_normal, (const float*)_center, _radius, _weight);
  806. }
  807. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight = 0.0f)
  808. {
  809. const Attrib& attrib = m_attrib[m_stack];
  810. const uint32_t num = getCircleLod(attrib.m_lod);
  811. const float step = bx::pi * 2.0f / num;
  812. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  813. float xy0[2];
  814. float xy1[2];
  815. circle(xy0, 0.0f);
  816. squircle(xy1, 0.0f);
  817. float pos[3];
  818. getPoint(pos, _axis
  819. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  820. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  821. );
  822. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  823. for (uint32_t ii = 1; ii < num; ++ii)
  824. {
  825. float angle = step * ii;
  826. circle(xy0, angle);
  827. squircle(xy1, angle);
  828. getPoint(pos, _axis
  829. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  830. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  831. );
  832. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  833. }
  834. close();
  835. }
  836. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  837. {
  838. push();
  839. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  840. moveTo(_x, _y, _z);
  841. lineTo(_x + _len, _y, _z);
  842. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  843. moveTo(_x, _y, _z);
  844. lineTo(_x, _y + _len, _z);
  845. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  846. moveTo(_x, _y, _z);
  847. lineTo(_x, _y, _z + _len);
  848. pop();
  849. }
  850. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  851. {
  852. float udir[3];
  853. float vdir[3];
  854. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  855. calcPlaneUv(plane, udir, vdir);
  856. bx::vec3Mul(udir, udir, _step);
  857. bx::vec3Mul(vdir, vdir, _step);
  858. const uint32_t num = (_size/2)*2+1;
  859. const float halfExtent = float(_size/2);
  860. float umin[3];
  861. bx::vec3Mul(umin, udir, -halfExtent);
  862. float umax[3];
  863. bx::vec3Mul(umax, udir, halfExtent);
  864. float vmin[3];
  865. bx::vec3Mul(vmin, vdir, -halfExtent);
  866. float vmax[3];
  867. bx::vec3Mul(vmax, vdir, halfExtent);
  868. float tmp[3];
  869. float xs[3];
  870. float xe[3];
  871. bx::vec3Add(tmp, umin, vmin);
  872. bx::vec3Add(xs, _center, tmp);
  873. bx::vec3Add(tmp, umax, vmin);
  874. bx::vec3Add(xe, _center, tmp);
  875. float ys[3];
  876. float ye[3];
  877. bx::vec3Add(tmp, umin, vmin);
  878. bx::vec3Add(ys, _center, tmp);
  879. bx::vec3Add(tmp, umin, vmax);
  880. bx::vec3Add(ye, _center, tmp);
  881. for (uint32_t ii = 0; ii < num; ++ii)
  882. {
  883. moveTo(xs);
  884. lineTo(xe);
  885. bx::vec3Add(xs, xs, vdir);
  886. bx::vec3Add(xe, xe, vdir);
  887. moveTo(ys);
  888. lineTo(ye);
  889. bx::vec3Add(ys, ys, udir);
  890. bx::vec3Add(ye, ye, udir);
  891. }
  892. }
  893. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  894. {
  895. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  896. }
  897. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  898. {
  899. push();
  900. setTranslate(_center);
  901. const uint32_t num = (_size/2)*2-1;
  902. const float halfExtent = float(_size/2) * _step;
  903. setColor(0xff606060);
  904. float yy = -halfExtent + _step;
  905. for (uint32_t ii = 0; ii < num; ++ii)
  906. {
  907. moveTo(_axis, -halfExtent, yy);
  908. lineTo(_axis, halfExtent, yy);
  909. moveTo(_axis, yy, -halfExtent);
  910. lineTo(_axis, yy, halfExtent);
  911. yy += _step;
  912. }
  913. setColor(0xff101010);
  914. moveTo(_axis, -halfExtent, -halfExtent);
  915. lineTo(_axis, -halfExtent, halfExtent);
  916. lineTo(_axis, halfExtent, halfExtent);
  917. lineTo(_axis, halfExtent, -halfExtent);
  918. close();
  919. moveTo(_axis, -halfExtent, 0.0f);
  920. lineTo(_axis, halfExtent, 0.0f);
  921. moveTo(_axis, 0.0f, -halfExtent);
  922. lineTo(_axis, 0.0f, halfExtent);
  923. pop();
  924. }
  925. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  926. {
  927. drawGrid(_axis, (const float*)_center, _size, _step);
  928. }
  929. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  930. {
  931. push();
  932. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  933. drawCircle(Axis::X, _x, _y, _z, _radius);
  934. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  935. drawCircle(Axis::Y, _x, _y, _z, _radius);
  936. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  937. drawCircle(Axis::Z, _x, _y, _z, _radius);
  938. pop();
  939. }
  940. private:
  941. struct Mesh
  942. {
  943. enum Enum
  944. {
  945. Sphere0,
  946. Sphere1,
  947. Sphere2,
  948. Sphere3,
  949. Cube,
  950. Count,
  951. SphereMaxLod = Sphere3 - Sphere0,
  952. };
  953. uint32_t m_startVertex;
  954. uint32_t m_numVertices;
  955. uint32_t m_startIndex[2];
  956. uint32_t m_numIndices[2];
  957. };
  958. struct Program
  959. {
  960. enum Enum
  961. {
  962. Lines,
  963. LinesStipple,
  964. Fill,
  965. FillLit,
  966. Count
  967. };
  968. };
  969. void draw(Mesh::Enum _mesh, const float* _mtx, bool _wireframe) const
  970. {
  971. const Mesh& mesh = m_mesh[_mesh];
  972. const Attrib& attrib = m_attrib[m_stack];
  973. if (0 != mesh.m_numIndices[_wireframe])
  974. {
  975. bgfx::setIndexBuffer(m_ibh
  976. , mesh.m_startIndex[_wireframe]
  977. , mesh.m_numIndices[_wireframe]
  978. );
  979. }
  980. float params[4][4] =
  981. {
  982. {
  983. 0.0f,
  984. -1.0f,
  985. 0.0f,
  986. 3.0f,
  987. },
  988. {
  989. 1.0f,
  990. 0.9f,
  991. 0.8f,
  992. 0.0f,
  993. },
  994. {
  995. 0.2f,
  996. 0.22f,
  997. 0.5f,
  998. 0.0f,
  999. },
  1000. {
  1001. ( (attrib.m_abgr>>24) )/255.0f,
  1002. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1003. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1004. ( (attrib.m_abgr )&0xff)/255.0f,
  1005. },
  1006. };
  1007. bx::vec3Norm(params[0], params[0]);
  1008. bgfx::setUniform(u_params, params, 4);
  1009. bgfx::setTransform(_mtx);
  1010. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1011. bgfx::setState(0
  1012. | attrib.m_state
  1013. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA : 0)
  1014. );
  1015. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1016. }
  1017. void softFlush()
  1018. {
  1019. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1020. {
  1021. flush();
  1022. }
  1023. }
  1024. void flush()
  1025. {
  1026. if (0 != m_pos)
  1027. {
  1028. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1029. {
  1030. bgfx::TransientVertexBuffer tvb;
  1031. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1032. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1033. bgfx::TransientIndexBuffer tib;
  1034. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1035. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1036. bgfx::setVertexBuffer(&tvb);
  1037. bgfx::setIndexBuffer(&tib);
  1038. bgfx::setState(0
  1039. | BGFX_STATE_RGB_WRITE
  1040. | BGFX_STATE_PT_LINES
  1041. | BGFX_STATE_DEPTH_TEST_LEQUAL
  1042. | BGFX_STATE_DEPTH_WRITE
  1043. | BGFX_STATE_LINEAA
  1044. | BGFX_STATE_BLEND_ALPHA
  1045. );
  1046. bgfx::setTransform(m_mtx);
  1047. bgfx::ProgramHandle program = m_program[m_attrib[m_stack].m_stipple ? 1 : 0];
  1048. bgfx::submit(m_viewId, program);
  1049. }
  1050. m_state = State::None;
  1051. m_pos = 0;
  1052. m_indexPos = 0;
  1053. m_vertexPos = 0;
  1054. }
  1055. }
  1056. struct State
  1057. {
  1058. enum Enum
  1059. {
  1060. None,
  1061. MoveTo,
  1062. LineTo,
  1063. Count
  1064. };
  1065. };
  1066. static const uint32_t cacheSize = 1024;
  1067. static const uint32_t stackSize = 16;
  1068. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1069. DebugVertex m_cache[cacheSize+1];
  1070. uint32_t m_mtx;
  1071. uint16_t m_indices[cacheSize*2];
  1072. uint16_t m_pos;
  1073. uint16_t m_indexPos;
  1074. uint16_t m_vertexPos;
  1075. uint8_t m_viewId;
  1076. uint8_t m_stack;
  1077. struct Attrib
  1078. {
  1079. uint64_t m_state;
  1080. float m_offset;
  1081. float m_scale;
  1082. uint32_t m_abgr;
  1083. bool m_stipple;
  1084. bool m_wireframe;
  1085. uint8_t m_lod;
  1086. };
  1087. Attrib m_attrib[stackSize];
  1088. State::Enum m_state;
  1089. Mesh m_mesh[Mesh::Count];
  1090. bgfx::ProgramHandle m_program[Program::Count];
  1091. bgfx::UniformHandle u_params;
  1092. bgfx::VertexBufferHandle m_vbh;
  1093. bgfx::IndexBufferHandle m_ibh;
  1094. bx::AllocatorI* m_allocator;
  1095. };
  1096. static DebugDraw s_dd;
  1097. void ddInit(bx::AllocatorI* _allocator)
  1098. {
  1099. s_dd.init(_allocator);
  1100. }
  1101. void ddShutdown()
  1102. {
  1103. s_dd.shutdown();
  1104. }
  1105. void ddBegin(uint8_t _viewId)
  1106. {
  1107. s_dd.begin(_viewId);
  1108. }
  1109. void ddEnd()
  1110. {
  1111. s_dd.end();
  1112. }
  1113. void ddPush()
  1114. {
  1115. s_dd.push();
  1116. }
  1117. void ddPop()
  1118. {
  1119. s_dd.pop();
  1120. }
  1121. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1122. {
  1123. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1124. }
  1125. void ddSetColor(uint32_t _abgr)
  1126. {
  1127. s_dd.setColor(_abgr);
  1128. }
  1129. void ddSetLod(uint8_t _lod)
  1130. {
  1131. s_dd.setLod(_lod);
  1132. }
  1133. void ddSetWireframe(bool _wireframe)
  1134. {
  1135. s_dd.setWireframe(_wireframe);
  1136. }
  1137. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1138. {
  1139. s_dd.setStipple(_stipple, _scale, _offset);
  1140. }
  1141. void ddSetTransform(const void* _mtx)
  1142. {
  1143. s_dd.setTransform(_mtx);
  1144. }
  1145. void ddSetTranslate(float _x, float _y, float _z)
  1146. {
  1147. s_dd.setTranslate(_x, _y, _z);
  1148. }
  1149. void ddMoveTo(float _x, float _y, float _z)
  1150. {
  1151. s_dd.moveTo(_x, _y, _z);
  1152. }
  1153. void ddMoveTo(const void* _pos)
  1154. {
  1155. s_dd.moveTo(_pos);
  1156. }
  1157. void ddLineTo(float _x, float _y, float _z)
  1158. {
  1159. s_dd.lineTo(_x, _y, _z);
  1160. }
  1161. void ddLineTo(const void* _pos)
  1162. {
  1163. s_dd.lineTo(_pos);
  1164. }
  1165. void ddClose()
  1166. {
  1167. s_dd.close();
  1168. }
  1169. void ddDraw(const Aabb& _aabb)
  1170. {
  1171. s_dd.draw(_aabb);
  1172. }
  1173. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1174. {
  1175. s_dd.draw(_cylinder, _capsule);
  1176. }
  1177. void ddDraw(const Disk& _disk)
  1178. {
  1179. s_dd.draw(_disk);
  1180. }
  1181. void ddDraw(const Obb& _obb)
  1182. {
  1183. s_dd.draw(_obb);
  1184. }
  1185. void ddDraw(const Sphere& _sphere)
  1186. {
  1187. s_dd.draw(_sphere);
  1188. }
  1189. void ddDrawFrustum(const void* _viewProj)
  1190. {
  1191. s_dd.drawFrustum(_viewProj);
  1192. }
  1193. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1194. {
  1195. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1196. }
  1197. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1198. {
  1199. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1200. }
  1201. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1202. {
  1203. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1204. }
  1205. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1206. {
  1207. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1208. }
  1209. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1210. {
  1211. s_dd.drawGrid(_normal, _center, _size, _step);
  1212. }
  1213. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1214. {
  1215. s_dd.drawGrid(_axis, _center, _size, _step);
  1216. }
  1217. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1218. {
  1219. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1220. }