renderer_gl.cpp 270 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  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 "emscripten.h"
  11. namespace bgfx { namespace gl
  12. {
  13. static char s_viewName[BGFX_CONFIG_MAX_VIEWS][BGFX_CONFIG_MAX_VIEW_NAME];
  14. inline void setViewType(ViewId _view, const bx::StringView _str)
  15. {
  16. if (BX_ENABLED(BGFX_CONFIG_DEBUG_ANNOTATION || BGFX_CONFIG_PROFILER) )
  17. {
  18. bx::memCopy(&s_viewName[_view][3], _str.getPtr(), _str.getLength() );
  19. }
  20. }
  21. struct PrimInfo
  22. {
  23. GLenum m_type;
  24. uint32_t m_min;
  25. uint32_t m_div;
  26. uint32_t m_sub;
  27. };
  28. static const PrimInfo s_primInfo[] =
  29. {
  30. { GL_TRIANGLES, 3, 3, 0 },
  31. { GL_TRIANGLE_STRIP, 3, 1, 2 },
  32. { GL_LINES, 2, 2, 0 },
  33. { GL_LINE_STRIP, 2, 1, 1 },
  34. { GL_POINTS, 1, 1, 0 },
  35. { GL_ZERO, 0, 0, 0 },
  36. };
  37. static_assert(Topology::Count == BX_COUNTOF(s_primInfo)-1);
  38. static const char* s_attribName[] =
  39. {
  40. "a_position",
  41. "a_normal",
  42. "a_tangent",
  43. "a_bitangent",
  44. "a_color0",
  45. "a_color1",
  46. "a_color2",
  47. "a_color3",
  48. "a_indices",
  49. "a_weight",
  50. "a_texcoord0",
  51. "a_texcoord1",
  52. "a_texcoord2",
  53. "a_texcoord3",
  54. "a_texcoord4",
  55. "a_texcoord5",
  56. "a_texcoord6",
  57. "a_texcoord7",
  58. };
  59. static_assert(Attrib::Count == BX_COUNTOF(s_attribName) );
  60. static const char* s_instanceDataName[] =
  61. {
  62. "i_data0",
  63. "i_data1",
  64. "i_data2",
  65. "i_data3",
  66. "i_data4",
  67. };
  68. static_assert(BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT == BX_COUNTOF(s_instanceDataName) );
  69. static const GLenum s_access[] =
  70. {
  71. GL_READ_ONLY,
  72. GL_WRITE_ONLY,
  73. GL_READ_WRITE,
  74. };
  75. static_assert(Access::Count == BX_COUNTOF(s_access) );
  76. static const GLenum s_attribType[] =
  77. {
  78. GL_UNSIGNED_BYTE, // Uint8
  79. GL_UNSIGNED_INT_10_10_10_2, // Uint10
  80. GL_SHORT, // Int16
  81. GL_HALF_FLOAT, // Half
  82. GL_FLOAT, // Float
  83. };
  84. static_assert(AttribType::Count == BX_COUNTOF(s_attribType) );
  85. struct Blend
  86. {
  87. GLenum m_src;
  88. GLenum m_dst;
  89. bool m_factor;
  90. };
  91. static const Blend s_blendFactor[] =
  92. {
  93. { 0, 0, false }, // ignored
  94. { GL_ZERO, GL_ZERO, false }, // ZERO
  95. { GL_ONE, GL_ONE, false }, // ONE
  96. { GL_SRC_COLOR, GL_SRC_COLOR, false }, // SRC_COLOR
  97. { GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, false }, // INV_SRC_COLOR
  98. { GL_SRC_ALPHA, GL_SRC_ALPHA, false }, // SRC_ALPHA
  99. { GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, false }, // INV_SRC_ALPHA
  100. { GL_DST_ALPHA, GL_DST_ALPHA, false }, // DST_ALPHA
  101. { GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, false }, // INV_DST_ALPHA
  102. { GL_DST_COLOR, GL_DST_COLOR, false }, // DST_COLOR
  103. { GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_DST_COLOR, false }, // INV_DST_COLOR
  104. { GL_SRC_ALPHA_SATURATE, GL_ONE, false }, // SRC_ALPHA_SAT
  105. { GL_CONSTANT_COLOR, GL_CONSTANT_COLOR, true }, // FACTOR
  106. { GL_ONE_MINUS_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, true }, // INV_FACTOR
  107. };
  108. static const GLenum s_blendEquation[] =
  109. {
  110. GL_FUNC_ADD,
  111. GL_FUNC_SUBTRACT,
  112. GL_FUNC_REVERSE_SUBTRACT,
  113. GL_MIN,
  114. GL_MAX,
  115. };
  116. static const GLenum s_cmpFunc[] =
  117. {
  118. 0, // ignored
  119. GL_LESS,
  120. GL_LEQUAL,
  121. GL_EQUAL,
  122. GL_GEQUAL,
  123. GL_GREATER,
  124. GL_NOTEQUAL,
  125. GL_NEVER,
  126. GL_ALWAYS,
  127. };
  128. static const GLenum s_stencilOp[] =
  129. {
  130. GL_ZERO,
  131. GL_KEEP,
  132. GL_REPLACE,
  133. GL_INCR_WRAP,
  134. GL_INCR,
  135. GL_DECR_WRAP,
  136. GL_DECR,
  137. GL_INVERT,
  138. };
  139. static const GLenum s_stencilFace[] =
  140. {
  141. GL_FRONT_AND_BACK,
  142. GL_FRONT,
  143. GL_BACK,
  144. };
  145. static GLenum s_textureAddress[] =
  146. {
  147. GL_REPEAT,
  148. GL_MIRRORED_REPEAT,
  149. GL_CLAMP_TO_EDGE,
  150. GL_CLAMP_TO_BORDER,
  151. };
  152. static const GLenum s_textureFilterMag[] =
  153. {
  154. GL_LINEAR,
  155. GL_NEAREST,
  156. GL_LINEAR,
  157. };
  158. static const GLenum s_textureFilterMin[][3] =
  159. {
  160. { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST },
  161. { GL_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_NEAREST },
  162. { GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST },
  163. };
  164. struct TextureFormatInfo
  165. {
  166. GLenum m_internalFmt;
  167. GLenum m_internalFmtSrgb;
  168. GLenum m_fmt;
  169. GLenum m_fmtSrgb;
  170. GLenum m_type;
  171. bool m_supported;
  172. GLint m_mapping[4];
  173. };
  174. // In desktop OpenGL 4+ and OpenGL ES 3.0+, specific GL formats GL_x_INTEGER are used for integer textures.
  175. // For older desktop OpenGL contexts, GL names without _INTEGER suffix were used.
  176. // See http://docs.gl/gl4/glTexImage2D, http://docs.gl/gl3/glTexImage2D, http://docs.gl/es3/glTexImage2D
  177. #if BGFX_CONFIG_RENDERER_OPENGL >= 40 || BGFX_CONFIG_RENDERER_OPENGLES
  178. # define RED_INTEGER GL_RED_INTEGER
  179. # define RG_INTEGER GL_RG_INTEGER
  180. # define RGB_INTEGER GL_RGB_INTEGER
  181. # define RGBA_INTEGER GL_RGBA_INTEGER
  182. #else
  183. # define RED_INTEGER GL_RED
  184. # define RG_INTEGER GL_RG
  185. # define RGB_INTEGER GL_RGB
  186. # define RGBA_INTEGER GL_RGBA
  187. #endif
  188. static TextureFormatInfo s_textureFormat[] =
  189. {
  190. #define $_ -1
  191. #define $0 GL_ZERO
  192. #define $1 GL_ONE
  193. #define $R GL_RED
  194. #define $G GL_GREEN
  195. #define $B GL_BLUE
  196. #define $A GL_ALPHA
  197. { GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_ZERO, false, { $_, $_, $_, $_ } }, // BC1
  198. { GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_ZERO, false, { $_, $_, $_, $_ } }, // BC2
  199. { GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_ZERO, false, { $_, $_, $_, $_ } }, // BC3
  200. { GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_ZERO, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_ZERO, false, { $_, $_, $_, $_ } }, // BC4
  201. { GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_ZERO, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_ZERO, false, { $_, $_, $_, $_ } }, // BC5
  202. { GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_ZERO, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, GL_ZERO, false, { $_, $_, $_, $_ } }, // BC6H
  203. { GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_COMPRESSED_RGBA_BPTC_UNORM_ARB, GL_ZERO, false, { $_, $_, $_, $_ } }, // BC7
  204. { GL_ETC1_RGB8_OES, GL_ZERO, GL_ETC1_RGB8_OES, GL_ETC1_RGB8_OES, GL_ZERO, false, { $_, $_, $_, $_ } }, // ETC1
  205. { GL_COMPRESSED_RGB8_ETC2, GL_ZERO, GL_COMPRESSED_RGB8_ETC2, GL_COMPRESSED_RGB8_ETC2, GL_ZERO, false, { $_, $_, $_, $_ } }, // ETC2
  206. { GL_COMPRESSED_RGBA8_ETC2_EAC, GL_COMPRESSED_SRGB8_ETC2, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_COMPRESSED_RGBA8_ETC2_EAC, GL_ZERO, false, { $_, $_, $_, $_ } }, // ETC2A
  207. { GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, GL_ZERO, false, { $_, $_, $_, $_ } }, // ETC2A1
  208. { GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, GL_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, GL_ZERO, false, { $_, $_, $_, $_ } }, // PTC12
  209. { GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, GL_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, GL_ZERO, false, { $_, $_, $_, $_ } }, // PTC14
  210. { GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, GL_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, GL_ZERO, false, { $_, $_, $_, $_ } }, // PTC12A
  211. { GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, GL_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, GL_ZERO, false, { $_, $_, $_, $_ } }, // PTC14A
  212. { GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, GL_ZERO, GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, GL_ZERO, false, { $_, $_, $_, $_ } }, // PTC22
  213. { GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, GL_ZERO, GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, GL_ZERO, false, { $_, $_, $_, $_ } }, // PTC24
  214. { GL_ATC_RGB_AMD, GL_ZERO, GL_ATC_RGB_AMD, GL_ATC_RGB_AMD, GL_ZERO, false, { $_, $_, $_, $_ } }, // ATC
  215. { GL_ATC_RGBA_EXPLICIT_ALPHA_AMD, GL_ZERO, GL_ATC_RGBA_EXPLICIT_ALPHA_AMD, GL_ATC_RGBA_EXPLICIT_ALPHA_AMD, GL_ZERO, false, { $_, $_, $_, $_ } }, // ATCE
  216. { GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, GL_ZERO, GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD, GL_ZERO, false, { $_, $_, $_, $_ } }, // ATCI
  217. { GL_COMPRESSED_RGBA_ASTC_4x4_KHR, GL_COMPRESSED_SRGB8_ASTC_4x4_KHR, GL_COMPRESSED_RGBA_ASTC_4x4_KHR, GL_COMPRESSED_RGBA_ASTC_4x4_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC4x4
  218. { GL_COMPRESSED_RGBA_ASTC_5x4_KHR, GL_COMPRESSED_SRGB8_ASTC_5x4_KHR, GL_COMPRESSED_RGBA_ASTC_5x4_KHR, GL_COMPRESSED_RGBA_ASTC_5x4_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC5x4
  219. { GL_COMPRESSED_RGBA_ASTC_5x5_KHR, GL_COMPRESSED_SRGB8_ASTC_5x5_KHR, GL_COMPRESSED_RGBA_ASTC_5x5_KHR, GL_COMPRESSED_RGBA_ASTC_5x5_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC5x5
  220. { GL_COMPRESSED_RGBA_ASTC_6x5_KHR, GL_COMPRESSED_SRGB8_ASTC_6x5_KHR, GL_COMPRESSED_RGBA_ASTC_6x5_KHR, GL_COMPRESSED_RGBA_ASTC_6x5_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC6x5
  221. { GL_COMPRESSED_RGBA_ASTC_6x6_KHR, GL_COMPRESSED_SRGB8_ASTC_6x6_KHR, GL_COMPRESSED_RGBA_ASTC_6x6_KHR, GL_COMPRESSED_RGBA_ASTC_6x6_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC6x6
  222. { GL_COMPRESSED_RGBA_ASTC_8x5_KHR, GL_COMPRESSED_SRGB8_ASTC_8x5_KHR, GL_COMPRESSED_RGBA_ASTC_8x5_KHR, GL_COMPRESSED_RGBA_ASTC_8x5_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC8x5
  223. { GL_COMPRESSED_RGBA_ASTC_8x6_KHR, GL_COMPRESSED_SRGB8_ASTC_8x6_KHR, GL_COMPRESSED_RGBA_ASTC_8x6_KHR, GL_COMPRESSED_RGBA_ASTC_8x6_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC8x6
  224. { GL_COMPRESSED_RGBA_ASTC_8x8_KHR, GL_COMPRESSED_SRGB8_ASTC_8x8_KHR, GL_COMPRESSED_RGBA_ASTC_8x8_KHR, GL_COMPRESSED_RGBA_ASTC_8x8_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC8x8
  225. { GL_COMPRESSED_RGBA_ASTC_10x5_KHR, GL_COMPRESSED_SRGB8_ASTC_10x5_KHR, GL_COMPRESSED_RGBA_ASTC_10x5_KHR, GL_COMPRESSED_RGBA_ASTC_10x5_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC10x5
  226. { GL_COMPRESSED_RGBA_ASTC_10x6_KHR, GL_COMPRESSED_SRGB8_ASTC_10x6_KHR, GL_COMPRESSED_RGBA_ASTC_10x6_KHR, GL_COMPRESSED_RGBA_ASTC_10x6_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC10x6
  227. { GL_COMPRESSED_RGBA_ASTC_10x8_KHR, GL_COMPRESSED_SRGB8_ASTC_10x8_KHR, GL_COMPRESSED_RGBA_ASTC_10x8_KHR, GL_COMPRESSED_RGBA_ASTC_10x8_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC10x8
  228. { GL_COMPRESSED_RGBA_ASTC_10x10_KHR, GL_COMPRESSED_SRGB8_ASTC_10x10_KHR, GL_COMPRESSED_RGBA_ASTC_10x10_KHR, GL_COMPRESSED_RGBA_ASTC_10x10_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC10x10
  229. { GL_COMPRESSED_RGBA_ASTC_12x10_KHR, GL_COMPRESSED_SRGB8_ASTC_12x10_KHR, GL_COMPRESSED_RGBA_ASTC_12x10_KHR, GL_COMPRESSED_RGBA_ASTC_12x10_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC12x10
  230. { GL_COMPRESSED_RGBA_ASTC_12x12_KHR, GL_COMPRESSED_SRGB8_ASTC_12x12_KHR, GL_COMPRESSED_RGBA_ASTC_12x12_KHR, GL_COMPRESSED_RGBA_ASTC_12x12_KHR, GL_ZERO, false, { $_, $_, $_, $_ } }, // ASTC12x12
  231. { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false, { $_, $_, $_, $_ } }, // Unknown
  232. { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false, { $_, $_, $_, $_ } }, // R1
  233. { GL_ALPHA, GL_ZERO, GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // A8
  234. { GL_R8, GL_ZERO, GL_RED, GL_RED, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // R8
  235. { GL_R8I, GL_ZERO, RED_INTEGER, GL_RED_INTEGER, GL_BYTE, false, { $_, $_, $_, $_ } }, // R8I
  236. { GL_R8UI, GL_ZERO, RED_INTEGER, GL_RED_INTEGER, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // R8U
  237. { GL_R8_SNORM, GL_ZERO, GL_RED, GL_RED, GL_BYTE, false, { $_, $_, $_, $_ } }, // R8S
  238. { GL_R16, GL_ZERO, GL_RED, GL_RED, GL_UNSIGNED_SHORT, false, { $_, $_, $_, $_ } }, // R16
  239. { GL_R16I, GL_ZERO, RED_INTEGER, GL_RED_INTEGER, GL_SHORT, false, { $_, $_, $_, $_ } }, // R16I
  240. { GL_R16UI, GL_ZERO, RED_INTEGER, GL_RED_INTEGER, GL_UNSIGNED_SHORT, false, { $_, $_, $_, $_ } }, // R16U
  241. { GL_R16F, GL_ZERO, GL_RED, GL_RED, GL_HALF_FLOAT, false, { $_, $_, $_, $_ } }, // R16F
  242. { GL_R16_SNORM, GL_ZERO, GL_RED, GL_RED, GL_SHORT, false, { $_, $_, $_, $_ } }, // R16S
  243. { GL_R32I, GL_ZERO, RED_INTEGER, GL_RED_INTEGER, GL_INT, false, { $_, $_, $_, $_ } }, // R32I
  244. { GL_R32UI, GL_ZERO, RED_INTEGER, GL_RED_INTEGER, GL_UNSIGNED_INT, false, { $_, $_, $_, $_ } }, // R32U
  245. { GL_R32F, GL_ZERO, GL_RED, GL_RED, GL_FLOAT, false, { $_, $_, $_, $_ } }, // R32F
  246. { GL_RG8, GL_ZERO, GL_RG, GL_RG, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // RG8
  247. { GL_RG8I, GL_ZERO, RG_INTEGER, GL_RG_INTEGER, GL_BYTE, false, { $_, $_, $_, $_ } }, // RG8I
  248. { GL_RG8UI, GL_ZERO, RG_INTEGER, GL_RG_INTEGER, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // RG8U
  249. { GL_RG8_SNORM, GL_ZERO, GL_RG, GL_RG, GL_BYTE, false, { $_, $_, $_, $_ } }, // RG8S
  250. { GL_RG16, GL_ZERO, GL_RG, GL_RG, GL_UNSIGNED_SHORT, false, { $_, $_, $_, $_ } }, // RG16
  251. { GL_RG16I, GL_ZERO, RG_INTEGER, GL_RG_INTEGER, GL_SHORT, false, { $_, $_, $_, $_ } }, // RG16I
  252. { GL_RG16UI, GL_ZERO, RG_INTEGER, GL_RG_INTEGER, GL_UNSIGNED_SHORT, false, { $_, $_, $_, $_ } }, // RG16U
  253. { GL_RG16F, GL_ZERO, GL_RG, GL_RG, GL_HALF_FLOAT, false, { $_, $_, $_, $_ } }, // RG16F
  254. { GL_RG16_SNORM, GL_ZERO, GL_RG, GL_RG, GL_SHORT, false, { $_, $_, $_, $_ } }, // RG16S
  255. { GL_RG32I, GL_ZERO, RG_INTEGER, GL_RG_INTEGER, GL_INT, false, { $_, $_, $_, $_ } }, // RG32I
  256. { GL_RG32UI, GL_ZERO, RG_INTEGER, GL_RG_INTEGER, GL_UNSIGNED_INT, false, { $_, $_, $_, $_ } }, // RG32U
  257. { GL_RG32F, GL_ZERO, GL_RG, GL_RG, GL_FLOAT, false, { $_, $_, $_, $_ } }, // RG32F
  258. { GL_RGB8, GL_SRGB8, GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // RGB8
  259. { GL_RGB8I, GL_ZERO, RGB_INTEGER, GL_RGB_INTEGER, GL_BYTE, false, { $_, $_, $_, $_ } }, // RGB8I
  260. { GL_RGB8UI, GL_ZERO, RGB_INTEGER, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // RGB8U
  261. { GL_RGB8_SNORM, GL_ZERO, GL_RGB, GL_RGB, GL_BYTE, false, { $_, $_, $_, $_ } }, // RGB8S
  262. { GL_RGB9_E5, GL_ZERO, GL_RGB, GL_RGB, GL_UNSIGNED_INT_5_9_9_9_REV, false, { $_, $_, $_, $_ } }, // RGB9E5F
  263. { GL_RGBA8, GL_SRGB8_ALPHA8, GL_BGRA, GL_BGRA, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // BGRA8
  264. { GL_RGBA8, GL_SRGB8_ALPHA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // RGBA8
  265. { GL_RGBA8I, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_BYTE, false, { $_, $_, $_, $_ } }, // RGBA8I
  266. { GL_RGBA8UI, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // RGBA8U
  267. { GL_RGBA8_SNORM, GL_ZERO, GL_RGBA, GL_RGBA, GL_BYTE, false, { $_, $_, $_, $_ } }, // RGBA8S
  268. { GL_RGBA16, GL_ZERO, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT, false, { $_, $_, $_, $_ } }, // RGBA16
  269. { GL_RGBA16I, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_SHORT, false, { $_, $_, $_, $_ } }, // RGBA16I
  270. { GL_RGBA16UI, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, false, { $_, $_, $_, $_ } }, // RGBA16U
  271. { GL_RGBA16F, GL_ZERO, GL_RGBA, GL_RGBA, GL_HALF_FLOAT, false, { $_, $_, $_, $_ } }, // RGBA16F
  272. { GL_RGBA16_SNORM, GL_ZERO, GL_RGBA, GL_RGBA, GL_SHORT, false, { $_, $_, $_, $_ } }, // RGBA16S
  273. { GL_RGBA32I, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_INT, false, { $_, $_, $_, $_ } }, // RGBA32I
  274. { GL_RGBA32UI, GL_ZERO, RGBA_INTEGER, GL_RGBA_INTEGER, GL_UNSIGNED_INT, false, { $_, $_, $_, $_ } }, // RGBA32U
  275. { GL_RGBA32F, GL_ZERO, GL_RGBA, GL_RGBA, GL_FLOAT, false, { $_, $_, $_, $_ } }, // RGBA32F
  276. { GL_RGB565, GL_ZERO, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, false, { $_, $_, $_, $_ } }, // B5G6R5
  277. { GL_RGB565, GL_ZERO, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, false, { $_, $_, $_, $_ } }, // R5G6B5
  278. { GL_RGBA4, GL_ZERO, GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV, false, { $_, $_, $_, $_ } }, // BGRA4
  279. { GL_RGBA4, GL_ZERO, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV, false, { $_, $_, $_, $_ } }, // RGBA4
  280. { GL_RGB5_A1, GL_ZERO, GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, false, { $_, $_, $_, $_ } }, // BGR5A1
  281. { GL_RGB5_A1, GL_ZERO, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV, false, { $_, $_, $_, $_ } }, // RGB5A1
  282. { GL_RGB10_A2, GL_ZERO, GL_RGBA, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, false, { $_, $_, $_, $_ } }, // RGB10A2
  283. { GL_R11F_G11F_B10F, GL_ZERO, GL_RGB, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, false, { $_, $_, $_, $_ } }, // RG11B10F
  284. { GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, GL_ZERO, false, { $_, $_, $_, $_ } }, // UnknownDepth
  285. { GL_DEPTH_COMPONENT16, GL_ZERO, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, false, { $_, $_, $_, $_ } }, // D16
  286. { GL_DEPTH_COMPONENT24, GL_ZERO, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, false, { $_, $_, $_, $_ } }, // D24
  287. { GL_DEPTH24_STENCIL8, GL_ZERO, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, false, { $_, $_, $_, $_ } }, // D24S8
  288. { GL_DEPTH_COMPONENT32, GL_ZERO, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, false, { $_, $_, $_, $_ } }, // D32
  289. { GL_DEPTH_COMPONENT32F, GL_ZERO, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT, false, { $_, $_, $_, $_ } }, // D16F
  290. { GL_DEPTH_COMPONENT32F, GL_ZERO, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT, false, { $_, $_, $_, $_ } }, // D24F
  291. { GL_DEPTH_COMPONENT32F, GL_ZERO, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_FLOAT, false, { $_, $_, $_, $_ } }, // D32F
  292. { GL_STENCIL_INDEX8, GL_ZERO, GL_STENCIL_INDEX, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, false, { $_, $_, $_, $_ } }, // D0S8
  293. #undef $_
  294. #undef $0
  295. #undef $1
  296. #undef $R
  297. #undef $G
  298. #undef $B
  299. #undef $A
  300. };
  301. static_assert(TextureFormat::Count == BX_COUNTOF(s_textureFormat) );
  302. static bool s_textureFilter[TextureFormat::Count+1];
  303. static GLenum s_rboFormat[] =
  304. {
  305. GL_ZERO, // BC1
  306. GL_ZERO, // BC2
  307. GL_ZERO, // BC3
  308. GL_ZERO, // BC4
  309. GL_ZERO, // BC5
  310. GL_ZERO, // BC6H
  311. GL_ZERO, // BC7
  312. GL_ZERO, // ETC1
  313. GL_ZERO, // ETC2
  314. GL_ZERO, // ETC2A
  315. GL_ZERO, // ETC2A1
  316. GL_ZERO, // PTC12
  317. GL_ZERO, // PTC14
  318. GL_ZERO, // PTC12A
  319. GL_ZERO, // PTC14A
  320. GL_ZERO, // PTC22
  321. GL_ZERO, // PTC24
  322. GL_ZERO, // ATC
  323. GL_ZERO, // ATCE
  324. GL_ZERO, // ATCI
  325. GL_ZERO, // ASTC4x4
  326. GL_ZERO, // ASTC5x4
  327. GL_ZERO, // ASTC5x5
  328. GL_ZERO, // ASTC6x5
  329. GL_ZERO, // ASTC6x6
  330. GL_ZERO, // ASTC8x5
  331. GL_ZERO, // ASTC8x6
  332. GL_ZERO, // ASTC8x8
  333. GL_ZERO, // ASTC10x5
  334. GL_ZERO, // ASTC10x6
  335. GL_ZERO, // ASTC10x8
  336. GL_ZERO, // ASTC10x10
  337. GL_ZERO, // ASTC12x10
  338. GL_ZERO, // ASTC12x12
  339. GL_ZERO, // Unknown
  340. GL_ZERO, // R1
  341. GL_ALPHA, // A8
  342. GL_R8, // R8
  343. GL_R8I, // R8I
  344. GL_R8UI, // R8U
  345. GL_R8_SNORM, // R8S
  346. GL_R16, // R16
  347. GL_R16I, // R16I
  348. GL_R16UI, // R16U
  349. GL_R16F, // R16F
  350. GL_R16_SNORM, // R16S
  351. GL_R32I, // R32I
  352. GL_R32UI, // R32U
  353. GL_R32F, // R32F
  354. GL_RG8, // RG8
  355. GL_RG8I, // RG8I
  356. GL_RG8UI, // RG8U
  357. GL_RG8_SNORM, // RG8S
  358. GL_RG16, // RG16
  359. GL_RG16I, // RG16I
  360. GL_RG16UI, // RG16U
  361. GL_RG16F, // RG16F
  362. GL_RG16_SNORM, // RG16S
  363. GL_RG32I, // RG32I
  364. GL_RG32UI, // RG32U
  365. GL_RG32F, // RG32F
  366. GL_RGB8, // RGB8
  367. GL_RGB8I, // RGB8I
  368. GL_RGB8UI, // RGB8UI
  369. GL_RGB8_SNORM, // RGB8S
  370. GL_RGB9_E5, // RGB9E5F
  371. GL_RGBA8, // BGRA8
  372. GL_RGBA8, // RGBA8
  373. GL_RGBA8I, // RGBA8I
  374. GL_RGBA8UI, // RGBA8UI
  375. GL_RGBA8_SNORM, // RGBA8S
  376. GL_RGBA16, // RGBA16
  377. GL_RGBA16I, // RGBA16I
  378. GL_RGBA16UI, // RGBA16U
  379. GL_RGBA16F, // RGBA16F
  380. GL_RGBA16_SNORM, // RGBA16S
  381. GL_RGBA32I, // RGBA32I
  382. GL_RGBA32UI, // RGBA32U
  383. GL_RGBA32F, // RGBA32F
  384. GL_RGB565, // B5G6R5
  385. GL_RGB565, // R5G6B5
  386. GL_RGBA4, // BGRA4
  387. GL_RGBA4, // RGBA4
  388. GL_RGB5_A1, // BGR5A1
  389. GL_RGB5_A1, // RGB5A1
  390. GL_RGB10_A2, // RGB10A2
  391. GL_R11F_G11F_B10F, // RG11B10F
  392. GL_ZERO, // UnknownDepth
  393. GL_DEPTH_COMPONENT16, // D16
  394. GL_DEPTH_COMPONENT24, // D24
  395. GL_DEPTH24_STENCIL8, // D24S8
  396. GL_DEPTH_COMPONENT32, // D32
  397. GL_DEPTH_COMPONENT32F, // D16F
  398. GL_DEPTH_COMPONENT32F, // D24F
  399. GL_DEPTH_COMPONENT32F, // D32F
  400. GL_STENCIL_INDEX8, // D0S8
  401. };
  402. static_assert(TextureFormat::Count == BX_COUNTOF(s_rboFormat) );
  403. static GLenum s_imageFormat[] =
  404. {
  405. GL_ZERO, // BC1
  406. GL_ZERO, // BC2
  407. GL_ZERO, // BC3
  408. GL_ZERO, // BC4
  409. GL_ZERO, // BC5
  410. GL_ZERO, // BC6H
  411. GL_ZERO, // BC7
  412. GL_ZERO, // ETC1
  413. GL_ZERO, // ETC2
  414. GL_ZERO, // ETC2A
  415. GL_ZERO, // ETC2A1
  416. GL_ZERO, // PTC12
  417. GL_ZERO, // PTC14
  418. GL_ZERO, // PTC12A
  419. GL_ZERO, // PTC14A
  420. GL_ZERO, // PTC22
  421. GL_ZERO, // PTC24
  422. GL_ZERO, // ATC
  423. GL_ZERO, // ATCE
  424. GL_ZERO, // ATCI
  425. GL_ZERO, // ASTC4x4
  426. GL_ZERO, // ASTC5x4
  427. GL_ZERO, // ASTC5x5
  428. GL_ZERO, // ASTC6x5
  429. GL_ZERO, // ASTC6x6
  430. GL_ZERO, // ASTC8x5
  431. GL_ZERO, // ASTC8x6
  432. GL_ZERO, // ASTC8x8
  433. GL_ZERO, // ASTC10x5
  434. GL_ZERO, // ASTC10x6
  435. GL_ZERO, // ASTC10x8
  436. GL_ZERO, // ASTC10x10
  437. GL_ZERO, // ASTC12x10
  438. GL_ZERO, // ASTC12x12
  439. GL_ZERO, // Unknown
  440. GL_ZERO, // R1
  441. GL_ALPHA, // A8
  442. GL_R8, // R8
  443. GL_R8I, // R8I
  444. GL_R8UI, // R8UI
  445. GL_R8_SNORM, // R8S
  446. GL_R16, // R16
  447. GL_R16I, // R16I
  448. GL_R16UI, // R16U
  449. GL_R16F, // R16F
  450. GL_R16_SNORM, // R16S
  451. GL_R32I, // R32I
  452. GL_R32UI, // R32U
  453. GL_R32F, // R32F
  454. GL_RG8, // RG8
  455. GL_RG8I, // RG8I
  456. GL_RG8UI, // RG8U
  457. GL_RG8_SNORM, // RG8S
  458. GL_RG16, // RG16
  459. GL_RG16I, // RG16I
  460. GL_RG16UI, // RG16U
  461. GL_RG16F, // RG16F
  462. GL_RG16_SNORM, // RG16S
  463. GL_RG32I, // RG32I
  464. GL_RG32UI, // RG32U
  465. GL_RG32F, // RG32F
  466. GL_RGB8, // RGB8
  467. GL_RGB8I, // RGB8I
  468. GL_RGB8UI, // RGB8UI
  469. GL_RGB8_SNORM, // RGB8S
  470. GL_RGB9_E5, // RGB9E5F
  471. GL_RGBA8, // BGRA8
  472. GL_RGBA8, // RGBA8
  473. GL_RGBA8I, // RGBA8I
  474. GL_RGBA8UI, // RGBA8UI
  475. GL_RGBA8_SNORM, // RGBA8S
  476. GL_RGBA16, // RGBA16
  477. GL_RGBA16I, // RGBA16I
  478. GL_RGBA16UI, // RGBA16U
  479. GL_RGBA16F, // RGBA16F
  480. GL_RGBA16_SNORM, // RGBA16S
  481. GL_RGBA32I, // RGBA32I
  482. GL_RGBA32UI, // RGBA32U
  483. GL_RGBA32F, // RGBA32F
  484. GL_RGB565, // B5G6R5
  485. GL_RGB565, // R5G6B5
  486. GL_RGBA4, // BGRA4
  487. GL_RGBA4, // RGBA4
  488. GL_RGB5_A1, // BGR5A1
  489. GL_RGB5_A1, // RGB5A1
  490. GL_RGB10_A2, // RGB10A2
  491. GL_R11F_G11F_B10F, // RG11B10F
  492. GL_ZERO, // UnknownDepth
  493. GL_ZERO, // D16
  494. GL_ZERO, // D24
  495. GL_ZERO, // D24S8
  496. GL_ZERO, // D32
  497. GL_ZERO, // D16F
  498. GL_ZERO, // D24F
  499. GL_ZERO, // D32F
  500. GL_ZERO, // D0S8
  501. };
  502. static_assert(TextureFormat::Count == BX_COUNTOF(s_imageFormat) );
  503. struct Extension
  504. {
  505. enum Enum
  506. {
  507. AMD_conservative_depth,
  508. AMD_multi_draw_indirect,
  509. ANGLE_depth_texture,
  510. ANGLE_framebuffer_blit,
  511. ANGLE_framebuffer_multisample,
  512. ANGLE_instanced_arrays,
  513. ANGLE_texture_compression_dxt1,
  514. ANGLE_texture_compression_dxt3,
  515. ANGLE_texture_compression_dxt5,
  516. ANGLE_timer_query,
  517. ANGLE_translated_shader_source,
  518. APPLE_texture_format_BGRA8888,
  519. APPLE_texture_max_level,
  520. ARB_clip_control,
  521. ARB_compute_shader,
  522. ARB_conservative_depth,
  523. ARB_copy_image,
  524. ARB_debug_label,
  525. ARB_debug_output,
  526. ARB_depth_buffer_float,
  527. ARB_depth_clamp,
  528. ARB_draw_buffers_blend,
  529. ARB_draw_indirect,
  530. ARB_draw_instanced,
  531. ARB_ES3_compatibility,
  532. ARB_framebuffer_object,
  533. ARB_framebuffer_sRGB,
  534. ARB_get_program_binary,
  535. ARB_half_float_pixel,
  536. ARB_half_float_vertex,
  537. ARB_indirect_parameters,
  538. ARB_instanced_arrays,
  539. ARB_internalformat_query,
  540. ARB_internalformat_query2,
  541. ARB_invalidate_subdata,
  542. ARB_map_buffer_range,
  543. ARB_multi_draw_indirect,
  544. ARB_multisample,
  545. ARB_occlusion_query,
  546. ARB_occlusion_query2,
  547. ARB_program_interface_query,
  548. ARB_provoking_vertex,
  549. ARB_sampler_objects,
  550. ARB_seamless_cube_map,
  551. ARB_shader_bit_encoding,
  552. ARB_shader_image_load_store,
  553. ARB_shader_storage_buffer_object,
  554. ARB_shader_texture_lod,
  555. ARB_shader_viewport_layer_array,
  556. ARB_texture_compression_bptc,
  557. ARB_texture_compression_rgtc,
  558. ARB_texture_cube_map_array,
  559. ARB_texture_float,
  560. ARB_texture_multisample,
  561. ARB_texture_rg,
  562. ARB_texture_rgb10_a2ui,
  563. ARB_texture_stencil8,
  564. ARB_texture_storage,
  565. ARB_texture_swizzle,
  566. ARB_timer_query,
  567. ARB_uniform_buffer_object,
  568. ARB_vertex_array_object,
  569. ARB_vertex_type_2_10_10_10_rev,
  570. ATI_meminfo,
  571. CHROMIUM_color_buffer_float_rgb,
  572. CHROMIUM_color_buffer_float_rgba,
  573. CHROMIUM_depth_texture,
  574. CHROMIUM_framebuffer_multisample,
  575. CHROMIUM_texture_compression_dxt3,
  576. CHROMIUM_texture_compression_dxt5,
  577. EXT_bgra,
  578. EXT_blend_color,
  579. EXT_blend_minmax,
  580. EXT_blend_subtract,
  581. EXT_color_buffer_half_float,
  582. EXT_color_buffer_float,
  583. EXT_copy_image,
  584. EXT_compressed_ETC1_RGB8_sub_texture,
  585. EXT_debug_label,
  586. EXT_debug_marker,
  587. EXT_debug_tool,
  588. EXT_discard_framebuffer,
  589. EXT_disjoint_timer_query,
  590. EXT_draw_buffers,
  591. EXT_draw_instanced,
  592. EXT_instanced_arrays,
  593. EXT_frag_depth,
  594. EXT_framebuffer_blit,
  595. EXT_framebuffer_object,
  596. EXT_framebuffer_sRGB,
  597. EXT_gpu_shader4,
  598. EXT_multi_draw_indirect,
  599. EXT_occlusion_query_boolean,
  600. EXT_packed_float,
  601. EXT_read_format_bgra,
  602. EXT_shader_image_load_store,
  603. EXT_shader_texture_lod,
  604. EXT_shadow_samplers,
  605. EXT_sRGB_write_control,
  606. EXT_texture_array,
  607. EXT_texture_compression_dxt1,
  608. EXT_texture_compression_latc,
  609. EXT_texture_compression_rgtc,
  610. EXT_texture_compression_s3tc,
  611. EXT_texture_cube_map_array,
  612. EXT_texture_filter_anisotropic,
  613. EXT_texture_format_BGRA8888,
  614. EXT_texture_rg,
  615. EXT_texture_shared_exponent,
  616. EXT_texture_snorm,
  617. EXT_texture_sRGB,
  618. EXT_texture_storage,
  619. EXT_texture_swizzle,
  620. EXT_texture_type_2_10_10_10_REV,
  621. EXT_timer_query,
  622. EXT_unpack_subimage,
  623. EXT_sRGB,
  624. EXT_multisampled_render_to_texture,
  625. GOOGLE_depth_texture,
  626. IMG_multisampled_render_to_texture,
  627. IMG_read_format,
  628. IMG_shader_binary,
  629. IMG_texture_compression_pvrtc,
  630. IMG_texture_compression_pvrtc2,
  631. IMG_texture_format_BGRA8888,
  632. INTEL_fragment_shader_ordering,
  633. KHR_debug,
  634. KHR_no_error,
  635. MOZ_WEBGL_compressed_texture_s3tc,
  636. MOZ_WEBGL_depth_texture,
  637. NV_conservative_raster,
  638. NV_copy_image,
  639. NV_draw_buffers,
  640. NV_draw_instanced,
  641. NV_instanced_arrays,
  642. NV_occlusion_query,
  643. NV_texture_border_clamp,
  644. NVX_gpu_memory_info,
  645. OES_copy_image,
  646. OES_compressed_ETC1_RGB8_texture,
  647. OES_depth24,
  648. OES_depth32,
  649. OES_depth_texture,
  650. OES_element_index_uint,
  651. OES_fragment_precision_high,
  652. OES_fbo_render_mipmap,
  653. OES_get_program_binary,
  654. OES_required_internalformat,
  655. OES_packed_depth_stencil,
  656. OES_read_format,
  657. OES_rgb8_rgba8,
  658. OES_standard_derivatives,
  659. OES_texture_3D,
  660. OES_texture_float,
  661. OES_texture_float_linear,
  662. OES_texture_npot,
  663. OES_texture_half_float,
  664. OES_texture_half_float_linear,
  665. OES_texture_stencil8,
  666. OES_texture_storage_multisample_2d_array,
  667. OES_vertex_array_object,
  668. OES_vertex_half_float,
  669. OES_vertex_type_10_10_10_2,
  670. WEBGL_color_buffer_float,
  671. WEBGL_compressed_texture_etc1,
  672. WEBGL_compressed_texture_s3tc,
  673. WEBGL_compressed_texture_pvrtc,
  674. WEBGL_depth_texture,
  675. WEBGL_draw_buffers,
  676. WEBKIT_EXT_texture_filter_anisotropic,
  677. WEBKIT_WEBGL_compressed_texture_s3tc,
  678. WEBKIT_WEBGL_depth_texture,
  679. Count
  680. };
  681. const char* m_name;
  682. bool m_supported;
  683. bool m_initialize;
  684. };
  685. // Extension registry
  686. //
  687. // ANGLE:
  688. // https://github.com/google/angle/tree/master/extensions
  689. //
  690. // CHROMIUM:
  691. // https://chromium.googlesource.com/chromium/src.git/+/refs/heads/git-svn/gpu/GLES2/extensions/CHROMIUM
  692. //
  693. // EGL:
  694. // https://www.khronos.org/registry/egl/extensions/
  695. //
  696. // GL:
  697. // https://www.opengl.org/registry/
  698. //
  699. // GLES:
  700. // https://www.khronos.org/registry/gles/extensions/
  701. //
  702. // WEBGL:
  703. // https://www.khronos.org/registry/webgl/extensions/
  704. //
  705. static Extension s_extension[] =
  706. {
  707. { "AMD_conservative_depth", false, true },
  708. { "AMD_multi_draw_indirect", false, true },
  709. { "ANGLE_depth_texture", false, true },
  710. { "ANGLE_framebuffer_blit", false, true },
  711. { "ANGLE_framebuffer_multisample", false, false },
  712. { "ANGLE_instanced_arrays", false, true },
  713. { "ANGLE_texture_compression_dxt1", false, true },
  714. { "ANGLE_texture_compression_dxt3", false, true },
  715. { "ANGLE_texture_compression_dxt5", false, true },
  716. { "ANGLE_timer_query", false, true },
  717. { "ANGLE_translated_shader_source", false, true },
  718. { "APPLE_texture_format_BGRA8888", false, true },
  719. { "APPLE_texture_max_level", false, true },
  720. { "ARB_clip_control", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  721. { "ARB_compute_shader", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  722. { "ARB_conservative_depth", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  723. { "ARB_copy_image", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  724. { "ARB_debug_label", false, true },
  725. { "ARB_debug_output", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  726. { "ARB_depth_buffer_float", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  727. { "ARB_depth_clamp", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  728. { "ARB_draw_buffers_blend", BGFX_CONFIG_RENDERER_OPENGL >= 40, true },
  729. { "ARB_draw_indirect", BGFX_CONFIG_RENDERER_OPENGL >= 40, true },
  730. { "ARB_draw_instanced", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  731. { "ARB_ES3_compatibility", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  732. { "ARB_framebuffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  733. { "ARB_framebuffer_sRGB", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  734. { "ARB_get_program_binary", BGFX_CONFIG_RENDERER_OPENGL >= 41, true },
  735. { "ARB_half_float_pixel", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  736. { "ARB_half_float_vertex", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  737. { "ARB_indirect_parameters", BGFX_CONFIG_RENDERER_OPENGL >= 46, true },
  738. { "ARB_instanced_arrays", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  739. { "ARB_internalformat_query", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  740. { "ARB_internalformat_query2", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  741. { "ARB_invalidate_subdata", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  742. { "ARB_map_buffer_range", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  743. { "ARB_multi_draw_indirect", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  744. { "ARB_multisample", false, true },
  745. { "ARB_occlusion_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  746. { "ARB_occlusion_query2", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  747. { "ARB_program_interface_query", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  748. { "ARB_provoking_vertex", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  749. { "ARB_sampler_objects", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  750. { "ARB_seamless_cube_map", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  751. { "ARB_shader_bit_encoding", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  752. { "ARB_shader_image_load_store", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  753. { "ARB_shader_storage_buffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  754. { "ARB_shader_texture_lod", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  755. { "ARB_shader_viewport_layer_array", false, true },
  756. { "ARB_texture_compression_bptc", BGFX_CONFIG_RENDERER_OPENGL >= 44, true },
  757. { "ARB_texture_compression_rgtc", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  758. { "ARB_texture_cube_map_array", BGFX_CONFIG_RENDERER_OPENGL >= 40, true },
  759. { "ARB_texture_float", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  760. { "ARB_texture_multisample", BGFX_CONFIG_RENDERER_OPENGL >= 32, true },
  761. { "ARB_texture_rg", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  762. { "ARB_texture_rgb10_a2ui", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  763. { "ARB_texture_stencil8", false, true },
  764. { "ARB_texture_storage", BGFX_CONFIG_RENDERER_OPENGL >= 42, true },
  765. { "ARB_texture_swizzle", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  766. { "ARB_timer_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  767. { "ARB_uniform_buffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 31, true },
  768. { "ARB_vertex_array_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  769. { "ARB_vertex_type_2_10_10_10_rev", false, true },
  770. { "ATI_meminfo", false, true },
  771. { "CHROMIUM_color_buffer_float_rgb", false, true },
  772. { "CHROMIUM_color_buffer_float_rgba", false, true },
  773. { "CHROMIUM_depth_texture", false, true },
  774. { "CHROMIUM_framebuffer_multisample", false, true },
  775. { "CHROMIUM_texture_compression_dxt3", false, true },
  776. { "CHROMIUM_texture_compression_dxt5", false, true },
  777. { "EXT_bgra", false, true },
  778. { "EXT_blend_color", BGFX_CONFIG_RENDERER_OPENGL >= 31, true },
  779. { "EXT_blend_minmax", BGFX_CONFIG_RENDERER_OPENGL >= 14, true },
  780. { "EXT_blend_subtract", BGFX_CONFIG_RENDERER_OPENGL >= 14, true },
  781. { "EXT_color_buffer_half_float", false, true }, // GLES2 extension.
  782. { "EXT_color_buffer_float", false, true }, // GLES2 extension.
  783. { "EXT_copy_image", false, true }, // GLES2 extension.
  784. { "EXT_compressed_ETC1_RGB8_sub_texture", false, true }, // GLES2 extension.
  785. { "EXT_debug_label", false, true },
  786. { "EXT_debug_marker", false, true },
  787. { "EXT_debug_tool", false, true }, // RenderDoc extension.
  788. { "EXT_discard_framebuffer", false, true }, // GLES2 extension.
  789. { "EXT_disjoint_timer_query", false, true }, // GLES2 extension.
  790. { "EXT_draw_buffers", false, true }, // GLES2 extension.
  791. { "EXT_draw_instanced", false, true }, // GLES2 extension.
  792. { "EXT_instanced_arrays", false, true }, // GLES2 extension.
  793. { "EXT_frag_depth", false, true }, // GLES2 extension.
  794. { "EXT_framebuffer_blit", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  795. { "EXT_framebuffer_object", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  796. { "EXT_framebuffer_sRGB", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  797. { "EXT_gpu_shader4", BGFX_CONFIG_RENDERER_OPENGL >= 31, true },
  798. { "EXT_multi_draw_indirect", false, true }, // GLES3.1 extension.
  799. { "EXT_occlusion_query_boolean", false, true }, // GLES2 extension.
  800. { "EXT_packed_float", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  801. { "EXT_read_format_bgra", false, true },
  802. { "EXT_shader_image_load_store", false, true },
  803. { "EXT_shader_texture_lod", false, true }, // GLES2 extension.
  804. { "EXT_shadow_samplers", false, true },
  805. { "EXT_sRGB_write_control", false, true }, // GLES2 extension.
  806. { "EXT_texture_array", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  807. { "EXT_texture_compression_dxt1", false, true },
  808. { "EXT_texture_compression_latc", false, true },
  809. { "EXT_texture_compression_rgtc", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  810. { "EXT_texture_compression_s3tc", false, true },
  811. { "EXT_texture_cube_map_array", false, true }, // GLES3.1 extension.
  812. { "EXT_texture_filter_anisotropic", false, true },
  813. { "EXT_texture_format_BGRA8888", false, true },
  814. { "EXT_texture_rg", false, true }, // GLES2 extension.
  815. { "EXT_texture_shared_exponent", false, true },
  816. { "EXT_texture_snorm", BGFX_CONFIG_RENDERER_OPENGL >= 30, true },
  817. { "EXT_texture_sRGB", false, true },
  818. { "EXT_texture_storage", false, true },
  819. { "EXT_texture_swizzle", false, true },
  820. { "EXT_texture_type_2_10_10_10_REV", false, true },
  821. { "EXT_timer_query", BGFX_CONFIG_RENDERER_OPENGL >= 33, true },
  822. { "EXT_unpack_subimage", false, true },
  823. { "EXT_sRGB", false, true }, // GLES2 extension.
  824. { "EXT_multisampled_render_to_texture", false, true }, // GLES2 extension.
  825. { "GOOGLE_depth_texture", false, true },
  826. { "IMG_multisampled_render_to_texture", false, true },
  827. { "IMG_read_format", false, true },
  828. { "IMG_shader_binary", false, true },
  829. { "IMG_texture_compression_pvrtc", false, true },
  830. { "IMG_texture_compression_pvrtc2", false, true },
  831. { "IMG_texture_format_BGRA8888", false, true },
  832. { "INTEL_fragment_shader_ordering", false, true },
  833. { "KHR_debug", BGFX_CONFIG_RENDERER_OPENGL >= 43, true },
  834. { "KHR_no_error", false, true },
  835. { "MOZ_WEBGL_compressed_texture_s3tc", false, true },
  836. { "MOZ_WEBGL_depth_texture", false, true },
  837. { "NV_conservative_raster", false, true },
  838. { "NV_copy_image", false, true },
  839. { "NV_draw_buffers", false, true }, // GLES2 extension.
  840. { "NV_draw_instanced", false, true }, // GLES2 extension.
  841. { "NV_instanced_arrays", false, true }, // GLES2 extension.
  842. { "NV_occlusion_query", false, true },
  843. { "NV_texture_border_clamp", false, true }, // GLES2 extension.
  844. { "NVX_gpu_memory_info", false, true },
  845. { "OES_copy_image", false, true },
  846. { "OES_compressed_ETC1_RGB8_texture", false, true },
  847. { "OES_depth24", false, true },
  848. { "OES_depth32", false, true },
  849. { "OES_depth_texture", false, true },
  850. { "OES_element_index_uint", false, true },
  851. { "OES_fragment_precision_high", false, true },
  852. { "OES_fbo_render_mipmap", false, true },
  853. { "OES_get_program_binary", false, true },
  854. { "OES_required_internalformat", false, true },
  855. { "OES_packed_depth_stencil", false, true },
  856. { "OES_read_format", false, true },
  857. { "OES_rgb8_rgba8", false, true },
  858. { "OES_standard_derivatives", false, true },
  859. { "OES_texture_3D", false, true },
  860. { "OES_texture_float", false, true },
  861. { "OES_texture_float_linear", false, true },
  862. { "OES_texture_npot", false, true },
  863. { "OES_texture_half_float", false, true },
  864. { "OES_texture_half_float_linear", false, true },
  865. { "OES_texture_stencil8", false, true },
  866. { "OES_texture_storage_multisample_2d_array", false, true },
  867. { "OES_vertex_array_object", false, true },
  868. { "OES_vertex_half_float", false, true },
  869. { "OES_vertex_type_10_10_10_2", false, true },
  870. { "WEBGL_color_buffer_float", false, true },
  871. { "WEBGL_compressed_texture_etc1", false, true },
  872. { "WEBGL_compressed_texture_s3tc", false, true },
  873. { "WEBGL_compressed_texture_pvrtc", false, true },
  874. { "WEBGL_depth_texture", false, true },
  875. { "WEBGL_draw_buffers", false, true },
  876. { "WEBKIT_EXT_texture_filter_anisotropic", false, true },
  877. { "WEBKIT_WEBGL_compressed_texture_s3tc", false, true },
  878. { "WEBKIT_WEBGL_depth_texture", false, true },
  879. };
  880. static_assert(Extension::Count == BX_COUNTOF(s_extension) );
  881. static const char* s_ARB_shader_texture_lod[] =
  882. {
  883. "texture2DLod",
  884. "texture2DArrayLod", // BK - interacts with ARB_texture_array.
  885. "texture2DProjLod",
  886. "texture2DGrad",
  887. "texture2DProjGrad",
  888. "texture3DLod",
  889. "texture3DProjLod",
  890. "texture3DGrad",
  891. "texture3DProjGrad",
  892. "textureCubeLod",
  893. "textureCubeGrad",
  894. "shadow2DLod",
  895. "shadow2DProjLod",
  896. NULL
  897. // "texture1DLod",
  898. // "texture1DProjLod",
  899. // "shadow1DLod",
  900. // "shadow1DProjLod",
  901. };
  902. static const char* s_EXT_shader_texture_lod[] =
  903. {
  904. "texture2DLod",
  905. "texture2DProjLod",
  906. "textureCubeLod",
  907. "texture2DGrad",
  908. "texture2DProjGrad",
  909. "textureCubeGrad",
  910. NULL
  911. };
  912. static const char* s_ARB_shader_viewport_layer_array[] =
  913. {
  914. "gl_ViewportIndex",
  915. "gl_Layer",
  916. NULL
  917. };
  918. static const char* s_EXT_shadow_samplers[] =
  919. {
  920. "shadow2D",
  921. "shadow2DProj",
  922. NULL
  923. };
  924. static const char* s_OES_standard_derivatives[] =
  925. {
  926. "dFdx",
  927. "dFdy",
  928. "fwidth",
  929. NULL
  930. };
  931. static const char* s_uisamplers[] =
  932. {
  933. "isampler2D",
  934. "usampler2D",
  935. "isampler3D",
  936. "usampler3D",
  937. "isamplerCube",
  938. "usamplerCube",
  939. NULL
  940. };
  941. static const char* s_uint[] =
  942. {
  943. "uint",
  944. "uvec2",
  945. "uvec3",
  946. "uvec4",
  947. NULL
  948. };
  949. static const char* s_texelFetch[] =
  950. {
  951. "texture",
  952. "textureLod",
  953. "textureGrad",
  954. "textureProj",
  955. "textureProjLod",
  956. "texelFetch",
  957. "texelFetchOffset",
  958. NULL
  959. };
  960. static const char* s_texture3D[] =
  961. {
  962. "sampler3D",
  963. "sampler3DArray",
  964. NULL
  965. };
  966. static const char* s_textureArray[] =
  967. {
  968. "sampler2DArray",
  969. "sampler2DMSArray",
  970. "samplerCubeArray",
  971. "sampler2DArrayShadow",
  972. NULL
  973. };
  974. static const char* s_ARB_texture_multisample[] =
  975. {
  976. "sampler2DMS",
  977. "isampler2DMS",
  978. "usampler2DMS",
  979. NULL
  980. };
  981. static const char* s_EXT_gpu_shader4[] =
  982. {
  983. "gl_VertexID",
  984. "gl_InstanceID",
  985. "uint",
  986. NULL
  987. };
  988. static const char* s_ARB_gpu_shader5[] =
  989. {
  990. "bitfieldReverse",
  991. "floatBitsToInt",
  992. "floatBitsToUint",
  993. "intBitsToFloat",
  994. "uintBitsToFloat",
  995. NULL
  996. };
  997. static const char* s_ARB_shading_language_packing[] =
  998. {
  999. "packHalf2x16",
  1000. "unpackHalf2x16",
  1001. NULL
  1002. };
  1003. static const char* s_intepolationQualifier[] =
  1004. {
  1005. "flat",
  1006. "smooth",
  1007. "noperspective",
  1008. "centroid",
  1009. NULL
  1010. };
  1011. static void GL_APIENTRY stubVertexAttribDivisor(GLuint /*_index*/, GLuint /*_divisor*/)
  1012. {
  1013. }
  1014. static void GL_APIENTRY stubDrawArraysInstanced(GLenum _mode, GLint _first, GLsizei _count, GLsizei /*_primcount*/)
  1015. {
  1016. GL_CHECK(glDrawArrays(_mode, _first, _count) );
  1017. }
  1018. static void GL_APIENTRY stubDrawElementsInstanced(GLenum _mode, GLsizei _count, GLenum _type, const GLvoid* _indices, GLsizei /*_primcount*/)
  1019. {
  1020. GL_CHECK(glDrawElements(_mode, _count, _type, _indices) );
  1021. }
  1022. static void GL_APIENTRY stubInsertEventMarker(GLsizei /*_length*/, const char* /*_marker*/)
  1023. {
  1024. }
  1025. static void GL_APIENTRY stubPushDebugGroup(GLenum /*_source*/, GLuint /*_id*/, GLsizei /*_length*/, const char* /*_message*/)
  1026. {
  1027. }
  1028. static void GL_APIENTRY stubPopDebugGroup()
  1029. {
  1030. }
  1031. static void GL_APIENTRY stubObjectLabel(GLenum /*_identifier*/, GLuint /*_name*/, GLsizei /*_length*/, const char* /*_label*/)
  1032. {
  1033. }
  1034. static void GL_APIENTRY stubInvalidateFramebuffer(GLenum /*_target*/, GLsizei /*_numAttachments*/, const GLenum* /*_attachments*/)
  1035. {
  1036. }
  1037. static void GL_APIENTRY stubFramebufferTexture(GLenum _target, GLenum _attachment, GLuint _texture, GLint _level)
  1038. {
  1039. GL_CHECK(glFramebufferTextureLayer(_target
  1040. , _attachment
  1041. , _texture
  1042. , _level
  1043. , 0
  1044. ) );
  1045. }
  1046. static void GL_APIENTRY stubMultiDrawArraysIndirect(GLenum _mode, const void* _indirect, GLsizei _drawCount, GLsizei _stride)
  1047. {
  1048. const uint8_t* args = (const uint8_t*)_indirect;
  1049. for (GLsizei ii = 0; ii < _drawCount; ++ii)
  1050. {
  1051. GL_CHECK(glDrawArraysIndirect(_mode, (void*)args) );
  1052. args += _stride;
  1053. }
  1054. }
  1055. static void GL_APIENTRY stubMultiDrawElementsIndirect(GLenum _mode, GLenum _type, const void* _indirect, GLsizei _drawCount, GLsizei _stride)
  1056. {
  1057. const uint8_t* args = (const uint8_t*)_indirect;
  1058. for (GLsizei ii = 0; ii < _drawCount; ++ii)
  1059. {
  1060. GL_CHECK(glDrawElementsIndirect(_mode, _type, (void*)args) );
  1061. args += _stride;
  1062. }
  1063. }
  1064. static void GL_APIENTRY stubPolygonMode(GLenum /*_face*/, GLenum /*_mode*/)
  1065. {
  1066. }
  1067. typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height);
  1068. void flushGlError()
  1069. {
  1070. for (GLenum err = glGetError(); err != 0; err = glGetError() );
  1071. }
  1072. GLenum getGlError()
  1073. {
  1074. GLenum err = glGetError();
  1075. flushGlError();
  1076. return err;
  1077. }
  1078. static const char* getGLString(GLenum _name)
  1079. {
  1080. const char* str = (const char*)glGetString(_name);
  1081. getGlError(); // ignore error if glGetString returns NULL.
  1082. if (NULL != str)
  1083. {
  1084. return str;
  1085. }
  1086. return "<unknown>";
  1087. }
  1088. static uint32_t getGLStringHash(GLenum _name)
  1089. {
  1090. const char* str = (const char*)glGetString(_name);
  1091. getGlError(); // ignore error if glGetString returns NULL.
  1092. if (NULL != str)
  1093. {
  1094. return bx::hash<bx::HashMurmur2A>(str, (uint32_t)bx::strLen(str) );
  1095. }
  1096. return 0;
  1097. }
  1098. void dumpExtensions(const char* _extensions)
  1099. {
  1100. if (NULL != _extensions)
  1101. {
  1102. char name[1024];
  1103. const char* pos = _extensions;
  1104. const char* end = _extensions + bx::strLen(_extensions);
  1105. while (pos < end)
  1106. {
  1107. uint32_t len;
  1108. bx::StringView space = bx::strFind(pos, ' ');
  1109. if (!space.isEmpty() )
  1110. {
  1111. len = bx::uint32_min(sizeof(name), (uint32_t)(space.getPtr() - pos) );
  1112. }
  1113. else
  1114. {
  1115. len = bx::uint32_min(sizeof(name), (uint32_t)bx::strLen(pos) );
  1116. }
  1117. bx::strCopy(name, BX_COUNTOF(name), pos, len);
  1118. name[len] = '\0';
  1119. BX_TRACE("\t%s", name);
  1120. pos += len+1;
  1121. }
  1122. }
  1123. }
  1124. const char* toString(GLenum _enum)
  1125. {
  1126. switch (_enum)
  1127. {
  1128. case GL_DEBUG_SOURCE_API: return "API";
  1129. case GL_DEBUG_SOURCE_WINDOW_SYSTEM: return "WinSys";
  1130. case GL_DEBUG_SOURCE_SHADER_COMPILER: return "Shader";
  1131. case GL_DEBUG_SOURCE_THIRD_PARTY: return "3rdparty";
  1132. case GL_DEBUG_SOURCE_APPLICATION: return "Application";
  1133. case GL_DEBUG_SOURCE_OTHER: return "Other";
  1134. case GL_DEBUG_TYPE_ERROR: return "Error";
  1135. case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: return "Deprecated behavior";
  1136. case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: return "Undefined behavior";
  1137. case GL_DEBUG_TYPE_PORTABILITY: return "Portability";
  1138. case GL_DEBUG_TYPE_PERFORMANCE: return "Performance";
  1139. case GL_DEBUG_TYPE_OTHER: return "Other";
  1140. case GL_DEBUG_SEVERITY_HIGH: return "High";
  1141. case GL_DEBUG_SEVERITY_MEDIUM: return "Medium";
  1142. case GL_DEBUG_SEVERITY_LOW: return "Low";
  1143. case GL_DEBUG_SEVERITY_NOTIFICATION: return "SPAM";
  1144. default:
  1145. break;
  1146. }
  1147. return "<unknown>";
  1148. }
  1149. void GL_APIENTRY debugProcCb(GLenum _source, GLenum _type, GLuint _id, GLenum _severity, GLsizei /*_length*/, const GLchar* _message, const void* /*_userParam*/)
  1150. {
  1151. if (GL_DEBUG_SEVERITY_NOTIFICATION != _severity)
  1152. {
  1153. BX_TRACE("src %s, type %s, id %d, severity %s, '%s'"
  1154. , toString(_source)
  1155. , toString(_type)
  1156. , _id
  1157. , toString(_severity)
  1158. , _message
  1159. );
  1160. BX_UNUSED(_source, _type, _id, _severity, _message);
  1161. }
  1162. }
  1163. GLint glGet(GLenum _pname)
  1164. {
  1165. GLint result = 0;
  1166. glGetIntegerv(_pname, &result);
  1167. GLenum err = getGlError();
  1168. BX_WARN(0 == err, "glGetIntegerv(0x%04x, ...) failed with GL error: 0x%04x.", _pname, err);
  1169. return 0 == err ? result : 0;
  1170. }
  1171. static uint64_t s_currentlyEnabledVertexAttribArrays = 0;
  1172. static uint64_t s_vertexAttribArraysPendingDisable = 0;
  1173. static uint64_t s_vertexAttribArraysPendingEnable = 0;
  1174. void initLazyEnabledVertexAttributes()
  1175. {
  1176. s_currentlyEnabledVertexAttribArrays = 0;
  1177. s_vertexAttribArraysPendingDisable = 0;
  1178. s_vertexAttribArraysPendingEnable = 0;
  1179. }
  1180. void lazyEnableVertexAttribArray(GLuint index)
  1181. {
  1182. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  1183. {
  1184. if (index >= 64)
  1185. {
  1186. // On WebGL platform calling out to WebGL API is detrimental to performance, so optimize
  1187. // out redundant API calls to glEnable/DisableVertexAttribArray.
  1188. GL_CHECK(glEnableVertexAttribArray(index) );
  1189. return;
  1190. }
  1191. const uint64_t mask = UINT64_C(1) << index;
  1192. s_vertexAttribArraysPendingEnable |= mask & (~s_currentlyEnabledVertexAttribArrays);
  1193. s_vertexAttribArraysPendingDisable &= ~mask;
  1194. }
  1195. else
  1196. {
  1197. GL_CHECK(glEnableVertexAttribArray(index) );
  1198. }
  1199. }
  1200. void lazyDisableVertexAttribArray(GLuint index)
  1201. {
  1202. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  1203. {
  1204. if (index >= 64)
  1205. {
  1206. // On WebGL platform calling out to WebGL API is detrimental to performance, so optimize
  1207. // out redundant API calls to glEnable/DisableVertexAttribArray.
  1208. GL_CHECK(glDisableVertexAttribArray(index) );
  1209. return;
  1210. }
  1211. const uint64_t mask = UINT64_C(1) << index;
  1212. s_vertexAttribArraysPendingDisable |= mask & s_currentlyEnabledVertexAttribArrays;
  1213. s_vertexAttribArraysPendingEnable &= ~mask;
  1214. }
  1215. else
  1216. {
  1217. GL_CHECK(glDisableVertexAttribArray(index) );
  1218. }
  1219. }
  1220. void applyLazyEnabledVertexAttributes()
  1221. {
  1222. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  1223. {
  1224. while (s_vertexAttribArraysPendingDisable)
  1225. {
  1226. uint32_t index = bx::countTrailingZeros(s_vertexAttribArraysPendingDisable);
  1227. uint64_t mask = ~(UINT64_C(1) << index);
  1228. s_vertexAttribArraysPendingDisable &= mask;
  1229. s_currentlyEnabledVertexAttribArrays &= mask;
  1230. GL_CHECK(glDisableVertexAttribArray(index) );
  1231. }
  1232. while (s_vertexAttribArraysPendingEnable)
  1233. {
  1234. uint32_t index = bx::countTrailingZeros(s_vertexAttribArraysPendingEnable);
  1235. uint64_t mask = UINT64_C(1) << index;
  1236. s_vertexAttribArraysPendingEnable &= ~mask;
  1237. s_currentlyEnabledVertexAttribArrays |= mask;
  1238. GL_CHECK(glEnableVertexAttribArray(index) );
  1239. }
  1240. }
  1241. }
  1242. void setTextureFormat(TextureFormat::Enum _format, GLenum _internalFmt, GLenum _fmt, GLenum _type = GL_ZERO)
  1243. {
  1244. TextureFormatInfo& tfi = s_textureFormat[_format];
  1245. tfi.m_internalFmt = _internalFmt;
  1246. tfi.m_fmt = _fmt;
  1247. tfi.m_fmtSrgb = _fmt;
  1248. tfi.m_type = _type;
  1249. }
  1250. void setTextureFormatSrgb(TextureFormat::Enum _format, GLenum _internalFmtSrgb, GLenum _fmtSrgb)
  1251. {
  1252. TextureFormatInfo& tfi = s_textureFormat[_format];
  1253. tfi.m_internalFmtSrgb = _internalFmtSrgb;
  1254. tfi.m_fmtSrgb = _fmtSrgb;
  1255. }
  1256. static void texSubImage(
  1257. GLenum _target
  1258. , GLint _level
  1259. , GLint _xoffset
  1260. , GLint _yoffset
  1261. , GLint _zoffset
  1262. , GLsizei _width
  1263. , GLsizei _height
  1264. , GLsizei _depth
  1265. , GLenum _format
  1266. , GLenum _type
  1267. , const GLvoid* _data
  1268. )
  1269. {
  1270. if (NULL == _data)
  1271. {
  1272. return;
  1273. }
  1274. if (_target == GL_TEXTURE_3D
  1275. || _target == GL_TEXTURE_2D_ARRAY
  1276. || _target == GL_TEXTURE_CUBE_MAP_ARRAY)
  1277. {
  1278. glTexSubImage3D(
  1279. _target
  1280. , _level
  1281. , _xoffset
  1282. , _yoffset
  1283. , _zoffset
  1284. , _width
  1285. , _height
  1286. , _depth
  1287. , _format
  1288. , _type
  1289. , _data
  1290. );
  1291. }
  1292. else if (_target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
  1293. {
  1294. }
  1295. else
  1296. {
  1297. BX_UNUSED(_zoffset, _depth);
  1298. glTexSubImage2D(
  1299. _target
  1300. , _level
  1301. , _xoffset
  1302. , _yoffset
  1303. , _width
  1304. , _height
  1305. , _format
  1306. , _type
  1307. , _data
  1308. );
  1309. }
  1310. }
  1311. static void texImage(
  1312. GLenum _target
  1313. , uint32_t _msaaQuality
  1314. , GLint _level
  1315. , GLint _internalFormat
  1316. , GLsizei _width
  1317. , GLsizei _height
  1318. , GLsizei _depth
  1319. , GLint _border
  1320. , GLenum _format
  1321. , GLenum _type
  1322. , const GLvoid* _data
  1323. )
  1324. {
  1325. if (_target == GL_TEXTURE_3D)
  1326. {
  1327. glTexImage3D(
  1328. _target
  1329. , _level
  1330. , _internalFormat
  1331. , _width
  1332. , _height
  1333. , _depth
  1334. , _border
  1335. , _format
  1336. , _type
  1337. , _data
  1338. );
  1339. }
  1340. else if (_target == GL_TEXTURE_2D_ARRAY
  1341. || _target == GL_TEXTURE_CUBE_MAP_ARRAY)
  1342. {
  1343. texSubImage(
  1344. _target
  1345. , _level
  1346. , 0
  1347. , 0
  1348. , _depth
  1349. , _width
  1350. , _height
  1351. , 1
  1352. , _format
  1353. , _type
  1354. , _data
  1355. );
  1356. }
  1357. else if (_target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
  1358. {
  1359. }
  1360. else if (_target == GL_TEXTURE_2D_MULTISAMPLE)
  1361. {
  1362. glTexImage2DMultisample(
  1363. _target
  1364. , _msaaQuality
  1365. , _internalFormat
  1366. , _width
  1367. , _height
  1368. , false
  1369. );
  1370. }
  1371. else
  1372. {
  1373. glTexImage2D(
  1374. _target
  1375. , _level
  1376. , _internalFormat
  1377. , _width
  1378. , _height
  1379. , _border
  1380. , _format
  1381. , _type
  1382. , _data
  1383. );
  1384. }
  1385. BX_UNUSED(_msaaQuality, _depth, _border, _data);
  1386. }
  1387. static void compressedTexSubImage(
  1388. GLenum _target
  1389. , GLint _level
  1390. , GLint _xoffset
  1391. , GLint _yoffset
  1392. , GLint _zoffset
  1393. , GLsizei _width
  1394. , GLsizei _height
  1395. , GLsizei _depth
  1396. , GLenum _format
  1397. , GLsizei _imageSize
  1398. , const GLvoid* _data
  1399. )
  1400. {
  1401. if (_target == GL_TEXTURE_3D
  1402. || _target == GL_TEXTURE_2D_ARRAY)
  1403. {
  1404. glCompressedTexSubImage3D(
  1405. _target
  1406. , _level
  1407. , _xoffset
  1408. , _yoffset
  1409. , _zoffset
  1410. , _width
  1411. , _height
  1412. , _depth
  1413. , _format
  1414. , _imageSize
  1415. , _data
  1416. );
  1417. }
  1418. else
  1419. {
  1420. BX_UNUSED(_zoffset, _depth);
  1421. glCompressedTexSubImage2D(
  1422. _target
  1423. , _level
  1424. , _xoffset
  1425. , _yoffset
  1426. , _width
  1427. , _height
  1428. , _format
  1429. , _imageSize
  1430. , _data
  1431. );
  1432. }
  1433. }
  1434. static void compressedTexImage(
  1435. GLenum _target
  1436. , GLint _level
  1437. , GLenum _internalformat
  1438. , GLsizei _width
  1439. , GLsizei _height
  1440. , GLsizei _depth
  1441. , GLint _border
  1442. , GLsizei _imageSize
  1443. , const GLvoid* _data
  1444. )
  1445. {
  1446. if (_target == GL_TEXTURE_3D)
  1447. {
  1448. glCompressedTexImage3D(
  1449. _target
  1450. , _level
  1451. , _internalformat
  1452. , _width
  1453. , _height
  1454. , _depth
  1455. , _border
  1456. , _imageSize
  1457. , _data
  1458. );
  1459. }
  1460. else if (_target == GL_TEXTURE_2D_ARRAY
  1461. || _target == GL_TEXTURE_CUBE_MAP_ARRAY)
  1462. {
  1463. compressedTexSubImage(
  1464. _target
  1465. , _level
  1466. , 0
  1467. , 0
  1468. , _depth
  1469. , _width
  1470. , _height
  1471. , 1
  1472. , _internalformat
  1473. , _imageSize
  1474. , _data
  1475. );
  1476. }
  1477. else if (_target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
  1478. {
  1479. }
  1480. else
  1481. {
  1482. BX_UNUSED(_depth);
  1483. glCompressedTexImage2D(
  1484. _target
  1485. , _level
  1486. , _internalformat
  1487. , _width
  1488. , _height
  1489. , _border
  1490. , _imageSize
  1491. , _data
  1492. );
  1493. }
  1494. }
  1495. GLenum initTestTexture(TextureFormat::Enum _format, bool _srgb, bool _mipmaps, bool _array, GLsizei _dim)
  1496. {
  1497. const TextureFormatInfo& tfi = s_textureFormat[_format];
  1498. GLenum internalFmt = _srgb
  1499. ? tfi.m_internalFmtSrgb
  1500. : tfi.m_internalFmt
  1501. ;
  1502. GLenum fmt = _srgb
  1503. ? tfi.m_fmtSrgb
  1504. : tfi.m_fmt
  1505. ;
  1506. GLsizei bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(_format) );
  1507. GLsizei size = (_dim*_dim*bpp)/8;
  1508. void* data = NULL;
  1509. if (bimg::isDepth(bimg::TextureFormat::Enum(_format) ) )
  1510. {
  1511. _srgb = false;
  1512. _mipmaps = false;
  1513. _array = false;
  1514. }
  1515. else
  1516. {
  1517. data = bx::alignPtr(BX_STACK_ALLOC(size+16), 0, 16);
  1518. }
  1519. flushGlError();
  1520. GLenum err = 0;
  1521. const GLenum target = _array
  1522. ? GL_TEXTURE_2D_ARRAY
  1523. : GL_TEXTURE_2D
  1524. ;
  1525. if (bimg::isCompressed(bimg::TextureFormat::Enum(_format) ) )
  1526. {
  1527. for (uint32_t ii = 0, dim = _dim; ii < (_mipmaps ? 5u : 1u) && 0 == err; ++ii, dim >>= 1)
  1528. {
  1529. dim = bx::uint32_max(1, dim);
  1530. uint32_t block = bx::uint32_max(4, dim);
  1531. size = (block*block*bpp)/8;
  1532. compressedTexImage(target, ii, internalFmt, dim, dim, 0, 0, size, data);
  1533. err |= getGlError();
  1534. }
  1535. }
  1536. else
  1537. {
  1538. for (uint32_t ii = 0, dim = _dim; ii < (_mipmaps ? 5u : 1u) && 0 == err; ++ii, dim >>= 1)
  1539. {
  1540. dim = bx::uint32_max(1, dim);
  1541. size = (dim*dim*bpp)/8;
  1542. texImage(target, 0, ii, internalFmt, dim, dim, 0, 0, fmt, tfi.m_type, data);
  1543. err |= getGlError();
  1544. }
  1545. }
  1546. return err;
  1547. }
  1548. #if BX_PLATFORM_EMSCRIPTEN
  1549. static bool isTextureFormatValidPerSpec(
  1550. TextureFormat::Enum _format
  1551. , bool _srgb
  1552. , bool _mipAutogen
  1553. )
  1554. {
  1555. // Avoid creating test textures for WebGL, that causes error noise in the browser
  1556. // console; instead examine the supported texture formats from the spec.
  1557. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context();
  1558. EmscriptenWebGLContextAttributes attrs;
  1559. EMSCRIPTEN_CHECK(emscripten_webgl_get_context_attributes(ctx, &attrs) );
  1560. const int glesVersion = attrs.majorVersion + 1;
  1561. switch(_format)
  1562. {
  1563. case TextureFormat::A8:
  1564. case TextureFormat::R8: // Luminance
  1565. case TextureFormat::B5G6R5:
  1566. case TextureFormat::R5G6B5:
  1567. case TextureFormat::BGRA4:
  1568. case TextureFormat::RGBA4:
  1569. case TextureFormat::BGR5A1:
  1570. case TextureFormat::RGB5A1:
  1571. // GLES2 formats without sRGB.
  1572. return !_srgb;
  1573. case TextureFormat::D16:
  1574. // GLES2 formats without sRGB, depth textures do not support mipmaps.
  1575. return !_srgb
  1576. && !_mipAutogen
  1577. ;
  1578. case TextureFormat::R16F:
  1579. case TextureFormat::R32F:
  1580. case TextureFormat::RG8:
  1581. case TextureFormat::RG16F:
  1582. case TextureFormat::RG32F:
  1583. case TextureFormat::RGB10A2:
  1584. case TextureFormat::RG11B10F:
  1585. // GLES3 formats without sRGB
  1586. return !_srgb
  1587. && glesVersion >= 3
  1588. ;
  1589. case TextureFormat::R8I:
  1590. case TextureFormat::R8U:
  1591. case TextureFormat::R16I:
  1592. case TextureFormat::R16U:
  1593. case TextureFormat::R32I:
  1594. case TextureFormat::R32U:
  1595. case TextureFormat::RG8I:
  1596. case TextureFormat::RG8U:
  1597. case TextureFormat::RG16I:
  1598. case TextureFormat::RG16U:
  1599. case TextureFormat::RG32I:
  1600. case TextureFormat::RG32U:
  1601. case TextureFormat::RGB8I:
  1602. case TextureFormat::RGB8U:
  1603. case TextureFormat::RGBA8I:
  1604. case TextureFormat::RGBA8U:
  1605. case TextureFormat::RGBA16I:
  1606. case TextureFormat::RGBA16U:
  1607. case TextureFormat::RGBA32I:
  1608. case TextureFormat::RGBA32U:
  1609. case TextureFormat::D32F:
  1610. case TextureFormat::R8S:
  1611. case TextureFormat::RG8S:
  1612. case TextureFormat::RGB8S:
  1613. case TextureFormat::RGBA8S:
  1614. case TextureFormat::RGB9E5F:
  1615. // GLES3 formats without sRGB that are not texture filterable or color renderable.
  1616. return !_srgb && glesVersion >= 3
  1617. && !_mipAutogen
  1618. ;
  1619. case TextureFormat::D24:
  1620. case TextureFormat::D24S8:
  1621. case TextureFormat::D32:
  1622. // GLES3 formats without sRGB, depth textures do not support mipmaps.
  1623. return !_srgb && !_mipAutogen
  1624. && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "WEBGL_depth_texture") )
  1625. ;
  1626. case TextureFormat::D16F:
  1627. case TextureFormat::D24F:
  1628. // GLES3 depth formats without sRGB, depth textures do not support mipmaps.
  1629. return !_srgb
  1630. && !_mipAutogen
  1631. && glesVersion >= 3
  1632. ;
  1633. case TextureFormat::RGBA16F:
  1634. case TextureFormat::RGBA32F:
  1635. // GLES3 formats without sRGB
  1636. return !_srgb
  1637. && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "OES_texture_half_float") )
  1638. ;
  1639. case TextureFormat::RGB8:
  1640. case TextureFormat::RGBA8:
  1641. // sRGB formats
  1642. return !_srgb
  1643. || glesVersion >= 3
  1644. || emscripten_webgl_enable_extension(ctx, "EXT_sRGB")
  1645. ;
  1646. case TextureFormat::BC1:
  1647. case TextureFormat::BC2:
  1648. case TextureFormat::BC3:
  1649. return emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_s3tc")
  1650. && (!_srgb || emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_s3tc_srgb") )
  1651. ;
  1652. case TextureFormat::PTC12:
  1653. case TextureFormat::PTC14:
  1654. case TextureFormat::PTC12A:
  1655. case TextureFormat::PTC14A:
  1656. return !_srgb
  1657. && emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_pvrtc")
  1658. ;
  1659. case TextureFormat::ETC1:
  1660. return !_srgb
  1661. && emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_etc1")
  1662. ;
  1663. case TextureFormat::ETC2:
  1664. case TextureFormat::ETC2A:
  1665. case TextureFormat::ETC2A1:
  1666. return emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_etc");
  1667. case TextureFormat::ASTC4x4:
  1668. case TextureFormat::ASTC5x5:
  1669. case TextureFormat::ASTC6x6:
  1670. case TextureFormat::ASTC8x5:
  1671. case TextureFormat::ASTC8x6:
  1672. case TextureFormat::ASTC10x5:
  1673. return emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_astc");
  1674. default:
  1675. break;
  1676. }
  1677. return false;
  1678. }
  1679. #endif // BX_PLATFORM_EMSCRIPTEN
  1680. static bool isTextureFormatValid(
  1681. TextureFormat::Enum _format
  1682. , bool _srgb = false
  1683. , bool _mipAutogen = false
  1684. , bool _array = false
  1685. , GLsizei _dim = 16
  1686. )
  1687. {
  1688. #if BX_PLATFORM_EMSCRIPTEN
  1689. // On web platform read the validity of textures based on the available GL context and extensions
  1690. // to avoid developer unfriendly console error noise that would come from probing.
  1691. BX_UNUSED(_array, _dim);
  1692. return isTextureFormatValidPerSpec(_format, _srgb, _mipAutogen);
  1693. #else
  1694. // On other platforms probe the supported textures.
  1695. const TextureFormatInfo& tfi = s_textureFormat[_format];
  1696. GLenum internalFmt = _srgb
  1697. ? tfi.m_internalFmtSrgb
  1698. : tfi.m_internalFmt
  1699. ;
  1700. if (GL_ZERO == internalFmt)
  1701. {
  1702. return false;
  1703. }
  1704. const GLenum target = _array
  1705. ? GL_TEXTURE_2D_ARRAY
  1706. : GL_TEXTURE_2D
  1707. ;
  1708. GLuint id;
  1709. GL_CHECK(glGenTextures(1, &id) );
  1710. GL_CHECK(glBindTexture(target, id) );
  1711. GLenum err = 0;
  1712. if (_array)
  1713. {
  1714. glTexStorage3D(target
  1715. , 1 + GLsizei(bx::ceilLog2( (int32_t)_dim) )
  1716. , internalFmt
  1717. , _dim
  1718. , _dim
  1719. , _dim
  1720. );
  1721. err = getGlError();
  1722. }
  1723. if (0 == err)
  1724. {
  1725. err = initTestTexture(_format, _srgb, _mipAutogen, _array, _dim);
  1726. BX_WARN(0 == err, "TextureFormat::%s %s%s%sis not supported (%x: %s)."
  1727. , getName(_format)
  1728. , _srgb ? "+sRGB " : ""
  1729. , _mipAutogen ? "+mipAutoGen " : ""
  1730. , _array ? "+array " : ""
  1731. , err
  1732. , glEnumName(err)
  1733. );
  1734. if (0 == err
  1735. && _mipAutogen)
  1736. {
  1737. glGenerateMipmap(target);
  1738. err = getGlError();
  1739. }
  1740. }
  1741. GL_CHECK(glDeleteTextures(1, &id) );
  1742. return 0 == err;
  1743. #endif
  1744. }
  1745. static bool isImageFormatValid(TextureFormat::Enum _format, GLsizei _dim = 16)
  1746. {
  1747. if (GL_ZERO == s_imageFormat[_format])
  1748. {
  1749. return false;
  1750. }
  1751. GLuint id;
  1752. GL_CHECK(glGenTextures(1, &id) );
  1753. GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) );
  1754. flushGlError();
  1755. GLenum err = 0;
  1756. glTexStorage2D(GL_TEXTURE_2D, 1, s_imageFormat[_format], _dim, _dim);
  1757. err |= getGlError();
  1758. if (0 == err)
  1759. {
  1760. glBindImageTexture(0
  1761. , id
  1762. , 0
  1763. , GL_FALSE
  1764. , 0
  1765. , GL_READ_WRITE
  1766. , s_imageFormat[_format]
  1767. );
  1768. err |= getGlError();
  1769. }
  1770. GL_CHECK(glDeleteTextures(1, &id) );
  1771. return 0 == err;
  1772. }
  1773. #if BX_PLATFORM_EMSCRIPTEN
  1774. static bool isFramebufferFormatValidPerSpec(
  1775. TextureFormat::Enum _format
  1776. , bool _srgb
  1777. , bool _writeOnly
  1778. )
  1779. {
  1780. // Avoid creating test textures for WebGL, that causes error noise in the browser console; instead examine the supported texture formats from the spec.
  1781. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context();
  1782. EmscriptenWebGLContextAttributes attrs;
  1783. EMSCRIPTEN_CHECK(emscripten_webgl_get_context_attributes(ctx, &attrs) );
  1784. const int glesVersion = attrs.majorVersion + 1;
  1785. switch(_format)
  1786. {
  1787. // GLES2 textures
  1788. case TextureFormat::B5G6R5:
  1789. case TextureFormat::R5G6B5:
  1790. case TextureFormat::BGRA4:
  1791. case TextureFormat::RGBA4:
  1792. case TextureFormat::BGR5A1:
  1793. case TextureFormat::RGB5A1:
  1794. case TextureFormat::D16:
  1795. return !_srgb;
  1796. // GLES2 renderbuffers not a texture in GLES3
  1797. case TextureFormat::D0S8:
  1798. return !_srgb
  1799. && _writeOnly
  1800. ;
  1801. // GLES2 textures that are not renderbuffers
  1802. case TextureFormat::RGB8:
  1803. case TextureFormat::RGBA8:
  1804. return !_srgb
  1805. && (!_writeOnly || glesVersion >= 3)
  1806. ;
  1807. // GLES3 EXT_color_buffer_float renderbuffer formats
  1808. case TextureFormat::R16F:
  1809. case TextureFormat::RG16F:
  1810. case TextureFormat::R32F:
  1811. case TextureFormat::RG32F:
  1812. case TextureFormat::RG11B10F:
  1813. if (_writeOnly)
  1814. {
  1815. return emscripten_webgl_enable_extension(ctx, "EXT_color_buffer_float");
  1816. }
  1817. return !_srgb && glesVersion >= 3;
  1818. // GLES2 float extension:
  1819. case TextureFormat::RGBA16F:
  1820. if (_writeOnly && emscripten_webgl_enable_extension(ctx, "EXT_color_buffer_half_float") )
  1821. {
  1822. return true;
  1823. }
  1824. [[fallthrough]];
  1825. case TextureFormat::RGBA32F:
  1826. if (_writeOnly)
  1827. {
  1828. return emscripten_webgl_enable_extension(ctx, "EXT_color_buffer_float") || emscripten_webgl_enable_extension(ctx, "WEBGL_color_buffer_float");
  1829. }
  1830. // GLES3 formats without sRGB
  1831. return !_srgb
  1832. && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "OES_texture_half_float") )
  1833. ;
  1834. case TextureFormat::D24:
  1835. case TextureFormat::D24S8:
  1836. // GLES3 formats without sRGB, depth textures do not support mipmaps.
  1837. return !_srgb
  1838. && (glesVersion >= 3 || (!_writeOnly && emscripten_webgl_enable_extension(ctx, "WEBGL_depth_texture") ) )
  1839. ;
  1840. case TextureFormat::D32:
  1841. // GLES3 formats without sRGB, depth textures do not support mipmaps.
  1842. return !_srgb
  1843. && !_writeOnly
  1844. && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "WEBGL_depth_texture") )
  1845. ;
  1846. // GLES3 textures
  1847. case TextureFormat::R8:
  1848. case TextureFormat::RG8:
  1849. case TextureFormat::R8I:
  1850. case TextureFormat::R8U:
  1851. case TextureFormat::R16I:
  1852. case TextureFormat::R16U:
  1853. case TextureFormat::R32I:
  1854. case TextureFormat::R32U:
  1855. case TextureFormat::RG8I:
  1856. case TextureFormat::RG8U:
  1857. case TextureFormat::RGBA8I:
  1858. case TextureFormat::RGBA8U:
  1859. case TextureFormat::RG16I:
  1860. case TextureFormat::RG16U:
  1861. case TextureFormat::RG32I:
  1862. case TextureFormat::RG32U:
  1863. case TextureFormat::RGBA16I:
  1864. case TextureFormat::RGBA16U:
  1865. case TextureFormat::RGBA32I:
  1866. case TextureFormat::RGBA32U:
  1867. case TextureFormat::RGB10A2:
  1868. case TextureFormat::D16F:
  1869. case TextureFormat::D24F:
  1870. case TextureFormat::D32F:
  1871. return !_srgb
  1872. && glesVersion >= 3
  1873. ;
  1874. case TextureFormat::BGRA8:
  1875. return !_srgb
  1876. && _writeOnly
  1877. && glesVersion >= 3
  1878. ;
  1879. default:
  1880. break;
  1881. }
  1882. return false;
  1883. }
  1884. #endif
  1885. static bool isFramebufferFormatValid(
  1886. TextureFormat::Enum _format
  1887. , bool _srgb = false
  1888. , bool _writeOnly = false
  1889. , GLsizei _dim = 16
  1890. )
  1891. {
  1892. #if BX_PLATFORM_EMSCRIPTEN
  1893. // On web platform read the validity of framebuffers based on the available GL context and extensions
  1894. // to avoid developer unfriendly console error noise that would come from probing.
  1895. BX_UNUSED(_dim);
  1896. return isFramebufferFormatValidPerSpec(_format, _srgb, _writeOnly);
  1897. #else
  1898. // On other platforms probe the supported textures.
  1899. const TextureFormatInfo& tfi = s_textureFormat[_format];
  1900. GLenum internalFmt = _srgb
  1901. ? tfi.m_internalFmtSrgb
  1902. : tfi.m_internalFmt
  1903. ;
  1904. if (GL_ZERO == internalFmt)
  1905. {
  1906. return false;
  1907. }
  1908. if (_writeOnly)
  1909. {
  1910. GLuint rbo;
  1911. glGenRenderbuffers(1, &rbo);
  1912. glBindRenderbuffer(GL_RENDERBUFFER, rbo);
  1913. glRenderbufferStorage(GL_RENDERBUFFER
  1914. , s_rboFormat[_format]
  1915. , _dim
  1916. , _dim
  1917. );
  1918. glBindRenderbuffer(GL_RENDERBUFFER, 0);
  1919. glDeleteRenderbuffers(1, &rbo);
  1920. GLenum err = getGlError();
  1921. return 0 == err;
  1922. }
  1923. GLuint fbo;
  1924. GL_CHECK(glGenFramebuffers(1, &fbo) );
  1925. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, fbo) );
  1926. GLuint id;
  1927. GL_CHECK(glGenTextures(1, &id) );
  1928. GL_CHECK(glBindTexture(GL_TEXTURE_2D, id) );
  1929. GLenum err = initTestTexture(_format, _srgb, false, false, _dim);
  1930. GLenum attachment;
  1931. if (bimg::isDepth(bimg::TextureFormat::Enum(_format) ) )
  1932. {
  1933. const bimg::ImageBlockInfo& info = bimg::getBlockInfo(bimg::TextureFormat::Enum(_format) );
  1934. if (0 == info.depthBits)
  1935. {
  1936. attachment = GL_STENCIL_ATTACHMENT;
  1937. }
  1938. else if (0 == info.stencilBits)
  1939. {
  1940. attachment = GL_DEPTH_ATTACHMENT;
  1941. }
  1942. else
  1943. {
  1944. attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  1945. }
  1946. }
  1947. else
  1948. {
  1949. attachment = GL_COLOR_ATTACHMENT0;
  1950. }
  1951. glFramebufferTexture2D(GL_FRAMEBUFFER
  1952. , attachment
  1953. , GL_TEXTURE_2D
  1954. , id
  1955. , 0
  1956. );
  1957. err = getGlError();
  1958. if (0 == err)
  1959. {
  1960. err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  1961. }
  1962. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  1963. GL_CHECK(glDeleteFramebuffers(1, &fbo) );
  1964. GL_CHECK(glDeleteTextures(1, &id) );
  1965. return GL_FRAMEBUFFER_COMPLETE == err;
  1966. #endif
  1967. }
  1968. static void getFilters(uint32_t _flags, bool _hasMips, GLenum& _magFilter, GLenum& _minFilter)
  1969. {
  1970. const uint32_t mag = (_flags&BGFX_SAMPLER_MAG_MASK)>>BGFX_SAMPLER_MAG_SHIFT;
  1971. const uint32_t min = (_flags&BGFX_SAMPLER_MIN_MASK)>>BGFX_SAMPLER_MIN_SHIFT;
  1972. const uint32_t mip = (_flags&BGFX_SAMPLER_MIP_MASK)>>BGFX_SAMPLER_MIP_SHIFT;
  1973. _magFilter = s_textureFilterMag[mag];
  1974. _minFilter = s_textureFilterMin[min][_hasMips ? mip+1 : 0];
  1975. }
  1976. void updateExtension(const bx::StringView& _name)
  1977. {
  1978. bx::StringView ext(_name);
  1979. if (0 == bx::strCmp(ext, "GL_", 3) ) // skip GL_
  1980. {
  1981. ext.set(ext.getPtr()+3, ext.getTerm() );
  1982. }
  1983. bool supported = false;
  1984. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  1985. {
  1986. Extension& extension = s_extension[ii];
  1987. if (!extension.m_supported
  1988. && extension.m_initialize)
  1989. {
  1990. if (0 == bx::strCmp(ext, extension.m_name) )
  1991. {
  1992. #if BX_PLATFORM_EMSCRIPTEN
  1993. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context();
  1994. supported = emscripten_webgl_enable_extension(ctx, extension.m_name);
  1995. #else
  1996. supported = true;
  1997. #endif
  1998. extension.m_supported = supported;
  1999. break;
  2000. }
  2001. }
  2002. }
  2003. BX_TRACE("GL_EXTENSION %s: %.*s", supported ? " (supported)" : "", _name.getLength(), _name.getPtr() );
  2004. BX_UNUSED(supported);
  2005. }
  2006. struct VendorId
  2007. {
  2008. const char* name;
  2009. uint16_t id;
  2010. };
  2011. static const VendorId s_vendorIds[] =
  2012. {
  2013. { "NVIDIA Corporation", BGFX_PCI_ID_NVIDIA },
  2014. { "Advanced Micro Devices, Inc.", BGFX_PCI_ID_AMD },
  2015. { "Intel", BGFX_PCI_ID_INTEL },
  2016. { "ATI Technologies Inc.", BGFX_PCI_ID_AMD },
  2017. { "ARM", BGFX_PCI_ID_ARM },
  2018. };
  2019. struct Workaround
  2020. {
  2021. void reset()
  2022. {
  2023. m_detachShader = true;
  2024. }
  2025. bool m_detachShader;
  2026. };
  2027. struct RendererContextGL : public RendererContextI
  2028. {
  2029. RendererContextGL()
  2030. : m_numWindows(1)
  2031. , m_rtMsaa(false)
  2032. , m_fbDiscard(BGFX_CLEAR_NONE)
  2033. , m_capture(NULL)
  2034. , m_captureSize(0)
  2035. , m_maxAnisotropy(0.0f)
  2036. , m_maxAnisotropyDefault(0.0f)
  2037. , m_maxMsaa(0)
  2038. , m_vao(0)
  2039. , m_blitSupported(false)
  2040. , m_readBackSupported(BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  2041. , m_vaoSupport(false)
  2042. , m_samplerObjectSupport(false)
  2043. , m_shadowSamplersSupport(false)
  2044. , m_srgbWriteControlSupport(BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  2045. , m_borderColorSupport(BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  2046. , m_programBinarySupport(false)
  2047. , m_textureSwizzleSupport(false)
  2048. , m_depthTextureSupport(false)
  2049. , m_timerQuerySupport(false)
  2050. , m_occlusionQuerySupport(false)
  2051. , m_atocSupport(false)
  2052. , m_conservativeRasterSupport(false)
  2053. , m_flip(false)
  2054. , m_hash( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT)
  2055. , m_backBufferFbo(0)
  2056. , m_msaaBackBufferFbo(0)
  2057. , m_msaaBlitProgram(0)
  2058. , m_clearQuadColor(BGFX_INVALID_HANDLE)
  2059. , m_clearQuadDepth(BGFX_INVALID_HANDLE)
  2060. {
  2061. bx::memSet(m_msaaBackBufferRbos, 0, sizeof(m_msaaBackBufferRbos) );
  2062. }
  2063. ~RendererContextGL()
  2064. {
  2065. }
  2066. bool init(const Init& _init)
  2067. {
  2068. struct ErrorState
  2069. {
  2070. enum Enum
  2071. {
  2072. Default,
  2073. };
  2074. };
  2075. ErrorState::Enum errorState = ErrorState::Default;
  2076. initLazyEnabledVertexAttributes();
  2077. if (_init.debug
  2078. || _init.profile)
  2079. {
  2080. m_renderdocdll = loadRenderDoc();
  2081. }
  2082. m_fbh.idx = kInvalidHandle;
  2083. bx::memSet(m_uniforms, 0, sizeof(m_uniforms) );
  2084. bx::memSet(&m_resolution, 0, sizeof(m_resolution) );
  2085. setRenderContextSize(_init.resolution.width, _init.resolution.height, _init.resolution.reset);
  2086. m_vendor = getGLString(GL_VENDOR);
  2087. m_renderer = getGLString(GL_RENDERER);
  2088. m_version = getGLString(GL_VERSION);
  2089. m_glslVersion = getGLString(GL_SHADING_LANGUAGE_VERSION);
  2090. {
  2091. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  2092. {
  2093. int32_t majorGlVersion = 0;
  2094. int32_t minorGlVersion = 0;
  2095. const char* version = m_version;
  2096. while (*version && !bx::isNumeric(*version) )
  2097. {
  2098. ++version;
  2099. }
  2100. bx::fromString(&majorGlVersion, version);
  2101. bx::fromString(&minorGlVersion, version + 2);
  2102. int32_t glVersion = majorGlVersion*10 + minorGlVersion;
  2103. BX_TRACE("WebGL context version %d (%d.%d).", glVersion, majorGlVersion, minorGlVersion);
  2104. m_gles3 = glVersion >= 30;
  2105. }
  2106. else
  2107. {
  2108. m_gles3 = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30);
  2109. }
  2110. }
  2111. for (uint32_t ii = 0; ii < BX_COUNTOF(s_vendorIds); ++ii)
  2112. {
  2113. const VendorId& vendorId = s_vendorIds[ii];
  2114. if (0 == bx::strCmp(vendorId.name, m_vendor, bx::strLen(vendorId.name) ) )
  2115. {
  2116. g_caps.vendorId = vendorId.id;
  2117. break;
  2118. }
  2119. }
  2120. m_workaround.reset();
  2121. GLint numCmpFormats = 0;
  2122. GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &numCmpFormats) );
  2123. BX_TRACE("GL_NUM_COMPRESSED_TEXTURE_FORMATS %d", numCmpFormats);
  2124. GLint* cmpFormat = NULL;
  2125. if (0 < numCmpFormats)
  2126. {
  2127. numCmpFormats = numCmpFormats > 256 ? 256 : numCmpFormats;
  2128. cmpFormat = (GLint*)BX_STACK_ALLOC(sizeof(GLint)*numCmpFormats);
  2129. GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, cmpFormat) );
  2130. for (GLint ii = 0; ii < numCmpFormats; ++ii)
  2131. {
  2132. GLint internalFmt = cmpFormat[ii];
  2133. uint32_t fmt = uint32_t(TextureFormat::Unknown);
  2134. for (uint32_t jj = 0; jj < fmt; ++jj)
  2135. {
  2136. if (s_textureFormat[jj].m_internalFmt == (GLenum)internalFmt)
  2137. {
  2138. s_textureFormat[jj].m_supported = true;
  2139. fmt = jj;
  2140. }
  2141. }
  2142. BX_TRACE(" %3d: %8x %s", ii, internalFmt, getName( (TextureFormat::Enum)fmt) );
  2143. }
  2144. }
  2145. if (BX_ENABLED(BGFX_CONFIG_DEBUG) )
  2146. {
  2147. #define GL_GET(_pname, _min) BX_TRACE(" " #_pname " %d (min: %d)", glGet(_pname), _min)
  2148. BX_TRACE("Defaults:");
  2149. #if BGFX_CONFIG_RENDERER_OPENGL >= 41 || BGFX_CONFIG_RENDERER_OPENGLES
  2150. GL_GET(GL_MAX_FRAGMENT_UNIFORM_VECTORS, 16);
  2151. GL_GET(GL_MAX_VERTEX_UNIFORM_VECTORS, 128);
  2152. GL_GET(GL_MAX_VARYING_VECTORS, 8);
  2153. #else
  2154. GL_GET(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, 16 * 4);
  2155. GL_GET(GL_MAX_VERTEX_UNIFORM_COMPONENTS, 128 * 4);
  2156. GL_GET(GL_MAX_VARYING_FLOATS, 8 * 4);
  2157. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 41 || BGFX_CONFIG_RENDERER_OPENGLES
  2158. GL_GET(GL_MAX_VERTEX_ATTRIBS, 8);
  2159. GL_GET(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, 8);
  2160. GL_GET(GL_MAX_CUBE_MAP_TEXTURE_SIZE, 16);
  2161. GL_GET(GL_MAX_TEXTURE_IMAGE_UNITS, 8);
  2162. GL_GET(GL_MAX_TEXTURE_SIZE, 64);
  2163. GL_GET(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, 0);
  2164. GL_GET(GL_MAX_RENDERBUFFER_SIZE, 1);
  2165. GL_GET(GL_MAX_COLOR_ATTACHMENTS, 1);
  2166. GL_GET(GL_MAX_DRAW_BUFFERS, 1);
  2167. #undef GL_GET
  2168. BX_TRACE(" Vendor: %s", m_vendor);
  2169. BX_TRACE(" Renderer: %s", m_renderer);
  2170. BX_TRACE(" Version: %s", m_version);
  2171. BX_TRACE("GLSL version: %s", m_glslVersion);
  2172. }
  2173. // Initial binary shader hash depends on driver version.
  2174. m_hash = ( (BX_PLATFORM_WINDOWS<<1) | BX_ARCH_64BIT)
  2175. ^ (uint64_t(getGLStringHash(GL_VENDOR ) )<<32)
  2176. ^ (uint64_t(getGLStringHash(GL_RENDERER) )<<0 )
  2177. ^ (uint64_t(getGLStringHash(GL_VERSION ) )<<16)
  2178. ;
  2179. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  2180. && 0 == bx::strCmp(m_vendor, "Imagination Technologies")
  2181. && !bx::strFind(m_version, "(SDK 3.5@3510720)").isEmpty() )
  2182. {
  2183. // Skip initializing extensions that are broken in emulator.
  2184. s_extension[Extension::ARB_program_interface_query ].m_initialize =
  2185. s_extension[Extension::ARB_shader_storage_buffer_object].m_initialize = false;
  2186. }
  2187. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES)
  2188. && 0 == bx::strCmp(m_vendor, "Imagination Technologies")
  2189. && !bx::strFind(m_version, "1.8@905891").isEmpty() )
  2190. {
  2191. m_workaround.m_detachShader = false;
  2192. }
  2193. if (BX_ENABLED(BGFX_CONFIG_RENDERER_USE_EXTENSIONS) )
  2194. {
  2195. const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
  2196. getGlError(); // ignore error if glGetString returns NULL.
  2197. if (NULL != extensions)
  2198. {
  2199. bx::StringView ext(extensions);
  2200. while (!ext.isEmpty() )
  2201. {
  2202. const bx::StringView space = bx::strFind(ext, ' ');
  2203. const bx::StringView token = bx::StringView(ext.getPtr(), space.getPtr() );
  2204. updateExtension(token);
  2205. ext.set(space.getPtr() + (space.isEmpty() ? 0 : 1), ext.getTerm() );
  2206. }
  2207. }
  2208. else if (NULL != glGetStringi)
  2209. {
  2210. GLint numExtensions = 0;
  2211. glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
  2212. getGlError(); // ignore error if glGetIntegerv returns NULL.
  2213. for (GLint index = 0; index < numExtensions; ++index)
  2214. {
  2215. const char* name = (const char*)glGetStringi(GL_EXTENSIONS, index);
  2216. updateExtension(name);
  2217. }
  2218. }
  2219. BX_TRACE("Supported extensions:");
  2220. for (uint32_t ii = 0; ii < Extension::Count; ++ii)
  2221. {
  2222. if (s_extension[ii].m_supported)
  2223. {
  2224. BX_TRACE("\t%2d: %s", ii, s_extension[ii].m_name);
  2225. }
  2226. }
  2227. }
  2228. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  2229. && !s_extension[Extension::ARB_framebuffer_object].m_supported)
  2230. {
  2231. BX_TRACE("Init error: ARB_framebuffer_object not supported.");
  2232. goto error;
  2233. }
  2234. {
  2235. // Allow all texture filters.
  2236. bx::memSet(s_textureFilter, true, BX_COUNTOF(s_textureFilter) );
  2237. bool bc123Supported = 0
  2238. || s_extension[Extension::EXT_texture_compression_s3tc ].m_supported
  2239. || s_extension[Extension::MOZ_WEBGL_compressed_texture_s3tc ].m_supported
  2240. || s_extension[Extension::WEBGL_compressed_texture_s3tc ].m_supported
  2241. || s_extension[Extension::WEBKIT_WEBGL_compressed_texture_s3tc].m_supported
  2242. ;
  2243. s_textureFormat[TextureFormat::BC1].m_supported |= bc123Supported
  2244. || s_extension[Extension::ANGLE_texture_compression_dxt1].m_supported
  2245. || s_extension[Extension::EXT_texture_compression_dxt1 ].m_supported
  2246. ;
  2247. if (!s_textureFormat[TextureFormat::BC1].m_supported
  2248. && ( s_textureFormat[TextureFormat::BC2].m_supported || s_textureFormat[TextureFormat::BC3].m_supported) )
  2249. {
  2250. // If RGBA_S3TC_DXT1 is not supported, maybe RGB_S3TC_DXT1 is?
  2251. for (GLint ii = 0; ii < numCmpFormats; ++ii)
  2252. {
  2253. if (GL_COMPRESSED_RGB_S3TC_DXT1_EXT == cmpFormat[ii])
  2254. {
  2255. setTextureFormat(TextureFormat::BC1, GL_COMPRESSED_RGB_S3TC_DXT1_EXT, GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
  2256. s_textureFormat[TextureFormat::BC1].m_supported = true;
  2257. break;
  2258. }
  2259. }
  2260. }
  2261. s_textureFormat[TextureFormat::BC2].m_supported |= bc123Supported
  2262. || s_extension[Extension::ANGLE_texture_compression_dxt3 ].m_supported
  2263. || s_extension[Extension::CHROMIUM_texture_compression_dxt3].m_supported
  2264. ;
  2265. s_textureFormat[TextureFormat::BC3].m_supported |= bc123Supported
  2266. || s_extension[Extension::ANGLE_texture_compression_dxt5 ].m_supported
  2267. || s_extension[Extension::CHROMIUM_texture_compression_dxt5].m_supported
  2268. ;
  2269. if (s_extension[Extension::EXT_texture_compression_latc].m_supported)
  2270. {
  2271. setTextureFormat(TextureFormat::BC4, GL_COMPRESSED_LUMINANCE_LATC1_EXT, GL_COMPRESSED_LUMINANCE_LATC1_EXT);
  2272. setTextureFormat(TextureFormat::BC5, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT);
  2273. }
  2274. if (s_extension[Extension::ARB_texture_compression_rgtc].m_supported
  2275. || s_extension[Extension::EXT_texture_compression_rgtc].m_supported)
  2276. {
  2277. setTextureFormat(TextureFormat::BC4, GL_COMPRESSED_RED_RGTC1, GL_COMPRESSED_RED_RGTC1);
  2278. setTextureFormat(TextureFormat::BC5, GL_COMPRESSED_RG_RGTC2, GL_COMPRESSED_RG_RGTC2);
  2279. }
  2280. bool etc1Supported = 0
  2281. || s_extension[Extension::OES_compressed_ETC1_RGB8_texture].m_supported
  2282. || s_extension[Extension::WEBGL_compressed_texture_etc1 ].m_supported
  2283. ;
  2284. s_textureFormat[TextureFormat::ETC1].m_supported |= etc1Supported;
  2285. bool etc2Supported = !!(BGFX_CONFIG_RENDERER_OPENGLES >= 30)
  2286. || s_extension[Extension::ARB_ES3_compatibility].m_supported
  2287. ;
  2288. s_textureFormat[TextureFormat::ETC2 ].m_supported |= etc2Supported;
  2289. s_textureFormat[TextureFormat::ETC2A ].m_supported |= etc2Supported;
  2290. s_textureFormat[TextureFormat::ETC2A1].m_supported |= etc2Supported;
  2291. if (!s_textureFormat[TextureFormat::ETC1].m_supported
  2292. && s_textureFormat[TextureFormat::ETC2].m_supported)
  2293. {
  2294. // When ETC2 is supported override ETC1 texture format settings.
  2295. s_textureFormat[TextureFormat::ETC1].m_internalFmt = GL_COMPRESSED_RGB8_ETC2;
  2296. s_textureFormat[TextureFormat::ETC1].m_fmt = GL_COMPRESSED_RGB8_ETC2;
  2297. s_textureFormat[TextureFormat::ETC1].m_supported = true;
  2298. }
  2299. bool ptc1Supported = 0
  2300. || s_extension[Extension::IMG_texture_compression_pvrtc ].m_supported
  2301. || s_extension[Extension::WEBGL_compressed_texture_pvrtc].m_supported
  2302. ;
  2303. s_textureFormat[TextureFormat::PTC12 ].m_supported |= ptc1Supported;
  2304. s_textureFormat[TextureFormat::PTC14 ].m_supported |= ptc1Supported;
  2305. s_textureFormat[TextureFormat::PTC12A].m_supported |= ptc1Supported;
  2306. s_textureFormat[TextureFormat::PTC14A].m_supported |= ptc1Supported;
  2307. bool ptc2Supported = s_extension[Extension::IMG_texture_compression_pvrtc2].m_supported;
  2308. s_textureFormat[TextureFormat::PTC22].m_supported |= ptc2Supported;
  2309. s_textureFormat[TextureFormat::PTC24].m_supported |= ptc2Supported;
  2310. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) )
  2311. {
  2312. if (m_gles3)
  2313. {
  2314. setTextureFormat(TextureFormat::R16F, GL_R16F, GL_RED, 0x140B /* == GL_HALF_FLOAT, but bgfx overwrites it globally with GL_HALF_FLOAT_OES */);
  2315. setTextureFormat(TextureFormat::RG16F, GL_RG16F, GL_RG, 0x140B /* == GL_HALF_FLOAT, but bgfx overwrites it globally with GL_HALF_FLOAT_OES */);
  2316. setTextureFormat(TextureFormat::RGBA16F, GL_RGBA16F, GL_RGBA, 0x140B /* == GL_HALF_FLOAT, but bgfx overwrites it globally with GL_HALF_FLOAT_OES */);
  2317. }
  2318. else
  2319. {
  2320. setTextureFormat(TextureFormat::RGBA16F, GL_RGBA, GL_RGBA, GL_HALF_FLOAT); // Note: this is actually GL_HALF_FLOAT_OES and not GL_HALF_FLOAT if compiling for GLES target.
  2321. setTextureFormat(TextureFormat::RGBA32F, GL_RGBA, GL_RGBA, GL_FLOAT);
  2322. // internalFormat and format must match:
  2323. // https://www.khronos.org/opengles/sdk/docs/man/xhtml/glTexImage2D.xml
  2324. setTextureFormat(TextureFormat::RGBA8, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE);
  2325. setTextureFormat(TextureFormat::B5G6R5, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV);
  2326. setTextureFormat(TextureFormat::R5G6B5, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV);
  2327. setTextureFormat(TextureFormat::BGRA4, GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_4_4_4_4_REV);
  2328. setTextureFormat(TextureFormat::RGBA4, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4_REV);
  2329. setTextureFormat(TextureFormat::BGR5A1, GL_BGRA, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV);
  2330. setTextureFormat(TextureFormat::RGB5A1, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_1_5_5_5_REV);
  2331. if (s_extension[Extension::EXT_sRGB].m_supported)
  2332. {
  2333. setTextureFormatSrgb(TextureFormat::RGBA8, GL_SRGB_ALPHA_EXT, GL_SRGB_ALPHA_EXT);
  2334. setTextureFormatSrgb(TextureFormat::RGB8, GL_SRGB_EXT, GL_SRGB_EXT);
  2335. }
  2336. if (s_extension[Extension::EXT_texture_swizzle].m_supported)
  2337. {
  2338. s_textureFormat[TextureFormat::R5G6B5].m_mapping[0] = GL_BLUE;
  2339. s_textureFormat[TextureFormat::R5G6B5].m_mapping[2] = GL_RED;
  2340. }
  2341. if (s_extension[Extension::OES_texture_half_float].m_supported
  2342. || s_extension[Extension::OES_texture_float ].m_supported)
  2343. {
  2344. // https://www.khronos.org/registry/gles/extensions/OES/OES_texture_float.txt
  2345. // When half/float is available via extensions texture will be marked as
  2346. // incomplete if it uses anything other than nearest filter.
  2347. const bool linear16F = s_extension[Extension::OES_texture_half_float_linear].m_supported;
  2348. const bool linear32F = s_extension[Extension::OES_texture_float_linear ].m_supported;
  2349. s_textureFilter[TextureFormat::R16F] = linear16F;
  2350. s_textureFilter[TextureFormat::RG16F] = linear16F;
  2351. s_textureFilter[TextureFormat::RGBA16F] = linear16F;
  2352. s_textureFilter[TextureFormat::R32F] = linear32F;
  2353. s_textureFilter[TextureFormat::RG32F] = linear32F;
  2354. s_textureFilter[TextureFormat::RGBA32F] = linear32F;
  2355. }
  2356. }
  2357. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN)
  2358. && (s_extension[Extension::WEBGL_depth_texture].m_supported
  2359. || s_extension[Extension::MOZ_WEBGL_depth_texture].m_supported
  2360. || s_extension[Extension::WEBKIT_WEBGL_depth_texture].m_supported) )
  2361. {
  2362. setTextureFormat(TextureFormat::D16, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT);
  2363. setTextureFormat(TextureFormat::D24, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT); // N.b. OpenGL ES does not guarantee that there are 24 bits available here, could be 16. See https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/
  2364. setTextureFormat(TextureFormat::D32, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT); // N.b. same as above.
  2365. setTextureFormat(TextureFormat::D24S8, GL_DEPTH_STENCIL, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
  2366. }
  2367. // OpenGL ES 3.0 depth formats.
  2368. if (m_gles3)
  2369. {
  2370. setTextureFormat(TextureFormat::D16, GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT);
  2371. setTextureFormat(TextureFormat::D24, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
  2372. setTextureFormat(TextureFormat::D32, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
  2373. setTextureFormat(TextureFormat::D24S8, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8);
  2374. setTextureFormat(TextureFormat::D16F, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT); // GLES 3.0 does not have D16F, overshoot to D32F
  2375. setTextureFormat(TextureFormat::D24F, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT); // GLES 3.0 does not have D24F, overshoot to D32F
  2376. setTextureFormat(TextureFormat::D32F, GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT);
  2377. setTextureFormat(TextureFormat::D0S8, GL_STENCIL_INDEX8, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE); // Only works as renderbuffer, not as texture
  2378. }
  2379. }
  2380. else
  2381. {
  2382. setTextureFormat(TextureFormat::R5G6B5, GL_BGR, GL_BGR, GL_UNSIGNED_SHORT_5_6_5);
  2383. setTextureFormatSrgb(TextureFormat::R5G6B5, GL_ZERO, GL_BGR);
  2384. }
  2385. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  2386. || m_gles3)
  2387. {
  2388. setTextureFormat(TextureFormat::R8I, GL_R8I, GL_RED_INTEGER, GL_BYTE);
  2389. setTextureFormat(TextureFormat::R8U, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE);
  2390. setTextureFormat(TextureFormat::R16I, GL_R16I, GL_RED_INTEGER, GL_SHORT);
  2391. setTextureFormat(TextureFormat::R16U, GL_R16UI, GL_RED_INTEGER, GL_UNSIGNED_SHORT);
  2392. // setTextureFormat(TextureFormat::RG16, GL_RG16UI, GL_RG_INTEGER, GL_UNSIGNED_SHORT);
  2393. // setTextureFormat(TextureFormat::RGBA16, GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT);
  2394. setTextureFormat(TextureFormat::R32U, GL_R32UI, GL_RED_INTEGER, GL_UNSIGNED_INT);
  2395. setTextureFormat(TextureFormat::RG32U, GL_RG32UI, GL_RG_INTEGER, GL_UNSIGNED_INT);
  2396. setTextureFormat(TextureFormat::RGBA32U, GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT);
  2397. }
  2398. if (s_extension[Extension::EXT_texture_format_BGRA8888 ].m_supported
  2399. || s_extension[Extension::EXT_bgra ].m_supported
  2400. || s_extension[Extension::IMG_texture_format_BGRA8888 ].m_supported
  2401. || s_extension[Extension::APPLE_texture_format_BGRA8888].m_supported)
  2402. {
  2403. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  2404. {
  2405. m_readPixelsFmt = GL_BGRA;
  2406. }
  2407. // Mixing GLES and GL extensions here. OpenGL EXT_bgra and
  2408. // APPLE_texture_format_BGRA8888 wants
  2409. // format to be BGRA but internal format to stay RGBA, but
  2410. // EXT_texture_format_BGRA8888 wants both format and internal
  2411. // format to be BGRA.
  2412. //
  2413. // Reference(s):
  2414. // - https://web.archive.org/web/20181126035829/https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_texture_format_BGRA8888.txt
  2415. // - https://web.archive.org/web/20181126035841/https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_bgra.txt
  2416. // - https://web.archive.org/web/20181126035851/https://www.khronos.org/registry/OpenGL/extensions/APPLE/APPLE_texture_format_BGRA8888.txt
  2417. //
  2418. if (!s_extension[Extension::EXT_bgra ].m_supported
  2419. && !s_extension[Extension::APPLE_texture_format_BGRA8888].m_supported)
  2420. {
  2421. s_textureFormat[TextureFormat::BGRA8].m_internalFmt = GL_BGRA;
  2422. }
  2423. if (!isTextureFormatValid(TextureFormat::BGRA8) )
  2424. {
  2425. // Revert back to RGBA if texture can't be created.
  2426. setTextureFormat(TextureFormat::BGRA8, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE);
  2427. }
  2428. }
  2429. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  2430. {
  2431. // OpenGL ES does not have reversed BGRA4 and BGR5A1 support.
  2432. setTextureFormat(TextureFormat::BGRA4, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4);
  2433. setTextureFormat(TextureFormat::RGBA4, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4);
  2434. setTextureFormat(TextureFormat::BGR5A1, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);
  2435. setTextureFormat(TextureFormat::RGB5A1, GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);
  2436. setTextureFormat(TextureFormat::B5G6R5, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
  2437. setTextureFormat(TextureFormat::R5G6B5, GL_RGB, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
  2438. if (!m_gles3)
  2439. {
  2440. // OpenGL ES 2.0 uses unsized internal formats.
  2441. s_textureFormat[TextureFormat::RGB8].m_internalFmt = GL_RGB;
  2442. // OpenGL ES 2.0 does not have R8 texture format, only L8. Open GL ES 2.0 extension https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_texture_rg.txt
  2443. // adds support for R8 to GLES 2.0 core contexts. For those use L8 instead.
  2444. if (!s_extension[Extension::EXT_texture_rg].m_supported)
  2445. {
  2446. s_textureFormat[TextureFormat::R8].m_internalFmt = GL_LUMINANCE;
  2447. s_textureFormat[TextureFormat::R8].m_fmt = GL_LUMINANCE;
  2448. }
  2449. }
  2450. }
  2451. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  2452. {
  2453. if (TextureFormat::Unknown != ii
  2454. && TextureFormat::UnknownDepth != ii)
  2455. {
  2456. s_textureFormat[ii].m_supported = isTextureFormatValid(TextureFormat::Enum(ii) );
  2457. }
  2458. }
  2459. if (BX_ENABLED(0) )
  2460. {
  2461. // Disable all compressed texture formats. For testing only.
  2462. for (uint32_t ii = 0; ii < TextureFormat::Unknown; ++ii)
  2463. {
  2464. s_textureFormat[ii].m_supported = false;
  2465. }
  2466. }
  2467. const bool computeSupport = false
  2468. || !!(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  2469. || s_extension[Extension::ARB_compute_shader].m_supported
  2470. ;
  2471. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  2472. {
  2473. const TextureFormat::Enum fmt = TextureFormat::Enum(ii);
  2474. uint16_t supported = BGFX_CAPS_FORMAT_TEXTURE_NONE;
  2475. supported |= s_textureFormat[ii].m_supported
  2476. ? BGFX_CAPS_FORMAT_TEXTURE_2D
  2477. | BGFX_CAPS_FORMAT_TEXTURE_3D
  2478. | BGFX_CAPS_FORMAT_TEXTURE_CUBE
  2479. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2480. ;
  2481. supported |= isTextureFormatValid(fmt, true)
  2482. ? BGFX_CAPS_FORMAT_TEXTURE_2D_SRGB
  2483. | BGFX_CAPS_FORMAT_TEXTURE_3D_SRGB
  2484. | BGFX_CAPS_FORMAT_TEXTURE_CUBE_SRGB
  2485. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2486. ;
  2487. if (!bimg::isCompressed(bimg::TextureFormat::Enum(fmt) ) )
  2488. {
  2489. supported |= isTextureFormatValid(fmt, false, true)
  2490. ? BGFX_CAPS_FORMAT_TEXTURE_MIP_AUTOGEN
  2491. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2492. ;
  2493. }
  2494. supported |= computeSupport
  2495. && isImageFormatValid(fmt)
  2496. ? (BGFX_CAPS_FORMAT_TEXTURE_IMAGE_READ | BGFX_CAPS_FORMAT_TEXTURE_IMAGE_WRITE)
  2497. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2498. ;
  2499. supported |= isFramebufferFormatValid(fmt)
  2500. ? BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER
  2501. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2502. ;
  2503. supported |= isFramebufferFormatValid(fmt, false, true)
  2504. ? BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER
  2505. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2506. ;
  2507. if (NULL != glGetInternalformativ)
  2508. {
  2509. GLint maxSamples;
  2510. glGetInternalformativ(GL_RENDERBUFFER
  2511. , s_textureFormat[ii].m_internalFmt
  2512. , GL_SAMPLES
  2513. , 1
  2514. , &maxSamples
  2515. );
  2516. GLenum err = getGlError();
  2517. supported |= 0 == err && maxSamples > 0
  2518. ? BGFX_CAPS_FORMAT_TEXTURE_FRAMEBUFFER_MSAA
  2519. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2520. ;
  2521. glGetInternalformativ(GL_TEXTURE_2D_MULTISAMPLE
  2522. , s_textureFormat[ii].m_internalFmt
  2523. , GL_SAMPLES
  2524. , 1
  2525. , &maxSamples
  2526. );
  2527. err = getGlError();
  2528. supported |= 0 == err && maxSamples > 0
  2529. ? BGFX_CAPS_FORMAT_TEXTURE_MSAA
  2530. : BGFX_CAPS_FORMAT_TEXTURE_NONE
  2531. ;
  2532. }
  2533. g_caps.formats[ii] = supported;
  2534. }
  2535. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || m_gles3)
  2536. || s_extension[Extension::OES_texture_3D].m_supported
  2537. ? BGFX_CAPS_TEXTURE_3D
  2538. : 0
  2539. ;
  2540. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || m_gles3)
  2541. || s_extension[Extension::EXT_shadow_samplers].m_supported
  2542. ? BGFX_CAPS_TEXTURE_COMPARE_ALL
  2543. : 0
  2544. ;
  2545. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || m_gles3)
  2546. || s_extension[Extension::OES_vertex_half_float].m_supported
  2547. ? BGFX_CAPS_VERTEX_ATTRIB_HALF
  2548. : 0
  2549. ;
  2550. g_caps.supported |= false
  2551. || s_extension[Extension::ARB_vertex_type_2_10_10_10_rev].m_supported
  2552. || s_extension[Extension::OES_vertex_type_10_10_10_2].m_supported
  2553. ? BGFX_CAPS_VERTEX_ATTRIB_UINT10
  2554. : 0
  2555. ;
  2556. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || m_gles3)
  2557. || s_extension[Extension::EXT_frag_depth].m_supported
  2558. ? BGFX_CAPS_FRAGMENT_DEPTH
  2559. : 0
  2560. ;
  2561. g_caps.supported |= s_extension[Extension::ARB_draw_buffers_blend].m_supported
  2562. ? BGFX_CAPS_BLEND_INDEPENDENT
  2563. : 0
  2564. ;
  2565. g_caps.supported |= s_extension[Extension::INTEL_fragment_shader_ordering].m_supported
  2566. ? BGFX_CAPS_FRAGMENT_ORDERING
  2567. : 0
  2568. ;
  2569. g_caps.supported |= !!(BGFX_CONFIG_RENDERER_OPENGL || m_gles3)
  2570. || s_extension[Extension::OES_element_index_uint].m_supported
  2571. ? BGFX_CAPS_INDEX32
  2572. : 0
  2573. ;
  2574. const bool drawIndirectSupported = false
  2575. || s_extension[Extension::AMD_multi_draw_indirect].m_supported
  2576. || s_extension[Extension::ARB_draw_indirect ].m_supported
  2577. || s_extension[Extension::ARB_multi_draw_indirect].m_supported
  2578. || s_extension[Extension::EXT_multi_draw_indirect].m_supported
  2579. ;
  2580. if (drawIndirectSupported)
  2581. {
  2582. if (NULL == glMultiDrawArraysIndirect
  2583. || NULL == glMultiDrawElementsIndirect)
  2584. {
  2585. glMultiDrawArraysIndirect = stubMultiDrawArraysIndirect;
  2586. glMultiDrawElementsIndirect = stubMultiDrawElementsIndirect;
  2587. }
  2588. }
  2589. g_caps.supported |= drawIndirectSupported
  2590. ? BGFX_CAPS_DRAW_INDIRECT
  2591. : 0
  2592. ;
  2593. g_caps.supported |= s_extension[Extension::ARB_indirect_parameters].m_supported
  2594. ? BGFX_CAPS_DRAW_INDIRECT_COUNT
  2595. : 0
  2596. ;
  2597. if (BX_ENABLED(BX_PLATFORM_EMSCRIPTEN)
  2598. || NULL == glPolygonMode)
  2599. {
  2600. glPolygonMode = stubPolygonMode;
  2601. }
  2602. if (s_extension[Extension::ARB_copy_image].m_supported
  2603. || s_extension[Extension::EXT_copy_image].m_supported
  2604. || s_extension[Extension:: NV_copy_image].m_supported
  2605. || s_extension[Extension::OES_copy_image].m_supported)
  2606. {
  2607. m_blitSupported = NULL != glCopyImageSubData;
  2608. }
  2609. g_caps.supported |= m_blitSupported || BX_ENABLED(BGFX_GL_CONFIG_BLIT_EMULATION)
  2610. ? BGFX_CAPS_TEXTURE_BLIT
  2611. : 0
  2612. ;
  2613. g_caps.supported |= (m_readBackSupported || BX_ENABLED(BGFX_GL_CONFIG_TEXTURE_READ_BACK_EMULATION) )
  2614. ? BGFX_CAPS_TEXTURE_READ_BACK
  2615. : 0
  2616. ;
  2617. g_caps.supported |= false
  2618. || s_extension[Extension::EXT_texture_array].m_supported
  2619. || s_extension[Extension::EXT_gpu_shader4].m_supported
  2620. || (m_gles3 && !BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  2621. ? BGFX_CAPS_TEXTURE_2D_ARRAY
  2622. : 0
  2623. ;
  2624. g_caps.supported |= false
  2625. || s_extension[Extension::EXT_gpu_shader4].m_supported
  2626. || (m_gles3 && !BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) )
  2627. ? BGFX_CAPS_VERTEX_ID
  2628. : 0
  2629. ;
  2630. g_caps.supported |= false
  2631. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 32)
  2632. || (m_gles3 && !BX_ENABLED(BX_PLATFORM_EMSCRIPTEN) && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 32) )
  2633. ? BGFX_CAPS_PRIMITIVE_ID
  2634. : 0
  2635. ;
  2636. g_caps.supported |= false
  2637. || s_extension[Extension::ARB_texture_cube_map_array].m_supported
  2638. || s_extension[Extension::EXT_texture_cube_map_array].m_supported
  2639. ? BGFX_CAPS_TEXTURE_CUBE_ARRAY
  2640. : 0
  2641. ;
  2642. g_caps.limits.maxTextureSize = uint32_t(glGet(GL_MAX_TEXTURE_SIZE) );
  2643. g_caps.limits.maxTextureLayers = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 30) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 30) || s_extension[Extension::EXT_texture_array].m_supported ? uint16_t(bx::max(glGet(GL_MAX_ARRAY_TEXTURE_LAYERS), 1) ) : 1;
  2644. g_caps.limits.maxComputeBindings = computeSupport ? BGFX_MAX_COMPUTE_BINDINGS : 0;
  2645. g_caps.limits.maxVertexStreams = BGFX_CONFIG_MAX_VERTEX_STREAMS;
  2646. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  2647. || m_gles3
  2648. || s_extension[Extension::EXT_draw_buffers ].m_supported
  2649. || s_extension[Extension::WEBGL_draw_buffers].m_supported)
  2650. {
  2651. g_caps.limits.maxFBAttachments = uint8_t(bx::uint32_clamp(
  2652. glGet(GL_MAX_DRAW_BUFFERS)
  2653. , 1
  2654. , BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS)
  2655. );
  2656. }
  2657. // if (s_extension[Extension::ARB_clip_control].m_supported)
  2658. // {
  2659. // GL_CHECK(glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE) );
  2660. // g_caps.originBottomLeft = true;
  2661. // }
  2662. // else
  2663. {
  2664. g_caps.homogeneousDepth = true;
  2665. g_caps.originBottomLeft = true;
  2666. }
  2667. m_vaoSupport = !BX_ENABLED(BX_PLATFORM_EMSCRIPTEN)
  2668. && (m_gles3
  2669. || s_extension[Extension::ARB_vertex_array_object].m_supported
  2670. || s_extension[Extension::OES_vertex_array_object].m_supported
  2671. );
  2672. if (m_vaoSupport)
  2673. {
  2674. GL_CHECK(glGenVertexArrays(1, &m_vao) );
  2675. GL_CHECK(glBindVertexArray(m_vao) );
  2676. }
  2677. m_samplerObjectSupport = false
  2678. || m_gles3
  2679. || s_extension[Extension::ARB_sampler_objects].m_supported
  2680. ;
  2681. m_shadowSamplersSupport = !!(BGFX_CONFIG_RENDERER_OPENGL || m_gles3)
  2682. || s_extension[Extension::EXT_shadow_samplers].m_supported
  2683. ;
  2684. m_programBinarySupport = !BX_ENABLED(BX_PLATFORM_EMSCRIPTEN)
  2685. && (m_gles3
  2686. || s_extension[Extension::ARB_get_program_binary].m_supported
  2687. || s_extension[Extension::OES_get_program_binary].m_supported
  2688. || s_extension[Extension::IMG_shader_binary ].m_supported
  2689. );
  2690. m_textureSwizzleSupport = false
  2691. || s_extension[Extension::ARB_texture_swizzle].m_supported
  2692. || s_extension[Extension::EXT_texture_swizzle].m_supported
  2693. ;
  2694. m_depthTextureSupport = !!(BGFX_CONFIG_RENDERER_OPENGL || m_gles3)
  2695. || s_extension[Extension::ANGLE_depth_texture ].m_supported
  2696. || s_extension[Extension::CHROMIUM_depth_texture ].m_supported
  2697. || s_extension[Extension::GOOGLE_depth_texture ].m_supported
  2698. || s_extension[Extension::OES_depth_texture ].m_supported
  2699. || s_extension[Extension::MOZ_WEBGL_depth_texture ].m_supported
  2700. || s_extension[Extension::WEBGL_depth_texture ].m_supported
  2701. || s_extension[Extension::WEBKIT_WEBGL_depth_texture].m_supported
  2702. ;
  2703. m_timerQuerySupport = false
  2704. || s_extension[Extension::ANGLE_timer_query ].m_supported
  2705. || s_extension[Extension::ARB_timer_query ].m_supported
  2706. || s_extension[Extension::EXT_disjoint_timer_query].m_supported
  2707. || s_extension[Extension::EXT_timer_query ].m_supported
  2708. ;
  2709. m_timerQuerySupport &= true
  2710. && NULL != glQueryCounter
  2711. && NULL != glGetQueryObjectiv
  2712. && NULL != glGetQueryObjectui64v
  2713. ;
  2714. m_occlusionQuerySupport = false
  2715. || s_extension[Extension::ARB_occlusion_query ].m_supported
  2716. || s_extension[Extension::ARB_occlusion_query2 ].m_supported
  2717. || s_extension[Extension::EXT_occlusion_query_boolean].m_supported
  2718. || s_extension[Extension::NV_occlusion_query ].m_supported
  2719. ;
  2720. m_occlusionQuerySupport &= true
  2721. && NULL != glGenQueries
  2722. && NULL != glDeleteQueries
  2723. && NULL != glBeginQuery
  2724. && NULL != glEndQuery
  2725. ;
  2726. m_atocSupport = s_extension[Extension::ARB_multisample].m_supported;
  2727. m_conservativeRasterSupport = s_extension[Extension::NV_conservative_raster].m_supported;
  2728. m_imageLoadStoreSupport = false
  2729. || s_extension[Extension::ARB_shader_image_load_store].m_supported
  2730. || s_extension[Extension::EXT_shader_image_load_store].m_supported
  2731. ;
  2732. g_caps.supported |= 0
  2733. | (m_atocSupport ? BGFX_CAPS_ALPHA_TO_COVERAGE : 0)
  2734. | (m_conservativeRasterSupport ? BGFX_CAPS_CONSERVATIVE_RASTER : 0)
  2735. | (m_occlusionQuerySupport ? BGFX_CAPS_OCCLUSION_QUERY : 0)
  2736. | (m_depthTextureSupport ? BGFX_CAPS_TEXTURE_COMPARE_LEQUAL : 0)
  2737. | (computeSupport ? BGFX_CAPS_COMPUTE : 0)
  2738. | (m_imageLoadStoreSupport ? BGFX_CAPS_IMAGE_RW : 0)
  2739. ;
  2740. g_caps.supported |= m_glctx.getCaps();
  2741. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES) )
  2742. {
  2743. m_srgbWriteControlSupport = s_extension[Extension::EXT_sRGB_write_control].m_supported;
  2744. m_borderColorSupport = s_extension[Extension::NV_texture_border_clamp].m_supported;
  2745. s_textureAddress[BGFX_SAMPLER_U_BORDER>>BGFX_SAMPLER_U_SHIFT] = s_extension[Extension::NV_texture_border_clamp].m_supported
  2746. ? GL_CLAMP_TO_BORDER
  2747. : GL_CLAMP_TO_EDGE
  2748. ;
  2749. }
  2750. if (s_extension[Extension::EXT_texture_filter_anisotropic].m_supported)
  2751. {
  2752. GL_CHECK(glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &m_maxAnisotropyDefault) );
  2753. }
  2754. if (s_extension[Extension::ARB_texture_multisample].m_supported
  2755. || s_extension[Extension::ANGLE_framebuffer_multisample].m_supported
  2756. || s_extension[Extension::EXT_multisampled_render_to_texture].m_supported)
  2757. {
  2758. GL_CHECK(glGetIntegerv(GL_MAX_SAMPLES, &m_maxMsaa) );
  2759. }
  2760. #if BGFX_CONFIG_RENDERER_OPENGLES && (BGFX_CONFIG_RENDERER_OPENGLES < 30)
  2761. if (!m_maxMsaa && s_extension[Extension::IMG_multisampled_render_to_texture].m_supported)
  2762. {
  2763. GL_CHECK(glGetIntegerv(GL_MAX_SAMPLES_IMG, &m_maxMsaa) );
  2764. }
  2765. #endif // BGFX_CONFIG_RENDERER_OPENGLES < 30
  2766. if (s_extension[Extension::OES_read_format].m_supported
  2767. && (s_extension[Extension::IMG_read_format].m_supported || s_extension[Extension::EXT_read_format_bgra].m_supported) )
  2768. {
  2769. m_readPixelsFmt = GL_BGRA;
  2770. }
  2771. else
  2772. {
  2773. m_readPixelsFmt = GL_RGBA;
  2774. }
  2775. if (m_gles3)
  2776. {
  2777. g_caps.supported |= BGFX_CAPS_INSTANCING;
  2778. }
  2779. else
  2780. {
  2781. if (s_extension[Extension::ANGLE_instanced_arrays].m_supported
  2782. || s_extension[Extension:: ARB_instanced_arrays].m_supported
  2783. || s_extension[Extension:: EXT_instanced_arrays].m_supported
  2784. || (s_extension[Extension:: NV_instanced_arrays].m_supported && s_extension[Extension::NV_draw_instanced].m_supported)
  2785. )
  2786. {
  2787. if (NULL != glVertexAttribDivisor
  2788. && NULL != glDrawArraysInstanced
  2789. && NULL != glDrawElementsInstanced)
  2790. {
  2791. g_caps.supported |= BGFX_CAPS_INSTANCING;
  2792. }
  2793. else if (NULL != glVertexAttribDivisorNV
  2794. && NULL != glDrawArraysInstancedNV
  2795. && NULL != glDrawElementsInstancedNV)
  2796. {
  2797. glVertexAttribDivisor = glVertexAttribDivisorNV;
  2798. glDrawArraysInstanced = glDrawArraysInstancedNV;
  2799. glDrawElementsInstanced = glDrawElementsInstancedNV;
  2800. g_caps.supported |= BGFX_CAPS_INSTANCING;
  2801. }
  2802. }
  2803. if (0 == (g_caps.supported & BGFX_CAPS_INSTANCING) )
  2804. {
  2805. glVertexAttribDivisor = stubVertexAttribDivisor;
  2806. glDrawArraysInstanced = stubDrawArraysInstanced;
  2807. glDrawElementsInstanced = stubDrawElementsInstanced;
  2808. }
  2809. }
  2810. g_caps.supported |= s_extension[Extension::ARB_shader_viewport_layer_array].m_supported
  2811. ? BGFX_CAPS_VIEWPORT_LAYER_ARRAY
  2812. : 0
  2813. ;
  2814. g_caps.supported |= BX_ENABLED(BX_PLATFORM_WINRT) ? BGFX_CAPS_TRANSPARENT_BACKBUFFER : 0;
  2815. if (s_extension[Extension::ARB_debug_output].m_supported
  2816. || s_extension[Extension::KHR_debug].m_supported)
  2817. {
  2818. if (NULL != glDebugMessageControl
  2819. && NULL != glDebugMessageInsert
  2820. && NULL != glDebugMessageCallback
  2821. && NULL != glGetDebugMessageLog)
  2822. {
  2823. GL_CHECK(glDebugMessageCallback(debugProcCb, NULL) );
  2824. GL_CHECK(glDebugMessageControl(GL_DONT_CARE
  2825. , GL_DONT_CARE
  2826. , GL_DEBUG_SEVERITY_MEDIUM
  2827. , 0
  2828. , NULL
  2829. , GL_TRUE
  2830. ) );
  2831. }
  2832. }
  2833. if (NULL == glPushDebugGroup
  2834. || NULL == glPopDebugGroup)
  2835. {
  2836. glPushDebugGroup = stubPushDebugGroup;
  2837. glPopDebugGroup = stubPopDebugGroup;
  2838. }
  2839. if (s_extension[Extension::ARB_seamless_cube_map].m_supported)
  2840. {
  2841. GL_CHECK(glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS) );
  2842. }
  2843. if (NULL != glProvokingVertex
  2844. && s_extension[Extension::ARB_provoking_vertex].m_supported)
  2845. {
  2846. GL_CHECK(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION) );
  2847. }
  2848. if (NULL == glInsertEventMarker
  2849. || !s_extension[Extension::EXT_debug_marker].m_supported)
  2850. {
  2851. glInsertEventMarker = stubInsertEventMarker;
  2852. }
  2853. m_maxLabelLen = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 32) || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 43) || s_extension[Extension::KHR_debug].m_supported ? uint16_t(glGet(GL_MAX_LABEL_LENGTH) ) : 0;
  2854. setGraphicsDebuggerPresent(s_extension[Extension::EXT_debug_tool].m_supported);
  2855. if (NULL == glObjectLabel)
  2856. {
  2857. glObjectLabel = stubObjectLabel;
  2858. }
  2859. if (NULL == glInvalidateFramebuffer)
  2860. {
  2861. glInvalidateFramebuffer = stubInvalidateFramebuffer;
  2862. }
  2863. if (NULL == glFramebufferTexture)
  2864. {
  2865. glFramebufferTexture = stubFramebufferTexture;
  2866. }
  2867. if (m_timerQuerySupport)
  2868. {
  2869. m_gpuTimer.create();
  2870. }
  2871. if (m_occlusionQuerySupport)
  2872. {
  2873. m_occlusionQuery.create();
  2874. }
  2875. // Init reserved part of view name.
  2876. for (uint32_t ii = 0; ii < BGFX_CONFIG_MAX_VIEWS; ++ii)
  2877. {
  2878. bx::snprintf(s_viewName[ii], BGFX_CONFIG_MAX_VIEW_NAME_RESERVED+1, "%3d ", ii);
  2879. }
  2880. m_needPresent = false;
  2881. }
  2882. return true;
  2883. error:
  2884. switch (errorState)
  2885. {
  2886. case ErrorState::Default:
  2887. break;
  2888. }
  2889. m_glctx.destroy();
  2890. unloadRenderDoc(m_renderdocdll);
  2891. return false;
  2892. }
  2893. void shutdown()
  2894. {
  2895. if (m_vaoSupport)
  2896. {
  2897. GL_CHECK(glBindVertexArray(0) );
  2898. GL_CHECK(glDeleteVertexArrays(1, &m_vao) );
  2899. m_vao = 0;
  2900. }
  2901. captureFinish();
  2902. invalidateCache();
  2903. if (m_timerQuerySupport)
  2904. {
  2905. m_gpuTimer.destroy();
  2906. }
  2907. if (m_occlusionQuerySupport)
  2908. {
  2909. m_occlusionQuery.destroy();
  2910. }
  2911. destroyMsaaFbo();
  2912. m_glctx.destroy();
  2913. m_flip = false;
  2914. unloadRenderDoc(m_renderdocdll);
  2915. }
  2916. RendererType::Enum getRendererType() const override
  2917. {
  2918. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  2919. {
  2920. return RendererType::OpenGL;
  2921. }
  2922. return RendererType::OpenGLES;
  2923. }
  2924. const char* getRendererName() const override
  2925. {
  2926. return BGFX_RENDERER_OPENGL_NAME;
  2927. }
  2928. bool isDeviceRemoved() override
  2929. {
  2930. return false;
  2931. }
  2932. void flip() override
  2933. {
  2934. if (m_flip)
  2935. {
  2936. for (uint32_t ii = 1, num = m_numWindows; ii < num; ++ii)
  2937. {
  2938. FrameBufferGL& frameBuffer = m_frameBuffers[m_windows[ii].idx];
  2939. if (frameBuffer.m_needPresent)
  2940. {
  2941. m_glctx.swap(frameBuffer.m_swapChain);
  2942. frameBuffer.m_needPresent = false;
  2943. }
  2944. }
  2945. if (m_needPresent)
  2946. {
  2947. // Ensure the back buffer is bound as the source of the flip
  2948. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  2949. m_glctx.swap();
  2950. m_needPresent = false;
  2951. }
  2952. }
  2953. }
  2954. void createIndexBuffer(IndexBufferHandle _handle, const Memory* _mem, uint16_t _flags) override
  2955. {
  2956. m_indexBuffers[_handle.idx].create(_mem->size, _mem->data, _flags);
  2957. }
  2958. void destroyIndexBuffer(IndexBufferHandle _handle) override
  2959. {
  2960. m_indexBuffers[_handle.idx].destroy();
  2961. }
  2962. void createVertexLayout(VertexLayoutHandle _handle, const VertexLayout& _layout) override
  2963. {
  2964. VertexLayout& layout = m_vertexLayouts[_handle.idx];
  2965. bx::memCopy(&layout, &_layout, sizeof(VertexLayout) );
  2966. dump(layout);
  2967. }
  2968. void destroyVertexLayout(VertexLayoutHandle /*_handle*/) override
  2969. {
  2970. }
  2971. void createVertexBuffer(VertexBufferHandle _handle, const Memory* _mem, VertexLayoutHandle _layoutHandle, uint16_t _flags) override
  2972. {
  2973. m_vertexBuffers[_handle.idx].create(_mem->size, _mem->data, _layoutHandle, _flags);
  2974. }
  2975. void destroyVertexBuffer(VertexBufferHandle _handle) override
  2976. {
  2977. m_vertexBuffers[_handle.idx].destroy();
  2978. }
  2979. void createDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _size, uint16_t _flags) override
  2980. {
  2981. m_indexBuffers[_handle.idx].create(_size, NULL, _flags);
  2982. }
  2983. void updateDynamicIndexBuffer(IndexBufferHandle _handle, uint32_t _offset, uint32_t _size, const Memory* _mem) override
  2984. {
  2985. m_indexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  2986. }
  2987. void destroyDynamicIndexBuffer(IndexBufferHandle _handle) override
  2988. {
  2989. m_indexBuffers[_handle.idx].destroy();
  2990. }
  2991. void createDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _size, uint16_t _flags) override
  2992. {
  2993. VertexLayoutHandle layoutHandle = BGFX_INVALID_HANDLE;
  2994. m_vertexBuffers[_handle.idx].create(_size, NULL, layoutHandle, _flags);
  2995. }
  2996. void updateDynamicVertexBuffer(VertexBufferHandle _handle, uint32_t _offset, uint32_t _size, const Memory* _mem) override
  2997. {
  2998. m_vertexBuffers[_handle.idx].update(_offset, bx::uint32_min(_size, _mem->size), _mem->data);
  2999. }
  3000. void destroyDynamicVertexBuffer(VertexBufferHandle _handle) override
  3001. {
  3002. m_vertexBuffers[_handle.idx].destroy();
  3003. }
  3004. void createShader(ShaderHandle _handle, const Memory* _mem) override
  3005. {
  3006. m_shaders[_handle.idx].create(_mem);
  3007. }
  3008. void destroyShader(ShaderHandle _handle) override
  3009. {
  3010. m_shaders[_handle.idx].destroy();
  3011. }
  3012. void createProgram(ProgramHandle _handle, ShaderHandle _vsh, ShaderHandle _fsh) override
  3013. {
  3014. ShaderGL dummyFragmentShader;
  3015. m_program[_handle.idx].create(m_shaders[_vsh.idx], isValid(_fsh) ? m_shaders[_fsh.idx] : dummyFragmentShader);
  3016. }
  3017. void destroyProgram(ProgramHandle _handle) override
  3018. {
  3019. m_program[_handle.idx].destroy();
  3020. }
  3021. void* createTexture(TextureHandle _handle, const Memory* _mem, uint64_t _flags, uint8_t _skip) override
  3022. {
  3023. m_textures[_handle.idx].create(_mem, _flags, _skip);
  3024. return NULL;
  3025. }
  3026. void updateTextureBegin(TextureHandle /*_handle*/, uint8_t /*_side*/, uint8_t /*_mip*/) override
  3027. {
  3028. }
  3029. 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) override
  3030. {
  3031. m_textures[_handle.idx].update(_side, _mip, _rect, _z, _depth, _pitch, _mem);
  3032. }
  3033. void updateTextureEnd() override
  3034. {
  3035. }
  3036. void readTexture(TextureHandle _handle, void* _data, uint8_t _mip) override
  3037. {
  3038. if (m_readBackSupported)
  3039. {
  3040. const TextureGL& texture = m_textures[_handle.idx];
  3041. const bool compressed = bimg::isCompressed(bimg::TextureFormat::Enum(texture.m_textureFormat) );
  3042. GL_CHECK(glBindTexture(texture.m_target, texture.m_id) );
  3043. if (compressed)
  3044. {
  3045. GL_CHECK(glGetCompressedTexImage(texture.m_target
  3046. , _mip
  3047. , _data
  3048. ) );
  3049. }
  3050. else
  3051. {
  3052. GL_CHECK(glGetTexImage(texture.m_target
  3053. , _mip
  3054. , texture.m_fmt
  3055. , texture.m_type
  3056. , _data
  3057. ) );
  3058. }
  3059. GL_CHECK(glBindTexture(texture.m_target, 0) );
  3060. }
  3061. else if (BX_ENABLED(BGFX_GL_CONFIG_TEXTURE_READ_BACK_EMULATION) )
  3062. {
  3063. const TextureGL& texture = m_textures[_handle.idx];
  3064. const bool compressed = bimg::isCompressed(bimg::TextureFormat::Enum(texture.m_textureFormat) );
  3065. if (!compressed)
  3066. {
  3067. Attachment at[1];
  3068. at[0].init(_handle);
  3069. FrameBufferGL frameBuffer;
  3070. frameBuffer.create(BX_COUNTOF(at), at);
  3071. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer.m_fbo[0]) );
  3072. GL_CHECK(glFramebufferTexture2D(
  3073. GL_FRAMEBUFFER
  3074. , GL_COLOR_ATTACHMENT0
  3075. , GL_TEXTURE_2D
  3076. , texture.m_id
  3077. , at[0].mip
  3078. ) );
  3079. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || m_gles3)
  3080. {
  3081. GL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT0) );
  3082. }
  3083. if (GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER) )
  3084. {
  3085. GL_CHECK(glReadPixels(
  3086. 0
  3087. , 0
  3088. , texture.m_width
  3089. , texture.m_height
  3090. , m_readPixelsFmt
  3091. , GL_UNSIGNED_BYTE
  3092. , _data
  3093. ) );
  3094. }
  3095. frameBuffer.destroy();
  3096. }
  3097. }
  3098. }
  3099. void resizeTexture(TextureHandle _handle, uint16_t _width, uint16_t _height, uint8_t _numMips, uint16_t _numLayers) override
  3100. {
  3101. TextureGL& texture = m_textures[_handle.idx];
  3102. uint32_t size = sizeof(uint32_t) + sizeof(TextureCreate);
  3103. const Memory* mem = alloc(size);
  3104. bx::StaticMemoryBlockWriter writer(mem->data, mem->size);
  3105. uint32_t magic = BGFX_CHUNK_MAGIC_TEX;
  3106. bx::write(&writer, magic, bx::ErrorAssert{});
  3107. TextureCreate tc;
  3108. tc.m_width = _width;
  3109. tc.m_height = _height;
  3110. tc.m_depth = 0;
  3111. tc.m_numLayers = _numLayers;
  3112. tc.m_numMips = _numMips;
  3113. tc.m_format = TextureFormat::Enum(texture.m_requestedFormat);
  3114. tc.m_cubeMap = false;
  3115. tc.m_mem = NULL;
  3116. bx::write(&writer, tc, bx::ErrorAssert{});
  3117. texture.destroy();
  3118. texture.create(mem, texture.m_flags, 0);
  3119. release(mem);
  3120. }
  3121. void overrideInternal(TextureHandle _handle, uintptr_t _ptr) override
  3122. {
  3123. m_textures[_handle.idx].overrideInternal(_ptr);
  3124. }
  3125. uintptr_t getInternal(TextureHandle _handle) override
  3126. {
  3127. return uintptr_t(m_textures[_handle.idx].m_id);
  3128. }
  3129. void destroyTexture(TextureHandle _handle) override
  3130. {
  3131. m_textures[_handle.idx].destroy();
  3132. }
  3133. void createFrameBuffer(FrameBufferHandle _handle, uint8_t _num, const Attachment* _attachment) override
  3134. {
  3135. m_frameBuffers[_handle.idx].create(_num, _attachment);
  3136. }
  3137. void createFrameBuffer(FrameBufferHandle _handle, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, TextureFormat::Enum _depthFormat) override
  3138. {
  3139. uint16_t denseIdx = m_numWindows++;
  3140. m_windows[denseIdx] = _handle;
  3141. m_frameBuffers[_handle.idx].create(denseIdx, _nwh, _width, _height, _format, _depthFormat);
  3142. }
  3143. void destroyFrameBuffer(FrameBufferHandle _handle) override
  3144. {
  3145. uint16_t denseIdx = m_frameBuffers[_handle.idx].destroy();
  3146. if (UINT16_MAX != denseIdx)
  3147. {
  3148. --m_numWindows;
  3149. if (m_numWindows > 1)
  3150. {
  3151. FrameBufferHandle handle = m_windows[m_numWindows];
  3152. m_windows[m_numWindows] = {kInvalidHandle};
  3153. if (m_numWindows != denseIdx)
  3154. {
  3155. m_windows[denseIdx] = handle;
  3156. m_frameBuffers[handle.idx].m_denseIdx = denseIdx;
  3157. }
  3158. }
  3159. }
  3160. }
  3161. void createUniform(UniformHandle _handle, UniformType::Enum _type, uint16_t _num, const char* _name) override
  3162. {
  3163. if (NULL != m_uniforms[_handle.idx])
  3164. {
  3165. bx::free(g_allocator, m_uniforms[_handle.idx]);
  3166. }
  3167. uint32_t size = g_uniformTypeSize[_type]*_num;
  3168. void* data = bx::alloc(g_allocator, size);
  3169. bx::memSet(data, 0, size);
  3170. m_uniforms[_handle.idx] = data;
  3171. m_uniformReg.add(_handle, _name);
  3172. }
  3173. void destroyUniform(UniformHandle _handle) override
  3174. {
  3175. bx::free(g_allocator, m_uniforms[_handle.idx]);
  3176. m_uniforms[_handle.idx] = NULL;
  3177. m_uniformReg.remove(_handle);
  3178. }
  3179. void requestScreenShot(FrameBufferHandle _handle, const char* _filePath) override
  3180. {
  3181. SwapChainGL* swapChain = NULL;
  3182. uint32_t width = m_resolution.width;
  3183. uint32_t height = m_resolution.height;
  3184. if (isValid(_handle) )
  3185. {
  3186. const FrameBufferGL& frameBuffer = m_frameBuffers[_handle.idx];
  3187. swapChain = frameBuffer.m_swapChain;
  3188. width = frameBuffer.m_width;
  3189. height = frameBuffer.m_height;
  3190. }
  3191. m_glctx.makeCurrent(swapChain);
  3192. uint32_t length = width*height*4;
  3193. uint8_t* data = (uint8_t*)bx::alloc(g_allocator, length);
  3194. GL_CHECK(glReadPixels(0
  3195. , 0
  3196. , width
  3197. , height
  3198. , m_readPixelsFmt
  3199. , GL_UNSIGNED_BYTE
  3200. , data
  3201. ) );
  3202. if (GL_RGBA == m_readPixelsFmt)
  3203. {
  3204. bimg::imageSwizzleBgra8(data, width*4, width, height, data, width*4);
  3205. }
  3206. g_callback->screenShot(_filePath
  3207. , width
  3208. , height
  3209. , width*4
  3210. , data
  3211. , length
  3212. , true
  3213. );
  3214. bx::free(g_allocator, data);
  3215. }
  3216. void updateViewName(ViewId _id, const char* _name) override
  3217. {
  3218. bx::strCopy(&s_viewName[_id][BGFX_CONFIG_MAX_VIEW_NAME_RESERVED]
  3219. , BX_COUNTOF(s_viewName[0])-BGFX_CONFIG_MAX_VIEW_NAME_RESERVED
  3220. , _name
  3221. );
  3222. }
  3223. void updateUniform(uint16_t _loc, const void* _data, uint32_t _size) override
  3224. {
  3225. bx::memCopy(m_uniforms[_loc], _data, _size);
  3226. }
  3227. void invalidateOcclusionQuery(OcclusionQueryHandle _handle) override
  3228. {
  3229. m_occlusionQuery.invalidate(_handle);
  3230. }
  3231. void setMarker(const char* _marker, uint16_t _len) override
  3232. {
  3233. GL_CHECK(glInsertEventMarker(_len, _marker) );
  3234. }
  3235. virtual void setName(Handle _handle, const char* _name, uint16_t _len) override
  3236. {
  3237. uint16_t len = bx::min(_len, m_maxLabelLen);
  3238. switch (_handle.type)
  3239. {
  3240. case Handle::IndexBuffer:
  3241. GL_CHECK(glObjectLabel(GL_BUFFER, m_indexBuffers[_handle.idx].m_id, len, _name) );
  3242. break;
  3243. case Handle::Shader:
  3244. GL_CHECK(glObjectLabel(GL_SHADER, m_shaders[_handle.idx].m_id, len, _name) );
  3245. break;
  3246. case Handle::Texture:
  3247. {
  3248. GLint id = m_textures[_handle.idx].m_id;
  3249. if (0 != id)
  3250. {
  3251. GL_CHECK(glObjectLabel(GL_TEXTURE, id, len, _name) );
  3252. }
  3253. else
  3254. {
  3255. GL_CHECK(glObjectLabel(GL_RENDERBUFFER, m_textures[_handle.idx].m_rbo, len, _name) );
  3256. }
  3257. }
  3258. break;
  3259. case Handle::VertexBuffer:
  3260. GL_CHECK(glObjectLabel(GL_BUFFER, m_vertexBuffers[_handle.idx].m_id, len, _name) );
  3261. break;
  3262. default:
  3263. BX_ASSERT(false, "Invalid handle type?! %d", _handle.type);
  3264. break;
  3265. }
  3266. }
  3267. void submitBlit(BlitState& _bs, uint16_t _view);
  3268. void submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) override;
  3269. void blitSetup(TextVideoMemBlitter& _blitter) override
  3270. {
  3271. uint32_t width = m_resolution.width;
  3272. uint32_t height = m_resolution.height;
  3273. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  3274. GL_CHECK(glViewport(0, 0, width, height) );
  3275. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  3276. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  3277. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  3278. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  3279. GL_CHECK(glDisable(GL_CULL_FACE) );
  3280. GL_CHECK(glDisable(GL_BLEND) );
  3281. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  3282. ProgramGL& program = m_program[_blitter.m_program.idx];
  3283. setProgram(program.m_id);
  3284. setUniform1i(program.m_sampler[0], 0);
  3285. float proj[16];
  3286. bx::mtxOrtho(proj, 0.0f, (float)width, (float)height, 0.0f, 0.0f, 1000.0f, 0.0f, g_caps.homogeneousDepth);
  3287. setUniformMatrix4fv(program.m_predefined[0].m_loc
  3288. , 1
  3289. , GL_FALSE
  3290. , proj
  3291. );
  3292. GL_CHECK(glActiveTexture(GL_TEXTURE0) );
  3293. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_textures[_blitter.m_texture.idx].m_id) );
  3294. if (m_samplerObjectSupport)
  3295. {
  3296. GL_CHECK(glBindSampler(0, 0) );
  3297. }
  3298. }
  3299. void blitRender(TextVideoMemBlitter& _blitter, uint32_t _numIndices) override
  3300. {
  3301. const uint32_t numVertices = _numIndices*4/6;
  3302. if (0 < numVertices)
  3303. {
  3304. m_indexBuffers[_blitter.m_ib->handle.idx].update(0, _numIndices*2, _blitter.m_ib->data);
  3305. m_vertexBuffers[_blitter.m_vb->handle.idx].update(0, numVertices*_blitter.m_layout.m_stride, _blitter.m_vb->data);
  3306. VertexBufferGL& vb = m_vertexBuffers[_blitter.m_vb->handle.idx];
  3307. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  3308. IndexBufferGL& ib = m_indexBuffers[_blitter.m_ib->handle.idx];
  3309. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  3310. ProgramGL& program = m_program[_blitter.m_program.idx];
  3311. program.bindAttributesBegin();
  3312. program.bindAttributes(_blitter.m_layout, 0);
  3313. program.bindAttributesEnd();
  3314. GL_CHECK(glDrawElements(GL_TRIANGLES
  3315. , _numIndices
  3316. , GL_UNSIGNED_SHORT
  3317. , (void*)0
  3318. ) );
  3319. }
  3320. }
  3321. void updateResolution(const Resolution& _resolution)
  3322. {
  3323. m_maxAnisotropy = !!(_resolution.reset & BGFX_RESET_MAXANISOTROPY)
  3324. ? m_maxAnisotropyDefault
  3325. : 0.0f
  3326. ;
  3327. if (s_extension[Extension::ARB_depth_clamp].m_supported)
  3328. {
  3329. if (!!(_resolution.reset & BGFX_RESET_DEPTH_CLAMP) )
  3330. {
  3331. GL_CHECK(glEnable(GL_DEPTH_CLAMP) );
  3332. }
  3333. else
  3334. {
  3335. GL_CHECK(glDisable(GL_DEPTH_CLAMP) );
  3336. }
  3337. }
  3338. const uint32_t maskFlags = ~(0
  3339. | BGFX_RESET_MAXANISOTROPY
  3340. | BGFX_RESET_DEPTH_CLAMP
  3341. | BGFX_RESET_SUSPEND
  3342. );
  3343. if (m_resolution.width != _resolution.width
  3344. || m_resolution.height != _resolution.height
  3345. || (m_resolution.reset&maskFlags) != (_resolution.reset&maskFlags) )
  3346. {
  3347. uint32_t flags = _resolution.reset & (~BGFX_RESET_INTERNAL_FORCE);
  3348. m_resolution = _resolution;
  3349. m_resolution.reset = flags;
  3350. m_textVideoMem.resize(false, _resolution.width, _resolution.height);
  3351. m_textVideoMem.clear();
  3352. setRenderContextSize(m_resolution.width
  3353. , m_resolution.height
  3354. , flags
  3355. );
  3356. updateCapture();
  3357. for (uint32_t ii = 0; ii < BX_COUNTOF(m_frameBuffers); ++ii)
  3358. {
  3359. m_frameBuffers[ii].postReset();
  3360. }
  3361. m_currentFbo = 0;
  3362. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_currentFbo) );
  3363. }
  3364. }
  3365. void setShaderUniform4f(uint8_t /*_flags*/, uint32_t _regIndex, const void* _val, uint32_t _numRegs)
  3366. {
  3367. setUniform4fv(_regIndex
  3368. , _numRegs
  3369. , (const GLfloat*)_val
  3370. );
  3371. }
  3372. void setShaderUniform4x4f(uint8_t /*_flags*/, uint32_t _regIndex, const void* _val, uint32_t _numRegs)
  3373. {
  3374. setUniformMatrix4fv(_regIndex
  3375. , _numRegs
  3376. , GL_FALSE
  3377. , (const GLfloat*)_val
  3378. );
  3379. }
  3380. uint32_t setFrameBuffer(FrameBufferHandle _fbh, uint32_t _height, uint16_t _discard = BGFX_CLEAR_NONE, bool _msaa = true)
  3381. {
  3382. if (isValid(m_fbh)
  3383. && m_fbh.idx != _fbh.idx)
  3384. {
  3385. FrameBufferGL& frameBuffer = m_frameBuffers[m_fbh.idx];
  3386. frameBuffer.resolve();
  3387. if (BGFX_CLEAR_NONE != m_fbDiscard)
  3388. {
  3389. frameBuffer.discard(m_fbDiscard);
  3390. m_fbDiscard = BGFX_CLEAR_NONE;
  3391. }
  3392. }
  3393. m_glctx.makeCurrent(NULL);
  3394. if (!isValid(_fbh) )
  3395. {
  3396. m_needPresent |= true;
  3397. m_currentFbo = m_msaaBackBufferFbo;
  3398. }
  3399. else
  3400. {
  3401. FrameBufferGL& frameBuffer = m_frameBuffers[_fbh.idx];
  3402. _height = frameBuffer.m_height;
  3403. if (UINT16_MAX != frameBuffer.m_denseIdx)
  3404. {
  3405. m_glctx.makeCurrent(frameBuffer.m_swapChain);
  3406. GL_CHECK(glFrontFace(GL_CW) );
  3407. frameBuffer.m_needPresent = true;
  3408. m_currentFbo = 0;
  3409. }
  3410. else
  3411. {
  3412. m_glctx.makeCurrent(NULL);
  3413. m_currentFbo = frameBuffer.m_fbo[0];
  3414. }
  3415. }
  3416. if (0 != m_vao)
  3417. {
  3418. GL_CHECK(glDeleteVertexArrays(1, &m_vao) );
  3419. GL_CHECK(glGenVertexArrays(1, &m_vao) );
  3420. GL_CHECK(glBindVertexArray(m_vao) );
  3421. }
  3422. if (m_srgbWriteControlSupport)
  3423. {
  3424. if (0 == m_currentFbo)
  3425. {
  3426. if (0 != (m_resolution.reset & BGFX_RESET_SRGB_BACKBUFFER) )
  3427. {
  3428. GL_CHECK(glEnable(GL_FRAMEBUFFER_SRGB) );
  3429. }
  3430. else
  3431. {
  3432. GL_CHECK(glDisable(GL_FRAMEBUFFER_SRGB) );
  3433. }
  3434. }
  3435. else
  3436. {
  3437. // actual sRGB write/blending determined by FBO's color attachments format
  3438. GL_CHECK(glEnable(GL_FRAMEBUFFER_SRGB) );
  3439. }
  3440. }
  3441. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_currentFbo) );
  3442. m_fbh = _fbh;
  3443. m_fbDiscard = _discard;
  3444. m_rtMsaa = _msaa;
  3445. return _height;
  3446. }
  3447. uint32_t getNumRt() const
  3448. {
  3449. if (isValid(m_fbh) )
  3450. {
  3451. const FrameBufferGL& frameBuffer = m_frameBuffers[m_fbh.idx];
  3452. return frameBuffer.m_num;
  3453. }
  3454. return 1;
  3455. }
  3456. void createMsaaFbo(uint32_t _width, uint32_t _height, uint32_t _msaa)
  3457. {
  3458. if (0 == m_msaaBackBufferFbo // iOS
  3459. && 1 < _msaa
  3460. && !m_glctx.m_msaaContext)
  3461. {
  3462. GLenum storageFormat = m_resolution.reset & BGFX_RESET_SRGB_BACKBUFFER
  3463. ? GL_SRGB8_ALPHA8
  3464. : GL_RGBA8
  3465. ;
  3466. GLenum attachment = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || m_gles3
  3467. ? GL_DEPTH_STENCIL_ATTACHMENT
  3468. : GL_DEPTH_ATTACHMENT
  3469. ;
  3470. GL_CHECK(glGenFramebuffers(1, &m_msaaBackBufferFbo) );
  3471. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  3472. if (m_gles3)
  3473. {
  3474. GL_CHECK(glGenTextures(BX_COUNTOF(m_msaaBackBufferTextures), m_msaaBackBufferTextures) );
  3475. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_msaaBackBufferTextures[0]) );
  3476. GL_CHECK(glTexStorage2D(GL_TEXTURE_2D, 1, storageFormat, _width, _height) );
  3477. GL_CHECK(glFramebufferTexture2DMultisampleEXT(
  3478. GL_FRAMEBUFFER
  3479. , GL_COLOR_ATTACHMENT0
  3480. , GL_TEXTURE_2D
  3481. , m_msaaBackBufferTextures[0]
  3482. , 0
  3483. , _msaa
  3484. ) );
  3485. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  3486. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_msaaBackBufferTextures[1]) );
  3487. GL_CHECK(glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH24_STENCIL8, _width, _height) );
  3488. GL_CHECK(glFramebufferTexture2DMultisampleEXT(
  3489. GL_FRAMEBUFFER
  3490. , attachment
  3491. , GL_TEXTURE_2D
  3492. , m_msaaBackBufferTextures[1]
  3493. , 0
  3494. , _msaa
  3495. ) );
  3496. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, 0) );
  3497. BX_ASSERT(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)
  3498. , "glCheckFramebufferStatus failed 0x%08x"
  3499. , glCheckFramebufferStatus(GL_FRAMEBUFFER)
  3500. );
  3501. if (0 == m_msaaBlitProgram)
  3502. {
  3503. static const char* msaa_blit_vs =
  3504. R"(#version 300 es
  3505. precision highp float;
  3506. out vec2 UV;
  3507. void main()
  3508. {
  3509. float x = -1.0 + float( (gl_VertexID & 1) << 2);
  3510. float y = -1.0 + float( (gl_VertexID & 2) << 1);
  3511. gl_Position = vec4(x, y, 0, 1);
  3512. UV = vec2(gl_Position.x + 1.0, gl_Position.y + 1.0) * 0.5;
  3513. }
  3514. )";
  3515. static const char* msaa_blit_fs =
  3516. R"(#version 300 es
  3517. precision mediump float;
  3518. in vec2 UV;
  3519. uniform sampler2D msaaTexture;
  3520. out vec4 oFragColor;
  3521. void main()
  3522. {
  3523. oFragColor = texture(msaaTexture, UV);
  3524. }
  3525. )";
  3526. const GLchar *const vs = msaa_blit_vs;
  3527. const GLchar *const fs = msaa_blit_fs;
  3528. GLuint shader_vs = glCreateShader(GL_VERTEX_SHADER);
  3529. {
  3530. BX_WARN(0 != shader_vs, "Failed to create msaa Blit Vertex shader.");
  3531. GL_CHECK(glShaderSource(shader_vs, 1, &vs, NULL) );
  3532. GL_CHECK(glCompileShader(shader_vs) );
  3533. GLint compiled = 0;
  3534. GL_CHECK(glGetShaderiv(shader_vs, GL_COMPILE_STATUS, &compiled) );
  3535. BX_WARN(0 == shader_vs, "Unable to compile msaa Blit Vertex shader.");
  3536. }
  3537. GLuint shader_fs = glCreateShader(GL_FRAGMENT_SHADER);
  3538. {
  3539. BX_WARN(0 != shader_fs, "Failed to create msaa Blit Fragment shader.");
  3540. GL_CHECK(glShaderSource(shader_fs, 1, &fs, NULL) );
  3541. GL_CHECK(glCompileShader(shader_fs) );
  3542. GLint compiled = 0;
  3543. GL_CHECK(glGetShaderiv(shader_fs, GL_COMPILE_STATUS, &compiled) );
  3544. BX_WARN(0 == shader_vs, "Unable to compile msaa Blit Fragment shader.");
  3545. }
  3546. m_msaaBlitProgram = glCreateProgram();
  3547. if (m_msaaBlitProgram)
  3548. {
  3549. GL_CHECK(glAttachShader(m_msaaBlitProgram, shader_vs) );
  3550. GL_CHECK(glAttachShader(m_msaaBlitProgram, shader_fs) );
  3551. GL_CHECK(glLinkProgram(m_msaaBlitProgram) );
  3552. GLint linked = 0;
  3553. GL_CHECK(glGetProgramiv(m_msaaBlitProgram, GL_LINK_STATUS, &linked) );
  3554. if (0 == linked)
  3555. {
  3556. char log[1024];
  3557. GL_CHECK(glGetProgramInfoLog(
  3558. m_msaaBlitProgram
  3559. , sizeof(log)
  3560. , NULL
  3561. , log
  3562. ) );
  3563. BX_TRACE("%d: %s", linked, log);
  3564. }
  3565. GL_CHECK(glDetachShader(m_msaaBlitProgram, shader_vs) );
  3566. GL_CHECK(glDeleteShader(shader_vs) );
  3567. GL_CHECK(glDetachShader(m_msaaBlitProgram, shader_fs) );
  3568. GL_CHECK(glDeleteShader(shader_fs) );
  3569. }
  3570. }
  3571. }
  3572. else
  3573. {
  3574. GL_CHECK(glGenRenderbuffers(BX_COUNTOF(m_msaaBackBufferRbos), m_msaaBackBufferRbos) );
  3575. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_msaaBackBufferRbos[0]) );
  3576. GL_CHECK(glRenderbufferStorageMultisample(
  3577. GL_RENDERBUFFER
  3578. , _msaa
  3579. , storageFormat
  3580. , _width
  3581. , _height
  3582. ) );
  3583. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_msaaBackBufferRbos[1]) );
  3584. GL_CHECK(glRenderbufferStorageMultisample(
  3585. GL_RENDERBUFFER
  3586. , _msaa
  3587. , GL_DEPTH24_STENCIL8
  3588. , _width
  3589. , _height
  3590. ) );
  3591. GL_CHECK(glFramebufferRenderbuffer(
  3592. GL_FRAMEBUFFER
  3593. , GL_COLOR_ATTACHMENT0
  3594. , GL_RENDERBUFFER
  3595. , m_msaaBackBufferRbos[0]
  3596. ) );
  3597. GL_CHECK(glFramebufferRenderbuffer(
  3598. GL_FRAMEBUFFER
  3599. , attachment
  3600. , GL_RENDERBUFFER
  3601. , m_msaaBackBufferRbos[1]
  3602. ) );
  3603. BX_ASSERT(GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatus(GL_FRAMEBUFFER)
  3604. , "glCheckFramebufferStatus failed 0x%08x"
  3605. , glCheckFramebufferStatus(GL_FRAMEBUFFER)
  3606. );
  3607. }
  3608. }
  3609. }
  3610. void destroyMsaaFbo()
  3611. {
  3612. if (m_backBufferFbo != m_msaaBackBufferFbo // iOS
  3613. && 0 != m_msaaBackBufferFbo)
  3614. {
  3615. GL_CHECK(glDeleteFramebuffers(1, &m_msaaBackBufferFbo) );
  3616. m_msaaBackBufferFbo = 0;
  3617. if (m_gles3)
  3618. {
  3619. if (0 != m_msaaBackBufferTextures[0])
  3620. {
  3621. GL_CHECK(glDeleteTextures(BX_COUNTOF(m_msaaBackBufferTextures), m_msaaBackBufferTextures) );
  3622. m_msaaBackBufferTextures[0] = 0;
  3623. m_msaaBackBufferTextures[1] = 0;
  3624. }
  3625. if (0 != m_msaaBlitProgram)
  3626. {
  3627. GL_CHECK(glDeleteProgram(m_msaaBlitProgram) );
  3628. m_msaaBlitProgram = 0;
  3629. }
  3630. }
  3631. else
  3632. {
  3633. if (0 != m_msaaBackBufferRbos[0])
  3634. {
  3635. GL_CHECK(glDeleteRenderbuffers(BX_COUNTOF(m_msaaBackBufferRbos), m_msaaBackBufferRbos) );
  3636. m_msaaBackBufferRbos[0] = 0;
  3637. m_msaaBackBufferRbos[1] = 0;
  3638. }
  3639. }
  3640. }
  3641. }
  3642. void blitMsaaFbo()
  3643. {
  3644. if (m_backBufferFbo != m_msaaBackBufferFbo // iOS
  3645. && 0 != m_msaaBackBufferFbo)
  3646. {
  3647. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  3648. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  3649. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_msaaBackBufferFbo) );
  3650. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0) );
  3651. const uint32_t width = m_resolution.width;
  3652. const uint32_t height = m_resolution.height;
  3653. const GLenum filter = BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || !m_gles3
  3654. ? GL_NEAREST
  3655. : GL_LINEAR
  3656. ;
  3657. if (m_gles3)
  3658. {
  3659. GL_CHECK(glUseProgram(m_msaaBlitProgram) );
  3660. GL_CHECK(glActiveTexture(GL_TEXTURE0) );
  3661. GL_CHECK(glBindTexture(GL_TEXTURE_2D, m_msaaBackBufferTextures[0]) );
  3662. GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 3) );
  3663. }
  3664. else
  3665. {
  3666. GL_CHECK(glBlitFramebuffer(
  3667. 0
  3668. , 0
  3669. , width
  3670. , height
  3671. , 0
  3672. , 0
  3673. , width
  3674. , height
  3675. , GL_COLOR_BUFFER_BIT
  3676. , filter
  3677. ) );
  3678. }
  3679. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  3680. }
  3681. }
  3682. void setRenderContextSize(uint32_t _width, uint32_t _height, uint32_t _flags = 0)
  3683. {
  3684. if (!m_glctx.isValid() )
  3685. {
  3686. m_glctx.create(_width, _height, _flags);
  3687. }
  3688. else
  3689. {
  3690. destroyMsaaFbo();
  3691. m_glctx.resize(_width, _height, _flags);
  3692. uint32_t msaa = (_flags&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  3693. msaa = bx::uint32_min(m_maxMsaa, msaa == 0 ? 0 : 1<<msaa);
  3694. createMsaaFbo(_width, _height, msaa);
  3695. }
  3696. m_flip = true;
  3697. }
  3698. void invalidateCache()
  3699. {
  3700. if (m_samplerObjectSupport)
  3701. {
  3702. m_samplerStateCache.invalidate();
  3703. }
  3704. }
  3705. void setSamplerState(uint32_t _stage, uint32_t _numMips, uint32_t _flags, const float _rgba[4])
  3706. {
  3707. BX_ASSERT(m_samplerObjectSupport, "Cannot use Sampler Objects");
  3708. if (0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & _flags) )
  3709. {
  3710. const uint32_t index = (_flags & BGFX_SAMPLER_BORDER_COLOR_MASK) >> BGFX_SAMPLER_BORDER_COLOR_SHIFT;
  3711. _flags &= ~BGFX_SAMPLER_RESERVED_MASK;
  3712. _flags &= BGFX_SAMPLER_BITS_MASK;
  3713. _flags |= _numMips<<BGFX_SAMPLER_RESERVED_SHIFT;
  3714. GLuint sampler;
  3715. bool hasBorderColor = false;
  3716. bx::HashMurmur2A murmur;
  3717. uint32_t hash;
  3718. murmur.begin();
  3719. murmur.add(_flags);
  3720. if (!needBorderColor(_flags) )
  3721. {
  3722. murmur.add(-1);
  3723. hash = murmur.end();
  3724. sampler = m_samplerStateCache.find(hash);
  3725. }
  3726. else
  3727. {
  3728. murmur.add(index);
  3729. hash = murmur.end();
  3730. if (NULL != _rgba)
  3731. {
  3732. hasBorderColor = true;
  3733. sampler = UINT32_MAX;
  3734. }
  3735. else
  3736. {
  3737. sampler = m_samplerStateCache.find(hash);
  3738. }
  3739. }
  3740. if (UINT32_MAX == sampler)
  3741. {
  3742. sampler = m_samplerStateCache.add(hash);
  3743. GL_CHECK(glSamplerParameteri(sampler
  3744. , GL_TEXTURE_WRAP_S
  3745. , s_textureAddress[(_flags&BGFX_SAMPLER_U_MASK)>>BGFX_SAMPLER_U_SHIFT]
  3746. ) );
  3747. GL_CHECK(glSamplerParameteri(sampler
  3748. , GL_TEXTURE_WRAP_T
  3749. , s_textureAddress[(_flags&BGFX_SAMPLER_V_MASK)>>BGFX_SAMPLER_V_SHIFT]
  3750. ) );
  3751. GL_CHECK(glSamplerParameteri(sampler
  3752. , GL_TEXTURE_WRAP_R
  3753. , s_textureAddress[(_flags&BGFX_SAMPLER_W_MASK)>>BGFX_SAMPLER_W_SHIFT]
  3754. ) );
  3755. GLenum minFilter;
  3756. GLenum magFilter;
  3757. getFilters(_flags, 1 < _numMips, magFilter, minFilter);
  3758. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, magFilter) );
  3759. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, minFilter) );
  3760. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  3761. {
  3762. GL_CHECK(glSamplerParameterf(sampler, GL_TEXTURE_LOD_BIAS, float(BGFX_CONFIG_MIP_LOD_BIAS) ) );
  3763. }
  3764. if (m_borderColorSupport
  3765. && hasBorderColor)
  3766. {
  3767. GL_CHECK(glSamplerParameterfv(sampler, GL_TEXTURE_BORDER_COLOR, _rgba) );
  3768. }
  3769. if (0 != (_flags & (BGFX_SAMPLER_MIN_ANISOTROPIC|BGFX_SAMPLER_MAG_ANISOTROPIC) )
  3770. && 0.0f < m_maxAnisotropy)
  3771. {
  3772. GL_CHECK(glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, m_maxAnisotropy) );
  3773. }
  3774. if (m_gles3
  3775. || m_shadowSamplersSupport)
  3776. {
  3777. const uint32_t cmpFunc = (_flags&BGFX_SAMPLER_COMPARE_MASK)>>BGFX_SAMPLER_COMPARE_SHIFT;
  3778. if (0 == cmpFunc)
  3779. {
  3780. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_NONE) );
  3781. }
  3782. else
  3783. {
  3784. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) );
  3785. GL_CHECK(glSamplerParameteri(sampler, GL_TEXTURE_COMPARE_FUNC, s_cmpFunc[cmpFunc]) );
  3786. }
  3787. }
  3788. }
  3789. GL_CHECK(glBindSampler(_stage, sampler) );
  3790. }
  3791. else
  3792. {
  3793. GL_CHECK(glBindSampler(_stage, 0) );
  3794. }
  3795. }
  3796. bool isVisible(Frame* _render, OcclusionQueryHandle _handle, bool _visible)
  3797. {
  3798. m_occlusionQuery.resolve(_render);
  3799. return _visible == (0 != _render->m_occlusion[_handle.idx]);
  3800. }
  3801. void updateCapture()
  3802. {
  3803. if (m_resolution.reset&BGFX_RESET_CAPTURE)
  3804. {
  3805. m_captureSize = m_resolution.width*m_resolution.height*4;
  3806. m_capture = bx::realloc(g_allocator, m_capture, m_captureSize);
  3807. g_callback->captureBegin(m_resolution.width, m_resolution.height, m_resolution.width*4, TextureFormat::BGRA8, true);
  3808. }
  3809. else
  3810. {
  3811. captureFinish();
  3812. }
  3813. }
  3814. void capture()
  3815. {
  3816. if (NULL != m_capture)
  3817. {
  3818. GL_CHECK(glReadPixels(0
  3819. , 0
  3820. , m_resolution.width
  3821. , m_resolution.height
  3822. , m_readPixelsFmt
  3823. , GL_UNSIGNED_BYTE
  3824. , m_capture
  3825. ) );
  3826. if (GL_RGBA == m_readPixelsFmt)
  3827. {
  3828. bimg::imageSwizzleBgra8(
  3829. m_capture
  3830. , m_resolution.width*4
  3831. , m_resolution.width
  3832. , m_resolution.height
  3833. , m_capture
  3834. , m_resolution.width*4
  3835. );
  3836. }
  3837. g_callback->captureFrame(m_capture, m_captureSize);
  3838. }
  3839. }
  3840. void captureFinish()
  3841. {
  3842. if (NULL != m_capture)
  3843. {
  3844. g_callback->captureEnd();
  3845. bx::free(g_allocator, m_capture);
  3846. m_capture = NULL;
  3847. m_captureSize = 0;
  3848. }
  3849. }
  3850. bool programFetchFromCache(GLuint programId, uint64_t _id)
  3851. {
  3852. _id ^= m_hash;
  3853. bool cached = false;
  3854. if (m_programBinarySupport)
  3855. {
  3856. uint32_t length = g_callback->cacheReadSize(_id);
  3857. cached = length > 0;
  3858. if (cached)
  3859. {
  3860. void* data = bx::alloc(g_allocator, length);
  3861. if (g_callback->cacheRead(_id, data, length) )
  3862. {
  3863. bx::Error err;
  3864. bx::MemoryReader reader(data, length);
  3865. GLenum format;
  3866. bx::read(&reader, format, &err);
  3867. GL_CHECK(glProgramBinary(programId, format, reader.getDataPtr(), (GLsizei)reader.remaining() ) );
  3868. }
  3869. bx::free(g_allocator, data);
  3870. }
  3871. #if BGFX_CONFIG_RENDERER_OPENGL
  3872. GL_CHECK(glProgramParameteri(programId, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE) );
  3873. #endif // BGFX_CONFIG_RENDERER_OPENGL
  3874. }
  3875. return cached;
  3876. }
  3877. void programCache(GLuint programId, uint64_t _id)
  3878. {
  3879. _id ^= m_hash;
  3880. if (m_programBinarySupport)
  3881. {
  3882. GLint programLength;
  3883. GLenum format;
  3884. GL_CHECK(glGetProgramiv(programId, GL_PROGRAM_BINARY_LENGTH, &programLength) );
  3885. if (0 < programLength)
  3886. {
  3887. uint32_t length = programLength + 4;
  3888. uint8_t* data = (uint8_t*)bx::alloc(g_allocator, length);
  3889. GL_CHECK(glGetProgramBinary(programId, programLength, NULL, &format, &data[4]) );
  3890. *(uint32_t*)data = format;
  3891. g_callback->cacheWrite(_id, data, length);
  3892. bx::free(g_allocator, data);
  3893. }
  3894. }
  3895. }
  3896. void commit(UniformBuffer& _uniformBuffer)
  3897. {
  3898. _uniformBuffer.reset();
  3899. for (;;)
  3900. {
  3901. uint32_t opcode = _uniformBuffer.read();
  3902. if (UniformType::End == opcode)
  3903. {
  3904. break;
  3905. }
  3906. uint8_t type;
  3907. uint16_t ignore;
  3908. uint16_t num;
  3909. uint16_t copy;
  3910. UniformBuffer::decodeOpcode(opcode, type, ignore, num, copy);
  3911. const char* data;
  3912. if (copy)
  3913. {
  3914. data = _uniformBuffer.read(g_uniformTypeSize[type]*num);
  3915. }
  3916. else
  3917. {
  3918. UniformHandle handle;
  3919. bx::memCopy(&handle, _uniformBuffer.read(sizeof(UniformHandle) ), sizeof(UniformHandle) );
  3920. data = (const char*)m_uniforms[handle.idx];
  3921. }
  3922. uint32_t loc = _uniformBuffer.read();
  3923. switch (type)
  3924. {
  3925. #if BX_PLATFORM_EMSCRIPTEN
  3926. // For WebAssembly the array forms glUniform1iv/glUniform4fv are much slower compared to glUniform1i/glUniform4f
  3927. // since they need to marshal an array over from Wasm to JS, so optimize the case when there is exactly one
  3928. // uniform to upload.
  3929. case UniformType::Sampler:
  3930. if (num > 1)
  3931. {
  3932. setUniform1iv(loc, num, (int32_t*)data);
  3933. }
  3934. else
  3935. {
  3936. setUniform1i(loc, *(int32_t*)data);
  3937. }
  3938. break;
  3939. case UniformType::Vec4:
  3940. if (num > 1)
  3941. {
  3942. setUniform4fv(loc, num, (float*)data);
  3943. }
  3944. else
  3945. {
  3946. float* vec4 = (float*)data;
  3947. setUniform4f(loc, vec4[0], vec4[1], vec4[2], vec4[3]);
  3948. }
  3949. break;
  3950. #else
  3951. case UniformType::Sampler:
  3952. setUniform1iv(loc, num, (int32_t*)data);
  3953. break;
  3954. case UniformType::Vec4:
  3955. setUniform4fv(loc, num, (float*)data);
  3956. break;
  3957. #endif // BX_PLATFORM_EMSCRIPTEN
  3958. case UniformType::Mat3:
  3959. setUniformMatrix3fv(loc, num, GL_FALSE, (float*)data);
  3960. break;
  3961. case UniformType::Mat4:
  3962. setUniformMatrix4fv(loc, num, GL_FALSE, (float*)data);
  3963. break;
  3964. case UniformType::End:
  3965. break;
  3966. default:
  3967. BX_TRACE("%4d: INVALID 0x%08x, t %d, l %d, n %d, c %d", _uniformBuffer.getPos(), opcode, type, loc, num, copy);
  3968. break;
  3969. }
  3970. }
  3971. }
  3972. void clearQuad(ClearQuad& _clearQuad, const Rect& _rect, const Clear& _clear, uint32_t _height, const float _palette[][4])
  3973. {
  3974. uint32_t numMrt = 1;
  3975. FrameBufferHandle fbh = m_fbh;
  3976. if (isValid(fbh) )
  3977. {
  3978. const FrameBufferGL& fb = m_frameBuffers[fbh.idx];
  3979. numMrt = bx::uint32_max(1, fb.m_num);
  3980. }
  3981. if (1 == numMrt)
  3982. {
  3983. GLuint flags = 0;
  3984. if (BGFX_CLEAR_COLOR & _clear.m_flags)
  3985. {
  3986. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  3987. {
  3988. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[0]);
  3989. const float* rgba = _palette[index];
  3990. const float rr = rgba[0];
  3991. const float gg = rgba[1];
  3992. const float bb = rgba[2];
  3993. const float aa = rgba[3];
  3994. GL_CHECK(glClearColor(rr, gg, bb, aa) );
  3995. }
  3996. else
  3997. {
  3998. float rr = _clear.m_index[0]*1.0f/255.0f;
  3999. float gg = _clear.m_index[1]*1.0f/255.0f;
  4000. float bb = _clear.m_index[2]*1.0f/255.0f;
  4001. float aa = _clear.m_index[3]*1.0f/255.0f;
  4002. GL_CHECK(glClearColor(rr, gg, bb, aa) );
  4003. }
  4004. flags |= GL_COLOR_BUFFER_BIT;
  4005. GL_CHECK(glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE) );
  4006. }
  4007. if (BGFX_CLEAR_DEPTH & _clear.m_flags)
  4008. {
  4009. flags |= GL_DEPTH_BUFFER_BIT;
  4010. GL_CHECK(glClearDepth(_clear.m_depth) );
  4011. GL_CHECK(glDepthMask(GL_TRUE) );
  4012. }
  4013. if (BGFX_CLEAR_STENCIL & _clear.m_flags)
  4014. {
  4015. flags |= GL_STENCIL_BUFFER_BIT;
  4016. GL_CHECK(glClearStencil(_clear.m_stencil) );
  4017. }
  4018. if (0 != flags)
  4019. {
  4020. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  4021. GL_CHECK(glScissor(_rect.m_x, _height-_rect.m_height-_rect.m_y, _rect.m_width, _rect.m_height) );
  4022. GL_CHECK(glClear(flags) );
  4023. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  4024. }
  4025. }
  4026. else
  4027. {
  4028. if (0 != m_vao)
  4029. {
  4030. GL_CHECK(glBindVertexArray(m_vao) );
  4031. }
  4032. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  4033. GL_CHECK(glDisable(GL_CULL_FACE) );
  4034. GL_CHECK(glDisable(GL_BLEND) );
  4035. GLboolean colorMask = !!(BGFX_CLEAR_COLOR & _clear.m_flags);
  4036. GL_CHECK(glColorMask(colorMask, colorMask, colorMask, colorMask) );
  4037. if (BGFX_CLEAR_DEPTH & _clear.m_flags)
  4038. {
  4039. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  4040. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  4041. GL_CHECK(glDepthMask(GL_TRUE) );
  4042. }
  4043. else
  4044. {
  4045. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  4046. }
  4047. if (BGFX_CLEAR_STENCIL & _clear.m_flags)
  4048. {
  4049. GL_CHECK(glEnable(GL_STENCIL_TEST) );
  4050. GL_CHECK(glStencilFuncSeparate(GL_FRONT_AND_BACK, GL_ALWAYS, _clear.m_stencil, 0xff) );
  4051. GL_CHECK(glStencilOpSeparate(GL_FRONT_AND_BACK, GL_REPLACE, GL_REPLACE, GL_REPLACE) );
  4052. }
  4053. else
  4054. {
  4055. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  4056. }
  4057. VertexBufferGL& vb = m_vertexBuffers[_clearQuad.m_vb.idx];
  4058. VertexLayout& layout = _clearQuad.m_layout;
  4059. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  4060. ProgramGL& program = m_program[_clearQuad.m_program[numMrt-1].idx];
  4061. setProgram(program.m_id);
  4062. program.bindAttributesBegin();
  4063. program.bindAttributes(layout, 0);
  4064. program.bindAttributesEnd();
  4065. if (m_clearQuadColor.idx == kInvalidHandle)
  4066. {
  4067. const UniformRegInfo* infoClearColor = m_uniformReg.find("bgfx_clear_color");
  4068. if (NULL != infoClearColor)
  4069. {
  4070. m_clearQuadColor = infoClearColor->m_handle;
  4071. }
  4072. }
  4073. if (m_clearQuadDepth.idx == kInvalidHandle)
  4074. {
  4075. const UniformRegInfo* infoClearDepth = m_uniformReg.find("bgfx_clear_depth");
  4076. if (NULL != infoClearDepth)
  4077. {
  4078. m_clearQuadDepth = infoClearDepth->m_handle;
  4079. }
  4080. }
  4081. float mrtClearDepth[4] = { g_caps.homogeneousDepth ? (_clear.m_depth * 2.0f - 1.0f) : _clear.m_depth };
  4082. updateUniform(m_clearQuadDepth.idx, mrtClearDepth, sizeof(float)*4);
  4083. float mrtClearColor[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS][4];
  4084. if (BGFX_CLEAR_COLOR_USE_PALETTE & _clear.m_flags)
  4085. {
  4086. for (uint32_t ii = 0; ii < numMrt; ++ii)
  4087. {
  4088. uint8_t index = (uint8_t)bx::uint32_min(BGFX_CONFIG_MAX_COLOR_PALETTE-1, _clear.m_index[ii]);
  4089. bx::memCopy(mrtClearColor[ii], _palette[index], 16);
  4090. }
  4091. }
  4092. else
  4093. {
  4094. float rgba[4] =
  4095. {
  4096. _clear.m_index[0] * 1.0f / 255.0f,
  4097. _clear.m_index[1] * 1.0f / 255.0f,
  4098. _clear.m_index[2] * 1.0f / 255.0f,
  4099. _clear.m_index[3] * 1.0f / 255.0f,
  4100. };
  4101. for (uint32_t ii = 0; ii < numMrt; ++ii)
  4102. {
  4103. bx::memCopy(mrtClearColor[ii], rgba, 16);
  4104. }
  4105. }
  4106. updateUniform(m_clearQuadColor.idx, mrtClearColor[0], numMrt * sizeof(float) * 4);
  4107. commit(*program.m_constantBuffer);
  4108. GL_CHECK(glDrawArrays(GL_TRIANGLE_STRIP
  4109. , 0
  4110. , 4
  4111. ) );
  4112. }
  4113. }
  4114. void setProgram(GLuint program)
  4115. {
  4116. m_uniformStateCache.saveCurrentProgram(program);
  4117. GL_CHECK(glUseProgram(program) );
  4118. }
  4119. // Cache uniform uploads to avoid redundant uploading of state that is
  4120. // already set to a shader program
  4121. void setUniform1i(uint32_t loc, int value)
  4122. {
  4123. if (m_uniformStateCache.updateUniformCache(loc, value) )
  4124. {
  4125. GL_CHECK(glUniform1i(loc, value) );
  4126. }
  4127. }
  4128. void setUniform1iv(uint32_t loc, int num, const int *data)
  4129. {
  4130. bool changed = false;
  4131. for(int i = 0; i < num; ++i)
  4132. {
  4133. if (m_uniformStateCache.updateUniformCache(loc+i, data[i]) )
  4134. {
  4135. changed = true;
  4136. }
  4137. }
  4138. if (changed)
  4139. {
  4140. GL_CHECK(glUniform1iv(loc, num, data) );
  4141. }
  4142. }
  4143. void setUniform4f(uint32_t loc, float x, float y, float z, float w)
  4144. {
  4145. UniformStateCache::f4 f; f.val[0] = x; f.val[1] = y; f.val[2] = z; f.val[3] = w;
  4146. if (m_uniformStateCache.updateUniformCache(loc, f) )
  4147. {
  4148. GL_CHECK(glUniform4f(loc, x, y, z, w) );
  4149. }
  4150. }
  4151. void setUniform4fv(uint32_t loc, int num, const float *data)
  4152. {
  4153. bool changed = false;
  4154. for(int i = 0; i < num; ++i)
  4155. {
  4156. if (m_uniformStateCache.updateUniformCache(loc+i, *(const UniformStateCache::f4*)&data[4*i]) )
  4157. {
  4158. changed = true;
  4159. }
  4160. }
  4161. if (changed)
  4162. {
  4163. GL_CHECK(glUniform4fv(loc, num, data) );
  4164. }
  4165. }
  4166. void setUniformMatrix3fv(uint32_t loc, int num, GLboolean transpose, const float *data)
  4167. {
  4168. bool changed = false;
  4169. for(int i = 0; i < num; ++i)
  4170. {
  4171. if (m_uniformStateCache.updateUniformCache(loc+i, *(const UniformStateCache::f3x3*)&data[9*i]) )
  4172. {
  4173. changed = true;
  4174. }
  4175. }
  4176. if (changed)
  4177. {
  4178. GL_CHECK(glUniformMatrix3fv(loc, num, transpose, data) );
  4179. }
  4180. }
  4181. void setUniformMatrix4fv(uint32_t loc, int num, GLboolean transpose, const float *data)
  4182. {
  4183. bool changed = false;
  4184. for(int i = 0; i < num; ++i)
  4185. {
  4186. if (m_uniformStateCache.updateUniformCache(loc+i, *(const UniformStateCache::f4x4*)&data[16*i]) )
  4187. {
  4188. changed = true;
  4189. }
  4190. }
  4191. if (changed)
  4192. {
  4193. GL_CHECK(glUniformMatrix4fv(loc, num, transpose, data) );
  4194. }
  4195. }
  4196. void* m_renderdocdll;
  4197. uint16_t m_numWindows;
  4198. FrameBufferHandle m_windows[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  4199. IndexBufferGL m_indexBuffers[BGFX_CONFIG_MAX_INDEX_BUFFERS];
  4200. VertexBufferGL m_vertexBuffers[BGFX_CONFIG_MAX_VERTEX_BUFFERS];
  4201. ShaderGL m_shaders[BGFX_CONFIG_MAX_SHADERS];
  4202. ProgramGL m_program[BGFX_CONFIG_MAX_PROGRAMS];
  4203. TextureGL m_textures[BGFX_CONFIG_MAX_TEXTURES];
  4204. VertexLayout m_vertexLayouts[BGFX_CONFIG_MAX_VERTEX_LAYOUTS];
  4205. FrameBufferGL m_frameBuffers[BGFX_CONFIG_MAX_FRAME_BUFFERS];
  4206. UniformRegistry m_uniformReg;
  4207. void* m_uniforms[BGFX_CONFIG_MAX_UNIFORMS];
  4208. TimerQueryGL m_gpuTimer;
  4209. OcclusionQueryGL m_occlusionQuery;
  4210. SamplerStateCache m_samplerStateCache;
  4211. UniformStateCache m_uniformStateCache;
  4212. TextVideoMem m_textVideoMem;
  4213. bool m_rtMsaa;
  4214. FrameBufferHandle m_fbh;
  4215. uint16_t m_fbDiscard;
  4216. Resolution m_resolution;
  4217. void* m_capture;
  4218. uint32_t m_captureSize;
  4219. float m_maxAnisotropy;
  4220. float m_maxAnisotropyDefault;
  4221. int32_t m_maxMsaa;
  4222. GLuint m_vao;
  4223. uint16_t m_maxLabelLen;
  4224. bool m_blitSupported;
  4225. bool m_readBackSupported;
  4226. bool m_vaoSupport;
  4227. bool m_samplerObjectSupport;
  4228. bool m_shadowSamplersSupport;
  4229. bool m_srgbWriteControlSupport;
  4230. bool m_borderColorSupport;
  4231. bool m_programBinarySupport;
  4232. bool m_textureSwizzleSupport;
  4233. bool m_depthTextureSupport;
  4234. bool m_timerQuerySupport;
  4235. bool m_occlusionQuerySupport;
  4236. bool m_atocSupport;
  4237. bool m_conservativeRasterSupport;
  4238. bool m_imageLoadStoreSupport;
  4239. bool m_flip;
  4240. uint64_t m_hash;
  4241. GLenum m_readPixelsFmt;
  4242. GLuint m_backBufferFbo;
  4243. GLuint m_msaaBackBufferFbo;
  4244. union {
  4245. GLuint m_msaaBackBufferRbos[2];
  4246. GLuint m_msaaBackBufferTextures[2];
  4247. };
  4248. GLuint m_msaaBlitProgram;
  4249. GlContext m_glctx;
  4250. bool m_needPresent;
  4251. UniformHandle m_clearQuadColor;
  4252. UniformHandle m_clearQuadDepth;
  4253. const char* m_vendor;
  4254. const char* m_renderer;
  4255. const char* m_version;
  4256. const char* m_glslVersion;
  4257. bool m_gles3;
  4258. Workaround m_workaround;
  4259. GLuint m_currentFbo;
  4260. };
  4261. RendererContextGL* s_renderGL;
  4262. RendererContextI* rendererCreate(const Init& _init)
  4263. {
  4264. s_renderGL = BX_NEW(g_allocator, RendererContextGL);
  4265. if (!s_renderGL->init(_init) )
  4266. {
  4267. bx::deleteObject(g_allocator, s_renderGL);
  4268. s_renderGL = NULL;
  4269. }
  4270. return s_renderGL;
  4271. }
  4272. void rendererDestroy()
  4273. {
  4274. s_renderGL->shutdown();
  4275. bx::deleteObject(g_allocator, s_renderGL);
  4276. s_renderGL = NULL;
  4277. }
  4278. static void frameBufferValidate()
  4279. {
  4280. GLenum complete = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  4281. BX_ASSERT(GL_FRAMEBUFFER_COMPLETE == complete
  4282. , "glCheckFramebufferStatus failed 0x%08x: %s"
  4283. , complete
  4284. , glEnumName(complete)
  4285. );
  4286. BX_UNUSED(complete);
  4287. }
  4288. const char* glslTypeName(GLuint _type)
  4289. {
  4290. #define GLSL_TYPE(_ty) case _ty: return #_ty
  4291. switch (_type)
  4292. {
  4293. GLSL_TYPE(GL_BOOL);
  4294. GLSL_TYPE(GL_INT);
  4295. GLSL_TYPE(GL_INT_VEC2);
  4296. GLSL_TYPE(GL_INT_VEC3);
  4297. GLSL_TYPE(GL_INT_VEC4);
  4298. GLSL_TYPE(GL_UNSIGNED_INT);
  4299. GLSL_TYPE(GL_UNSIGNED_INT_VEC2);
  4300. GLSL_TYPE(GL_UNSIGNED_INT_VEC3);
  4301. GLSL_TYPE(GL_UNSIGNED_INT_VEC4);
  4302. GLSL_TYPE(GL_FLOAT);
  4303. GLSL_TYPE(GL_FLOAT_VEC2);
  4304. GLSL_TYPE(GL_FLOAT_VEC3);
  4305. GLSL_TYPE(GL_FLOAT_VEC4);
  4306. GLSL_TYPE(GL_FLOAT_MAT2);
  4307. GLSL_TYPE(GL_FLOAT_MAT3);
  4308. GLSL_TYPE(GL_FLOAT_MAT4);
  4309. GLSL_TYPE(GL_SAMPLER_2D);
  4310. GLSL_TYPE(GL_SAMPLER_2D_ARRAY);
  4311. GLSL_TYPE(GL_SAMPLER_2D_MULTISAMPLE);
  4312. GLSL_TYPE(GL_INT_SAMPLER_2D);
  4313. GLSL_TYPE(GL_INT_SAMPLER_2D_ARRAY);
  4314. GLSL_TYPE(GL_INT_SAMPLER_2D_MULTISAMPLE);
  4315. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_2D);
  4316. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_2D_ARRAY);
  4317. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE);
  4318. GLSL_TYPE(GL_SAMPLER_2D_SHADOW);
  4319. GLSL_TYPE(GL_SAMPLER_2D_ARRAY_SHADOW);
  4320. GLSL_TYPE(GL_SAMPLER_3D);
  4321. GLSL_TYPE(GL_INT_SAMPLER_3D);
  4322. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_3D);
  4323. GLSL_TYPE(GL_SAMPLER_CUBE);
  4324. GLSL_TYPE(GL_INT_SAMPLER_CUBE);
  4325. GLSL_TYPE(GL_UNSIGNED_INT_SAMPLER_CUBE);
  4326. GLSL_TYPE(GL_IMAGE_1D);
  4327. GLSL_TYPE(GL_INT_IMAGE_1D);
  4328. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_1D);
  4329. GLSL_TYPE(GL_IMAGE_2D);
  4330. GLSL_TYPE(GL_IMAGE_2D_ARRAY);
  4331. GLSL_TYPE(GL_INT_IMAGE_2D);
  4332. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_2D);
  4333. GLSL_TYPE(GL_IMAGE_3D);
  4334. GLSL_TYPE(GL_INT_IMAGE_3D);
  4335. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_3D);
  4336. GLSL_TYPE(GL_IMAGE_CUBE);
  4337. GLSL_TYPE(GL_INT_IMAGE_CUBE);
  4338. GLSL_TYPE(GL_UNSIGNED_INT_IMAGE_CUBE);
  4339. }
  4340. #undef GLSL_TYPE
  4341. BX_ASSERT(false, "Unknown GLSL type? %x", _type);
  4342. return "UNKNOWN GLSL TYPE!";
  4343. }
  4344. const char* glEnumName(GLenum _enum)
  4345. {
  4346. #define GLENUM(_ty) case _ty: return #_ty
  4347. switch (_enum)
  4348. {
  4349. GLENUM(GL_TEXTURE);
  4350. GLENUM(GL_RENDERBUFFER);
  4351. GLENUM(GL_INVALID_ENUM);
  4352. GLENUM(GL_INVALID_FRAMEBUFFER_OPERATION);
  4353. GLENUM(GL_INVALID_VALUE);
  4354. GLENUM(GL_INVALID_OPERATION);
  4355. GLENUM(GL_OUT_OF_MEMORY);
  4356. GLENUM(GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT);
  4357. GLENUM(GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT);
  4358. // GLENUM(GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER);
  4359. // GLENUM(GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER);
  4360. GLENUM(GL_FRAMEBUFFER_UNSUPPORTED);
  4361. }
  4362. #undef GLENUM
  4363. BX_WARN(false, "Unknown enum? %x", _enum);
  4364. return "<GLenum?>";
  4365. }
  4366. UniformType::Enum convertGlType(GLenum _type)
  4367. {
  4368. switch (_type)
  4369. {
  4370. case GL_INT:
  4371. case GL_UNSIGNED_INT:
  4372. return UniformType::Sampler;
  4373. case GL_FLOAT:
  4374. case GL_FLOAT_VEC2:
  4375. case GL_FLOAT_VEC3:
  4376. case GL_FLOAT_VEC4:
  4377. return UniformType::Vec4;
  4378. case GL_FLOAT_MAT2:
  4379. break;
  4380. case GL_FLOAT_MAT3:
  4381. return UniformType::Mat3;
  4382. case GL_FLOAT_MAT4:
  4383. return UniformType::Mat4;
  4384. case GL_SAMPLER_2D:
  4385. case GL_SAMPLER_2D_ARRAY:
  4386. case GL_SAMPLER_2D_MULTISAMPLE:
  4387. case GL_INT_SAMPLER_2D:
  4388. case GL_INT_SAMPLER_2D_ARRAY:
  4389. case GL_INT_SAMPLER_2D_MULTISAMPLE:
  4390. case GL_UNSIGNED_INT_SAMPLER_2D:
  4391. case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
  4392. case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
  4393. case GL_SAMPLER_2D_SHADOW:
  4394. case GL_SAMPLER_2D_ARRAY_SHADOW:
  4395. case GL_SAMPLER_3D:
  4396. case GL_INT_SAMPLER_3D:
  4397. case GL_UNSIGNED_INT_SAMPLER_3D:
  4398. case GL_SAMPLER_CUBE:
  4399. case GL_INT_SAMPLER_CUBE:
  4400. case GL_UNSIGNED_INT_SAMPLER_CUBE:
  4401. case GL_IMAGE_1D:
  4402. case GL_INT_IMAGE_1D:
  4403. case GL_UNSIGNED_INT_IMAGE_1D:
  4404. case GL_IMAGE_2D:
  4405. case GL_IMAGE_2D_ARRAY:
  4406. case GL_INT_IMAGE_2D:
  4407. case GL_UNSIGNED_INT_IMAGE_2D:
  4408. case GL_IMAGE_3D:
  4409. case GL_INT_IMAGE_3D:
  4410. case GL_UNSIGNED_INT_IMAGE_3D:
  4411. case GL_IMAGE_CUBE:
  4412. case GL_INT_IMAGE_CUBE:
  4413. case GL_UNSIGNED_INT_IMAGE_CUBE:
  4414. return UniformType::Sampler;
  4415. };
  4416. BX_ASSERT(false, "Unrecognized GL type 0x%04x.", _type);
  4417. return UniformType::End;
  4418. }
  4419. void ProgramGL::create(const ShaderGL& _vsh, const ShaderGL& _fsh)
  4420. {
  4421. m_id = glCreateProgram();
  4422. BX_TRACE("Program create: GL%d: GL%d, GL%d", m_id, _vsh.m_id, _fsh.m_id);
  4423. const uint64_t id = (uint64_t(_vsh.m_hash)<<32) | _fsh.m_hash;
  4424. const bool cached = s_renderGL->programFetchFromCache(m_id, id);
  4425. if (!cached)
  4426. {
  4427. GLint linked = 0;
  4428. if (0 != _vsh.m_id)
  4429. {
  4430. GL_CHECK(glAttachShader(m_id, _vsh.m_id) );
  4431. if (0 != _fsh.m_id)
  4432. {
  4433. GL_CHECK(glAttachShader(m_id, _fsh.m_id) );
  4434. }
  4435. GL_CHECK(glLinkProgram(m_id) );
  4436. GL_CHECK(glGetProgramiv(m_id, GL_LINK_STATUS, &linked) );
  4437. if (0 == linked)
  4438. {
  4439. char log[1024];
  4440. GL_CHECK(glGetProgramInfoLog(m_id, sizeof(log), NULL, log) );
  4441. BX_TRACE("%d: %s", linked, log);
  4442. }
  4443. }
  4444. if (0 == linked)
  4445. {
  4446. BX_WARN(0 != _vsh.m_id, "Invalid vertex/compute shader.");
  4447. GL_CHECK(glDeleteProgram(m_id) );
  4448. m_usedCount = 0;
  4449. m_id = 0;
  4450. return;
  4451. }
  4452. s_renderGL->programCache(m_id, id);
  4453. }
  4454. init();
  4455. if (!cached
  4456. && s_renderGL->m_workaround.m_detachShader)
  4457. {
  4458. // Must be after init, otherwise init might fail to lookup shader
  4459. // info (NVIDIA Tegra 3 OpenGL ES 2.0 14.01003).
  4460. GL_CHECK(glDetachShader(m_id, _vsh.m_id) );
  4461. if (0 != _fsh.m_id)
  4462. {
  4463. GL_CHECK(glDetachShader(m_id, _fsh.m_id) );
  4464. }
  4465. }
  4466. }
  4467. void ProgramGL::destroy()
  4468. {
  4469. if (NULL != m_constantBuffer)
  4470. {
  4471. UniformBuffer::destroy(m_constantBuffer);
  4472. m_constantBuffer = NULL;
  4473. }
  4474. m_numPredefined = 0;
  4475. if (0 != m_id)
  4476. {
  4477. s_renderGL->setProgram(0);
  4478. GL_CHECK(glDeleteProgram(m_id) );
  4479. m_id = 0;
  4480. }
  4481. }
  4482. void ProgramGL::init()
  4483. {
  4484. GLint activeAttribs = 0;
  4485. GLint activeUniforms = 0;
  4486. GLint activeBuffers = 0;
  4487. #if BGFX_CONFIG_RENDERER_OPENGL >= 31
  4488. GL_CHECK(glBindFragDataLocation(m_id, 0, "bgfx_FragColor") );
  4489. #endif // BGFX_CONFIG_RENDERER_OPENGL >= 31
  4490. GLint max0, max1;
  4491. bool piqSupported = true
  4492. && s_extension[Extension::ARB_program_interface_query ].m_supported
  4493. && s_extension[Extension::ARB_shader_storage_buffer_object].m_supported
  4494. ;
  4495. if (piqSupported)
  4496. {
  4497. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_PROGRAM_INPUT, GL_ACTIVE_RESOURCES, &activeAttribs ) );
  4498. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_UNIFORM, GL_ACTIVE_RESOURCES, &activeUniforms) );
  4499. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_BUFFER_VARIABLE, GL_ACTIVE_RESOURCES, &activeBuffers ) );
  4500. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_PROGRAM_INPUT, GL_MAX_NAME_LENGTH, &max0 ) );
  4501. GL_CHECK(glGetProgramInterfaceiv(m_id, GL_UNIFORM, GL_MAX_NAME_LENGTH, &max1 ) );
  4502. }
  4503. else
  4504. {
  4505. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTES, &activeAttribs ) );
  4506. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORMS, &activeUniforms) );
  4507. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max0) );
  4508. GL_CHECK(glGetProgramiv(m_id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max1) );
  4509. }
  4510. uint32_t maxLength = bx::uint32_max(max0, max1);
  4511. char* name = (char*)BX_STACK_ALLOC(maxLength + 1);
  4512. BX_TRACE("Program %d", m_id);
  4513. BX_TRACE("Attributes (%d):", activeAttribs);
  4514. for (int32_t ii = 0; ii < activeAttribs; ++ii)
  4515. {
  4516. GLint size;
  4517. GLenum type = 0;
  4518. if (piqSupported)
  4519. {
  4520. GL_CHECK(glGetProgramResourceName(m_id, GL_PROGRAM_INPUT, ii, maxLength + 1, &size, name) );
  4521. GLenum typeProp[] = { GL_TYPE };
  4522. GL_CHECK(glGetProgramResourceiv(m_id
  4523. , GL_PROGRAM_INPUT
  4524. , ii
  4525. , BX_COUNTOF(typeProp)
  4526. , typeProp
  4527. , 1
  4528. , NULL
  4529. , (GLint *)&type)
  4530. );
  4531. }
  4532. else
  4533. {
  4534. GL_CHECK(glGetActiveAttrib(m_id, ii, maxLength + 1, NULL, &size, &type, name) );
  4535. }
  4536. BX_TRACE("\t%s %s is at location %d"
  4537. , glslTypeName(type)
  4538. , name
  4539. , glGetAttribLocation(m_id, name)
  4540. );
  4541. }
  4542. m_numPredefined = 0;
  4543. m_numSamplers = 0;
  4544. BX_TRACE("Uniforms (%d):", activeUniforms);
  4545. for (int32_t ii = 0; ii < activeUniforms; ++ii)
  4546. {
  4547. struct VariableInfo
  4548. {
  4549. GLenum type;
  4550. GLint loc;
  4551. GLint num;
  4552. };
  4553. VariableInfo vi;
  4554. GLenum props[] = { GL_TYPE, GL_LOCATION, GL_ARRAY_SIZE };
  4555. GLenum gltype;
  4556. GLint num;
  4557. GLint loc;
  4558. if (piqSupported)
  4559. {
  4560. GL_CHECK(glGetProgramResourceiv(m_id
  4561. , GL_UNIFORM
  4562. , ii
  4563. , BX_COUNTOF(props)
  4564. , props
  4565. , BX_COUNTOF(props)
  4566. , NULL
  4567. , (GLint*)&vi
  4568. ) );
  4569. GL_CHECK(glGetProgramResourceName(m_id
  4570. , GL_UNIFORM
  4571. , ii
  4572. , maxLength + 1
  4573. , NULL
  4574. , name
  4575. ) );
  4576. gltype = vi.type;
  4577. loc = vi.loc;
  4578. num = vi.num;
  4579. }
  4580. else
  4581. {
  4582. GL_CHECK(glGetActiveUniform(m_id, ii, maxLength + 1, NULL, &num, &gltype, name) );
  4583. loc = glGetUniformLocation(m_id, name);
  4584. }
  4585. num = bx::uint32_max(num, 1);
  4586. int32_t offset = 0;
  4587. const bx::StringView array = bx::strFind(name, '[');
  4588. if (!array.isEmpty() )
  4589. {
  4590. name[array.getPtr() - name] = '\0';
  4591. BX_TRACE("--- %s", name);
  4592. const bx::StringView end = bx::strFind(array.getPtr()+1, ']');
  4593. bx::fromString(&offset, bx::StringView(array.getPtr()+1, end.getPtr() ) );
  4594. }
  4595. switch (gltype)
  4596. {
  4597. case GL_SAMPLER_2D:
  4598. case GL_SAMPLER_2D_ARRAY:
  4599. case GL_SAMPLER_2D_MULTISAMPLE:
  4600. case GL_INT_SAMPLER_2D:
  4601. case GL_INT_SAMPLER_2D_ARRAY:
  4602. case GL_INT_SAMPLER_2D_MULTISAMPLE:
  4603. case GL_UNSIGNED_INT_SAMPLER_2D:
  4604. case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
  4605. case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
  4606. case GL_SAMPLER_2D_SHADOW:
  4607. case GL_SAMPLER_2D_ARRAY_SHADOW:
  4608. case GL_SAMPLER_3D:
  4609. case GL_INT_SAMPLER_3D:
  4610. case GL_UNSIGNED_INT_SAMPLER_3D:
  4611. case GL_SAMPLER_CUBE:
  4612. case GL_INT_SAMPLER_CUBE:
  4613. case GL_UNSIGNED_INT_SAMPLER_CUBE:
  4614. case GL_IMAGE_1D:
  4615. case GL_INT_IMAGE_1D:
  4616. case GL_UNSIGNED_INT_IMAGE_1D:
  4617. case GL_IMAGE_2D:
  4618. case GL_INT_IMAGE_2D:
  4619. case GL_UNSIGNED_INT_IMAGE_2D:
  4620. case GL_IMAGE_3D:
  4621. case GL_INT_IMAGE_3D:
  4622. case GL_UNSIGNED_INT_IMAGE_3D:
  4623. case GL_IMAGE_CUBE:
  4624. case GL_INT_IMAGE_CUBE:
  4625. case GL_UNSIGNED_INT_IMAGE_CUBE:
  4626. if (m_numSamplers < BX_COUNTOF(m_sampler) )
  4627. {
  4628. BX_TRACE("Sampler #%d at location %d.", m_numSamplers, loc);
  4629. m_sampler[m_numSamplers] = loc;
  4630. m_numSamplers++;
  4631. }
  4632. else
  4633. {
  4634. BX_TRACE("Too many samplers (max: %d)! Sampler at location %d."
  4635. , BX_COUNTOF(m_sampler)
  4636. , loc
  4637. );
  4638. }
  4639. break;
  4640. default:
  4641. break;
  4642. }
  4643. PredefinedUniform::Enum predefined = nameToPredefinedUniformEnum(name);
  4644. if (PredefinedUniform::Count != predefined)
  4645. {
  4646. m_predefined[m_numPredefined].m_loc = loc;
  4647. m_predefined[m_numPredefined].m_count = uint16_t(num);
  4648. m_predefined[m_numPredefined].m_type = uint8_t(predefined);
  4649. m_numPredefined++;
  4650. }
  4651. else
  4652. {
  4653. const UniformRegInfo* info = s_renderGL->m_uniformReg.find(name);
  4654. BX_WARN(NULL != info, "User defined uniform '%s' is not found, it won't be set.", name);
  4655. if (NULL != info)
  4656. {
  4657. if (NULL == m_constantBuffer)
  4658. {
  4659. m_constantBuffer = UniformBuffer::create(1024);
  4660. }
  4661. UniformType::Enum type = convertGlType(gltype);
  4662. m_constantBuffer->writeUniformHandle(type, 0, info->m_handle, uint16_t(num) );
  4663. m_constantBuffer->write(loc);
  4664. BX_TRACE("store %s %d", name, info->m_handle);
  4665. }
  4666. }
  4667. BX_TRACE("\tuniform %s %s%s is at location %d, size %d, offset %d"
  4668. , glslTypeName(gltype)
  4669. , name
  4670. , PredefinedUniform::Count != predefined ? "*" : ""
  4671. , loc
  4672. , num
  4673. , offset
  4674. );
  4675. BX_UNUSED(offset);
  4676. }
  4677. if (NULL != m_constantBuffer)
  4678. {
  4679. m_constantBuffer->finish();
  4680. }
  4681. if (piqSupported)
  4682. {
  4683. struct VariableInfo
  4684. {
  4685. GLenum type;
  4686. };
  4687. VariableInfo vi;
  4688. GLenum props[] = { GL_TYPE };
  4689. BX_TRACE("Buffers (%d):", activeBuffers);
  4690. for (int32_t ii = 0; ii < activeBuffers; ++ii)
  4691. {
  4692. GL_CHECK(glGetProgramResourceiv(m_id
  4693. , GL_BUFFER_VARIABLE
  4694. , ii
  4695. , BX_COUNTOF(props)
  4696. , props
  4697. , BX_COUNTOF(props)
  4698. , NULL
  4699. , (GLint*)&vi
  4700. ) );
  4701. GL_CHECK(glGetProgramResourceName(m_id
  4702. , GL_BUFFER_VARIABLE
  4703. , ii
  4704. , maxLength + 1
  4705. , NULL
  4706. , name
  4707. ) );
  4708. BX_TRACE("\t%s %s at %d"
  4709. , glslTypeName(vi.type)
  4710. , name
  4711. , 0 //vi.loc
  4712. );
  4713. }
  4714. }
  4715. bx::memSet(m_attributes, 0xff, sizeof(m_attributes) );
  4716. uint32_t used = 0;
  4717. for (uint8_t ii = 0; ii < Attrib::Count; ++ii)
  4718. {
  4719. GLint loc = glGetAttribLocation(m_id, s_attribName[ii]);
  4720. if (-1 != loc)
  4721. {
  4722. BX_TRACE("attr %s: %d", s_attribName[ii], loc);
  4723. m_attributes[ii] = loc;
  4724. m_used[used++] = ii;
  4725. }
  4726. }
  4727. BX_ASSERT(used < BX_COUNTOF(m_used), "Out of bounds %d > array size %d.", used, Attrib::Count);
  4728. m_usedCount = (uint8_t)used;
  4729. used = 0;
  4730. for (uint32_t ii = 0, baseVertex = 0; ii < BX_COUNTOF(s_instanceDataName); ++ii, baseVertex += 16)
  4731. {
  4732. GLint loc = glGetAttribLocation(m_id, s_instanceDataName[ii]);
  4733. if (-1 != loc)
  4734. {
  4735. BX_TRACE("instance data %s: %d", s_instanceDataName[ii], loc);
  4736. m_instanceData[used] = loc;
  4737. m_instanceOffset[used] = uint16_t(baseVertex);
  4738. used++;
  4739. }
  4740. }
  4741. BX_ASSERT(used < BX_COUNTOF(m_instanceData)
  4742. , "Out of bounds %d > array size %d."
  4743. , used
  4744. , BX_COUNTOF(m_instanceData)
  4745. );
  4746. m_instanceData[used] = -1;
  4747. }
  4748. void ProgramGL::bindAttributesBegin()
  4749. {
  4750. bx::memCopy(m_unboundUsedAttrib, m_used, sizeof(m_unboundUsedAttrib) );
  4751. }
  4752. void ProgramGL::bindAttributes(const VertexLayout& _layout, uint32_t _baseVertex)
  4753. {
  4754. for (uint32_t ii = 0, iiEnd = m_usedCount; ii < iiEnd; ++ii)
  4755. {
  4756. Attrib::Enum attr = Attrib::Enum(m_used[ii]);
  4757. GLint loc = m_attributes[attr];
  4758. uint8_t num;
  4759. AttribType::Enum type;
  4760. bool normalized;
  4761. bool asInt;
  4762. _layout.decode(attr, num, type, normalized, asInt);
  4763. if (-1 != loc)
  4764. {
  4765. if (UINT16_MAX != _layout.m_attributes[attr])
  4766. {
  4767. lazyEnableVertexAttribArray(loc);
  4768. GL_CHECK(glVertexAttribDivisor(loc, 0) );
  4769. uint32_t baseVertex = _baseVertex*_layout.m_stride + _layout.m_offset[attr];
  4770. if ( (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 30) || s_renderGL->m_gles3)
  4771. && !isFloat(type)
  4772. && !normalized)
  4773. {
  4774. GL_CHECK(glVertexAttribIPointer(loc
  4775. , num
  4776. , s_attribType[type]
  4777. , _layout.m_stride
  4778. , (void*)(uintptr_t)baseVertex)
  4779. );
  4780. }
  4781. else
  4782. {
  4783. GL_CHECK(glVertexAttribPointer(loc
  4784. , num
  4785. , s_attribType[type]
  4786. , normalized
  4787. , _layout.m_stride
  4788. , (void*)(uintptr_t)baseVertex)
  4789. );
  4790. }
  4791. m_unboundUsedAttrib[ii] = Attrib::Count;
  4792. }
  4793. }
  4794. }
  4795. }
  4796. void ProgramGL::bindInstanceData(uint32_t _stride, uint32_t _baseVertex) const
  4797. {
  4798. for (uint32_t ii = 0; -1 != m_instanceData[ii]; ++ii)
  4799. {
  4800. GLint loc = m_instanceData[ii];
  4801. lazyEnableVertexAttribArray(loc);
  4802. const uint32_t baseVertex = _baseVertex + m_instanceOffset[ii];
  4803. GL_CHECK(glVertexAttribPointer(loc, 4, GL_FLOAT, GL_FALSE, _stride, (void*)(uintptr_t)baseVertex) );
  4804. GL_CHECK(glVertexAttribDivisor(loc, 1) );
  4805. }
  4806. }
  4807. void ProgramGL::bindAttributesEnd()
  4808. {
  4809. for (uint32_t ii = 0, iiEnd = m_usedCount; ii < iiEnd; ++ii)
  4810. {
  4811. if (Attrib::Count != m_unboundUsedAttrib[ii])
  4812. {
  4813. Attrib::Enum attr = Attrib::Enum(m_unboundUsedAttrib[ii]);
  4814. GLint loc = m_attributes[attr];
  4815. lazyDisableVertexAttribArray(loc);
  4816. }
  4817. }
  4818. applyLazyEnabledVertexAttributes();
  4819. }
  4820. void ProgramGL::unbindAttributes()
  4821. {
  4822. for(uint32_t ii = 0, iiEnd = m_usedCount; ii < iiEnd; ++ii)
  4823. {
  4824. if (Attrib::Count == m_unboundUsedAttrib[ii])
  4825. {
  4826. Attrib::Enum attr = Attrib::Enum(m_used[ii]);
  4827. GLint loc = m_attributes[attr];
  4828. lazyDisableVertexAttribArray(loc);
  4829. }
  4830. }
  4831. }
  4832. void ProgramGL::unbindInstanceData() const
  4833. {
  4834. for(uint32_t ii = 0; -1 != m_instanceData[ii]; ++ii)
  4835. {
  4836. GLint loc = m_instanceData[ii];
  4837. lazyDisableVertexAttribArray(loc);
  4838. }
  4839. }
  4840. void IndexBufferGL::destroy()
  4841. {
  4842. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  4843. GL_CHECK(glDeleteBuffers(1, &m_id) );
  4844. }
  4845. void VertexBufferGL::destroy()
  4846. {
  4847. GL_CHECK(glBindBuffer(m_target, 0) );
  4848. GL_CHECK(glDeleteBuffers(1, &m_id) );
  4849. }
  4850. bool TextureGL::init(GLenum _target, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint64_t _flags)
  4851. {
  4852. m_target = _target;
  4853. m_numMips = _numMips;
  4854. m_flags = _flags;
  4855. m_width = _width;
  4856. m_height = _height;
  4857. m_depth = _depth;
  4858. m_currentSamplerHash = UINT32_MAX;
  4859. const bool writeOnly = 0 != (m_flags&BGFX_TEXTURE_RT_WRITE_ONLY);
  4860. const bool computeWrite = 0 != (m_flags&BGFX_TEXTURE_COMPUTE_WRITE );
  4861. const bool srgb = 0 != (m_flags&BGFX_TEXTURE_SRGB);
  4862. const bool renderTarget = 0 != (m_flags&BGFX_TEXTURE_RT_MASK);
  4863. const bool textureArray = false
  4864. || _target == GL_TEXTURE_2D_ARRAY
  4865. || _target == GL_TEXTURE_CUBE_MAP_ARRAY
  4866. ;
  4867. if (!writeOnly
  4868. || (renderTarget && textureArray) )
  4869. {
  4870. GL_CHECK(glGenTextures(1, &m_id) );
  4871. BX_ASSERT(0 != m_id, "Failed to generate texture id.");
  4872. GL_CHECK(glBindTexture(_target, m_id) );
  4873. GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1) );
  4874. const TextureFormatInfo& tfi = s_textureFormat[m_textureFormat];
  4875. const GLenum fmt = srgb
  4876. ? s_textureFormat[m_textureFormat].m_fmtSrgb
  4877. : s_textureFormat[m_textureFormat].m_fmt
  4878. ;
  4879. m_fmt = fmt;
  4880. m_type = tfi.m_type;
  4881. const bool swizzle = true
  4882. && TextureFormat::BGRA8 == m_requestedFormat
  4883. && !s_textureFormat[m_requestedFormat].m_supported
  4884. && !s_renderGL->m_textureSwizzleSupport
  4885. ;
  4886. const bool convert = false
  4887. || m_textureFormat != m_requestedFormat
  4888. || swizzle
  4889. ;
  4890. if (convert)
  4891. {
  4892. m_textureFormat = (uint8_t)TextureFormat::RGBA8;
  4893. const TextureFormatInfo& tfiRgba8 = s_textureFormat[TextureFormat::RGBA8];
  4894. m_fmt = tfiRgba8.m_fmt;
  4895. m_type = tfiRgba8.m_type;
  4896. }
  4897. const GLenum internalFmt = srgb
  4898. ? s_textureFormat[m_textureFormat].m_internalFmtSrgb
  4899. : s_textureFormat[m_textureFormat].m_internalFmt
  4900. ;
  4901. if (textureArray)
  4902. {
  4903. GL_CHECK(glTexStorage3D(_target
  4904. , _numMips
  4905. , internalFmt
  4906. , m_width
  4907. , m_height
  4908. , _depth
  4909. ) );
  4910. }
  4911. else if (computeWrite)
  4912. {
  4913. if (_target == GL_TEXTURE_3D)
  4914. {
  4915. GL_CHECK(glTexStorage3D(_target
  4916. , _numMips
  4917. , internalFmt
  4918. , m_width
  4919. , m_height
  4920. , _depth
  4921. ) );
  4922. }
  4923. else
  4924. {
  4925. GL_CHECK(glTexStorage2D(_target
  4926. , _numMips
  4927. , internalFmt
  4928. , m_width
  4929. , m_height
  4930. ) );
  4931. }
  4932. }
  4933. setSamplerState(uint32_t(_flags), NULL);
  4934. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  4935. && TextureFormat::BGRA8 == m_requestedFormat
  4936. && !s_textureFormat[m_requestedFormat].m_supported
  4937. && s_renderGL->m_textureSwizzleSupport)
  4938. {
  4939. GLint swizzleMask[] = { GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
  4940. GL_CHECK(glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask) );
  4941. }
  4942. }
  4943. if (renderTarget)
  4944. {
  4945. uint32_t msaaQuality = ( (m_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT);
  4946. msaaQuality = bx::uint32_satsub(msaaQuality, 1);
  4947. msaaQuality = bx::uint32_min(s_renderGL->m_maxMsaa, msaaQuality == 0 ? 0 : 1<<msaaQuality);
  4948. const bool msaaSample = 0 != (m_flags&BGFX_TEXTURE_MSAA_SAMPLE);
  4949. if (!msaaSample
  4950. && (0 != msaaQuality || writeOnly)
  4951. && !textureArray)
  4952. {
  4953. GL_CHECK(glGenRenderbuffers(1, &m_rbo) );
  4954. BX_ASSERT(0 != m_rbo, "Failed to generate renderbuffer id.");
  4955. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, m_rbo) );
  4956. if (0 == msaaQuality)
  4957. {
  4958. GL_CHECK(glRenderbufferStorage(GL_RENDERBUFFER
  4959. , s_rboFormat[m_textureFormat]
  4960. , _width
  4961. , _height
  4962. ) );
  4963. }
  4964. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || s_renderGL->m_gles3)
  4965. {
  4966. GL_CHECK(glRenderbufferStorageMultisample(GL_RENDERBUFFER
  4967. , msaaQuality
  4968. , s_rboFormat[m_textureFormat]
  4969. , _width
  4970. , _height
  4971. ) );
  4972. }
  4973. GL_CHECK(glBindRenderbuffer(GL_RENDERBUFFER, 0) );
  4974. if (writeOnly)
  4975. {
  4976. // This is render buffer, there is no sampling, no need
  4977. // to create texture.
  4978. return false;
  4979. }
  4980. }
  4981. }
  4982. return true;
  4983. }
  4984. void TextureGL::create(const Memory* _mem, uint64_t _flags, uint8_t _skip)
  4985. {
  4986. bimg::ImageContainer imageContainer;
  4987. if (bimg::imageParse(imageContainer, _mem->data, _mem->size) )
  4988. {
  4989. const uint8_t startLod = bx::min<uint8_t>(_skip, imageContainer.m_numMips-1);
  4990. bimg::TextureInfo ti;
  4991. bimg::imageGetSize(
  4992. &ti
  4993. , uint16_t(imageContainer.m_width >>startLod)
  4994. , uint16_t(imageContainer.m_height>>startLod)
  4995. , uint16_t(imageContainer.m_depth >>startLod)
  4996. , imageContainer.m_cubeMap
  4997. , 1 < imageContainer.m_numMips
  4998. , imageContainer.m_numLayers
  4999. , imageContainer.m_format
  5000. );
  5001. ti.numMips = bx::min<uint8_t>(imageContainer.m_numMips-startLod, ti.numMips);
  5002. m_requestedFormat = uint8_t(imageContainer.m_format);
  5003. m_textureFormat = uint8_t(getViableTextureFormat(imageContainer) );
  5004. const bool computeWrite = 0 != (_flags&BGFX_TEXTURE_COMPUTE_WRITE);
  5005. const bool srgb = 0 != (_flags&BGFX_TEXTURE_SRGB);
  5006. const bool msaaSample = 0 != (_flags&BGFX_TEXTURE_MSAA_SAMPLE);
  5007. uint32_t msaaQuality = ( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT);
  5008. msaaQuality = bx::uint32_satsub(msaaQuality, 1);
  5009. msaaQuality = bx::uint32_min(s_renderGL->m_maxMsaa, msaaQuality == 0 ? 0 : 1<<msaaQuality);
  5010. GLenum target = msaaSample ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
  5011. if (imageContainer.m_cubeMap)
  5012. {
  5013. target = GL_TEXTURE_CUBE_MAP;
  5014. }
  5015. else if (imageContainer.m_depth > 1)
  5016. {
  5017. target = GL_TEXTURE_3D;
  5018. }
  5019. const bool textureArray = 1 < ti.numLayers;
  5020. if (textureArray)
  5021. {
  5022. switch (target)
  5023. {
  5024. case GL_TEXTURE_CUBE_MAP: target = GL_TEXTURE_CUBE_MAP_ARRAY; break;
  5025. case GL_TEXTURE_2D_MULTISAMPLE: target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY; break;
  5026. default: target = GL_TEXTURE_2D_ARRAY; break;
  5027. }
  5028. }
  5029. if (!init(target
  5030. , ti.width
  5031. , ti.height
  5032. , textureArray ? ti.numLayers : ti.depth
  5033. , ti.numMips
  5034. , _flags
  5035. ) )
  5036. {
  5037. return;
  5038. }
  5039. m_numLayers = ti.numLayers;
  5040. target = isCubeMap()
  5041. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
  5042. : m_target
  5043. ;
  5044. const GLenum internalFmt = srgb
  5045. ? s_textureFormat[m_textureFormat].m_internalFmtSrgb
  5046. : s_textureFormat[m_textureFormat].m_internalFmt
  5047. ;
  5048. const GLenum fmt = srgb
  5049. ? s_textureFormat[m_textureFormat].m_fmtSrgb
  5050. : s_textureFormat[m_textureFormat].m_fmt
  5051. ;
  5052. const bool swizzle = true
  5053. && TextureFormat::BGRA8 == m_requestedFormat
  5054. && !s_textureFormat[m_requestedFormat].m_supported
  5055. && !s_renderGL->m_textureSwizzleSupport
  5056. ;
  5057. const bool compressed = bimg::isCompressed(bimg::TextureFormat::Enum(m_requestedFormat) );
  5058. const bool convert = false
  5059. || m_textureFormat != m_requestedFormat
  5060. || swizzle
  5061. ;
  5062. BX_TRACE("Texture%-4s %3d: %s %s(requested: %s), layers %d, %dx%dx%d%s."
  5063. , imageContainer.m_cubeMap ? "Cube" : (1 < imageContainer.m_depth ? "3D" : "2D")
  5064. , this - s_renderGL->m_textures
  5065. , getName( (TextureFormat::Enum)m_textureFormat)
  5066. , srgb ? "+sRGB " : ""
  5067. , getName( (TextureFormat::Enum)m_requestedFormat)
  5068. , ti.numLayers
  5069. , ti.width
  5070. , ti.height
  5071. , imageContainer.m_cubeMap ? 6 : (1 < imageContainer.m_depth ? imageContainer.m_depth : 0)
  5072. , 0 != (m_flags&BGFX_TEXTURE_RT_MASK) ? " (render target)" : ""
  5073. );
  5074. BX_WARN(!convert, "Texture %s%s%s from %s to %s."
  5075. , swizzle ? "swizzle" : ""
  5076. , swizzle&&convert ? " and " : ""
  5077. , convert ? "convert" : ""
  5078. , getName( (TextureFormat::Enum)m_requestedFormat)
  5079. , getName( (TextureFormat::Enum)m_textureFormat)
  5080. );
  5081. uint8_t* temp = NULL;
  5082. if (convert)
  5083. {
  5084. temp = (uint8_t*)bx::alloc(g_allocator, ti.width*ti.height*4);
  5085. }
  5086. const uint16_t numSides = ti.numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  5087. for (uint16_t side = 0; side < numSides; ++side)
  5088. {
  5089. uint32_t width = ti.width;
  5090. uint32_t height = ti.height;
  5091. uint32_t depth = ti.depth;
  5092. GLenum imageTarget = imageContainer.m_cubeMap && !textureArray
  5093. ? target+side
  5094. : target
  5095. ;
  5096. for (uint8_t lod = 0, num = ti.numMips; lod < num; ++lod)
  5097. {
  5098. width = bx::uint32_max(1, width);
  5099. height = bx::uint32_max(1, height);
  5100. depth = 1 < imageContainer.m_depth
  5101. ? bx::uint32_max(1, depth)
  5102. : side
  5103. ;
  5104. bimg::ImageMip mip;
  5105. if (bimg::imageGetRawData(imageContainer, side, lod+startLod, _mem->data, _mem->size, mip) )
  5106. {
  5107. if (compressed
  5108. && !convert)
  5109. {
  5110. GL_CHECK(compressedTexImage(imageTarget
  5111. , lod
  5112. , internalFmt
  5113. , width
  5114. , height
  5115. , depth
  5116. , 0
  5117. , mip.m_size
  5118. , mip.m_data
  5119. ) );
  5120. }
  5121. else
  5122. {
  5123. const uint8_t* data = mip.m_data;
  5124. if (convert)
  5125. {
  5126. imageDecodeToRgba8(
  5127. g_allocator
  5128. , temp
  5129. , mip.m_data
  5130. , mip.m_width
  5131. , mip.m_height
  5132. , mip.m_width*4
  5133. , mip.m_format
  5134. );
  5135. data = temp;
  5136. }
  5137. GL_CHECK(texImage(imageTarget
  5138. , msaaQuality
  5139. , lod
  5140. , internalFmt
  5141. , width
  5142. , height
  5143. , depth
  5144. , 0
  5145. , fmt
  5146. , m_type
  5147. , data
  5148. ) );
  5149. }
  5150. }
  5151. else if (!computeWrite)
  5152. {
  5153. if (compressed
  5154. && !convert)
  5155. {
  5156. uint32_t size = bx::max<uint32_t>(1, (width + 3)>>2)
  5157. * bx::max<uint32_t>(1, (height + 3)>>2)
  5158. * 4*4* bimg::getBitsPerPixel(bimg::TextureFormat::Enum(m_textureFormat) )/8
  5159. ;
  5160. GL_CHECK(compressedTexImage(imageTarget
  5161. , lod
  5162. , internalFmt
  5163. , width
  5164. , height
  5165. , depth
  5166. , 0
  5167. , size
  5168. , NULL
  5169. ) );
  5170. }
  5171. else
  5172. {
  5173. GL_CHECK(texImage(imageTarget
  5174. , msaaQuality
  5175. , lod
  5176. , internalFmt
  5177. , width
  5178. , height
  5179. , depth
  5180. , 0
  5181. , fmt
  5182. , m_type
  5183. , NULL
  5184. ) );
  5185. }
  5186. }
  5187. width >>= 1;
  5188. height >>= 1;
  5189. depth >>= 1;
  5190. }
  5191. }
  5192. GLint mapping[4] = {
  5193. s_textureFormat[m_textureFormat].m_mapping[0],
  5194. s_textureFormat[m_textureFormat].m_mapping[1],
  5195. s_textureFormat[m_textureFormat].m_mapping[2],
  5196. s_textureFormat[m_textureFormat].m_mapping[3],
  5197. };
  5198. if (s_renderGL->m_textureSwizzleSupport
  5199. && (-1 != mapping[0] || -1 != mapping[1] || -1 != mapping[2] || -1 != mapping[3]) )
  5200. {
  5201. mapping[0] = -1 == mapping[0] ? GL_RED : mapping[0];
  5202. mapping[1] = -1 == mapping[1] ? GL_GREEN : mapping[1];
  5203. mapping[2] = -1 == mapping[2] ? GL_BLUE : mapping[2];
  5204. mapping[3] = -1 == mapping[3] ? GL_ALPHA : mapping[3];
  5205. GL_CHECK(glTexParameteriv(m_target, GL_TEXTURE_SWIZZLE_RGBA, mapping));
  5206. }
  5207. if (NULL != temp)
  5208. {
  5209. bx::free(g_allocator, temp);
  5210. }
  5211. }
  5212. GL_CHECK(glBindTexture(m_target, 0) );
  5213. }
  5214. void TextureGL::destroy()
  5215. {
  5216. if (0 == (m_flags & BGFX_SAMPLER_INTERNAL_SHARED)
  5217. && 0 != m_id)
  5218. {
  5219. GL_CHECK(glBindTexture(m_target, 0) );
  5220. GL_CHECK(glDeleteTextures(1, &m_id) );
  5221. m_id = 0;
  5222. }
  5223. if (0 != m_rbo)
  5224. {
  5225. GL_CHECK(glDeleteRenderbuffers(1, &m_rbo) );
  5226. m_rbo = 0;
  5227. }
  5228. }
  5229. void TextureGL::overrideInternal(uintptr_t _ptr)
  5230. {
  5231. destroy();
  5232. m_flags |= BGFX_SAMPLER_INTERNAL_SHARED;
  5233. m_id = (GLuint)_ptr;
  5234. }
  5235. void TextureGL::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem)
  5236. {
  5237. const uint32_t bpp = bimg::getBitsPerPixel(bimg::TextureFormat::Enum(m_textureFormat) );
  5238. const uint32_t rectpitch = _rect.m_width*bpp/8;
  5239. uint32_t srcpitch = UINT16_MAX == _pitch ? rectpitch : _pitch;
  5240. GL_CHECK(glBindTexture(m_target, m_id) );
  5241. GL_CHECK(glPixelStorei(GL_UNPACK_ALIGNMENT, 1) );
  5242. GLenum target = isCubeMap()
  5243. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X
  5244. : m_target
  5245. ;
  5246. const bool swizzle = true
  5247. && TextureFormat::BGRA8 == m_requestedFormat
  5248. && !s_textureFormat[m_requestedFormat].m_supported
  5249. && !s_renderGL->m_textureSwizzleSupport
  5250. ;
  5251. const bool unpackRowLength = !!BGFX_CONFIG_RENDERER_OPENGL || s_extension[Extension::EXT_unpack_subimage].m_supported;
  5252. const bool compressed = bimg::isCompressed(bimg::TextureFormat::Enum(m_requestedFormat) );
  5253. const bool convert = false
  5254. || (compressed && m_textureFormat != m_requestedFormat)
  5255. || swizzle
  5256. ;
  5257. Rect rect;
  5258. rect.setIntersect(_rect
  5259. , {
  5260. 0, 0,
  5261. uint16_t(bx::max(1u, m_width >> _mip) ),
  5262. uint16_t(bx::max(1u, m_height >> _mip) ),
  5263. });
  5264. uint32_t width = rect.m_width;
  5265. uint32_t height = rect.m_height;
  5266. uint8_t* temp = NULL;
  5267. if (convert
  5268. || !unpackRowLength)
  5269. {
  5270. temp = (uint8_t*)bx::alloc(g_allocator, rectpitch*height);
  5271. }
  5272. else if (unpackRowLength)
  5273. {
  5274. GL_CHECK(glPixelStorei(GL_UNPACK_ROW_LENGTH, srcpitch*8/bpp) );
  5275. }
  5276. if (compressed
  5277. && !convert)
  5278. {
  5279. const uint8_t* data = _mem->data;
  5280. if (!unpackRowLength)
  5281. {
  5282. bimg::imageCopy(temp, width, height, 1, bpp, srcpitch, data);
  5283. data = temp;
  5284. }
  5285. const GLenum internalFmt = (0 != (m_flags & BGFX_TEXTURE_SRGB) )
  5286. ? s_textureFormat[m_textureFormat].m_internalFmtSrgb
  5287. : s_textureFormat[m_textureFormat].m_internalFmt
  5288. ;
  5289. GL_CHECK(compressedTexSubImage(target+_side
  5290. , _mip
  5291. , rect.m_x
  5292. , rect.m_y
  5293. , _z
  5294. , rect.m_width
  5295. , rect.m_height
  5296. , _depth
  5297. , internalFmt
  5298. , _mem->size
  5299. , data
  5300. ) );
  5301. }
  5302. else
  5303. {
  5304. const uint8_t* data = _mem->data;
  5305. if (convert)
  5306. {
  5307. bimg::imageDecodeToRgba8(g_allocator, temp, data, width, height, srcpitch, bimg::TextureFormat::Enum(m_requestedFormat) );
  5308. data = temp;
  5309. srcpitch = rectpitch;
  5310. }
  5311. if (BX_IGNORE_C4127(true
  5312. && !unpackRowLength
  5313. && !convert) )
  5314. {
  5315. bimg::imageCopy(temp, width, height, 1, bpp, srcpitch, data);
  5316. data = temp;
  5317. }
  5318. GL_CHECK(texSubImage(target+_side
  5319. , _mip
  5320. , rect.m_x
  5321. , rect.m_y
  5322. , _z
  5323. , rect.m_width
  5324. , rect.m_height
  5325. , _depth
  5326. , m_fmt
  5327. , m_type
  5328. , data
  5329. ) );
  5330. }
  5331. if (!convert
  5332. && unpackRowLength)
  5333. {
  5334. GL_CHECK(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0) );
  5335. }
  5336. if (NULL != temp)
  5337. {
  5338. bx::free(g_allocator, temp);
  5339. }
  5340. }
  5341. void TextureGL::setSamplerState(uint32_t _flags, const float _rgba[4])
  5342. {
  5343. if (!s_renderGL->m_gles3
  5344. && !s_textureFilter[m_textureFormat])
  5345. {
  5346. // Force point sampling when texture format doesn't support linear sampling.
  5347. _flags &= ~(0
  5348. | BGFX_SAMPLER_MIN_MASK
  5349. | BGFX_SAMPLER_MAG_MASK
  5350. | BGFX_SAMPLER_MIP_MASK
  5351. );
  5352. _flags |= 0
  5353. | BGFX_SAMPLER_MIN_POINT
  5354. | BGFX_SAMPLER_MAG_POINT
  5355. | BGFX_SAMPLER_MIP_POINT
  5356. ;
  5357. }
  5358. const uint32_t flags = (0 != (BGFX_SAMPLER_INTERNAL_DEFAULT & _flags) ? m_flags : _flags) & BGFX_SAMPLER_BITS_MASK;
  5359. bool hasBorderColor = false;
  5360. bx::HashMurmur2A murmur;
  5361. murmur.begin();
  5362. murmur.add(flags);
  5363. if (NULL != _rgba)
  5364. {
  5365. if (BGFX_SAMPLER_U_BORDER == (flags & BGFX_SAMPLER_U_BORDER)
  5366. || BGFX_SAMPLER_V_BORDER == (flags & BGFX_SAMPLER_V_BORDER)
  5367. || BGFX_SAMPLER_W_BORDER == (flags & BGFX_SAMPLER_W_BORDER) )
  5368. {
  5369. murmur.add(_rgba, 16);
  5370. hasBorderColor = true;
  5371. }
  5372. }
  5373. uint32_t hash = murmur.end();
  5374. if (hash != m_currentSamplerHash)
  5375. {
  5376. const GLenum target = m_target == GL_TEXTURE_2D_MULTISAMPLE ? GL_TEXTURE_2D : m_target;
  5377. const GLenum targetMsaa = m_target;
  5378. const uint8_t numMips = m_numMips;
  5379. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_S, s_textureAddress[(flags&BGFX_SAMPLER_U_MASK)>>BGFX_SAMPLER_U_SHIFT]) );
  5380. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_T, s_textureAddress[(flags&BGFX_SAMPLER_V_MASK)>>BGFX_SAMPLER_V_SHIFT]) );
  5381. if (1 < numMips
  5382. && (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || s_renderGL->m_gles3 || s_extension[Extension::APPLE_texture_max_level].m_supported) )
  5383. {
  5384. GL_CHECK(glTexParameteri(targetMsaa, GL_TEXTURE_MAX_LEVEL, numMips-1) );
  5385. }
  5386. if (target == GL_TEXTURE_3D)
  5387. {
  5388. GL_CHECK(glTexParameteri(target, GL_TEXTURE_WRAP_R, s_textureAddress[(flags&BGFX_SAMPLER_W_MASK)>>BGFX_SAMPLER_W_SHIFT]) );
  5389. }
  5390. GLenum magFilter;
  5391. GLenum minFilter;
  5392. getFilters(flags, 1 < numMips, magFilter, minFilter);
  5393. GL_CHECK(glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilter) );
  5394. GL_CHECK(glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilter) );
  5395. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  5396. {
  5397. GL_CHECK(glTexParameterf(target, GL_TEXTURE_LOD_BIAS, float(BGFX_CONFIG_MIP_LOD_BIAS) ) );
  5398. }
  5399. if (s_renderGL->m_borderColorSupport
  5400. && hasBorderColor)
  5401. {
  5402. GL_CHECK(glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _rgba) );
  5403. }
  5404. if (0 != (flags & (BGFX_SAMPLER_MIN_ANISOTROPIC|BGFX_SAMPLER_MAG_ANISOTROPIC) )
  5405. && 0.0f < s_renderGL->m_maxAnisotropy)
  5406. {
  5407. GL_CHECK(glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, s_renderGL->m_maxAnisotropy) );
  5408. }
  5409. if (s_renderGL->m_gles3
  5410. || s_renderGL->m_shadowSamplersSupport)
  5411. {
  5412. const uint32_t cmpFunc = (flags&BGFX_SAMPLER_COMPARE_MASK)>>BGFX_SAMPLER_COMPARE_SHIFT;
  5413. if (0 == cmpFunc)
  5414. {
  5415. GL_CHECK(glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_NONE) );
  5416. }
  5417. else
  5418. {
  5419. GL_CHECK(glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE) );
  5420. GL_CHECK(glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, s_cmpFunc[cmpFunc]) );
  5421. }
  5422. }
  5423. m_currentSamplerHash = hash;
  5424. }
  5425. }
  5426. void TextureGL::commit(uint32_t _stage, uint32_t _flags, const float _palette[][4])
  5427. {
  5428. const uint32_t flags = 0 == (BGFX_SAMPLER_INTERNAL_DEFAULT & _flags)
  5429. ? _flags
  5430. : uint32_t(m_flags)
  5431. ;
  5432. const uint32_t index = (flags & BGFX_SAMPLER_BORDER_COLOR_MASK) >> BGFX_SAMPLER_BORDER_COLOR_SHIFT;
  5433. GL_CHECK(glActiveTexture(GL_TEXTURE0+_stage) );
  5434. GL_CHECK(glBindTexture(m_target, m_id) );
  5435. if (s_renderGL->m_samplerObjectSupport)
  5436. {
  5437. s_renderGL->setSamplerState(_stage, m_numMips, flags, _palette[index]);
  5438. }
  5439. else
  5440. {
  5441. setSamplerState(flags, _palette[index]);
  5442. }
  5443. }
  5444. void TextureGL::resolve(uint8_t _resolve) const
  5445. {
  5446. const bool renderTarget = 0 != (m_flags&BGFX_TEXTURE_RT_MASK);
  5447. if (renderTarget
  5448. && 1 < m_numMips
  5449. && 0 != (_resolve & BGFX_RESOLVE_AUTO_GEN_MIPS) )
  5450. {
  5451. GL_CHECK(glBindTexture(m_target, m_id) );
  5452. GL_CHECK(glGenerateMipmap(m_target) );
  5453. GL_CHECK(glBindTexture(m_target, 0) );
  5454. }
  5455. }
  5456. void strins(char* _str, const char* _insert)
  5457. {
  5458. size_t len = bx::strLen(_insert);
  5459. bx::memMove(&_str[len], _str, bx::strLen(_str)+1);
  5460. bx::memCopy(_str, _insert, len);
  5461. }
  5462. void ShaderGL::create(const Memory* _mem)
  5463. {
  5464. bx::MemoryReader reader(_mem->data, _mem->size);
  5465. m_hash = bx::hash<bx::HashMurmur2A>(_mem->data, _mem->size);
  5466. bx::ErrorAssert err;
  5467. uint32_t magic;
  5468. bx::read(&reader, magic, &err);
  5469. if (isShaderType(magic, 'C') )
  5470. {
  5471. m_type = GL_COMPUTE_SHADER;
  5472. }
  5473. else if (isShaderType(magic, 'F') )
  5474. {
  5475. m_type = GL_FRAGMENT_SHADER;
  5476. }
  5477. else if (isShaderType(magic, 'V') )
  5478. {
  5479. m_type = GL_VERTEX_SHADER;
  5480. }
  5481. uint32_t hashIn;
  5482. bx::read(&reader, hashIn, &err);
  5483. uint32_t hashOut;
  5484. if (isShaderVerLess(magic, 6) )
  5485. {
  5486. hashOut = hashIn;
  5487. }
  5488. else
  5489. {
  5490. bx::read(&reader, hashOut, &err);
  5491. }
  5492. uint16_t count;
  5493. bx::read(&reader, count, &err);
  5494. BX_TRACE("%s Shader consts %d"
  5495. , getShaderTypeName(magic)
  5496. , count
  5497. );
  5498. for (uint32_t ii = 0; ii < count; ++ii)
  5499. {
  5500. uint8_t nameSize = 0;
  5501. bx::read(&reader, nameSize, &err);
  5502. char name[256];
  5503. bx::read(&reader, &name, nameSize, &err);
  5504. name[nameSize] = '\0';
  5505. uint8_t type;
  5506. bx::read(&reader, type, &err);
  5507. uint8_t num;
  5508. bx::read(&reader, num, &err);
  5509. uint16_t regIndex;
  5510. bx::read(&reader, regIndex, &err);
  5511. uint16_t regCount;
  5512. bx::read(&reader, regCount, &err);
  5513. if (!isShaderVerLess(magic, 8) )
  5514. {
  5515. uint16_t texInfo = 0;
  5516. bx::read(&reader, texInfo, &err);
  5517. }
  5518. if (!isShaderVerLess(magic, 10) )
  5519. {
  5520. uint16_t texFormat = 0;
  5521. bx::read(&reader, texFormat, &err);
  5522. }
  5523. }
  5524. uint32_t shaderSize;
  5525. bx::read(&reader, shaderSize, &err);
  5526. m_id = glCreateShader(m_type);
  5527. BX_WARN(0 != m_id, "Failed to create shader.");
  5528. bx::StringView code( (const char*)reader.getDataPtr(), shaderSize);
  5529. if (0 != m_id)
  5530. {
  5531. if (GL_COMPUTE_SHADER != m_type
  5532. && 0 != bx::strCmp(code, "#version", 8) ) // #2000
  5533. {
  5534. int32_t tempLen = code.getLength() + (4<<10);
  5535. char* temp = (char*)BX_STACK_ALLOC(tempLen);
  5536. bx::StaticMemoryBlockWriter writer(temp, tempLen);
  5537. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES)
  5538. && !s_renderGL->m_gles3)
  5539. {
  5540. bx::write(&writer
  5541. , "#define centroid\n"
  5542. "#define flat\n"
  5543. "#define noperspective\n"
  5544. "#define smooth\n"
  5545. , &err
  5546. );
  5547. bool usesDerivatives = s_extension[Extension::OES_standard_derivatives].m_supported
  5548. && !bx::findIdentifierMatch(code, s_OES_standard_derivatives).isEmpty()
  5549. ;
  5550. const bool usesFragData = !bx::findIdentifierMatch(code, "gl_FragData").isEmpty();
  5551. const bool usesFragDepth = !bx::findIdentifierMatch(code, "gl_FragDepth").isEmpty();
  5552. const bool usesShadowSamplers = !bx::findIdentifierMatch(code, s_EXT_shadow_samplers).isEmpty();
  5553. const bool usesTextureLod = !bx::findIdentifierMatch(code, s_EXT_shader_texture_lod).isEmpty();
  5554. const bool usesFragmentOrdering = !bx::findIdentifierMatch(code, "beginFragmentShaderOrdering").isEmpty();
  5555. const bool usesTexture3D = true
  5556. && s_extension[Extension::OES_texture_3D].m_supported
  5557. && !bx::findIdentifierMatch(code, s_texture3D).isEmpty()
  5558. ;
  5559. if (usesDerivatives)
  5560. {
  5561. bx::write(&writer, "#extension GL_OES_standard_derivatives : enable\n", &err);
  5562. }
  5563. if (usesFragData)
  5564. {
  5565. BX_WARN(s_extension[Extension::EXT_draw_buffers ].m_supported
  5566. || s_extension[Extension::WEBGL_draw_buffers].m_supported
  5567. , "EXT_draw_buffers is used but not supported by GLES2 driver."
  5568. );
  5569. bx::write(&writer, "#extension GL_EXT_draw_buffers : enable\n", &err);
  5570. }
  5571. bool insertFragDepth = false;
  5572. if (usesFragDepth)
  5573. {
  5574. BX_WARN(s_extension[Extension::EXT_frag_depth].m_supported, "EXT_frag_depth is used but not supported by GLES2 driver.");
  5575. if (s_extension[Extension::EXT_frag_depth].m_supported)
  5576. {
  5577. bx::write(&writer
  5578. , "#extension GL_EXT_frag_depth : enable\n"
  5579. "#define bgfx_FragDepth gl_FragDepthEXT\n"
  5580. , &err
  5581. );
  5582. char str[128];
  5583. bx::snprintf(str, BX_COUNTOF(str), "%s float gl_FragDepthEXT;\n"
  5584. , s_extension[Extension::OES_fragment_precision_high].m_supported ? "highp" : "mediump"
  5585. );
  5586. bx::write(&writer, str, &err);
  5587. }
  5588. else
  5589. {
  5590. insertFragDepth = true;
  5591. }
  5592. }
  5593. if (usesShadowSamplers)
  5594. {
  5595. if (s_renderGL->m_shadowSamplersSupport)
  5596. {
  5597. bx::write(&writer
  5598. , "#extension GL_EXT_shadow_samplers : enable\n"
  5599. "#define shadow2D shadow2DEXT\n"
  5600. "#define shadow2DProj shadow2DProjEXT\n"
  5601. , &err
  5602. );
  5603. }
  5604. else
  5605. {
  5606. bx::write(&writer
  5607. , "#define sampler2DShadow sampler2D\n"
  5608. "#define shadow2D(_sampler, _coord) step(_coord.z, texture2D(_sampler, _coord.xy).x)\n"
  5609. "#define shadow2DProj(_sampler, _coord) step(_coord.z/_coord.w, texture2DProj(_sampler, _coord).x)\n"
  5610. , &err
  5611. );
  5612. }
  5613. }
  5614. if (usesTexture3D)
  5615. {
  5616. bx::write(&writer, "#extension GL_OES_texture_3D : enable\n", &err);
  5617. }
  5618. if (usesTextureLod)
  5619. {
  5620. BX_WARN(s_extension[Extension::ARB_shader_texture_lod].m_supported
  5621. || s_extension[Extension::EXT_shader_texture_lod].m_supported
  5622. , "(ARB|EXT)_shader_texture_lod is used but not supported by GLES2 driver."
  5623. );
  5624. if (s_extension[Extension::ARB_shader_texture_lod].m_supported)
  5625. {
  5626. bx::write(&writer
  5627. , "#extension GL_ARB_shader_texture_lod : enable\n"
  5628. "#define texture2DLod texture2DLodARB\n"
  5629. "#define texture2DProjLod texture2DProjLodARB\n"
  5630. "#define textureCubeLod textureCubeLodARB\n"
  5631. "#define texture2DGrad texture2DGradARB\n"
  5632. "#define texture2DProjGrad texture2DProjGradARB\n"
  5633. "#define textureCubeGrad textureCubeGradARB\n"
  5634. , &err
  5635. );
  5636. }
  5637. else
  5638. {
  5639. if (s_extension[Extension::EXT_shader_texture_lod].m_supported)
  5640. {
  5641. bx::write(&writer
  5642. , "#extension GL_EXT_shader_texture_lod : enable\n"
  5643. "#define texture2DLod texture2DLodEXT\n"
  5644. "#define texture2DProjLod texture2DProjLodEXT\n"
  5645. "#define textureCubeLod textureCubeLodEXT\n"
  5646. , &err
  5647. );
  5648. }
  5649. else
  5650. {
  5651. bx::write(&writer
  5652. , "#define texture2DLod(_sampler, _coord, _level) texture2D(_sampler, _coord)\n"
  5653. "#define texture2DProjLod(_sampler, _coord, _level) texture2DProj(_sampler, _coord)\n"
  5654. "#define textureCubeLod(_sampler, _coord, _level) textureCube(_sampler, _coord)\n"
  5655. , &err
  5656. );
  5657. }
  5658. }
  5659. }
  5660. if (usesFragmentOrdering)
  5661. {
  5662. if (s_extension[Extension::INTEL_fragment_shader_ordering].m_supported)
  5663. {
  5664. bx::write(&writer, "#extension GL_INTEL_fragment_shader_ordering : enable\n", &err);
  5665. }
  5666. else
  5667. {
  5668. bx::write(&writer, "#define beginFragmentShaderOrdering()\n", &err);
  5669. }
  5670. }
  5671. bx::write(&writer, &err, "precision %s float;\n"
  5672. , m_type == GL_FRAGMENT_SHADER ? "mediump" : "highp"
  5673. );
  5674. bx::write(&writer, code, &err);
  5675. bx::write(&writer, '\0', &err);
  5676. if (insertFragDepth)
  5677. {
  5678. bx::StringView shader(temp);
  5679. bx::StringView entry = bx::strFind(shader, "void main ()");
  5680. if (!entry.isEmpty() )
  5681. {
  5682. entry.set(entry.getTerm(), shader.getTerm() );
  5683. bx::StringView brace = bx::strFind(entry, "{");
  5684. if (!brace.isEmpty() )
  5685. {
  5686. bx::StringView block = bx::strFindBlock(bx::StringView(brace.getPtr(), shader.getTerm() ), '{', '}');
  5687. if (!block.isEmpty() )
  5688. {
  5689. strins(const_cast<char*>(brace.getPtr()+1), "\n float bgfx_FragDepth = 0.0;\n");
  5690. }
  5691. }
  5692. }
  5693. }
  5694. // Replace all instances of gl_FragDepth with bgfx_FragDepth.
  5695. for (bx::StringView fragDepth = bx::findIdentifierMatch(temp, "gl_FragDepth")
  5696. ; !fragDepth.isEmpty()
  5697. ; fragDepth = bx::findIdentifierMatch(fragDepth.getPtr(), "gl_FragDepth")
  5698. )
  5699. {
  5700. char* insert = const_cast<char*>(fragDepth.getPtr() );
  5701. strins(insert, "bg");
  5702. bx::memCopy(insert + 2, "fx", 2);
  5703. }
  5704. }
  5705. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  5706. && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL <= 21) )
  5707. {
  5708. const bool usesTextureLod = true
  5709. && s_extension[Extension::ARB_shader_texture_lod].m_supported
  5710. && !bx::findIdentifierMatch(code, s_ARB_shader_texture_lod).isEmpty()
  5711. ;
  5712. const bool usesVertexID = true
  5713. && !s_extension[Extension::EXT_gpu_shader4].m_supported
  5714. && !bx::findIdentifierMatch(code, "gl_VertexID").isEmpty()
  5715. ;
  5716. const bool usesInstanceID = true
  5717. && !s_extension[Extension::EXT_gpu_shader4].m_supported
  5718. && !bx::findIdentifierMatch(code, "gl_InstanceID").isEmpty()
  5719. ;
  5720. const bool usesGpuShader4 = true
  5721. && s_extension[Extension::EXT_gpu_shader4].m_supported
  5722. && !bx::findIdentifierMatch(code, s_EXT_gpu_shader4).isEmpty()
  5723. ;
  5724. // GpuShader5 extension is not supported on the fragment shader!
  5725. const bool usesGpuShader5 = true
  5726. && m_type != GL_FRAGMENT_SHADER
  5727. && !bx::findIdentifierMatch(code, s_ARB_gpu_shader5).isEmpty()
  5728. ;
  5729. const bool usesViewportLayerArray = true
  5730. && s_extension[Extension::ARB_shader_viewport_layer_array].m_supported
  5731. && !bx::findIdentifierMatch(code, s_ARB_shader_viewport_layer_array).isEmpty()
  5732. ;
  5733. const bool usesIUsamplers = !bx::findIdentifierMatch(code, s_uisamplers).isEmpty();
  5734. const bool usesUint = !bx::findIdentifierMatch(code, s_uint).isEmpty();
  5735. const bool usesTexelFetch = !bx::findIdentifierMatch(code, s_texelFetch).isEmpty();
  5736. const bool usesTextureArray = !bx::findIdentifierMatch(code, s_textureArray).isEmpty();
  5737. const bool usesTexture3D = !bx::findIdentifierMatch(code, s_texture3D).isEmpty();
  5738. const bool usesTextureMS = !bx::findIdentifierMatch(code, s_ARB_texture_multisample).isEmpty();
  5739. const bool usesPacking = !bx::findIdentifierMatch(code, s_ARB_shading_language_packing).isEmpty();
  5740. const bool usesInterpQ = !bx::findIdentifierMatch(code, s_intepolationQualifier).isEmpty();
  5741. uint32_t version = false
  5742. || usesTextureArray
  5743. || usesTexture3D
  5744. || usesIUsamplers
  5745. || usesVertexID
  5746. || usesUint
  5747. || usesTexelFetch
  5748. || usesGpuShader4
  5749. || usesGpuShader5
  5750. || usesInterpQ
  5751. ? 130
  5752. : 120
  5753. ;
  5754. version = 0 == bx::strCmp(code, "#version 430", 12) ? 430 : version;
  5755. bx::write(&writer, &err, "#version %d\n", version);
  5756. if (430 > version && usesTextureLod)
  5757. {
  5758. if (m_type == GL_FRAGMENT_SHADER)
  5759. {
  5760. bx::write(&writer
  5761. , "#extension GL_ARB_shader_texture_lod : enable\n"
  5762. "#define texture2DGrad texture2DGradARB\n"
  5763. "#define texture2DProjGrad texture2DProjGradARB\n"
  5764. "#define textureCubeGrad textureCubeGradARB\n"
  5765. , &err
  5766. );
  5767. }
  5768. }
  5769. if (usesInstanceID)
  5770. {
  5771. bx::write(&writer, "#extension GL_ARB_draw_instanced : enable\n", &err);
  5772. }
  5773. if (usesGpuShader4)
  5774. {
  5775. bx::write(&writer, "#extension GL_EXT_gpu_shader4 : enable\n", &err);
  5776. }
  5777. if (usesGpuShader5)
  5778. {
  5779. bx::write(&writer, "#extension GL_ARB_gpu_shader5 : enable\n", &err);
  5780. }
  5781. if (usesViewportLayerArray)
  5782. {
  5783. bx::write(&writer, "#extension GL_ARB_shader_viewport_layer_array : enable\n", &err);
  5784. }
  5785. if (usesPacking)
  5786. {
  5787. bx::write(&writer, "#extension GL_ARB_shading_language_packing : enable\n", &err);
  5788. }
  5789. if (usesTextureMS)
  5790. {
  5791. bx::write(&writer, "#extension GL_ARB_texture_multisample : enable\n", &err);
  5792. }
  5793. if (usesTextureArray)
  5794. {
  5795. bx::write(&writer, "#extension GL_EXT_texture_array : enable\n", &err);
  5796. bx::write(&writer, "#define texture2DArrayLodEXT texture2DArrayLod\n", &err);
  5797. bx::write(&writer, "#define textureArray texture\n", &err);
  5798. }
  5799. if (usesTexture3D)
  5800. {
  5801. bx::write(&writer, "#define texture3DEXT texture3D\n", &err);
  5802. bx::write(&writer, "#define texture3DLodEXT texture3DLod\n", &err);
  5803. }
  5804. if (130 <= version)
  5805. {
  5806. if (430 > version)
  5807. {
  5808. if (m_type == GL_FRAGMENT_SHADER)
  5809. {
  5810. bx::write(&writer, "#define varying in\n", &err);
  5811. }
  5812. else
  5813. {
  5814. bx::write(&writer, "#define attribute in\n", &err);
  5815. bx::write(&writer, "#define varying out\n", &err);
  5816. }
  5817. }
  5818. uint32_t fragData = 0;
  5819. if (!bx::findIdentifierMatch(code, "gl_FragData").isEmpty() )
  5820. {
  5821. for (uint32_t ii = 0, num = g_caps.limits.maxFBAttachments; ii < num; ++ii)
  5822. {
  5823. char tmpFragData[16];
  5824. bx::snprintf(tmpFragData, BX_COUNTOF(tmpFragData), "gl_FragData[%d]", ii);
  5825. fragData = bx::uint32_max(fragData, bx::strFind(code, tmpFragData).isEmpty() ? 0 : ii+1);
  5826. }
  5827. BGFX_FATAL(0 != fragData, Fatal::InvalidShader, "Unable to find and patch gl_FragData!");
  5828. }
  5829. if (0 != fragData)
  5830. {
  5831. bx::write(&writer, &err, "out vec4 bgfx_FragData[%d];\n", fragData);
  5832. bx::write(&writer, "#define gl_FragData bgfx_FragData\n", &err);
  5833. }
  5834. else if (!bx::findIdentifierMatch(code, "gl_FragColor").isEmpty() )
  5835. {
  5836. bx::write(&writer
  5837. , "out vec4 bgfx_FragColor;\n"
  5838. "#define gl_FragColor bgfx_FragColor\n"
  5839. , &err
  5840. );
  5841. }
  5842. }
  5843. else
  5844. {
  5845. if (m_type == GL_FRAGMENT_SHADER)
  5846. {
  5847. bx::write(&writer, "#define in varying\n", &err);
  5848. }
  5849. else
  5850. {
  5851. bx::write(&writer
  5852. , "#define in attribute\n"
  5853. "#define out varying\n"
  5854. , &err
  5855. );
  5856. }
  5857. }
  5858. bx::write(&writer
  5859. , "#define lowp\n"
  5860. "#define mediump\n"
  5861. "#define highp\n"
  5862. , &err
  5863. );
  5864. if (!usesInterpQ)
  5865. {
  5866. bx::write(&writer
  5867. ,
  5868. "#define centroid\n"
  5869. "#define flat\n"
  5870. "#define noperspective\n"
  5871. "#define smooth\n"
  5872. , &err
  5873. );
  5874. }
  5875. if (version == 430)
  5876. {
  5877. int32_t verLen = bx::strLen("#version 430\n");
  5878. bx::write(&writer, code.getPtr()+verLen, code.getLength()-verLen, &err);
  5879. }
  5880. else
  5881. {
  5882. bx::write(&writer, code, &err);
  5883. }
  5884. bx::write(&writer, '\0', &err);
  5885. }
  5886. else if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 31)
  5887. || s_renderGL->m_gles3)
  5888. {
  5889. if (s_renderGL->m_gles3)
  5890. {
  5891. bx::write(&writer, &err
  5892. , "#version 300 es\n"
  5893. "precision %s float;\n"
  5894. , m_type == GL_FRAGMENT_SHADER ? "mediump" : "highp"
  5895. );
  5896. }
  5897. else
  5898. {
  5899. bx::write(&writer, "#version 140\n", &err);
  5900. }
  5901. bx::write(&writer
  5902. , "#define texture2DLod textureLod\n"
  5903. "#define texture3DLod textureLod\n"
  5904. "#define textureCubeLod textureLod\n"
  5905. "#define texture2DGrad textureGrad\n"
  5906. "#define texture3DGrad textureGrad\n"
  5907. "#define textureCubeGrad textureGrad\n"
  5908. , &err
  5909. );
  5910. if (m_type == GL_FRAGMENT_SHADER)
  5911. {
  5912. bx::write(&writer
  5913. , "#define varying in\n"
  5914. "#define texture2D texture\n"
  5915. "#define texture2DArray texture\n"
  5916. "#define texture2DProj textureProj\n"
  5917. , &err
  5918. );
  5919. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  5920. {
  5921. bx::write(&writer
  5922. , "#define shadow2D(_sampler, _coord) vec2(textureProj(_sampler, vec4(_coord, 1.0) ) )\n"
  5923. "#define shadow2DProj(_sampler, _coord) vec2(textureProj(_sampler, _coord) )\n"
  5924. , &err
  5925. );
  5926. }
  5927. else
  5928. {
  5929. bx::write(&writer
  5930. , "#define shadow2D(_sampler, _coord) (textureProj(_sampler, vec4(_coord, 1.0) ) )\n"
  5931. "#define shadow2DProj(_sampler, _coord) (textureProj(_sampler, _coord) )\n"
  5932. , &err
  5933. );
  5934. }
  5935. bx::write(&writer
  5936. , "#define texture3D texture\n"
  5937. "#define textureCube texture\n"
  5938. , &err
  5939. );
  5940. uint32_t fragData = 0;
  5941. bool patchedFragData = s_renderGL->m_gles3 && !bx::findIdentifierMatch(code, "bgfx_FragData").isEmpty();
  5942. if (!patchedFragData && !bx::findIdentifierMatch(code, "gl_FragData").isEmpty() )
  5943. {
  5944. for (uint32_t ii = 0, num = g_caps.limits.maxFBAttachments; ii < num; ++ii)
  5945. {
  5946. char tmpFragData[16];
  5947. bx::snprintf(tmpFragData, BX_COUNTOF(tmpFragData), "gl_FragData[%d]", ii);
  5948. fragData = bx::max(fragData, bx::strFind(code, tmpFragData).isEmpty() ? 0 : ii+1);
  5949. }
  5950. BGFX_FATAL(0 != fragData, Fatal::InvalidShader, "Unable to find and patch gl_FragData!");
  5951. }
  5952. if (!bx::findIdentifierMatch(code, "beginFragmentShaderOrdering").isEmpty() )
  5953. {
  5954. if (s_extension[Extension::INTEL_fragment_shader_ordering].m_supported)
  5955. {
  5956. bx::write(&writer, "#extension GL_INTEL_fragment_shader_ordering : enable\n", &err);
  5957. }
  5958. else
  5959. {
  5960. bx::write(&writer, "#define beginFragmentShaderOrdering()\n", &err);
  5961. }
  5962. }
  5963. if (!bx::findIdentifierMatch(code, s_ARB_texture_multisample).isEmpty() )
  5964. {
  5965. bx::write(&writer, "#extension GL_ARB_texture_multisample : enable\n", &err);
  5966. }
  5967. if (0 != fragData)
  5968. {
  5969. if (!patchedFragData)
  5970. {
  5971. bx::write(&writer, &err, "out vec4 bgfx_FragData[%d];\n", fragData);
  5972. bx::write(&writer, "#define gl_FragData bgfx_FragData\n", &err);
  5973. }
  5974. }
  5975. else if (!patchedFragData)
  5976. {
  5977. if (bx::findIdentifierMatch(code, "bgfx_FragColor").isEmpty() )
  5978. {
  5979. bx::write(&writer
  5980. , "out vec4 bgfx_FragColor;\n"
  5981. "#define gl_FragColor bgfx_FragColor\n"
  5982. , &err
  5983. );
  5984. }
  5985. }
  5986. }
  5987. else
  5988. {
  5989. bx::write(&writer
  5990. , "#define attribute in\n"
  5991. "#define varying out\n"
  5992. , &err
  5993. );
  5994. }
  5995. if (!s_renderGL->m_gles3)
  5996. {
  5997. bx::write(&writer
  5998. , "#define lowp\n"
  5999. "#define mediump\n"
  6000. "#define highp\n"
  6001. , &err
  6002. );
  6003. }
  6004. bx::write(&writer, code.getPtr(), code.getLength(), &err);
  6005. bx::write(&writer, '\0', &err);
  6006. }
  6007. code.set(temp);
  6008. }
  6009. else if (GL_COMPUTE_SHADER == m_type)
  6010. {
  6011. int32_t codeLen = (int32_t)bx::strLen(code);
  6012. int32_t tempLen = codeLen + (4<<10);
  6013. char* temp = (char*)BX_STACK_ALLOC(tempLen);
  6014. bx::StaticMemoryBlockWriter writer(temp, tempLen);
  6015. int32_t verLen = 0;
  6016. if (s_renderGL->m_gles3)
  6017. {
  6018. const char* str = "#version 310 es\n";
  6019. verLen = bx::strLen(str);
  6020. bx::write(&writer, &err, str);
  6021. }
  6022. else
  6023. {
  6024. const char* str = "#version 430\n";
  6025. verLen = bx::strLen(str);
  6026. bx::write(&writer, &err, str);
  6027. }
  6028. bx::write(&writer
  6029. , "#define texture2DLod textureLod\n"
  6030. "#define texture2DLodOffset textureLodOffset\n"
  6031. "#define texture2DArrayLod textureLod\n"
  6032. "#define texture2DArrayLodOffset textureLodOffset\n"
  6033. "#define texture3DLod textureLod\n"
  6034. "#define textureCubeLod textureLod\n"
  6035. "#define texture2DGrad textureGrad\n"
  6036. "#define texture3DGrad textureGrad\n"
  6037. "#define textureCubeGrad textureGrad\n"
  6038. , &err
  6039. );
  6040. bx::write(&writer, code.getPtr()+verLen, codeLen-verLen, &err);
  6041. bx::write(&writer, '\0', &err);
  6042. code = temp;
  6043. }
  6044. {
  6045. const GLchar* str = (const GLchar*)code.getPtr();
  6046. int32_t len = code.getLength();
  6047. GL_CHECK(glShaderSource(m_id, 1, &str, &len) );
  6048. }
  6049. GL_CHECK(glCompileShader(m_id) );
  6050. GLint compiled = 0;
  6051. GL_CHECK(glGetShaderiv(m_id, GL_COMPILE_STATUS, &compiled) );
  6052. if (0 == compiled)
  6053. {
  6054. bx::LineReader lineReader(code);
  6055. for (int32_t line = 1; !lineReader.isDone(); ++line)
  6056. {
  6057. bx::StringView str = lineReader.next();
  6058. BX_TRACE("%3d %.*s", line, str.getLength(), str.getPtr() );
  6059. BX_UNUSED(str, line);
  6060. }
  6061. GLsizei len;
  6062. char log[1024];
  6063. GL_CHECK(glGetShaderInfoLog(m_id, sizeof(log), &len, log) );
  6064. GL_CHECK(glDeleteShader(m_id) );
  6065. m_id = 0;
  6066. BGFX_FATAL(false, bgfx::Fatal::InvalidShader, "Failed to compile shader. %d: %s", compiled, log);
  6067. }
  6068. else if (BX_ENABLED(BGFX_CONFIG_DEBUG)
  6069. && s_extension[Extension::ANGLE_translated_shader_source].m_supported
  6070. && NULL != glGetTranslatedShaderSourceANGLE)
  6071. {
  6072. GLsizei len;
  6073. GL_CHECK(glGetShaderiv(m_id, GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE, &len) );
  6074. char* source = (char*)BX_STACK_ALLOC(len);
  6075. GL_CHECK(glGetTranslatedShaderSourceANGLE(m_id, len, &len, source) );
  6076. BX_TRACE("ANGLE source (len: %d):\n%s\n####", len, source);
  6077. }
  6078. }
  6079. }
  6080. void ShaderGL::destroy()
  6081. {
  6082. if (0 != m_id)
  6083. {
  6084. GL_CHECK(glDeleteShader(m_id) );
  6085. m_id = 0;
  6086. }
  6087. }
  6088. void FrameBufferGL::create(uint8_t _num, const Attachment* _attachment)
  6089. {
  6090. GL_CHECK(glGenFramebuffers(1, &m_fbo[0]) );
  6091. m_denseIdx = UINT16_MAX;
  6092. m_numTh = _num;
  6093. bx::memCopy(m_attachment, _attachment, _num*sizeof(Attachment) );
  6094. m_needPresent = false;
  6095. postReset();
  6096. }
  6097. void FrameBufferGL::postReset()
  6098. {
  6099. if (0 != m_fbo[0])
  6100. {
  6101. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo[0]) );
  6102. bool needResolve = false;
  6103. GLenum buffers[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS];
  6104. uint32_t colorIdx = 0;
  6105. for (uint32_t ii = 0; ii < m_numTh; ++ii)
  6106. {
  6107. const Attachment& at = m_attachment[ii];
  6108. if (isValid(at.handle) )
  6109. {
  6110. const TextureGL& texture = s_renderGL->m_textures[at.handle.idx];
  6111. if (0 == colorIdx)
  6112. {
  6113. m_width = bx::uint32_max(texture.m_width >> at.mip, 1);
  6114. m_height = bx::uint32_max(texture.m_height >> at.mip, 1);
  6115. }
  6116. GLenum attachment = GL_COLOR_ATTACHMENT0 + colorIdx;
  6117. bimg::TextureFormat::Enum format = bimg::TextureFormat::Enum(texture.m_textureFormat);
  6118. if (bimg::isDepth(format) )
  6119. {
  6120. const bimg::ImageBlockInfo& info = bimg::getBlockInfo(format);
  6121. if (0 < info.stencilBits)
  6122. {
  6123. attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  6124. }
  6125. else if (0 == info.depthBits)
  6126. {
  6127. attachment = GL_STENCIL_ATTACHMENT;
  6128. }
  6129. else
  6130. {
  6131. attachment = GL_DEPTH_ATTACHMENT;
  6132. }
  6133. }
  6134. else if (Access::Write == at.access)
  6135. {
  6136. buffers[colorIdx] = attachment;
  6137. ++colorIdx;
  6138. }
  6139. if (0 != texture.m_rbo)
  6140. {
  6141. if (!(BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL >= 30) || s_renderGL->m_gles3)
  6142. && GL_DEPTH_STENCIL_ATTACHMENT == attachment)
  6143. {
  6144. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER
  6145. , GL_DEPTH_ATTACHMENT
  6146. , GL_RENDERBUFFER
  6147. , texture.m_rbo
  6148. ) );
  6149. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER
  6150. , GL_STENCIL_ATTACHMENT
  6151. , GL_RENDERBUFFER
  6152. , texture.m_rbo
  6153. ) );
  6154. }
  6155. else
  6156. {
  6157. GL_CHECK(glFramebufferRenderbuffer(GL_FRAMEBUFFER
  6158. , attachment
  6159. , GL_RENDERBUFFER
  6160. , texture.m_rbo
  6161. ) );
  6162. }
  6163. }
  6164. else
  6165. {
  6166. if (1 < texture.m_numLayers
  6167. && !texture.isCubeMap() )
  6168. {
  6169. if (1 < at.numLayers)
  6170. {
  6171. BX_ASSERT(0 == at.layer, "Can't use start layer > 0 when binding multiple layers to a framebuffer.");
  6172. GL_CHECK(glFramebufferTexture(GL_FRAMEBUFFER
  6173. , attachment
  6174. , texture.m_id
  6175. , at.mip
  6176. ) );
  6177. }
  6178. else
  6179. {
  6180. GL_CHECK(glFramebufferTextureLayer(GL_FRAMEBUFFER
  6181. , attachment
  6182. , texture.m_id
  6183. , at.mip
  6184. , at.layer
  6185. ) );
  6186. }
  6187. }
  6188. else
  6189. {
  6190. GLenum target = texture.isCubeMap()
  6191. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + at.layer
  6192. : texture.m_target
  6193. ;
  6194. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  6195. , attachment
  6196. , target
  6197. , texture.m_id
  6198. , at.mip
  6199. ) );
  6200. }
  6201. }
  6202. needResolve |= (0 != texture.m_rbo) && (0 != texture.m_id);
  6203. }
  6204. }
  6205. m_num = uint8_t(colorIdx);
  6206. if (0 == colorIdx && BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  6207. {
  6208. // When only depth is attached disable draw buffer to avoid
  6209. // GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER.
  6210. GL_CHECK(glDrawBuffer(GL_NONE) );
  6211. }
  6212. else if (g_caps.limits.maxFBAttachments > 1)
  6213. {
  6214. GL_CHECK(glDrawBuffers(colorIdx, buffers) );
  6215. }
  6216. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || s_renderGL->m_gles3)
  6217. {
  6218. // Disable read buffer to avoid GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER.
  6219. GL_CHECK(glReadBuffer(GL_NONE) );
  6220. }
  6221. frameBufferValidate();
  6222. if (needResolve)
  6223. {
  6224. GL_CHECK(glGenFramebuffers(1, &m_fbo[1]) );
  6225. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_fbo[1]) );
  6226. colorIdx = 0;
  6227. for (uint32_t ii = 0; ii < m_numTh; ++ii)
  6228. {
  6229. const Attachment& at = m_attachment[ii];
  6230. if (isValid(at.handle) )
  6231. {
  6232. const TextureGL& texture = s_renderGL->m_textures[at.handle.idx];
  6233. if (0 != texture.m_id)
  6234. {
  6235. GLenum attachment = GL_INVALID_ENUM;
  6236. bimg::TextureFormat::Enum format = bimg::TextureFormat::Enum(texture.m_textureFormat);
  6237. if (bimg::isDepth(format) )
  6238. {
  6239. const bimg::ImageBlockInfo& info = bimg::getBlockInfo(format);
  6240. if (0 < info.stencilBits)
  6241. {
  6242. attachment = GL_DEPTH_STENCIL_ATTACHMENT;
  6243. }
  6244. else if (0 == info.depthBits)
  6245. {
  6246. attachment = GL_STENCIL_ATTACHMENT;
  6247. }
  6248. else
  6249. {
  6250. attachment = GL_DEPTH_ATTACHMENT;
  6251. }
  6252. }
  6253. else
  6254. {
  6255. attachment = GL_COLOR_ATTACHMENT0 + colorIdx;
  6256. ++colorIdx;
  6257. }
  6258. GLenum target = texture.isCubeMap()
  6259. ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + at.layer
  6260. : texture.m_target
  6261. ;
  6262. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  6263. , attachment
  6264. , target
  6265. , texture.m_id
  6266. , at.mip
  6267. ) );
  6268. }
  6269. }
  6270. }
  6271. frameBufferValidate();
  6272. }
  6273. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderGL->m_msaaBackBufferFbo) );
  6274. }
  6275. }
  6276. void FrameBufferGL::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _format, TextureFormat::Enum _depthFormat)
  6277. {
  6278. BX_UNUSED(_format, _depthFormat);
  6279. m_swapChain = s_renderGL->m_glctx.createSwapChain(_nwh, _width, _height);
  6280. m_width = _width;
  6281. m_height = _height;
  6282. m_numTh = 0;
  6283. m_denseIdx = _denseIdx;
  6284. m_needPresent = false;
  6285. }
  6286. uint16_t FrameBufferGL::destroy()
  6287. {
  6288. if (0 != m_fbo[0])
  6289. {
  6290. GL_CHECK(glDeleteFramebuffers(0 == m_fbo[1] ? 1 : 2, m_fbo) );
  6291. m_num = 0;
  6292. }
  6293. if (NULL != m_swapChain)
  6294. {
  6295. s_renderGL->m_glctx.destroySwapChain(m_swapChain);
  6296. m_swapChain = NULL;
  6297. }
  6298. bx::memSet(m_fbo, 0, sizeof(m_fbo) );
  6299. uint16_t denseIdx = m_denseIdx;
  6300. m_denseIdx = UINT16_MAX;
  6301. m_needPresent = false;
  6302. m_numTh = 0;
  6303. return denseIdx;
  6304. }
  6305. void FrameBufferGL::resolve()
  6306. {
  6307. if (0 != m_fbo[1])
  6308. {
  6309. uint32_t colorIdx = 0;
  6310. for (uint32_t ii = 0; ii < m_numTh; ++ii)
  6311. {
  6312. const Attachment& at = m_attachment[ii];
  6313. if (isValid(at.handle) )
  6314. {
  6315. const TextureGL& texture = s_renderGL->m_textures[at.handle.idx];
  6316. const bool writeOnly = 0 != (texture.m_flags&BGFX_TEXTURE_RT_WRITE_ONLY);
  6317. bimg::TextureFormat::Enum format = bimg::TextureFormat::Enum(texture.m_textureFormat);
  6318. if (!bimg::isDepth(format) )
  6319. {
  6320. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  6321. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[0]) );
  6322. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[1]) );
  6323. GL_CHECK(glReadBuffer(GL_COLOR_ATTACHMENT0 + colorIdx) );
  6324. GL_CHECK(glDrawBuffer(GL_COLOR_ATTACHMENT0 + colorIdx) );
  6325. colorIdx++;
  6326. GL_CHECK(glBlitFramebuffer(0
  6327. , 0
  6328. , m_width
  6329. , m_height
  6330. , 0
  6331. , 0
  6332. , m_width
  6333. , m_height
  6334. , GL_COLOR_BUFFER_BIT
  6335. , GL_LINEAR
  6336. ) );
  6337. }
  6338. else if (!writeOnly)
  6339. {
  6340. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  6341. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[0]) );
  6342. GL_CHECK(glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo[1]) );
  6343. GL_CHECK(glBlitFramebuffer(0
  6344. , 0
  6345. , m_width
  6346. , m_height
  6347. , 0
  6348. , 0
  6349. , m_width
  6350. , m_height
  6351. , GL_DEPTH_BUFFER_BIT
  6352. , GL_NEAREST
  6353. ) );
  6354. }
  6355. }
  6356. }
  6357. GL_CHECK(glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo[0]) );
  6358. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) || s_renderGL->m_gles3)
  6359. {
  6360. GL_CHECK(glReadBuffer(GL_NONE) );
  6361. }
  6362. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, s_renderGL->m_msaaBackBufferFbo) );
  6363. }
  6364. for (uint32_t ii = 0; ii < m_numTh; ++ii)
  6365. {
  6366. const Attachment& at = m_attachment[ii];
  6367. if (isValid(at.handle) )
  6368. {
  6369. const TextureGL& texture = s_renderGL->m_textures[at.handle.idx];
  6370. texture.resolve(at.resolve);
  6371. }
  6372. }
  6373. }
  6374. void FrameBufferGL::discard(uint16_t _flags)
  6375. {
  6376. GLenum buffers[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS+2];
  6377. uint32_t idx = 0;
  6378. if (BGFX_CLEAR_NONE != (_flags & BGFX_CLEAR_DISCARD_COLOR_MASK) )
  6379. {
  6380. for (uint32_t ii = 0, num = m_num; ii < num; ++ii)
  6381. {
  6382. if (BGFX_CLEAR_NONE != (_flags & (BGFX_CLEAR_DISCARD_COLOR_0<<ii) ) )
  6383. {
  6384. buffers[idx++] = GL_COLOR_ATTACHMENT0 + ii;
  6385. }
  6386. }
  6387. }
  6388. uint32_t dsFlags = _flags & (BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL);
  6389. if (BGFX_CLEAR_NONE != dsFlags)
  6390. {
  6391. if (!BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES)
  6392. && (BGFX_CLEAR_DISCARD_DEPTH|BGFX_CLEAR_DISCARD_STENCIL) == dsFlags)
  6393. {
  6394. buffers[idx++] = GL_DEPTH_STENCIL_ATTACHMENT;
  6395. }
  6396. else if (BGFX_CLEAR_DISCARD_DEPTH == dsFlags)
  6397. {
  6398. buffers[idx++] = GL_DEPTH_ATTACHMENT;
  6399. }
  6400. else if (BGFX_CLEAR_DISCARD_STENCIL == dsFlags)
  6401. {
  6402. buffers[idx++] = GL_STENCIL_ATTACHMENT;
  6403. }
  6404. }
  6405. GL_CHECK(glInvalidateFramebuffer(GL_FRAMEBUFFER, idx, buffers) );
  6406. }
  6407. void OcclusionQueryGL::create()
  6408. {
  6409. for (uint32_t ii = 0; ii < BX_COUNTOF(m_query); ++ii)
  6410. {
  6411. Query& query = m_query[ii];
  6412. GL_CHECK(glGenQueries(1, &query.m_id) );
  6413. }
  6414. }
  6415. void OcclusionQueryGL::destroy()
  6416. {
  6417. for (uint32_t ii = 0; ii < BX_COUNTOF(m_query); ++ii)
  6418. {
  6419. Query& query = m_query[ii];
  6420. GL_CHECK(glDeleteQueries(1, &query.m_id) );
  6421. }
  6422. }
  6423. void OcclusionQueryGL::begin(Frame* _render, OcclusionQueryHandle _handle)
  6424. {
  6425. while (0 == m_control.reserve(1) )
  6426. {
  6427. resolve(_render, true);
  6428. }
  6429. Query& query = m_query[m_control.m_current];
  6430. GL_CHECK(glBeginQuery(GL_SAMPLES_PASSED, query.m_id) );
  6431. query.m_handle = _handle;
  6432. }
  6433. void OcclusionQueryGL::end()
  6434. {
  6435. GL_CHECK(glEndQuery(GL_SAMPLES_PASSED) );
  6436. m_control.commit(1);
  6437. }
  6438. void OcclusionQueryGL::resolve(Frame* _render, bool _wait)
  6439. {
  6440. while (0 != m_control.available() )
  6441. {
  6442. Query& query = m_query[m_control.m_read];
  6443. if (isValid(query.m_handle) )
  6444. {
  6445. int32_t result;
  6446. if (!_wait)
  6447. {
  6448. GL_CHECK(glGetQueryObjectiv(query.m_id, GL_QUERY_RESULT_AVAILABLE, &result) );
  6449. if (!result)
  6450. {
  6451. break;
  6452. }
  6453. }
  6454. GL_CHECK(glGetQueryObjectiv(query.m_id, GL_QUERY_RESULT, &result) );
  6455. _render->m_occlusion[query.m_handle.idx] = int32_t(result);
  6456. }
  6457. m_control.consume(1);
  6458. }
  6459. }
  6460. void OcclusionQueryGL::invalidate(OcclusionQueryHandle _handle)
  6461. {
  6462. const uint32_t size = m_control.m_size;
  6463. for (uint32_t ii = 0, num = m_control.available(); ii < num; ++ii)
  6464. {
  6465. Query& query = m_query[(m_control.m_read + ii) % size];
  6466. if (query.m_handle.idx == _handle.idx)
  6467. {
  6468. query.m_handle.idx = bgfx::kInvalidHandle;
  6469. }
  6470. }
  6471. }
  6472. void RendererContextGL::submitBlit(BlitState& _bs, uint16_t _view)
  6473. {
  6474. if (m_blitSupported)
  6475. {
  6476. while (_bs.hasItem(_view) )
  6477. {
  6478. const BlitItem& bi = _bs.advance();
  6479. const TextureGL& src = m_textures[bi.m_src.idx];
  6480. const TextureGL& dst = m_textures[bi.m_dst.idx];
  6481. GL_CHECK(glCopyImageSubData(src.m_id
  6482. , src.m_target
  6483. , bi.m_srcMip
  6484. , bi.m_srcX
  6485. , bi.m_srcY
  6486. , bi.m_srcZ
  6487. , dst.m_id
  6488. , dst.m_target
  6489. , bi.m_dstMip
  6490. , bi.m_dstX
  6491. , bi.m_dstY
  6492. , bi.m_dstZ
  6493. , bi.m_width
  6494. , bi.m_height
  6495. , bx::uint32_imax(bi.m_depth, 1)
  6496. ) );
  6497. }
  6498. }
  6499. else if (BX_ENABLED(BGFX_GL_CONFIG_BLIT_EMULATION) )
  6500. {
  6501. while (_bs.hasItem(_view) )
  6502. {
  6503. const BlitItem& bi = _bs.advance();
  6504. const TextureGL& src = m_textures[bi.m_src.idx];
  6505. const TextureGL& dst = m_textures[bi.m_dst.idx];
  6506. BX_ASSERT(0 == bi.m_srcZ && 0 == bi.m_dstZ && 1 >= bi.m_depth
  6507. , "Blitting 3D regions is not supported"
  6508. );
  6509. GLuint fbo;
  6510. GL_CHECK(glGenFramebuffers(1, &fbo) );
  6511. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, fbo) );
  6512. GL_CHECK(glFramebufferTexture2D(GL_FRAMEBUFFER
  6513. , GL_COLOR_ATTACHMENT0
  6514. , GL_TEXTURE_2D
  6515. , src.m_id
  6516. , bi.m_srcMip
  6517. ) );
  6518. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  6519. BX_ASSERT(GL_FRAMEBUFFER_COMPLETE == status, "glCheckFramebufferStatus failed 0x%08x", status);
  6520. BX_UNUSED(status);
  6521. GL_CHECK(glActiveTexture(GL_TEXTURE0) );
  6522. GL_CHECK(glBindTexture(GL_TEXTURE_2D, dst.m_id) );
  6523. GL_CHECK(glCopyTexSubImage2D(GL_TEXTURE_2D
  6524. , bi.m_dstMip
  6525. , bi.m_dstX
  6526. , bi.m_dstY
  6527. , bi.m_srcX
  6528. , bi.m_srcY
  6529. , bi.m_width
  6530. , bi.m_height
  6531. ) );
  6532. GL_CHECK(glDeleteFramebuffers(1, &fbo) );
  6533. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_currentFbo) );
  6534. }
  6535. }
  6536. }
  6537. void RendererContextGL::submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter)
  6538. {
  6539. if (_render->m_capture)
  6540. {
  6541. renderDocTriggerCapture();
  6542. }
  6543. m_glctx.makeCurrent(NULL);
  6544. BGFX_GL_PROFILER_BEGIN_LITERAL("rendererSubmit", kColorView);
  6545. if (0 != m_vao)
  6546. {
  6547. GL_CHECK(glBindVertexArray(m_vao) );
  6548. }
  6549. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_backBufferFbo) );
  6550. GL_CHECK(glFrontFace(GL_CW) );
  6551. updateResolution(_render->m_resolution);
  6552. int64_t timeBegin = bx::getHPCounter();
  6553. int64_t captureElapsed = 0;
  6554. uint32_t frameQueryIdx = UINT32_MAX;
  6555. if (m_timerQuerySupport)
  6556. {
  6557. frameQueryIdx = m_gpuTimer.begin(BGFX_CONFIG_MAX_VIEWS, _render->m_frameNum);
  6558. }
  6559. if (0 < _render->m_iboffset)
  6560. {
  6561. BGFX_PROFILER_SCOPE("bgfx/Update transient index buffer", kColorResource);
  6562. TransientIndexBuffer* ib = _render->m_transientIb;
  6563. m_indexBuffers[ib->handle.idx].update(0, _render->m_iboffset, ib->data, true);
  6564. }
  6565. if (0 < _render->m_vboffset)
  6566. {
  6567. BGFX_PROFILER_SCOPE("bgfx/Update transient vertex buffer", kColorResource);
  6568. TransientVertexBuffer* vb = _render->m_transientVb;
  6569. m_vertexBuffers[vb->handle.idx].update(0, _render->m_vboffset, vb->data, true);
  6570. }
  6571. _render->sort();
  6572. RenderDraw currentState;
  6573. currentState.clear();
  6574. currentState.m_stateFlags = BGFX_STATE_NONE;
  6575. currentState.m_stencil = packStencil(BGFX_STENCIL_NONE, BGFX_STENCIL_NONE);
  6576. RenderBind currentBind;
  6577. currentBind.clear();
  6578. static ViewState viewState;
  6579. viewState.reset(_render);
  6580. ProgramHandle currentProgram = BGFX_INVALID_HANDLE;
  6581. ProgramHandle boundProgram = BGFX_INVALID_HANDLE;
  6582. SortKey key;
  6583. uint16_t view = UINT16_MAX;
  6584. FrameBufferHandle fbh = { BGFX_CONFIG_MAX_FRAME_BUFFERS };
  6585. BlitState bs(_render);
  6586. int32_t resolutionHeight = _render->m_resolution.height;
  6587. uint32_t blendFactor = 0;
  6588. uint8_t primIndex;
  6589. {
  6590. const uint64_t pt = 0;
  6591. primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  6592. }
  6593. PrimInfo prim = s_primInfo[primIndex];
  6594. GL_CHECK(glPolygonMode(GL_FRONT_AND_BACK
  6595. , _render->m_debug&BGFX_DEBUG_WIREFRAME
  6596. ? GL_LINE
  6597. : GL_FILL
  6598. ) );
  6599. bool wasCompute = false;
  6600. bool viewHasScissor = false;
  6601. Rect viewScissorRect;
  6602. viewScissorRect.clear();
  6603. uint16_t discardFlags = BGFX_CLEAR_NONE;
  6604. const bool blendIndependentSupported = s_extension[Extension::ARB_draw_buffers_blend].m_supported;
  6605. const bool computeSupported = false
  6606. || (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) && s_extension[Extension::ARB_compute_shader].m_supported)
  6607. || BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGLES >= 31)
  6608. ;
  6609. const uint32_t maxComputeBindings = g_caps.limits.maxComputeBindings;
  6610. uint32_t statsNumPrimsSubmitted[BX_COUNTOF(s_primInfo)] = {};
  6611. uint32_t statsNumPrimsRendered[BX_COUNTOF(s_primInfo)] = {};
  6612. uint32_t statsNumInstances[BX_COUNTOF(s_primInfo)] = {};
  6613. uint32_t statsNumIndices = 0;
  6614. uint32_t statsKeyType[2] = {};
  6615. Profiler<TimerQueryGL> profiler(
  6616. _render
  6617. , m_gpuTimer
  6618. , s_viewName
  6619. , m_timerQuerySupport
  6620. );
  6621. if (m_occlusionQuerySupport)
  6622. {
  6623. m_occlusionQuery.resolve(_render);
  6624. }
  6625. if (0 == (_render->m_debug&BGFX_DEBUG_IFH) )
  6626. {
  6627. GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_msaaBackBufferFbo) );
  6628. viewState.m_rect = _render->m_view[0].m_rect;
  6629. int32_t numItems = _render->m_numRenderItems;
  6630. for (int32_t item = 0; item < numItems;)
  6631. {
  6632. const uint64_t encodedKey = _render->m_sortKeys[item];
  6633. const bool isCompute = key.decode(encodedKey, _render->m_viewRemap);
  6634. statsKeyType[isCompute]++;
  6635. const bool viewChanged = 0
  6636. || key.m_view != view
  6637. || item == numItems
  6638. ;
  6639. const uint32_t itemIdx = _render->m_sortValues[item];
  6640. const RenderItem& renderItem = _render->m_renderItem[itemIdx];
  6641. const RenderBind& renderBind = _render->m_renderItemBind[itemIdx];
  6642. ++item;
  6643. if (viewChanged)
  6644. {
  6645. view = key.m_view;
  6646. currentProgram = BGFX_INVALID_HANDLE;
  6647. if (item > 1)
  6648. {
  6649. profiler.end();
  6650. }
  6651. BGFX_GL_PROFILER_END();
  6652. if (_render->m_view[view].m_fbh.idx != fbh.idx)
  6653. {
  6654. fbh = _render->m_view[view].m_fbh;
  6655. resolutionHeight = _render->m_resolution.height;
  6656. resolutionHeight = setFrameBuffer(fbh, resolutionHeight, discardFlags);
  6657. }
  6658. setViewType(view, " ");
  6659. BGFX_GL_PROFILER_BEGIN(view, kColorView);
  6660. profiler.begin(view);
  6661. viewState.m_rect = _render->m_view[view].m_rect;
  6662. const Rect& scissorRect = _render->m_view[view].m_scissor;
  6663. viewHasScissor = !scissorRect.isZero();
  6664. viewScissorRect = viewHasScissor ? scissorRect : viewState.m_rect;
  6665. GL_CHECK(glViewport(viewState.m_rect.m_x
  6666. , resolutionHeight-viewState.m_rect.m_height-viewState.m_rect.m_y
  6667. , viewState.m_rect.m_width
  6668. , viewState.m_rect.m_height
  6669. ) );
  6670. Clear& clear = _render->m_view[view].m_clear;
  6671. discardFlags = clear.m_flags & BGFX_CLEAR_DISCARD_MASK;
  6672. if (BGFX_CLEAR_NONE != (clear.m_flags & BGFX_CLEAR_MASK) )
  6673. {
  6674. clearQuad(_clearQuad, viewState.m_rect, clear, resolutionHeight, _render->m_colorPalette);
  6675. }
  6676. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  6677. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  6678. GL_CHECK(glDepthFunc(GL_LESS) );
  6679. GL_CHECK(glEnable(GL_CULL_FACE) );
  6680. GL_CHECK(glDisable(GL_BLEND) );
  6681. submitBlit(bs, view);
  6682. }
  6683. if (isCompute)
  6684. {
  6685. if (!wasCompute)
  6686. {
  6687. wasCompute = true;
  6688. setViewType(view, "C");
  6689. BGFX_GL_PROFILER_END();
  6690. BGFX_GL_PROFILER_BEGIN(view, kColorCompute);
  6691. }
  6692. if (computeSupported)
  6693. {
  6694. const RenderCompute& compute = renderItem.compute;
  6695. ProgramGL& program = m_program[key.m_program.idx];
  6696. setProgram(program.m_id);
  6697. GLbitfield barrier = 0;
  6698. for (uint32_t ii = 0; ii < maxComputeBindings; ++ii)
  6699. {
  6700. const Binding& bind = renderBind.m_bind[ii];
  6701. if (kInvalidHandle != bind.m_idx)
  6702. {
  6703. switch (bind.m_type)
  6704. {
  6705. case Binding::Texture:
  6706. {
  6707. TextureGL& texture = m_textures[bind.m_idx];
  6708. texture.commit(ii, bind.m_samplerFlags, _render->m_colorPalette);
  6709. }
  6710. break;
  6711. case Binding::Image:
  6712. {
  6713. const TextureGL& texture = m_textures[bind.m_idx];
  6714. GL_CHECK(glBindImageTexture(ii
  6715. , texture.m_id
  6716. , bind.m_mip
  6717. , texture.isCubeMap() || texture.m_target == GL_TEXTURE_2D_ARRAY ? GL_TRUE : GL_FALSE
  6718. , 0
  6719. , s_access[bind.m_access]
  6720. , s_imageFormat[bind.m_format])
  6721. );
  6722. barrier |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT;
  6723. }
  6724. break;
  6725. case Binding::IndexBuffer:
  6726. {
  6727. const IndexBufferGL& buffer = m_indexBuffers[bind.m_idx];
  6728. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id) );
  6729. barrier |= GL_SHADER_STORAGE_BARRIER_BIT;
  6730. }
  6731. break;
  6732. case Binding::VertexBuffer:
  6733. {
  6734. const VertexBufferGL& buffer = m_vertexBuffers[bind.m_idx];
  6735. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ii, buffer.m_id) );
  6736. barrier |= GL_SHADER_STORAGE_BARRIER_BIT;
  6737. }
  6738. break;
  6739. }
  6740. }
  6741. }
  6742. if (0 != barrier)
  6743. {
  6744. bool constantsChanged = compute.m_uniformBegin < compute.m_uniformEnd;
  6745. rendererUpdateUniforms(this, _render->m_uniformBuffer[compute.m_uniformIdx], compute.m_uniformBegin, compute.m_uniformEnd);
  6746. if (constantsChanged
  6747. && NULL != program.m_constantBuffer)
  6748. {
  6749. commit(*program.m_constantBuffer);
  6750. }
  6751. viewState.setPredefined<1>(this, view, program, _render, compute);
  6752. if (isValid(compute.m_indirectBuffer) )
  6753. {
  6754. barrier |= GL_COMMAND_BARRIER_BIT;
  6755. const VertexBufferGL& vb = m_vertexBuffers[compute.m_indirectBuffer.idx];
  6756. if (currentState.m_indirectBuffer.idx != compute.m_indirectBuffer.idx)
  6757. {
  6758. currentState.m_indirectBuffer = compute.m_indirectBuffer;
  6759. GL_CHECK(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, vb.m_id) );
  6760. }
  6761. uint32_t numDrawIndirect = UINT32_MAX == compute.m_numIndirect
  6762. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  6763. : compute.m_numIndirect
  6764. ;
  6765. uintptr_t args = compute.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  6766. for (uint32_t ii = 0; ii < numDrawIndirect; ++ii)
  6767. {
  6768. GL_CHECK(glDispatchComputeIndirect( (GLintptr)args) );
  6769. args += BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  6770. }
  6771. }
  6772. else
  6773. {
  6774. if (isValid(currentState.m_indirectBuffer) )
  6775. {
  6776. currentState.m_indirectBuffer.idx = kInvalidHandle;
  6777. GL_CHECK(glBindBuffer(GL_DISPATCH_INDIRECT_BUFFER, 0) );
  6778. }
  6779. GL_CHECK(glDispatchCompute(compute.m_numX, compute.m_numY, compute.m_numZ) );
  6780. }
  6781. GL_CHECK(glMemoryBarrier(barrier) );
  6782. }
  6783. }
  6784. continue;
  6785. }
  6786. bool resetState = viewChanged || wasCompute;
  6787. if (wasCompute)
  6788. {
  6789. wasCompute = false;
  6790. setViewType(view, " ");
  6791. BGFX_GL_PROFILER_END();
  6792. BGFX_GL_PROFILER_BEGIN(view, kColorDraw);
  6793. }
  6794. const RenderDraw& draw = renderItem.draw;
  6795. const bool hasOcclusionQuery = 0 != (draw.m_stateFlags & BGFX_STATE_INTERNAL_OCCLUSION_QUERY);
  6796. {
  6797. const bool occluded = true
  6798. && isValid(draw.m_occlusionQuery)
  6799. && !hasOcclusionQuery
  6800. && !isVisible(_render, draw.m_occlusionQuery, 0 != (draw.m_submitFlags&BGFX_SUBMIT_INTERNAL_OCCLUSION_VISIBLE) )
  6801. ;
  6802. if (occluded
  6803. || _render->m_frameCache.isZeroArea(viewScissorRect, draw.m_scissor) )
  6804. {
  6805. if (resetState)
  6806. {
  6807. currentState.clear();
  6808. currentState.m_scissor = !draw.m_scissor;
  6809. currentBind.clear();
  6810. }
  6811. continue;
  6812. }
  6813. }
  6814. const uint64_t newFlags = draw.m_stateFlags;
  6815. uint64_t changedFlags = currentState.m_stateFlags ^ draw.m_stateFlags;
  6816. currentState.m_stateFlags = newFlags;
  6817. const uint64_t newStencil = draw.m_stencil;
  6818. uint64_t changedStencil = currentState.m_stencil ^ draw.m_stencil;
  6819. currentState.m_stencil = newStencil;
  6820. if (resetState)
  6821. {
  6822. currentState.clear();
  6823. currentState.m_scissor = !draw.m_scissor;
  6824. changedFlags = BGFX_STATE_MASK;
  6825. changedStencil = packStencil(BGFX_STENCIL_MASK, BGFX_STENCIL_MASK);
  6826. currentState.m_stateFlags = newFlags;
  6827. currentState.m_stencil = newStencil;
  6828. currentBind.clear();
  6829. }
  6830. uint16_t scissor = draw.m_scissor;
  6831. if (currentState.m_scissor != scissor)
  6832. {
  6833. currentState.m_scissor = scissor;
  6834. if (UINT16_MAX == scissor)
  6835. {
  6836. if (viewHasScissor)
  6837. {
  6838. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  6839. GL_CHECK(glScissor(viewScissorRect.m_x
  6840. , resolutionHeight-viewScissorRect.m_height-viewScissorRect.m_y
  6841. , viewScissorRect.m_width
  6842. , viewScissorRect.m_height
  6843. ) );
  6844. }
  6845. else
  6846. {
  6847. GL_CHECK(glDisable(GL_SCISSOR_TEST) );
  6848. }
  6849. }
  6850. else
  6851. {
  6852. Rect scissorRect;
  6853. scissorRect.setIntersect(viewScissorRect, _render->m_frameCache.m_rectCache.m_cache[scissor]);
  6854. GL_CHECK(glEnable(GL_SCISSOR_TEST) );
  6855. GL_CHECK(glScissor(scissorRect.m_x
  6856. , resolutionHeight-scissorRect.m_height-scissorRect.m_y
  6857. , scissorRect.m_width
  6858. , scissorRect.m_height
  6859. ) );
  6860. }
  6861. }
  6862. if (0 != changedStencil)
  6863. {
  6864. if (0 != newStencil)
  6865. {
  6866. GL_CHECK(glEnable(GL_STENCIL_TEST) );
  6867. uint32_t bstencil = unpackStencil(1, newStencil);
  6868. uint8_t frontAndBack = bstencil != BGFX_STENCIL_NONE && bstencil != unpackStencil(0, newStencil);
  6869. // uint32_t bchanged = unpackStencil(1, changedStencil);
  6870. // if (BGFX_STENCIL_FUNC_RMASK_MASK & bchanged)
  6871. // {
  6872. // uint32_t wmask = (bstencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  6873. // GL_CHECK(glStencilMask(wmask) );
  6874. // }
  6875. for (uint8_t ii = 0, num = frontAndBack+1; ii < num; ++ii)
  6876. {
  6877. uint32_t stencil = unpackStencil(ii, newStencil);
  6878. uint32_t changed = unpackStencil(ii, changedStencil);
  6879. GLenum face = s_stencilFace[frontAndBack+ii];
  6880. if ( (BGFX_STENCIL_TEST_MASK|BGFX_STENCIL_FUNC_REF_MASK|BGFX_STENCIL_FUNC_RMASK_MASK) & changed)
  6881. {
  6882. GLint ref = (stencil&BGFX_STENCIL_FUNC_REF_MASK)>>BGFX_STENCIL_FUNC_REF_SHIFT;
  6883. GLint mask = (stencil&BGFX_STENCIL_FUNC_RMASK_MASK)>>BGFX_STENCIL_FUNC_RMASK_SHIFT;
  6884. uint32_t func = (stencil&BGFX_STENCIL_TEST_MASK)>>BGFX_STENCIL_TEST_SHIFT;
  6885. GL_CHECK(glStencilFuncSeparate(face, s_cmpFunc[func], ref, mask) );
  6886. }
  6887. if ( (BGFX_STENCIL_OP_FAIL_S_MASK|BGFX_STENCIL_OP_FAIL_Z_MASK|BGFX_STENCIL_OP_PASS_Z_MASK) & changed)
  6888. {
  6889. uint32_t sfail = (stencil&BGFX_STENCIL_OP_FAIL_S_MASK)>>BGFX_STENCIL_OP_FAIL_S_SHIFT;
  6890. uint32_t zfail = (stencil&BGFX_STENCIL_OP_FAIL_Z_MASK)>>BGFX_STENCIL_OP_FAIL_Z_SHIFT;
  6891. uint32_t zpass = (stencil&BGFX_STENCIL_OP_PASS_Z_MASK)>>BGFX_STENCIL_OP_PASS_Z_SHIFT;
  6892. GL_CHECK(glStencilOpSeparate(face, s_stencilOp[sfail], s_stencilOp[zfail], s_stencilOp[zpass]) );
  6893. }
  6894. }
  6895. }
  6896. else
  6897. {
  6898. GL_CHECK(glDisable(GL_STENCIL_TEST) );
  6899. }
  6900. }
  6901. if ( ( (0
  6902. | BGFX_STATE_ALPHA_REF_MASK
  6903. | BGFX_STATE_BLEND_ALPHA_TO_COVERAGE
  6904. | BGFX_STATE_BLEND_EQUATION_MASK
  6905. | BGFX_STATE_BLEND_INDEPENDENT
  6906. | BGFX_STATE_BLEND_MASK
  6907. | BGFX_STATE_CONSERVATIVE_RASTER
  6908. | BGFX_STATE_CULL_MASK
  6909. | BGFX_STATE_FRONT_CCW
  6910. | BGFX_STATE_DEPTH_TEST_MASK
  6911. | BGFX_STATE_LINEAA
  6912. | BGFX_STATE_MSAA
  6913. | BGFX_STATE_POINT_SIZE_MASK
  6914. | BGFX_STATE_PT_MASK
  6915. | BGFX_STATE_WRITE_A
  6916. | BGFX_STATE_WRITE_RGB
  6917. | BGFX_STATE_WRITE_Z
  6918. ) & changedFlags)
  6919. || blendFactor != draw.m_rgba)
  6920. {
  6921. if (BGFX_STATE_FRONT_CCW & changedFlags)
  6922. {
  6923. GL_CHECK(glFrontFace( (BGFX_STATE_FRONT_CCW & newFlags) ? GL_CCW : GL_CW) );
  6924. }
  6925. if (BGFX_STATE_CULL_MASK & changedFlags)
  6926. {
  6927. if (BGFX_STATE_CULL_CCW & newFlags)
  6928. {
  6929. GL_CHECK(glEnable(GL_CULL_FACE) );
  6930. GL_CHECK(glCullFace(GL_BACK) );
  6931. }
  6932. else if (BGFX_STATE_CULL_CW & newFlags)
  6933. {
  6934. GL_CHECK(glEnable(GL_CULL_FACE) );
  6935. GL_CHECK(glCullFace(GL_FRONT) );
  6936. }
  6937. else
  6938. {
  6939. GL_CHECK(glDisable(GL_CULL_FACE) );
  6940. }
  6941. }
  6942. if (BGFX_STATE_WRITE_Z & changedFlags)
  6943. {
  6944. GL_CHECK(glDepthMask(!!(BGFX_STATE_WRITE_Z & newFlags) ) );
  6945. }
  6946. if (BGFX_STATE_DEPTH_TEST_MASK & changedFlags)
  6947. {
  6948. uint32_t func = (newFlags&BGFX_STATE_DEPTH_TEST_MASK)>>BGFX_STATE_DEPTH_TEST_SHIFT;
  6949. if (0 != func)
  6950. {
  6951. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  6952. GL_CHECK(glDepthFunc(s_cmpFunc[func]) );
  6953. }
  6954. else
  6955. {
  6956. if (BGFX_STATE_WRITE_Z & newFlags)
  6957. {
  6958. GL_CHECK(glEnable(GL_DEPTH_TEST) );
  6959. GL_CHECK(glDepthFunc(GL_ALWAYS) );
  6960. }
  6961. else
  6962. {
  6963. GL_CHECK(glDisable(GL_DEPTH_TEST) );
  6964. }
  6965. }
  6966. }
  6967. if (BGFX_STATE_ALPHA_REF_MASK & changedFlags)
  6968. {
  6969. uint32_t ref = (newFlags&BGFX_STATE_ALPHA_REF_MASK)>>BGFX_STATE_ALPHA_REF_SHIFT;
  6970. viewState.m_alphaRef = ref/255.0f;
  6971. }
  6972. if (BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL) )
  6973. {
  6974. if ( (BGFX_STATE_PT_POINTS|BGFX_STATE_POINT_SIZE_MASK) & changedFlags)
  6975. {
  6976. float pointSize = (float)(bx::uint32_max(1, (newFlags&BGFX_STATE_POINT_SIZE_MASK)>>BGFX_STATE_POINT_SIZE_SHIFT) );
  6977. GL_CHECK(glPointSize(pointSize) );
  6978. }
  6979. if (BGFX_STATE_MSAA & changedFlags)
  6980. {
  6981. GL_CHECK(BGFX_STATE_MSAA & newFlags
  6982. ? glEnable(GL_MULTISAMPLE)
  6983. : glDisable(GL_MULTISAMPLE)
  6984. );
  6985. }
  6986. if (BGFX_STATE_LINEAA & changedFlags)
  6987. {
  6988. GL_CHECK(BGFX_STATE_LINEAA & newFlags
  6989. ? glEnable(GL_LINE_SMOOTH)
  6990. : glDisable(GL_LINE_SMOOTH)
  6991. );
  6992. }
  6993. if (m_conservativeRasterSupport
  6994. && BGFX_STATE_CONSERVATIVE_RASTER & changedFlags)
  6995. {
  6996. GL_CHECK(BGFX_STATE_CONSERVATIVE_RASTER & newFlags
  6997. ? glEnable(GL_CONSERVATIVE_RASTERIZATION_NV)
  6998. : glDisable(GL_CONSERVATIVE_RASTERIZATION_NV)
  6999. );
  7000. }
  7001. }
  7002. if ( (BGFX_STATE_WRITE_A|BGFX_STATE_WRITE_RGB) & changedFlags)
  7003. {
  7004. const GLboolean rr = !!(newFlags&BGFX_STATE_WRITE_R);
  7005. const GLboolean gg = !!(newFlags&BGFX_STATE_WRITE_G);
  7006. const GLboolean bb = !!(newFlags&BGFX_STATE_WRITE_B);
  7007. const GLboolean aa = !!(newFlags&BGFX_STATE_WRITE_A);
  7008. GL_CHECK(glColorMask(rr, gg, bb, aa) );
  7009. }
  7010. if ( ( (0
  7011. | BGFX_STATE_BLEND_ALPHA_TO_COVERAGE
  7012. | BGFX_STATE_BLEND_EQUATION_MASK
  7013. | BGFX_STATE_BLEND_INDEPENDENT
  7014. | BGFX_STATE_BLEND_MASK
  7015. ) & changedFlags)
  7016. || blendFactor != draw.m_rgba)
  7017. {
  7018. if (m_atocSupport)
  7019. {
  7020. if (BGFX_STATE_BLEND_ALPHA_TO_COVERAGE & newFlags)
  7021. {
  7022. GL_CHECK(glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE) );
  7023. }
  7024. else
  7025. {
  7026. GL_CHECK(glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE) );
  7027. }
  7028. }
  7029. if ( ( (0
  7030. | BGFX_STATE_BLEND_EQUATION_MASK
  7031. | BGFX_STATE_BLEND_INDEPENDENT
  7032. | BGFX_STATE_BLEND_MASK
  7033. ) & newFlags)
  7034. || blendFactor != draw.m_rgba)
  7035. {
  7036. const bool enabled = !!(BGFX_STATE_BLEND_MASK & newFlags);
  7037. const bool independent = !!(BGFX_STATE_BLEND_INDEPENDENT & newFlags)
  7038. && blendIndependentSupported
  7039. ;
  7040. const uint32_t blend = uint32_t( (newFlags&BGFX_STATE_BLEND_MASK)>>BGFX_STATE_BLEND_SHIFT);
  7041. const uint32_t srcRGB = (blend )&0xf;
  7042. const uint32_t dstRGB = (blend>> 4)&0xf;
  7043. const uint32_t srcA = (blend>> 8)&0xf;
  7044. const uint32_t dstA = (blend>>12)&0xf;
  7045. const uint32_t equ = uint32_t( (newFlags&BGFX_STATE_BLEND_EQUATION_MASK)>>BGFX_STATE_BLEND_EQUATION_SHIFT);
  7046. const uint32_t equRGB = (equ )&0x7;
  7047. const uint32_t equA = (equ>>3)&0x7;
  7048. const uint32_t numRt = getNumRt();
  7049. if (!BX_ENABLED(BGFX_CONFIG_RENDERER_OPENGL)
  7050. || 1 >= numRt
  7051. || !independent)
  7052. {
  7053. if (enabled)
  7054. {
  7055. GL_CHECK(glEnable(GL_BLEND) );
  7056. GL_CHECK(glBlendFuncSeparate(s_blendFactor[srcRGB].m_src
  7057. , s_blendFactor[dstRGB].m_dst
  7058. , s_blendFactor[srcA].m_src
  7059. , s_blendFactor[dstA].m_dst
  7060. ) );
  7061. GL_CHECK(glBlendEquationSeparate(s_blendEquation[equRGB], s_blendEquation[equA]) );
  7062. if ( (s_blendFactor[srcRGB].m_factor || s_blendFactor[dstRGB].m_factor)
  7063. && blendFactor != draw.m_rgba)
  7064. {
  7065. const uint32_t rgba = draw.m_rgba;
  7066. GLclampf rr = ( (rgba>>24) )/255.0f;
  7067. GLclampf gg = ( (rgba>>16)&0xff)/255.0f;
  7068. GLclampf bb = ( (rgba>> 8)&0xff)/255.0f;
  7069. GLclampf aa = ( (rgba )&0xff)/255.0f;
  7070. GL_CHECK(glBlendColor(rr, gg, bb, aa) );
  7071. }
  7072. }
  7073. else
  7074. {
  7075. GL_CHECK(glDisable(GL_BLEND) );
  7076. }
  7077. }
  7078. else
  7079. {
  7080. if (enabled)
  7081. {
  7082. GL_CHECK(glEnablei(GL_BLEND, 0) );
  7083. GL_CHECK(glBlendFuncSeparatei(0
  7084. , s_blendFactor[srcRGB].m_src
  7085. , s_blendFactor[dstRGB].m_dst
  7086. , s_blendFactor[srcA].m_src
  7087. , s_blendFactor[dstA].m_dst
  7088. ) );
  7089. GL_CHECK(glBlendEquationSeparatei(0
  7090. , s_blendEquation[equRGB]
  7091. , s_blendEquation[equA]
  7092. ) );
  7093. }
  7094. else
  7095. {
  7096. GL_CHECK(glDisablei(GL_BLEND, 0) );
  7097. }
  7098. for (uint32_t ii = 1, rgba = draw.m_rgba; ii < numRt; ++ii, rgba >>= 11)
  7099. {
  7100. if (0 != (rgba&0x7ff) )
  7101. {
  7102. const uint32_t src = (rgba )&0xf;
  7103. const uint32_t dst = (rgba>>4)&0xf;
  7104. const uint32_t equation = (rgba>>8)&0x7;
  7105. GL_CHECK(glEnablei(GL_BLEND, ii) );
  7106. GL_CHECK(glBlendFunci(ii, s_blendFactor[src].m_src, s_blendFactor[dst].m_dst) );
  7107. GL_CHECK(glBlendEquationi(ii, s_blendEquation[equation]) );
  7108. }
  7109. else
  7110. {
  7111. GL_CHECK(glDisablei(GL_BLEND, ii) );
  7112. }
  7113. }
  7114. }
  7115. }
  7116. else
  7117. {
  7118. GL_CHECK(glDisable(GL_BLEND) );
  7119. }
  7120. blendFactor = draw.m_rgba;
  7121. }
  7122. const uint64_t pt = newFlags&BGFX_STATE_PT_MASK;
  7123. primIndex = uint8_t(pt>>BGFX_STATE_PT_SHIFT);
  7124. prim = s_primInfo[primIndex];
  7125. }
  7126. bool programChanged = false;
  7127. bool constantsChanged = draw.m_uniformBegin < draw.m_uniformEnd;
  7128. bool bindAttribs = false;
  7129. rendererUpdateUniforms(this, _render->m_uniformBuffer[draw.m_uniformIdx], draw.m_uniformBegin, draw.m_uniformEnd);
  7130. if (key.m_program.idx != currentProgram.idx)
  7131. {
  7132. currentProgram = key.m_program;
  7133. GLuint id = isValid(currentProgram) ? m_program[currentProgram.idx].m_id : 0;
  7134. // Skip rendering if program index is valid, but program is invalid.
  7135. currentProgram = 0 == id ? ProgramHandle{kInvalidHandle} : currentProgram;
  7136. setProgram(id);
  7137. programChanged =
  7138. constantsChanged =
  7139. bindAttribs = true;
  7140. }
  7141. if (isValid(currentProgram) )
  7142. {
  7143. ProgramGL& program = m_program[currentProgram.idx];
  7144. if (constantsChanged
  7145. && NULL != program.m_constantBuffer)
  7146. {
  7147. commit(*program.m_constantBuffer);
  7148. }
  7149. viewState.setPredefined<1>(this, view, program, _render, draw);
  7150. {
  7151. GLbitfield barrier = 0;
  7152. for (uint32_t stage = 0; stage < BGFX_CONFIG_MAX_TEXTURE_SAMPLERS; ++stage)
  7153. {
  7154. const Binding& bind = renderBind.m_bind[stage];
  7155. Binding& current = currentBind.m_bind[stage];
  7156. if (current.m_idx != bind.m_idx
  7157. || current.m_type != bind.m_type
  7158. || current.m_samplerFlags != bind.m_samplerFlags
  7159. || programChanged)
  7160. {
  7161. if (kInvalidHandle != bind.m_idx)
  7162. {
  7163. switch (bind.m_type)
  7164. {
  7165. case Binding::Image:
  7166. {
  7167. const TextureGL& texture = m_textures[bind.m_idx];
  7168. GL_CHECK(glBindImageTexture(stage
  7169. , texture.m_id
  7170. , bind.m_mip
  7171. , texture.isCubeMap() || texture.m_target == GL_TEXTURE_2D_ARRAY ? GL_TRUE : GL_FALSE
  7172. , 0
  7173. , s_access[bind.m_access]
  7174. , s_imageFormat[bind.m_format])
  7175. );
  7176. barrier |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT;
  7177. }
  7178. break;
  7179. case Binding::Texture:
  7180. {
  7181. TextureGL& texture = m_textures[bind.m_idx];
  7182. texture.commit(stage, bind.m_samplerFlags, _render->m_colorPalette);
  7183. }
  7184. break;
  7185. case Binding::IndexBuffer:
  7186. {
  7187. const IndexBufferGL& buffer = m_indexBuffers[bind.m_idx];
  7188. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, stage, buffer.m_id) );
  7189. }
  7190. break;
  7191. case Binding::VertexBuffer:
  7192. {
  7193. const VertexBufferGL& buffer = m_vertexBuffers[bind.m_idx];
  7194. GL_CHECK(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, stage, buffer.m_id) );
  7195. }
  7196. break;
  7197. }
  7198. }
  7199. }
  7200. current = bind;
  7201. }
  7202. if (0 != barrier)
  7203. {
  7204. GL_CHECK(glMemoryBarrier(barrier) );
  7205. }
  7206. }
  7207. {
  7208. for (uint32_t idx = 0, streamMask = draw.m_streamMask
  7209. ; 0 != streamMask
  7210. ; streamMask >>= 1, idx += 1
  7211. )
  7212. {
  7213. const uint32_t ntz = bx::uint32_cnttz(streamMask);
  7214. streamMask >>= ntz;
  7215. idx += ntz;
  7216. if (currentState.m_stream[idx].m_handle.idx != draw.m_stream[idx].m_handle.idx)
  7217. {
  7218. currentState.m_stream[idx].m_handle = draw.m_stream[idx].m_handle;
  7219. bindAttribs = true;
  7220. }
  7221. if (currentState.m_stream[idx].m_startVertex != draw.m_stream[idx].m_startVertex)
  7222. {
  7223. currentState.m_stream[idx].m_startVertex = draw.m_stream[idx].m_startVertex;
  7224. bindAttribs = true;
  7225. }
  7226. }
  7227. if (programChanged
  7228. || currentState.m_streamMask != draw.m_streamMask
  7229. || currentState.m_instanceDataBuffer.idx != draw.m_instanceDataBuffer.idx
  7230. || currentState.m_instanceDataOffset != draw.m_instanceDataOffset
  7231. || currentState.m_instanceDataStride != draw.m_instanceDataStride)
  7232. {
  7233. currentState.m_streamMask = draw.m_streamMask;
  7234. currentState.m_instanceDataBuffer = draw.m_instanceDataBuffer;
  7235. currentState.m_instanceDataOffset = draw.m_instanceDataOffset;
  7236. currentState.m_instanceDataStride = draw.m_instanceDataStride;
  7237. bindAttribs = true;
  7238. }
  7239. if (currentState.m_indexBuffer.idx != draw.m_indexBuffer.idx)
  7240. {
  7241. currentState.m_indexBuffer = draw.m_indexBuffer;
  7242. if (isValid(draw.m_indexBuffer) )
  7243. {
  7244. IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  7245. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.m_id) );
  7246. }
  7247. else
  7248. {
  7249. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) );
  7250. }
  7251. }
  7252. if (currentState.m_startIndex != draw.m_startIndex)
  7253. {
  7254. currentState.m_startIndex = draw.m_startIndex;
  7255. }
  7256. if (0 != currentState.m_streamMask)
  7257. {
  7258. if (bindAttribs)
  7259. {
  7260. if (isValid(boundProgram) )
  7261. {
  7262. m_program[boundProgram.idx].unbindAttributes();
  7263. m_program[boundProgram.idx].unbindInstanceData();
  7264. }
  7265. boundProgram = currentProgram;
  7266. program.bindAttributesBegin();
  7267. if (UINT8_MAX != draw.m_streamMask)
  7268. {
  7269. for (uint32_t idx = 0, streamMask = draw.m_streamMask
  7270. ; 0 != streamMask
  7271. ; streamMask >>= 1, idx += 1
  7272. )
  7273. {
  7274. const uint32_t ntz = bx::uint32_cnttz(streamMask);
  7275. streamMask >>= ntz;
  7276. idx += ntz;
  7277. const VertexBufferGL& vb = m_vertexBuffers[draw.m_stream[idx].m_handle.idx];
  7278. const uint16_t decl = isValid(draw.m_stream[idx].m_layoutHandle)
  7279. ? draw.m_stream[idx].m_layoutHandle.idx
  7280. : vb.m_layoutHandle.idx;
  7281. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vb.m_id) );
  7282. program.bindAttributes(m_vertexLayouts[decl], draw.m_stream[idx].m_startVertex);
  7283. }
  7284. }
  7285. if (isValid(draw.m_instanceDataBuffer) )
  7286. {
  7287. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffers[draw.m_instanceDataBuffer.idx].m_id) );
  7288. program.bindInstanceData(draw.m_instanceDataStride, draw.m_instanceDataOffset);
  7289. }
  7290. program.bindAttributesEnd();
  7291. }
  7292. }
  7293. }
  7294. if (0 != currentState.m_streamMask)
  7295. {
  7296. uint32_t numVertices = draw.m_numVertices;
  7297. if (UINT32_MAX == numVertices)
  7298. {
  7299. for (uint32_t idx = 0, streamMask = draw.m_streamMask
  7300. ; 0 != streamMask
  7301. ; streamMask >>= 1, idx += 1
  7302. )
  7303. {
  7304. const uint32_t ntz = bx::uint32_cnttz(streamMask);
  7305. streamMask >>= ntz;
  7306. idx += ntz;
  7307. const VertexBufferGL& vb = m_vertexBuffers[draw.m_stream[idx].m_handle.idx];
  7308. uint16_t decl = !isValid(vb.m_layoutHandle) ? draw.m_stream[idx].m_layoutHandle.idx : vb.m_layoutHandle.idx;
  7309. const VertexLayout& layout = m_vertexLayouts[decl];
  7310. numVertices = bx::uint32_min(numVertices, vb.m_size/layout.m_stride);
  7311. }
  7312. }
  7313. uint32_t numIndices = 0;
  7314. uint32_t numPrimsSubmitted = 0;
  7315. uint32_t numInstances = 0;
  7316. uint32_t numPrimsRendered = 0;
  7317. uint32_t numDrawIndirect = 0;
  7318. if (hasOcclusionQuery)
  7319. {
  7320. m_occlusionQuery.begin(_render, draw.m_occlusionQuery);
  7321. }
  7322. if (isValid(draw.m_indirectBuffer) )
  7323. {
  7324. const VertexBufferGL& vb = m_vertexBuffers[draw.m_indirectBuffer.idx];
  7325. if (currentState.m_indirectBuffer.idx != draw.m_indirectBuffer.idx)
  7326. {
  7327. currentState.m_indirectBuffer = draw.m_indirectBuffer;
  7328. GL_CHECK(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, vb.m_id) );
  7329. }
  7330. uint32_t numOffsetIndirect = 0;
  7331. if (isValid(draw.m_numIndirectBuffer) )
  7332. {
  7333. if (currentState.m_numIndirectBuffer.idx != draw.m_numIndirectBuffer.idx)
  7334. {
  7335. const IndexBufferGL& nb = m_indexBuffers[draw.m_numIndirectBuffer.idx];
  7336. currentState.m_numIndirectBuffer = draw.m_numIndirectBuffer;
  7337. GL_CHECK(glBindBuffer(GL_PARAMETER_BUFFER_ARB, nb.m_id) );
  7338. }
  7339. numOffsetIndirect = draw.m_numIndirectIndex * sizeof(uint32_t);
  7340. }
  7341. if (isValid(draw.m_indexBuffer) )
  7342. {
  7343. const IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  7344. const bool hasIndex16 = 0 == (ib.m_flags & BGFX_BUFFER_INDEX32);
  7345. const GLenum indexFormat = hasIndex16
  7346. ? GL_UNSIGNED_SHORT
  7347. : GL_UNSIGNED_INT
  7348. ;
  7349. numDrawIndirect = UINT32_MAX == draw.m_numIndirect
  7350. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  7351. : draw.m_numIndirect
  7352. ;
  7353. uintptr_t args = draw.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  7354. if (isValid(draw.m_numIndirectBuffer) )
  7355. {
  7356. GL_CHECK(glMultiDrawElementsIndirectCount(prim.m_type, indexFormat
  7357. , (void*)args
  7358. , numOffsetIndirect
  7359. , numDrawIndirect
  7360. , BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  7361. ) );
  7362. }
  7363. else
  7364. {
  7365. GL_CHECK(glMultiDrawElementsIndirect(prim.m_type, indexFormat
  7366. , (void*)args
  7367. , numDrawIndirect
  7368. , BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  7369. ) );
  7370. }
  7371. }
  7372. else
  7373. {
  7374. numDrawIndirect = UINT32_MAX == draw.m_numIndirect
  7375. ? vb.m_size/BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  7376. : draw.m_numIndirect
  7377. ;
  7378. uintptr_t args = draw.m_startIndirect * BGFX_CONFIG_DRAW_INDIRECT_STRIDE;
  7379. if (isValid(draw.m_numIndirectBuffer) )
  7380. {
  7381. GL_CHECK(glMultiDrawArraysIndirectCount(prim.m_type
  7382. , (void*)args
  7383. , numOffsetIndirect
  7384. , numDrawIndirect
  7385. , BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  7386. ) );
  7387. }
  7388. else
  7389. {
  7390. GL_CHECK(glMultiDrawArraysIndirect(prim.m_type
  7391. , (void*)args
  7392. , numDrawIndirect
  7393. , BGFX_CONFIG_DRAW_INDIRECT_STRIDE
  7394. ) );
  7395. }
  7396. }
  7397. }
  7398. else
  7399. {
  7400. if (isValid(currentState.m_indirectBuffer) )
  7401. {
  7402. currentState.m_indirectBuffer.idx = kInvalidHandle;
  7403. GL_CHECK(glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0) );
  7404. if (isValid(currentState.m_numIndirectBuffer) )
  7405. {
  7406. currentState.m_numIndirectBuffer.idx = kInvalidHandle;
  7407. GL_CHECK(glBindBuffer(GL_PARAMETER_BUFFER_ARB, 0) );
  7408. }
  7409. }
  7410. if (isValid(draw.m_indexBuffer) )
  7411. {
  7412. const IndexBufferGL& ib = m_indexBuffers[draw.m_indexBuffer.idx];
  7413. const bool isIndex16 = draw.isIndex16();
  7414. const uint32_t indexSize = isIndex16 ? 2 : 4;
  7415. const GLenum indexFormat = isIndex16
  7416. ? GL_UNSIGNED_SHORT
  7417. : GL_UNSIGNED_INT
  7418. ;
  7419. if (UINT32_MAX == draw.m_numIndices)
  7420. {
  7421. numIndices = ib.m_size/indexSize;
  7422. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  7423. numInstances = draw.m_numInstances;
  7424. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  7425. GL_CHECK(glDrawElementsInstanced(prim.m_type
  7426. , numIndices
  7427. , indexFormat
  7428. , (void*)0
  7429. , draw.m_numInstances
  7430. ) );
  7431. }
  7432. else if (prim.m_min <= draw.m_numIndices)
  7433. {
  7434. numIndices = draw.m_numIndices;
  7435. numPrimsSubmitted = numIndices/prim.m_div - prim.m_sub;
  7436. numInstances = draw.m_numInstances;
  7437. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  7438. GL_CHECK(glDrawElementsInstanced(prim.m_type
  7439. , numIndices
  7440. , indexFormat
  7441. , (void*)(uintptr_t)(draw.m_startIndex*indexSize)
  7442. , draw.m_numInstances
  7443. ) );
  7444. }
  7445. }
  7446. else
  7447. {
  7448. numPrimsSubmitted = numVertices/prim.m_div - prim.m_sub;
  7449. numInstances = draw.m_numInstances;
  7450. numPrimsRendered = numPrimsSubmitted*draw.m_numInstances;
  7451. GL_CHECK(glDrawArraysInstanced(prim.m_type
  7452. , 0
  7453. , numVertices
  7454. , draw.m_numInstances
  7455. ) );
  7456. }
  7457. }
  7458. if (hasOcclusionQuery)
  7459. {
  7460. m_occlusionQuery.end();
  7461. }
  7462. statsNumPrimsSubmitted[primIndex] += numPrimsSubmitted;
  7463. statsNumPrimsRendered[primIndex] += numPrimsRendered;
  7464. statsNumInstances[primIndex] += numInstances;
  7465. statsNumIndices += numIndices;
  7466. }
  7467. }
  7468. }
  7469. if (isValid(boundProgram) )
  7470. {
  7471. m_program[boundProgram.idx].unbindAttributes();
  7472. boundProgram = BGFX_INVALID_HANDLE;
  7473. }
  7474. if (wasCompute)
  7475. {
  7476. setViewType(view, "C");
  7477. BGFX_GL_PROFILER_END();
  7478. BGFX_GL_PROFILER_BEGIN(view, kColorCompute);
  7479. }
  7480. submitBlit(bs, BGFX_CONFIG_MAX_VIEWS);
  7481. blitMsaaFbo();
  7482. if (0 < _render->m_numRenderItems)
  7483. {
  7484. if (0 != (m_resolution.reset & BGFX_RESET_FLUSH_AFTER_RENDER) )
  7485. {
  7486. GL_CHECK(glFlush() );
  7487. }
  7488. captureElapsed = -bx::getHPCounter();
  7489. capture();
  7490. captureElapsed += bx::getHPCounter();
  7491. profiler.end();
  7492. }
  7493. if (m_srgbWriteControlSupport)
  7494. {
  7495. // switch state back to default for cases when the on-screen draw is done externally
  7496. GL_CHECK(glDisable(GL_FRAMEBUFFER_SRGB) );
  7497. }
  7498. }
  7499. BGFX_GL_PROFILER_END();
  7500. m_glctx.makeCurrent(NULL);
  7501. int64_t timeEnd = bx::getHPCounter();
  7502. int64_t frameTime = timeEnd - timeBegin;
  7503. static int64_t min = frameTime;
  7504. static int64_t max = frameTime;
  7505. min = min > frameTime ? frameTime : min;
  7506. max = max < frameTime ? frameTime : max;
  7507. static uint32_t maxGpuLatency = 0;
  7508. static double maxGpuElapsed = 0.0f;
  7509. double elapsedGpuMs = 0.0;
  7510. if (UINT32_MAX != frameQueryIdx)
  7511. {
  7512. m_gpuTimer.end(frameQueryIdx);
  7513. const TimerQueryGL::Result& result = m_gpuTimer.m_result[BGFX_CONFIG_MAX_VIEWS];
  7514. double toGpuMs = 1000.0 / 1e9;
  7515. elapsedGpuMs = (result.m_end - result.m_begin) * toGpuMs;
  7516. maxGpuElapsed = elapsedGpuMs > maxGpuElapsed ? elapsedGpuMs : maxGpuElapsed;
  7517. maxGpuLatency = bx::uint32_imax(maxGpuLatency, result.m_pending-1);
  7518. }
  7519. const int64_t timerFreq = bx::getHPFrequency();
  7520. Stats& perfStats = _render->m_perfStats;
  7521. perfStats.cpuTimeBegin = timeBegin;
  7522. perfStats.cpuTimeEnd = timeEnd;
  7523. perfStats.cpuTimerFreq = timerFreq;
  7524. const TimerQueryGL::Result& result = m_gpuTimer.m_result[BGFX_CONFIG_MAX_VIEWS];
  7525. perfStats.gpuTimeBegin = result.m_begin;
  7526. perfStats.gpuTimeEnd = result.m_end;
  7527. perfStats.gpuTimerFreq = 1000000000;
  7528. perfStats.numDraw = statsKeyType[0];
  7529. perfStats.numCompute = statsKeyType[1];
  7530. perfStats.numBlit = _render->m_numBlitItems;
  7531. perfStats.maxGpuLatency = maxGpuLatency;
  7532. perfStats.gpuFrameNum = result.m_frameNum;
  7533. bx::memCopy(perfStats.numPrims, statsNumPrimsRendered, sizeof(perfStats.numPrims) );
  7534. perfStats.gpuMemoryMax = -INT64_MAX;
  7535. perfStats.gpuMemoryUsed = -INT64_MAX;
  7536. if (_render->m_debug & (BGFX_DEBUG_IFH|BGFX_DEBUG_STATS) )
  7537. {
  7538. BGFX_GL_PROFILER_BEGIN_LITERAL("debugstats", kColorFrame);
  7539. m_needPresent = true;
  7540. TextVideoMem& tvm = m_textVideoMem;
  7541. static int64_t next = timeEnd;
  7542. if (timeEnd >= next)
  7543. {
  7544. next = timeEnd + timerFreq;
  7545. double freq = double(timerFreq);
  7546. double toMs = 1000.0/freq;
  7547. tvm.clear();
  7548. uint16_t pos = 0;
  7549. tvm.printf(0, pos++, BGFX_CONFIG_DEBUG ? 0x8c : 0x8f
  7550. , " %s / " BX_COMPILER_NAME
  7551. " / " BX_CPU_NAME
  7552. " / " BX_ARCH_NAME
  7553. " / " BX_PLATFORM_NAME
  7554. " / Version 1.%d.%d (commit: " BGFX_REV_SHA1 ")"
  7555. , getRendererName()
  7556. , BGFX_API_VERSION
  7557. , BGFX_REV_NUMBER
  7558. );
  7559. tvm.printf(0, pos++, 0x8f, " Vendor: %s ", m_vendor);
  7560. tvm.printf(0, pos++, 0x8f, " Renderer: %s ", m_renderer);
  7561. tvm.printf(0, pos++, 0x8f, " Version: %s ", m_version);
  7562. tvm.printf(0, pos++, 0x8f, " GLSL version: %s ", m_glslVersion);
  7563. char processMemoryUsed[16];
  7564. bx::prettify(processMemoryUsed, BX_COUNTOF(processMemoryUsed), bx::getProcessMemoryUsed() );
  7565. tvm.printf(0, pos++, 0x8f, " Memory: %s (process) ", processMemoryUsed);
  7566. pos = 10;
  7567. tvm.printf(10, pos++, 0x8b, " Frame: %7.3f, % 7.3f \x1f, % 7.3f \x1e [ms] / % 6.2f FPS "
  7568. , double(frameTime)*toMs
  7569. , double(min)*toMs
  7570. , double(max)*toMs
  7571. , freq/frameTime
  7572. );
  7573. const uint32_t msaa = (m_resolution.reset&BGFX_RESET_MSAA_MASK)>>BGFX_RESET_MSAA_SHIFT;
  7574. tvm.printf(10, pos++, 0x8b, " Reset flags: [%c] vsync, [%c] MSAAx%d, [%c] MaxAnisotropy "
  7575. , !!(m_resolution.reset&BGFX_RESET_VSYNC) ? '\xfe' : ' '
  7576. , 0 != msaa ? '\xfe' : ' '
  7577. , 1<<msaa
  7578. , !!(m_resolution.reset&BGFX_RESET_MAXANISOTROPY) ? '\xfe' : ' '
  7579. );
  7580. double elapsedCpuMs = double(frameTime)*toMs;
  7581. tvm.printf(10, pos++, 0x8b, " Submitted: %5d (draw %5d, compute %4d) / CPU %7.4f [ms] %c GPU %7.4f [ms] (latency %d) "
  7582. , _render->m_numRenderItems
  7583. , statsKeyType[0]
  7584. , statsKeyType[1]
  7585. , elapsedCpuMs
  7586. , elapsedCpuMs > elapsedGpuMs ? '>' : '<'
  7587. , maxGpuElapsed
  7588. , maxGpuLatency
  7589. );
  7590. maxGpuLatency = 0;
  7591. maxGpuElapsed = 0.0;
  7592. for (uint32_t ii = 0; ii < BX_COUNTOF(s_primInfo); ++ii)
  7593. {
  7594. tvm.printf(10, pos++, 0x8b, " %10s: %7d (#inst: %5d), submitted: %7d "
  7595. , getName(Topology::Enum(ii) )
  7596. , statsNumPrimsRendered[ii]
  7597. , statsNumInstances[ii]
  7598. , statsNumPrimsSubmitted[ii]
  7599. );
  7600. }
  7601. if (NULL != m_renderdocdll)
  7602. {
  7603. tvm.printf(tvm.m_width-27, 0, 0x4f, " [F11 - RenderDoc capture] ");
  7604. }
  7605. tvm.printf(10, pos++, 0x8b, " Indices: %7d ", statsNumIndices);
  7606. tvm.printf(10, pos++, 0x8b, " DVB size: %7d ", _render->m_vboffset);
  7607. tvm.printf(10, pos++, 0x8b, " DIB size: %7d ", _render->m_iboffset);
  7608. pos++;
  7609. tvm.printf(10, pos++, 0x8b, " State cache: ");
  7610. tvm.printf(10, pos++, 0x8b, " Sampler ");
  7611. tvm.printf(10, pos++, 0x8b, " %6d "
  7612. , m_samplerStateCache.getCount()
  7613. );
  7614. #if BGFX_CONFIG_RENDERER_OPENGL
  7615. if (s_extension[Extension::ATI_meminfo].m_supported)
  7616. {
  7617. GLint vboFree[4];
  7618. GL_CHECK(glGetIntegerv(GL_VBO_FREE_MEMORY_ATI, vboFree) );
  7619. GLint texFree[4];
  7620. GL_CHECK(glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, texFree) );
  7621. GLint rbfFree[4];
  7622. GL_CHECK(glGetIntegerv(GL_RENDERBUFFER_FREE_MEMORY_ATI, rbfFree) );
  7623. pos++;
  7624. tvm.printf(10, pos++, 0x8c, " -------------| free| free b| aux| aux fb ");
  7625. char tmp0[16];
  7626. char tmp1[16];
  7627. char tmp2[16];
  7628. char tmp3[16];
  7629. bx::prettify(tmp0, BX_COUNTOF(tmp0), vboFree[0]);
  7630. bx::prettify(tmp1, BX_COUNTOF(tmp1), vboFree[1]);
  7631. bx::prettify(tmp2, BX_COUNTOF(tmp2), vboFree[2]);
  7632. bx::prettify(tmp3, BX_COUNTOF(tmp3), vboFree[3]);
  7633. tvm.printf(10, pos++, 0x8b, " VBO: %10s, %10s, %10s, %10s ", tmp0, tmp1, tmp2, tmp3);
  7634. bx::prettify(tmp0, BX_COUNTOF(tmp0), texFree[0]);
  7635. bx::prettify(tmp1, BX_COUNTOF(tmp1), texFree[1]);
  7636. bx::prettify(tmp2, BX_COUNTOF(tmp2), texFree[2]);
  7637. bx::prettify(tmp3, BX_COUNTOF(tmp3), texFree[3]);
  7638. tvm.printf(10, pos++, 0x8b, " Texture: %10s, %10s, %10s, %10s ", tmp0, tmp1, tmp2, tmp3);
  7639. bx::prettify(tmp0, BX_COUNTOF(tmp0), rbfFree[0]);
  7640. bx::prettify(tmp1, BX_COUNTOF(tmp1), rbfFree[1]);
  7641. bx::prettify(tmp2, BX_COUNTOF(tmp2), rbfFree[2]);
  7642. bx::prettify(tmp3, BX_COUNTOF(tmp3), rbfFree[3]);
  7643. tvm.printf(10, pos++, 0x8b, " Render Buffer: %10s, %10s, %10s, %10s ", tmp0, tmp1, tmp2, tmp3);
  7644. }
  7645. else if (s_extension[Extension::NVX_gpu_memory_info].m_supported)
  7646. {
  7647. GLint dedicated;
  7648. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &dedicated) );
  7649. GLint totalAvail;
  7650. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &totalAvail) );
  7651. GLint currAvail;
  7652. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &currAvail) );
  7653. GLint evictedCount;
  7654. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX, &evictedCount) );
  7655. GLint evictedMemory;
  7656. GL_CHECK(glGetIntegerv(GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX, &evictedMemory) );
  7657. pos++;
  7658. char tmp0[16];
  7659. char tmp1[16];
  7660. bx::prettify(tmp0, BX_COUNTOF(tmp0), dedicated);
  7661. tvm.printf(10, pos++, 0x8b, " Dedicated: %10s ", tmp0);
  7662. bx::prettify(tmp0, BX_COUNTOF(tmp0), currAvail);
  7663. bx::prettify(tmp1, BX_COUNTOF(tmp1), totalAvail);
  7664. tvm.printf(10, pos++, 0x8b, " Available: %10s / %10s ", tmp0, tmp1);
  7665. bx::prettify(tmp0, BX_COUNTOF(tmp0), evictedCount);
  7666. bx::prettify(tmp1, BX_COUNTOF(tmp1), evictedMemory);
  7667. tvm.printf(10, pos++, 0x8b, " Eviction: %10s / %10s ", tmp0, tmp1);
  7668. }
  7669. #endif // BGFX_CONFIG_RENDERER_OPENGL
  7670. pos++;
  7671. double captureMs = double(captureElapsed)*toMs;
  7672. tvm.printf(10, pos++, 0x8b, " Capture: %7.4f [ms] ", captureMs);
  7673. uint8_t attr[2] = { 0x8c, 0x8a };
  7674. uint8_t attrIndex = _render->m_waitSubmit < _render->m_waitRender;
  7675. tvm.printf(10, pos++, attr[attrIndex&1], " Submit wait: %7.4f [ms] ", double(_render->m_waitSubmit)*toMs);
  7676. tvm.printf(10, pos++, attr[(attrIndex+1)&1], " Render wait: %7.4f [ms] ", double(_render->m_waitRender)*toMs);
  7677. min = frameTime;
  7678. max = frameTime;
  7679. }
  7680. blit(this, _textVideoMemBlitter, tvm);
  7681. BGFX_GL_PROFILER_END();
  7682. }
  7683. else if (_render->m_debug & BGFX_DEBUG_TEXT)
  7684. {
  7685. BGFX_GL_PROFILER_BEGIN_LITERAL("debugtext", kColorFrame);
  7686. blit(this, _textVideoMemBlitter, _render->m_textVideoMem);
  7687. BGFX_GL_PROFILER_END();
  7688. }
  7689. if (0 != m_vao)
  7690. {
  7691. GL_CHECK(glBindVertexArray(0) );
  7692. }
  7693. }
  7694. } } // namespace bgfx
  7695. #else
  7696. namespace bgfx { namespace gl
  7697. {
  7698. RendererContextI* rendererCreate(const Init& _init)
  7699. {
  7700. BX_UNUSED(_init);
  7701. return NULL;
  7702. }
  7703. void rendererDestroy()
  7704. {
  7705. }
  7706. } /* namespace gl */ } // namespace bgfx
  7707. #endif // (BGFX_CONFIG_RENDERER_OPENGLES || BGFX_CONFIG_RENDERER_OPENGL)