debugdraw.cpp 67 KB

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