debugdraw.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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_depthTestLess(true)
  282. , m_state(State::Count)
  283. {
  284. }
  285. void init(bool _depthTestLess, bx::AllocatorI* _allocator)
  286. {
  287. m_allocator = _allocator;
  288. m_depthTestLess = _depthTestLess;
  289. #if BX_CONFIG_ALLOCATOR_CRT
  290. if (NULL == _allocator)
  291. {
  292. static bx::CrtAllocator allocator;
  293. m_allocator = &allocator;
  294. }
  295. #endif // BX_CONFIG_ALLOCATOR_CRT
  296. DebugVertex::init();
  297. DebugShapeVertex::init();
  298. bgfx::RendererType::Enum type = bgfx::getRendererType();
  299. m_program[Program::Lines] =
  300. bgfx::createProgram(createEmbeddedShader(type, 0)
  301. , createEmbeddedShader(type, 1)
  302. , true
  303. );
  304. m_program[Program::LinesStipple] =
  305. bgfx::createProgram(createEmbeddedShader(type, 2)
  306. , createEmbeddedShader(type, 3)
  307. , true
  308. );
  309. m_program[Program::Fill] =
  310. bgfx::createProgram(createEmbeddedShader(type, 4)
  311. , createEmbeddedShader(type, 5)
  312. , true
  313. );
  314. m_program[Program::FillLit] =
  315. bgfx::createProgram(createEmbeddedShader(type, 6)
  316. , createEmbeddedShader(type, 7)
  317. , true
  318. );
  319. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
  320. void* vertices[Mesh::Count] = {};
  321. uint16_t* indices[Mesh::Count] = {};
  322. uint16_t stride = DebugShapeVertex::ms_decl.getStride();
  323. uint32_t startVertex = 0;
  324. uint32_t startIndex = 0;
  325. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  326. {
  327. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  328. const uint8_t tess = uint8_t(3-mesh);
  329. const uint32_t numVertices = genSphere(tess);
  330. const uint32_t numIndices = numVertices;
  331. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  332. genSphere(tess, vertices[id], stride);
  333. uint16_t* trilist = (uint16_t*)BX_ALLOC(m_allocator, numIndices*sizeof(uint16_t) );
  334. for (uint32_t ii = 0; ii < numIndices; ++ii)
  335. {
  336. trilist[ii] = uint16_t(ii);
  337. }
  338. uint32_t numLineListIndices = bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  339. , NULL
  340. , 0
  341. , trilist
  342. , numIndices
  343. , false
  344. );
  345. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  346. uint16_t* indicesOut = indices[id];
  347. memcpy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
  348. bgfx::topologyConvert(bgfx::TopologyConvert::TriListToLineList
  349. , &indicesOut[numIndices]
  350. , numLineListIndices*sizeof(uint16_t)
  351. , trilist
  352. , numIndices
  353. , false
  354. );
  355. m_mesh[id].m_startVertex = startVertex;
  356. m_mesh[id].m_numVertices = numVertices;
  357. m_mesh[id].m_startIndex[0] = startIndex;
  358. m_mesh[id].m_numIndices[0] = numIndices;
  359. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  360. m_mesh[id].m_numIndices[1] = numLineListIndices;
  361. startVertex += numVertices;
  362. startIndex += numIndices + numLineListIndices;
  363. BX_FREE(m_allocator, trilist);
  364. }
  365. m_mesh[Mesh::Cube].m_startVertex = startVertex;
  366. m_mesh[Mesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
  367. m_mesh[Mesh::Cube].m_startIndex[0] = startIndex;
  368. m_mesh[Mesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
  369. m_mesh[Mesh::Cube].m_startIndex[1] = 0;
  370. m_mesh[Mesh::Cube].m_numIndices[1] = 0;
  371. startVertex += m_mesh[Mesh::Cube].m_numVertices;
  372. startIndex += m_mesh[Mesh::Cube].m_numIndices[0];
  373. const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
  374. const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
  375. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  376. {
  377. Mesh::Enum id = Mesh::Enum(Mesh::Sphere0+mesh);
  378. memcpy(&vb->data[m_mesh[id].m_startVertex * stride]
  379. , vertices[id]
  380. , m_mesh[id].m_numVertices*stride
  381. );
  382. memcpy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
  383. , indices[id]
  384. , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
  385. );
  386. BX_FREE(m_allocator, vertices[id]);
  387. BX_FREE(m_allocator, indices[id]);
  388. }
  389. memcpy(&vb->data[m_mesh[Mesh::Cube].m_startVertex * stride]
  390. , s_cubeVertices
  391. , sizeof(s_cubeVertices)
  392. );
  393. memcpy(&ib->data[m_mesh[Mesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
  394. , s_cubeIndices
  395. , sizeof(s_cubeIndices)
  396. );
  397. m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_decl);
  398. m_ibh = bgfx::createIndexBuffer(ib);
  399. m_mtx = 0;
  400. m_viewId = 0;
  401. m_pos = 0;
  402. m_indexPos = 0;
  403. m_vertexPos = 0;
  404. }
  405. void shutdown()
  406. {
  407. bgfx::destroyIndexBuffer(m_ibh);
  408. bgfx::destroyVertexBuffer(m_vbh);
  409. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  410. {
  411. bgfx::destroyProgram(m_program[ii]);
  412. }
  413. bgfx::destroyUniform(u_params);
  414. }
  415. void begin(uint8_t _viewId)
  416. {
  417. BX_CHECK(State::Count == m_state);
  418. m_viewId = _viewId;
  419. m_mtx = 0;
  420. m_state = State::None;
  421. m_stack = 0;
  422. Attrib& attrib = m_attrib[0];
  423. attrib.m_state = 0
  424. | BGFX_STATE_RGB_WRITE
  425. | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER)
  426. | BGFX_STATE_CULL_CW
  427. | BGFX_STATE_DEPTH_WRITE
  428. ;
  429. attrib.m_scale = 1.0f;
  430. attrib.m_offset = 0.0f;
  431. attrib.m_abgr = UINT32_MAX;
  432. attrib.m_stipple = false;
  433. attrib.m_wireframe = false;
  434. attrib.m_lod = 0;
  435. }
  436. void end()
  437. {
  438. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  439. flush();
  440. m_state = State::Count;
  441. }
  442. void push()
  443. {
  444. BX_CHECK(State::Count != m_state);
  445. ++m_stack;
  446. m_attrib[m_stack] = m_attrib[m_stack-1];
  447. }
  448. void pop()
  449. {
  450. BX_CHECK(State::Count != m_state);
  451. const Attrib& curr = m_attrib[m_stack];
  452. const Attrib& prev = m_attrib[m_stack-1];
  453. if (curr.m_stipple != prev.m_stipple
  454. || curr.m_state != prev.m_state)
  455. {
  456. flush();
  457. }
  458. --m_stack;
  459. }
  460. void setTransform(const void* _mtx)
  461. {
  462. BX_CHECK(State::Count != m_state);
  463. flush();
  464. if (NULL == _mtx)
  465. {
  466. m_mtx = 0;
  467. return;
  468. }
  469. bgfx::Transform transform;
  470. m_mtx = bgfx::allocTransform(&transform, 1);
  471. memcpy(transform.data, _mtx, 64);
  472. }
  473. void setTranslate(float _x, float _y, float _z)
  474. {
  475. float mtx[16];
  476. bx::mtxTranslate(mtx, _x, _y, _z);
  477. setTransform(mtx);
  478. }
  479. void setTranslate(const float* _pos)
  480. {
  481. setTranslate(_pos[0], _pos[1], _pos[2]);
  482. }
  483. void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  484. {
  485. const uint64_t depthTest = m_depthTestLess
  486. ? BGFX_STATE_DEPTH_TEST_LESS
  487. : BGFX_STATE_DEPTH_TEST_GREATER
  488. ;
  489. m_attrib[m_stack].m_state &= ~(0
  490. | BGFX_STATE_DEPTH_TEST_MASK
  491. | BGFX_STATE_DEPTH_WRITE
  492. | BGFX_STATE_CULL_CW
  493. | BGFX_STATE_CULL_CCW
  494. );
  495. m_attrib[m_stack].m_state |= _depthTest
  496. ? depthTest
  497. : 0
  498. ;
  499. m_attrib[m_stack].m_state |= _depthWrite
  500. ? BGFX_STATE_DEPTH_WRITE
  501. : 0
  502. ;
  503. m_attrib[m_stack].m_state |= _clockwise
  504. ? BGFX_STATE_CULL_CW
  505. : BGFX_STATE_CULL_CCW
  506. ;
  507. }
  508. void setColor(uint32_t _abgr)
  509. {
  510. BX_CHECK(State::Count != m_state);
  511. m_attrib[m_stack].m_abgr = _abgr;
  512. }
  513. void setLod(uint8_t _lod)
  514. {
  515. BX_CHECK(State::Count != m_state);
  516. m_attrib[m_stack].m_lod = _lod;
  517. }
  518. void setWireframe(bool _wireframe)
  519. {
  520. BX_CHECK(State::Count != m_state);
  521. m_attrib[m_stack].m_wireframe = _wireframe;
  522. }
  523. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  524. {
  525. BX_CHECK(State::Count != m_state);
  526. Attrib& attrib = m_attrib[m_stack];
  527. if (attrib.m_stipple != _stipple)
  528. {
  529. flush();
  530. }
  531. attrib.m_stipple = _stipple;
  532. attrib.m_offset = _offset;
  533. attrib.m_scale = _scale;
  534. }
  535. void moveTo(float _x, float _y, float _z = 0.0f)
  536. {
  537. BX_CHECK(State::Count != m_state);
  538. softFlush();
  539. m_state = State::MoveTo;
  540. DebugVertex& vertex = m_cache[m_pos];
  541. vertex.m_x = _x;
  542. vertex.m_y = _y;
  543. vertex.m_z = _z;
  544. Attrib& attrib = m_attrib[m_stack];
  545. vertex.m_abgr = attrib.m_abgr;
  546. vertex.m_len = attrib.m_offset;
  547. m_vertexPos = m_pos;
  548. }
  549. void moveTo(const void* _pos)
  550. {
  551. BX_CHECK(State::Count != m_state);
  552. const float* pos = (const float*)_pos;
  553. moveTo(pos[0], pos[1], pos[2]);
  554. }
  555. void moveTo(Axis::Enum _axis, float _x, float _y)
  556. {
  557. float pos[3];
  558. getPoint(pos, _axis, _x, _y);
  559. moveTo(pos);
  560. }
  561. void lineTo(float _x, float _y, float _z = 0.0f)
  562. {
  563. BX_CHECK(State::Count != m_state);
  564. if (State::None == m_state)
  565. {
  566. moveTo(_x, _y, _z);
  567. return;
  568. }
  569. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  570. {
  571. uint32_t pos = m_pos;
  572. uint32_t vertexPos = m_vertexPos;
  573. flush();
  574. memcpy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  575. if (vertexPos == pos)
  576. {
  577. m_pos = 1;
  578. }
  579. else
  580. {
  581. memcpy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  582. m_pos = 2;
  583. }
  584. m_state = State::LineTo;
  585. }
  586. else if (State::MoveTo == m_state)
  587. {
  588. ++m_pos;
  589. m_state = State::LineTo;
  590. }
  591. uint16_t prev = m_pos-1;
  592. uint16_t curr = m_pos++;
  593. DebugVertex& vertex = m_cache[curr];
  594. vertex.m_x = _x;
  595. vertex.m_y = _y;
  596. vertex.m_z = _z;
  597. Attrib& attrib = m_attrib[m_stack];
  598. vertex.m_abgr = attrib.m_abgr;
  599. vertex.m_len = attrib.m_offset;
  600. float tmp[3];
  601. bx::vec3Sub(tmp, &vertex.m_x, &m_cache[prev].m_x);
  602. float len = bx::vec3Length(tmp) * attrib.m_scale;
  603. vertex.m_len = m_cache[prev].m_len + len;
  604. m_indices[m_indexPos++] = prev;
  605. m_indices[m_indexPos++] = curr;
  606. }
  607. void lineTo(const void* _pos)
  608. {
  609. BX_CHECK(State::Count != m_state);
  610. const float* pos = (const float*)_pos;
  611. lineTo(pos[0], pos[1], pos[2]);
  612. }
  613. void lineTo(Axis::Enum _axis, float _x, float _y)
  614. {
  615. float pos[3];
  616. getPoint(pos, _axis, _x, _y);
  617. lineTo(pos);
  618. }
  619. void close()
  620. {
  621. BX_CHECK(State::Count != m_state);
  622. DebugVertex& vertex = m_cache[m_vertexPos];
  623. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  624. m_state = State::None;
  625. }
  626. void draw(const Aabb& _aabb)
  627. {
  628. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  629. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  630. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  631. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  632. close();
  633. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  634. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  635. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  636. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  637. close();
  638. moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
  639. lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
  640. moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
  641. lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
  642. moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
  643. lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
  644. moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
  645. lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
  646. }
  647. void draw(const Cylinder& _cylinder, bool _capsule)
  648. {
  649. BX_UNUSED(_cylinder, _capsule);
  650. }
  651. void draw(const Disk& _disk)
  652. {
  653. BX_UNUSED(_disk);
  654. }
  655. void draw(const Obb& _obb)
  656. {
  657. const Attrib& attrib = m_attrib[m_stack];
  658. if (attrib.m_wireframe)
  659. {
  660. setTransform(_obb.m_mtx);
  661. moveTo(-1.0f, -1.0f, -1.0f);
  662. lineTo( 1.0f, -1.0f, -1.0f);
  663. lineTo( 1.0f, 1.0f, -1.0f);
  664. lineTo(-1.0f, 1.0f, -1.0f);
  665. close();
  666. moveTo(-1.0f, 1.0f, 1.0f);
  667. lineTo( 1.0f, 1.0f, 1.0f);
  668. lineTo( 1.0f, -1.0f, 1.0f);
  669. lineTo(-1.0f, -1.0f, 1.0f);
  670. close();
  671. moveTo( 1.0f, -1.0f, -1.0f);
  672. lineTo( 1.0f, -1.0f, 1.0f);
  673. moveTo( 1.0f, 1.0f, -1.0f);
  674. lineTo( 1.0f, 1.0f, 1.0f);
  675. moveTo(-1.0f, 1.0f, -1.0f);
  676. lineTo(-1.0f, 1.0f, 1.0f);
  677. moveTo(-1.0f, -1.0f, -1.0f);
  678. lineTo(-1.0f, -1.0f, 1.0f);
  679. setTransform(NULL);
  680. }
  681. else
  682. {
  683. draw(Mesh::Cube, _obb.m_mtx, false);
  684. }
  685. }
  686. void draw(const Sphere& _sphere)
  687. {
  688. const Attrib& attrib = m_attrib[m_stack];
  689. float mtx[16];
  690. bx::mtxSRT(mtx
  691. , _sphere.m_radius
  692. , _sphere.m_radius
  693. , _sphere.m_radius
  694. , 0.0f
  695. , 0.0f
  696. , 0.0f
  697. , _sphere.m_center[0]
  698. , _sphere.m_center[1]
  699. , _sphere.m_center[2]
  700. );
  701. uint8_t lod = attrib.m_lod > Mesh::SphereMaxLod
  702. ? uint8_t(Mesh::SphereMaxLod)
  703. : attrib.m_lod
  704. ;
  705. draw(Mesh::Enum(Mesh::Sphere0 + lod), mtx, attrib.m_wireframe);
  706. }
  707. void drawFrustum(const float* _viewProj)
  708. {
  709. Plane planes[6];
  710. buildFrustumPlanes(planes, _viewProj);
  711. float points[24];
  712. intersectPlanes(&points[ 0], planes[0], planes[2], planes[4]);
  713. intersectPlanes(&points[ 3], planes[0], planes[3], planes[4]);
  714. intersectPlanes(&points[ 6], planes[0], planes[3], planes[5]);
  715. intersectPlanes(&points[ 9], planes[0], planes[2], planes[5]);
  716. intersectPlanes(&points[12], planes[1], planes[2], planes[4]);
  717. intersectPlanes(&points[15], planes[1], planes[3], planes[4]);
  718. intersectPlanes(&points[18], planes[1], planes[3], planes[5]);
  719. intersectPlanes(&points[21], planes[1], planes[2], planes[5]);
  720. moveTo(&points[ 0]);
  721. lineTo(&points[ 3]);
  722. lineTo(&points[ 6]);
  723. lineTo(&points[ 9]);
  724. close();
  725. moveTo(&points[12]);
  726. lineTo(&points[15]);
  727. lineTo(&points[18]);
  728. lineTo(&points[21]);
  729. close();
  730. moveTo(&points[ 0]);
  731. lineTo(&points[12]);
  732. moveTo(&points[ 3]);
  733. lineTo(&points[15]);
  734. moveTo(&points[ 6]);
  735. lineTo(&points[18]);
  736. moveTo(&points[ 9]);
  737. lineTo(&points[21]);
  738. }
  739. void drawFrustum(const void* _viewProj)
  740. {
  741. drawFrustum( (const float*)_viewProj);
  742. }
  743. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  744. {
  745. const Attrib& attrib = m_attrib[m_stack];
  746. const uint32_t num = getCircleLod(attrib.m_lod);
  747. const float step = bx::pi * 2.0f / num;
  748. _degrees = bx::fwrap(_degrees, 360.0f);
  749. float pos[3];
  750. getPoint(pos, _axis
  751. , bx::fsin(step * 0)*_radius
  752. , bx::fcos(step * 0)*_radius
  753. );
  754. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  755. uint32_t n = uint32_t(num*_degrees/360.0f);
  756. for (uint32_t ii = 1; ii < n+1; ++ii)
  757. {
  758. getPoint(pos, _axis
  759. , bx::fsin(step * ii)*_radius
  760. , bx::fcos(step * ii)*_radius
  761. );
  762. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  763. }
  764. moveTo(_x, _y, _z);
  765. getPoint(pos, _axis
  766. , bx::fsin(step * 0)*_radius
  767. , bx::fcos(step * 0)*_radius
  768. );
  769. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  770. getPoint(pos, _axis
  771. , bx::fsin(step * n)*_radius
  772. , bx::fcos(step * n)*_radius
  773. );
  774. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  775. lineTo(_x, _y, _z);
  776. }
  777. void drawCircle(const float* _normal, const float* _center, float _radius, float _weight = 0.0f)
  778. {
  779. const Attrib& attrib = m_attrib[m_stack];
  780. const uint32_t num = getCircleLod(attrib.m_lod);
  781. const float step = bx::pi * 2.0f / num;
  782. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  783. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  784. float udir[3];
  785. float vdir[3];
  786. calcPlaneUv(plane, udir, vdir);
  787. float pos[3];
  788. float tmp0[3];
  789. float tmp1[3];
  790. float xy0[2];
  791. float xy1[2];
  792. circle(xy0, 0.0f);
  793. squircle(xy1, 0.0f);
  794. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  795. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  796. bx::vec3Add(tmp1, pos, tmp0);
  797. bx::vec3Add(pos, tmp1, _center);
  798. moveTo(pos);
  799. for (uint32_t ii = 1; ii < num; ++ii)
  800. {
  801. float angle = step * ii;
  802. circle(xy0, angle);
  803. squircle(xy1, angle);
  804. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  805. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  806. bx::vec3Add(tmp1, pos, tmp0);
  807. bx::vec3Add(pos, tmp1, _center);
  808. lineTo(pos);
  809. }
  810. close();
  811. }
  812. void drawCircle(const void* _normal, const void* _center, float _radius, float _weight = 0.0f)
  813. {
  814. drawCircle( (const float*)_normal, (const float*)_center, _radius, _weight);
  815. }
  816. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight = 0.0f)
  817. {
  818. const Attrib& attrib = m_attrib[m_stack];
  819. const uint32_t num = getCircleLod(attrib.m_lod);
  820. const float step = bx::pi * 2.0f / num;
  821. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  822. float xy0[2];
  823. float xy1[2];
  824. circle(xy0, 0.0f);
  825. squircle(xy1, 0.0f);
  826. float pos[3];
  827. getPoint(pos, _axis
  828. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  829. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  830. );
  831. moveTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  832. for (uint32_t ii = 1; ii < num; ++ii)
  833. {
  834. float angle = step * ii;
  835. circle(xy0, angle);
  836. squircle(xy1, angle);
  837. getPoint(pos, _axis
  838. , bx::flerp(xy0[0], xy1[0], _weight)*_radius
  839. , bx::flerp(xy0[1], xy1[1], _weight)*_radius
  840. );
  841. lineTo(pos[0] + _x, pos[1] + _y, pos[2] + _z);
  842. }
  843. close();
  844. }
  845. void drawCone(const float* _from, const float* _to, float _radius, float _weight = 0.0f)
  846. {
  847. const Attrib& attrib = m_attrib[m_stack];
  848. const uint32_t num = getCircleLod(attrib.m_lod);
  849. const float step = bx::pi * 2.0f / num;
  850. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  851. float pos[3];
  852. float tmp0[3];
  853. float tmp1[3];
  854. bx::vec3Sub(tmp0, _from, _to);
  855. Plane plane;
  856. plane.m_dist = 0.0f;
  857. bx::vec3Norm(plane.m_normal, tmp0);
  858. float udir[3];
  859. float vdir[3];
  860. calcPlaneUv(plane, udir, vdir);
  861. float xy0[2];
  862. float xy1[2];
  863. circle(xy0, 0.0f);
  864. squircle(xy1, 0.0f);
  865. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  866. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  867. bx::vec3Add(tmp1, pos, tmp0);
  868. bx::vec3Add(pos, tmp1, _from);
  869. moveTo(pos);
  870. for (uint32_t ii = 1; ii < num; ++ii)
  871. {
  872. float angle = step * ii;
  873. circle(xy0, angle);
  874. squircle(xy1, angle);
  875. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  876. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  877. bx::vec3Add(tmp1, pos, tmp0);
  878. bx::vec3Add(pos, tmp1, _from);
  879. lineTo(pos);
  880. }
  881. close();
  882. for (uint32_t ii = 0; ii < num; ++ii)
  883. {
  884. float angle = step * ii;
  885. circle(xy0, angle);
  886. squircle(xy1, angle);
  887. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  888. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  889. bx::vec3Add(tmp1, pos, tmp0);
  890. bx::vec3Add(pos, tmp1, _from);
  891. moveTo(pos);
  892. lineTo(_to);
  893. }
  894. }
  895. void drawCone(const void* _from, const void* _to, float _radius, float _weight = 0.0f)
  896. {
  897. drawCone( (const float*)_from, (const float*)_to, _radius, _weight);
  898. }
  899. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  900. {
  901. push();
  902. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  903. moveTo(_x, _y, _z);
  904. lineTo(_x + _len, _y, _z);
  905. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  906. moveTo(_x, _y, _z);
  907. lineTo(_x, _y + _len, _z);
  908. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  909. moveTo(_x, _y, _z);
  910. lineTo(_x, _y, _z + _len);
  911. pop();
  912. }
  913. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  914. {
  915. float udir[3];
  916. float vdir[3];
  917. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  918. calcPlaneUv(plane, udir, vdir);
  919. bx::vec3Mul(udir, udir, _step);
  920. bx::vec3Mul(vdir, vdir, _step);
  921. const uint32_t num = (_size/2)*2+1;
  922. const float halfExtent = float(_size/2);
  923. float umin[3];
  924. bx::vec3Mul(umin, udir, -halfExtent);
  925. float umax[3];
  926. bx::vec3Mul(umax, udir, halfExtent);
  927. float vmin[3];
  928. bx::vec3Mul(vmin, vdir, -halfExtent);
  929. float vmax[3];
  930. bx::vec3Mul(vmax, vdir, halfExtent);
  931. float tmp[3];
  932. float xs[3];
  933. float xe[3];
  934. bx::vec3Add(tmp, umin, vmin);
  935. bx::vec3Add(xs, _center, tmp);
  936. bx::vec3Add(tmp, umax, vmin);
  937. bx::vec3Add(xe, _center, tmp);
  938. float ys[3];
  939. float ye[3];
  940. bx::vec3Add(tmp, umin, vmin);
  941. bx::vec3Add(ys, _center, tmp);
  942. bx::vec3Add(tmp, umin, vmax);
  943. bx::vec3Add(ye, _center, tmp);
  944. for (uint32_t ii = 0; ii < num; ++ii)
  945. {
  946. moveTo(xs);
  947. lineTo(xe);
  948. bx::vec3Add(xs, xs, vdir);
  949. bx::vec3Add(xe, xe, vdir);
  950. moveTo(ys);
  951. lineTo(ye);
  952. bx::vec3Add(ys, ys, udir);
  953. bx::vec3Add(ye, ye, udir);
  954. }
  955. }
  956. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  957. {
  958. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  959. }
  960. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  961. {
  962. push();
  963. setTranslate(_center);
  964. const uint32_t num = (_size/2)*2-1;
  965. const float halfExtent = float(_size/2) * _step;
  966. setColor(0xff606060);
  967. float yy = -halfExtent + _step;
  968. for (uint32_t ii = 0; ii < num; ++ii)
  969. {
  970. moveTo(_axis, -halfExtent, yy);
  971. lineTo(_axis, halfExtent, yy);
  972. moveTo(_axis, yy, -halfExtent);
  973. lineTo(_axis, yy, halfExtent);
  974. yy += _step;
  975. }
  976. setColor(0xff101010);
  977. moveTo(_axis, -halfExtent, -halfExtent);
  978. lineTo(_axis, -halfExtent, halfExtent);
  979. lineTo(_axis, halfExtent, halfExtent);
  980. lineTo(_axis, halfExtent, -halfExtent);
  981. close();
  982. moveTo(_axis, -halfExtent, 0.0f);
  983. lineTo(_axis, halfExtent, 0.0f);
  984. moveTo(_axis, 0.0f, -halfExtent);
  985. lineTo(_axis, 0.0f, halfExtent);
  986. pop();
  987. }
  988. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  989. {
  990. drawGrid(_axis, (const float*)_center, _size, _step);
  991. }
  992. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  993. {
  994. push();
  995. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  996. drawCircle(Axis::X, _x, _y, _z, _radius);
  997. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  998. drawCircle(Axis::Y, _x, _y, _z, _radius);
  999. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1000. drawCircle(Axis::Z, _x, _y, _z, _radius);
  1001. pop();
  1002. }
  1003. private:
  1004. struct Mesh
  1005. {
  1006. enum Enum
  1007. {
  1008. Sphere0,
  1009. Sphere1,
  1010. Sphere2,
  1011. Sphere3,
  1012. Cube,
  1013. Count,
  1014. SphereMaxLod = Sphere3 - Sphere0,
  1015. };
  1016. uint32_t m_startVertex;
  1017. uint32_t m_numVertices;
  1018. uint32_t m_startIndex[2];
  1019. uint32_t m_numIndices[2];
  1020. };
  1021. struct Program
  1022. {
  1023. enum Enum
  1024. {
  1025. Lines,
  1026. LinesStipple,
  1027. Fill,
  1028. FillLit,
  1029. Count
  1030. };
  1031. };
  1032. void draw(Mesh::Enum _mesh, const float* _mtx, bool _wireframe) const
  1033. {
  1034. const Mesh& mesh = m_mesh[_mesh];
  1035. const Attrib& attrib = m_attrib[m_stack];
  1036. if (0 != mesh.m_numIndices[_wireframe])
  1037. {
  1038. bgfx::setIndexBuffer(m_ibh
  1039. , mesh.m_startIndex[_wireframe]
  1040. , mesh.m_numIndices[_wireframe]
  1041. );
  1042. }
  1043. float params[4][4] =
  1044. {
  1045. {
  1046. 0.0f,
  1047. -1.0f,
  1048. 0.0f,
  1049. 3.0f,
  1050. },
  1051. {
  1052. 1.0f,
  1053. 0.9f,
  1054. 0.8f,
  1055. 0.0f,
  1056. },
  1057. {
  1058. 0.2f,
  1059. 0.22f,
  1060. 0.5f,
  1061. 0.0f,
  1062. },
  1063. {
  1064. ( (attrib.m_abgr>>24) )/255.0f,
  1065. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1066. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1067. ( (attrib.m_abgr )&0xff)/255.0f,
  1068. },
  1069. };
  1070. bx::vec3Norm(params[0], params[0]);
  1071. bgfx::setUniform(u_params, params, 4);
  1072. bgfx::setTransform(_mtx);
  1073. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1074. bgfx::setState(0
  1075. | attrib.m_state
  1076. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA : 0)
  1077. );
  1078. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1079. }
  1080. void softFlush()
  1081. {
  1082. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1083. {
  1084. flush();
  1085. }
  1086. }
  1087. void flush()
  1088. {
  1089. if (0 != m_pos)
  1090. {
  1091. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1092. {
  1093. bgfx::TransientVertexBuffer tvb;
  1094. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1095. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1096. bgfx::TransientIndexBuffer tib;
  1097. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1098. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1099. const Attrib& attrib = m_attrib[m_stack];
  1100. bgfx::setVertexBuffer(&tvb);
  1101. bgfx::setIndexBuffer(&tib);
  1102. bgfx::setState(0
  1103. | BGFX_STATE_RGB_WRITE
  1104. | BGFX_STATE_PT_LINES
  1105. | attrib.m_state
  1106. | BGFX_STATE_LINEAA
  1107. | BGFX_STATE_BLEND_ALPHA
  1108. );
  1109. bgfx::setTransform(m_mtx);
  1110. bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0];
  1111. bgfx::submit(m_viewId, program);
  1112. }
  1113. m_state = State::None;
  1114. m_pos = 0;
  1115. m_indexPos = 0;
  1116. m_vertexPos = 0;
  1117. }
  1118. }
  1119. struct State
  1120. {
  1121. enum Enum
  1122. {
  1123. None,
  1124. MoveTo,
  1125. LineTo,
  1126. Count
  1127. };
  1128. };
  1129. static const uint32_t cacheSize = 1024;
  1130. static const uint32_t stackSize = 16;
  1131. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1132. DebugVertex m_cache[cacheSize+1];
  1133. uint32_t m_mtx;
  1134. uint16_t m_indices[cacheSize*2];
  1135. uint16_t m_pos;
  1136. uint16_t m_indexPos;
  1137. uint16_t m_vertexPos;
  1138. uint8_t m_viewId;
  1139. uint8_t m_stack;
  1140. bool m_depthTestLess;
  1141. struct Attrib
  1142. {
  1143. uint64_t m_state;
  1144. float m_offset;
  1145. float m_scale;
  1146. uint32_t m_abgr;
  1147. bool m_stipple;
  1148. bool m_wireframe;
  1149. uint8_t m_lod;
  1150. };
  1151. Attrib m_attrib[stackSize];
  1152. State::Enum m_state;
  1153. Mesh m_mesh[Mesh::Count];
  1154. bgfx::ProgramHandle m_program[Program::Count];
  1155. bgfx::UniformHandle u_params;
  1156. bgfx::VertexBufferHandle m_vbh;
  1157. bgfx::IndexBufferHandle m_ibh;
  1158. bx::AllocatorI* m_allocator;
  1159. };
  1160. static DebugDraw s_dd;
  1161. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1162. {
  1163. s_dd.init(_depthTestLess, _allocator);
  1164. }
  1165. void ddShutdown()
  1166. {
  1167. s_dd.shutdown();
  1168. }
  1169. void ddBegin(uint8_t _viewId)
  1170. {
  1171. s_dd.begin(_viewId);
  1172. }
  1173. void ddEnd()
  1174. {
  1175. s_dd.end();
  1176. }
  1177. void ddPush()
  1178. {
  1179. s_dd.push();
  1180. }
  1181. void ddPop()
  1182. {
  1183. s_dd.pop();
  1184. }
  1185. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1186. {
  1187. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1188. }
  1189. void ddSetColor(uint32_t _abgr)
  1190. {
  1191. s_dd.setColor(_abgr);
  1192. }
  1193. void ddSetLod(uint8_t _lod)
  1194. {
  1195. s_dd.setLod(_lod);
  1196. }
  1197. void ddSetWireframe(bool _wireframe)
  1198. {
  1199. s_dd.setWireframe(_wireframe);
  1200. }
  1201. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1202. {
  1203. s_dd.setStipple(_stipple, _scale, _offset);
  1204. }
  1205. void ddSetTransform(const void* _mtx)
  1206. {
  1207. s_dd.setTransform(_mtx);
  1208. }
  1209. void ddSetTranslate(float _x, float _y, float _z)
  1210. {
  1211. s_dd.setTranslate(_x, _y, _z);
  1212. }
  1213. void ddMoveTo(float _x, float _y, float _z)
  1214. {
  1215. s_dd.moveTo(_x, _y, _z);
  1216. }
  1217. void ddMoveTo(const void* _pos)
  1218. {
  1219. s_dd.moveTo(_pos);
  1220. }
  1221. void ddLineTo(float _x, float _y, float _z)
  1222. {
  1223. s_dd.lineTo(_x, _y, _z);
  1224. }
  1225. void ddLineTo(const void* _pos)
  1226. {
  1227. s_dd.lineTo(_pos);
  1228. }
  1229. void ddClose()
  1230. {
  1231. s_dd.close();
  1232. }
  1233. void ddDraw(const Aabb& _aabb)
  1234. {
  1235. s_dd.draw(_aabb);
  1236. }
  1237. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1238. {
  1239. s_dd.draw(_cylinder, _capsule);
  1240. }
  1241. void ddDraw(const Disk& _disk)
  1242. {
  1243. s_dd.draw(_disk);
  1244. }
  1245. void ddDraw(const Obb& _obb)
  1246. {
  1247. s_dd.draw(_obb);
  1248. }
  1249. void ddDraw(const Sphere& _sphere)
  1250. {
  1251. s_dd.draw(_sphere);
  1252. }
  1253. void ddDrawFrustum(const void* _viewProj)
  1254. {
  1255. s_dd.drawFrustum(_viewProj);
  1256. }
  1257. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1258. {
  1259. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1260. }
  1261. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1262. {
  1263. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1264. }
  1265. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1266. {
  1267. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1268. }
  1269. void ddDrawCone(const void* _from, const void* _to, float _radius, float _weight)
  1270. {
  1271. s_dd.drawCone(_from, _to, _radius, _weight);
  1272. }
  1273. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1274. {
  1275. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1276. }
  1277. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1278. {
  1279. s_dd.drawGrid(_normal, _center, _size, _step);
  1280. }
  1281. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1282. {
  1283. s_dd.drawGrid(_axis, _center, _size, _step);
  1284. }
  1285. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1286. {
  1287. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1288. }