debugdraw.cpp 62 KB

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