debugdraw.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bgfx/embedded_shader.h>
  7. #include "debugdraw.h"
  8. #include "../bgfx_utils.h"
  9. #include "../packrect.h"
  10. #include <bx/mutex.h>
  11. #include <bx/math.h>
  12. #include <bx/sort.h>
  13. #include <bx/uint32_t.h>
  14. #include <bx/handlealloc.h>
  15. #ifndef DEBUG_DRAW_CONFIG_MAX_GEOMETRY
  16. # define DEBUG_DRAW_CONFIG_MAX_GEOMETRY 256
  17. #endif // DEBUG_DRAW_CONFIG_MAX_GEOMETRY
  18. struct DebugVertex
  19. {
  20. float m_x;
  21. float m_y;
  22. float m_z;
  23. float m_len;
  24. uint32_t m_abgr;
  25. static void init()
  26. {
  27. ms_layout
  28. .begin()
  29. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  30. .add(bgfx::Attrib::TexCoord0, 1, bgfx::AttribType::Float)
  31. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  32. .end();
  33. }
  34. static bgfx::VertexLayout ms_layout;
  35. };
  36. bgfx::VertexLayout DebugVertex::ms_layout;
  37. struct DebugUvVertex
  38. {
  39. float m_x;
  40. float m_y;
  41. float m_z;
  42. float m_u;
  43. float m_v;
  44. uint32_t m_abgr;
  45. static void init()
  46. {
  47. ms_layout
  48. .begin()
  49. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  50. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  51. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  52. .end();
  53. }
  54. static bgfx::VertexLayout ms_layout;
  55. };
  56. bgfx::VertexLayout DebugUvVertex::ms_layout;
  57. struct DebugShapeVertex
  58. {
  59. float m_x;
  60. float m_y;
  61. float m_z;
  62. uint8_t m_indices[4];
  63. static void init()
  64. {
  65. ms_layout
  66. .begin()
  67. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  68. .add(bgfx::Attrib::Indices, 4, bgfx::AttribType::Uint8)
  69. .end();
  70. }
  71. static bgfx::VertexLayout ms_layout;
  72. };
  73. bgfx::VertexLayout DebugShapeVertex::ms_layout;
  74. struct DebugMeshVertex
  75. {
  76. float m_x;
  77. float m_y;
  78. float m_z;
  79. static void init()
  80. {
  81. ms_layout
  82. .begin()
  83. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  84. .end();
  85. }
  86. static bgfx::VertexLayout ms_layout;
  87. };
  88. bgfx::VertexLayout DebugMeshVertex::ms_layout;
  89. static DebugShapeVertex s_quadVertices[4] =
  90. {
  91. {-1.0f, 0.0f, 1.0f, { 0, 0, 0, 0 } },
  92. { 1.0f, 0.0f, 1.0f, { 0, 0, 0, 0 } },
  93. {-1.0f, 0.0f, -1.0f, { 0, 0, 0, 0 } },
  94. { 1.0f, 0.0f, -1.0f, { 0, 0, 0, 0 } },
  95. };
  96. static const uint16_t s_quadIndices[6] =
  97. {
  98. 0, 1, 2,
  99. 1, 3, 2,
  100. };
  101. static DebugShapeVertex s_cubeVertices[8] =
  102. {
  103. {-1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  104. { 1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
  105. {-1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  106. { 1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
  107. {-1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  108. { 1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
  109. {-1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  110. { 1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
  111. };
  112. static const uint16_t s_cubeIndices[36] =
  113. {
  114. 0, 1, 2, // 0
  115. 1, 3, 2,
  116. 4, 6, 5, // 2
  117. 5, 6, 7,
  118. 0, 2, 4, // 4
  119. 4, 2, 6,
  120. 1, 5, 3, // 6
  121. 5, 7, 3,
  122. 0, 4, 1, // 8
  123. 4, 5, 1,
  124. 2, 3, 6, // 10
  125. 6, 3, 7,
  126. };
  127. static const uint8_t s_circleLod[] =
  128. {
  129. 37,
  130. 29,
  131. 23,
  132. 17,
  133. 11,
  134. };
  135. static uint8_t getCircleLod(uint8_t _lod)
  136. {
  137. _lod = _lod > BX_COUNTOF(s_circleLod)-1 ? BX_COUNTOF(s_circleLod)-1 : _lod;
  138. return s_circleLod[_lod];
  139. }
  140. static void circle(float* _out, float _angle)
  141. {
  142. float sa = bx::sin(_angle);
  143. float ca = bx::cos(_angle);
  144. _out[0] = sa;
  145. _out[1] = ca;
  146. }
  147. static void squircle(float* _out, float _angle)
  148. {
  149. float sa = bx::sin(_angle);
  150. float ca = bx::cos(_angle);
  151. _out[0] = bx::sqrt(bx::abs(sa) ) * bx::sign(sa);
  152. _out[1] = bx::sqrt(bx::abs(ca) ) * bx::sign(ca);
  153. }
  154. uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0)
  155. {
  156. if (NULL != _pos0)
  157. {
  158. struct Gen
  159. {
  160. Gen(void* _pos, uint16_t _posStride, void* _normals, uint16_t _normalStride, uint8_t _subdiv)
  161. : m_pos( (uint8_t*)_pos)
  162. , m_normals( (uint8_t*)_normals)
  163. , m_posStride(_posStride)
  164. , m_normalStride(_normalStride)
  165. {
  166. static const float scale = 1.0f;
  167. static const float golden = 1.6180339887f;
  168. static const float len = bx::sqrt(golden*golden + 1.0f);
  169. static const float ss = 1.0f/len * scale;
  170. static const float ll = ss*golden;
  171. static const bx::Vec3 vv[] =
  172. {
  173. { -ll, 0.0f, -ss },
  174. { ll, 0.0f, -ss },
  175. { ll, 0.0f, ss },
  176. { -ll, 0.0f, ss },
  177. { -ss, ll, 0.0f },
  178. { ss, ll, 0.0f },
  179. { ss, -ll, 0.0f },
  180. { -ss, -ll, 0.0f },
  181. { 0.0f, -ss, ll },
  182. { 0.0f, ss, ll },
  183. { 0.0f, ss, -ll },
  184. { 0.0f, -ss, -ll },
  185. };
  186. m_numVertices = 0;
  187. triangle(vv[ 0], vv[ 4], vv[ 3], scale, _subdiv);
  188. triangle(vv[ 0], vv[10], vv[ 4], scale, _subdiv);
  189. triangle(vv[ 4], vv[10], vv[ 5], scale, _subdiv);
  190. triangle(vv[ 5], vv[10], vv[ 1], scale, _subdiv);
  191. triangle(vv[ 5], vv[ 1], vv[ 2], scale, _subdiv);
  192. triangle(vv[ 5], vv[ 2], vv[ 9], scale, _subdiv);
  193. triangle(vv[ 5], vv[ 9], vv[ 4], scale, _subdiv);
  194. triangle(vv[ 3], vv[ 4], vv[ 9], scale, _subdiv);
  195. triangle(vv[ 0], vv[ 3], vv[ 7], scale, _subdiv);
  196. triangle(vv[ 0], vv[ 7], vv[11], scale, _subdiv);
  197. triangle(vv[11], vv[ 7], vv[ 6], scale, _subdiv);
  198. triangle(vv[11], vv[ 6], vv[ 1], scale, _subdiv);
  199. triangle(vv[ 1], vv[ 6], vv[ 2], scale, _subdiv);
  200. triangle(vv[ 2], vv[ 6], vv[ 8], scale, _subdiv);
  201. triangle(vv[ 8], vv[ 6], vv[ 7], scale, _subdiv);
  202. triangle(vv[ 8], vv[ 7], vv[ 3], scale, _subdiv);
  203. triangle(vv[ 0], vv[11], vv[10], scale, _subdiv);
  204. triangle(vv[ 1], vv[10], vv[11], scale, _subdiv);
  205. triangle(vv[ 2], vv[ 8], vv[ 9], scale, _subdiv);
  206. triangle(vv[ 3], vv[ 9], vv[ 8], scale, _subdiv);
  207. }
  208. void addVert(const bx::Vec3& _v)
  209. {
  210. bx::store(m_pos, _v);
  211. m_pos += m_posStride;
  212. if (NULL != m_normals)
  213. {
  214. const bx::Vec3 normal = bx::normalize(_v);
  215. bx::store(m_normals, normal);
  216. m_normals += m_normalStride;
  217. }
  218. m_numVertices++;
  219. }
  220. void triangle(const bx::Vec3& _v0, const bx::Vec3& _v1, const bx::Vec3& _v2, float _scale, uint8_t _subdiv)
  221. {
  222. if (0 == _subdiv)
  223. {
  224. addVert(_v0);
  225. addVert(_v1);
  226. addVert(_v2);
  227. }
  228. else
  229. {
  230. const bx::Vec3 v01 = bx::mul(bx::normalize(bx::add(_v0, _v1) ), _scale);
  231. const bx::Vec3 v12 = bx::mul(bx::normalize(bx::add(_v1, _v2) ), _scale);
  232. const bx::Vec3 v20 = bx::mul(bx::normalize(bx::add(_v2, _v0) ), _scale);
  233. --_subdiv;
  234. triangle(_v0, v01, v20, _scale, _subdiv);
  235. triangle(_v1, v12, v01, _scale, _subdiv);
  236. triangle(_v2, v20, v12, _scale, _subdiv);
  237. triangle(v01, v12, v20, _scale, _subdiv);
  238. }
  239. }
  240. uint8_t* m_pos;
  241. uint8_t* m_normals;
  242. uint16_t m_posStride;
  243. uint16_t m_normalStride;
  244. uint32_t m_numVertices;
  245. } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0);
  246. }
  247. uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::pow(4.0f, _subdiv0) );
  248. return numVertices;
  249. }
  250. bx::Vec3 getPoint(Axis::Enum _axis, float _x, float _y)
  251. {
  252. bx::Vec3 result;
  253. switch (_axis)
  254. {
  255. case Axis::X:
  256. result.x = 0.0f;
  257. result.y = _x;
  258. result.z = _y;
  259. break;
  260. case Axis::Y:
  261. result.x = _y;
  262. result.y = 0.0f;
  263. result.z = _x;
  264. break;
  265. default:
  266. result.x = _x;
  267. result.y = _y;
  268. result.z = 0.0f;
  269. break;
  270. }
  271. return result;
  272. }
  273. #include "vs_debugdraw_lines.bin.h"
  274. #include "fs_debugdraw_lines.bin.h"
  275. #include "vs_debugdraw_lines_stipple.bin.h"
  276. #include "fs_debugdraw_lines_stipple.bin.h"
  277. #include "vs_debugdraw_fill.bin.h"
  278. #include "vs_debugdraw_fill_mesh.bin.h"
  279. #include "fs_debugdraw_fill.bin.h"
  280. #include "vs_debugdraw_fill_lit.bin.h"
  281. #include "vs_debugdraw_fill_lit_mesh.bin.h"
  282. #include "fs_debugdraw_fill_lit.bin.h"
  283. #include "vs_debugdraw_fill_texture.bin.h"
  284. #include "fs_debugdraw_fill_texture.bin.h"
  285. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  286. {
  287. BGFX_EMBEDDED_SHADER(vs_debugdraw_lines),
  288. BGFX_EMBEDDED_SHADER(fs_debugdraw_lines),
  289. BGFX_EMBEDDED_SHADER(vs_debugdraw_lines_stipple),
  290. BGFX_EMBEDDED_SHADER(fs_debugdraw_lines_stipple),
  291. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill),
  292. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_mesh),
  293. BGFX_EMBEDDED_SHADER(fs_debugdraw_fill),
  294. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit),
  295. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit_mesh),
  296. BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_lit),
  297. BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_texture),
  298. BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_texture),
  299. BGFX_EMBEDDED_SHADER_END()
  300. };
  301. #define SPRITE_TEXTURE_SIZE 1024
  302. template<uint16_t MaxHandlesT = 256, uint16_t TextureSizeT = 1024>
  303. struct SpriteT
  304. {
  305. SpriteT()
  306. : m_ra(TextureSizeT, TextureSizeT)
  307. {
  308. }
  309. SpriteHandle create(uint16_t _width, uint16_t _height)
  310. {
  311. bx::MutexScope lock(m_lock);
  312. SpriteHandle handle = { bx::kInvalidHandle };
  313. if (m_handleAlloc.getNumHandles() < m_handleAlloc.getMaxHandles() )
  314. {
  315. Pack2D pack;
  316. if (m_ra.find(_width, _height, pack) )
  317. {
  318. handle.idx = m_handleAlloc.alloc();
  319. if (isValid(handle) )
  320. {
  321. m_pack[handle.idx] = pack;
  322. }
  323. else
  324. {
  325. m_ra.clear(pack);
  326. }
  327. }
  328. }
  329. return handle;
  330. }
  331. void destroy(SpriteHandle _sprite)
  332. {
  333. const Pack2D& pack = m_pack[_sprite.idx];
  334. m_ra.clear(pack);
  335. m_handleAlloc.free(_sprite.idx);
  336. }
  337. const Pack2D& get(SpriteHandle _sprite) const
  338. {
  339. return m_pack[_sprite.idx];
  340. }
  341. bx::Mutex m_lock;
  342. bx::HandleAllocT<MaxHandlesT> m_handleAlloc;
  343. Pack2D m_pack[MaxHandlesT];
  344. RectPack2DT<256> m_ra;
  345. };
  346. template<uint16_t MaxHandlesT = DEBUG_DRAW_CONFIG_MAX_GEOMETRY>
  347. struct GeometryT
  348. {
  349. GeometryT()
  350. {
  351. }
  352. GeometryHandle create(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32)
  353. {
  354. BX_UNUSED(_numVertices, _vertices, _numIndices, _indices, _index32);
  355. GeometryHandle handle;
  356. {
  357. bx::MutexScope lock(m_lock);
  358. handle = { m_handleAlloc.alloc() };
  359. }
  360. if (isValid(handle) )
  361. {
  362. Geometry& geometry = m_geometry[handle.idx];
  363. geometry.m_vbh = bgfx::createVertexBuffer(
  364. bgfx::copy(_vertices, _numVertices*sizeof(DdVertex) )
  365. , DebugMeshVertex::ms_layout
  366. );
  367. geometry.m_topologyNumIndices[0] = _numIndices;
  368. geometry.m_topologyNumIndices[1] = bgfx::topologyConvert(
  369. bgfx::TopologyConvert::TriListToLineList
  370. , NULL
  371. , 0
  372. , _indices
  373. , _numIndices
  374. , _index32
  375. );
  376. const uint32_t indexSize = _index32 ? sizeof(uint32_t) : sizeof(uint16_t);
  377. const uint32_t numIndices = 0
  378. + geometry.m_topologyNumIndices[0]
  379. + geometry.m_topologyNumIndices[1]
  380. ;
  381. const bgfx::Memory* mem = bgfx::alloc(numIndices*indexSize );
  382. uint8_t* indexData = mem->data;
  383. bx::memCopy(indexData, _indices, _numIndices*indexSize );
  384. bgfx::topologyConvert(
  385. bgfx::TopologyConvert::TriListToLineList
  386. , &indexData[geometry.m_topologyNumIndices[0]*indexSize ]
  387. , geometry.m_topologyNumIndices[1]*indexSize
  388. , _indices
  389. , _numIndices
  390. , _index32
  391. );
  392. geometry.m_ibh = bgfx::createIndexBuffer(
  393. mem
  394. , _index32 ? BGFX_BUFFER_INDEX32 : BGFX_BUFFER_NONE
  395. );
  396. }
  397. return handle;
  398. }
  399. void destroy(GeometryHandle _handle)
  400. {
  401. bx::MutexScope lock(m_lock);
  402. Geometry& geometry = m_geometry[_handle.idx];
  403. bgfx::destroy(geometry.m_vbh);
  404. bgfx::destroy(geometry.m_ibh);
  405. m_handleAlloc.free(_handle.idx);
  406. }
  407. struct Geometry
  408. {
  409. Geometry()
  410. {
  411. m_vbh.idx = bx::kInvalidHandle;
  412. m_ibh.idx = bx::kInvalidHandle;
  413. m_topologyNumIndices[0] = 0;
  414. m_topologyNumIndices[1] = 0;
  415. }
  416. bgfx::VertexBufferHandle m_vbh;
  417. bgfx::IndexBufferHandle m_ibh;
  418. uint32_t m_topologyNumIndices[2];
  419. };
  420. bx::Mutex m_lock;
  421. bx::HandleAllocT<MaxHandlesT> m_handleAlloc;
  422. Geometry m_geometry[MaxHandlesT];
  423. };
  424. struct Attrib
  425. {
  426. uint64_t m_state;
  427. float m_offset;
  428. float m_scale;
  429. float m_spin;
  430. uint32_t m_abgr;
  431. bool m_stipple;
  432. bool m_wireframe;
  433. uint8_t m_lod;
  434. };
  435. struct Program
  436. {
  437. enum Enum
  438. {
  439. Lines,
  440. LinesStipple,
  441. Fill,
  442. FillMesh,
  443. FillLit,
  444. FillLitMesh,
  445. FillTexture,
  446. Count
  447. };
  448. };
  449. struct DebugMesh
  450. {
  451. enum Enum
  452. {
  453. Sphere0,
  454. Sphere1,
  455. Sphere2,
  456. Sphere3,
  457. Cone0,
  458. Cone1,
  459. Cone2,
  460. Cone3,
  461. Cylinder0,
  462. Cylinder1,
  463. Cylinder2,
  464. Cylinder3,
  465. Capsule0,
  466. Capsule1,
  467. Capsule2,
  468. Capsule3,
  469. Quad,
  470. Cube,
  471. Count,
  472. SphereMaxLod = Sphere3 - Sphere0,
  473. ConeMaxLod = Cone3 - Cone0,
  474. CylinderMaxLod = Cylinder3 - Cylinder0,
  475. CapsuleMaxLod = Capsule3 - Capsule0,
  476. };
  477. uint32_t m_startVertex;
  478. uint32_t m_numVertices;
  479. uint32_t m_startIndex[2];
  480. uint32_t m_numIndices[2];
  481. };
  482. typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite;
  483. typedef GeometryT<DEBUG_DRAW_CONFIG_MAX_GEOMETRY> Geometry;
  484. struct DebugDrawShared
  485. {
  486. void init(bx::AllocatorI* _allocator)
  487. {
  488. if (NULL == _allocator)
  489. {
  490. static bx::DefaultAllocator allocator;
  491. m_allocator = &allocator;
  492. }
  493. else
  494. {
  495. m_allocator = _allocator;
  496. }
  497. DebugVertex::init();
  498. DebugUvVertex::init();
  499. DebugShapeVertex::init();
  500. DebugMeshVertex::init();
  501. bgfx::RendererType::Enum type = bgfx::getRendererType();
  502. m_program[Program::Lines] = bgfx::createProgram(
  503. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_lines")
  504. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_lines")
  505. , true
  506. );
  507. m_program[Program::LinesStipple] = bgfx::createProgram(
  508. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_lines_stipple")
  509. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_lines_stipple")
  510. , true
  511. );
  512. m_program[Program::Fill] = bgfx::createProgram(
  513. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill")
  514. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill")
  515. , true
  516. );
  517. m_program[Program::FillMesh] = bgfx::createProgram(
  518. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_mesh")
  519. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill")
  520. , true
  521. );
  522. m_program[Program::FillLit] = bgfx::createProgram(
  523. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit")
  524. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit")
  525. , true
  526. );
  527. m_program[Program::FillLitMesh] = bgfx::createProgram(
  528. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit_mesh")
  529. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit")
  530. , true
  531. );
  532. m_program[Program::FillTexture] = bgfx::createProgram(
  533. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_texture")
  534. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_texture")
  535. , true
  536. );
  537. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
  538. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
  539. m_texture = bgfx::createTexture2D(SPRITE_TEXTURE_SIZE, SPRITE_TEXTURE_SIZE, false, 1, bgfx::TextureFormat::BGRA8);
  540. void* vertices[DebugMesh::Count] = {};
  541. uint16_t* indices[DebugMesh::Count] = {};
  542. uint16_t stride = DebugShapeVertex::ms_layout.getStride();
  543. uint32_t startVertex = 0;
  544. uint32_t startIndex = 0;
  545. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  546. {
  547. DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Sphere0+mesh);
  548. const uint8_t tess = uint8_t(3-mesh);
  549. const uint32_t numVertices = genSphere(tess);
  550. const uint32_t numIndices = numVertices;
  551. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  552. bx::memSet(vertices[id], 0, numVertices*stride);
  553. genSphere(tess, vertices[id], stride);
  554. uint16_t* trilist = (uint16_t*)BX_ALLOC(m_allocator, numIndices*sizeof(uint16_t) );
  555. for (uint32_t ii = 0; ii < numIndices; ++ii)
  556. {
  557. trilist[ii] = uint16_t(ii);
  558. }
  559. uint32_t numLineListIndices = bgfx::topologyConvert(
  560. bgfx::TopologyConvert::TriListToLineList
  561. , NULL
  562. , 0
  563. , trilist
  564. , numIndices
  565. , false
  566. );
  567. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  568. uint16_t* indicesOut = indices[id];
  569. bx::memCopy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
  570. bgfx::topologyConvert(
  571. bgfx::TopologyConvert::TriListToLineList
  572. , &indicesOut[numIndices]
  573. , numLineListIndices*sizeof(uint16_t)
  574. , trilist
  575. , numIndices
  576. , false
  577. );
  578. m_mesh[id].m_startVertex = startVertex;
  579. m_mesh[id].m_numVertices = numVertices;
  580. m_mesh[id].m_startIndex[0] = startIndex;
  581. m_mesh[id].m_numIndices[0] = numIndices;
  582. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  583. m_mesh[id].m_numIndices[1] = numLineListIndices;
  584. startVertex += numVertices;
  585. startIndex += numIndices + numLineListIndices;
  586. BX_FREE(m_allocator, trilist);
  587. }
  588. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  589. {
  590. DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Cone0+mesh);
  591. const uint32_t num = getCircleLod(uint8_t(mesh) );
  592. const float step = bx::kPi * 2.0f / num;
  593. const uint32_t numVertices = num+1;
  594. const uint32_t numIndices = num*6;
  595. const uint32_t numLineListIndices = num*4;
  596. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  597. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  598. bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  599. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  600. uint16_t* index = indices[id];
  601. vertex[num].m_x = 0.0f;
  602. vertex[num].m_y = 0.0f;
  603. vertex[num].m_z = 0.0f;
  604. vertex[num].m_indices[0] = 1;
  605. for (uint32_t ii = 0; ii < num; ++ii)
  606. {
  607. const float angle = step * ii;
  608. float xy[2];
  609. circle(xy, angle);
  610. vertex[ii].m_x = xy[1];
  611. vertex[ii].m_y = 0.0f;
  612. vertex[ii].m_z = xy[0];
  613. vertex[ii].m_indices[0] = 0;
  614. index[ii*3+0] = uint16_t(num);
  615. index[ii*3+1] = uint16_t( (ii+1)%num);
  616. index[ii*3+2] = uint16_t(ii);
  617. index[num*3+ii*3+0] = 0;
  618. index[num*3+ii*3+1] = uint16_t(ii);
  619. index[num*3+ii*3+2] = uint16_t( (ii+1)%num);
  620. index[numIndices+ii*2+0] = uint16_t(ii);
  621. index[numIndices+ii*2+1] = uint16_t(num);
  622. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  623. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  624. }
  625. m_mesh[id].m_startVertex = startVertex;
  626. m_mesh[id].m_numVertices = numVertices;
  627. m_mesh[id].m_startIndex[0] = startIndex;
  628. m_mesh[id].m_numIndices[0] = numIndices;
  629. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  630. m_mesh[id].m_numIndices[1] = numLineListIndices;
  631. startVertex += numVertices;
  632. startIndex += numIndices + numLineListIndices;
  633. }
  634. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  635. {
  636. DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Cylinder0+mesh);
  637. const uint32_t num = getCircleLod(uint8_t(mesh) );
  638. const float step = bx::kPi * 2.0f / num;
  639. const uint32_t numVertices = num*2;
  640. const uint32_t numIndices = num*12;
  641. const uint32_t numLineListIndices = num*6;
  642. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  643. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  644. bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  645. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  646. uint16_t* index = indices[id];
  647. for (uint32_t ii = 0; ii < num; ++ii)
  648. {
  649. const float angle = step * ii;
  650. float xy[2];
  651. circle(xy, angle);
  652. vertex[ii].m_x = xy[1];
  653. vertex[ii].m_y = 0.0f;
  654. vertex[ii].m_z = xy[0];
  655. vertex[ii].m_indices[0] = 0;
  656. vertex[ii+num].m_x = xy[1];
  657. vertex[ii+num].m_y = 0.0f;
  658. vertex[ii+num].m_z = xy[0];
  659. vertex[ii+num].m_indices[0] = 1;
  660. index[ii*6+0] = uint16_t(ii+num);
  661. index[ii*6+1] = uint16_t( (ii+1)%num);
  662. index[ii*6+2] = uint16_t(ii);
  663. index[ii*6+3] = uint16_t(ii+num);
  664. index[ii*6+4] = uint16_t( (ii+1)%num+num);
  665. index[ii*6+5] = uint16_t( (ii+1)%num);
  666. index[num*6+ii*6+0] = uint16_t(0);
  667. index[num*6+ii*6+1] = uint16_t(ii);
  668. index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
  669. index[num*6+ii*6+3] = uint16_t(num);
  670. index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
  671. index[num*6+ii*6+5] = uint16_t(ii+num);
  672. index[numIndices+ii*2+0] = uint16_t(ii);
  673. index[numIndices+ii*2+1] = uint16_t(ii+num);
  674. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  675. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  676. index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
  677. index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
  678. }
  679. m_mesh[id].m_startVertex = startVertex;
  680. m_mesh[id].m_numVertices = numVertices;
  681. m_mesh[id].m_startIndex[0] = startIndex;
  682. m_mesh[id].m_numIndices[0] = numIndices;
  683. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  684. m_mesh[id].m_numIndices[1] = numLineListIndices;
  685. startVertex += numVertices;
  686. startIndex += numIndices + numLineListIndices;
  687. }
  688. for (uint32_t mesh = 0; mesh < 4; ++mesh)
  689. {
  690. DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Capsule0+mesh);
  691. const uint32_t num = getCircleLod(uint8_t(mesh) );
  692. const float step = bx::kPi * 2.0f / num;
  693. const uint32_t numVertices = num*2;
  694. const uint32_t numIndices = num*6;
  695. const uint32_t numLineListIndices = num*6;
  696. vertices[id] = BX_ALLOC(m_allocator, numVertices*stride);
  697. indices[id] = (uint16_t*)BX_ALLOC(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  698. bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
  699. DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
  700. uint16_t* index = indices[id];
  701. for (uint32_t ii = 0; ii < num; ++ii)
  702. {
  703. const float angle = step * ii;
  704. float xy[2];
  705. circle(xy, angle);
  706. vertex[ii].m_x = xy[1];
  707. vertex[ii].m_y = 0.0f;
  708. vertex[ii].m_z = xy[0];
  709. vertex[ii].m_indices[0] = 0;
  710. vertex[ii+num].m_x = xy[1];
  711. vertex[ii+num].m_y = 0.0f;
  712. vertex[ii+num].m_z = xy[0];
  713. vertex[ii+num].m_indices[0] = 1;
  714. index[ii*6+0] = uint16_t(ii+num);
  715. index[ii*6+1] = uint16_t( (ii+1)%num);
  716. index[ii*6+2] = uint16_t(ii);
  717. index[ii*6+3] = uint16_t(ii+num);
  718. index[ii*6+4] = uint16_t( (ii+1)%num+num);
  719. index[ii*6+5] = uint16_t( (ii+1)%num);
  720. // index[num*6+ii*6+0] = uint16_t(0);
  721. // index[num*6+ii*6+1] = uint16_t(ii);
  722. // index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
  723. // index[num*6+ii*6+3] = uint16_t(num);
  724. // index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
  725. // index[num*6+ii*6+5] = uint16_t(ii+num);
  726. index[numIndices+ii*2+0] = uint16_t(ii);
  727. index[numIndices+ii*2+1] = uint16_t(ii+num);
  728. index[numIndices+num*2+ii*2+0] = uint16_t(ii);
  729. index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
  730. index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
  731. index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
  732. }
  733. m_mesh[id].m_startVertex = startVertex;
  734. m_mesh[id].m_numVertices = numVertices;
  735. m_mesh[id].m_startIndex[0] = startIndex;
  736. m_mesh[id].m_numIndices[0] = numIndices;
  737. m_mesh[id].m_startIndex[1] = startIndex+numIndices;
  738. m_mesh[id].m_numIndices[1] = numLineListIndices;
  739. startVertex += numVertices;
  740. startIndex += numIndices + numLineListIndices;
  741. }
  742. m_mesh[DebugMesh::Quad].m_startVertex = startVertex;
  743. m_mesh[DebugMesh::Quad].m_numVertices = BX_COUNTOF(s_quadVertices);
  744. m_mesh[DebugMesh::Quad].m_startIndex[0] = startIndex;
  745. m_mesh[DebugMesh::Quad].m_numIndices[0] = BX_COUNTOF(s_quadIndices);
  746. m_mesh[DebugMesh::Quad].m_startIndex[1] = 0;
  747. m_mesh[DebugMesh::Quad].m_numIndices[1] = 0;
  748. startVertex += BX_COUNTOF(s_quadVertices);
  749. startIndex += BX_COUNTOF(s_quadIndices);
  750. m_mesh[DebugMesh::Cube].m_startVertex = startVertex;
  751. m_mesh[DebugMesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
  752. m_mesh[DebugMesh::Cube].m_startIndex[0] = startIndex;
  753. m_mesh[DebugMesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
  754. m_mesh[DebugMesh::Cube].m_startIndex[1] = 0;
  755. m_mesh[DebugMesh::Cube].m_numIndices[1] = 0;
  756. startVertex += m_mesh[DebugMesh::Cube].m_numVertices;
  757. startIndex += m_mesh[DebugMesh::Cube].m_numIndices[0];
  758. const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
  759. const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
  760. for (uint32_t mesh = DebugMesh::Sphere0; mesh < DebugMesh::Quad; ++mesh)
  761. {
  762. DebugMesh::Enum id = DebugMesh::Enum(mesh);
  763. bx::memCopy(&vb->data[m_mesh[id].m_startVertex * stride]
  764. , vertices[id]
  765. , m_mesh[id].m_numVertices*stride
  766. );
  767. bx::memCopy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
  768. , indices[id]
  769. , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
  770. );
  771. BX_FREE(m_allocator, vertices[id]);
  772. BX_FREE(m_allocator, indices[id]);
  773. }
  774. bx::memCopy(&vb->data[m_mesh[DebugMesh::Quad].m_startVertex * stride]
  775. , s_quadVertices
  776. , sizeof(s_quadVertices)
  777. );
  778. bx::memCopy(&ib->data[m_mesh[DebugMesh::Quad].m_startIndex[0] * sizeof(uint16_t)]
  779. , s_quadIndices
  780. , sizeof(s_quadIndices)
  781. );
  782. bx::memCopy(&vb->data[m_mesh[DebugMesh::Cube].m_startVertex * stride]
  783. , s_cubeVertices
  784. , sizeof(s_cubeVertices)
  785. );
  786. bx::memCopy(&ib->data[m_mesh[DebugMesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
  787. , s_cubeIndices
  788. , sizeof(s_cubeIndices)
  789. );
  790. m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_layout);
  791. m_ibh = bgfx::createIndexBuffer(ib);
  792. }
  793. void shutdown()
  794. {
  795. bgfx::destroy(m_ibh);
  796. bgfx::destroy(m_vbh);
  797. for (uint32_t ii = 0; ii < Program::Count; ++ii)
  798. {
  799. bgfx::destroy(m_program[ii]);
  800. }
  801. bgfx::destroy(u_params);
  802. bgfx::destroy(s_texColor);
  803. bgfx::destroy(m_texture);
  804. }
  805. SpriteHandle createSprite(uint16_t _width, uint16_t _height, const void* _data)
  806. {
  807. SpriteHandle handle = m_sprite.create(_width, _height);
  808. if (isValid(handle) )
  809. {
  810. const Pack2D& pack = m_sprite.get(handle);
  811. bgfx::updateTexture2D(
  812. m_texture
  813. , 0
  814. , 0
  815. , pack.m_x
  816. , pack.m_y
  817. , pack.m_width
  818. , pack.m_height
  819. , bgfx::copy(_data, pack.m_width*pack.m_height*4)
  820. );
  821. }
  822. return handle;
  823. }
  824. void destroy(SpriteHandle _handle)
  825. {
  826. m_sprite.destroy(_handle);
  827. }
  828. GeometryHandle createGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32)
  829. {
  830. return m_geometry.create(_numVertices, _vertices, _numIndices, _indices, _index32);
  831. }
  832. void destroy(GeometryHandle _handle)
  833. {
  834. m_geometry.destroy(_handle);
  835. }
  836. bx::AllocatorI* m_allocator;
  837. Sprite m_sprite;
  838. Geometry m_geometry;
  839. DebugMesh m_mesh[DebugMesh::Count];
  840. bgfx::UniformHandle s_texColor;
  841. bgfx::TextureHandle m_texture;
  842. bgfx::ProgramHandle m_program[Program::Count];
  843. bgfx::UniformHandle u_params;
  844. bgfx::VertexBufferHandle m_vbh;
  845. bgfx::IndexBufferHandle m_ibh;
  846. };
  847. static DebugDrawShared s_dds;
  848. struct DebugDrawEncoderImpl
  849. {
  850. DebugDrawEncoderImpl()
  851. : m_depthTestLess(true)
  852. , m_state(State::Count)
  853. , m_defaultEncoder(NULL)
  854. {
  855. }
  856. void init(bgfx::Encoder* _encoder)
  857. {
  858. m_defaultEncoder = _encoder;
  859. }
  860. void shutdown()
  861. {
  862. }
  863. void begin(bgfx::ViewId _viewId, bool _depthTestLess, bgfx::Encoder* _encoder)
  864. {
  865. BX_CHECK(State::Count == m_state);
  866. m_viewId = _viewId;
  867. m_encoder = _encoder == NULL ? m_defaultEncoder : _encoder;
  868. m_state = State::None;
  869. m_stack = 0;
  870. m_depthTestLess = _depthTestLess;
  871. m_pos = 0;
  872. m_indexPos = 0;
  873. m_vertexPos = 0;
  874. m_posQuad = 0;
  875. Attrib& attrib = m_attrib[0];
  876. attrib.m_state = 0
  877. | BGFX_STATE_WRITE_RGB
  878. | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER)
  879. | BGFX_STATE_CULL_CW
  880. | BGFX_STATE_WRITE_Z
  881. ;
  882. attrib.m_scale = 1.0f;
  883. attrib.m_spin = 0.0f;
  884. attrib.m_offset = 0.0f;
  885. attrib.m_abgr = UINT32_MAX;
  886. attrib.m_stipple = false;
  887. attrib.m_wireframe = false;
  888. attrib.m_lod = 0;
  889. m_mtxStackCurrent = 0;
  890. m_mtxStack[m_mtxStackCurrent].reset();
  891. }
  892. void end()
  893. {
  894. BX_CHECK(0 == m_stack, "Invalid stack %d.", m_stack);
  895. flushQuad();
  896. flush();
  897. m_encoder = NULL;
  898. m_state = State::Count;
  899. }
  900. void push()
  901. {
  902. BX_CHECK(State::Count != m_state);
  903. ++m_stack;
  904. m_attrib[m_stack] = m_attrib[m_stack-1];
  905. }
  906. void pop()
  907. {
  908. BX_CHECK(State::Count != m_state);
  909. const Attrib& curr = m_attrib[m_stack];
  910. const Attrib& prev = m_attrib[m_stack-1];
  911. if (curr.m_stipple != prev.m_stipple
  912. || curr.m_state != prev.m_state)
  913. {
  914. flush();
  915. }
  916. --m_stack;
  917. }
  918. void setDepthTestLess(bool _depthTestLess)
  919. {
  920. BX_CHECK(State::Count != m_state);
  921. if (m_depthTestLess != _depthTestLess)
  922. {
  923. m_depthTestLess = _depthTestLess;
  924. Attrib& attrib = m_attrib[m_stack];
  925. if (attrib.m_state & BGFX_STATE_DEPTH_TEST_MASK)
  926. {
  927. flush();
  928. attrib.m_state &= ~BGFX_STATE_DEPTH_TEST_MASK;
  929. attrib.m_state |= _depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER;
  930. }
  931. }
  932. }
  933. void setTransform(const void* _mtx, uint16_t _num = 1, bool _flush = true)
  934. {
  935. BX_CHECK(State::Count != m_state);
  936. if (_flush)
  937. {
  938. flush();
  939. }
  940. MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
  941. if (NULL == _mtx)
  942. {
  943. stack.reset();
  944. return;
  945. }
  946. bgfx::Transform transform;
  947. stack.mtx = m_encoder->allocTransform(&transform, _num);
  948. stack.num = _num;
  949. stack.data = transform.data;
  950. bx::memCopy(transform.data, _mtx, _num*64);
  951. }
  952. void setTranslate(float _x, float _y, float _z)
  953. {
  954. float mtx[16];
  955. bx::mtxTranslate(mtx, _x, _y, _z);
  956. setTransform(mtx);
  957. }
  958. void setTranslate(const float* _pos)
  959. {
  960. setTranslate(_pos[0], _pos[1], _pos[2]);
  961. }
  962. void pushTransform(const void* _mtx, uint16_t _num, bool _flush = true)
  963. {
  964. BX_CHECK(m_mtxStackCurrent < BX_COUNTOF(m_mtxStack), "Out of matrix stack!");
  965. BX_CHECK(State::Count != m_state);
  966. if (_flush)
  967. {
  968. flush();
  969. }
  970. float* mtx = NULL;
  971. const MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
  972. if (NULL == stack.data)
  973. {
  974. mtx = (float*)_mtx;
  975. }
  976. else
  977. {
  978. mtx = (float*)alloca(_num*64);
  979. for (uint16_t ii = 0; ii < _num; ++ii)
  980. {
  981. const float* mtxTransform = (const float*)_mtx;
  982. bx::mtxMul(&mtx[ii*16], &mtxTransform[ii*16], stack.data);
  983. }
  984. }
  985. m_mtxStackCurrent++;
  986. setTransform(mtx, _num, _flush);
  987. }
  988. void popTransform(bool _flush = true)
  989. {
  990. BX_CHECK(State::Count != m_state);
  991. if (_flush)
  992. {
  993. flush();
  994. }
  995. m_mtxStackCurrent--;
  996. }
  997. void pushTranslate(float _x, float _y, float _z)
  998. {
  999. float mtx[16];
  1000. bx::mtxTranslate(mtx, _x, _y, _z);
  1001. pushTransform(mtx, 1);
  1002. }
  1003. void pushTranslate(const bx::Vec3& _pos)
  1004. {
  1005. pushTranslate(_pos.x, _pos.y, _pos.z);
  1006. }
  1007. void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1008. {
  1009. const uint64_t depthTest = m_depthTestLess
  1010. ? BGFX_STATE_DEPTH_TEST_LESS
  1011. : BGFX_STATE_DEPTH_TEST_GREATER
  1012. ;
  1013. uint64_t state = m_attrib[m_stack].m_state & ~(0
  1014. | BGFX_STATE_DEPTH_TEST_MASK
  1015. | BGFX_STATE_WRITE_Z
  1016. | BGFX_STATE_CULL_CW
  1017. | BGFX_STATE_CULL_CCW
  1018. );
  1019. state |= _depthTest
  1020. ? depthTest
  1021. : 0
  1022. ;
  1023. state |= _depthWrite
  1024. ? BGFX_STATE_WRITE_Z
  1025. : 0
  1026. ;
  1027. state |= _clockwise
  1028. ? BGFX_STATE_CULL_CW
  1029. : BGFX_STATE_CULL_CCW
  1030. ;
  1031. if (m_attrib[m_stack].m_state != state)
  1032. {
  1033. flush();
  1034. }
  1035. m_attrib[m_stack].m_state = state;
  1036. }
  1037. void setColor(uint32_t _abgr)
  1038. {
  1039. BX_CHECK(State::Count != m_state);
  1040. m_attrib[m_stack].m_abgr = _abgr;
  1041. }
  1042. void setLod(uint8_t _lod)
  1043. {
  1044. BX_CHECK(State::Count != m_state);
  1045. m_attrib[m_stack].m_lod = _lod;
  1046. }
  1047. void setWireframe(bool _wireframe)
  1048. {
  1049. BX_CHECK(State::Count != m_state);
  1050. m_attrib[m_stack].m_wireframe = _wireframe;
  1051. }
  1052. void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
  1053. {
  1054. BX_CHECK(State::Count != m_state);
  1055. Attrib& attrib = m_attrib[m_stack];
  1056. if (attrib.m_stipple != _stipple)
  1057. {
  1058. flush();
  1059. }
  1060. attrib.m_stipple = _stipple;
  1061. attrib.m_offset = _offset;
  1062. attrib.m_scale = _scale;
  1063. }
  1064. void setSpin(float _spin)
  1065. {
  1066. Attrib& attrib = m_attrib[m_stack];
  1067. attrib.m_spin = _spin;
  1068. }
  1069. void moveTo(float _x, float _y, float _z = 0.0f)
  1070. {
  1071. BX_CHECK(State::Count != m_state);
  1072. softFlush();
  1073. m_state = State::MoveTo;
  1074. DebugVertex& vertex = m_cache[m_pos];
  1075. vertex.m_x = _x;
  1076. vertex.m_y = _y;
  1077. vertex.m_z = _z;
  1078. Attrib& attrib = m_attrib[m_stack];
  1079. vertex.m_abgr = attrib.m_abgr;
  1080. vertex.m_len = attrib.m_offset;
  1081. m_vertexPos = m_pos;
  1082. }
  1083. void moveTo(const bx::Vec3& _pos)
  1084. {
  1085. BX_CHECK(State::Count != m_state);
  1086. moveTo(_pos.x, _pos.y, _pos.z);
  1087. }
  1088. void moveTo(Axis::Enum _axis, float _x, float _y)
  1089. {
  1090. moveTo(getPoint(_axis, _x, _y) );
  1091. }
  1092. void lineTo(float _x, float _y, float _z = 0.0f)
  1093. {
  1094. BX_CHECK(State::Count != m_state);
  1095. if (State::None == m_state)
  1096. {
  1097. moveTo(_x, _y, _z);
  1098. return;
  1099. }
  1100. if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
  1101. {
  1102. uint32_t pos = m_pos;
  1103. uint32_t vertexPos = m_vertexPos;
  1104. flush();
  1105. bx::memCopy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
  1106. if (vertexPos == pos)
  1107. {
  1108. m_pos = 1;
  1109. }
  1110. else
  1111. {
  1112. bx::memCopy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
  1113. m_pos = 2;
  1114. }
  1115. m_state = State::LineTo;
  1116. }
  1117. else if (State::MoveTo == m_state)
  1118. {
  1119. ++m_pos;
  1120. m_state = State::LineTo;
  1121. }
  1122. uint16_t prev = m_pos-1;
  1123. uint16_t curr = m_pos++;
  1124. DebugVertex& vertex = m_cache[curr];
  1125. vertex.m_x = _x;
  1126. vertex.m_y = _y;
  1127. vertex.m_z = _z;
  1128. Attrib& attrib = m_attrib[m_stack];
  1129. vertex.m_abgr = attrib.m_abgr;
  1130. vertex.m_len = attrib.m_offset;
  1131. float len = bx::length(bx::sub(bx::load<bx::Vec3>(&vertex.m_x), bx::load<bx::Vec3>(&m_cache[prev].m_x) ) ) * attrib.m_scale;
  1132. vertex.m_len = m_cache[prev].m_len + len;
  1133. m_indices[m_indexPos++] = prev;
  1134. m_indices[m_indexPos++] = curr;
  1135. }
  1136. void lineTo(const bx::Vec3& _pos)
  1137. {
  1138. BX_CHECK(State::Count != m_state);
  1139. lineTo(_pos.x, _pos.y, _pos.z);
  1140. }
  1141. void lineTo(Axis::Enum _axis, float _x, float _y)
  1142. {
  1143. lineTo(getPoint(_axis, _x, _y) );
  1144. }
  1145. void close()
  1146. {
  1147. BX_CHECK(State::Count != m_state);
  1148. DebugVertex& vertex = m_cache[m_vertexPos];
  1149. lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
  1150. m_state = State::None;
  1151. }
  1152. void draw(const Aabb& _aabb)
  1153. {
  1154. const Attrib& attrib = m_attrib[m_stack];
  1155. if (attrib.m_wireframe)
  1156. {
  1157. moveTo(_aabb.min.x, _aabb.min.y, _aabb.min.z);
  1158. lineTo(_aabb.max.x, _aabb.min.y, _aabb.min.z);
  1159. lineTo(_aabb.max.x, _aabb.max.y, _aabb.min.z);
  1160. lineTo(_aabb.min.x, _aabb.max.y, _aabb.min.z);
  1161. close();
  1162. moveTo(_aabb.min.x, _aabb.min.y, _aabb.max.z);
  1163. lineTo(_aabb.max.x, _aabb.min.y, _aabb.max.z);
  1164. lineTo(_aabb.max.x, _aabb.max.y, _aabb.max.z);
  1165. lineTo(_aabb.min.x, _aabb.max.y, _aabb.max.z);
  1166. close();
  1167. moveTo(_aabb.min.x, _aabb.min.y, _aabb.min.z);
  1168. lineTo(_aabb.min.x, _aabb.min.y, _aabb.max.z);
  1169. moveTo(_aabb.max.x, _aabb.min.y, _aabb.min.z);
  1170. lineTo(_aabb.max.x, _aabb.min.y, _aabb.max.z);
  1171. moveTo(_aabb.min.x, _aabb.max.y, _aabb.min.z);
  1172. lineTo(_aabb.min.x, _aabb.max.y, _aabb.max.z);
  1173. moveTo(_aabb.max.x, _aabb.max.y, _aabb.min.z);
  1174. lineTo(_aabb.max.x, _aabb.max.y, _aabb.max.z);
  1175. }
  1176. else
  1177. {
  1178. Obb obb;
  1179. toObb(obb, _aabb);
  1180. draw(DebugMesh::Cube, obb.mtx, 1, false);
  1181. }
  1182. }
  1183. void draw(const Cylinder& _cylinder, bool _capsule)
  1184. {
  1185. drawCylinder(_cylinder.pos, _cylinder.end, _cylinder.radius, _capsule);
  1186. }
  1187. void draw(const Disk& _disk)
  1188. {
  1189. drawCircle(_disk.normal, _disk.center, _disk.radius, 0.0f);
  1190. }
  1191. void draw(const Obb& _obb)
  1192. {
  1193. const Attrib& attrib = m_attrib[m_stack];
  1194. if (attrib.m_wireframe)
  1195. {
  1196. pushTransform(_obb.mtx, 1);
  1197. moveTo(-1.0f, -1.0f, -1.0f);
  1198. lineTo( 1.0f, -1.0f, -1.0f);
  1199. lineTo( 1.0f, 1.0f, -1.0f);
  1200. lineTo(-1.0f, 1.0f, -1.0f);
  1201. close();
  1202. moveTo(-1.0f, 1.0f, 1.0f);
  1203. lineTo( 1.0f, 1.0f, 1.0f);
  1204. lineTo( 1.0f, -1.0f, 1.0f);
  1205. lineTo(-1.0f, -1.0f, 1.0f);
  1206. close();
  1207. moveTo( 1.0f, -1.0f, -1.0f);
  1208. lineTo( 1.0f, -1.0f, 1.0f);
  1209. moveTo( 1.0f, 1.0f, -1.0f);
  1210. lineTo( 1.0f, 1.0f, 1.0f);
  1211. moveTo(-1.0f, 1.0f, -1.0f);
  1212. lineTo(-1.0f, 1.0f, 1.0f);
  1213. moveTo(-1.0f, -1.0f, -1.0f);
  1214. lineTo(-1.0f, -1.0f, 1.0f);
  1215. popTransform();
  1216. }
  1217. else
  1218. {
  1219. draw(DebugMesh::Cube, _obb.mtx, 1, false);
  1220. }
  1221. }
  1222. void draw(const Sphere& _sphere)
  1223. {
  1224. const Attrib& attrib = m_attrib[m_stack];
  1225. float mtx[16];
  1226. bx::mtxSRT(mtx
  1227. , _sphere.radius
  1228. , _sphere.radius
  1229. , _sphere.radius
  1230. , 0.0f
  1231. , 0.0f
  1232. , 0.0f
  1233. , _sphere.center.x
  1234. , _sphere.center.y
  1235. , _sphere.center.z
  1236. );
  1237. uint8_t lod = attrib.m_lod > DebugMesh::SphereMaxLod
  1238. ? uint8_t(DebugMesh::SphereMaxLod)
  1239. : attrib.m_lod
  1240. ;
  1241. draw(DebugMesh::Enum(DebugMesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
  1242. }
  1243. void draw(const Triangle& _triangle)
  1244. {
  1245. Attrib& attrib = m_attrib[m_stack];
  1246. if (attrib.m_wireframe)
  1247. {
  1248. moveTo(_triangle.v0);
  1249. lineTo(_triangle.v1);
  1250. lineTo(_triangle.v2);
  1251. close();
  1252. }
  1253. else
  1254. {
  1255. BX_STATIC_ASSERT(sizeof(DdVertex) == sizeof(bx::Vec3), "");
  1256. uint64_t old = attrib.m_state;
  1257. attrib.m_state &= ~BGFX_STATE_CULL_MASK;
  1258. draw(false, 3, reinterpret_cast<const DdVertex*>(&_triangle.v0.x), 0, NULL);
  1259. attrib.m_state = old;
  1260. }
  1261. }
  1262. void setUParams(const Attrib& _attrib, bool _wireframe)
  1263. {
  1264. const float flip = 0 == (_attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f;
  1265. const uint8_t alpha = _attrib.m_abgr >> 24;
  1266. float params[4][4] =
  1267. {
  1268. { // lightDir
  1269. 0.0f * flip,
  1270. -1.0f * flip,
  1271. 0.0f * flip,
  1272. 3.0f, // shininess
  1273. },
  1274. { // skyColor
  1275. 1.0f,
  1276. 0.9f,
  1277. 0.8f,
  1278. 0.0f, // unused
  1279. },
  1280. { // groundColor.xyz0
  1281. 0.2f,
  1282. 0.22f,
  1283. 0.5f,
  1284. 0.0f, // unused
  1285. },
  1286. { // matColor
  1287. ( (_attrib.m_abgr) & 0xff) / 255.0f,
  1288. ( (_attrib.m_abgr >> 8) & 0xff) / 255.0f,
  1289. ( (_attrib.m_abgr >> 16) & 0xff) / 255.0f,
  1290. (alpha) / 255.0f,
  1291. },
  1292. };
  1293. bx::store(params[0], bx::normalize(bx::load<bx::Vec3>(params[0]) ) );
  1294. m_encoder->setUniform(s_dds.u_params, params, 4);
  1295. m_encoder->setState(0
  1296. | _attrib.m_state
  1297. | (_wireframe ? BGFX_STATE_PT_LINES | BGFX_STATE_LINEAA | BGFX_STATE_BLEND_ALPHA
  1298. : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0)
  1299. );
  1300. }
  1301. void draw(GeometryHandle _handle)
  1302. {
  1303. const Geometry::Geometry& geometry = s_dds.m_geometry.m_geometry[_handle.idx];
  1304. m_encoder->setVertexBuffer(0, geometry.m_vbh);
  1305. const Attrib& attrib = m_attrib[m_stack];
  1306. const bool wireframe = attrib.m_wireframe;
  1307. setUParams(attrib, wireframe);
  1308. if (wireframe)
  1309. {
  1310. m_encoder->setIndexBuffer(
  1311. geometry.m_ibh
  1312. , geometry.m_topologyNumIndices[0]
  1313. , geometry.m_topologyNumIndices[1]
  1314. );
  1315. }
  1316. else if (0 != geometry.m_topologyNumIndices[0])
  1317. {
  1318. m_encoder->setIndexBuffer(
  1319. geometry.m_ibh
  1320. , 0
  1321. , geometry.m_topologyNumIndices[0]
  1322. );
  1323. }
  1324. m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1325. bgfx::ProgramHandle program = s_dds.m_program[wireframe ? Program::FillMesh : Program::FillLitMesh];
  1326. m_encoder->submit(m_viewId, program);
  1327. }
  1328. void draw(bool _lineList, uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  1329. {
  1330. flush();
  1331. if (_numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, DebugMeshVertex::ms_layout) )
  1332. {
  1333. bgfx::TransientVertexBuffer tvb;
  1334. bgfx::allocTransientVertexBuffer(&tvb, _numVertices, DebugMeshVertex::ms_layout);
  1335. bx::memCopy(tvb.data, _vertices, _numVertices * DebugMeshVertex::ms_layout.m_stride);
  1336. m_encoder->setVertexBuffer(0, &tvb);
  1337. const Attrib& attrib = m_attrib[m_stack];
  1338. const bool wireframe = _lineList || attrib.m_wireframe;
  1339. setUParams(attrib, wireframe);
  1340. if (0 < _numIndices)
  1341. {
  1342. uint32_t numIndices = _numIndices;
  1343. bgfx::TransientIndexBuffer tib;
  1344. if (!_lineList && wireframe)
  1345. {
  1346. numIndices = bgfx::topologyConvert(
  1347. bgfx::TopologyConvert::TriListToLineList
  1348. , NULL
  1349. , 0
  1350. , _indices
  1351. , _numIndices
  1352. , false
  1353. );
  1354. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1355. bgfx::topologyConvert(
  1356. bgfx::TopologyConvert::TriListToLineList
  1357. , tib.data
  1358. , numIndices * sizeof(uint16_t)
  1359. , _indices
  1360. , _numIndices
  1361. , false
  1362. );
  1363. }
  1364. else
  1365. {
  1366. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1367. bx::memCopy(tib.data, _indices, numIndices * sizeof(uint16_t) );
  1368. }
  1369. m_encoder->setIndexBuffer(&tib);
  1370. }
  1371. m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1372. bgfx::ProgramHandle program = s_dds.m_program[wireframe
  1373. ? Program::FillMesh
  1374. : Program::FillLitMesh
  1375. ];
  1376. m_encoder->submit(m_viewId, program);
  1377. }
  1378. }
  1379. void drawFrustum(const float* _viewProj)
  1380. {
  1381. bx::Plane planes[6];
  1382. buildFrustumPlanes(planes, _viewProj);
  1383. const bx::Vec3 points[8] =
  1384. {
  1385. intersectPlanes(planes[0], planes[2], planes[4]),
  1386. intersectPlanes(planes[0], planes[3], planes[4]),
  1387. intersectPlanes(planes[0], planes[3], planes[5]),
  1388. intersectPlanes(planes[0], planes[2], planes[5]),
  1389. intersectPlanes(planes[1], planes[2], planes[4]),
  1390. intersectPlanes(planes[1], planes[3], planes[4]),
  1391. intersectPlanes(planes[1], planes[3], planes[5]),
  1392. intersectPlanes(planes[1], planes[2], planes[5]),
  1393. };
  1394. moveTo(points[0]);
  1395. lineTo(points[1]);
  1396. lineTo(points[2]);
  1397. lineTo(points[3]);
  1398. close();
  1399. moveTo(points[4]);
  1400. lineTo(points[5]);
  1401. lineTo(points[6]);
  1402. lineTo(points[7]);
  1403. close();
  1404. moveTo(points[0]);
  1405. lineTo(points[4]);
  1406. moveTo(points[1]);
  1407. lineTo(points[5]);
  1408. moveTo(points[2]);
  1409. lineTo(points[6]);
  1410. moveTo(points[3]);
  1411. lineTo(points[7]);
  1412. }
  1413. void drawFrustum(const void* _viewProj)
  1414. {
  1415. drawFrustum( (const float*)_viewProj);
  1416. }
  1417. void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  1418. {
  1419. const Attrib& attrib = m_attrib[m_stack];
  1420. const uint32_t num = getCircleLod(attrib.m_lod);
  1421. const float step = bx::kPi * 2.0f / num;
  1422. _degrees = bx::wrap(_degrees, 360.0f);
  1423. bx::Vec3 pos = getPoint(
  1424. _axis
  1425. , bx::sin(step * 0)*_radius
  1426. , bx::cos(step * 0)*_radius
  1427. );
  1428. moveTo({pos.x + _x, pos.y + _y, pos.z + _z});
  1429. uint32_t n = uint32_t(num*_degrees/360.0f);
  1430. for (uint32_t ii = 1; ii < n+1; ++ii)
  1431. {
  1432. pos = getPoint(
  1433. _axis
  1434. , bx::sin(step * ii)*_radius
  1435. , bx::cos(step * ii)*_radius
  1436. );
  1437. lineTo({pos.x + _x, pos.y + _y, pos.z + _z});
  1438. }
  1439. moveTo(_x, _y, _z);
  1440. pos = getPoint(
  1441. _axis
  1442. , bx::sin(step * 0)*_radius
  1443. , bx::cos(step * 0)*_radius
  1444. );
  1445. lineTo({pos.x + _x, pos.y + _y, pos.z + _z});
  1446. pos = getPoint(
  1447. _axis
  1448. , bx::sin(step * n)*_radius
  1449. , bx::cos(step * n)*_radius
  1450. );
  1451. moveTo({pos.x + _x, pos.y + _y, pos.z + _z});
  1452. lineTo(_x, _y, _z);
  1453. }
  1454. void drawCircle(const bx::Vec3& _normal, const bx::Vec3& _center, float _radius, float _weight)
  1455. {
  1456. const Attrib& attrib = m_attrib[m_stack];
  1457. const uint32_t num = getCircleLod(attrib.m_lod);
  1458. const float step = bx::kPi * 2.0f / num;
  1459. _weight = bx::clamp(_weight, 0.0f, 2.0f);
  1460. bx::Vec3 udir;
  1461. bx::Vec3 vdir;
  1462. bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
  1463. float xy0[2];
  1464. float xy1[2];
  1465. circle(xy0, 0.0f);
  1466. squircle(xy1, 0.0f);
  1467. bx::Vec3 pos = bx::mul(udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius);
  1468. bx::Vec3 tmp0 = bx::mul(vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius);
  1469. bx::Vec3 tmp1 = bx::add(pos, tmp0);
  1470. bx::Vec3 tmp2 = bx::add(tmp1, _center);
  1471. moveTo(tmp2);
  1472. for (uint32_t ii = 1; ii < num; ++ii)
  1473. {
  1474. float angle = step * ii;
  1475. circle(xy0, angle);
  1476. squircle(xy1, angle);
  1477. pos = bx::mul(udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius);
  1478. tmp0 = bx::mul(vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius);
  1479. tmp1 = bx::add(pos, tmp0);
  1480. tmp2 = bx::add(tmp1, _center);
  1481. lineTo(tmp2);
  1482. }
  1483. close();
  1484. }
  1485. void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  1486. {
  1487. const Attrib& attrib = m_attrib[m_stack];
  1488. const uint32_t num = getCircleLod(attrib.m_lod);
  1489. const float step = bx::kPi * 2.0f / num;
  1490. _weight = bx::clamp(_weight, 0.0f, 2.0f);
  1491. float xy0[2];
  1492. float xy1[2];
  1493. circle(xy0, 0.0f);
  1494. squircle(xy1, 0.0f);
  1495. bx::Vec3 pos = getPoint(
  1496. _axis
  1497. , bx::lerp(xy0[0], xy1[0], _weight)*_radius
  1498. , bx::lerp(xy0[1], xy1[1], _weight)*_radius
  1499. );
  1500. moveTo({pos.x + _x, pos.y + _y, pos.z + _z});
  1501. for (uint32_t ii = 1; ii < num; ++ii)
  1502. {
  1503. float angle = step * ii;
  1504. circle(xy0, angle);
  1505. squircle(xy1, angle);
  1506. pos = getPoint(
  1507. _axis
  1508. , bx::lerp(xy0[0], xy1[0], _weight)*_radius
  1509. , bx::lerp(xy0[1], xy1[1], _weight)*_radius
  1510. );
  1511. lineTo({pos.x + _x, pos.y + _y, pos.z + _z});
  1512. }
  1513. close();
  1514. }
  1515. void drawQuad(const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
  1516. {
  1517. const Attrib& attrib = m_attrib[m_stack];
  1518. if (attrib.m_wireframe)
  1519. {
  1520. bx::Vec3 udir, vdir;
  1521. bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
  1522. const float halfExtent = _size*0.5f;
  1523. const bx::Vec3 umin = bx::mul(udir, -halfExtent);
  1524. const bx::Vec3 umax = bx::mul(udir, halfExtent);
  1525. const bx::Vec3 vmin = bx::mul(vdir, -halfExtent);
  1526. const bx::Vec3 vmax = bx::mul(vdir, halfExtent);
  1527. const bx::Vec3 center = _center;
  1528. moveTo(bx::add(center, bx::add(umin, vmin) ) );
  1529. lineTo(bx::add(center, bx::add(umax, vmin) ) );
  1530. lineTo(bx::add(center, bx::add(umax, vmax) ) );
  1531. lineTo(bx::add(center, bx::add(umin, vmax) ) );
  1532. close();
  1533. }
  1534. else
  1535. {
  1536. float mtx[16];
  1537. bx::mtxFromNormal(mtx, _normal, _size*0.5f, _center, attrib.m_spin);
  1538. draw(DebugMesh::Quad, mtx, 1, false);
  1539. }
  1540. }
  1541. void drawQuad(SpriteHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
  1542. {
  1543. if (!isValid(_handle) )
  1544. {
  1545. drawQuad(_normal, _center, _size);
  1546. return;
  1547. }
  1548. if (m_posQuad == BX_COUNTOF(m_cacheQuad) )
  1549. {
  1550. flushQuad();
  1551. }
  1552. const Attrib& attrib = m_attrib[m_stack];
  1553. bx::Vec3 udir, vdir;
  1554. bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
  1555. const Pack2D& pack = s_dds.m_sprite.get(_handle);
  1556. const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE;
  1557. const float us = pack.m_x * invTextureSize;
  1558. const float vs = pack.m_y * invTextureSize;
  1559. const float ue = (pack.m_x + pack.m_width ) * invTextureSize;
  1560. const float ve = (pack.m_y + pack.m_height) * invTextureSize;
  1561. const float aspectRatio = float(pack.m_width)/float(pack.m_height);
  1562. const float halfExtentU = aspectRatio*_size*0.5f;
  1563. const float halfExtentV = 1.0f/aspectRatio*_size*0.5f;
  1564. const bx::Vec3 umin = bx::mul(udir, -halfExtentU);
  1565. const bx::Vec3 umax = bx::mul(udir, halfExtentU);
  1566. const bx::Vec3 vmin = bx::mul(vdir, -halfExtentV);
  1567. const bx::Vec3 vmax = bx::mul(vdir, halfExtentV);
  1568. const bx::Vec3 center = _center;
  1569. DebugUvVertex* vertex = &m_cacheQuad[m_posQuad];
  1570. m_posQuad += 4;
  1571. bx::store(&vertex->m_x, bx::add(center, bx::add(umin, vmin) ) );
  1572. vertex->m_u = us;
  1573. vertex->m_v = vs;
  1574. vertex->m_abgr = attrib.m_abgr;
  1575. ++vertex;
  1576. bx::store(&vertex->m_x, bx::add(center, bx::add(umax, vmin) ) );
  1577. vertex->m_u = ue;
  1578. vertex->m_v = vs;
  1579. vertex->m_abgr = attrib.m_abgr;
  1580. ++vertex;
  1581. bx::store(&vertex->m_x, bx::add(center, bx::add(umin, vmax) ) );
  1582. vertex->m_u = us;
  1583. vertex->m_v = ve;
  1584. vertex->m_abgr = attrib.m_abgr;
  1585. ++vertex;
  1586. bx::store(&vertex->m_x, bx::add(center, bx::add(umax, vmax) ) );
  1587. vertex->m_u = ue;
  1588. vertex->m_v = ve;
  1589. vertex->m_abgr = attrib.m_abgr;
  1590. ++vertex;
  1591. }
  1592. void drawQuad(bgfx::TextureHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
  1593. {
  1594. BX_UNUSED(_handle, _normal, _center, _size);
  1595. }
  1596. void drawCone(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
  1597. {
  1598. const Attrib& attrib = m_attrib[m_stack];
  1599. const bx::Vec3 normal = bx::normalize(bx::sub(_from, _to) );
  1600. float mtx[2][16];
  1601. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1602. bx::memCopy(mtx[1], mtx[0], 64);
  1603. mtx[1][12] = _to.x;
  1604. mtx[1][13] = _to.y;
  1605. mtx[1][14] = _to.z;
  1606. uint8_t lod = attrib.m_lod > DebugMesh::ConeMaxLod
  1607. ? uint8_t(DebugMesh::ConeMaxLod)
  1608. : attrib.m_lod
  1609. ;
  1610. draw(DebugMesh::Enum(DebugMesh::Cone0 + lod), mtx[0], 2, attrib.m_wireframe);
  1611. }
  1612. void drawCylinder(const bx::Vec3& _from, const bx::Vec3& _to, float _radius, bool _capsule)
  1613. {
  1614. const Attrib& attrib = m_attrib[m_stack];
  1615. const bx::Vec3 normal = bx::normalize(bx::sub(_from, _to) );
  1616. float mtx[2][16];
  1617. bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
  1618. bx::memCopy(mtx[1], mtx[0], 64);
  1619. mtx[1][12] = _to.x;
  1620. mtx[1][13] = _to.y;
  1621. mtx[1][14] = _to.z;
  1622. if (_capsule)
  1623. {
  1624. uint8_t lod = attrib.m_lod > DebugMesh::CapsuleMaxLod
  1625. ? uint8_t(DebugMesh::CapsuleMaxLod)
  1626. : attrib.m_lod
  1627. ;
  1628. draw(DebugMesh::Enum(DebugMesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe);
  1629. Sphere sphere;
  1630. sphere.center = _from;
  1631. sphere.radius = _radius;
  1632. draw(sphere);
  1633. sphere.center = _to;
  1634. draw(sphere);
  1635. }
  1636. else
  1637. {
  1638. uint8_t lod = attrib.m_lod > DebugMesh::CylinderMaxLod
  1639. ? uint8_t(DebugMesh::CylinderMaxLod)
  1640. : attrib.m_lod
  1641. ;
  1642. draw(DebugMesh::Enum(DebugMesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe);
  1643. }
  1644. }
  1645. void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
  1646. {
  1647. push();
  1648. if (_thickness > 0.0f)
  1649. {
  1650. const bx::Vec3 from = { _x, _y, _z };
  1651. bx::Vec3 mid;
  1652. bx::Vec3 to;
  1653. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1654. mid = { _x + _len - _thickness, _y, _z };
  1655. to = { _x + _len, _y, _z };
  1656. drawCylinder(from, mid, _thickness, false);
  1657. drawCone(mid, to, _thickness);
  1658. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1659. mid = { _x, _y + _len - _thickness, _z };
  1660. to = { _x, _y + _len, _z };
  1661. drawCylinder(from, mid, _thickness, false);
  1662. drawCone(mid, to, _thickness);
  1663. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1664. mid = { _x, _y, _z + _len - _thickness };
  1665. to = { _x, _y, _z + _len };
  1666. drawCylinder(from, mid, _thickness, false);
  1667. drawCone(mid, to, _thickness);
  1668. }
  1669. else
  1670. {
  1671. setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
  1672. moveTo(_x, _y, _z);
  1673. lineTo(_x + _len, _y, _z);
  1674. setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
  1675. moveTo(_x, _y, _z);
  1676. lineTo(_x, _y + _len, _z);
  1677. setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
  1678. moveTo(_x, _y, _z);
  1679. lineTo(_x, _y, _z + _len);
  1680. }
  1681. pop();
  1682. }
  1683. void drawGrid(const bx::Vec3& _normal, const bx::Vec3& _center, uint32_t _size, float _step)
  1684. {
  1685. const Attrib& attrib = m_attrib[m_stack];
  1686. bx::Vec3 udir;
  1687. bx::Vec3 vdir;
  1688. bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
  1689. udir = bx::mul(udir, _step);
  1690. vdir = bx::mul(vdir, _step);
  1691. const uint32_t num = (_size/2)*2+1;
  1692. const float halfExtent = float(_size/2);
  1693. const bx::Vec3 umin = bx::mul(udir, -halfExtent);
  1694. const bx::Vec3 umax = bx::mul(udir, halfExtent);
  1695. const bx::Vec3 vmin = bx::mul(vdir, -halfExtent);
  1696. const bx::Vec3 vmax = bx::mul(vdir, halfExtent);
  1697. bx::Vec3 xs = bx::add(_center, bx::add(umin, vmin) );
  1698. bx::Vec3 xe = bx::add(_center, bx::add(umax, vmin) );
  1699. bx::Vec3 ys = bx::add(_center, bx::add(umin, vmin) );
  1700. bx::Vec3 ye = bx::add(_center, bx::add(umin, vmax) );
  1701. for (uint32_t ii = 0; ii < num; ++ii)
  1702. {
  1703. moveTo(xs);
  1704. lineTo(xe);
  1705. xs = bx::add(xs, vdir);
  1706. xe = bx::add(xe, vdir);
  1707. moveTo(ys);
  1708. lineTo(ye);
  1709. ys = bx::add(ys, udir);
  1710. ye = bx::add(ye, udir);
  1711. }
  1712. }
  1713. void drawGrid(Axis::Enum _axis, const bx::Vec3& _center, uint32_t _size, float _step)
  1714. {
  1715. push();
  1716. pushTranslate(_center);
  1717. const uint32_t num = (_size/2)*2-1;
  1718. const float halfExtent = float(_size/2) * _step;
  1719. setColor(0xff606060);
  1720. float yy = -halfExtent + _step;
  1721. for (uint32_t ii = 0; ii < num; ++ii)
  1722. {
  1723. moveTo(_axis, -halfExtent, yy);
  1724. lineTo(_axis, halfExtent, yy);
  1725. moveTo(_axis, yy, -halfExtent);
  1726. lineTo(_axis, yy, halfExtent);
  1727. yy += _step;
  1728. }
  1729. setColor(0xff101010);
  1730. moveTo(_axis, -halfExtent, -halfExtent);
  1731. lineTo(_axis, -halfExtent, halfExtent);
  1732. lineTo(_axis, halfExtent, halfExtent);
  1733. lineTo(_axis, halfExtent, -halfExtent);
  1734. close();
  1735. moveTo(_axis, -halfExtent, 0.0f);
  1736. lineTo(_axis, halfExtent, 0.0f);
  1737. moveTo(_axis, 0.0f, -halfExtent);
  1738. lineTo(_axis, 0.0f, halfExtent);
  1739. popTransform();
  1740. pop();
  1741. }
  1742. void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
  1743. {
  1744. push();
  1745. setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
  1746. drawCircle(Axis::X, _x, _y, _z, _radius, 0.0f);
  1747. setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
  1748. drawCircle(Axis::Y, _x, _y, _z, _radius, 0.0f);
  1749. setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
  1750. drawCircle(Axis::Z, _x, _y, _z, _radius, 0.0f);
  1751. pop();
  1752. }
  1753. void draw(DebugMesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe)
  1754. {
  1755. pushTransform(_mtx, _num, false /* flush */);
  1756. const DebugMesh& mesh = s_dds.m_mesh[_mesh];
  1757. if (0 != mesh.m_numIndices[_wireframe])
  1758. {
  1759. m_encoder->setIndexBuffer(s_dds.m_ibh
  1760. , mesh.m_startIndex[_wireframe]
  1761. , mesh.m_numIndices[_wireframe]
  1762. );
  1763. }
  1764. const Attrib& attrib = m_attrib[m_stack];
  1765. setUParams(attrib, _wireframe);
  1766. MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
  1767. m_encoder->setTransform(stack.mtx, stack.num);
  1768. m_encoder->setVertexBuffer(0, s_dds.m_vbh, mesh.m_startVertex, mesh.m_numVertices);
  1769. m_encoder->submit(m_viewId, s_dds.m_program[_wireframe ? Program::Fill : Program::FillLit]);
  1770. popTransform(false /* flush */);
  1771. }
  1772. void softFlush()
  1773. {
  1774. if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
  1775. {
  1776. flush();
  1777. }
  1778. }
  1779. void flush()
  1780. {
  1781. if (0 != m_pos)
  1782. {
  1783. if (checkAvailTransientBuffers(m_pos, DebugVertex::ms_layout, m_indexPos) )
  1784. {
  1785. bgfx::TransientVertexBuffer tvb;
  1786. bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_layout);
  1787. bx::memCopy(tvb.data, m_cache, m_pos * DebugVertex::ms_layout.m_stride);
  1788. bgfx::TransientIndexBuffer tib;
  1789. bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
  1790. bx::memCopy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
  1791. const Attrib& attrib = m_attrib[m_stack];
  1792. m_encoder->setVertexBuffer(0, &tvb);
  1793. m_encoder->setIndexBuffer(&tib);
  1794. m_encoder->setState(0
  1795. | BGFX_STATE_WRITE_RGB
  1796. | BGFX_STATE_PT_LINES
  1797. | attrib.m_state
  1798. | BGFX_STATE_LINEAA
  1799. | BGFX_STATE_BLEND_ALPHA
  1800. );
  1801. m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1802. bgfx::ProgramHandle program = s_dds.m_program[attrib.m_stipple ? 1 : 0];
  1803. m_encoder->submit(m_viewId, program);
  1804. }
  1805. m_state = State::None;
  1806. m_pos = 0;
  1807. m_indexPos = 0;
  1808. m_vertexPos = 0;
  1809. }
  1810. }
  1811. void flushQuad()
  1812. {
  1813. if (0 != m_posQuad)
  1814. {
  1815. const uint32_t numIndices = m_posQuad/4*6;
  1816. if (checkAvailTransientBuffers(m_posQuad, DebugUvVertex::ms_layout, numIndices) )
  1817. {
  1818. bgfx::TransientVertexBuffer tvb;
  1819. bgfx::allocTransientVertexBuffer(&tvb, m_posQuad, DebugUvVertex::ms_layout);
  1820. bx::memCopy(tvb.data, m_cacheQuad, m_posQuad * DebugUvVertex::ms_layout.m_stride);
  1821. bgfx::TransientIndexBuffer tib;
  1822. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1823. uint16_t* indices = (uint16_t*)tib.data;
  1824. for (uint16_t ii = 0, num = m_posQuad/4; ii < num; ++ii)
  1825. {
  1826. uint16_t startVertex = ii*4;
  1827. indices[0] = startVertex+0;
  1828. indices[1] = startVertex+1;
  1829. indices[2] = startVertex+2;
  1830. indices[3] = startVertex+1;
  1831. indices[4] = startVertex+3;
  1832. indices[5] = startVertex+2;
  1833. indices += 6;
  1834. }
  1835. const Attrib& attrib = m_attrib[m_stack];
  1836. m_encoder->setVertexBuffer(0, &tvb);
  1837. m_encoder->setIndexBuffer(&tib);
  1838. m_encoder->setState(0
  1839. | (attrib.m_state & ~BGFX_STATE_CULL_MASK)
  1840. );
  1841. m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
  1842. m_encoder->setTexture(0, s_dds.s_texColor, s_dds.m_texture);
  1843. m_encoder->submit(m_viewId, s_dds.m_program[Program::FillTexture]);
  1844. }
  1845. m_posQuad = 0;
  1846. }
  1847. }
  1848. struct State
  1849. {
  1850. enum Enum
  1851. {
  1852. None,
  1853. MoveTo,
  1854. LineTo,
  1855. Count
  1856. };
  1857. };
  1858. static const uint32_t kCacheSize = 1024;
  1859. static const uint32_t kStackSize = 16;
  1860. static const uint32_t kCacheQuadSize = 1024;
  1861. BX_STATIC_ASSERT(kCacheSize >= 3, "Cache must be at least 3 elements.");
  1862. DebugVertex m_cache[kCacheSize+1];
  1863. DebugUvVertex m_cacheQuad[kCacheQuadSize];
  1864. uint16_t m_indices[kCacheSize*2];
  1865. uint16_t m_pos;
  1866. uint16_t m_posQuad;
  1867. uint16_t m_indexPos;
  1868. uint16_t m_vertexPos;
  1869. uint32_t m_mtxStackCurrent;
  1870. struct MatrixStack
  1871. {
  1872. void reset()
  1873. {
  1874. mtx = 0;
  1875. num = 1;
  1876. data = NULL;
  1877. }
  1878. uint32_t mtx;
  1879. uint16_t num;
  1880. float* data;
  1881. };
  1882. MatrixStack m_mtxStack[32];
  1883. bgfx::ViewId m_viewId;
  1884. uint8_t m_stack;
  1885. bool m_depthTestLess;
  1886. Attrib m_attrib[kStackSize];
  1887. State::Enum m_state;
  1888. bgfx::Encoder* m_encoder;
  1889. bgfx::Encoder* m_defaultEncoder;
  1890. };
  1891. static DebugDrawEncoderImpl s_dde;
  1892. BX_STATIC_ASSERT(sizeof(DebugDrawEncoderImpl) <= sizeof(DebugDrawEncoder), "Size must match");
  1893. void ddInit(bx::AllocatorI* _allocator)
  1894. {
  1895. s_dds.init(_allocator);
  1896. s_dde.init(bgfx::begin() );
  1897. }
  1898. void ddShutdown()
  1899. {
  1900. s_dde.shutdown();
  1901. s_dds.shutdown();
  1902. }
  1903. SpriteHandle ddCreateSprite(uint16_t _width, uint16_t _height, const void* _data)
  1904. {
  1905. return s_dds.createSprite(_width, _height, _data);
  1906. }
  1907. void ddDestroy(SpriteHandle _handle)
  1908. {
  1909. s_dds.destroy(_handle);
  1910. }
  1911. GeometryHandle ddCreateGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32)
  1912. {
  1913. return s_dds.createGeometry(_numVertices, _vertices, _numIndices, _indices, _index32);
  1914. }
  1915. void ddDestroy(GeometryHandle _handle)
  1916. {
  1917. s_dds.destroy(_handle);
  1918. }
  1919. #define DEBUG_DRAW_ENCODER(_func) reinterpret_cast<DebugDrawEncoderImpl*>(this)->_func
  1920. DebugDrawEncoder::DebugDrawEncoder()
  1921. {
  1922. DEBUG_DRAW_ENCODER(init(s_dde.m_defaultEncoder) );
  1923. }
  1924. DebugDrawEncoder::~DebugDrawEncoder()
  1925. {
  1926. DEBUG_DRAW_ENCODER(shutdown() );
  1927. }
  1928. void DebugDrawEncoder::begin(uint16_t _viewId, bool _depthTestLess, bgfx::Encoder* _encoder)
  1929. {
  1930. DEBUG_DRAW_ENCODER(begin(_viewId, _depthTestLess, _encoder) );
  1931. }
  1932. void DebugDrawEncoder::end()
  1933. {
  1934. DEBUG_DRAW_ENCODER(end() );
  1935. }
  1936. void DebugDrawEncoder::push()
  1937. {
  1938. DEBUG_DRAW_ENCODER(push() );
  1939. }
  1940. void DebugDrawEncoder::pop()
  1941. {
  1942. DEBUG_DRAW_ENCODER(pop() );
  1943. }
  1944. void DebugDrawEncoder::setDepthTestLess(bool _depthTestLess)
  1945. {
  1946. DEBUG_DRAW_ENCODER(setDepthTestLess(_depthTestLess) );
  1947. }
  1948. void DebugDrawEncoder::setState(bool _depthTest, bool _depthWrite, bool _clockwise)
  1949. {
  1950. DEBUG_DRAW_ENCODER(setState(_depthTest, _depthWrite, _clockwise) );
  1951. }
  1952. void DebugDrawEncoder::setColor(uint32_t _abgr)
  1953. {
  1954. DEBUG_DRAW_ENCODER(setColor(_abgr) );
  1955. }
  1956. void DebugDrawEncoder::setLod(uint8_t _lod)
  1957. {
  1958. DEBUG_DRAW_ENCODER(setLod(_lod) );
  1959. }
  1960. void DebugDrawEncoder::setWireframe(bool _wireframe)
  1961. {
  1962. DEBUG_DRAW_ENCODER(setWireframe(_wireframe) );
  1963. }
  1964. void DebugDrawEncoder::setStipple(bool _stipple, float _scale, float _offset)
  1965. {
  1966. DEBUG_DRAW_ENCODER(setStipple(_stipple, _scale, _offset) );
  1967. }
  1968. void DebugDrawEncoder::setSpin(float _spin)
  1969. {
  1970. DEBUG_DRAW_ENCODER(setSpin(_spin) );
  1971. }
  1972. void DebugDrawEncoder::setTransform(const void* _mtx)
  1973. {
  1974. DEBUG_DRAW_ENCODER(setTransform(_mtx) );
  1975. }
  1976. void DebugDrawEncoder::setTranslate(float _x, float _y, float _z)
  1977. {
  1978. DEBUG_DRAW_ENCODER(setTranslate(_x, _y, _z) );
  1979. }
  1980. void DebugDrawEncoder::pushTransform(const void* _mtx)
  1981. {
  1982. DEBUG_DRAW_ENCODER(pushTransform(_mtx, 1) );
  1983. }
  1984. void DebugDrawEncoder::popTransform()
  1985. {
  1986. DEBUG_DRAW_ENCODER(popTransform() );
  1987. }
  1988. void DebugDrawEncoder::moveTo(float _x, float _y, float _z)
  1989. {
  1990. DEBUG_DRAW_ENCODER(moveTo(_x, _y, _z) );
  1991. }
  1992. void DebugDrawEncoder::moveTo(const bx::Vec3& _pos)
  1993. {
  1994. DEBUG_DRAW_ENCODER(moveTo(_pos) );
  1995. }
  1996. void DebugDrawEncoder::lineTo(float _x, float _y, float _z)
  1997. {
  1998. DEBUG_DRAW_ENCODER(lineTo(_x, _y, _z) );
  1999. }
  2000. void DebugDrawEncoder::lineTo(const bx::Vec3& _pos)
  2001. {
  2002. DEBUG_DRAW_ENCODER(lineTo(_pos) );
  2003. }
  2004. void DebugDrawEncoder::close()
  2005. {
  2006. DEBUG_DRAW_ENCODER(close() );
  2007. }
  2008. void DebugDrawEncoder::draw(const Aabb& _aabb)
  2009. {
  2010. DEBUG_DRAW_ENCODER(draw(_aabb) );
  2011. }
  2012. void DebugDrawEncoder::draw(const Cylinder& _cylinder)
  2013. {
  2014. DEBUG_DRAW_ENCODER(draw(_cylinder, false) );
  2015. }
  2016. void DebugDrawEncoder::draw(const Capsule& _capsule)
  2017. {
  2018. DEBUG_DRAW_ENCODER(draw(*( (const Cylinder*)&_capsule), true) );
  2019. }
  2020. void DebugDrawEncoder::draw(const Disk& _disk)
  2021. {
  2022. DEBUG_DRAW_ENCODER(draw(_disk) );
  2023. }
  2024. void DebugDrawEncoder::draw(const Obb& _obb)
  2025. {
  2026. DEBUG_DRAW_ENCODER(draw(_obb) );
  2027. }
  2028. void DebugDrawEncoder::draw(const Sphere& _sphere)
  2029. {
  2030. DEBUG_DRAW_ENCODER(draw(_sphere) );
  2031. }
  2032. void DebugDrawEncoder::draw(const Triangle& _triangle)
  2033. {
  2034. DEBUG_DRAW_ENCODER(draw(_triangle) );
  2035. }
  2036. void DebugDrawEncoder::draw(const Cone& _cone)
  2037. {
  2038. DEBUG_DRAW_ENCODER(drawCone(_cone.pos, _cone.end, _cone.radius) );
  2039. }
  2040. void DebugDrawEncoder::draw(GeometryHandle _handle)
  2041. {
  2042. DEBUG_DRAW_ENCODER(draw(_handle) );
  2043. }
  2044. void DebugDrawEncoder::drawLineList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  2045. {
  2046. DEBUG_DRAW_ENCODER(draw(true, _numVertices, _vertices, _numIndices, _indices) );
  2047. }
  2048. void DebugDrawEncoder::drawTriList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
  2049. {
  2050. DEBUG_DRAW_ENCODER(draw(false, _numVertices, _vertices, _numIndices, _indices) );
  2051. }
  2052. void DebugDrawEncoder::drawFrustum(const void* _viewProj)
  2053. {
  2054. DEBUG_DRAW_ENCODER(drawFrustum(_viewProj) );
  2055. }
  2056. void DebugDrawEncoder::drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
  2057. {
  2058. DEBUG_DRAW_ENCODER(drawArc(_axis, _x, _y, _z, _radius, _degrees) );
  2059. }
  2060. void DebugDrawEncoder::drawCircle(const bx::Vec3& _normal, const bx::Vec3& _center, float _radius, float _weight)
  2061. {
  2062. DEBUG_DRAW_ENCODER(drawCircle(_normal, _center, _radius, _weight) );
  2063. }
  2064. void DebugDrawEncoder::drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
  2065. {
  2066. DEBUG_DRAW_ENCODER(drawCircle(_axis, _x, _y, _z, _radius, _weight) );
  2067. }
  2068. void DebugDrawEncoder::drawQuad(const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
  2069. {
  2070. DEBUG_DRAW_ENCODER(drawQuad(_normal, _center, _size) );
  2071. }
  2072. void DebugDrawEncoder::drawQuad(SpriteHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
  2073. {
  2074. DEBUG_DRAW_ENCODER(drawQuad(_handle, _normal, _center, _size) );
  2075. }
  2076. void DebugDrawEncoder::drawQuad(bgfx::TextureHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
  2077. {
  2078. DEBUG_DRAW_ENCODER(drawQuad(_handle, _normal, _center, _size) );
  2079. }
  2080. void DebugDrawEncoder::drawCone(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
  2081. {
  2082. DEBUG_DRAW_ENCODER(drawCone(_from, _to, _radius) );
  2083. }
  2084. void DebugDrawEncoder::drawCylinder(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
  2085. {
  2086. DEBUG_DRAW_ENCODER(drawCylinder(_from, _to, _radius, false) );
  2087. }
  2088. void DebugDrawEncoder::drawCapsule(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
  2089. {
  2090. DEBUG_DRAW_ENCODER(drawCylinder(_from, _to, _radius, true) );
  2091. }
  2092. void DebugDrawEncoder::drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
  2093. {
  2094. DEBUG_DRAW_ENCODER(drawAxis(_x, _y, _z, _len, _highlight, _thickness) );
  2095. }
  2096. void DebugDrawEncoder::drawGrid(const bx::Vec3& _normal, const bx::Vec3& _center, uint32_t _size, float _step)
  2097. {
  2098. DEBUG_DRAW_ENCODER(drawGrid(_normal, _center, _size, _step) );
  2099. }
  2100. void DebugDrawEncoder::drawGrid(Axis::Enum _axis, const bx::Vec3& _center, uint32_t _size, float _step)
  2101. {
  2102. DEBUG_DRAW_ENCODER(drawGrid(_axis, _center, _size, _step) );
  2103. }
  2104. void DebugDrawEncoder::drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _highlight)
  2105. {
  2106. DEBUG_DRAW_ENCODER(drawOrb(_x, _y, _z, _radius, _highlight) );
  2107. }
  2108. DebugDrawEncoderScopePush::DebugDrawEncoderScopePush(DebugDrawEncoder& _dde)
  2109. : m_dde(_dde)
  2110. {
  2111. m_dde.push();
  2112. }
  2113. DebugDrawEncoderScopePush::~DebugDrawEncoderScopePush()
  2114. {
  2115. m_dde.pop();
  2116. }