renderer_gl.cpp 191 KB

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