debugdraw.cpp 31 KB

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