debugdraw.cpp 65 KB

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