renderer_gl.cpp 158 KB

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