renderer_gl.cpp 160 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304
  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. #if (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)
  7. # include "renderer_gl.h"
  8. # include <bx/timer.h>
  9. # include <bx/uint32_t.h>
  10. namespace bgfx
  11. {
  12. static char s_viewName[BGFX_CONFIG_MAX_VIEWS][BGFX_CONFIG_MAX_VIEW_NAME];
  13. struct PrimInfo
  14. {
  15. GLenum m_type;
  16. uint32_t m_min;
  17. uint32_t m_div;
  18. uint32_t m_sub;
  19. };
  20. static const PrimInfo s_primInfo[] =
  21. {
  22. { GL_TRIANGLES, 3, 3, 0 },
  23. { GL_TRIANGLE_STRIP, 3, 1, 2 },
  24. { GL_LINES, 2, 2, 0 },
  25. { GL_LINE_STRIP, 2, 1, 1 },
  26. { GL_POINTS, 1, 1, 0 },
  27. };
  28. static const char* s_primName[] =
  29. {
  30. "TriList",
  31. "TriStrip",
  32. "Line",
  33. "LineStrip",
  34. "Point",
  35. };
  36. static const char* s_attribName[] =
  37. {
  38. "a_position",
  39. "a_normal",
  40. "a_tangent",
  41. "a_bitangent",
  42. "a_color0",
  43. "a_color1",
  44. "a_indices",
  45. "a_weight",
  46. "a_texcoord0",
  47. "a_texcoord1",
  48. "a_texcoord2",
  49. "a_texcoord3",
  50. "a_texcoord4",
  51. "a_texcoord5",
  52. "a_texcoord6",
  53. "a_texcoord7",
  54. };
  55. BX_STATIC_ASSERT(Attrib::Count == BX_COUNTOF(s_attribName) );
  56. static const char* s_instanceDataName[] =
  57. {
  58. "i_data0",
  59. "i_data1",
  60. "i_data2",
  61. "i_data3",
  62. "i_data4",
  63. };
  64. BX_STATIC_ASSERT(BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT == BX_COUNTOF(s_instanceDataName) );
  65. static const GLenum s_access[] =
  66. {
  67. GL_READ_ONLY,
  68. GL_WRITE_ONLY,
  69. GL_READ_WRITE,
  70. };
  71. BX_STATIC_ASSERT(Access::Count == BX_COUNTOF(s_access) );
  72. static const GLenum s_attribType[] =
  73. {
  74. GL_UNSIGNED_BYTE,
  75. GL_SHORT,
  76. GL_HALF_FLOAT,
  77. GL_FLOAT,
  78. };
  79. BX_STATIC_ASSERT(AttribType::Count == BX_COUNTOF(s_attribType) );
  80. struct Blend
  81. {
  82. GLenum m_src;
  83. GLenum m_dst;
  84. bool m_factor;
  85. };
  86. static const Blend s_blendFactor[] =
  87. {
  88. { 0, 0, false }, // ignored
  89. { GL_ZERO, GL_ZERO, false }, // ZERO
  90. { GL_ONE, GL_ONE, false }, // ONE
  91. { GL_SRC_COLOR, GL_SRC_COLOR, false }, // SRC_COLOR
  92. { GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, false }, // INV_SRC_COLOR
  93. { GL_SRC_ALPHA, GL_SRC_ALPHA, false }, // SRC_ALPHA
  94. { GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false }, // INV_SRC_ALPHA
  95. { GL_DST_ALPHA, GL_DST_ALPHA, false }, // DST_ALPHA
  96. { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, false }, // INV_DST_ALPHA
  97. { GL_DST_COLOR, GL_DST_COLOR, false }, // DST_COLOR
  98. { GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR, false }, // INV_DST_COLOR
  99. { GL_SRC_ALPHA_SATURATE, GL_ONE, false }, // SRC_ALPHA_SAT
  100. { GL_CONSTANT_COLOR, GL_CONSTANT_COLOR, true }, // FACTOR
  101. { GL_ONE_MINUS_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, true }, // INV_FACTOR
  102. };
  103. static const GLenum s_blendEquation[] =
  104. {
  105. GL_FUNC_ADD,
  106. GL_FUNC_SUBTRACT,
  107. GL_FUNC_REVERSE_SUBTRACT,
  108. GL_MIN,
  109. GL_MAX,
  110. };
  111. static const GLenum s_cmpFunc[] =
  112. {
  113. 0, // ignored
  114. GL_LESS,
  115. GL_LEQUAL,
  116. GL_EQUAL,
  117. GL_GEQUAL,
  118. GL_GREATER,
  119. GL_NOTEQUAL,
  120. GL_NEVER,
  121. GL_ALWAYS,
  122. };
  123. static const GLenum s_stencilOp[] =
  124. {
  125. GL_ZERO,
  126. GL_KEEP,
  127. GL_REPLACE,
  128. GL_INCR_WRAP,
  129. GL_INCR,
  130. GL_DECR_WRAP,
  131. GL_DECR,
  132. GL_INVERT,
  133. };
  134. static const GLenum s_stencilFace[] =
  135. {
  136. GL_FRONT_AND_BACK,
  137. GL_FRONT,
  138. GL_BACK,
  139. };
  140. static const GLenum s_textureAddress[] =
  141. {
  142. GL_REPEAT,
  143. GL_MIRRORED_REPEAT,
  144. GL_CLAMP_TO_EDGE,
  145. };
  146. static const GLenum s_textureFilterMag[] =
  147. {
  148. GL_LINEAR,
  149. GL_NEAREST,
  150. GL_LINEAR,
  151. };
  152. static const GLenum s_textureFilterMin[][3] =
  153. {
  154. { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_LINEAR },
  155. { GL_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_NEAREST },
  156. { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_LINEAR },
  157. };
  158. struct TextureFormatInfo
  159. {
  160. GLenum m_internalFmt;
  161. GLenum m_fmt;
  162. GLenum m_type;
  163. bool m_supported;
  164. };
  165. static TextureFormatInfo s_textureFormat[] =
  166. {
  167. { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_ZERO, false }, // BC1
  168. { GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_ZERO, false }, // BC2
  169. { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_ZERO, false }, // BC3
  170. { GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_ZERO, false }, // BC4
  171. { GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_ZERO, false }, // BC5
  172. { GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_ZERO, false }, // BC6H
  173. { GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_ZERO, false }, // BC7
  174. { GL_ETC1_RGB8_OES, GL_ETC1_RGB8_OES, GL_ZERO, false }, // ETC1
  175. { GL_COMPRESSED_RGB8_ETC2, GL_COMPRESSED_RGB8_ETC2, GL_ZERO, false }, // ETC2
  176. { GL_COMPRESSED_RGBA8_ETC2_EAC, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_ZERO, false }, // ETC2A
  177. { GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_ZERO, false }, // ETC2A1
  178. { GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, GL_ZERO, false }, // PTC12
  179. { GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, GL_ZERO, false }, // PTC14
  180. { GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, GL_ZERO, false }, // PTC12A
  181. { GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, GL_ZERO, false }, // PTC14A
  182. { GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, GL_ZERO, false }, // PTC22
  183. { GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, GL_ZERO, false }, // PTC24
  184. { GL_ZERO, GL_ZERO, GL_ZERO, true }, // Unknown
  185. { GL_ZERO, GL_ZERO, GL_ZERO, true }, // R1
  186. { GL_R8, GL_RED, GL_UNSIGNED_BYTE, true }, // R8
  187. { GL_R16, GL_RED, GL_UNSIGNED_SHORT, true }, // R16
  188. { GL_R16F, GL_RED, GL_HALF_FLOAT, true }, // R16F
  189. { GL_R32UI, GL_RED, GL_UNSIGNED_INT, true }, // R32
  190. { GL_R32F, GL_RED, GL_FLOAT, true }, // R32F
  191. { GL_RG8, GL_RG, GL_UNSIGNED_BYTE, true }, // RG8
  192. { GL_RG16, GL_RG, GL_UNSIGNED_SHORT, true }, // RG16
  193. { GL_RG16F, GL_RG, GL_FLOAT, true }, // RG16F
  194. { GL_RG32UI, GL_RG, GL_UNSIGNED_INT, true }, // RG32
  195. { GL_RG32F, GL_RG, GL_FLOAT, true }, // RG32F
  196. { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, true }, // BGRA8
  197. { GL_RGBA16, GL_RGBA, GL_UNSIGNED_BYTE, true }, // RGBA16
  198. { GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, true }, // RGBA16F
  199. { GL_RGBA32UI, GL_RGBA, GL_UNSIGNED_INT, true }, // RGBA32
  200. { GL_RGBA32F, GL_RGBA, GL_FLOAT, true }, // RGBA32F
  201. { GL_RGB565, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, true }, // R5G6B5
  202. { GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, true }, // RGBA4
  203. { GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, true }, // RGB5A1
  204. { GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, true }, // RGB10A2
  205. { GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, true }, // R11G11B10F
  206. { GL_ZERO, GL_ZERO, GL_ZERO, true }, // UnknownDepth
  207. { GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, false }, // D16
  208. { GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, false }, // D24
  209. { GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, false }, // D24S8
  210. { GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, false }, // D32
  211. { GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, false }, // D16F
  212. { GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, false }, // D24F
  213. { GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, false }, // D32F
  214. { GL_STENCIL_INDEX8, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, false }, // D0S8
  215. };
  216. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormat) );
  217. static GLenum s_rboFormat[] =
  218. {
  219. GL_ZERO, // BC1
  220. GL_ZERO, // BC2
  221. GL_ZERO, // BC3
  222. GL_ZERO, // BC4
  223. GL_ZERO, // BC5
  224. GL_ZERO, // BC6H
  225. GL_ZERO, // BC7
  226. GL_ZERO, // ETC1
  227. GL_ZERO, // ETC2
  228. GL_ZERO, // ETC2A
  229. GL_ZERO, // ETC2A1
  230. GL_ZERO, // PTC12
  231. GL_ZERO, // PTC14
  232. GL_ZERO, // PTC12A
  233. GL_ZERO, // PTC14A
  234. GL_ZERO, // PTC22
  235. GL_ZERO, // PTC24
  236. GL_ZERO, // Unknown
  237. GL_ZERO, // R1
  238. GL_R8, // R8
  239. GL_R16, // R16
  240. GL_R16F, // R16F
  241. GL_R32UI, // R32
  242. GL_R32F, // R32F
  243. GL_RG8, // RG8
  244. GL_RG16, // RG16
  245. GL_RG16F, // RG16F
  246. GL_RG32UI, // RG32
  247. GL_RG32F, // RG32F
  248. GL_RGBA8, // BGRA8
  249. GL_RGBA16, // RGBA16
  250. GL_RGBA16F, // RGBA16F
  251. GL_RGBA32UI, // RGBA32
  252. GL_RGBA32F, // RGBA32F
  253. GL_RGB565, // R5G6B5
  254. GL_RGBA4, // RGBA4
  255. GL_RGB5_A1, // RGB5A1
  256. GL_RGB10_A2, // RGB10A2
  257. GL_R11F_G11F_B10F, // R11G11B10F
  258. GL_ZERO, // UnknownDepth
  259. GL_DEPTH_COMPONENT16, // D16
  260. GL_DEPTH_COMPONENT24, // D24
  261. GL_DEPTH24_STENCIL8, // D24S8
  262. GL_DEPTH_COMPONENT32, // D32
  263. GL_DEPTH_COMPONENT32F, // D16F
  264. GL_DEPTH_COMPONENT32F, // D24F
  265. GL_DEPTH_COMPONENT32F, // D32F
  266. GL_STENCIL_INDEX8, // D0S8
  267. };
  268. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_rboFormat) );
  269. static GLenum s_imageFormat[] =
  270. {
  271. GL_ZERO, // BC1
  272. GL_ZERO, // BC2
  273. GL_ZERO, // BC3
  274. GL_ZERO, // BC4
  275. GL_ZERO, // BC5
  276. GL_ZERO, // BC6H
  277. GL_ZERO, // BC7
  278. GL_ZERO, // ETC1
  279. GL_ZERO, // ETC2
  280. GL_ZERO, // ETC2A
  281. GL_ZERO, // ETC2A1
  282. GL_ZERO, // PTC12
  283. GL_ZERO, // PTC14
  284. GL_ZERO, // PTC12A
  285. GL_ZERO, // PTC14A
  286. GL_ZERO, // PTC22
  287. GL_ZERO, // PTC24
  288. GL_ZERO, // Unknown
  289. GL_ZERO, // R1
  290. GL_R8, // R8
  291. GL_R16, // R16
  292. GL_R16F, // R16F
  293. GL_R32UI, // R32
  294. GL_R32F, // R32F
  295. GL_RG8, // RG8
  296. GL_RG16, // RG16
  297. GL_RG16F, // RG16F
  298. GL_RG32UI, // RG32
  299. GL_RG32F, // RG32F
  300. GL_RGBA8, // BGRA8
  301. GL_RGBA16, // RGBA16
  302. GL_RGBA16F, // RGBA16F
  303. GL_RGBA32UI, // RGBA32
  304. GL_RGBA32F, // RGBA32F
  305. GL_RGB565, // R5G6B5
  306. GL_RGBA4, // RGBA4
  307. GL_RGB5_A1, // RGB5A1
  308. GL_RGB10_A2, // RGB10A2
  309. GL_R11F_G11F_B10F, // R11G11B10F
  310. GL_ZERO, // UnknownDepth
  311. GL_ZERO, // D16
  312. GL_ZERO, // D24
  313. GL_ZERO, // D24S8
  314. GL_ZERO, // D32
  315. GL_ZERO, // D16F
  316. GL_ZERO, // D24F
  317. GL_ZERO, // D32F
  318. GL_ZERO, // D0S8
  319. };
  320. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageFormat) );
  321. struct Extension
  322. {
  323. enum Enum
  324. {
  325. AMD_conservative_depth,
  326. ANGLE_depth_texture,
  327. ANGLE_framebuffer_blit,
  328. ANGLE_framebuffer_multisample,
  329. ANGLE_instanced_arrays,
  330. ANGLE_texture_compression_dxt1,
  331. ANGLE_texture_compression_dxt3,
  332. ANGLE_texture_compression_dxt5,
  333. ANGLE_translated_shader_source,
  334. APPLE_texture_format_BGRA8888,
  335. APPLE_texture_max_level,
  336. ARB_compute_shader,
  337. ARB_conservative_depth,
  338. ARB_debug_label,
  339. ARB_debug_output,
  340. ARB_depth_buffer_float,
  341. ARB_depth_clamp,
  342. ARB_draw_buffers_blend,
  343. ARB_draw_instanced,
  344. ARB_ES3_compatibility,
  345. ARB_framebuffer_object,
  346. ARB_framebuffer_sRGB,
  347. ARB_get_program_binary,
  348. ARB_half_float_pixel,
  349. ARB_half_float_vertex,
  350. ARB_instanced_arrays,
  351. ARB_invalidate_subdata,
  352. ARB_map_buffer_range,
  353. ARB_multisample,
  354. ARB_occlusion_query,
  355. ARB_occlusion_query2,
  356. ARB_program_interface_query,
  357. ARB_sampler_objects,
  358. ARB_seamless_cube_map,
  359. ARB_shader_bit_encoding,
  360. ARB_shader_image_load_store,
  361. ARB_shader_storage_buffer_object,
  362. ARB_shader_texture_lod,
  363. ARB_texture_compression_bptc,
  364. ARB_texture_compression_rgtc,
  365. ARB_texture_float,
  366. ARB_texture_multisample,
  367. ARB_texture_rg,
  368. ARB_texture_rgb10_a2ui,
  369. ARB_texture_stencil8,
  370. ARB_texture_storage,
  371. ARB_texture_swizzle,
  372. ARB_timer_query,
  373. ARB_uniform_buffer_object,
  374. ARB_vertex_array_object,
  375. ARB_vertex_type_2_10_10_10_rev,
  376. ATI_meminfo,
  377. CHROMIUM_color_buffer_float_rgb,
  378. CHROMIUM_color_buffer_float_rgba,
  379. CHROMIUM_depth_texture,
  380. CHROMIUM_framebuffer_multisample,
  381. CHROMIUM_texture_compression_dxt3,
  382. CHROMIUM_texture_compression_dxt5,
  383. EXT_bgra,
  384. EXT_blend_color,
  385. EXT_blend_minmax,
  386. EXT_blend_subtract,
  387. EXT_compressed_ETC1_RGB8_sub_texture,
  388. EXT_debug_label,
  389. EXT_debug_marker,
  390. EXT_discard_framebuffer,
  391. EXT_draw_buffers,
  392. EXT_frag_depth,
  393. EXT_framebuffer_blit,
  394. EXT_framebuffer_object,
  395. EXT_framebuffer_sRGB,
  396. EXT_occlusion_query_boolean,
  397. EXT_packed_float,
  398. EXT_read_format_bgra,
  399. EXT_shader_image_load_store,
  400. EXT_shader_texture_lod,
  401. EXT_shadow_samplers,
  402. EXT_texture_array,
  403. EXT_texture_compression_dxt1,
  404. EXT_texture_compression_latc,
  405. EXT_texture_compression_rgtc,
  406. EXT_texture_compression_s3tc,
  407. EXT_texture_filter_anisotropic,
  408. EXT_texture_format_BGRA8888,
  409. EXT_texture_rg,
  410. EXT_texture_sRGB,
  411. EXT_texture_storage,
  412. EXT_texture_swizzle,
  413. EXT_texture_type_2_10_10_10_REV,
  414. EXT_timer_query,
  415. EXT_unpack_subimage,
  416. GOOGLE_depth_texture,
  417. GREMEDY_string_marker,
  418. GREMEDY_frame_terminator,
  419. IMG_multisampled_render_to_texture,
  420. IMG_read_format,
  421. IMG_shader_binary,
  422. IMG_texture_compression_pvrtc,
  423. IMG_texture_compression_pvrtc2,
  424. IMG_texture_format_BGRA8888,
  425. INTEL_fragment_shader_ordering,
  426. KHR_debug,
  427. MOZ_WEBGL_compressed_texture_s3tc,
  428. MOZ_WEBGL_depth_texture,
  429. NV_draw_buffers,
  430. NVX_gpu_memory_info,
  431. OES_compressed_ETC1_RGB8_texture,
  432. OES_depth24,
  433. OES_depth32,
  434. OES_depth_texture,
  435. OES_fragment_precision_high,
  436. OES_get_program_binary,
  437. OES_required_internalformat,
  438. OES_packed_depth_stencil,
  439. OES_read_format,
  440. OES_rgb8_rgba8,
  441. OES_standard_derivatives,
  442. OES_texture_3D,
  443. OES_texture_float,
  444. OES_texture_float_linear,
  445. OES_texture_npot,
  446. OES_texture_half_float,
  447. OES_texture_half_float_linear,
  448. OES_texture_stencil8,
  449. OES_vertex_array_object,
  450. OES_vertex_half_float,
  451. OES_vertex_type_10_10_10_2,
  452. WEBGL_compressed_texture_etc1,
  453. WEBGL_compressed_texture_s3tc,
  454. WEBGL_compressed_texture_pvrtc,
  455. WEBGL_depth_texture,
  456. WEBKIT_EXT_texture_filter_anisotropic,
  457. WEBKIT_WEBGL_compressed_texture_s3tc,
  458. WEBKIT_WEBGL_depth_texture,
  459. Count
  460. };
  461. const char* m_name;
  462. bool m_supported;
  463. bool m_initialize;
  464. };
  465. static Extension s_extension[Extension::Count] =
  466. {
  467. { "AMD_conservative_depth", false, true },
  468. { "ANGLE_depth_texture", false, true },
  469. { "ANGLE_framebuffer_blit", false, true },
  470. { "ANGLE_framebuffer_multisample", false, false },
  471. { "ANGLE_instanced_arrays", false, true },
  472. { "ANGLE_texture_compression_dxt1", false, true },
  473. { "ANGLE_texture_compression_dxt3", false, true },
  474. { "ANGLE_texture_compression_dxt5", false, true },
  475. { "ANGLE_translated_shader_source", false, true },
  476. { "APPLE_texture_format_BGRA8888", false, true },
  477. { "APPLE_texture_max_level", false, true },
  478. { "ARB_compute_shader", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  479. { "ARB_conservative_depth", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  480. { "ARB_debug_label", false, true },
  481. { "ARB_debug_output", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  482. { "ARB_depth_buffer_float", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  483. { "ARB_depth_clamp", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  484. { "ARB_draw_buffers_blend", BGFX_CONFIG_RENDERER_OPENGL >= 40, true },
  485. { "ARB_draw_instanced", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  486. { "ARB_ES3_compatibility", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  487. { "ARB_framebuffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  488. { "ARB_framebuffer_sRGB", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  489. { "ARB_get_program_binary", BGFX_CONFIG_RENDERER_OPENGL >= 41, true },
  490. { "ARB_half_float_pixel", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  491. { "ARB_half_float_vertex", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  492. { "ARB_instanced_arrays", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  493. { "ARB_invalidate_subdata", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  494. { "ARB_map_buffer_range", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  495. { "ARB_multisample", false, true },
  496. { "ARB_occlusion_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  497. { "ARB_occlusion_query2", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  498. { "ARB_program_interface_query", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  499. { "ARB_sampler_objects", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  500. { "ARB_seamless_cube_map", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  501. { "ARB_shader_bit_encoding", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  502. { "ARB_shader_image_load_store", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  503. { "ARB_shader_storage_buffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  504. { "ARB_shader_texture_lod", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  505. { "ARB_texture_compression_bptc", BGFX_CONFIG_RENDERER_OPENGL >= 44, true },
  506. { "ARB_texture_compression_rgtc", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  507. { "ARB_texture_float", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  508. { "ARB_texture_multisample", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  509. { "ARB_texture_rg", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  510. { "ARB_texture_rgb10_a2ui", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  511. { "ARB_texture_stencil8", false, true },
  512. { "ARB_texture_storage", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  513. { "ARB_texture_swizzle", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  514. { "ARB_timer_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  515. { "ARB_uniform_buffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 31, true },
  516. { "ARB_vertex_array_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  517. { "ARB_vertex_type_2_10_10_10_rev", false, true },
  518. { "ATI_meminfo", false, true },
  519. { "CHROMIUM_color_buffer_float_rgb", false, true },
  520. { "CHROMIUM_color_buffer_float_rgba", false, true },
  521. { "CHROMIUM_depth_texture", false, true },
  522. { "CHROMIUM_framebuffer_multisample", false, true },
  523. { "CHROMIUM_texture_compression_dxt3", false, true },
  524. { "CHROMIUM_texture_compression_dxt5", false, true },
  525. { "EXT_bgra", false, true },
  526. { "EXT_blend_color", BGFX_CONFIG_RENDERER_OPENGL >= 31, true },
  527. { "EXT_blend_minmax", BGFX_CONFIG_RENDERER_OPENGL >= 14, true },
  528. { "EXT_blend_subtract", BGFX_CONFIG_RENDERER_OPENGL >= 14, true },
  529. { "EXT_compressed_ETC1_RGB8_sub_texture", false, true }, // GLES2 extension.
  530. { "EXT_debug_label", false, true },
  531. { "EXT_debug_marker", false, true },
  532. { "EXT_discard_framebuffer", false, true }, // GLES2 extension.
  533. { "EXT_draw_buffers", false, true }, // GLES2 extension.
  534. { "EXT_frag_depth", false, true }, // GLES2 extension.
  535. { "EXT_framebuffer_blit", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  536. { "EXT_framebuffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  537. { "EXT_framebuffer_sRGB", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  538. { "EXT_occlusion_query_boolean", false, true },
  539. { "EXT_packed_float", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  540. { "EXT_read_format_bgra", false, true },
  541. { "EXT_shader_image_load_store", false, true },
  542. { "EXT_shader_texture_lod", false, true }, // GLES2 extension.
  543. { "EXT_shadow_samplers", false, true },
  544. { "EXT_texture_array", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  545. { "EXT_texture_compression_dxt1", false, true },
  546. { "EXT_texture_compression_latc", false, true },
  547. { "EXT_texture_compression_rgtc", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  548. { "EXT_texture_compression_s3tc", false, true },
  549. { "EXT_texture_filter_anisotropic", false, true },
  550. { "EXT_texture_format_BGRA8888", false, true },
  551. { "EXT_texture_rg", false, true }, // GLES2 extension.
  552. { "EXT_texture_sRGB", false, true },
  553. { "EXT_texture_storage", false, true },
  554. { "EXT_texture_swizzle", false, true },
  555. { "EXT_texture_type_2_10_10_10_REV", false, true },
  556. { "EXT_timer_query", false, true },
  557. { "EXT_unpack_subimage", false, true },
  558. { "GOOGLE_depth_texture", false, true },
  559. { "GREMEDY_string_marker", false, true },
  560. { "GREMEDY_frame_terminator", false, true },
  561. { "IMG_multisampled_render_to_texture", false, true },
  562. { "IMG_read_format", false, true },
  563. { "IMG_shader_binary", false, true },
  564. { "IMG_texture_compression_pvrtc", false, true },
  565. { "IMG_texture_compression_pvrtc2", false, true },
  566. { "IMG_texture_format_BGRA8888", false, true },
  567. { "INTEL_fragment_shader_ordering", false, true },
  568. { "KHR_debug", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  569. { "MOZ_WEBGL_compressed_texture_s3tc", false, true },
  570. { "MOZ_WEBGL_depth_texture", false, true },
  571. { "NV_draw_buffers", false, true }, // GLES2 extension.
  572. { "NVX_gpu_memory_info", false, true },
  573. { "OES_compressed_ETC1_RGB8_texture", false, true },
  574. { "OES_depth24", false, true },
  575. { "OES_depth32", false, true },
  576. { "OES_depth_texture", false, true },
  577. { "OES_fragment_precision_high", false, true },
  578. { "OES_get_program_binary", false, true },
  579. { "OES_required_internalformat", false, true },
  580. { "OES_packed_depth_stencil", false, true },
  581. { "OES_read_format", false, true },
  582. { "OES_rgb8_rgba8", false, true },
  583. { "OES_standard_derivatives", false, true },
  584. { "OES_texture_3D", false, true },
  585. { "OES_texture_float", false, true },
  586. { "OES_texture_float_linear", false, true },
  587. { "OES_texture_npot", false, true },
  588. { "OES_texture_half_float", false, true },
  589. { "OES_texture_half_float_linear", false, true },
  590. { "OES_texture_stencil8", false, true },
  591. { "OES_vertex_array_object", false, !BX_PLATFORM_IOS },
  592. { "OES_vertex_half_float", false, true },
  593. { "OES_vertex_type_10_10_10_2", false, true },
  594. { "WEBGL_compressed_texture_etc1", false, true },
  595. { "WEBGL_compressed_texture_s3tc", false, true },
  596. { "WEBGL_compressed_texture_pvrtc", false, true },
  597. { "WEBGL_depth_texture", false, true },
  598. { "WEBKIT_EXT_texture_filter_anisotropic", false, true },
  599. { "WEBKIT_WEBGL_compressed_texture_s3tc", false, true },
  600. { "WEBKIT_WEBGL_depth_texture", false, true },
  601. };
  602. static const char* s_ARB_shader_texture_lod[] =
  603. {
  604. "texture2DLod",
  605. "texture2DProjLod",
  606. "texture3DLod",
  607. "texture3DProjLod",
  608. "textureCubeLod",
  609. "shadow2DLod",
  610. "shadow2DProjLod",
  611. NULL
  612. // "texture1DLod",
  613. // "texture1DProjLod",
  614. // "shadow1DLod",
  615. // "shadow1DProjLod",
  616. };
  617. static const char* s_EXT_shader_texture_lod[] =
  618. {
  619. "texture2DLod",
  620. "texture2DProjLod",
  621. "textureCubeLod",
  622. NULL
  623. // "texture2DGrad",
  624. // "texture2DProjGrad",
  625. // "textureCubeGrad",
  626. };
  627. static const char* s_EXT_shadow_samplers[] =
  628. {
  629. "shadow2D",
  630. "shadow2DProj",
  631. NULL
  632. };
  633. static const char* s_OES_standard_derivatives[] =
  634. {
  635. "dFdx",
  636. "dFdy",
  637. "fwidth",
  638. NULL
  639. };
  640. static const char* s_OES_texture_3D[] =
  641. {
  642. "texture3D",
  643. "texture3DProj",
  644. "texture3DLod",
  645. "texture3DProjLod",
  646. NULL
  647. };
  648. static void GL_APIENTRY stubVertexAttribDivisor(GLuint /*_index*/, GLuint /*_divisor*/)
  649. {
  650. }
  651. static void GL_APIENTRY stubDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei /*_primcount*/)
  652. {
  653. GL_CHECK(glDrawArrays(_mode, _first, _count) );
  654. }
  655. static void GL_APIENTRY stubDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei /*_primcount*/)
  656. {
  657. GL_CHECK(glDrawElements(_mode, _count, _type, _indices) );
  658. }
  659. static void GL_APIENTRY stubFrameTerminatorGREMEDY()
  660. {
  661. }
  662. static void GL_APIENTRY stubInsertEventMarker(GLsizei /*_length*/, const char* /*_marker*/)
  663. {
  664. }
  665. static void GL_APIENTRY stubInsertEventMarkerGREMEDY(GLsizei _length, const char* _marker)
  666. {
  667. // If <marker> is a null-terminated string then <length> should not
  668. // include the terminator.
  669. //
  670. // If <length> is 0 then <marker> is assumed to be null-terminated.
  671. uint32_t size = (0 == _length ? (uint32_t)strlen(_marker) : _length) + 1;
  672. size *= sizeof(wchar_t);
  673. wchar_t* name = (wchar_t*)alloca(size);
  674. mbstowcs(name, _marker, size-2);
  675. GL_CHECK(glStringMarkerGREMEDY(_length, _marker) );
  676. }
  677. static void GL_APIENTRY stubObjectLabel(GLenum /*_identifier*/, GLuint /*_name*/, GLsizei /*_length*/, const char* /*_label*/)
  678. {
  679. }
  680. static void GL_APIENTRY stubInvalidateFramebuffer(GLenum /*_target*/, GLsizei /*_numAttachments*/, const GLenum* /*_attachments*/)
  681. {
  682. }
  683. typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height);
  684. static const char* getGLString(GLenum _name)
  685. {
  686. const char* str = (const char*)glGetString(_name);
  687. glGetError(); // ignore error if glGetString returns NULL.
  688. if (NULL != str)
  689. {
  690. return str;
  691. }
  692. return "<unknown>";
  693. }
  694. static uint32_t getGLStringHash(GLenum _name)
  695. {
  696. const char* str = (const char*)glGetString(_name);
  697. glGetError(); // ignore error if glGetString returns NULL.
  698. if (NULL != str)
  699. {
  700. return bx::hashMurmur2A(str, (uint32_t)strlen(str) );
  701. }
  702. return 0;
  703. }
  704. void dumpExtensions(const char* _extensions)
  705. {
  706. if (NULL != _extensions)
  707. {
  708. char name[1024];
  709. const char* pos = _extensions;
  710. const char* end = _extensions + strlen(_extensions);
  711. while (pos < end)
  712. {
  713. uint32_t len;
  714. const char* space = strchr(pos, ' ');
  715. if (NULL != space)
  716. {
  717. len = bx::uint32_min(sizeof(name), (uint32_t)(space - pos) );
  718. }
  719. else
  720. {
  721. len = bx::uint32_min(sizeof(name), (uint32_t)strlen(pos) );
  722. }
  723. strncpy(name, pos, len);
  724. name[len] = '\0';
  725. BX_TRACE("\t%s", name);
  726. pos += len+1;
  727. }
  728. }
  729. }
  730. const char* toString(GLenum _enum)
  731. {
  732. #if defined(GL_DEBUG_SOURCE_API_ARB)
  733. switch (_enum)
  734. {
  735. case GL_DEBUG_SOURCE_API_ARB: return "API";
  736. case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB: return "WinSys";
  737. case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB: return "Shader";
  738. case GL_DEBUG_SOURCE_THIRD_PARTY_ARB: return "3rdparty";
  739. case GL_DEBUG_SOURCE_APPLICATION_ARB: return "Application";
  740. case GL_DEBUG_SOURCE_OTHER_ARB: return "Other";
  741. case GL_DEBUG_TYPE_ERROR_ARB: return "Error";
  742. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB: return "Deprecated behavior";
  743. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB: return "Undefined behavior";
  744. case GL_DEBUG_TYPE_PORTABILITY_ARB: return "Portability";
  745. case GL_DEBUG_TYPE_PERFORMANCE_ARB: return "Performance";
  746. case GL_DEBUG_TYPE_OTHER_ARB: return "Other";
  747. case GL_DEBUG_SEVERITY_HIGH_ARB: return "High";
  748. case GL_DEBUG_SEVERITY_MEDIUM_ARB: return "Medium";
  749. case GL_DEBUG_SEVERITY_LOW_ARB: return "Low";
  750. default:
  751. break;
  752. }
  753. #else
  754. BX_UNUSED(_enum);
  755. #endif // defined(GL_DEBUG_SOURCE_API_ARB)
  756. return "<unknown>";
  757. }
  758. void GL_APIENTRY debugProcCb(GLenum _source, GLenum _type, GLuint _id, GLenum _severity, GLsizei /*_length*/, const GLchar* _message, const void* /*_userParam*/)
  759. {
  760. BX_TRACE("src %s, type %s, id %d, severity %s, '%s'"
  761. , toString(_source)
  762. , toString(_type)
  763. , _id
  764. , toString(_severity)
  765. , _message
  766. );
  767. BX_UNUSED(_source, _type, _id, _severity, _message);
  768. }
  769. GLint glGet(GLenum _pname)
  770. {
  771. GLint result = 0;
  772. glGetIntegerv(_pname, &result);
  773. GLenum err = glGetError();
  774. BX_WARN(0 == err, "glGetIntegerv(0x%04x, ...) failed with GL error: 0x%04x.", _pname, err);
  775. return 0 == err ? result : 0;
  776. }
  777. void setTextureFormat(TextureFormat::Enum _format, GLenum _internalFmt, GLenum _fmt, GLenum _type = GL_ZERO)
  778. {
  779. TextureFormatInfo& tfi = s_textureFormat[_format];
  780. tfi.m_internalFmt = _internalFmt;
  781. tfi.m_fmt = _fmt;
  782. tfi.m_type = _type;
  783. }
  784. bool isTextureFormatValid(TextureFormat::Enum _format)
  785. {
  786. const TextureFormatInfo& tfi = s_textureFormat[_format];
  787. if (GL_ZERO == tfi.m_internalFmt)
  788. {
  789. return false;
  790. }
  791. GLuint id;
  792. GL_CHECK(glGenTextures(1, &id) );
  793. GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) );
  794. GLsizei size = (16*16*getBitsPerPixel(_format) )/8;
  795. void* data = alloca(size);
  796. if (isCompressed(_format) )
  797. {
  798. glCompressedTexImage2D(GL_TEXTURE_2D, 0, tfi.m_internalFmt, 16, 16, 0, size, data);
  799. }
  800. else
  801. {
  802. glTexImage2D(GL_TEXTURE_2D, 0, tfi.m_internalFmt, 16, 16, 0, tfi.m_fmt, tfi.m_type, data);
  803. }
  804. GLenum err = glGetError();
  805. BX_WARN(0 == err, "TextureFormat::%s is not supported (%x: %s).", getName(_format), err, glEnumName(err) );
  806. GL_CHECK(glDeleteTextures(1, &id) );
  807. return 0 == err;
  808. }
  809. struct RendererContextGL : public RendererContextI
  810. {
  811. RendererContextGL()
  812. : m_numWindows(1)
  813. , m_rtMsaa(false)
  814. , m_fbDiscard(BGFX_CLEAR_NONE)
  815. , m_capture(NULL)
  816. , m_captureSize(0)
  817. , m_maxAnisotropy(0.0f)
  818. , m_maxAnisotropyDefault(0.0f)
  819. , m_maxMsaa(0)
  820. , m_vao(0)
  821. , m_vaoSupport(false)
  822. , m_samplerObjectSupport(false)
  823. , m_shadowSamplersSupport(false)
  824. , m_programBinarySupport(false)
  825. , m_textureSwizzleSupport(false)
  826. , m_depthTextureSupport(false)
  827. , m_flip(false)
  828. , m_hash( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT)
  829. , m_backBufferFbo(0)
  830. , m_msaaBackBufferFbo(0)
  831. , m_ovrFbo(0)
  832. {
  833. memset(m_msaaBackBufferRbos, 0, sizeof(m_msaaBackBufferRbos) );
  834. }
  835. ~RendererContextGL()
  836. {
  837. }
  838. void init()
  839. {
  840. m_renderdocdll = loadRenderDoc();
  841. m_fbh.idx = invalidHandle;
  842. memset(m_uniforms, 0, sizeof(m_uniforms) );
  843. memset(&m_resolution, 0, sizeof(m_resolution) );
  844. setRenderContextSize(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT);
  845. // Must be after context is initialized?!
  846. m_ovr.init();
  847. m_vendor = getGLString(GL_VENDOR);
  848. m_renderer = getGLString(GL_RENDERER);
  849. m_version = getGLString(GL_VERSION);
  850. m_glslVersion = getGLString(GL_SHADING_LANGUAGE_VERSION);
  851. GLint numCmpFormats = 0;
  852. GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCmpFormats) );
  853. BX_TRACE("GL_NUM_COMPRESSED_TEXTURE_FORMATS %d", numCmpFormats);
  854. GLint* cmpFormat = NULL;
  855. if (0 < numCmpFormats)
  856. {
  857. numCmpFormats = numCmpFormats > 256 ? 256 : numCmpFormats;
  858. cmpFormat = (GLint*)alloca(sizeof(GLint)*numCmpFormats);
  859. GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, cmpFormat) );
  860. for (GLint ii = 0; ii < numCmpFormats; ++ii)
  861. {
  862. GLint internalFmt = cmpFormat[ii];
  863. uint32_t fmt = uint32_t(TextureFormat::Unknown);
  864. for (uint32_t jj = 0; jj < fmt; ++jj)
  865. {
  866. if (s_textureFormat[jj].m_internalFmt == (GLenum)internalFmt)
  867. {
  868. s_textureFormat[jj].m_supported = true;
  869. fmt = jj;
  870. }
  871. }
  872. BX_TRACE(" %3d: %8x %s", ii, internalFmt, getName( (TextureFormat::Enum)fmt) );
  873. }
  874. }
  875. if (BX_ENABLED(BGFX_CONFIG_DEBUG) )
  876. {
  877. #define GL_GET(_pname, _min) BX_TRACE(" " #_pname " %d (min: %d)", glGet(_pname), _min)
  878. BX_TRACE("Defaults:");
  879. #if BGFX_CONFIG_RENDERER_OPENGL >= 41 || BGFX_CONFIG_RENDERER_OPENGLES
  880. GL_GET(GL_MAX_FRAGMENT_UNIFORM_VECTORS, 16);
  881. GL_GET(GL_MAX_VERTEX_UNIFORM_VECTORS, 128);
  882. GL_GET(GL_MAX_VARYING_VECTORS, 8);
  883. #else
  884. GL_GET(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, 16 * 4);
  885. GL_GET(GL_MAX_VERTEX_UNIFORM_COMPONENTS, 128 * 4);
  886. GL_GET(GL_MAX_VARYING_FLOATS, 8 * 4);
  887. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 41 || BGFX_CONFIG_RENDERER_OPENGLES
  888. GL_GET(GL_MAX_VERTEX_ATTRIBS, 8);
  889. GL_GET(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, 8);
  890. GL_GET(GL_MAX_CUBE_MAP_TEXTURE_SIZE, 16);
  891. GL_GET(GL_MAX_TEXTURE_IMAGE_UNITS, 8);
  892. GL_GET(GL_MAX_TEXTURE_SIZE, 64);
  893. GL_GET(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, 0);
  894. GL_GET(GL_MAX_RENDERBUFFER_SIZE, 1);
  895. #undef GL_GET
  896. BX_TRACE(" Vendor: %s", m_vendor);
  897. BX_TRACE(" Renderer: %s", m_renderer);
  898. BX_TRACE(" Version: %s", m_version);
  899. BX_TRACE("GLSL version: %s", m_glslVersion);
  900. }
  901. // Initial binary shader hash depends on driver version.
  902. m_hash = ( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT)
  903. ^ (uint64_t(getGLStringHash(GL_VENDOR ) )<<32)
  904. ^ (uint64_t(getGLStringHash(GL_RENDERER) )<<0 )
  905. ^ (uint64_t(getGLStringHash(GL_VERSION ) )<<16)
  906. ;
  907. if (BX_ENABLED(BGFX_CONFIG_RENDERER_USE_EXTENSIONS) )
  908. {
  909. const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
  910. glGetError(); // ignore error if glGetString returns NULL.
  911. if (NULL != extensions)
  912. {
  913. char name[1024];
  914. const char* pos = extensions;
  915. const char* end = extensions + strlen(extensions);
  916. uint32_t index = 0;
  917. while (pos < end)
  918. {
  919. uint32_t len;
  920. const char* space = strchr(pos, ' ');
  921. if (NULL != space)
  922. {
  923. len = bx::uint32_min(sizeof(name), (uint32_t)(space - pos) );
  924. }
  925. else
  926. {
  927. len = bx::uint32_min(sizeof(name), (uint32_t)strlen(pos) );
  928. }
  929. strncpy(name, pos, len);
  930. name[len] = '\0';
  931. bool supported = false;
  932. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  933. {
  934. Extension& extension = s_extension[ii];
  935. if (!extension.m_supported
  936. && extension.m_initialize)
  937. {
  938. const char* ext = name;
  939. if (0 == strncmp(ext, "GL_", 3) ) // skip GL_
  940. {
  941. ext += 3;
  942. }
  943. if (0 == strcmp(ext, extension.m_name) )
  944. {
  945. extension.m_supported = true;
  946. supported = true;
  947. break;
  948. }
  949. }
  950. }
  951. BX_TRACE("GL_EXTENSION %3d%s: %s", index, supported ? " (supported)" : "", name);
  952. BX_UNUSED(supported);
  953. pos += len+1;
  954. ++index;
  955. }
  956. BX_TRACE("Supported extensions:");
  957. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  958. {
  959. if (s_extension[ii].m_supported)
  960. {
  961. BX_TRACE("\t%2d: %s", ii, s_extension[ii].m_name);
  962. }
  963. }
  964. }
  965. }
  966. bool bc123Supported = 0
  967. || s_extension[Extension::EXT_texture_compression_s3tc ].m_supported
  968. || s_extension[Extension::MOZ_WEBGL_compressed_texture_s3tc ].m_supported
  969. || s_extension[Extension::WEBGL_compressed_texture_s3tc ].m_supported
  970. || s_extension[Extension::WEBKIT_WEBGL_compressed_texture_s3tc].m_supported
  971. ;
  972. s_textureFormat[TextureFormat::BC1].m_supported |= bc123Supported
  973. || s_extension[Extension::ANGLE_texture_compression_dxt1].m_supported
  974. || s_extension[Extension::EXT_texture_compression_dxt1 ].m_supported
  975. ;
  976. if (!s_textureFormat[TextureFormat::BC1].m_supported
  977. && ( s_textureFormat[TextureFormat::BC2].m_supported || s_textureFormat[TextureFormat::BC3].m_supported) )
  978. {
  979. // If RGBA_S3TC_DXT1 is not supported, maybe RGB_S3TC_DXT1 is?
  980. for (GLint ii = 0; ii < numCmpFormats; ++ii)
  981. {
  982. if (GL_COMPRESSED_RGB_S3TC_DXT1_EXT == cmpFormat[ii])
  983. {
  984. setTextureFormat(TextureFormat::BC1, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
  985. s_textureFormat[TextureFormat::BC1].m_supported = true;
  986. break;
  987. }
  988. }
  989. }
  990. s_textureFormat[TextureFormat::BC2].m_supported |= bc123Supported
  991. || s_extension[Extension::ANGLE_texture_compression_dxt3 ].m_supported
  992. || s_extension[Extension::CHROMIUM_texture_compression_dxt3].m_supported
  993. ;
  994. s_textureFormat[TextureFormat::BC3].m_supported |= bc123Supported
  995. || s_extension[Extension::ANGLE_texture_compression_dxt5 ].m_supported
  996. || s_extension[Extension::CHROMIUM_texture_compression_dxt5].m_supported
  997. ;
  998. if (s_extension[Extension::EXT_texture_compression_latc].m_supported)
  999. {
  1000. setTextureFormat(TextureFormat::BC4, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_COMPRESSED_LUMINANCE_LATC1_EXT);
  1001. setTextureFormat(TextureFormat::BC5, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT);
  1002. }
  1003. if (s_extension[Extension::ARB_texture_compression_rgtc].m_supported
  1004. || s_extension[Extension::EXT_texture_compression_rgtc].m_supported)
  1005. {
  1006. setTextureFormat(TextureFormat::BC4, GL_COMPRESSED_RED_RGTC1, GL_COMPRESSED_RED_RGTC1);
  1007. setTextureFormat(TextureFormat::BC5, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2);
  1008. }
  1009. bool etc1Supported = 0
  1010. || s_extension[Extension::OES_compressed_ETC1_RGB8_texture].m_supported
  1011. || s_extension[Extension::WEBGL_compressed_texture_etc1 ].m_supported
  1012. ;
  1013. s_textureFormat[TextureFormat::ETC1].m_supported |= etc1Supported;
  1014. bool etc2Supported = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1015. || s_extension[Extension::ARB_ES3_compatibility].m_supported
  1016. ;
  1017. s_textureFormat[TextureFormat::ETC2 ].m_supported |= etc2Supported;
  1018. s_textureFormat[TextureFormat::ETC2A ].m_supported |= etc2Supported;
  1019. s_textureFormat[TextureFormat::ETC2A1].m_supported |= etc2Supported;
  1020. if (!s_textureFormat[TextureFormat::ETC1].m_supported
  1021. && s_textureFormat[TextureFormat::ETC2].m_supported)
  1022. {
  1023. // When ETC2 is supported override ETC1 texture format settings.
  1024. s_textureFormat[TextureFormat::ETC1].m_internalFmt = GL_COMPRESSED_RGB8_ETC2;
  1025. s_textureFormat[TextureFormat::ETC1].m_fmt = GL_COMPRESSED_RGB8_ETC2;
  1026. s_textureFormat[TextureFormat::ETC1].m_supported = true;
  1027. }
  1028. bool ptc1Supported = 0
  1029. || s_extension[Extension::IMG_texture_compression_pvrtc ].m_supported
  1030. || s_extension[Extension::WEBGL_compressed_texture_pvrtc].m_supported
  1031. ;
  1032. s_textureFormat[TextureFormat::PTC12 ].m_supported |= ptc1Supported;
  1033. s_textureFormat[TextureFormat::PTC14 ].m_supported |= ptc1Supported;
  1034. s_textureFormat[TextureFormat::PTC12A].m_supported |= ptc1Supported;
  1035. s_textureFormat[TextureFormat::PTC14A].m_supported |= ptc1Supported;
  1036. bool ptc2Supported = s_extension[Extension::IMG_texture_compression_pvrtc2].m_supported;
  1037. s_textureFormat[TextureFormat::PTC22].m_supported |= ptc2Supported;
  1038. s_textureFormat[TextureFormat::PTC24].m_supported |= ptc2Supported;
  1039. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) )
  1040. {
  1041. setTextureFormat(TextureFormat::D32, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
  1042. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) )
  1043. {
  1044. setTextureFormat(TextureFormat::RGBA16F, GL_RGBA, GL_RGBA, GL_HALF_FLOAT);
  1045. if (BX_ENABLED(BX_PLATFORM_IOS) )
  1046. {
  1047. setTextureFormat(TextureFormat::D16, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT);
  1048. setTextureFormat(TextureFormat::D24S8, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
  1049. }
  1050. }
  1051. }
  1052. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  1053. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  1054. {
  1055. setTextureFormat(TextureFormat::R16, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT);
  1056. setTextureFormat(TextureFormat::RG16, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT);
  1057. setTextureFormat(TextureFormat::RGBA16, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT);
  1058. setTextureFormat(TextureFormat::R32, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT);
  1059. setTextureFormat(TextureFormat::RG32, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT);
  1060. setTextureFormat(TextureFormat::RGBA32, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT);
  1061. }
  1062. if (s_extension[Extension::EXT_texture_format_BGRA8888 ].m_supported
  1063. || s_extension[Extension::EXT_bgra ].m_supported
  1064. || s_extension[Extension::IMG_texture_format_BGRA8888 ].m_supported
  1065. || s_extension[Extension::APPLE_texture_format_BGRA8888].m_supported)
  1066. {
  1067. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1068. {
  1069. m_readPixelsFmt = GL_BGRA;
  1070. }
  1071. s_textureFormat[TextureFormat::BGRA8].m_fmt = GL_BGRA;
  1072. // Mixing GLES and GL extensions here. OpenGL EXT_bgra and
  1073. // APPLE_texture_format_BGRA8888 wants
  1074. // format to be BGRA but internal format to stay RGBA, but
  1075. // EXT_texture_format_BGRA8888 wants both format and internal
  1076. // format to be BGRA.
  1077. //
  1078. // Reference:
  1079. // https://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_format_BGRA8888.txt
  1080. // https://www.opengl.org/registry/specs/EXT/bgra.txt
  1081. // https://www.khronos.org/registry/gles/extensions/APPLE/APPLE_texture_format_BGRA8888.txt
  1082. if (!s_extension[Extension::EXT_bgra ].m_supported
  1083. && !s_extension[Extension::APPLE_texture_format_BGRA8888].m_supported)
  1084. {
  1085. s_textureFormat[TextureFormat::BGRA8].m_internalFmt = GL_BGRA;
  1086. }
  1087. if (!isTextureFormatValid(TextureFormat::BGRA8) )
  1088. {
  1089. // Revert back to RGBA if texture can't be created.
  1090. setTextureFormat(TextureFormat::BGRA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
  1091. }
  1092. }
  1093. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN)
  1094. || !isTextureFormatValid(TextureFormat::R8) )
  1095. {
  1096. // GL core has to use GL_R8 Issue#208, GLES2 has to use GL_LUMINANCE issue#226
  1097. s_textureFormat[TextureFormat::R8].m_internalFmt = GL_LUMINANCE;
  1098. s_textureFormat[TextureFormat::R8].m_fmt = GL_LUMINANCE;
  1099. }
  1100. if (!BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  1101. {
  1102. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  1103. {
  1104. if (TextureFormat::Unknown != ii
  1105. && TextureFormat::UnknownDepth != ii)
  1106. {
  1107. s_textureFormat[ii].m_supported = isTextureFormatValid( (TextureFormat::Enum)ii);
  1108. }
  1109. }
  1110. }
  1111. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  1112. {
  1113. g_caps.formats[ii] = s_textureFormat[ii].m_supported ? 1 : 0;
  1114. }
  1115. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30) || s_extension[Extension::OES_texture_3D].m_supported
  1116. ? BGFX_CAPS_TEXTURE_3D
  1117. : 0
  1118. ;
  1119. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30) || s_extension[Extension::EXT_shadow_samplers].m_supported
  1120. ? BGFX_CAPS_TEXTURE_COMPARE_ALL
  1121. : 0
  1122. ;
  1123. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30) || s_extension[Extension::OES_vertex_half_float].m_supported
  1124. ? BGFX_CAPS_VERTEX_ATTRIB_HALF
  1125. : 0
  1126. ;
  1127. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30) || s_extension[Extension::EXT_frag_depth].m_supported
  1128. ? BGFX_CAPS_FRAGMENT_DEPTH
  1129. : 0
  1130. ;
  1131. g_caps.supported |= s_extension[Extension::ARB_draw_buffers_blend].m_supported
  1132. ? BGFX_CAPS_BLEND_INDEPENDENT
  1133. : 0
  1134. ;
  1135. g_caps.supported |= s_extension[Extension::INTEL_fragment_shader_ordering].m_supported
  1136. ? BGFX_CAPS_FRAGMENT_ORDERING
  1137. : 0
  1138. ;
  1139. g_caps.maxTextureSize = glGet(GL_MAX_TEXTURE_SIZE);
  1140. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  1141. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1142. || s_extension[Extension::EXT_draw_buffers].m_supported)
  1143. {
  1144. g_caps.maxFBAttachments = bx::uint32_min(glGet(GL_MAX_COLOR_ATTACHMENTS), BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS);
  1145. }
  1146. m_vaoSupport = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1147. || s_extension[Extension::ARB_vertex_array_object].m_supported
  1148. || s_extension[Extension::OES_vertex_array_object].m_supported
  1149. ;
  1150. if (BX_ENABLED(BX_PLATFORM_NACL) )
  1151. {
  1152. m_vaoSupport &= NULL != glGenVertexArrays
  1153. && NULL != glDeleteVertexArrays
  1154. && NULL != glBindVertexArray
  1155. ;
  1156. }
  1157. if (m_vaoSupport)
  1158. {
  1159. GL_CHECK(glGenVertexArrays(1, &m_vao) );
  1160. }
  1161. m_samplerObjectSupport = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1162. || s_extension[Extension::ARB_sampler_objects].m_supported
  1163. ;
  1164. m_shadowSamplersSupport = !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1165. || s_extension[Extension::EXT_shadow_samplers].m_supported
  1166. ;
  1167. m_programBinarySupport = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1168. || s_extension[Extension::ARB_get_program_binary].m_supported
  1169. || s_extension[Extension::OES_get_program_binary].m_supported
  1170. || s_extension[Extension::IMG_shader_binary ].m_supported
  1171. ;
  1172. m_textureSwizzleSupport = false
  1173. || s_extension[Extension::ARB_texture_swizzle].m_supported
  1174. || s_extension[Extension::EXT_texture_swizzle].m_supported
  1175. ;
  1176. m_depthTextureSupport = !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1177. || s_extension[Extension::ANGLE_depth_texture ].m_supported
  1178. || s_extension[Extension::CHROMIUM_depth_texture ].m_supported
  1179. || s_extension[Extension::GOOGLE_depth_texture ].m_supported
  1180. || s_extension[Extension::OES_depth_texture ].m_supported
  1181. || s_extension[Extension::MOZ_WEBGL_depth_texture ].m_supported
  1182. || s_extension[Extension::WEBGL_depth_texture ].m_supported
  1183. || s_extension[Extension::WEBKIT_WEBGL_depth_texture].m_supported
  1184. ;
  1185. g_caps.supported |= m_depthTextureSupport
  1186. ? BGFX_CAPS_TEXTURE_COMPARE_LEQUAL
  1187. : 0
  1188. ;
  1189. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  1190. || s_extension[Extension::ARB_compute_shader].m_supported
  1191. ? BGFX_CAPS_COMPUTE
  1192. : 0
  1193. ;
  1194. g_caps.supported |= GlContext::isSwapChainSupported()
  1195. ? BGFX_CAPS_SWAP_CHAIN
  1196. : 0
  1197. ;
  1198. if (s_extension[Extension::EXT_texture_filter_anisotropic].m_supported)
  1199. {
  1200. GL_CHECK(glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_maxAnisotropyDefault) );
  1201. }
  1202. if (s_extension[Extension::ARB_texture_multisample].m_supported
  1203. || s_extension[Extension::ANGLE_framebuffer_multisample].m_supported)
  1204. {
  1205. GL_CHECK(glGetIntegerv(GL_MAX_SAMPLES, &m_maxMsaa) );
  1206. }
  1207. if (s_extension[Extension::OES_read_format].m_supported
  1208. && (s_extension[Extension::IMG_read_format].m_supported || s_extension[Extension::EXT_read_format_bgra].m_supported) )
  1209. {
  1210. m_readPixelsFmt = GL_BGRA;
  1211. }
  1212. else
  1213. {
  1214. m_readPixelsFmt = GL_RGBA;
  1215. }
  1216. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  1217. {
  1218. g_caps.supported |= BGFX_CAPS_INSTANCING;
  1219. }
  1220. else
  1221. {
  1222. if (!BX_ENABLED(BX_PLATFORM_IOS) )
  1223. {
  1224. if (s_extension[Extension::ARB_instanced_arrays].m_supported
  1225. || s_extension[Extension::ANGLE_instanced_arrays].m_supported)
  1226. {
  1227. if (NULL != glVertexAttribDivisor
  1228. && NULL != glDrawArraysInstanced
  1229. && NULL != glDrawElementsInstanced)
  1230. {
  1231. g_caps.supported |= BGFX_CAPS_INSTANCING;
  1232. }
  1233. }
  1234. }
  1235. if (0 == (g_caps.supported & BGFX_CAPS_INSTANCING) )
  1236. {
  1237. glVertexAttribDivisor = stubVertexAttribDivisor;
  1238. glDrawArraysInstanced = stubDrawArraysInstanced;
  1239. glDrawElementsInstanced = stubDrawElementsInstanced;
  1240. }
  1241. }
  1242. #if BGFX_CONFIG_RENDERER_OPENGL
  1243. if (s_extension[Extension::ARB_debug_output].m_supported
  1244. || s_extension[Extension::KHR_debug].m_supported)
  1245. {
  1246. GL_CHECK(glDebugMessageCallback(debugProcCb, NULL) );
  1247. GL_CHECK(glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM_ARB, 0, NULL, GL_TRUE) );
  1248. }
  1249. if (s_extension[Extension::ARB_seamless_cube_map].m_supported)
  1250. {
  1251. GL_CHECK(glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS) );
  1252. }
  1253. if (s_extension[Extension::ARB_depth_clamp].m_supported)
  1254. {
  1255. GL_CHECK(glEnable(GL_DEPTH_CLAMP) );
  1256. }
  1257. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1258. if (NULL == glFrameTerminatorGREMEDY
  1259. || !s_extension[Extension::GREMEDY_frame_terminator].m_supported)
  1260. {
  1261. glFrameTerminatorGREMEDY = stubFrameTerminatorGREMEDY;
  1262. }
  1263. if (NULL == glInsertEventMarker
  1264. || !s_extension[Extension::EXT_debug_marker].m_supported)
  1265. {
  1266. glInsertEventMarker = (NULL != glStringMarkerGREMEDY && s_extension[Extension::GREMEDY_string_marker].m_supported)
  1267. ? stubInsertEventMarkerGREMEDY
  1268. : stubInsertEventMarker
  1269. ;
  1270. }
  1271. if (NULL == glObjectLabel)
  1272. {
  1273. glObjectLabel = stubObjectLabel;
  1274. }
  1275. if (NULL == glInvalidateFramebuffer)
  1276. {
  1277. glInvalidateFramebuffer = stubInvalidateFramebuffer;
  1278. }
  1279. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1280. {
  1281. m_queries.create();
  1282. }
  1283. // Init reserved part of view name.
  1284. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
  1285. {
  1286. bx::snprintf(s_viewName[ii], BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1, "%3d ", ii);
  1287. }
  1288. ovrPostReset();
  1289. }
  1290. void shutdown()
  1291. {
  1292. ovrPreReset();
  1293. m_ovr.shutdown();
  1294. if (m_vaoSupport)
  1295. {
  1296. GL_CHECK(glBindVertexArray(0) );
  1297. GL_CHECK(glDeleteVertexArrays(1, &m_vao) );
  1298. m_vao = 0;
  1299. }
  1300. captureFinish();
  1301. invalidateCache();
  1302. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1303. {
  1304. m_queries.destroy();
  1305. }
  1306. destroyMsaaFbo();
  1307. m_glctx.destroy();
  1308. m_flip = false;
  1309. unloadRenderDoc(m_renderdocdll);
  1310. }
  1311. RendererType::Enum getRendererType() const BX_OVERRIDE
  1312. {
  1313. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1314. {
  1315. return RendererType::OpenGL;
  1316. }
  1317. return RendererType::OpenGLES;
  1318. }
  1319. const char* getRendererName() const BX_OVERRIDE
  1320. {
  1321. return BGFX_RENDERER_OPENGL_NAME;
  1322. }
  1323. void flip()
  1324. {
  1325. if (m_flip)
  1326. {
  1327. for (uint32_t ii = 1, num = m_numWindows; ii < num; ++ii)
  1328. {
  1329. m_glctx.swap(m_frameBuffers[m_windows[ii].idx].m_swapChain);
  1330. }
  1331. if (!m_ovr.swap() )
  1332. {
  1333. m_glctx.swap();
  1334. }
  1335. }
  1336. }
  1337. void createIndexBuffer(IndexBufferHandle _handle, Memory* _mem, uint8_t /*_flags*/) BX_OVERRIDE
  1338. {
  1339. m_indexBuffers[_handle.idx].create(_mem->size, _mem->data);
  1340. }
  1341. void destroyIndexBuffer(IndexBufferHandle _handle) BX_OVERRIDE
  1342. {
  1343. m_indexBuffers[_handle.idx].destroy();
  1344. }
  1345. void createVertexDecl(VertexDeclHandle _handle, const VertexDecl& _decl) BX_OVERRIDE
  1346. {
  1347. VertexDecl& decl = m_vertexDecls[_handle.idx];
  1348. memcpy(&decl, &_decl, sizeof(VertexDecl) );
  1349. dump(decl);
  1350. }
  1351. void destroyVertexDecl(VertexDeclHandle /*_handle*/) BX_OVERRIDE
  1352. {
  1353. }
  1354. void createVertexBuffer(VertexBufferHandle _handle, Memory* _mem, VertexDeclHandle _declHandle, uint8_t /*_flags*/) BX_OVERRIDE
  1355. {
  1356. m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _declHandle);
  1357. }
  1358. void destroyVertexBuffer(VertexBufferHandle _handle) BX_OVERRIDE
  1359. {
  1360. m_vertexBuffers[_handle.idx].destroy();
  1361. }
  1362. void createDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size, uint8_t /*_flags*/) BX_OVERRIDE
  1363. {
  1364. m_indexBuffers[_handle.idx].create(_size, NULL);
  1365. }
  1366. void updateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem) BX_OVERRIDE
  1367. {
  1368. m_indexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  1369. }
  1370. void destroyDynamicIndexBuffer(IndexBufferHandle _handle) BX_OVERRIDE
  1371. {
  1372. m_indexBuffers[_handle.idx].destroy();
  1373. }
  1374. void createDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size, uint8_t /*_flags*/) BX_OVERRIDE
  1375. {
  1376. VertexDeclHandle decl = BGFX_INVALID_HANDLE;
  1377. m_vertexBuffers[_handle.idx].create(_size, NULL, decl);
  1378. }
  1379. void updateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem) BX_OVERRIDE
  1380. {
  1381. m_vertexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  1382. }
  1383. void destroyDynamicVertexBuffer(VertexBufferHandle _handle) BX_OVERRIDE
  1384. {
  1385. m_vertexBuffers[_handle.idx].destroy();
  1386. }
  1387. void createShader(ShaderHandle _handle, Memory* _mem) BX_OVERRIDE
  1388. {
  1389. m_shaders[_handle.idx].create(_mem);
  1390. }
  1391. void destroyShader(ShaderHandle _handle) BX_OVERRIDE
  1392. {
  1393. m_shaders[_handle.idx].destroy();
  1394. }
  1395. void createProgram(ProgramHandle _handle, ShaderHandle _vsh, ShaderHandle _fsh) BX_OVERRIDE
  1396. {
  1397. ShaderGL dummyFragmentShader;
  1398. m_program[_handle.idx].create(m_shaders[_vsh.idx], isValid(_fsh) ? m_shaders[_fsh.idx] : dummyFragmentShader);
  1399. }
  1400. void destroyProgram(ProgramHandle _handle) BX_OVERRIDE
  1401. {
  1402. m_program[_handle.idx].destroy();
  1403. }
  1404. void createTexture(TextureHandle _handle, Memory* _mem, uint32_t _flags, uint8_t _skip) BX_OVERRIDE
  1405. {
  1406. m_textures[_handle.idx].create(_mem, _flags, _skip);
  1407. }
  1408. void updateTextureBegin(TextureHandle /*_handle*/, uint8_t /*_side*/, uint8_t /*_mip*/) BX_OVERRIDE
  1409. {
  1410. }
  1411. void updateTexture(TextureHandle _handle, uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem) BX_OVERRIDE
  1412. {
  1413. m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _pitch, _mem);
  1414. }
  1415. void updateTextureEnd() BX_OVERRIDE
  1416. {
  1417. }
  1418. void destroyTexture(TextureHandle _handle) BX_OVERRIDE
  1419. {
  1420. m_textures[_handle.idx].destroy();
  1421. }
  1422. void createFrameBuffer(FrameBufferHandle _handle, uint8_t _num, const TextureHandle* _textureHandles) BX_OVERRIDE
  1423. {
  1424. m_frameBuffers[_handle.idx].create(_num, _textureHandles);
  1425. }
  1426. void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) BX_OVERRIDE
  1427. {
  1428. uint16_t denseIdx = m_numWindows++;
  1429. m_windows[denseIdx] = _handle;
  1430. m_frameBuffers[_handle.idx].create(denseIdx, _nwh, _width, _height, _depthFormat);
  1431. }
  1432. void destroyFrameBuffer(FrameBufferHandle _handle) BX_OVERRIDE
  1433. {
  1434. uint16_t denseIdx = m_frameBuffers[_handle.idx].destroy();
  1435. if (UINT16_MAX != denseIdx)
  1436. {
  1437. --m_numWindows;
  1438. if (m_numWindows > 1)
  1439. {
  1440. FrameBufferHandle handle = m_windows[m_numWindows];
  1441. m_windows[denseIdx] = handle;
  1442. m_frameBuffers[handle.idx].m_denseIdx = denseIdx;
  1443. }
  1444. }
  1445. }
  1446. void createUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name) BX_OVERRIDE
  1447. {
  1448. if (NULL != m_uniforms[_handle.idx])
  1449. {
  1450. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  1451. }
  1452. uint32_t size = g_uniformTypeSize[_type]*_num;
  1453. void* data = BX_ALLOC(g_allocator, size);
  1454. memset(data, 0, size);
  1455. m_uniforms[_handle.idx] = data;
  1456. m_uniformReg.add(_handle, _name, m_uniforms[_handle.idx]);
  1457. }
  1458. void destroyUniform(UniformHandle _handle) BX_OVERRIDE
  1459. {
  1460. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  1461. m_uniforms[_handle.idx] = NULL;
  1462. }
  1463. void saveScreenShot(const char* _filePath) BX_OVERRIDE
  1464. {
  1465. uint32_t length = m_resolution.m_width*m_resolution.m_height*4;
  1466. uint8_t* data = (uint8_t*)BX_ALLOC(g_allocator, length);
  1467. uint32_t width = m_resolution.m_width;
  1468. uint32_t height = m_resolution.m_height;
  1469. GL_CHECK(glReadPixels(0
  1470. , 0
  1471. , width
  1472. , height
  1473. , m_readPixelsFmt
  1474. , GL_UNSIGNED_BYTE
  1475. , data
  1476. ) );
  1477. if (GL_RGBA == m_readPixelsFmt)
  1478. {
  1479. imageSwizzleBgra8(width, height, width*4, data, data);
  1480. }
  1481. g_callback->screenShot(_filePath
  1482. , width
  1483. , height
  1484. , width*4
  1485. , data
  1486. , length
  1487. , true
  1488. );
  1489. BX_FREE(g_allocator, data);
  1490. }
  1491. void updateViewName(uint8_t _id, const char* _name) BX_OVERRIDE
  1492. {
  1493. bx::strlcpy(&s_viewName[_id][BGFX_CONFIG_MAX_VIEW_NAME_RESERVED]
  1494. , _name
  1495. , BX_COUNTOF(s_viewName[0])-BGFX_CONFIG_MAX_VIEW_NAME_RESERVED
  1496. );
  1497. }
  1498. void updateUniform(uint16_t _loc, const void* _data, uint32_t _size) BX_OVERRIDE
  1499. {
  1500. memcpy(m_uniforms[_loc], _data, _size);
  1501. }
  1502. void setMarker(const char* _marker, uint32_t _size) BX_OVERRIDE
  1503. {
  1504. GL_CHECK(glInsertEventMarker(_size, _marker) );
  1505. }
  1506. void submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) BX_OVERRIDE;
  1507. void blitSetup(TextVideoMemBlitter& _blitter) BX_OVERRIDE
  1508. {
  1509. if (0 != m_vao)
  1510. {
  1511. GL_CHECK(glBindVertexArray(m_vao) );
  1512. }
  1513. uint32_t width = m_resolution.m_width;
  1514. uint32_t height = m_resolution.m_height;
  1515. if (m_ovr.isEnabled() )
  1516. {
  1517. m_ovr.getSize(width, height);
  1518. }
  1519. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  1520. GL_CHECK(glViewport(0, 0, width, height) );
  1521. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  1522. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  1523. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  1524. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  1525. GL_CHECK(glDisable(GL_CULL_FACE) );
  1526. GL_CHECK(glDisable(GL_BLEND) );
  1527. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  1528. ProgramGL& program = m_program[_blitter.m_program.idx];
  1529. GL_CHECK(glUseProgram(program.m_id) );
  1530. GL_CHECK(glUniform1i(program.m_sampler[0], 0) );
  1531. float proj[16];
  1532. mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  1533. GL_CHECK(glUniformMatrix4fv(program.m_predefined[0].m_loc
  1534. , 1
  1535. , GL_FALSE
  1536. , proj
  1537. ) );
  1538. GL_CHECK(glActiveTexture(GL_TEXTURE0) );
  1539. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_textures[_blitter.m_texture.idx].m_id) );
  1540. }
  1541. void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) BX_OVERRIDE
  1542. {
  1543. const uint32_t numVertices = _numIndices*4/6;
  1544. if (0 < numVertices)
  1545. {
  1546. m_indexBuffers[_blitter.m_ib->handle.idx].update(0, _numIndices*2, _blitter.m_ib->data);
  1547. m_vertexBuffers[_blitter.m_vb->handle.idx].update(0, numVertices*_blitter.m_decl.m_stride, _blitter.m_vb->data);
  1548. VertexBufferGL& vb = m_vertexBuffers[_blitter.m_vb->handle.idx];
  1549. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  1550. IndexBufferGL& ib = m_indexBuffers[_blitter.m_ib->handle.idx];
  1551. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  1552. ProgramGL& program = m_program[_blitter.m_program.idx];
  1553. program.bindAttributes(_blitter.m_decl, 0);
  1554. GL_CHECK(glDrawElements(GL_TRIANGLES
  1555. , _numIndices
  1556. , GL_UNSIGNED_SHORT
  1557. , (void*)0
  1558. ) );
  1559. }
  1560. }
  1561. void updateResolution(const Resolution& _resolution)
  1562. {
  1563. bool recenter = !!(_resolution.m_flags & BGFX_RESET_HMD_RECENTER);
  1564. m_maxAnisotropy = !!(_resolution.m_flags & BGFX_RESET_MAXANISOTROPY)
  1565. ? m_maxAnisotropyDefault
  1566. : 0.0f
  1567. ;
  1568. uint32_t flags = _resolution.m_flags & ~(BGFX_RESET_HMD_RECENTER | BGFX_RESET_MAXANISOTROPY);
  1569. if (m_resolution.m_width != _resolution.m_width
  1570. || m_resolution.m_height != _resolution.m_height
  1571. || m_resolution.m_flags != flags)
  1572. {
  1573. m_textVideoMem.resize(false, _resolution.m_width, _resolution.m_height);
  1574. m_textVideoMem.clear();
  1575. m_resolution = _resolution;
  1576. m_resolution.m_flags = flags;
  1577. uint32_t msaa = (m_resolution.m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  1578. msaa = bx::uint32_min(m_maxMsaa, msaa == 0 ? 0 : 1<<msaa);
  1579. bool vsync = !!(m_resolution.m_flags&BGFX_RESET_VSYNC);
  1580. setRenderContextSize(_resolution.m_width, _resolution.m_height, msaa, vsync);
  1581. updateCapture();
  1582. ovrPreReset();
  1583. ovrPostReset();
  1584. }
  1585. if (recenter)
  1586. {
  1587. m_ovr.recenter();
  1588. }
  1589. }
  1590. void setShaderUniform4f(uint8_t /*_flags*/, uint16_t _regIndex, const void* _val, uint16_t _numRegs)
  1591. {
  1592. GL_CHECK(glUniform4fv(_regIndex
  1593. , _numRegs
  1594. , (const GLfloat*)_val
  1595. ) );
  1596. }
  1597. void setShaderUniform4x4f(uint8_t /*_flags*/, uint16_t _regIndex, const void* _val, uint16_t _numRegs)
  1598. {
  1599. GL_CHECK(glUniformMatrix4fv(_regIndex
  1600. , _numRegs
  1601. , GL_FALSE
  1602. , (const GLfloat*)_val
  1603. ) );
  1604. }
  1605. uint32_t setFrameBuffer(FrameBufferHandle _fbh, uint32_t _height, uint8_t _discard = BGFX_CLEAR_NONE, bool _msaa = true)
  1606. {
  1607. if (isValid(m_fbh)
  1608. && m_fbh.idx != _fbh.idx
  1609. && (BGFX_CLEAR_NONE != m_fbDiscard || m_rtMsaa) )
  1610. {
  1611. FrameBufferGL& frameBuffer = m_frameBuffers[m_fbh.idx];
  1612. if (m_rtMsaa)
  1613. {
  1614. frameBuffer.resolve();
  1615. }
  1616. if (BGFX_CLEAR_NONE != m_fbDiscard)
  1617. {
  1618. frameBuffer.discard(m_fbDiscard);
  1619. }
  1620. m_fbDiscard = BGFX_CLEAR_NONE;
  1621. }
  1622. m_glctx.makeCurrent(NULL);
  1623. if (!isValid(_fbh) )
  1624. {
  1625. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  1626. }
  1627. else
  1628. {
  1629. FrameBufferGL& frameBuffer = m_frameBuffers[_fbh.idx];
  1630. _height = frameBuffer.m_height;
  1631. if (UINT16_MAX != frameBuffer.m_denseIdx)
  1632. {
  1633. m_glctx.makeCurrent(frameBuffer.m_swapChain);
  1634. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1635. }
  1636. else
  1637. {
  1638. m_glctx.makeCurrent(NULL);
  1639. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer.m_fbo[0]) );
  1640. }
  1641. }
  1642. m_fbh = _fbh;
  1643. m_fbDiscard = _discard;
  1644. m_rtMsaa = _msaa;
  1645. return _height;
  1646. }
  1647. uint32_t getNumRt() const
  1648. {
  1649. if (isValid(m_fbh) )
  1650. {
  1651. const FrameBufferGL& frameBuffer = m_frameBuffers[m_fbh.idx];
  1652. return frameBuffer.m_num;
  1653. }
  1654. return 1;
  1655. }
  1656. void createMsaaFbo(uint32_t _width, uint32_t _height, uint32_t _msaa)
  1657. {
  1658. if (0 == m_msaaBackBufferFbo // iOS
  1659. && 1 < _msaa)
  1660. {
  1661. GL_CHECK(glGenFramebuffers(1, &m_msaaBackBufferFbo) );
  1662. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  1663. GL_CHECK(glGenRenderbuffers(BX_COUNTOF(m_msaaBackBufferRbos), m_msaaBackBufferRbos) );
  1664. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_msaaBackBufferRbos[0]) );
  1665. GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_RGBA8, _width, _height) );
  1666. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_msaaBackBufferRbos[1]) );
  1667. GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_DEPTH24_STENCIL8, _width, _height) );
  1668. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_msaaBackBufferRbos[0]) );
  1669. GLenum attachment = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1670. ? GL_DEPTH_STENCIL_ATTACHMENT
  1671. : GL_DEPTH_ATTACHMENT
  1672. ;
  1673. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, m_msaaBackBufferRbos[1]) );
  1674. BX_CHECK(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)
  1675. , "glCheckFramebufferStatus failed 0x%08x"
  1676. , glCheckFramebufferStatus(GL_FRAMEBUFFER)
  1677. );
  1678. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  1679. }
  1680. }
  1681. void destroyMsaaFbo()
  1682. {
  1683. if (m_backBufferFbo != m_msaaBackBufferFbo // iOS
  1684. && 0 != m_msaaBackBufferFbo)
  1685. {
  1686. GL_CHECK(glDeleteFramebuffers(1, &m_msaaBackBufferFbo) );
  1687. m_msaaBackBufferFbo = 0;
  1688. if (0 != m_msaaBackBufferRbos[0])
  1689. {
  1690. GL_CHECK(glDeleteRenderbuffers(BX_COUNTOF(m_msaaBackBufferRbos), m_msaaBackBufferRbos) );
  1691. m_msaaBackBufferRbos[0] = 0;
  1692. m_msaaBackBufferRbos[1] = 0;
  1693. }
  1694. }
  1695. }
  1696. void blitMsaaFbo()
  1697. {
  1698. if (m_backBufferFbo != m_msaaBackBufferFbo // iOS
  1699. && 0 != m_msaaBackBufferFbo)
  1700. {
  1701. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  1702. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  1703. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_msaaBackBufferFbo) );
  1704. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) );
  1705. uint32_t width = m_resolution.m_width;
  1706. uint32_t height = m_resolution.m_height;
  1707. GLenum filter = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30)
  1708. ? GL_NEAREST
  1709. : GL_LINEAR
  1710. ;
  1711. GL_CHECK(glBlitFramebuffer(0
  1712. , 0
  1713. , width
  1714. , height
  1715. , 0
  1716. , 0
  1717. , width
  1718. , height
  1719. , GL_COLOR_BUFFER_BIT
  1720. , filter
  1721. ) );
  1722. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  1723. }
  1724. }
  1725. void setRenderContextSize(uint32_t _width, uint32_t _height, uint32_t _msaa = 0, bool _vsync = false)
  1726. {
  1727. if (_width != 0
  1728. || _height != 0)
  1729. {
  1730. if (!m_glctx.isValid() )
  1731. {
  1732. m_glctx.create(_width, _height);
  1733. #if BX_PLATFORM_IOS
  1734. // iOS: need to figure out how to deal with FBO created by context.
  1735. m_backBufferFbo = m_msaaBackBufferFbo = m_glctx.getFbo();
  1736. #endif // BX_PLATFORM_IOS
  1737. }
  1738. else
  1739. {
  1740. destroyMsaaFbo();
  1741. m_glctx.resize(_width, _height, _vsync);
  1742. createMsaaFbo(_width, _height, _msaa);
  1743. }
  1744. }
  1745. m_flip = true;
  1746. }
  1747. void invalidateCache()
  1748. {
  1749. if (m_vaoSupport)
  1750. {
  1751. m_vaoStateCache.invalidate();
  1752. }
  1753. if ( (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  1754. && m_samplerObjectSupport)
  1755. {
  1756. m_samplerStateCache.invalidate();
  1757. }
  1758. }
  1759. void setSamplerState(uint32_t _stage, uint32_t _numMips, uint32_t _flags)
  1760. {
  1761. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  1762. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  1763. {
  1764. if (0 == (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) )
  1765. {
  1766. _flags &= ~BGFX_TEXTURE_RESERVED_MASK;
  1767. _flags &= BGFX_TEXTURE_SAMPLER_BITS_MASK;
  1768. _flags |= _numMips<<BGFX_TEXTURE_RESERVED_SHIFT;
  1769. GLuint sampler = m_samplerStateCache.find(_flags);
  1770. if (UINT32_MAX == sampler)
  1771. {
  1772. sampler = m_samplerStateCache.add(_flags);
  1773. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]) );
  1774. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]) );
  1775. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_WRAP_R, s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]) );
  1776. const uint32_t mag = (_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT;
  1777. const uint32_t min = (_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT;
  1778. const uint32_t mip = (_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT;
  1779. GLenum minFilter = s_textureFilterMin[min][1 < _numMips ? mip+1 : 0];
  1780. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, s_textureFilterMag[mag]) );
  1781. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, minFilter) );
  1782. if (0 != (_flags & (BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC) )
  1783. && 0.0f < m_maxAnisotropy)
  1784. {
  1785. GL_CHECK(glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, m_maxAnisotropy) );
  1786. }
  1787. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1788. || m_shadowSamplersSupport)
  1789. {
  1790. const uint32_t cmpFunc = (_flags&BGFX_TEXTURE_COMPARE_MASK)>>BGFX_TEXTURE_COMPARE_SHIFT;
  1791. if (0 == cmpFunc)
  1792. {
  1793. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE) );
  1794. }
  1795. else
  1796. {
  1797. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) );
  1798. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, s_cmpFunc[cmpFunc]) );
  1799. }
  1800. }
  1801. }
  1802. GL_CHECK(glBindSampler(_stage, sampler) );
  1803. }
  1804. else
  1805. {
  1806. GL_CHECK(glBindSampler(_stage, 0) );
  1807. }
  1808. }
  1809. }
  1810. void ovrPostReset()
  1811. {
  1812. #if BGFX_CONFIG_USE_OVR
  1813. if (m_resolution.m_flags & (BGFX_RESET_HMD|BGFX_RESET_HMD_DEBUG) )
  1814. {
  1815. ovrGLConfig config;
  1816. config.OGL.Header.API = ovrRenderAPI_OpenGL;
  1817. # if OVR_VERSION > OVR_VERSION_043
  1818. config.OGL.Header.BackBufferSize.w = m_resolution.m_width;
  1819. config.OGL.Header.BackBufferSize.h = m_resolution.m_height;
  1820. # else
  1821. config.OGL.Header.RTSize.w = m_resolution.m_width;
  1822. config.OGL.Header.RTSize.h = m_resolution.m_height;
  1823. # endif // OVR_VERSION > OVR_VERSION_043
  1824. config.OGL.Header.Multisample = 0;
  1825. config.OGL.Window = g_bgfxHwnd;
  1826. config.OGL.DC = GetDC(g_bgfxHwnd);
  1827. if (m_ovr.postReset(g_bgfxHwnd, &config.Config, !!(m_resolution.m_flags & BGFX_RESET_HMD_DEBUG) ) )
  1828. {
  1829. uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
  1830. const Memory* mem = alloc(size);
  1831. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  1832. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  1833. bx::write(&writer, magic);
  1834. TextureCreate tc;
  1835. tc.m_flags = BGFX_TEXTURE_RT;
  1836. tc.m_width = m_ovr.m_rtSize.w;
  1837. tc.m_height = m_ovr.m_rtSize.h;
  1838. tc.m_sides = 0;
  1839. tc.m_depth = 0;
  1840. tc.m_numMips = 1;
  1841. tc.m_format = uint8_t(bgfx::TextureFormat::BGRA8);
  1842. tc.m_cubeMap = false;
  1843. tc.m_mem = NULL;
  1844. bx::write(&writer, tc);
  1845. m_ovrRT.create(mem, tc.m_flags, 0);
  1846. release(mem);
  1847. m_ovrFbo = m_msaaBackBufferFbo;
  1848. GL_CHECK(glGenFramebuffers(1, &m_msaaBackBufferFbo) );
  1849. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  1850. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  1851. , GL_COLOR_ATTACHMENT0
  1852. , GL_TEXTURE_2D
  1853. , m_ovrRT.m_id
  1854. , 0
  1855. ) );
  1856. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_ovrFbo) );
  1857. ovrGLTexture texture;
  1858. texture.OGL.Header.API = ovrRenderAPI_OpenGL;
  1859. texture.OGL.Header.TextureSize = m_ovr.m_rtSize;
  1860. texture.OGL.TexId = m_ovrRT.m_id;
  1861. m_ovr.postReset(texture.Texture);
  1862. }
  1863. }
  1864. #endif // BGFX_CONFIG_USE_OVR
  1865. }
  1866. void ovrPreReset()
  1867. {
  1868. #if BGFX_CONFIG_USE_OVR
  1869. m_ovr.preReset();
  1870. if (m_ovr.isEnabled() )
  1871. {
  1872. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1873. GL_CHECK(glDeleteFramebuffers(1, &m_msaaBackBufferFbo) );
  1874. m_msaaBackBufferFbo = m_ovrFbo;
  1875. m_ovrFbo = 0;
  1876. m_ovrRT.destroy();
  1877. }
  1878. #endif // BGFX_CONFIG_USE_OVR
  1879. }
  1880. void updateCapture()
  1881. {
  1882. if (m_resolution.m_flags&BGFX_RESET_CAPTURE)
  1883. {
  1884. m_captureSize = m_resolution.m_width*m_resolution.m_height*4;
  1885. m_capture = BX_REALLOC(g_allocator, m_capture, m_captureSize);
  1886. g_callback->captureBegin(m_resolution.m_width, m_resolution.m_height, m_resolution.m_width*4, TextureFormat::BGRA8, true);
  1887. }
  1888. else
  1889. {
  1890. captureFinish();
  1891. }
  1892. }
  1893. void capture()
  1894. {
  1895. if (NULL != m_capture)
  1896. {
  1897. GL_CHECK(glReadPixels(0
  1898. , 0
  1899. , m_resolution.m_width
  1900. , m_resolution.m_height
  1901. , m_readPixelsFmt
  1902. , GL_UNSIGNED_BYTE
  1903. , m_capture
  1904. ) );
  1905. g_callback->captureFrame(m_capture, m_captureSize);
  1906. }
  1907. }
  1908. void captureFinish()
  1909. {
  1910. if (NULL != m_capture)
  1911. {
  1912. g_callback->captureEnd();
  1913. BX_FREE(g_allocator, m_capture);
  1914. m_capture = NULL;
  1915. m_captureSize = 0;
  1916. }
  1917. }
  1918. bool programFetchFromCache(GLuint programId, uint64_t _id)
  1919. {
  1920. _id ^= m_hash;
  1921. bool cached = false;
  1922. if (m_programBinarySupport)
  1923. {
  1924. uint32_t length = g_callback->cacheReadSize(_id);
  1925. cached = length > 0;
  1926. if (cached)
  1927. {
  1928. void* data = BX_ALLOC(g_allocator, length);
  1929. if (g_callback->cacheRead(_id, data, length) )
  1930. {
  1931. bx::MemoryReader reader(data, length);
  1932. GLenum format;
  1933. bx::read(&reader, format);
  1934. GL_CHECK(glProgramBinary(programId, format, reader.getDataPtr(), (GLsizei)reader.remaining() ) );
  1935. }
  1936. BX_FREE(g_allocator, data);
  1937. }
  1938. #if BGFX_CONFIG_RENDERER_OPENGL
  1939. GL_CHECK(glProgramParameteri(programId, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE) );
  1940. #endif // BGFX_CONFIG_RENDERER_OPENGL
  1941. }
  1942. return cached;
  1943. }
  1944. void programCache(GLuint programId, uint64_t _id)
  1945. {
  1946. _id ^= m_hash;
  1947. if (m_programBinarySupport)
  1948. {
  1949. GLint programLength;
  1950. GLenum format;
  1951. GL_CHECK(glGetProgramiv(programId, GL_PROGRAM_BINARY_LENGTH, &programLength) );
  1952. if (0 < programLength)
  1953. {
  1954. uint32_t length = programLength + 4;
  1955. uint8_t* data = (uint8_t*)BX_ALLOC(g_allocator, length);
  1956. GL_CHECK(glGetProgramBinary(programId, programLength, NULL, &format, &data[4]) );
  1957. *(uint32_t*)data = format;
  1958. g_callback->cacheWrite(_id, data, length);
  1959. BX_FREE(g_allocator, data);
  1960. }
  1961. }
  1962. }
  1963. void commit(ConstantBuffer& _constantBuffer)
  1964. {
  1965. _constantBuffer.reset();
  1966. for (;;)
  1967. {
  1968. uint32_t opcode = _constantBuffer.read();
  1969. if (UniformType::End == opcode)
  1970. {
  1971. break;
  1972. }
  1973. UniformType::Enum type;
  1974. uint16_t ignore;
  1975. uint16_t num;
  1976. uint16_t copy;
  1977. ConstantBuffer::decodeOpcode(opcode, type, ignore, num, copy);
  1978. const char* data;
  1979. if (copy)
  1980. {
  1981. data = _constantBuffer.read(g_uniformTypeSize[type]*num);
  1982. }
  1983. else
  1984. {
  1985. UniformHandle handle;
  1986. memcpy(&handle, _constantBuffer.read(sizeof(UniformHandle) ), sizeof(UniformHandle) );
  1987. data = (const char*)m_uniforms[handle.idx];
  1988. }
  1989. uint32_t loc = _constantBuffer.read();
  1990. #define CASE_IMPLEMENT_UNIFORM(_uniform, _glsuffix, _dxsuffix, _type) \
  1991. case UniformType::_uniform: \
  1992. { \
  1993. _type* value = (_type*)data; \
  1994. GL_CHECK(glUniform##_glsuffix(loc, num, value) ); \
  1995. } \
  1996. break;
  1997. #define CASE_IMPLEMENT_UNIFORM_T(_uniform, _glsuffix, _dxsuffix, _type) \
  1998. case UniformType::_uniform: \
  1999. { \
  2000. _type* value = (_type*)data; \
  2001. GL_CHECK(glUniform##_glsuffix(loc, num, GL_FALSE, value) ); \
  2002. } \
  2003. break;
  2004. switch (type)
  2005. {
  2006. // case ConstantType::Uniform1iv:
  2007. // {
  2008. // int* value = (int*)data;
  2009. // BX_TRACE("Uniform1iv sampler %d, loc %d (num %d, copy %d)", *value, loc, num, copy);
  2010. // GL_CHECK(glUniform1iv(loc, num, value) );
  2011. // }
  2012. // break;
  2013. CASE_IMPLEMENT_UNIFORM(Uniform1i, 1iv, I, int);
  2014. CASE_IMPLEMENT_UNIFORM(Uniform1f, 1fv, F, float);
  2015. CASE_IMPLEMENT_UNIFORM(Uniform1iv, 1iv, I, int);
  2016. CASE_IMPLEMENT_UNIFORM(Uniform1fv, 1fv, F, float);
  2017. CASE_IMPLEMENT_UNIFORM(Uniform2fv, 2fv, F, float);
  2018. CASE_IMPLEMENT_UNIFORM(Uniform3fv, 3fv, F, float);
  2019. CASE_IMPLEMENT_UNIFORM(Uniform4fv, 4fv, F, float);
  2020. CASE_IMPLEMENT_UNIFORM_T(Uniform3x3fv, Matrix3fv, F, float);
  2021. CASE_IMPLEMENT_UNIFORM_T(Uniform4x4fv, Matrix4fv, F, float);
  2022. case UniformType::End:
  2023. break;
  2024. default:
  2025. BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", _constantBuffer.getPos(), opcode, type, loc, num, copy);
  2026. break;
  2027. }
  2028. #undef CASE_IMPLEMENT_UNIFORM
  2029. #undef CASE_IMPLEMENT_UNIFORM_T
  2030. }
  2031. }
  2032. void clearQuad(ClearQuad& _clearQuad, const Rect& _rect, const Clear& _clear, uint32_t _height, const float _palette[][4])
  2033. {
  2034. uint32_t numMrt = 1;
  2035. FrameBufferHandle fbh = m_fbh;
  2036. if (isValid(fbh) )
  2037. {
  2038. const FrameBufferGL& fb = m_frameBuffers[fbh.idx];
  2039. numMrt = bx::uint32_max(1, fb.m_num);
  2040. }
  2041. if (1 == numMrt)
  2042. {
  2043. GLuint flags = 0;
  2044. if (BGFX_CLEAR_COLOR & _clear.m_flags)
  2045. {
  2046. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  2047. {
  2048. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE-1, _clear.m_index[0]);
  2049. const float* rgba = _palette[index];
  2050. const float rr = rgba[0];
  2051. const float gg = rgba[1];
  2052. const float bb = rgba[2];
  2053. const float aa = rgba[3];
  2054. GL_CHECK(glClearColor(rr, gg, bb, aa) );
  2055. }
  2056. else
  2057. {
  2058. float rr = _clear.m_index[0]*1.0f/255.0f;
  2059. float gg = _clear.m_index[1]*1.0f/255.0f;
  2060. float bb = _clear.m_index[2]*1.0f/255.0f;
  2061. float aa = _clear.m_index[3]*1.0f/255.0f;
  2062. GL_CHECK(glClearColor(rr, gg, bb, aa) );
  2063. }
  2064. flags |= GL_COLOR_BUFFER_BIT;
  2065. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  2066. }
  2067. if (BGFX_CLEAR_DEPTH & _clear.m_flags)
  2068. {
  2069. flags |= GL_DEPTH_BUFFER_BIT;
  2070. GL_CHECK(glClearDepth(_clear.m_depth) );
  2071. GL_CHECK(glDepthMask(GL_TRUE) );
  2072. }
  2073. if (BGFX_CLEAR_STENCIL & _clear.m_flags)
  2074. {
  2075. flags |= GL_STENCIL_BUFFER_BIT;
  2076. GL_CHECK(glClearStencil(_clear.m_stencil) );
  2077. }
  2078. if (0 != flags)
  2079. {
  2080. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  2081. GL_CHECK(glScissor(_rect.m_x, _height-_rect.m_height-_rect.m_y, _rect.m_width, _rect.m_height) );
  2082. GL_CHECK(glClear(flags) );
  2083. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  2084. }
  2085. }
  2086. else
  2087. {
  2088. const GLuint defaultVao = m_vao;
  2089. if (0 != defaultVao)
  2090. {
  2091. GL_CHECK(glBindVertexArray(defaultVao) );
  2092. }
  2093. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  2094. GL_CHECK(glDisable(GL_CULL_FACE) );
  2095. GL_CHECK(glDisable(GL_BLEND) );
  2096. GLboolean colorMask = !!(BGFX_CLEAR_COLOR & _clear.m_flags);
  2097. GL_CHECK(glColorMask(colorMask, colorMask, colorMask, colorMask) );
  2098. if (BGFX_CLEAR_DEPTH & _clear.m_flags)
  2099. {
  2100. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  2101. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  2102. GL_CHECK(glDepthMask(GL_TRUE) );
  2103. }
  2104. else
  2105. {
  2106. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  2107. }
  2108. if (BGFX_CLEAR_STENCIL & _clear.m_flags)
  2109. {
  2110. GL_CHECK(glEnable(GL_STENCIL_TEST) );
  2111. GL_CHECK(glStencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS, _clear.m_stencil, 0xff) );
  2112. GL_CHECK(glStencilOpSeparate(GL_FRONT_AND_BACK, GL_REPLACE, GL_REPLACE, GL_REPLACE) );
  2113. }
  2114. else
  2115. {
  2116. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  2117. }
  2118. VertexBufferGL& vb = m_vertexBuffers[_clearQuad.m_vb->handle.idx];
  2119. VertexDecl& vertexDecl = m_vertexDecls[_clearQuad.m_vb->decl.idx];
  2120. {
  2121. struct Vertex
  2122. {
  2123. float m_x;
  2124. float m_y;
  2125. float m_z;
  2126. };
  2127. Vertex* vertex = (Vertex*)_clearQuad.m_vb->data;
  2128. BX_CHECK(vertexDecl.m_stride == sizeof(Vertex), "Stride/Vertex mismatch (stride %d, sizeof(Vertex) %d)", vertexDecl.m_stride, sizeof(Vertex) );
  2129. const float depth = _clear.m_depth;
  2130. vertex->m_x = -1.0f;
  2131. vertex->m_y = -1.0f;
  2132. vertex->m_z = depth;
  2133. vertex++;
  2134. vertex->m_x = 1.0f;
  2135. vertex->m_y = -1.0f;
  2136. vertex->m_z = depth;
  2137. vertex++;
  2138. vertex->m_x = -1.0f;
  2139. vertex->m_y = 1.0f;
  2140. vertex->m_z = depth;
  2141. vertex++;
  2142. vertex->m_x = 1.0f;
  2143. vertex->m_y = 1.0f;
  2144. vertex->m_z = depth;
  2145. }
  2146. vb.update(0, 4*_clearQuad.m_decl.m_stride, _clearQuad.m_vb->data);
  2147. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  2148. ProgramGL& program = m_program[_clearQuad.m_program[numMrt-1].idx];
  2149. GL_CHECK(glUseProgram(program.m_id) );
  2150. program.bindAttributes(vertexDecl, 0);
  2151. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  2152. {
  2153. float mrtClear[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS][4];
  2154. for (uint32_t ii = 0; ii < numMrt; ++ii)
  2155. {
  2156. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_CLEAR_COLOR_PALETTE-1, _clear.m_index[ii]);
  2157. memcpy(mrtClear[ii], _palette[index], 16);
  2158. }
  2159. GL_CHECK(glUniform4fv(0, numMrt, mrtClear[0]) );
  2160. }
  2161. else
  2162. {
  2163. float rgba[4] =
  2164. {
  2165. _clear.m_index[0]*1.0f/255.0f,
  2166. _clear.m_index[1]*1.0f/255.0f,
  2167. _clear.m_index[2]*1.0f/255.0f,
  2168. _clear.m_index[3]*1.0f/255.0f,
  2169. };
  2170. GL_CHECK(glUniform4fv(0, 1, rgba) );
  2171. }
  2172. GL_CHECK(glDrawArrays(GL_TRIANGLE_STRIP
  2173. , 0
  2174. , 4
  2175. ) );
  2176. }
  2177. }
  2178. void* m_renderdocdll;
  2179. uint16_t m_numWindows;
  2180. FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  2181. IndexBufferGL m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS];
  2182. VertexBufferGL m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
  2183. ShaderGL m_shaders[BGFX_CONFIG_MAX_SHADERS];
  2184. ProgramGL m_program[BGFX_CONFIG_MAX_PROGRAMS];
  2185. TextureGL m_textures[BGFX_CONFIG_MAX_TEXTURES];
  2186. VertexDecl m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_DECLS];
  2187. FrameBufferGL m_frameBuffers[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  2188. UniformRegistry m_uniformReg;
  2189. void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS];
  2190. QueriesGL m_queries;
  2191. VaoStateCache m_vaoStateCache;
  2192. SamplerStateCache m_samplerStateCache;
  2193. TextVideoMem m_textVideoMem;
  2194. bool m_rtMsaa;
  2195. FrameBufferHandle m_fbh;
  2196. uint32_t m_fbDiscard;
  2197. Resolution m_resolution;
  2198. void* m_capture;
  2199. uint32_t m_captureSize;
  2200. float m_maxAnisotropy;
  2201. float m_maxAnisotropyDefault;
  2202. int32_t m_maxMsaa;
  2203. GLuint m_vao;
  2204. bool m_vaoSupport;
  2205. bool m_samplerObjectSupport;
  2206. bool m_shadowSamplersSupport;
  2207. bool m_programBinarySupport;
  2208. bool m_textureSwizzleSupport;
  2209. bool m_depthTextureSupport;
  2210. bool m_flip;
  2211. uint64_t m_hash;
  2212. GLenum m_readPixelsFmt;
  2213. GLuint m_backBufferFbo;
  2214. GLuint m_msaaBackBufferFbo;
  2215. GLuint m_msaaBackBufferRbos[2];
  2216. GlContext m_glctx;
  2217. const char* m_vendor;
  2218. const char* m_renderer;
  2219. const char* m_version;
  2220. const char* m_glslVersion;
  2221. OVR m_ovr;
  2222. TextureGL m_ovrRT;
  2223. GLint m_ovrFbo;
  2224. };
  2225. RendererContextGL* s_renderGL;
  2226. RendererContextI* rendererCreateGL()
  2227. {
  2228. s_renderGL = BX_NEW(g_allocator, RendererContextGL);
  2229. s_renderGL->init();
  2230. return s_renderGL;
  2231. }
  2232. void rendererDestroyGL()
  2233. {
  2234. s_renderGL->shutdown();
  2235. BX_DELETE(g_allocator, s_renderGL);
  2236. s_renderGL = NULL;
  2237. }
  2238. const char* glslTypeName(GLuint _type)
  2239. {
  2240. #define GLSL_TYPE(_ty) case _ty: return #_ty
  2241. switch (_type)
  2242. {
  2243. GLSL_TYPE(GL_INT);
  2244. GLSL_TYPE(GL_INT_VEC2);
  2245. GLSL_TYPE(GL_INT_VEC3);
  2246. GLSL_TYPE(GL_INT_VEC4);
  2247. GLSL_TYPE(GL_UNSIGNED_INT);
  2248. GLSL_TYPE(GL_UNSIGNED_INT_VEC2);
  2249. GLSL_TYPE(GL_UNSIGNED_INT_VEC3);
  2250. GLSL_TYPE(GL_UNSIGNED_INT_VEC4);
  2251. GLSL_TYPE(GL_FLOAT);
  2252. GLSL_TYPE(GL_FLOAT_VEC2);
  2253. GLSL_TYPE(GL_FLOAT_VEC3);
  2254. GLSL_TYPE(GL_FLOAT_VEC4);
  2255. GLSL_TYPE(GL_FLOAT_MAT2);
  2256. GLSL_TYPE(GL_FLOAT_MAT3);
  2257. GLSL_TYPE(GL_FLOAT_MAT4);
  2258. // GLSL_TYPE(GL_FLOAT_MAT2x3);
  2259. // GLSL_TYPE(GL_FLOAT_MAT2x4);
  2260. // GLSL_TYPE(GL_FLOAT_MAT3x2);
  2261. // GLSL_TYPE(GL_FLOAT_MAT3x4);
  2262. // GLSL_TYPE(GL_FLOAT_MAT4x2);
  2263. // GLSL_TYPE(GL_FLOAT_MAT4x3);
  2264. // GLSL_TYPE(GL_SAMPLER_1D);
  2265. GLSL_TYPE(GL_SAMPLER_2D);
  2266. GLSL_TYPE(GL_SAMPLER_3D);
  2267. GLSL_TYPE(GL_SAMPLER_CUBE);
  2268. // GLSL_TYPE(GL_SAMPLER_1D_SHADOW);
  2269. GLSL_TYPE(GL_SAMPLER_2D_SHADOW);
  2270. GLSL_TYPE(GL_IMAGE_1D);
  2271. GLSL_TYPE(GL_IMAGE_2D);
  2272. GLSL_TYPE(GL_IMAGE_3D);
  2273. GLSL_TYPE(GL_IMAGE_CUBE);
  2274. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_1D);
  2275. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_2D);
  2276. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_3D);
  2277. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_CUBE);
  2278. }
  2279. #undef GLSL_TYPE
  2280. BX_CHECK(false, "Unknown GLSL type? %x", _type);
  2281. return "UNKNOWN GLSL TYPE!";
  2282. }
  2283. const char* glEnumName(GLenum _enum)
  2284. {
  2285. #define GLENUM(_ty) case _ty: return #_ty
  2286. switch (_enum)
  2287. {
  2288. GLENUM(GL_TEXTURE);
  2289. GLENUM(GL_RENDERBUFFER);
  2290. GLENUM(GL_INVALID_ENUM);
  2291. GLENUM(GL_INVALID_VALUE);
  2292. GLENUM(GL_INVALID_OPERATION);
  2293. GLENUM(GL_OUT_OF_MEMORY);
  2294. GLENUM(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
  2295. GLENUM(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
  2296. // GLENUM(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER);
  2297. // GLENUM(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER);
  2298. GLENUM(GL_FRAMEBUFFER_UNSUPPORTED);
  2299. }
  2300. #undef GLENUM
  2301. BX_WARN(false, "Unknown enum? %x", _enum);
  2302. return "<GLenum?>";
  2303. }
  2304. UniformType::Enum convertGlType(GLenum _type)
  2305. {
  2306. switch (_type)
  2307. {
  2308. case GL_INT:
  2309. case GL_UNSIGNED_INT:
  2310. return UniformType::Uniform1iv;
  2311. case GL_FLOAT:
  2312. return UniformType::Uniform1fv;
  2313. case GL_FLOAT_VEC2:
  2314. return UniformType::Uniform2fv;
  2315. case GL_FLOAT_VEC3:
  2316. return UniformType::Uniform3fv;
  2317. case GL_FLOAT_VEC4:
  2318. return UniformType::Uniform4fv;
  2319. case GL_FLOAT_MAT2:
  2320. break;
  2321. case GL_FLOAT_MAT3:
  2322. return UniformType::Uniform3x3fv;
  2323. case GL_FLOAT_MAT4:
  2324. return UniformType::Uniform4x4fv;
  2325. // case GL_FLOAT_MAT2x3:
  2326. // case GL_FLOAT_MAT2x4:
  2327. // case GL_FLOAT_MAT3x2:
  2328. // case GL_FLOAT_MAT3x4:
  2329. // case GL_FLOAT_MAT4x2:
  2330. // case GL_FLOAT_MAT4x3:
  2331. // break;
  2332. // case GL_SAMPLER_1D:
  2333. case GL_SAMPLER_2D:
  2334. case GL_SAMPLER_3D:
  2335. case GL_SAMPLER_CUBE:
  2336. // case GL_SAMPLER_1D_SHADOW:
  2337. case GL_SAMPLER_2D_SHADOW:
  2338. case GL_IMAGE_1D:
  2339. case GL_IMAGE_2D:
  2340. case GL_IMAGE_3D:
  2341. case GL_IMAGE_CUBE:
  2342. case GL_UNSIGNED_INT_IMAGE_1D:
  2343. case GL_UNSIGNED_INT_IMAGE_2D:
  2344. case GL_UNSIGNED_INT_IMAGE_3D:
  2345. case GL_UNSIGNED_INT_IMAGE_CUBE:
  2346. return UniformType::Uniform1iv;
  2347. };
  2348. BX_CHECK(false, "Unrecognized GL type 0x%04x.", _type);
  2349. return UniformType::End;
  2350. }
  2351. void ProgramGL::create(const ShaderGL& _vsh, const ShaderGL& _fsh)
  2352. {
  2353. m_id = glCreateProgram();
  2354. BX_TRACE("program create: %d: %d, %d", m_id, _vsh.m_id, _fsh.m_id);
  2355. const uint64_t id = (uint64_t(_vsh.m_hash)<<32) | _fsh.m_hash;
  2356. const bool cached = s_renderGL->programFetchFromCache(m_id, id);
  2357. if (!cached)
  2358. {
  2359. GL_CHECK(glAttachShader(m_id, _vsh.m_id) );
  2360. if (0 != _fsh.m_id)
  2361. {
  2362. GL_CHECK(glAttachShader(m_id, _fsh.m_id) );
  2363. }
  2364. GL_CHECK(glLinkProgram(m_id) );
  2365. GLint linked = 0;
  2366. GL_CHECK(glGetProgramiv(m_id, GL_LINK_STATUS, &linked) );
  2367. if (0 == linked)
  2368. {
  2369. char log[1024];
  2370. GL_CHECK(glGetProgramInfoLog(m_id, sizeof(log), NULL, log) );
  2371. BX_TRACE("%d: %s", linked, log);
  2372. GL_CHECK(glDeleteProgram(m_id) );
  2373. return;
  2374. }
  2375. s_renderGL->programCache(m_id, id);
  2376. }
  2377. init();
  2378. if (!cached)
  2379. {
  2380. // Must be after init, otherwise init might fail to lookup shader
  2381. // info (NVIDIA Tegra 3 OpenGL ES 2.0 14.01003).
  2382. GL_CHECK(glDetachShader(m_id, _vsh.m_id) );
  2383. if (0 != _fsh.m_id)
  2384. {
  2385. GL_CHECK(glDetachShader(m_id, _fsh.m_id) );
  2386. }
  2387. }
  2388. }
  2389. void ProgramGL::destroy()
  2390. {
  2391. if (NULL != m_constantBuffer)
  2392. {
  2393. ConstantBuffer::destroy(m_constantBuffer);
  2394. m_constantBuffer = NULL;
  2395. }
  2396. m_numPredefined = 0;
  2397. if (0 != m_id)
  2398. {
  2399. GL_CHECK(glUseProgram(0) );
  2400. GL_CHECK(glDeleteProgram(m_id) );
  2401. m_id = 0;
  2402. }
  2403. m_vcref.invalidate(s_renderGL->m_vaoStateCache);
  2404. }
  2405. void ProgramGL::init()
  2406. {
  2407. GLint activeAttribs = 0;
  2408. GLint activeUniforms = 0;
  2409. GLint activeBuffers = 0;
  2410. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  2411. GL_CHECK(glBindFragDataLocation(m_id, 0, "bgfx_FragColor") );
  2412. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  2413. if (s_extension[Extension::ARB_program_interface_query].m_supported
  2414. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31) )
  2415. {
  2416. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &activeAttribs ) );
  2417. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_UNIFORM, GL_ACTIVE_RESOURCES, &activeUniforms) );
  2418. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_BUFFER_VARIABLE, GL_ACTIVE_RESOURCES, &activeBuffers ) );
  2419. }
  2420. else
  2421. {
  2422. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTES, &activeAttribs ) );
  2423. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORMS, &activeUniforms) );
  2424. }
  2425. GLint max0, max1;
  2426. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max0) );
  2427. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max1) );
  2428. uint32_t maxLength = bx::uint32_max(max0, max1);
  2429. char* name = (char*)alloca(maxLength + 1);
  2430. BX_TRACE("Program %d", m_id);
  2431. BX_TRACE("Attributes (%d):", activeAttribs);
  2432. for (int32_t ii = 0; ii < activeAttribs; ++ii)
  2433. {
  2434. GLint size;
  2435. GLenum type;
  2436. GL_CHECK(glGetActiveAttrib(m_id, ii, maxLength + 1, NULL, &size, &type, name) );
  2437. BX_TRACE("\t%s %s is at location %d"
  2438. , glslTypeName(type)
  2439. , name
  2440. , glGetAttribLocation(m_id, name)
  2441. );
  2442. }
  2443. m_numPredefined = 0;
  2444. m_numSamplers = 0;
  2445. struct VariableInfo
  2446. {
  2447. GLenum type;
  2448. GLint loc;
  2449. GLint num;
  2450. };
  2451. VariableInfo vi;
  2452. GLenum props[] = { GL_TYPE, GL_LOCATION, GL_ARRAY_SIZE };
  2453. const bool piqSupported = s_extension[Extension::ARB_program_interface_query].m_supported;
  2454. BX_TRACE("Uniforms (%d):", activeUniforms);
  2455. for (int32_t ii = 0; ii < activeUniforms; ++ii)
  2456. {
  2457. GLenum gltype;
  2458. GLint num;
  2459. GLint loc;
  2460. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  2461. || piqSupported)
  2462. {
  2463. GL_CHECK(glGetProgramResourceiv(m_id
  2464. , GL_UNIFORM
  2465. , ii
  2466. , BX_COUNTOF(props)
  2467. , props
  2468. , BX_COUNTOF(props)
  2469. , NULL
  2470. , (GLint*)&vi
  2471. ) );
  2472. GL_CHECK(glGetProgramResourceName(m_id
  2473. , GL_UNIFORM
  2474. , ii
  2475. , maxLength + 1
  2476. , NULL
  2477. , name
  2478. ) );
  2479. gltype = vi.type;
  2480. loc = vi.loc;
  2481. num = vi.num;
  2482. }
  2483. else
  2484. {
  2485. GL_CHECK(glGetActiveUniform(m_id, ii, maxLength + 1, NULL, &num, &gltype, name) );
  2486. loc = glGetUniformLocation(m_id, name);
  2487. }
  2488. num = bx::uint32_max(num, 1);
  2489. int offset = 0;
  2490. char* array = strchr(name, '[');
  2491. if (NULL != array)
  2492. {
  2493. BX_TRACE("--- %s", name);
  2494. *array = '\0';
  2495. array++;
  2496. char* end = strchr(array, ']');
  2497. *end = '\0';
  2498. offset = atoi(array);
  2499. }
  2500. switch (gltype)
  2501. {
  2502. case GL_SAMPLER_2D:
  2503. case GL_SAMPLER_3D:
  2504. case GL_SAMPLER_CUBE:
  2505. case GL_SAMPLER_2D_SHADOW:
  2506. case GL_IMAGE_1D:
  2507. case GL_IMAGE_2D:
  2508. case GL_IMAGE_3D:
  2509. case GL_IMAGE_CUBE:
  2510. case GL_UNSIGNED_INT_IMAGE_1D:
  2511. case GL_UNSIGNED_INT_IMAGE_2D:
  2512. case GL_UNSIGNED_INT_IMAGE_3D:
  2513. case GL_UNSIGNED_INT_IMAGE_CUBE:
  2514. BX_TRACE("Sampler #%d at location %d.", m_numSamplers, loc);
  2515. m_sampler[m_numSamplers] = loc;
  2516. m_numSamplers++;
  2517. break;
  2518. default:
  2519. break;
  2520. }
  2521. PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
  2522. if (PredefinedUniform::Count != predefined)
  2523. {
  2524. m_predefined[m_numPredefined].m_loc = loc;
  2525. m_predefined[m_numPredefined].m_type = predefined;
  2526. m_predefined[m_numPredefined].m_count = num;
  2527. m_numPredefined++;
  2528. }
  2529. else
  2530. {
  2531. const UniformInfo* info = s_renderGL->m_uniformReg.find(name);
  2532. if (NULL != info)
  2533. {
  2534. if (NULL == m_constantBuffer)
  2535. {
  2536. m_constantBuffer = ConstantBuffer::create(1024);
  2537. }
  2538. UniformType::Enum type = convertGlType(gltype);
  2539. m_constantBuffer->writeUniformHandle(type, 0, info->m_handle, num);
  2540. m_constantBuffer->write(loc);
  2541. BX_TRACE("store %s %d", name, info->m_handle);
  2542. }
  2543. }
  2544. BX_TRACE("\tuniform %s %s%s is at location %d, size %d, offset %d"
  2545. , glslTypeName(gltype)
  2546. , name
  2547. , PredefinedUniform::Count != predefined ? "*" : ""
  2548. , loc
  2549. , num
  2550. , offset
  2551. );
  2552. BX_UNUSED(offset);
  2553. }
  2554. if (NULL != m_constantBuffer)
  2555. {
  2556. m_constantBuffer->finish();
  2557. }
  2558. if (s_extension[Extension::ARB_program_interface_query].m_supported
  2559. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31) )
  2560. {
  2561. struct VariableInfo
  2562. {
  2563. GLenum type;
  2564. };
  2565. VariableInfo vi;
  2566. GLenum props[] = { GL_TYPE };
  2567. BX_TRACE("Buffers (%d):", activeBuffers);
  2568. for (int32_t ii = 0; ii < activeBuffers; ++ii)
  2569. {
  2570. GL_CHECK(glGetProgramResourceiv(m_id
  2571. , GL_BUFFER_VARIABLE
  2572. , ii
  2573. , BX_COUNTOF(props)
  2574. , props
  2575. , BX_COUNTOF(props)
  2576. , NULL
  2577. , (GLint*)&vi
  2578. ) );
  2579. GL_CHECK(glGetProgramResourceName(m_id
  2580. , GL_BUFFER_VARIABLE
  2581. , ii
  2582. , maxLength + 1
  2583. , NULL
  2584. , name
  2585. ) );
  2586. BX_TRACE("\t%s %s at %d"
  2587. , glslTypeName(vi.type)
  2588. , name
  2589. , 0 //vi.loc
  2590. );
  2591. }
  2592. }
  2593. memset(m_attributes, 0xff, sizeof(m_attributes) );
  2594. uint32_t used = 0;
  2595. for (uint32_t ii = 0; ii < Attrib::Count; ++ii)
  2596. {
  2597. GLint loc = glGetAttribLocation(m_id, s_attribName[ii]);
  2598. if (-1 != loc)
  2599. {
  2600. BX_TRACE("attr %s: %d", s_attribName[ii], loc);
  2601. m_attributes[ii] = loc;
  2602. m_used[used++] = ii;
  2603. }
  2604. }
  2605. m_used[used] = Attrib::Count;
  2606. used = 0;
  2607. for (uint32_t ii = 0; ii < BX_COUNTOF(s_instanceDataName); ++ii)
  2608. {
  2609. GLuint loc = glGetAttribLocation(m_id, s_instanceDataName[ii]);
  2610. if (GLuint(-1) != loc )
  2611. {
  2612. BX_TRACE("instance data %s: %d", s_instanceDataName[ii], loc);
  2613. m_instanceData[used++] = loc;
  2614. }
  2615. }
  2616. m_instanceData[used] = 0xffff;
  2617. }
  2618. void ProgramGL::bindAttributes(const VertexDecl& _vertexDecl, uint32_t _baseVertex) const
  2619. {
  2620. for (uint32_t ii = 0; Attrib::Count != m_used[ii]; ++ii)
  2621. {
  2622. Attrib::Enum attr = Attrib::Enum(m_used[ii]);
  2623. GLint loc = m_attributes[attr];
  2624. uint8_t num;
  2625. AttribType::Enum type;
  2626. bool normalized;
  2627. bool asInt;
  2628. _vertexDecl.decode(attr, num, type, normalized, asInt);
  2629. if (-1 != loc)
  2630. {
  2631. if (0xff != _vertexDecl.m_attributes[attr])
  2632. {
  2633. GL_CHECK(glEnableVertexAttribArray(loc) );
  2634. GL_CHECK(glVertexAttribDivisor(loc, 0) );
  2635. uint32_t baseVertex = _baseVertex*_vertexDecl.m_stride + _vertexDecl.m_offset[attr];
  2636. GL_CHECK(glVertexAttribPointer(loc, num, s_attribType[type], normalized, _vertexDecl.m_stride, (void*)(uintptr_t)baseVertex) );
  2637. }
  2638. else
  2639. {
  2640. GL_CHECK(glDisableVertexAttribArray(loc) );
  2641. }
  2642. }
  2643. }
  2644. }
  2645. void ProgramGL::bindInstanceData(uint32_t _stride, uint32_t _baseVertex) const
  2646. {
  2647. uint32_t baseVertex = _baseVertex;
  2648. for (uint32_t ii = 0; 0xffff != m_instanceData[ii]; ++ii)
  2649. {
  2650. GLint loc = m_instanceData[ii];
  2651. GL_CHECK(glEnableVertexAttribArray(loc) );
  2652. GL_CHECK(glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, _stride, (void*)(uintptr_t)baseVertex) );
  2653. GL_CHECK(glVertexAttribDivisor(loc, 1) );
  2654. baseVertex += 16;
  2655. }
  2656. }
  2657. void IndexBufferGL::destroy()
  2658. {
  2659. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  2660. GL_CHECK(glDeleteBuffers(1, &m_id) );
  2661. m_vcref.invalidate(s_renderGL->m_vaoStateCache);
  2662. }
  2663. void VertexBufferGL::destroy()
  2664. {
  2665. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  2666. GL_CHECK(glDeleteBuffers(1, &m_id) );
  2667. m_vcref.invalidate(s_renderGL->m_vaoStateCache);
  2668. }
  2669. static void texImage(GLenum _target, GLint _level, GLint _internalFormat, GLsizei _width, GLsizei _height, GLsizei _depth, GLint _border, GLenum _format, GLenum _type, const GLvoid* _data)
  2670. {
  2671. if (_target == GL_TEXTURE_3D)
  2672. {
  2673. GL_CHECK(glTexImage3D(_target, _level, _internalFormat, _width, _height, _depth, _border, _format, _type, _data) );
  2674. }
  2675. else
  2676. {
  2677. BX_UNUSED(_depth);
  2678. GL_CHECK(glTexImage2D(_target, _level, _internalFormat, _width, _height, _border, _format, _type, _data) );
  2679. }
  2680. }
  2681. static void texSubImage(GLenum _target, GLint _level, GLint _xoffset, GLint _yoffset, GLint _zoffset, GLsizei _width, GLsizei _height, GLsizei _depth, GLenum _format, GLenum _type, const GLvoid* _data)
  2682. {
  2683. if (_target == GL_TEXTURE_3D)
  2684. {
  2685. GL_CHECK(glTexSubImage3D(_target, _level, _xoffset, _yoffset, _zoffset, _width, _height, _depth, _format, _type, _data) );
  2686. }
  2687. else
  2688. {
  2689. BX_UNUSED(_zoffset, _depth);
  2690. GL_CHECK(glTexSubImage2D(_target, _level, _xoffset, _yoffset, _width, _height, _format, _type, _data) );
  2691. }
  2692. }
  2693. static void compressedTexImage(GLenum _target, GLint _level, GLenum _internalformat, GLsizei _width, GLsizei _height, GLsizei _depth, GLint _border, GLsizei _imageSize, const GLvoid* _data)
  2694. {
  2695. if (_target == GL_TEXTURE_3D)
  2696. {
  2697. GL_CHECK(glCompressedTexImage3D(_target, _level, _internalformat, _width, _height, _depth, _border, _imageSize, _data) );
  2698. }
  2699. else
  2700. {
  2701. BX_UNUSED(_depth);
  2702. GL_CHECK(glCompressedTexImage2D(_target, _level, _internalformat, _width, _height, _border, _imageSize, _data) );
  2703. }
  2704. }
  2705. static void compressedTexSubImage(GLenum _target, GLint _level, GLint _xoffset, GLint _yoffset, GLint _zoffset, GLsizei _width, GLsizei _height, GLsizei _depth, GLenum _format, GLsizei _imageSize, const GLvoid* _data)
  2706. {
  2707. if (_target == GL_TEXTURE_3D)
  2708. {
  2709. GL_CHECK(glCompressedTexSubImage3D(_target, _level, _xoffset, _yoffset, _zoffset, _width, _height, _depth, _format, _imageSize, _data) );
  2710. }
  2711. else
  2712. {
  2713. BX_UNUSED(_zoffset, _depth);
  2714. GL_CHECK(glCompressedTexSubImage2D(_target, _level, _xoffset, _yoffset, _width, _height, _format, _imageSize, _data) );
  2715. }
  2716. }
  2717. bool TextureGL::init(GLenum _target, uint32_t _width, uint32_t _height, uint8_t _format, uint8_t _numMips, uint32_t _flags)
  2718. {
  2719. m_target = _target;
  2720. m_numMips = _numMips;
  2721. m_flags = _flags;
  2722. m_currentFlags = UINT32_MAX;
  2723. m_width = _width;
  2724. m_height = _height;
  2725. m_requestedFormat = _format;
  2726. m_textureFormat = _format;
  2727. const bool bufferOnly = 0 != (m_flags&BGFX_TEXTURE_RT_BUFFER_ONLY);
  2728. if (!bufferOnly)
  2729. {
  2730. GL_CHECK(glGenTextures(1, &m_id) );
  2731. BX_CHECK(0 != m_id, "Failed to generate texture id.");
  2732. GL_CHECK(glBindTexture(_target, m_id) );
  2733. setSamplerState(_flags);
  2734. const TextureFormatInfo& tfi = s_textureFormat[_format];
  2735. m_fmt = tfi.m_fmt;
  2736. m_type = tfi.m_type;
  2737. const bool compressed = isCompressed(TextureFormat::Enum(_format) );
  2738. const bool decompress = !tfi.m_supported && compressed;
  2739. if (decompress)
  2740. {
  2741. m_textureFormat = (uint8_t)TextureFormat::BGRA8;
  2742. const TextureFormatInfo& tfi = s_textureFormat[TextureFormat::BGRA8];
  2743. m_fmt = tfi.m_fmt;
  2744. m_type = tfi.m_type;
  2745. }
  2746. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  2747. && TextureFormat::BGRA8 == m_textureFormat
  2748. && GL_RGBA == m_fmt
  2749. && s_renderGL->m_textureSwizzleSupport)
  2750. {
  2751. GLint swizzleMask[] = { GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
  2752. GL_CHECK(glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask) );
  2753. }
  2754. }
  2755. const bool renderTarget = 0 != (m_flags&BGFX_TEXTURE_RT_MASK);
  2756. if (renderTarget)
  2757. {
  2758. uint32_t msaaQuality = ( (m_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT);
  2759. msaaQuality = bx::uint32_satsub(msaaQuality, 1);
  2760. msaaQuality = bx::uint32_min(s_renderGL->m_maxMsaa, msaaQuality == 0 ? 0 : 1<<msaaQuality);
  2761. if (0 != msaaQuality
  2762. || bufferOnly)
  2763. {
  2764. GL_CHECK(glGenRenderbuffers(1, &m_rbo) );
  2765. BX_CHECK(0 != m_rbo, "Failed to generate renderbuffer id.");
  2766. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_rbo) );
  2767. if (0 == msaaQuality)
  2768. {
  2769. GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER
  2770. , s_rboFormat[m_textureFormat]
  2771. , _width
  2772. , _height
  2773. ) );
  2774. }
  2775. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  2776. {
  2777. GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER
  2778. , msaaQuality
  2779. , s_rboFormat[m_textureFormat]
  2780. , _width
  2781. , _height
  2782. ) );
  2783. }
  2784. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0) );
  2785. if (bufferOnly)
  2786. {
  2787. // This is render buffer, there is no sampling, no need
  2788. // to create texture.
  2789. return false;
  2790. }
  2791. }
  2792. }
  2793. return true;
  2794. }
  2795. void TextureGL::create(const Memory* _mem, uint32_t _flags, uint8_t _skip)
  2796. {
  2797. ImageContainer imageContainer;
  2798. if (imageParse(imageContainer, _mem->data, _mem->size) )
  2799. {
  2800. uint8_t numMips = imageContainer.m_numMips;
  2801. const uint32_t startLod = bx::uint32_min(_skip, numMips-1);
  2802. numMips -= uint8_t(startLod);
  2803. const ImageBlockInfo& blockInfo = getBlockInfo(TextureFormat::Enum(imageContainer.m_format) );
  2804. const uint32_t textureWidth = bx::uint32_max(blockInfo.blockWidth, imageContainer.m_width >>startLod);
  2805. const uint32_t textureHeight = bx::uint32_max(blockInfo.blockHeight, imageContainer.m_height>>startLod);
  2806. GLenum target = GL_TEXTURE_2D;
  2807. if (imageContainer.m_cubeMap)
  2808. {
  2809. target = GL_TEXTURE_CUBE_MAP;
  2810. }
  2811. else if (imageContainer.m_depth > 1)
  2812. {
  2813. target = GL_TEXTURE_3D;
  2814. }
  2815. if (!init(target
  2816. , textureWidth
  2817. , textureHeight
  2818. , imageContainer.m_format
  2819. , numMips
  2820. , _flags
  2821. ) )
  2822. {
  2823. return;
  2824. }
  2825. target = GL_TEXTURE_CUBE_MAP == m_target ? GL_TEXTURE_CUBE_MAP_POSITIVE_X : m_target;
  2826. const GLenum internalFmt = s_textureFormat[m_textureFormat].m_internalFmt;
  2827. const bool swizzle = true
  2828. && TextureFormat::BGRA8 == m_textureFormat
  2829. && GL_RGBA == m_fmt
  2830. && !s_renderGL->m_textureSwizzleSupport
  2831. ;
  2832. const bool convert = m_textureFormat != m_requestedFormat;
  2833. const bool compressed = isCompressed(TextureFormat::Enum(m_textureFormat) );
  2834. uint32_t blockWidth = 1;
  2835. uint32_t blockHeight = 1;
  2836. if (convert && compressed)
  2837. {
  2838. blockWidth = blockInfo.blockWidth;
  2839. blockHeight = blockInfo.blockHeight;
  2840. }
  2841. BX_TRACE("Texture %3d: %s (requested: %s), %dx%d%s%s."
  2842. , this - s_renderGL->m_textures
  2843. , getName( (TextureFormat::Enum)m_textureFormat)
  2844. , getName( (TextureFormat::Enum)m_requestedFormat)
  2845. , textureWidth
  2846. , textureHeight
  2847. , imageContainer.m_cubeMap ? "x6" : ""
  2848. , 0 != (m_flags&BGFX_TEXTURE_RT_MASK) ? " (render target)" : ""
  2849. );
  2850. BX_WARN(!swizzle && !convert, "Texture %s%s%s from %s to %s."
  2851. , swizzle ? "swizzle" : ""
  2852. , swizzle&&convert ? " and " : ""
  2853. , convert ? "convert" : ""
  2854. , getName( (TextureFormat::Enum)m_requestedFormat)
  2855. , getName( (TextureFormat::Enum)m_textureFormat)
  2856. );
  2857. uint8_t* temp = NULL;
  2858. if (convert || swizzle)
  2859. {
  2860. temp = (uint8_t*)BX_ALLOC(g_allocator, textureWidth*textureHeight*4);
  2861. }
  2862. for (uint8_t side = 0, numSides = imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
  2863. {
  2864. uint32_t width = textureWidth;
  2865. uint32_t height = textureHeight;
  2866. uint32_t depth = imageContainer.m_depth;
  2867. for (uint32_t lod = 0, num = numMips; lod < num; ++lod)
  2868. {
  2869. width = bx::uint32_max(blockWidth, width);
  2870. height = bx::uint32_max(blockHeight, height);
  2871. depth = bx::uint32_max(1, depth);
  2872. ImageMip mip;
  2873. if (imageGetRawData(imageContainer, side, lod+startLod, _mem->data, _mem->size, mip) )
  2874. {
  2875. if (compressed)
  2876. {
  2877. compressedTexImage(target+side
  2878. , lod
  2879. , internalFmt
  2880. , width
  2881. , height
  2882. , depth
  2883. , 0
  2884. , mip.m_size
  2885. , mip.m_data
  2886. );
  2887. }
  2888. else
  2889. {
  2890. const uint8_t* data = mip.m_data;
  2891. if (convert)
  2892. {
  2893. imageDecodeToBgra8(temp, mip.m_data, mip.m_width, mip.m_height, mip.m_width*4, mip.m_format);
  2894. data = temp;
  2895. }
  2896. if (swizzle)
  2897. {
  2898. imageSwizzleBgra8(width, height, mip.m_width*4, data, temp);
  2899. data = temp;
  2900. }
  2901. texImage(target+side
  2902. , lod
  2903. , internalFmt
  2904. , width
  2905. , height
  2906. , depth
  2907. , 0
  2908. , m_fmt
  2909. , m_type
  2910. , data
  2911. );
  2912. }
  2913. }
  2914. else
  2915. {
  2916. if (compressed)
  2917. {
  2918. uint32_t size = bx::uint32_max(1, (width + 3)>>2)
  2919. * bx::uint32_max(1, (height + 3)>>2)
  2920. * 4*4*getBitsPerPixel(TextureFormat::Enum(m_textureFormat) )/8
  2921. ;
  2922. compressedTexImage(target+side
  2923. , lod
  2924. , internalFmt
  2925. , width
  2926. , height
  2927. , depth
  2928. , 0
  2929. , size
  2930. , NULL
  2931. );
  2932. }
  2933. else
  2934. {
  2935. texImage(target+side
  2936. , lod
  2937. , internalFmt
  2938. , width
  2939. , height
  2940. , depth
  2941. , 0
  2942. , m_fmt
  2943. , m_type
  2944. , NULL
  2945. );
  2946. }
  2947. }
  2948. width >>= 1;
  2949. height >>= 1;
  2950. depth >>= 1;
  2951. }
  2952. }
  2953. if (NULL != temp)
  2954. {
  2955. BX_FREE(g_allocator, temp);
  2956. }
  2957. }
  2958. GL_CHECK(glBindTexture(m_target, 0) );
  2959. }
  2960. void TextureGL::destroy()
  2961. {
  2962. if (0 != m_id)
  2963. {
  2964. GL_CHECK(glBindTexture(m_target, 0) );
  2965. GL_CHECK(glDeleteTextures(1, &m_id) );
  2966. m_id = 0;
  2967. }
  2968. if (0 != m_rbo)
  2969. {
  2970. GL_CHECK(glDeleteRenderbuffers(1, &m_rbo) );
  2971. m_rbo = 0;
  2972. }
  2973. }
  2974. void TextureGL::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem)
  2975. {
  2976. BX_UNUSED(_z, _depth);
  2977. const uint32_t bpp = getBitsPerPixel(TextureFormat::Enum(m_textureFormat) );
  2978. const uint32_t rectpitch = _rect.m_width*bpp/8;
  2979. uint32_t srcpitch = UINT16_MAX == _pitch ? rectpitch : _pitch;
  2980. GL_CHECK(glBindTexture(m_target, m_id) );
  2981. GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1) );
  2982. GLenum target = GL_TEXTURE_CUBE_MAP == m_target ? GL_TEXTURE_CUBE_MAP_POSITIVE_X : m_target;
  2983. const bool swizzle = true
  2984. && TextureFormat::BGRA8 == m_textureFormat
  2985. && GL_RGBA == m_fmt
  2986. && !s_renderGL->m_textureSwizzleSupport
  2987. ;
  2988. const bool unpackRowLength = BX_IGNORE_C4127(!!BGFX_CONFIG_RENDERER_OPENGL || s_extension[Extension::EXT_unpack_subimage].m_supported);
  2989. const bool convert = m_textureFormat != m_requestedFormat;
  2990. const bool compressed = isCompressed(TextureFormat::Enum(m_textureFormat) );
  2991. const uint32_t width = _rect.m_width;
  2992. const uint32_t height = _rect.m_height;
  2993. uint8_t* temp = NULL;
  2994. if (convert
  2995. || swizzle
  2996. || !unpackRowLength)
  2997. {
  2998. temp = (uint8_t*)BX_ALLOC(g_allocator, rectpitch*height);
  2999. }
  3000. else if (unpackRowLength)
  3001. {
  3002. GL_CHECK(glPixelStorei(GL_UNPACK_ROW_LENGTH, srcpitch*8/bpp) );
  3003. }
  3004. if (compressed)
  3005. {
  3006. const uint8_t* data = _mem->data;
  3007. if (!unpackRowLength)
  3008. {
  3009. imageCopy(width, height, bpp, srcpitch, data, temp);
  3010. data = temp;
  3011. }
  3012. GL_CHECK(compressedTexSubImage(target+_side
  3013. , _mip
  3014. , _rect.m_x
  3015. , _rect.m_y
  3016. , _z
  3017. , _rect.m_width
  3018. , _rect.m_height
  3019. , _depth
  3020. , m_fmt
  3021. , _mem->size
  3022. , data
  3023. ) );
  3024. }
  3025. else
  3026. {
  3027. const uint8_t* data = _mem->data;
  3028. if (convert)
  3029. {
  3030. imageDecodeToBgra8(temp, data, width, height, srcpitch, m_requestedFormat);
  3031. data = temp;
  3032. srcpitch = rectpitch;
  3033. }
  3034. if (swizzle)
  3035. {
  3036. imageSwizzleBgra8(width, height, srcpitch, data, temp);
  3037. data = temp;
  3038. }
  3039. else if (!unpackRowLength && !convert)
  3040. {
  3041. imageCopy(width, height, bpp, srcpitch, data, temp);
  3042. data = temp;
  3043. }
  3044. GL_CHECK(texSubImage(target+_side
  3045. , _mip
  3046. , _rect.m_x
  3047. , _rect.m_y
  3048. , _z
  3049. , _rect.m_width
  3050. , _rect.m_height
  3051. , _depth
  3052. , m_fmt
  3053. , m_type
  3054. , data
  3055. ) );
  3056. }
  3057. if (NULL != temp)
  3058. {
  3059. BX_FREE(g_allocator, temp);
  3060. }
  3061. }
  3062. void TextureGL::setSamplerState(uint32_t _flags)
  3063. {
  3064. const uint32_t flags = (0 != (BGFX_SAMPLER_DEFAULT_FLAGS & _flags) ? m_flags : _flags) & BGFX_TEXTURE_SAMPLER_BITS_MASK;
  3065. if (flags != m_currentFlags)
  3066. {
  3067. const GLenum target = m_target;
  3068. const uint8_t numMips = m_numMips;
  3069. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_S, s_textureAddress[(flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]) );
  3070. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_T, s_textureAddress[(flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]) );
  3071. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  3072. || s_extension[Extension::APPLE_texture_max_level].m_supported)
  3073. {
  3074. GL_CHECK(glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, numMips-1) );
  3075. }
  3076. if (target == GL_TEXTURE_3D)
  3077. {
  3078. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_R, s_textureAddress[(flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]) );
  3079. }
  3080. const uint32_t mag = (flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT;
  3081. const uint32_t min = (flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT;
  3082. const uint32_t mip = (flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT;
  3083. const GLenum minFilter = s_textureFilterMin[min][1 < numMips ? mip+1 : 0];
  3084. GL_CHECK(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, s_textureFilterMag[mag]) );
  3085. GL_CHECK(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter) );
  3086. if (0 != (flags & (BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC) )
  3087. && 0.0f < s_renderGL->m_maxAnisotropy)
  3088. {
  3089. GL_CHECK(glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, s_renderGL->m_maxAnisotropy) );
  3090. }
  3091. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  3092. || s_renderGL->m_shadowSamplersSupport)
  3093. {
  3094. const uint32_t cmpFunc = (flags&BGFX_TEXTURE_COMPARE_MASK)>>BGFX_TEXTURE_COMPARE_SHIFT;
  3095. if (0 == cmpFunc)
  3096. {
  3097. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_COMPARE_MODE, GL_NONE) );
  3098. }
  3099. else
  3100. {
  3101. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) );
  3102. GL_CHECK(glTexParameteri(m_target, GL_TEXTURE_COMPARE_FUNC, s_cmpFunc[cmpFunc]) );
  3103. }
  3104. }
  3105. m_currentFlags = flags;
  3106. }
  3107. }
  3108. void TextureGL::commit(uint32_t _stage, uint32_t _flags)
  3109. {
  3110. GL_CHECK(glActiveTexture(GL_TEXTURE0+_stage) );
  3111. GL_CHECK(glBindTexture(m_target, m_id) );
  3112. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES)
  3113. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) )
  3114. {
  3115. // GLES2 doesn't have support for sampler object.
  3116. setSamplerState(_flags);
  3117. }
  3118. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  3119. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL < 31) )
  3120. {
  3121. // In case that GL 2.1 sampler object is supported via extension.
  3122. if (s_renderGL->m_samplerObjectSupport)
  3123. {
  3124. s_renderGL->setSamplerState(_stage, m_numMips, _flags);
  3125. }
  3126. else
  3127. {
  3128. setSamplerState(_flags);
  3129. }
  3130. }
  3131. else
  3132. {
  3133. // Everything else has sampler object.
  3134. s_renderGL->setSamplerState(_stage, m_numMips, _flags);
  3135. }
  3136. }
  3137. void writeString(bx::WriterI* _writer, const char* _str)
  3138. {
  3139. bx::write(_writer, _str, (int32_t)strlen(_str) );
  3140. }
  3141. void writeStringf(bx::WriterI* _writer, const char* _format, ...)
  3142. {
  3143. char temp[512];
  3144. va_list argList;
  3145. va_start(argList, _format);
  3146. int len = bx::vsnprintf(temp, BX_COUNTOF(temp), _format, argList);
  3147. va_end(argList);
  3148. bx::write(_writer, temp, len);
  3149. }
  3150. void strins(char* _str, const char* _insert)
  3151. {
  3152. size_t len = strlen(_insert);
  3153. memmove(&_str[len], _str, strlen(_str)+1);
  3154. memcpy(_str, _insert, len);
  3155. }
  3156. void ShaderGL::create(Memory* _mem)
  3157. {
  3158. bx::MemoryReader reader(_mem->data, _mem->size);
  3159. m_hash = bx::hashMurmur2A(_mem->data, _mem->size);
  3160. uint32_t magic;
  3161. bx::read(&reader, magic);
  3162. switch (magic)
  3163. {
  3164. case BGFX_CHUNK_MAGIC_CSH: m_type = GL_COMPUTE_SHADER; break;
  3165. case BGFX_CHUNK_MAGIC_FSH: m_type = GL_FRAGMENT_SHADER; break;
  3166. case BGFX_CHUNK_MAGIC_VSH: m_type = GL_VERTEX_SHADER; break;
  3167. default:
  3168. BGFX_FATAL(false, Fatal::InvalidShader, "Unknown shader format %x.", magic);
  3169. break;
  3170. }
  3171. uint32_t iohash;
  3172. bx::read(&reader, iohash);
  3173. uint16_t count;
  3174. bx::read(&reader, count);
  3175. BX_TRACE("%s Shader consts %d"
  3176. , BGFX_CHUNK_MAGIC_FSH == magic ? "Fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "Vertex" : "Compute"
  3177. , count
  3178. );
  3179. for (uint32_t ii = 0; ii < count; ++ii)
  3180. {
  3181. uint8_t nameSize;
  3182. bx::read(&reader, nameSize);
  3183. char name[256];
  3184. bx::read(&reader, &name, nameSize);
  3185. name[nameSize] = '\0';
  3186. uint8_t type;
  3187. bx::read(&reader, type);
  3188. uint8_t num;
  3189. bx::read(&reader, num);
  3190. uint16_t regIndex;
  3191. bx::read(&reader, regIndex);
  3192. uint16_t regCount;
  3193. bx::read(&reader, regCount);
  3194. }
  3195. uint32_t shaderSize;
  3196. bx::read(&reader, shaderSize);
  3197. m_id = glCreateShader(m_type);
  3198. BX_WARN(0 != m_id, "Failed to create %s shader."
  3199. , BGFX_CHUNK_MAGIC_FSH == magic ? "fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "vertex" : "compute"
  3200. );
  3201. const char* code = (const char*)reader.getDataPtr();
  3202. if (0 != m_id)
  3203. {
  3204. if (GL_COMPUTE_SHADER != m_type)
  3205. {
  3206. int32_t codeLen = (int32_t)strlen(code);
  3207. int32_t tempLen = codeLen + (4<<10);
  3208. char* temp = (char*)alloca(tempLen);
  3209. bx::StaticMemoryBlockWriter writer(temp, tempLen);
  3210. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES)
  3211. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) )
  3212. {
  3213. writeString(&writer
  3214. , "#define flat\n"
  3215. "#define smooth\n"
  3216. "#define noperspective\n"
  3217. );
  3218. bool usesDerivatives = s_extension[Extension::OES_standard_derivatives].m_supported
  3219. && bx::findIdentifierMatch(code, s_OES_standard_derivatives)
  3220. ;
  3221. bool usesFragData = !!bx::findIdentifierMatch(code, "gl_FragData");
  3222. bool usesFragDepth = !!bx::findIdentifierMatch(code, "gl_FragDepth");
  3223. bool usesShadowSamplers = !!bx::findIdentifierMatch(code, s_EXT_shadow_samplers);
  3224. bool usesTexture3D = s_extension[Extension::OES_texture_3D].m_supported
  3225. && bx::findIdentifierMatch(code, s_OES_texture_3D)
  3226. ;
  3227. bool usesTextureLod = !!bx::findIdentifierMatch(code, s_EXT_shader_texture_lod);
  3228. bool usesFragmentOrdering = !!bx::findIdentifierMatch(code, "beginFragmentShaderOrdering");
  3229. if (usesDerivatives)
  3230. {
  3231. writeString(&writer, "#extension GL_OES_standard_derivatives : enable\n");
  3232. }
  3233. if (usesFragData)
  3234. {
  3235. BX_WARN(s_extension[Extension::EXT_draw_buffers].m_supported, "EXT_draw_buffers is used but not supported by GLES2 driver.");
  3236. writeString(&writer
  3237. , "#extension GL_EXT_draw_buffers : enable\n"
  3238. );
  3239. }
  3240. bool insertFragDepth = false;
  3241. if (usesFragDepth)
  3242. {
  3243. BX_WARN(s_extension[Extension::EXT_frag_depth].m_supported, "EXT_frag_depth is used but not supported by GLES2 driver.");
  3244. if (s_extension[Extension::EXT_frag_depth].m_supported)
  3245. {
  3246. writeString(&writer
  3247. , "#extension GL_EXT_frag_depth : enable\n"
  3248. "#define bgfx_FragDepth gl_FragDepthEXT\n"
  3249. );
  3250. char str[128];
  3251. bx::snprintf(str, BX_COUNTOF(str), "%s float gl_FragDepthEXT;\n"
  3252. , s_extension[Extension::OES_fragment_precision_high].m_supported ? "highp" : "mediump"
  3253. );
  3254. writeString(&writer, str);
  3255. }
  3256. else
  3257. {
  3258. insertFragDepth = true;
  3259. }
  3260. }
  3261. if (usesShadowSamplers)
  3262. {
  3263. if (s_renderGL->m_shadowSamplersSupport)
  3264. {
  3265. writeString(&writer
  3266. , "#extension GL_EXT_shadow_samplers : enable\n"
  3267. "#define shadow2D shadow2DEXT\n"
  3268. "#define shadow2DProj shadow2DProjEXT\n"
  3269. );
  3270. }
  3271. else
  3272. {
  3273. writeString(&writer
  3274. , "#define sampler2DShadow sampler2D\n"
  3275. "#define shadow2D(_sampler, _coord) step(_coord.z, texture2D(_sampler, _coord.xy).x)\n"
  3276. "#define shadow2DProj(_sampler, _coord) step(_coord.z/_coord.w, texture2DProj(_sampler, _coord).x)\n"
  3277. );
  3278. }
  3279. }
  3280. if (usesTexture3D)
  3281. {
  3282. writeString(&writer, "#extension GL_OES_texture_3D : enable\n");
  3283. }
  3284. if (usesTextureLod)
  3285. {
  3286. BX_WARN(s_extension[Extension::EXT_shader_texture_lod].m_supported, "EXT_shader_texture_lod is used but not supported by GLES2 driver.");
  3287. if (s_extension[Extension::EXT_shader_texture_lod].m_supported)
  3288. {
  3289. writeString(&writer
  3290. , "#extension GL_EXT_shader_texture_lod : enable\n"
  3291. "#define texture2DLod texture2DLodEXT\n"
  3292. "#define texture2DProjLod texture2DProjLodEXT\n"
  3293. "#define textureCubeLod textureCubeLodEXT\n"
  3294. );
  3295. }
  3296. else
  3297. {
  3298. writeString(&writer
  3299. , "#define texture2DLod(_sampler, _coord, _level) texture2D(_sampler, _coord)\n"
  3300. "#define texture2DProjLod(_sampler, _coord, _level) texture2DProj(_sampler, _coord)\n"
  3301. "#define textureCubeLod(_sampler, _coord, _level) textureCube(_sampler, _coord)\n"
  3302. );
  3303. }
  3304. }
  3305. if (usesFragmentOrdering)
  3306. {
  3307. if (s_extension[Extension::INTEL_fragment_shader_ordering].m_supported)
  3308. {
  3309. writeString(&writer, "#extension GL_INTEL_fragment_shader_ordering : enable\n");
  3310. }
  3311. else
  3312. {
  3313. writeString(&writer, "#define beginFragmentShaderOrdering()\n");
  3314. }
  3315. }
  3316. writeStringf(&writer, "precision %s float;\n"
  3317. , m_type == GL_FRAGMENT_SHADER ? "mediump" : "highp"
  3318. );
  3319. bx::write(&writer, code, codeLen);
  3320. bx::write(&writer, '\0');
  3321. if (insertFragDepth)
  3322. {
  3323. char* entry = strstr(temp, "void main ()");
  3324. if (NULL != entry)
  3325. {
  3326. char* brace = strstr(entry, "{");
  3327. if (NULL != brace)
  3328. {
  3329. const char* end = bx::strmb(brace, '{', '}');
  3330. if (NULL != end)
  3331. {
  3332. strins(brace+1, "\n float bgfx_FragDepth = 0.0;\n");
  3333. }
  3334. }
  3335. }
  3336. }
  3337. // Replace all instances of gl_FragDepth with bgfx_FragDepth.
  3338. for (const char* fragDepth = bx::findIdentifierMatch(temp, "gl_FragDepth"); NULL != fragDepth; fragDepth = bx::findIdentifierMatch(fragDepth, "gl_FragDepth") )
  3339. {
  3340. char* insert = const_cast<char*>(fragDepth);
  3341. strins(insert, "bg");
  3342. memcpy(insert + 2, "fx", 2);
  3343. }
  3344. }
  3345. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  3346. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL <= 21) )
  3347. {
  3348. bool usesTextureLod = s_extension[Extension::ARB_shader_texture_lod].m_supported
  3349. && bx::findIdentifierMatch(code, s_ARB_shader_texture_lod)
  3350. ;
  3351. if (usesTextureLod)
  3352. {
  3353. writeString(&writer, "#version 120\n");
  3354. if (m_type == GL_FRAGMENT_SHADER)
  3355. {
  3356. writeString(&writer, "#extension GL_ARB_shader_texture_lod : enable\n");
  3357. }
  3358. }
  3359. writeString(&writer
  3360. , "#define lowp\n"
  3361. "#define mediump\n"
  3362. "#define highp\n"
  3363. "#define flat\n"
  3364. "#define smooth\n"
  3365. "#define noperspective\n"
  3366. );
  3367. bx::write(&writer, code, codeLen);
  3368. bx::write(&writer, '\0');
  3369. }
  3370. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 31)
  3371. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  3372. {
  3373. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  3374. {
  3375. writeString(&writer
  3376. , "#version 300 es\n"
  3377. "precision mediump float;\n"
  3378. );
  3379. }
  3380. else
  3381. {
  3382. writeString(&writer, "#version 140\n");
  3383. }
  3384. if (m_type == GL_FRAGMENT_SHADER)
  3385. {
  3386. writeString(&writer, "#define varying in\n");
  3387. writeString(&writer, "#define texture2D texture\n");
  3388. writeString(&writer, "#define texture2DLod textureLod\n");
  3389. writeString(&writer, "#define texture2DProj textureProj\n");
  3390. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  3391. {
  3392. writeString(&writer, "#define shadow2D(_sampler, _coord) vec2(textureProj(_sampler, vec4(_coord, 1.0) ) )\n");
  3393. writeString(&writer, "#define shadow2DProj(_sampler, _coord) vec2(textureProj(_sampler, _coord) ) )\n");
  3394. }
  3395. else
  3396. {
  3397. writeString(&writer, "#define shadow2D(_sampler, _coord) (textureProj(_sampler, vec4(_coord, 1.0) ) )\n");
  3398. writeString(&writer, "#define shadow2DProj(_sampler, _coord) (textureProj(_sampler, _coord) ) )\n");
  3399. }
  3400. writeString(&writer, "#define texture3D texture\n");
  3401. writeString(&writer, "#define texture3DLod textureLod\n");
  3402. writeString(&writer, "#define textureCube texture\n");
  3403. writeString(&writer, "#define textureCubeLod textureLod\n");
  3404. uint32_t fragData = 0;
  3405. if (!!bx::findIdentifierMatch(code, "gl_FragData") )
  3406. {
  3407. for (uint32_t ii = 0, num = g_caps.maxFBAttachments; ii < num; ++ii)
  3408. {
  3409. char temp[16];
  3410. bx::snprintf(temp, BX_COUNTOF(temp), "gl_FragData[%d]", ii);
  3411. fragData = bx::uint32_max(fragData, NULL == strstr(code, temp) ? 0 : ii+1);
  3412. }
  3413. BGFX_FATAL(0 != fragData, Fatal::InvalidShader, "Unable to find and patch gl_FragData!");
  3414. }
  3415. if (!!bx::findIdentifierMatch(code, "beginFragmentShaderOrdering") )
  3416. {
  3417. if (s_extension[Extension::INTEL_fragment_shader_ordering].m_supported)
  3418. {
  3419. writeString(&writer, "#extension GL_INTEL_fragment_shader_ordering : enable\n");
  3420. }
  3421. else
  3422. {
  3423. writeString(&writer, "#define beginFragmentShaderOrdering()\n");
  3424. }
  3425. }
  3426. if (0 != fragData)
  3427. {
  3428. writeStringf(&writer, "out vec4 bgfx_FragData[%d];\n", fragData);
  3429. writeString(&writer, "#define gl_FragData bgfx_FragData\n");
  3430. }
  3431. else
  3432. {
  3433. writeString(&writer, "out vec4 bgfx_FragColor;\n");
  3434. writeString(&writer, "#define gl_FragColor bgfx_FragColor\n");
  3435. }
  3436. }
  3437. else
  3438. {
  3439. writeString(&writer, "#define attribute in\n");
  3440. writeString(&writer, "#define varying out\n");
  3441. }
  3442. if (!BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  3443. {
  3444. writeString(&writer
  3445. , "#define lowp\n"
  3446. "#define mediump\n"
  3447. "#define highp\n"
  3448. );
  3449. }
  3450. bx::write(&writer, code, codeLen);
  3451. bx::write(&writer, '\0');
  3452. }
  3453. code = temp;
  3454. }
  3455. GL_CHECK(glShaderSource(m_id, 1, (const GLchar**)&code, NULL) );
  3456. GL_CHECK(glCompileShader(m_id) );
  3457. GLint compiled = 0;
  3458. GL_CHECK(glGetShaderiv(m_id, GL_COMPILE_STATUS, &compiled) );
  3459. if (0 == compiled)
  3460. {
  3461. BX_TRACE("\n####\n%s\n####", code);
  3462. GLsizei len;
  3463. char log[1024];
  3464. GL_CHECK(glGetShaderInfoLog(m_id, sizeof(log), &len, log) );
  3465. BX_TRACE("Failed to compile shader. %d: %s", compiled, log);
  3466. GL_CHECK(glDeleteShader(m_id) );
  3467. BGFX_FATAL(false, bgfx::Fatal::InvalidShader, "Failed to compile shader.");
  3468. }
  3469. else if (BX_ENABLED(BGFX_CONFIG_DEBUG)
  3470. && s_extension[Extension::ANGLE_translated_shader_source].m_supported
  3471. && NULL != glGetTranslatedShaderSourceANGLE)
  3472. {
  3473. GLsizei len;
  3474. GL_CHECK(glGetShaderiv(m_id, GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE, &len) );
  3475. char* source = (char*)alloca(len);
  3476. GL_CHECK(glGetTranslatedShaderSourceANGLE(m_id, len, &len, source) );
  3477. BX_TRACE("ANGLE source (len: %d):\n%s\n####", len, source);
  3478. }
  3479. }
  3480. }
  3481. void ShaderGL::destroy()
  3482. {
  3483. if (0 != m_id)
  3484. {
  3485. GL_CHECK(glDeleteShader(m_id) );
  3486. m_id = 0;
  3487. }
  3488. }
  3489. static void frameBufferValidate()
  3490. {
  3491. GLenum complete = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  3492. BX_CHECK(GL_FRAMEBUFFER_COMPLETE == complete
  3493. , "glCheckFramebufferStatus failed 0x%08x: %s"
  3494. , complete
  3495. , glEnumName(complete)
  3496. );
  3497. BX_UNUSED(complete);
  3498. }
  3499. void FrameBufferGL::create(uint8_t _num, const TextureHandle* _handles)
  3500. {
  3501. GL_CHECK(glGenFramebuffers(1, &m_fbo[0]) );
  3502. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo[0]) );
  3503. // m_denseIdx = UINT16_MAX;
  3504. bool needResolve = false;
  3505. GLenum buffers[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
  3506. uint32_t colorIdx = 0;
  3507. for (uint32_t ii = 0; ii < _num; ++ii)
  3508. {
  3509. TextureHandle handle = _handles[ii];
  3510. if (isValid(handle) )
  3511. {
  3512. const TextureGL& texture = s_renderGL->m_textures[handle.idx];
  3513. if (0 == colorIdx)
  3514. {
  3515. m_width = texture.m_width;
  3516. m_height = texture.m_height;
  3517. }
  3518. GLenum attachment = GL_COLOR_ATTACHMENT0 + colorIdx;
  3519. if (isDepth( (TextureFormat::Enum)texture.m_textureFormat) )
  3520. {
  3521. attachment = GL_DEPTH_ATTACHMENT;
  3522. }
  3523. else
  3524. {
  3525. buffers[colorIdx] = attachment;
  3526. ++colorIdx;
  3527. }
  3528. if (0 != texture.m_rbo)
  3529. {
  3530. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER
  3531. , attachment
  3532. , GL_RENDERBUFFER
  3533. , texture.m_rbo
  3534. ) );
  3535. }
  3536. else
  3537. {
  3538. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  3539. , attachment
  3540. , texture.m_target
  3541. , texture.m_id
  3542. , 0
  3543. ) );
  3544. }
  3545. needResolve |= (0 != texture.m_rbo) && (0 != texture.m_id);
  3546. }
  3547. }
  3548. m_num = colorIdx;
  3549. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  3550. {
  3551. if (0 == colorIdx)
  3552. {
  3553. // When only depth is attached disable draw buffer to avoid
  3554. // GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER.
  3555. GL_CHECK(glDrawBuffer(GL_NONE) );
  3556. }
  3557. else
  3558. {
  3559. GL_CHECK(glDrawBuffers(colorIdx, buffers) );
  3560. }
  3561. // Disable read buffer to avoid GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER.
  3562. GL_CHECK(glReadBuffer(GL_NONE) );
  3563. }
  3564. frameBufferValidate();
  3565. if (needResolve)
  3566. {
  3567. GL_CHECK(glGenFramebuffers(1, &m_fbo[1]) );
  3568. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo[1]) );
  3569. for (uint32_t ii = 0, colorIdx = 0; ii < _num; ++ii)
  3570. {
  3571. TextureHandle handle = _handles[ii];
  3572. if (isValid(handle) )
  3573. {
  3574. const TextureGL& texture = s_renderGL->m_textures[handle.idx];
  3575. if (0 != texture.m_id)
  3576. {
  3577. GLenum attachment = GL_COLOR_ATTACHMENT0 + colorIdx;
  3578. if (!isDepth( (TextureFormat::Enum)texture.m_textureFormat) )
  3579. {
  3580. ++colorIdx;
  3581. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  3582. , attachment
  3583. , texture.m_target
  3584. , texture.m_id
  3585. , 0
  3586. ) );
  3587. }
  3588. }
  3589. }
  3590. }
  3591. frameBufferValidate();
  3592. }
  3593. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderGL->m_msaaBackBufferFbo) );
  3594. }
  3595. void FrameBufferGL::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat)
  3596. {
  3597. BX_UNUSED(_depthFormat);
  3598. m_swapChain = s_renderGL->m_glctx.createSwapChain(_nwh);
  3599. m_width = _width;
  3600. m_height = _height;
  3601. m_denseIdx = _denseIdx;
  3602. }
  3603. uint16_t FrameBufferGL::destroy()
  3604. {
  3605. if (0 != m_num)
  3606. {
  3607. GL_CHECK(glDeleteFramebuffers(0 == m_fbo[1] ? 1 : 2, m_fbo) );
  3608. memset(m_fbo, 0, sizeof(m_fbo) );
  3609. m_num = 0;
  3610. }
  3611. if (NULL != m_swapChain)
  3612. {
  3613. s_renderGL->m_glctx.destroySwapChain(m_swapChain);
  3614. m_swapChain = NULL;
  3615. }
  3616. uint16_t denseIdx = m_denseIdx;
  3617. m_denseIdx = UINT16_MAX;
  3618. return denseIdx;
  3619. }
  3620. void FrameBufferGL::resolve()
  3621. {
  3622. if (0 != m_fbo[1])
  3623. {
  3624. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[0]) );
  3625. GL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT0) );
  3626. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[1]) );
  3627. GL_CHECK(glBlitFramebuffer(0
  3628. , 0
  3629. , m_width
  3630. , m_height
  3631. , 0
  3632. , 0
  3633. , m_width
  3634. , m_height
  3635. , GL_COLOR_BUFFER_BIT
  3636. , GL_LINEAR
  3637. ) );
  3638. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[0]) );
  3639. GL_CHECK(glReadBuffer(GL_NONE) );
  3640. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderGL->m_msaaBackBufferFbo) );
  3641. }
  3642. }
  3643. void FrameBufferGL::discard(uint8_t _flags)
  3644. {
  3645. GLenum buffers[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS+2];
  3646. uint32_t idx = 0;
  3647. if (BGFX_CLEAR_NONE != (_flags & BGFX_CLEAR_DISCARD_COLOR_MASK) )
  3648. {
  3649. for (uint32_t ii = 0, num = m_num; ii < num; ++ii)
  3650. {
  3651. if (BGFX_CLEAR_NONE != (_flags & (BGFX_CLEAR_DISCARD_COLOR_0<<ii) ) )
  3652. {
  3653. buffers[idx++] = GL_COLOR_ATTACHMENT0 + ii;
  3654. }
  3655. }
  3656. }
  3657. uint32_t dsFlags = _flags & (BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL);
  3658. if (BGFX_CLEAR_NONE != dsFlags)
  3659. {
  3660. if ( (BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL) == dsFlags)
  3661. {
  3662. buffers[idx++] = GL_DEPTH_STENCIL_ATTACHMENT;
  3663. }
  3664. else if (BGFX_CLEAR_DISCARD_DEPTH == dsFlags)
  3665. {
  3666. buffers[idx++] = GL_DEPTH_ATTACHMENT;
  3667. }
  3668. else if (BGFX_CLEAR_DISCARD_STENCIL == dsFlags)
  3669. {
  3670. buffers[idx++] = GL_STENCIL_ATTACHMENT;
  3671. }
  3672. }
  3673. GL_CHECK(glInvalidateFramebuffer(GL_FRAMEBUFFER, idx, buffers) );
  3674. }
  3675. void RendererContextGL::submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter)
  3676. {
  3677. if (1 < m_numWindows
  3678. && m_vaoSupport)
  3679. {
  3680. m_vaoSupport = false;
  3681. GL_CHECK(glBindVertexArray(0) );
  3682. GL_CHECK(glDeleteVertexArrays(1, &m_vao) );
  3683. m_vao = 0;
  3684. m_vaoStateCache.invalidate();
  3685. }
  3686. m_glctx.makeCurrent(NULL);
  3687. const GLuint defaultVao = m_vao;
  3688. if (0 != defaultVao)
  3689. {
  3690. GL_CHECK(glBindVertexArray(defaultVao) );
  3691. }
  3692. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  3693. updateResolution(_render->m_resolution);
  3694. int64_t elapsed = -bx::getHPCounter();
  3695. int64_t captureElapsed = 0;
  3696. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  3697. && (_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) ) )
  3698. {
  3699. m_queries.begin(0, GL_TIME_ELAPSED);
  3700. }
  3701. if (0 < _render->m_iboffset)
  3702. {
  3703. TransientIndexBuffer* ib = _render->m_transientIb;
  3704. m_indexBuffers[ib->handle.idx].update(0, _render->m_iboffset, ib->data);
  3705. }
  3706. if (0 < _render->m_vboffset)
  3707. {
  3708. TransientVertexBuffer* vb = _render->m_transientVb;
  3709. m_vertexBuffers[vb->handle.idx].update(0, _render->m_vboffset, vb->data);
  3710. }
  3711. _render->sort();
  3712. RenderDraw currentState;
  3713. currentState.clear();
  3714. currentState.m_flags = BGFX_STATE_NONE;
  3715. currentState.m_stencil = packStencil(BGFX_STENCIL_NONE, BGFX_STENCIL_NONE);
  3716. const bool hmdEnabled = m_ovr.isEnabled() || m_ovr.isDebug();
  3717. _render->m_hmdEnabled = hmdEnabled;
  3718. if (hmdEnabled)
  3719. {
  3720. HMD& hmd = _render->m_hmd;
  3721. m_ovr.getEyePose(hmd);
  3722. }
  3723. ViewState viewState(_render, hmdEnabled);
  3724. uint16_t programIdx = invalidHandle;
  3725. SortKey key;
  3726. uint8_t view = 0xff;
  3727. FrameBufferHandle fbh = BGFX_INVALID_HANDLE;
  3728. int32_t height = hmdEnabled
  3729. ? _render->m_hmd.height
  3730. : _render->m_resolution.m_height
  3731. ;
  3732. uint32_t blendFactor = 0;
  3733. const uint64_t pt = _render->m_debug&BGFX_DEBUG_WIREFRAME ? BGFX_STATE_PT_LINES : 0;
  3734. uint8_t primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  3735. PrimInfo prim = s_primInfo[primIndex];
  3736. uint32_t baseVertex = 0;
  3737. GLuint currentVao = 0;
  3738. bool viewHasScissor = false;
  3739. Rect viewScissorRect;
  3740. viewScissorRect.clear();
  3741. uint8_t discardFlags = BGFX_CLEAR_NONE;
  3742. const bool blendIndependentSupported = s_extension[Extension::ARB_draw_buffers_blend].m_supported;
  3743. const bool computeSupported = (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) && s_extension[Extension::ARB_compute_shader].m_supported)
  3744. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  3745. ;
  3746. uint32_t statsNumPrimsSubmitted[BX_COUNTOF(s_primInfo)] = {};
  3747. uint32_t statsNumPrimsRendered[BX_COUNTOF(s_primInfo)] = {};
  3748. uint32_t statsNumInstances[BX_COUNTOF(s_primInfo)] = {};
  3749. uint32_t statsNumIndices = 0;
  3750. if (0 == (_render->m_debug&BGFX_DEBUG_IFH) )
  3751. {
  3752. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  3753. bool viewRestart = false;
  3754. uint8_t eye = 0;
  3755. uint8_t restartState = 0;
  3756. viewState.m_rect = _render->m_rect[0];
  3757. int32_t numItems = _render->m_num;
  3758. for (int32_t item = 0, restartItem = numItems; item < numItems || restartItem < numItems;)
  3759. {
  3760. const bool isCompute = key.decode(_render->m_sortKeys[item], _render->m_viewRemap);
  3761. const bool viewChanged = 0
  3762. || key.m_view != view
  3763. || item == numItems
  3764. ;
  3765. const RenderItem& renderItem = _render->m_renderItem[_render->m_sortValues[item] ];
  3766. ++item;
  3767. if (viewChanged)
  3768. {
  3769. if (1 == restartState)
  3770. {
  3771. restartState = 2;
  3772. item = restartItem;
  3773. restartItem = numItems;
  3774. view = 0xff;
  3775. continue;
  3776. }
  3777. view = key.m_view;
  3778. programIdx = invalidHandle;
  3779. if (_render->m_fb[view].idx != fbh.idx)
  3780. {
  3781. fbh = _render->m_fb[view];
  3782. height = hmdEnabled
  3783. ? _render->m_hmd.height
  3784. : _render->m_resolution.m_height
  3785. ;
  3786. height = setFrameBuffer(fbh, height, discardFlags);
  3787. }
  3788. viewRestart = ( (BGFX_VIEW_STEREO == (_render->m_viewFlags[view] & BGFX_VIEW_STEREO) ) );
  3789. viewRestart &= hmdEnabled;
  3790. if (viewRestart)
  3791. {
  3792. if (0 == restartState)
  3793. {
  3794. restartState = 1;
  3795. restartItem = item - 1;
  3796. }
  3797. eye = (restartState - 1) & 1;
  3798. restartState &= 1;
  3799. }
  3800. else
  3801. {
  3802. eye = 0;
  3803. }
  3804. viewState.m_rect = _render->m_rect[view];
  3805. if (viewRestart)
  3806. {
  3807. char* viewName = s_viewName[view];
  3808. viewName[3] = eye ? 'R' : 'L';
  3809. GL_CHECK(glInsertEventMarker(0, viewName) );
  3810. viewState.m_rect.m_x = eye * (viewState.m_rect.m_width+1)/2;
  3811. viewState.m_rect.m_width /= 2;
  3812. }
  3813. else
  3814. {
  3815. char* viewName = s_viewName[view];
  3816. viewName[3] = ' ';
  3817. GL_CHECK(glInsertEventMarker(0, viewName) );
  3818. }
  3819. const Rect& scissorRect = _render->m_scissor[view];
  3820. viewHasScissor = !scissorRect.isZero();
  3821. viewScissorRect = viewHasScissor ? scissorRect : viewState.m_rect;
  3822. GL_CHECK(glViewport(viewState.m_rect.m_x
  3823. , height-viewState.m_rect.m_height-viewState.m_rect.m_y
  3824. , viewState.m_rect.m_width
  3825. , viewState.m_rect.m_height
  3826. ) );
  3827. Clear& clear = _render->m_clear[view];
  3828. discardFlags = clear.m_flags & BGFX_CLEAR_DISCARD_MASK;
  3829. if (BGFX_CLEAR_NONE != (clear.m_flags & BGFX_CLEAR_MASK) )
  3830. {
  3831. clearQuad(_clearQuad, viewState.m_rect, clear, height, _render->m_clearColor);
  3832. }
  3833. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  3834. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  3835. GL_CHECK(glDepthFunc(GL_LESS) );
  3836. GL_CHECK(glEnable(GL_CULL_FACE) );
  3837. GL_CHECK(glDisable(GL_BLEND) );
  3838. }
  3839. if (isCompute)
  3840. {
  3841. if (computeSupported)
  3842. {
  3843. const RenderCompute& compute = renderItem.compute;
  3844. ProgramGL& program = m_program[key.m_program];
  3845. GL_CHECK(glUseProgram(program.m_id) );
  3846. GLbitfield barrier = 0;
  3847. for (uint32_t ii = 0; ii < BGFX_MAX_COMPUTE_BINDINGS; ++ii)
  3848. {
  3849. const Binding& bind = compute.m_bind[ii];
  3850. if (invalidHandle != bind.m_idx)
  3851. {
  3852. switch (bind.m_type)
  3853. {
  3854. case Binding::Image:
  3855. {
  3856. const TextureGL& texture = m_textures[bind.m_idx];
  3857. GL_CHECK(glBindImageTexture(ii
  3858. , texture.m_id
  3859. , bind.m_un.m_compute.m_mip
  3860. , GL_FALSE
  3861. , 0
  3862. , s_access[bind.m_un.m_compute.m_access]
  3863. , s_imageFormat[bind.m_un.m_compute.m_format])
  3864. );
  3865. barrier |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT;
  3866. }
  3867. break;
  3868. case Binding::IndexBuffer:
  3869. {
  3870. const IndexBufferGL& buffer = m_indexBuffers[bind.m_idx];
  3871. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id));
  3872. barrier |= GL_SHADER_STORAGE_BARRIER_BIT;
  3873. }
  3874. break;
  3875. case Binding::VertexBuffer:
  3876. {
  3877. const VertexBufferGL& buffer = m_vertexBuffers[bind.m_idx];
  3878. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id));
  3879. barrier |= GL_SHADER_STORAGE_BARRIER_BIT;
  3880. }
  3881. break;
  3882. }
  3883. }
  3884. }
  3885. if (0 != barrier)
  3886. {
  3887. bool constantsChanged = compute.m_constBegin < compute.m_constEnd;
  3888. rendererUpdateUniforms(this, _render->m_constantBuffer, compute.m_constBegin, compute.m_constEnd);
  3889. if (constantsChanged
  3890. && NULL != program.m_constantBuffer)
  3891. {
  3892. commit(*program.m_constantBuffer);
  3893. }
  3894. viewState.setPredefined<1>(this, view, eye, program, _render, compute);
  3895. GL_CHECK(glDispatchCompute(compute.m_numX, compute.m_numY, compute.m_numZ) );
  3896. GL_CHECK(glMemoryBarrier(barrier) );
  3897. }
  3898. }
  3899. continue;
  3900. }
  3901. const RenderDraw& draw = renderItem.draw;
  3902. const uint64_t newFlags = draw.m_flags;
  3903. uint64_t changedFlags = currentState.m_flags ^ draw.m_flags;
  3904. currentState.m_flags = newFlags;
  3905. const uint64_t newStencil = draw.m_stencil;
  3906. uint64_t changedStencil = currentState.m_stencil ^ draw.m_stencil;
  3907. currentState.m_stencil = newStencil;
  3908. if (viewChanged)
  3909. {
  3910. currentState.clear();
  3911. currentState.m_scissor = !draw.m_scissor;
  3912. changedFlags = BGFX_STATE_MASK;
  3913. changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK);
  3914. currentState.m_flags = newFlags;
  3915. currentState.m_stencil = newStencil;
  3916. }
  3917. uint16_t scissor = draw.m_scissor;
  3918. if (currentState.m_scissor != scissor)
  3919. {
  3920. currentState.m_scissor = scissor;
  3921. if (UINT16_MAX == scissor)
  3922. {
  3923. if (viewHasScissor)
  3924. {
  3925. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  3926. GL_CHECK(glScissor(viewScissorRect.m_x
  3927. , height-viewScissorRect.m_height-viewScissorRect.m_y
  3928. , viewScissorRect.m_width
  3929. , viewScissorRect.m_height
  3930. ) );
  3931. }
  3932. else
  3933. {
  3934. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  3935. }
  3936. }
  3937. else
  3938. {
  3939. Rect scissorRect;
  3940. scissorRect.intersect(viewScissorRect, _render->m_rectCache.m_cache[scissor]);
  3941. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  3942. GL_CHECK(glScissor(scissorRect.m_x
  3943. , height-scissorRect.m_height-scissorRect.m_y
  3944. , scissorRect.m_width
  3945. , scissorRect.m_height
  3946. ) );
  3947. }
  3948. }
  3949. if (0 != changedStencil)
  3950. {
  3951. if (0 != newStencil)
  3952. {
  3953. GL_CHECK(glEnable(GL_STENCIL_TEST) );
  3954. uint32_t bstencil = unpackStencil(1, newStencil);
  3955. uint32_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != unpackStencil(0, newStencil);
  3956. // uint32_t bchanged = unpackStencil(1, changedStencil);
  3957. // if (BGFX_STENCIL_FUNC_RMASK_MASK & bchanged)
  3958. // {
  3959. // uint32_t wmask = (bstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  3960. // GL_CHECK(glStencilMask(wmask) );
  3961. // }
  3962. for (uint32_t ii = 0, num = frontAndBack+1; ii < num; ++ii)
  3963. {
  3964. uint32_t stencil = unpackStencil(ii, newStencil);
  3965. uint32_t changed = unpackStencil(ii, changedStencil);
  3966. GLenum face = s_stencilFace[frontAndBack+ii];
  3967. if ( (BGFX_STENCIL_TEST_MASK|BGFX_STENCIL_FUNC_REF_MASK|BGFX_STENCIL_FUNC_RMASK_MASK) & changed)
  3968. {
  3969. GLint ref = (stencil&BGFX_STENCIL_FUNC_REF_MASK)>>BGFX_STENCIL_FUNC_REF_SHIFT;
  3970. GLint mask = (stencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  3971. uint32_t func = (stencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT;
  3972. GL_CHECK(glStencilFuncSeparate(face, s_cmpFunc[func], ref, mask));
  3973. }
  3974. if ( (BGFX_STENCIL_OP_FAIL_S_MASK|BGFX_STENCIL_OP_FAIL_Z_MASK|BGFX_STENCIL_OP_PASS_Z_MASK) & changed)
  3975. {
  3976. uint32_t sfail = (stencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT;
  3977. uint32_t zfail = (stencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT;
  3978. uint32_t zpass = (stencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT;
  3979. GL_CHECK(glStencilOpSeparate(face, s_stencilOp[sfail], s_stencilOp[zfail], s_stencilOp[zpass]) );
  3980. }
  3981. }
  3982. }
  3983. else
  3984. {
  3985. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  3986. }
  3987. }
  3988. if ( (0
  3989. | BGFX_STATE_CULL_MASK
  3990. | BGFX_STATE_DEPTH_WRITE
  3991. | BGFX_STATE_DEPTH_TEST_MASK
  3992. | BGFX_STATE_RGB_WRITE
  3993. | BGFX_STATE_ALPHA_WRITE
  3994. | BGFX_STATE_BLEND_MASK
  3995. | BGFX_STATE_BLEND_EQUATION_MASK
  3996. | BGFX_STATE_ALPHA_REF_MASK
  3997. | BGFX_STATE_PT_MASK
  3998. | BGFX_STATE_POINT_SIZE_MASK
  3999. | BGFX_STATE_MSAA
  4000. ) & changedFlags)
  4001. {
  4002. if (BGFX_STATE_CULL_MASK & changedFlags)
  4003. {
  4004. if (BGFX_STATE_CULL_CW & newFlags)
  4005. {
  4006. GL_CHECK(glEnable(GL_CULL_FACE) );
  4007. GL_CHECK(glCullFace(GL_BACK) );
  4008. }
  4009. else if (BGFX_STATE_CULL_CCW & newFlags)
  4010. {
  4011. GL_CHECK(glEnable(GL_CULL_FACE) );
  4012. GL_CHECK(glCullFace(GL_FRONT) );
  4013. }
  4014. else
  4015. {
  4016. GL_CHECK(glDisable(GL_CULL_FACE) );
  4017. }
  4018. }
  4019. if (BGFX_STATE_DEPTH_WRITE & changedFlags)
  4020. {
  4021. GL_CHECK(glDepthMask(!!(BGFX_STATE_DEPTH_WRITE & newFlags) ) );
  4022. }
  4023. if (BGFX_STATE_DEPTH_TEST_MASK & changedFlags)
  4024. {
  4025. uint32_t func = (newFlags&BGFX_STATE_DEPTH_TEST_MASK)>>BGFX_STATE_DEPTH_TEST_SHIFT;
  4026. if (0 != func)
  4027. {
  4028. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  4029. GL_CHECK(glDepthFunc(s_cmpFunc[func]) );
  4030. }
  4031. else
  4032. {
  4033. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  4034. }
  4035. }
  4036. if (BGFX_STATE_ALPHA_REF_MASK & changedFlags)
  4037. {
  4038. uint32_t ref = (newFlags&BGFX_STATE_ALPHA_REF_MASK)>>BGFX_STATE_ALPHA_REF_SHIFT;
  4039. viewState.m_alphaRef = ref/255.0f;
  4040. }
  4041. #if BGFX_CONFIG_RENDERER_OPENGL
  4042. if ( (BGFX_STATE_PT_POINTS|BGFX_STATE_POINT_SIZE_MASK) & changedFlags)
  4043. {
  4044. float pointSize = (float)(bx::uint32_max(1, (newFlags&BGFX_STATE_POINT_SIZE_MASK)>>BGFX_STATE_POINT_SIZE_SHIFT) );
  4045. GL_CHECK(glPointSize(pointSize) );
  4046. }
  4047. if (BGFX_STATE_MSAA & changedFlags)
  4048. {
  4049. if (BGFX_STATE_MSAA & newFlags)
  4050. {
  4051. GL_CHECK(glEnable(GL_MULTISAMPLE) );
  4052. }
  4053. else
  4054. {
  4055. GL_CHECK(glDisable(GL_MULTISAMPLE) );
  4056. }
  4057. }
  4058. #endif // BGFX_CONFIG_RENDERER_OPENGL
  4059. if ( (BGFX_STATE_ALPHA_WRITE|BGFX_STATE_RGB_WRITE) & changedFlags)
  4060. {
  4061. GLboolean alpha = !!(newFlags&BGFX_STATE_ALPHA_WRITE);
  4062. GLboolean rgb = !!(newFlags&BGFX_STATE_RGB_WRITE);
  4063. GL_CHECK(glColorMask(rgb, rgb, rgb, alpha) );
  4064. }
  4065. if ( (BGFX_STATE_BLEND_MASK|BGFX_STATE_BLEND_EQUATION_MASK|BGFX_STATE_BLEND_INDEPENDENT) & changedFlags
  4066. || blendFactor != draw.m_rgba)
  4067. {
  4068. if ( (BGFX_STATE_BLEND_MASK|BGFX_STATE_BLEND_EQUATION_MASK|BGFX_STATE_BLEND_INDEPENDENT) & newFlags
  4069. || blendFactor != draw.m_rgba)
  4070. {
  4071. const bool enabled = !!(BGFX_STATE_BLEND_MASK & newFlags);
  4072. const bool independent = !!(BGFX_STATE_BLEND_INDEPENDENT & newFlags)
  4073. && blendIndependentSupported
  4074. ;
  4075. const uint32_t blend = uint32_t( (newFlags&BGFX_STATE_BLEND_MASK)>>BGFX_STATE_BLEND_SHIFT);
  4076. const uint32_t equation = uint32_t( (newFlags&BGFX_STATE_BLEND_EQUATION_MASK)>>BGFX_STATE_BLEND_EQUATION_SHIFT);
  4077. const uint32_t srcRGB = (blend )&0xf;
  4078. const uint32_t dstRGB = (blend>> 4)&0xf;
  4079. const uint32_t srcA = (blend>> 8)&0xf;
  4080. const uint32_t dstA = (blend>>12)&0xf;
  4081. const uint32_t equRGB = (equation )&0x7;
  4082. const uint32_t equA = (equation>>3)&0x7;
  4083. const uint32_t numRt = getNumRt();
  4084. if (!BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  4085. || 1 >= numRt
  4086. || !independent)
  4087. {
  4088. if (enabled)
  4089. {
  4090. GL_CHECK(glEnable(GL_BLEND) );
  4091. GL_CHECK(glBlendFuncSeparate(s_blendFactor[srcRGB].m_src
  4092. , s_blendFactor[dstRGB].m_dst
  4093. , s_blendFactor[srcA].m_src
  4094. , s_blendFactor[dstA].m_dst
  4095. ) );
  4096. GL_CHECK(glBlendEquationSeparate(s_blendEquation[equRGB], s_blendEquation[equA]) );
  4097. if ( (s_blendFactor[srcRGB].m_factor || s_blendFactor[dstRGB].m_factor)
  4098. && blendFactor != draw.m_rgba)
  4099. {
  4100. const uint32_t rgba = draw.m_rgba;
  4101. GLclampf rr = ( (rgba>>24) )/255.0f;
  4102. GLclampf gg = ( (rgba>>16)&0xff)/255.0f;
  4103. GLclampf bb = ( (rgba>> 8)&0xff)/255.0f;
  4104. GLclampf aa = ( (rgba )&0xff)/255.0f;
  4105. GL_CHECK(glBlendColor(rr, gg, bb, aa) );
  4106. }
  4107. }
  4108. else
  4109. {
  4110. GL_CHECK(glDisable(GL_BLEND) );
  4111. }
  4112. }
  4113. else
  4114. {
  4115. if (enabled)
  4116. {
  4117. GL_CHECK(glEnablei(GL_BLEND, 0) );
  4118. GL_CHECK(glBlendFuncSeparatei(0
  4119. , s_blendFactor[srcRGB].m_src
  4120. , s_blendFactor[dstRGB].m_dst
  4121. , s_blendFactor[srcA].m_src
  4122. , s_blendFactor[dstA].m_dst
  4123. ) );
  4124. GL_CHECK(glBlendEquationSeparatei(0
  4125. , s_blendEquation[equRGB]
  4126. , s_blendEquation[equA]
  4127. ) );
  4128. }
  4129. else
  4130. {
  4131. GL_CHECK(glDisablei(GL_BLEND, 0) );
  4132. }
  4133. for (uint32_t ii = 1, rgba = draw.m_rgba; ii < numRt; ++ii, rgba >>= 11)
  4134. {
  4135. if (0 != (rgba&0x7ff) )
  4136. {
  4137. const uint32_t src = (rgba )&0xf;
  4138. const uint32_t dst = (rgba>>4)&0xf;
  4139. const uint32_t equation = (rgba>>8)&0x7;
  4140. GL_CHECK(glEnablei(GL_BLEND, ii) );
  4141. GL_CHECK(glBlendFunci(ii, s_blendFactor[src].m_src, s_blendFactor[dst].m_dst) );
  4142. GL_CHECK(glBlendEquationi(ii, s_blendEquation[equation]) );
  4143. }
  4144. else
  4145. {
  4146. GL_CHECK(glDisablei(GL_BLEND, ii) );
  4147. }
  4148. }
  4149. }
  4150. }
  4151. else
  4152. {
  4153. GL_CHECK(glDisable(GL_BLEND) );
  4154. }
  4155. blendFactor = draw.m_rgba;
  4156. }
  4157. const uint64_t pt = _render->m_debug&BGFX_DEBUG_WIREFRAME ? BGFX_STATE_PT_LINES : newFlags&BGFX_STATE_PT_MASK;
  4158. primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  4159. prim = s_primInfo[primIndex];
  4160. }
  4161. bool programChanged = false;
  4162. bool constantsChanged = draw.m_constBegin < draw.m_constEnd;
  4163. bool bindAttribs = false;
  4164. rendererUpdateUniforms(this, _render->m_constantBuffer, draw.m_constBegin, draw.m_constEnd);
  4165. if (key.m_program != programIdx)
  4166. {
  4167. programIdx = key.m_program;
  4168. GLuint id = invalidHandle == programIdx ? 0 : m_program[programIdx].m_id;
  4169. GL_CHECK(glUseProgram(id) );
  4170. programChanged =
  4171. constantsChanged =
  4172. bindAttribs = true;
  4173. }
  4174. if (invalidHandle != programIdx)
  4175. {
  4176. ProgramGL& program = m_program[programIdx];
  4177. if (constantsChanged
  4178. && NULL != program.m_constantBuffer)
  4179. {
  4180. commit(*program.m_constantBuffer);
  4181. }
  4182. viewState.setPredefined<1>(this, view, eye, program, _render, draw);
  4183. {
  4184. for (uint32_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage)
  4185. {
  4186. const Binding& sampler = draw.m_bind[stage];
  4187. Binding& current = currentState.m_bind[stage];
  4188. if (current.m_idx != sampler.m_idx
  4189. || current.m_un.m_draw.m_flags != sampler.m_un.m_draw.m_flags
  4190. || programChanged)
  4191. {
  4192. if (invalidHandle != sampler.m_idx)
  4193. {
  4194. TextureGL& texture = m_textures[sampler.m_idx];
  4195. texture.commit(stage, sampler.m_un.m_draw.m_flags);
  4196. }
  4197. }
  4198. current = sampler;
  4199. }
  4200. }
  4201. if (0 != defaultVao
  4202. && 0 == draw.m_startVertex
  4203. && 0 == draw.m_instanceDataOffset)
  4204. {
  4205. if (programChanged
  4206. || baseVertex != draw.m_startVertex
  4207. || currentState.m_vertexBuffer.idx != draw.m_vertexBuffer.idx
  4208. || currentState.m_indexBuffer.idx != draw.m_indexBuffer.idx
  4209. || currentState.m_instanceDataOffset != draw.m_instanceDataOffset
  4210. || currentState.m_instanceDataStride != draw.m_instanceDataStride
  4211. || currentState.m_instanceDataBuffer.idx != draw.m_instanceDataBuffer.idx)
  4212. {
  4213. bx::HashMurmur2A murmur;
  4214. murmur.begin();
  4215. murmur.add(draw.m_vertexBuffer.idx);
  4216. if (isValid(draw.m_vertexBuffer) )
  4217. {
  4218. const VertexBufferGL& vb = m_vertexBuffers[draw.m_vertexBuffer.idx];
  4219. uint16_t decl = !isValid(vb.m_decl) ? draw.m_vertexDecl.idx : vb.m_decl.idx;
  4220. murmur.add(decl);
  4221. }
  4222. murmur.add(draw.m_indexBuffer.idx);
  4223. murmur.add(draw.m_instanceDataBuffer.idx);
  4224. murmur.add(draw.m_instanceDataOffset);
  4225. murmur.add(draw.m_instanceDataStride);
  4226. murmur.add(programIdx);
  4227. uint32_t hash = murmur.end();
  4228. currentState.m_vertexBuffer = draw.m_vertexBuffer;
  4229. currentState.m_indexBuffer = draw.m_indexBuffer;
  4230. currentState.m_instanceDataOffset = draw.m_instanceDataOffset;
  4231. currentState.m_instanceDataStride = draw.m_instanceDataStride;
  4232. baseVertex = draw.m_startVertex;
  4233. GLuint id = m_vaoStateCache.find(hash);
  4234. if (UINT32_MAX != id)
  4235. {
  4236. currentVao = id;
  4237. GL_CHECK(glBindVertexArray(id) );
  4238. }
  4239. else
  4240. {
  4241. id = m_vaoStateCache.add(hash);
  4242. currentVao = id;
  4243. GL_CHECK(glBindVertexArray(id) );
  4244. ProgramGL& program = m_program[programIdx];
  4245. program.add(hash);
  4246. if (isValid(draw.m_vertexBuffer) )
  4247. {
  4248. VertexBufferGL& vb = m_vertexBuffers[draw.m_vertexBuffer.idx];
  4249. vb.add(hash);
  4250. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  4251. uint16_t decl = !isValid(vb.m_decl) ? draw.m_vertexDecl.idx : vb.m_decl.idx;
  4252. program.bindAttributes(m_vertexDecls[decl], draw.m_startVertex);
  4253. if (isValid(draw.m_instanceDataBuffer) )
  4254. {
  4255. VertexBufferGL& instanceVb = m_vertexBuffers[draw.m_instanceDataBuffer.idx];
  4256. instanceVb.add(hash);
  4257. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, instanceVb.m_id) );
  4258. program.bindInstanceData(draw.m_instanceDataStride, draw.m_instanceDataOffset);
  4259. }
  4260. }
  4261. else
  4262. {
  4263. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  4264. }
  4265. if (isValid(draw.m_indexBuffer) )
  4266. {
  4267. IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  4268. ib.add(hash);
  4269. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  4270. }
  4271. else
  4272. {
  4273. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  4274. }
  4275. }
  4276. }
  4277. }
  4278. else
  4279. {
  4280. if (0 != defaultVao
  4281. && 0 != currentVao)
  4282. {
  4283. GL_CHECK(glBindVertexArray(defaultVao) );
  4284. currentState.m_vertexBuffer.idx = invalidHandle;
  4285. currentState.m_indexBuffer.idx = invalidHandle;
  4286. bindAttribs = true;
  4287. currentVao = 0;
  4288. }
  4289. if (programChanged
  4290. || currentState.m_vertexBuffer.idx != draw.m_vertexBuffer.idx
  4291. || currentState.m_instanceDataBuffer.idx != draw.m_instanceDataBuffer.idx
  4292. || currentState.m_instanceDataOffset != draw.m_instanceDataOffset
  4293. || currentState.m_instanceDataStride != draw.m_instanceDataStride)
  4294. {
  4295. currentState.m_vertexBuffer = draw.m_vertexBuffer;
  4296. currentState.m_instanceDataBuffer.idx = draw.m_instanceDataBuffer.idx;
  4297. currentState.m_instanceDataOffset = draw.m_instanceDataOffset;
  4298. currentState.m_instanceDataStride = draw.m_instanceDataStride;
  4299. uint16_t handle = draw.m_vertexBuffer.idx;
  4300. if (invalidHandle != handle)
  4301. {
  4302. VertexBufferGL& vb = m_vertexBuffers[handle];
  4303. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  4304. bindAttribs = true;
  4305. }
  4306. else
  4307. {
  4308. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  4309. }
  4310. }
  4311. if (currentState.m_indexBuffer.idx != draw.m_indexBuffer.idx)
  4312. {
  4313. currentState.m_indexBuffer = draw.m_indexBuffer;
  4314. uint16_t handle = draw.m_indexBuffer.idx;
  4315. if (invalidHandle != handle)
  4316. {
  4317. IndexBufferGL& ib = m_indexBuffers[handle];
  4318. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  4319. }
  4320. else
  4321. {
  4322. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  4323. }
  4324. }
  4325. if (isValid(currentState.m_vertexBuffer) )
  4326. {
  4327. if (baseVertex != draw.m_startVertex
  4328. || bindAttribs)
  4329. {
  4330. baseVertex = draw.m_startVertex;
  4331. const VertexBufferGL& vb = m_vertexBuffers[draw.m_vertexBuffer.idx];
  4332. uint16_t decl = !isValid(vb.m_decl) ? draw.m_vertexDecl.idx : vb.m_decl.idx;
  4333. const ProgramGL& program = m_program[programIdx];
  4334. program.bindAttributes(m_vertexDecls[decl], draw.m_startVertex);
  4335. if (isValid(draw.m_instanceDataBuffer) )
  4336. {
  4337. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffers[draw.m_instanceDataBuffer.idx].m_id) );
  4338. program.bindInstanceData(draw.m_instanceDataStride, draw.m_instanceDataOffset);
  4339. }
  4340. }
  4341. }
  4342. }
  4343. if (isValid(currentState.m_vertexBuffer) )
  4344. {
  4345. uint32_t numVertices = draw.m_numVertices;
  4346. if (UINT32_MAX == numVertices)
  4347. {
  4348. const VertexBufferGL& vb = m_vertexBuffers[currentState.m_vertexBuffer.idx];
  4349. uint16_t decl = !isValid(vb.m_decl) ? draw.m_vertexDecl.idx : vb.m_decl.idx;
  4350. const VertexDecl& vertexDecl = m_vertexDecls[decl];
  4351. numVertices = vb.m_size/vertexDecl.m_stride;
  4352. }
  4353. uint32_t numIndices = 0;
  4354. uint32_t numPrimsSubmitted = 0;
  4355. uint32_t numInstances = 0;
  4356. uint32_t numPrimsRendered = 0;
  4357. if (isValid(draw.m_indexBuffer) )
  4358. {
  4359. if (UINT32_MAX == draw.m_numIndices)
  4360. {
  4361. numIndices = m_indexBuffers[draw.m_indexBuffer.idx].m_size/2;
  4362. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  4363. numInstances = draw.m_numInstances;
  4364. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  4365. GL_CHECK(glDrawElementsInstanced(prim.m_type
  4366. , numIndices
  4367. , GL_UNSIGNED_SHORT
  4368. , (void*)0
  4369. , draw.m_numInstances
  4370. ) );
  4371. }
  4372. else if (prim.m_min <= draw.m_numIndices)
  4373. {
  4374. numIndices = draw.m_numIndices;
  4375. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  4376. numInstances = draw.m_numInstances;
  4377. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  4378. GL_CHECK(glDrawElementsInstanced(prim.m_type
  4379. , numIndices
  4380. , GL_UNSIGNED_SHORT
  4381. , (void*)(uintptr_t)(draw.m_startIndex*2)
  4382. , draw.m_numInstances
  4383. ) );
  4384. }
  4385. }
  4386. else
  4387. {
  4388. numPrimsSubmitted = numVertices/prim.m_div - prim.m_sub;
  4389. numInstances = draw.m_numInstances;
  4390. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  4391. GL_CHECK(glDrawArraysInstanced(prim.m_type
  4392. , 0
  4393. , numVertices
  4394. , draw.m_numInstances
  4395. ) );
  4396. }
  4397. statsNumPrimsSubmitted[primIndex] += numPrimsSubmitted;
  4398. statsNumPrimsRendered[primIndex] += numPrimsRendered;
  4399. statsNumInstances[primIndex] += numInstances;
  4400. statsNumIndices += numIndices;
  4401. }
  4402. }
  4403. }
  4404. blitMsaaFbo();
  4405. if (0 < _render->m_num)
  4406. {
  4407. captureElapsed = -bx::getHPCounter();
  4408. capture();
  4409. captureElapsed += bx::getHPCounter();
  4410. }
  4411. }
  4412. m_glctx.makeCurrent(NULL);
  4413. int64_t now = bx::getHPCounter();
  4414. elapsed += now;
  4415. static int64_t last = now;
  4416. int64_t frameTime = now - last;
  4417. last = now;
  4418. static int64_t min = frameTime;
  4419. static int64_t max = frameTime;
  4420. min = min > frameTime ? frameTime : min;
  4421. max = max < frameTime ? frameTime : max;
  4422. if (_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  4423. {
  4424. double elapsedGpuMs = 0.0;
  4425. #if BGFX_CONFIG_RENDERER_OPENGL
  4426. m_queries.end(GL_TIME_ELAPSED);
  4427. uint64_t elapsedGl = m_queries.getResult(0);
  4428. elapsedGpuMs = double(elapsedGl)/1e6;
  4429. #endif // BGFX_CONFIG_RENDERER_OPENGL
  4430. TextVideoMem& tvm = m_textVideoMem;
  4431. static int64_t next = now;
  4432. if (now >= next)
  4433. {
  4434. next = now + bx::getHPFrequency();
  4435. double freq = double(bx::getHPFrequency() );
  4436. double toMs = 1000.0/freq;
  4437. tvm.clear();
  4438. uint16_t pos = 0;
  4439. tvm.printf(0, pos++, BGFX_CONFIG_DEBUG ? 0x89 : 0x8f, " %s / " BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME " "
  4440. , getRendererName()
  4441. );
  4442. tvm.printf(0, pos++, 0x0f, " Vendor: %s", m_vendor);
  4443. tvm.printf(0, pos++, 0x0f, " Renderer: %s", m_renderer);
  4444. tvm.printf(0, pos++, 0x0f, " Version: %s", m_version);
  4445. tvm.printf(0, pos++, 0x0f, "GLSL version: %s", m_glslVersion);
  4446. pos = 10;
  4447. tvm.printf(10, pos++, 0x8e, " Frame CPU: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS "
  4448. , double(frameTime)*toMs
  4449. , double(min)*toMs
  4450. , double(max)*toMs
  4451. , freq/frameTime
  4452. );
  4453. char hmd[16];
  4454. bx::snprintf(hmd, BX_COUNTOF(hmd), ", [%c] HMD ", hmdEnabled ? '\xfe' : ' ');
  4455. const uint32_t msaa = (m_resolution.m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  4456. tvm.printf(10, pos++, 0x8e, " Reset flags: [%c] vsync, [%c] MSAAx%d%s, [%c] MaxAnisotropy "
  4457. , !!(m_resolution.m_flags&BGFX_RESET_VSYNC) ? '\xfe' : ' '
  4458. , 0 != msaa ? '\xfe' : ' '
  4459. , 1<<msaa
  4460. , m_ovr.isInitialized() ? hmd : ", no-HMD "
  4461. , !!(m_resolution.m_flags&BGFX_RESET_MAXANISOTROPY) ? '\xfe' : ' '
  4462. );
  4463. double elapsedCpuMs = double(elapsed)*toMs;
  4464. tvm.printf(10, pos++, 0x8e, " Draw calls: %4d / CPU %3.4f [ms] %c GPU %3.4f [ms]"
  4465. , _render->m_num
  4466. , elapsedCpuMs
  4467. , elapsedCpuMs > elapsedGpuMs ? '>' : '<'
  4468. , elapsedGpuMs
  4469. );
  4470. for (uint32_t ii = 0; ii < BX_COUNTOF(s_primInfo); ++ii)
  4471. {
  4472. tvm.printf(10, pos++, 0x8e, " %8s: %7d (#inst: %5d), submitted: %7d"
  4473. , s_primName[ii]
  4474. , statsNumPrimsRendered[ii]
  4475. , statsNumInstances[ii]
  4476. , statsNumPrimsSubmitted[ii]
  4477. );
  4478. }
  4479. if (NULL != m_renderdocdll)
  4480. {
  4481. tvm.printf(tvm.m_width-27, 0, 0x1f, " [F11 - RenderDoc capture] ");
  4482. }
  4483. tvm.printf(10, pos++, 0x8e, " Indices: %7d", statsNumIndices);
  4484. tvm.printf(10, pos++, 0x8e, " DVB size: %7d", _render->m_vboffset);
  4485. tvm.printf(10, pos++, 0x8e, " DIB size: %7d", _render->m_iboffset);
  4486. pos++;
  4487. tvm.printf(10, pos++, 0x8e, " State cache: ");
  4488. tvm.printf(10, pos++, 0x8e, " VAO | Sampler ");
  4489. tvm.printf(10, pos++, 0x8e, " %6d | %6d "
  4490. , m_vaoStateCache.getCount()
  4491. , m_samplerStateCache.getCount()
  4492. );
  4493. pos++;
  4494. double captureMs = double(captureElapsed)*toMs;
  4495. tvm.printf(10, pos++, 0x8e, " Capture: %3.4f [ms]", captureMs);
  4496. #if BGFX_CONFIG_RENDERER_OPENGL
  4497. if (s_extension[Extension::ATI_meminfo].m_supported)
  4498. {
  4499. GLint vboFree[4];
  4500. GL_CHECK(glGetIntegerv(GL_VBO_FREE_MEMORY_ATI, vboFree) );
  4501. GLint texFree[4];
  4502. GL_CHECK(glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, texFree) );
  4503. GLint rbfFree[4];
  4504. GL_CHECK(glGetIntegerv(GL_RENDERBUFFER_FREE_MEMORY_ATI, rbfFree) );
  4505. pos++;
  4506. tvm.printf(10, pos++, 0x8c, " -------------| free| free b| aux| aux fb");
  4507. char tmp0[16];
  4508. char tmp1[16];
  4509. char tmp2[16];
  4510. char tmp3[16];
  4511. bx::prettify(tmp0, BX_COUNTOF(tmp0), vboFree[0]);
  4512. bx::prettify(tmp1, BX_COUNTOF(tmp1), vboFree[1]);
  4513. bx::prettify(tmp2, BX_COUNTOF(tmp2), vboFree[2]);
  4514. bx::prettify(tmp3, BX_COUNTOF(tmp3), vboFree[3]);
  4515. tvm.printf(10, pos++, 0x8e, " VBO: %10s, %10s, %10s, %10s", tmp0, tmp1, tmp2, tmp3);
  4516. bx::prettify(tmp0, BX_COUNTOF(tmp0), texFree[0]);
  4517. bx::prettify(tmp1, BX_COUNTOF(tmp1), texFree[1]);
  4518. bx::prettify(tmp2, BX_COUNTOF(tmp2), texFree[2]);
  4519. bx::prettify(tmp3, BX_COUNTOF(tmp3), texFree[3]);
  4520. tvm.printf(10, pos++, 0x8e, " Texture: %10s, %10s, %10s, %10s", tmp0, tmp1, tmp2, tmp3);
  4521. bx::prettify(tmp0, BX_COUNTOF(tmp0), rbfFree[0]);
  4522. bx::prettify(tmp1, BX_COUNTOF(tmp1), rbfFree[1]);
  4523. bx::prettify(tmp2, BX_COUNTOF(tmp2), rbfFree[2]);
  4524. bx::prettify(tmp3, BX_COUNTOF(tmp3), rbfFree[3]);
  4525. tvm.printf(10, pos++, 0x8e, " Render Buffer: %10s, %10s, %10s, %10s", tmp0, tmp1, tmp2, tmp3);
  4526. }
  4527. else if (s_extension[Extension::NVX_gpu_memory_info].m_supported)
  4528. {
  4529. GLint dedicated;
  4530. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &dedicated) );
  4531. GLint totalAvail;
  4532. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &totalAvail) );
  4533. GLint currAvail;
  4534. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &currAvail) );
  4535. GLint evictedCount;
  4536. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX, &evictedCount) );
  4537. GLint evictedMemory;
  4538. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX, &evictedMemory) );
  4539. pos += 2;
  4540. char tmp0[16];
  4541. char tmp1[16];
  4542. bx::prettify(tmp0, BX_COUNTOF(tmp0), dedicated);
  4543. tvm.printf(10, pos++, 0x8e, " Dedicated: %10s", tmp0);
  4544. bx::prettify(tmp0, BX_COUNTOF(tmp0), currAvail);
  4545. bx::prettify(tmp1, BX_COUNTOF(tmp1), totalAvail);
  4546. tvm.printf(10, pos++, 0x8e, " Available: %10s / %10s", tmp0, tmp1);
  4547. bx::prettify(tmp0, BX_COUNTOF(tmp0), evictedCount);
  4548. bx::prettify(tmp1, BX_COUNTOF(tmp1), evictedMemory);
  4549. tvm.printf(10, pos++, 0x8e, " Eviction: %10s / %10s", tmp0, tmp1);
  4550. }
  4551. #endif // BGFX_CONFIG_RENDERER_OPENGL
  4552. uint8_t attr[2] = { 0x89, 0x8a };
  4553. uint8_t attrIndex = _render->m_waitSubmit < _render->m_waitRender;
  4554. pos++;
  4555. tvm.printf(10, pos++, attr[attrIndex&1], "Submit wait: %3.4f [ms]", double(_render->m_waitSubmit)*toMs);
  4556. tvm.printf(10, pos++, attr[(attrIndex+1)&1], "Render wait: %3.4f [ms]", double(_render->m_waitRender)*toMs);
  4557. min = frameTime;
  4558. max = frameTime;
  4559. }
  4560. blit(this, _textVideoMemBlitter, tvm);
  4561. }
  4562. else if (_render->m_debug & BGFX_DEBUG_TEXT)
  4563. {
  4564. blit(this, _textVideoMemBlitter, _render->m_textVideoMem);
  4565. }
  4566. GL_CHECK(glFrameTerminatorGREMEDY() );
  4567. }
  4568. } // namespace bgfx
  4569. #else
  4570. namespace bgfx
  4571. {
  4572. RendererContextI* rendererCreateGL()
  4573. {
  4574. return NULL;
  4575. }
  4576. void rendererDestroyGL()
  4577. {
  4578. }
  4579. } // namespace bgfx
  4580. #endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)