| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564 |
- /*
- * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
- * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
- */
- #include <bgfx/bgfx.h>
- #include <bgfx/embedded_shader.h>
- #include "debugdraw.h"
- #include "../bgfx_utils.h"
- #include "../packrect.h"
- #include <bx/debug.h>
- #include <bx/mutex.h>
- #include <bx/math.h>
- #include <bx/sort.h>
- #include <bx/uint32_t.h>
- #include <bx/handlealloc.h>
- #ifndef DEBUG_DRAW_CONFIG_MAX_GEOMETRY
- # define DEBUG_DRAW_CONFIG_MAX_GEOMETRY 256
- #endif // DEBUG_DRAW_CONFIG_MAX_GEOMETRY
- struct DebugVertex
- {
- float m_x;
- float m_y;
- float m_z;
- float m_len;
- uint32_t m_abgr;
- static void init()
- {
- ms_layout
- .begin()
- .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
- .add(bgfx::Attrib::TexCoord0, 1, bgfx::AttribType::Float)
- .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
- .end();
- }
- static bgfx::VertexLayout ms_layout;
- };
- bgfx::VertexLayout DebugVertex::ms_layout;
- struct DebugUvVertex
- {
- float m_x;
- float m_y;
- float m_z;
- float m_u;
- float m_v;
- uint32_t m_abgr;
- static void init()
- {
- ms_layout
- .begin()
- .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
- .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
- .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
- .end();
- }
- static bgfx::VertexLayout ms_layout;
- };
- bgfx::VertexLayout DebugUvVertex::ms_layout;
- struct DebugShapeVertex
- {
- float m_x;
- float m_y;
- float m_z;
- uint8_t m_indices[4];
- static void init()
- {
- ms_layout
- .begin()
- .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
- .add(bgfx::Attrib::Indices, 4, bgfx::AttribType::Uint8)
- .end();
- }
- static bgfx::VertexLayout ms_layout;
- };
- bgfx::VertexLayout DebugShapeVertex::ms_layout;
- struct DebugMeshVertex
- {
- float m_x;
- float m_y;
- float m_z;
- static void init()
- {
- ms_layout
- .begin()
- .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
- .end();
- }
- static bgfx::VertexLayout ms_layout;
- };
- bgfx::VertexLayout DebugMeshVertex::ms_layout;
- static DebugShapeVertex s_quadVertices[4] =
- {
- {-1.0f, 0.0f, 1.0f, { 0, 0, 0, 0 } },
- { 1.0f, 0.0f, 1.0f, { 0, 0, 0, 0 } },
- {-1.0f, 0.0f, -1.0f, { 0, 0, 0, 0 } },
- { 1.0f, 0.0f, -1.0f, { 0, 0, 0, 0 } },
- };
- static const uint16_t s_quadIndices[6] =
- {
- 0, 1, 2,
- 1, 3, 2,
- };
- static DebugShapeVertex s_cubeVertices[8] =
- {
- {-1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
- { 1.0f, 1.0f, 1.0f, { 0, 0, 0, 0 } },
- {-1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
- { 1.0f, -1.0f, 1.0f, { 0, 0, 0, 0 } },
- {-1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
- { 1.0f, 1.0f, -1.0f, { 0, 0, 0, 0 } },
- {-1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
- { 1.0f, -1.0f, -1.0f, { 0, 0, 0, 0 } },
- };
- static const uint16_t s_cubeIndices[36] =
- {
- 0, 1, 2, // 0
- 1, 3, 2,
- 4, 6, 5, // 2
- 5, 6, 7,
- 0, 2, 4, // 4
- 4, 2, 6,
- 1, 5, 3, // 6
- 5, 7, 3,
- 0, 4, 1, // 8
- 4, 5, 1,
- 2, 3, 6, // 10
- 6, 3, 7,
- };
- static const uint8_t s_circleLod[] =
- {
- 37,
- 29,
- 23,
- 17,
- 11,
- };
- static uint8_t getCircleLod(uint8_t _lod)
- {
- _lod = _lod > BX_COUNTOF(s_circleLod)-1 ? BX_COUNTOF(s_circleLod)-1 : _lod;
- return s_circleLod[_lod];
- }
- static void circle(float* _out, float _angle)
- {
- float sa = bx::sin(_angle);
- float ca = bx::cos(_angle);
- _out[0] = sa;
- _out[1] = ca;
- }
- static void squircle(float* _out, float _angle)
- {
- float sa = bx::sin(_angle);
- float ca = bx::cos(_angle);
- _out[0] = bx::sqrt(bx::abs(sa) ) * bx::sign(sa);
- _out[1] = bx::sqrt(bx::abs(ca) ) * bx::sign(ca);
- }
- uint32_t genSphere(uint8_t _subdiv0, void* _pos0 = NULL, uint16_t _posStride0 = 0, void* _normals0 = NULL, uint16_t _normalStride0 = 0)
- {
- if (NULL != _pos0)
- {
- struct Gen
- {
- Gen(void* _pos, uint16_t _posStride, void* _normals, uint16_t _normalStride, uint8_t _subdiv)
- : m_pos( (uint8_t*)_pos)
- , m_normals( (uint8_t*)_normals)
- , m_posStride(_posStride)
- , m_normalStride(_normalStride)
- {
- static const float scale = 1.0f;
- static const float golden = 1.6180339887f;
- static const float len = bx::sqrt(golden*golden + 1.0f);
- static const float ss = 1.0f/len * scale;
- static const float ll = ss*golden;
- static const bx::Vec3 vv[] =
- {
- { -ll, 0.0f, -ss },
- { ll, 0.0f, -ss },
- { ll, 0.0f, ss },
- { -ll, 0.0f, ss },
- { -ss, ll, 0.0f },
- { ss, ll, 0.0f },
- { ss, -ll, 0.0f },
- { -ss, -ll, 0.0f },
- { 0.0f, -ss, ll },
- { 0.0f, ss, ll },
- { 0.0f, ss, -ll },
- { 0.0f, -ss, -ll },
- };
- m_numVertices = 0;
- triangle(vv[ 0], vv[ 4], vv[ 3], scale, _subdiv);
- triangle(vv[ 0], vv[10], vv[ 4], scale, _subdiv);
- triangle(vv[ 4], vv[10], vv[ 5], scale, _subdiv);
- triangle(vv[ 5], vv[10], vv[ 1], scale, _subdiv);
- triangle(vv[ 5], vv[ 1], vv[ 2], scale, _subdiv);
- triangle(vv[ 5], vv[ 2], vv[ 9], scale, _subdiv);
- triangle(vv[ 5], vv[ 9], vv[ 4], scale, _subdiv);
- triangle(vv[ 3], vv[ 4], vv[ 9], scale, _subdiv);
- triangle(vv[ 0], vv[ 3], vv[ 7], scale, _subdiv);
- triangle(vv[ 0], vv[ 7], vv[11], scale, _subdiv);
- triangle(vv[11], vv[ 7], vv[ 6], scale, _subdiv);
- triangle(vv[11], vv[ 6], vv[ 1], scale, _subdiv);
- triangle(vv[ 1], vv[ 6], vv[ 2], scale, _subdiv);
- triangle(vv[ 2], vv[ 6], vv[ 8], scale, _subdiv);
- triangle(vv[ 8], vv[ 6], vv[ 7], scale, _subdiv);
- triangle(vv[ 8], vv[ 7], vv[ 3], scale, _subdiv);
- triangle(vv[ 0], vv[11], vv[10], scale, _subdiv);
- triangle(vv[ 1], vv[10], vv[11], scale, _subdiv);
- triangle(vv[ 2], vv[ 8], vv[ 9], scale, _subdiv);
- triangle(vv[ 3], vv[ 9], vv[ 8], scale, _subdiv);
- }
- void addVert(const bx::Vec3& _v)
- {
- bx::store(m_pos, _v);
- m_pos += m_posStride;
- if (NULL != m_normals)
- {
- const bx::Vec3 normal = bx::normalize(_v);
- bx::store(m_normals, normal);
- m_normals += m_normalStride;
- }
- m_numVertices++;
- }
- void triangle(const bx::Vec3& _v0, const bx::Vec3& _v1, const bx::Vec3& _v2, float _scale, uint8_t _subdiv)
- {
- if (0 == _subdiv)
- {
- addVert(_v0);
- addVert(_v1);
- addVert(_v2);
- }
- else
- {
- const bx::Vec3 v01 = bx::mul(bx::normalize(bx::add(_v0, _v1) ), _scale);
- const bx::Vec3 v12 = bx::mul(bx::normalize(bx::add(_v1, _v2) ), _scale);
- const bx::Vec3 v20 = bx::mul(bx::normalize(bx::add(_v2, _v0) ), _scale);
- --_subdiv;
- triangle(_v0, v01, v20, _scale, _subdiv);
- triangle(_v1, v12, v01, _scale, _subdiv);
- triangle(_v2, v20, v12, _scale, _subdiv);
- triangle(v01, v12, v20, _scale, _subdiv);
- }
- }
- uint8_t* m_pos;
- uint8_t* m_normals;
- uint16_t m_posStride;
- uint16_t m_normalStride;
- uint32_t m_numVertices;
- } gen(_pos0, _posStride0, _normals0, _normalStride0, _subdiv0);
- }
- uint32_t numVertices = 20*3*bx::uint32_max(1, (uint32_t)bx::pow(4.0f, _subdiv0) );
- return numVertices;
- }
- bx::Vec3 getPoint(Axis::Enum _axis, float _x, float _y)
- {
- switch (_axis)
- {
- case Axis::X: return { 0.0f, _x, _y };
- case Axis::Y: return { _y, 0.0f, _x };
- default: break;
- }
- return { _x, _y, 0.0f };
- }
- #include "vs_debugdraw_lines.bin.h"
- #include "fs_debugdraw_lines.bin.h"
- #include "vs_debugdraw_lines_stipple.bin.h"
- #include "fs_debugdraw_lines_stipple.bin.h"
- #include "vs_debugdraw_fill.bin.h"
- #include "vs_debugdraw_fill_mesh.bin.h"
- #include "fs_debugdraw_fill.bin.h"
- #include "vs_debugdraw_fill_lit.bin.h"
- #include "vs_debugdraw_fill_lit_mesh.bin.h"
- #include "fs_debugdraw_fill_lit.bin.h"
- #include "vs_debugdraw_fill_texture.bin.h"
- #include "fs_debugdraw_fill_texture.bin.h"
- static const bgfx::EmbeddedShader s_embeddedShaders[] =
- {
- BGFX_EMBEDDED_SHADER(vs_debugdraw_lines),
- BGFX_EMBEDDED_SHADER(fs_debugdraw_lines),
- BGFX_EMBEDDED_SHADER(vs_debugdraw_lines_stipple),
- BGFX_EMBEDDED_SHADER(fs_debugdraw_lines_stipple),
- BGFX_EMBEDDED_SHADER(vs_debugdraw_fill),
- BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_mesh),
- BGFX_EMBEDDED_SHADER(fs_debugdraw_fill),
- BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit),
- BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_lit_mesh),
- BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_lit),
- BGFX_EMBEDDED_SHADER(vs_debugdraw_fill_texture),
- BGFX_EMBEDDED_SHADER(fs_debugdraw_fill_texture),
- BGFX_EMBEDDED_SHADER_END()
- };
- #define SPRITE_TEXTURE_SIZE 1024
- template<uint16_t MaxHandlesT = 256, uint16_t TextureSizeT = 1024>
- struct SpriteT
- {
- SpriteT()
- : m_ra(TextureSizeT, TextureSizeT)
- {
- }
- SpriteHandle create(uint16_t _width, uint16_t _height)
- {
- bx::MutexScope lock(m_lock);
- SpriteHandle handle = { bx::kInvalidHandle };
- if (m_handleAlloc.getNumHandles() < m_handleAlloc.getMaxHandles() )
- {
- Pack2D pack;
- if (m_ra.find(_width, _height, pack) )
- {
- handle.idx = m_handleAlloc.alloc();
- if (isValid(handle) )
- {
- m_pack[handle.idx] = pack;
- }
- else
- {
- m_ra.clear(pack);
- }
- }
- }
- return handle;
- }
- void destroy(SpriteHandle _sprite)
- {
- const Pack2D& pack = m_pack[_sprite.idx];
- m_ra.clear(pack);
- m_handleAlloc.free(_sprite.idx);
- }
- const Pack2D& get(SpriteHandle _sprite) const
- {
- return m_pack[_sprite.idx];
- }
- bx::Mutex m_lock;
- bx::HandleAllocT<MaxHandlesT> m_handleAlloc;
- Pack2D m_pack[MaxHandlesT];
- RectPack2DT<256> m_ra;
- };
- template<uint16_t MaxHandlesT = DEBUG_DRAW_CONFIG_MAX_GEOMETRY>
- struct GeometryT
- {
- GeometryT()
- {
- }
- GeometryHandle create(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32)
- {
- BX_UNUSED(_numVertices, _vertices, _numIndices, _indices, _index32);
- GeometryHandle handle;
- {
- bx::MutexScope lock(m_lock);
- handle = { m_handleAlloc.alloc() };
- }
- if (isValid(handle) )
- {
- Geometry& geometry = m_geometry[handle.idx];
- geometry.m_vbh = bgfx::createVertexBuffer(
- bgfx::copy(_vertices, _numVertices*sizeof(DdVertex) )
- , DebugMeshVertex::ms_layout
- );
- geometry.m_topologyNumIndices[0] = _numIndices;
- geometry.m_topologyNumIndices[1] = bgfx::topologyConvert(
- bgfx::TopologyConvert::TriListToLineList
- , NULL
- , 0
- , _indices
- , _numIndices
- , _index32
- );
- const uint32_t indexSize = _index32 ? sizeof(uint32_t) : sizeof(uint16_t);
- const uint32_t numIndices = 0
- + geometry.m_topologyNumIndices[0]
- + geometry.m_topologyNumIndices[1]
- ;
- const bgfx::Memory* mem = bgfx::alloc(numIndices*indexSize );
- uint8_t* indexData = mem->data;
- bx::memCopy(indexData, _indices, _numIndices*indexSize );
- bgfx::topologyConvert(
- bgfx::TopologyConvert::TriListToLineList
- , &indexData[geometry.m_topologyNumIndices[0]*indexSize ]
- , geometry.m_topologyNumIndices[1]*indexSize
- , _indices
- , _numIndices
- , _index32
- );
- geometry.m_ibh = bgfx::createIndexBuffer(
- mem
- , _index32 ? BGFX_BUFFER_INDEX32 : BGFX_BUFFER_NONE
- );
- }
- return handle;
- }
- void destroy(GeometryHandle _handle)
- {
- bx::MutexScope lock(m_lock);
- Geometry& geometry = m_geometry[_handle.idx];
- bgfx::destroy(geometry.m_vbh);
- bgfx::destroy(geometry.m_ibh);
- m_handleAlloc.free(_handle.idx);
- }
- struct Geometry
- {
- Geometry()
- {
- m_vbh.idx = bx::kInvalidHandle;
- m_ibh.idx = bx::kInvalidHandle;
- m_topologyNumIndices[0] = 0;
- m_topologyNumIndices[1] = 0;
- }
- bgfx::VertexBufferHandle m_vbh;
- bgfx::IndexBufferHandle m_ibh;
- uint32_t m_topologyNumIndices[2];
- };
- bx::Mutex m_lock;
- bx::HandleAllocT<MaxHandlesT> m_handleAlloc;
- Geometry m_geometry[MaxHandlesT];
- };
- struct Attrib
- {
- uint64_t m_state;
- float m_offset;
- float m_scale;
- float m_spin;
- uint32_t m_abgr;
- bool m_stipple;
- bool m_wireframe;
- uint8_t m_lod;
- };
- struct Program
- {
- enum Enum
- {
- Lines,
- LinesStipple,
- Fill,
- FillMesh,
- FillLit,
- FillLitMesh,
- FillTexture,
- Count
- };
- };
- struct DebugMesh
- {
- enum Enum
- {
- Sphere0,
- Sphere1,
- Sphere2,
- Sphere3,
- Cone0,
- Cone1,
- Cone2,
- Cone3,
- Cylinder0,
- Cylinder1,
- Cylinder2,
- Cylinder3,
- Capsule0,
- Capsule1,
- Capsule2,
- Capsule3,
- Quad,
- Cube,
- Count,
- SphereMaxLod = Sphere3 - Sphere0,
- ConeMaxLod = Cone3 - Cone0,
- CylinderMaxLod = Cylinder3 - Cylinder0,
- CapsuleMaxLod = Capsule3 - Capsule0,
- };
- uint32_t m_startVertex;
- uint32_t m_numVertices;
- uint32_t m_startIndex[2];
- uint32_t m_numIndices[2];
- };
- typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite;
- typedef GeometryT<DEBUG_DRAW_CONFIG_MAX_GEOMETRY> Geometry;
- struct DebugDrawShared
- {
- void init(bx::AllocatorI* _allocator)
- {
- if (NULL == _allocator)
- {
- static bx::DefaultAllocator allocator;
- m_allocator = &allocator;
- }
- else
- {
- m_allocator = _allocator;
- }
- DebugVertex::init();
- DebugUvVertex::init();
- DebugShapeVertex::init();
- DebugMeshVertex::init();
- bgfx::RendererType::Enum type = bgfx::getRendererType();
- m_program[Program::Lines] = bgfx::createProgram(
- bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_lines")
- , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_lines")
- , true
- );
- m_program[Program::LinesStipple] = bgfx::createProgram(
- bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_lines_stipple")
- , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_lines_stipple")
- , true
- );
- m_program[Program::Fill] = bgfx::createProgram(
- bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill")
- , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill")
- , true
- );
- m_program[Program::FillMesh] = bgfx::createProgram(
- bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_mesh")
- , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill")
- , true
- );
- m_program[Program::FillLit] = bgfx::createProgram(
- bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit")
- , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit")
- , true
- );
- m_program[Program::FillLitMesh] = bgfx::createProgram(
- bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_lit_mesh")
- , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_lit")
- , true
- );
- m_program[Program::FillTexture] = bgfx::createProgram(
- bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_debugdraw_fill_texture")
- , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_debugdraw_fill_texture")
- , true
- );
- u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 4);
- s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Sampler);
- m_texture = bgfx::createTexture2D(SPRITE_TEXTURE_SIZE, SPRITE_TEXTURE_SIZE, false, 1, bgfx::TextureFormat::BGRA8);
- void* vertices[DebugMesh::Count] = {};
- uint16_t* indices[DebugMesh::Count] = {};
- uint16_t stride = DebugShapeVertex::ms_layout.getStride();
- uint32_t startVertex = 0;
- uint32_t startIndex = 0;
- for (uint32_t mesh = 0; mesh < 4; ++mesh)
- {
- DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Sphere0+mesh);
- const uint8_t tess = uint8_t(3-mesh);
- const uint32_t numVertices = genSphere(tess);
- const uint32_t numIndices = numVertices;
- vertices[id] = bx::alloc(m_allocator, numVertices*stride);
- bx::memSet(vertices[id], 0, numVertices*stride);
- genSphere(tess, vertices[id], stride);
- uint16_t* trilist = (uint16_t*)bx::alloc(m_allocator, numIndices*sizeof(uint16_t) );
- for (uint32_t ii = 0; ii < numIndices; ++ii)
- {
- trilist[ii] = uint16_t(ii);
- }
- uint32_t numLineListIndices = bgfx::topologyConvert(
- bgfx::TopologyConvert::TriListToLineList
- , NULL
- , 0
- , trilist
- , numIndices
- , false
- );
- indices[id] = (uint16_t*)bx::alloc(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
- uint16_t* indicesOut = indices[id];
- bx::memCopy(indicesOut, trilist, numIndices*sizeof(uint16_t) );
- bgfx::topologyConvert(
- bgfx::TopologyConvert::TriListToLineList
- , &indicesOut[numIndices]
- , numLineListIndices*sizeof(uint16_t)
- , trilist
- , numIndices
- , false
- );
- m_mesh[id].m_startVertex = startVertex;
- m_mesh[id].m_numVertices = numVertices;
- m_mesh[id].m_startIndex[0] = startIndex;
- m_mesh[id].m_numIndices[0] = numIndices;
- m_mesh[id].m_startIndex[1] = startIndex+numIndices;
- m_mesh[id].m_numIndices[1] = numLineListIndices;
- startVertex += numVertices;
- startIndex += numIndices + numLineListIndices;
- bx::free(m_allocator, trilist);
- }
- for (uint32_t mesh = 0; mesh < 4; ++mesh)
- {
- DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Cone0+mesh);
- const uint32_t num = getCircleLod(uint8_t(mesh) );
- const float step = bx::kPi * 2.0f / num;
- const uint32_t numVertices = num+1;
- const uint32_t numIndices = num*6;
- const uint32_t numLineListIndices = num*4;
- vertices[id] = bx::alloc(m_allocator, numVertices*stride);
- indices[id] = (uint16_t*)bx::alloc(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
- bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
- DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
- uint16_t* index = indices[id];
- vertex[num].m_x = 0.0f;
- vertex[num].m_y = 0.0f;
- vertex[num].m_z = 0.0f;
- vertex[num].m_indices[0] = 1;
- for (uint32_t ii = 0; ii < num; ++ii)
- {
- const float angle = step * ii;
- float xy[2];
- circle(xy, angle);
- vertex[ii].m_x = xy[1];
- vertex[ii].m_y = 0.0f;
- vertex[ii].m_z = xy[0];
- vertex[ii].m_indices[0] = 0;
- index[ii*3+0] = uint16_t(num);
- index[ii*3+1] = uint16_t( (ii+1)%num);
- index[ii*3+2] = uint16_t(ii);
- index[num*3+ii*3+0] = 0;
- index[num*3+ii*3+1] = uint16_t(ii);
- index[num*3+ii*3+2] = uint16_t( (ii+1)%num);
- index[numIndices+ii*2+0] = uint16_t(ii);
- index[numIndices+ii*2+1] = uint16_t(num);
- index[numIndices+num*2+ii*2+0] = uint16_t(ii);
- index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
- }
- m_mesh[id].m_startVertex = startVertex;
- m_mesh[id].m_numVertices = numVertices;
- m_mesh[id].m_startIndex[0] = startIndex;
- m_mesh[id].m_numIndices[0] = numIndices;
- m_mesh[id].m_startIndex[1] = startIndex+numIndices;
- m_mesh[id].m_numIndices[1] = numLineListIndices;
- startVertex += numVertices;
- startIndex += numIndices + numLineListIndices;
- }
- for (uint32_t mesh = 0; mesh < 4; ++mesh)
- {
- DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Cylinder0+mesh);
- const uint32_t num = getCircleLod(uint8_t(mesh) );
- const float step = bx::kPi * 2.0f / num;
- const uint32_t numVertices = num*2;
- const uint32_t numIndices = num*12;
- const uint32_t numLineListIndices = num*6;
- vertices[id] = bx::alloc(m_allocator, numVertices*stride);
- indices[id] = (uint16_t*)bx::alloc(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
- bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
- DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
- uint16_t* index = indices[id];
- for (uint32_t ii = 0; ii < num; ++ii)
- {
- const float angle = step * ii;
- float xy[2];
- circle(xy, angle);
- vertex[ii].m_x = xy[1];
- vertex[ii].m_y = 0.0f;
- vertex[ii].m_z = xy[0];
- vertex[ii].m_indices[0] = 0;
- vertex[ii+num].m_x = xy[1];
- vertex[ii+num].m_y = 0.0f;
- vertex[ii+num].m_z = xy[0];
- vertex[ii+num].m_indices[0] = 1;
- index[ii*6+0] = uint16_t(ii+num);
- index[ii*6+1] = uint16_t( (ii+1)%num);
- index[ii*6+2] = uint16_t(ii);
- index[ii*6+3] = uint16_t(ii+num);
- index[ii*6+4] = uint16_t( (ii+1)%num+num);
- index[ii*6+5] = uint16_t( (ii+1)%num);
- index[num*6+ii*6+0] = uint16_t(0);
- index[num*6+ii*6+1] = uint16_t(ii);
- index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
- index[num*6+ii*6+3] = uint16_t(num);
- index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
- index[num*6+ii*6+5] = uint16_t(ii+num);
- index[numIndices+ii*2+0] = uint16_t(ii);
- index[numIndices+ii*2+1] = uint16_t(ii+num);
- index[numIndices+num*2+ii*2+0] = uint16_t(ii);
- index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
- index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
- index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
- }
- m_mesh[id].m_startVertex = startVertex;
- m_mesh[id].m_numVertices = numVertices;
- m_mesh[id].m_startIndex[0] = startIndex;
- m_mesh[id].m_numIndices[0] = numIndices;
- m_mesh[id].m_startIndex[1] = startIndex+numIndices;
- m_mesh[id].m_numIndices[1] = numLineListIndices;
- startVertex += numVertices;
- startIndex += numIndices + numLineListIndices;
- }
- for (uint32_t mesh = 0; mesh < 4; ++mesh)
- {
- DebugMesh::Enum id = DebugMesh::Enum(DebugMesh::Capsule0+mesh);
- const uint32_t num = getCircleLod(uint8_t(mesh) );
- const float step = bx::kPi * 2.0f / num;
- const uint32_t numVertices = num*2;
- const uint32_t numIndices = num*6;
- const uint32_t numLineListIndices = num*6;
- vertices[id] = bx::alloc(m_allocator, numVertices*stride);
- indices[id] = (uint16_t*)bx::alloc(m_allocator, (numIndices + numLineListIndices)*sizeof(uint16_t) );
- bx::memSet(indices[id], 0, (numIndices + numLineListIndices)*sizeof(uint16_t) );
- DebugShapeVertex* vertex = (DebugShapeVertex*)vertices[id];
- uint16_t* index = indices[id];
- for (uint32_t ii = 0; ii < num; ++ii)
- {
- const float angle = step * ii;
- float xy[2];
- circle(xy, angle);
- vertex[ii].m_x = xy[1];
- vertex[ii].m_y = 0.0f;
- vertex[ii].m_z = xy[0];
- vertex[ii].m_indices[0] = 0;
- vertex[ii+num].m_x = xy[1];
- vertex[ii+num].m_y = 0.0f;
- vertex[ii+num].m_z = xy[0];
- vertex[ii+num].m_indices[0] = 1;
- index[ii*6+0] = uint16_t(ii+num);
- index[ii*6+1] = uint16_t( (ii+1)%num);
- index[ii*6+2] = uint16_t(ii);
- index[ii*6+3] = uint16_t(ii+num);
- index[ii*6+4] = uint16_t( (ii+1)%num+num);
- index[ii*6+5] = uint16_t( (ii+1)%num);
- // index[num*6+ii*6+0] = uint16_t(0);
- // index[num*6+ii*6+1] = uint16_t(ii);
- // index[num*6+ii*6+2] = uint16_t( (ii+1)%num);
- // index[num*6+ii*6+3] = uint16_t(num);
- // index[num*6+ii*6+4] = uint16_t( (ii+1)%num+num);
- // index[num*6+ii*6+5] = uint16_t(ii+num);
- index[numIndices+ii*2+0] = uint16_t(ii);
- index[numIndices+ii*2+1] = uint16_t(ii+num);
- index[numIndices+num*2+ii*2+0] = uint16_t(ii);
- index[numIndices+num*2+ii*2+1] = uint16_t( (ii+1)%num);
- index[numIndices+num*4+ii*2+0] = uint16_t(num + ii);
- index[numIndices+num*4+ii*2+1] = uint16_t(num + (ii+1)%num);
- }
- m_mesh[id].m_startVertex = startVertex;
- m_mesh[id].m_numVertices = numVertices;
- m_mesh[id].m_startIndex[0] = startIndex;
- m_mesh[id].m_numIndices[0] = numIndices;
- m_mesh[id].m_startIndex[1] = startIndex+numIndices;
- m_mesh[id].m_numIndices[1] = numLineListIndices;
- startVertex += numVertices;
- startIndex += numIndices + numLineListIndices;
- }
- m_mesh[DebugMesh::Quad].m_startVertex = startVertex;
- m_mesh[DebugMesh::Quad].m_numVertices = BX_COUNTOF(s_quadVertices);
- m_mesh[DebugMesh::Quad].m_startIndex[0] = startIndex;
- m_mesh[DebugMesh::Quad].m_numIndices[0] = BX_COUNTOF(s_quadIndices);
- m_mesh[DebugMesh::Quad].m_startIndex[1] = 0;
- m_mesh[DebugMesh::Quad].m_numIndices[1] = 0;
- startVertex += BX_COUNTOF(s_quadVertices);
- startIndex += BX_COUNTOF(s_quadIndices);
- m_mesh[DebugMesh::Cube].m_startVertex = startVertex;
- m_mesh[DebugMesh::Cube].m_numVertices = BX_COUNTOF(s_cubeVertices);
- m_mesh[DebugMesh::Cube].m_startIndex[0] = startIndex;
- m_mesh[DebugMesh::Cube].m_numIndices[0] = BX_COUNTOF(s_cubeIndices);
- m_mesh[DebugMesh::Cube].m_startIndex[1] = 0;
- m_mesh[DebugMesh::Cube].m_numIndices[1] = 0;
- startVertex += m_mesh[DebugMesh::Cube].m_numVertices;
- startIndex += m_mesh[DebugMesh::Cube].m_numIndices[0];
- const bgfx::Memory* vb = bgfx::alloc(startVertex*stride);
- const bgfx::Memory* ib = bgfx::alloc(startIndex*sizeof(uint16_t) );
- for (uint32_t mesh = DebugMesh::Sphere0; mesh < DebugMesh::Quad; ++mesh)
- {
- DebugMesh::Enum id = DebugMesh::Enum(mesh);
- bx::memCopy(&vb->data[m_mesh[id].m_startVertex * stride]
- , vertices[id]
- , m_mesh[id].m_numVertices*stride
- );
- bx::memCopy(&ib->data[m_mesh[id].m_startIndex[0] * sizeof(uint16_t)]
- , indices[id]
- , (m_mesh[id].m_numIndices[0]+m_mesh[id].m_numIndices[1])*sizeof(uint16_t)
- );
- bx::free(m_allocator, vertices[id]);
- bx::free(m_allocator, indices[id]);
- }
- bx::memCopy(&vb->data[m_mesh[DebugMesh::Quad].m_startVertex * stride]
- , s_quadVertices
- , sizeof(s_quadVertices)
- );
- bx::memCopy(&ib->data[m_mesh[DebugMesh::Quad].m_startIndex[0] * sizeof(uint16_t)]
- , s_quadIndices
- , sizeof(s_quadIndices)
- );
- bx::memCopy(&vb->data[m_mesh[DebugMesh::Cube].m_startVertex * stride]
- , s_cubeVertices
- , sizeof(s_cubeVertices)
- );
- bx::memCopy(&ib->data[m_mesh[DebugMesh::Cube].m_startIndex[0] * sizeof(uint16_t)]
- , s_cubeIndices
- , sizeof(s_cubeIndices)
- );
- m_vbh = bgfx::createVertexBuffer(vb, DebugShapeVertex::ms_layout);
- m_ibh = bgfx::createIndexBuffer(ib);
- }
- void shutdown()
- {
- bgfx::destroy(m_ibh);
- bgfx::destroy(m_vbh);
- for (uint32_t ii = 0; ii < Program::Count; ++ii)
- {
- bgfx::destroy(m_program[ii]);
- }
- bgfx::destroy(u_params);
- bgfx::destroy(s_texColor);
- bgfx::destroy(m_texture);
- }
- SpriteHandle createSprite(uint16_t _width, uint16_t _height, const void* _data)
- {
- SpriteHandle handle = m_sprite.create(_width, _height);
- if (isValid(handle) )
- {
- const Pack2D& pack = m_sprite.get(handle);
- bgfx::updateTexture2D(
- m_texture
- , 0
- , 0
- , pack.m_x
- , pack.m_y
- , pack.m_width
- , pack.m_height
- , bgfx::copy(_data, pack.m_width*pack.m_height*4)
- );
- }
- return handle;
- }
- void destroy(SpriteHandle _handle)
- {
- m_sprite.destroy(_handle);
- }
- GeometryHandle createGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32)
- {
- return m_geometry.create(_numVertices, _vertices, _numIndices, _indices, _index32);
- }
- void destroy(GeometryHandle _handle)
- {
- m_geometry.destroy(_handle);
- }
- bx::AllocatorI* m_allocator;
- Sprite m_sprite;
- Geometry m_geometry;
- DebugMesh m_mesh[DebugMesh::Count];
- bgfx::UniformHandle s_texColor;
- bgfx::TextureHandle m_texture;
- bgfx::ProgramHandle m_program[Program::Count];
- bgfx::UniformHandle u_params;
- bgfx::VertexBufferHandle m_vbh;
- bgfx::IndexBufferHandle m_ibh;
- };
- static DebugDrawShared s_dds;
- struct DebugDrawEncoderImpl
- {
- DebugDrawEncoderImpl()
- : m_depthTestLess(true)
- , m_state(State::Count)
- , m_defaultEncoder(NULL)
- {
- }
- void init(bgfx::Encoder* _encoder)
- {
- m_defaultEncoder = _encoder;
- m_state = State::Count;
- }
- void shutdown()
- {
- }
- void begin(bgfx::ViewId _viewId, bool _depthTestLess, bgfx::Encoder* _encoder)
- {
- BX_ASSERT(State::Count == m_state, "");
- m_viewId = _viewId;
- m_encoder = _encoder == NULL ? m_defaultEncoder : _encoder;
- m_state = State::None;
- m_stack = 0;
- m_depthTestLess = _depthTestLess;
- m_pos = 0;
- m_indexPos = 0;
- m_vertexPos = 0;
- m_posQuad = 0;
- Attrib& attrib = m_attrib[0];
- attrib.m_state = 0
- | BGFX_STATE_WRITE_RGB
- | (m_depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER)
- | BGFX_STATE_CULL_CW
- | BGFX_STATE_WRITE_Z
- ;
- attrib.m_scale = 1.0f;
- attrib.m_spin = 0.0f;
- attrib.m_offset = 0.0f;
- attrib.m_abgr = UINT32_MAX;
- attrib.m_stipple = false;
- attrib.m_wireframe = false;
- attrib.m_lod = 0;
- m_mtxStackCurrent = 0;
- m_mtxStack[m_mtxStackCurrent].reset();
- }
- void end()
- {
- BX_ASSERT(0 == m_stack, "Invalid stack %d.", m_stack);
- flushQuad();
- flush();
- m_encoder = NULL;
- m_state = State::Count;
- }
- void push()
- {
- BX_ASSERT(State::Count != m_state, "");
- ++m_stack;
- m_attrib[m_stack] = m_attrib[m_stack-1];
- }
- void pop()
- {
- BX_ASSERT(State::Count != m_state, "");
- const Attrib& curr = m_attrib[m_stack];
- const Attrib& prev = m_attrib[m_stack-1];
- if (curr.m_stipple != prev.m_stipple
- || curr.m_state != prev.m_state)
- {
- flush();
- }
- --m_stack;
- }
- void setDepthTestLess(bool _depthTestLess)
- {
- BX_ASSERT(State::Count != m_state, "");
- if (m_depthTestLess != _depthTestLess)
- {
- m_depthTestLess = _depthTestLess;
- Attrib& attrib = m_attrib[m_stack];
- if (attrib.m_state & BGFX_STATE_DEPTH_TEST_MASK)
- {
- flush();
- attrib.m_state &= ~BGFX_STATE_DEPTH_TEST_MASK;
- attrib.m_state |= _depthTestLess ? BGFX_STATE_DEPTH_TEST_LESS : BGFX_STATE_DEPTH_TEST_GREATER;
- }
- }
- }
- void setTransform(const void* _mtx, uint16_t _num = 1, bool _flush = true)
- {
- BX_ASSERT(State::Count != m_state, "");
- if (_flush)
- {
- flush();
- }
- MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
- if (NULL == _mtx)
- {
- stack.reset();
- return;
- }
- bgfx::Transform transform;
- stack.mtx = m_encoder->allocTransform(&transform, _num);
- stack.num = _num;
- stack.data = transform.data;
- bx::memCopy(transform.data, _mtx, _num*64);
- }
- void setTranslate(float _x, float _y, float _z)
- {
- float mtx[16];
- bx::mtxTranslate(mtx, _x, _y, _z);
- setTransform(mtx);
- }
- void setTranslate(const float* _pos)
- {
- setTranslate(_pos[0], _pos[1], _pos[2]);
- }
- void pushTransform(const void* _mtx, uint16_t _num, bool _flush = true)
- {
- BX_ASSERT(m_mtxStackCurrent < BX_COUNTOF(m_mtxStack), "Out of matrix stack!");
- BX_ASSERT(State::Count != m_state, "");
- if (_flush)
- {
- flush();
- }
- float* mtx = NULL;
- const MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
- if (NULL == stack.data)
- {
- mtx = (float*)_mtx;
- }
- else
- {
- mtx = (float*)BX_STACK_ALLOC(_num*64);
- for (uint16_t ii = 0; ii < _num; ++ii)
- {
- const float* mtxTransform = (const float*)_mtx;
- bx::mtxMul(&mtx[ii*16], &mtxTransform[ii*16], stack.data);
- }
- }
- m_mtxStackCurrent++;
- setTransform(mtx, _num, _flush);
- }
- void popTransform(bool _flush = true)
- {
- BX_ASSERT(State::Count != m_state, "");
- if (_flush)
- {
- flush();
- }
- m_mtxStackCurrent--;
- }
- void pushTranslate(float _x, float _y, float _z)
- {
- float mtx[16];
- bx::mtxTranslate(mtx, _x, _y, _z);
- pushTransform(mtx, 1);
- }
- void pushTranslate(const bx::Vec3& _pos)
- {
- pushTranslate(_pos.x, _pos.y, _pos.z);
- }
- void setState(bool _depthTest, bool _depthWrite, bool _clockwise)
- {
- const uint64_t depthTest = m_depthTestLess
- ? BGFX_STATE_DEPTH_TEST_LESS
- : BGFX_STATE_DEPTH_TEST_GREATER
- ;
- uint64_t state = m_attrib[m_stack].m_state & ~(0
- | BGFX_STATE_DEPTH_TEST_MASK
- | BGFX_STATE_WRITE_Z
- | BGFX_STATE_CULL_CW
- | BGFX_STATE_CULL_CCW
- );
- state |= _depthTest
- ? depthTest
- : 0
- ;
- state |= _depthWrite
- ? BGFX_STATE_WRITE_Z
- : 0
- ;
- state |= _clockwise
- ? BGFX_STATE_CULL_CW
- : BGFX_STATE_CULL_CCW
- ;
- if (m_attrib[m_stack].m_state != state)
- {
- flush();
- }
- m_attrib[m_stack].m_state = state;
- }
- void setColor(uint32_t _abgr)
- {
- BX_ASSERT(State::Count != m_state, "");
- m_attrib[m_stack].m_abgr = _abgr;
- }
- void setLod(uint8_t _lod)
- {
- BX_ASSERT(State::Count != m_state, "");
- m_attrib[m_stack].m_lod = _lod;
- }
- void setWireframe(bool _wireframe)
- {
- BX_ASSERT(State::Count != m_state, "");
- m_attrib[m_stack].m_wireframe = _wireframe;
- }
- void setStipple(bool _stipple, float _scale = 1.0f, float _offset = 0.0f)
- {
- BX_ASSERT(State::Count != m_state, "");
- Attrib& attrib = m_attrib[m_stack];
- if (attrib.m_stipple != _stipple)
- {
- flush();
- }
- attrib.m_stipple = _stipple;
- attrib.m_offset = _offset;
- attrib.m_scale = _scale;
- }
- void setSpin(float _spin)
- {
- Attrib& attrib = m_attrib[m_stack];
- attrib.m_spin = _spin;
- }
- void moveTo(float _x, float _y, float _z = 0.0f)
- {
- BX_ASSERT(State::Count != m_state, "");
- softFlush();
- m_state = State::MoveTo;
- DebugVertex& vertex = m_cache[m_pos];
- vertex.m_x = _x;
- vertex.m_y = _y;
- vertex.m_z = _z;
- Attrib& attrib = m_attrib[m_stack];
- vertex.m_abgr = attrib.m_abgr;
- vertex.m_len = attrib.m_offset;
- m_vertexPos = m_pos;
- }
- void moveTo(const bx::Vec3& _pos)
- {
- BX_ASSERT(State::Count != m_state, "");
- moveTo(_pos.x, _pos.y, _pos.z);
- }
- void moveTo(Axis::Enum _axis, float _x, float _y)
- {
- moveTo(getPoint(_axis, _x, _y) );
- }
- void lineTo(float _x, float _y, float _z = 0.0f)
- {
- BX_ASSERT(State::Count != m_state, "");
- if (State::None == m_state)
- {
- moveTo(_x, _y, _z);
- return;
- }
- if (m_pos+2 > uint16_t(BX_COUNTOF(m_cache) ) )
- {
- uint32_t pos = m_pos;
- uint32_t vertexPos = m_vertexPos;
- flush();
- bx::memCopy(&m_cache[0], &m_cache[vertexPos], sizeof(DebugVertex) );
- if (vertexPos == pos)
- {
- m_pos = 1;
- }
- else
- {
- bx::memCopy(&m_cache[1], &m_cache[pos - 1], sizeof(DebugVertex) );
- m_pos = 2;
- }
- m_state = State::LineTo;
- }
- else if (State::MoveTo == m_state)
- {
- ++m_pos;
- m_state = State::LineTo;
- }
- uint16_t prev = m_pos-1;
- uint16_t curr = m_pos++;
- DebugVertex& vertex = m_cache[curr];
- vertex.m_x = _x;
- vertex.m_y = _y;
- vertex.m_z = _z;
- Attrib& attrib = m_attrib[m_stack];
- vertex.m_abgr = attrib.m_abgr;
- vertex.m_len = attrib.m_offset;
- 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;
- vertex.m_len = m_cache[prev].m_len + len;
- m_indices[m_indexPos++] = prev;
- m_indices[m_indexPos++] = curr;
- }
- void lineTo(const bx::Vec3& _pos)
- {
- BX_ASSERT(State::Count != m_state, "");
- lineTo(_pos.x, _pos.y, _pos.z);
- }
- void lineTo(Axis::Enum _axis, float _x, float _y)
- {
- lineTo(getPoint(_axis, _x, _y) );
- }
- void close()
- {
- BX_ASSERT(State::Count != m_state, "");
- DebugVertex& vertex = m_cache[m_vertexPos];
- lineTo(vertex.m_x, vertex.m_y, vertex.m_z);
- m_state = State::None;
- }
- void draw(const bx::Aabb& _aabb)
- {
- const Attrib& attrib = m_attrib[m_stack];
- if (attrib.m_wireframe)
- {
- moveTo(_aabb.min.x, _aabb.min.y, _aabb.min.z);
- lineTo(_aabb.max.x, _aabb.min.y, _aabb.min.z);
- lineTo(_aabb.max.x, _aabb.max.y, _aabb.min.z);
- lineTo(_aabb.min.x, _aabb.max.y, _aabb.min.z);
- close();
- moveTo(_aabb.min.x, _aabb.min.y, _aabb.max.z);
- lineTo(_aabb.max.x, _aabb.min.y, _aabb.max.z);
- lineTo(_aabb.max.x, _aabb.max.y, _aabb.max.z);
- lineTo(_aabb.min.x, _aabb.max.y, _aabb.max.z);
- close();
- moveTo(_aabb.min.x, _aabb.min.y, _aabb.min.z);
- lineTo(_aabb.min.x, _aabb.min.y, _aabb.max.z);
- moveTo(_aabb.max.x, _aabb.min.y, _aabb.min.z);
- lineTo(_aabb.max.x, _aabb.min.y, _aabb.max.z);
- moveTo(_aabb.min.x, _aabb.max.y, _aabb.min.z);
- lineTo(_aabb.min.x, _aabb.max.y, _aabb.max.z);
- moveTo(_aabb.max.x, _aabb.max.y, _aabb.min.z);
- lineTo(_aabb.max.x, _aabb.max.y, _aabb.max.z);
- }
- else
- {
- bx::Obb obb;
- toObb(obb, _aabb);
- draw(DebugMesh::Cube, obb.mtx, 1, false);
- }
- }
- void draw(const bx::Cylinder& _cylinder, bool _capsule)
- {
- drawCylinder(_cylinder.pos, _cylinder.end, _cylinder.radius, _capsule);
- }
- void draw(const bx::Disk& _disk)
- {
- drawCircle(_disk.normal, _disk.center, _disk.radius, 0.0f);
- }
- void draw(const bx::Obb& _obb)
- {
- const Attrib& attrib = m_attrib[m_stack];
- if (attrib.m_wireframe)
- {
- pushTransform(_obb.mtx, 1);
- moveTo(-1.0f, -1.0f, -1.0f);
- lineTo( 1.0f, -1.0f, -1.0f);
- lineTo( 1.0f, 1.0f, -1.0f);
- lineTo(-1.0f, 1.0f, -1.0f);
- close();
- moveTo(-1.0f, 1.0f, 1.0f);
- lineTo( 1.0f, 1.0f, 1.0f);
- lineTo( 1.0f, -1.0f, 1.0f);
- lineTo(-1.0f, -1.0f, 1.0f);
- close();
- moveTo( 1.0f, -1.0f, -1.0f);
- lineTo( 1.0f, -1.0f, 1.0f);
- moveTo( 1.0f, 1.0f, -1.0f);
- lineTo( 1.0f, 1.0f, 1.0f);
- moveTo(-1.0f, 1.0f, -1.0f);
- lineTo(-1.0f, 1.0f, 1.0f);
- moveTo(-1.0f, -1.0f, -1.0f);
- lineTo(-1.0f, -1.0f, 1.0f);
- popTransform();
- }
- else
- {
- draw(DebugMesh::Cube, _obb.mtx, 1, false);
- }
- }
- void draw(const bx::Sphere& _sphere)
- {
- const Attrib& attrib = m_attrib[m_stack];
- float mtx[16];
- bx::mtxSRT(mtx
- , _sphere.radius
- , _sphere.radius
- , _sphere.radius
- , 0.0f
- , 0.0f
- , 0.0f
- , _sphere.center.x
- , _sphere.center.y
- , _sphere.center.z
- );
- uint8_t lod = attrib.m_lod > DebugMesh::SphereMaxLod
- ? uint8_t(DebugMesh::SphereMaxLod)
- : attrib.m_lod
- ;
- draw(DebugMesh::Enum(DebugMesh::Sphere0 + lod), mtx, 1, attrib.m_wireframe);
- }
- void draw(const bx::Triangle& _triangle)
- {
- Attrib& attrib = m_attrib[m_stack];
- if (attrib.m_wireframe)
- {
- moveTo(_triangle.v0);
- lineTo(_triangle.v1);
- lineTo(_triangle.v2);
- close();
- }
- else
- {
- static_assert(sizeof(DdVertex) == sizeof(bx::Vec3), "");
- uint64_t old = attrib.m_state;
- attrib.m_state &= ~BGFX_STATE_CULL_MASK;
- draw(false, 3, reinterpret_cast<const DdVertex*>(&_triangle.v0.x), 0, NULL);
- attrib.m_state = old;
- }
- }
- void setUParams(const Attrib& _attrib, bool _wireframe)
- {
- const float flip = 0 == (_attrib.m_state & BGFX_STATE_CULL_CCW) ? 1.0f : -1.0f;
- const uint8_t alpha = _attrib.m_abgr >> 24;
- float params[4][4] =
- {
- { // lightDir
- 0.0f * flip,
- -1.0f * flip,
- 0.0f * flip,
- 3.0f, // shininess
- },
- { // skyColor
- 1.0f,
- 0.9f,
- 0.8f,
- 0.0f, // unused
- },
- { // groundColor.xyz0
- 0.2f,
- 0.22f,
- 0.5f,
- 0.0f, // unused
- },
- { // matColor
- ( (_attrib.m_abgr) & 0xff) / 255.0f,
- ( (_attrib.m_abgr >> 8) & 0xff) / 255.0f,
- ( (_attrib.m_abgr >> 16) & 0xff) / 255.0f,
- (alpha) / 255.0f,
- },
- };
- bx::store(params[0], bx::normalize(bx::load<bx::Vec3>(params[0]) ) );
- m_encoder->setUniform(s_dds.u_params, params, 4);
- m_encoder->setState(0
- | _attrib.m_state
- | (_wireframe ? BGFX_STATE_PT_LINES | BGFX_STATE_LINEAA | BGFX_STATE_BLEND_ALPHA
- : (alpha < 0xff) ? BGFX_STATE_BLEND_ALPHA : 0)
- );
- }
- void draw(GeometryHandle _handle)
- {
- const Geometry::Geometry& geometry = s_dds.m_geometry.m_geometry[_handle.idx];
- m_encoder->setVertexBuffer(0, geometry.m_vbh);
- const Attrib& attrib = m_attrib[m_stack];
- const bool wireframe = attrib.m_wireframe;
- setUParams(attrib, wireframe);
- if (wireframe)
- {
- m_encoder->setIndexBuffer(
- geometry.m_ibh
- , geometry.m_topologyNumIndices[0]
- , geometry.m_topologyNumIndices[1]
- );
- }
- else if (0 != geometry.m_topologyNumIndices[0])
- {
- m_encoder->setIndexBuffer(
- geometry.m_ibh
- , 0
- , geometry.m_topologyNumIndices[0]
- );
- }
- m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
- bgfx::ProgramHandle program = s_dds.m_program[wireframe ? Program::FillMesh : Program::FillLitMesh];
- m_encoder->submit(m_viewId, program);
- }
- void draw(bool _lineList, uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
- {
- flush();
- if (_numVertices == bgfx::getAvailTransientVertexBuffer(_numVertices, DebugMeshVertex::ms_layout) )
- {
- bgfx::TransientVertexBuffer tvb;
- bgfx::allocTransientVertexBuffer(&tvb, _numVertices, DebugMeshVertex::ms_layout);
- bx::memCopy(tvb.data, _vertices, _numVertices * DebugMeshVertex::ms_layout.m_stride);
- m_encoder->setVertexBuffer(0, &tvb);
- const Attrib& attrib = m_attrib[m_stack];
- const bool wireframe = _lineList || attrib.m_wireframe;
- setUParams(attrib, wireframe);
- if (0 < _numIndices)
- {
- uint32_t numIndices = _numIndices;
- bgfx::TransientIndexBuffer tib;
- if (!_lineList && wireframe)
- {
- numIndices = bgfx::topologyConvert(
- bgfx::TopologyConvert::TriListToLineList
- , NULL
- , 0
- , _indices
- , _numIndices
- , false
- );
- bgfx::allocTransientIndexBuffer(&tib, numIndices);
- bgfx::topologyConvert(
- bgfx::TopologyConvert::TriListToLineList
- , tib.data
- , numIndices * sizeof(uint16_t)
- , _indices
- , _numIndices
- , false
- );
- }
- else
- {
- bgfx::allocTransientIndexBuffer(&tib, numIndices);
- bx::memCopy(tib.data, _indices, numIndices * sizeof(uint16_t) );
- }
- m_encoder->setIndexBuffer(&tib);
- }
- m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
- bgfx::ProgramHandle program = s_dds.m_program[wireframe
- ? Program::FillMesh
- : Program::FillLitMesh
- ];
- m_encoder->submit(m_viewId, program);
- }
- }
- void drawFrustum(const float* _viewProj)
- {
- bx::Plane planes[6] = { bx::InitNone, bx::InitNone, bx::InitNone, bx::InitNone, bx::InitNone, bx::InitNone };
- buildFrustumPlanes(planes, _viewProj);
- const bx::Vec3 points[8] =
- {
- intersectPlanes(planes[0], planes[2], planes[4]),
- intersectPlanes(planes[0], planes[3], planes[4]),
- intersectPlanes(planes[0], planes[3], planes[5]),
- intersectPlanes(planes[0], planes[2], planes[5]),
- intersectPlanes(planes[1], planes[2], planes[4]),
- intersectPlanes(planes[1], planes[3], planes[4]),
- intersectPlanes(planes[1], planes[3], planes[5]),
- intersectPlanes(planes[1], planes[2], planes[5]),
- };
- moveTo(points[0]);
- lineTo(points[1]);
- lineTo(points[2]);
- lineTo(points[3]);
- close();
- moveTo(points[4]);
- lineTo(points[5]);
- lineTo(points[6]);
- lineTo(points[7]);
- close();
- moveTo(points[0]);
- lineTo(points[4]);
- moveTo(points[1]);
- lineTo(points[5]);
- moveTo(points[2]);
- lineTo(points[6]);
- moveTo(points[3]);
- lineTo(points[7]);
- }
- void drawFrustum(const void* _viewProj)
- {
- drawFrustum( (const float*)_viewProj);
- }
- void drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
- {
- const Attrib& attrib = m_attrib[m_stack];
- const uint32_t num = getCircleLod(attrib.m_lod);
- const float step = bx::kPi * 2.0f / num;
- _degrees = bx::wrap(_degrees, 360.0f);
- bx::Vec3 pos = getPoint(
- _axis
- , bx::sin(step * 0)*_radius
- , bx::cos(step * 0)*_radius
- );
- moveTo({pos.x + _x, pos.y + _y, pos.z + _z});
- uint32_t n = uint32_t(num*_degrees/360.0f);
- for (uint32_t ii = 1; ii < n+1; ++ii)
- {
- pos = getPoint(
- _axis
- , bx::sin(step * ii)*_radius
- , bx::cos(step * ii)*_radius
- );
- lineTo({pos.x + _x, pos.y + _y, pos.z + _z});
- }
- moveTo(_x, _y, _z);
- pos = getPoint(
- _axis
- , bx::sin(step * 0)*_radius
- , bx::cos(step * 0)*_radius
- );
- lineTo({pos.x + _x, pos.y + _y, pos.z + _z});
- pos = getPoint(
- _axis
- , bx::sin(step * n)*_radius
- , bx::cos(step * n)*_radius
- );
- moveTo({pos.x + _x, pos.y + _y, pos.z + _z});
- lineTo(_x, _y, _z);
- }
- void drawCircle(const bx::Vec3& _normal, const bx::Vec3& _center, float _radius, float _weight)
- {
- const Attrib& attrib = m_attrib[m_stack];
- const uint32_t num = getCircleLod(attrib.m_lod);
- const float step = bx::kPi * 2.0f / num;
- _weight = bx::clamp(_weight, 0.0f, 2.0f);
- bx::Vec3 udir(bx::InitNone);
- bx::Vec3 vdir(bx::InitNone);
- bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
- float xy0[2];
- float xy1[2];
- circle(xy0, 0.0f);
- squircle(xy1, 0.0f);
- bx::Vec3 pos = bx::mul(udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius);
- bx::Vec3 tmp0 = bx::mul(vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius);
- bx::Vec3 tmp1 = bx::add(pos, tmp0);
- bx::Vec3 tmp2 = bx::add(tmp1, _center);
- moveTo(tmp2);
- for (uint32_t ii = 1; ii < num; ++ii)
- {
- float angle = step * ii;
- circle(xy0, angle);
- squircle(xy1, angle);
- pos = bx::mul(udir, bx::lerp(xy0[0], xy1[0], _weight)*_radius);
- tmp0 = bx::mul(vdir, bx::lerp(xy0[1], xy1[1], _weight)*_radius);
- tmp1 = bx::add(pos, tmp0);
- tmp2 = bx::add(tmp1, _center);
- lineTo(tmp2);
- }
- close();
- }
- void drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
- {
- const Attrib& attrib = m_attrib[m_stack];
- const uint32_t num = getCircleLod(attrib.m_lod);
- const float step = bx::kPi * 2.0f / num;
- _weight = bx::clamp(_weight, 0.0f, 2.0f);
- float xy0[2];
- float xy1[2];
- circle(xy0, 0.0f);
- squircle(xy1, 0.0f);
- bx::Vec3 pos = getPoint(
- _axis
- , bx::lerp(xy0[0], xy1[0], _weight)*_radius
- , bx::lerp(xy0[1], xy1[1], _weight)*_radius
- );
- moveTo({pos.x + _x, pos.y + _y, pos.z + _z});
- for (uint32_t ii = 1; ii < num; ++ii)
- {
- float angle = step * ii;
- circle(xy0, angle);
- squircle(xy1, angle);
- pos = getPoint(
- _axis
- , bx::lerp(xy0[0], xy1[0], _weight)*_radius
- , bx::lerp(xy0[1], xy1[1], _weight)*_radius
- );
- lineTo({pos.x + _x, pos.y + _y, pos.z + _z});
- }
- close();
- }
- void drawQuad(const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
- {
- const Attrib& attrib = m_attrib[m_stack];
- if (attrib.m_wireframe)
- {
- bx::Vec3 udir(bx::InitNone);
- bx::Vec3 vdir(bx::InitNone);
- bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
- const float halfExtent = _size*0.5f;
- const bx::Vec3 umin = bx::mul(udir, -halfExtent);
- const bx::Vec3 umax = bx::mul(udir, halfExtent);
- const bx::Vec3 vmin = bx::mul(vdir, -halfExtent);
- const bx::Vec3 vmax = bx::mul(vdir, halfExtent);
- const bx::Vec3 center = _center;
- moveTo(bx::add(center, bx::add(umin, vmin) ) );
- lineTo(bx::add(center, bx::add(umax, vmin) ) );
- lineTo(bx::add(center, bx::add(umax, vmax) ) );
- lineTo(bx::add(center, bx::add(umin, vmax) ) );
- close();
- }
- else
- {
- float mtx[16];
- bx::mtxFromNormal(mtx, _normal, _size*0.5f, _center, attrib.m_spin);
- draw(DebugMesh::Quad, mtx, 1, false);
- }
- }
- void drawQuad(SpriteHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
- {
- if (!isValid(_handle) )
- {
- drawQuad(_normal, _center, _size);
- return;
- }
- if (m_posQuad == BX_COUNTOF(m_cacheQuad) )
- {
- flushQuad();
- }
- const Attrib& attrib = m_attrib[m_stack];
- bx::Vec3 udir(bx::InitNone);
- bx::Vec3 vdir(bx::InitNone);
- bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
- const Pack2D& pack = s_dds.m_sprite.get(_handle);
- const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE;
- const float us = pack.m_x * invTextureSize;
- const float vs = pack.m_y * invTextureSize;
- const float ue = (pack.m_x + pack.m_width ) * invTextureSize;
- const float ve = (pack.m_y + pack.m_height) * invTextureSize;
- const float aspectRatio = float(pack.m_width)/float(pack.m_height);
- const float halfExtentU = aspectRatio*_size*0.5f;
- const float halfExtentV = 1.0f/aspectRatio*_size*0.5f;
- const bx::Vec3 umin = bx::mul(udir, -halfExtentU);
- const bx::Vec3 umax = bx::mul(udir, halfExtentU);
- const bx::Vec3 vmin = bx::mul(vdir, -halfExtentV);
- const bx::Vec3 vmax = bx::mul(vdir, halfExtentV);
- const bx::Vec3 center = _center;
- DebugUvVertex* vertex = &m_cacheQuad[m_posQuad];
- m_posQuad += 4;
- bx::store(&vertex->m_x, bx::add(center, bx::add(umin, vmin) ) );
- vertex->m_u = us;
- vertex->m_v = vs;
- vertex->m_abgr = attrib.m_abgr;
- ++vertex;
- bx::store(&vertex->m_x, bx::add(center, bx::add(umax, vmin) ) );
- vertex->m_u = ue;
- vertex->m_v = vs;
- vertex->m_abgr = attrib.m_abgr;
- ++vertex;
- bx::store(&vertex->m_x, bx::add(center, bx::add(umin, vmax) ) );
- vertex->m_u = us;
- vertex->m_v = ve;
- vertex->m_abgr = attrib.m_abgr;
- ++vertex;
- bx::store(&vertex->m_x, bx::add(center, bx::add(umax, vmax) ) );
- vertex->m_u = ue;
- vertex->m_v = ve;
- vertex->m_abgr = attrib.m_abgr;
- ++vertex;
- }
- void drawQuad(bgfx::TextureHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
- {
- BX_UNUSED(_handle, _normal, _center, _size);
- }
- void drawCone(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
- {
- const Attrib& attrib = m_attrib[m_stack];
- const bx::Vec3 normal = bx::normalize(bx::sub(_from, _to) );
- float mtx[2][16];
- bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
- bx::memCopy(mtx[1], mtx[0], 64);
- mtx[1][12] = _to.x;
- mtx[1][13] = _to.y;
- mtx[1][14] = _to.z;
- uint8_t lod = attrib.m_lod > DebugMesh::ConeMaxLod
- ? uint8_t(DebugMesh::ConeMaxLod)
- : attrib.m_lod
- ;
- draw(DebugMesh::Enum(DebugMesh::Cone0 + lod), mtx[0], 2, attrib.m_wireframe);
- }
- void drawCylinder(const bx::Vec3& _from, const bx::Vec3& _to, float _radius, bool _capsule)
- {
- const Attrib& attrib = m_attrib[m_stack];
- const bx::Vec3 normal = bx::normalize(bx::sub(_from, _to) );
- float mtx[2][16];
- bx::mtxFromNormal(mtx[0], normal, _radius, _from, attrib.m_spin);
- bx::memCopy(mtx[1], mtx[0], 64);
- mtx[1][12] = _to.x;
- mtx[1][13] = _to.y;
- mtx[1][14] = _to.z;
- if (_capsule)
- {
- uint8_t lod = attrib.m_lod > DebugMesh::CapsuleMaxLod
- ? uint8_t(DebugMesh::CapsuleMaxLod)
- : attrib.m_lod
- ;
- draw(DebugMesh::Enum(DebugMesh::Capsule0 + lod), mtx[0], 2, attrib.m_wireframe);
- bx::Sphere sphere;
- sphere.center = _from;
- sphere.radius = _radius;
- draw(sphere);
- sphere.center = _to;
- draw(sphere);
- }
- else
- {
- uint8_t lod = attrib.m_lod > DebugMesh::CylinderMaxLod
- ? uint8_t(DebugMesh::CylinderMaxLod)
- : attrib.m_lod
- ;
- draw(DebugMesh::Enum(DebugMesh::Cylinder0 + lod), mtx[0], 2, attrib.m_wireframe);
- }
- }
- void drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
- {
- push();
- if (_thickness > 0.0f)
- {
- const bx::Vec3 from = { _x, _y, _z };
- bx::Vec3 mid(bx::InitNone);
- bx::Vec3 to(bx::InitNone);
- setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
- mid = { _x + _len - _thickness, _y, _z };
- to = { _x + _len, _y, _z };
- drawCylinder(from, mid, _thickness, false);
- drawCone(mid, to, _thickness);
- setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
- mid = { _x, _y + _len - _thickness, _z };
- to = { _x, _y + _len, _z };
- drawCylinder(from, mid, _thickness, false);
- drawCone(mid, to, _thickness);
- setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
- mid = { _x, _y, _z + _len - _thickness };
- to = { _x, _y, _z + _len };
- drawCylinder(from, mid, _thickness, false);
- drawCone(mid, to, _thickness);
- }
- else
- {
- setColor(Axis::X == _highlight ? 0xff00ffff : 0xff0000ff);
- moveTo(_x, _y, _z);
- lineTo(_x + _len, _y, _z);
- setColor(Axis::Y == _highlight ? 0xff00ffff : 0xff00ff00);
- moveTo(_x, _y, _z);
- lineTo(_x, _y + _len, _z);
- setColor(Axis::Z == _highlight ? 0xff00ffff : 0xffff0000);
- moveTo(_x, _y, _z);
- lineTo(_x, _y, _z + _len);
- }
- pop();
- }
- void drawGrid(const bx::Vec3& _normal, const bx::Vec3& _center, uint32_t _size, float _step)
- {
- const Attrib& attrib = m_attrib[m_stack];
- bx::Vec3 udir(bx::InitNone);
- bx::Vec3 vdir(bx::InitNone);
- bx::calcTangentFrame(udir, vdir, _normal, attrib.m_spin);
- udir = bx::mul(udir, _step);
- vdir = bx::mul(vdir, _step);
- const uint32_t num = (_size/2)*2+1;
- const float halfExtent = float(_size/2);
- const bx::Vec3 umin = bx::mul(udir, -halfExtent);
- const bx::Vec3 umax = bx::mul(udir, halfExtent);
- const bx::Vec3 vmin = bx::mul(vdir, -halfExtent);
- const bx::Vec3 vmax = bx::mul(vdir, halfExtent);
- bx::Vec3 xs = bx::add(_center, bx::add(umin, vmin) );
- bx::Vec3 xe = bx::add(_center, bx::add(umax, vmin) );
- bx::Vec3 ys = bx::add(_center, bx::add(umin, vmin) );
- bx::Vec3 ye = bx::add(_center, bx::add(umin, vmax) );
- for (uint32_t ii = 0; ii < num; ++ii)
- {
- moveTo(xs);
- lineTo(xe);
- xs = bx::add(xs, vdir);
- xe = bx::add(xe, vdir);
- moveTo(ys);
- lineTo(ye);
- ys = bx::add(ys, udir);
- ye = bx::add(ye, udir);
- }
- }
- void drawGrid(Axis::Enum _axis, const bx::Vec3& _center, uint32_t _size, float _step)
- {
- push();
- pushTranslate(_center);
- const uint32_t num = (_size/2)*2-1;
- const float halfExtent = float(_size/2) * _step;
- setColor(0xff606060);
- float yy = -halfExtent + _step;
- for (uint32_t ii = 0; ii < num; ++ii)
- {
- moveTo(_axis, -halfExtent, yy);
- lineTo(_axis, halfExtent, yy);
- moveTo(_axis, yy, -halfExtent);
- lineTo(_axis, yy, halfExtent);
- yy += _step;
- }
- setColor(0xff101010);
- moveTo(_axis, -halfExtent, -halfExtent);
- lineTo(_axis, -halfExtent, halfExtent);
- lineTo(_axis, halfExtent, halfExtent);
- lineTo(_axis, halfExtent, -halfExtent);
- close();
- moveTo(_axis, -halfExtent, 0.0f);
- lineTo(_axis, halfExtent, 0.0f);
- moveTo(_axis, 0.0f, -halfExtent);
- lineTo(_axis, 0.0f, halfExtent);
- popTransform();
- pop();
- }
- void drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _hightlight)
- {
- push();
- setColor(Axis::X == _hightlight ? 0xff00ffff : 0xff0000ff);
- drawCircle(Axis::X, _x, _y, _z, _radius, 0.0f);
- setColor(Axis::Y == _hightlight ? 0xff00ffff : 0xff00ff00);
- drawCircle(Axis::Y, _x, _y, _z, _radius, 0.0f);
- setColor(Axis::Z == _hightlight ? 0xff00ffff : 0xffff0000);
- drawCircle(Axis::Z, _x, _y, _z, _radius, 0.0f);
- pop();
- }
- void draw(DebugMesh::Enum _mesh, const float* _mtx, uint16_t _num, bool _wireframe)
- {
- pushTransform(_mtx, _num, false /* flush */);
- const DebugMesh& mesh = s_dds.m_mesh[_mesh];
- if (0 != mesh.m_numIndices[_wireframe])
- {
- m_encoder->setIndexBuffer(s_dds.m_ibh
- , mesh.m_startIndex[_wireframe]
- , mesh.m_numIndices[_wireframe]
- );
- }
- const Attrib& attrib = m_attrib[m_stack];
- setUParams(attrib, _wireframe);
- MatrixStack& stack = m_mtxStack[m_mtxStackCurrent];
- m_encoder->setTransform(stack.mtx, stack.num);
- m_encoder->setVertexBuffer(0, s_dds.m_vbh, mesh.m_startVertex, mesh.m_numVertices);
- m_encoder->submit(m_viewId, s_dds.m_program[_wireframe ? Program::Fill : Program::FillLit]);
- popTransform(false /* flush */);
- }
- void softFlush()
- {
- if (m_pos == uint16_t(BX_COUNTOF(m_cache) ) )
- {
- flush();
- }
- }
- void flush()
- {
- if (0 != m_pos)
- {
- if (checkAvailTransientBuffers(m_pos, DebugVertex::ms_layout, m_indexPos) )
- {
- bgfx::TransientVertexBuffer tvb;
- bgfx::allocTransientVertexBuffer(&tvb, m_pos, DebugVertex::ms_layout);
- bx::memCopy(tvb.data, m_cache, m_pos * DebugVertex::ms_layout.m_stride);
- bgfx::TransientIndexBuffer tib;
- bgfx::allocTransientIndexBuffer(&tib, m_indexPos);
- bx::memCopy(tib.data, m_indices, m_indexPos * sizeof(uint16_t) );
- const Attrib& attrib = m_attrib[m_stack];
- m_encoder->setVertexBuffer(0, &tvb);
- m_encoder->setIndexBuffer(&tib);
- m_encoder->setState(0
- | BGFX_STATE_WRITE_RGB
- | BGFX_STATE_PT_LINES
- | attrib.m_state
- | BGFX_STATE_LINEAA
- | BGFX_STATE_BLEND_ALPHA
- );
- m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
- bgfx::ProgramHandle program = s_dds.m_program[attrib.m_stipple ? 1 : 0];
- m_encoder->submit(m_viewId, program);
- }
- m_state = State::None;
- m_pos = 0;
- m_indexPos = 0;
- m_vertexPos = 0;
- }
- }
- void flushQuad()
- {
- if (0 != m_posQuad)
- {
- const uint32_t numIndices = m_posQuad/4*6;
- if (checkAvailTransientBuffers(m_posQuad, DebugUvVertex::ms_layout, numIndices) )
- {
- bgfx::TransientVertexBuffer tvb;
- bgfx::allocTransientVertexBuffer(&tvb, m_posQuad, DebugUvVertex::ms_layout);
- bx::memCopy(tvb.data, m_cacheQuad, m_posQuad * DebugUvVertex::ms_layout.m_stride);
- bgfx::TransientIndexBuffer tib;
- bgfx::allocTransientIndexBuffer(&tib, numIndices);
- uint16_t* indices = (uint16_t*)tib.data;
- for (uint16_t ii = 0, num = m_posQuad/4; ii < num; ++ii)
- {
- uint16_t startVertex = ii*4;
- indices[0] = startVertex+0;
- indices[1] = startVertex+1;
- indices[2] = startVertex+2;
- indices[3] = startVertex+1;
- indices[4] = startVertex+3;
- indices[5] = startVertex+2;
- indices += 6;
- }
- const Attrib& attrib = m_attrib[m_stack];
- m_encoder->setVertexBuffer(0, &tvb);
- m_encoder->setIndexBuffer(&tib);
- m_encoder->setState(0
- | (attrib.m_state & ~BGFX_STATE_CULL_MASK)
- );
- m_encoder->setTransform(m_mtxStack[m_mtxStackCurrent].mtx);
- m_encoder->setTexture(0, s_dds.s_texColor, s_dds.m_texture);
- m_encoder->submit(m_viewId, s_dds.m_program[Program::FillTexture]);
- }
- m_posQuad = 0;
- }
- }
- struct State
- {
- enum Enum
- {
- None,
- MoveTo,
- LineTo,
- Count
- };
- };
- static const uint32_t kCacheSize = 1024;
- static const uint32_t kStackSize = 16;
- static const uint32_t kCacheQuadSize = 1024;
- static_assert(kCacheSize >= 3, "Cache must be at least 3 elements.");
- DebugVertex m_cache[kCacheSize+1];
- DebugUvVertex m_cacheQuad[kCacheQuadSize];
- uint16_t m_indices[kCacheSize*2];
- uint16_t m_pos;
- uint16_t m_posQuad;
- uint16_t m_indexPos;
- uint16_t m_vertexPos;
- uint32_t m_mtxStackCurrent;
- struct MatrixStack
- {
- void reset()
- {
- mtx = 0;
- num = 1;
- data = NULL;
- }
- uint32_t mtx;
- uint16_t num;
- float* data;
- };
- MatrixStack m_mtxStack[32];
- bgfx::ViewId m_viewId;
- uint8_t m_stack;
- bool m_depthTestLess;
- Attrib m_attrib[kStackSize];
- State::Enum m_state;
- bgfx::Encoder* m_encoder;
- bgfx::Encoder* m_defaultEncoder;
- };
- static DebugDrawEncoderImpl s_dde;
- static_assert(sizeof(DebugDrawEncoderImpl) <= sizeof(DebugDrawEncoder), "Size must match");
- void ddInit(bx::AllocatorI* _allocator)
- {
- s_dds.init(_allocator);
- s_dde.init(bgfx::begin() );
- }
- void ddShutdown()
- {
- s_dde.shutdown();
- s_dds.shutdown();
- }
- SpriteHandle ddCreateSprite(uint16_t _width, uint16_t _height, const void* _data)
- {
- return s_dds.createSprite(_width, _height, _data);
- }
- void ddDestroy(SpriteHandle _handle)
- {
- s_dds.destroy(_handle);
- }
- GeometryHandle ddCreateGeometry(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const void* _indices, bool _index32)
- {
- return s_dds.createGeometry(_numVertices, _vertices, _numIndices, _indices, _index32);
- }
- void ddDestroy(GeometryHandle _handle)
- {
- s_dds.destroy(_handle);
- }
- #define DEBUG_DRAW_ENCODER(_func) reinterpret_cast<DebugDrawEncoderImpl*>(this)->_func
- DebugDrawEncoder::DebugDrawEncoder()
- {
- DEBUG_DRAW_ENCODER(init(s_dde.m_defaultEncoder) );
- }
- DebugDrawEncoder::~DebugDrawEncoder()
- {
- DEBUG_DRAW_ENCODER(shutdown() );
- }
- void DebugDrawEncoder::begin(uint16_t _viewId, bool _depthTestLess, bgfx::Encoder* _encoder)
- {
- DEBUG_DRAW_ENCODER(begin(_viewId, _depthTestLess, _encoder) );
- }
- void DebugDrawEncoder::end()
- {
- DEBUG_DRAW_ENCODER(end() );
- }
- void DebugDrawEncoder::push()
- {
- DEBUG_DRAW_ENCODER(push() );
- }
- void DebugDrawEncoder::pop()
- {
- DEBUG_DRAW_ENCODER(pop() );
- }
- void DebugDrawEncoder::setDepthTestLess(bool _depthTestLess)
- {
- DEBUG_DRAW_ENCODER(setDepthTestLess(_depthTestLess) );
- }
- void DebugDrawEncoder::setState(bool _depthTest, bool _depthWrite, bool _clockwise)
- {
- DEBUG_DRAW_ENCODER(setState(_depthTest, _depthWrite, _clockwise) );
- }
- void DebugDrawEncoder::setColor(uint32_t _abgr)
- {
- DEBUG_DRAW_ENCODER(setColor(_abgr) );
- }
- void DebugDrawEncoder::setLod(uint8_t _lod)
- {
- DEBUG_DRAW_ENCODER(setLod(_lod) );
- }
- void DebugDrawEncoder::setWireframe(bool _wireframe)
- {
- DEBUG_DRAW_ENCODER(setWireframe(_wireframe) );
- }
- void DebugDrawEncoder::setStipple(bool _stipple, float _scale, float _offset)
- {
- DEBUG_DRAW_ENCODER(setStipple(_stipple, _scale, _offset) );
- }
- void DebugDrawEncoder::setSpin(float _spin)
- {
- DEBUG_DRAW_ENCODER(setSpin(_spin) );
- }
- void DebugDrawEncoder::setTransform(const void* _mtx)
- {
- DEBUG_DRAW_ENCODER(setTransform(_mtx) );
- }
- void DebugDrawEncoder::setTranslate(float _x, float _y, float _z)
- {
- DEBUG_DRAW_ENCODER(setTranslate(_x, _y, _z) );
- }
- void DebugDrawEncoder::pushTransform(const void* _mtx)
- {
- DEBUG_DRAW_ENCODER(pushTransform(_mtx, 1) );
- }
- void DebugDrawEncoder::popTransform()
- {
- DEBUG_DRAW_ENCODER(popTransform() );
- }
- void DebugDrawEncoder::moveTo(float _x, float _y, float _z)
- {
- DEBUG_DRAW_ENCODER(moveTo(_x, _y, _z) );
- }
- void DebugDrawEncoder::moveTo(const bx::Vec3& _pos)
- {
- DEBUG_DRAW_ENCODER(moveTo(_pos) );
- }
- void DebugDrawEncoder::lineTo(float _x, float _y, float _z)
- {
- DEBUG_DRAW_ENCODER(lineTo(_x, _y, _z) );
- }
- void DebugDrawEncoder::lineTo(const bx::Vec3& _pos)
- {
- DEBUG_DRAW_ENCODER(lineTo(_pos) );
- }
- void DebugDrawEncoder::close()
- {
- DEBUG_DRAW_ENCODER(close() );
- }
- void DebugDrawEncoder::draw(const bx::Aabb& _aabb)
- {
- DEBUG_DRAW_ENCODER(draw(_aabb) );
- }
- void DebugDrawEncoder::draw(const bx::Cylinder& _cylinder)
- {
- DEBUG_DRAW_ENCODER(draw(_cylinder, false) );
- }
- void DebugDrawEncoder::draw(const bx::Capsule& _capsule)
- {
- DEBUG_DRAW_ENCODER(draw(*( (const bx::Cylinder*)&_capsule), true) );
- }
- void DebugDrawEncoder::draw(const bx::Disk& _disk)
- {
- DEBUG_DRAW_ENCODER(draw(_disk) );
- }
- void DebugDrawEncoder::draw(const bx::Obb& _obb)
- {
- DEBUG_DRAW_ENCODER(draw(_obb) );
- }
- void DebugDrawEncoder::draw(const bx::Sphere& _sphere)
- {
- DEBUG_DRAW_ENCODER(draw(_sphere) );
- }
- void DebugDrawEncoder::draw(const bx::Triangle& _triangle)
- {
- DEBUG_DRAW_ENCODER(draw(_triangle) );
- }
- void DebugDrawEncoder::draw(const bx::Cone& _cone)
- {
- DEBUG_DRAW_ENCODER(drawCone(_cone.pos, _cone.end, _cone.radius) );
- }
- void DebugDrawEncoder::draw(GeometryHandle _handle)
- {
- DEBUG_DRAW_ENCODER(draw(_handle) );
- }
- void DebugDrawEncoder::drawLineList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
- {
- DEBUG_DRAW_ENCODER(draw(true, _numVertices, _vertices, _numIndices, _indices) );
- }
- void DebugDrawEncoder::drawTriList(uint32_t _numVertices, const DdVertex* _vertices, uint32_t _numIndices, const uint16_t* _indices)
- {
- DEBUG_DRAW_ENCODER(draw(false, _numVertices, _vertices, _numIndices, _indices) );
- }
- void DebugDrawEncoder::drawFrustum(const void* _viewProj)
- {
- DEBUG_DRAW_ENCODER(drawFrustum(_viewProj) );
- }
- void DebugDrawEncoder::drawArc(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _degrees)
- {
- DEBUG_DRAW_ENCODER(drawArc(_axis, _x, _y, _z, _radius, _degrees) );
- }
- void DebugDrawEncoder::drawCircle(const bx::Vec3& _normal, const bx::Vec3& _center, float _radius, float _weight)
- {
- DEBUG_DRAW_ENCODER(drawCircle(_normal, _center, _radius, _weight) );
- }
- void DebugDrawEncoder::drawCircle(Axis::Enum _axis, float _x, float _y, float _z, float _radius, float _weight)
- {
- DEBUG_DRAW_ENCODER(drawCircle(_axis, _x, _y, _z, _radius, _weight) );
- }
- void DebugDrawEncoder::drawQuad(const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
- {
- DEBUG_DRAW_ENCODER(drawQuad(_normal, _center, _size) );
- }
- void DebugDrawEncoder::drawQuad(SpriteHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
- {
- DEBUG_DRAW_ENCODER(drawQuad(_handle, _normal, _center, _size) );
- }
- void DebugDrawEncoder::drawQuad(bgfx::TextureHandle _handle, const bx::Vec3& _normal, const bx::Vec3& _center, float _size)
- {
- DEBUG_DRAW_ENCODER(drawQuad(_handle, _normal, _center, _size) );
- }
- void DebugDrawEncoder::drawCone(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
- {
- DEBUG_DRAW_ENCODER(drawCone(_from, _to, _radius) );
- }
- void DebugDrawEncoder::drawCylinder(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
- {
- DEBUG_DRAW_ENCODER(drawCylinder(_from, _to, _radius, false) );
- }
- void DebugDrawEncoder::drawCapsule(const bx::Vec3& _from, const bx::Vec3& _to, float _radius)
- {
- DEBUG_DRAW_ENCODER(drawCylinder(_from, _to, _radius, true) );
- }
- void DebugDrawEncoder::drawAxis(float _x, float _y, float _z, float _len, Axis::Enum _highlight, float _thickness)
- {
- DEBUG_DRAW_ENCODER(drawAxis(_x, _y, _z, _len, _highlight, _thickness) );
- }
- void DebugDrawEncoder::drawGrid(const bx::Vec3& _normal, const bx::Vec3& _center, uint32_t _size, float _step)
- {
- DEBUG_DRAW_ENCODER(drawGrid(_normal, _center, _size, _step) );
- }
- void DebugDrawEncoder::drawGrid(Axis::Enum _axis, const bx::Vec3& _center, uint32_t _size, float _step)
- {
- DEBUG_DRAW_ENCODER(drawGrid(_axis, _center, _size, _step) );
- }
- void DebugDrawEncoder::drawOrb(float _x, float _y, float _z, float _radius, Axis::Enum _highlight)
- {
- DEBUG_DRAW_ENCODER(drawOrb(_x, _y, _z, _radius, _highlight) );
- }
- DebugDrawEncoderScopePush::DebugDrawEncoderScopePush(DebugDrawEncoder& _dde)
- : m_dde(_dde)
- {
- m_dde.push();
- }
- DebugDrawEncoderScopePush::~DebugDrawEncoderScopePush()
- {
- m_dde.pop();
- }
|