renderer_gl.cpp 219 KB

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