debugdraw.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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 drawCylinder(const float* _from, const float* _to, float _radius, float _weight = 0.0f)
  900. {
  901. const Attrib& attrib = m_attrib[m_stack];
  902. const uint32_t num = getCircleLod(attrib.m_lod);
  903. const float step = bx::pi * 2.0f / num;
  904. _weight = bx::fclamp(_weight, 0.0f, 2.0f);
  905. float pos[3];
  906. float tmp0[3];
  907. float tmp1[3];
  908. bx::vec3Sub(tmp0, _from, _to);
  909. Plane plane;
  910. plane.m_dist = 0.0f;
  911. bx::vec3Norm(plane.m_normal, tmp0);
  912. float udir[3];
  913. float vdir[3];
  914. calcPlaneUv(plane, udir, vdir);
  915. float xy0[2];
  916. float xy1[2];
  917. circle(xy0, 0.0f);
  918. squircle(xy1, 0.0f);
  919. float pos1[3];
  920. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  921. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  922. bx::vec3Add(tmp1, pos, tmp0);
  923. bx::vec3Add(pos, tmp1, _from);
  924. bx::vec3Add(pos1, tmp1, _to);
  925. for (uint32_t ii = 1; ii < num+1; ++ii)
  926. {
  927. float angle = step * ii;
  928. circle(xy0, angle);
  929. squircle(xy1, angle);
  930. moveTo(pos); lineTo(pos1);
  931. moveTo(pos);
  932. bx::vec3Mul(pos, udir, bx::flerp(xy0[0], xy1[0], _weight)*_radius);
  933. bx::vec3Mul(tmp0, vdir, bx::flerp(xy0[1], xy1[1], _weight)*_radius);
  934. bx::vec3Add(tmp1, pos, tmp0);
  935. bx::vec3Add(pos, tmp1, _from);
  936. lineTo(pos);
  937. moveTo(pos1);
  938. bx::vec3Add(pos1, tmp1, _to);
  939. lineTo(pos1);
  940. }
  941. }
  942. void drawCylinder(const void* _from, const void* _to, float _radius, float _weight = 0.0f)
  943. {
  944. drawCylinder( (const float*)_from, (const float*)_to, _radius, _weight);
  945. }
  946. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  947. {
  948. push();
  949. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  950. moveTo(_x, _y, _z);
  951. lineTo(_x + _len, _y, _z);
  952. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  953. moveTo(_x, _y, _z);
  954. lineTo(_x, _y + _len, _z);
  955. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  956. moveTo(_x, _y, _z);
  957. lineTo(_x, _y, _z + _len);
  958. pop();
  959. }
  960. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  961. {
  962. float udir[3];
  963. float vdir[3];
  964. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  965. calcPlaneUv(plane, udir, vdir);
  966. bx::vec3Mul(udir, udir, _step);
  967. bx::vec3Mul(vdir, vdir, _step);
  968. const uint32_t num = (_size/2)*2+1;
  969. const float halfExtent = float(_size/2);
  970. float umin[3];
  971. bx::vec3Mul(umin, udir, -halfExtent);
  972. float umax[3];
  973. bx::vec3Mul(umax, udir, halfExtent);
  974. float vmin[3];
  975. bx::vec3Mul(vmin, vdir, -halfExtent);
  976. float vmax[3];
  977. bx::vec3Mul(vmax, vdir, halfExtent);
  978. float tmp[3];
  979. float xs[3];
  980. float xe[3];
  981. bx::vec3Add(tmp, umin, vmin);
  982. bx::vec3Add(xs, _center, tmp);
  983. bx::vec3Add(tmp, umax, vmin);
  984. bx::vec3Add(xe, _center, tmp);
  985. float ys[3];
  986. float ye[3];
  987. bx::vec3Add(tmp, umin, vmin);
  988. bx::vec3Add(ys, _center, tmp);
  989. bx::vec3Add(tmp, umin, vmax);
  990. bx::vec3Add(ye, _center, tmp);
  991. for (uint32_t ii = 0; ii < num; ++ii)
  992. {
  993. moveTo(xs);
  994. lineTo(xe);
  995. bx::vec3Add(xs, xs, vdir);
  996. bx::vec3Add(xe, xe, vdir);
  997. moveTo(ys);
  998. lineTo(ye);
  999. bx::vec3Add(ys, ys, udir);
  1000. bx::vec3Add(ye, ye, udir);
  1001. }
  1002. }
  1003. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1004. {
  1005. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  1006. }
  1007. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  1008. {
  1009. push();
  1010. setTranslate(_center);
  1011. const uint32_t num = (_size/2)*2-1;
  1012. const float halfExtent = float(_size/2) * _step;
  1013. setColor(0xff606060);
  1014. float yy = -halfExtent + _step;
  1015. for (uint32_t ii = 0; ii < num; ++ii)
  1016. {
  1017. moveTo(_axis, -halfExtent, yy);
  1018. lineTo(_axis, halfExtent, yy);
  1019. moveTo(_axis, yy, -halfExtent);
  1020. lineTo(_axis, yy, halfExtent);
  1021. yy += _step;
  1022. }
  1023. setColor(0xff101010);
  1024. moveTo(_axis, -halfExtent, -halfExtent);
  1025. lineTo(_axis, -halfExtent, halfExtent);
  1026. lineTo(_axis, halfExtent, halfExtent);
  1027. lineTo(_axis, halfExtent, -halfExtent);
  1028. close();
  1029. moveTo(_axis, -halfExtent, 0.0f);
  1030. lineTo(_axis, halfExtent, 0.0f);
  1031. moveTo(_axis, 0.0f, -halfExtent);
  1032. lineTo(_axis, 0.0f, halfExtent);
  1033. pop();
  1034. }
  1035. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1036. {
  1037. drawGrid(_axis, (const float*)_center, _size, _step);
  1038. }
  1039. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1040. {
  1041. push();
  1042. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  1043. drawCircle(Axis::X, _x, _y, _z, _radius);
  1044. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  1045. drawCircle(Axis::Y, _x, _y, _z, _radius);
  1046. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1047. drawCircle(Axis::Z, _x, _y, _z, _radius);
  1048. pop();
  1049. }
  1050. private:
  1051. struct Mesh
  1052. {
  1053. enum Enum
  1054. {
  1055. Sphere0,
  1056. Sphere1,
  1057. Sphere2,
  1058. Sphere3,
  1059. Cube,
  1060. Count,
  1061. SphereMaxLod = Sphere3 - Sphere0,
  1062. };
  1063. uint32_t m_startVertex;
  1064. uint32_t m_numVertices;
  1065. uint32_t m_startIndex[2];
  1066. uint32_t m_numIndices[2];
  1067. };
  1068. struct Program
  1069. {
  1070. enum Enum
  1071. {
  1072. Lines,
  1073. LinesStipple,
  1074. Fill,
  1075. FillLit,
  1076. Count
  1077. };
  1078. };
  1079. void draw(Mesh::Enum _mesh, const float* _mtx, bool _wireframe) const
  1080. {
  1081. const Mesh& mesh = m_mesh[_mesh];
  1082. const Attrib& attrib = m_attrib[m_stack];
  1083. if (0 != mesh.m_numIndices[_wireframe])
  1084. {
  1085. bgfx::setIndexBuffer(m_ibh
  1086. , mesh.m_startIndex[_wireframe]
  1087. , mesh.m_numIndices[_wireframe]
  1088. );
  1089. }
  1090. float params[4][4] =
  1091. {
  1092. {
  1093. 0.0f,
  1094. -1.0f,
  1095. 0.0f,
  1096. 3.0f,
  1097. },
  1098. {
  1099. 1.0f,
  1100. 0.9f,
  1101. 0.8f,
  1102. 0.0f,
  1103. },
  1104. {
  1105. 0.2f,
  1106. 0.22f,
  1107. 0.5f,
  1108. 0.0f,
  1109. },
  1110. {
  1111. ( (attrib.m_abgr>>24) )/255.0f,
  1112. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1113. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1114. ( (attrib.m_abgr )&0xff)/255.0f,
  1115. },
  1116. };
  1117. bx::vec3Norm(params[0], params[0]);
  1118. bgfx::setUniform(u_params, params, 4);
  1119. bgfx::setTransform(_mtx);
  1120. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1121. bgfx::setState(0
  1122. | attrib.m_state
  1123. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA : 0)
  1124. );
  1125. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1126. }
  1127. void softFlush()
  1128. {
  1129. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1130. {
  1131. flush();
  1132. }
  1133. }
  1134. void flush()
  1135. {
  1136. if (0 != m_pos)
  1137. {
  1138. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1139. {
  1140. bgfx::TransientVertexBuffer tvb;
  1141. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1142. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1143. bgfx::TransientIndexBuffer tib;
  1144. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1145. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1146. const Attrib& attrib = m_attrib[m_stack];
  1147. bgfx::setVertexBuffer(&tvb);
  1148. bgfx::setIndexBuffer(&tib);
  1149. bgfx::setState(0
  1150. | BGFX_STATE_RGB_WRITE
  1151. | BGFX_STATE_PT_LINES
  1152. | attrib.m_state
  1153. | BGFX_STATE_LINEAA
  1154. | BGFX_STATE_BLEND_ALPHA
  1155. );
  1156. bgfx::setTransform(m_mtx);
  1157. bgfx::ProgramHandle program = m_program[attrib.m_stipple ? 1 : 0];
  1158. bgfx::submit(m_viewId, program);
  1159. }
  1160. m_state = State::None;
  1161. m_pos = 0;
  1162. m_indexPos = 0;
  1163. m_vertexPos = 0;
  1164. }
  1165. }
  1166. struct State
  1167. {
  1168. enum Enum
  1169. {
  1170. None,
  1171. MoveTo,
  1172. LineTo,
  1173. Count
  1174. };
  1175. };
  1176. static const uint32_t cacheSize = 1024;
  1177. static const uint32_t stackSize = 16;
  1178. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1179. DebugVertex m_cache[cacheSize+1];
  1180. uint32_t m_mtx;
  1181. uint16_t m_indices[cacheSize*2];
  1182. uint16_t m_pos;
  1183. uint16_t m_indexPos;
  1184. uint16_t m_vertexPos;
  1185. uint8_t m_viewId;
  1186. uint8_t m_stack;
  1187. bool m_depthTestLess;
  1188. struct Attrib
  1189. {
  1190. uint64_t m_state;
  1191. float m_offset;
  1192. float m_scale;
  1193. uint32_t m_abgr;
  1194. bool m_stipple;
  1195. bool m_wireframe;
  1196. uint8_t m_lod;
  1197. };
  1198. Attrib m_attrib[stackSize];
  1199. State::Enum m_state;
  1200. Mesh m_mesh[Mesh::Count];
  1201. bgfx::ProgramHandle m_program[Program::Count];
  1202. bgfx::UniformHandle u_params;
  1203. bgfx::VertexBufferHandle m_vbh;
  1204. bgfx::IndexBufferHandle m_ibh;
  1205. bx::AllocatorI* m_allocator;
  1206. };
  1207. static DebugDraw s_dd;
  1208. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1209. {
  1210. s_dd.init(_depthTestLess, _allocator);
  1211. }
  1212. void ddShutdown()
  1213. {
  1214. s_dd.shutdown();
  1215. }
  1216. void ddBegin(uint8_t _viewId)
  1217. {
  1218. s_dd.begin(_viewId);
  1219. }
  1220. void ddEnd()
  1221. {
  1222. s_dd.end();
  1223. }
  1224. void ddPush()
  1225. {
  1226. s_dd.push();
  1227. }
  1228. void ddPop()
  1229. {
  1230. s_dd.pop();
  1231. }
  1232. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1233. {
  1234. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1235. }
  1236. void ddSetColor(uint32_t _abgr)
  1237. {
  1238. s_dd.setColor(_abgr);
  1239. }
  1240. void ddSetLod(uint8_t _lod)
  1241. {
  1242. s_dd.setLod(_lod);
  1243. }
  1244. void ddSetWireframe(bool _wireframe)
  1245. {
  1246. s_dd.setWireframe(_wireframe);
  1247. }
  1248. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1249. {
  1250. s_dd.setStipple(_stipple, _scale, _offset);
  1251. }
  1252. void ddSetTransform(const void* _mtx)
  1253. {
  1254. s_dd.setTransform(_mtx);
  1255. }
  1256. void ddSetTranslate(float _x, float _y, float _z)
  1257. {
  1258. s_dd.setTranslate(_x, _y, _z);
  1259. }
  1260. void ddMoveTo(float _x, float _y, float _z)
  1261. {
  1262. s_dd.moveTo(_x, _y, _z);
  1263. }
  1264. void ddMoveTo(const void* _pos)
  1265. {
  1266. s_dd.moveTo(_pos);
  1267. }
  1268. void ddLineTo(float _x, float _y, float _z)
  1269. {
  1270. s_dd.lineTo(_x, _y, _z);
  1271. }
  1272. void ddLineTo(const void* _pos)
  1273. {
  1274. s_dd.lineTo(_pos);
  1275. }
  1276. void ddClose()
  1277. {
  1278. s_dd.close();
  1279. }
  1280. void ddDraw(const Aabb& _aabb)
  1281. {
  1282. s_dd.draw(_aabb);
  1283. }
  1284. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1285. {
  1286. s_dd.draw(_cylinder, _capsule);
  1287. }
  1288. void ddDraw(const Disk& _disk)
  1289. {
  1290. s_dd.draw(_disk);
  1291. }
  1292. void ddDraw(const Obb& _obb)
  1293. {
  1294. s_dd.draw(_obb);
  1295. }
  1296. void ddDraw(const Sphere& _sphere)
  1297. {
  1298. s_dd.draw(_sphere);
  1299. }
  1300. void ddDrawFrustum(const void* _viewProj)
  1301. {
  1302. s_dd.drawFrustum(_viewProj);
  1303. }
  1304. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1305. {
  1306. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1307. }
  1308. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1309. {
  1310. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1311. }
  1312. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1313. {
  1314. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1315. }
  1316. void ddDrawCone(const void* _from, const void* _to, float _radius, float _weight)
  1317. {
  1318. s_dd.drawCone(_from, _to, _radius, _weight);
  1319. }
  1320. void ddDrawCylinder(const void* _from, const void* _to, float _radius, float _weight)
  1321. {
  1322. s_dd.drawCylinder(_from, _to, _radius, _weight);
  1323. }
  1324. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1325. {
  1326. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1327. }
  1328. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1329. {
  1330. s_dd.drawGrid(_normal, _center, _size, _step);
  1331. }
  1332. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1333. {
  1334. s_dd.drawGrid(_axis, _center, _size, _step);
  1335. }
  1336. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1337. {
  1338. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1339. }