2
0

debugdraw.cpp 33 KB

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