renderer_gl.cpp 225 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-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. # include "hmd_ovr.h"
  11. #if BGFX_CONFIG_PROFILER_REMOTERY
  12. # define BGFX_GPU_PROFILER_BIND() rmt_BindOpenGL()
  13. # define BGFX_GPU_PROFILER_UNBIND() rmt_UnbindOpenGL()
  14. # define BGFX_GPU_PROFILER_BEGIN(_group, _name, _color) rmt_BeginOpenGLSample(_group##_##_name)
  15. # define BGFX_GPU_PROFILER_BEGIN_DYNAMIC(_namestr) rmt_BeginOpenGLSampleDynamic(_namestr)
  16. # define BGFX_GPU_PROFILER_END() rmt_EndOpenGLSample()
  17. #else
  18. # define BGFX_GPU_PROFILER_BIND() BX_NOOP()
  19. # define BGFX_GPU_PROFILER_UNBIND() BX_NOOP()
  20. # define BGFX_GPU_PROFILER_BEGIN(_group, _name, _color) BX_NOOP()
  21. # define BGFX_GPU_PROFILER_BEGIN_DYNAMIC(_namestr) BX_NOOP()
  22. # define BGFX_GPU_PROFILER_END() BX_NOOP()
  23. #endif // BGFX_CONFIG_PROFILER_REMOTERY
  24. namespace bgfx { namespace gl
  25. {
  26. static char s_viewName[BGFX_CONFIG_MAX_VIEWS][BGFX_CONFIG_MAX_VIEW_NAME];
  27. struct PrimInfo
  28. {
  29. GLenum m_type;
  30. uint32_t m_min;
  31. uint32_t m_div;
  32. uint32_t m_sub;
  33. };
  34. static const PrimInfo s_primInfo[] =
  35. {
  36. { GL_TRIANGLES, 3, 3, 0 },
  37. { GL_TRIANGLE_STRIP, 3, 1, 2 },
  38. { GL_LINES, 2, 2, 0 },
  39. { GL_LINE_STRIP, 2, 1, 1 },
  40. { GL_POINTS, 1, 1, 0 },
  41. };
  42. static const char* s_primName[] =
  43. {
  44. "TriList",
  45. "TriStrip",
  46. "Line",
  47. "LineStrip",
  48. "Point",
  49. };
  50. static const char* s_attribName[] =
  51. {
  52. "a_position",
  53. "a_normal",
  54. "a_tangent",
  55. "a_bitangent",
  56. "a_color0",
  57. "a_color1",
  58. "a_indices",
  59. "a_weight",
  60. "a_texcoord0",
  61. "a_texcoord1",
  62. "a_texcoord2",
  63. "a_texcoord3",
  64. "a_texcoord4",
  65. "a_texcoord5",
  66. "a_texcoord6",
  67. "a_texcoord7",
  68. };
  69. BX_STATIC_ASSERT(Attrib::Count == BX_COUNTOF(s_attribName) );
  70. static const char* s_instanceDataName[] =
  71. {
  72. "i_data0",
  73. "i_data1",
  74. "i_data2",
  75. "i_data3",
  76. "i_data4",
  77. };
  78. BX_STATIC_ASSERT(BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT == BX_COUNTOF(s_instanceDataName) );
  79. static const GLenum s_access[] =
  80. {
  81. GL_READ_ONLY,
  82. GL_WRITE_ONLY,
  83. GL_READ_WRITE,
  84. };
  85. BX_STATIC_ASSERT(Access::Count == BX_COUNTOF(s_access) );
  86. static const GLenum s_attribType[] =
  87. {
  88. GL_UNSIGNED_BYTE, // Uint8
  89. GL_UNSIGNED_INT_10_10_10_2, // Uint10
  90. GL_SHORT, // Int16
  91. GL_HALF_FLOAT, // Half
  92. GL_FLOAT, // Float
  93. };
  94. BX_STATIC_ASSERT(AttribType::Count == BX_COUNTOF(s_attribType) );
  95. struct Blend
  96. {
  97. GLenum m_src;
  98. GLenum m_dst;
  99. bool m_factor;
  100. };
  101. static const Blend s_blendFactor[] =
  102. {
  103. { 0, 0, false }, // ignored
  104. { GL_ZERO, GL_ZERO, false }, // ZERO
  105. { GL_ONE, GL_ONE, false }, // ONE
  106. { GL_SRC_COLOR, GL_SRC_COLOR, false }, // SRC_COLOR
  107. { GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, false }, // INV_SRC_COLOR
  108. { GL_SRC_ALPHA, GL_SRC_ALPHA, false }, // SRC_ALPHA
  109. { GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false }, // INV_SRC_ALPHA
  110. { GL_DST_ALPHA, GL_DST_ALPHA, false }, // DST_ALPHA
  111. { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, false }, // INV_DST_ALPHA
  112. { GL_DST_COLOR, GL_DST_COLOR, false }, // DST_COLOR
  113. { GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR, false }, // INV_DST_COLOR
  114. { GL_SRC_ALPHA_SATURATE, GL_ONE, false }, // SRC_ALPHA_SAT
  115. { GL_CONSTANT_COLOR, GL_CONSTANT_COLOR, true }, // FACTOR
  116. { GL_ONE_MINUS_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, true }, // INV_FACTOR
  117. };
  118. static const GLenum s_blendEquation[] =
  119. {
  120. GL_FUNC_ADD,
  121. GL_FUNC_SUBTRACT,
  122. GL_FUNC_REVERSE_SUBTRACT,
  123. GL_MIN,
  124. GL_MAX,
  125. };
  126. static const GLenum s_cmpFunc[] =
  127. {
  128. 0, // ignored
  129. GL_LESS,
  130. GL_LEQUAL,
  131. GL_EQUAL,
  132. GL_GEQUAL,
  133. GL_GREATER,
  134. GL_NOTEQUAL,
  135. GL_NEVER,
  136. GL_ALWAYS,
  137. };
  138. static const GLenum s_stencilOp[] =
  139. {
  140. GL_ZERO,
  141. GL_KEEP,
  142. GL_REPLACE,
  143. GL_INCR_WRAP,
  144. GL_INCR,
  145. GL_DECR_WRAP,
  146. GL_DECR,
  147. GL_INVERT,
  148. };
  149. static const GLenum s_stencilFace[] =
  150. {
  151. GL_FRONT_AND_BACK,
  152. GL_FRONT,
  153. GL_BACK,
  154. };
  155. static GLenum s_textureAddress[] =
  156. {
  157. GL_REPEAT,
  158. GL_MIRRORED_REPEAT,
  159. GL_CLAMP_TO_EDGE,
  160. GL_CLAMP_TO_BORDER,
  161. };
  162. static const GLenum s_textureFilterMag[] =
  163. {
  164. GL_LINEAR,
  165. GL_NEAREST,
  166. GL_LINEAR,
  167. };
  168. static const GLenum s_textureFilterMin[][3] =
  169. {
  170. { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST },
  171. { GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_NEAREST },
  172. { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST },
  173. };
  174. struct TextureFormatInfo
  175. {
  176. GLenum m_internalFmt;
  177. GLenum m_internalFmtSrgb;
  178. GLenum m_fmt;
  179. GLenum m_type;
  180. bool m_supported;
  181. };
  182. static TextureFormatInfo s_textureFormat[] =
  183. {
  184. { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_ZERO, false }, // BC1
  185. { GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_ZERO, false }, // BC2
  186. { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_ZERO, false }, // BC3
  187. { GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_ZERO, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_ZERO, false }, // BC4
  188. { GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_ZERO, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_ZERO, false }, // BC5
  189. { GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_ZERO, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_ZERO, false }, // BC6H
  190. { GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_ZERO, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_ZERO, false }, // BC7
  191. { GL_ETC1_RGB8_OES, GL_ZERO, GL_ETC1_RGB8_OES, GL_ZERO, false }, // ETC1
  192. { GL_COMPRESSED_RGB8_ETC2, GL_ZERO, GL_COMPRESSED_RGB8_ETC2, GL_ZERO, false }, // ETC2
  193. { GL_COMPRESSED_RGBA8_ETC2_EAC, GL_COMPRESSED_SRGB8_ETC2, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_ZERO, false }, // ETC2A
  194. { GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_ZERO, false }, // ETC2A1
  195. { GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, GL_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, GL_ZERO, false }, // PTC12
  196. { GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, GL_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, GL_ZERO, false }, // PTC14
  197. { GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, GL_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, GL_ZERO, false }, // PTC12A
  198. { GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, GL_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, GL_ZERO, false }, // PTC14A
  199. { GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, GL_ZERO, GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, GL_ZERO, false }, // PTC22
  200. { GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, GL_ZERO, GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, GL_ZERO, false }, // PTC24
  201. { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false }, // Unknown
  202. { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false }, // R1
  203. { GL_ALPHA, GL_ZERO, GL_ALPHA, GL_UNSIGNED_BYTE, false }, // A8
  204. { GL_R8, GL_ZERO, GL_RED, GL_UNSIGNED_BYTE, false }, // R8
  205. { GL_R8I, GL_ZERO, GL_RED, GL_BYTE, false }, // R8I
  206. { GL_R8UI, GL_ZERO, GL_RED, GL_UNSIGNED_BYTE, false }, // R8U
  207. { GL_R8_SNORM, GL_ZERO, GL_RED, GL_BYTE, false }, // R8S
  208. { GL_R16, GL_ZERO, GL_RED, GL_UNSIGNED_SHORT, false }, // R16
  209. { GL_R16I, GL_ZERO, GL_RED, GL_SHORT, false }, // R16I
  210. { GL_R16UI, GL_ZERO, GL_RED, GL_UNSIGNED_SHORT, false }, // R16U
  211. { GL_R16F, GL_ZERO, GL_RED, GL_HALF_FLOAT, false }, // R16F
  212. { GL_R16_SNORM, GL_ZERO, GL_RED, GL_SHORT, false }, // R16S
  213. { GL_R32I, GL_ZERO, GL_RED, GL_INT, false }, // R32I
  214. { GL_R32UI, GL_ZERO, GL_RED, GL_UNSIGNED_INT, false }, // R32U
  215. { GL_R32F, GL_ZERO, GL_RED, GL_FLOAT, false }, // R32F
  216. { GL_RG8, GL_ZERO, GL_RG, GL_UNSIGNED_BYTE, false }, // RG8
  217. { GL_RG8I, GL_ZERO, GL_RG, GL_BYTE, false }, // RG8I
  218. { GL_RG8UI, GL_ZERO, GL_RG, GL_UNSIGNED_BYTE, false }, // RG8U
  219. { GL_RG8_SNORM, GL_ZERO, GL_RG, GL_BYTE, false }, // RG8S
  220. { GL_RG16, GL_ZERO, GL_RG, GL_UNSIGNED_SHORT, false }, // RG16
  221. { GL_RG16I, GL_ZERO, GL_RG, GL_SHORT, false }, // RG16I
  222. { GL_RG16UI, GL_ZERO, GL_RG, GL_UNSIGNED_SHORT, false }, // RG16U
  223. { GL_RG16F, GL_ZERO, GL_RG, GL_FLOAT, false }, // RG16F
  224. { GL_RG16_SNORM, GL_ZERO, GL_RG, GL_SHORT, false }, // RG16S
  225. { GL_RG32I, GL_ZERO, GL_RG, GL_INT, false }, // RG32I
  226. { GL_RG32UI, GL_ZERO, GL_RG, GL_UNSIGNED_INT, false }, // RG32U
  227. { GL_RG32F, GL_ZERO, GL_RG, GL_FLOAT, false }, // RG32F
  228. { GL_RGB8, GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, false }, // RGB8
  229. { GL_RGB8I, GL_ZERO, GL_RGB, GL_BYTE, false }, // RGB8I
  230. { GL_RGB8UI, GL_ZERO, GL_RGB, GL_UNSIGNED_BYTE, false }, // RGB8U
  231. { GL_RGB8_SNORM, GL_ZERO, GL_RGB, GL_BYTE, false }, // RGB8S
  232. { GL_RGB9_E5, GL_ZERO, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, false }, // RGB9E5F
  233. { GL_RGBA8, GL_SRGB8_ALPHA8, GL_BGRA, GL_UNSIGNED_BYTE, false }, // BGRA8
  234. { GL_RGBA8, GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, false }, // RGBA8
  235. { GL_RGBA8I, GL_ZERO, GL_RGBA, GL_BYTE, false }, // RGBA8I
  236. { GL_RGBA8UI, GL_ZERO, GL_RGBA, GL_UNSIGNED_BYTE, false }, // RGBA8U
  237. { GL_RGBA8_SNORM, GL_ZERO, GL_RGBA, GL_BYTE, false }, // RGBA8S
  238. { GL_RGBA16, GL_ZERO, GL_RGBA, GL_UNSIGNED_SHORT, false }, // RGBA16
  239. { GL_RGBA16I, GL_ZERO, GL_RGBA, GL_SHORT, false }, // RGBA16I
  240. { GL_RGBA16UI, GL_ZERO, GL_RGBA, GL_UNSIGNED_SHORT, false }, // RGBA16U
  241. { GL_RGBA16F, GL_ZERO, GL_RGBA, GL_HALF_FLOAT, false }, // RGBA16F
  242. { GL_RGBA16_SNORM, GL_ZERO, GL_RGBA, GL_SHORT, false }, // RGBA16S
  243. { GL_RGBA32I, GL_ZERO, GL_RGBA, GL_INT, false }, // RGBA32I
  244. { GL_RGBA32UI, GL_ZERO, GL_RGBA, GL_UNSIGNED_INT, false }, // RGBA32U
  245. { GL_RGBA32F, GL_ZERO, GL_RGBA, GL_FLOAT, false }, // RGBA32F
  246. { GL_RGB565, GL_ZERO, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, false }, // R5G6B5
  247. { GL_RGBA4, GL_ZERO, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, false }, // RGBA4
  248. { GL_RGB5_A1, GL_ZERO, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, false }, // RGB5A1
  249. { GL_RGB10_A2, GL_ZERO, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, false }, // RGB10A2
  250. { GL_R11F_G11F_B10F, GL_ZERO, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, false }, // R11G11B10F
  251. { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false }, // UnknownDepth
  252. { GL_DEPTH_COMPONENT16, GL_ZERO, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, false }, // D16
  253. { GL_DEPTH_COMPONENT24, GL_ZERO, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, false }, // D24
  254. { GL_DEPTH24_STENCIL8, GL_ZERO, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, false }, // D24S8
  255. { GL_DEPTH_COMPONENT32, GL_ZERO, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, false }, // D32
  256. { GL_DEPTH_COMPONENT32F, GL_ZERO, GL_DEPTH_COMPONENT, GL_FLOAT, false }, // D16F
  257. { GL_DEPTH_COMPONENT32F, GL_ZERO, GL_DEPTH_COMPONENT, GL_FLOAT, false }, // D24F
  258. { GL_DEPTH_COMPONENT32F, GL_ZERO, GL_DEPTH_COMPONENT, GL_FLOAT, false }, // D32F
  259. { GL_STENCIL_INDEX8, GL_ZERO, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, false }, // D0S8
  260. };
  261. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormat) );
  262. static bool s_textureFilter[TextureFormat::Count+1];
  263. static GLenum s_rboFormat[] =
  264. {
  265. GL_ZERO, // BC1
  266. GL_ZERO, // BC2
  267. GL_ZERO, // BC3
  268. GL_ZERO, // BC4
  269. GL_ZERO, // BC5
  270. GL_ZERO, // BC6H
  271. GL_ZERO, // BC7
  272. GL_ZERO, // ETC1
  273. GL_ZERO, // ETC2
  274. GL_ZERO, // ETC2A
  275. GL_ZERO, // ETC2A1
  276. GL_ZERO, // PTC12
  277. GL_ZERO, // PTC14
  278. GL_ZERO, // PTC12A
  279. GL_ZERO, // PTC14A
  280. GL_ZERO, // PTC22
  281. GL_ZERO, // PTC24
  282. GL_ZERO, // Unknown
  283. GL_ZERO, // R1
  284. GL_ALPHA, // A8
  285. GL_R8, // R8
  286. GL_R8I, // R8I
  287. GL_R8UI, // R8U
  288. GL_R8_SNORM, // R8S
  289. GL_R16, // R16
  290. GL_R16I, // R16I
  291. GL_R16UI, // R16U
  292. GL_R16F, // R16F
  293. GL_R16_SNORM, // R16S
  294. GL_R32I, // R32I
  295. GL_R32UI, // R32U
  296. GL_R32F, // R32F
  297. GL_RG8, // RG8
  298. GL_RG8I, // RG8I
  299. GL_RG8UI, // RG8U
  300. GL_RG8_SNORM, // RG8S
  301. GL_RG16, // RG16
  302. GL_RG16I, // RG16I
  303. GL_RG16UI, // RG16U
  304. GL_RG16F, // RG16F
  305. GL_RG16_SNORM, // RG16S
  306. GL_RG32I, // RG32I
  307. GL_RG32UI, // RG32U
  308. GL_RG32F, // RG32F
  309. GL_RGB8, // RGB8
  310. GL_RGB8I, // RGB8I
  311. GL_RGB8UI, // RGB8UI
  312. GL_RGB8_SNORM, // RGB8S
  313. GL_RGB9_E5, // RGB9E5F
  314. GL_RGBA8, // BGRA8
  315. GL_RGBA8, // RGBA8
  316. GL_RGBA8I, // RGBA8I
  317. GL_RGBA8UI, // RGBA8UI
  318. GL_RGBA8_SNORM, // RGBA8S
  319. GL_RGBA16, // RGBA16
  320. GL_RGBA16I, // RGBA16I
  321. GL_RGBA16UI, // RGBA16U
  322. GL_RGBA16F, // RGBA16F
  323. GL_RGBA16_SNORM, // RGBA16S
  324. GL_RGBA32I, // RGBA32I
  325. GL_RGBA32UI, // RGBA32U
  326. GL_RGBA32F, // RGBA32F
  327. GL_RGB565, // R5G6B5
  328. GL_RGBA4, // RGBA4
  329. GL_RGB5_A1, // RGB5A1
  330. GL_RGB10_A2, // RGB10A2
  331. GL_R11F_G11F_B10F, // R11G11B10F
  332. GL_ZERO, // UnknownDepth
  333. GL_DEPTH_COMPONENT16, // D16
  334. GL_DEPTH_COMPONENT24, // D24
  335. GL_DEPTH24_STENCIL8, // D24S8
  336. GL_DEPTH_COMPONENT32, // D32
  337. GL_DEPTH_COMPONENT32F, // D16F
  338. GL_DEPTH_COMPONENT32F, // D24F
  339. GL_DEPTH_COMPONENT32F, // D32F
  340. GL_STENCIL_INDEX8, // D0S8
  341. };
  342. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_rboFormat) );
  343. static GLenum s_imageFormat[] =
  344. {
  345. GL_ZERO, // BC1
  346. GL_ZERO, // BC2
  347. GL_ZERO, // BC3
  348. GL_ZERO, // BC4
  349. GL_ZERO, // BC5
  350. GL_ZERO, // BC6H
  351. GL_ZERO, // BC7
  352. GL_ZERO, // ETC1
  353. GL_ZERO, // ETC2
  354. GL_ZERO, // ETC2A
  355. GL_ZERO, // ETC2A1
  356. GL_ZERO, // PTC12
  357. GL_ZERO, // PTC14
  358. GL_ZERO, // PTC12A
  359. GL_ZERO, // PTC14A
  360. GL_ZERO, // PTC22
  361. GL_ZERO, // PTC24
  362. GL_ZERO, // Unknown
  363. GL_ZERO, // R1
  364. GL_ALPHA, // A8
  365. GL_R8, // R8
  366. GL_R8I, // R8I
  367. GL_R8UI, // R8UI
  368. GL_R8_SNORM, // R8S
  369. GL_R16, // R16
  370. GL_R16I, // R16I
  371. GL_R16UI, // R16U
  372. GL_R16F, // R16F
  373. GL_R16_SNORM, // R16S
  374. GL_R32I, // R32I
  375. GL_R32UI, // R32U
  376. GL_R32F, // R32F
  377. GL_RG8, // RG8
  378. GL_RG8I, // RG8I
  379. GL_RG8UI, // RG8U
  380. GL_RG8_SNORM, // RG8S
  381. GL_RG16, // RG16
  382. GL_RG16I, // RG16I
  383. GL_RG16UI, // RG16U
  384. GL_RG16F, // RG16F
  385. GL_RG16_SNORM, // RG16S
  386. GL_RG32I, // RG32I
  387. GL_RG32UI, // RG32U
  388. GL_RG32F, // RG32F
  389. GL_RGB8, // RGB8
  390. GL_RGB8I, // RGB8I
  391. GL_RGB8UI, // RGB8UI
  392. GL_RGB8_SNORM, // RGB8S
  393. GL_RGB9_E5, // RGB9E5F
  394. GL_RGBA8, // BGRA8
  395. GL_RGBA8, // RGBA8
  396. GL_RGBA8I, // RGBA8I
  397. GL_RGBA8UI, // RGBA8UI
  398. GL_RGBA8_SNORM, // RGBA8S
  399. GL_RGBA16, // RGBA16
  400. GL_RGBA16I, // RGBA16I
  401. GL_RGBA16UI, // RGBA16U
  402. GL_RGBA16F, // RGBA16F
  403. GL_RGBA16_SNORM, // RGBA16S
  404. GL_RGBA32I, // RGBA32I
  405. GL_RGBA32UI, // RGBA32U
  406. GL_RGBA32F, // RGBA32F
  407. GL_RGB565, // R5G6B5
  408. GL_RGBA4, // RGBA4
  409. GL_RGB5_A1, // RGB5A1
  410. GL_RGB10_A2, // RGB10A2
  411. GL_R11F_G11F_B10F, // R11G11B10F
  412. GL_ZERO, // UnknownDepth
  413. GL_ZERO, // D16
  414. GL_ZERO, // D24
  415. GL_ZERO, // D24S8
  416. GL_ZERO, // D32
  417. GL_ZERO, // D16F
  418. GL_ZERO, // D24F
  419. GL_ZERO, // D32F
  420. GL_ZERO, // D0S8
  421. };
  422. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageFormat) );
  423. struct Extension
  424. {
  425. enum Enum
  426. {
  427. AMD_conservative_depth,
  428. AMD_multi_draw_indirect,
  429. ANGLE_depth_texture,
  430. ANGLE_framebuffer_blit,
  431. ANGLE_framebuffer_multisample,
  432. ANGLE_instanced_arrays,
  433. ANGLE_texture_compression_dxt1,
  434. ANGLE_texture_compression_dxt3,
  435. ANGLE_texture_compression_dxt5,
  436. ANGLE_timer_query,
  437. ANGLE_translated_shader_source,
  438. APPLE_texture_format_BGRA8888,
  439. APPLE_texture_max_level,
  440. ARB_clip_control,
  441. ARB_compute_shader,
  442. ARB_conservative_depth,
  443. ARB_copy_image,
  444. ARB_debug_label,
  445. ARB_debug_output,
  446. ARB_depth_buffer_float,
  447. ARB_depth_clamp,
  448. ARB_draw_buffers_blend,
  449. ARB_draw_indirect,
  450. ARB_draw_instanced,
  451. ARB_ES3_compatibility,
  452. ARB_framebuffer_object,
  453. ARB_framebuffer_sRGB,
  454. ARB_get_program_binary,
  455. ARB_half_float_pixel,
  456. ARB_half_float_vertex,
  457. ARB_instanced_arrays,
  458. ARB_internalformat_query,
  459. ARB_internalformat_query2,
  460. ARB_invalidate_subdata,
  461. ARB_map_buffer_range,
  462. ARB_multi_draw_indirect,
  463. ARB_multisample,
  464. ARB_occlusion_query,
  465. ARB_occlusion_query2,
  466. ARB_program_interface_query,
  467. ARB_sampler_objects,
  468. ARB_seamless_cube_map,
  469. ARB_shader_bit_encoding,
  470. ARB_shader_image_load_store,
  471. ARB_shader_storage_buffer_object,
  472. ARB_shader_texture_lod,
  473. ARB_texture_compression_bptc,
  474. ARB_texture_compression_rgtc,
  475. ARB_texture_cube_map_array,
  476. ARB_texture_float,
  477. ARB_texture_multisample,
  478. ARB_texture_rg,
  479. ARB_texture_rgb10_a2ui,
  480. ARB_texture_stencil8,
  481. ARB_texture_storage,
  482. ARB_texture_swizzle,
  483. ARB_timer_query,
  484. ARB_uniform_buffer_object,
  485. ARB_vertex_array_object,
  486. ARB_vertex_type_2_10_10_10_rev,
  487. ATI_meminfo,
  488. CHROMIUM_color_buffer_float_rgb,
  489. CHROMIUM_color_buffer_float_rgba,
  490. CHROMIUM_depth_texture,
  491. CHROMIUM_framebuffer_multisample,
  492. CHROMIUM_texture_compression_dxt3,
  493. CHROMIUM_texture_compression_dxt5,
  494. EXT_bgra,
  495. EXT_blend_color,
  496. EXT_blend_minmax,
  497. EXT_blend_subtract,
  498. EXT_color_buffer_half_float,
  499. EXT_color_buffer_float,
  500. EXT_copy_image,
  501. EXT_compressed_ETC1_RGB8_sub_texture,
  502. EXT_debug_label,
  503. EXT_debug_marker,
  504. EXT_debug_tool,
  505. EXT_discard_framebuffer,
  506. EXT_disjoint_timer_query,
  507. EXT_draw_buffers,
  508. EXT_draw_instanced,
  509. EXT_instanced_arrays,
  510. EXT_frag_depth,
  511. EXT_framebuffer_blit,
  512. EXT_framebuffer_object,
  513. EXT_framebuffer_sRGB,
  514. EXT_gpu_shader4,
  515. EXT_multi_draw_indirect,
  516. EXT_occlusion_query_boolean,
  517. EXT_packed_float,
  518. EXT_read_format_bgra,
  519. EXT_shader_image_load_store,
  520. EXT_shader_texture_lod,
  521. EXT_shadow_samplers,
  522. EXT_sRGB_write_control,
  523. EXT_texture_array,
  524. EXT_texture_compression_dxt1,
  525. EXT_texture_compression_latc,
  526. EXT_texture_compression_rgtc,
  527. EXT_texture_compression_s3tc,
  528. EXT_texture_cube_map_array,
  529. EXT_texture_filter_anisotropic,
  530. EXT_texture_format_BGRA8888,
  531. EXT_texture_rg,
  532. EXT_texture_shared_exponent,
  533. EXT_texture_snorm,
  534. EXT_texture_sRGB,
  535. EXT_texture_storage,
  536. EXT_texture_swizzle,
  537. EXT_texture_type_2_10_10_10_REV,
  538. EXT_timer_query,
  539. EXT_unpack_subimage,
  540. GOOGLE_depth_texture,
  541. GREMEDY_string_marker,
  542. GREMEDY_frame_terminator,
  543. IMG_multisampled_render_to_texture,
  544. IMG_read_format,
  545. IMG_shader_binary,
  546. IMG_texture_compression_pvrtc,
  547. IMG_texture_compression_pvrtc2,
  548. IMG_texture_format_BGRA8888,
  549. INTEL_fragment_shader_ordering,
  550. KHR_debug,
  551. KHR_no_error,
  552. MOZ_WEBGL_compressed_texture_s3tc,
  553. MOZ_WEBGL_depth_texture,
  554. NV_conservative_raster,
  555. NV_copy_image,
  556. NV_draw_buffers,
  557. NV_occlusion_query,
  558. NV_texture_border_clamp,
  559. NVX_gpu_memory_info,
  560. OES_copy_image,
  561. OES_compressed_ETC1_RGB8_texture,
  562. OES_depth24,
  563. OES_depth32,
  564. OES_depth_texture,
  565. OES_element_index_uint,
  566. OES_fragment_precision_high,
  567. OES_get_program_binary,
  568. OES_required_internalformat,
  569. OES_packed_depth_stencil,
  570. OES_read_format,
  571. OES_rgb8_rgba8,
  572. OES_standard_derivatives,
  573. OES_texture_3D,
  574. OES_texture_float,
  575. OES_texture_float_linear,
  576. OES_texture_npot,
  577. OES_texture_half_float,
  578. OES_texture_half_float_linear,
  579. OES_texture_stencil8,
  580. OES_texture_storage_multisample_2d_array,
  581. OES_vertex_array_object,
  582. OES_vertex_half_float,
  583. OES_vertex_type_10_10_10_2,
  584. WEBGL_color_buffer_float,
  585. WEBGL_compressed_texture_etc1,
  586. WEBGL_compressed_texture_s3tc,
  587. WEBGL_compressed_texture_pvrtc,
  588. WEBGL_depth_texture,
  589. WEBGL_draw_buffers,
  590. WEBKIT_EXT_texture_filter_anisotropic,
  591. WEBKIT_WEBGL_compressed_texture_s3tc,
  592. WEBKIT_WEBGL_depth_texture,
  593. Count
  594. };
  595. const char* m_name;
  596. bool m_supported;
  597. bool m_initialize;
  598. };
  599. // Extension registry
  600. //
  601. // ANGLE:
  602. // https://github.com/google/angle/tree/master/extensions
  603. //
  604. // CHROMIUM:
  605. // https://chromium.googlesource.com/chromium/src.git/+/refs/heads/git-svn/gpu/GLES2/extensions/CHROMIUM
  606. //
  607. // EGL:
  608. // https://www.khronos.org/registry/egl/extensions/
  609. //
  610. // GL:
  611. // https://www.opengl.org/registry/
  612. //
  613. // GLES:
  614. // https://www.khronos.org/registry/gles/extensions/
  615. //
  616. // WEBGL:
  617. // https://www.khronos.org/registry/webgl/extensions/
  618. //
  619. static Extension s_extension[] =
  620. {
  621. { "AMD_conservative_depth", false, true },
  622. { "AMD_multi_draw_indirect", false, true },
  623. { "ANGLE_depth_texture", false, true },
  624. { "ANGLE_framebuffer_blit", false, true },
  625. { "ANGLE_framebuffer_multisample", false, false },
  626. { "ANGLE_instanced_arrays", false, true },
  627. { "ANGLE_texture_compression_dxt1", false, true },
  628. { "ANGLE_texture_compression_dxt3", false, true },
  629. { "ANGLE_texture_compression_dxt5", false, true },
  630. { "ANGLE_timer_query", false, true },
  631. { "ANGLE_translated_shader_source", false, true },
  632. { "APPLE_texture_format_BGRA8888", false, true },
  633. { "APPLE_texture_max_level", false, true },
  634. { "ARB_clip_control", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  635. { "ARB_compute_shader", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  636. { "ARB_conservative_depth", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  637. { "ARB_copy_image", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  638. { "ARB_debug_label", false, true },
  639. { "ARB_debug_output", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  640. { "ARB_depth_buffer_float", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  641. { "ARB_depth_clamp", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  642. { "ARB_draw_buffers_blend", BGFX_CONFIG_RENDERER_OPENGL >= 40, true },
  643. { "ARB_draw_indirect", BGFX_CONFIG_RENDERER_OPENGL >= 40, true },
  644. { "ARB_draw_instanced", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  645. { "ARB_ES3_compatibility", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  646. { "ARB_framebuffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  647. { "ARB_framebuffer_sRGB", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  648. { "ARB_get_program_binary", BGFX_CONFIG_RENDERER_OPENGL >= 41, true },
  649. { "ARB_half_float_pixel", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  650. { "ARB_half_float_vertex", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  651. { "ARB_instanced_arrays", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  652. { "ARB_internalformat_query", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  653. { "ARB_internalformat_query2", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  654. { "ARB_invalidate_subdata", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  655. { "ARB_map_buffer_range", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  656. { "ARB_multi_draw_indirect", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  657. { "ARB_multisample", false, true },
  658. { "ARB_occlusion_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  659. { "ARB_occlusion_query2", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  660. { "ARB_program_interface_query", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  661. { "ARB_sampler_objects", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  662. { "ARB_seamless_cube_map", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  663. { "ARB_shader_bit_encoding", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  664. { "ARB_shader_image_load_store", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  665. { "ARB_shader_storage_buffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  666. { "ARB_shader_texture_lod", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  667. { "ARB_texture_compression_bptc", BGFX_CONFIG_RENDERER_OPENGL >= 44, true },
  668. { "ARB_texture_compression_rgtc", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  669. { "ARB_texture_cube_map_array", BGFX_CONFIG_RENDERER_OPENGL >= 40, true },
  670. { "ARB_texture_float", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  671. { "ARB_texture_multisample", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  672. { "ARB_texture_rg", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  673. { "ARB_texture_rgb10_a2ui", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  674. { "ARB_texture_stencil8", false, true },
  675. { "ARB_texture_storage", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  676. { "ARB_texture_swizzle", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  677. { "ARB_timer_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  678. { "ARB_uniform_buffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 31, true },
  679. { "ARB_vertex_array_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  680. { "ARB_vertex_type_2_10_10_10_rev", false, true },
  681. { "ATI_meminfo", false, true },
  682. { "CHROMIUM_color_buffer_float_rgb", false, true },
  683. { "CHROMIUM_color_buffer_float_rgba", false, true },
  684. { "CHROMIUM_depth_texture", false, true },
  685. { "CHROMIUM_framebuffer_multisample", false, true },
  686. { "CHROMIUM_texture_compression_dxt3", false, true },
  687. { "CHROMIUM_texture_compression_dxt5", false, true },
  688. { "EXT_bgra", false, true },
  689. { "EXT_blend_color", BGFX_CONFIG_RENDERER_OPENGL >= 31, true },
  690. { "EXT_blend_minmax", BGFX_CONFIG_RENDERER_OPENGL >= 14, true },
  691. { "EXT_blend_subtract", BGFX_CONFIG_RENDERER_OPENGL >= 14, true },
  692. { "EXT_color_buffer_half_float", false, true }, // GLES2 extension.
  693. { "EXT_color_buffer_float", false, true }, // GLES2 extension.
  694. { "EXT_copy_image", false, true }, // GLES2 extension.
  695. { "EXT_compressed_ETC1_RGB8_sub_texture", false, true }, // GLES2 extension.
  696. { "EXT_debug_label", false, true },
  697. { "EXT_debug_marker", false, true },
  698. { "EXT_debug_tool", false, true }, // RenderDoc extension.
  699. { "EXT_discard_framebuffer", false, true }, // GLES2 extension.
  700. { "EXT_disjoint_timer_query", false, true }, // GLES2 extension.
  701. { "EXT_draw_buffers", false, true }, // GLES2 extension.
  702. { "EXT_draw_instanced", false, true }, // GLES2 extension.
  703. { "EXT_instanced_arrays", false, true }, // GLES2 extension.
  704. { "EXT_frag_depth", false, true }, // GLES2 extension.
  705. { "EXT_framebuffer_blit", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  706. { "EXT_framebuffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  707. { "EXT_framebuffer_sRGB", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  708. { "EXT_gpu_shader4", false, true },
  709. { "EXT_multi_draw_indirect", false, true }, // GLES3.1 extension.
  710. { "EXT_occlusion_query_boolean", false, true }, // GLES2 extension.
  711. { "EXT_packed_float", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  712. { "EXT_read_format_bgra", false, true },
  713. { "EXT_shader_image_load_store", false, true },
  714. { "EXT_shader_texture_lod", false, true }, // GLES2 extension.
  715. { "EXT_shadow_samplers", false, true },
  716. { "EXT_sRGB_write_control", false, true }, // GLES2 extension.
  717. { "EXT_texture_array", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  718. { "EXT_texture_compression_dxt1", false, true },
  719. { "EXT_texture_compression_latc", false, true },
  720. { "EXT_texture_compression_rgtc", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  721. { "EXT_texture_compression_s3tc", false, true },
  722. { "EXT_texture_cube_map_array", false, true }, // GLES3.1 extension.
  723. { "EXT_texture_filter_anisotropic", false, true },
  724. { "EXT_texture_format_BGRA8888", false, true },
  725. { "EXT_texture_rg", false, true }, // GLES2 extension.
  726. { "EXT_texture_shared_exponent", false, true },
  727. { "EXT_texture_snorm", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  728. { "EXT_texture_sRGB", false, true },
  729. { "EXT_texture_storage", false, true },
  730. { "EXT_texture_swizzle", false, true },
  731. { "EXT_texture_type_2_10_10_10_REV", false, true },
  732. { "EXT_timer_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  733. { "EXT_unpack_subimage", false, true },
  734. { "GOOGLE_depth_texture", false, true },
  735. { "GREMEDY_string_marker", false, true },
  736. { "GREMEDY_frame_terminator", false, true },
  737. { "IMG_multisampled_render_to_texture", false, true },
  738. { "IMG_read_format", false, true },
  739. { "IMG_shader_binary", false, true },
  740. { "IMG_texture_compression_pvrtc", false, true },
  741. { "IMG_texture_compression_pvrtc2", false, true },
  742. { "IMG_texture_format_BGRA8888", false, true },
  743. { "INTEL_fragment_shader_ordering", false, true },
  744. { "KHR_debug", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  745. { "KHR_no_error", false, true },
  746. { "MOZ_WEBGL_compressed_texture_s3tc", false, true },
  747. { "MOZ_WEBGL_depth_texture", false, true },
  748. { "NV_conservative_raster", false, true },
  749. { "NV_copy_image", false, true },
  750. { "NV_draw_buffers", false, true }, // GLES2 extension.
  751. { "NV_occlusion_query", false, true },
  752. { "NV_texture_border_clamp", false, true }, // GLES2 extension.
  753. { "NVX_gpu_memory_info", false, true },
  754. { "OES_copy_image", false, true },
  755. { "OES_compressed_ETC1_RGB8_texture", false, true },
  756. { "OES_depth24", false, true },
  757. { "OES_depth32", false, true },
  758. { "OES_depth_texture", false, true },
  759. { "OES_element_index_uint", false, true },
  760. { "OES_fragment_precision_high", false, true },
  761. { "OES_get_program_binary", false, true },
  762. { "OES_required_internalformat", false, true },
  763. { "OES_packed_depth_stencil", false, true },
  764. { "OES_read_format", false, true },
  765. { "OES_rgb8_rgba8", false, true },
  766. { "OES_standard_derivatives", false, true },
  767. { "OES_texture_3D", false, true },
  768. { "OES_texture_float", false, true },
  769. { "OES_texture_float_linear", false, true },
  770. { "OES_texture_npot", false, true },
  771. { "OES_texture_half_float", false, true },
  772. { "OES_texture_half_float_linear", false, true },
  773. { "OES_texture_stencil8", false, true },
  774. { "OES_texture_storage_multisample_2d_array", false, true },
  775. { "OES_vertex_array_object", false, !BX_PLATFORM_IOS },
  776. { "OES_vertex_half_float", false, true },
  777. { "OES_vertex_type_10_10_10_2", false, true },
  778. { "WEBGL_color_buffer_float", false, true },
  779. { "WEBGL_compressed_texture_etc1", false, true },
  780. { "WEBGL_compressed_texture_s3tc", false, true },
  781. { "WEBGL_compressed_texture_pvrtc", false, true },
  782. { "WEBGL_depth_texture", false, true },
  783. { "WEBGL_draw_buffers", false, true },
  784. { "WEBKIT_EXT_texture_filter_anisotropic", false, true },
  785. { "WEBKIT_WEBGL_compressed_texture_s3tc", false, true },
  786. { "WEBKIT_WEBGL_depth_texture", false, true },
  787. };
  788. BX_STATIC_ASSERT(Extension::Count == BX_COUNTOF(s_extension) );
  789. static const char* s_ARB_shader_texture_lod[] =
  790. {
  791. "texture2DLod",
  792. "texture2DProjLod",
  793. "texture3DLod",
  794. "texture3DProjLod",
  795. "textureCubeLod",
  796. "shadow2DLod",
  797. "shadow2DProjLod",
  798. NULL
  799. // "texture1DLod",
  800. // "texture1DProjLod",
  801. // "shadow1DLod",
  802. // "shadow1DProjLod",
  803. };
  804. static const char* s_EXT_shader_texture_lod[] =
  805. {
  806. "texture2DLod",
  807. "texture2DProjLod",
  808. "textureCubeLod",
  809. NULL
  810. // "texture2DGrad",
  811. // "texture2DProjGrad",
  812. // "textureCubeGrad",
  813. };
  814. static const char* s_EXT_shadow_samplers[] =
  815. {
  816. "shadow2D",
  817. "shadow2DProj",
  818. NULL
  819. };
  820. static const char* s_OES_standard_derivatives[] =
  821. {
  822. "dFdx",
  823. "dFdy",
  824. "fwidth",
  825. NULL
  826. };
  827. static const char* s_OES_texture_3D[] =
  828. {
  829. "texture3D",
  830. "texture3DProj",
  831. "texture3DLod",
  832. "texture3DProjLod",
  833. NULL
  834. };
  835. static const char* s_uisamplers[] =
  836. {
  837. "isampler2D",
  838. "usampler2D",
  839. "isampler3D",
  840. "usampler3D",
  841. "isamplerCube",
  842. "usamplerCube",
  843. NULL
  844. };
  845. static const char* s_texelFetch[] =
  846. {
  847. "texelFetch",
  848. "texelFetchOffset",
  849. NULL
  850. };
  851. static const char* s_textureArray[] =
  852. {
  853. "sampler2DArray",
  854. "sampler2DMSArray",
  855. "samplerCubeArray",
  856. "sampler2DArrayShadow",
  857. NULL
  858. };
  859. static const char* s_ARB_texture_multisample[] =
  860. {
  861. "sampler2DMS",
  862. "isampler2DMS",
  863. "usampler2DMS",
  864. NULL
  865. };
  866. static const char* s_ARB_gpu_shader5[] =
  867. {
  868. "bitfieldReverse",
  869. "floatBitsToInt",
  870. "floatBitsToUint",
  871. "intBitsToFloat",
  872. "uintBitsToFloat",
  873. NULL
  874. };
  875. static const char* s_ARB_shading_language_packing[] =
  876. {
  877. "packHalf2x16",
  878. "unpackHalf2x16",
  879. NULL
  880. };
  881. static void GL_APIENTRY stubVertexAttribDivisor(GLuint /*_index*/, GLuint /*_divisor*/)
  882. {
  883. }
  884. static void GL_APIENTRY stubDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei /*_primcount*/)
  885. {
  886. GL_CHECK(glDrawArrays(_mode, _first, _count) );
  887. }
  888. static void GL_APIENTRY stubDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei /*_primcount*/)
  889. {
  890. GL_CHECK(glDrawElements(_mode, _count, _type, _indices) );
  891. }
  892. static void GL_APIENTRY stubFrameTerminatorGREMEDY()
  893. {
  894. }
  895. static void GL_APIENTRY stubInsertEventMarker(GLsizei /*_length*/, const char* /*_marker*/)
  896. {
  897. }
  898. static void GL_APIENTRY stubInsertEventMarkerGREMEDY(GLsizei _length, const char* _marker)
  899. {
  900. // If <marker> is a null-terminated string then <length> should not
  901. // include the terminator.
  902. //
  903. // If <length> is 0 then <marker> is assumed to be null-terminated.
  904. uint32_t size = (0 == _length ? (uint32_t)strlen(_marker) : _length) + 1;
  905. size *= sizeof(wchar_t);
  906. wchar_t* name = (wchar_t*)alloca(size);
  907. mbstowcs(name, _marker, size-2);
  908. GL_CHECK(glStringMarkerGREMEDY(_length, _marker) );
  909. }
  910. static void GL_APIENTRY stubObjectLabel(GLenum /*_identifier*/, GLuint /*_name*/, GLsizei /*_length*/, const char* /*_label*/)
  911. {
  912. }
  913. static void GL_APIENTRY stubInvalidateFramebuffer(GLenum /*_target*/, GLsizei /*_numAttachments*/, const GLenum* /*_attachments*/)
  914. {
  915. }
  916. static void GL_APIENTRY stubMultiDrawArraysIndirect(GLenum _mode, const void* _indirect, GLsizei _drawcount, GLsizei _stride)
  917. {
  918. const uint8_t* args = (const uint8_t*)_indirect;
  919. for (GLsizei ii = 0; ii < _drawcount; ++ii)
  920. {
  921. GL_CHECK(glDrawArraysIndirect(_mode, (void*)args) );
  922. args += _stride;
  923. }
  924. }
  925. static void GL_APIENTRY stubMultiDrawElementsIndirect(GLenum _mode, GLenum _type, const void* _indirect, GLsizei _drawcount, GLsizei _stride)
  926. {
  927. const uint8_t* args = (const uint8_t*)_indirect;
  928. for (GLsizei ii = 0; ii < _drawcount; ++ii)
  929. {
  930. GL_CHECK(glDrawElementsIndirect(_mode, _type, (void*)args) );
  931. args += _stride;
  932. }
  933. }
  934. typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height);
  935. static const char* getGLString(GLenum _name)
  936. {
  937. const char* str = (const char*)glGetString(_name);
  938. glGetError(); // ignore error if glGetString returns NULL.
  939. if (NULL != str)
  940. {
  941. return str;
  942. }
  943. return "<unknown>";
  944. }
  945. static uint32_t getGLStringHash(GLenum _name)
  946. {
  947. const char* str = (const char*)glGetString(_name);
  948. glGetError(); // ignore error if glGetString returns NULL.
  949. if (NULL != str)
  950. {
  951. return bx::hashMurmur2A(str, (uint32_t)strlen(str) );
  952. }
  953. return 0;
  954. }
  955. void dumpExtensions(const char* _extensions)
  956. {
  957. if (NULL != _extensions)
  958. {
  959. char name[1024];
  960. const char* pos = _extensions;
  961. const char* end = _extensions + strlen(_extensions);
  962. while (pos < end)
  963. {
  964. uint32_t len;
  965. const char* space = strchr(pos, ' ');
  966. if (NULL != space)
  967. {
  968. len = bx::uint32_min(sizeof(name), (uint32_t)(space - pos) );
  969. }
  970. else
  971. {
  972. len = bx::uint32_min(sizeof(name), (uint32_t)strlen(pos) );
  973. }
  974. strncpy(name, pos, len);
  975. name[len] = '\0';
  976. BX_TRACE("\t%s", name);
  977. pos += len+1;
  978. }
  979. }
  980. }
  981. const char* toString(GLenum _enum)
  982. {
  983. switch (_enum)
  984. {
  985. case GL_DEBUG_SOURCE_API: return "API";
  986. case GL_DEBUG_SOURCE_WINDOW_SYSTEM: return "WinSys";
  987. case GL_DEBUG_SOURCE_SHADER_COMPILER: return "Shader";
  988. case GL_DEBUG_SOURCE_THIRD_PARTY: return "3rdparty";
  989. case GL_DEBUG_SOURCE_APPLICATION: return "Application";
  990. case GL_DEBUG_SOURCE_OTHER: return "Other";
  991. case GL_DEBUG_TYPE_ERROR: return "Error";
  992. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: return "Deprecated behavior";
  993. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: return "Undefined behavior";
  994. case GL_DEBUG_TYPE_PORTABILITY: return "Portability";
  995. case GL_DEBUG_TYPE_PERFORMANCE: return "Performance";
  996. case GL_DEBUG_TYPE_OTHER: return "Other";
  997. case GL_DEBUG_SEVERITY_HIGH: return "High";
  998. case GL_DEBUG_SEVERITY_MEDIUM: return "Medium";
  999. case GL_DEBUG_SEVERITY_LOW: return "Low";
  1000. case GL_DEBUG_SEVERITY_NOTIFICATION: return "SPAM";
  1001. default:
  1002. break;
  1003. }
  1004. return "<unknown>";
  1005. }
  1006. void GL_APIENTRY debugProcCb(GLenum _source, GLenum _type, GLuint _id, GLenum _severity, GLsizei /*_length*/, const GLchar* _message, const void* /*_userParam*/)
  1007. {
  1008. if (GL_DEBUG_SEVERITY_NOTIFICATION != _severity)
  1009. {
  1010. BX_TRACE("src %s, type %s, id %d, severity %s, '%s'"
  1011. , toString(_source)
  1012. , toString(_type)
  1013. , _id
  1014. , toString(_severity)
  1015. , _message
  1016. );
  1017. BX_UNUSED(_source, _type, _id, _severity, _message);
  1018. }
  1019. }
  1020. GLint glGet(GLenum _pname)
  1021. {
  1022. GLint result = 0;
  1023. glGetIntegerv(_pname, &result);
  1024. GLenum err = glGetError();
  1025. BX_WARN(0 == err, "glGetIntegerv(0x%04x, ...) failed with GL error: 0x%04x.", _pname, err);
  1026. return 0 == err ? result : 0;
  1027. }
  1028. void setTextureFormat(TextureFormat::Enum _format, GLenum _internalFmt, GLenum _fmt, GLenum _type = GL_ZERO)
  1029. {
  1030. TextureFormatInfo& tfi = s_textureFormat[_format];
  1031. tfi.m_internalFmt = _internalFmt;
  1032. tfi.m_fmt = _fmt;
  1033. tfi.m_type = _type;
  1034. }
  1035. void flushGlError()
  1036. {
  1037. for (GLenum err = glGetError(); err != 0; err = glGetError() );
  1038. }
  1039. GLenum initTestTexture(TextureFormat::Enum _format, bool _srgb, bool _mipmaps)
  1040. {
  1041. const TextureFormatInfo& tfi = s_textureFormat[_format];
  1042. GLenum internalFmt = _srgb
  1043. ? tfi.m_internalFmtSrgb
  1044. : tfi.m_internalFmt
  1045. ;
  1046. GLsizei size = (16*16*getBitsPerPixel(_format) )/8;
  1047. void* data = bx::alignPtr(alloca(size+16), 0, 16);
  1048. flushGlError();
  1049. GLenum err = 0;
  1050. if (isCompressed(_format) )
  1051. {
  1052. glCompressedTexImage2D(GL_TEXTURE_2D, 0, internalFmt, 16, 16, 0, size, data);
  1053. err |= glGetError();
  1054. if (_mipmaps)
  1055. {
  1056. glCompressedTexImage2D(GL_TEXTURE_2D, 1, internalFmt, 8, 8, 0, size, data);
  1057. err |= glGetError();
  1058. glCompressedTexImage2D(GL_TEXTURE_2D, 2, internalFmt, 4, 4, 0, size, data);
  1059. err |= glGetError();
  1060. glCompressedTexImage2D(GL_TEXTURE_2D, 3, internalFmt, 2, 2, 0, size, data);
  1061. err |= glGetError();
  1062. glCompressedTexImage2D(GL_TEXTURE_2D, 4, internalFmt, 1, 1, 0, size, data);
  1063. err |= glGetError();
  1064. }
  1065. }
  1066. else
  1067. {
  1068. glTexImage2D(GL_TEXTURE_2D, 0, internalFmt, 16, 16, 0, tfi.m_fmt, tfi.m_type, data);
  1069. err |= glGetError();
  1070. if (_mipmaps)
  1071. {
  1072. glTexImage2D(GL_TEXTURE_2D, 1, internalFmt, 8, 8, 0, tfi.m_fmt, tfi.m_type, data);
  1073. err |= glGetError();
  1074. glTexImage2D(GL_TEXTURE_2D, 2, internalFmt, 4, 4, 0, tfi.m_fmt, tfi.m_type, data);
  1075. err |= glGetError();
  1076. glTexImage2D(GL_TEXTURE_2D, 3, internalFmt, 2, 2, 0, tfi.m_fmt, tfi.m_type, data);
  1077. err |= glGetError();
  1078. glTexImage2D(GL_TEXTURE_2D, 4, internalFmt, 1, 1, 0, tfi.m_fmt, tfi.m_type, data);
  1079. err |= glGetError();
  1080. }
  1081. }
  1082. return err;
  1083. }
  1084. static bool isTextureFormatValid(TextureFormat::Enum _format, bool _srgb = false, bool _mipAutogen = false)
  1085. {
  1086. const TextureFormatInfo& tfi = s_textureFormat[_format];
  1087. GLenum internalFmt = _srgb
  1088. ? tfi.m_internalFmtSrgb
  1089. : tfi.m_internalFmt
  1090. ;
  1091. if (GL_ZERO == internalFmt)
  1092. {
  1093. return false;
  1094. }
  1095. GLuint id;
  1096. GL_CHECK(glGenTextures(1, &id) );
  1097. GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) );
  1098. GLenum err = initTestTexture(_format, _srgb, _mipAutogen);
  1099. BX_WARN(0 == err, "TextureFormat::%s is not supported (%x: %s).", getName(_format), err, glEnumName(err) );
  1100. if (0 == err
  1101. && _mipAutogen)
  1102. {
  1103. glGenerateMipmap(GL_TEXTURE_2D);
  1104. err = glGetError();
  1105. }
  1106. GL_CHECK(glDeleteTextures(1, &id) );
  1107. return 0 == err;
  1108. }
  1109. static bool isImageFormatValid(TextureFormat::Enum _format)
  1110. {
  1111. if (GL_ZERO == s_imageFormat[_format])
  1112. {
  1113. return false;
  1114. }
  1115. GLuint id;
  1116. GL_CHECK(glGenTextures(1, &id) );
  1117. GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) );
  1118. flushGlError();
  1119. GLenum err = 0;
  1120. glTexStorage2D(GL_TEXTURE_2D, 1, s_imageFormat[_format], 16, 16);
  1121. err |= glGetError();
  1122. if (0 == err)
  1123. {
  1124. glBindImageTexture(0
  1125. , id
  1126. , 0
  1127. , GL_FALSE
  1128. , 0
  1129. , GL_READ_WRITE
  1130. , s_imageFormat[_format]
  1131. );
  1132. err |= glGetError();
  1133. }
  1134. GL_CHECK(glDeleteTextures(1, &id) );
  1135. return 0 == err;
  1136. }
  1137. static bool isFramebufferFormatValid(TextureFormat::Enum _format, bool _srgb = false)
  1138. {
  1139. const TextureFormatInfo& tfi = s_textureFormat[_format];
  1140. GLenum internalFmt = _srgb
  1141. ? tfi.m_internalFmtSrgb
  1142. : tfi.m_internalFmt
  1143. ;
  1144. if (GL_ZERO == internalFmt
  1145. || !tfi.m_supported)
  1146. {
  1147. return false;
  1148. }
  1149. GLuint fbo;
  1150. GL_CHECK(glGenFramebuffers(1, &fbo) );
  1151. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, fbo) );
  1152. GLuint id;
  1153. GL_CHECK(glGenTextures(1, &id) );
  1154. GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) );
  1155. GLenum err = initTestTexture(_format, _srgb, false);
  1156. GLenum attachment;
  1157. if (isDepth(_format) )
  1158. {
  1159. const ImageBlockInfo& info = getBlockInfo(_format);
  1160. if (0 == info.depthBits)
  1161. {
  1162. attachment = GL_STENCIL_ATTACHMENT;
  1163. }
  1164. else if (0 == info.stencilBits)
  1165. {
  1166. attachment = GL_DEPTH_ATTACHMENT;
  1167. }
  1168. else
  1169. {
  1170. attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  1171. }
  1172. }
  1173. else
  1174. {
  1175. attachment = GL_COLOR_ATTACHMENT0;
  1176. }
  1177. glFramebufferTexture2D(GL_FRAMEBUFFER
  1178. , attachment
  1179. , GL_TEXTURE_2D
  1180. , id
  1181. , 0
  1182. );
  1183. err = glGetError();
  1184. if (0 == err)
  1185. {
  1186. err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1187. }
  1188. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1189. GL_CHECK(glDeleteFramebuffers(1, &fbo) );
  1190. GL_CHECK(glDeleteTextures(1, &id) );
  1191. return GL_FRAMEBUFFER_COMPLETE == err;
  1192. }
  1193. static void getFilters(uint32_t _flags, bool _hasMips, GLenum& _magFilter, GLenum& _minFilter)
  1194. {
  1195. const uint32_t mag = (_flags&BGFX_TEXTURE_MAG_MASK)>>BGFX_TEXTURE_MAG_SHIFT;
  1196. const uint32_t min = (_flags&BGFX_TEXTURE_MIN_MASK)>>BGFX_TEXTURE_MIN_SHIFT;
  1197. const uint32_t mip = (_flags&BGFX_TEXTURE_MIP_MASK)>>BGFX_TEXTURE_MIP_SHIFT;
  1198. _magFilter = s_textureFilterMag[mag];
  1199. _minFilter = s_textureFilterMin[min][_hasMips ? mip+1 : 0];
  1200. }
  1201. void updateExtension(const char* _name)
  1202. {
  1203. bool supported = false;
  1204. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  1205. {
  1206. Extension& extension = s_extension[ii];
  1207. if (!extension.m_supported
  1208. && extension.m_initialize)
  1209. {
  1210. const char* ext = _name;
  1211. if (0 == strncmp(ext, "GL_", 3) ) // skip GL_
  1212. {
  1213. ext += 3;
  1214. }
  1215. if (0 == strcmp(ext, extension.m_name) )
  1216. {
  1217. extension.m_supported = true;
  1218. supported = true;
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. BX_TRACE("GL_EXTENSION %s: %s", supported ? " (supported)" : "", _name);
  1224. BX_UNUSED(supported);
  1225. }
  1226. #if BGFX_CONFIG_USE_OVR
  1227. class VRImplOVRGL : public VRImplOVR
  1228. {
  1229. public:
  1230. VRImplOVRGL();
  1231. virtual bool createSwapChain(const VRDesc& _desc, int _msaaSamples, int _mirrorWidth, int _mirrorHeight) BX_OVERRIDE;
  1232. virtual void destroySwapChain() BX_OVERRIDE;
  1233. virtual void destroyMirror() BX_OVERRIDE;
  1234. virtual void renderEyeStart(const VRDesc& _desc, uint8_t _eye) BX_OVERRIDE;
  1235. virtual bool submitSwapChain(const VRDesc& _desc) BX_OVERRIDE;
  1236. private:
  1237. GLuint m_eyeFbo[2];
  1238. GLuint m_eyeTexId[2][4];
  1239. GLuint m_depthBuffer[2];
  1240. GLuint m_msaaEyeFbo[2];
  1241. GLuint m_msaaEyeTexId[2];
  1242. GLuint m_msaaDepthBuffer[2];
  1243. GLuint m_mirrorFbo;
  1244. GLint m_mirrorWidth;
  1245. GLint m_mirrorHeight;
  1246. ovrTextureSwapChain m_textureSwapChain[2];
  1247. ovrMirrorTexture m_mirrorTexture;
  1248. };
  1249. #endif // BGFX_CONFIG_USE_OVR
  1250. struct VendorId
  1251. {
  1252. const char* name;
  1253. uint16_t id;
  1254. };
  1255. static const VendorId s_vendorIds[] =
  1256. {
  1257. { "NVIDIA Corporation", BGFX_PCI_ID_NVIDIA },
  1258. { "Advanced Micro Devices, Inc.", BGFX_PCI_ID_AMD },
  1259. { "Intel", BGFX_PCI_ID_INTEL },
  1260. };
  1261. struct RendererContextGL : public RendererContextI
  1262. {
  1263. RendererContextGL()
  1264. : m_numWindows(1)
  1265. , m_rtMsaa(false)
  1266. , m_fbDiscard(BGFX_CLEAR_NONE)
  1267. , m_capture(NULL)
  1268. , m_captureSize(0)
  1269. , m_maxAnisotropy(0.0f)
  1270. , m_maxAnisotropyDefault(0.0f)
  1271. , m_maxMsaa(0)
  1272. , m_vao(0)
  1273. , m_blitSupported(false)
  1274. , m_readBackSupported(BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1275. , m_vaoSupport(false)
  1276. , m_samplerObjectSupport(false)
  1277. , m_shadowSamplersSupport(false)
  1278. , m_srgbWriteControlSupport(BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1279. , m_borderColorSupport(BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1280. , m_programBinarySupport(false)
  1281. , m_textureSwizzleSupport(false)
  1282. , m_depthTextureSupport(false)
  1283. , m_timerQuerySupport(false)
  1284. , m_occlusionQuerySupport(false)
  1285. , m_atocSupport(false)
  1286. , m_conservativeRasterSupport(false)
  1287. , m_flip(false)
  1288. , m_hash( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT)
  1289. , m_backBufferFbo(0)
  1290. , m_msaaBackBufferFbo(0)
  1291. {
  1292. memset(m_msaaBackBufferRbos, 0, sizeof(m_msaaBackBufferRbos) );
  1293. }
  1294. ~RendererContextGL()
  1295. {
  1296. }
  1297. void init()
  1298. {
  1299. m_renderdocdll = loadRenderDoc();
  1300. m_fbh.idx = invalidHandle;
  1301. memset(m_uniforms, 0, sizeof(m_uniforms) );
  1302. memset(&m_resolution, 0, sizeof(m_resolution) );
  1303. setRenderContextSize(BGFX_DEFAULT_WIDTH, BGFX_DEFAULT_HEIGHT);
  1304. // Must be after context is initialized?!
  1305. VRImplI* vrImpl = NULL;
  1306. #if BGFX_CONFIG_USE_OVR
  1307. vrImpl = &m_ovrRender;
  1308. #endif
  1309. m_ovr.init(vrImpl);
  1310. m_vendor = getGLString(GL_VENDOR);
  1311. m_renderer = getGLString(GL_RENDERER);
  1312. m_version = getGLString(GL_VERSION);
  1313. m_glslVersion = getGLString(GL_SHADING_LANGUAGE_VERSION);
  1314. for (uint32_t ii = 0; ii < BX_COUNTOF(s_vendorIds); ++ii)
  1315. {
  1316. const VendorId& vendorId = s_vendorIds[ii];
  1317. if (0 == strncmp(vendorId.name, m_vendor, strlen(vendorId.name) ) )
  1318. {
  1319. g_caps.vendorId = vendorId.id;
  1320. break;
  1321. }
  1322. }
  1323. GLint numCmpFormats = 0;
  1324. GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCmpFormats) );
  1325. BX_TRACE("GL_NUM_COMPRESSED_TEXTURE_FORMATS %d", numCmpFormats);
  1326. GLint* cmpFormat = NULL;
  1327. if (0 < numCmpFormats)
  1328. {
  1329. numCmpFormats = numCmpFormats > 256 ? 256 : numCmpFormats;
  1330. cmpFormat = (GLint*)alloca(sizeof(GLint)*numCmpFormats);
  1331. GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, cmpFormat) );
  1332. for (GLint ii = 0; ii < numCmpFormats; ++ii)
  1333. {
  1334. GLint internalFmt = cmpFormat[ii];
  1335. uint32_t fmt = uint32_t(TextureFormat::Unknown);
  1336. for (uint32_t jj = 0; jj < fmt; ++jj)
  1337. {
  1338. if (s_textureFormat[jj].m_internalFmt == (GLenum)internalFmt)
  1339. {
  1340. s_textureFormat[jj].m_supported = true;
  1341. fmt = jj;
  1342. }
  1343. }
  1344. BX_TRACE(" %3d: %8x %s", ii, internalFmt, getName( (TextureFormat::Enum)fmt) );
  1345. }
  1346. }
  1347. if (BX_ENABLED(BGFX_CONFIG_DEBUG) )
  1348. {
  1349. #define GL_GET(_pname, _min) BX_TRACE(" " #_pname " %d (min: %d)", glGet(_pname), _min)
  1350. BX_TRACE("Defaults:");
  1351. #if BGFX_CONFIG_RENDERER_OPENGL >= 41 || BGFX_CONFIG_RENDERER_OPENGLES
  1352. GL_GET(GL_MAX_FRAGMENT_UNIFORM_VECTORS, 16);
  1353. GL_GET(GL_MAX_VERTEX_UNIFORM_VECTORS, 128);
  1354. GL_GET(GL_MAX_VARYING_VECTORS, 8);
  1355. #else
  1356. GL_GET(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, 16 * 4);
  1357. GL_GET(GL_MAX_VERTEX_UNIFORM_COMPONENTS, 128 * 4);
  1358. GL_GET(GL_MAX_VARYING_FLOATS, 8 * 4);
  1359. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 41 || BGFX_CONFIG_RENDERER_OPENGLES
  1360. GL_GET(GL_MAX_VERTEX_ATTRIBS, 8);
  1361. GL_GET(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, 8);
  1362. GL_GET(GL_MAX_CUBE_MAP_TEXTURE_SIZE, 16);
  1363. GL_GET(GL_MAX_TEXTURE_IMAGE_UNITS, 8);
  1364. GL_GET(GL_MAX_TEXTURE_SIZE, 64);
  1365. GL_GET(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, 0);
  1366. GL_GET(GL_MAX_RENDERBUFFER_SIZE, 1);
  1367. GL_GET(GL_MAX_COLOR_ATTACHMENTS, 1);
  1368. GL_GET(GL_MAX_DRAW_BUFFERS, 1);
  1369. #undef GL_GET
  1370. BX_TRACE(" Vendor: %s", m_vendor);
  1371. BX_TRACE(" Renderer: %s", m_renderer);
  1372. BX_TRACE(" Version: %s", m_version);
  1373. BX_TRACE("GLSL version: %s", m_glslVersion);
  1374. }
  1375. // Initial binary shader hash depends on driver version.
  1376. m_hash = ( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT)
  1377. ^ (uint64_t(getGLStringHash(GL_VENDOR ) )<<32)
  1378. ^ (uint64_t(getGLStringHash(GL_RENDERER) )<<0 )
  1379. ^ (uint64_t(getGLStringHash(GL_VERSION ) )<<16)
  1380. ;
  1381. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  1382. && 0 == strcmp(m_vendor, "Imagination Technologies")
  1383. && NULL != strstr(m_version, "(SDK 3.5@3510720)") )
  1384. {
  1385. // Skip initializing extensions that are broken in emulator.
  1386. s_extension[Extension::ARB_program_interface_query ].m_initialize =
  1387. s_extension[Extension::ARB_shader_storage_buffer_object].m_initialize = false;
  1388. }
  1389. if (BX_ENABLED(BGFX_CONFIG_RENDERER_USE_EXTENSIONS) )
  1390. {
  1391. const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
  1392. glGetError(); // ignore error if glGetString returns NULL.
  1393. if (NULL != extensions)
  1394. {
  1395. char name[1024];
  1396. const char* pos = extensions;
  1397. const char* end = extensions + strlen(extensions);
  1398. uint32_t index = 0;
  1399. while (pos < end)
  1400. {
  1401. uint32_t len;
  1402. const char* space = strchr(pos, ' ');
  1403. if (NULL != space)
  1404. {
  1405. len = bx::uint32_min(sizeof(name), (uint32_t)(space - pos) );
  1406. }
  1407. else
  1408. {
  1409. len = bx::uint32_min(sizeof(name), (uint32_t)strlen(pos) );
  1410. }
  1411. strncpy(name, pos, len);
  1412. name[len] = '\0';
  1413. updateExtension(name);
  1414. pos += len+1;
  1415. ++index;
  1416. }
  1417. }
  1418. else if (NULL != glGetStringi)
  1419. {
  1420. GLint numExtensions = 0;
  1421. glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
  1422. glGetError(); // ignore error if glGetIntegerv returns NULL.
  1423. for (GLint index = 0; index < numExtensions; ++index)
  1424. {
  1425. const char* name = (const char*)glGetStringi(GL_EXTENSIONS, index);
  1426. updateExtension(name);
  1427. }
  1428. }
  1429. BX_TRACE("Supported extensions:");
  1430. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  1431. {
  1432. if (s_extension[ii].m_supported)
  1433. {
  1434. BX_TRACE("\t%2d: %s", ii, s_extension[ii].m_name);
  1435. }
  1436. }
  1437. }
  1438. // Allow all texture filters.
  1439. memset(s_textureFilter, true, BX_COUNTOF(s_textureFilter) );
  1440. bool bc123Supported = 0
  1441. || s_extension[Extension::EXT_texture_compression_s3tc ].m_supported
  1442. || s_extension[Extension::MOZ_WEBGL_compressed_texture_s3tc ].m_supported
  1443. || s_extension[Extension::WEBGL_compressed_texture_s3tc ].m_supported
  1444. || s_extension[Extension::WEBKIT_WEBGL_compressed_texture_s3tc].m_supported
  1445. ;
  1446. s_textureFormat[TextureFormat::BC1].m_supported |= bc123Supported
  1447. || s_extension[Extension::ANGLE_texture_compression_dxt1].m_supported
  1448. || s_extension[Extension::EXT_texture_compression_dxt1 ].m_supported
  1449. ;
  1450. if (!s_textureFormat[TextureFormat::BC1].m_supported
  1451. && ( s_textureFormat[TextureFormat::BC2].m_supported || s_textureFormat[TextureFormat::BC3].m_supported) )
  1452. {
  1453. // If RGBA_S3TC_DXT1 is not supported, maybe RGB_S3TC_DXT1 is?
  1454. for (GLint ii = 0; ii < numCmpFormats; ++ii)
  1455. {
  1456. if (GL_COMPRESSED_RGB_S3TC_DXT1_EXT == cmpFormat[ii])
  1457. {
  1458. setTextureFormat(TextureFormat::BC1, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
  1459. s_textureFormat[TextureFormat::BC1].m_supported = true;
  1460. break;
  1461. }
  1462. }
  1463. }
  1464. s_textureFormat[TextureFormat::BC2].m_supported |= bc123Supported
  1465. || s_extension[Extension::ANGLE_texture_compression_dxt3 ].m_supported
  1466. || s_extension[Extension::CHROMIUM_texture_compression_dxt3].m_supported
  1467. ;
  1468. s_textureFormat[TextureFormat::BC3].m_supported |= bc123Supported
  1469. || s_extension[Extension::ANGLE_texture_compression_dxt5 ].m_supported
  1470. || s_extension[Extension::CHROMIUM_texture_compression_dxt5].m_supported
  1471. ;
  1472. if (s_extension[Extension::EXT_texture_compression_latc].m_supported)
  1473. {
  1474. setTextureFormat(TextureFormat::BC4, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_COMPRESSED_LUMINANCE_LATC1_EXT);
  1475. setTextureFormat(TextureFormat::BC5, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT);
  1476. }
  1477. if (s_extension[Extension::ARB_texture_compression_rgtc].m_supported
  1478. || s_extension[Extension::EXT_texture_compression_rgtc].m_supported)
  1479. {
  1480. setTextureFormat(TextureFormat::BC4, GL_COMPRESSED_RED_RGTC1, GL_COMPRESSED_RED_RGTC1);
  1481. setTextureFormat(TextureFormat::BC5, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2);
  1482. }
  1483. bool etc1Supported = 0
  1484. || s_extension[Extension::OES_compressed_ETC1_RGB8_texture].m_supported
  1485. || s_extension[Extension::WEBGL_compressed_texture_etc1 ].m_supported
  1486. ;
  1487. s_textureFormat[TextureFormat::ETC1].m_supported |= etc1Supported;
  1488. bool etc2Supported = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1489. || s_extension[Extension::ARB_ES3_compatibility].m_supported
  1490. ;
  1491. s_textureFormat[TextureFormat::ETC2 ].m_supported |= etc2Supported;
  1492. s_textureFormat[TextureFormat::ETC2A ].m_supported |= etc2Supported;
  1493. s_textureFormat[TextureFormat::ETC2A1].m_supported |= etc2Supported;
  1494. if (!s_textureFormat[TextureFormat::ETC1].m_supported
  1495. && s_textureFormat[TextureFormat::ETC2].m_supported)
  1496. {
  1497. // When ETC2 is supported override ETC1 texture format settings.
  1498. s_textureFormat[TextureFormat::ETC1].m_internalFmt = GL_COMPRESSED_RGB8_ETC2;
  1499. s_textureFormat[TextureFormat::ETC1].m_fmt = GL_COMPRESSED_RGB8_ETC2;
  1500. s_textureFormat[TextureFormat::ETC1].m_supported = true;
  1501. }
  1502. bool ptc1Supported = 0
  1503. || s_extension[Extension::IMG_texture_compression_pvrtc ].m_supported
  1504. || s_extension[Extension::WEBGL_compressed_texture_pvrtc].m_supported
  1505. ;
  1506. s_textureFormat[TextureFormat::PTC12 ].m_supported |= ptc1Supported;
  1507. s_textureFormat[TextureFormat::PTC14 ].m_supported |= ptc1Supported;
  1508. s_textureFormat[TextureFormat::PTC12A].m_supported |= ptc1Supported;
  1509. s_textureFormat[TextureFormat::PTC14A].m_supported |= ptc1Supported;
  1510. bool ptc2Supported = s_extension[Extension::IMG_texture_compression_pvrtc2].m_supported;
  1511. s_textureFormat[TextureFormat::PTC22].m_supported |= ptc2Supported;
  1512. s_textureFormat[TextureFormat::PTC24].m_supported |= ptc2Supported;
  1513. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) )
  1514. {
  1515. setTextureFormat(TextureFormat::D32, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
  1516. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) )
  1517. {
  1518. setTextureFormat(TextureFormat::RGBA16F, GL_RGBA, GL_RGBA, GL_HALF_FLOAT);
  1519. setTextureFormat(TextureFormat::RGBA32F, GL_RGBA, GL_RGBA, GL_FLOAT);
  1520. // internalFormat and format must match:
  1521. // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml
  1522. setTextureFormat(TextureFormat::RGBA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
  1523. setTextureFormat(TextureFormat::R5G6B5, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV);
  1524. setTextureFormat(TextureFormat::RGBA4, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV);
  1525. setTextureFormat(TextureFormat::RGB5A1, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV);
  1526. if (s_extension[Extension::OES_texture_half_float].m_supported
  1527. || s_extension[Extension::OES_texture_float ].m_supported)
  1528. {
  1529. // https://www.khronos.org/registry/gles/extensions/OES/OES_texture_float.txt
  1530. // When half/float is available via extensions texture will be marked as
  1531. // incomplete if it uses anything other than nearest filter.
  1532. const bool linear16F = s_extension[Extension::OES_texture_half_float_linear].m_supported;
  1533. const bool linear32F = s_extension[Extension::OES_texture_float_linear ].m_supported;
  1534. s_textureFilter[TextureFormat::R16F] = linear16F;
  1535. s_textureFilter[TextureFormat::RG16F] = linear16F;
  1536. s_textureFilter[TextureFormat::RGBA16F] = linear16F;
  1537. s_textureFilter[TextureFormat::R32F] = linear32F;
  1538. s_textureFilter[TextureFormat::RG32F] = linear32F;
  1539. s_textureFilter[TextureFormat::RGBA32F] = linear32F;
  1540. }
  1541. if (BX_ENABLED(BX_PLATFORM_IOS) )
  1542. {
  1543. setTextureFormat(TextureFormat::D16, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT);
  1544. setTextureFormat(TextureFormat::D24S8, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
  1545. }
  1546. }
  1547. }
  1548. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  1549. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  1550. {
  1551. setTextureFormat(TextureFormat::R8I, GL_R8I, GL_RED_INTEGER, GL_BYTE);
  1552. setTextureFormat(TextureFormat::R8U, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE);
  1553. setTextureFormat(TextureFormat::R16I, GL_R16I, GL_RED_INTEGER, GL_SHORT);
  1554. setTextureFormat(TextureFormat::R16U, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT);
  1555. // setTextureFormat(TextureFormat::RG16, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT);
  1556. // setTextureFormat(TextureFormat::RGBA16, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT);
  1557. setTextureFormat(TextureFormat::R32U, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT);
  1558. setTextureFormat(TextureFormat::RG32U, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT);
  1559. setTextureFormat(TextureFormat::RGBA32U, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT);
  1560. }
  1561. if (s_extension[Extension::EXT_texture_format_BGRA8888 ].m_supported
  1562. || s_extension[Extension::EXT_bgra ].m_supported
  1563. || s_extension[Extension::IMG_texture_format_BGRA8888 ].m_supported
  1564. || s_extension[Extension::APPLE_texture_format_BGRA8888].m_supported)
  1565. {
  1566. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1567. {
  1568. m_readPixelsFmt = GL_BGRA;
  1569. }
  1570. // Mixing GLES and GL extensions here. OpenGL EXT_bgra and
  1571. // APPLE_texture_format_BGRA8888 wants
  1572. // format to be BGRA but internal format to stay RGBA, but
  1573. // EXT_texture_format_BGRA8888 wants both format and internal
  1574. // format to be BGRA.
  1575. //
  1576. // Reference:
  1577. // https://www.khronos.org/registry/gles/extensions/EXT/EXT_texture_format_BGRA8888.txt
  1578. // https://www.opengl.org/registry/specs/EXT/bgra.txt
  1579. // https://www.khronos.org/registry/gles/extensions/APPLE/APPLE_texture_format_BGRA8888.txt
  1580. if (!s_extension[Extension::EXT_bgra ].m_supported
  1581. && !s_extension[Extension::APPLE_texture_format_BGRA8888].m_supported)
  1582. {
  1583. s_textureFormat[TextureFormat::BGRA8].m_internalFmt = GL_BGRA;
  1584. }
  1585. if (!isTextureFormatValid(TextureFormat::BGRA8) )
  1586. {
  1587. // Revert back to RGBA if texture can't be created.
  1588. setTextureFormat(TextureFormat::BGRA8, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE);
  1589. }
  1590. }
  1591. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN)
  1592. || !isTextureFormatValid(TextureFormat::R8) )
  1593. {
  1594. // GL core has to use GL_R8 Issue#208, GLES2 has to use GL_LUMINANCE issue#226
  1595. s_textureFormat[TextureFormat::R8].m_internalFmt = GL_LUMINANCE;
  1596. s_textureFormat[TextureFormat::R8].m_fmt = GL_LUMINANCE;
  1597. }
  1598. for (uint32_t ii = BX_ENABLED(BX_PLATFORM_IOS) ? TextureFormat::Unknown : 0 // skip test on iOS!
  1599. ; ii < TextureFormat::Count
  1600. ; ++ii
  1601. )
  1602. {
  1603. if (TextureFormat::Unknown != ii
  1604. && TextureFormat::UnknownDepth != ii)
  1605. {
  1606. s_textureFormat[ii].m_supported = isTextureFormatValid(TextureFormat::Enum(ii) );
  1607. }
  1608. }
  1609. if (BX_ENABLED(0) )
  1610. {
  1611. // Disable all compressed texture formats. For testing only.
  1612. for (uint32_t ii = 0; ii < TextureFormat::Unknown; ++ii)
  1613. {
  1614. s_textureFormat[ii].m_supported = false;
  1615. }
  1616. }
  1617. const bool computeSupport = false
  1618. || !!(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  1619. || s_extension[Extension::ARB_compute_shader].m_supported
  1620. ;
  1621. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  1622. {
  1623. uint16_t supported = BGFX_CAPS_FORMAT_TEXTURE_NONE;
  1624. supported |= s_textureFormat[ii].m_supported
  1625. ? BGFX_CAPS_FORMAT_TEXTURE_2D
  1626. | BGFX_CAPS_FORMAT_TEXTURE_3D
  1627. | BGFX_CAPS_FORMAT_TEXTURE_CUBE
  1628. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  1629. ;
  1630. supported |= isTextureFormatValid(TextureFormat::Enum(ii), true)
  1631. ? BGFX_CAPS_FORMAT_TEXTURE_2D_SRGB
  1632. | BGFX_CAPS_FORMAT_TEXTURE_3D_SRGB
  1633. | BGFX_CAPS_FORMAT_TEXTURE_CUBE_SRGB
  1634. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  1635. ;
  1636. supported |= isTextureFormatValid(TextureFormat::Enum(ii), false, true)
  1637. ? BGFX_CAPS_FORMAT_TEXTURE_MIP_AUTOGEN
  1638. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  1639. ;
  1640. supported |= computeSupport
  1641. && isImageFormatValid(TextureFormat::Enum(ii) )
  1642. ? BGFX_CAPS_FORMAT_TEXTURE_IMAGE
  1643. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  1644. ;
  1645. supported |= isFramebufferFormatValid(TextureFormat::Enum(ii) )
  1646. ? BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER
  1647. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  1648. ;
  1649. if (NULL != glGetInternalformativ)
  1650. {
  1651. GLint maxSamples;
  1652. glGetInternalformativ(GL_RENDERBUFFER
  1653. , s_textureFormat[ii].m_internalFmt
  1654. , GL_SAMPLES
  1655. , 1
  1656. , &maxSamples
  1657. );
  1658. GLenum err = glGetError();
  1659. supported |= 0 == err && maxSamples > 0
  1660. ? BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA
  1661. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  1662. ;
  1663. glGetInternalformativ(GL_TEXTURE_2D_MULTISAMPLE
  1664. , s_textureFormat[ii].m_internalFmt
  1665. , GL_SAMPLES
  1666. , 1
  1667. , &maxSamples
  1668. );
  1669. err = glGetError();
  1670. supported |= 0 == err && maxSamples > 0
  1671. ? BGFX_CAPS_FORMAT_TEXTURE_MSAA
  1672. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  1673. ;
  1674. }
  1675. g_caps.formats[ii] = supported;
  1676. }
  1677. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1678. || s_extension[Extension::OES_texture_3D].m_supported
  1679. ? BGFX_CAPS_TEXTURE_3D
  1680. : 0
  1681. ;
  1682. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1683. || s_extension[Extension::EXT_shadow_samplers].m_supported
  1684. ? BGFX_CAPS_TEXTURE_COMPARE_ALL
  1685. : 0
  1686. ;
  1687. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1688. || s_extension[Extension::OES_vertex_half_float].m_supported
  1689. ? BGFX_CAPS_VERTEX_ATTRIB_HALF
  1690. : 0
  1691. ;
  1692. g_caps.supported |= false
  1693. || s_extension[Extension::ARB_vertex_type_2_10_10_10_rev].m_supported
  1694. || s_extension[Extension::OES_vertex_type_10_10_10_2].m_supported
  1695. ? BGFX_CAPS_VERTEX_ATTRIB_UINT10
  1696. : 0
  1697. ;
  1698. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1699. || s_extension[Extension::EXT_frag_depth].m_supported
  1700. ? BGFX_CAPS_FRAGMENT_DEPTH
  1701. : 0
  1702. ;
  1703. g_caps.supported |= s_extension[Extension::ARB_draw_buffers_blend].m_supported
  1704. ? BGFX_CAPS_BLEND_INDEPENDENT
  1705. : 0
  1706. ;
  1707. g_caps.supported |= s_extension[Extension::INTEL_fragment_shader_ordering].m_supported
  1708. ? BGFX_CAPS_FRAGMENT_ORDERING
  1709. : 0
  1710. ;
  1711. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1712. || s_extension[Extension::OES_element_index_uint].m_supported
  1713. ? BGFX_CAPS_INDEX32
  1714. : 0
  1715. ;
  1716. const bool drawIndirectSupported = false
  1717. || s_extension[Extension::AMD_multi_draw_indirect].m_supported
  1718. || s_extension[Extension::ARB_draw_indirect ].m_supported
  1719. || s_extension[Extension::ARB_multi_draw_indirect].m_supported
  1720. || s_extension[Extension::EXT_multi_draw_indirect].m_supported
  1721. ;
  1722. if (drawIndirectSupported)
  1723. {
  1724. if (NULL == glMultiDrawArraysIndirect
  1725. || NULL == glMultiDrawElementsIndirect)
  1726. {
  1727. glMultiDrawArraysIndirect = stubMultiDrawArraysIndirect;
  1728. glMultiDrawElementsIndirect = stubMultiDrawElementsIndirect;
  1729. }
  1730. }
  1731. g_caps.supported |= drawIndirectSupported
  1732. ? BGFX_CAPS_DRAW_INDIRECT
  1733. : 0
  1734. ;
  1735. if (s_extension[Extension::ARB_copy_image].m_supported
  1736. || s_extension[Extension::EXT_copy_image].m_supported
  1737. || s_extension[Extension:: NV_copy_image].m_supported
  1738. || s_extension[Extension::OES_copy_image].m_supported)
  1739. {
  1740. m_blitSupported = NULL != glCopyImageSubData;
  1741. g_caps.supported |= m_blitSupported
  1742. ? BGFX_CAPS_TEXTURE_BLIT
  1743. : 0
  1744. ;
  1745. }
  1746. g_caps.supported |= m_readBackSupported
  1747. ? BGFX_CAPS_TEXTURE_READ_BACK
  1748. : 0
  1749. ;
  1750. g_caps.supported |= false
  1751. || s_extension[Extension::EXT_texture_array].m_supported
  1752. || s_extension[Extension::EXT_gpu_shader4].m_supported
  1753. || !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1754. ? BGFX_CAPS_TEXTURE_2D_ARRAY
  1755. : 0
  1756. ;
  1757. g_caps.supported |= false
  1758. || s_extension[Extension::ARB_texture_cube_map_array].m_supported
  1759. || s_extension[Extension::EXT_texture_cube_map_array].m_supported
  1760. ? BGFX_CAPS_TEXTURE_CUBE_ARRAY
  1761. : 0
  1762. ;
  1763. g_caps.limits.maxTextureSize = uint16_t(glGet(GL_MAX_TEXTURE_SIZE) );
  1764. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  1765. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1766. || s_extension[Extension::EXT_draw_buffers ].m_supported
  1767. || s_extension[Extension::WEBGL_draw_buffers].m_supported)
  1768. {
  1769. g_caps.limits.maxFBAttachments = uint8_t(bx::uint32_min(glGet(GL_MAX_DRAW_BUFFERS)
  1770. , BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS)
  1771. );
  1772. }
  1773. // if (s_extension[Extension::ARB_clip_control].m_supported)
  1774. // {
  1775. // GL_CHECK(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE) );
  1776. // g_caps.originBottomLeft = true;
  1777. // }
  1778. // else
  1779. {
  1780. g_caps.homogeneousDepth = true;
  1781. g_caps.originBottomLeft = true;
  1782. }
  1783. m_vaoSupport = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1784. || s_extension[Extension::ARB_vertex_array_object].m_supported
  1785. || s_extension[Extension::OES_vertex_array_object].m_supported
  1786. ;
  1787. if (BX_ENABLED(BX_PLATFORM_NACL) )
  1788. {
  1789. m_vaoSupport &= true
  1790. && NULL != glGenVertexArrays
  1791. && NULL != glDeleteVertexArrays
  1792. && NULL != glBindVertexArray
  1793. ;
  1794. }
  1795. if (m_vaoSupport)
  1796. {
  1797. GL_CHECK(glGenVertexArrays(1, &m_vao) );
  1798. }
  1799. m_samplerObjectSupport = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1800. || s_extension[Extension::ARB_sampler_objects].m_supported
  1801. ;
  1802. m_shadowSamplersSupport = !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1803. || s_extension[Extension::EXT_shadow_samplers].m_supported
  1804. ;
  1805. m_programBinarySupport = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1806. || s_extension[Extension::ARB_get_program_binary].m_supported
  1807. || s_extension[Extension::OES_get_program_binary].m_supported
  1808. || s_extension[Extension::IMG_shader_binary ].m_supported
  1809. ;
  1810. m_textureSwizzleSupport = false
  1811. || s_extension[Extension::ARB_texture_swizzle].m_supported
  1812. || s_extension[Extension::EXT_texture_swizzle].m_supported
  1813. ;
  1814. m_depthTextureSupport = !!(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  1815. || s_extension[Extension::ANGLE_depth_texture ].m_supported
  1816. || s_extension[Extension::CHROMIUM_depth_texture ].m_supported
  1817. || s_extension[Extension::GOOGLE_depth_texture ].m_supported
  1818. || s_extension[Extension::OES_depth_texture ].m_supported
  1819. || s_extension[Extension::MOZ_WEBGL_depth_texture ].m_supported
  1820. || s_extension[Extension::WEBGL_depth_texture ].m_supported
  1821. || s_extension[Extension::WEBKIT_WEBGL_depth_texture].m_supported
  1822. ;
  1823. m_timerQuerySupport = false
  1824. || s_extension[Extension::ANGLE_timer_query ].m_supported
  1825. || s_extension[Extension::ARB_timer_query ].m_supported
  1826. || s_extension[Extension::EXT_disjoint_timer_query].m_supported
  1827. || s_extension[Extension::EXT_timer_query ].m_supported
  1828. ;
  1829. m_timerQuerySupport &= true
  1830. && NULL != glQueryCounter
  1831. && NULL != glGetQueryObjectiv
  1832. && NULL != glGetQueryObjectui64v
  1833. ;
  1834. m_occlusionQuerySupport = false
  1835. || s_extension[Extension::ARB_occlusion_query ].m_supported
  1836. || s_extension[Extension::ARB_occlusion_query2 ].m_supported
  1837. || s_extension[Extension::EXT_occlusion_query_boolean].m_supported
  1838. || s_extension[Extension::NV_occlusion_query ].m_supported
  1839. ;
  1840. m_occlusionQuerySupport &= true
  1841. && NULL != glGenQueries
  1842. && NULL != glDeleteQueries
  1843. && NULL != glBeginQuery
  1844. && NULL != glEndQuery
  1845. ;
  1846. m_atocSupport = s_extension[Extension::ARB_multisample].m_supported;
  1847. m_conservativeRasterSupport = s_extension[Extension::NV_conservative_raster].m_supported;
  1848. g_caps.supported |= 0
  1849. | (m_atocSupport ? BGFX_CAPS_ALPHA_TO_COVERAGE : 0)
  1850. | (m_conservativeRasterSupport ? BGFX_CAPS_CONSERVATIVE_RASTER : 0)
  1851. | (m_occlusionQuerySupport ? BGFX_CAPS_OCCLUSION_QUERY : 0)
  1852. | (m_depthTextureSupport ? BGFX_CAPS_TEXTURE_COMPARE_LEQUAL : 0)
  1853. | (computeSupport ? BGFX_CAPS_COMPUTE : 0)
  1854. ;
  1855. g_caps.supported |= m_glctx.getCaps();
  1856. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) )
  1857. {
  1858. m_srgbWriteControlSupport = s_extension[Extension::EXT_sRGB_write_control].m_supported;
  1859. m_borderColorSupport = s_extension[Extension::NV_texture_border_clamp].m_supported;
  1860. s_textureAddress[BGFX_TEXTURE_U_BORDER>>BGFX_TEXTURE_U_SHIFT] = s_extension[Extension::NV_texture_border_clamp].m_supported
  1861. ? GL_CLAMP_TO_BORDER
  1862. : GL_CLAMP_TO_EDGE
  1863. ;
  1864. }
  1865. if (s_extension[Extension::EXT_texture_filter_anisotropic].m_supported)
  1866. {
  1867. GL_CHECK(glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_maxAnisotropyDefault) );
  1868. }
  1869. if (s_extension[Extension::ARB_texture_multisample].m_supported
  1870. || s_extension[Extension::ANGLE_framebuffer_multisample].m_supported)
  1871. {
  1872. GL_CHECK(glGetIntegerv(GL_MAX_SAMPLES, &m_maxMsaa) );
  1873. }
  1874. if (s_extension[Extension::OES_read_format].m_supported
  1875. && (s_extension[Extension::IMG_read_format].m_supported || s_extension[Extension::EXT_read_format_bgra].m_supported) )
  1876. {
  1877. m_readPixelsFmt = GL_BGRA;
  1878. }
  1879. else
  1880. {
  1881. m_readPixelsFmt = GL_RGBA;
  1882. }
  1883. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  1884. {
  1885. g_caps.supported |= BGFX_CAPS_INSTANCING;
  1886. }
  1887. else
  1888. {
  1889. if (s_extension[Extension::ANGLE_instanced_arrays].m_supported
  1890. || s_extension[Extension::ARB_instanced_arrays].m_supported
  1891. || s_extension[Extension::EXT_instanced_arrays].m_supported)
  1892. {
  1893. if (NULL != glVertexAttribDivisor
  1894. && NULL != glDrawArraysInstanced
  1895. && NULL != glDrawElementsInstanced)
  1896. {
  1897. g_caps.supported |= BGFX_CAPS_INSTANCING;
  1898. }
  1899. }
  1900. if (0 == (g_caps.supported & BGFX_CAPS_INSTANCING) )
  1901. {
  1902. glVertexAttribDivisor = stubVertexAttribDivisor;
  1903. glDrawArraysInstanced = stubDrawArraysInstanced;
  1904. glDrawElementsInstanced = stubDrawElementsInstanced;
  1905. }
  1906. }
  1907. if (s_extension[Extension::ARB_debug_output].m_supported
  1908. || s_extension[Extension::KHR_debug].m_supported)
  1909. {
  1910. if (NULL != glDebugMessageControl
  1911. && NULL != glDebugMessageInsert
  1912. && NULL != glDebugMessageCallback
  1913. && NULL != glGetDebugMessageLog)
  1914. {
  1915. GL_CHECK(glDebugMessageCallback(debugProcCb, NULL) );
  1916. GL_CHECK(glDebugMessageControl(GL_DONT_CARE
  1917. , GL_DONT_CARE
  1918. , GL_DEBUG_SEVERITY_MEDIUM
  1919. , 0
  1920. , NULL
  1921. , GL_TRUE
  1922. ) );
  1923. }
  1924. }
  1925. if (s_extension[Extension::ARB_seamless_cube_map].m_supported)
  1926. {
  1927. GL_CHECK(glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS) );
  1928. }
  1929. if (NULL == glFrameTerminatorGREMEDY
  1930. || !s_extension[Extension::GREMEDY_frame_terminator].m_supported)
  1931. {
  1932. glFrameTerminatorGREMEDY = stubFrameTerminatorGREMEDY;
  1933. }
  1934. if (NULL == glInsertEventMarker
  1935. || !s_extension[Extension::EXT_debug_marker].m_supported)
  1936. {
  1937. glInsertEventMarker = (NULL != glStringMarkerGREMEDY && s_extension[Extension::GREMEDY_string_marker].m_supported)
  1938. ? stubInsertEventMarkerGREMEDY
  1939. : stubInsertEventMarker
  1940. ;
  1941. }
  1942. setGraphicsDebuggerPresent(s_extension[Extension::EXT_debug_tool].m_supported);
  1943. if (NULL == glObjectLabel)
  1944. {
  1945. glObjectLabel = stubObjectLabel;
  1946. }
  1947. if (NULL == glInvalidateFramebuffer)
  1948. {
  1949. glInvalidateFramebuffer = stubInvalidateFramebuffer;
  1950. }
  1951. if (m_timerQuerySupport)
  1952. {
  1953. m_gpuTimer.create();
  1954. }
  1955. if (m_occlusionQuerySupport)
  1956. {
  1957. m_occlusionQuery.create();
  1958. }
  1959. // Init reserved part of view name.
  1960. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
  1961. {
  1962. bx::snprintf(s_viewName[ii], BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1, "%3d ", ii);
  1963. }
  1964. ovrPostReset();
  1965. BGFX_GPU_PROFILER_BIND();
  1966. }
  1967. void shutdown()
  1968. {
  1969. BGFX_GPU_PROFILER_UNBIND();
  1970. ovrPreReset();
  1971. m_ovr.shutdown();
  1972. if (m_vaoSupport)
  1973. {
  1974. GL_CHECK(glBindVertexArray(0) );
  1975. GL_CHECK(glDeleteVertexArrays(1, &m_vao) );
  1976. m_vao = 0;
  1977. }
  1978. captureFinish();
  1979. invalidateCache();
  1980. if (m_timerQuerySupport)
  1981. {
  1982. m_gpuTimer.destroy();
  1983. }
  1984. if (m_occlusionQuerySupport)
  1985. {
  1986. m_occlusionQuery.destroy();
  1987. }
  1988. destroyMsaaFbo();
  1989. m_glctx.destroy();
  1990. m_flip = false;
  1991. unloadRenderDoc(m_renderdocdll);
  1992. }
  1993. RendererType::Enum getRendererType() const BX_OVERRIDE
  1994. {
  1995. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  1996. {
  1997. return RendererType::OpenGL;
  1998. }
  1999. return RendererType::OpenGLES;
  2000. }
  2001. const char* getRendererName() const BX_OVERRIDE
  2002. {
  2003. return BGFX_RENDERER_OPENGL_NAME;
  2004. }
  2005. void flip(HMD& _hmd)
  2006. {
  2007. if (m_flip)
  2008. {
  2009. for (uint32_t ii = 1, num = m_numWindows; ii < num; ++ii)
  2010. {
  2011. m_glctx.swap(m_frameBuffers[m_windows[ii].idx].m_swapChain);
  2012. }
  2013. m_ovr.flip();
  2014. m_ovr.swap(_hmd); // TODO - move this out of end-of-frame
  2015. // need to swap GL render context even if OVR is enabled to get the mirror texture in the output
  2016. m_glctx.swap();
  2017. }
  2018. }
  2019. void createIndexBuffer(IndexBufferHandle _handle, Memory* _mem, uint16_t _flags) BX_OVERRIDE
  2020. {
  2021. m_indexBuffers[_handle.idx].create(_mem->size, _mem->data, _flags);
  2022. }
  2023. void destroyIndexBuffer(IndexBufferHandle _handle) BX_OVERRIDE
  2024. {
  2025. m_indexBuffers[_handle.idx].destroy();
  2026. }
  2027. void createVertexDecl(VertexDeclHandle _handle, const VertexDecl& _decl) BX_OVERRIDE
  2028. {
  2029. VertexDecl& decl = m_vertexDecls[_handle.idx];
  2030. memcpy(&decl, &_decl, sizeof(VertexDecl) );
  2031. dump(decl);
  2032. }
  2033. void destroyVertexDecl(VertexDeclHandle /*_handle*/) BX_OVERRIDE
  2034. {
  2035. }
  2036. void createVertexBuffer(VertexBufferHandle _handle, Memory* _mem, VertexDeclHandle _declHandle, uint16_t _flags) BX_OVERRIDE
  2037. {
  2038. m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _declHandle, _flags);
  2039. }
  2040. void destroyVertexBuffer(VertexBufferHandle _handle) BX_OVERRIDE
  2041. {
  2042. m_vertexBuffers[_handle.idx].destroy();
  2043. }
  2044. void createDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size, uint16_t _flags) BX_OVERRIDE
  2045. {
  2046. m_indexBuffers[_handle.idx].create(_size, NULL, _flags);
  2047. }
  2048. void updateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem) BX_OVERRIDE
  2049. {
  2050. m_indexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  2051. }
  2052. void destroyDynamicIndexBuffer(IndexBufferHandle _handle) BX_OVERRIDE
  2053. {
  2054. m_indexBuffers[_handle.idx].destroy();
  2055. }
  2056. void createDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size, uint16_t _flags) BX_OVERRIDE
  2057. {
  2058. VertexDeclHandle decl = BGFX_INVALID_HANDLE;
  2059. m_vertexBuffers[_handle.idx].create(_size, NULL, decl, _flags);
  2060. }
  2061. void updateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, Memory* _mem) BX_OVERRIDE
  2062. {
  2063. m_vertexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  2064. }
  2065. void destroyDynamicVertexBuffer(VertexBufferHandle _handle) BX_OVERRIDE
  2066. {
  2067. m_vertexBuffers[_handle.idx].destroy();
  2068. }
  2069. void createShader(ShaderHandle _handle, Memory* _mem) BX_OVERRIDE
  2070. {
  2071. m_shaders[_handle.idx].create(_mem);
  2072. }
  2073. void destroyShader(ShaderHandle _handle) BX_OVERRIDE
  2074. {
  2075. m_shaders[_handle.idx].destroy();
  2076. }
  2077. void createProgram(ProgramHandle _handle, ShaderHandle _vsh, ShaderHandle _fsh) BX_OVERRIDE
  2078. {
  2079. ShaderGL dummyFragmentShader;
  2080. m_program[_handle.idx].create(m_shaders[_vsh.idx], isValid(_fsh) ? m_shaders[_fsh.idx] : dummyFragmentShader);
  2081. }
  2082. void destroyProgram(ProgramHandle _handle) BX_OVERRIDE
  2083. {
  2084. m_program[_handle.idx].destroy();
  2085. }
  2086. void createTexture(TextureHandle _handle, Memory* _mem, uint32_t _flags, uint8_t _skip) BX_OVERRIDE
  2087. {
  2088. m_textures[_handle.idx].create(_mem, _flags, _skip);
  2089. }
  2090. void updateTextureBegin(TextureHandle /*_handle*/, uint8_t /*_side*/, uint8_t /*_mip*/) BX_OVERRIDE
  2091. {
  2092. }
  2093. 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
  2094. {
  2095. m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _pitch, _mem);
  2096. }
  2097. void updateTextureEnd() BX_OVERRIDE
  2098. {
  2099. }
  2100. void readTexture(TextureHandle _handle, void* _data) BX_OVERRIDE
  2101. {
  2102. if (m_readBackSupported)
  2103. {
  2104. const TextureGL& texture = m_textures[_handle.idx];
  2105. const bool compressed = isCompressed(TextureFormat::Enum(texture.m_textureFormat) );
  2106. GL_CHECK(glBindTexture(texture.m_target, texture.m_id) );
  2107. if (compressed)
  2108. {
  2109. GL_CHECK(glGetCompressedTexImage(texture.m_target
  2110. , 0
  2111. , _data
  2112. ) );
  2113. }
  2114. else
  2115. {
  2116. GL_CHECK(glGetTexImage(texture.m_target
  2117. , 0
  2118. , texture.m_fmt
  2119. , texture.m_type
  2120. , _data
  2121. ) );
  2122. }
  2123. GL_CHECK(glBindTexture(texture.m_target, 0) );
  2124. }
  2125. }
  2126. void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height, uint8_t _numMips) BX_OVERRIDE
  2127. {
  2128. TextureGL& texture = m_textures[_handle.idx];
  2129. uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
  2130. const Memory* mem = alloc(size);
  2131. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  2132. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  2133. bx::write(&writer, magic);
  2134. TextureCreate tc;
  2135. tc.m_width = _width;
  2136. tc.m_height = _height;
  2137. tc.m_depth = 0;
  2138. tc.m_numLayers = 1;
  2139. tc.m_numMips = _numMips;
  2140. tc.m_format = TextureFormat::Enum(texture.m_requestedFormat);
  2141. tc.m_cubeMap = false;
  2142. tc.m_mem = NULL;
  2143. bx::write(&writer, tc);
  2144. texture.destroy();
  2145. texture.create(mem, texture.m_flags, 0);
  2146. release(mem);
  2147. }
  2148. void overrideInternal(TextureHandle _handle, uintptr_t _ptr) BX_OVERRIDE
  2149. {
  2150. m_textures[_handle.idx].overrideInternal(_ptr);
  2151. }
  2152. uintptr_t getInternal(TextureHandle _handle) BX_OVERRIDE
  2153. {
  2154. return uintptr_t(m_textures[_handle.idx].m_id);
  2155. }
  2156. void destroyTexture(TextureHandle _handle) BX_OVERRIDE
  2157. {
  2158. m_textures[_handle.idx].destroy();
  2159. }
  2160. void createFrameBuffer(FrameBufferHandle _handle, uint8_t _num, const Attachment* _attachment) BX_OVERRIDE
  2161. {
  2162. m_frameBuffers[_handle.idx].create(_num, _attachment);
  2163. }
  2164. void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat) BX_OVERRIDE
  2165. {
  2166. uint16_t denseIdx = m_numWindows++;
  2167. m_windows[denseIdx] = _handle;
  2168. m_frameBuffers[_handle.idx].create(denseIdx, _nwh, _width, _height, _depthFormat);
  2169. }
  2170. void destroyFrameBuffer(FrameBufferHandle _handle) BX_OVERRIDE
  2171. {
  2172. uint16_t denseIdx = m_frameBuffers[_handle.idx].destroy();
  2173. if (UINT16_MAX != denseIdx)
  2174. {
  2175. --m_numWindows;
  2176. if (m_numWindows > 1)
  2177. {
  2178. FrameBufferHandle handle = m_windows[m_numWindows];
  2179. m_windows[denseIdx] = handle;
  2180. m_frameBuffers[handle.idx].m_denseIdx = denseIdx;
  2181. }
  2182. }
  2183. }
  2184. void createUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name) BX_OVERRIDE
  2185. {
  2186. if (NULL != m_uniforms[_handle.idx])
  2187. {
  2188. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  2189. }
  2190. uint32_t size = g_uniformTypeSize[_type]*_num;
  2191. void* data = BX_ALLOC(g_allocator, size);
  2192. memset(data, 0, size);
  2193. m_uniforms[_handle.idx] = data;
  2194. m_uniformReg.add(_handle, _name, m_uniforms[_handle.idx]);
  2195. }
  2196. void destroyUniform(UniformHandle _handle) BX_OVERRIDE
  2197. {
  2198. BX_FREE(g_allocator, m_uniforms[_handle.idx]);
  2199. m_uniforms[_handle.idx] = NULL;
  2200. m_uniformReg.remove(_handle);
  2201. }
  2202. void saveScreenShot(const char* _filePath) BX_OVERRIDE
  2203. {
  2204. uint32_t length = m_resolution.m_width*m_resolution.m_height*4;
  2205. uint8_t* data = (uint8_t*)BX_ALLOC(g_allocator, length);
  2206. uint32_t width = m_resolution.m_width;
  2207. uint32_t height = m_resolution.m_height;
  2208. GL_CHECK(glReadPixels(0
  2209. , 0
  2210. , width
  2211. , height
  2212. , m_readPixelsFmt
  2213. , GL_UNSIGNED_BYTE
  2214. , data
  2215. ) );
  2216. if (GL_RGBA == m_readPixelsFmt)
  2217. {
  2218. imageSwizzleBgra8(width, height, width*4, data, data);
  2219. }
  2220. g_callback->screenShot(_filePath
  2221. , width
  2222. , height
  2223. , width*4
  2224. , data
  2225. , length
  2226. , true
  2227. );
  2228. BX_FREE(g_allocator, data);
  2229. }
  2230. void updateViewName(uint8_t _id, const char* _name) BX_OVERRIDE
  2231. {
  2232. bx::strlcpy(&s_viewName[_id][BGFX_CONFIG_MAX_VIEW_NAME_RESERVED]
  2233. , _name
  2234. , BX_COUNTOF(s_viewName[0])-BGFX_CONFIG_MAX_VIEW_NAME_RESERVED
  2235. );
  2236. }
  2237. void updateUniform(uint16_t _loc, const void* _data, uint32_t _size) BX_OVERRIDE
  2238. {
  2239. memcpy(m_uniforms[_loc], _data, _size);
  2240. }
  2241. void setMarker(const char* _marker, uint32_t _size) BX_OVERRIDE
  2242. {
  2243. GL_CHECK(glInsertEventMarker(_size, _marker) );
  2244. }
  2245. void submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) BX_OVERRIDE;
  2246. void blitSetup(TextVideoMemBlitter& _blitter) BX_OVERRIDE
  2247. {
  2248. if (0 != m_vao)
  2249. {
  2250. GL_CHECK(glBindVertexArray(m_vao) );
  2251. }
  2252. uint32_t width = m_resolution.m_width;
  2253. uint32_t height = m_resolution.m_height;
  2254. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  2255. GL_CHECK(glViewport(0, 0, width, height) );
  2256. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  2257. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  2258. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  2259. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  2260. GL_CHECK(glDisable(GL_CULL_FACE) );
  2261. GL_CHECK(glDisable(GL_BLEND) );
  2262. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  2263. ProgramGL& program = m_program[_blitter.m_program.idx];
  2264. GL_CHECK(glUseProgram(program.m_id) );
  2265. GL_CHECK(glUniform1i(program.m_sampler[0], 0) );
  2266. float proj[16];
  2267. bx::mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f);
  2268. GL_CHECK(glUniformMatrix4fv(program.m_predefined[0].m_loc
  2269. , 1
  2270. , GL_FALSE
  2271. , proj
  2272. ) );
  2273. GL_CHECK(glActiveTexture(GL_TEXTURE0) );
  2274. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_textures[_blitter.m_texture.idx].m_id) );
  2275. if (!BX_ENABLED(BX_PLATFORM_OSX) )
  2276. {
  2277. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  2278. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  2279. {
  2280. if (m_samplerObjectSupport)
  2281. {
  2282. GL_CHECK(glBindSampler(0, 0) );
  2283. }
  2284. }
  2285. }
  2286. }
  2287. void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) BX_OVERRIDE
  2288. {
  2289. const uint32_t numVertices = _numIndices*4/6;
  2290. if (0 < numVertices)
  2291. {
  2292. m_indexBuffers[_blitter.m_ib->handle.idx].update(0, _numIndices*2, _blitter.m_ib->data);
  2293. m_vertexBuffers[_blitter.m_vb->handle.idx].update(0, numVertices*_blitter.m_decl.m_stride, _blitter.m_vb->data);
  2294. VertexBufferGL& vb = m_vertexBuffers[_blitter.m_vb->handle.idx];
  2295. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  2296. IndexBufferGL& ib = m_indexBuffers[_blitter.m_ib->handle.idx];
  2297. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  2298. ProgramGL& program = m_program[_blitter.m_program.idx];
  2299. program.bindAttributes(_blitter.m_decl, 0);
  2300. GL_CHECK(glDrawElements(GL_TRIANGLES
  2301. , _numIndices
  2302. , GL_UNSIGNED_SHORT
  2303. , (void*)0
  2304. ) );
  2305. }
  2306. }
  2307. void updateResolution(const Resolution& _resolution)
  2308. {
  2309. bool recenter = !!(_resolution.m_flags & BGFX_RESET_HMD_RECENTER);
  2310. m_maxAnisotropy = !!(_resolution.m_flags & BGFX_RESET_MAXANISOTROPY)
  2311. ? m_maxAnisotropyDefault
  2312. : 0.0f
  2313. ;
  2314. if (s_extension[Extension::ARB_depth_clamp].m_supported)
  2315. {
  2316. if (!!(_resolution.m_flags & BGFX_RESET_DEPTH_CLAMP) )
  2317. {
  2318. GL_CHECK(glEnable(GL_DEPTH_CLAMP) );
  2319. }
  2320. else
  2321. {
  2322. GL_CHECK(glDisable(GL_DEPTH_CLAMP) );
  2323. }
  2324. }
  2325. const uint32_t maskFlags = ~(0
  2326. | BGFX_RESET_HMD_RECENTER
  2327. | BGFX_RESET_MAXANISOTROPY
  2328. | BGFX_RESET_DEPTH_CLAMP
  2329. | BGFX_RESET_SUSPEND
  2330. );
  2331. if (m_resolution.m_width != _resolution.m_width
  2332. || m_resolution.m_height != _resolution.m_height
  2333. || (m_resolution.m_flags&maskFlags) != (_resolution.m_flags&maskFlags) )
  2334. {
  2335. uint32_t flags = _resolution.m_flags & (~BGFX_RESET_INTERNAL_FORCE);
  2336. m_resolution = _resolution;
  2337. m_resolution.m_flags = flags;
  2338. m_textVideoMem.resize(false, _resolution.m_width, _resolution.m_height);
  2339. m_textVideoMem.clear();
  2340. if ( (flags & BGFX_RESET_HMD)
  2341. && m_ovr.isInitialized() )
  2342. {
  2343. flags &= ~BGFX_RESET_MSAA_MASK;
  2344. }
  2345. setRenderContextSize(m_resolution.m_width
  2346. , m_resolution.m_height
  2347. , flags
  2348. );
  2349. updateCapture();
  2350. for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii)
  2351. {
  2352. m_frameBuffers[ii].postReset();
  2353. }
  2354. ovrPreReset();
  2355. ovrPostReset();
  2356. }
  2357. if (recenter)
  2358. {
  2359. m_ovr.recenter();
  2360. }
  2361. }
  2362. void setShaderUniform4f(uint8_t /*_flags*/, uint32_t _regIndex, const void* _val, uint32_t _numRegs)
  2363. {
  2364. GL_CHECK(glUniform4fv(_regIndex
  2365. , _numRegs
  2366. , (const GLfloat*)_val
  2367. ) );
  2368. }
  2369. void setShaderUniform4x4f(uint8_t /*_flags*/, uint32_t _regIndex, const void* _val, uint32_t _numRegs)
  2370. {
  2371. GL_CHECK(glUniformMatrix4fv(_regIndex
  2372. , _numRegs
  2373. , GL_FALSE
  2374. , (const GLfloat*)_val
  2375. ) );
  2376. }
  2377. uint32_t setFrameBuffer(FrameBufferHandle _fbh, uint32_t _height, uint16_t _discard = BGFX_CLEAR_NONE, bool _msaa = true)
  2378. {
  2379. if (isValid(m_fbh)
  2380. && m_fbh.idx != _fbh.idx)
  2381. {
  2382. FrameBufferGL& frameBuffer = m_frameBuffers[m_fbh.idx];
  2383. frameBuffer.resolve();
  2384. if (BGFX_CLEAR_NONE != m_fbDiscard)
  2385. {
  2386. frameBuffer.discard(m_fbDiscard);
  2387. m_fbDiscard = BGFX_CLEAR_NONE;
  2388. }
  2389. }
  2390. m_glctx.makeCurrent(NULL);
  2391. if (!isValid(_fbh) )
  2392. {
  2393. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  2394. if (m_srgbWriteControlSupport)
  2395. {
  2396. if (0 != (m_resolution.m_flags & BGFX_RESET_SRGB_BACKBUFFER) )
  2397. {
  2398. GL_CHECK(glEnable(GL_FRAMEBUFFER_SRGB) );
  2399. }
  2400. else
  2401. {
  2402. GL_CHECK(glDisable(GL_FRAMEBUFFER_SRGB) );
  2403. }
  2404. }
  2405. }
  2406. else
  2407. {
  2408. FrameBufferGL& frameBuffer = m_frameBuffers[_fbh.idx];
  2409. _height = frameBuffer.m_height;
  2410. if (UINT16_MAX != frameBuffer.m_denseIdx)
  2411. {
  2412. m_glctx.makeCurrent(frameBuffer.m_swapChain);
  2413. }
  2414. else
  2415. {
  2416. m_glctx.makeCurrent(NULL);
  2417. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer.m_fbo[0]) );
  2418. }
  2419. }
  2420. m_fbh = _fbh;
  2421. m_fbDiscard = _discard;
  2422. m_rtMsaa = _msaa;
  2423. return _height;
  2424. }
  2425. uint32_t getNumRt() const
  2426. {
  2427. if (isValid(m_fbh) )
  2428. {
  2429. const FrameBufferGL& frameBuffer = m_frameBuffers[m_fbh.idx];
  2430. return frameBuffer.m_num;
  2431. }
  2432. return 1;
  2433. }
  2434. void createMsaaFbo(uint32_t _width, uint32_t _height, uint32_t _msaa)
  2435. {
  2436. if (0 == m_msaaBackBufferFbo // iOS
  2437. && 1 < _msaa)
  2438. {
  2439. GL_CHECK(glGenFramebuffers(1, &m_msaaBackBufferFbo) );
  2440. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  2441. GL_CHECK(glGenRenderbuffers(BX_COUNTOF(m_msaaBackBufferRbos), m_msaaBackBufferRbos) );
  2442. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_msaaBackBufferRbos[0]) );
  2443. GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_RGBA8, _width, _height) );
  2444. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_msaaBackBufferRbos[1]) );
  2445. GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER, _msaa, GL_DEPTH24_STENCIL8, _width, _height) );
  2446. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_msaaBackBufferRbos[0]) );
  2447. GLenum attachment = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  2448. ? GL_DEPTH_STENCIL_ATTACHMENT
  2449. : GL_DEPTH_ATTACHMENT
  2450. ;
  2451. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, m_msaaBackBufferRbos[1]) );
  2452. BX_CHECK(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)
  2453. , "glCheckFramebufferStatus failed 0x%08x"
  2454. , glCheckFramebufferStatus(GL_FRAMEBUFFER)
  2455. );
  2456. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  2457. }
  2458. }
  2459. void destroyMsaaFbo()
  2460. {
  2461. if (m_backBufferFbo != m_msaaBackBufferFbo // iOS
  2462. && 0 != m_msaaBackBufferFbo)
  2463. {
  2464. GL_CHECK(glDeleteFramebuffers(1, &m_msaaBackBufferFbo) );
  2465. m_msaaBackBufferFbo = 0;
  2466. if (0 != m_msaaBackBufferRbos[0])
  2467. {
  2468. GL_CHECK(glDeleteRenderbuffers(BX_COUNTOF(m_msaaBackBufferRbos), m_msaaBackBufferRbos) );
  2469. m_msaaBackBufferRbos[0] = 0;
  2470. m_msaaBackBufferRbos[1] = 0;
  2471. }
  2472. }
  2473. }
  2474. void blitMsaaFbo()
  2475. {
  2476. if (m_backBufferFbo != m_msaaBackBufferFbo // iOS
  2477. && 0 != m_msaaBackBufferFbo)
  2478. {
  2479. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  2480. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  2481. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_msaaBackBufferFbo) );
  2482. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) );
  2483. uint32_t width = m_resolution.m_width;
  2484. uint32_t height = m_resolution.m_height;
  2485. GLenum filter = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30)
  2486. ? GL_NEAREST
  2487. : GL_LINEAR
  2488. ;
  2489. GL_CHECK(glBlitFramebuffer(0
  2490. , 0
  2491. , width
  2492. , height
  2493. , 0
  2494. , 0
  2495. , width
  2496. , height
  2497. , GL_COLOR_BUFFER_BIT
  2498. , filter
  2499. ) );
  2500. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  2501. }
  2502. }
  2503. void setRenderContextSize(uint32_t _width, uint32_t _height, uint32_t _flags = 0)
  2504. {
  2505. if (_width != 0
  2506. || _height != 0)
  2507. {
  2508. if (!m_glctx.isValid() )
  2509. {
  2510. m_glctx.create(_width, _height);
  2511. #if BX_PLATFORM_IOS
  2512. // iOS: need to figure out how to deal with FBO created by context.
  2513. m_backBufferFbo = m_msaaBackBufferFbo = m_glctx.getFbo();
  2514. #endif // BX_PLATFORM_IOS
  2515. }
  2516. else
  2517. {
  2518. destroyMsaaFbo();
  2519. m_glctx.resize(_width, _height, _flags);
  2520. uint32_t msaa = (_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  2521. msaa = bx::uint32_min(m_maxMsaa, msaa == 0 ? 0 : 1<<msaa);
  2522. createMsaaFbo(_width, _height, msaa);
  2523. }
  2524. }
  2525. m_flip = true;
  2526. }
  2527. void invalidateCache()
  2528. {
  2529. if (m_vaoSupport)
  2530. {
  2531. m_vaoStateCache.invalidate();
  2532. }
  2533. if ( (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  2534. && m_samplerObjectSupport)
  2535. {
  2536. m_samplerStateCache.invalidate();
  2537. }
  2538. }
  2539. void setSamplerState(uint32_t _stage, uint32_t _numMips, uint32_t _flags, const float _rgba[4])
  2540. {
  2541. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  2542. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  2543. {
  2544. if (0 == (BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER & _flags) )
  2545. {
  2546. const uint32_t index = (_flags & BGFX_TEXTURE_BORDER_COLOR_MASK) >> BGFX_TEXTURE_BORDER_COLOR_SHIFT;
  2547. _flags &= ~BGFX_TEXTURE_RESERVED_MASK;
  2548. _flags &= BGFX_TEXTURE_SAMPLER_BITS_MASK;
  2549. _flags |= _numMips<<BGFX_TEXTURE_RESERVED_SHIFT;
  2550. GLuint sampler;
  2551. bool hasBorderColor = false;
  2552. bx::HashMurmur2A murmur;
  2553. uint32_t hash;
  2554. murmur.begin();
  2555. murmur.add(_flags);
  2556. if (!needBorderColor(_flags) )
  2557. {
  2558. murmur.add(-1);
  2559. hash = murmur.end();
  2560. sampler = m_samplerStateCache.find(hash);
  2561. }
  2562. else
  2563. {
  2564. murmur.add(index);
  2565. hash = murmur.end();
  2566. if (NULL != _rgba)
  2567. {
  2568. hasBorderColor = true;
  2569. sampler = UINT32_MAX;
  2570. }
  2571. else
  2572. {
  2573. sampler = m_samplerStateCache.find(hash);
  2574. }
  2575. }
  2576. if (UINT32_MAX == sampler)
  2577. {
  2578. sampler = m_samplerStateCache.add(hash);
  2579. GL_CHECK(glSamplerParameteri(sampler
  2580. , GL_TEXTURE_WRAP_S
  2581. , s_textureAddress[(_flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]
  2582. ) );
  2583. GL_CHECK(glSamplerParameteri(sampler
  2584. , GL_TEXTURE_WRAP_T
  2585. , s_textureAddress[(_flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]
  2586. ) );
  2587. GL_CHECK(glSamplerParameteri(sampler
  2588. , GL_TEXTURE_WRAP_R
  2589. , s_textureAddress[(_flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]
  2590. ) );
  2591. GLenum minFilter;
  2592. GLenum magFilter;
  2593. getFilters(_flags, 1 < _numMips, magFilter, minFilter);
  2594. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, magFilter) );
  2595. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, minFilter) );
  2596. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  2597. || m_borderColorSupport)
  2598. {
  2599. if (hasBorderColor)
  2600. {
  2601. GL_CHECK(glSamplerParameterfv(sampler, GL_TEXTURE_BORDER_COLOR, _rgba) );
  2602. }
  2603. }
  2604. if (0 != (_flags & (BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC) )
  2605. && 0.0f < m_maxAnisotropy)
  2606. {
  2607. GL_CHECK(glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, m_maxAnisotropy) );
  2608. }
  2609. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  2610. || m_shadowSamplersSupport)
  2611. {
  2612. const uint32_t cmpFunc = (_flags&BGFX_TEXTURE_COMPARE_MASK)>>BGFX_TEXTURE_COMPARE_SHIFT;
  2613. if (0 == cmpFunc)
  2614. {
  2615. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE) );
  2616. }
  2617. else
  2618. {
  2619. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) );
  2620. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, s_cmpFunc[cmpFunc]) );
  2621. }
  2622. }
  2623. }
  2624. GL_CHECK(glBindSampler(_stage, sampler) );
  2625. }
  2626. else
  2627. {
  2628. GL_CHECK(glBindSampler(_stage, 0) );
  2629. }
  2630. }
  2631. }
  2632. bool isVisible(Frame* _render, OcclusionQueryHandle _handle, bool _visible)
  2633. {
  2634. m_occlusionQuery.resolve(_render);
  2635. return _visible == (0 != _render->m_occlusion[_handle.idx]);
  2636. }
  2637. void ovrPostReset()
  2638. {
  2639. #if BGFX_CONFIG_USE_OVR
  2640. if (m_resolution.m_flags & (BGFX_RESET_HMD|BGFX_RESET_HMD_DEBUG) )
  2641. {
  2642. const uint32_t msaaSamples = 1 << ( (m_resolution.m_flags&BGFX_RESET_MSAA_MASK) >> BGFX_RESET_MSAA_SHIFT);
  2643. m_ovr.postReset(msaaSamples, m_resolution.m_width, m_resolution.m_height);
  2644. }
  2645. #endif // BGFX_CONFIG_USE_OVR
  2646. }
  2647. void ovrPreReset()
  2648. {
  2649. #if BGFX_CONFIG_USE_OVR
  2650. m_ovr.preReset();
  2651. #endif // BGFX_CONFIG_USE_OVR
  2652. }
  2653. void updateCapture()
  2654. {
  2655. if (m_resolution.m_flags&BGFX_RESET_CAPTURE)
  2656. {
  2657. m_captureSize = m_resolution.m_width*m_resolution.m_height*4;
  2658. m_capture = BX_REALLOC(g_allocator, m_capture, m_captureSize);
  2659. g_callback->captureBegin(m_resolution.m_width, m_resolution.m_height, m_resolution.m_width*4, TextureFormat::BGRA8, true);
  2660. }
  2661. else
  2662. {
  2663. captureFinish();
  2664. }
  2665. }
  2666. void capture()
  2667. {
  2668. if (NULL != m_capture)
  2669. {
  2670. GL_CHECK(glReadPixels(0
  2671. , 0
  2672. , m_resolution.m_width
  2673. , m_resolution.m_height
  2674. , m_readPixelsFmt
  2675. , GL_UNSIGNED_BYTE
  2676. , m_capture
  2677. ) );
  2678. if (GL_RGBA == m_readPixelsFmt)
  2679. {
  2680. imageSwizzleBgra8(m_resolution.m_width, m_resolution.m_height, m_resolution.m_width*4, m_capture, m_capture);
  2681. }
  2682. g_callback->captureFrame(m_capture, m_captureSize);
  2683. }
  2684. }
  2685. void captureFinish()
  2686. {
  2687. if (NULL != m_capture)
  2688. {
  2689. g_callback->captureEnd();
  2690. BX_FREE(g_allocator, m_capture);
  2691. m_capture = NULL;
  2692. m_captureSize = 0;
  2693. }
  2694. }
  2695. bool programFetchFromCache(GLuint programId, uint64_t _id)
  2696. {
  2697. _id ^= m_hash;
  2698. bool cached = false;
  2699. if (m_programBinarySupport)
  2700. {
  2701. uint32_t length = g_callback->cacheReadSize(_id);
  2702. cached = length > 0;
  2703. if (cached)
  2704. {
  2705. void* data = BX_ALLOC(g_allocator, length);
  2706. if (g_callback->cacheRead(_id, data, length) )
  2707. {
  2708. bx::MemoryReader reader(data, length);
  2709. GLenum format;
  2710. bx::read(&reader, format);
  2711. GL_CHECK(glProgramBinary(programId, format, reader.getDataPtr(), (GLsizei)reader.remaining() ) );
  2712. }
  2713. BX_FREE(g_allocator, data);
  2714. }
  2715. #if BGFX_CONFIG_RENDERER_OPENGL
  2716. GL_CHECK(glProgramParameteri(programId, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE) );
  2717. #endif // BGFX_CONFIG_RENDERER_OPENGL
  2718. }
  2719. return cached;
  2720. }
  2721. void programCache(GLuint programId, uint64_t _id)
  2722. {
  2723. _id ^= m_hash;
  2724. if (m_programBinarySupport)
  2725. {
  2726. GLint programLength;
  2727. GLenum format;
  2728. GL_CHECK(glGetProgramiv(programId, GL_PROGRAM_BINARY_LENGTH, &programLength) );
  2729. if (0 < programLength)
  2730. {
  2731. uint32_t length = programLength + 4;
  2732. uint8_t* data = (uint8_t*)BX_ALLOC(g_allocator, length);
  2733. GL_CHECK(glGetProgramBinary(programId, programLength, NULL, &format, &data[4]) );
  2734. *(uint32_t*)data = format;
  2735. g_callback->cacheWrite(_id, data, length);
  2736. BX_FREE(g_allocator, data);
  2737. }
  2738. }
  2739. }
  2740. void commit(UniformBuffer& _uniformBuffer)
  2741. {
  2742. _uniformBuffer.reset();
  2743. for (;;)
  2744. {
  2745. uint32_t opcode = _uniformBuffer.read();
  2746. if (UniformType::End == opcode)
  2747. {
  2748. break;
  2749. }
  2750. UniformType::Enum type;
  2751. uint16_t ignore;
  2752. uint16_t num;
  2753. uint16_t copy;
  2754. UniformBuffer::decodeOpcode(opcode, type, ignore, num, copy);
  2755. const char* data;
  2756. if (copy)
  2757. {
  2758. data = _uniformBuffer.read(g_uniformTypeSize[type]*num);
  2759. }
  2760. else
  2761. {
  2762. UniformHandle handle;
  2763. memcpy(&handle, _uniformBuffer.read(sizeof(UniformHandle) ), sizeof(UniformHandle) );
  2764. data = (const char*)m_uniforms[handle.idx];
  2765. }
  2766. uint32_t loc = _uniformBuffer.read();
  2767. #define CASE_IMPLEMENT_UNIFORM(_uniform, _glsuffix, _dxsuffix, _type) \
  2768. case UniformType::_uniform: \
  2769. { \
  2770. _type* value = (_type*)data; \
  2771. GL_CHECK(glUniform##_glsuffix(loc, num, value) ); \
  2772. } \
  2773. break;
  2774. #define CASE_IMPLEMENT_UNIFORM_T(_uniform, _glsuffix, _dxsuffix, _type) \
  2775. case UniformType::_uniform: \
  2776. { \
  2777. _type* value = (_type*)data; \
  2778. GL_CHECK(glUniform##_glsuffix(loc, num, GL_FALSE, value) ); \
  2779. } \
  2780. break;
  2781. switch (type)
  2782. {
  2783. // case ConstantType::Int1:
  2784. // {
  2785. // int* value = (int*)data;
  2786. // BX_TRACE("Int1 sampler %d, loc %d (num %d, copy %d)", *value, loc, num, copy);
  2787. // GL_CHECK(glUniform1iv(loc, num, value) );
  2788. // }
  2789. // break;
  2790. CASE_IMPLEMENT_UNIFORM(Int1, 1iv, I, int);
  2791. CASE_IMPLEMENT_UNIFORM(Vec4, 4fv, F, float);
  2792. CASE_IMPLEMENT_UNIFORM_T(Mat3, Matrix3fv, F, float);
  2793. CASE_IMPLEMENT_UNIFORM_T(Mat4, Matrix4fv, F, float);
  2794. case UniformType::End:
  2795. break;
  2796. default:
  2797. BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", _uniformBuffer.getPos(), opcode, type, loc, num, copy);
  2798. break;
  2799. }
  2800. #undef CASE_IMPLEMENT_UNIFORM
  2801. #undef CASE_IMPLEMENT_UNIFORM_T
  2802. }
  2803. }
  2804. void clearQuad(ClearQuad& _clearQuad, const Rect& _rect, const Clear& _clear, uint32_t _height, const float _palette[][4])
  2805. {
  2806. uint32_t numMrt = 1;
  2807. FrameBufferHandle fbh = m_fbh;
  2808. if (isValid(fbh) )
  2809. {
  2810. const FrameBufferGL& fb = m_frameBuffers[fbh.idx];
  2811. numMrt = bx::uint32_max(1, fb.m_num);
  2812. }
  2813. if (1 == numMrt)
  2814. {
  2815. GLuint flags = 0;
  2816. if (BGFX_CLEAR_COLOR & _clear.m_flags)
  2817. {
  2818. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  2819. {
  2820. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[0]);
  2821. const float* rgba = _palette[index];
  2822. const float rr = rgba[0];
  2823. const float gg = rgba[1];
  2824. const float bb = rgba[2];
  2825. const float aa = rgba[3];
  2826. GL_CHECK(glClearColor(rr, gg, bb, aa) );
  2827. }
  2828. else
  2829. {
  2830. float rr = _clear.m_index[0]*1.0f/255.0f;
  2831. float gg = _clear.m_index[1]*1.0f/255.0f;
  2832. float bb = _clear.m_index[2]*1.0f/255.0f;
  2833. float aa = _clear.m_index[3]*1.0f/255.0f;
  2834. GL_CHECK(glClearColor(rr, gg, bb, aa) );
  2835. }
  2836. flags |= GL_COLOR_BUFFER_BIT;
  2837. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  2838. }
  2839. if (BGFX_CLEAR_DEPTH & _clear.m_flags)
  2840. {
  2841. flags |= GL_DEPTH_BUFFER_BIT;
  2842. GL_CHECK(glClearDepth(_clear.m_depth) );
  2843. GL_CHECK(glDepthMask(GL_TRUE) );
  2844. }
  2845. if (BGFX_CLEAR_STENCIL & _clear.m_flags)
  2846. {
  2847. flags |= GL_STENCIL_BUFFER_BIT;
  2848. GL_CHECK(glClearStencil(_clear.m_stencil) );
  2849. }
  2850. if (0 != flags)
  2851. {
  2852. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  2853. GL_CHECK(glScissor(_rect.m_x, _height-_rect.m_height-_rect.m_y, _rect.m_width, _rect.m_height) );
  2854. GL_CHECK(glClear(flags) );
  2855. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  2856. }
  2857. }
  2858. else
  2859. {
  2860. const GLuint defaultVao = m_vao;
  2861. if (0 != defaultVao)
  2862. {
  2863. GL_CHECK(glBindVertexArray(defaultVao) );
  2864. }
  2865. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  2866. GL_CHECK(glDisable(GL_CULL_FACE) );
  2867. GL_CHECK(glDisable(GL_BLEND) );
  2868. GLboolean colorMask = !!(BGFX_CLEAR_COLOR & _clear.m_flags);
  2869. GL_CHECK(glColorMask(colorMask, colorMask, colorMask, colorMask) );
  2870. if (BGFX_CLEAR_DEPTH & _clear.m_flags)
  2871. {
  2872. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  2873. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  2874. GL_CHECK(glDepthMask(GL_TRUE) );
  2875. }
  2876. else
  2877. {
  2878. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  2879. }
  2880. if (BGFX_CLEAR_STENCIL & _clear.m_flags)
  2881. {
  2882. GL_CHECK(glEnable(GL_STENCIL_TEST) );
  2883. GL_CHECK(glStencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS, _clear.m_stencil, 0xff) );
  2884. GL_CHECK(glStencilOpSeparate(GL_FRONT_AND_BACK, GL_REPLACE, GL_REPLACE, GL_REPLACE) );
  2885. }
  2886. else
  2887. {
  2888. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  2889. }
  2890. VertexBufferGL& vb = m_vertexBuffers[_clearQuad.m_vb->handle.idx];
  2891. VertexDecl& vertexDecl = m_vertexDecls[_clearQuad.m_vb->decl.idx];
  2892. {
  2893. struct Vertex
  2894. {
  2895. float m_x;
  2896. float m_y;
  2897. float m_z;
  2898. };
  2899. Vertex* vertex = (Vertex*)_clearQuad.m_vb->data;
  2900. BX_CHECK(vertexDecl.m_stride == sizeof(Vertex), "Stride/Vertex mismatch (stride %d, sizeof(Vertex) %d)", vertexDecl.m_stride, sizeof(Vertex) );
  2901. const float depth = _clear.m_depth * 2.0f - 1.0f;
  2902. vertex->m_x = -1.0f;
  2903. vertex->m_y = -1.0f;
  2904. vertex->m_z = depth;
  2905. vertex++;
  2906. vertex->m_x = 1.0f;
  2907. vertex->m_y = -1.0f;
  2908. vertex->m_z = depth;
  2909. vertex++;
  2910. vertex->m_x = -1.0f;
  2911. vertex->m_y = 1.0f;
  2912. vertex->m_z = depth;
  2913. vertex++;
  2914. vertex->m_x = 1.0f;
  2915. vertex->m_y = 1.0f;
  2916. vertex->m_z = depth;
  2917. }
  2918. vb.update(0, 4*_clearQuad.m_decl.m_stride, _clearQuad.m_vb->data);
  2919. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  2920. ProgramGL& program = m_program[_clearQuad.m_program[numMrt-1].idx];
  2921. GL_CHECK(glUseProgram(program.m_id) );
  2922. program.bindAttributes(vertexDecl, 0);
  2923. float mrtClear[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS][4];
  2924. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  2925. {
  2926. for (uint32_t ii = 0; ii < numMrt; ++ii)
  2927. {
  2928. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[ii]);
  2929. memcpy(mrtClear[ii], _palette[index], 16);
  2930. }
  2931. }
  2932. else
  2933. {
  2934. float rgba[4] =
  2935. {
  2936. _clear.m_index[0] * 1.0f / 255.0f,
  2937. _clear.m_index[1] * 1.0f / 255.0f,
  2938. _clear.m_index[2] * 1.0f / 255.0f,
  2939. _clear.m_index[3] * 1.0f / 255.0f,
  2940. };
  2941. for (uint32_t ii = 0; ii < numMrt; ++ii)
  2942. {
  2943. memcpy(mrtClear[ii], rgba, 16);
  2944. }
  2945. }
  2946. GL_CHECK(glUniform4fv(0, numMrt, mrtClear[0]) );
  2947. GL_CHECK(glDrawArrays(GL_TRIANGLE_STRIP
  2948. , 0
  2949. , 4
  2950. ) );
  2951. }
  2952. }
  2953. void* m_renderdocdll;
  2954. uint16_t m_numWindows;
  2955. FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  2956. IndexBufferGL m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS];
  2957. VertexBufferGL m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
  2958. ShaderGL m_shaders[BGFX_CONFIG_MAX_SHADERS];
  2959. ProgramGL m_program[BGFX_CONFIG_MAX_PROGRAMS];
  2960. TextureGL m_textures[BGFX_CONFIG_MAX_TEXTURES];
  2961. VertexDecl m_vertexDecls[BGFX_CONFIG_MAX_VERTEX_DECLS];
  2962. FrameBufferGL m_frameBuffers[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  2963. UniformRegistry m_uniformReg;
  2964. void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS];
  2965. TimerQueryGL m_gpuTimer;
  2966. OcclusionQueryGL m_occlusionQuery;
  2967. VaoStateCache m_vaoStateCache;
  2968. SamplerStateCache m_samplerStateCache;
  2969. TextVideoMem m_textVideoMem;
  2970. bool m_rtMsaa;
  2971. FrameBufferHandle m_fbh;
  2972. uint16_t m_fbDiscard;
  2973. Resolution m_resolution;
  2974. void* m_capture;
  2975. uint32_t m_captureSize;
  2976. float m_maxAnisotropy;
  2977. float m_maxAnisotropyDefault;
  2978. int32_t m_maxMsaa;
  2979. GLuint m_vao;
  2980. bool m_blitSupported;
  2981. bool m_readBackSupported;
  2982. bool m_vaoSupport;
  2983. bool m_samplerObjectSupport;
  2984. bool m_shadowSamplersSupport;
  2985. bool m_srgbWriteControlSupport;
  2986. bool m_borderColorSupport;
  2987. bool m_programBinarySupport;
  2988. bool m_textureSwizzleSupport;
  2989. bool m_depthTextureSupport;
  2990. bool m_timerQuerySupport;
  2991. bool m_occlusionQuerySupport;
  2992. bool m_atocSupport;
  2993. bool m_conservativeRasterSupport;
  2994. bool m_flip;
  2995. uint64_t m_hash;
  2996. GLenum m_readPixelsFmt;
  2997. GLuint m_backBufferFbo;
  2998. GLuint m_msaaBackBufferFbo;
  2999. GLuint m_msaaBackBufferRbos[2];
  3000. GlContext m_glctx;
  3001. const char* m_vendor;
  3002. const char* m_renderer;
  3003. const char* m_version;
  3004. const char* m_glslVersion;
  3005. VR m_ovr;
  3006. #if BGFX_CONFIG_USE_OVR
  3007. VRImplOVRGL m_ovrRender;
  3008. #endif // BGFX_CONFIG_USE_OVR
  3009. };
  3010. RendererContextGL* s_renderGL;
  3011. RendererContextI* rendererCreate()
  3012. {
  3013. s_renderGL = BX_NEW(g_allocator, RendererContextGL);
  3014. s_renderGL->init();
  3015. return s_renderGL;
  3016. }
  3017. void rendererDestroy()
  3018. {
  3019. s_renderGL->shutdown();
  3020. BX_DELETE(g_allocator, s_renderGL);
  3021. s_renderGL = NULL;
  3022. }
  3023. #if BGFX_CONFIG_USE_OVR
  3024. VRImplOVRGL::VRImplOVRGL()
  3025. : m_mirrorTexture(NULL)
  3026. {
  3027. memset(&m_textureSwapChain, 0, sizeof(m_textureSwapChain) );
  3028. }
  3029. static void setDefaultSamplerState()
  3030. {
  3031. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) );
  3032. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) );
  3033. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) );
  3034. GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) );
  3035. }
  3036. bool VRImplOVRGL::createSwapChain(const VRDesc& _desc, int _msaaSamples, int _mirrorWidth, int _mirrorHeight)
  3037. {
  3038. for (int eye = 0; eye < 2; ++eye)
  3039. {
  3040. if (NULL == m_textureSwapChain[eye])
  3041. {
  3042. m_eyeFbo[eye] = 0;
  3043. m_depthBuffer[eye] = 0;
  3044. m_msaaEyeFbo[eye] = 0;
  3045. m_msaaEyeTexId[eye] = 0;
  3046. m_msaaDepthBuffer[eye] = 0;
  3047. memset(&m_eyeTexId[eye], 0, sizeof(m_eyeTexId[eye]) );
  3048. ovrTextureSwapChainDesc swapchainDesc = {};
  3049. swapchainDesc.Type = ovrTexture_2D;
  3050. swapchainDesc.ArraySize = 1;
  3051. swapchainDesc.Width = _desc.m_eyeSize[eye].m_w;
  3052. swapchainDesc.Height = _desc.m_eyeSize[eye].m_h;
  3053. swapchainDesc.MipLevels = 1;
  3054. swapchainDesc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
  3055. swapchainDesc.SampleCount = 1;
  3056. swapchainDesc.StaticImage = ovrFalse;
  3057. ovrResult result = ovr_CreateTextureSwapChainGL(m_session, &swapchainDesc, &m_textureSwapChain[eye]);
  3058. if (!OVR_SUCCESS(result) )
  3059. {
  3060. destroySwapChain();
  3061. return false;
  3062. }
  3063. int textureCount = 0;
  3064. ovr_GetTextureSwapChainLength(m_session, m_textureSwapChain[eye], &textureCount);
  3065. for (int ii = 0; ii < textureCount; ++ii)
  3066. {
  3067. ovr_GetTextureSwapChainBufferGL(m_session, m_textureSwapChain[eye], ii, &m_eyeTexId[eye][ii]);
  3068. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_eyeTexId[eye][ii]) );
  3069. setDefaultSamplerState();
  3070. }
  3071. GL_CHECK(glGenFramebuffers(1, &m_eyeFbo[eye]) );
  3072. // create depth buffer
  3073. GL_CHECK(glGenTextures(1, &m_depthBuffer[eye]) );
  3074. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_depthBuffer[eye]) );
  3075. setDefaultSamplerState();
  3076. GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, _desc.m_eyeSize[eye].m_w, _desc.m_eyeSize[eye].m_h, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL) );
  3077. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_eyeFbo[eye]) );
  3078. GL_CHECK(glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthBuffer[eye], 0) );
  3079. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) );
  3080. // create MSAA targets if needed
  3081. if (_msaaSamples > 1)
  3082. {
  3083. GL_CHECK(glGenFramebuffers(1, &m_msaaEyeFbo[eye]) );
  3084. // create color MSAA texture
  3085. GL_CHECK(glGenTextures(1, &m_msaaEyeTexId[eye]) );
  3086. GL_CHECK(glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_msaaEyeTexId[eye]) );
  3087. GL_CHECK(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, _msaaSamples, GL_RGBA, _desc.m_eyeSize[eye].m_w, _desc.m_eyeSize[eye].m_h, false) );
  3088. setDefaultSamplerState();
  3089. // create MSAA depth buffer
  3090. GL_CHECK(glGenTextures(1, &m_msaaDepthBuffer[eye]) );
  3091. GL_CHECK(glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_depthBuffer[eye]) );
  3092. GL_CHECK(glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, _msaaSamples, GL_DEPTH_COMPONENT, _desc.m_eyeSize[eye].m_w, _desc.m_eyeSize[eye].m_h, false) );
  3093. setDefaultSamplerState();
  3094. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_msaaEyeFbo[eye]) );
  3095. GL_CHECK(glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, m_msaaEyeTexId[eye], 0) );
  3096. GL_CHECK(glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0) );
  3097. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) );
  3098. }
  3099. m_renderLayer.ColorTexture[eye] = m_textureSwapChain[eye];
  3100. }
  3101. }
  3102. m_renderLayer.Header.Flags |= ovrLayerFlag_TextureOriginAtBottomLeft;
  3103. if (NULL == m_mirrorTexture)
  3104. {
  3105. m_mirrorFbo = 0;
  3106. ovrMirrorTextureDesc mirrorDesc = {};
  3107. mirrorDesc.Width = _mirrorWidth;
  3108. mirrorDesc.Height = _mirrorHeight;
  3109. mirrorDesc.Format = OVR_FORMAT_R8G8B8A8_UNORM_SRGB;
  3110. // Fallback to doing nothing if mirror was not created. This is to prevent errors with fast window resizes
  3111. ovr_CreateMirrorTextureGL(m_session, &mirrorDesc, &m_mirrorTexture);
  3112. if (m_mirrorTexture)
  3113. {
  3114. m_mirrorWidth = _mirrorWidth;
  3115. m_mirrorHeight = _mirrorHeight;
  3116. // Configure the mirror read buffer
  3117. GLuint texId;
  3118. ovr_GetMirrorTextureBufferGL(m_session, m_mirrorTexture, &texId);
  3119. GL_CHECK(glGenFramebuffers(1, &m_mirrorFbo) );
  3120. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_mirrorFbo) );
  3121. GL_CHECK(glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0) );
  3122. GL_CHECK(glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0) );
  3123. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) );
  3124. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  3125. {
  3126. GL_CHECK(glDeleteFramebuffers(1, &m_mirrorFbo) );
  3127. ovr_DestroyMirrorTexture(m_session, m_mirrorTexture);
  3128. m_mirrorTexture = NULL;
  3129. BX_CHECK(false, "Could not initialize VR mirror buffers!");
  3130. }
  3131. }
  3132. }
  3133. return true;
  3134. }
  3135. void VRImplOVRGL::destroySwapChain()
  3136. {
  3137. for (int eye = 0; eye < 2; ++eye)
  3138. {
  3139. if (NULL != m_textureSwapChain[eye])
  3140. {
  3141. GL_CHECK(glDeleteFramebuffers(1, &m_eyeFbo[eye]) );
  3142. GL_CHECK(glDeleteTextures(1, &m_depthBuffer[eye]) );
  3143. ovr_DestroyTextureSwapChain(m_session, m_textureSwapChain[eye]);
  3144. m_textureSwapChain[eye] = NULL;
  3145. if (0 != m_msaaEyeFbo[eye])
  3146. {
  3147. GL_CHECK(glDeleteFramebuffers(1, &m_msaaEyeFbo[eye]) );
  3148. GL_CHECK(glDeleteTextures(1, &m_msaaEyeTexId[eye]) );
  3149. GL_CHECK(glDeleteTextures(1, &m_msaaDepthBuffer[eye]) );
  3150. m_msaaEyeFbo[eye] = 0;
  3151. }
  3152. }
  3153. }
  3154. destroyMirror();
  3155. }
  3156. void VRImplOVRGL::destroyMirror()
  3157. {
  3158. if (NULL != m_mirrorTexture)
  3159. {
  3160. GL_CHECK(glDeleteFramebuffers(1, &m_mirrorFbo) );
  3161. ovr_DestroyMirrorTexture(m_session, m_mirrorTexture);
  3162. m_mirrorTexture = NULL;
  3163. }
  3164. }
  3165. void VRImplOVRGL::renderEyeStart(const VRDesc& _desc, uint8_t _eye)
  3166. {
  3167. // set the current eye texture in the swap chain
  3168. if (0 != m_msaaEyeFbo[_eye])
  3169. {
  3170. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaEyeFbo[_eye]) );
  3171. }
  3172. else
  3173. {
  3174. int texIndex;
  3175. ovr_GetTextureSwapChainCurrentIndex(m_session, m_textureSwapChain[_eye], &texIndex);
  3176. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_eyeFbo[_eye]) );
  3177. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_eyeTexId[_eye][texIndex], 0) );
  3178. }
  3179. GL_CHECK(glViewport(0, 0, _desc.m_eyeSize[_eye].m_w, _desc.m_eyeSize[_eye].m_h) );
  3180. GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) );
  3181. }
  3182. bool VRImplOVRGL::submitSwapChain(const VRDesc& _desc)
  3183. {
  3184. for (int eye = 0; eye < 2; ++eye)
  3185. {
  3186. if (0 != m_msaaEyeFbo[eye])
  3187. {
  3188. // blit the contents of MSAA FBO to the regulare eye buffer "connected" to the HMD
  3189. int destIndex;
  3190. ovr_GetTextureSwapChainCurrentIndex(m_session, m_textureSwapChain[eye], &destIndex);
  3191. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_msaaEyeFbo[eye]) );
  3192. BX_CHECK(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_READ_FRAMEBUFFER)
  3193. , "glCheckFramebufferStatus failed 0x%08x"
  3194. , glCheckFramebufferStatus(GL_READ_FRAMEBUFFER)
  3195. );
  3196. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_eyeFbo[eye]) );
  3197. GL_CHECK(glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_eyeTexId[eye][destIndex], 0) );
  3198. BX_CHECK(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER)
  3199. , "glCheckFramebufferStatus failed 0x%08x"
  3200. , glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER)
  3201. );
  3202. GL_CHECK(glBlitFramebuffer(0, 0, _desc.m_eyeSize[eye].m_w, _desc.m_eyeSize[eye].m_h, 0, 0, _desc.m_eyeSize[eye].m_w, _desc.m_eyeSize[eye].m_h, GL_COLOR_BUFFER_BIT, GL_NEAREST) );
  3203. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) );
  3204. }
  3205. ovrResult result = ovr_CommitTextureSwapChain(m_session, m_textureSwapChain[eye]);
  3206. if (!OVR_SUCCESS(result) )
  3207. {
  3208. return false;
  3209. }
  3210. }
  3211. ovrLayerHeader* layerList = &m_renderLayer.Header;
  3212. ovrResult result = ovr_SubmitFrame(m_session, 0, &m_viewScale, &layerList, 1);
  3213. if (!OVR_SUCCESS(result) )
  3214. {
  3215. return false;
  3216. }
  3217. if (result != ovrSuccess_NotVisible && NULL != m_mirrorTexture)
  3218. {
  3219. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_mirrorFbo) );
  3220. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) );
  3221. GL_CHECK(glBlitFramebuffer(0, m_mirrorHeight, m_mirrorWidth, 0, 0, 0, m_mirrorWidth, m_mirrorHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST) );
  3222. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, 0) );
  3223. }
  3224. return true;
  3225. }
  3226. #endif // BGFX_CONFIG_USE_OVR
  3227. const char* glslTypeName(GLuint _type)
  3228. {
  3229. #define GLSL_TYPE(_ty) case _ty: return #_ty
  3230. switch (_type)
  3231. {
  3232. GLSL_TYPE(GL_INT);
  3233. GLSL_TYPE(GL_INT_VEC2);
  3234. GLSL_TYPE(GL_INT_VEC3);
  3235. GLSL_TYPE(GL_INT_VEC4);
  3236. GLSL_TYPE(GL_UNSIGNED_INT);
  3237. GLSL_TYPE(GL_UNSIGNED_INT_VEC2);
  3238. GLSL_TYPE(GL_UNSIGNED_INT_VEC3);
  3239. GLSL_TYPE(GL_UNSIGNED_INT_VEC4);
  3240. GLSL_TYPE(GL_FLOAT);
  3241. GLSL_TYPE(GL_FLOAT_VEC2);
  3242. GLSL_TYPE(GL_FLOAT_VEC3);
  3243. GLSL_TYPE(GL_FLOAT_VEC4);
  3244. GLSL_TYPE(GL_FLOAT_MAT2);
  3245. GLSL_TYPE(GL_FLOAT_MAT3);
  3246. GLSL_TYPE(GL_FLOAT_MAT4);
  3247. GLSL_TYPE(GL_SAMPLER_2D);
  3248. GLSL_TYPE(GL_SAMPLER_2D_ARRAY);
  3249. GLSL_TYPE(GL_SAMPLER_2D_MULTISAMPLE);
  3250. GLSL_TYPE(GL_INT_SAMPLER_2D);
  3251. GLSL_TYPE(GL_INT_SAMPLER_2D_ARRAY);
  3252. GLSL_TYPE(GL_INT_SAMPLER_2D_MULTISAMPLE);
  3253. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_2D);
  3254. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_2D_ARRAY);
  3255. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE);
  3256. GLSL_TYPE(GL_SAMPLER_2D_SHADOW);
  3257. GLSL_TYPE(GL_SAMPLER_2D_ARRAY_SHADOW);
  3258. GLSL_TYPE(GL_SAMPLER_3D);
  3259. GLSL_TYPE(GL_INT_SAMPLER_3D);
  3260. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_3D);
  3261. GLSL_TYPE(GL_SAMPLER_CUBE);
  3262. GLSL_TYPE(GL_INT_SAMPLER_CUBE);
  3263. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_CUBE);
  3264. GLSL_TYPE(GL_IMAGE_1D);
  3265. GLSL_TYPE(GL_INT_IMAGE_1D);
  3266. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_1D);
  3267. GLSL_TYPE(GL_IMAGE_2D);
  3268. GLSL_TYPE(GL_INT_IMAGE_2D);
  3269. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_2D);
  3270. GLSL_TYPE(GL_IMAGE_3D);
  3271. GLSL_TYPE(GL_INT_IMAGE_3D);
  3272. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_3D);
  3273. GLSL_TYPE(GL_IMAGE_CUBE);
  3274. GLSL_TYPE(GL_INT_IMAGE_CUBE);
  3275. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_CUBE);
  3276. }
  3277. #undef GLSL_TYPE
  3278. BX_CHECK(false, "Unknown GLSL type? %x", _type);
  3279. return "UNKNOWN GLSL TYPE!";
  3280. }
  3281. const char* glEnumName(GLenum _enum)
  3282. {
  3283. #define GLENUM(_ty) case _ty: return #_ty
  3284. switch (_enum)
  3285. {
  3286. GLENUM(GL_TEXTURE);
  3287. GLENUM(GL_RENDERBUFFER);
  3288. GLENUM(GL_INVALID_ENUM);
  3289. GLENUM(GL_INVALID_FRAMEBUFFER_OPERATION);
  3290. GLENUM(GL_INVALID_VALUE);
  3291. GLENUM(GL_INVALID_OPERATION);
  3292. GLENUM(GL_OUT_OF_MEMORY);
  3293. GLENUM(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
  3294. GLENUM(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
  3295. // GLENUM(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER);
  3296. // GLENUM(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER);
  3297. GLENUM(GL_FRAMEBUFFER_UNSUPPORTED);
  3298. }
  3299. #undef GLENUM
  3300. BX_WARN(false, "Unknown enum? %x", _enum);
  3301. return "<GLenum?>";
  3302. }
  3303. UniformType::Enum convertGlType(GLenum _type)
  3304. {
  3305. switch (_type)
  3306. {
  3307. case GL_INT:
  3308. case GL_UNSIGNED_INT:
  3309. return UniformType::Int1;
  3310. case GL_FLOAT:
  3311. case GL_FLOAT_VEC2:
  3312. case GL_FLOAT_VEC3:
  3313. case GL_FLOAT_VEC4:
  3314. return UniformType::Vec4;
  3315. case GL_FLOAT_MAT2:
  3316. break;
  3317. case GL_FLOAT_MAT3:
  3318. return UniformType::Mat3;
  3319. case GL_FLOAT_MAT4:
  3320. return UniformType::Mat4;
  3321. case GL_SAMPLER_2D:
  3322. case GL_SAMPLER_2D_ARRAY:
  3323. case GL_SAMPLER_2D_MULTISAMPLE:
  3324. case GL_INT_SAMPLER_2D:
  3325. case GL_INT_SAMPLER_2D_ARRAY:
  3326. case GL_INT_SAMPLER_2D_MULTISAMPLE:
  3327. case GL_UNSIGNED_INT_SAMPLER_2D:
  3328. case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
  3329. case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
  3330. case GL_SAMPLER_2D_SHADOW:
  3331. case GL_SAMPLER_2D_ARRAY_SHADOW:
  3332. case GL_SAMPLER_3D:
  3333. case GL_INT_SAMPLER_3D:
  3334. case GL_UNSIGNED_INT_SAMPLER_3D:
  3335. case GL_SAMPLER_CUBE:
  3336. case GL_INT_SAMPLER_CUBE:
  3337. case GL_UNSIGNED_INT_SAMPLER_CUBE:
  3338. case GL_IMAGE_1D:
  3339. case GL_INT_IMAGE_1D:
  3340. case GL_UNSIGNED_INT_IMAGE_1D:
  3341. case GL_IMAGE_2D:
  3342. case GL_INT_IMAGE_2D:
  3343. case GL_UNSIGNED_INT_IMAGE_2D:
  3344. case GL_IMAGE_3D:
  3345. case GL_INT_IMAGE_3D:
  3346. case GL_UNSIGNED_INT_IMAGE_3D:
  3347. case GL_IMAGE_CUBE:
  3348. case GL_INT_IMAGE_CUBE:
  3349. case GL_UNSIGNED_INT_IMAGE_CUBE:
  3350. return UniformType::Int1;
  3351. };
  3352. BX_CHECK(false, "Unrecognized GL type 0x%04x.", _type);
  3353. return UniformType::End;
  3354. }
  3355. void ProgramGL::create(const ShaderGL& _vsh, const ShaderGL& _fsh)
  3356. {
  3357. m_id = glCreateProgram();
  3358. BX_TRACE("Program create: GL%d: GL%d, GL%d", m_id, _vsh.m_id, _fsh.m_id);
  3359. const uint64_t id = (uint64_t(_vsh.m_hash)<<32) | _fsh.m_hash;
  3360. const bool cached = s_renderGL->programFetchFromCache(m_id, id);
  3361. if (!cached)
  3362. {
  3363. GLint linked = 0;
  3364. if (0 != _vsh.m_id)
  3365. {
  3366. GL_CHECK(glAttachShader(m_id, _vsh.m_id) );
  3367. if (0 != _fsh.m_id)
  3368. {
  3369. GL_CHECK(glAttachShader(m_id, _fsh.m_id) );
  3370. }
  3371. GL_CHECK(glLinkProgram(m_id) );
  3372. GL_CHECK(glGetProgramiv(m_id, GL_LINK_STATUS, &linked) );
  3373. if (0 == linked)
  3374. {
  3375. char log[1024];
  3376. GL_CHECK(glGetProgramInfoLog(m_id, sizeof(log), NULL, log) );
  3377. BX_TRACE("%d: %s", linked, log);
  3378. }
  3379. }
  3380. if (0 == linked)
  3381. {
  3382. BX_WARN(0 != _vsh.m_id, "Invalid vertex/compute shader.");
  3383. GL_CHECK(glDeleteProgram(m_id) );
  3384. m_used[0] = Attrib::Count;
  3385. m_id = 0;
  3386. return;
  3387. }
  3388. s_renderGL->programCache(m_id, id);
  3389. }
  3390. init();
  3391. if (!cached)
  3392. {
  3393. // Must be after init, otherwise init might fail to lookup shader
  3394. // info (NVIDIA Tegra 3 OpenGL ES 2.0 14.01003).
  3395. GL_CHECK(glDetachShader(m_id, _vsh.m_id) );
  3396. if (0 != _fsh.m_id)
  3397. {
  3398. GL_CHECK(glDetachShader(m_id, _fsh.m_id) );
  3399. }
  3400. }
  3401. }
  3402. void ProgramGL::destroy()
  3403. {
  3404. if (NULL != m_constantBuffer)
  3405. {
  3406. UniformBuffer::destroy(m_constantBuffer);
  3407. m_constantBuffer = NULL;
  3408. }
  3409. m_numPredefined = 0;
  3410. if (0 != m_id)
  3411. {
  3412. GL_CHECK(glUseProgram(0) );
  3413. GL_CHECK(glDeleteProgram(m_id) );
  3414. m_id = 0;
  3415. }
  3416. m_vcref.invalidate(s_renderGL->m_vaoStateCache);
  3417. }
  3418. void ProgramGL::init()
  3419. {
  3420. GLint activeAttribs = 0;
  3421. GLint activeUniforms = 0;
  3422. GLint activeBuffers = 0;
  3423. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  3424. GL_CHECK(glBindFragDataLocation(m_id, 0, "bgfx_FragColor") );
  3425. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  3426. GLint max0, max1;
  3427. bool piqSupported = true
  3428. && s_extension[Extension::ARB_program_interface_query ].m_supported
  3429. && s_extension[Extension::ARB_shader_storage_buffer_object].m_supported
  3430. ;
  3431. if (piqSupported)
  3432. {
  3433. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &activeAttribs ) );
  3434. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_UNIFORM, GL_ACTIVE_RESOURCES, &activeUniforms) );
  3435. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_BUFFER_VARIABLE, GL_ACTIVE_RESOURCES, &activeBuffers ) );
  3436. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_PROGRAM_INPUT, GL_MAX_NAME_LENGTH, &max0 ) );
  3437. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_UNIFORM, GL_MAX_NAME_LENGTH, &max1 ) );
  3438. }
  3439. else
  3440. {
  3441. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTES, &activeAttribs ) );
  3442. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORMS, &activeUniforms) );
  3443. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max0) );
  3444. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max1) );
  3445. }
  3446. uint32_t maxLength = bx::uint32_max(max0, max1);
  3447. char* name = (char*)alloca(maxLength + 1);
  3448. BX_TRACE("Program %d", m_id);
  3449. BX_TRACE("Attributes (%d):", activeAttribs);
  3450. for (int32_t ii = 0; ii < activeAttribs; ++ii)
  3451. {
  3452. GLint size;
  3453. GLenum type = 0;
  3454. if (piqSupported)
  3455. {
  3456. GL_CHECK(glGetProgramResourceName(m_id, GL_PROGRAM_INPUT, ii, maxLength + 1, &size, name) );
  3457. GLenum typeProp[] = { GL_TYPE };
  3458. GL_CHECK(glGetProgramResourceiv(m_id
  3459. , GL_PROGRAM_INPUT
  3460. , ii
  3461. , BX_COUNTOF(typeProp)
  3462. , typeProp
  3463. , 1
  3464. , NULL
  3465. , (GLint *)&type)
  3466. );
  3467. }
  3468. else
  3469. {
  3470. GL_CHECK(glGetActiveAttrib(m_id, ii, maxLength + 1, NULL, &size, &type, name) );
  3471. }
  3472. BX_TRACE("\t%s %s is at location %d"
  3473. , glslTypeName(type)
  3474. , name
  3475. , glGetAttribLocation(m_id, name)
  3476. );
  3477. }
  3478. m_numPredefined = 0;
  3479. m_numSamplers = 0;
  3480. BX_TRACE("Uniforms (%d):", activeUniforms);
  3481. for (int32_t ii = 0; ii < activeUniforms; ++ii)
  3482. {
  3483. struct VariableInfo
  3484. {
  3485. GLenum type;
  3486. GLint loc;
  3487. GLint num;
  3488. };
  3489. VariableInfo vi;
  3490. GLenum props[] ={ GL_TYPE, GL_LOCATION, GL_ARRAY_SIZE };
  3491. GLenum gltype;
  3492. GLint num;
  3493. GLint loc;
  3494. if (piqSupported)
  3495. {
  3496. GL_CHECK(glGetProgramResourceiv(m_id
  3497. , GL_UNIFORM
  3498. , ii
  3499. , BX_COUNTOF(props)
  3500. , props
  3501. , BX_COUNTOF(props)
  3502. , NULL
  3503. , (GLint*)&vi
  3504. ) );
  3505. GL_CHECK(glGetProgramResourceName(m_id
  3506. , GL_UNIFORM
  3507. , ii
  3508. , maxLength + 1
  3509. , NULL
  3510. , name
  3511. ) );
  3512. gltype = vi.type;
  3513. loc = vi.loc;
  3514. num = vi.num;
  3515. }
  3516. else
  3517. {
  3518. GL_CHECK(glGetActiveUniform(m_id, ii, maxLength + 1, NULL, &num, &gltype, name) );
  3519. loc = glGetUniformLocation(m_id, name);
  3520. }
  3521. num = bx::uint32_max(num, 1);
  3522. int offset = 0;
  3523. char* array = strchr(name, '[');
  3524. if (NULL != array)
  3525. {
  3526. BX_TRACE("--- %s", name);
  3527. *array = '\0';
  3528. array++;
  3529. char* end = strchr(array, ']');
  3530. if (NULL != end)
  3531. { // Some devices (Amazon Fire) might not return terminating brace.
  3532. *end = '\0';
  3533. offset = atoi(array);
  3534. }
  3535. }
  3536. switch (gltype)
  3537. {
  3538. case GL_SAMPLER_2D:
  3539. case GL_SAMPLER_2D_ARRAY:
  3540. case GL_SAMPLER_2D_MULTISAMPLE:
  3541. case GL_INT_SAMPLER_2D:
  3542. case GL_INT_SAMPLER_2D_ARRAY:
  3543. case GL_INT_SAMPLER_2D_MULTISAMPLE:
  3544. case GL_UNSIGNED_INT_SAMPLER_2D:
  3545. case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
  3546. case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
  3547. case GL_SAMPLER_2D_SHADOW:
  3548. case GL_SAMPLER_2D_ARRAY_SHADOW:
  3549. case GL_SAMPLER_3D:
  3550. case GL_INT_SAMPLER_3D:
  3551. case GL_UNSIGNED_INT_SAMPLER_3D:
  3552. case GL_SAMPLER_CUBE:
  3553. case GL_INT_SAMPLER_CUBE:
  3554. case GL_UNSIGNED_INT_SAMPLER_CUBE:
  3555. case GL_IMAGE_1D:
  3556. case GL_INT_IMAGE_1D:
  3557. case GL_UNSIGNED_INT_IMAGE_1D:
  3558. case GL_IMAGE_2D:
  3559. case GL_INT_IMAGE_2D:
  3560. case GL_UNSIGNED_INT_IMAGE_2D:
  3561. case GL_IMAGE_3D:
  3562. case GL_INT_IMAGE_3D:
  3563. case GL_UNSIGNED_INT_IMAGE_3D:
  3564. case GL_IMAGE_CUBE:
  3565. case GL_INT_IMAGE_CUBE:
  3566. case GL_UNSIGNED_INT_IMAGE_CUBE:
  3567. if (m_numSamplers < BX_COUNTOF(m_sampler) )
  3568. {
  3569. BX_TRACE("Sampler #%d at location %d.", m_numSamplers, loc);
  3570. m_sampler[m_numSamplers] = loc;
  3571. m_numSamplers++;
  3572. }
  3573. else
  3574. {
  3575. BX_TRACE("Too many samplers (max: %d)! Sampler at location %d."
  3576. , BX_COUNTOF(m_sampler)
  3577. , loc
  3578. );
  3579. }
  3580. break;
  3581. default:
  3582. break;
  3583. }
  3584. PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
  3585. if (PredefinedUniform::Count != predefined)
  3586. {
  3587. m_predefined[m_numPredefined].m_loc = loc;
  3588. m_predefined[m_numPredefined].m_count = uint16_t(num);
  3589. m_predefined[m_numPredefined].m_type = uint8_t(predefined);
  3590. m_numPredefined++;
  3591. }
  3592. else
  3593. {
  3594. const UniformInfo* info = s_renderGL->m_uniformReg.find(name);
  3595. BX_WARN(NULL != info, "User defined uniform '%s' is not found, it won't be set.", name);
  3596. if (NULL != info)
  3597. {
  3598. if (NULL == m_constantBuffer)
  3599. {
  3600. m_constantBuffer = UniformBuffer::create(1024);
  3601. }
  3602. UniformType::Enum type = convertGlType(gltype);
  3603. m_constantBuffer->writeUniformHandle(type, 0, info->m_handle, uint16_t(num) );
  3604. m_constantBuffer->write(loc);
  3605. BX_TRACE("store %s %d", name, info->m_handle);
  3606. }
  3607. }
  3608. BX_TRACE("\tuniform %s %s%s is at location %d, size %d, offset %d"
  3609. , glslTypeName(gltype)
  3610. , name
  3611. , PredefinedUniform::Count != predefined ? "*" : ""
  3612. , loc
  3613. , num
  3614. , offset
  3615. );
  3616. BX_UNUSED(offset);
  3617. }
  3618. if (NULL != m_constantBuffer)
  3619. {
  3620. m_constantBuffer->finish();
  3621. }
  3622. if (piqSupported)
  3623. {
  3624. struct VariableInfo
  3625. {
  3626. GLenum type;
  3627. };
  3628. VariableInfo vi;
  3629. GLenum props[] = { GL_TYPE };
  3630. BX_TRACE("Buffers (%d):", activeBuffers);
  3631. for (int32_t ii = 0; ii < activeBuffers; ++ii)
  3632. {
  3633. GL_CHECK(glGetProgramResourceiv(m_id
  3634. , GL_BUFFER_VARIABLE
  3635. , ii
  3636. , BX_COUNTOF(props)
  3637. , props
  3638. , BX_COUNTOF(props)
  3639. , NULL
  3640. , (GLint*)&vi
  3641. ) );
  3642. GL_CHECK(glGetProgramResourceName(m_id
  3643. , GL_BUFFER_VARIABLE
  3644. , ii
  3645. , maxLength + 1
  3646. , NULL
  3647. , name
  3648. ) );
  3649. BX_TRACE("\t%s %s at %d"
  3650. , glslTypeName(vi.type)
  3651. , name
  3652. , 0 //vi.loc
  3653. );
  3654. }
  3655. }
  3656. memset(m_attributes, 0xff, sizeof(m_attributes) );
  3657. uint32_t used = 0;
  3658. for (uint8_t ii = 0; ii < Attrib::Count; ++ii)
  3659. {
  3660. GLint loc = glGetAttribLocation(m_id, s_attribName[ii]);
  3661. if (-1 != loc)
  3662. {
  3663. BX_TRACE("attr %s: %d", s_attribName[ii], loc);
  3664. m_attributes[ii] = loc;
  3665. m_used[used++] = ii;
  3666. }
  3667. }
  3668. BX_CHECK(used < BX_COUNTOF(m_used), "Out of bounds %d > array size %d."
  3669. , used
  3670. , BX_COUNTOF(m_used)
  3671. );
  3672. m_used[used] = Attrib::Count;
  3673. used = 0;
  3674. for (uint32_t ii = 0; ii < BX_COUNTOF(s_instanceDataName); ++ii)
  3675. {
  3676. GLuint loc = glGetAttribLocation(m_id, s_instanceDataName[ii]);
  3677. if (GLuint(-1) != loc )
  3678. {
  3679. BX_TRACE("instance data %s: %d", s_instanceDataName[ii], loc);
  3680. m_instanceData[used++] = loc;
  3681. }
  3682. }
  3683. BX_CHECK(used < BX_COUNTOF(m_instanceData), "Out of bounds %d > array size %d."
  3684. , used
  3685. , BX_COUNTOF(m_instanceData)
  3686. );
  3687. m_instanceData[used] = 0xffff;
  3688. }
  3689. void ProgramGL::bindAttributes(const VertexDecl& _vertexDecl, uint32_t _baseVertex) const
  3690. {
  3691. for (uint32_t ii = 0; Attrib::Count != m_used[ii]; ++ii)
  3692. {
  3693. Attrib::Enum attr = Attrib::Enum(m_used[ii]);
  3694. GLint loc = m_attributes[attr];
  3695. uint8_t num;
  3696. AttribType::Enum type;
  3697. bool normalized;
  3698. bool asInt;
  3699. _vertexDecl.decode(attr, num, type, normalized, asInt);
  3700. if (-1 != loc)
  3701. {
  3702. if (UINT16_MAX != _vertexDecl.m_attributes[attr])
  3703. {
  3704. GL_CHECK(glEnableVertexAttribArray(loc) );
  3705. GL_CHECK(glVertexAttribDivisor(loc, 0) );
  3706. uint32_t baseVertex = _baseVertex*_vertexDecl.m_stride + _vertexDecl.m_offset[attr];
  3707. if ( (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 30) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31) )
  3708. && (AttribType::Uint8 == type || AttribType::Int16 == type)
  3709. && !normalized)
  3710. {
  3711. GL_CHECK(glVertexAttribIPointer(loc
  3712. , num
  3713. , s_attribType[type]
  3714. , _vertexDecl.m_stride
  3715. , (void*)(uintptr_t)baseVertex)
  3716. );
  3717. }
  3718. else
  3719. {
  3720. GL_CHECK(glVertexAttribPointer(loc
  3721. , num
  3722. , s_attribType[type]
  3723. , normalized
  3724. , _vertexDecl.m_stride
  3725. , (void*)(uintptr_t)baseVertex)
  3726. );
  3727. }
  3728. }
  3729. else
  3730. {
  3731. GL_CHECK(glDisableVertexAttribArray(loc) );
  3732. }
  3733. }
  3734. }
  3735. }
  3736. void ProgramGL::bindInstanceData(uint32_t _stride, uint32_t _baseVertex) const
  3737. {
  3738. uint32_t baseVertex = _baseVertex;
  3739. for (uint32_t ii = 0; 0xffff != m_instanceData[ii]; ++ii)
  3740. {
  3741. GLint loc = m_instanceData[ii];
  3742. GL_CHECK(glEnableVertexAttribArray(loc) );
  3743. GL_CHECK(glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, _stride, (void*)(uintptr_t)baseVertex) );
  3744. GL_CHECK(glVertexAttribDivisor(loc, 1) );
  3745. baseVertex += 16;
  3746. }
  3747. }
  3748. void IndexBufferGL::destroy()
  3749. {
  3750. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  3751. GL_CHECK(glDeleteBuffers(1, &m_id) );
  3752. m_vcref.invalidate(s_renderGL->m_vaoStateCache);
  3753. }
  3754. void VertexBufferGL::destroy()
  3755. {
  3756. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  3757. GL_CHECK(glDeleteBuffers(1, &m_id) );
  3758. m_vcref.invalidate(s_renderGL->m_vaoStateCache);
  3759. }
  3760. static void texSubImage(
  3761. GLenum _target
  3762. , GLint _level
  3763. , GLint _xoffset
  3764. , GLint _yoffset
  3765. , GLint _zoffset
  3766. , GLsizei _width
  3767. , GLsizei _height
  3768. , GLsizei _depth
  3769. , GLenum _format
  3770. , GLenum _type
  3771. , const GLvoid* _data
  3772. )
  3773. {
  3774. if (_target == GL_TEXTURE_3D
  3775. || _target == GL_TEXTURE_2D_ARRAY
  3776. || _target == GL_TEXTURE_CUBE_MAP_ARRAY)
  3777. {
  3778. GL_CHECK(glTexSubImage3D(
  3779. _target
  3780. , _level
  3781. , _xoffset
  3782. , _yoffset
  3783. , _zoffset
  3784. , _width
  3785. , _height
  3786. , _depth
  3787. , _format
  3788. , _type
  3789. , _data
  3790. ) );
  3791. }
  3792. else if (_target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
  3793. {
  3794. }
  3795. else
  3796. {
  3797. BX_UNUSED(_zoffset, _depth);
  3798. GL_CHECK(glTexSubImage2D(
  3799. _target
  3800. , _level
  3801. , _xoffset
  3802. , _yoffset
  3803. , _width
  3804. , _height
  3805. , _format
  3806. , _type
  3807. , _data
  3808. ) );
  3809. }
  3810. }
  3811. static void texImage(
  3812. GLenum _target
  3813. , uint32_t _msaaQuality
  3814. , GLint _level
  3815. , GLint _internalFormat
  3816. , GLsizei _width
  3817. , GLsizei _height
  3818. , GLsizei _depth
  3819. , GLint _border
  3820. , GLenum _format
  3821. , GLenum _type
  3822. , const GLvoid* _data
  3823. )
  3824. {
  3825. if (_target == GL_TEXTURE_3D)
  3826. {
  3827. GL_CHECK(glTexImage3D(
  3828. _target
  3829. , _level
  3830. , _internalFormat
  3831. , _width
  3832. , _height
  3833. , _depth
  3834. , _border
  3835. , _format
  3836. , _type
  3837. , _data
  3838. ) );
  3839. }
  3840. else if (_target == GL_TEXTURE_2D_ARRAY
  3841. || _target == GL_TEXTURE_CUBE_MAP_ARRAY)
  3842. {
  3843. texSubImage(_target, _level, 0, 0, _depth, _width, _height, 1, _format, _type, _data);
  3844. }
  3845. else if (_target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
  3846. {
  3847. }
  3848. else if (_target == GL_TEXTURE_2D_MULTISAMPLE)
  3849. {
  3850. GL_CHECK(glTexImage2DMultisample(
  3851. _target
  3852. , _msaaQuality
  3853. , _internalFormat
  3854. , _width
  3855. , _height
  3856. , false
  3857. ) );
  3858. }
  3859. else
  3860. {
  3861. GL_CHECK(glTexImage2D(
  3862. _target
  3863. , _level
  3864. , _internalFormat
  3865. , _width
  3866. , _height
  3867. , _border
  3868. , _format
  3869. , _type
  3870. , _data
  3871. ) );
  3872. }
  3873. BX_UNUSED(_msaaQuality, _depth, _border, _data);
  3874. }
  3875. static void compressedTexSubImage(
  3876. GLenum _target
  3877. , GLint _level
  3878. , GLint _xoffset
  3879. , GLint _yoffset
  3880. , GLint _zoffset
  3881. , GLsizei _width
  3882. , GLsizei _height
  3883. , GLsizei _depth
  3884. , GLenum _format
  3885. , GLsizei _imageSize
  3886. , const GLvoid* _data
  3887. )
  3888. {
  3889. if (_target == GL_TEXTURE_3D)
  3890. {
  3891. GL_CHECK(glCompressedTexSubImage3D(
  3892. _target
  3893. , _level
  3894. , _xoffset
  3895. , _yoffset
  3896. , _zoffset
  3897. , _width
  3898. , _height
  3899. , _depth
  3900. , _format
  3901. , _imageSize
  3902. , _data
  3903. ) );
  3904. }
  3905. else
  3906. {
  3907. BX_UNUSED(_zoffset, _depth);
  3908. GL_CHECK(glCompressedTexSubImage2D(
  3909. _target
  3910. , _level
  3911. , _xoffset
  3912. , _yoffset
  3913. , _width
  3914. , _height
  3915. , _format
  3916. , _imageSize
  3917. , _data
  3918. ) );
  3919. }
  3920. }
  3921. static void compressedTexImage(
  3922. GLenum _target
  3923. , GLint _level
  3924. , GLenum _internalformat
  3925. , GLsizei _width
  3926. , GLsizei _height
  3927. , GLsizei _depth
  3928. , GLint _border
  3929. , GLsizei _imageSize
  3930. , const GLvoid* _data
  3931. )
  3932. {
  3933. if (_target == GL_TEXTURE_3D)
  3934. {
  3935. GL_CHECK(glCompressedTexImage3D(
  3936. _target
  3937. , _level
  3938. , _internalformat
  3939. , _width
  3940. , _height
  3941. , _depth
  3942. , _border
  3943. , _imageSize
  3944. , _data
  3945. ) );
  3946. }
  3947. else if (_target == GL_TEXTURE_2D_ARRAY
  3948. || _target == GL_TEXTURE_CUBE_MAP_ARRAY)
  3949. {
  3950. compressedTexSubImage(_target, _level, 0, 0, _depth, _width, _height, 1, _internalformat, _imageSize, _data);
  3951. }
  3952. else if (_target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
  3953. {
  3954. }
  3955. else
  3956. {
  3957. BX_UNUSED(_depth);
  3958. GL_CHECK(glCompressedTexImage2D(
  3959. _target
  3960. , _level
  3961. , _internalformat
  3962. , _width
  3963. , _height
  3964. , _border
  3965. , _imageSize
  3966. , _data
  3967. ) );
  3968. }
  3969. }
  3970. bool TextureGL::init(GLenum _target, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _flags)
  3971. {
  3972. m_target = _target;
  3973. m_numMips = _numMips;
  3974. m_flags = _flags;
  3975. m_width = _width;
  3976. m_height = _height;
  3977. m_depth = _depth;
  3978. m_currentSamplerHash = UINT32_MAX;
  3979. const bool writeOnly = 0 != (m_flags&BGFX_TEXTURE_RT_WRITE_ONLY);
  3980. const bool computeWrite = 0 != (m_flags&BGFX_TEXTURE_COMPUTE_WRITE );
  3981. const bool textureArray = false
  3982. || _target == GL_TEXTURE_2D_ARRAY
  3983. || _target == GL_TEXTURE_CUBE_MAP_ARRAY
  3984. ;
  3985. if (!writeOnly)
  3986. {
  3987. GL_CHECK(glGenTextures(1, &m_id) );
  3988. BX_CHECK(0 != m_id, "Failed to generate texture id.");
  3989. GL_CHECK(glBindTexture(_target, m_id) );
  3990. const TextureFormatInfo& tfi = s_textureFormat[m_textureFormat];
  3991. m_fmt = tfi.m_fmt;
  3992. m_type = tfi.m_type;
  3993. const bool swizzle = true
  3994. && TextureFormat::BGRA8 == m_requestedFormat
  3995. && !s_textureFormat[m_requestedFormat].m_supported
  3996. && !s_renderGL->m_textureSwizzleSupport
  3997. ;
  3998. const bool convert = false
  3999. || m_textureFormat != m_requestedFormat
  4000. || swizzle
  4001. ;
  4002. if (convert)
  4003. {
  4004. m_textureFormat = (uint8_t)TextureFormat::RGBA8;
  4005. const TextureFormatInfo& tfiRgba8 = s_textureFormat[TextureFormat::RGBA8];
  4006. m_fmt = tfiRgba8.m_fmt;
  4007. m_type = tfiRgba8.m_type;
  4008. }
  4009. if (textureArray)
  4010. {
  4011. GL_CHECK(glTexStorage3D(_target
  4012. , _numMips
  4013. , s_textureFormat[m_textureFormat].m_internalFmt
  4014. , m_width
  4015. , m_height
  4016. , _depth
  4017. ) );
  4018. }
  4019. if (computeWrite)
  4020. {
  4021. if (_target == GL_TEXTURE_3D)
  4022. {
  4023. GL_CHECK(glTexStorage3D(_target
  4024. , _numMips
  4025. , s_textureFormat[m_textureFormat].m_internalFmt
  4026. , m_width
  4027. , m_height
  4028. , _depth
  4029. ) );
  4030. }
  4031. else
  4032. {
  4033. GL_CHECK(glTexStorage2D(_target
  4034. , _numMips
  4035. , s_textureFormat[m_textureFormat].m_internalFmt
  4036. , m_width
  4037. , m_height
  4038. ) );
  4039. }
  4040. }
  4041. setSamplerState(_flags, NULL);
  4042. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  4043. && TextureFormat::BGRA8 == m_requestedFormat
  4044. && !s_textureFormat[m_requestedFormat].m_supported
  4045. && s_renderGL->m_textureSwizzleSupport)
  4046. {
  4047. GLint swizzleMask[] = { GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
  4048. GL_CHECK(glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask) );
  4049. }
  4050. }
  4051. const bool renderTarget = 0 != (m_flags&BGFX_TEXTURE_RT_MASK);
  4052. if (renderTarget)
  4053. {
  4054. uint32_t msaaQuality = ( (m_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT);
  4055. msaaQuality = bx::uint32_satsub(msaaQuality, 1);
  4056. msaaQuality = bx::uint32_min(s_renderGL->m_maxMsaa, msaaQuality == 0 ? 0 : 1<<msaaQuality);
  4057. const bool msaaSample = 0 != (m_flags&BGFX_TEXTURE_MSAA_SAMPLE);
  4058. if (!msaaSample
  4059. && (0 != msaaQuality || writeOnly) )
  4060. {
  4061. GL_CHECK(glGenRenderbuffers(1, &m_rbo) );
  4062. BX_CHECK(0 != m_rbo, "Failed to generate renderbuffer id.");
  4063. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_rbo) );
  4064. if (0 == msaaQuality)
  4065. {
  4066. GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER
  4067. , s_rboFormat[m_textureFormat]
  4068. , _width
  4069. , _height
  4070. ) );
  4071. }
  4072. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  4073. {
  4074. GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER
  4075. , msaaQuality
  4076. , s_rboFormat[m_textureFormat]
  4077. , _width
  4078. , _height
  4079. ) );
  4080. }
  4081. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0) );
  4082. if (writeOnly)
  4083. {
  4084. // This is render buffer, there is no sampling, no need
  4085. // to create texture.
  4086. return false;
  4087. }
  4088. }
  4089. }
  4090. return true;
  4091. }
  4092. void TextureGL::create(const Memory* _mem, uint32_t _flags, uint8_t _skip)
  4093. {
  4094. ImageContainer imageContainer;
  4095. if (imageParse(imageContainer, _mem->data, _mem->size) )
  4096. {
  4097. uint8_t numMips = imageContainer.m_numMips;
  4098. const uint8_t startLod = uint8_t(bx::uint32_min(_skip, numMips-1) );
  4099. numMips -= startLod;
  4100. const uint16_t numLayers = imageContainer.m_numLayers;
  4101. uint32_t textureWidth;
  4102. uint32_t textureHeight;
  4103. uint32_t textureDepth;
  4104. {
  4105. const ImageBlockInfo& ibi = getBlockInfo(TextureFormat::Enum(imageContainer.m_format) );
  4106. textureWidth = bx::uint32_max(ibi.blockWidth, imageContainer.m_width >>startLod);
  4107. textureHeight = bx::uint32_max(ibi.blockHeight, imageContainer.m_height>>startLod);
  4108. textureDepth = 1 < imageContainer.m_depth
  4109. ? imageContainer.m_depth
  4110. : imageContainer.m_numLayers
  4111. ;
  4112. }
  4113. m_requestedFormat = uint8_t(imageContainer.m_format);
  4114. m_textureFormat = uint8_t(getViableTextureFormat(imageContainer) );
  4115. const bool computeWrite = 0 != (_flags&BGFX_TEXTURE_COMPUTE_WRITE);
  4116. const bool srgb = 0 != (_flags&BGFX_TEXTURE_SRGB);
  4117. const bool msaaSample = 0 != (_flags&BGFX_TEXTURE_MSAA_SAMPLE);
  4118. uint32_t msaaQuality = ( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT);
  4119. msaaQuality = bx::uint32_satsub(msaaQuality, 1);
  4120. msaaQuality = bx::uint32_min(s_renderGL->m_maxMsaa, msaaQuality == 0 ? 0 : 1<<msaaQuality);
  4121. GLenum target = msaaSample ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
  4122. if (imageContainer.m_cubeMap)
  4123. {
  4124. target = GL_TEXTURE_CUBE_MAP;
  4125. }
  4126. else if (imageContainer.m_depth > 1)
  4127. {
  4128. target = GL_TEXTURE_3D;
  4129. }
  4130. const bool textureArray = 1 < numLayers;
  4131. if (textureArray)
  4132. {
  4133. switch (target)
  4134. {
  4135. case GL_TEXTURE_CUBE_MAP: target = GL_TEXTURE_CUBE_MAP_ARRAY;
  4136. case GL_TEXTURE_2D_MULTISAMPLE: target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
  4137. default: target = GL_TEXTURE_2D_ARRAY;
  4138. }
  4139. }
  4140. if (!init(target
  4141. , textureWidth
  4142. , textureHeight
  4143. , textureDepth
  4144. , numMips
  4145. , _flags
  4146. ) )
  4147. {
  4148. return;
  4149. }
  4150. target = isCubeMap()
  4151. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
  4152. : m_target
  4153. ;
  4154. const GLenum internalFmt = srgb
  4155. ? s_textureFormat[m_textureFormat].m_internalFmtSrgb
  4156. : s_textureFormat[m_textureFormat].m_internalFmt
  4157. ;
  4158. const bool swizzle = true
  4159. && TextureFormat::BGRA8 == m_requestedFormat
  4160. && !s_textureFormat[m_requestedFormat].m_supported
  4161. && !s_renderGL->m_textureSwizzleSupport
  4162. ;
  4163. const bool compressed = isCompressed(TextureFormat::Enum(m_requestedFormat) );
  4164. const bool convert = false
  4165. || m_textureFormat != m_requestedFormat
  4166. || swizzle
  4167. ;
  4168. BX_TRACE("Texture%-4s %3d: %s (requested: %s), layers %d, %dx%dx%d%s."
  4169. , imageContainer.m_cubeMap ? "Cube" : (1 < imageContainer.m_depth ? "3D" : "2D")
  4170. , this - s_renderGL->m_textures
  4171. , getName( (TextureFormat::Enum)m_textureFormat)
  4172. , getName( (TextureFormat::Enum)m_requestedFormat)
  4173. , numLayers
  4174. , textureWidth
  4175. , textureHeight
  4176. , imageContainer.m_cubeMap ? 6 : (1 < imageContainer.m_depth ? imageContainer.m_depth : 0)
  4177. , 0 != (m_flags&BGFX_TEXTURE_RT_MASK) ? " (render target)" : ""
  4178. );
  4179. BX_WARN(!convert, "Texture %s%s%s from %s to %s."
  4180. , swizzle ? "swizzle" : ""
  4181. , swizzle&&convert ? " and " : ""
  4182. , convert ? "convert" : ""
  4183. , getName( (TextureFormat::Enum)m_requestedFormat)
  4184. , getName( (TextureFormat::Enum)m_textureFormat)
  4185. );
  4186. uint8_t* temp = NULL;
  4187. if (convert)
  4188. {
  4189. temp = (uint8_t*)BX_ALLOC(g_allocator, textureWidth*textureHeight*4);
  4190. }
  4191. const uint16_t numSides = numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  4192. for (uint16_t side = 0; side < numSides; ++side)
  4193. {
  4194. uint32_t width = textureWidth;
  4195. uint32_t height = textureHeight;
  4196. uint32_t depth = imageContainer.m_depth;
  4197. GLenum imageTarget = imageContainer.m_cubeMap && !textureArray
  4198. ? target+side
  4199. : target
  4200. ;
  4201. for (uint8_t lod = 0, num = numMips; lod < num; ++lod)
  4202. {
  4203. width = bx::uint32_max(1, width);
  4204. height = bx::uint32_max(1, height);
  4205. depth = 1 < imageContainer.m_depth
  4206. ? bx::uint32_max(1, depth)
  4207. : side
  4208. ;
  4209. ImageMip mip;
  4210. if (imageGetRawData(imageContainer, side, lod+startLod, _mem->data, _mem->size, mip) )
  4211. {
  4212. if (compressed
  4213. && !convert)
  4214. {
  4215. compressedTexImage(imageTarget
  4216. , lod
  4217. , internalFmt
  4218. , width
  4219. , height
  4220. , depth
  4221. , 0
  4222. , mip.m_size
  4223. , mip.m_data
  4224. );
  4225. }
  4226. else
  4227. {
  4228. const uint8_t* data = mip.m_data;
  4229. if (convert)
  4230. {
  4231. imageDecodeToRgba8(temp
  4232. , mip.m_data
  4233. , mip.m_width
  4234. , mip.m_height
  4235. , mip.m_width*4
  4236. , mip.m_format
  4237. );
  4238. data = temp;
  4239. }
  4240. texImage(imageTarget
  4241. , msaaQuality
  4242. , lod
  4243. , internalFmt
  4244. , width
  4245. , height
  4246. , depth
  4247. , 0
  4248. , m_fmt
  4249. , m_type
  4250. , data
  4251. );
  4252. }
  4253. }
  4254. else if (!computeWrite)
  4255. {
  4256. if (compressed)
  4257. {
  4258. uint32_t size = bx::uint32_max(1, (width + 3)>>2)
  4259. * bx::uint32_max(1, (height + 3)>>2)
  4260. * 4*4*getBitsPerPixel(TextureFormat::Enum(m_textureFormat) )/8
  4261. ;
  4262. compressedTexImage(imageTarget
  4263. , lod
  4264. , internalFmt
  4265. , width
  4266. , height
  4267. , depth
  4268. , 0
  4269. , size
  4270. , NULL
  4271. );
  4272. }
  4273. else
  4274. {
  4275. texImage(imageTarget
  4276. , msaaQuality
  4277. , lod
  4278. , internalFmt
  4279. , width
  4280. , height
  4281. , depth
  4282. , 0
  4283. , m_fmt
  4284. , m_type
  4285. , NULL
  4286. );
  4287. }
  4288. }
  4289. width >>= 1;
  4290. height >>= 1;
  4291. depth >>= 1;
  4292. }
  4293. }
  4294. if (NULL != temp)
  4295. {
  4296. BX_FREE(g_allocator, temp);
  4297. }
  4298. }
  4299. GL_CHECK(glBindTexture(m_target, 0) );
  4300. }
  4301. void TextureGL::destroy()
  4302. {
  4303. if (0 == (m_flags & BGFX_TEXTURE_INTERNAL_SHARED)
  4304. && 0 != m_id)
  4305. {
  4306. GL_CHECK(glBindTexture(m_target, 0) );
  4307. GL_CHECK(glDeleteTextures(1, &m_id) );
  4308. m_id = 0;
  4309. }
  4310. if (0 != m_rbo)
  4311. {
  4312. GL_CHECK(glDeleteRenderbuffers(1, &m_rbo) );
  4313. m_rbo = 0;
  4314. }
  4315. }
  4316. void TextureGL::overrideInternal(uintptr_t _ptr)
  4317. {
  4318. destroy();
  4319. m_flags |= BGFX_TEXTURE_INTERNAL_SHARED;
  4320. m_id = (GLuint)_ptr;
  4321. }
  4322. void TextureGL::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem)
  4323. {
  4324. const uint32_t bpp = getBitsPerPixel(TextureFormat::Enum(m_textureFormat) );
  4325. const uint32_t rectpitch = _rect.m_width*bpp/8;
  4326. uint32_t srcpitch = UINT16_MAX == _pitch ? rectpitch : _pitch;
  4327. GL_CHECK(glBindTexture(m_target, m_id) );
  4328. GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1) );
  4329. GLenum target = isCubeMap()
  4330. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
  4331. : m_target
  4332. ;
  4333. const bool swizzle = true
  4334. && TextureFormat::BGRA8 == m_requestedFormat
  4335. && !s_textureFormat[m_requestedFormat].m_supported
  4336. && !s_renderGL->m_textureSwizzleSupport
  4337. ;
  4338. const bool unpackRowLength = BX_IGNORE_C4127(!!BGFX_CONFIG_RENDERER_OPENGL || s_extension[Extension::EXT_unpack_subimage].m_supported);
  4339. const bool compressed = isCompressed(TextureFormat::Enum(m_requestedFormat) );
  4340. const bool convert = false
  4341. || (compressed && m_textureFormat != m_requestedFormat)
  4342. || swizzle
  4343. ;
  4344. const uint32_t width = _rect.m_width;
  4345. const uint32_t height = _rect.m_height;
  4346. uint8_t* temp = NULL;
  4347. if (convert
  4348. || !unpackRowLength)
  4349. {
  4350. temp = (uint8_t*)BX_ALLOC(g_allocator, rectpitch*height);
  4351. }
  4352. else if (unpackRowLength)
  4353. {
  4354. GL_CHECK(glPixelStorei(GL_UNPACK_ROW_LENGTH, srcpitch*8/bpp) );
  4355. }
  4356. if (compressed)
  4357. {
  4358. const uint8_t* data = _mem->data;
  4359. if (!unpackRowLength)
  4360. {
  4361. imageCopy(width, height, bpp, srcpitch, data, temp);
  4362. data = temp;
  4363. }
  4364. GL_CHECK(compressedTexSubImage(target+_side
  4365. , _mip
  4366. , _rect.m_x
  4367. , _rect.m_y
  4368. , _z
  4369. , _rect.m_width
  4370. , _rect.m_height
  4371. , _depth
  4372. , m_fmt
  4373. , _mem->size
  4374. , data
  4375. ) );
  4376. }
  4377. else
  4378. {
  4379. const uint8_t* data = _mem->data;
  4380. if (convert)
  4381. {
  4382. imageDecodeToRgba8(temp, data, width, height, srcpitch, TextureFormat::Enum(m_requestedFormat) );
  4383. data = temp;
  4384. srcpitch = rectpitch;
  4385. }
  4386. if (!unpackRowLength
  4387. && !convert)
  4388. {
  4389. imageCopy(width, height, bpp, srcpitch, data, temp);
  4390. data = temp;
  4391. }
  4392. GL_CHECK(texSubImage(target+_side
  4393. , _mip
  4394. , _rect.m_x
  4395. , _rect.m_y
  4396. , _z
  4397. , _rect.m_width
  4398. , _rect.m_height
  4399. , _depth
  4400. , m_fmt
  4401. , m_type
  4402. , data
  4403. ) );
  4404. }
  4405. if (!convert
  4406. && unpackRowLength)
  4407. {
  4408. GL_CHECK(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0) );
  4409. }
  4410. if (NULL != temp)
  4411. {
  4412. BX_FREE(g_allocator, temp);
  4413. }
  4414. }
  4415. void TextureGL::setSamplerState(uint32_t _flags, const float _rgba[4])
  4416. {
  4417. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30)
  4418. && !s_textureFilter[m_textureFormat])
  4419. {
  4420. // Force point sampling when texture format doesn't support linear sampling.
  4421. _flags &= ~(0
  4422. | BGFX_TEXTURE_MIN_MASK
  4423. | BGFX_TEXTURE_MAG_MASK
  4424. | BGFX_TEXTURE_MIP_MASK
  4425. );
  4426. _flags |= 0
  4427. | BGFX_TEXTURE_MIN_POINT
  4428. | BGFX_TEXTURE_MAG_POINT
  4429. | BGFX_TEXTURE_MIP_POINT
  4430. ;
  4431. }
  4432. const uint32_t flags = (0 != (BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER & _flags) ? m_flags : _flags) & BGFX_TEXTURE_SAMPLER_BITS_MASK;
  4433. bool hasBorderColor = false;
  4434. bx::HashMurmur2A murmur;
  4435. murmur.begin();
  4436. murmur.add(flags);
  4437. if (NULL != _rgba)
  4438. {
  4439. if (BGFX_TEXTURE_U_BORDER == (flags & BGFX_TEXTURE_U_BORDER)
  4440. || BGFX_TEXTURE_V_BORDER == (flags & BGFX_TEXTURE_V_BORDER)
  4441. || BGFX_TEXTURE_W_BORDER == (flags & BGFX_TEXTURE_W_BORDER) )
  4442. {
  4443. murmur.add(_rgba, 16);
  4444. hasBorderColor = true;
  4445. }
  4446. }
  4447. uint32_t hash = murmur.end();
  4448. if (hash != m_currentSamplerHash)
  4449. {
  4450. const GLenum target = m_target == GL_TEXTURE_2D_MULTISAMPLE ? GL_TEXTURE_2D : m_target;
  4451. const GLenum targetMsaa = m_target;
  4452. const uint8_t numMips = m_numMips;
  4453. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_S, s_textureAddress[(flags&BGFX_TEXTURE_U_MASK)>>BGFX_TEXTURE_U_SHIFT]) );
  4454. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_T, s_textureAddress[(flags&BGFX_TEXTURE_V_MASK)>>BGFX_TEXTURE_V_SHIFT]) );
  4455. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  4456. || s_extension[Extension::APPLE_texture_max_level].m_supported)
  4457. {
  4458. GL_CHECK(glTexParameteri(targetMsaa, GL_TEXTURE_MAX_LEVEL, numMips-1) );
  4459. }
  4460. if (target == GL_TEXTURE_3D)
  4461. {
  4462. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_R, s_textureAddress[(flags&BGFX_TEXTURE_W_MASK)>>BGFX_TEXTURE_W_SHIFT]) );
  4463. }
  4464. GLenum magFilter;
  4465. GLenum minFilter;
  4466. getFilters(flags, 1 < numMips, magFilter, minFilter);
  4467. GL_CHECK(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter) );
  4468. GL_CHECK(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter) );
  4469. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  4470. || s_renderGL->m_borderColorSupport)
  4471. {
  4472. if (hasBorderColor)
  4473. {
  4474. GL_CHECK(glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _rgba) );
  4475. }
  4476. }
  4477. if (0 != (flags & (BGFX_TEXTURE_MIN_ANISOTROPIC|BGFX_TEXTURE_MAG_ANISOTROPIC) )
  4478. && 0.0f < s_renderGL->m_maxAnisotropy)
  4479. {
  4480. GL_CHECK(glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, s_renderGL->m_maxAnisotropy) );
  4481. }
  4482. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  4483. || s_renderGL->m_shadowSamplersSupport)
  4484. {
  4485. const uint32_t cmpFunc = (flags&BGFX_TEXTURE_COMPARE_MASK)>>BGFX_TEXTURE_COMPARE_SHIFT;
  4486. if (0 == cmpFunc)
  4487. {
  4488. GL_CHECK(glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_NONE) );
  4489. }
  4490. else
  4491. {
  4492. GL_CHECK(glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) );
  4493. GL_CHECK(glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, s_cmpFunc[cmpFunc]) );
  4494. }
  4495. }
  4496. m_currentSamplerHash = hash;
  4497. }
  4498. }
  4499. void TextureGL::commit(uint32_t _stage, uint32_t _flags, const float _palette[][4])
  4500. {
  4501. const uint32_t flags = 0 == (BGFX_TEXTURE_INTERNAL_DEFAULT_SAMPLER & _flags)
  4502. ? _flags
  4503. : m_flags
  4504. ;
  4505. const uint32_t index = (flags & BGFX_TEXTURE_BORDER_COLOR_MASK) >> BGFX_TEXTURE_BORDER_COLOR_SHIFT;
  4506. GL_CHECK(glActiveTexture(GL_TEXTURE0+_stage) );
  4507. GL_CHECK(glBindTexture(m_target, m_id) );
  4508. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES)
  4509. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) )
  4510. {
  4511. // GLES2 doesn't have support for sampler object.
  4512. setSamplerState(flags, _palette[index]);
  4513. }
  4514. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  4515. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL < 31) )
  4516. {
  4517. // In case that GL 2.1 sampler object is supported via extension.
  4518. if (s_renderGL->m_samplerObjectSupport)
  4519. {
  4520. s_renderGL->setSamplerState(_stage, m_numMips, flags, _palette[index]);
  4521. }
  4522. else
  4523. {
  4524. setSamplerState(flags, _palette[index]);
  4525. }
  4526. }
  4527. else
  4528. {
  4529. // Everything else has sampler object.
  4530. s_renderGL->setSamplerState(_stage, m_numMips, flags, _palette[index]);
  4531. }
  4532. }
  4533. void TextureGL::resolve() const
  4534. {
  4535. const bool renderTarget = 0 != (m_flags&BGFX_TEXTURE_RT_MASK);
  4536. if (renderTarget
  4537. && 1 < m_numMips)
  4538. {
  4539. GL_CHECK(glBindTexture(m_target, m_id) );
  4540. GL_CHECK(glGenerateMipmap(m_target) );
  4541. GL_CHECK(glBindTexture(m_target, 0) );
  4542. }
  4543. }
  4544. void writeString(bx::WriterI* _writer, const char* _str)
  4545. {
  4546. bx::write(_writer, _str, (int32_t)strlen(_str) );
  4547. }
  4548. void writeStringf(bx::WriterI* _writer, const char* _format, ...)
  4549. {
  4550. char temp[512];
  4551. va_list argList;
  4552. va_start(argList, _format);
  4553. int len = bx::vsnprintf(temp, BX_COUNTOF(temp), _format, argList);
  4554. va_end(argList);
  4555. bx::write(_writer, temp, len);
  4556. }
  4557. void strins(char* _str, const char* _insert)
  4558. {
  4559. size_t len = strlen(_insert);
  4560. memmove(&_str[len], _str, strlen(_str)+1);
  4561. memcpy(_str, _insert, len);
  4562. }
  4563. void ShaderGL::create(Memory* _mem)
  4564. {
  4565. bx::MemoryReader reader(_mem->data, _mem->size);
  4566. m_hash = bx::hashMurmur2A(_mem->data, _mem->size);
  4567. uint32_t magic;
  4568. bx::read(&reader, magic);
  4569. switch (magic)
  4570. {
  4571. case BGFX_CHUNK_MAGIC_CSH: m_type = GL_COMPUTE_SHADER; break;
  4572. case BGFX_CHUNK_MAGIC_FSH: m_type = GL_FRAGMENT_SHADER; break;
  4573. case BGFX_CHUNK_MAGIC_VSH: m_type = GL_VERTEX_SHADER; break;
  4574. default:
  4575. BGFX_FATAL(false, Fatal::InvalidShader, "Unknown shader format %x.", magic);
  4576. break;
  4577. }
  4578. uint32_t iohash;
  4579. bx::read(&reader, iohash);
  4580. uint16_t count;
  4581. bx::read(&reader, count);
  4582. BX_TRACE("%s Shader consts %d"
  4583. , BGFX_CHUNK_MAGIC_FSH == magic ? "Fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "Vertex" : "Compute"
  4584. , count
  4585. );
  4586. for (uint32_t ii = 0; ii < count; ++ii)
  4587. {
  4588. uint8_t nameSize = 0;
  4589. bx::read(&reader, nameSize);
  4590. char name[256];
  4591. bx::read(&reader, &name, nameSize);
  4592. name[nameSize] = '\0';
  4593. uint8_t type;
  4594. bx::read(&reader, type);
  4595. uint8_t num;
  4596. bx::read(&reader, num);
  4597. uint16_t regIndex;
  4598. bx::read(&reader, regIndex);
  4599. uint16_t regCount;
  4600. bx::read(&reader, regCount);
  4601. }
  4602. uint32_t shaderSize;
  4603. bx::read(&reader, shaderSize);
  4604. m_id = glCreateShader(m_type);
  4605. BX_WARN(0 != m_id, "Failed to create %s shader."
  4606. , BGFX_CHUNK_MAGIC_FSH == magic ? "fragment" : BGFX_CHUNK_MAGIC_VSH == magic ? "vertex" : "compute"
  4607. );
  4608. const char* code = (const char*)reader.getDataPtr();
  4609. if (0 != m_id)
  4610. {
  4611. if (GL_COMPUTE_SHADER != m_type)
  4612. {
  4613. int32_t codeLen = (int32_t)strlen(code);
  4614. int32_t tempLen = codeLen + (4<<10);
  4615. char* temp = (char*)alloca(tempLen);
  4616. bx::StaticMemoryBlockWriter writer(temp, tempLen);
  4617. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES)
  4618. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES < 30) )
  4619. {
  4620. writeString(&writer,
  4621. "#define centroid\n"
  4622. "#define flat\n"
  4623. "#define noperspective\n"
  4624. "#define smooth\n"
  4625. );
  4626. bool usesDerivatives = s_extension[Extension::OES_standard_derivatives].m_supported
  4627. && bx::findIdentifierMatch(code, s_OES_standard_derivatives)
  4628. ;
  4629. bool usesFragData = !!bx::findIdentifierMatch(code, "gl_FragData");
  4630. bool usesFragDepth = !!bx::findIdentifierMatch(code, "gl_FragDepth");
  4631. bool usesShadowSamplers = !!bx::findIdentifierMatch(code, s_EXT_shadow_samplers);
  4632. bool usesTexture3D = s_extension[Extension::OES_texture_3D].m_supported
  4633. && bx::findIdentifierMatch(code, s_OES_texture_3D)
  4634. ;
  4635. bool usesTextureLod = !!bx::findIdentifierMatch(code, s_EXT_shader_texture_lod);
  4636. bool usesFragmentOrdering = !!bx::findIdentifierMatch(code, "beginFragmentShaderOrdering");
  4637. if (usesDerivatives)
  4638. {
  4639. writeString(&writer, "#extension GL_OES_standard_derivatives : enable\n");
  4640. }
  4641. if (usesFragData)
  4642. {
  4643. BX_WARN(s_extension[Extension::EXT_draw_buffers ].m_supported
  4644. || s_extension[Extension::WEBGL_draw_buffers].m_supported
  4645. , "EXT_draw_buffers is used but not supported by GLES2 driver."
  4646. );
  4647. writeString(&writer
  4648. , "#extension GL_EXT_draw_buffers : enable\n"
  4649. );
  4650. }
  4651. bool insertFragDepth = false;
  4652. if (usesFragDepth)
  4653. {
  4654. BX_WARN(s_extension[Extension::EXT_frag_depth].m_supported, "EXT_frag_depth is used but not supported by GLES2 driver.");
  4655. if (s_extension[Extension::EXT_frag_depth].m_supported)
  4656. {
  4657. writeString(&writer
  4658. , "#extension GL_EXT_frag_depth : enable\n"
  4659. "#define bgfx_FragDepth gl_FragDepthEXT\n"
  4660. );
  4661. char str[128];
  4662. bx::snprintf(str, BX_COUNTOF(str), "%s float gl_FragDepthEXT;\n"
  4663. , s_extension[Extension::OES_fragment_precision_high].m_supported ? "highp" : "mediump"
  4664. );
  4665. writeString(&writer, str);
  4666. }
  4667. else
  4668. {
  4669. insertFragDepth = true;
  4670. }
  4671. }
  4672. if (usesShadowSamplers)
  4673. {
  4674. if (s_renderGL->m_shadowSamplersSupport)
  4675. {
  4676. writeString(&writer
  4677. , "#extension GL_EXT_shadow_samplers : enable\n"
  4678. "#define shadow2D shadow2DEXT\n"
  4679. "#define shadow2DProj shadow2DProjEXT\n"
  4680. );
  4681. }
  4682. else
  4683. {
  4684. writeString(&writer
  4685. , "#define sampler2DShadow sampler2D\n"
  4686. "#define shadow2D(_sampler, _coord) step(_coord.z, texture2D(_sampler, _coord.xy).x)\n"
  4687. "#define shadow2DProj(_sampler, _coord) step(_coord.z/_coord.w, texture2DProj(_sampler, _coord).x)\n"
  4688. );
  4689. }
  4690. }
  4691. if (usesTexture3D)
  4692. {
  4693. writeString(&writer, "#extension GL_OES_texture_3D : enable\n");
  4694. }
  4695. if (usesTextureLod)
  4696. {
  4697. BX_WARN(s_extension[Extension::EXT_shader_texture_lod].m_supported, "EXT_shader_texture_lod is used but not supported by GLES2 driver.");
  4698. if (s_extension[Extension::EXT_shader_texture_lod].m_supported
  4699. /*&& GL_VERTEX_SHADER == m_type*/)
  4700. {
  4701. writeString(&writer
  4702. , "#extension GL_EXT_shader_texture_lod : enable\n"
  4703. "#define texture2DLod texture2DLodEXT\n"
  4704. "#define texture2DProjLod texture2DProjLodEXT\n"
  4705. "#define textureCubeLod textureCubeLodEXT\n"
  4706. );
  4707. }
  4708. else
  4709. {
  4710. writeString(&writer
  4711. , "#define texture2DLod(_sampler, _coord, _level) texture2D(_sampler, _coord)\n"
  4712. "#define texture2DProjLod(_sampler, _coord, _level) texture2DProj(_sampler, _coord)\n"
  4713. "#define textureCubeLod(_sampler, _coord, _level) textureCube(_sampler, _coord)\n"
  4714. );
  4715. }
  4716. }
  4717. if (usesFragmentOrdering)
  4718. {
  4719. if (s_extension[Extension::INTEL_fragment_shader_ordering].m_supported)
  4720. {
  4721. writeString(&writer, "#extension GL_INTEL_fragment_shader_ordering : enable\n");
  4722. }
  4723. else
  4724. {
  4725. writeString(&writer, "#define beginFragmentShaderOrdering()\n");
  4726. }
  4727. }
  4728. writeStringf(&writer, "precision %s float;\n"
  4729. , m_type == GL_FRAGMENT_SHADER ? "mediump" : "highp"
  4730. );
  4731. bx::write(&writer, code, codeLen);
  4732. bx::write(&writer, '\0');
  4733. if (insertFragDepth)
  4734. {
  4735. char* entry = strstr(temp, "void main ()");
  4736. if (NULL != entry)
  4737. {
  4738. char* brace = strstr(entry, "{");
  4739. if (NULL != brace)
  4740. {
  4741. const char* end = bx::strmb(brace, '{', '}');
  4742. if (NULL != end)
  4743. {
  4744. strins(brace+1, "\n float bgfx_FragDepth = 0.0;\n");
  4745. }
  4746. }
  4747. }
  4748. }
  4749. // Replace all instances of gl_FragDepth with bgfx_FragDepth.
  4750. for (const char* fragDepth = bx::findIdentifierMatch(temp, "gl_FragDepth"); NULL != fragDepth; fragDepth = bx::findIdentifierMatch(fragDepth, "gl_FragDepth") )
  4751. {
  4752. char* insert = const_cast<char*>(fragDepth);
  4753. strins(insert, "bg");
  4754. memcpy(insert + 2, "fx", 2);
  4755. }
  4756. }
  4757. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  4758. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL <= 21) )
  4759. {
  4760. const bool usesTextureLod = true
  4761. && s_extension[Extension::ARB_shader_texture_lod].m_supported
  4762. && bx::findIdentifierMatch(code, s_ARB_shader_texture_lod)
  4763. ;
  4764. const bool usesGpuShader5 = !!bx::findIdentifierMatch(code, s_ARB_gpu_shader5);
  4765. const bool usesIUsamplers = !!bx::findIdentifierMatch(code, s_uisamplers);
  4766. const bool usesTexelFetch = !!bx::findIdentifierMatch(code, s_texelFetch);
  4767. const bool usesTextureArray = !!bx::findIdentifierMatch(code, s_textureArray);
  4768. const bool usesTextureMS = !!bx::findIdentifierMatch(code, s_ARB_texture_multisample);
  4769. const bool usesPacking = !!bx::findIdentifierMatch(code, s_ARB_shading_language_packing);
  4770. uint32_t version =
  4771. usesIUsamplers || usesTexelFetch ? 130
  4772. : usesTextureLod ? 120
  4773. : 120
  4774. ;
  4775. if (0 != version)
  4776. {
  4777. writeStringf(&writer, "#version %d\n", version);
  4778. }
  4779. if (usesTextureLod)
  4780. {
  4781. if (m_type == GL_FRAGMENT_SHADER)
  4782. {
  4783. writeString(&writer, "#extension GL_ARB_shader_texture_lod : enable\n");
  4784. }
  4785. }
  4786. if (usesGpuShader5)
  4787. {
  4788. writeString(&writer, "#extension GL_ARB_gpu_shader5 : enable\n");
  4789. }
  4790. if (usesPacking)
  4791. {
  4792. writeString(&writer, "#extension GL_ARB_shading_language_packing : enable\n");
  4793. }
  4794. if (usesTextureMS)
  4795. {
  4796. writeString(&writer, "#extension GL_ARB_texture_multisample : enable\n");
  4797. }
  4798. if (usesTextureArray)
  4799. {
  4800. writeString(&writer, "#extension GL_EXT_texture_array : enable\n");
  4801. }
  4802. if (130 <= version)
  4803. {
  4804. if (m_type == GL_FRAGMENT_SHADER)
  4805. {
  4806. writeString(&writer, "#define varying in\n");
  4807. }
  4808. else
  4809. {
  4810. writeString(&writer, "#define attribute in\n");
  4811. writeString(&writer, "#define varying out\n");
  4812. }
  4813. uint32_t fragData = 0;
  4814. if (!!bx::findIdentifierMatch(code, "gl_FragData") )
  4815. {
  4816. for (uint32_t ii = 0, num = g_caps.limits.maxFBAttachments; ii < num; ++ii)
  4817. {
  4818. char tmpFragData[16];
  4819. bx::snprintf(tmpFragData, BX_COUNTOF(tmpFragData), "gl_FragData[%d]", ii);
  4820. fragData = bx::uint32_max(fragData, NULL == strstr(code, tmpFragData) ? 0 : ii+1);
  4821. }
  4822. BGFX_FATAL(0 != fragData, Fatal::InvalidShader, "Unable to find and patch gl_FragData!");
  4823. }
  4824. if (0 != fragData)
  4825. {
  4826. writeStringf(&writer, "out vec4 bgfx_FragData[%d];\n", fragData);
  4827. writeString(&writer, "#define gl_FragData bgfx_FragData\n");
  4828. }
  4829. else
  4830. {
  4831. writeString(&writer, "out vec4 bgfx_FragColor;\n");
  4832. writeString(&writer, "#define gl_FragColor bgfx_FragColor\n");
  4833. }
  4834. }
  4835. else
  4836. {
  4837. if (m_type == GL_FRAGMENT_SHADER)
  4838. {
  4839. writeString(&writer, "#define in varying\n");
  4840. }
  4841. else
  4842. {
  4843. writeString(&writer, "#define in attribute\n");
  4844. writeString(&writer, "#define out varying\n");
  4845. }
  4846. }
  4847. writeString(&writer,
  4848. "#define lowp\n"
  4849. "#define mediump\n"
  4850. "#define highp\n"
  4851. "#define centroid\n"
  4852. "#define flat\n"
  4853. "#define noperspective\n"
  4854. "#define smooth\n"
  4855. );
  4856. bx::write(&writer, code, codeLen);
  4857. bx::write(&writer, '\0');
  4858. }
  4859. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 31)
  4860. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  4861. {
  4862. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  4863. {
  4864. writeString(&writer
  4865. , "#version 300 es\n"
  4866. "precision mediump float;\n"
  4867. );
  4868. }
  4869. else
  4870. {
  4871. writeString(&writer, "#version 140\n");
  4872. }
  4873. writeString(&writer, "#define texture2DLod textureLod\n");
  4874. writeString(&writer, "#define texture3DLod textureLod\n");
  4875. writeString(&writer, "#define textureCubeLod textureLod\n");
  4876. if (m_type == GL_FRAGMENT_SHADER)
  4877. {
  4878. writeString(&writer, "#define varying in\n");
  4879. writeString(&writer, "#define texture2D texture\n");
  4880. writeString(&writer, "#define texture2DProj textureProj\n");
  4881. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  4882. {
  4883. writeString(&writer, "#define shadow2D(_sampler, _coord) vec2(textureProj(_sampler, vec4(_coord, 1.0) ) )\n");
  4884. writeString(&writer, "#define shadow2DProj(_sampler, _coord) vec2(textureProj(_sampler, _coord) ) )\n");
  4885. }
  4886. else
  4887. {
  4888. writeString(&writer, "#define shadow2D(_sampler, _coord) (textureProj(_sampler, vec4(_coord, 1.0) ) )\n");
  4889. writeString(&writer, "#define shadow2DProj(_sampler, _coord) (textureProj(_sampler, _coord) ) )\n");
  4890. }
  4891. writeString(&writer, "#define texture3D texture\n");
  4892. writeString(&writer, "#define textureCube texture\n");
  4893. uint32_t fragData = 0;
  4894. if (!!bx::findIdentifierMatch(code, "gl_FragData") )
  4895. {
  4896. for (uint32_t ii = 0, num = g_caps.limits.maxFBAttachments; ii < num; ++ii)
  4897. {
  4898. char tmpFragData[16];
  4899. bx::snprintf(tmpFragData, BX_COUNTOF(tmpFragData), "gl_FragData[%d]", ii);
  4900. fragData = bx::uint32_max(fragData, NULL == strstr(code, tmpFragData) ? 0 : ii+1);
  4901. }
  4902. BGFX_FATAL(0 != fragData, Fatal::InvalidShader, "Unable to find and patch gl_FragData!");
  4903. }
  4904. if (!!bx::findIdentifierMatch(code, "beginFragmentShaderOrdering") )
  4905. {
  4906. if (s_extension[Extension::INTEL_fragment_shader_ordering].m_supported)
  4907. {
  4908. writeString(&writer, "#extension GL_INTEL_fragment_shader_ordering : enable\n");
  4909. }
  4910. else
  4911. {
  4912. writeString(&writer, "#define beginFragmentShaderOrdering()\n");
  4913. }
  4914. }
  4915. if (!!bx::findIdentifierMatch(code, s_ARB_texture_multisample) )
  4916. {
  4917. writeString(&writer, "#extension GL_ARB_texture_multisample : enable\n");
  4918. }
  4919. if (0 != fragData)
  4920. {
  4921. writeStringf(&writer, "out vec4 bgfx_FragData[%d];\n", fragData);
  4922. writeString(&writer, "#define gl_FragData bgfx_FragData\n");
  4923. }
  4924. else
  4925. {
  4926. writeString(&writer, "out vec4 bgfx_FragColor;\n");
  4927. writeString(&writer, "#define gl_FragColor bgfx_FragColor\n");
  4928. }
  4929. }
  4930. else
  4931. {
  4932. writeString(&writer, "#define attribute in\n");
  4933. writeString(&writer, "#define varying out\n");
  4934. }
  4935. if (!BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) )
  4936. {
  4937. writeString(&writer
  4938. , "#define lowp\n"
  4939. "#define mediump\n"
  4940. "#define highp\n"
  4941. );
  4942. }
  4943. bx::write(&writer, code, codeLen);
  4944. bx::write(&writer, '\0');
  4945. }
  4946. code = temp;
  4947. }
  4948. GL_CHECK(glShaderSource(m_id, 1, (const GLchar**)&code, NULL) );
  4949. GL_CHECK(glCompileShader(m_id) );
  4950. GLint compiled = 0;
  4951. GL_CHECK(glGetShaderiv(m_id, GL_COMPILE_STATUS, &compiled) );
  4952. if (0 == compiled)
  4953. {
  4954. BX_TRACE("\n####\n%s\n####", code);
  4955. GLsizei len;
  4956. char log[1024];
  4957. GL_CHECK(glGetShaderInfoLog(m_id, sizeof(log), &len, log) );
  4958. BX_TRACE("Failed to compile shader. %d: %s", compiled, log);
  4959. GL_CHECK(glDeleteShader(m_id) );
  4960. m_id = 0;
  4961. BGFX_FATAL(false, bgfx::Fatal::InvalidShader, "Failed to compile shader.");
  4962. }
  4963. else if (BX_ENABLED(BGFX_CONFIG_DEBUG)
  4964. && s_extension[Extension::ANGLE_translated_shader_source].m_supported
  4965. && NULL != glGetTranslatedShaderSourceANGLE)
  4966. {
  4967. GLsizei len;
  4968. GL_CHECK(glGetShaderiv(m_id, GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE, &len) );
  4969. char* source = (char*)alloca(len);
  4970. GL_CHECK(glGetTranslatedShaderSourceANGLE(m_id, len, &len, source) );
  4971. BX_TRACE("ANGLE source (len: %d):\n%s\n####", len, source);
  4972. }
  4973. }
  4974. }
  4975. void ShaderGL::destroy()
  4976. {
  4977. if (0 != m_id)
  4978. {
  4979. GL_CHECK(glDeleteShader(m_id) );
  4980. m_id = 0;
  4981. }
  4982. }
  4983. static void frameBufferValidate()
  4984. {
  4985. GLenum complete = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4986. BX_CHECK(GL_FRAMEBUFFER_COMPLETE == complete
  4987. , "glCheckFramebufferStatus failed 0x%08x: %s"
  4988. , complete
  4989. , glEnumName(complete)
  4990. );
  4991. BX_UNUSED(complete);
  4992. }
  4993. void FrameBufferGL::create(uint8_t _num, const Attachment* _attachment)
  4994. {
  4995. GL_CHECK(glGenFramebuffers(1, &m_fbo[0]) );
  4996. m_numTh = _num;
  4997. memcpy(m_attachment, _attachment, _num*sizeof(Attachment) );
  4998. postReset();
  4999. }
  5000. void FrameBufferGL::postReset()
  5001. {
  5002. if (0 != m_fbo[0])
  5003. {
  5004. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo[0]) );
  5005. bool needResolve = false;
  5006. GLenum buffers[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
  5007. uint32_t colorIdx = 0;
  5008. for (uint32_t ii = 0; ii < m_numTh; ++ii)
  5009. {
  5010. TextureHandle handle = m_attachment[ii].handle;
  5011. if (isValid(handle) )
  5012. {
  5013. const TextureGL& texture = s_renderGL->m_textures[handle.idx];
  5014. if (0 == colorIdx)
  5015. {
  5016. m_width = bx::uint32_max(texture.m_width >> m_attachment[ii].mip, 1);
  5017. m_height = bx::uint32_max(texture.m_height >> m_attachment[ii].mip, 1);
  5018. }
  5019. GLenum attachment = GL_COLOR_ATTACHMENT0 + colorIdx;
  5020. TextureFormat::Enum format = (TextureFormat::Enum)texture.m_textureFormat;
  5021. if (isDepth(format) )
  5022. {
  5023. const ImageBlockInfo& info = getBlockInfo(format);
  5024. if (0 < info.stencilBits)
  5025. {
  5026. attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  5027. }
  5028. else if (0 == info.depthBits)
  5029. {
  5030. attachment = GL_STENCIL_ATTACHMENT;
  5031. }
  5032. else
  5033. {
  5034. attachment = GL_DEPTH_ATTACHMENT;
  5035. }
  5036. }
  5037. else
  5038. {
  5039. buffers[colorIdx] = attachment;
  5040. ++colorIdx;
  5041. }
  5042. if (0 != texture.m_rbo)
  5043. {
  5044. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER
  5045. , attachment
  5046. , GL_RENDERBUFFER
  5047. , texture.m_rbo
  5048. ) );
  5049. }
  5050. else
  5051. {
  5052. GLenum target = texture.isCubeMap()
  5053. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + m_attachment[ii].layer
  5054. : texture.m_target
  5055. ;
  5056. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  5057. , attachment
  5058. , target
  5059. , texture.m_id
  5060. , m_attachment[ii].mip
  5061. ) );
  5062. }
  5063. needResolve |= (0 != texture.m_rbo) && (0 != texture.m_id);
  5064. }
  5065. }
  5066. m_num = uint8_t(colorIdx);
  5067. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL || BGFX_CONFIG_RENDERER_OPENGLES >= 31) )
  5068. {
  5069. if (0 == colorIdx)
  5070. {
  5071. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  5072. {
  5073. // When only depth is attached disable draw buffer to avoid
  5074. // GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER.
  5075. GL_CHECK(glDrawBuffer(GL_NONE) );
  5076. }
  5077. }
  5078. else
  5079. {
  5080. GL_CHECK(glDrawBuffers(colorIdx, buffers) );
  5081. }
  5082. // Disable read buffer to avoid GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER.
  5083. GL_CHECK(glReadBuffer(GL_NONE) );
  5084. }
  5085. frameBufferValidate();
  5086. if (needResolve)
  5087. {
  5088. GL_CHECK(glGenFramebuffers(1, &m_fbo[1]) );
  5089. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo[1]) );
  5090. colorIdx = 0;
  5091. for (uint32_t ii = 0; ii < m_numTh; ++ii)
  5092. {
  5093. TextureHandle handle = m_attachment[ii].handle;
  5094. if (isValid(handle) )
  5095. {
  5096. const TextureGL& texture = s_renderGL->m_textures[handle.idx];
  5097. if (0 != texture.m_id)
  5098. {
  5099. GLenum attachment = GL_COLOR_ATTACHMENT0 + colorIdx;
  5100. if (!isDepth( (TextureFormat::Enum)texture.m_textureFormat) )
  5101. {
  5102. ++colorIdx;
  5103. GLenum target = texture.isCubeMap()
  5104. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + m_attachment[ii].layer
  5105. : texture.m_target
  5106. ;
  5107. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  5108. , attachment
  5109. , target
  5110. , texture.m_id
  5111. , m_attachment[ii].mip
  5112. ) );
  5113. }
  5114. }
  5115. }
  5116. }
  5117. frameBufferValidate();
  5118. }
  5119. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderGL->m_msaaBackBufferFbo) );
  5120. }
  5121. }
  5122. void FrameBufferGL::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat)
  5123. {
  5124. BX_UNUSED(_depthFormat);
  5125. m_swapChain = s_renderGL->m_glctx.createSwapChain(_nwh);
  5126. m_width = _width;
  5127. m_height = _height;
  5128. m_denseIdx = _denseIdx;
  5129. }
  5130. uint16_t FrameBufferGL::destroy()
  5131. {
  5132. if (0 != m_num)
  5133. {
  5134. GL_CHECK(glDeleteFramebuffers(0 == m_fbo[1] ? 1 : 2, m_fbo) );
  5135. m_num = 0;
  5136. }
  5137. if (NULL != m_swapChain)
  5138. {
  5139. s_renderGL->m_glctx.destroySwapChain(m_swapChain);
  5140. m_swapChain = NULL;
  5141. }
  5142. memset(m_fbo, 0, sizeof(m_fbo) );
  5143. uint16_t denseIdx = m_denseIdx;
  5144. m_denseIdx = UINT16_MAX;
  5145. return denseIdx;
  5146. }
  5147. void FrameBufferGL::resolve()
  5148. {
  5149. if (0 != m_fbo[1])
  5150. {
  5151. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[0]) );
  5152. GL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT0) );
  5153. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[1]) );
  5154. GL_CHECK(glBlitFramebuffer(0
  5155. , 0
  5156. , m_width
  5157. , m_height
  5158. , 0
  5159. , 0
  5160. , m_width
  5161. , m_height
  5162. , GL_COLOR_BUFFER_BIT
  5163. , GL_LINEAR
  5164. ) );
  5165. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[0]) );
  5166. GL_CHECK(glReadBuffer(GL_NONE) );
  5167. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderGL->m_msaaBackBufferFbo) );
  5168. }
  5169. if (0 < m_numTh)
  5170. {
  5171. for (uint32_t ii = 0; ii < m_numTh; ++ii)
  5172. {
  5173. TextureHandle handle = m_attachment[ii].handle;
  5174. if (isValid(handle) )
  5175. {
  5176. const TextureGL& texture = s_renderGL->m_textures[handle.idx];
  5177. texture.resolve();
  5178. }
  5179. }
  5180. }
  5181. }
  5182. void FrameBufferGL::discard(uint16_t _flags)
  5183. {
  5184. GLenum buffers[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS+2];
  5185. uint32_t idx = 0;
  5186. if (BGFX_CLEAR_NONE != (_flags & BGFX_CLEAR_DISCARD_COLOR_MASK) )
  5187. {
  5188. for (uint32_t ii = 0, num = m_num; ii < num; ++ii)
  5189. {
  5190. if (BGFX_CLEAR_NONE != (_flags & (BGFX_CLEAR_DISCARD_COLOR_0<<ii) ) )
  5191. {
  5192. buffers[idx++] = GL_COLOR_ATTACHMENT0 + ii;
  5193. }
  5194. }
  5195. }
  5196. uint32_t dsFlags = _flags & (BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL);
  5197. if (BGFX_CLEAR_NONE != dsFlags)
  5198. {
  5199. if ( (BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL) == dsFlags)
  5200. {
  5201. buffers[idx++] = GL_DEPTH_STENCIL_ATTACHMENT;
  5202. }
  5203. else if (BGFX_CLEAR_DISCARD_DEPTH == dsFlags)
  5204. {
  5205. buffers[idx++] = GL_DEPTH_ATTACHMENT;
  5206. }
  5207. else if (BGFX_CLEAR_DISCARD_STENCIL == dsFlags)
  5208. {
  5209. buffers[idx++] = GL_STENCIL_ATTACHMENT;
  5210. }
  5211. }
  5212. GL_CHECK(glInvalidateFramebuffer(GL_FRAMEBUFFER, idx, buffers) );
  5213. }
  5214. void OcclusionQueryGL::create()
  5215. {
  5216. for (uint32_t ii = 0; ii < BX_COUNTOF(m_query); ++ii)
  5217. {
  5218. Query& query = m_query[ii];
  5219. GL_CHECK(glGenQueries(1, &query.m_id) );
  5220. }
  5221. }
  5222. void OcclusionQueryGL::destroy()
  5223. {
  5224. for (uint32_t ii = 0; ii < BX_COUNTOF(m_query); ++ii)
  5225. {
  5226. Query& query = m_query[ii];
  5227. GL_CHECK(glDeleteQueries(1, &query.m_id) );
  5228. }
  5229. }
  5230. void OcclusionQueryGL::begin(Frame* _render, OcclusionQueryHandle _handle)
  5231. {
  5232. while (0 == m_control.reserve(1) )
  5233. {
  5234. resolve(_render, true);
  5235. }
  5236. Query& query = m_query[m_control.m_current];
  5237. GL_CHECK(glBeginQuery(GL_SAMPLES_PASSED, query.m_id) );
  5238. query.m_handle = _handle;
  5239. }
  5240. void OcclusionQueryGL::end()
  5241. {
  5242. GL_CHECK(glEndQuery(GL_SAMPLES_PASSED) );
  5243. m_control.commit(1);
  5244. }
  5245. void OcclusionQueryGL::resolve(Frame* _render, bool _wait)
  5246. {
  5247. while (0 != m_control.available() )
  5248. {
  5249. Query& query = m_query[m_control.m_read];
  5250. int32_t result;
  5251. if (!_wait)
  5252. {
  5253. GL_CHECK(glGetQueryObjectiv(query.m_id, GL_QUERY_RESULT_AVAILABLE, &result) );
  5254. if (!result)
  5255. {
  5256. break;
  5257. }
  5258. }
  5259. GL_CHECK(glGetQueryObjectiv(query.m_id, GL_QUERY_RESULT, &result) );
  5260. _render->m_occlusion[query.m_handle.idx] = 0 < result;
  5261. m_control.consume(1);
  5262. }
  5263. }
  5264. void RendererContextGL::submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter)
  5265. {
  5266. BGFX_GPU_PROFILER_BEGIN_DYNAMIC("rendererSubmit");
  5267. if (_render->m_capture)
  5268. {
  5269. renderDocTriggerCapture();
  5270. }
  5271. if (1 < m_numWindows
  5272. && m_vaoSupport)
  5273. {
  5274. m_vaoSupport = false;
  5275. GL_CHECK(glBindVertexArray(0) );
  5276. GL_CHECK(glDeleteVertexArrays(1, &m_vao) );
  5277. m_vao = 0;
  5278. m_vaoStateCache.invalidate();
  5279. }
  5280. m_glctx.makeCurrent(NULL);
  5281. const GLuint defaultVao = m_vao;
  5282. if (0 != defaultVao)
  5283. {
  5284. GL_CHECK(glBindVertexArray(defaultVao) );
  5285. }
  5286. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  5287. updateResolution(_render->m_resolution);
  5288. int64_t elapsed = -bx::getHPCounter();
  5289. int64_t captureElapsed = 0;
  5290. if (m_timerQuerySupport)
  5291. {
  5292. m_gpuTimer.begin();
  5293. }
  5294. if (0 < _render->m_iboffset)
  5295. {
  5296. TransientIndexBuffer* ib = _render->m_transientIb;
  5297. m_indexBuffers[ib->handle.idx].update(0, _render->m_iboffset, ib->data, true);
  5298. }
  5299. if (0 < _render->m_vboffset)
  5300. {
  5301. TransientVertexBuffer* vb = _render->m_transientVb;
  5302. m_vertexBuffers[vb->handle.idx].update(0, _render->m_vboffset, vb->data, true);
  5303. }
  5304. _render->sort();
  5305. RenderDraw currentState;
  5306. currentState.clear();
  5307. currentState.m_stateFlags = BGFX_STATE_NONE;
  5308. currentState.m_stencil = packStencil(BGFX_STENCIL_NONE, BGFX_STENCIL_NONE);
  5309. _render->m_hmdInitialized = m_ovr.isInitialized();
  5310. const bool hmdEnabled = m_ovr.isEnabled();
  5311. static ViewState viewState;
  5312. viewState.reset(_render, hmdEnabled);
  5313. uint16_t programIdx = invalidHandle;
  5314. SortKey key;
  5315. uint16_t view = UINT16_MAX;
  5316. FrameBufferHandle fbh = { BGFX_CONFIG_MAX_FRAME_BUFFERS };
  5317. BlitKey blitKey;
  5318. blitKey.decode(_render->m_blitKeys[0]);
  5319. uint16_t numBlitItems = _render->m_numBlitItems;
  5320. uint16_t blitItem = 0;
  5321. int32_t resolutionHeight = hmdEnabled
  5322. ? _render->m_hmd.height
  5323. : _render->m_resolution.m_height
  5324. ;
  5325. uint32_t blendFactor = 0;
  5326. uint8_t primIndex;
  5327. {
  5328. const uint64_t pt = _render->m_debug&BGFX_DEBUG_WIREFRAME ? BGFX_STATE_PT_LINES : 0;
  5329. primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  5330. }
  5331. PrimInfo prim = s_primInfo[primIndex];
  5332. uint32_t baseVertex = 0;
  5333. GLuint currentVao = 0;
  5334. bool wasCompute = false;
  5335. bool viewHasScissor = false;
  5336. Rect viewScissorRect;
  5337. viewScissorRect.clear();
  5338. uint16_t discardFlags = BGFX_CLEAR_NONE;
  5339. const bool blendIndependentSupported = s_extension[Extension::ARB_draw_buffers_blend].m_supported;
  5340. const bool computeSupported = (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) && s_extension[Extension::ARB_compute_shader].m_supported)
  5341. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  5342. ;
  5343. uint32_t statsNumPrimsSubmitted[BX_COUNTOF(s_primInfo)] = {};
  5344. uint32_t statsNumPrimsRendered[BX_COUNTOF(s_primInfo)] = {};
  5345. uint32_t statsNumInstances[BX_COUNTOF(s_primInfo)] = {};
  5346. uint32_t statsNumIndices = 0;
  5347. uint32_t statsKeyType[2] = {};
  5348. if (m_occlusionQuerySupport)
  5349. {
  5350. m_occlusionQuery.resolve(_render);
  5351. }
  5352. uint8_t eye = 0;
  5353. if (0 == (_render->m_debug&BGFX_DEBUG_IFH) )
  5354. {
  5355. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  5356. bool viewRestart = false;
  5357. uint8_t restartState = 0;
  5358. viewState.m_rect = _render->m_rect[0];
  5359. int32_t numItems = _render->m_num;
  5360. for (int32_t item = 0, restartItem = numItems; item < numItems || restartItem < numItems;)
  5361. {
  5362. const bool isCompute = key.decode(_render->m_sortKeys[item], _render->m_viewRemap);
  5363. statsKeyType[isCompute]++;
  5364. const bool viewChanged = 0
  5365. || key.m_view != view
  5366. || item == numItems
  5367. ;
  5368. const RenderItem& renderItem = _render->m_renderItem[_render->m_sortValues[item] ];
  5369. ++item;
  5370. if (viewChanged)
  5371. {
  5372. if (1 == restartState)
  5373. {
  5374. restartState = 2;
  5375. item = restartItem;
  5376. restartItem = numItems;
  5377. view = UINT16_MAX;
  5378. continue;
  5379. }
  5380. view = key.m_view;
  5381. programIdx = invalidHandle;
  5382. if (_render->m_fb[view].idx != fbh.idx)
  5383. {
  5384. fbh = _render->m_fb[view];
  5385. resolutionHeight = hmdEnabled
  5386. ? _render->m_hmd.height
  5387. : _render->m_resolution.m_height
  5388. ;
  5389. resolutionHeight = setFrameBuffer(fbh, resolutionHeight, discardFlags);
  5390. }
  5391. viewRestart = ( (BGFX_VIEW_STEREO == (_render->m_viewFlags[view] & BGFX_VIEW_STEREO) ) );
  5392. viewRestart &= hmdEnabled;
  5393. if (viewRestart)
  5394. {
  5395. if (0 == restartState)
  5396. {
  5397. restartState = 1;
  5398. restartItem = item - 1;
  5399. }
  5400. eye = (restartState - 1) & 1;
  5401. restartState &= 1;
  5402. }
  5403. else
  5404. {
  5405. eye = 0;
  5406. }
  5407. if (item > 1)
  5408. {
  5409. BGFX_GPU_PROFILER_END();
  5410. BGFX_PROFILER_END();
  5411. }
  5412. BGFX_PROFILER_BEGIN_DYNAMIC(s_viewName[view]);
  5413. BGFX_GPU_PROFILER_BEGIN_DYNAMIC(s_viewName[view]);
  5414. viewState.m_rect = _render->m_rect[view];
  5415. if (viewRestart)
  5416. {
  5417. if (BX_ENABLED(BGFX_CONFIG_DEBUG_PIX) )
  5418. {
  5419. char* viewName = s_viewName[view];
  5420. viewName[3] = ' ';
  5421. viewName[4] = eye ? 'R' : 'L';
  5422. GL_CHECK(glInsertEventMarker(0, viewName) );
  5423. }
  5424. if (m_ovr.isEnabled() )
  5425. {
  5426. m_ovr.renderEyeStart(eye, &viewState.m_rect);
  5427. }
  5428. else
  5429. {
  5430. viewState.m_rect.m_x = eye * (viewState.m_rect.m_width+1)/2;
  5431. viewState.m_rect.m_width /= 2;
  5432. }
  5433. }
  5434. else
  5435. {
  5436. if (BX_ENABLED(BGFX_CONFIG_DEBUG_PIX) )
  5437. {
  5438. char* viewName = s_viewName[view];
  5439. viewName[3] = ' ';
  5440. viewName[4] = ' ';
  5441. GL_CHECK(glInsertEventMarker(0, viewName) );
  5442. }
  5443. }
  5444. const Rect& scissorRect = _render->m_scissor[view];
  5445. viewHasScissor = !scissorRect.isZero();
  5446. viewScissorRect = viewHasScissor ? scissorRect : viewState.m_rect;
  5447. GL_CHECK(glViewport(viewState.m_rect.m_x
  5448. , resolutionHeight-viewState.m_rect.m_height-viewState.m_rect.m_y
  5449. , viewState.m_rect.m_width
  5450. , viewState.m_rect.m_height
  5451. ) );
  5452. Clear& clear = _render->m_clear[view];
  5453. discardFlags = clear.m_flags & BGFX_CLEAR_DISCARD_MASK;
  5454. if (BGFX_CLEAR_NONE != (clear.m_flags & BGFX_CLEAR_MASK) )
  5455. {
  5456. clearQuad(_clearQuad, viewState.m_rect, clear, resolutionHeight, _render->m_colorPalette);
  5457. currentVao = UINT32_MAX; // clearQuad will mess with VAO, invalidate it.
  5458. }
  5459. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  5460. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  5461. GL_CHECK(glDepthFunc(GL_LESS) );
  5462. GL_CHECK(glEnable(GL_CULL_FACE) );
  5463. GL_CHECK(glDisable(GL_BLEND) );
  5464. if (m_blitSupported)
  5465. {
  5466. for (; blitItem < numBlitItems && blitKey.m_view <= view; blitItem++)
  5467. {
  5468. const BlitItem& bi = _render->m_blitItem[blitItem];
  5469. blitKey.decode(_render->m_blitKeys[blitItem + 1]);
  5470. const TextureGL& src = m_textures[bi.m_src.idx];
  5471. const TextureGL& dst = m_textures[bi.m_dst.idx];
  5472. uint32_t srcWidth = bx::uint32_min(src.m_width, bi.m_srcX + bi.m_width) - bi.m_srcX;
  5473. uint32_t srcHeight = bx::uint32_min(src.m_height, bi.m_srcY + bi.m_height) - bi.m_srcY;
  5474. uint32_t srcDepth = bx::uint32_min(src.m_depth, bi.m_srcZ + bi.m_depth) - bi.m_srcZ;
  5475. uint32_t dstWidth = bx::uint32_min(dst.m_width, bi.m_dstX + bi.m_width) - bi.m_dstX;
  5476. uint32_t dstHeight = bx::uint32_min(dst.m_height, bi.m_dstY + bi.m_height) - bi.m_dstY;
  5477. uint32_t dstDepth = bx::uint32_min(dst.m_depth, bi.m_dstZ + bi.m_depth) - bi.m_dstZ;
  5478. uint32_t width = bx::uint32_min(srcWidth, dstWidth);
  5479. uint32_t height = bx::uint32_min(srcHeight, dstHeight);
  5480. uint32_t depth = bx::uint32_min(srcDepth, dstDepth);
  5481. GL_CHECK(glCopyImageSubData(src.m_id
  5482. , src.m_target
  5483. , bi.m_srcMip
  5484. , bi.m_srcX
  5485. , bi.m_srcY
  5486. , bi.m_srcZ
  5487. , dst.m_id
  5488. , dst.m_target
  5489. , bi.m_dstMip
  5490. , bi.m_dstX
  5491. , bi.m_dstY
  5492. , bi.m_dstZ
  5493. , width
  5494. , height
  5495. , bx::uint32_imax(depth, 1)
  5496. ) );
  5497. }
  5498. }
  5499. }
  5500. if (isCompute)
  5501. {
  5502. if (!wasCompute)
  5503. {
  5504. wasCompute = true;
  5505. if (BX_ENABLED(BGFX_CONFIG_DEBUG_PIX) )
  5506. {
  5507. char* viewName = s_viewName[view];
  5508. viewName[3] = 'C';
  5509. GL_CHECK(glInsertEventMarker(0, viewName) );
  5510. }
  5511. }
  5512. if (computeSupported)
  5513. {
  5514. const RenderCompute& compute = renderItem.compute;
  5515. ProgramGL& program = m_program[key.m_program];
  5516. GL_CHECK(glUseProgram(program.m_id) );
  5517. GLbitfield barrier = 0;
  5518. for (uint32_t ii = 0; ii < BGFX_MAX_COMPUTE_BINDINGS; ++ii)
  5519. {
  5520. const Binding& bind = compute.m_bind[ii];
  5521. if (invalidHandle != bind.m_idx)
  5522. {
  5523. switch (bind.m_type)
  5524. {
  5525. case Binding::Image:
  5526. {
  5527. const TextureGL& texture = m_textures[bind.m_idx];
  5528. GL_CHECK(glBindImageTexture(ii
  5529. , texture.m_id
  5530. , bind.m_un.m_compute.m_mip
  5531. , GL_FALSE
  5532. , 0
  5533. , s_access[bind.m_un.m_compute.m_access]
  5534. , s_imageFormat[bind.m_un.m_compute.m_format])
  5535. );
  5536. barrier |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT;
  5537. }
  5538. break;
  5539. case Binding::IndexBuffer:
  5540. {
  5541. const IndexBufferGL& buffer = m_indexBuffers[bind.m_idx];
  5542. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id) );
  5543. barrier |= GL_SHADER_STORAGE_BARRIER_BIT;
  5544. }
  5545. break;
  5546. case Binding::VertexBuffer:
  5547. {
  5548. const VertexBufferGL& buffer = m_vertexBuffers[bind.m_idx];
  5549. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id) );
  5550. barrier |= GL_SHADER_STORAGE_BARRIER_BIT;
  5551. }
  5552. break;
  5553. }
  5554. }
  5555. }
  5556. if (0 != barrier)
  5557. {
  5558. bool constantsChanged = compute.m_constBegin < compute.m_constEnd;
  5559. rendererUpdateUniforms(this, _render->m_uniformBuffer, compute.m_constBegin, compute.m_constEnd);
  5560. if (constantsChanged
  5561. && NULL != program.m_constantBuffer)
  5562. {
  5563. commit(*program.m_constantBuffer);
  5564. }
  5565. viewState.setPredefined<1>(this, view, eye, program, _render, compute);
  5566. if (isValid(compute.m_indirectBuffer) )
  5567. {
  5568. const VertexBufferGL& vb = m_vertexBuffers[compute.m_indirectBuffer.idx];
  5569. if (currentState.m_indirectBuffer.idx != compute.m_indirectBuffer.idx)
  5570. {
  5571. currentState.m_indirectBuffer = compute.m_indirectBuffer;
  5572. GL_CHECK(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, vb.m_id) );
  5573. }
  5574. uint32_t numDrawIndirect = UINT16_MAX == compute.m_numIndirect
  5575. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  5576. : compute.m_numIndirect
  5577. ;
  5578. uintptr_t args = compute.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  5579. for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
  5580. {
  5581. GL_CHECK(glDispatchComputeIndirect( (GLintptr)args) );
  5582. args += BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  5583. }
  5584. }
  5585. else
  5586. {
  5587. if (isValid(currentState.m_indirectBuffer) )
  5588. {
  5589. currentState.m_indirectBuffer.idx = invalidHandle;
  5590. GL_CHECK(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0) );
  5591. }
  5592. GL_CHECK(glDispatchCompute(compute.m_numX, compute.m_numY, compute.m_numZ) );
  5593. }
  5594. GL_CHECK(glMemoryBarrier(barrier) );
  5595. }
  5596. }
  5597. continue;
  5598. }
  5599. bool resetState = viewChanged || wasCompute;
  5600. if (wasCompute)
  5601. {
  5602. wasCompute = false;
  5603. if (BX_ENABLED(BGFX_CONFIG_DEBUG_PIX) )
  5604. {
  5605. char* viewName = s_viewName[view];
  5606. viewName[3] = ' ';
  5607. GL_CHECK(glInsertEventMarker(0, viewName) );
  5608. }
  5609. }
  5610. const RenderDraw& draw = renderItem.draw;
  5611. const bool hasOcclusionQuery = 0 != (draw.m_stateFlags & BGFX_STATE_INTERNAL_OCCLUSION_QUERY);
  5612. if (isValid(draw.m_occlusionQuery)
  5613. && !hasOcclusionQuery
  5614. && !isVisible(_render, draw.m_occlusionQuery, 0 != (draw.m_submitFlags&BGFX_SUBMIT_INTERNAL_OCCLUSION_VISIBLE) ) )
  5615. {
  5616. continue;
  5617. }
  5618. const uint64_t newFlags = draw.m_stateFlags;
  5619. uint64_t changedFlags = currentState.m_stateFlags ^ draw.m_stateFlags;
  5620. currentState.m_stateFlags = newFlags;
  5621. const uint64_t newStencil = draw.m_stencil;
  5622. uint64_t changedStencil = currentState.m_stencil ^ draw.m_stencil;
  5623. currentState.m_stencil = newStencil;
  5624. if (resetState)
  5625. {
  5626. currentState.clear();
  5627. currentState.m_scissor = !draw.m_scissor;
  5628. changedFlags = BGFX_STATE_MASK;
  5629. changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK);
  5630. currentState.m_stateFlags = newFlags;
  5631. currentState.m_stencil = newStencil;
  5632. }
  5633. uint16_t scissor = draw.m_scissor;
  5634. if (currentState.m_scissor != scissor)
  5635. {
  5636. currentState.m_scissor = scissor;
  5637. if (UINT16_MAX == scissor)
  5638. {
  5639. if (viewHasScissor)
  5640. {
  5641. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  5642. GL_CHECK(glScissor(viewScissorRect.m_x
  5643. , resolutionHeight-viewScissorRect.m_height-viewScissorRect.m_y
  5644. , viewScissorRect.m_width
  5645. , viewScissorRect.m_height
  5646. ) );
  5647. }
  5648. else
  5649. {
  5650. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  5651. }
  5652. }
  5653. else
  5654. {
  5655. Rect scissorRect;
  5656. scissorRect.intersect(viewScissorRect, _render->m_rectCache.m_cache[scissor]);
  5657. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  5658. GL_CHECK(glScissor(scissorRect.m_x
  5659. , resolutionHeight-scissorRect.m_height-scissorRect.m_y
  5660. , scissorRect.m_width
  5661. , scissorRect.m_height
  5662. ) );
  5663. }
  5664. }
  5665. if (0 != changedStencil)
  5666. {
  5667. if (0 != newStencil)
  5668. {
  5669. GL_CHECK(glEnable(GL_STENCIL_TEST) );
  5670. uint32_t bstencil = unpackStencil(1, newStencil);
  5671. uint8_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != unpackStencil(0, newStencil);
  5672. // uint32_t bchanged = unpackStencil(1, changedStencil);
  5673. // if (BGFX_STENCIL_FUNC_RMASK_MASK & bchanged)
  5674. // {
  5675. // uint32_t wmask = (bstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  5676. // GL_CHECK(glStencilMask(wmask) );
  5677. // }
  5678. for (uint8_t ii = 0, num = frontAndBack+1; ii < num; ++ii)
  5679. {
  5680. uint32_t stencil = unpackStencil(ii, newStencil);
  5681. uint32_t changed = unpackStencil(ii, changedStencil);
  5682. GLenum face = s_stencilFace[frontAndBack+ii];
  5683. if ( (BGFX_STENCIL_TEST_MASK|BGFX_STENCIL_FUNC_REF_MASK|BGFX_STENCIL_FUNC_RMASK_MASK) & changed)
  5684. {
  5685. GLint ref = (stencil&BGFX_STENCIL_FUNC_REF_MASK)>>BGFX_STENCIL_FUNC_REF_SHIFT;
  5686. GLint mask = (stencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  5687. uint32_t func = (stencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT;
  5688. GL_CHECK(glStencilFuncSeparate(face, s_cmpFunc[func], ref, mask) );
  5689. }
  5690. if ( (BGFX_STENCIL_OP_FAIL_S_MASK|BGFX_STENCIL_OP_FAIL_Z_MASK|BGFX_STENCIL_OP_PASS_Z_MASK) & changed)
  5691. {
  5692. uint32_t sfail = (stencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT;
  5693. uint32_t zfail = (stencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT;
  5694. uint32_t zpass = (stencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT;
  5695. GL_CHECK(glStencilOpSeparate(face, s_stencilOp[sfail], s_stencilOp[zfail], s_stencilOp[zpass]) );
  5696. }
  5697. }
  5698. }
  5699. else
  5700. {
  5701. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  5702. }
  5703. }
  5704. if ( (0
  5705. | BGFX_STATE_CULL_MASK
  5706. | BGFX_STATE_DEPTH_WRITE
  5707. | BGFX_STATE_DEPTH_TEST_MASK
  5708. | BGFX_STATE_RGB_WRITE
  5709. | BGFX_STATE_ALPHA_WRITE
  5710. | BGFX_STATE_BLEND_MASK
  5711. | BGFX_STATE_BLEND_EQUATION_MASK
  5712. | BGFX_STATE_ALPHA_REF_MASK
  5713. | BGFX_STATE_PT_MASK
  5714. | BGFX_STATE_POINT_SIZE_MASK
  5715. | BGFX_STATE_MSAA
  5716. | BGFX_STATE_LINEAA
  5717. | BGFX_STATE_CONSERVATIVE_RASTER
  5718. ) & changedFlags)
  5719. {
  5720. if (BGFX_STATE_CULL_MASK & changedFlags)
  5721. {
  5722. if (BGFX_STATE_CULL_CW & newFlags)
  5723. {
  5724. GL_CHECK(glEnable(GL_CULL_FACE) );
  5725. GL_CHECK(glCullFace(GL_BACK) );
  5726. }
  5727. else if (BGFX_STATE_CULL_CCW & newFlags)
  5728. {
  5729. GL_CHECK(glEnable(GL_CULL_FACE) );
  5730. GL_CHECK(glCullFace(GL_FRONT) );
  5731. }
  5732. else
  5733. {
  5734. GL_CHECK(glDisable(GL_CULL_FACE) );
  5735. }
  5736. }
  5737. if (BGFX_STATE_DEPTH_WRITE & changedFlags)
  5738. {
  5739. GL_CHECK(glDepthMask(!!(BGFX_STATE_DEPTH_WRITE & newFlags) ) );
  5740. }
  5741. if (BGFX_STATE_DEPTH_TEST_MASK & changedFlags)
  5742. {
  5743. uint32_t func = (newFlags&BGFX_STATE_DEPTH_TEST_MASK)>>BGFX_STATE_DEPTH_TEST_SHIFT;
  5744. if (0 != func)
  5745. {
  5746. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  5747. GL_CHECK(glDepthFunc(s_cmpFunc[func]) );
  5748. }
  5749. else
  5750. {
  5751. if (BGFX_STATE_DEPTH_WRITE & newFlags)
  5752. {
  5753. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  5754. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  5755. }
  5756. else
  5757. {
  5758. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  5759. }
  5760. }
  5761. }
  5762. if (BGFX_STATE_ALPHA_REF_MASK & changedFlags)
  5763. {
  5764. uint32_t ref = (newFlags&BGFX_STATE_ALPHA_REF_MASK)>>BGFX_STATE_ALPHA_REF_SHIFT;
  5765. viewState.m_alphaRef = ref/255.0f;
  5766. }
  5767. #if BGFX_CONFIG_RENDERER_OPENGL
  5768. if ( (BGFX_STATE_PT_POINTS|BGFX_STATE_POINT_SIZE_MASK) & changedFlags)
  5769. {
  5770. float pointSize = (float)(bx::uint32_max(1, (newFlags&BGFX_STATE_POINT_SIZE_MASK)>>BGFX_STATE_POINT_SIZE_SHIFT) );
  5771. GL_CHECK(glPointSize(pointSize) );
  5772. }
  5773. if (BGFX_STATE_MSAA & changedFlags)
  5774. {
  5775. GL_CHECK(BGFX_STATE_MSAA & newFlags
  5776. ? glEnable(GL_MULTISAMPLE)
  5777. : glDisable(GL_MULTISAMPLE)
  5778. );
  5779. }
  5780. if (BGFX_STATE_LINEAA & changedFlags)
  5781. {
  5782. GL_CHECK(BGFX_STATE_LINEAA & newFlags
  5783. ? glEnable(GL_LINE_SMOOTH)
  5784. : glDisable(GL_LINE_SMOOTH)
  5785. );
  5786. }
  5787. if (m_conservativeRasterSupport
  5788. && BGFX_STATE_CONSERVATIVE_RASTER & changedFlags)
  5789. {
  5790. GL_CHECK(BGFX_STATE_CONSERVATIVE_RASTER & newFlags
  5791. ? glEnable(GL_CONSERVATIVE_RASTERIZATION_NV)
  5792. : glDisable(GL_CONSERVATIVE_RASTERIZATION_NV)
  5793. );
  5794. }
  5795. #endif // BGFX_CONFIG_RENDERER_OPENGL
  5796. if ( (BGFX_STATE_ALPHA_WRITE|BGFX_STATE_RGB_WRITE) & changedFlags)
  5797. {
  5798. GLboolean alpha = !!(newFlags&BGFX_STATE_ALPHA_WRITE);
  5799. GLboolean rgb = !!(newFlags&BGFX_STATE_RGB_WRITE);
  5800. GL_CHECK(glColorMask(rgb, rgb, rgb, alpha) );
  5801. }
  5802. if ( ( (0
  5803. | BGFX_STATE_BLEND_MASK
  5804. | BGFX_STATE_BLEND_EQUATION_MASK
  5805. | BGFX_STATE_BLEND_INDEPENDENT
  5806. | BGFX_STATE_BLEND_ALPHA_TO_COVERAGE
  5807. ) & changedFlags)
  5808. || blendFactor != draw.m_rgba)
  5809. {
  5810. if (m_atocSupport)
  5811. {
  5812. if (BGFX_STATE_BLEND_ALPHA_TO_COVERAGE & newFlags)
  5813. {
  5814. GL_CHECK(glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE) );
  5815. }
  5816. else
  5817. {
  5818. GL_CHECK(glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE) );
  5819. }
  5820. }
  5821. if ( ( (0
  5822. | BGFX_STATE_BLEND_MASK
  5823. | BGFX_STATE_BLEND_EQUATION_MASK
  5824. | BGFX_STATE_BLEND_INDEPENDENT) & newFlags)
  5825. || blendFactor != draw.m_rgba)
  5826. {
  5827. const bool enabled = !!(BGFX_STATE_BLEND_MASK & newFlags);
  5828. const bool independent = !!(BGFX_STATE_BLEND_INDEPENDENT & newFlags)
  5829. && blendIndependentSupported
  5830. ;
  5831. const uint32_t blend = uint32_t( (newFlags&BGFX_STATE_BLEND_MASK)>>BGFX_STATE_BLEND_SHIFT);
  5832. const uint32_t srcRGB = (blend )&0xf;
  5833. const uint32_t dstRGB = (blend>> 4)&0xf;
  5834. const uint32_t srcA = (blend>> 8)&0xf;
  5835. const uint32_t dstA = (blend>>12)&0xf;
  5836. const uint32_t equ = uint32_t( (newFlags&BGFX_STATE_BLEND_EQUATION_MASK)>>BGFX_STATE_BLEND_EQUATION_SHIFT);
  5837. const uint32_t equRGB = (equ )&0x7;
  5838. const uint32_t equA = (equ>>3)&0x7;
  5839. const uint32_t numRt = getNumRt();
  5840. if (!BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  5841. || 1 >= numRt
  5842. || !independent)
  5843. {
  5844. if (enabled)
  5845. {
  5846. GL_CHECK(glEnable(GL_BLEND) );
  5847. GL_CHECK(glBlendFuncSeparate(s_blendFactor[srcRGB].m_src
  5848. , s_blendFactor[dstRGB].m_dst
  5849. , s_blendFactor[srcA].m_src
  5850. , s_blendFactor[dstA].m_dst
  5851. ) );
  5852. GL_CHECK(glBlendEquationSeparate(s_blendEquation[equRGB], s_blendEquation[equA]) );
  5853. if ( (s_blendFactor[srcRGB].m_factor || s_blendFactor[dstRGB].m_factor)
  5854. && blendFactor != draw.m_rgba)
  5855. {
  5856. const uint32_t rgba = draw.m_rgba;
  5857. GLclampf rr = ( (rgba>>24) )/255.0f;
  5858. GLclampf gg = ( (rgba>>16)&0xff)/255.0f;
  5859. GLclampf bb = ( (rgba>> 8)&0xff)/255.0f;
  5860. GLclampf aa = ( (rgba )&0xff)/255.0f;
  5861. GL_CHECK(glBlendColor(rr, gg, bb, aa) );
  5862. }
  5863. }
  5864. else
  5865. {
  5866. GL_CHECK(glDisable(GL_BLEND) );
  5867. }
  5868. }
  5869. else
  5870. {
  5871. if (enabled)
  5872. {
  5873. GL_CHECK(glEnablei(GL_BLEND, 0) );
  5874. GL_CHECK(glBlendFuncSeparatei(0
  5875. , s_blendFactor[srcRGB].m_src
  5876. , s_blendFactor[dstRGB].m_dst
  5877. , s_blendFactor[srcA].m_src
  5878. , s_blendFactor[dstA].m_dst
  5879. ) );
  5880. GL_CHECK(glBlendEquationSeparatei(0
  5881. , s_blendEquation[equRGB]
  5882. , s_blendEquation[equA]
  5883. ) );
  5884. }
  5885. else
  5886. {
  5887. GL_CHECK(glDisablei(GL_BLEND, 0) );
  5888. }
  5889. for (uint32_t ii = 1, rgba = draw.m_rgba; ii < numRt; ++ii, rgba >>= 11)
  5890. {
  5891. if (0 != (rgba&0x7ff) )
  5892. {
  5893. const uint32_t src = (rgba )&0xf;
  5894. const uint32_t dst = (rgba>>4)&0xf;
  5895. const uint32_t equation = (rgba>>8)&0x7;
  5896. GL_CHECK(glEnablei(GL_BLEND, ii) );
  5897. GL_CHECK(glBlendFunci(ii, s_blendFactor[src].m_src, s_blendFactor[dst].m_dst) );
  5898. GL_CHECK(glBlendEquationi(ii, s_blendEquation[equation]) );
  5899. }
  5900. else
  5901. {
  5902. GL_CHECK(glDisablei(GL_BLEND, ii) );
  5903. }
  5904. }
  5905. }
  5906. }
  5907. else
  5908. {
  5909. GL_CHECK(glDisable(GL_BLEND) );
  5910. }
  5911. blendFactor = draw.m_rgba;
  5912. }
  5913. const uint64_t pt = _render->m_debug&BGFX_DEBUG_WIREFRAME ? BGFX_STATE_PT_LINES : newFlags&BGFX_STATE_PT_MASK;
  5914. primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  5915. prim = s_primInfo[primIndex];
  5916. }
  5917. bool programChanged = false;
  5918. bool constantsChanged = draw.m_constBegin < draw.m_constEnd;
  5919. bool bindAttribs = false;
  5920. rendererUpdateUniforms(this, _render->m_uniformBuffer, draw.m_constBegin, draw.m_constEnd);
  5921. if (key.m_program != programIdx)
  5922. {
  5923. programIdx = key.m_program;
  5924. GLuint id = invalidHandle == programIdx ? 0 : m_program[programIdx].m_id;
  5925. // Skip rendering if program index is valid, but program is invalid.
  5926. programIdx = 0 == id ? invalidHandle : programIdx;
  5927. GL_CHECK(glUseProgram(id) );
  5928. programChanged =
  5929. constantsChanged =
  5930. bindAttribs = true;
  5931. }
  5932. if (invalidHandle != programIdx)
  5933. {
  5934. ProgramGL& program = m_program[programIdx];
  5935. if (constantsChanged
  5936. && NULL != program.m_constantBuffer)
  5937. {
  5938. commit(*program.m_constantBuffer);
  5939. }
  5940. viewState.setPredefined<1>(this, view, eye, program, _render, draw);
  5941. {
  5942. for (uint32_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage)
  5943. {
  5944. const Binding& bind = draw.m_bind[stage];
  5945. Binding& current = currentState.m_bind[stage];
  5946. if (current.m_idx != bind.m_idx
  5947. || current.m_un.m_draw.m_textureFlags != bind.m_un.m_draw.m_textureFlags
  5948. || programChanged)
  5949. {
  5950. if (invalidHandle != bind.m_idx)
  5951. {
  5952. TextureGL& texture = m_textures[bind.m_idx];
  5953. texture.commit(stage, bind.m_un.m_draw.m_textureFlags, _render->m_colorPalette);
  5954. }
  5955. }
  5956. current = bind;
  5957. }
  5958. }
  5959. if (0 != defaultVao
  5960. && 0 == draw.m_stream[0].m_startVertex
  5961. && 0 == draw.m_instanceDataOffset)
  5962. {
  5963. if (programChanged
  5964. || baseVertex != draw.m_stream[0].m_startVertex
  5965. || currentState.m_streamMask != draw.m_streamMask
  5966. || currentState.m_stream[0].m_handle.idx != draw.m_stream[0].m_handle.idx
  5967. || currentState.m_indexBuffer.idx != draw.m_indexBuffer.idx
  5968. || currentState.m_instanceDataOffset != draw.m_instanceDataOffset
  5969. || currentState.m_instanceDataStride != draw.m_instanceDataStride
  5970. || currentState.m_instanceDataBuffer.idx != draw.m_instanceDataBuffer.idx)
  5971. {
  5972. bx::HashMurmur2A murmur;
  5973. murmur.begin();
  5974. const Stream& stream = draw.m_stream[0];
  5975. murmur.add(stream.m_handle.idx);
  5976. if (isValid(stream.m_handle) )
  5977. {
  5978. const VertexBufferGL& vb = m_vertexBuffers[stream.m_handle.idx];
  5979. uint16_t decl = !isValid(vb.m_decl) ? stream.m_decl.idx : vb.m_decl.idx;
  5980. murmur.add(decl);
  5981. }
  5982. murmur.add(draw.m_indexBuffer.idx);
  5983. murmur.add(draw.m_instanceDataBuffer.idx);
  5984. murmur.add(draw.m_instanceDataOffset);
  5985. murmur.add(draw.m_instanceDataStride);
  5986. murmur.add(programIdx);
  5987. uint32_t hash = murmur.end();
  5988. currentState.m_streamMask = draw.m_streamMask;
  5989. currentState.m_stream[0].m_handle = stream.m_handle;
  5990. baseVertex = stream.m_startVertex;
  5991. currentState.m_indexBuffer = draw.m_indexBuffer;
  5992. currentState.m_instanceDataOffset = draw.m_instanceDataOffset;
  5993. currentState.m_instanceDataStride = draw.m_instanceDataStride;
  5994. GLuint id = m_vaoStateCache.find(hash);
  5995. if (UINT32_MAX != id)
  5996. {
  5997. currentVao = id;
  5998. GL_CHECK(glBindVertexArray(id) );
  5999. }
  6000. else
  6001. {
  6002. id = m_vaoStateCache.add(hash);
  6003. currentVao = id;
  6004. GL_CHECK(glBindVertexArray(id) );
  6005. program.add(hash);
  6006. if (isValid(stream.m_handle) )
  6007. {
  6008. VertexBufferGL& vb = m_vertexBuffers[stream.m_handle.idx];
  6009. vb.add(hash);
  6010. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  6011. uint16_t decl = !isValid(vb.m_decl) ? stream.m_decl.idx : vb.m_decl.idx;
  6012. program.bindAttributes(m_vertexDecls[decl], stream.m_startVertex);
  6013. if (isValid(draw.m_instanceDataBuffer) )
  6014. {
  6015. VertexBufferGL& instanceVb = m_vertexBuffers[draw.m_instanceDataBuffer.idx];
  6016. instanceVb.add(hash);
  6017. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, instanceVb.m_id) );
  6018. program.bindInstanceData(draw.m_instanceDataStride, draw.m_instanceDataOffset);
  6019. }
  6020. }
  6021. else
  6022. {
  6023. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  6024. }
  6025. if (isValid(draw.m_indexBuffer) )
  6026. {
  6027. IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  6028. ib.add(hash);
  6029. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  6030. }
  6031. else
  6032. {
  6033. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  6034. }
  6035. }
  6036. }
  6037. }
  6038. else
  6039. {
  6040. if (0 != defaultVao
  6041. && 0 != currentVao)
  6042. {
  6043. GL_CHECK(glBindVertexArray(defaultVao) );
  6044. currentState.m_streamMask = 0;
  6045. currentState.m_stream[0].m_handle.idx = invalidHandle;
  6046. currentState.m_indexBuffer.idx = invalidHandle;
  6047. bindAttribs = true;
  6048. currentVao = 0;
  6049. }
  6050. if (programChanged
  6051. || currentState.m_streamMask != draw.m_streamMask
  6052. || currentState.m_stream[0].m_handle.idx != draw.m_stream[0].m_handle.idx
  6053. || currentState.m_instanceDataBuffer.idx != draw.m_instanceDataBuffer.idx
  6054. || currentState.m_instanceDataOffset != draw.m_instanceDataOffset
  6055. || currentState.m_instanceDataStride != draw.m_instanceDataStride)
  6056. {
  6057. currentState.m_streamMask = draw.m_streamMask;
  6058. currentState.m_stream[0].m_handle = draw.m_stream[0].m_handle;
  6059. currentState.m_instanceDataBuffer.idx = draw.m_instanceDataBuffer.idx;
  6060. currentState.m_instanceDataOffset = draw.m_instanceDataOffset;
  6061. currentState.m_instanceDataStride = draw.m_instanceDataStride;
  6062. uint16_t handle = draw.m_stream[0].m_handle.idx;
  6063. if (invalidHandle != handle)
  6064. {
  6065. VertexBufferGL& vb = m_vertexBuffers[handle];
  6066. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  6067. bindAttribs = true;
  6068. }
  6069. else
  6070. {
  6071. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0) );
  6072. }
  6073. }
  6074. if (currentState.m_indexBuffer.idx != draw.m_indexBuffer.idx)
  6075. {
  6076. currentState.m_indexBuffer = draw.m_indexBuffer;
  6077. uint16_t handle = draw.m_indexBuffer.idx;
  6078. if (invalidHandle != handle)
  6079. {
  6080. IndexBufferGL& ib = m_indexBuffers[handle];
  6081. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  6082. }
  6083. else
  6084. {
  6085. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  6086. }
  6087. }
  6088. if (0 != currentState.m_streamMask)
  6089. {
  6090. if (baseVertex != draw.m_stream[0].m_startVertex
  6091. || bindAttribs)
  6092. {
  6093. baseVertex = draw.m_stream[0].m_startVertex;
  6094. const VertexBufferGL& vb = m_vertexBuffers[draw.m_stream[0].m_handle.idx];
  6095. uint16_t decl = !isValid(vb.m_decl) ? draw.m_stream[0].m_decl.idx : vb.m_decl.idx;
  6096. program.bindAttributes(m_vertexDecls[decl], draw.m_stream[0].m_startVertex);
  6097. if (isValid(draw.m_instanceDataBuffer) )
  6098. {
  6099. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffers[draw.m_instanceDataBuffer.idx].m_id) );
  6100. program.bindInstanceData(draw.m_instanceDataStride, draw.m_instanceDataOffset);
  6101. }
  6102. }
  6103. }
  6104. }
  6105. if (0 != currentState.m_streamMask)
  6106. {
  6107. uint32_t numVertices = draw.m_numVertices;
  6108. if (UINT32_MAX == numVertices)
  6109. {
  6110. const VertexBufferGL& vb = m_vertexBuffers[currentState.m_stream[0].m_handle.idx];
  6111. uint16_t decl = !isValid(vb.m_decl) ? draw.m_stream[0].m_decl.idx : vb.m_decl.idx;
  6112. const VertexDecl& vertexDecl = m_vertexDecls[decl];
  6113. numVertices = vb.m_size/vertexDecl.m_stride;
  6114. }
  6115. uint32_t numIndices = 0;
  6116. uint32_t numPrimsSubmitted = 0;
  6117. uint32_t numInstances = 0;
  6118. uint32_t numPrimsRendered = 0;
  6119. uint32_t numDrawIndirect = 0;
  6120. if (hasOcclusionQuery)
  6121. {
  6122. m_occlusionQuery.begin(_render, draw.m_occlusionQuery);
  6123. }
  6124. if (isValid(draw.m_indirectBuffer) )
  6125. {
  6126. const VertexBufferGL& vb = m_vertexBuffers[draw.m_indirectBuffer.idx];
  6127. if (currentState.m_indirectBuffer.idx != draw.m_indirectBuffer.idx)
  6128. {
  6129. currentState.m_indirectBuffer = draw.m_indirectBuffer;
  6130. GL_CHECK(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, vb.m_id) );
  6131. }
  6132. if (isValid(draw.m_indexBuffer) )
  6133. {
  6134. const IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  6135. const bool hasIndex16 = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32);
  6136. const GLenum indexFormat = hasIndex16
  6137. ? GL_UNSIGNED_SHORT
  6138. : GL_UNSIGNED_INT
  6139. ;
  6140. numDrawIndirect = UINT16_MAX == draw.m_numIndirect
  6141. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  6142. : draw.m_numIndirect
  6143. ;
  6144. uintptr_t args = draw.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  6145. GL_CHECK(glMultiDrawElementsIndirect(prim.m_type, indexFormat
  6146. , (void*)args
  6147. , numDrawIndirect
  6148. , BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  6149. ) );
  6150. }
  6151. else
  6152. {
  6153. numDrawIndirect = UINT16_MAX == draw.m_numIndirect
  6154. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  6155. : draw.m_numIndirect
  6156. ;
  6157. uintptr_t args = draw.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  6158. GL_CHECK(glMultiDrawArraysIndirect(prim.m_type
  6159. , (void*)args
  6160. , numDrawIndirect
  6161. , BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  6162. ) );
  6163. }
  6164. }
  6165. else
  6166. {
  6167. if (isValid(currentState.m_indirectBuffer) )
  6168. {
  6169. currentState.m_indirectBuffer.idx = invalidHandle;
  6170. GL_CHECK(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0) );
  6171. }
  6172. if (isValid(draw.m_indexBuffer) )
  6173. {
  6174. const IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  6175. const bool hasIndex16 = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32);
  6176. const uint32_t indexSize = hasIndex16 ? 2 : 4;
  6177. const GLenum indexFormat = hasIndex16
  6178. ? GL_UNSIGNED_SHORT
  6179. : GL_UNSIGNED_INT
  6180. ;
  6181. if (UINT32_MAX == draw.m_numIndices)
  6182. {
  6183. numIndices = ib.m_size/indexSize;
  6184. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  6185. numInstances = draw.m_numInstances;
  6186. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  6187. GL_CHECK(glDrawElementsInstanced(prim.m_type
  6188. , numIndices
  6189. , indexFormat
  6190. , (void*)0
  6191. , draw.m_numInstances
  6192. ) );
  6193. }
  6194. else if (prim.m_min <= draw.m_numIndices)
  6195. {
  6196. numIndices = draw.m_numIndices;
  6197. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  6198. numInstances = draw.m_numInstances;
  6199. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  6200. GL_CHECK(glDrawElementsInstanced(prim.m_type
  6201. , numIndices
  6202. , indexFormat
  6203. , (void*)(uintptr_t)(draw.m_startIndex*indexSize)
  6204. , draw.m_numInstances
  6205. ) );
  6206. }
  6207. }
  6208. else
  6209. {
  6210. numPrimsSubmitted = numVertices/prim.m_div - prim.m_sub;
  6211. numInstances = draw.m_numInstances;
  6212. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  6213. GL_CHECK(glDrawArraysInstanced(prim.m_type
  6214. , 0
  6215. , numVertices
  6216. , draw.m_numInstances
  6217. ) );
  6218. }
  6219. }
  6220. if (hasOcclusionQuery)
  6221. {
  6222. m_occlusionQuery.end();
  6223. }
  6224. statsNumPrimsSubmitted[primIndex] += numPrimsSubmitted;
  6225. statsNumPrimsRendered[primIndex] += numPrimsRendered;
  6226. statsNumInstances[primIndex] += numInstances;
  6227. statsNumIndices += numIndices;
  6228. }
  6229. }
  6230. }
  6231. blitMsaaFbo();
  6232. if (m_vaoSupport)
  6233. {
  6234. GL_CHECK(glBindVertexArray(m_vao) );
  6235. }
  6236. if (0 < _render->m_num)
  6237. {
  6238. if (0 != (m_resolution.m_flags & BGFX_RESET_FLUSH_AFTER_RENDER) )
  6239. {
  6240. GL_CHECK(glFlush() );
  6241. }
  6242. captureElapsed = -bx::getHPCounter();
  6243. capture();
  6244. captureElapsed += bx::getHPCounter();
  6245. BGFX_GPU_PROFILER_END();
  6246. BGFX_PROFILER_END();
  6247. }
  6248. }
  6249. BGFX_GPU_PROFILER_END();
  6250. m_glctx.makeCurrent(NULL);
  6251. int64_t now = bx::getHPCounter();
  6252. elapsed += now;
  6253. static int64_t last = now;
  6254. Stats& perfStats = _render->m_perfStats;
  6255. perfStats.cpuTimeBegin = last;
  6256. int64_t frameTime = now - last;
  6257. last = now;
  6258. static int64_t min = frameTime;
  6259. static int64_t max = frameTime;
  6260. min = min > frameTime ? frameTime : min;
  6261. max = max < frameTime ? frameTime : max;
  6262. static uint32_t maxGpuLatency = 0;
  6263. static double maxGpuElapsed = 0.0f;
  6264. double elapsedGpuMs = 0.0;
  6265. uint64_t elapsedGl = 0;
  6266. if (m_timerQuerySupport)
  6267. {
  6268. m_gpuTimer.end();
  6269. do
  6270. {
  6271. elapsedGl = m_gpuTimer.m_elapsed;
  6272. elapsedGpuMs = double(elapsedGl)/1e6;
  6273. maxGpuElapsed = elapsedGpuMs > maxGpuElapsed ? elapsedGpuMs : maxGpuElapsed;
  6274. }
  6275. while (m_gpuTimer.get() );
  6276. maxGpuLatency = bx::uint32_imax(maxGpuLatency, m_gpuTimer.m_control.available()-1);
  6277. }
  6278. const int64_t timerFreq = bx::getHPFrequency();
  6279. perfStats.cpuTimeEnd = now;
  6280. perfStats.cpuTimerFreq = timerFreq;
  6281. perfStats.gpuTimeBegin = m_gpuTimer.m_begin;
  6282. perfStats.gpuTimeEnd = m_gpuTimer.m_end;
  6283. perfStats.gpuTimerFreq = 1000000000;
  6284. if (_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  6285. {
  6286. TextVideoMem& tvm = m_textVideoMem;
  6287. static int64_t next = now;
  6288. if (now >= next)
  6289. {
  6290. next = now + timerFreq;
  6291. double freq = double(timerFreq);
  6292. double toMs = 1000.0/freq;
  6293. tvm.clear();
  6294. uint16_t pos = 0;
  6295. tvm.printf(0, pos++, BGFX_CONFIG_DEBUG ? 0x89 : 0x8f, " %s / " BX_COMPILER_NAME " / " BX_CPU_NAME " / " BX_ARCH_NAME " / " BX_PLATFORM_NAME " "
  6296. , getRendererName()
  6297. );
  6298. tvm.printf(0, pos++, 0x8f, " Vendor: %s ", m_vendor);
  6299. tvm.printf(0, pos++, 0x8f, " Renderer: %s ", m_renderer);
  6300. tvm.printf(0, pos++, 0x8f, " Version: %s ", m_version);
  6301. tvm.printf(0, pos++, 0x8f, " GLSL version: %s ", m_glslVersion);
  6302. char processMemoryUsed[16];
  6303. bx::prettify(processMemoryUsed, BX_COUNTOF(processMemoryUsed), bx::getProcessMemoryUsed() );
  6304. tvm.printf(0, pos++, 0x8f, " Memory: %s (process) ", processMemoryUsed);
  6305. pos = 10;
  6306. tvm.printf(10, pos++, 0x8e, " Frame: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS "
  6307. , double(frameTime)*toMs
  6308. , double(min)*toMs
  6309. , double(max)*toMs
  6310. , freq/frameTime
  6311. );
  6312. char hmd[16];
  6313. bx::snprintf(hmd, BX_COUNTOF(hmd), ", [%c] HMD ", hmdEnabled ? '\xfe' : ' ');
  6314. const uint32_t msaa = (m_resolution.m_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  6315. tvm.printf(10, pos++, 0x8e, " Reset flags: [%c] vsync, [%c] MSAAx%d%s, [%c] MaxAnisotropy "
  6316. , !!(m_resolution.m_flags&BGFX_RESET_VSYNC) ? '\xfe' : ' '
  6317. , 0 != msaa ? '\xfe' : ' '
  6318. , 1<<msaa
  6319. , m_ovr.isInitialized() ? hmd : ", no-HMD "
  6320. , !!(m_resolution.m_flags&BGFX_RESET_MAXANISOTROPY) ? '\xfe' : ' '
  6321. );
  6322. double elapsedCpuMs = double(elapsed)*toMs;
  6323. tvm.printf(10, pos++, 0x8e, " Submitted: %5d (draw %5d, compute %4d) / CPU %7.4f [ms] %c GPU %7.4f [ms] (latency %d) "
  6324. , _render->m_num
  6325. , statsKeyType[0]
  6326. , statsKeyType[1]
  6327. , elapsedCpuMs
  6328. , elapsedCpuMs > elapsedGpuMs ? '>' : '<'
  6329. , maxGpuElapsed
  6330. , maxGpuLatency
  6331. );
  6332. maxGpuLatency = 0;
  6333. maxGpuElapsed = 0.0;
  6334. for (uint32_t ii = 0; ii < BX_COUNTOF(s_primInfo); ++ii)
  6335. {
  6336. tvm.printf(10, pos++, 0x8e, " %10s: %7d (#inst: %5d), submitted: %7d "
  6337. , s_primName[ii]
  6338. , statsNumPrimsRendered[ii]
  6339. , statsNumInstances[ii]
  6340. , statsNumPrimsSubmitted[ii]
  6341. );
  6342. }
  6343. if (NULL != m_renderdocdll)
  6344. {
  6345. tvm.printf(tvm.m_width-27, 0, 0x1f, " [F11 - RenderDoc capture] ");
  6346. }
  6347. tvm.printf(10, pos++, 0x8e, " Indices: %7d ", statsNumIndices);
  6348. tvm.printf(10, pos++, 0x8e, " Uniform size: %7d, Max: %7d ", _render->m_uniformEnd, _render->m_uniformMax);
  6349. tvm.printf(10, pos++, 0x8e, " DVB size: %7d ", _render->m_vboffset);
  6350. tvm.printf(10, pos++, 0x8e, " DIB size: %7d ", _render->m_iboffset);
  6351. pos++;
  6352. tvm.printf(10, pos++, 0x8e, " State cache: ");
  6353. tvm.printf(10, pos++, 0x8e, " VAO | Sampler ");
  6354. tvm.printf(10, pos++, 0x8e, " %6d | %6d "
  6355. , m_vaoStateCache.getCount()
  6356. , m_samplerStateCache.getCount()
  6357. );
  6358. #if BGFX_CONFIG_RENDERER_OPENGL
  6359. if (s_extension[Extension::ATI_meminfo].m_supported)
  6360. {
  6361. GLint vboFree[4];
  6362. GL_CHECK(glGetIntegerv(GL_VBO_FREE_MEMORY_ATI, vboFree) );
  6363. GLint texFree[4];
  6364. GL_CHECK(glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, texFree) );
  6365. GLint rbfFree[4];
  6366. GL_CHECK(glGetIntegerv(GL_RENDERBUFFER_FREE_MEMORY_ATI, rbfFree) );
  6367. pos++;
  6368. tvm.printf(10, pos++, 0x8c, " -------------| free| free b| aux| aux fb ");
  6369. char tmp0[16];
  6370. char tmp1[16];
  6371. char tmp2[16];
  6372. char tmp3[16];
  6373. bx::prettify(tmp0, BX_COUNTOF(tmp0), vboFree[0]);
  6374. bx::prettify(tmp1, BX_COUNTOF(tmp1), vboFree[1]);
  6375. bx::prettify(tmp2, BX_COUNTOF(tmp2), vboFree[2]);
  6376. bx::prettify(tmp3, BX_COUNTOF(tmp3), vboFree[3]);
  6377. tvm.printf(10, pos++, 0x8e, " VBO: %10s, %10s, %10s, %10s ", tmp0, tmp1, tmp2, tmp3);
  6378. bx::prettify(tmp0, BX_COUNTOF(tmp0), texFree[0]);
  6379. bx::prettify(tmp1, BX_COUNTOF(tmp1), texFree[1]);
  6380. bx::prettify(tmp2, BX_COUNTOF(tmp2), texFree[2]);
  6381. bx::prettify(tmp3, BX_COUNTOF(tmp3), texFree[3]);
  6382. tvm.printf(10, pos++, 0x8e, " Texture: %10s, %10s, %10s, %10s ", tmp0, tmp1, tmp2, tmp3);
  6383. bx::prettify(tmp0, BX_COUNTOF(tmp0), rbfFree[0]);
  6384. bx::prettify(tmp1, BX_COUNTOF(tmp1), rbfFree[1]);
  6385. bx::prettify(tmp2, BX_COUNTOF(tmp2), rbfFree[2]);
  6386. bx::prettify(tmp3, BX_COUNTOF(tmp3), rbfFree[3]);
  6387. tvm.printf(10, pos++, 0x8e, " Render Buffer: %10s, %10s, %10s, %10s ", tmp0, tmp1, tmp2, tmp3);
  6388. }
  6389. else if (s_extension[Extension::NVX_gpu_memory_info].m_supported)
  6390. {
  6391. GLint dedicated;
  6392. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &dedicated) );
  6393. GLint totalAvail;
  6394. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &totalAvail) );
  6395. GLint currAvail;
  6396. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &currAvail) );
  6397. GLint evictedCount;
  6398. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX, &evictedCount) );
  6399. GLint evictedMemory;
  6400. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX, &evictedMemory) );
  6401. pos++;
  6402. char tmp0[16];
  6403. char tmp1[16];
  6404. bx::prettify(tmp0, BX_COUNTOF(tmp0), dedicated);
  6405. tvm.printf(10, pos++, 0x8e, " Dedicated: %10s ", tmp0);
  6406. bx::prettify(tmp0, BX_COUNTOF(tmp0), currAvail);
  6407. bx::prettify(tmp1, BX_COUNTOF(tmp1), totalAvail);
  6408. tvm.printf(10, pos++, 0x8e, " Available: %10s / %10s ", tmp0, tmp1);
  6409. bx::prettify(tmp0, BX_COUNTOF(tmp0), evictedCount);
  6410. bx::prettify(tmp1, BX_COUNTOF(tmp1), evictedMemory);
  6411. tvm.printf(10, pos++, 0x8e, " Eviction: %10s / %10s ", tmp0, tmp1);
  6412. }
  6413. #endif // BGFX_CONFIG_RENDERER_OPENGL
  6414. pos++;
  6415. double captureMs = double(captureElapsed)*toMs;
  6416. tvm.printf(10, pos++, 0x8e, " Capture: %7.4f [ms] ", captureMs);
  6417. uint8_t attr[2] = { 0x89, 0x8a };
  6418. uint8_t attrIndex = _render->m_waitSubmit < _render->m_waitRender;
  6419. tvm.printf(10, pos++, attr[attrIndex&1], " Submit wait: %7.4f [ms] ", double(_render->m_waitSubmit)*toMs);
  6420. tvm.printf(10, pos++, attr[(attrIndex+1)&1], " Render wait: %7.4f [ms] ", double(_render->m_waitRender)*toMs);
  6421. min = frameTime;
  6422. max = frameTime;
  6423. }
  6424. blit(this, _textVideoMemBlitter, tvm);
  6425. }
  6426. else if (_render->m_debug & BGFX_DEBUG_TEXT)
  6427. {
  6428. blit(this, _textVideoMemBlitter, _render->m_textVideoMem);
  6429. }
  6430. GL_CHECK(glFrameTerminatorGREMEDY() );
  6431. }
  6432. } } // namespace bgfx
  6433. #undef BGFX_GPU_PROFILER_BIND
  6434. #undef BGFX_GPU_PROFILER_UNBIND
  6435. #undef BGFX_GPU_PROFILER_BEGIN
  6436. #undef BGFX_GPU_PROFILER_BEGIN_DYNAMIC
  6437. #undef BGFX_GPU_PROFILER_END
  6438. #else
  6439. namespace bgfx { namespace gl
  6440. {
  6441. RendererContextI* rendererCreate()
  6442. {
  6443. return NULL;
  6444. }
  6445. void rendererDestroy()
  6446. {
  6447. }
  6448. } /* namespace gl */ } // namespace bgfx
  6449. #endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)