debugdraw.cpp 62 KB

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