renderer_gl.cpp 206 KB

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