renderer_gl.cpp 234 KB

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