renderer_gl.cpp 229 KB

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