bgfx.cpp 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. namespace bgfx
  7. {
  8. #define BGFX_MAIN_THREAD_MAGIC UINT32_C(0x78666762)
  9. #if BGFX_CONFIG_MULTITHREADED
  10. # define BGFX_CHECK_MAIN_THREAD() \
  11. BX_CHECK(NULL != s_ctx, "Library is not initialized yet."); \
  12. BX_CHECK(BGFX_MAIN_THREAD_MAGIC == s_threadIndex, "Must be called from main thread.")
  13. # define BGFX_CHECK_RENDER_THREAD() BX_CHECK(BGFX_MAIN_THREAD_MAGIC != s_threadIndex, "Must be called from render thread.")
  14. #else
  15. # define BGFX_CHECK_MAIN_THREAD()
  16. # define BGFX_CHECK_RENDER_THREAD()
  17. #endif // BGFX_CONFIG_MULTITHREADED
  18. #if BGFX_CONFIG_USE_TINYSTL
  19. void* TinyStlAllocator::static_allocate(size_t _bytes)
  20. {
  21. return BX_ALLOC(g_allocator, _bytes);
  22. }
  23. void TinyStlAllocator::static_deallocate(void* _ptr, size_t /*_bytes*/)
  24. {
  25. if (NULL != _ptr)
  26. {
  27. BX_FREE(g_allocator, _ptr);
  28. }
  29. }
  30. #endif // BGFX_CONFIG_USE_TINYSTL
  31. struct CallbackStub : public CallbackI
  32. {
  33. virtual ~CallbackStub()
  34. {
  35. }
  36. virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) BX_OVERRIDE
  37. {
  38. dbgPrintf("%s (%d): ", _filePath, _line);
  39. dbgPrintfVargs(_format, _argList);
  40. }
  41. virtual void fatal(Fatal::Enum _code, const char* _str) BX_OVERRIDE
  42. {
  43. if (Fatal::DebugCheck == _code)
  44. {
  45. bx::debugBreak();
  46. }
  47. else
  48. {
  49. BX_TRACE("0x%08x: %s", _code, _str);
  50. BX_UNUSED(_code, _str);
  51. abort();
  52. }
  53. }
  54. virtual uint32_t cacheReadSize(uint64_t /*_id*/) BX_OVERRIDE
  55. {
  56. return 0;
  57. }
  58. virtual bool cacheRead(uint64_t /*_id*/, void* /*_data*/, uint32_t /*_size*/) BX_OVERRIDE
  59. {
  60. return false;
  61. }
  62. virtual void cacheWrite(uint64_t /*_id*/, const void* /*_data*/, uint32_t /*_size*/) BX_OVERRIDE
  63. {
  64. }
  65. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t _size, bool _yflip) BX_OVERRIDE
  66. {
  67. BX_UNUSED(_filePath, _width, _height, _pitch, _data, _size, _yflip);
  68. #if BX_CONFIG_CRT_FILE_READER_WRITER
  69. char* filePath = (char*)alloca(strlen(_filePath)+5);
  70. strcpy(filePath, _filePath);
  71. strcat(filePath, ".tga");
  72. bx::CrtFileWriter writer;
  73. if (0 == writer.open(filePath) )
  74. {
  75. imageWriteTga(&writer, _width, _height, _pitch, _data, false, _yflip);
  76. writer.close();
  77. }
  78. #endif // BX_CONFIG_CRT_FILE_READER_WRITER
  79. }
  80. virtual void captureBegin(uint32_t /*_width*/, uint32_t /*_height*/, uint32_t /*_pitch*/, TextureFormat::Enum /*_format*/, bool /*_yflip*/) BX_OVERRIDE
  81. {
  82. BX_TRACE("Warning: using capture without callback (a.k.a. pointless).");
  83. }
  84. virtual void captureEnd() BX_OVERRIDE
  85. {
  86. }
  87. virtual void captureFrame(const void* /*_data*/, uint32_t /*_size*/) BX_OVERRIDE
  88. {
  89. }
  90. };
  91. #ifndef BGFX_CONFIG_MEMORY_TRACKING
  92. # define BGFX_CONFIG_MEMORY_TRACKING (BGFX_CONFIG_DEBUG && BX_CONFIG_SUPPORTS_THREADING)
  93. #endif // BGFX_CONFIG_MEMORY_TRACKING
  94. class AllocatorStub : public bx::ReallocatorI
  95. {
  96. public:
  97. AllocatorStub()
  98. #if BGFX_CONFIG_MEMORY_TRACKING
  99. : m_numBlocks(0)
  100. , m_maxBlocks(0)
  101. #endif // BGFX_CONFIG_MEMORY_TRACKING
  102. {
  103. }
  104. virtual void* alloc(size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  105. {
  106. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  107. {
  108. #if BGFX_CONFIG_MEMORY_TRACKING
  109. {
  110. bx::LwMutexScope scope(m_mutex);
  111. ++m_numBlocks;
  112. m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
  113. }
  114. #endif // BGFX_CONFIG_MEMORY_TRACKING
  115. return ::malloc(_size);
  116. }
  117. return bx::alignedAlloc(this, _size, _align, _file, _line);
  118. }
  119. virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  120. {
  121. if (NULL != _ptr)
  122. {
  123. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  124. {
  125. #if BGFX_CONFIG_MEMORY_TRACKING
  126. {
  127. bx::LwMutexScope scope(m_mutex);
  128. BX_CHECK(m_numBlocks > 0, "Number of blocks is 0. Possible alloc/free mismatch?");
  129. --m_numBlocks;
  130. }
  131. #endif // BGFX_CONFIG_MEMORY_TRACKING
  132. ::free(_ptr);
  133. }
  134. else
  135. {
  136. bx::alignedFree(this, _ptr, _align, _file, _line);
  137. }
  138. }
  139. }
  140. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  141. {
  142. if (BX_CONFIG_ALLOCATOR_NATURAL_ALIGNMENT >= _align)
  143. {
  144. #if BGFX_CONFIG_MEMORY_TRACKING
  145. if (NULL == _ptr)
  146. {
  147. bx::LwMutexScope scope(m_mutex);
  148. ++m_numBlocks;
  149. m_maxBlocks = bx::uint32_max(m_maxBlocks, m_numBlocks);
  150. }
  151. #endif // BGFX_CONFIG_MEMORY_TRACKING
  152. return ::realloc(_ptr, _size);
  153. }
  154. return bx::alignedRealloc(this, _ptr, _size, _align, _file, _line);
  155. }
  156. void checkLeaks();
  157. protected:
  158. #if BGFX_CONFIG_MEMORY_TRACKING
  159. bx::LwMutex m_mutex;
  160. uint32_t m_numBlocks;
  161. uint32_t m_maxBlocks;
  162. #endif // BGFX_CONFIG_MEMORY_TRACKING
  163. };
  164. static CallbackStub* s_callbackStub = NULL;
  165. static AllocatorStub* s_allocatorStub = NULL;
  166. static bool s_graphicsDebuggerPresent = false;
  167. CallbackI* g_callback = NULL;
  168. bx::ReallocatorI* g_allocator = NULL;
  169. Caps g_caps;
  170. #if BGFX_CONFIG_MULTITHREADED && !defined(BX_THREAD_LOCAL)
  171. class ThreadData
  172. {
  173. BX_CLASS(ThreadData
  174. , NO_COPY
  175. , NO_ASSIGNMENT
  176. );
  177. public:
  178. ThreadData(uintptr_t _rhs)
  179. {
  180. union { uintptr_t ui; void* ptr; } cast = { _rhs };
  181. m_tls.set(cast.ptr);
  182. }
  183. operator uintptr_t() const
  184. {
  185. union { uintptr_t ui; void* ptr; } cast;
  186. cast.ptr = m_tls.get();
  187. return cast.ui;
  188. }
  189. uintptr_t operator=(uintptr_t _rhs)
  190. {
  191. union { uintptr_t ui; void* ptr; } cast = { _rhs };
  192. m_tls.set(cast.ptr);
  193. return _rhs;
  194. }
  195. bool operator==(uintptr_t _rhs) const
  196. {
  197. uintptr_t lhs = *this;
  198. return lhs == _rhs;
  199. }
  200. private:
  201. bx::TlsData m_tls;
  202. };
  203. static ThreadData s_threadIndex(0);
  204. #elif !BGFX_CONFIG_MULTITHREADED
  205. static uint32_t s_threadIndex(0);
  206. #else
  207. static BX_THREAD_LOCAL uint32_t s_threadIndex(0);
  208. #endif
  209. static Context* s_ctx = NULL;
  210. static bool s_renderFrameCalled = false;
  211. PlatformData g_platformData;
  212. void AllocatorStub::checkLeaks()
  213. {
  214. #if BGFX_CONFIG_MEMORY_TRACKING
  215. // BK - CallbackStub will be deleted after printing this info, so there is always one
  216. // leak if CallbackStub is used.
  217. BX_WARN(uint32_t(NULL != s_callbackStub ? 1 : 0) == m_numBlocks
  218. , "MEMORY LEAK: %d (max: %d)"
  219. , m_numBlocks
  220. , m_maxBlocks
  221. );
  222. #endif // BGFX_CONFIG_MEMORY_TRACKING
  223. }
  224. void setPlatformData(const PlatformData& _pd)
  225. {
  226. if (NULL != s_ctx)
  227. {
  228. BGFX_FATAL(true
  229. && g_platformData.ndt == _pd.ndt
  230. && g_platformData.nwh == _pd.nwh
  231. && g_platformData.context == _pd.context
  232. , Fatal::UnableToInitialize
  233. , "Only backbuffer pointer can be changed after initialization!"
  234. );
  235. }
  236. memcpy(&g_platformData, &_pd, sizeof(PlatformData) );
  237. }
  238. void setGraphicsDebuggerPresent(bool _present)
  239. {
  240. BX_TRACE("Graphics debugger is %spresent.", _present ? "" : "not ");
  241. s_graphicsDebuggerPresent = _present;
  242. }
  243. bool isGraphicsDebuggerPresent()
  244. {
  245. return s_graphicsDebuggerPresent;
  246. }
  247. void fatal(Fatal::Enum _code, const char* _format, ...)
  248. {
  249. char temp[8192];
  250. va_list argList;
  251. va_start(argList, _format);
  252. char* out = temp;
  253. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, argList);
  254. if ( (int32_t)sizeof(temp) < len)
  255. {
  256. out = (char*)alloca(len+1);
  257. len = bx::vsnprintf(out, len, _format, argList);
  258. }
  259. out[len] = '\0';
  260. va_end(argList);
  261. g_callback->fatal(_code, out);
  262. }
  263. void trace(const char* _filePath, uint16_t _line, const char* _format, ...)
  264. {
  265. va_list argList;
  266. va_start(argList, _format);
  267. g_callback->traceVargs(_filePath, _line, _format, argList);
  268. va_end(argList);
  269. }
  270. #include "charset.h"
  271. void charsetFillTexture(const uint8_t* _charset, uint8_t* _rgba, uint32_t _height, uint32_t _pitch, uint32_t _bpp)
  272. {
  273. for (uint32_t ii = 0; ii < 256; ++ii)
  274. {
  275. uint8_t* pix = &_rgba[ii*8*_bpp];
  276. for (uint32_t yy = 0; yy < _height; ++yy)
  277. {
  278. for (uint32_t xx = 0; xx < 8; ++xx)
  279. {
  280. uint8_t bit = 1<<(7-xx);
  281. memset(&pix[xx*_bpp], _charset[ii*_height+yy]&bit ? 255 : 0, _bpp);
  282. }
  283. pix += _pitch;
  284. }
  285. }
  286. }
  287. static const uint32_t numCharsPerBatch = 1024;
  288. static const uint32_t numBatchVertices = numCharsPerBatch*4;
  289. static const uint32_t numBatchIndices = numCharsPerBatch*6;
  290. void TextVideoMemBlitter::init()
  291. {
  292. BGFX_CHECK_MAIN_THREAD();
  293. m_decl
  294. .begin()
  295. .add(Attrib::Position, 3, AttribType::Float)
  296. .add(Attrib::Color0, 4, AttribType::Uint8, true)
  297. .add(Attrib::Color1, 4, AttribType::Uint8, true)
  298. .add(Attrib::TexCoord0, 2, AttribType::Float)
  299. .end();
  300. uint16_t width = 2048;
  301. uint16_t height = 24;
  302. uint8_t bpp = 1;
  303. uint32_t pitch = width*bpp;
  304. const Memory* mem;
  305. mem = alloc(pitch*height);
  306. uint8_t* rgba = mem->data;
  307. charsetFillTexture(vga8x8, rgba, 8, pitch, bpp);
  308. charsetFillTexture(vga8x16, &rgba[8*pitch], 16, pitch, bpp);
  309. m_texture = createTexture2D(width, height, 1, TextureFormat::R8
  310. , BGFX_TEXTURE_MIN_POINT
  311. | BGFX_TEXTURE_MAG_POINT
  312. | BGFX_TEXTURE_MIP_POINT
  313. | BGFX_TEXTURE_U_CLAMP
  314. | BGFX_TEXTURE_V_CLAMP
  315. , mem
  316. );
  317. switch (g_caps.rendererType)
  318. {
  319. case RendererType::Direct3D9:
  320. mem = makeRef(vs_debugfont_dx9, sizeof(vs_debugfont_dx9) );
  321. break;
  322. case RendererType::Direct3D11:
  323. case RendererType::Direct3D12:
  324. mem = makeRef(vs_debugfont_dx11, sizeof(vs_debugfont_dx11) );
  325. break;
  326. case RendererType::Metal:
  327. mem = makeRef(vs_debugfont_mtl, sizeof(vs_debugfont_mtl) );
  328. break;
  329. default:
  330. mem = makeRef(vs_debugfont_glsl, sizeof(vs_debugfont_glsl) );
  331. break;
  332. }
  333. ShaderHandle vsh = createShader(mem);
  334. switch (g_caps.rendererType)
  335. {
  336. case RendererType::Direct3D9:
  337. mem = makeRef(fs_debugfont_dx9, sizeof(fs_debugfont_dx9) );
  338. break;
  339. case RendererType::Direct3D11:
  340. case RendererType::Direct3D12:
  341. mem = makeRef(fs_debugfont_dx11, sizeof(fs_debugfont_dx11) );
  342. break;
  343. case RendererType::Metal:
  344. mem = makeRef(fs_debugfont_mtl, sizeof(fs_debugfont_mtl) );
  345. break;
  346. default:
  347. mem = makeRef(fs_debugfont_glsl, sizeof(fs_debugfont_glsl) );
  348. break;
  349. }
  350. ShaderHandle fsh = createShader(mem);
  351. m_program = createProgram(vsh, fsh, true);
  352. m_vb = s_ctx->createTransientVertexBuffer(numBatchVertices*m_decl.m_stride, &m_decl);
  353. m_ib = s_ctx->createTransientIndexBuffer(numBatchIndices*2);
  354. }
  355. void TextVideoMemBlitter::shutdown()
  356. {
  357. BGFX_CHECK_MAIN_THREAD();
  358. destroyProgram(m_program);
  359. destroyTexture(m_texture);
  360. s_ctx->destroyTransientVertexBuffer(m_vb);
  361. s_ctx->destroyTransientIndexBuffer(m_ib);
  362. }
  363. void blit(RendererContextI* _renderCtx, TextVideoMemBlitter& _blitter, const TextVideoMem& _mem)
  364. {
  365. struct Vertex
  366. {
  367. float m_x;
  368. float m_y;
  369. float m_z;
  370. uint32_t m_fg;
  371. uint32_t m_bg;
  372. float m_u;
  373. float m_v;
  374. };
  375. static uint32_t palette[16] =
  376. {
  377. 0x0,
  378. 0xff0000cc,
  379. 0xff069a4e,
  380. 0xff00a0c4,
  381. 0xffa46534,
  382. 0xff7b5075,
  383. 0xff9a9806,
  384. 0xffcfd7d3,
  385. 0xff535755,
  386. 0xff2929ef,
  387. 0xff34e28a,
  388. 0xff4fe9fc,
  389. 0xffcf9f72,
  390. 0xffa87fad,
  391. 0xffe2e234,
  392. 0xffeceeee,
  393. };
  394. uint32_t yy = 0;
  395. uint32_t xx = 0;
  396. const float texelWidth = 1.0f/2048.0f;
  397. const float texelWidthHalf = RendererType::Direct3D9 == g_caps.rendererType ? 0.0f : texelWidth*0.5f;
  398. const float texelHeight = 1.0f/24.0f;
  399. const float texelHeightHalf = RendererType::Direct3D9 == g_caps.rendererType ? texelHeight*0.5f : 0.0f;
  400. const float utop = (_mem.m_small ? 0.0f : 8.0f)*texelHeight + texelHeightHalf;
  401. const float ubottom = (_mem.m_small ? 8.0f : 24.0f)*texelHeight + texelHeightHalf;
  402. const float fontHeight = (_mem.m_small ? 8.0f : 16.0f);
  403. _renderCtx->blitSetup(_blitter);
  404. for (;yy < _mem.m_height;)
  405. {
  406. Vertex* vertex = (Vertex*)_blitter.m_vb->data;
  407. uint16_t* indices = (uint16_t*)_blitter.m_ib->data;
  408. uint32_t startVertex = 0;
  409. uint32_t numIndices = 0;
  410. for (; yy < _mem.m_height && numIndices < numBatchIndices; ++yy)
  411. {
  412. xx = xx < _mem.m_width ? xx : 0;
  413. const uint8_t* line = &_mem.m_mem[(yy*_mem.m_width+xx)*2];
  414. for (; xx < _mem.m_width && numIndices < numBatchIndices; ++xx)
  415. {
  416. uint8_t ch = line[0];
  417. uint8_t attr = line[1];
  418. if (0 != (ch|attr)
  419. && (' ' != ch || 0 != (attr&0xf0) ) )
  420. {
  421. uint32_t fg = palette[attr&0xf];
  422. uint32_t bg = palette[(attr>>4)&0xf];
  423. Vertex vert[4] =
  424. {
  425. { (xx )*8.0f, (yy )*fontHeight, 0.0f, fg, bg, (ch )*8.0f*texelWidth - texelWidthHalf, utop },
  426. { (xx+1)*8.0f, (yy )*fontHeight, 0.0f, fg, bg, (ch+1)*8.0f*texelWidth - texelWidthHalf, utop },
  427. { (xx+1)*8.0f, (yy+1)*fontHeight, 0.0f, fg, bg, (ch+1)*8.0f*texelWidth - texelWidthHalf, ubottom },
  428. { (xx )*8.0f, (yy+1)*fontHeight, 0.0f, fg, bg, (ch )*8.0f*texelWidth - texelWidthHalf, ubottom },
  429. };
  430. memcpy(vertex, vert, sizeof(vert) );
  431. vertex += 4;
  432. indices[0] = uint16_t(startVertex+0);
  433. indices[1] = uint16_t(startVertex+1);
  434. indices[2] = uint16_t(startVertex+2);
  435. indices[3] = uint16_t(startVertex+2);
  436. indices[4] = uint16_t(startVertex+3);
  437. indices[5] = uint16_t(startVertex+0);
  438. startVertex += 4;
  439. indices += 6;
  440. numIndices += 6;
  441. }
  442. line += 2;
  443. }
  444. if (numIndices >= numBatchIndices)
  445. {
  446. break;
  447. }
  448. }
  449. _renderCtx->blitRender(_blitter, numIndices);
  450. }
  451. }
  452. void ClearQuad::init()
  453. {
  454. BGFX_CHECK_MAIN_THREAD();
  455. if (RendererType::Null != g_caps.rendererType)
  456. {
  457. m_decl
  458. .begin()
  459. .add(Attrib::Position, 3, AttribType::Float)
  460. .end();
  461. ShaderHandle vsh = BGFX_INVALID_HANDLE;
  462. struct Mem
  463. {
  464. Mem(const void* _data, size_t _size)
  465. : data(_data)
  466. , size(_size)
  467. {
  468. }
  469. const void* data;
  470. size_t size;
  471. };
  472. const Memory* fragMem[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
  473. if (RendererType::Direct3D9 == g_caps.rendererType)
  474. {
  475. vsh = createShader(makeRef(vs_clear_dx9, sizeof(vs_clear_dx9) ) );
  476. const Mem mem[] =
  477. {
  478. Mem(fs_clear0_dx9, sizeof(fs_clear0_dx9) ),
  479. Mem(fs_clear1_dx9, sizeof(fs_clear1_dx9) ),
  480. Mem(fs_clear2_dx9, sizeof(fs_clear2_dx9) ),
  481. Mem(fs_clear3_dx9, sizeof(fs_clear3_dx9) ),
  482. Mem(fs_clear4_dx9, sizeof(fs_clear4_dx9) ),
  483. Mem(fs_clear5_dx9, sizeof(fs_clear5_dx9) ),
  484. Mem(fs_clear6_dx9, sizeof(fs_clear6_dx9) ),
  485. Mem(fs_clear7_dx9, sizeof(fs_clear7_dx9) ),
  486. };
  487. for (uint32_t ii = 0, num = g_caps.maxFBAttachments; ii < num; ++ii)
  488. {
  489. fragMem[ii] = makeRef(mem[ii].data, uint32_t(mem[ii].size) );
  490. }
  491. }
  492. else if (RendererType::Direct3D11 == g_caps.rendererType
  493. || RendererType::Direct3D12 == g_caps.rendererType)
  494. {
  495. vsh = createShader(makeRef(vs_clear_dx11, sizeof(vs_clear_dx11) ) );
  496. const Mem mem[] =
  497. {
  498. Mem(fs_clear0_dx11, sizeof(fs_clear0_dx11) ),
  499. Mem(fs_clear1_dx11, sizeof(fs_clear1_dx11) ),
  500. Mem(fs_clear2_dx11, sizeof(fs_clear2_dx11) ),
  501. Mem(fs_clear3_dx11, sizeof(fs_clear3_dx11) ),
  502. Mem(fs_clear4_dx11, sizeof(fs_clear4_dx11) ),
  503. Mem(fs_clear5_dx11, sizeof(fs_clear5_dx11) ),
  504. Mem(fs_clear6_dx11, sizeof(fs_clear6_dx11) ),
  505. Mem(fs_clear7_dx11, sizeof(fs_clear7_dx11) ),
  506. };
  507. for (uint32_t ii = 0, num = g_caps.maxFBAttachments; ii < num; ++ii)
  508. {
  509. fragMem[ii] = makeRef(mem[ii].data, uint32_t(mem[ii].size) );
  510. }
  511. }
  512. else if (RendererType::OpenGLES == g_caps.rendererType
  513. || RendererType::OpenGL == g_caps.rendererType)
  514. {
  515. vsh = createShader(makeRef(vs_clear_glsl, sizeof(vs_clear_glsl) ) );
  516. const Mem mem[] =
  517. {
  518. Mem(fs_clear0_glsl, sizeof(fs_clear0_glsl) ),
  519. Mem(fs_clear1_glsl, sizeof(fs_clear1_glsl) ),
  520. Mem(fs_clear2_glsl, sizeof(fs_clear2_glsl) ),
  521. Mem(fs_clear3_glsl, sizeof(fs_clear3_glsl) ),
  522. Mem(fs_clear4_glsl, sizeof(fs_clear4_glsl) ),
  523. Mem(fs_clear5_glsl, sizeof(fs_clear5_glsl) ),
  524. Mem(fs_clear6_glsl, sizeof(fs_clear6_glsl) ),
  525. Mem(fs_clear7_glsl, sizeof(fs_clear7_glsl) ),
  526. };
  527. for (uint32_t ii = 0, num = g_caps.maxFBAttachments; ii < num; ++ii)
  528. {
  529. fragMem[ii] = makeRef(mem[ii].data, uint32_t(mem[ii].size) );
  530. }
  531. }
  532. else if (RendererType::Metal == g_caps.rendererType)
  533. {
  534. vsh = createShader(makeRef(vs_clear_mtl, sizeof(vs_clear_mtl) ) );
  535. const Mem mem[] =
  536. {
  537. Mem(fs_clear0_mtl, sizeof(fs_clear0_mtl) ),
  538. Mem(fs_clear1_mtl, sizeof(fs_clear1_mtl) ),
  539. Mem(fs_clear2_mtl, sizeof(fs_clear2_mtl) ),
  540. Mem(fs_clear3_mtl, sizeof(fs_clear3_mtl) ),
  541. Mem(fs_clear4_mtl, sizeof(fs_clear4_mtl) ),
  542. Mem(fs_clear5_mtl, sizeof(fs_clear5_mtl) ),
  543. Mem(fs_clear6_mtl, sizeof(fs_clear6_mtl) ),
  544. Mem(fs_clear7_mtl, sizeof(fs_clear7_mtl) ),
  545. };
  546. for (uint32_t ii = 0, num = g_caps.maxFBAttachments; ii < num; ++ii)
  547. {
  548. fragMem[ii] = makeRef(mem[ii].data, uint32_t(mem[ii].size) );
  549. }
  550. }
  551. else
  552. {
  553. BGFX_FATAL(false, Fatal::UnableToInitialize, "Unknown renderer type %d", g_caps.rendererType);
  554. }
  555. for (uint32_t ii = 0, num = g_caps.maxFBAttachments; ii < num; ++ii)
  556. {
  557. ShaderHandle fsh = createShader(fragMem[ii]);
  558. m_program[ii] = createProgram(vsh, fsh);
  559. BX_CHECK(isValid(m_program[ii]), "Failed to create clear quad program.");
  560. destroyShader(fsh);
  561. }
  562. destroyShader(vsh);
  563. m_vb = s_ctx->createTransientVertexBuffer(4*m_decl.m_stride, &m_decl);
  564. }
  565. }
  566. void ClearQuad::shutdown()
  567. {
  568. BGFX_CHECK_MAIN_THREAD();
  569. if (RendererType::Null != g_caps.rendererType)
  570. {
  571. for (uint32_t ii = 0, num = g_caps.maxFBAttachments; ii < num; ++ii)
  572. {
  573. if (isValid(m_program[ii]) )
  574. {
  575. destroyProgram(m_program[ii]);
  576. m_program[ii].idx = invalidHandle;
  577. }
  578. }
  579. s_ctx->destroyTransientVertexBuffer(m_vb);
  580. }
  581. }
  582. const char* s_uniformTypeName[] =
  583. {
  584. "int1",
  585. NULL,
  586. "vec4",
  587. "mat3",
  588. "mat4",
  589. };
  590. BX_STATIC_ASSERT(UniformType::Count == BX_COUNTOF(s_uniformTypeName) );
  591. const char* getUniformTypeName(UniformType::Enum _enum)
  592. {
  593. BX_CHECK(_enum < UniformType::Count, "%d < UniformType::Count %d", _enum, UniformType::Count);
  594. return s_uniformTypeName[_enum];
  595. }
  596. UniformType::Enum nameToUniformTypeEnum(const char* _name)
  597. {
  598. for (uint32_t ii = 0; ii < UniformType::Count; ++ii)
  599. {
  600. if (NULL != s_uniformTypeName[ii]
  601. && 0 == strcmp(_name, s_uniformTypeName[ii]) )
  602. {
  603. return UniformType::Enum(ii);
  604. }
  605. }
  606. return UniformType::Count;
  607. }
  608. static const char* s_predefinedName[PredefinedUniform::Count] =
  609. {
  610. "u_viewRect",
  611. "u_viewTexel",
  612. "u_view",
  613. "u_invView",
  614. "u_proj",
  615. "u_invProj",
  616. "u_viewProj",
  617. "u_invViewProj",
  618. "u_model",
  619. "u_modelView",
  620. "u_modelViewProj",
  621. "u_alphaRef4",
  622. };
  623. const char* getPredefinedUniformName(PredefinedUniform::Enum _enum)
  624. {
  625. return s_predefinedName[_enum];
  626. }
  627. PredefinedUniform::Enum nameToPredefinedUniformEnum(const char* _name)
  628. {
  629. for (uint32_t ii = 0; ii < PredefinedUniform::Count; ++ii)
  630. {
  631. if (0 == strcmp(_name, s_predefinedName[ii]) )
  632. {
  633. return PredefinedUniform::Enum(ii);
  634. }
  635. }
  636. return PredefinedUniform::Count;
  637. }
  638. uint32_t Frame::submit(uint8_t _id, ProgramHandle _handle, int32_t _depth)
  639. {
  640. if (m_discard)
  641. {
  642. discard();
  643. return m_num;
  644. }
  645. if (BGFX_CONFIG_MAX_DRAW_CALLS-1 <= m_num
  646. || (0 == m_draw.m_numVertices && 0 == m_draw.m_numIndices) )
  647. {
  648. ++m_numDropped;
  649. return m_num;
  650. }
  651. m_uniformEnd = m_uniformBuffer->getPos();
  652. m_key.m_program = invalidHandle == _handle.idx
  653. ? 0
  654. : _handle.idx
  655. ;
  656. m_key.m_depth = (uint32_t)_depth;
  657. m_key.m_view = _id;
  658. m_key.m_seq = s_ctx->m_seq[_id] & s_ctx->m_seqMask[_id];
  659. s_ctx->m_seq[_id]++;
  660. uint64_t key = m_key.encodeDraw();
  661. m_sortKeys[m_num] = key;
  662. m_sortValues[m_num] = m_numRenderItems;
  663. ++m_num;
  664. m_draw.m_constBegin = m_uniformBegin;
  665. m_draw.m_constEnd = m_uniformEnd;
  666. m_draw.m_flags |= m_flags;
  667. m_renderItem[m_numRenderItems].draw = m_draw;
  668. ++m_numRenderItems;
  669. m_draw.clear();
  670. m_uniformBegin = m_uniformEnd;
  671. m_flags = BGFX_STATE_NONE;
  672. return m_num;
  673. }
  674. uint32_t Frame::dispatch(uint8_t _id, ProgramHandle _handle, uint16_t _numX, uint16_t _numY, uint16_t _numZ, uint8_t _flags)
  675. {
  676. if (m_discard)
  677. {
  678. discard();
  679. return m_num;
  680. }
  681. if (BGFX_CONFIG_MAX_DRAW_CALLS-1 <= m_num)
  682. {
  683. ++m_numDropped;
  684. return m_num;
  685. }
  686. m_uniformEnd = m_uniformBuffer->getPos();
  687. m_compute.m_matrix = m_draw.m_matrix;
  688. m_compute.m_num = m_draw.m_num;
  689. m_compute.m_numX = bx::uint16_max(_numX, 1);
  690. m_compute.m_numY = bx::uint16_max(_numY, 1);
  691. m_compute.m_numZ = bx::uint16_max(_numZ, 1);
  692. m_compute.m_submitFlags = _flags;
  693. m_key.m_program = _handle.idx;
  694. m_key.m_depth = 0;
  695. m_key.m_view = _id;
  696. m_key.m_seq = s_ctx->m_seq[_id];
  697. s_ctx->m_seq[_id]++;
  698. uint64_t key = m_key.encodeCompute();
  699. m_sortKeys[m_num] = key;
  700. m_sortValues[m_num] = m_numRenderItems;
  701. ++m_num;
  702. m_compute.m_constBegin = m_uniformBegin;
  703. m_compute.m_constEnd = m_uniformEnd;
  704. m_renderItem[m_numRenderItems].compute = m_compute;
  705. ++m_numRenderItems;
  706. m_compute.clear();
  707. m_uniformBegin = m_uniformEnd;
  708. return m_num;
  709. }
  710. void Frame::sort()
  711. {
  712. for (uint32_t ii = 0, num = m_num; ii < num; ++ii)
  713. {
  714. m_sortKeys[ii] = SortKey::remapView(m_sortKeys[ii], m_viewRemap);
  715. }
  716. bx::radixSort64(m_sortKeys, s_ctx->m_tempKeys, m_sortValues, s_ctx->m_tempValues, m_num);
  717. }
  718. RenderFrame::Enum renderFrame()
  719. {
  720. if (BX_ENABLED(BGFX_CONFIG_MULTITHREADED) )
  721. {
  722. if (NULL == s_ctx)
  723. {
  724. s_renderFrameCalled = true;
  725. s_threadIndex = ~BGFX_MAIN_THREAD_MAGIC;
  726. return RenderFrame::NoContext;
  727. }
  728. BGFX_CHECK_RENDER_THREAD();
  729. if (s_ctx->renderFrame() )
  730. {
  731. Context* ctx = s_ctx;
  732. ctx->gameSemWait();
  733. s_ctx = NULL;
  734. ctx->renderSemPost();
  735. return RenderFrame::Exiting;
  736. }
  737. return RenderFrame::Render;
  738. }
  739. BX_CHECK(false, "This call only makes sense if used with multi-threaded renderer.");
  740. return RenderFrame::NoContext;
  741. }
  742. const uint32_t g_uniformTypeSize[UniformType::Count+1] =
  743. {
  744. sizeof(int32_t),
  745. 0,
  746. 4*sizeof(float),
  747. 3*3*sizeof(float),
  748. 4*4*sizeof(float),
  749. 1,
  750. };
  751. void UniformBuffer::writeUniform(UniformType::Enum _type, uint16_t _loc, const void* _value, uint16_t _num)
  752. {
  753. uint32_t opcode = encodeOpcode(_type, _loc, _num, true);
  754. write(opcode);
  755. write(_value, g_uniformTypeSize[_type]*_num);
  756. }
  757. void UniformBuffer::writeUniformHandle(UniformType::Enum _type, uint16_t _loc, UniformHandle _handle, uint16_t _num)
  758. {
  759. uint32_t opcode = encodeOpcode(_type, _loc, _num, false);
  760. write(opcode);
  761. write(&_handle, sizeof(UniformHandle) );
  762. }
  763. void UniformBuffer::writeMarker(const char* _marker)
  764. {
  765. uint16_t num = (uint16_t)strlen(_marker)+1;
  766. uint32_t opcode = encodeOpcode(bgfx::UniformType::Count, 0, num, true);
  767. write(opcode);
  768. write(_marker, num);
  769. }
  770. struct CapsFlags
  771. {
  772. uint64_t m_flag;
  773. const char* m_str;
  774. };
  775. static const CapsFlags s_capsFlags[] =
  776. {
  777. #define CAPS_FLAGS(_x) { _x, #_x }
  778. CAPS_FLAGS(BGFX_CAPS_TEXTURE_COMPARE_LEQUAL),
  779. CAPS_FLAGS(BGFX_CAPS_TEXTURE_COMPARE_ALL),
  780. CAPS_FLAGS(BGFX_CAPS_TEXTURE_3D),
  781. CAPS_FLAGS(BGFX_CAPS_VERTEX_ATTRIB_HALF),
  782. CAPS_FLAGS(BGFX_CAPS_VERTEX_ATTRIB_UINT10),
  783. CAPS_FLAGS(BGFX_CAPS_INSTANCING),
  784. CAPS_FLAGS(BGFX_CAPS_RENDERER_MULTITHREADED),
  785. CAPS_FLAGS(BGFX_CAPS_FRAGMENT_DEPTH),
  786. CAPS_FLAGS(BGFX_CAPS_BLEND_INDEPENDENT),
  787. CAPS_FLAGS(BGFX_CAPS_COMPUTE),
  788. CAPS_FLAGS(BGFX_CAPS_FRAGMENT_ORDERING),
  789. CAPS_FLAGS(BGFX_CAPS_SWAP_CHAIN),
  790. CAPS_FLAGS(BGFX_CAPS_HMD),
  791. CAPS_FLAGS(BGFX_CAPS_INDEX32),
  792. CAPS_FLAGS(BGFX_CAPS_DRAW_INDIRECT),
  793. CAPS_FLAGS(BGFX_CAPS_HIDPI),
  794. #undef CAPS_FLAGS
  795. };
  796. static void dumpCaps()
  797. {
  798. BX_TRACE("Supported capabilities (renderer %s, vendor 0x%04x, device 0x%04x):"
  799. , s_ctx->m_renderCtx->getRendererName()
  800. , g_caps.vendorId
  801. , g_caps.deviceId
  802. );
  803. for (uint32_t ii = 0; ii < BX_COUNTOF(s_capsFlags); ++ii)
  804. {
  805. if (0 != (g_caps.supported & s_capsFlags[ii].m_flag) )
  806. {
  807. BX_TRACE("\t%s", s_capsFlags[ii].m_str);
  808. }
  809. }
  810. BX_TRACE("Supported texture formats:");
  811. BX_TRACE("\t +----------- x = supported / * = emulated");
  812. BX_TRACE("\t |+---------- sRGB format");
  813. BX_TRACE("\t ||+--------- vertex format");
  814. BX_TRACE("\t |||+-------- image");
  815. BX_TRACE("\t ||||+------- framebuffer");
  816. BX_TRACE("\t |||||+------ MSAA framebuffer");
  817. BX_TRACE("\t ||||||+----- MSAA texture");
  818. BX_TRACE("\t ||||||| +-- name");
  819. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  820. {
  821. if (TextureFormat::Unknown != ii
  822. && TextureFormat::UnknownDepth != ii)
  823. {
  824. uint8_t flags = g_caps.formats[ii];
  825. BX_TRACE("\t[%c%c%c%c%c%c%c] %s"
  826. , flags&BGFX_CAPS_FORMAT_TEXTURE_COLOR ? 'x' : flags&BGFX_CAPS_FORMAT_TEXTURE_EMULATED ? '*' : ' '
  827. , flags&BGFX_CAPS_FORMAT_TEXTURE_COLOR_SRGB ? 'l' : ' '
  828. , flags&BGFX_CAPS_FORMAT_TEXTURE_VERTEX ? 'v' : ' '
  829. , flags&BGFX_CAPS_FORMAT_TEXTURE_IMAGE ? 'i' : ' '
  830. , flags&BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER ? 'f' : ' '
  831. , flags&BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA ? '+' : ' '
  832. , flags&BGFX_CAPS_FORMAT_TEXTURE_MSAA ? 'm' : ' '
  833. , getName(TextureFormat::Enum(ii) )
  834. );
  835. BX_UNUSED(flags);
  836. }
  837. }
  838. BX_TRACE("Max FB attachments: %d", g_caps.maxFBAttachments);
  839. }
  840. static TextureFormat::Enum s_emulatedFormats[] =
  841. {
  842. TextureFormat::BC1,
  843. TextureFormat::BC2,
  844. TextureFormat::BC3,
  845. TextureFormat::BC4,
  846. TextureFormat::BC5,
  847. TextureFormat::ETC1,
  848. TextureFormat::ETC2,
  849. TextureFormat::ETC2A,
  850. TextureFormat::ETC2A1,
  851. TextureFormat::PTC14,
  852. TextureFormat::PTC14A,
  853. TextureFormat::BGRA8, // GL doesn't support BGRA8 without extensions.
  854. TextureFormat::RGBA8, // D3D9 doesn't support RGBA8
  855. };
  856. bool Context::init(RendererType::Enum _type)
  857. {
  858. BX_CHECK(!m_rendererInitialized, "Already initialized?");
  859. m_exit = false;
  860. m_frames = 0;
  861. m_debug = BGFX_DEBUG_NONE;
  862. m_submit->create();
  863. #if BGFX_CONFIG_MULTITHREADED
  864. m_render->create();
  865. if (s_renderFrameCalled)
  866. {
  867. // When bgfx::renderFrame is called before init render thread
  868. // should not be created.
  869. BX_TRACE("Application called bgfx::renderFrame directly, not creating render thread.");
  870. m_singleThreaded = true
  871. && ~BGFX_MAIN_THREAD_MAGIC == s_threadIndex
  872. ;
  873. }
  874. else
  875. {
  876. BX_TRACE("Creating rendering thread.");
  877. m_thread.init(renderThread, this, 0, "bgfx - renderer backend thread");
  878. m_singleThreaded = false;
  879. }
  880. #else
  881. BX_TRACE("Multithreaded renderer is disabled.");
  882. m_singleThreaded = true;
  883. #endif // BGFX_CONFIG_MULTITHREADED
  884. BX_TRACE("Running in %s-threaded mode", m_singleThreaded ? "single" : "multi");
  885. s_threadIndex = BGFX_MAIN_THREAD_MAGIC;
  886. for (uint32_t ii = 0; ii < BX_COUNTOF(m_viewRemap); ++ii)
  887. {
  888. m_viewRemap[ii] = uint8_t(ii);
  889. }
  890. memset(m_fb, 0xff, sizeof(m_fb) );
  891. memset(m_clear, 0, sizeof(m_clear) );
  892. memset(m_rect, 0, sizeof(m_rect) );
  893. memset(m_scissor, 0, sizeof(m_scissor) );
  894. memset(m_seq, 0, sizeof(m_seq) );
  895. memset(m_seqMask, 0, sizeof(m_seqMask) );
  896. for (uint32_t ii = 0; ii < BX_COUNTOF(m_rect); ++ii)
  897. {
  898. m_rect[ii].m_width = 1;
  899. m_rect[ii].m_height = 1;
  900. }
  901. for (uint32_t ii = 0; ii < BX_COUNTOF(m_clearColor); ++ii)
  902. {
  903. m_clearColor[ii][0] = 0.0f;
  904. m_clearColor[ii][1] = 0.0f;
  905. m_clearColor[ii][2] = 0.0f;
  906. m_clearColor[ii][3] = 1.0f;
  907. }
  908. m_declRef.init();
  909. CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::RendererInit);
  910. cmdbuf.write(_type);
  911. frameNoRenderWait();
  912. // Make sure renderer init is called from render thread.
  913. // g_caps is initialized and available after this point.
  914. frame();
  915. if (!m_rendererInitialized)
  916. {
  917. getCommandBuffer(CommandBuffer::RendererShutdownEnd);
  918. frame();
  919. frame();
  920. m_declRef.shutdown(m_vertexDeclHandle);
  921. m_submit->destroy();
  922. #if BGFX_CONFIG_MULTITHREADED
  923. m_render->destroy();
  924. #endif // BGFX_CONFIG_MULTITHREADED
  925. return false;
  926. }
  927. for (uint32_t ii = 0; ii < BX_COUNTOF(s_emulatedFormats); ++ii)
  928. {
  929. if (0 == (g_caps.formats[s_emulatedFormats[ii] ] & BGFX_CAPS_FORMAT_TEXTURE_COLOR) )
  930. {
  931. g_caps.formats[s_emulatedFormats[ii] ] |= BGFX_CAPS_FORMAT_TEXTURE_EMULATED;
  932. }
  933. }
  934. g_caps.rendererType = m_renderCtx->getRendererType();
  935. initAttribTypeSizeTable(g_caps.rendererType);
  936. g_caps.supported |= 0
  937. | (BX_ENABLED(BGFX_CONFIG_MULTITHREADED) && !m_singleThreaded ? BGFX_CAPS_RENDERER_MULTITHREADED : 0)
  938. ;
  939. dumpCaps();
  940. m_textVideoMemBlitter.init();
  941. m_clearQuad.init();
  942. m_submit->m_transientVb = createTransientVertexBuffer(BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE);
  943. m_submit->m_transientIb = createTransientIndexBuffer(BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE);
  944. frame();
  945. if (BX_ENABLED(BGFX_CONFIG_MULTITHREADED) )
  946. {
  947. m_submit->m_transientVb = createTransientVertexBuffer(BGFX_CONFIG_TRANSIENT_VERTEX_BUFFER_SIZE);
  948. m_submit->m_transientIb = createTransientIndexBuffer(BGFX_CONFIG_TRANSIENT_INDEX_BUFFER_SIZE);
  949. frame();
  950. }
  951. return true;
  952. }
  953. void Context::shutdown()
  954. {
  955. getCommandBuffer(CommandBuffer::RendererShutdownBegin);
  956. frame();
  957. destroyTransientVertexBuffer(m_submit->m_transientVb);
  958. destroyTransientIndexBuffer(m_submit->m_transientIb);
  959. m_textVideoMemBlitter.shutdown();
  960. m_clearQuad.shutdown();
  961. frame();
  962. if (BX_ENABLED(BGFX_CONFIG_MULTITHREADED) )
  963. {
  964. destroyTransientVertexBuffer(m_submit->m_transientVb);
  965. destroyTransientIndexBuffer(m_submit->m_transientIb);
  966. frame();
  967. }
  968. frame(); // If any VertexDecls needs to be destroyed.
  969. getCommandBuffer(CommandBuffer::RendererShutdownEnd);
  970. frame();
  971. m_dynVertexBufferAllocator.compact();
  972. m_dynIndexBufferAllocator.compact();
  973. m_declRef.shutdown(m_vertexDeclHandle);
  974. #if BGFX_CONFIG_MULTITHREADED
  975. // Render thread shutdown sequence.
  976. renderSemWait(); // Wait for previous frame.
  977. gameSemPost(); // OK to set context to NULL.
  978. // s_ctx is NULL here.
  979. renderSemWait(); // In RenderFrame::Exiting state.
  980. if (m_thread.isRunning() )
  981. {
  982. m_thread.shutdown();
  983. }
  984. m_render->destroy();
  985. #endif // BGFX_CONFIG_MULTITHREADED
  986. s_ctx = NULL;
  987. m_submit->destroy();
  988. if (BX_ENABLED(BGFX_CONFIG_DEBUG) )
  989. {
  990. #define CHECK_HANDLE_LEAK(_handleAlloc) \
  991. BX_MACRO_BLOCK_BEGIN \
  992. BX_WARN(0 == _handleAlloc.getNumHandles() \
  993. , "LEAK: " #_handleAlloc " %d (max: %d)" \
  994. , _handleAlloc.getNumHandles() \
  995. , _handleAlloc.getMaxHandles() \
  996. ); \
  997. BX_MACRO_BLOCK_END
  998. CHECK_HANDLE_LEAK(m_dynamicIndexBufferHandle);
  999. CHECK_HANDLE_LEAK(m_dynamicVertexBufferHandle);
  1000. CHECK_HANDLE_LEAK(m_indexBufferHandle);
  1001. CHECK_HANDLE_LEAK(m_vertexDeclHandle);
  1002. CHECK_HANDLE_LEAK(m_vertexBufferHandle);
  1003. CHECK_HANDLE_LEAK(m_shaderHandle);
  1004. CHECK_HANDLE_LEAK(m_programHandle);
  1005. CHECK_HANDLE_LEAK(m_textureHandle);
  1006. CHECK_HANDLE_LEAK(m_frameBufferHandle);
  1007. CHECK_HANDLE_LEAK(m_uniformHandle);
  1008. #undef CHECK_HANDLE_LEAK
  1009. }
  1010. }
  1011. void Context::freeDynamicBuffers()
  1012. {
  1013. for (uint16_t ii = 0, num = m_numFreeDynamicIndexBufferHandles; ii < num; ++ii)
  1014. {
  1015. destroyDynamicIndexBufferInternal(m_freeDynamicIndexBufferHandle[ii]);
  1016. }
  1017. m_numFreeDynamicIndexBufferHandles = 0;
  1018. for (uint16_t ii = 0, num = m_numFreeDynamicVertexBufferHandles; ii < num; ++ii)
  1019. {
  1020. destroyDynamicVertexBufferInternal(m_freeDynamicVertexBufferHandle[ii]);
  1021. }
  1022. m_numFreeDynamicVertexBufferHandles = 0;
  1023. }
  1024. void Context::freeAllHandles(Frame* _frame)
  1025. {
  1026. for (uint16_t ii = 0, num = _frame->m_numFreeIndexBufferHandles; ii < num; ++ii)
  1027. {
  1028. m_indexBufferHandle.free(_frame->m_freeIndexBufferHandle[ii].idx);
  1029. }
  1030. for (uint16_t ii = 0, num = _frame->m_numFreeVertexDeclHandles; ii < num; ++ii)
  1031. {
  1032. m_vertexDeclHandle.free(_frame->m_freeVertexDeclHandle[ii].idx);
  1033. }
  1034. for (uint16_t ii = 0, num = _frame->m_numFreeVertexBufferHandles; ii < num; ++ii)
  1035. {
  1036. destroyVertexBufferInternal(_frame->m_freeVertexBufferHandle[ii]);
  1037. }
  1038. for (uint16_t ii = 0, num = _frame->m_numFreeShaderHandles; ii < num; ++ii)
  1039. {
  1040. m_shaderHandle.free(_frame->m_freeShaderHandle[ii].idx);
  1041. }
  1042. for (uint16_t ii = 0, num = _frame->m_numFreeProgramHandles; ii < num; ++ii)
  1043. {
  1044. m_programHandle.free(_frame->m_freeProgramHandle[ii].idx);
  1045. }
  1046. for (uint16_t ii = 0, num = _frame->m_numFreeTextureHandles; ii < num; ++ii)
  1047. {
  1048. m_textureHandle.free(_frame->m_freeTextureHandle[ii].idx);
  1049. }
  1050. for (uint16_t ii = 0, num = _frame->m_numFreeFrameBufferHandles; ii < num; ++ii)
  1051. {
  1052. m_frameBufferHandle.free(_frame->m_freeFrameBufferHandle[ii].idx);
  1053. }
  1054. for (uint16_t ii = 0, num = _frame->m_numFreeUniformHandles; ii < num; ++ii)
  1055. {
  1056. m_uniformHandle.free(_frame->m_freeUniformHandle[ii].idx);
  1057. }
  1058. }
  1059. uint32_t Context::frame()
  1060. {
  1061. BX_CHECK(0 == m_instBufferCount, "Instance buffer allocated, but not used. This is incorrect, and causes memory leak.");
  1062. // wait for render thread to finish
  1063. renderSemWait();
  1064. frameNoRenderWait();
  1065. return m_frames;
  1066. }
  1067. void Context::frameNoRenderWait()
  1068. {
  1069. swap();
  1070. // release render thread
  1071. gameSemPost();
  1072. }
  1073. void Context::swap()
  1074. {
  1075. freeDynamicBuffers();
  1076. m_submit->m_resolution = m_resolution;
  1077. m_submit->m_debug = m_debug;
  1078. memcpy(m_submit->m_viewRemap, m_viewRemap, sizeof(m_viewRemap) );
  1079. memcpy(m_submit->m_fb, m_fb, sizeof(m_fb) );
  1080. memcpy(m_submit->m_clear, m_clear, sizeof(m_clear) );
  1081. memcpy(m_submit->m_rect, m_rect, sizeof(m_rect) );
  1082. memcpy(m_submit->m_scissor, m_scissor, sizeof(m_scissor) );
  1083. memcpy(m_submit->m_view, m_view, sizeof(m_view) );
  1084. memcpy(m_submit->m_proj, m_proj, sizeof(m_proj) );
  1085. memcpy(m_submit->m_viewFlags, m_viewFlags, sizeof(m_viewFlags) );
  1086. if (m_colorPaletteDirty > 0)
  1087. {
  1088. --m_colorPaletteDirty;
  1089. memcpy(m_submit->m_colorPalette, m_clearColor, sizeof(m_clearColor) );
  1090. }
  1091. m_submit->finish();
  1092. bx::xchg(m_render, m_submit);
  1093. if (!BX_ENABLED(BGFX_CONFIG_MULTITHREADED)
  1094. || m_singleThreaded)
  1095. {
  1096. renderFrame();
  1097. }
  1098. m_frames++;
  1099. m_submit->start();
  1100. memset(m_seq, 0, sizeof(m_seq) );
  1101. freeAllHandles(m_submit);
  1102. m_submit->resetFreeHandles();
  1103. m_submit->m_textVideoMem->resize(m_render->m_textVideoMem->m_small
  1104. , m_resolution.m_width
  1105. , m_resolution.m_height
  1106. );
  1107. }
  1108. const char* Context::getName(UniformHandle _handle) const
  1109. {
  1110. for (UniformHashMap::const_iterator it = m_uniformHashMap.begin(), itEnd = m_uniformHashMap.end(); it != itEnd; ++it)
  1111. {
  1112. if (it->second.idx == _handle.idx)
  1113. {
  1114. return it->first.c_str();
  1115. }
  1116. }
  1117. return NULL;
  1118. }
  1119. bool Context::renderFrame()
  1120. {
  1121. if (m_rendererInitialized
  1122. && !m_flipAfterRender)
  1123. {
  1124. m_renderCtx->flip(m_render->m_hmd);
  1125. }
  1126. gameSemWait();
  1127. rendererExecCommands(m_render->m_cmdPre);
  1128. if (m_rendererInitialized)
  1129. {
  1130. m_renderCtx->submit(m_render, m_clearQuad, m_textVideoMemBlitter);
  1131. }
  1132. rendererExecCommands(m_render->m_cmdPost);
  1133. renderSemPost();
  1134. if (m_rendererInitialized
  1135. && m_flipAfterRender)
  1136. {
  1137. m_renderCtx->flip(m_render->m_hmd);
  1138. }
  1139. return m_exit;
  1140. }
  1141. void rendererUpdateUniforms(RendererContextI* _renderCtx, UniformBuffer* _uniformBuffer, uint32_t _begin, uint32_t _end)
  1142. {
  1143. _uniformBuffer->reset(_begin);
  1144. while (_uniformBuffer->getPos() < _end)
  1145. {
  1146. uint32_t opcode = _uniformBuffer->read();
  1147. if (UniformType::End == opcode)
  1148. {
  1149. break;
  1150. }
  1151. UniformType::Enum type;
  1152. uint16_t loc;
  1153. uint16_t num;
  1154. uint16_t copy;
  1155. UniformBuffer::decodeOpcode(opcode, type, loc, num, copy);
  1156. uint32_t size = g_uniformTypeSize[type]*num;
  1157. const char* data = _uniformBuffer->read(size);
  1158. if (UniformType::Count > type)
  1159. {
  1160. if (copy)
  1161. {
  1162. _renderCtx->updateUniform(loc, data, size);
  1163. }
  1164. else
  1165. {
  1166. _renderCtx->updateUniform(loc, *(const char**)(data), size);
  1167. }
  1168. }
  1169. else
  1170. {
  1171. _renderCtx->setMarker(data, size);
  1172. }
  1173. }
  1174. }
  1175. void Context::flushTextureUpdateBatch(CommandBuffer& _cmdbuf)
  1176. {
  1177. if (m_textureUpdateBatch.sort() )
  1178. {
  1179. const uint32_t pos = _cmdbuf.m_pos;
  1180. uint32_t currentKey = UINT32_MAX;
  1181. for (uint32_t ii = 0, num = m_textureUpdateBatch.m_num; ii < num; ++ii)
  1182. {
  1183. _cmdbuf.m_pos = m_textureUpdateBatch.m_values[ii];
  1184. TextureHandle handle;
  1185. _cmdbuf.read(handle);
  1186. uint8_t side;
  1187. _cmdbuf.read(side);
  1188. uint8_t mip;
  1189. _cmdbuf.read(mip);
  1190. Rect rect;
  1191. _cmdbuf.read(rect);
  1192. uint16_t zz;
  1193. _cmdbuf.read(zz);
  1194. uint16_t depth;
  1195. _cmdbuf.read(depth);
  1196. uint16_t pitch;
  1197. _cmdbuf.read(pitch);
  1198. Memory* mem;
  1199. _cmdbuf.read(mem);
  1200. uint32_t key = m_textureUpdateBatch.m_keys[ii];
  1201. if (key != currentKey)
  1202. {
  1203. if (currentKey != UINT32_MAX)
  1204. {
  1205. m_renderCtx->updateTextureEnd();
  1206. }
  1207. currentKey = key;
  1208. m_renderCtx->updateTextureBegin(handle, side, mip);
  1209. }
  1210. m_renderCtx->updateTexture(handle, side, mip, rect, zz, depth, pitch, mem);
  1211. release(mem);
  1212. }
  1213. if (currentKey != UINT32_MAX)
  1214. {
  1215. m_renderCtx->updateTextureEnd();
  1216. }
  1217. m_textureUpdateBatch.reset();
  1218. _cmdbuf.m_pos = pos;
  1219. }
  1220. }
  1221. typedef RendererContextI* (*RendererCreateFn)();
  1222. typedef void (*RendererDestroyFn)();
  1223. #define BGFX_RENDERER_CONTEXT(_namespace) \
  1224. namespace _namespace \
  1225. { \
  1226. extern RendererContextI* rendererCreate(); \
  1227. extern void rendererDestroy(); \
  1228. }
  1229. BGFX_RENDERER_CONTEXT(noop);
  1230. BGFX_RENDERER_CONTEXT(d3d9);
  1231. BGFX_RENDERER_CONTEXT(d3d11);
  1232. BGFX_RENDERER_CONTEXT(d3d12);
  1233. BGFX_RENDERER_CONTEXT(mtl);
  1234. BGFX_RENDERER_CONTEXT(gl);
  1235. BGFX_RENDERER_CONTEXT(vk);
  1236. #undef BGFX_RENDERER_CONTEXT
  1237. struct RendererCreator
  1238. {
  1239. RendererCreateFn createFn;
  1240. RendererDestroyFn destroyFn;
  1241. const char* name;
  1242. bool supported;
  1243. };
  1244. static RendererCreator s_rendererCreator[] =
  1245. {
  1246. { noop::rendererCreate, noop::rendererDestroy, BGFX_RENDERER_NULL_NAME, !!BGFX_CONFIG_RENDERER_NULL }, // Noop
  1247. { d3d9::rendererCreate, d3d9::rendererDestroy, BGFX_RENDERER_DIRECT3D9_NAME, !!BGFX_CONFIG_RENDERER_DIRECT3D9 }, // Direct3D9
  1248. { d3d11::rendererCreate, d3d11::rendererDestroy, BGFX_RENDERER_DIRECT3D11_NAME, !!BGFX_CONFIG_RENDERER_DIRECT3D11 }, // Direct3D11
  1249. { d3d12::rendererCreate, d3d12::rendererDestroy, BGFX_RENDERER_DIRECT3D12_NAME, !!BGFX_CONFIG_RENDERER_DIRECT3D12 }, // Direct3D12
  1250. #if BX_PLATFORM_OSX || BX_PLATFORM_IOS
  1251. { mtl::rendererCreate, mtl::rendererDestroy, BGFX_RENDERER_METAL_NAME, !!BGFX_CONFIG_RENDERER_METAL }, // Metal
  1252. #else
  1253. { noop::rendererCreate, noop::rendererDestroy, BGFX_RENDERER_NULL_NAME, !!BGFX_CONFIG_RENDERER_NULL }, // Noop
  1254. #endif // BX_PLATFORM_OSX || BX_PLATFORM_IOS
  1255. { gl::rendererCreate, gl::rendererDestroy, BGFX_RENDERER_OPENGL_NAME, !!BGFX_CONFIG_RENDERER_OPENGLES }, // OpenGLES
  1256. { gl::rendererCreate, gl::rendererDestroy, BGFX_RENDERER_OPENGL_NAME, !!BGFX_CONFIG_RENDERER_OPENGL }, // OpenGL
  1257. { vk::rendererCreate, vk::rendererDestroy, BGFX_RENDERER_VULKAN_NAME, !!BGFX_CONFIG_RENDERER_VULKAN }, // Vulkan
  1258. };
  1259. BX_STATIC_ASSERT(BX_COUNTOF(s_rendererCreator) == RendererType::Count);
  1260. static RendererDestroyFn s_rendererDestroyFn;
  1261. struct Condition
  1262. {
  1263. enum Enum
  1264. {
  1265. LessEqual,
  1266. GreaterEqual,
  1267. };
  1268. };
  1269. bool windowsVersionIs(Condition::Enum _op, uint32_t _version)
  1270. {
  1271. #if BX_PLATFORM_WINDOWS
  1272. static const uint8_t s_condition[] =
  1273. {
  1274. VER_LESS_EQUAL,
  1275. VER_GREATER_EQUAL,
  1276. };
  1277. OSVERSIONINFOEXA ovi;
  1278. memset(&ovi, 0, sizeof(ovi) );
  1279. ovi.dwOSVersionInfoSize = sizeof(ovi);
  1280. // _WIN32_WINNT_WINBLUE 0x0603
  1281. // _WIN32_WINNT_WIN8 0x0602
  1282. // _WIN32_WINNT_WIN7 0x0601
  1283. // _WIN32_WINNT_VISTA 0x0600
  1284. ovi.dwMajorVersion = HIBYTE(_version);
  1285. ovi.dwMinorVersion = LOBYTE(_version);
  1286. DWORDLONG cond = 0;
  1287. VER_SET_CONDITION(cond, VER_MAJORVERSION, s_condition[_op]);
  1288. VER_SET_CONDITION(cond, VER_MINORVERSION, s_condition[_op]);
  1289. return !!VerifyVersionInfoA(&ovi, VER_MAJORVERSION | VER_MINORVERSION, cond);
  1290. #else
  1291. BX_UNUSED(_op, _version);
  1292. return false;
  1293. #endif // BX_PLATFORM_WINDOWS
  1294. }
  1295. RendererContextI* rendererCreate(RendererType::Enum _type)
  1296. {
  1297. if (RendererType::Count == _type)
  1298. {
  1299. again:
  1300. if (BX_ENABLED(BX_PLATFORM_WINDOWS) )
  1301. {
  1302. RendererType::Enum first = RendererType::Direct3D9;
  1303. RendererType::Enum second = RendererType::Direct3D11;
  1304. if (windowsVersionIs(Condition::GreaterEqual, 0x0602) )
  1305. {
  1306. first = RendererType::Direct3D12;
  1307. second = RendererType::Direct3D11;
  1308. if (!s_rendererCreator[second].supported)
  1309. {
  1310. second = RendererType::Direct3D9;
  1311. }
  1312. }
  1313. else if (windowsVersionIs(Condition::GreaterEqual, 0x0601) )
  1314. {
  1315. first = RendererType::Direct3D11;
  1316. second = RendererType::Direct3D9;
  1317. }
  1318. if (s_rendererCreator[first].supported)
  1319. {
  1320. _type = first;
  1321. }
  1322. else if (s_rendererCreator[second].supported)
  1323. {
  1324. _type = second;
  1325. }
  1326. else if (s_rendererCreator[RendererType::OpenGL].supported)
  1327. {
  1328. _type = RendererType::OpenGL;
  1329. }
  1330. else if (s_rendererCreator[RendererType::OpenGLES].supported)
  1331. {
  1332. _type = RendererType::OpenGLES;
  1333. }
  1334. else if (s_rendererCreator[RendererType::Direct3D12].supported)
  1335. {
  1336. _type = RendererType::Direct3D12;
  1337. }
  1338. else if (s_rendererCreator[RendererType::Vulkan].supported)
  1339. {
  1340. _type = RendererType::Vulkan;
  1341. }
  1342. else
  1343. {
  1344. _type = RendererType::Null;
  1345. }
  1346. }
  1347. else if (BX_ENABLED(BX_PLATFORM_IOS) )
  1348. {
  1349. if (s_rendererCreator[RendererType::Metal].supported)
  1350. {
  1351. _type = RendererType::Metal;
  1352. }
  1353. else if (s_rendererCreator[RendererType::OpenGLES].supported)
  1354. {
  1355. _type = RendererType::OpenGLES;
  1356. }
  1357. }
  1358. else if (BX_ENABLED(0
  1359. || BX_PLATFORM_ANDROID
  1360. || BX_PLATFORM_EMSCRIPTEN
  1361. || BX_PLATFORM_NACL
  1362. || BX_PLATFORM_RPI
  1363. ) )
  1364. {
  1365. _type = RendererType::OpenGLES;
  1366. }
  1367. else if (BX_ENABLED(BX_PLATFORM_WINRT) )
  1368. {
  1369. _type = RendererType::Direct3D11;
  1370. }
  1371. else
  1372. {
  1373. #if 1
  1374. if (s_rendererCreator[RendererType::Metal].supported)
  1375. {
  1376. _type = RendererType::Metal;
  1377. }
  1378. else
  1379. #endif // 0
  1380. if (s_rendererCreator[RendererType::OpenGL].supported)
  1381. {
  1382. _type = RendererType::OpenGL;
  1383. }
  1384. else if (s_rendererCreator[RendererType::OpenGLES].supported)
  1385. {
  1386. _type = RendererType::OpenGLES;
  1387. }
  1388. }
  1389. if (!s_rendererCreator[_type].supported)
  1390. {
  1391. _type = RendererType::Null;
  1392. }
  1393. }
  1394. RendererContextI* renderCtx = s_rendererCreator[_type].createFn();
  1395. if (NULL == renderCtx)
  1396. {
  1397. s_rendererCreator[_type].supported = false;
  1398. goto again;
  1399. }
  1400. s_rendererDestroyFn = s_rendererCreator[_type].destroyFn;
  1401. return renderCtx;
  1402. }
  1403. void rendererDestroy()
  1404. {
  1405. s_rendererDestroyFn();
  1406. }
  1407. void Context::rendererExecCommands(CommandBuffer& _cmdbuf)
  1408. {
  1409. _cmdbuf.reset();
  1410. bool end = false;
  1411. if (NULL == m_renderCtx)
  1412. {
  1413. uint8_t command;
  1414. _cmdbuf.read(command);
  1415. switch (command)
  1416. {
  1417. case CommandBuffer::RendererShutdownEnd:
  1418. m_exit = true;
  1419. return;
  1420. case CommandBuffer::End:
  1421. return;
  1422. default:
  1423. {
  1424. BX_CHECK(CommandBuffer::RendererInit == command
  1425. , "RendererInit must be the first command in command buffer before initialization. Unexpected command %d?"
  1426. , command
  1427. );
  1428. BX_CHECK(!m_rendererInitialized, "This shouldn't happen! Bad synchronization?");
  1429. RendererType::Enum type;
  1430. _cmdbuf.read(type);
  1431. m_renderCtx = rendererCreate(type);
  1432. m_rendererInitialized = NULL != m_renderCtx;
  1433. if (!m_rendererInitialized)
  1434. {
  1435. _cmdbuf.read(command);
  1436. BX_CHECK(CommandBuffer::End == command, "Unexpected command %d?"
  1437. , command
  1438. );
  1439. return;
  1440. }
  1441. }
  1442. break;
  1443. }
  1444. }
  1445. do
  1446. {
  1447. uint8_t command;
  1448. _cmdbuf.read(command);
  1449. switch (command)
  1450. {
  1451. case CommandBuffer::RendererShutdownBegin:
  1452. {
  1453. BX_CHECK(m_rendererInitialized, "This shouldn't happen! Bad synchronization?");
  1454. m_rendererInitialized = false;
  1455. }
  1456. break;
  1457. case CommandBuffer::RendererShutdownEnd:
  1458. {
  1459. BX_CHECK(!m_rendererInitialized && !m_exit, "This shouldn't happen! Bad synchronization?");
  1460. rendererDestroy();
  1461. m_renderCtx = NULL;
  1462. m_exit = true;
  1463. }
  1464. // fall through
  1465. case CommandBuffer::End:
  1466. end = true;
  1467. break;
  1468. case CommandBuffer::CreateIndexBuffer:
  1469. {
  1470. IndexBufferHandle handle;
  1471. _cmdbuf.read(handle);
  1472. Memory* mem;
  1473. _cmdbuf.read(mem);
  1474. uint16_t flags;
  1475. _cmdbuf.read(flags);
  1476. m_renderCtx->createIndexBuffer(handle, mem, flags);
  1477. release(mem);
  1478. }
  1479. break;
  1480. case CommandBuffer::DestroyIndexBuffer:
  1481. {
  1482. IndexBufferHandle handle;
  1483. _cmdbuf.read(handle);
  1484. m_renderCtx->destroyIndexBuffer(handle);
  1485. }
  1486. break;
  1487. case CommandBuffer::CreateVertexDecl:
  1488. {
  1489. VertexDeclHandle handle;
  1490. _cmdbuf.read(handle);
  1491. VertexDecl decl;
  1492. _cmdbuf.read(decl);
  1493. m_renderCtx->createVertexDecl(handle, decl);
  1494. }
  1495. break;
  1496. case CommandBuffer::DestroyVertexDecl:
  1497. {
  1498. VertexDeclHandle handle;
  1499. _cmdbuf.read(handle);
  1500. m_renderCtx->destroyVertexDecl(handle);
  1501. }
  1502. break;
  1503. case CommandBuffer::CreateVertexBuffer:
  1504. {
  1505. VertexBufferHandle handle;
  1506. _cmdbuf.read(handle);
  1507. Memory* mem;
  1508. _cmdbuf.read(mem);
  1509. VertexDeclHandle declHandle;
  1510. _cmdbuf.read(declHandle);
  1511. uint16_t flags;
  1512. _cmdbuf.read(flags);
  1513. m_renderCtx->createVertexBuffer(handle, mem, declHandle, flags);
  1514. release(mem);
  1515. }
  1516. break;
  1517. case CommandBuffer::DestroyVertexBuffer:
  1518. {
  1519. VertexBufferHandle handle;
  1520. _cmdbuf.read(handle);
  1521. m_renderCtx->destroyVertexBuffer(handle);
  1522. }
  1523. break;
  1524. case CommandBuffer::CreateDynamicIndexBuffer:
  1525. {
  1526. IndexBufferHandle handle;
  1527. _cmdbuf.read(handle);
  1528. uint32_t size;
  1529. _cmdbuf.read(size);
  1530. uint16_t flags;
  1531. _cmdbuf.read(flags);
  1532. m_renderCtx->createDynamicIndexBuffer(handle, size, flags);
  1533. }
  1534. break;
  1535. case CommandBuffer::UpdateDynamicIndexBuffer:
  1536. {
  1537. IndexBufferHandle handle;
  1538. _cmdbuf.read(handle);
  1539. uint32_t offset;
  1540. _cmdbuf.read(offset);
  1541. uint32_t size;
  1542. _cmdbuf.read(size);
  1543. Memory* mem;
  1544. _cmdbuf.read(mem);
  1545. m_renderCtx->updateDynamicIndexBuffer(handle, offset, size, mem);
  1546. release(mem);
  1547. }
  1548. break;
  1549. case CommandBuffer::DestroyDynamicIndexBuffer:
  1550. {
  1551. IndexBufferHandle handle;
  1552. _cmdbuf.read(handle);
  1553. m_renderCtx->destroyDynamicIndexBuffer(handle);
  1554. }
  1555. break;
  1556. case CommandBuffer::CreateDynamicVertexBuffer:
  1557. {
  1558. VertexBufferHandle handle;
  1559. _cmdbuf.read(handle);
  1560. uint32_t size;
  1561. _cmdbuf.read(size);
  1562. uint16_t flags;
  1563. _cmdbuf.read(flags);
  1564. m_renderCtx->createDynamicVertexBuffer(handle, size, flags);
  1565. }
  1566. break;
  1567. case CommandBuffer::UpdateDynamicVertexBuffer:
  1568. {
  1569. VertexBufferHandle handle;
  1570. _cmdbuf.read(handle);
  1571. uint32_t offset;
  1572. _cmdbuf.read(offset);
  1573. uint32_t size;
  1574. _cmdbuf.read(size);
  1575. Memory* mem;
  1576. _cmdbuf.read(mem);
  1577. m_renderCtx->updateDynamicVertexBuffer(handle, offset, size, mem);
  1578. release(mem);
  1579. }
  1580. break;
  1581. case CommandBuffer::DestroyDynamicVertexBuffer:
  1582. {
  1583. VertexBufferHandle handle;
  1584. _cmdbuf.read(handle);
  1585. m_renderCtx->destroyDynamicVertexBuffer(handle);
  1586. }
  1587. break;
  1588. case CommandBuffer::CreateShader:
  1589. {
  1590. ShaderHandle handle;
  1591. _cmdbuf.read(handle);
  1592. Memory* mem;
  1593. _cmdbuf.read(mem);
  1594. m_renderCtx->createShader(handle, mem);
  1595. release(mem);
  1596. }
  1597. break;
  1598. case CommandBuffer::DestroyShader:
  1599. {
  1600. ShaderHandle handle;
  1601. _cmdbuf.read(handle);
  1602. m_renderCtx->destroyShader(handle);
  1603. }
  1604. break;
  1605. case CommandBuffer::CreateProgram:
  1606. {
  1607. ProgramHandle handle;
  1608. _cmdbuf.read(handle);
  1609. ShaderHandle vsh;
  1610. _cmdbuf.read(vsh);
  1611. ShaderHandle fsh;
  1612. _cmdbuf.read(fsh);
  1613. m_renderCtx->createProgram(handle, vsh, fsh);
  1614. }
  1615. break;
  1616. case CommandBuffer::DestroyProgram:
  1617. {
  1618. ProgramHandle handle;
  1619. _cmdbuf.read(handle);
  1620. m_renderCtx->destroyProgram(handle);
  1621. }
  1622. break;
  1623. case CommandBuffer::CreateTexture:
  1624. {
  1625. TextureHandle handle;
  1626. _cmdbuf.read(handle);
  1627. Memory* mem;
  1628. _cmdbuf.read(mem);
  1629. uint32_t flags;
  1630. _cmdbuf.read(flags);
  1631. uint8_t skip;
  1632. _cmdbuf.read(skip);
  1633. m_renderCtx->createTexture(handle, mem, flags, skip);
  1634. bx::MemoryReader reader(mem->data, mem->size);
  1635. uint32_t magic;
  1636. bx::read(&reader, magic);
  1637. if (BGFX_CHUNK_MAGIC_TEX == magic)
  1638. {
  1639. TextureCreate tc;
  1640. bx::read(&reader, tc);
  1641. if (NULL != tc.m_mem)
  1642. {
  1643. release(tc.m_mem);
  1644. }
  1645. }
  1646. release(mem);
  1647. }
  1648. break;
  1649. case CommandBuffer::UpdateTexture:
  1650. {
  1651. if (m_textureUpdateBatch.isFull() )
  1652. {
  1653. flushTextureUpdateBatch(_cmdbuf);
  1654. }
  1655. uint32_t value = _cmdbuf.m_pos;
  1656. TextureHandle handle;
  1657. _cmdbuf.read(handle);
  1658. uint8_t side;
  1659. _cmdbuf.read(side);
  1660. uint8_t mip;
  1661. _cmdbuf.read(mip);
  1662. _cmdbuf.skip<Rect>();
  1663. _cmdbuf.skip<uint16_t>();
  1664. _cmdbuf.skip<uint16_t>();
  1665. _cmdbuf.skip<uint16_t>();
  1666. _cmdbuf.skip<Memory*>();
  1667. uint32_t key = (handle.idx<<16)
  1668. | (side<<8)
  1669. | mip
  1670. ;
  1671. m_textureUpdateBatch.add(key, value);
  1672. }
  1673. break;
  1674. case CommandBuffer::ResizeTexture:
  1675. {
  1676. TextureHandle handle;
  1677. _cmdbuf.read(handle);
  1678. uint16_t width;
  1679. _cmdbuf.read(width);
  1680. uint16_t height;
  1681. _cmdbuf.read(height);
  1682. m_renderCtx->resizeTexture(handle, width, height);
  1683. }
  1684. break;
  1685. case CommandBuffer::DestroyTexture:
  1686. {
  1687. TextureHandle handle;
  1688. _cmdbuf.read(handle);
  1689. m_renderCtx->destroyTexture(handle);
  1690. }
  1691. break;
  1692. case CommandBuffer::CreateFrameBuffer:
  1693. {
  1694. FrameBufferHandle handle;
  1695. _cmdbuf.read(handle);
  1696. bool window;
  1697. _cmdbuf.read(window);
  1698. if (window)
  1699. {
  1700. void* nwh;
  1701. _cmdbuf.read(nwh);
  1702. uint16_t width;
  1703. _cmdbuf.read(width);
  1704. uint16_t height;
  1705. _cmdbuf.read(height);
  1706. TextureFormat::Enum depthFormat;
  1707. _cmdbuf.read(depthFormat);
  1708. m_renderCtx->createFrameBuffer(handle, nwh, width, height, depthFormat);
  1709. }
  1710. else
  1711. {
  1712. uint8_t num;
  1713. _cmdbuf.read(num);
  1714. TextureHandle textureHandles[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
  1715. for (uint32_t ii = 0; ii < num; ++ii)
  1716. {
  1717. _cmdbuf.read(textureHandles[ii]);
  1718. }
  1719. m_renderCtx->createFrameBuffer(handle, num, textureHandles);
  1720. }
  1721. }
  1722. break;
  1723. case CommandBuffer::DestroyFrameBuffer:
  1724. {
  1725. FrameBufferHandle handle;
  1726. _cmdbuf.read(handle);
  1727. m_renderCtx->destroyFrameBuffer(handle);
  1728. }
  1729. break;
  1730. case CommandBuffer::CreateUniform:
  1731. {
  1732. UniformHandle handle;
  1733. _cmdbuf.read(handle);
  1734. UniformType::Enum type;
  1735. _cmdbuf.read(type);
  1736. uint16_t num;
  1737. _cmdbuf.read(num);
  1738. uint8_t len;
  1739. _cmdbuf.read(len);
  1740. const char* name = (const char*)_cmdbuf.skip(len);
  1741. m_renderCtx->createUniform(handle, type, num, name);
  1742. }
  1743. break;
  1744. case CommandBuffer::DestroyUniform:
  1745. {
  1746. UniformHandle handle;
  1747. _cmdbuf.read(handle);
  1748. m_renderCtx->destroyUniform(handle);
  1749. }
  1750. break;
  1751. case CommandBuffer::SaveScreenShot:
  1752. {
  1753. uint16_t len;
  1754. _cmdbuf.read(len);
  1755. const char* filePath = (const char*)_cmdbuf.skip(len);
  1756. m_renderCtx->saveScreenShot(filePath);
  1757. }
  1758. break;
  1759. case CommandBuffer::UpdateViewName:
  1760. {
  1761. uint8_t id;
  1762. _cmdbuf.read(id);
  1763. uint16_t len;
  1764. _cmdbuf.read(len);
  1765. const char* name = (const char*)_cmdbuf.skip(len);
  1766. m_renderCtx->updateViewName(id, name);
  1767. }
  1768. break;
  1769. default:
  1770. BX_CHECK(false, "Invalid command: %d", command);
  1771. break;
  1772. }
  1773. } while (!end);
  1774. flushTextureUpdateBatch(_cmdbuf);
  1775. }
  1776. uint8_t getSupportedRenderers(RendererType::Enum _enum[RendererType::Count])
  1777. {
  1778. uint8_t num = 0;
  1779. for (uint8_t ii = 0; ii < uint8_t(RendererType::Count); ++ii)
  1780. {
  1781. if ( (RendererType::Direct3D11 == ii || RendererType::Direct3D12 == ii)
  1782. && windowsVersionIs(Condition::LessEqual, 0x0502) )
  1783. {
  1784. continue;
  1785. }
  1786. if (s_rendererCreator[ii].supported)
  1787. {
  1788. _enum[num++] = RendererType::Enum(ii);
  1789. }
  1790. }
  1791. return num;
  1792. }
  1793. const char* getRendererName(RendererType::Enum _type)
  1794. {
  1795. BX_CHECK(_type < RendererType::Count, "Invalid renderer type %d.", _type);
  1796. return s_rendererCreator[_type].name;
  1797. }
  1798. bool init(RendererType::Enum _type, uint16_t _vendorId, uint16_t _deviceId, CallbackI* _callback, bx::ReallocatorI* _allocator)
  1799. {
  1800. BX_CHECK(NULL == s_ctx, "bgfx is already initialized.");
  1801. memset(&g_caps, 0, sizeof(g_caps) );
  1802. g_caps.maxViews = BGFX_CONFIG_MAX_VIEWS;
  1803. g_caps.maxDrawCalls = BGFX_CONFIG_MAX_DRAW_CALLS;
  1804. g_caps.maxFBAttachments = 1;
  1805. g_caps.vendorId = _vendorId;
  1806. g_caps.deviceId = _deviceId;
  1807. if (NULL != _allocator)
  1808. {
  1809. g_allocator = _allocator;
  1810. }
  1811. else
  1812. {
  1813. bx::CrtAllocator allocator;
  1814. g_allocator =
  1815. s_allocatorStub = BX_NEW(&allocator, AllocatorStub);
  1816. }
  1817. if (NULL != _callback)
  1818. {
  1819. g_callback = _callback;
  1820. }
  1821. else
  1822. {
  1823. g_callback =
  1824. s_callbackStub = BX_NEW(g_allocator, CallbackStub);
  1825. }
  1826. BX_TRACE("Init...");
  1827. s_ctx = BX_ALIGNED_NEW(g_allocator, Context, 16);
  1828. if (!s_ctx->init(_type) )
  1829. {
  1830. BX_TRACE("Init failed.");
  1831. BX_ALIGNED_DELETE(g_allocator, s_ctx, 16);
  1832. s_ctx = NULL;
  1833. if (NULL != s_callbackStub)
  1834. {
  1835. BX_DELETE(g_allocator, s_callbackStub);
  1836. s_callbackStub = NULL;
  1837. }
  1838. if (NULL != s_allocatorStub)
  1839. {
  1840. bx::CrtAllocator allocator;
  1841. BX_DELETE(&allocator, s_allocatorStub);
  1842. s_allocatorStub = NULL;
  1843. }
  1844. s_threadIndex = 0;
  1845. g_callback = NULL;
  1846. g_allocator = NULL;
  1847. return false;
  1848. }
  1849. BX_TRACE("Init complete.");
  1850. return true;
  1851. }
  1852. void shutdown()
  1853. {
  1854. BX_TRACE("Shutdown...");
  1855. BGFX_CHECK_MAIN_THREAD();
  1856. Context* ctx = s_ctx; // it's going to be NULLd inside shutdown.
  1857. ctx->shutdown();
  1858. BX_CHECK(NULL == s_ctx, "bgfx is should be uninitialized here.");
  1859. BX_ALIGNED_DELETE(g_allocator, ctx, 16);
  1860. BX_TRACE("Shutdown complete.");
  1861. if (NULL != s_allocatorStub)
  1862. {
  1863. s_allocatorStub->checkLeaks();
  1864. }
  1865. if (NULL != s_callbackStub)
  1866. {
  1867. BX_DELETE(g_allocator, s_callbackStub);
  1868. s_callbackStub = NULL;
  1869. }
  1870. if (NULL != s_allocatorStub)
  1871. {
  1872. bx::CrtAllocator allocator;
  1873. BX_DELETE(&allocator, s_allocatorStub);
  1874. s_allocatorStub = NULL;
  1875. }
  1876. s_threadIndex = 0;
  1877. g_callback = NULL;
  1878. g_allocator = NULL;
  1879. }
  1880. void reset(uint32_t _width, uint32_t _height, uint32_t _flags)
  1881. {
  1882. BGFX_CHECK_MAIN_THREAD();
  1883. s_ctx->reset(_width, _height, _flags);
  1884. }
  1885. uint32_t frame()
  1886. {
  1887. BGFX_CHECK_MAIN_THREAD();
  1888. return s_ctx->frame();
  1889. }
  1890. const Caps* getCaps()
  1891. {
  1892. return &g_caps;
  1893. }
  1894. const HMD* getHMD()
  1895. {
  1896. return s_ctx->getHMD();
  1897. }
  1898. const Stats* getStats()
  1899. {
  1900. return s_ctx->getPerfStats();
  1901. }
  1902. RendererType::Enum getRendererType()
  1903. {
  1904. return g_caps.rendererType;
  1905. }
  1906. const Memory* alloc(uint32_t _size)
  1907. {
  1908. BX_CHECK(0 < _size, "Invalid memory operation. _size is 0.");
  1909. Memory* mem = (Memory*)BX_ALLOC(g_allocator, sizeof(Memory) + _size);
  1910. mem->size = _size;
  1911. mem->data = (uint8_t*)mem + sizeof(Memory);
  1912. return mem;
  1913. }
  1914. const Memory* copy(const void* _data, uint32_t _size)
  1915. {
  1916. BX_CHECK(0 < _size, "Invalid memory operation. _size is 0.");
  1917. const Memory* mem = alloc(_size);
  1918. memcpy(mem->data, _data, _size);
  1919. return mem;
  1920. }
  1921. struct MemoryRef
  1922. {
  1923. Memory mem;
  1924. ReleaseFn releaseFn;
  1925. void* userData;
  1926. };
  1927. const Memory* makeRef(const void* _data, uint32_t _size, ReleaseFn _releaseFn, void* _userData)
  1928. {
  1929. MemoryRef* memRef = (MemoryRef*)BX_ALLOC(g_allocator, sizeof(MemoryRef) );
  1930. memRef->mem.size = _size;
  1931. memRef->mem.data = (uint8_t*)_data;
  1932. memRef->releaseFn = _releaseFn;
  1933. memRef->userData = _userData;
  1934. return &memRef->mem;
  1935. }
  1936. bool isMemoryRef(const Memory* _mem)
  1937. {
  1938. return _mem->data != (uint8_t*)_mem + sizeof(Memory);
  1939. }
  1940. void release(const Memory* _mem)
  1941. {
  1942. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  1943. Memory* mem = const_cast<Memory*>(_mem);
  1944. if (isMemoryRef(mem) )
  1945. {
  1946. MemoryRef* memRef = reinterpret_cast<MemoryRef*>(mem);
  1947. if (NULL != memRef->releaseFn)
  1948. {
  1949. memRef->releaseFn(mem->data, memRef->userData);
  1950. }
  1951. }
  1952. BX_FREE(g_allocator, mem);
  1953. }
  1954. void setDebug(uint32_t _debug)
  1955. {
  1956. BGFX_CHECK_MAIN_THREAD();
  1957. s_ctx->setDebug(_debug);
  1958. }
  1959. void dbgTextClear(uint8_t _attr, bool _small)
  1960. {
  1961. BGFX_CHECK_MAIN_THREAD();
  1962. s_ctx->dbgTextClear(_attr, _small);
  1963. }
  1964. void dbgTextPrintfVargs(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList)
  1965. {
  1966. s_ctx->dbgTextPrintfVargs(_x, _y, _attr, _format, _argList);
  1967. }
  1968. void dbgTextPrintf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...)
  1969. {
  1970. BGFX_CHECK_MAIN_THREAD();
  1971. va_list argList;
  1972. va_start(argList, _format);
  1973. s_ctx->dbgTextPrintfVargs(_x, _y, _attr, _format, argList);
  1974. va_end(argList);
  1975. }
  1976. void dbgTextImage(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const void* _data, uint16_t _pitch)
  1977. {
  1978. BGFX_CHECK_MAIN_THREAD();
  1979. s_ctx->dbgTextImage(_x, _y, _width, _height, _data, _pitch);
  1980. }
  1981. IndexBufferHandle createIndexBuffer(const Memory* _mem, uint16_t _flags)
  1982. {
  1983. BGFX_CHECK_MAIN_THREAD();
  1984. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  1985. return s_ctx->createIndexBuffer(_mem, _flags);
  1986. }
  1987. void destroyIndexBuffer(IndexBufferHandle _handle)
  1988. {
  1989. BGFX_CHECK_MAIN_THREAD();
  1990. s_ctx->destroyIndexBuffer(_handle);
  1991. }
  1992. VertexBufferHandle createVertexBuffer(const Memory* _mem, const VertexDecl& _decl, uint16_t _flags)
  1993. {
  1994. BGFX_CHECK_MAIN_THREAD();
  1995. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  1996. BX_CHECK(0 != _decl.m_stride, "Invalid VertexDecl.");
  1997. return s_ctx->createVertexBuffer(_mem, _decl, _flags);
  1998. }
  1999. void destroyVertexBuffer(VertexBufferHandle _handle)
  2000. {
  2001. BGFX_CHECK_MAIN_THREAD();
  2002. s_ctx->destroyVertexBuffer(_handle);
  2003. }
  2004. DynamicIndexBufferHandle createDynamicIndexBuffer(uint32_t _num, uint16_t _flags)
  2005. {
  2006. BGFX_CHECK_MAIN_THREAD();
  2007. return s_ctx->createDynamicIndexBuffer(_num, _flags);
  2008. }
  2009. DynamicIndexBufferHandle createDynamicIndexBuffer(const Memory* _mem, uint16_t _flags)
  2010. {
  2011. BGFX_CHECK_MAIN_THREAD();
  2012. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2013. return s_ctx->createDynamicIndexBuffer(_mem, _flags);
  2014. }
  2015. void updateDynamicIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _startIndex, const Memory* _mem)
  2016. {
  2017. BGFX_CHECK_MAIN_THREAD();
  2018. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2019. s_ctx->updateDynamicIndexBuffer(_handle, _startIndex, _mem);
  2020. }
  2021. void destroyDynamicIndexBuffer(DynamicIndexBufferHandle _handle)
  2022. {
  2023. BGFX_CHECK_MAIN_THREAD();
  2024. s_ctx->destroyDynamicIndexBuffer(_handle);
  2025. }
  2026. DynamicVertexBufferHandle createDynamicVertexBuffer(uint32_t _num, const VertexDecl& _decl, uint16_t _flags)
  2027. {
  2028. BGFX_CHECK_MAIN_THREAD();
  2029. BX_CHECK(0 != _decl.m_stride, "Invalid VertexDecl.");
  2030. return s_ctx->createDynamicVertexBuffer(_num, _decl, _flags);
  2031. }
  2032. DynamicVertexBufferHandle createDynamicVertexBuffer(const Memory* _mem, const VertexDecl& _decl, uint16_t _flags)
  2033. {
  2034. BGFX_CHECK_MAIN_THREAD();
  2035. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2036. BX_CHECK(0 != _decl.m_stride, "Invalid VertexDecl.");
  2037. return s_ctx->createDynamicVertexBuffer(_mem, _decl, _flags);
  2038. }
  2039. void updateDynamicVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, const Memory* _mem)
  2040. {
  2041. BGFX_CHECK_MAIN_THREAD();
  2042. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2043. s_ctx->updateDynamicVertexBuffer(_handle, _startVertex, _mem);
  2044. }
  2045. void destroyDynamicVertexBuffer(DynamicVertexBufferHandle _handle)
  2046. {
  2047. BGFX_CHECK_MAIN_THREAD();
  2048. s_ctx->destroyDynamicVertexBuffer(_handle);
  2049. }
  2050. bool checkAvailTransientIndexBuffer(uint32_t _num)
  2051. {
  2052. BGFX_CHECK_MAIN_THREAD();
  2053. BX_CHECK(0 < _num, "Requesting 0 indices.");
  2054. return s_ctx->checkAvailTransientIndexBuffer(_num);
  2055. }
  2056. bool checkAvailTransientVertexBuffer(uint32_t _num, const VertexDecl& _decl)
  2057. {
  2058. BGFX_CHECK_MAIN_THREAD();
  2059. BX_CHECK(0 < _num, "Requesting 0 vertices.");
  2060. BX_CHECK(0 != _decl.m_stride, "Invalid VertexDecl.");
  2061. return s_ctx->checkAvailTransientVertexBuffer(_num, _decl.m_stride);
  2062. }
  2063. bool checkAvailInstanceDataBuffer(uint32_t _num, uint16_t _stride)
  2064. {
  2065. BGFX_CHECK_MAIN_THREAD();
  2066. BX_CHECK(0 < _num, "Requesting 0 instances.");
  2067. return s_ctx->checkAvailTransientVertexBuffer(_num, _stride);
  2068. }
  2069. bool checkAvailTransientBuffers(uint32_t _numVertices, const VertexDecl& _decl, uint32_t _numIndices)
  2070. {
  2071. BX_CHECK(0 != _decl.m_stride, "Invalid VertexDecl.");
  2072. return checkAvailTransientVertexBuffer(_numVertices, _decl)
  2073. && checkAvailTransientIndexBuffer(_numIndices)
  2074. ;
  2075. }
  2076. void allocTransientIndexBuffer(TransientIndexBuffer* _tib, uint32_t _num)
  2077. {
  2078. BGFX_CHECK_MAIN_THREAD();
  2079. BX_CHECK(NULL != _tib, "_tib can't be NULL");
  2080. BX_CHECK(0 < _num, "Requesting 0 indices.");
  2081. return s_ctx->allocTransientIndexBuffer(_tib, _num);
  2082. }
  2083. void allocTransientVertexBuffer(TransientVertexBuffer* _tvb, uint32_t _num, const VertexDecl& _decl)
  2084. {
  2085. BGFX_CHECK_MAIN_THREAD();
  2086. BX_CHECK(NULL != _tvb, "_tvb can't be NULL");
  2087. BX_CHECK(0 < _num, "Requesting 0 vertices.");
  2088. BX_CHECK(UINT16_MAX >= _num, "Requesting %d vertices (max: %d).", _num, UINT16_MAX);
  2089. BX_CHECK(0 != _decl.m_stride, "Invalid VertexDecl.");
  2090. return s_ctx->allocTransientVertexBuffer(_tvb, _num, _decl);
  2091. }
  2092. bool allocTransientBuffers(bgfx::TransientVertexBuffer* _tvb, const bgfx::VertexDecl& _decl, uint32_t _numVertices, bgfx::TransientIndexBuffer* _tib, uint32_t _numIndices)
  2093. {
  2094. if (checkAvailTransientBuffers(_numVertices, _decl, _numIndices) )
  2095. {
  2096. allocTransientVertexBuffer(_tvb, _numVertices, _decl);
  2097. allocTransientIndexBuffer(_tib, _numIndices);
  2098. return true;
  2099. }
  2100. return false;
  2101. }
  2102. const InstanceDataBuffer* allocInstanceDataBuffer(uint32_t _num, uint16_t _stride)
  2103. {
  2104. BGFX_CHECK_MAIN_THREAD();
  2105. BX_CHECK(0 != (g_caps.supported & BGFX_CAPS_INSTANCING), "Instancing is not supported! Use bgfx::getCaps to check backend renderer capabilities.");
  2106. BX_CHECK(0 < _num, "Requesting 0 instanced data vertices.");
  2107. return s_ctx->allocInstanceDataBuffer(_num, _stride);
  2108. }
  2109. IndirectBufferHandle createIndirectBuffer(uint32_t _num)
  2110. {
  2111. BGFX_CHECK_MAIN_THREAD();
  2112. return s_ctx->createIndirectBuffer(_num);
  2113. }
  2114. void destroyIndirectBuffer(IndirectBufferHandle _handle)
  2115. {
  2116. BGFX_CHECK_MAIN_THREAD();
  2117. s_ctx->destroyIndirectBuffer(_handle);
  2118. }
  2119. ShaderHandle createShader(const Memory* _mem)
  2120. {
  2121. BGFX_CHECK_MAIN_THREAD();
  2122. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2123. return s_ctx->createShader(_mem);
  2124. }
  2125. uint16_t getShaderUniforms(ShaderHandle _handle, UniformHandle* _uniforms, uint16_t _max)
  2126. {
  2127. BGFX_CHECK_MAIN_THREAD();
  2128. return s_ctx->getShaderUniforms(_handle, _uniforms, _max);
  2129. }
  2130. void destroyShader(ShaderHandle _handle)
  2131. {
  2132. BGFX_CHECK_MAIN_THREAD();
  2133. s_ctx->destroyShader(_handle);
  2134. }
  2135. ProgramHandle createProgram(ShaderHandle _vsh, ShaderHandle _fsh, bool _destroyShaders)
  2136. {
  2137. BGFX_CHECK_MAIN_THREAD();
  2138. if (!isValid(_fsh) )
  2139. {
  2140. return createProgram(_vsh, _destroyShaders);
  2141. }
  2142. return s_ctx->createProgram(_vsh, _fsh, _destroyShaders);
  2143. }
  2144. ProgramHandle createProgram(ShaderHandle _csh, bool _destroyShader)
  2145. {
  2146. BGFX_CHECK_MAIN_THREAD();
  2147. return s_ctx->createProgram(_csh, _destroyShader);
  2148. }
  2149. void destroyProgram(ProgramHandle _handle)
  2150. {
  2151. BGFX_CHECK_MAIN_THREAD();
  2152. s_ctx->destroyProgram(_handle);
  2153. }
  2154. void calcTextureSize(TextureInfo& _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, uint8_t _numMips, TextureFormat::Enum _format)
  2155. {
  2156. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  2157. const uint8_t bpp = blockInfo.bitsPerPixel;
  2158. const uint16_t blockWidth = blockInfo.blockWidth;
  2159. const uint16_t blockHeight = blockInfo.blockHeight;
  2160. const uint16_t minBlockX = blockInfo.minBlockX;
  2161. const uint16_t minBlockY = blockInfo.minBlockY;
  2162. _width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  2163. _height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  2164. _depth = bx::uint16_max(1, _depth);
  2165. _numMips = uint8_t(bx::uint16_max(1, _numMips) );
  2166. uint32_t width = _width;
  2167. uint32_t height = _height;
  2168. uint32_t depth = _depth;
  2169. uint32_t sides = _cubeMap ? 6 : 1;
  2170. uint32_t size = 0;
  2171. for (uint32_t lod = 0; lod < _numMips; ++lod)
  2172. {
  2173. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  2174. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  2175. depth = bx::uint32_max(1, depth);
  2176. size += width*height*depth*bpp/8 * sides;
  2177. width >>= 1;
  2178. height >>= 1;
  2179. depth >>= 1;
  2180. }
  2181. _info.format = _format;
  2182. _info.width = _width;
  2183. _info.height = _height;
  2184. _info.depth = _depth;
  2185. _info.numMips = _numMips;
  2186. _info.cubeMap = _cubeMap;
  2187. _info.storageSize = size;
  2188. _info.bitsPerPixel = bpp;
  2189. }
  2190. TextureHandle createTexture(const Memory* _mem, uint32_t _flags, uint8_t _skip, TextureInfo* _info)
  2191. {
  2192. BGFX_CHECK_MAIN_THREAD();
  2193. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2194. return s_ctx->createTexture(_mem, _flags, _skip, _info, BackbufferRatio::Count);
  2195. }
  2196. void getTextureSizeFromRatio(BackbufferRatio::Enum _ratio, uint16_t& _width, uint16_t& _height)
  2197. {
  2198. switch (_ratio)
  2199. {
  2200. case BackbufferRatio::Half: _width /= 2; _height /= 2; break;
  2201. case BackbufferRatio::Quarter: _width /= 4; _height /= 4; break;
  2202. case BackbufferRatio::Eighth: _width /= 8; _height /= 8; break;
  2203. case BackbufferRatio::Sixteenth: _width /= 16; _height /= 16; break;
  2204. case BackbufferRatio::Double: _width *= 2; _height *= 2; break;
  2205. default:
  2206. break;
  2207. }
  2208. _width = bx::uint16_max(1, _width);
  2209. _height = bx::uint16_max(1, _height);
  2210. }
  2211. TextureHandle createTexture2D(BackbufferRatio::Enum _ratio, uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem)
  2212. {
  2213. BGFX_CHECK_MAIN_THREAD();
  2214. _numMips = uint8_t(bx::uint32_max(1, _numMips) );
  2215. if (BX_ENABLED(BGFX_CONFIG_DEBUG)
  2216. && NULL != _mem)
  2217. {
  2218. TextureInfo ti;
  2219. calcTextureSize(ti, _width, _height, 1, false, _numMips, _format);
  2220. BX_CHECK(ti.storageSize == _mem->size
  2221. , "createTexture2D: Texture storage size doesn't match passed memory size (storage size: %d, memory size: %d)"
  2222. , ti.storageSize
  2223. , _mem->size
  2224. );
  2225. }
  2226. uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate);
  2227. const Memory* mem = alloc(size);
  2228. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  2229. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  2230. bx::write(&writer, magic);
  2231. if (BackbufferRatio::Count != _ratio)
  2232. {
  2233. _width = uint16_t(s_ctx->m_resolution.m_width);
  2234. _height = uint16_t(s_ctx->m_resolution.m_height);
  2235. getTextureSizeFromRatio(_ratio, _width, _height);
  2236. }
  2237. TextureCreate tc;
  2238. tc.m_flags = _flags;
  2239. tc.m_width = _width;
  2240. tc.m_height = _height;
  2241. tc.m_sides = 0;
  2242. tc.m_depth = 0;
  2243. tc.m_numMips = _numMips;
  2244. tc.m_format = uint8_t(_format);
  2245. tc.m_cubeMap = false;
  2246. tc.m_mem = _mem;
  2247. bx::write(&writer, tc);
  2248. return s_ctx->createTexture(mem, _flags, 0, NULL, _ratio);
  2249. }
  2250. TextureHandle createTexture2D(uint16_t _width, uint16_t _height, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem)
  2251. {
  2252. BX_CHECK(_width > 0 && _height > 0, "Invalid texture size (width %d, height %d).", _width, _height);
  2253. return createTexture2D(BackbufferRatio::Count, _width, _height, _numMips, _format, _flags, _mem);
  2254. }
  2255. TextureHandle createTexture2D(BackbufferRatio::Enum _ratio, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags)
  2256. {
  2257. BX_CHECK(_ratio < BackbufferRatio::Count, "Invalid back buffer ratio.");
  2258. return createTexture2D(_ratio, 0, 0, _numMips, _format, _flags, NULL);
  2259. }
  2260. TextureHandle createTexture3D(uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem)
  2261. {
  2262. BGFX_CHECK_MAIN_THREAD();
  2263. BX_CHECK(0 != (g_caps.supported & BGFX_CAPS_TEXTURE_3D), "Texture3D is not supported! Use bgfx::getCaps to check backend renderer capabilities.");
  2264. _numMips = uint8_t(bx::uint32_max(1, _numMips) );
  2265. if (BX_ENABLED(BGFX_CONFIG_DEBUG)
  2266. && NULL != _mem)
  2267. {
  2268. TextureInfo ti;
  2269. calcTextureSize(ti, _width, _height, _depth, false, _numMips, _format);
  2270. BX_CHECK(ti.storageSize == _mem->size
  2271. , "createTexture3D: Texture storage size doesn't match passed memory size (storage size: %d, memory size: %d)"
  2272. , ti.storageSize
  2273. , _mem->size
  2274. );
  2275. }
  2276. uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate);
  2277. const Memory* mem = alloc(size);
  2278. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  2279. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  2280. bx::write(&writer, magic);
  2281. TextureCreate tc;
  2282. tc.m_flags = _flags;
  2283. tc.m_width = _width;
  2284. tc.m_height = _height;
  2285. tc.m_sides = 0;
  2286. tc.m_depth = _depth;
  2287. tc.m_numMips = _numMips;
  2288. tc.m_format = uint8_t(_format);
  2289. tc.m_cubeMap = false;
  2290. tc.m_mem = _mem;
  2291. bx::write(&writer, tc);
  2292. return s_ctx->createTexture(mem, _flags, 0, NULL, BackbufferRatio::Count);
  2293. }
  2294. TextureHandle createTextureCube(uint16_t _size, uint8_t _numMips, TextureFormat::Enum _format, uint32_t _flags, const Memory* _mem)
  2295. {
  2296. BGFX_CHECK_MAIN_THREAD();
  2297. _numMips = uint8_t(bx::uint32_max(1, _numMips) );
  2298. if (BX_ENABLED(BGFX_CONFIG_DEBUG)
  2299. && NULL != _mem)
  2300. {
  2301. TextureInfo ti;
  2302. calcTextureSize(ti, _size, _size, 1, true, _numMips, _format);
  2303. BX_CHECK(ti.storageSize == _mem->size
  2304. , "createTextureCube: Texture storage size doesn't match passed memory size (storage size: %d, memory size: %d)"
  2305. , ti.storageSize
  2306. , _mem->size
  2307. );
  2308. }
  2309. uint32_t size = sizeof(uint32_t)+sizeof(TextureCreate);
  2310. const Memory* mem = alloc(size);
  2311. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  2312. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  2313. bx::write(&writer, magic);
  2314. TextureCreate tc;
  2315. tc.m_flags = _flags;
  2316. tc.m_width = _size;
  2317. tc.m_height = _size;
  2318. tc.m_sides = 6;
  2319. tc.m_depth = 0;
  2320. tc.m_numMips = _numMips;
  2321. tc.m_format = uint8_t(_format);
  2322. tc.m_cubeMap = true;
  2323. tc.m_mem = _mem;
  2324. bx::write(&writer, tc);
  2325. return s_ctx->createTexture(mem, _flags, 0, NULL, BackbufferRatio::Count);
  2326. }
  2327. void destroyTexture(TextureHandle _handle)
  2328. {
  2329. BGFX_CHECK_MAIN_THREAD();
  2330. s_ctx->destroyTexture(_handle);
  2331. }
  2332. void updateTexture2D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem, uint16_t _pitch)
  2333. {
  2334. BGFX_CHECK_MAIN_THREAD();
  2335. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2336. if (_width == 0
  2337. || _height == 0)
  2338. {
  2339. release(_mem);
  2340. }
  2341. else
  2342. {
  2343. s_ctx->updateTexture(_handle, 0, _mip, _x, _y, 0, _width, _height, 1, _pitch, _mem);
  2344. }
  2345. }
  2346. void updateTexture3D(TextureHandle _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const Memory* _mem)
  2347. {
  2348. BGFX_CHECK_MAIN_THREAD();
  2349. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2350. if (_width == 0
  2351. || _height == 0
  2352. || _depth == 0)
  2353. {
  2354. release(_mem);
  2355. }
  2356. else
  2357. {
  2358. s_ctx->updateTexture(_handle, 0, _mip, _x, _y, _z, _width, _height, _depth, UINT16_MAX, _mem);
  2359. }
  2360. }
  2361. void updateTextureCube(TextureHandle _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const Memory* _mem, uint16_t _pitch)
  2362. {
  2363. BGFX_CHECK_MAIN_THREAD();
  2364. BX_CHECK(NULL != _mem, "_mem can't be NULL");
  2365. BX_CHECK(_side <= 5, "Invalid side %d.", _side);
  2366. if (_width == 0
  2367. || _height == 0)
  2368. {
  2369. release(_mem);
  2370. }
  2371. else
  2372. {
  2373. s_ctx->updateTexture(_handle, _side, _mip, _x, _y, 0, _width, _height, 1, _pitch, _mem);
  2374. }
  2375. }
  2376. FrameBufferHandle createFrameBuffer(uint16_t _width, uint16_t _height, TextureFormat::Enum _format, uint32_t _textureFlags)
  2377. {
  2378. _textureFlags |= _textureFlags&BGFX_TEXTURE_RT_MSAA_MASK ? 0 : BGFX_TEXTURE_RT;
  2379. TextureHandle th = createTexture2D(_width, _height, 1, _format, _textureFlags);
  2380. return createFrameBuffer(1, &th, true);
  2381. }
  2382. FrameBufferHandle createFrameBuffer(BackbufferRatio::Enum _ratio, TextureFormat::Enum _format, uint32_t _textureFlags)
  2383. {
  2384. BX_CHECK(_ratio < BackbufferRatio::Count, "Invalid back buffer ratio.");
  2385. _textureFlags |= _textureFlags&BGFX_TEXTURE_RT_MSAA_MASK ? 0 : BGFX_TEXTURE_RT;
  2386. TextureHandle th = createTexture2D(_ratio, 1, _format, _textureFlags);
  2387. return createFrameBuffer(1, &th, true);
  2388. }
  2389. FrameBufferHandle createFrameBuffer(uint8_t _num, TextureHandle* _handles, bool _destroyTextures)
  2390. {
  2391. BGFX_CHECK_MAIN_THREAD();
  2392. BX_CHECK(_num != 0, "Number of frame buffer attachments can't be 0.");
  2393. BX_CHECK(_num <= BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS, "Number of frame buffer attachments is larger than allowed %d (max: %d)."
  2394. , _num
  2395. , BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS
  2396. );
  2397. BX_CHECK(NULL != _handles, "_handles can't be NULL");
  2398. return s_ctx->createFrameBuffer(_num, _handles, _destroyTextures);
  2399. }
  2400. FrameBufferHandle createFrameBuffer(void* _nwh, uint16_t _width, uint16_t _height, TextureFormat::Enum _depthFormat)
  2401. {
  2402. BGFX_CHECK_MAIN_THREAD();
  2403. return s_ctx->createFrameBuffer(_nwh, _width, _height, _depthFormat);
  2404. }
  2405. void destroyFrameBuffer(FrameBufferHandle _handle)
  2406. {
  2407. BGFX_CHECK_MAIN_THREAD();
  2408. s_ctx->destroyFrameBuffer(_handle);
  2409. }
  2410. UniformHandle createUniform(const char* _name, UniformType::Enum _type, uint16_t _num)
  2411. {
  2412. BGFX_CHECK_MAIN_THREAD();
  2413. return s_ctx->createUniform(_name, _type, _num);
  2414. }
  2415. void destroyUniform(UniformHandle _handle)
  2416. {
  2417. BGFX_CHECK_MAIN_THREAD();
  2418. s_ctx->destroyUniform(_handle);
  2419. }
  2420. void setPaletteColor(uint8_t _index, uint32_t _rgba)
  2421. {
  2422. BGFX_CHECK_MAIN_THREAD();
  2423. const uint8_t rr = uint8_t(_rgba>>24);
  2424. const uint8_t gg = uint8_t(_rgba>>16);
  2425. const uint8_t bb = uint8_t(_rgba>> 8);
  2426. const uint8_t aa = uint8_t(_rgba>> 0);
  2427. float rgba[4] =
  2428. {
  2429. rr * 1.0f/255.0f,
  2430. gg * 1.0f/255.0f,
  2431. bb * 1.0f/255.0f,
  2432. aa * 1.0f/255.0f,
  2433. };
  2434. s_ctx->setPaletteColor(_index, rgba);
  2435. }
  2436. void setPaletteColor(uint8_t _index, float _r, float _g, float _b, float _a)
  2437. {
  2438. BGFX_CHECK_MAIN_THREAD();
  2439. float rgba[4] = { _r, _g, _b, _a };
  2440. s_ctx->setPaletteColor(_index, rgba);
  2441. }
  2442. void setPaletteColor(uint8_t _index, const float _rgba[4])
  2443. {
  2444. BGFX_CHECK_MAIN_THREAD();
  2445. s_ctx->setPaletteColor(_index, _rgba);
  2446. }
  2447. bool checkView(uint8_t _id)
  2448. {
  2449. // workaround GCC 4.9 type-limit check.
  2450. const uint32_t id = _id;
  2451. return id < BGFX_CONFIG_MAX_VIEWS;
  2452. }
  2453. void setViewName(uint8_t _id, const char* _name)
  2454. {
  2455. BGFX_CHECK_MAIN_THREAD();
  2456. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2457. s_ctx->setViewName(_id, _name);
  2458. }
  2459. void setViewRect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
  2460. {
  2461. BGFX_CHECK_MAIN_THREAD();
  2462. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2463. s_ctx->setViewRect(_id, _x, _y, _width, _height);
  2464. }
  2465. void setViewScissor(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
  2466. {
  2467. BGFX_CHECK_MAIN_THREAD();
  2468. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2469. s_ctx->setViewScissor(_id, _x, _y, _width, _height);
  2470. }
  2471. void setViewClear(uint8_t _id, uint16_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
  2472. {
  2473. BGFX_CHECK_MAIN_THREAD();
  2474. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2475. s_ctx->setViewClear(_id, _flags, _rgba, _depth, _stencil);
  2476. }
  2477. void setViewClear(uint8_t _id, uint16_t _flags, float _depth, uint8_t _stencil, uint8_t _0, uint8_t _1, uint8_t _2, uint8_t _3, uint8_t _4, uint8_t _5, uint8_t _6, uint8_t _7)
  2478. {
  2479. BGFX_CHECK_MAIN_THREAD();
  2480. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2481. s_ctx->setViewClear(_id, _flags, _depth, _stencil, _0, _1, _2, _3, _4, _5, _6, _7);
  2482. }
  2483. void setViewSeq(uint8_t _id, bool _enabled)
  2484. {
  2485. BGFX_CHECK_MAIN_THREAD();
  2486. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2487. s_ctx->setViewSeq(_id, _enabled);
  2488. }
  2489. void setViewFrameBuffer(uint8_t _id, FrameBufferHandle _handle)
  2490. {
  2491. BGFX_CHECK_MAIN_THREAD();
  2492. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2493. s_ctx->setViewFrameBuffer(_id, _handle);
  2494. }
  2495. void setViewTransform(uint8_t _id, const void* _view, const void* _projL, uint8_t _flags, const void* _projR)
  2496. {
  2497. BGFX_CHECK_MAIN_THREAD();
  2498. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2499. s_ctx->setViewTransform(_id, _view, _projL, _flags, _projR);
  2500. }
  2501. void setViewRemap(uint8_t _id, uint8_t _num, const void* _remap)
  2502. {
  2503. BGFX_CHECK_MAIN_THREAD();
  2504. BX_CHECK(checkView(_id), "Invalid view id: %d", _id);
  2505. s_ctx->setViewRemap(_id, _num, _remap);
  2506. }
  2507. void setMarker(const char* _marker)
  2508. {
  2509. BGFX_CHECK_MAIN_THREAD();
  2510. s_ctx->setMarker(_marker);
  2511. }
  2512. void setState(uint64_t _state, uint32_t _rgba)
  2513. {
  2514. BGFX_CHECK_MAIN_THREAD();
  2515. s_ctx->setState(_state, _rgba);
  2516. }
  2517. void setStencil(uint32_t _fstencil, uint32_t _bstencil)
  2518. {
  2519. BGFX_CHECK_MAIN_THREAD();
  2520. s_ctx->setStencil(_fstencil, _bstencil);
  2521. }
  2522. uint16_t setScissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
  2523. {
  2524. BGFX_CHECK_MAIN_THREAD();
  2525. return s_ctx->setScissor(_x, _y, _width, _height);
  2526. }
  2527. void setScissor(uint16_t _cache)
  2528. {
  2529. BGFX_CHECK_MAIN_THREAD();
  2530. s_ctx->setScissor(_cache);
  2531. }
  2532. uint32_t setTransform(const void* _mtx, uint16_t _num)
  2533. {
  2534. BGFX_CHECK_MAIN_THREAD();
  2535. return s_ctx->setTransform(_mtx, _num);
  2536. }
  2537. uint32_t allocTransform(Transform* _transform, uint16_t _num)
  2538. {
  2539. BGFX_CHECK_MAIN_THREAD();
  2540. return s_ctx->allocTransform(_transform, _num);
  2541. }
  2542. void setTransform(uint32_t _cache, uint16_t _num)
  2543. {
  2544. BGFX_CHECK_MAIN_THREAD();
  2545. s_ctx->setTransform(_cache, _num);
  2546. }
  2547. void setUniform(UniformHandle _handle, const void* _value, uint16_t _num)
  2548. {
  2549. BGFX_CHECK_MAIN_THREAD();
  2550. s_ctx->setUniform(_handle, _value, _num);
  2551. }
  2552. void setIndexBuffer(IndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices)
  2553. {
  2554. BGFX_CHECK_MAIN_THREAD();
  2555. s_ctx->setIndexBuffer(_handle, _firstIndex, _numIndices);
  2556. }
  2557. void setIndexBuffer(DynamicIndexBufferHandle _handle, uint32_t _firstIndex, uint32_t _numIndices)
  2558. {
  2559. BGFX_CHECK_MAIN_THREAD();
  2560. s_ctx->setIndexBuffer(_handle, _firstIndex, _numIndices);
  2561. }
  2562. void setIndexBuffer(const TransientIndexBuffer* _tib)
  2563. {
  2564. setIndexBuffer(_tib, 0, UINT32_MAX);
  2565. }
  2566. void setIndexBuffer(const TransientIndexBuffer* _tib, uint32_t _firstIndex, uint32_t _numIndices)
  2567. {
  2568. BGFX_CHECK_MAIN_THREAD();
  2569. BX_CHECK(NULL != _tib, "_tib can't be NULL");
  2570. uint32_t numIndices = bx::uint32_min(_numIndices, _tib->size/2);
  2571. s_ctx->setIndexBuffer(_tib, _tib->startIndex + _firstIndex, numIndices);
  2572. }
  2573. void setVertexBuffer(VertexBufferHandle _handle)
  2574. {
  2575. setVertexBuffer(_handle, 0, UINT32_MAX);
  2576. }
  2577. void setVertexBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _numVertices)
  2578. {
  2579. BGFX_CHECK_MAIN_THREAD();
  2580. s_ctx->setVertexBuffer(_handle, _startVertex, _numVertices);
  2581. }
  2582. void setVertexBuffer(DynamicVertexBufferHandle _handle, uint32_t _numVertices)
  2583. {
  2584. BGFX_CHECK_MAIN_THREAD();
  2585. s_ctx->setVertexBuffer(_handle, _numVertices);
  2586. }
  2587. void setVertexBuffer(const TransientVertexBuffer* _tvb)
  2588. {
  2589. setVertexBuffer(_tvb, 0, UINT32_MAX);
  2590. }
  2591. void setVertexBuffer(const TransientVertexBuffer* _tvb, uint32_t _startVertex, uint32_t _numVertices)
  2592. {
  2593. BGFX_CHECK_MAIN_THREAD();
  2594. BX_CHECK(NULL != _tvb, "_tvb can't be NULL");
  2595. s_ctx->setVertexBuffer(_tvb, _startVertex, _numVertices);
  2596. }
  2597. void setInstanceDataBuffer(const InstanceDataBuffer* _idb, uint32_t _num)
  2598. {
  2599. BGFX_CHECK_MAIN_THREAD();
  2600. BX_CHECK(NULL != _idb, "_idb can't be NULL");
  2601. s_ctx->setInstanceDataBuffer(_idb, _num);
  2602. }
  2603. void setInstanceDataBuffer(VertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num)
  2604. {
  2605. BGFX_CHECK_MAIN_THREAD();
  2606. s_ctx->setInstanceDataBuffer(_handle, _startVertex, _num);
  2607. }
  2608. void setInstanceDataBuffer(DynamicVertexBufferHandle _handle, uint32_t _startVertex, uint32_t _num)
  2609. {
  2610. BGFX_CHECK_MAIN_THREAD();
  2611. s_ctx->setInstanceDataBuffer(_handle, _startVertex, _num);
  2612. }
  2613. void setTexture(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint32_t _flags)
  2614. {
  2615. BGFX_CHECK_MAIN_THREAD();
  2616. s_ctx->setTexture(_stage, _sampler, _handle, _flags);
  2617. }
  2618. void setTexture(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment, uint32_t _flags)
  2619. {
  2620. BGFX_CHECK_MAIN_THREAD();
  2621. s_ctx->setTexture(_stage, _sampler, _handle, _attachment, _flags);
  2622. }
  2623. uint32_t touch(uint8_t _id)
  2624. {
  2625. ProgramHandle handle = BGFX_INVALID_HANDLE;
  2626. return submit(_id, handle);
  2627. }
  2628. uint32_t submit(uint8_t _id, ProgramHandle _handle, int32_t _depth)
  2629. {
  2630. BGFX_CHECK_MAIN_THREAD();
  2631. return s_ctx->submit(_id, _handle, _depth);
  2632. }
  2633. uint32_t submit(uint8_t _id, ProgramHandle _handle, IndirectBufferHandle _indirectHandle, uint16_t _start, uint16_t _num, int32_t _depth)
  2634. {
  2635. BGFX_CHECK_MAIN_THREAD();
  2636. return s_ctx->submit(_id, _handle, _indirectHandle, _start, _num, _depth);
  2637. }
  2638. void setBuffer(uint8_t _stage, IndexBufferHandle _handle, Access::Enum _access)
  2639. {
  2640. BGFX_CHECK_MAIN_THREAD();
  2641. s_ctx->setBuffer(_stage, _handle, _access);
  2642. }
  2643. void setBuffer(uint8_t _stage, VertexBufferHandle _handle, Access::Enum _access)
  2644. {
  2645. BGFX_CHECK_MAIN_THREAD();
  2646. s_ctx->setBuffer(_stage, _handle, _access);
  2647. }
  2648. void setBuffer(uint8_t _stage, DynamicIndexBufferHandle _handle, Access::Enum _access)
  2649. {
  2650. BGFX_CHECK_MAIN_THREAD();
  2651. s_ctx->setBuffer(_stage, _handle, _access);
  2652. }
  2653. void setBuffer(uint8_t _stage, DynamicVertexBufferHandle _handle, Access::Enum _access)
  2654. {
  2655. BGFX_CHECK_MAIN_THREAD();
  2656. s_ctx->setBuffer(_stage, _handle, _access);
  2657. }
  2658. void setBuffer(uint8_t _stage, IndirectBufferHandle _handle, Access::Enum _access)
  2659. {
  2660. BGFX_CHECK_MAIN_THREAD();
  2661. s_ctx->setBuffer(_stage, _handle, _access);
  2662. }
  2663. void setImage(uint8_t _stage, UniformHandle _sampler, TextureHandle _handle, uint8_t _mip, Access::Enum _access, TextureFormat::Enum _format)
  2664. {
  2665. BGFX_CHECK_MAIN_THREAD();
  2666. s_ctx->setImage(_stage, _sampler, _handle, _mip, _access, _format);
  2667. }
  2668. void setImage(uint8_t _stage, UniformHandle _sampler, FrameBufferHandle _handle, uint8_t _attachment, Access::Enum _access, TextureFormat::Enum _format)
  2669. {
  2670. BGFX_CHECK_MAIN_THREAD();
  2671. s_ctx->setImage(_stage, _sampler, _handle, _attachment, _access, _format);
  2672. }
  2673. uint32_t dispatch(uint8_t _id, ProgramHandle _handle, uint16_t _numX, uint16_t _numY, uint16_t _numZ, uint8_t _flags)
  2674. {
  2675. BGFX_CHECK_MAIN_THREAD();
  2676. return s_ctx->dispatch(_id, _handle, _numX, _numY, _numZ, _flags);
  2677. }
  2678. uint32_t dispatch(uint8_t _id, ProgramHandle _handle, IndirectBufferHandle _indirectHandle, uint16_t _start, uint16_t _num, uint8_t _flags)
  2679. {
  2680. BGFX_CHECK_MAIN_THREAD();
  2681. return s_ctx->dispatch(_id, _handle, _indirectHandle, _start, _num, _flags);
  2682. }
  2683. void discard()
  2684. {
  2685. BGFX_CHECK_MAIN_THREAD();
  2686. s_ctx->discard();
  2687. }
  2688. void saveScreenShot(const char* _filePath)
  2689. {
  2690. BGFX_CHECK_MAIN_THREAD();
  2691. s_ctx->saveScreenShot(_filePath);
  2692. }
  2693. } // namespace bgfx
  2694. #include <bgfx/c99/bgfx.h>
  2695. #include <bgfx/c99/bgfxplatform.h>
  2696. BX_STATIC_ASSERT(bgfx::Fatal::Count == bgfx::Fatal::Enum(BGFX_FATAL_COUNT) );
  2697. BX_STATIC_ASSERT(bgfx::RendererType::Count == bgfx::RendererType::Enum(BGFX_RENDERER_TYPE_COUNT) );
  2698. BX_STATIC_ASSERT(bgfx::Attrib::Count == bgfx::Attrib::Enum(BGFX_ATTRIB_COUNT) );
  2699. BX_STATIC_ASSERT(bgfx::AttribType::Count == bgfx::AttribType::Enum(BGFX_ATTRIB_TYPE_COUNT) );
  2700. BX_STATIC_ASSERT(bgfx::TextureFormat::Count == bgfx::TextureFormat::Enum(BGFX_TEXTURE_FORMAT_COUNT) );
  2701. BX_STATIC_ASSERT(bgfx::UniformType::Count == bgfx::UniformType::Enum(BGFX_UNIFORM_TYPE_COUNT) );
  2702. BX_STATIC_ASSERT(bgfx::RenderFrame::Count == bgfx::RenderFrame::Enum(BGFX_RENDER_FRAME_COUNT) );
  2703. BX_STATIC_ASSERT(sizeof(bgfx::Memory) == sizeof(bgfx_memory_t) );
  2704. BX_STATIC_ASSERT(sizeof(bgfx::VertexDecl) == sizeof(bgfx_vertex_decl_t) );
  2705. BX_STATIC_ASSERT(sizeof(bgfx::TransientIndexBuffer) == sizeof(bgfx_transient_index_buffer_t) );
  2706. BX_STATIC_ASSERT(sizeof(bgfx::TransientVertexBuffer) == sizeof(bgfx_transient_vertex_buffer_t) );
  2707. BX_STATIC_ASSERT(sizeof(bgfx::InstanceDataBuffer) == sizeof(bgfx_instance_data_buffer_t) );
  2708. BX_STATIC_ASSERT(sizeof(bgfx::TextureInfo) == sizeof(bgfx_texture_info_t) );
  2709. BX_STATIC_ASSERT(sizeof(bgfx::Caps) == sizeof(bgfx_caps_t) );
  2710. BX_STATIC_ASSERT(sizeof(bgfx::PlatformData) == sizeof(bgfx_platform_data_t) );
  2711. namespace bgfx
  2712. {
  2713. struct CallbackC99 : public CallbackI
  2714. {
  2715. virtual ~CallbackC99()
  2716. {
  2717. }
  2718. virtual void fatal(Fatal::Enum _code, const char* _str) BX_OVERRIDE
  2719. {
  2720. m_interface->vtbl->fatal(m_interface, (bgfx_fatal_t)_code, _str);
  2721. }
  2722. virtual void traceVargs(const char* _filePath, uint16_t _line, const char* _format, va_list _argList) BX_OVERRIDE
  2723. {
  2724. m_interface->vtbl->trace_vargs(m_interface, _filePath, _line, _format, _argList);
  2725. }
  2726. virtual uint32_t cacheReadSize(uint64_t _id) BX_OVERRIDE
  2727. {
  2728. return m_interface->vtbl->cache_read_size(m_interface, _id);
  2729. }
  2730. virtual bool cacheRead(uint64_t _id, void* _data, uint32_t _size) BX_OVERRIDE
  2731. {
  2732. return m_interface->vtbl->cache_read(m_interface, _id, _data, _size);
  2733. }
  2734. virtual void cacheWrite(uint64_t _id, const void* _data, uint32_t _size) BX_OVERRIDE
  2735. {
  2736. m_interface->vtbl->cache_write(m_interface, _id, _data, _size);
  2737. }
  2738. virtual void screenShot(const char* _filePath, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _data, uint32_t _size, bool _yflip) BX_OVERRIDE
  2739. {
  2740. m_interface->vtbl->screen_shot(m_interface, _filePath, _width, _height, _pitch, _data, _size, _yflip);
  2741. }
  2742. virtual void captureBegin(uint32_t _width, uint32_t _height, uint32_t _pitch, TextureFormat::Enum _format, bool _yflip) BX_OVERRIDE
  2743. {
  2744. m_interface->vtbl->capture_begin(m_interface, _width, _height, _pitch, (bgfx_texture_format_t)_format, _yflip);
  2745. }
  2746. virtual void captureEnd() BX_OVERRIDE
  2747. {
  2748. m_interface->vtbl->capture_end(m_interface);
  2749. }
  2750. virtual void captureFrame(const void* _data, uint32_t _size) BX_OVERRIDE
  2751. {
  2752. m_interface->vtbl->capture_frame(m_interface, _data, _size);
  2753. }
  2754. bgfx_callback_interface_t* m_interface;
  2755. };
  2756. class AllocatorC99 : public bx::ReallocatorI
  2757. {
  2758. public:
  2759. virtual ~AllocatorC99()
  2760. {
  2761. }
  2762. virtual void* alloc(size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  2763. {
  2764. return m_interface->vtbl->alloc(m_interface, _size, _align, _file, _line);
  2765. }
  2766. virtual void free(void* _ptr, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  2767. {
  2768. m_interface->vtbl->free(m_interface, _ptr, _align, _file, _line);
  2769. }
  2770. virtual void* realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line) BX_OVERRIDE
  2771. {
  2772. return m_interface->vtbl->realloc(m_interface, _ptr, _size, _align, _file, _line);
  2773. }
  2774. bgfx_reallocator_interface_t* m_interface;
  2775. };
  2776. } // namespace bgfx
  2777. BGFX_C_API void bgfx_vertex_decl_begin(bgfx_vertex_decl_t* _decl, bgfx_renderer_type_t _renderer)
  2778. {
  2779. bgfx::VertexDecl* decl = (bgfx::VertexDecl*)_decl;
  2780. decl->begin(bgfx::RendererType::Enum(_renderer) );
  2781. }
  2782. BGFX_C_API void bgfx_vertex_decl_add(bgfx_vertex_decl_t* _decl, bgfx_attrib_t _attrib, uint8_t _num, bgfx_attrib_type_t _type, bool _normalized, bool _asInt)
  2783. {
  2784. bgfx::VertexDecl* decl = (bgfx::VertexDecl*)_decl;
  2785. decl->add(bgfx::Attrib::Enum(_attrib)
  2786. , _num
  2787. , bgfx::AttribType::Enum(_type)
  2788. , _normalized
  2789. , _asInt
  2790. );
  2791. }
  2792. BGFX_C_API void bgfx_vertex_decl_skip(bgfx_vertex_decl_t* _decl, uint8_t _num)
  2793. {
  2794. bgfx::VertexDecl* decl = (bgfx::VertexDecl*)_decl;
  2795. decl->skip(_num);
  2796. }
  2797. BGFX_C_API void bgfx_vertex_decl_end(bgfx_vertex_decl_t* _decl)
  2798. {
  2799. bgfx::VertexDecl* decl = (bgfx::VertexDecl*)_decl;
  2800. decl->end();
  2801. }
  2802. BGFX_C_API void bgfx_vertex_pack(const float _input[4], bool _inputNormalized, bgfx_attrib_t _attr, const bgfx_vertex_decl_t* _decl, void* _data, uint32_t _index)
  2803. {
  2804. bgfx::VertexDecl& decl = *(bgfx::VertexDecl*)_decl;
  2805. bgfx::vertexPack(_input, _inputNormalized, bgfx::Attrib::Enum(_attr), decl, _data, _index);
  2806. }
  2807. BGFX_C_API void bgfx_vertex_unpack(float _output[4], bgfx_attrib_t _attr, const bgfx_vertex_decl_t* _decl, const void* _data, uint32_t _index)
  2808. {
  2809. bgfx::VertexDecl& decl = *(bgfx::VertexDecl*)_decl;
  2810. bgfx::vertexUnpack(_output, bgfx::Attrib::Enum(_attr), decl, _data, _index);
  2811. }
  2812. BGFX_C_API void bgfx_vertex_convert(const bgfx_vertex_decl_t* _destDecl, void* _destData, const bgfx_vertex_decl_t* _srcDecl, const void* _srcData, uint32_t _num)
  2813. {
  2814. bgfx::VertexDecl& destDecl = *(bgfx::VertexDecl*)_destDecl;
  2815. bgfx::VertexDecl& srcDecl = *(bgfx::VertexDecl*)_srcDecl;
  2816. bgfx::vertexConvert(destDecl, _destData, srcDecl, _srcData, _num);
  2817. }
  2818. BGFX_C_API uint16_t bgfx_weld_vertices(uint16_t* _output, const bgfx_vertex_decl_t* _decl, const void* _data, uint16_t _num, float _epsilon)
  2819. {
  2820. bgfx::VertexDecl& decl = *(bgfx::VertexDecl*)_decl;
  2821. return bgfx::weldVertices(_output, decl, _data, _num, _epsilon);
  2822. }
  2823. BGFX_C_API void bgfx_image_swizzle_bgra8(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
  2824. {
  2825. bgfx::imageSwizzleBgra8(_width, _height, _pitch, _src, _dst);
  2826. }
  2827. BGFX_C_API void bgfx_image_rgba8_downsample_2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
  2828. {
  2829. bgfx::imageRgba8Downsample2x2(_width, _height, _pitch, _src, _dst);
  2830. }
  2831. BGFX_C_API uint8_t bgfx_get_supported_renderers(bgfx_renderer_type_t _enum[BGFX_RENDERER_TYPE_COUNT])
  2832. {
  2833. return bgfx::getSupportedRenderers( (bgfx::RendererType::Enum*)_enum);
  2834. }
  2835. BGFX_C_API const char* bgfx_get_renderer_name(bgfx_renderer_type_t _type)
  2836. {
  2837. return bgfx::getRendererName(bgfx::RendererType::Enum(_type) );
  2838. }
  2839. BGFX_C_API bool bgfx_init(bgfx_renderer_type_t _type, uint16_t _vendorId, uint16_t _deviceId, bgfx_callback_interface_t* _callback, bgfx_reallocator_interface_t* _allocator)
  2840. {
  2841. static bgfx::CallbackC99 s_callback;
  2842. s_callback.m_interface = _callback;
  2843. static bgfx::AllocatorC99 s_allocator;
  2844. s_allocator.m_interface = _allocator;
  2845. return bgfx::init(bgfx::RendererType::Enum(_type)
  2846. , _vendorId
  2847. , _deviceId
  2848. , NULL == _callback ? NULL : &s_callback
  2849. , NULL == _allocator ? NULL : &s_allocator
  2850. );
  2851. }
  2852. BGFX_C_API void bgfx_shutdown()
  2853. {
  2854. return bgfx::shutdown();
  2855. }
  2856. BGFX_C_API void bgfx_reset(uint32_t _width, uint32_t _height, uint32_t _flags)
  2857. {
  2858. bgfx::reset(_width, _height, _flags);
  2859. }
  2860. BGFX_C_API uint32_t bgfx_frame()
  2861. {
  2862. return bgfx::frame();
  2863. }
  2864. BGFX_C_API bgfx_renderer_type_t bgfx_get_renderer_type()
  2865. {
  2866. return bgfx_renderer_type_t(bgfx::getRendererType() );
  2867. }
  2868. BGFX_C_API const bgfx_caps_t* bgfx_get_caps()
  2869. {
  2870. return (const bgfx_caps_t*)bgfx::getCaps();
  2871. }
  2872. BGFX_C_API const bgfx_hmd_t* bgfx_get_hmd()
  2873. {
  2874. return (const bgfx_hmd_t*)bgfx::getHMD();
  2875. }
  2876. BGFX_C_API const bgfx_stats_t* bgfx_get_stats()
  2877. {
  2878. return (const bgfx_stats_t*)bgfx::getStats();
  2879. }
  2880. BGFX_C_API const bgfx_memory_t* bgfx_alloc(uint32_t _size)
  2881. {
  2882. return (const bgfx_memory_t*)bgfx::alloc(_size);
  2883. }
  2884. BGFX_C_API const bgfx_memory_t* bgfx_copy(const void* _data, uint32_t _size)
  2885. {
  2886. return (const bgfx_memory_t*)bgfx::copy(_data, _size);
  2887. }
  2888. BGFX_C_API const bgfx_memory_t* bgfx_make_ref(const void* _data, uint32_t _size)
  2889. {
  2890. return (const bgfx_memory_t*)bgfx::makeRef(_data, _size);
  2891. }
  2892. BGFX_C_API const bgfx_memory_t* bgfx_make_ref_release(const void* _data, uint32_t _size, bgfx_release_fn_t _releaseFn, void* _userData)
  2893. {
  2894. return (const bgfx_memory_t*)bgfx::makeRef(_data, _size, _releaseFn, _userData);
  2895. }
  2896. BGFX_C_API void bgfx_set_debug(uint32_t _debug)
  2897. {
  2898. bgfx::setDebug(_debug);
  2899. }
  2900. BGFX_C_API void bgfx_dbg_text_clear(uint8_t _attr, bool _small)
  2901. {
  2902. bgfx::dbgTextClear(_attr, _small);
  2903. }
  2904. BGFX_C_API void bgfx_dbg_text_printf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...)
  2905. {
  2906. va_list argList;
  2907. va_start(argList, _format);
  2908. bgfx::dbgTextPrintfVargs(_x, _y, _attr, _format, argList);
  2909. va_end(argList);
  2910. }
  2911. BGFX_C_API void bgfx_dbg_text_image(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const void* _data, uint16_t _pitch)
  2912. {
  2913. bgfx::dbgTextImage(_x, _y, _width, _height, _data, _pitch);
  2914. }
  2915. BGFX_C_API bgfx_index_buffer_handle_t bgfx_create_index_buffer(const bgfx_memory_t* _mem, uint16_t _flags)
  2916. {
  2917. union { bgfx_index_buffer_handle_t c; bgfx::IndexBufferHandle cpp; } handle;
  2918. handle.cpp = bgfx::createIndexBuffer( (const bgfx::Memory*)_mem, _flags);
  2919. return handle.c;
  2920. }
  2921. BGFX_C_API void bgfx_destroy_index_buffer(bgfx_index_buffer_handle_t _handle)
  2922. {
  2923. union { bgfx_index_buffer_handle_t c; bgfx::IndexBufferHandle cpp; } handle = { _handle };
  2924. bgfx::destroyIndexBuffer(handle.cpp);
  2925. }
  2926. BGFX_C_API bgfx_vertex_buffer_handle_t bgfx_create_vertex_buffer(const bgfx_memory_t* _mem, const bgfx_vertex_decl_t* _decl, uint16_t _flags)
  2927. {
  2928. const bgfx::VertexDecl& decl = *(const bgfx::VertexDecl*)_decl;
  2929. union { bgfx_vertex_buffer_handle_t c; bgfx::VertexBufferHandle cpp; } handle;
  2930. handle.cpp = bgfx::createVertexBuffer( (const bgfx::Memory*)_mem, decl, _flags);
  2931. return handle.c;
  2932. }
  2933. BGFX_C_API void bgfx_destroy_vertex_buffer(bgfx_vertex_buffer_handle_t _handle)
  2934. {
  2935. union { bgfx_vertex_buffer_handle_t c; bgfx::VertexBufferHandle cpp; } handle = { _handle };
  2936. bgfx::destroyVertexBuffer(handle.cpp);
  2937. }
  2938. BGFX_C_API bgfx_dynamic_index_buffer_handle_t bgfx_create_dynamic_index_buffer(uint32_t _num, uint16_t _flags)
  2939. {
  2940. union { bgfx_dynamic_index_buffer_handle_t c; bgfx::DynamicIndexBufferHandle cpp; } handle;
  2941. handle.cpp = bgfx::createDynamicIndexBuffer(_num, _flags);
  2942. return handle.c;
  2943. }
  2944. BGFX_C_API bgfx_dynamic_index_buffer_handle_t bgfx_create_dynamic_index_buffer_mem(const bgfx_memory_t* _mem, uint16_t _flags)
  2945. {
  2946. union { bgfx_dynamic_index_buffer_handle_t c; bgfx::DynamicIndexBufferHandle cpp; } handle;
  2947. handle.cpp = bgfx::createDynamicIndexBuffer( (const bgfx::Memory*)_mem, _flags);
  2948. return handle.c;
  2949. }
  2950. BGFX_C_API void bgfx_update_dynamic_index_buffer(bgfx_dynamic_index_buffer_handle_t _handle, uint32_t _startIndex, const bgfx_memory_t* _mem)
  2951. {
  2952. union { bgfx_dynamic_index_buffer_handle_t c; bgfx::DynamicIndexBufferHandle cpp; } handle = { _handle };
  2953. bgfx::updateDynamicIndexBuffer(handle.cpp, _startIndex, (const bgfx::Memory*)_mem);
  2954. }
  2955. BGFX_C_API void bgfx_destroy_dynamic_index_buffer(bgfx_dynamic_index_buffer_handle_t _handle)
  2956. {
  2957. union { bgfx_dynamic_index_buffer_handle_t c; bgfx::DynamicIndexBufferHandle cpp; } handle = { _handle };
  2958. bgfx::destroyDynamicIndexBuffer(handle.cpp);
  2959. }
  2960. BGFX_C_API bgfx_dynamic_vertex_buffer_handle_t bgfx_create_dynamic_vertex_buffer(uint32_t _num, const bgfx_vertex_decl_t* _decl, uint16_t _flags)
  2961. {
  2962. const bgfx::VertexDecl& decl = *(const bgfx::VertexDecl*)_decl;
  2963. union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle;
  2964. handle.cpp = bgfx::createDynamicVertexBuffer(_num, decl, _flags);
  2965. return handle.c;
  2966. }
  2967. BGFX_C_API bgfx_dynamic_vertex_buffer_handle_t bgfx_create_dynamic_vertex_buffer_mem(const bgfx_memory_t* _mem, const bgfx_vertex_decl_t* _decl, uint16_t _flags)
  2968. {
  2969. const bgfx::VertexDecl& decl = *(const bgfx::VertexDecl*)_decl;
  2970. union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle;
  2971. handle.cpp = bgfx::createDynamicVertexBuffer( (const bgfx::Memory*)_mem, decl, _flags);
  2972. return handle.c;
  2973. }
  2974. BGFX_C_API void bgfx_update_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _startVertex, const bgfx_memory_t* _mem)
  2975. {
  2976. union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle = { _handle };
  2977. bgfx::updateDynamicVertexBuffer(handle.cpp, _startVertex, (const bgfx::Memory*)_mem);
  2978. }
  2979. BGFX_C_API void bgfx_destroy_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle)
  2980. {
  2981. union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle = { _handle };
  2982. bgfx::destroyDynamicVertexBuffer(handle.cpp);
  2983. }
  2984. BGFX_C_API bool bgfx_check_avail_transient_index_buffer(uint32_t _num)
  2985. {
  2986. return bgfx::checkAvailTransientIndexBuffer(_num);
  2987. }
  2988. BGFX_C_API bool bgfx_check_avail_transient_vertex_buffer(uint32_t _num, const bgfx_vertex_decl_t* _decl)
  2989. {
  2990. const bgfx::VertexDecl& decl = *(const bgfx::VertexDecl*)_decl;
  2991. return bgfx::checkAvailTransientVertexBuffer(_num, decl);
  2992. }
  2993. BGFX_C_API bool bgfx_check_avail_instance_data_buffer(uint32_t _num, uint16_t _stride)
  2994. {
  2995. return bgfx::checkAvailInstanceDataBuffer(_num, _stride);
  2996. }
  2997. BGFX_C_API bool bgfx_check_avail_transient_buffers(uint32_t _numVertices, const bgfx_vertex_decl_t* _decl, uint32_t _numIndices)
  2998. {
  2999. const bgfx::VertexDecl& decl = *(const bgfx::VertexDecl*)_decl;
  3000. return bgfx::checkAvailTransientBuffers(_numVertices, decl, _numIndices);
  3001. }
  3002. BGFX_C_API void bgfx_alloc_transient_index_buffer(bgfx_transient_index_buffer_t* _tib, uint32_t _num)
  3003. {
  3004. bgfx::allocTransientIndexBuffer( (bgfx::TransientIndexBuffer*)_tib, _num);
  3005. }
  3006. BGFX_C_API void bgfx_alloc_transient_vertex_buffer(bgfx_transient_vertex_buffer_t* _tvb, uint32_t _num, const bgfx_vertex_decl_t* _decl)
  3007. {
  3008. const bgfx::VertexDecl& decl = *(const bgfx::VertexDecl*)_decl;
  3009. bgfx::allocTransientVertexBuffer( (bgfx::TransientVertexBuffer*)_tvb, _num, decl);
  3010. }
  3011. BGFX_C_API bool bgfx_alloc_transient_buffers(bgfx_transient_vertex_buffer_t* _tvb, const bgfx_vertex_decl_t* _decl, uint32_t _numVertices, bgfx_transient_index_buffer_t* _tib, uint32_t _numIndices)
  3012. {
  3013. const bgfx::VertexDecl& decl = *(const bgfx::VertexDecl*)_decl;
  3014. return bgfx::allocTransientBuffers( (bgfx::TransientVertexBuffer*)_tvb, decl, _numVertices, (bgfx::TransientIndexBuffer*)_tib, _numIndices);
  3015. }
  3016. BGFX_C_API const bgfx_instance_data_buffer_t* bgfx_alloc_instance_data_buffer(uint32_t _num, uint16_t _stride)
  3017. {
  3018. return (bgfx_instance_data_buffer_t*)bgfx::allocInstanceDataBuffer(_num, _stride);
  3019. }
  3020. BGFX_C_API bgfx_indirect_buffer_handle_t bgfx_create_indirect_buffer(uint32_t _num)
  3021. {
  3022. union { bgfx_indirect_buffer_handle_t c; bgfx::IndirectBufferHandle cpp; } handle;
  3023. handle.cpp = bgfx::createIndirectBuffer(_num);
  3024. return handle.c;
  3025. }
  3026. BGFX_C_API void bgfx_destroy_indirect_buffer(bgfx_indirect_buffer_handle_t _handle)
  3027. {
  3028. union { bgfx_indirect_buffer_handle_t c; bgfx::IndirectBufferHandle cpp; } handle = { _handle };
  3029. bgfx::destroyIndirectBuffer(handle.cpp);
  3030. }
  3031. BGFX_C_API bgfx_shader_handle_t bgfx_create_shader(const bgfx_memory_t* _mem)
  3032. {
  3033. union { bgfx_shader_handle_t c; bgfx::ShaderHandle cpp; } handle;
  3034. handle.cpp = bgfx::createShader( (const bgfx::Memory*)_mem);
  3035. return handle.c;
  3036. }
  3037. BGFX_C_API uint16_t bgfx_get_shader_uniforms(bgfx_shader_handle_t _handle, bgfx_uniform_handle_t* _uniforms, uint16_t _max)
  3038. {
  3039. union { bgfx_shader_handle_t c; bgfx::ShaderHandle cpp; } handle = { _handle };
  3040. return bgfx::getShaderUniforms(handle.cpp, (bgfx::UniformHandle*)_uniforms, _max);
  3041. }
  3042. BGFX_C_API void bgfx_destroy_shader(bgfx_shader_handle_t _handle)
  3043. {
  3044. union { bgfx_shader_handle_t c; bgfx::ShaderHandle cpp; } handle = { _handle };
  3045. bgfx::destroyShader(handle.cpp);
  3046. }
  3047. BGFX_C_API bgfx_program_handle_t bgfx_create_program(bgfx_shader_handle_t _vsh, bgfx_shader_handle_t _fsh, bool _destroyShaders)
  3048. {
  3049. union { bgfx_shader_handle_t c; bgfx::ShaderHandle cpp; } vsh = { _vsh };
  3050. union { bgfx_shader_handle_t c; bgfx::ShaderHandle cpp; } fsh = { _fsh };
  3051. union { bgfx_program_handle_t c; bgfx::ProgramHandle cpp; } handle;
  3052. handle.cpp = bgfx::createProgram(vsh.cpp, fsh.cpp, _destroyShaders);
  3053. return handle.c;
  3054. }
  3055. BGFX_C_API bgfx_program_handle_t bgfx_create_compute_program(bgfx_shader_handle_t _csh, bool _destroyShaders)
  3056. {
  3057. union { bgfx_shader_handle_t c; bgfx::ShaderHandle cpp; } csh = { _csh };
  3058. union { bgfx_program_handle_t c; bgfx::ProgramHandle cpp; } handle;
  3059. handle.cpp = bgfx::createProgram(csh.cpp, _destroyShaders);
  3060. return handle.c;
  3061. }
  3062. BGFX_C_API void bgfx_destroy_program(bgfx_program_handle_t _handle)
  3063. {
  3064. union { bgfx_program_handle_t c; bgfx::ProgramHandle cpp; } handle = { _handle };
  3065. bgfx::destroyProgram(handle.cpp);
  3066. }
  3067. BGFX_C_API void bgfx_calc_texture_size(bgfx_texture_info_t* _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, uint8_t _numMips, bgfx_texture_format_t _format)
  3068. {
  3069. bgfx::TextureInfo& info = *(bgfx::TextureInfo*)_info;
  3070. bgfx::calcTextureSize(info, _width, _height, _depth, _cubeMap, _numMips, bgfx::TextureFormat::Enum(_format) );
  3071. }
  3072. BGFX_C_API bgfx_texture_handle_t bgfx_create_texture(const bgfx_memory_t* _mem, uint32_t _flags, uint8_t _skip, bgfx_texture_info_t* _info)
  3073. {
  3074. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle;
  3075. bgfx::TextureInfo* info = (bgfx::TextureInfo*)_info;
  3076. handle.cpp = bgfx::createTexture( (const bgfx::Memory*)_mem, _flags, _skip, info);
  3077. return handle.c;
  3078. }
  3079. BGFX_C_API bgfx_texture_handle_t bgfx_create_texture_2d(uint16_t _width, uint16_t _height, uint8_t _numMips, bgfx_texture_format_t _format, uint32_t _flags, const bgfx_memory_t* _mem)
  3080. {
  3081. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle;
  3082. handle.cpp = bgfx::createTexture2D(_width, _height, _numMips, bgfx::TextureFormat::Enum(_format), _flags, (const bgfx::Memory*)_mem);
  3083. return handle.c;
  3084. }
  3085. BGFX_C_API bgfx_texture_handle_t bgfx_create_texture_2d_scaled(bgfx_backbuffer_ratio_t _ratio, uint8_t _numMips, bgfx_texture_format_t _format, uint32_t _flags)
  3086. {
  3087. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle;
  3088. handle.cpp = bgfx::createTexture2D(bgfx::BackbufferRatio::Enum(_ratio), _numMips, bgfx::TextureFormat::Enum(_format), _flags);
  3089. return handle.c;
  3090. }
  3091. BGFX_C_API bgfx_texture_handle_t bgfx_create_texture_3d(uint16_t _width, uint16_t _height, uint16_t _depth, uint8_t _numMips, bgfx_texture_format_t _format, uint32_t _flags, const bgfx_memory_t* _mem)
  3092. {
  3093. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle;
  3094. handle.cpp = bgfx::createTexture3D(_width, _height, _depth, _numMips, bgfx::TextureFormat::Enum(_format), _flags, (const bgfx::Memory*)_mem);
  3095. return handle.c;
  3096. }
  3097. BGFX_C_API bgfx_texture_handle_t bgfx_create_texture_cube(uint16_t _size, uint8_t _numMips, bgfx_texture_format_t _format, uint32_t _flags, const bgfx_memory_t* _mem)
  3098. {
  3099. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle;
  3100. handle.cpp = bgfx::createTextureCube(_size, _numMips, bgfx::TextureFormat::Enum(_format), _flags, (const bgfx::Memory*)_mem);
  3101. return handle.c;
  3102. }
  3103. BGFX_C_API void bgfx_update_texture_2d(bgfx_texture_handle_t _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const bgfx_memory_t* _mem, uint16_t _pitch)
  3104. {
  3105. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle = { _handle };
  3106. bgfx::updateTexture2D(handle.cpp, _mip, _x, _y, _width, _height, (const bgfx::Memory*)_mem, _pitch);
  3107. }
  3108. BGFX_C_API void bgfx_update_texture_3d(bgfx_texture_handle_t _handle, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _z, uint16_t _width, uint16_t _height, uint16_t _depth, const bgfx_memory_t* _mem)
  3109. {
  3110. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle = { _handle };
  3111. bgfx::updateTexture3D(handle.cpp, _mip, _x, _y, _z, _width, _height, _depth, (const bgfx::Memory*)_mem);
  3112. }
  3113. BGFX_C_API void bgfx_update_texture_cube(bgfx_texture_handle_t _handle, uint8_t _side, uint8_t _mip, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height, const bgfx_memory_t* _mem, uint16_t _pitch)
  3114. {
  3115. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle = { _handle };
  3116. bgfx::updateTextureCube(handle.cpp, _side, _mip, _x, _y, _width, _height, (const bgfx::Memory*)_mem, _pitch);
  3117. }
  3118. BGFX_C_API void bgfx_destroy_texture(bgfx_texture_handle_t _handle)
  3119. {
  3120. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle = { _handle };
  3121. bgfx::destroyTexture(handle.cpp);
  3122. }
  3123. BGFX_C_API bgfx_frame_buffer_handle_t bgfx_create_frame_buffer(uint16_t _width, uint16_t _height, bgfx_texture_format_t _format, uint32_t _textureFlags)
  3124. {
  3125. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle;
  3126. handle.cpp = bgfx::createFrameBuffer(_width, _height, bgfx::TextureFormat::Enum(_format), _textureFlags);
  3127. return handle.c;
  3128. }
  3129. BGFX_C_API bgfx_frame_buffer_handle_t bgfx_create_frame_buffer_scaled(bgfx_backbuffer_ratio_t _ratio, bgfx_texture_format_t _format, uint32_t _textureFlags)
  3130. {
  3131. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle;
  3132. handle.cpp = bgfx::createFrameBuffer(bgfx::BackbufferRatio::Enum(_ratio), bgfx::TextureFormat::Enum(_format), _textureFlags);
  3133. return handle.c;
  3134. }
  3135. BGFX_C_API bgfx_frame_buffer_handle_t bgfx_create_frame_buffer_from_handles(uint8_t _num, bgfx_texture_handle_t* _handles, bool _destroyTextures)
  3136. {
  3137. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle;
  3138. handle.cpp = bgfx::createFrameBuffer(_num, (bgfx::TextureHandle*)_handles, _destroyTextures);
  3139. return handle.c;
  3140. }
  3141. BGFX_C_API bgfx_frame_buffer_handle_t bgfx_create_frame_buffer_from_nwh(void* _nwh, uint16_t _width, uint16_t _height, bgfx_texture_format_t _depthFormat)
  3142. {
  3143. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle;
  3144. handle.cpp = bgfx::createFrameBuffer(_nwh, _width, _height, bgfx::TextureFormat::Enum(_depthFormat) );
  3145. return handle.c;
  3146. }
  3147. BGFX_C_API void bgfx_destroy_frame_buffer(bgfx_frame_buffer_handle_t _handle)
  3148. {
  3149. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle = { _handle };
  3150. bgfx::destroyFrameBuffer(handle.cpp);
  3151. }
  3152. BGFX_C_API bgfx_uniform_handle_t bgfx_create_uniform(const char* _name, bgfx_uniform_type_t _type, uint16_t _num)
  3153. {
  3154. union { bgfx_uniform_handle_t c; bgfx::UniformHandle cpp; } handle;
  3155. handle.cpp = bgfx::createUniform(_name, bgfx::UniformType::Enum(_type), _num);
  3156. return handle.c;
  3157. }
  3158. BGFX_C_API void bgfx_destroy_uniform(bgfx_uniform_handle_t _handle)
  3159. {
  3160. union { bgfx_uniform_handle_t c; bgfx::UniformHandle cpp; } handle = { _handle };
  3161. bgfx::destroyUniform(handle.cpp);
  3162. }
  3163. BGFX_C_API void bgfx_set_palette_color(uint8_t _index, const float _rgba[4])
  3164. {
  3165. bgfx::setPaletteColor(_index, _rgba);
  3166. }
  3167. BGFX_C_API void bgfx_set_view_name(uint8_t _id, const char* _name)
  3168. {
  3169. bgfx::setViewName(_id, _name);
  3170. }
  3171. BGFX_C_API void bgfx_set_view_rect(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
  3172. {
  3173. bgfx::setViewRect(_id, _x, _y, _width, _height);
  3174. }
  3175. BGFX_C_API void bgfx_set_view_scissor(uint8_t _id, uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
  3176. {
  3177. bgfx::setViewScissor(_id, _x, _y, _width, _height);
  3178. }
  3179. BGFX_C_API void bgfx_set_view_clear(uint8_t _id, uint16_t _flags, uint32_t _rgba, float _depth, uint8_t _stencil)
  3180. {
  3181. bgfx::setViewClear(_id, _flags, _rgba, _depth, _stencil);
  3182. }
  3183. BGFX_C_API void bgfx_set_view_clear_mrt(uint8_t _id, uint16_t _flags, float _depth, uint8_t _stencil, uint8_t _0, uint8_t _1, uint8_t _2, uint8_t _3, uint8_t _4, uint8_t _5, uint8_t _6, uint8_t _7)
  3184. {
  3185. bgfx::setViewClear(_id, _flags, _depth, _stencil, _0, _1, _2, _3, _4, _5, _6, _7);
  3186. }
  3187. BGFX_C_API void bgfx_set_view_seq(uint8_t _id, bool _enabled)
  3188. {
  3189. bgfx::setViewSeq(_id, _enabled);
  3190. }
  3191. BGFX_C_API void bgfx_set_view_frame_buffer(uint8_t _id, bgfx_frame_buffer_handle_t _handle)
  3192. {
  3193. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle = { _handle };
  3194. bgfx::setViewFrameBuffer(_id, handle.cpp);
  3195. }
  3196. BGFX_C_API void bgfx_set_view_transform(uint8_t _id, const void* _view, const void* _proj)
  3197. {
  3198. bgfx::setViewTransform(_id, _view, _proj);
  3199. }
  3200. BGFX_C_API void bgfx_set_view_transform_stereo(uint8_t _id, const void* _view, const void* _projL, uint8_t _flags, const void* _projR)
  3201. {
  3202. bgfx::setViewTransform(_id, _view, _projL, _flags, _projR);
  3203. }
  3204. BGFX_C_API void bgfx_set_view_remap(uint8_t _id, uint8_t _num, const void* _remap)
  3205. {
  3206. bgfx::setViewRemap(_id, _num, _remap);
  3207. }
  3208. BGFX_C_API void bgfx_set_marker(const char* _marker)
  3209. {
  3210. bgfx::setMarker(_marker);
  3211. }
  3212. BGFX_C_API void bgfx_set_state(uint64_t _state, uint32_t _rgba)
  3213. {
  3214. bgfx::setState(_state, _rgba);
  3215. }
  3216. BGFX_C_API void bgfx_set_stencil(uint32_t _fstencil, uint32_t _bstencil)
  3217. {
  3218. bgfx::setStencil(_fstencil, _bstencil);
  3219. }
  3220. BGFX_C_API uint16_t bgfx_set_scissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
  3221. {
  3222. return bgfx::setScissor(_x, _y, _width, _height);
  3223. }
  3224. BGFX_C_API void bgfx_set_scissor_cached(uint16_t _cache)
  3225. {
  3226. bgfx::setScissor(_cache);
  3227. }
  3228. BGFX_C_API uint32_t bgfx_set_transform(const void* _mtx, uint16_t _num)
  3229. {
  3230. return bgfx::setTransform(_mtx, _num);
  3231. }
  3232. BGFX_C_API uint32_t bgfx_alloc_transform(bgfx_transform_t* _transform, uint16_t _num)
  3233. {
  3234. return bgfx::allocTransform( (bgfx::Transform*)_transform, _num);
  3235. }
  3236. BGFX_C_API void bgfx_set_transform_cached(uint32_t _cache, uint16_t _num)
  3237. {
  3238. bgfx::setTransform(_cache, _num);
  3239. }
  3240. BGFX_C_API void bgfx_set_uniform(bgfx_uniform_handle_t _handle, const void* _value, uint16_t _num)
  3241. {
  3242. union { bgfx_uniform_handle_t c; bgfx::UniformHandle cpp; } handle = { _handle };
  3243. bgfx::setUniform(handle.cpp, _value, _num);
  3244. }
  3245. BGFX_C_API void bgfx_set_index_buffer(bgfx_index_buffer_handle_t _handle, uint32_t _firstIndex, uint32_t _numIndices)
  3246. {
  3247. union { bgfx_index_buffer_handle_t c; bgfx::IndexBufferHandle cpp; } handle = { _handle };
  3248. bgfx::setIndexBuffer(handle.cpp, _firstIndex, _numIndices);
  3249. }
  3250. BGFX_C_API void bgfx_set_dynamic_index_buffer(bgfx_dynamic_index_buffer_handle_t _handle, uint32_t _firstIndex, uint32_t _numIndices)
  3251. {
  3252. union { bgfx_dynamic_index_buffer_handle_t c; bgfx::DynamicIndexBufferHandle cpp; } handle = { _handle };
  3253. bgfx::setIndexBuffer(handle.cpp, _firstIndex, _numIndices);
  3254. }
  3255. BGFX_C_API void bgfx_set_transient_index_buffer(const bgfx_transient_index_buffer_t* _tib, uint32_t _firstIndex, uint32_t _numIndices)
  3256. {
  3257. bgfx::setIndexBuffer( (const bgfx::TransientIndexBuffer*)_tib, _firstIndex, _numIndices);
  3258. }
  3259. BGFX_C_API void bgfx_set_vertex_buffer(bgfx_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _numVertices)
  3260. {
  3261. union { bgfx_vertex_buffer_handle_t c; bgfx::VertexBufferHandle cpp; } handle = { _handle };
  3262. bgfx::setVertexBuffer(handle.cpp, _startVertex, _numVertices);
  3263. }
  3264. BGFX_C_API void bgfx_set_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _numVertices)
  3265. {
  3266. union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle = { _handle };
  3267. bgfx::setVertexBuffer(handle.cpp, _numVertices);
  3268. }
  3269. BGFX_C_API void bgfx_set_transient_vertex_buffer(const bgfx_transient_vertex_buffer_t* _tvb, uint32_t _startVertex, uint32_t _numVertices)
  3270. {
  3271. bgfx::setVertexBuffer( (const bgfx::TransientVertexBuffer*)_tvb, _startVertex, _numVertices);
  3272. }
  3273. BGFX_C_API void bgfx_set_instance_data_buffer(const bgfx_instance_data_buffer_t* _idb, uint32_t _num)
  3274. {
  3275. bgfx::setInstanceDataBuffer( (const bgfx::InstanceDataBuffer*)_idb, _num);
  3276. }
  3277. BGFX_C_API void bgfx_set_instance_data_from_vertex_buffer(bgfx_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _num)
  3278. {
  3279. union { bgfx_vertex_buffer_handle_t c; bgfx::VertexBufferHandle cpp; } handle = { _handle };
  3280. bgfx::setInstanceDataBuffer(handle.cpp, _startVertex, _num);
  3281. }
  3282. BGFX_C_API void bgfx_set_instance_data_from_dynamic_vertex_buffer(bgfx_dynamic_vertex_buffer_handle_t _handle, uint32_t _startVertex, uint32_t _num)
  3283. {
  3284. union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle = { _handle };
  3285. bgfx::setInstanceDataBuffer(handle.cpp, _startVertex, _num);
  3286. }
  3287. BGFX_C_API void bgfx_set_texture(uint8_t _stage, bgfx_uniform_handle_t _sampler, bgfx_texture_handle_t _handle, uint32_t _flags)
  3288. {
  3289. union { bgfx_uniform_handle_t c; bgfx::UniformHandle cpp; } sampler = { _sampler };
  3290. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle = { _handle };
  3291. bgfx::setTexture(_stage, sampler.cpp, handle.cpp, _flags);
  3292. }
  3293. BGFX_C_API void bgfx_set_texture_from_frame_buffer(uint8_t _stage, bgfx_uniform_handle_t _sampler, bgfx_frame_buffer_handle_t _handle, uint8_t _attachment, uint32_t _flags)
  3294. {
  3295. union { bgfx_uniform_handle_t c; bgfx::UniformHandle cpp; } sampler = { _sampler };
  3296. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle = { _handle };
  3297. bgfx::setTexture(_stage, sampler.cpp, handle.cpp, _attachment, _flags);
  3298. }
  3299. BGFX_C_API uint32_t bgfx_touch(uint8_t _id)
  3300. {
  3301. return bgfx::touch(_id);
  3302. }
  3303. BGFX_C_API uint32_t bgfx_submit(uint8_t _id, bgfx_program_handle_t _handle, int32_t _depth)
  3304. {
  3305. union { bgfx_program_handle_t c; bgfx::ProgramHandle cpp; } handle = { _handle };
  3306. return bgfx::submit(_id, handle.cpp, _depth);
  3307. }
  3308. BGFX_C_API uint32_t bgfx_submit_indirect(uint8_t _id, bgfx_program_handle_t _handle, bgfx_indirect_buffer_handle_t _indirectHandle, uint16_t _start, uint16_t _num, int32_t _depth)
  3309. {
  3310. union { bgfx_program_handle_t c; bgfx::ProgramHandle cpp; } handle = { _handle };
  3311. union { bgfx_indirect_buffer_handle_t c; bgfx::IndirectBufferHandle cpp; } indirectHandle = { _indirectHandle };
  3312. return bgfx::submit(_id, handle.cpp, indirectHandle.cpp, _start, _num, _depth);
  3313. }
  3314. BGFX_C_API void bgfx_set_image(uint8_t _stage, bgfx_uniform_handle_t _sampler, bgfx_texture_handle_t _handle, uint8_t _mip, bgfx_access_t _access, bgfx_texture_format_t _format)
  3315. {
  3316. union { bgfx_uniform_handle_t c; bgfx::UniformHandle cpp; } sampler = { _sampler };
  3317. union { bgfx_texture_handle_t c; bgfx::TextureHandle cpp; } handle = { _handle };
  3318. bgfx::setImage(_stage, sampler.cpp, handle.cpp, _mip, bgfx::Access::Enum(_access), bgfx::TextureFormat::Enum(_format) );
  3319. }
  3320. BGFX_C_API void bgfx_set_image_from_frame_buffer(uint8_t _stage, bgfx_uniform_handle_t _sampler, bgfx_frame_buffer_handle_t _handle, uint8_t _attachment, bgfx_access_t _access, bgfx_texture_format_t _format)
  3321. {
  3322. union { bgfx_uniform_handle_t c; bgfx::UniformHandle cpp; } sampler = { _sampler };
  3323. union { bgfx_frame_buffer_handle_t c; bgfx::FrameBufferHandle cpp; } handle = { _handle };
  3324. bgfx::setImage(_stage, sampler.cpp, handle.cpp, _attachment, bgfx::Access::Enum(_access), bgfx::TextureFormat::Enum(_format) );
  3325. }
  3326. BGFX_C_API void bgfx_set_compute_index_buffer(uint8_t _stage, bgfx_index_buffer_handle_t _handle, bgfx_access_t _access)
  3327. {
  3328. union { bgfx_index_buffer_handle_t c; bgfx::IndexBufferHandle cpp; } handle = { _handle };
  3329. bgfx::setBuffer(_stage, handle.cpp, bgfx::Access::Enum(_access) );
  3330. }
  3331. BGFX_C_API void bgfx_set_compute_vertex_buffer(uint8_t _stage, bgfx_vertex_buffer_handle_t _handle, bgfx_access_t _access)
  3332. {
  3333. union { bgfx_vertex_buffer_handle_t c; bgfx::VertexBufferHandle cpp; } handle = { _handle };
  3334. bgfx::setBuffer(_stage, handle.cpp, bgfx::Access::Enum(_access) );
  3335. }
  3336. BGFX_C_API void bgfx_set_compute_dynamic_index_buffer(uint8_t _stage, bgfx_dynamic_index_buffer_handle_t _handle, bgfx_access_t _access)
  3337. {
  3338. union { bgfx_dynamic_index_buffer_handle_t c; bgfx::DynamicIndexBufferHandle cpp; } handle = { _handle };
  3339. bgfx::setBuffer(_stage, handle.cpp, bgfx::Access::Enum(_access) );
  3340. }
  3341. BGFX_C_API void bgfx_set_compute_dynamic_vertex_buffer(uint8_t _stage, bgfx_dynamic_vertex_buffer_handle_t _handle, bgfx_access_t _access)
  3342. {
  3343. union { bgfx_dynamic_vertex_buffer_handle_t c; bgfx::DynamicVertexBufferHandle cpp; } handle = { _handle };
  3344. bgfx::setBuffer(_stage, handle.cpp, bgfx::Access::Enum(_access) );
  3345. }
  3346. BGFX_C_API void bgfx_set_compute_indirect_buffer(uint8_t _stage, bgfx_indirect_buffer_handle_t _handle, bgfx_access_t _access)
  3347. {
  3348. union { bgfx_indirect_buffer_handle_t c; bgfx::IndirectBufferHandle cpp; } handle = { _handle };
  3349. bgfx::setBuffer(_stage, handle.cpp, bgfx::Access::Enum(_access) );
  3350. }
  3351. BGFX_C_API uint32_t bgfx_dispatch(uint8_t _id, bgfx_program_handle_t _handle, uint16_t _numX, uint16_t _numY, uint16_t _numZ, uint8_t _flags)
  3352. {
  3353. union { bgfx_program_handle_t c; bgfx::ProgramHandle cpp; } handle = { _handle };
  3354. return bgfx::dispatch(_id, handle.cpp, _numX, _numY, _numZ, _flags);
  3355. }
  3356. BGFX_C_API uint32_t bgfx_dispatch_indirect(uint8_t _id, bgfx_program_handle_t _handle, bgfx_indirect_buffer_handle_t _indirectHandle, uint16_t _start, uint16_t _num, uint8_t _flags)
  3357. {
  3358. union { bgfx_program_handle_t c; bgfx::ProgramHandle cpp; } handle = { _handle };
  3359. union { bgfx_indirect_buffer_handle_t c; bgfx::IndirectBufferHandle cpp; } indirectHandle = { _indirectHandle };
  3360. return bgfx::dispatch(_id, handle.cpp, indirectHandle.cpp, _start, _num, _flags);
  3361. }
  3362. BGFX_C_API void bgfx_discard()
  3363. {
  3364. bgfx::discard();
  3365. }
  3366. BGFX_C_API void bgfx_save_screen_shot(const char* _filePath)
  3367. {
  3368. bgfx::saveScreenShot(_filePath);
  3369. }
  3370. BGFX_C_API bgfx_render_frame_t bgfx_render_frame()
  3371. {
  3372. return bgfx_render_frame_t(bgfx::renderFrame() );
  3373. }
  3374. BGFX_C_API void bgfx_set_platform_data(bgfx_platform_data_t* _pd)
  3375. {
  3376. bgfx::setPlatformData(*(bgfx::PlatformData*)_pd);
  3377. }