debugdraw.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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 drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight)
  846. {
  847. push();
  848. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  849. moveTo(_x, _y, _z);
  850. lineTo(_x + _len, _y, _z);
  851. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  852. moveTo(_x, _y, _z);
  853. lineTo(_x, _y + _len, _z);
  854. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  855. moveTo(_x, _y, _z);
  856. lineTo(_x, _y, _z + _len);
  857. pop();
  858. }
  859. void drawGrid(const float* _normal, const float* _center, uint32_t _size, float _step)
  860. {
  861. float udir[3];
  862. float vdir[3];
  863. Plane plane = { { _normal[0], _normal[1], _normal[2] }, 0.0f };
  864. calcPlaneUv(plane, udir, vdir);
  865. bx::vec3Mul(udir, udir, _step);
  866. bx::vec3Mul(vdir, vdir, _step);
  867. const uint32_t num = (_size/2)*2+1;
  868. const float halfExtent = float(_size/2);
  869. float umin[3];
  870. bx::vec3Mul(umin, udir, -halfExtent);
  871. float umax[3];
  872. bx::vec3Mul(umax, udir, halfExtent);
  873. float vmin[3];
  874. bx::vec3Mul(vmin, vdir, -halfExtent);
  875. float vmax[3];
  876. bx::vec3Mul(vmax, vdir, halfExtent);
  877. float tmp[3];
  878. float xs[3];
  879. float xe[3];
  880. bx::vec3Add(tmp, umin, vmin);
  881. bx::vec3Add(xs, _center, tmp);
  882. bx::vec3Add(tmp, umax, vmin);
  883. bx::vec3Add(xe, _center, tmp);
  884. float ys[3];
  885. float ye[3];
  886. bx::vec3Add(tmp, umin, vmin);
  887. bx::vec3Add(ys, _center, tmp);
  888. bx::vec3Add(tmp, umin, vmax);
  889. bx::vec3Add(ye, _center, tmp);
  890. for (uint32_t ii = 0; ii < num; ++ii)
  891. {
  892. moveTo(xs);
  893. lineTo(xe);
  894. bx::vec3Add(xs, xs, vdir);
  895. bx::vec3Add(xe, xe, vdir);
  896. moveTo(ys);
  897. lineTo(ye);
  898. bx::vec3Add(ys, ys, udir);
  899. bx::vec3Add(ye, ye, udir);
  900. }
  901. }
  902. void drawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  903. {
  904. drawGrid( (const float*)_normal, (const float*)_center, _size, _step);
  905. }
  906. void drawGrid(Axis::Enum _axis, const float* _center, uint32_t _size, float _step)
  907. {
  908. push();
  909. setTranslate(_center);
  910. const uint32_t num = (_size/2)*2-1;
  911. const float halfExtent = float(_size/2) * _step;
  912. setColor(0xff606060);
  913. float yy = -halfExtent + _step;
  914. for (uint32_t ii = 0; ii < num; ++ii)
  915. {
  916. moveTo(_axis, -halfExtent, yy);
  917. lineTo(_axis, halfExtent, yy);
  918. moveTo(_axis, yy, -halfExtent);
  919. lineTo(_axis, yy, halfExtent);
  920. yy += _step;
  921. }
  922. setColor(0xff101010);
  923. moveTo(_axis, -halfExtent, -halfExtent);
  924. lineTo(_axis, -halfExtent, halfExtent);
  925. lineTo(_axis, halfExtent, halfExtent);
  926. lineTo(_axis, halfExtent, -halfExtent);
  927. close();
  928. moveTo(_axis, -halfExtent, 0.0f);
  929. lineTo(_axis, halfExtent, 0.0f);
  930. moveTo(_axis, 0.0f, -halfExtent);
  931. lineTo(_axis, 0.0f, halfExtent);
  932. pop();
  933. }
  934. void drawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  935. {
  936. drawGrid(_axis, (const float*)_center, _size, _step);
  937. }
  938. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  939. {
  940. push();
  941. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  942. drawCircle(Axis::X, _x, _y, _z, _radius);
  943. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  944. drawCircle(Axis::Y, _x, _y, _z, _radius);
  945. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  946. drawCircle(Axis::Z, _x, _y, _z, _radius);
  947. pop();
  948. }
  949. private:
  950. struct Mesh
  951. {
  952. enum Enum
  953. {
  954. Sphere0,
  955. Sphere1,
  956. Sphere2,
  957. Sphere3,
  958. Cube,
  959. Count,
  960. SphereMaxLod = Sphere3 - Sphere0,
  961. };
  962. uint32_t m_startVertex;
  963. uint32_t m_numVertices;
  964. uint32_t m_startIndex[2];
  965. uint32_t m_numIndices[2];
  966. };
  967. struct Program
  968. {
  969. enum Enum
  970. {
  971. Lines,
  972. LinesStipple,
  973. Fill,
  974. FillLit,
  975. Count
  976. };
  977. };
  978. void draw(Mesh::Enum _mesh, const float* _mtx, bool _wireframe) const
  979. {
  980. const Mesh& mesh = m_mesh[_mesh];
  981. const Attrib& attrib = m_attrib[m_stack];
  982. if (0 != mesh.m_numIndices[_wireframe])
  983. {
  984. bgfx::setIndexBuffer(m_ibh
  985. , mesh.m_startIndex[_wireframe]
  986. , mesh.m_numIndices[_wireframe]
  987. );
  988. }
  989. float params[4][4] =
  990. {
  991. {
  992. 0.0f,
  993. -1.0f,
  994. 0.0f,
  995. 3.0f,
  996. },
  997. {
  998. 1.0f,
  999. 0.9f,
  1000. 0.8f,
  1001. 0.0f,
  1002. },
  1003. {
  1004. 0.2f,
  1005. 0.22f,
  1006. 0.5f,
  1007. 0.0f,
  1008. },
  1009. {
  1010. ( (attrib.m_abgr>>24) )/255.0f,
  1011. ( (attrib.m_abgr>>16)&0xff)/255.0f,
  1012. ( (attrib.m_abgr>> 8)&0xff)/255.0f,
  1013. ( (attrib.m_abgr )&0xff)/255.0f,
  1014. },
  1015. };
  1016. bx::vec3Norm(params[0], params[0]);
  1017. bgfx::setUniform(u_params, params, 4);
  1018. bgfx::setTransform(_mtx);
  1019. bgfx::setVertexBuffer(m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1020. bgfx::setState(0
  1021. | attrib.m_state
  1022. | (_wireframe ? BGFX_STATE_PT_LINES|BGFX_STATE_LINEAA|BGFX_STATE_BLEND_ALPHA : 0)
  1023. );
  1024. bgfx::submit(m_viewId, m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1025. }
  1026. void softFlush()
  1027. {
  1028. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1029. {
  1030. flush();
  1031. }
  1032. }
  1033. void flush()
  1034. {
  1035. if (0 != m_pos)
  1036. {
  1037. if (bgfx::checkAvailTransientBuffers(m_pos, DebugVertex::ms_decl, m_indexPos) )
  1038. {
  1039. bgfx::TransientVertexBuffer tvb;
  1040. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_decl);
  1041. memcpy(tvb.data, m_cache, m_pos * DebugVertex::ms_decl.m_stride);
  1042. bgfx::TransientIndexBuffer tib;
  1043. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1044. memcpy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1045. bgfx::setVertexBuffer(&tvb);
  1046. bgfx::setIndexBuffer(&tib);
  1047. bgfx::setState(0
  1048. | BGFX_STATE_RGB_WRITE
  1049. | BGFX_STATE_PT_LINES
  1050. | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LEQUAL : BGFX_STATE_DEPTH_TEST_GEQUAL)
  1051. | BGFX_STATE_DEPTH_WRITE
  1052. | BGFX_STATE_LINEAA
  1053. | BGFX_STATE_BLEND_ALPHA
  1054. );
  1055. bgfx::setTransform(m_mtx);
  1056. bgfx::ProgramHandle program = m_program[m_attrib[m_stack].m_stipple ? 1 : 0];
  1057. bgfx::submit(m_viewId, program);
  1058. }
  1059. m_state = State::None;
  1060. m_pos = 0;
  1061. m_indexPos = 0;
  1062. m_vertexPos = 0;
  1063. }
  1064. }
  1065. struct State
  1066. {
  1067. enum Enum
  1068. {
  1069. None,
  1070. MoveTo,
  1071. LineTo,
  1072. Count
  1073. };
  1074. };
  1075. static const uint32_t cacheSize = 1024;
  1076. static const uint32_t stackSize = 16;
  1077. BX_STATIC_ASSERT(cacheSize >= 3, "Cache must be at least 3 elements.");
  1078. DebugVertex m_cache[cacheSize+1];
  1079. uint32_t m_mtx;
  1080. uint16_t m_indices[cacheSize*2];
  1081. uint16_t m_pos;
  1082. uint16_t m_indexPos;
  1083. uint16_t m_vertexPos;
  1084. uint8_t m_viewId;
  1085. uint8_t m_stack;
  1086. bool m_depthTestLess;
  1087. struct Attrib
  1088. {
  1089. uint64_t m_state;
  1090. float m_offset;
  1091. float m_scale;
  1092. uint32_t m_abgr;
  1093. bool m_stipple;
  1094. bool m_wireframe;
  1095. uint8_t m_lod;
  1096. };
  1097. Attrib m_attrib[stackSize];
  1098. State::Enum m_state;
  1099. Mesh m_mesh[Mesh::Count];
  1100. bgfx::ProgramHandle m_program[Program::Count];
  1101. bgfx::UniformHandle u_params;
  1102. bgfx::VertexBufferHandle m_vbh;
  1103. bgfx::IndexBufferHandle m_ibh;
  1104. bx::AllocatorI* m_allocator;
  1105. };
  1106. static DebugDraw s_dd;
  1107. void ddInit(bool _depthTestLess, bx::AllocatorI* _allocator)
  1108. {
  1109. s_dd.init(_depthTestLess, _allocator);
  1110. }
  1111. void ddShutdown()
  1112. {
  1113. s_dd.shutdown();
  1114. }
  1115. void ddBegin(uint8_t _viewId)
  1116. {
  1117. s_dd.begin(_viewId);
  1118. }
  1119. void ddEnd()
  1120. {
  1121. s_dd.end();
  1122. }
  1123. void ddPush()
  1124. {
  1125. s_dd.push();
  1126. }
  1127. void ddPop()
  1128. {
  1129. s_dd.pop();
  1130. }
  1131. void ddSetState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1132. {
  1133. s_dd.setState(_depthTest, _depthWrite, _clockwise);
  1134. }
  1135. void ddSetColor(uint32_t _abgr)
  1136. {
  1137. s_dd.setColor(_abgr);
  1138. }
  1139. void ddSetLod(uint8_t _lod)
  1140. {
  1141. s_dd.setLod(_lod);
  1142. }
  1143. void ddSetWireframe(bool _wireframe)
  1144. {
  1145. s_dd.setWireframe(_wireframe);
  1146. }
  1147. void ddSetStipple(bool _stipple, float _scale, float _offset)
  1148. {
  1149. s_dd.setStipple(_stipple, _scale, _offset);
  1150. }
  1151. void ddSetTransform(const void* _mtx)
  1152. {
  1153. s_dd.setTransform(_mtx);
  1154. }
  1155. void ddSetTranslate(float _x, float _y, float _z)
  1156. {
  1157. s_dd.setTranslate(_x, _y, _z);
  1158. }
  1159. void ddMoveTo(float _x, float _y, float _z)
  1160. {
  1161. s_dd.moveTo(_x, _y, _z);
  1162. }
  1163. void ddMoveTo(const void* _pos)
  1164. {
  1165. s_dd.moveTo(_pos);
  1166. }
  1167. void ddLineTo(float _x, float _y, float _z)
  1168. {
  1169. s_dd.lineTo(_x, _y, _z);
  1170. }
  1171. void ddLineTo(const void* _pos)
  1172. {
  1173. s_dd.lineTo(_pos);
  1174. }
  1175. void ddClose()
  1176. {
  1177. s_dd.close();
  1178. }
  1179. void ddDraw(const Aabb& _aabb)
  1180. {
  1181. s_dd.draw(_aabb);
  1182. }
  1183. void ddDraw(const Cylinder& _cylinder, bool _capsule)
  1184. {
  1185. s_dd.draw(_cylinder, _capsule);
  1186. }
  1187. void ddDraw(const Disk& _disk)
  1188. {
  1189. s_dd.draw(_disk);
  1190. }
  1191. void ddDraw(const Obb& _obb)
  1192. {
  1193. s_dd.draw(_obb);
  1194. }
  1195. void ddDraw(const Sphere& _sphere)
  1196. {
  1197. s_dd.draw(_sphere);
  1198. }
  1199. void ddDrawFrustum(const void* _viewProj)
  1200. {
  1201. s_dd.drawFrustum(_viewProj);
  1202. }
  1203. void ddDrawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1204. {
  1205. s_dd.drawArc(_axis, _x, _y, _z, _radius, _degrees);
  1206. }
  1207. void ddDrawCircle(const void* _normal, const void* _center, float _radius, float _weight)
  1208. {
  1209. s_dd.drawCircle(_normal, _center, _radius, _weight);
  1210. }
  1211. void ddDrawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1212. {
  1213. s_dd.drawCircle(_axis, _x, _y, _z, _radius, _weight);
  1214. }
  1215. void ddDrawAxis(float _x, float _y, float _z, float _len, Axis::Enum _hightlight)
  1216. {
  1217. s_dd.drawAxis(_x, _y, _z, _len, _hightlight);
  1218. }
  1219. void ddDrawGrid(const void* _normal, const void* _center, uint32_t _size, float _step)
  1220. {
  1221. s_dd.drawGrid(_normal, _center, _size, _step);
  1222. }
  1223. void ddDrawGrid(Axis::Enum _axis, const void* _center, uint32_t _size, float _step)
  1224. {
  1225. s_dd.drawGrid(_axis, _center, _size, _step);
  1226. }
  1227. void ddDrawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1228. {
  1229. s_dd.drawOrb(_x, _y, _z, _radius, _hightlight);
  1230. }