renderer_gl.cpp 270 KB

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