renderer_gl.cpp 255 KB

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