debugdraw.cpp 61 KB

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