bgfx.cpp 97 KB

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