renderer_gl.cpp 219 KB

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