renderer_gl.cpp 210 KB

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