debugdraw.cpp 66 KB

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