renderer_gl.cpp 224 KB

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