debugdraw.cpp 64 KB

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