debugdraw.cpp 63 KB

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