renderer_gl.cpp 254 KB

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