glGraphicsStateGuardian_src.cxx 427 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617
  1. // Filename: glGraphicsStateGuardian_src.cxx
  2. // Created by: drose (02Feb99)
  3. // Updated by: fperazzi, PandaSE (05May10) (added
  4. // get_supports_cg_profile)
  5. //
  6. ////////////////////////////////////////////////////////////////////
  7. //
  8. // PANDA 3D SOFTWARE
  9. // Copyright (c) Carnegie Mellon University. All rights reserved.
  10. //
  11. // All use of this software is subject to the terms of the revised BSD
  12. // license. You should have received a copy of this license along
  13. // with this source code in a file named "LICENSE."
  14. //
  15. ////////////////////////////////////////////////////////////////////
  16. #include "config_util.h"
  17. #include "displayRegion.h"
  18. #include "renderBuffer.h"
  19. #include "geom.h"
  20. #include "geomVertexData.h"
  21. #include "geomTriangles.h"
  22. #include "geomTristrips.h"
  23. #include "geomTrifans.h"
  24. #include "geomLines.h"
  25. #include "geomLinestrips.h"
  26. #include "geomPoints.h"
  27. #include "geomVertexReader.h"
  28. #include "graphicsWindow.h"
  29. #include "lens.h"
  30. #include "perspectiveLens.h"
  31. #include "directionalLight.h"
  32. #include "pointLight.h"
  33. #include "spotlight.h"
  34. #include "planeNode.h"
  35. #include "fog.h"
  36. #include "clockObject.h"
  37. #include "string_utils.h"
  38. #include "nodePath.h"
  39. #include "dcast.h"
  40. #include "pvector.h"
  41. #include "vector_string.h"
  42. #include "string_utils.h"
  43. #include "pnmImage.h"
  44. #include "config_gobj.h"
  45. #include "lightMutexHolder.h"
  46. #include "indirectLess.h"
  47. #include "pStatTimer.h"
  48. #include "load_prc_file.h"
  49. #include "bamCache.h"
  50. #include "bamCacheRecord.h"
  51. #include "alphaTestAttrib.h"
  52. #include "clipPlaneAttrib.h"
  53. #include "cullFaceAttrib.h"
  54. #include "depthOffsetAttrib.h"
  55. #include "depthWriteAttrib.h"
  56. #include "fogAttrib.h"
  57. #include "lightAttrib.h"
  58. #include "materialAttrib.h"
  59. #include "rescaleNormalAttrib.h"
  60. #include "scissorAttrib.h"
  61. #include "shadeModelAttrib.h"
  62. #include "stencilAttrib.h"
  63. #include "graphicsEngine.h"
  64. #include "shaderGenerator.h"
  65. #include "samplerState.h"
  66. #include "displayInformation.h"
  67. #if defined(HAVE_CG) && !defined(OPENGLES)
  68. #include "Cg/cgGL.h"
  69. #endif
  70. #include <algorithm>
  71. TypeHandle CLP(GraphicsStateGuardian)::_type_handle;
  72. PStatCollector CLP(GraphicsStateGuardian)::_load_display_list_pcollector("Draw:Transfer data:Display lists");
  73. PStatCollector CLP(GraphicsStateGuardian)::_primitive_batches_display_list_pcollector("Primitive batches:Display lists");
  74. PStatCollector CLP(GraphicsStateGuardian)::_vertices_display_list_pcollector("Vertices:Display lists");
  75. PStatCollector CLP(GraphicsStateGuardian)::_vertices_immediate_pcollector("Vertices:Immediate mode");
  76. PStatCollector CLP(GraphicsStateGuardian)::_memory_barrier_pcollector("Draw:Memory barriers");
  77. PStatCollector CLP(GraphicsStateGuardian)::_vertex_array_update_pcollector("Draw:Update arrays");
  78. PStatCollector CLP(GraphicsStateGuardian)::_texture_update_pcollector("Draw:Update texture");
  79. PStatCollector CLP(GraphicsStateGuardian)::_fbo_bind_pcollector("Draw:Bind FBO");
  80. PStatCollector CLP(GraphicsStateGuardian)::_check_error_pcollector("Draw:Check errors");
  81. #ifndef OPENGLES_1
  82. PT(Shader) CLP(GraphicsStateGuardian)::_default_shader = NULL;
  83. #endif
  84. // The following noop functions are assigned to the corresponding
  85. // glext function pointers in the class, in case the functions are not
  86. // defined by the GL, just so it will always be safe to call the
  87. // extension functions.
  88. static void APIENTRY
  89. null_glPointParameterfv(GLenum, const GLfloat *) {
  90. }
  91. #ifdef OPENGLES
  92. // OpenGL ES doesn't support this, period. Might as well macro it.
  93. #define _glDrawRangeElements(mode, start, end, count, type, indices) \
  94. glDrawElements(mode, count, type, indices)
  95. #else
  96. static void APIENTRY
  97. null_glDrawRangeElements(GLenum mode, GLuint start, GLuint end,
  98. GLsizei count, GLenum type, const GLvoid *indices) {
  99. // If we don't support glDrawRangeElements(), just use the original
  100. // glDrawElements() instead.
  101. glDrawElements(mode, count, type, indices);
  102. }
  103. #endif
  104. static void APIENTRY
  105. null_glActiveTexture(GLenum gl_texture_stage) {
  106. // If we don't support multitexture, we'd better not try to request
  107. // a texture beyond the first texture stage.
  108. nassertv(gl_texture_stage == GL_TEXTURE0);
  109. }
  110. static void APIENTRY
  111. null_glBlendEquation(GLenum) {
  112. }
  113. static void APIENTRY
  114. null_glBlendColor(GLclampf, GLclampf, GLclampf, GLclampf) {
  115. }
  116. #ifndef OPENGLES_1
  117. // We have a default shader that will be applied when there isn't any
  118. // shader applied (e.g. if it failed to compile). We need this because
  119. // OpenGL ES 2.x and OpenGL 3.2+ core don't have a fixed-function pipeline.
  120. // This default shader just applies a single texture, which is good enough
  121. // for drawing GUIs and such.
  122. static const string default_vshader =
  123. #ifndef OPENGLES
  124. "#version 130\n"
  125. "in vec4 p3d_Vertex;\n"
  126. "in vec4 p3d_Color;\n"
  127. "in vec2 p3d_MultiTexCoord0;\n"
  128. "out vec2 texcoord;\n"
  129. "out vec4 color;\n"
  130. #else
  131. "precision mediump float;\n"
  132. "attribute vec4 p3d_Vertex;\n"
  133. "attribute vec4 p3d_Color;\n"
  134. "attribute vec2 p3d_MultiTexCoord0;\n"
  135. "varying vec2 texcoord;\n"
  136. "varying vec4 color;\n"
  137. #endif
  138. "uniform mat4 p3d_ModelViewProjectionMatrix;\n"
  139. "uniform vec4 p3d_ColorScale;\n"
  140. "void main(void) {\n"
  141. " gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;\n"
  142. " texcoord = p3d_MultiTexCoord0;\n"
  143. " color = p3d_Color * p3d_ColorScale;\n"
  144. "}\n";
  145. static const string default_fshader =
  146. #ifndef OPENGLES
  147. "#version 130\n"
  148. "in vec2 texcoord;\n"
  149. "in vec4 color;\n"
  150. #else
  151. "precision mediump float;\n"
  152. "varying vec2 texcoord;\n"
  153. "varying vec4 color;\n"
  154. #endif
  155. "uniform sampler2D p3d_Texture0;\n"
  156. "void main(void) {\n"
  157. #ifndef OPENGLES
  158. " gl_FragColor = texture(p3d_Texture0, texcoord);\n"
  159. #else
  160. " gl_FragColor = texture2D(p3d_Texture0, texcoord).bgra;\n"
  161. #endif
  162. " gl_FragColor *= color;\n"
  163. "}\n";
  164. #endif
  165. ////////////////////////////////////////////////////////////////////
  166. // Function: uchar_bgr_to_rgb
  167. // Description: Recopies the given array of pixels, converting from
  168. // BGR to RGB arrangement.
  169. ////////////////////////////////////////////////////////////////////
  170. static void
  171. uchar_bgr_to_rgb(unsigned char *dest, const unsigned char *source,
  172. int num_pixels) {
  173. for (int i = 0; i < num_pixels; i++) {
  174. dest[0] = source[2];
  175. dest[1] = source[1];
  176. dest[2] = source[0];
  177. dest += 3;
  178. source += 3;
  179. }
  180. }
  181. ////////////////////////////////////////////////////////////////////
  182. // Function: uchar_bgra_to_rgba
  183. // Description: Recopies the given array of pixels, converting from
  184. // BGRA to RGBA arrangement.
  185. ////////////////////////////////////////////////////////////////////
  186. static void
  187. uchar_bgra_to_rgba(unsigned char *dest, const unsigned char *source,
  188. int num_pixels) {
  189. for (int i = 0; i < num_pixels; i++) {
  190. dest[0] = source[2];
  191. dest[1] = source[1];
  192. dest[2] = source[0];
  193. dest[3] = source[3];
  194. dest += 4;
  195. source += 4;
  196. }
  197. }
  198. ////////////////////////////////////////////////////////////////////
  199. // Function: ushort_bgr_to_rgb
  200. // Description: Recopies the given array of pixels, converting from
  201. // BGR to RGB arrangement.
  202. ////////////////////////////////////////////////////////////////////
  203. static void
  204. ushort_bgr_to_rgb(unsigned short *dest, const unsigned short *source,
  205. int num_pixels) {
  206. for (int i = 0; i < num_pixels; i++) {
  207. dest[0] = source[2];
  208. dest[1] = source[1];
  209. dest[2] = source[0];
  210. dest += 3;
  211. source += 3;
  212. }
  213. }
  214. ////////////////////////////////////////////////////////////////////
  215. // Function: ushort_bgra_to_rgba
  216. // Description: Recopies the given array of pixels, converting from
  217. // BGRA to RGBA arrangement.
  218. ////////////////////////////////////////////////////////////////////
  219. static void
  220. ushort_bgra_to_rgba(unsigned short *dest, const unsigned short *source,
  221. int num_pixels) {
  222. for (int i = 0; i < num_pixels; i++) {
  223. dest[0] = source[2];
  224. dest[1] = source[1];
  225. dest[2] = source[0];
  226. dest[3] = source[3];
  227. dest += 4;
  228. source += 4;
  229. }
  230. }
  231. ////////////////////////////////////////////////////////////////////
  232. // Function: fix_component_ordering
  233. // Description: Reverses the order of the components within the
  234. // image, to convert (for instance) GL_BGR to GL_RGB.
  235. // Returns the byte pointer representing the converted
  236. // image, or the original image if it is unchanged.
  237. //
  238. // new_image must be supplied; it is the PTA_uchar that
  239. // will be used to hold the converted image if required.
  240. // It will be modified only if the conversion is
  241. // necessary, in which case the data will be stored
  242. // there, and this pointer will be returned. If the
  243. // conversion is not necessary, this pointer will be
  244. // left unchanged.
  245. ////////////////////////////////////////////////////////////////////
  246. static const unsigned char *
  247. fix_component_ordering(PTA_uchar &new_image,
  248. const unsigned char *orig_image, size_t orig_image_size,
  249. GLenum external_format, Texture *tex) {
  250. const unsigned char *result = orig_image;
  251. switch (external_format) {
  252. case GL_RGB:
  253. switch (tex->get_component_type()) {
  254. case Texture::T_unsigned_byte:
  255. new_image = PTA_uchar::empty_array(orig_image_size);
  256. uchar_bgr_to_rgb(new_image, orig_image, orig_image_size / 3);
  257. result = new_image;
  258. break;
  259. case Texture::T_unsigned_short:
  260. new_image = PTA_uchar::empty_array(orig_image_size);
  261. ushort_bgr_to_rgb((unsigned short *)new_image.p(),
  262. (const unsigned short *)orig_image,
  263. orig_image_size / 6);
  264. result = new_image;
  265. break;
  266. default:
  267. break;
  268. }
  269. break;
  270. case GL_RGBA:
  271. switch (tex->get_component_type()) {
  272. case Texture::T_unsigned_byte:
  273. new_image = PTA_uchar::empty_array(orig_image_size);
  274. uchar_bgra_to_rgba(new_image, orig_image, orig_image_size / 4);
  275. result = new_image;
  276. break;
  277. case Texture::T_unsigned_short:
  278. new_image = PTA_uchar::empty_array(orig_image_size);
  279. ushort_bgra_to_rgba((unsigned short *)new_image.p(),
  280. (const unsigned short *)orig_image,
  281. orig_image_size / 8);
  282. result = new_image;
  283. break;
  284. default:
  285. break;
  286. }
  287. break;
  288. default:
  289. break;
  290. }
  291. return result;
  292. }
  293. //#--- Zhao Nov/2011
  294. string CLP(GraphicsStateGuardian)::get_driver_vendor() { return _gl_vendor; }
  295. string CLP(GraphicsStateGuardian)::get_driver_renderer() { return _gl_renderer; }
  296. string CLP(GraphicsStateGuardian)::get_driver_version() { return _gl_version; }
  297. int CLP(GraphicsStateGuardian)::get_driver_version_major() { return _gl_version_major; }
  298. int CLP(GraphicsStateGuardian)::get_driver_version_minor() { return _gl_version_minor; }
  299. int CLP(GraphicsStateGuardian)::get_driver_shader_version_major() { return _gl_shadlang_ver_major; }
  300. int CLP(GraphicsStateGuardian)::get_driver_shader_version_minor() { return _gl_shadlang_ver_minor; }
  301. ////////////////////////////////////////////////////////////////////
  302. // Function: GLGraphicsStateGuardian::Constructor
  303. // Access: Public
  304. // Description:
  305. ////////////////////////////////////////////////////////////////////
  306. CLP(GraphicsStateGuardian)::
  307. CLP(GraphicsStateGuardian)(GraphicsEngine *engine, GraphicsPipe *pipe) :
  308. GraphicsStateGuardian(gl_coordinate_system, engine, pipe),
  309. _renderbuffer_residency(get_prepared_objects()->get_name(), "renderbuffer")
  310. {
  311. _error_count = 0;
  312. _gl_shadlang_ver_major = 0;
  313. _gl_shadlang_ver_minor = 0;
  314. // Hack. Turn on the flag that we turned off at a higher level,
  315. // since we know this works properly in OpenGL, and we want the
  316. // performance benefit it gives us.
  317. _prepared_objects->_support_released_buffer_cache = true;
  318. // Assume that we will get a hardware-accelerated context, unless
  319. // the window tells us otherwise.
  320. _is_hardware = true;
  321. // calling glGetError() forces a sync, this turns it on if you want to.
  322. _check_errors = gl_check_errors;
  323. _force_flush = gl_force_flush;
  324. _scissor_enabled = false;
  325. _scissor_attrib_active = false;
  326. _white_texture = 0;
  327. #ifdef HAVE_CG
  328. _cg_context = 0;
  329. #endif
  330. #ifdef DO_PSTATS
  331. if (gl_finish) {
  332. GLCAT.warning()
  333. << "The config variable gl-finish is set to true. This may have a substantial negative impact on your render performance.\n";
  334. }
  335. #endif // DO_PSTATS
  336. }
  337. ////////////////////////////////////////////////////////////////////
  338. // Function: GLGraphicsStateGuardian::Destructor
  339. // Access: Public
  340. // Description:
  341. ////////////////////////////////////////////////////////////////////
  342. CLP(GraphicsStateGuardian)::
  343. ~CLP(GraphicsStateGuardian)() {
  344. if (GLCAT.is_debug()) {
  345. GLCAT.debug()
  346. << "GLGraphicsStateGuardian " << this << " destructing\n";
  347. }
  348. close_gsg();
  349. }
  350. ////////////////////////////////////////////////////////////////////
  351. // Function: GLGraphicsStateGuardian::debug_callback
  352. // Access: Public, Static
  353. // Description: This is called by the GL if an error occurs, if
  354. // gl_debug has been enabled (and the driver supports
  355. // the GL_ARB_debug_output extension).
  356. ////////////////////////////////////////////////////////////////////
  357. #ifndef OPENGLES_1
  358. void CLP(GraphicsStateGuardian)::
  359. debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, GLvoid *userParam) {
  360. // Determine how to map the severity level.
  361. NotifySeverity level;
  362. switch (severity) {
  363. case GL_DEBUG_SEVERITY_HIGH:
  364. level = NS_error;
  365. break;
  366. case GL_DEBUG_SEVERITY_MEDIUM:
  367. level = NS_warning;
  368. break;
  369. case GL_DEBUG_SEVERITY_LOW:
  370. level = NS_info;
  371. break;
  372. case GL_DEBUG_SEVERITY_NOTIFICATION:
  373. level = NS_debug;
  374. break;
  375. default:
  376. level = NS_fatal; //???
  377. break;
  378. }
  379. string msg_str(message, length);
  380. GLCAT.out(level) << msg_str << "\n";
  381. #ifndef NDEBUG
  382. if (level >= gl_debug_abort_level.get_value()) {
  383. abort();
  384. }
  385. #endif
  386. }
  387. #endif // OPENGLES_1
  388. ////////////////////////////////////////////////////////////////////
  389. // Function: GLGraphicsStateGuardian::reset
  390. // Access: Public, Virtual
  391. // Description: Resets all internal state as if the gsg were newly
  392. // created.
  393. ////////////////////////////////////////////////////////////////////
  394. void CLP(GraphicsStateGuardian)::
  395. reset() {
  396. free_pointers();
  397. GraphicsStateGuardian::reset();
  398. // Build _inv_state_mask as a mask of 1's where we don't care, and
  399. // 0's where we do care, about the state.
  400. //_inv_state_mask = RenderState::SlotMask::all_on();
  401. _inv_state_mask.clear_bit(ShaderAttrib::get_class_slot());
  402. _inv_state_mask.clear_bit(AlphaTestAttrib::get_class_slot());
  403. _inv_state_mask.clear_bit(AntialiasAttrib::get_class_slot());
  404. _inv_state_mask.clear_bit(ClipPlaneAttrib::get_class_slot());
  405. _inv_state_mask.clear_bit(ColorAttrib::get_class_slot());
  406. _inv_state_mask.clear_bit(ColorScaleAttrib::get_class_slot());
  407. _inv_state_mask.clear_bit(CullFaceAttrib::get_class_slot());
  408. _inv_state_mask.clear_bit(DepthOffsetAttrib::get_class_slot());
  409. _inv_state_mask.clear_bit(DepthTestAttrib::get_class_slot());
  410. _inv_state_mask.clear_bit(DepthWriteAttrib::get_class_slot());
  411. _inv_state_mask.clear_bit(RenderModeAttrib::get_class_slot());
  412. _inv_state_mask.clear_bit(RescaleNormalAttrib::get_class_slot());
  413. _inv_state_mask.clear_bit(ShadeModelAttrib::get_class_slot());
  414. _inv_state_mask.clear_bit(TransparencyAttrib::get_class_slot());
  415. _inv_state_mask.clear_bit(ColorWriteAttrib::get_class_slot());
  416. _inv_state_mask.clear_bit(ColorBlendAttrib::get_class_slot());
  417. _inv_state_mask.clear_bit(TextureAttrib::get_class_slot());
  418. _inv_state_mask.clear_bit(TexGenAttrib::get_class_slot());
  419. _inv_state_mask.clear_bit(TexMatrixAttrib::get_class_slot());
  420. _inv_state_mask.clear_bit(MaterialAttrib::get_class_slot());
  421. _inv_state_mask.clear_bit(LightAttrib::get_class_slot());
  422. _inv_state_mask.clear_bit(StencilAttrib::get_class_slot());
  423. _inv_state_mask.clear_bit(FogAttrib::get_class_slot());
  424. _inv_state_mask.clear_bit(ScissorAttrib::get_class_slot());
  425. // Output the vendor and version strings.
  426. query_gl_version();
  427. if (_gl_version_major == 0) {
  428. // Couldn't get GL. Fail.
  429. mark_new();
  430. return;
  431. }
  432. // Save the extensions tokens.
  433. _extensions.clear();
  434. // In OpenGL 3.0 and later, glGetString(GL_EXTENSIONS) is deprecated.
  435. #ifndef OPENGLES
  436. if (is_at_least_gl_version(3, 0)) {
  437. PFNGLGETSTRINGIPROC _glGetStringi =
  438. (PFNGLGETSTRINGIPROC)get_extension_func("glGetStringi");
  439. if (_glGetStringi != NULL) {
  440. GLint n = 0;
  441. glGetIntegerv(GL_NUM_EXTENSIONS, &n);
  442. for (GLint i = 0; i < n; ++i) {
  443. const char *extension = (const char *)_glGetStringi(GL_EXTENSIONS, i);
  444. _extensions.insert(string(extension));
  445. }
  446. } else {
  447. GLCAT.error() << "glGetStringi is not available!\n";
  448. save_extensions((const char *)glGetString(GL_EXTENSIONS));
  449. }
  450. } else
  451. #endif
  452. {
  453. save_extensions((const char *)glGetString(GL_EXTENSIONS));
  454. }
  455. get_extra_extensions();
  456. // This needs access to the extensions, so put this after save_extensions.
  457. query_glsl_version();
  458. #ifndef OPENGLES
  459. bool core_profile;
  460. if (_gl_version_major < 3 || has_extension("GL_ARB_compatibility")) {
  461. core_profile = false;
  462. } else {
  463. core_profile = true;
  464. }
  465. if (core_profile) {
  466. GLCAT.debug() << "Using core profile\n";
  467. } else {
  468. GLCAT.debug() << "Using compatibility profile\n";
  469. }
  470. #elif defined(OPENGLES_1)
  471. static const bool core_profile = false;
  472. #else
  473. static const bool core_profile = true;
  474. #endif
  475. // Print out a list of all extensions.
  476. report_extensions();
  477. // Initialize OpenGL debugging output first, if enabled and supported.
  478. _supports_debug = false;
  479. _use_object_labels = false;
  480. #ifndef OPENGLES_1
  481. if (gl_debug) {
  482. PFNGLDEBUGMESSAGECALLBACKPROC _glDebugMessageCallback;
  483. PFNGLDEBUGMESSAGECONTROLPROC _glDebugMessageControl;
  484. if (is_at_least_gl_version(4, 3) || has_extension("GL_KHR_debug")) {
  485. #ifdef OPENGLES
  486. _glDebugMessageCallback = (PFNGLDEBUGMESSAGECALLBACKPROC)
  487. get_extension_func("glDebugMessageCallbackKHR");
  488. _glDebugMessageControl = (PFNGLDEBUGMESSAGECONTROLPROC)
  489. get_extension_func("glDebugMessageControlKHR");
  490. _glObjectLabel = (PFNGLOBJECTLABELPROC)
  491. get_extension_func("glObjectLabelKHR");
  492. #else
  493. _glDebugMessageCallback = (PFNGLDEBUGMESSAGECALLBACKPROC)
  494. get_extension_func("glDebugMessageCallback");
  495. _glDebugMessageControl = (PFNGLDEBUGMESSAGECONTROLPROC)
  496. get_extension_func("glDebugMessageControl");
  497. _glObjectLabel = (PFNGLOBJECTLABELPROC)
  498. get_extension_func("glObjectLabel");
  499. #endif
  500. glEnable(GL_DEBUG_OUTPUT); // Not supported in ARB version
  501. _supports_debug = true;
  502. _use_object_labels = gl_debug_object_labels;
  503. #ifndef OPENGLES
  504. } else if (has_extension("GL_ARB_debug_output")) {
  505. _glDebugMessageCallback = (PFNGLDEBUGMESSAGECALLBACKPROC)
  506. get_extension_func("glDebugMessageCallbackARB");
  507. _glDebugMessageControl = (PFNGLDEBUGMESSAGECONTROLPROC)
  508. get_extension_func("glDebugMessageControlARB");
  509. _supports_debug = true;
  510. #endif
  511. }
  512. if (_supports_debug) {
  513. // Set the categories we want to listen to.
  514. _glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH,
  515. 0, NULL, GLCAT.is_error());
  516. _glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM,
  517. 0, NULL, GLCAT.is_warning());
  518. _glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW,
  519. 0, NULL, GLCAT.is_info());
  520. _glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION,
  521. 0, NULL, GLCAT.is_debug());
  522. // Enable the callback.
  523. _glDebugMessageCallback((GLDEBUGPROC) &debug_callback, (void*)this);
  524. if (gl_debug_synchronous) {
  525. glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
  526. }
  527. GLCAT.debug() << "gl-debug enabled.\n";
  528. } else {
  529. GLCAT.debug() << "gl-debug enabled, but NOT supported.\n";
  530. }
  531. } else {
  532. GLCAT.debug() << "gl-debug NOT enabled.\n";
  533. // However, still check if it is supported.
  534. _supports_debug = is_at_least_gl_version(4, 3)
  535. || has_extension("GL_KHR_debug")
  536. || has_extension("GL_ARB_debug_output");
  537. }
  538. #endif // OPENGLES_1
  539. _supported_geom_rendering =
  540. Geom::GR_indexed_point |
  541. Geom::GR_point | Geom::GR_point_uniform_size |
  542. Geom::GR_indexed_other |
  543. Geom::GR_triangle_strip | Geom::GR_triangle_fan |
  544. Geom::GR_line_strip |
  545. Geom::GR_flat_last_vertex;
  546. _supports_point_parameters = false;
  547. #ifndef OPENGLES_2
  548. if (is_at_least_gl_version(1, 4)) {
  549. _supports_point_parameters = true;
  550. _glPointParameterfv = (PFNGLPOINTPARAMETERFVPROC)
  551. get_extension_func("glPointParameterfv");
  552. } else if (has_extension("GL_ARB_point_parameters")) {
  553. _supports_point_parameters = true;
  554. _glPointParameterfv = (PFNGLPOINTPARAMETERFVPROC)
  555. get_extension_func("glPointParameterfvARB");
  556. }
  557. if (_supports_point_parameters) {
  558. if (_glPointParameterfv == NULL) {
  559. GLCAT.warning()
  560. << "glPointParameterfv advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  561. _supports_point_parameters = false;
  562. }
  563. }
  564. if (_supports_point_parameters) {
  565. _supported_geom_rendering |= Geom::GR_point_perspective | Geom::GR_point_scale;
  566. } else {
  567. _glPointParameterfv = null_glPointParameterfv;
  568. }
  569. #endif // !OPENGLES_2
  570. #if defined(OPENGLES_2)
  571. // OpenGL ES 2 doesn't have point sprites.
  572. _supports_point_sprite = false;
  573. #elif defined(OPENGLES_1)
  574. _supports_point_sprite = has_extension("GL_OES_point_sprite");
  575. #else
  576. _supports_point_sprite = has_extension("GL_ARB_point_sprite");
  577. #endif
  578. if (_supports_point_sprite) {
  579. // It appears that the point_sprite extension doesn't support
  580. // texture transforms on the generated texture coordinates. How
  581. // inconsistent. Because of this, we don't advertise
  582. // GR_point_sprite_tex_matrix.
  583. _supported_geom_rendering |= Geom::GR_point_sprite;
  584. }
  585. #ifndef OPENGLES
  586. _explicit_primitive_restart = false;
  587. _glPrimitiveRestartIndex = NULL;
  588. if (gl_support_primitive_restart_index) {
  589. if ((is_at_least_gl_version(4, 3) || has_extension("GL_ARB_ES3_compatibility")) &&
  590. _gl_renderer.substr(0, 7) != "Gallium") {
  591. // As long as we enable this, OpenGL will always use the highest possible index
  592. // for a numeric type as strip cut index, which coincides with our convention.
  593. // This saves us a call to glPrimitiveRestartIndex
  594. // ... of course, though, the Gallium driver bugs out here. See also:
  595. // https://www.panda3d.org/forums/viewtopic.php?f=5&t=17512
  596. glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
  597. _supported_geom_rendering |= Geom::GR_strip_cut_index;
  598. } else if (is_at_least_gl_version(3, 1)) {
  599. // We have to use an explicit primitive restart enable/index.
  600. _explicit_primitive_restart = true;
  601. _supported_geom_rendering |= Geom::GR_strip_cut_index;
  602. _glPrimitiveRestartIndex = (PFNGLPRIMITIVERESTARTINDEXPROC)
  603. get_extension_func("glPrimitiveRestartIndex");
  604. }
  605. }
  606. #endif
  607. #if !defined(OPENGLES) && defined(SUPPORT_FIXED_FUNCTION)
  608. if (is_at_least_gl_version(1, 4)) {
  609. _glSecondaryColorPointer = (PFNGLSECONDARYCOLORPOINTERPROC)
  610. get_extension_func("glSecondaryColorPointer");
  611. } else if (has_extension("GL_EXT_secondary_color")) {
  612. _glSecondaryColorPointer = (PFNGLSECONDARYCOLORPOINTERPROC)
  613. get_extension_func("glSecondaryColorPointerEXT");
  614. }
  615. #endif
  616. #ifndef OPENGLES
  617. _glDrawRangeElements = null_glDrawRangeElements;
  618. if (is_at_least_gl_version(1, 2)) {
  619. _glDrawRangeElements = (PFNGLDRAWRANGEELEMENTSPROC)
  620. get_extension_func("glDrawRangeElements");
  621. } else if (has_extension("GL_EXT_draw_range_elements")) {
  622. _glDrawRangeElements = (PFNGLDRAWRANGEELEMENTSPROC)
  623. get_extension_func("glDrawRangeElementsEXT");
  624. }
  625. if (_glDrawRangeElements == NULL) {
  626. GLCAT.warning()
  627. << "glDrawRangeElements advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  628. _glDrawRangeElements = null_glDrawRangeElements;
  629. }
  630. #endif
  631. _supports_3d_texture = false;
  632. #ifndef OPENGLES_1
  633. if (is_at_least_gl_version(1, 2)) {
  634. _supports_3d_texture = true;
  635. _glTexImage3D = (PFNGLTEXIMAGE3DPROC_P)
  636. get_extension_func("glTexImage3D");
  637. _glTexSubImage3D = (PFNGLTEXSUBIMAGE3DPROC)
  638. get_extension_func("glTexSubImage3D");
  639. _glCopyTexSubImage3D = (PFNGLCOPYTEXSUBIMAGE3DPROC)
  640. get_extension_func("glCopyTexSubImage3D");
  641. } else if (has_extension("GL_EXT_texture3D")) {
  642. _supports_3d_texture = true;
  643. _glTexImage3D = (PFNGLTEXIMAGE3DPROC_P)
  644. get_extension_func("glTexImage3DEXT");
  645. _glTexSubImage3D = (PFNGLTEXSUBIMAGE3DPROC)
  646. get_extension_func("glTexSubImage3DEXT");
  647. _glCopyTexSubImage3D = NULL;
  648. if (has_extension("GL_EXT_copy_texture")) {
  649. _glCopyTexSubImage3D = (PFNGLCOPYTEXSUBIMAGE3DPROC)
  650. get_extension_func("glCopyTexSubImage3DEXT");
  651. }
  652. } else if (has_extension("GL_OES_texture_3D")) {
  653. _supports_3d_texture = true;
  654. _glTexImage3D = (PFNGLTEXIMAGE3DPROC_P)
  655. get_extension_func("glTexImage3DOES");
  656. _glTexSubImage3D = (PFNGLTEXSUBIMAGE3DPROC)
  657. get_extension_func("glTexSubImage3DOES");
  658. _glCopyTexSubImage3D = (PFNGLCOPYTEXSUBIMAGE3DPROC)
  659. get_extension_func("glCopyTexSubImage3DOES");
  660. #ifdef OPENGLES_2
  661. _glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DOES)
  662. get_extension_func("glFramebufferTexture3DOES");
  663. #endif
  664. }
  665. if (_supports_3d_texture) {
  666. if (_glTexImage3D == NULL || _glTexSubImage3D == NULL) {
  667. GLCAT.warning()
  668. << "3-D textures advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  669. _supports_3d_texture = false;
  670. }
  671. }
  672. #endif // !OPENGLES_1
  673. _supports_tex_storage = false;
  674. #ifndef OPENGLES
  675. if (is_at_least_gl_version(4, 2) || has_extension("GL_ARB_texture_storage")) {
  676. _supports_tex_storage = true;
  677. _glTexStorage1D = (PFNGLTEXSTORAGE1DPROC)
  678. get_extension_func("glTexStorage1D");
  679. _glTexStorage2D = (PFNGLTEXSTORAGE2DPROC)
  680. get_extension_func("glTexStorage2D");
  681. _glTexStorage3D = (PFNGLTEXSTORAGE3DPROC)
  682. get_extension_func("glTexStorage3D");
  683. } else
  684. #endif
  685. if (has_extension("GL_EXT_texture_storage")) { // GLES case
  686. _supports_tex_storage = true;
  687. _glTexStorage1D = (PFNGLTEXSTORAGE1DPROC)
  688. get_extension_func("glTexStorage1DEXT");
  689. _glTexStorage2D = (PFNGLTEXSTORAGE2DPROC)
  690. get_extension_func("glTexStorage2DEXT");
  691. _glTexStorage3D = (PFNGLTEXSTORAGE3DPROC)
  692. get_extension_func("glTexStorage3DEXT");
  693. }
  694. if (_supports_tex_storage) {
  695. if (_glTexStorage1D == NULL || _glTexStorage2D == NULL || _glTexStorage3D == NULL) {
  696. GLCAT.warning()
  697. << "Immutable texture storage advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  698. _supports_tex_storage = false;
  699. }
  700. }
  701. _supports_clear_texture = false;
  702. #ifndef OPENGLES
  703. if (is_at_least_gl_version(4, 4) || has_extension("GL_ARB_clear_texture")) {
  704. _glClearTexImage = (PFNGLCLEARTEXIMAGEPROC)
  705. get_extension_func("glClearTexImage");
  706. if (_glClearTexImage == NULL) {
  707. GLCAT.warning()
  708. << "GL_ARB_clear_texture advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  709. } else {
  710. _supports_clear_texture = true;
  711. }
  712. }
  713. #endif
  714. _supports_2d_texture_array = false;
  715. #ifndef OPENGLES
  716. if (is_at_least_gl_version(3, 0)) {
  717. _supports_2d_texture_array = true;
  718. _glFramebufferTextureLayer = (PFNGLFRAMEBUFFERTEXTURELAYERPROC)
  719. get_extension_func("glFramebufferTextureLayer");
  720. } else if (has_extension("GL_EXT_texture_array")) {
  721. _supports_2d_texture_array = true;
  722. _glFramebufferTextureLayer = (PFNGLFRAMEBUFFERTEXTURELAYERPROC)
  723. get_extension_func("glFramebufferTextureLayerEXT");
  724. }
  725. if (_supports_2d_texture_array && _glFramebufferTextureLayer == NULL) {
  726. GLCAT.warning()
  727. << "Texture arrays advertised as supported by OpenGL runtime, but could not get pointer to glFramebufferTextureLayer function.\n";
  728. }
  729. #endif
  730. #ifdef OPENGLES_2
  731. _supports_cube_map = true;
  732. #else
  733. _supports_cube_map =
  734. has_extension("GL_ARB_texture_cube_map") || is_at_least_gl_version(1, 3) ||
  735. has_extension("GL_OES_texture_cube_map");
  736. #endif
  737. #ifndef OPENGLES
  738. if (_supports_cube_map && gl_cube_map_seamless) {
  739. if (is_at_least_gl_version(3, 2) || has_extension("GL_ARB_seamless_cube_map")) {
  740. glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
  741. }
  742. }
  743. #endif
  744. #ifndef OPENGLES
  745. if (is_at_least_gl_version(3, 0)) {
  746. _glTexBuffer = (PFNGLTEXBUFFERPROC)get_extension_func("glTexBuffer");
  747. _supports_buffer_texture = true;
  748. } else if (has_extension("GL_ARB_texture_buffer_object")) {
  749. _glTexBuffer = (PFNGLTEXBUFFERPROC)get_extension_func("glTexBufferARB");
  750. _supports_buffer_texture = true;
  751. }
  752. #endif
  753. _supports_texture_srgb = false;
  754. if (is_at_least_gl_version(2, 1) || has_extension("GL_EXT_texture_sRGB")) {
  755. _supports_texture_srgb = true;
  756. } else if (has_extension("GL_EXT_sRGB")) { // GLES case.
  757. _supports_texture_srgb = true;
  758. }
  759. _supports_compressed_texture = false;
  760. #ifdef OPENGLES
  761. _supports_compressed_texture = true;
  762. // Supported in the core. 1D textures are not supported by OpenGL ES.
  763. _glCompressedTexImage1D = NULL;
  764. _glCompressedTexImage2D = glCompressedTexImage2D;
  765. _glCompressedTexSubImage1D = NULL;
  766. _glCompressedTexSubImage2D = glCompressedTexSubImage2D;
  767. _glGetCompressedTexImage = NULL;
  768. _glCompressedTexImage3D = NULL;
  769. _glCompressedTexSubImage3D = NULL;
  770. #ifdef OPENGLES_2
  771. if (_supports_3d_texture) {
  772. _glCompressedTexImage3D = (PFNGLCOMPRESSEDTEXIMAGE3DPROC)
  773. get_extension_func("glCompressedTexImage3DOES");
  774. _glCompressedTexSubImage3D = (PFNGLCOMPRESSEDTEXSUBIMAGE3DPROC)
  775. get_extension_func("glCompressedTexSubImageOES");
  776. }
  777. #endif
  778. #else
  779. if (is_at_least_gl_version(1, 3)) {
  780. _supports_compressed_texture = true;
  781. _glCompressedTexImage1D = (PFNGLCOMPRESSEDTEXIMAGE1DPROC)
  782. get_extension_func("glCompressedTexImage1D");
  783. _glCompressedTexImage2D = (PFNGLCOMPRESSEDTEXIMAGE2DPROC)
  784. get_extension_func("glCompressedTexImage2D");
  785. _glCompressedTexImage3D = (PFNGLCOMPRESSEDTEXIMAGE3DPROC)
  786. get_extension_func("glCompressedTexImage3D");
  787. _glCompressedTexSubImage1D = (PFNGLCOMPRESSEDTEXSUBIMAGE1DPROC)
  788. get_extension_func("glCompressedTexSubImage1D");
  789. _glCompressedTexSubImage2D = (PFNGLCOMPRESSEDTEXSUBIMAGE2DPROC)
  790. get_extension_func("glCompressedTexSubImage2D");
  791. _glCompressedTexSubImage3D = (PFNGLCOMPRESSEDTEXSUBIMAGE3DPROC)
  792. get_extension_func("glCompressedTexSubImage3D");
  793. _glGetCompressedTexImage = (PFNGLGETCOMPRESSEDTEXIMAGEPROC)
  794. get_extension_func("glGetCompressedTexImage");
  795. } else if (has_extension("GL_ARB_texture_compression")) {
  796. _supports_compressed_texture = true;
  797. _glCompressedTexImage1D = (PFNGLCOMPRESSEDTEXIMAGE1DPROC)
  798. get_extension_func("glCompressedTexImage1DARB");
  799. _glCompressedTexImage2D = (PFNGLCOMPRESSEDTEXIMAGE2DPROC)
  800. get_extension_func("glCompressedTexImage2DARB");
  801. _glCompressedTexImage3D = (PFNGLCOMPRESSEDTEXIMAGE3DPROC)
  802. get_extension_func("glCompressedTexImage3DARB");
  803. _glCompressedTexSubImage1D = (PFNGLCOMPRESSEDTEXSUBIMAGE1DPROC)
  804. get_extension_func("glCompressedTexSubImage1DARB");
  805. _glCompressedTexSubImage2D = (PFNGLCOMPRESSEDTEXSUBIMAGE2DPROC)
  806. get_extension_func("glCompressedTexSubImage2DARB");
  807. _glCompressedTexSubImage3D = (PFNGLCOMPRESSEDTEXSUBIMAGE3DPROC)
  808. get_extension_func("glCompressedTexSubImage3DARB");
  809. _glGetCompressedTexImage = (PFNGLGETCOMPRESSEDTEXIMAGEPROC)
  810. get_extension_func("glGetCompressedTexImageARB");
  811. }
  812. if (_supports_compressed_texture) {
  813. if (_glCompressedTexImage1D == NULL ||
  814. _glCompressedTexImage2D == NULL ||
  815. _glCompressedTexImage3D == NULL ||
  816. _glCompressedTexSubImage1D == NULL ||
  817. _glCompressedTexSubImage2D == NULL ||
  818. _glCompressedTexSubImage3D == NULL ||
  819. _glGetCompressedTexImage == NULL) {
  820. GLCAT.warning()
  821. << "Compressed textures advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  822. _supports_compressed_texture = false;
  823. }
  824. }
  825. #endif
  826. if (_supports_compressed_texture) {
  827. #ifndef OPENGLES
  828. _compressed_texture_formats.set_bit(Texture::CM_on);
  829. #endif
  830. GLint num_compressed_formats = 0;
  831. glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_compressed_formats);
  832. GLint *formats = (GLint *)PANDA_MALLOC_ARRAY(num_compressed_formats * sizeof(GLint));
  833. glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats);
  834. for (int i = 0; i < num_compressed_formats; ++i) {
  835. switch (formats[i]) {
  836. case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  837. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  838. _compressed_texture_formats.set_bit(Texture::CM_dxt1);
  839. break;
  840. #ifdef OPENGLES
  841. case GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  842. case GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  843. _compressed_texture_formats.set_bit(Texture::CM_pvr1_2bpp);
  844. break;
  845. case GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  846. case GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  847. _compressed_texture_formats.set_bit(Texture::CM_pvr1_4bpp);
  848. break;
  849. #else
  850. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  851. _compressed_texture_formats.set_bit(Texture::CM_dxt3);
  852. break;
  853. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  854. _compressed_texture_formats.set_bit(Texture::CM_dxt5);
  855. break;
  856. case GL_COMPRESSED_RGB_FXT1_3DFX:
  857. case GL_COMPRESSED_RGBA_FXT1_3DFX:
  858. _compressed_texture_formats.set_bit(Texture::CM_fxt1);
  859. break;
  860. #endif
  861. default:
  862. break;
  863. }
  864. }
  865. PANDA_FREE_ARRAY(formats);
  866. }
  867. #ifdef OPENGLES_2
  868. _supports_bgr = false;
  869. #else
  870. _supports_bgr =
  871. has_extension("GL_EXT_bgra") || is_at_least_gl_version(1, 2);
  872. #endif
  873. #ifdef SUPPORT_FIXED_FUNCTION
  874. _supports_rescale_normal =
  875. !core_profile && gl_support_rescale_normal &&
  876. (has_extension("GL_EXT_rescale_normal") || is_at_least_gl_version(1, 2));
  877. #endif
  878. #ifdef OPENGLES
  879. _supports_packed_dabc = false;
  880. _supports_packed_ufloat = false;
  881. #else
  882. _supports_packed_dabc = is_at_least_gl_version(3, 2) ||
  883. has_extension("GL_ARB_vertex_array_bgra") ||
  884. has_extension("GL_EXT_vertex_array_bgra");
  885. _supports_packed_ufloat = is_at_least_gl_version(4, 4) ||
  886. has_extension("GL_ARB_vertex_type_10f_11f_11f_rev");
  887. #endif
  888. _supports_multisample =
  889. has_extension("GL_ARB_multisample") || is_at_least_gl_version(1, 3);
  890. #ifdef OPENGLES_2
  891. _supports_generate_mipmap = true;
  892. #else
  893. _supports_generate_mipmap =
  894. has_extension("GL_SGIS_generate_mipmap") || is_at_least_gl_version(1, 4) || is_at_least_gles_version(1, 1);
  895. #endif
  896. #ifdef OPENGLES
  897. _supports_tex_non_pow2 =
  898. has_extension("GL_OES_texture_npot");
  899. #else
  900. _supports_tex_non_pow2 =
  901. has_extension("GL_ARB_texture_non_power_of_two");
  902. #endif
  903. #ifdef OPENGLES_2
  904. _glActiveTexture = glActiveTexture;
  905. #else
  906. bool supports_multitexture = false;
  907. if (is_at_least_gl_version(1, 3) || is_at_least_gles_version(1, 1)) {
  908. supports_multitexture = true;
  909. _glActiveTexture = (PFNGLACTIVETEXTUREPROC)
  910. get_extension_func("glActiveTexture");
  911. #ifdef SUPPORT_FIXED_FUNCTION
  912. _glClientActiveTexture = (PFNGLACTIVETEXTUREPROC)
  913. get_extension_func("glClientActiveTexture");
  914. #endif
  915. #ifdef SUPPORT_IMMEDIATE_MODE
  916. _glMultiTexCoord1f = (PFNGLMULTITEXCOORD1FPROC)
  917. get_extension_func("glMultiTexCoord1f");
  918. _glMultiTexCoord2f = (PFNGLMULTITEXCOORD2FPROC)
  919. get_extension_func("glMultiTexCoord2f");
  920. _glMultiTexCoord3f = (PFNGLMULTITEXCOORD3FPROC)
  921. get_extension_func("glMultiTexCoord3f");
  922. _glMultiTexCoord4f = (PFNGLMULTITEXCOORD4FPROC)
  923. get_extension_func("glMultiTexCoord4f");
  924. _glMultiTexCoord1d = (PFNGLMULTITEXCOORD1DPROC)
  925. get_extension_func("glMultiTexCoord1d");
  926. _glMultiTexCoord2d = (PFNGLMULTITEXCOORD2DPROC)
  927. get_extension_func("glMultiTexCoord2d");
  928. _glMultiTexCoord3d = (PFNGLMULTITEXCOORD3DPROC)
  929. get_extension_func("glMultiTexCoord3d");
  930. _glMultiTexCoord4d = (PFNGLMULTITEXCOORD4DPROC)
  931. get_extension_func("glMultiTexCoord4d");
  932. #endif
  933. } else if (has_extension("GL_ARB_multitexture")) {
  934. supports_multitexture = true;
  935. _glActiveTexture = (PFNGLACTIVETEXTUREPROC)
  936. get_extension_func("glActiveTextureARB");
  937. #ifdef SUPPORT_FIXED_FUNCTION
  938. _glClientActiveTexture = (PFNGLACTIVETEXTUREPROC)
  939. get_extension_func("glClientActiveTextureARB");
  940. #endif
  941. #ifdef SUPPORT_IMMEDIATE_MODE
  942. _glMultiTexCoord1f = (PFNGLMULTITEXCOORD1FPROC)
  943. get_extension_func("glMultiTexCoord1fARB");
  944. _glMultiTexCoord2f = (PFNGLMULTITEXCOORD2FPROC)
  945. get_extension_func("glMultiTexCoord2fARB");
  946. _glMultiTexCoord3f = (PFNGLMULTITEXCOORD3FPROC)
  947. get_extension_func("glMultiTexCoord3fARB");
  948. _glMultiTexCoord4f = (PFNGLMULTITEXCOORD4FPROC)
  949. get_extension_func("glMultiTexCoord4fARB");
  950. _glMultiTexCoord1d = (PFNGLMULTITEXCOORD1DPROC)
  951. get_extension_func("glMultiTexCoord1dARB");
  952. _glMultiTexCoord2d = (PFNGLMULTITEXCOORD2DPROC)
  953. get_extension_func("glMultiTexCoord2dARB");
  954. _glMultiTexCoord3d = (PFNGLMULTITEXCOORD3DPROC)
  955. get_extension_func("glMultiTexCoord3dARB");
  956. _glMultiTexCoord4d = (PFNGLMULTITEXCOORD4DPROC)
  957. get_extension_func("glMultiTexCoord4dARB");
  958. #endif
  959. } else {
  960. supports_multitexture = false;
  961. }
  962. if (supports_multitexture) {
  963. if (_glActiveTexture == NULL
  964. #ifdef SUPPORT_FIXED_FUNCTION
  965. || _glClientActiveTexture == NULL
  966. #endif
  967. #ifdef SUPPORT_IMMEDIATE_MODE
  968. || GLf(_glMultiTexCoord1) == NULL || GLf(_glMultiTexCoord2) == NULL
  969. || GLf(_glMultiTexCoord3) == NULL || GLf(_glMultiTexCoord4) == NULL
  970. #endif
  971. ) {
  972. GLCAT.warning()
  973. << "Multitexture advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  974. supports_multitexture = false;
  975. }
  976. }
  977. if (!supports_multitexture) {
  978. // Replace with dummy no-op functions.
  979. _glActiveTexture = null_glActiveTexture;
  980. }
  981. #ifdef SUPPORT_FIXED_FUNCTION
  982. if (!supports_multitexture || core_profile) {
  983. _glClientActiveTexture = null_glActiveTexture;
  984. }
  985. #endif
  986. #endif // OPENGLES_2
  987. #ifdef OPENGLES
  988. if (has_extension("GL_ANGLE_depth_texture")) {
  989. // This extension provides both depth textures and depth-stencil support.
  990. _supports_depth_texture = true;
  991. _supports_depth_stencil = true;
  992. } else if (has_extension("GL_OES_depth_texture")) {
  993. _supports_depth_texture = true;
  994. _supports_depth_stencil = has_extension("GL_OES_packed_depth_stencil");
  995. }
  996. _supports_depth24 = has_extension("GL_OES_depth24");
  997. _supports_depth32 = has_extension("GL_OES_depth32");
  998. #else
  999. _supports_depth_texture = (is_at_least_gl_version(1, 4) ||
  1000. has_extension("GL_ARB_depth_texture"));
  1001. _supports_depth_stencil = (is_at_least_gl_version(3, 0) ||
  1002. has_extension("GL_ARB_framebuffer_object") ||
  1003. has_extension("GL_EXT_packed_depth_stencil"));
  1004. #endif
  1005. #ifdef OPENGLES_2
  1006. if (gl_support_shadow_filter && _supports_depth_texture &&
  1007. has_extension("GL_EXT_shadow_samplers")) {
  1008. _supports_shadow_filter = true;
  1009. }
  1010. #else
  1011. if (gl_support_shadow_filter &&
  1012. _supports_depth_texture &&
  1013. has_extension("GL_ARB_shadow") &&
  1014. has_extension("GL_ARB_fragment_program_shadow")) {
  1015. _supports_shadow_filter = true;
  1016. }
  1017. #endif
  1018. // Actually, we can't keep forever disabling ARB_shadow on ATI cards,
  1019. // since they do work correctly now. Maybe there is some feature
  1020. // level we can check somewhere?
  1021. /*if (_gl_vendor.substr(0,3)=="ATI") {
  1022. // ATI drivers have never provided correct shadow support.
  1023. _supports_shadow_filter = false;
  1024. }*/
  1025. #ifndef SUPPORT_FIXED_FUNCTION
  1026. _supports_texture_combine = false;
  1027. _supports_texture_saved_result = false;
  1028. _supports_texture_dot3 = false;
  1029. #else
  1030. if (!core_profile) {
  1031. _supports_texture_combine =
  1032. has_extension("GL_ARB_texture_env_combine") || is_at_least_gl_version(1, 3) || is_at_least_gles_version(1, 1);
  1033. _supports_texture_saved_result =
  1034. has_extension("GL_ARB_texture_env_crossbar") || has_extension("GL_OES_texture_env_crossbar") || is_at_least_gl_version(1, 4);
  1035. _supports_texture_dot3 =
  1036. has_extension("GL_ARB_texture_env_dot3") || is_at_least_gl_version(1, 3) || is_at_least_gles_version(1, 1);
  1037. }
  1038. #endif
  1039. #ifdef OPENGLES_2
  1040. _supports_buffers = true;
  1041. _glGenBuffers = glGenBuffers;
  1042. _glBindBuffer = glBindBuffer;
  1043. _glBufferData = glBufferData;
  1044. _glBufferSubData = glBufferSubData;
  1045. _glDeleteBuffers = glDeleteBuffers;
  1046. #else
  1047. _supports_buffers = false;
  1048. if (is_at_least_gl_version(1, 5) || is_at_least_gles_version(1, 1)) {
  1049. _supports_buffers = true;
  1050. _glGenBuffers = (PFNGLGENBUFFERSPROC)
  1051. get_extension_func("glGenBuffers");
  1052. _glBindBuffer = (PFNGLBINDBUFFERPROC)
  1053. get_extension_func("glBindBuffer");
  1054. _glBufferData = (PFNGLBUFFERDATAPROC)
  1055. get_extension_func("glBufferData");
  1056. _glBufferSubData = (PFNGLBUFFERSUBDATAPROC)
  1057. get_extension_func("glBufferSubData");
  1058. _glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)
  1059. get_extension_func("glDeleteBuffers");
  1060. }
  1061. #ifndef OPENGLES_1
  1062. else if (has_extension("GL_ARB_vertex_buffer_object")) {
  1063. _supports_buffers = true;
  1064. _glGenBuffers = (PFNGLGENBUFFERSPROC)
  1065. get_extension_func("glGenBuffersARB");
  1066. _glBindBuffer = (PFNGLBINDBUFFERPROC)
  1067. get_extension_func("glBindBufferARB");
  1068. _glBufferData = (PFNGLBUFFERDATAPROC)
  1069. get_extension_func("glBufferDataARB");
  1070. _glBufferSubData = (PFNGLBUFFERSUBDATAPROC)
  1071. get_extension_func("glBufferSubDataARB");
  1072. _glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)
  1073. get_extension_func("glDeleteBuffersARB");
  1074. }
  1075. #endif // OPENGLES_1
  1076. if (_supports_buffers) {
  1077. if (_glGenBuffers == NULL || _glBindBuffer == NULL ||
  1078. _glBufferData == NULL || _glBufferSubData == NULL ||
  1079. _glDeleteBuffers == NULL) {
  1080. GLCAT.warning()
  1081. << "Buffers advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  1082. _supports_buffers = false;
  1083. }
  1084. }
  1085. #endif
  1086. _supports_vao = false;
  1087. if (is_at_least_gl_version(3, 0) || has_extension("GL_ARB_vertex_array_object")) {
  1088. _supports_vao = true;
  1089. _glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)
  1090. get_extension_func("glBindVertexArray");
  1091. _glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)
  1092. get_extension_func("glDeleteVertexArrays");
  1093. _glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)
  1094. get_extension_func("glGenVertexArrays");
  1095. } else if (has_extension("GL_OES_vertex_array_object")) {
  1096. _supports_vao = true;
  1097. _glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)
  1098. get_extension_func("glBindVertexArrayOES");
  1099. _glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)
  1100. get_extension_func("glDeleteVertexArraysOES");
  1101. _glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)
  1102. get_extension_func("glGenVertexArraysOES");
  1103. }
  1104. if (_supports_vao) {
  1105. if (_glBindVertexArray == NULL || _glDeleteVertexArrays == NULL ||
  1106. _glGenVertexArrays == NULL) {
  1107. GLCAT.warning()
  1108. << "Vertex array objects advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  1109. _supports_vao = false;
  1110. }
  1111. }
  1112. // Check for GLSL support.
  1113. #if defined(OPENGLES_1)
  1114. _supports_glsl = false;
  1115. _supports_geometry_shaders = false;
  1116. _supports_tessellation_shaders = false;
  1117. #elif defined(OPENGLES)
  1118. _supports_glsl = true;
  1119. _supports_geometry_shaders = false;
  1120. _supports_tessellation_shaders = false;
  1121. #else
  1122. _supports_glsl = (_gl_shadlang_ver_major >= 1);
  1123. _supports_tessellation_shaders = is_at_least_gl_version(4, 0) || has_extension("GL_ARB_tessellation_shader");
  1124. if (is_at_least_gl_version(3, 2)) {
  1125. _supports_geometry_shaders = true;
  1126. _glFramebufferTexture = (PFNGLFRAMEBUFFERTEXTUREARBPROC)
  1127. get_extension_func("glFramebufferTexture");
  1128. } else if (has_extension("GL_ARB_geometry_shader4")) {
  1129. _supports_geometry_shaders = true;
  1130. _glFramebufferTexture = (PFNGLFRAMEBUFFERTEXTUREARBPROC)
  1131. get_extension_func("glFramebufferTextureARB");
  1132. _glProgramParameteri = (PFNGLPROGRAMPARAMETERIPROC)
  1133. get_extension_func("glProgramParameteriARB");
  1134. } else if (has_extension("GL_EXT_geometry_shader4")) {
  1135. _supports_geometry_shaders = true;
  1136. _glFramebufferTexture = NULL;
  1137. _glProgramParameteri = (PFNGLPROGRAMPARAMETERIPROC)
  1138. get_extension_func("glProgramParameteriEXT");
  1139. } else {
  1140. _supports_geometry_shaders = false;
  1141. _glFramebufferTexture = NULL;
  1142. }
  1143. #endif
  1144. _shader_caps._supports_glsl = _supports_glsl;
  1145. // Check for support for other types of shaders that can be used by Cg.
  1146. _supports_basic_shaders = false;
  1147. #if defined(HAVE_CG) && !defined(OPENGLES)
  1148. if (has_extension("GL_ARB_vertex_program") &&
  1149. has_extension("GL_ARB_fragment_program")) {
  1150. _supports_basic_shaders = true;
  1151. _shader_caps._active_vprofile = (int)CG_PROFILE_ARBVP1;
  1152. _shader_caps._active_fprofile = (int)CG_PROFILE_ARBFP1;
  1153. _shader_caps._active_gprofile = (int)CG_PROFILE_UNKNOWN; // No geometry shader if only using basic
  1154. if (basic_shaders_only) {
  1155. // We're happy with ARB programs, thanks.
  1156. } else if (has_extension("GL_NV_gpu_program5")) {
  1157. _shader_caps._active_vprofile = (int)CG_PROFILE_GP5VP;
  1158. _shader_caps._active_fprofile = (int)CG_PROFILE_GP5FP;
  1159. _shader_caps._active_gprofile = (int)CG_PROFILE_GP5GP;
  1160. } else if (has_extension("GL_NV_gpu_program4")) {
  1161. _shader_caps._active_vprofile = (int)CG_PROFILE_GP4VP;
  1162. _shader_caps._active_fprofile = (int)CG_PROFILE_GP4FP;
  1163. _shader_caps._active_gprofile = (int)CG_PROFILE_GP4GP;
  1164. } else if (has_extension("GL_NV_vertex_program3") &&
  1165. has_extension("GL_NV_fragment_program2")) {
  1166. _shader_caps._active_vprofile = (int)CG_PROFILE_VP40;
  1167. _shader_caps._active_fprofile = (int)CG_PROFILE_FP40;
  1168. _shader_caps._active_gprofile = (int)CG_PROFILE_UNKNOWN;
  1169. } else if (has_extension("GL_NV_vertex_program2") &&
  1170. has_extension("GL_NV_fragment_program")) {
  1171. _shader_caps._active_vprofile = (int)CG_PROFILE_VP30;
  1172. _shader_caps._active_fprofile = (int)CG_PROFILE_FP30;
  1173. _shader_caps._active_gprofile = (int)CG_PROFILE_UNKNOWN;
  1174. } else if (has_extension("GL_NV_vertex_program1_1") &&
  1175. has_extension("GL_NV_texture_shader2") &&
  1176. has_extension("GL_NV_register_combiners2")) {
  1177. _shader_caps._active_vprofile = (int)CG_PROFILE_VP20;
  1178. _shader_caps._active_fprofile = (int)CG_PROFILE_FP20;
  1179. _shader_caps._active_gprofile = (int)CG_PROFILE_UNKNOWN;
  1180. } else if (_supports_glsl) {
  1181. // This is what will be available to non-NVIDIA cards. It is the last
  1182. // option since it is slower to compile GLSL than the other options.
  1183. _shader_caps._active_vprofile = (int)CG_PROFILE_GLSLV;
  1184. _shader_caps._active_fprofile = (int)CG_PROFILE_GLSLF;
  1185. if (_supports_geometry_shaders) {
  1186. _shader_caps._active_gprofile = (int)CG_PROFILE_GLSLG;
  1187. }
  1188. }
  1189. _shader_caps._ultimate_vprofile = (int)CG_PROFILE_VP40;
  1190. _shader_caps._ultimate_fprofile = (int)CG_PROFILE_FP40;
  1191. _shader_caps._ultimate_gprofile = (int)CG_PROFILE_GPU_GP;
  1192. // Bug workaround for radeons.
  1193. if (_shader_caps._active_fprofile == CG_PROFILE_ARBFP1) {
  1194. if (has_extension("GL_ATI_draw_buffers")) {
  1195. _shader_caps._bug_list.insert(Shader::SBUG_ati_draw_buffers);
  1196. }
  1197. }
  1198. } else if (_supports_glsl) {
  1199. // No, but we do support GLSL...
  1200. _shader_caps._active_vprofile = (int)CG_PROFILE_GLSLV;
  1201. _shader_caps._active_fprofile = (int)CG_PROFILE_GLSLF;
  1202. if (_supports_geometry_shaders) {
  1203. _shader_caps._active_gprofile = (int)CG_PROFILE_GLSLG;
  1204. } else {
  1205. _shader_caps._active_gprofile = (int)CG_PROFILE_UNKNOWN;
  1206. }
  1207. }
  1208. #endif // HAVE_CG
  1209. _supports_compute_shaders = false;
  1210. #ifndef OPENGLES
  1211. if (is_at_least_gl_version(4, 3) || has_extension("GL_ARB_compute_shader")) {
  1212. _glDispatchCompute = (PFNGLDISPATCHCOMPUTEPROC)
  1213. get_extension_func("glDispatchCompute");
  1214. if (_glDispatchCompute != NULL) {
  1215. _supports_compute_shaders = true;
  1216. }
  1217. }
  1218. #endif
  1219. #ifndef OPENGLES
  1220. if (_supports_glsl) {
  1221. _glAttachShader = (PFNGLATTACHSHADERPROC)
  1222. get_extension_func("glAttachShader");
  1223. _glBindAttribLocation = (PFNGLBINDATTRIBLOCATIONPROC)
  1224. get_extension_func("glBindAttribLocation");
  1225. _glCompileShader = (PFNGLCOMPILESHADERPROC)
  1226. get_extension_func("glCompileShader");
  1227. _glCreateProgram = (PFNGLCREATEPROGRAMPROC)
  1228. get_extension_func("glCreateProgram");
  1229. _glCreateShader = (PFNGLCREATESHADERPROC)
  1230. get_extension_func("glCreateShader");
  1231. _glDeleteProgram = (PFNGLDELETEPROGRAMPROC)
  1232. get_extension_func("glDeleteProgram");
  1233. _glDeleteShader = (PFNGLDELETESHADERPROC)
  1234. get_extension_func("glDeleteShader");
  1235. _glDetachShader = (PFNGLDETACHSHADERPROC)
  1236. get_extension_func("glDetachShader");
  1237. _glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)
  1238. get_extension_func("glDisableVertexAttribArray");
  1239. _glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)
  1240. get_extension_func("glEnableVertexAttribArray");
  1241. _glGetActiveAttrib = (PFNGLGETACTIVEATTRIBPROC)
  1242. get_extension_func("glGetActiveAttrib");
  1243. _glGetActiveUniform = (PFNGLGETACTIVEUNIFORMPROC)
  1244. get_extension_func("glGetActiveUniform");
  1245. _glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)
  1246. get_extension_func("glGetAttribLocation");
  1247. _glGetProgramiv = (PFNGLGETPROGRAMIVPROC)
  1248. get_extension_func("glGetProgramiv");
  1249. _glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)
  1250. get_extension_func("glGetProgramInfoLog");
  1251. _glGetShaderiv = (PFNGLGETSHADERIVPROC)
  1252. get_extension_func("glGetShaderiv");
  1253. _glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)
  1254. get_extension_func("glGetShaderInfoLog");
  1255. _glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)
  1256. get_extension_func("glGetUniformLocation");
  1257. _glLinkProgram = (PFNGLLINKPROGRAMPROC)
  1258. get_extension_func("glLinkProgram");
  1259. _glShaderSource = (PFNGLSHADERSOURCEPROC_P)
  1260. get_extension_func("glShaderSource");
  1261. _glUseProgram = (PFNGLUSEPROGRAMPROC)
  1262. get_extension_func("glUseProgram");
  1263. _glUniform4f = (PFNGLUNIFORM4FPROC)
  1264. get_extension_func("glUniform4f");
  1265. _glUniform1i = (PFNGLUNIFORM1IPROC)
  1266. get_extension_func("glUniform1i");
  1267. _glUniform1fv = (PFNGLUNIFORM1FVPROC)
  1268. get_extension_func("glUniform1fv");
  1269. _glUniform2fv = (PFNGLUNIFORM2FVPROC)
  1270. get_extension_func("glUniform2fv");
  1271. _glUniform3fv = (PFNGLUNIFORM3FVPROC)
  1272. get_extension_func("glUniform3fv");
  1273. _glUniform4fv = (PFNGLUNIFORM4FVPROC)
  1274. get_extension_func("glUniform4fv");
  1275. _glUniform1iv = (PFNGLUNIFORM1IVPROC)
  1276. get_extension_func("glUniform1iv");
  1277. _glUniform2iv = (PFNGLUNIFORM2IVPROC)
  1278. get_extension_func("glUniform2iv");
  1279. _glUniform3iv = (PFNGLUNIFORM3IVPROC)
  1280. get_extension_func("glUniform3iv");
  1281. _glUniform4iv = (PFNGLUNIFORM4IVPROC)
  1282. get_extension_func("glUniform4iv");
  1283. _glUniformMatrix3fv = (PFNGLUNIFORMMATRIX3FVPROC)
  1284. get_extension_func("glUniformMatrix3fv");
  1285. _glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)
  1286. get_extension_func("glUniformMatrix4fv");
  1287. _glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)
  1288. get_extension_func("glValidateProgram");
  1289. _glVertexAttrib4fv = (PFNGLVERTEXATTRIB4FVPROC)
  1290. get_extension_func("glVertexAttrib4fv");
  1291. _glVertexAttrib4dv = (PFNGLVERTEXATTRIB4DVPROC)
  1292. get_extension_func("glVertexAttrib4dv");
  1293. _glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)
  1294. get_extension_func("glVertexAttribPointer");
  1295. if (is_at_least_gl_version(3, 0)) {
  1296. _glVertexAttribIPointer = (PFNGLVERTEXATTRIBIPOINTERPROC)
  1297. get_extension_func("glVertexAttribIPointer");
  1298. } else {
  1299. _glVertexAttribIPointer = NULL;
  1300. }
  1301. if (has_extension("GL_ARB_vertex_attrib_64bit")) {
  1302. _glVertexAttribLPointer = (PFNGLVERTEXATTRIBLPOINTERPROC)
  1303. get_extension_func("glVertexAttribLPointer");
  1304. } else {
  1305. _glVertexAttribLPointer = NULL;
  1306. }
  1307. if (_supports_tessellation_shaders) {
  1308. _glPatchParameteri = (PFNGLPATCHPARAMETERIPROC)
  1309. get_extension_func("glPatchParameteri");
  1310. }
  1311. }
  1312. #endif
  1313. #ifdef OPENGLES_2
  1314. _glAttachShader = glAttachShader;
  1315. _glBindAttribLocation = glBindAttribLocation;
  1316. _glCompileShader = glCompileShader;
  1317. _glCreateProgram = glCreateProgram;
  1318. _glCreateShader = glCreateShader;
  1319. _glDeleteProgram = glDeleteProgram;
  1320. _glDeleteShader = glDeleteShader;
  1321. _glDetachShader = glDetachShader;
  1322. _glDisableVertexAttribArray = glDisableVertexAttribArray;
  1323. _glEnableVertexAttribArray = glEnableVertexAttribArray;
  1324. _glGetActiveAttrib = glGetActiveAttrib;
  1325. _glGetActiveUniform = glGetActiveUniform;
  1326. _glGetAttribLocation = glGetAttribLocation;
  1327. _glGetProgramiv = glGetProgramiv;
  1328. _glGetProgramInfoLog = glGetProgramInfoLog;
  1329. _glGetShaderiv = glGetShaderiv;
  1330. _glGetShaderInfoLog = glGetShaderInfoLog;
  1331. _glGetUniformLocation = glGetUniformLocation;
  1332. _glLinkProgram = glLinkProgram;
  1333. _glShaderSource = (PFNGLSHADERSOURCEPROC_P) glShaderSource;
  1334. _glUseProgram = glUseProgram;
  1335. _glUniform4f = glUniform4f;
  1336. _glUniform1i = glUniform1i;
  1337. _glUniform1fv = glUniform1fv;
  1338. _glUniform2fv = glUniform2fv;
  1339. _glUniform3fv = glUniform3fv;
  1340. _glUniform4fv = glUniform4fv;
  1341. _glUniformMatrix3fv = glUniformMatrix3fv;
  1342. _glUniformMatrix4fv = glUniformMatrix4fv;
  1343. _glValidateProgram = glValidateProgram;
  1344. _glVertexAttrib4fv = glVertexAttrib4fv;
  1345. _glVertexAttrib4dv = NULL;
  1346. _glVertexAttribPointer = glVertexAttribPointer;
  1347. _glVertexAttribIPointer = NULL;
  1348. _glVertexAttribLPointer = NULL;
  1349. #endif
  1350. #ifndef OPENGLES_1
  1351. // We need to have a default shader to apply in case
  1352. // something didn't happen to have a shader applied, or
  1353. // if it failed to compile. This default shader just outputs
  1354. // a red color, indicating that something went wrong.
  1355. if (_default_shader == NULL && core_profile) {
  1356. _default_shader = Shader::make(Shader::SL_GLSL, default_vshader, default_fshader);
  1357. }
  1358. #endif
  1359. #ifndef OPENGLES
  1360. // Check for uniform buffers.
  1361. if (is_at_least_gl_version(3, 1) || has_extension("GL_ARB_uniform_buffer_object")) {
  1362. _supports_uniform_buffers = true;
  1363. _glGetActiveUniformsiv = (PFNGLGETACTIVEUNIFORMSIVPROC)
  1364. get_extension_func("glGetActiveUniformsiv");
  1365. _glGetActiveUniformBlockiv = (PFNGLGETACTIVEUNIFORMBLOCKIVPROC)
  1366. get_extension_func("glGetActiveUniformBlockiv");
  1367. _glGetActiveUniformBlockName = (PFNGLGETACTIVEUNIFORMBLOCKNAMEPROC)
  1368. get_extension_func("glGetActiveUniformBlockName");
  1369. } else {
  1370. _supports_uniform_buffers = false;
  1371. }
  1372. #else
  1373. _supports_uniform_buffers = false;
  1374. #endif
  1375. // Check whether we support geometry instancing and instanced vertex attribs.
  1376. #if defined(OPENGLES_1)
  1377. _supports_vertex_attrib_divisor = false;
  1378. _supports_geometry_instancing = false;
  1379. #elif defined(OPENGLES_2)
  1380. if (has_extension("GL_ANGLE_instanced_arrays")) {
  1381. // This extension has both things in one.
  1382. #ifdef __EMSCRIPTEN__
  1383. // Work around bug - it doesn't allow ANGLE suffix in getProcAddress.
  1384. _glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISORPROC)
  1385. get_extension_func("glVertexAttribDivisor");
  1386. _glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDPROC)
  1387. get_extension_func("glDrawArraysInstanced");
  1388. _glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDPROC)
  1389. get_extension_func("glDrawElementsInstanced");
  1390. #else
  1391. _glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISORPROC)
  1392. get_extension_func("glVertexAttribDivisorANGLE");
  1393. _glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDPROC)
  1394. get_extension_func("glDrawArraysInstancedANGLE");
  1395. _glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDPROC)
  1396. get_extension_func("glDrawElementsInstancedANGLE");
  1397. #endif
  1398. _supports_vertex_attrib_divisor = true;
  1399. _supports_geometry_instancing = true;
  1400. } else {
  1401. // Check separately for geometry instancing and instanced attribs.
  1402. if (has_extension("GL_EXT_draw_instanced")) {
  1403. _glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDPROC)
  1404. get_extension_func("glDrawArraysInstancedEXT");
  1405. _glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDPROC)
  1406. get_extension_func("glDrawElementsInstancedEXT");
  1407. _supports_geometry_instancing = true;
  1408. } else if (has_extension("GL_NV_draw_instanced")) {
  1409. _glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDPROC)
  1410. get_extension_func("glDrawArraysInstancedNV");
  1411. _glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDPROC)
  1412. get_extension_func("glDrawElementsInstancedNV");
  1413. _supports_geometry_instancing = true;
  1414. } else {
  1415. _supports_geometry_instancing = false;
  1416. }
  1417. if (has_extension("GL_EXT_instanced_arrays")) {
  1418. _glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISORPROC)
  1419. get_extension_func("glVertexAttribDivisorEXT");
  1420. _supports_vertex_attrib_divisor = true;
  1421. } else if (has_extension("GL_NV_instanced_arrays")) {
  1422. _glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISORPROC)
  1423. get_extension_func("glVertexAttribDivisorNV");
  1424. _supports_vertex_attrib_divisor = true;
  1425. } else {
  1426. _supports_vertex_attrib_divisor = false;
  1427. }
  1428. }
  1429. #else
  1430. if (is_at_least_gl_version(3, 3)) {
  1431. // This feature is in OpenGL core as of 3.3.
  1432. _glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISORPROC)
  1433. get_extension_func("glVertexAttribDivisor");
  1434. _supports_vertex_attrib_divisor = true;
  1435. } else if (has_extension("GL_ARB_instanced_arrays")) {
  1436. _glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISORPROC)
  1437. get_extension_func("glVertexAttribDivisorARB");
  1438. _supports_vertex_attrib_divisor = true;
  1439. } else {
  1440. _supports_vertex_attrib_divisor = false;
  1441. }
  1442. // Some drivers expose one extension, some expose the other.
  1443. if (is_at_least_gl_version(3, 1)) {
  1444. _glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDPROC)
  1445. get_extension_func("glDrawArraysInstanced");
  1446. _glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDPROC)
  1447. get_extension_func("glDrawElementsInstanced");
  1448. _supports_geometry_instancing = true;
  1449. } else if (has_extension("GL_ARB_draw_instanced")) {
  1450. _glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDPROC)
  1451. get_extension_func("glDrawArraysInstancedARB");
  1452. _glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDPROC)
  1453. get_extension_func("glDrawElementsInstancedARB");
  1454. _supports_geometry_instancing = true;
  1455. } else if (has_extension("GL_EXT_draw_instanced")) {
  1456. _glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDPROC)
  1457. get_extension_func("glDrawArraysInstancedEXT");
  1458. _glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDPROC)
  1459. get_extension_func("glDrawElementsInstancedEXT");
  1460. _supports_geometry_instancing = true;
  1461. } else {
  1462. _glDrawElementsInstanced = 0;
  1463. _glDrawArraysInstanced = 0;
  1464. _supports_geometry_instancing = false;
  1465. }
  1466. #endif
  1467. #ifndef OPENGLES_1
  1468. if (_supports_geometry_instancing) {
  1469. if (_glDrawArraysInstanced == NULL || _glDrawElementsInstanced == NULL) {
  1470. GLCAT.warning()
  1471. << "Geometry instancing advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  1472. _supports_geometry_instancing = false;
  1473. }
  1474. }
  1475. if (_supports_vertex_attrib_divisor) {
  1476. if (_glVertexAttribDivisor == NULL) {
  1477. GLCAT.warning()
  1478. << "Instanced vertex arrays advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  1479. _supports_vertex_attrib_divisor = false;
  1480. }
  1481. }
  1482. #endif
  1483. #ifdef OPENGLES_2
  1484. // In OpenGL ES 2.x, FBO's are supported in the core.
  1485. _supports_framebuffer_object = true;
  1486. _glIsRenderbuffer = glIsRenderbuffer;
  1487. _glBindRenderbuffer = glBindRenderbuffer;
  1488. _glDeleteRenderbuffers = glDeleteRenderbuffers;
  1489. _glGenRenderbuffers = glGenRenderbuffers;
  1490. _glRenderbufferStorage = glRenderbufferStorage;
  1491. _glGetRenderbufferParameteriv = glGetRenderbufferParameteriv;
  1492. _glIsFramebuffer = glIsFramebuffer;
  1493. _glBindFramebuffer = glBindFramebuffer;
  1494. _glDeleteFramebuffers = glDeleteFramebuffers;
  1495. _glGenFramebuffers = glGenFramebuffers;
  1496. _glCheckFramebufferStatus = glCheckFramebufferStatus;
  1497. _glFramebufferTexture1D = NULL;
  1498. _glFramebufferTexture2D = glFramebufferTexture2D;
  1499. _glFramebufferRenderbuffer = glFramebufferRenderbuffer;
  1500. _glGetFramebufferAttachmentParameteriv = glGetFramebufferAttachmentParameteriv;
  1501. _glGenerateMipmap = glGenerateMipmap;
  1502. #else
  1503. //TODO: add ARB/3.0 version
  1504. _supports_framebuffer_object = false;
  1505. if (has_extension("GL_EXT_framebuffer_object")) {
  1506. _supports_framebuffer_object = true;
  1507. _glIsRenderbuffer = (PFNGLISRENDERBUFFEREXTPROC)
  1508. get_extension_func("glIsRenderbufferEXT");
  1509. _glBindRenderbuffer = (PFNGLBINDRENDERBUFFEREXTPROC)
  1510. get_extension_func("glBindRenderbufferEXT");
  1511. _glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSEXTPROC)
  1512. get_extension_func("glDeleteRenderbuffersEXT");
  1513. _glGenRenderbuffers = (PFNGLGENRENDERBUFFERSEXTPROC)
  1514. get_extension_func("glGenRenderbuffersEXT");
  1515. _glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEEXTPROC)
  1516. get_extension_func("glRenderbufferStorageEXT");
  1517. _glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVEXTPROC)
  1518. get_extension_func("glGetRenderbufferParameterivEXT");
  1519. _glIsFramebuffer = (PFNGLISFRAMEBUFFEREXTPROC)
  1520. get_extension_func("glIsFramebufferEXT");
  1521. _glBindFramebuffer = (PFNGLBINDFRAMEBUFFEREXTPROC)
  1522. get_extension_func("glBindFramebufferEXT");
  1523. _glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSEXTPROC)
  1524. get_extension_func("glDeleteFramebuffersEXT");
  1525. _glGenFramebuffers = (PFNGLGENFRAMEBUFFERSEXTPROC)
  1526. get_extension_func("glGenFramebuffersEXT");
  1527. _glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSEXTPROC)
  1528. get_extension_func("glCheckFramebufferStatusEXT");
  1529. _glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DEXTPROC)
  1530. get_extension_func("glFramebufferTexture1DEXT");
  1531. _glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC)
  1532. get_extension_func("glFramebufferTexture2DEXT");
  1533. _glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DEXTPROC)
  1534. get_extension_func("glFramebufferTexture3DEXT");
  1535. _glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFEREXTPROC)
  1536. get_extension_func("glFramebufferRenderbufferEXT");
  1537. _glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVEXTPROC)
  1538. get_extension_func("glGetFramebufferAttachmentParameterivEXT");
  1539. _glGenerateMipmap = (PFNGLGENERATEMIPMAPEXTPROC)
  1540. get_extension_func("glGenerateMipmapEXT");
  1541. }
  1542. #ifdef OPENGLES
  1543. else if (has_extension("GL_OES_framebuffer_object")) {
  1544. _supports_framebuffer_object = true;
  1545. _glIsRenderbuffer = (PFNGLISRENDERBUFFEROESPROC)
  1546. get_extension_func("glIsRenderbufferOES");
  1547. _glBindRenderbuffer = (PFNGLBINDRENDERBUFFEROESPROC)
  1548. get_extension_func("glBindRenderbufferOES");
  1549. _glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSOESPROC)
  1550. get_extension_func("glDeleteRenderbuffersOES");
  1551. _glGenRenderbuffers = (PFNGLGENRENDERBUFFERSOESPROC)
  1552. get_extension_func("glGenRenderbuffersOES");
  1553. _glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEOESPROC)
  1554. get_extension_func("glRenderbufferStorageOES");
  1555. _glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVOESPROC)
  1556. get_extension_func("glGetRenderbufferParameterivOES");
  1557. _glIsFramebuffer = (PFNGLISFRAMEBUFFEROESPROC)
  1558. get_extension_func("glIsFramebufferOES");
  1559. _glBindFramebuffer = (PFNGLBINDFRAMEBUFFEROESPROC)
  1560. get_extension_func("glBindFramebufferOES");
  1561. _glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSOESPROC)
  1562. get_extension_func("glDeleteFramebuffersOES");
  1563. _glGenFramebuffers = (PFNGLGENFRAMEBUFFERSOESPROC)
  1564. get_extension_func("glGenFramebuffersOES");
  1565. _glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSOESPROC)
  1566. get_extension_func("glCheckFramebufferStatusOES");
  1567. _glFramebufferTexture1D = NULL;
  1568. _glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DOESPROC)
  1569. get_extension_func("glFramebufferTexture2DOES");
  1570. _glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFEROESPROC)
  1571. get_extension_func("glFramebufferRenderbufferOES");
  1572. _glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVOESPROC)
  1573. get_extension_func("glGetFramebufferAttachmentParameterivOES");
  1574. _glGenerateMipmap = (PFNGLGENERATEMIPMAPOESPROC)
  1575. get_extension_func("glGenerateMipmapOES");
  1576. }
  1577. #endif // OPENGLES
  1578. #endif
  1579. _supports_framebuffer_multisample = false;
  1580. if ( has_extension("GL_EXT_framebuffer_multisample") ) {
  1581. _supports_framebuffer_multisample = true;
  1582. _glRenderbufferStorageMultisample = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEEXTPROC)
  1583. get_extension_func("glRenderbufferStorageMultisampleEXT");
  1584. }
  1585. #ifndef OPENGLES
  1586. _supports_framebuffer_multisample_coverage_nv = false;
  1587. if ( has_extension("GL_NV_framebuffer_multisample_coverage") ) {
  1588. _supports_framebuffer_multisample_coverage_nv = true;
  1589. _glRenderbufferStorageMultisampleCoverage = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLECOVERAGENVPROC)
  1590. get_extension_func("glRenderbufferStorageMultisampleCoverageNV");
  1591. }
  1592. #endif
  1593. #ifndef OPENGLES_1
  1594. _supports_framebuffer_blit = false;
  1595. if ( has_extension("GL_EXT_framebuffer_blit") ) {
  1596. _supports_framebuffer_blit = true;
  1597. _glBlitFramebuffer = (PFNGLBLITFRAMEBUFFEREXTPROC)
  1598. get_extension_func("glBlitFramebufferEXT");
  1599. }
  1600. #endif
  1601. #if defined(OPENGLES_1)
  1602. _glDrawBuffers = NULL;
  1603. _glClearBufferfv = NULL;
  1604. _max_color_targets = 1;
  1605. #elif defined(OPENGLES_2)
  1606. if (has_extension("GL_EXT_draw_buffers")) {
  1607. _glDrawBuffers = (PFNGLDRAWBUFFERSPROC)
  1608. get_extension_func("glDrawBuffersEXT");
  1609. } else if (has_extension("GL_NV_draw_buffers")) {
  1610. _glDrawBuffers = (PFNGLDRAWBUFFERSPROC)
  1611. get_extension_func("glDrawBuffersNV");
  1612. }
  1613. #else
  1614. if (is_at_least_gl_version(2, 0)) {
  1615. _glDrawBuffers = (PFNGLDRAWBUFFERSPROC)
  1616. get_extension_func("glDrawBuffers");
  1617. } else if (has_extension("GL_ARB_draw_buffers")) {
  1618. _glDrawBuffers = (PFNGLDRAWBUFFERSPROC)
  1619. get_extension_func("glDrawBuffersARB");
  1620. }
  1621. #endif
  1622. #ifndef OPENGLES_1
  1623. _max_color_targets = 1;
  1624. if (_glDrawBuffers != 0) {
  1625. GLint max_draw_buffers = 0;
  1626. glGetIntegerv(GL_MAX_DRAW_BUFFERS, &max_draw_buffers);
  1627. _max_color_targets = max_draw_buffers;
  1628. }
  1629. #endif // !OPENGLES_1
  1630. #ifndef OPENGLES
  1631. if (is_at_least_gl_version(3, 0)) {
  1632. _glClearBufferfv = (PFNGLCLEARBUFFERFVPROC)
  1633. get_extension_func("glClearBufferfv");
  1634. } else {
  1635. _glClearBufferfv = NULL;
  1636. }
  1637. #endif // !OPENGLES
  1638. #ifndef OPENGLES
  1639. _supports_viewport_arrays = false;
  1640. if (is_at_least_gl_version(4, 1) || has_extension("GL_ARB_viewport_array")) {
  1641. _glViewportArrayv = (PFNGLVIEWPORTARRAYVPROC)
  1642. get_extension_func("glViewportArrayv");
  1643. _glScissorArrayv = (PFNGLSCISSORARRAYVPROC)
  1644. get_extension_func("glScissorArrayv");
  1645. _glDepthRangeArrayv = (PFNGLDEPTHRANGEARRAYVPROC)
  1646. get_extension_func("glDepthRangeArrayv");
  1647. if (_glViewportArrayv == NULL || _glScissorArrayv == NULL || _glDepthRangeArrayv == NULL) {
  1648. GLCAT.warning()
  1649. << "Viewport arrays advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  1650. } else {
  1651. _supports_viewport_arrays = true;
  1652. }
  1653. }
  1654. #endif // !OPENGLES
  1655. _max_fb_samples = 0;
  1656. if (_supports_framebuffer_multisample) {
  1657. GLint max_samples;
  1658. glGetIntegerv(GL_MAX_SAMPLES_EXT, &max_samples);
  1659. _max_fb_samples = max_samples;
  1660. }
  1661. _supports_occlusion_query = false;
  1662. #ifndef OPENGLES
  1663. if (gl_support_occlusion_query) {
  1664. if (is_at_least_gl_version(1, 5)) {
  1665. _supports_occlusion_query = true;
  1666. _glGenQueries = (PFNGLGENQUERIESPROC)
  1667. get_extension_func("glGenQueries");
  1668. _glBeginQuery = (PFNGLBEGINQUERYPROC)
  1669. get_extension_func("glBeginQuery");
  1670. _glEndQuery = (PFNGLENDQUERYPROC)
  1671. get_extension_func("glEndQuery");
  1672. _glDeleteQueries = (PFNGLDELETEQUERIESPROC)
  1673. get_extension_func("glDeleteQueries");
  1674. _glGetQueryiv = (PFNGLGETQUERYIVPROC)
  1675. get_extension_func("glGetQueryiv");
  1676. _glGetQueryObjectuiv = (PFNGLGETQUERYOBJECTUIVPROC)
  1677. get_extension_func("glGetQueryObjectuiv");
  1678. } else if (has_extension("GL_ARB_occlusion_query")) {
  1679. _supports_occlusion_query = true;
  1680. _glGenQueries = (PFNGLGENQUERIESPROC)
  1681. get_extension_func("glGenQueriesARB");
  1682. _glBeginQuery = (PFNGLBEGINQUERYPROC)
  1683. get_extension_func("glBeginQueryARB");
  1684. _glEndQuery = (PFNGLENDQUERYPROC)
  1685. get_extension_func("glEndQueryARB");
  1686. _glDeleteQueries = (PFNGLDELETEQUERIESPROC)
  1687. get_extension_func("glDeleteQueriesARB");
  1688. _glGetQueryiv = (PFNGLGETQUERYIVPROC)
  1689. get_extension_func("glGetQueryivARB");
  1690. _glGetQueryObjectuiv = (PFNGLGETQUERYOBJECTUIVPROC)
  1691. get_extension_func("glGetQueryObjectuivARB");
  1692. }
  1693. }
  1694. if (_supports_occlusion_query) {
  1695. if (_glGenQueries == NULL || _glBeginQuery == NULL ||
  1696. _glEndQuery == NULL || _glDeleteQueries == NULL ||
  1697. _glGetQueryiv == NULL || _glGetQueryObjectuiv == NULL) {
  1698. GLCAT.warning()
  1699. << "Occlusion queries advertised as supported by OpenGL runtime, but could not get pointers to extension functions.\n";
  1700. _supports_occlusion_query = false;
  1701. } else {
  1702. GLint num_bits;
  1703. _glGetQueryiv(GL_SAMPLES_PASSED, GL_QUERY_COUNTER_BITS, &num_bits);
  1704. if (num_bits == 0) {
  1705. _supports_occlusion_query = false;
  1706. }
  1707. if (GLCAT.is_debug()) {
  1708. GLCAT.debug()
  1709. << "Occlusion query counter provides " << num_bits << " bits.\n";
  1710. }
  1711. }
  1712. }
  1713. #endif // !OPENGLES
  1714. _supports_timer_query = false;
  1715. #if defined(DO_PSTATS) && !defined(OPENGLES)
  1716. if (is_at_least_gl_version(3, 3) || has_extension("GL_ARB_timer_query")) {
  1717. _supports_timer_query = true;
  1718. _glQueryCounter = (PFNGLQUERYCOUNTERPROC)
  1719. get_extension_func("glQueryCounter");
  1720. _glGetQueryObjecti64v = (PFNGLGETQUERYOBJECTI64VPROC)
  1721. get_extension_func("glGetQueryObjecti64v");
  1722. _glGetQueryObjectui64v = (PFNGLGETQUERYOBJECTUI64VPROC)
  1723. get_extension_func("glGetQueryObjectui64v");
  1724. _glGetInteger64v = (PFNGLGETINTEGER64VPROC)
  1725. get_extension_func("glGetInteger64v");
  1726. }
  1727. #endif
  1728. #ifdef OPENGLES_2
  1729. // In OpenGL ES 2.x, this is supported in the core.
  1730. _glBlendEquation = glBlendEquation;
  1731. #else
  1732. _glBlendEquation = NULL;
  1733. bool supports_blend_equation = false;
  1734. if (is_at_least_gl_version(1, 2)) {
  1735. supports_blend_equation = true;
  1736. _glBlendEquation = (PFNGLBLENDEQUATIONPROC)
  1737. get_extension_func("glBlendEquation");
  1738. } else if (has_extension("GL_OES_blend_subtract")) {
  1739. supports_blend_equation = true;
  1740. _glBlendEquation = (PFNGLBLENDEQUATIONPROC)
  1741. get_extension_func("glBlendEquationOES");
  1742. } else if (has_extension("GL_EXT_blend_minmax")) {
  1743. supports_blend_equation = true;
  1744. _glBlendEquation = (PFNGLBLENDEQUATIONPROC)
  1745. get_extension_func("glBlendEquationEXT");
  1746. }
  1747. if (supports_blend_equation && _glBlendEquation == NULL) {
  1748. GLCAT.warning()
  1749. << "BlendEquation advertised as supported by OpenGL runtime, but could not get pointers to extension function.\n";
  1750. }
  1751. if (_glBlendEquation == NULL) {
  1752. _glBlendEquation = null_glBlendEquation;
  1753. }
  1754. #endif
  1755. #ifdef OPENGLES_2
  1756. // In OpenGL ES 2.x, this is supported in the core.
  1757. _glBlendColor = glBlendColor;
  1758. #else
  1759. _glBlendColor = NULL;
  1760. bool supports_blend_color = false;
  1761. if (is_at_least_gl_version(1, 2)) {
  1762. supports_blend_color = true;
  1763. _glBlendColor = (PFNGLBLENDCOLORPROC)
  1764. get_extension_func("glBlendColor");
  1765. } else if (has_extension("GL_EXT_blend_color")) {
  1766. supports_blend_color = true;
  1767. _glBlendColor = (PFNGLBLENDCOLORPROC)
  1768. get_extension_func("glBlendColorEXT");
  1769. }
  1770. if (supports_blend_color && _glBlendColor == NULL) {
  1771. GLCAT.warning()
  1772. << "BlendColor advertised as supported by OpenGL runtime, but could not get pointers to extension function.\n";
  1773. }
  1774. if (_glBlendColor == NULL) {
  1775. _glBlendColor = null_glBlendColor;
  1776. }
  1777. #endif
  1778. #ifdef OPENGLES
  1779. _edge_clamp = GL_CLAMP_TO_EDGE;
  1780. #else
  1781. _edge_clamp = GL_CLAMP;
  1782. if (has_extension("GL_SGIS_texture_edge_clamp") ||
  1783. is_at_least_gl_version(1, 2) || is_at_least_gles_version(1, 1)) {
  1784. _edge_clamp = GL_CLAMP_TO_EDGE;
  1785. }
  1786. #endif
  1787. _border_clamp = _edge_clamp;
  1788. #ifndef OPENGLES
  1789. if (gl_support_clamp_to_border &&
  1790. (has_extension("GL_ARB_texture_border_clamp") ||
  1791. is_at_least_gl_version(1, 3))) {
  1792. _border_clamp = GL_CLAMP_TO_BORDER;
  1793. }
  1794. #endif
  1795. #ifdef OPENGLES_2
  1796. _mirror_repeat = GL_MIRRORED_REPEAT;
  1797. #else
  1798. _mirror_repeat = GL_REPEAT;
  1799. if (has_extension("GL_ARB_texture_mirrored_repeat") ||
  1800. is_at_least_gl_version(1, 4) ||
  1801. has_extension("GL_OES_texture_mirrored_repeat")) {
  1802. _mirror_repeat = GL_MIRRORED_REPEAT;
  1803. }
  1804. #endif
  1805. _mirror_clamp = _edge_clamp;
  1806. _mirror_edge_clamp = _edge_clamp;
  1807. _mirror_border_clamp = _border_clamp;
  1808. #ifndef OPENGLES
  1809. if (has_extension("GL_EXT_texture_mirror_clamp")) {
  1810. _mirror_clamp = GL_MIRROR_CLAMP_EXT;
  1811. _mirror_edge_clamp = GL_MIRROR_CLAMP_TO_EDGE_EXT;
  1812. _mirror_border_clamp = GL_MIRROR_CLAMP_TO_BORDER_EXT;
  1813. }
  1814. #endif
  1815. if (_supports_multisample) {
  1816. GLint sample_buffers = 0;
  1817. glGetIntegerv(GL_SAMPLE_BUFFERS, &sample_buffers);
  1818. if (sample_buffers != 1) {
  1819. // Even if the API supports multisample, we might have ended up
  1820. // with a framebuffer that doesn't have any multisample bits.
  1821. // (It's also possible the graphics card doesn't provide any
  1822. // framebuffers with multisample.) In this case, we don't
  1823. // really support the multisample API's, since they won't do
  1824. // anything.
  1825. _supports_multisample = false;
  1826. }
  1827. }
  1828. GLint max_texture_size = 0;
  1829. GLint max_3d_texture_size = 0;
  1830. GLint max_2d_texture_array_layers = 0;
  1831. GLint max_cube_map_size = 0;
  1832. GLint max_buffer_texture_size = 0;
  1833. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
  1834. _max_texture_dimension = max_texture_size;
  1835. if (_supports_3d_texture) {
  1836. #ifndef OPENGLES_1
  1837. glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &max_3d_texture_size);
  1838. #endif
  1839. _max_3d_texture_dimension = max_3d_texture_size;
  1840. } else {
  1841. _max_3d_texture_dimension = 0;
  1842. }
  1843. #ifndef OPENGLES
  1844. if(_supports_2d_texture_array) {
  1845. glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS_EXT, &max_2d_texture_array_layers);
  1846. _max_2d_texture_array_layers = max_2d_texture_array_layers;
  1847. }
  1848. #endif
  1849. if (_supports_cube_map) {
  1850. glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &max_cube_map_size);
  1851. _max_cube_map_dimension = max_cube_map_size;
  1852. } else {
  1853. _max_cube_map_dimension = 0;
  1854. }
  1855. #ifndef OPENGLES
  1856. if (_supports_buffer_texture) {
  1857. glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &max_buffer_texture_size);
  1858. _max_buffer_texture_size = max_buffer_texture_size;
  1859. } else {
  1860. _max_buffer_texture_size = 0;
  1861. }
  1862. #endif // !OPENGLES
  1863. GLint max_elements_vertices = 0, max_elements_indices = 0;
  1864. #ifndef OPENGLES
  1865. glGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &max_elements_vertices);
  1866. glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &max_elements_indices);
  1867. if (max_elements_vertices > 0) {
  1868. _max_vertices_per_array = max_elements_vertices;
  1869. }
  1870. if (max_elements_indices > 0) {
  1871. _max_vertices_per_primitive = max_elements_indices;
  1872. }
  1873. #endif // OPENGLES
  1874. if (GLCAT.is_debug()) {
  1875. GLCAT.debug()
  1876. << "max texture dimension = " << _max_texture_dimension
  1877. << ", max 3d texture = " << _max_3d_texture_dimension
  1878. << ", max 2d texture array = " << max_2d_texture_array_layers
  1879. << ", max cube map = " << _max_cube_map_dimension << "\n";
  1880. GLCAT.debug()
  1881. << "max_elements_vertices = " << max_elements_vertices
  1882. << ", max_elements_indices = " << max_elements_indices << "\n";
  1883. if (_supports_buffers) {
  1884. if (vertex_buffers) {
  1885. GLCAT.debug()
  1886. << "vertex buffer objects are supported.\n";
  1887. } else {
  1888. GLCAT.debug()
  1889. << "vertex buffer objects are supported (but not enabled).\n";
  1890. }
  1891. } else {
  1892. GLCAT.debug()
  1893. << "vertex buffer objects are NOT supported.\n";
  1894. }
  1895. #ifdef SUPPORT_IMMEDIATE_MODE
  1896. if (!vertex_arrays) {
  1897. GLCAT.debug()
  1898. << "immediate mode commands will be used instead of vertex arrays.\n";
  1899. }
  1900. #endif
  1901. if (!_supports_compressed_texture) {
  1902. GLCAT.debug()
  1903. << "Texture compression is not supported.\n";
  1904. } else {
  1905. GLint num_compressed_formats = 0;
  1906. glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &num_compressed_formats);
  1907. if (num_compressed_formats == 0) {
  1908. GLCAT.debug()
  1909. << "No specific compressed texture formats are supported.\n";
  1910. } else {
  1911. #ifndef NDEBUG
  1912. GLCAT.debug()
  1913. << "Supported compressed texture formats:\n";
  1914. GLint *formats = (GLint *)alloca(num_compressed_formats * sizeof(GLint));
  1915. glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, formats);
  1916. for (int i = 0; i < num_compressed_formats; ++i) {
  1917. const char *format_str = get_compressed_format_string(formats[i]);
  1918. if (format_str != NULL) {
  1919. GLCAT.debug(false) << " " << format_str << '\n';
  1920. } else {
  1921. GLCAT.debug(false)
  1922. << " Unknown compressed format 0x" << hex << formats[i]
  1923. << dec << "\n";
  1924. }
  1925. }
  1926. #endif
  1927. }
  1928. }
  1929. }
  1930. _num_active_texture_stages = 0;
  1931. // Check availability of anisotropic texture filtering.
  1932. _supports_anisotropy = false;
  1933. _max_anisotropy = 1.0;
  1934. if (has_extension("GL_EXT_texture_filter_anisotropic")) {
  1935. GLfloat max_anisotropy;
  1936. glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &max_anisotropy);
  1937. _max_anisotropy = (PN_stdfloat)max_anisotropy;
  1938. _supports_anisotropy = true;
  1939. }
  1940. // Check availability of image read/write functionality in shaders.
  1941. _max_image_units = 0;
  1942. #ifndef OPENGLES
  1943. if (is_at_least_gl_version(4, 2) || has_extension("GL_ARB_shader_image_load_store")) {
  1944. _glBindImageTexture = (PFNGLBINDIMAGETEXTUREPROC)
  1945. get_extension_func("glBindImageTexture");
  1946. _glMemoryBarrier = (PFNGLMEMORYBARRIERPROC)
  1947. get_extension_func("glMemoryBarrier");
  1948. glGetIntegerv(GL_MAX_IMAGE_UNITS, &_max_image_units);
  1949. } else if (has_extension("GL_EXT_shader_image_load_store")) {
  1950. _glBindImageTexture = (PFNGLBINDIMAGETEXTUREPROC)
  1951. get_extension_func("glBindImageTextureEXT");
  1952. _glMemoryBarrier = (PFNGLMEMORYBARRIERPROC)
  1953. get_extension_func("glMemoryBarrierEXT");
  1954. glGetIntegerv(GL_MAX_IMAGE_UNITS_EXT, &_max_image_units);
  1955. } else {
  1956. _glBindImageTexture = NULL;
  1957. _glMemoryBarrier = NULL;
  1958. }
  1959. _supports_sampler_objects = false;
  1960. #ifndef OPENGLES
  1961. if (gl_support_sampler_objects &&
  1962. ((is_at_least_gl_version(3, 3) || has_extension("GL_ARB_sampler_objects")))) {
  1963. _glGenSamplers = (PFNGLGENSAMPLERSPROC) get_extension_func("glGenSamplers");
  1964. _glDeleteSamplers = (PFNGLDELETESAMPLERSPROC) get_extension_func("glDeleteSamplers");
  1965. _glBindSampler = (PFNGLBINDSAMPLERPROC) get_extension_func("glBindSampler");
  1966. _glSamplerParameteri = (PFNGLSAMPLERPARAMETERIPROC) get_extension_func("glSamplerParameteri");
  1967. _glSamplerParameteriv = (PFNGLSAMPLERPARAMETERIVPROC) get_extension_func("glSamplerParameteriv");
  1968. _glSamplerParameterf = (PFNGLSAMPLERPARAMETERFPROC) get_extension_func("glSamplerParameterf");
  1969. _glSamplerParameterfv = (PFNGLSAMPLERPARAMETERFVPROC) get_extension_func("glSamplerParameterfv");
  1970. if (_glGenSamplers == NULL || _glDeleteSamplers == NULL ||
  1971. _glBindSampler == NULL || _glSamplerParameteri == NULL ||
  1972. _glSamplerParameteriv == NULL || _glSamplerParameterf == NULL ||
  1973. _glSamplerParameterfv == NULL) {
  1974. GLCAT.warning()
  1975. << "GL_ARB_sampler_objects advertised as supported by OpenGL runtime, but could not get pointers to extension function.\n";
  1976. } else {
  1977. _supports_sampler_objects = true;
  1978. }
  1979. }
  1980. #endif // OPENGLES
  1981. // Check availability of multi-bind functions.
  1982. _supports_multi_bind = false;
  1983. if (is_at_least_gl_version(4, 4) || has_extension("GL_ARB_multi_bind")) {
  1984. _glBindTextures = (PFNGLBINDTEXTURESPROC)
  1985. get_extension_func("glBindTextures");
  1986. _glBindImageTextures = (PFNGLBINDIMAGETEXTURESPROC)
  1987. get_extension_func("glBindImageTextures");
  1988. #ifndef OPENGLES
  1989. if (_supports_sampler_objects) {
  1990. _glBindSamplers = (PFNGLBINDSAMPLERSPROC)
  1991. get_extension_func("glBindSamplers");
  1992. }
  1993. #endif // OPENGLES
  1994. if (_glBindTextures != NULL && _glBindImageTextures != NULL) {
  1995. _supports_multi_bind = true;
  1996. } else {
  1997. GLCAT.warning()
  1998. << "ARB_multi_bind advertised as supported by OpenGL runtime, but could not get pointers to extension function.\n";
  1999. }
  2000. }
  2001. if (is_at_least_gl_version(4, 3) || has_extension("GL_ARB_internalformat_query2")) {
  2002. _glGetInternalformativ = (PFNGLGETINTERNALFORMATIVPROC)
  2003. get_extension_func("glGetInternalformativ");
  2004. if (_glGetInternalformativ == NULL) {
  2005. GLCAT.warning()
  2006. << "ARB_internalformat_query2 advertised as supported by OpenGL runtime, but could not get pointers to extension function.\n";
  2007. }
  2008. }
  2009. _supports_bindless_texture = false;
  2010. if (has_extension("GL_ARB_bindless_texture")) {
  2011. _glGetTextureHandle = (PFNGLGETTEXTUREHANDLEPROC)
  2012. get_extension_func("glGetTextureHandleARB");
  2013. _glGetTextureSamplerHandle = (PFNGLGETTEXTURESAMPLERHANDLEPROC)
  2014. get_extension_func("glGetTextureSamplerHandleARB");
  2015. _glMakeTextureHandleResident = (PFNGLMAKETEXTUREHANDLERESIDENTPROC)
  2016. get_extension_func("glMakeTextureHandleResidentARB");
  2017. _glUniformHandleui64 = (PFNGLUNIFORMHANDLEUI64PROC)
  2018. get_extension_func("glUniformHandleui64ARB");
  2019. if (_glGetTextureHandle == NULL || _glMakeTextureHandleResident == NULL ||
  2020. _glUniformHandleui64 == NULL) {
  2021. GLCAT.warning()
  2022. << "GL_ARB_bindless_texture advertised as supported by OpenGL runtime, but could not get pointers to extension function.\n";
  2023. } else {
  2024. _supports_bindless_texture = true;
  2025. }
  2026. }
  2027. #endif
  2028. #ifndef OPENGLES
  2029. _supports_get_program_binary = false;
  2030. if (is_at_least_gl_version(4, 1) || has_extension("GL_ARB_get_program_binary")) {
  2031. _glGetProgramBinary = (PFNGLGETPROGRAMBINARYPROC)
  2032. get_extension_func("glGetProgramBinary");
  2033. _glProgramParameteri = (PFNGLPROGRAMPARAMETERIPROC)
  2034. get_extension_func("glProgramParameteri");
  2035. if (_glGetProgramBinary != NULL && _glProgramParameteri != NULL) {
  2036. _supports_get_program_binary = true;
  2037. }
  2038. }
  2039. #endif
  2040. report_my_gl_errors();
  2041. if (core_profile) {
  2042. //TODO: better detection mechanism?
  2043. _supports_stencil = true;
  2044. }
  2045. #ifdef SUPPORT_FIXED_FUNCTION
  2046. else if (support_stencil) {
  2047. GLint num_stencil_bits;
  2048. glGetIntegerv(GL_STENCIL_BITS, &num_stencil_bits);
  2049. _supports_stencil = (num_stencil_bits != 0);
  2050. }
  2051. #endif
  2052. _supports_stencil_wrap =
  2053. has_extension("GL_EXT_stencil_wrap") || has_extension("GL_OES_stencil_wrap");
  2054. _supports_two_sided_stencil = false;
  2055. #ifndef OPENGLES
  2056. if (has_extension("GL_EXT_stencil_two_side")) {
  2057. _glActiveStencilFaceEXT = (PFNGLACTIVESTENCILFACEEXTPROC)
  2058. get_extension_func("glActiveStencilFaceEXT");
  2059. _supports_two_sided_stencil = true;
  2060. } else {
  2061. _glActiveStencilFaceEXT = 0;
  2062. }
  2063. #endif
  2064. // Ensure the initial state is what we say it should be (in some
  2065. // cases, we don't want the GL default settings; in others, we have
  2066. // to force the point with some drivers that aren't strictly
  2067. // compliant w.r.t. initial settings).
  2068. glFrontFace(GL_CCW);
  2069. #ifndef OPENGLES_2
  2070. glDisable(GL_LINE_SMOOTH);
  2071. #endif
  2072. #ifdef SUPPORT_FIXED_FUNCTION
  2073. if (!core_profile) {
  2074. glDisable(GL_POINT_SMOOTH);
  2075. }
  2076. #endif
  2077. #ifndef OPENGLES
  2078. glDisable(GL_POLYGON_SMOOTH);
  2079. #endif // OPENGLES
  2080. #ifndef OPENGLES_2
  2081. if (_supports_multisample) {
  2082. glDisable(GL_MULTISAMPLE);
  2083. }
  2084. #endif
  2085. // Set up all the enabled/disabled flags to GL's known initial
  2086. // values: everything off.
  2087. _multisample_mode = 0;
  2088. _line_smooth_enabled = false;
  2089. _point_smooth_enabled = false;
  2090. _polygon_smooth_enabled = false;
  2091. _stencil_test_enabled = false;
  2092. _blend_enabled = false;
  2093. _depth_test_enabled = false;
  2094. _fog_enabled = false;
  2095. _alpha_test_enabled = false;
  2096. _polygon_offset_enabled = false;
  2097. _flat_shade_model = false;
  2098. _decal_level = 0;
  2099. _active_color_write_mask = ColorWriteAttrib::C_all;
  2100. _tex_gen_point_sprite = false;
  2101. #ifndef OPENGLES
  2102. // Dither is on by default in GL; let's turn it off
  2103. glDisable(GL_DITHER);
  2104. #endif // OPENGLES
  2105. _dithering_enabled = false;
  2106. #ifndef OPENGLES_1
  2107. _current_shader = (Shader *)NULL;
  2108. _current_shader_context = (ShaderContext *)NULL;
  2109. _vertex_array_shader = (Shader *)NULL;
  2110. _vertex_array_shader_context = (ShaderContext *)NULL;
  2111. _texture_binding_shader = (Shader *)NULL;
  2112. _texture_binding_shader_context = (ShaderContext *)NULL;
  2113. #endif
  2114. // Count the max number of lights
  2115. _max_lights = 0;
  2116. #ifdef SUPPORT_FIXED_FUNCTION
  2117. if (!core_profile) {
  2118. GLint max_lights = 0;
  2119. glGetIntegerv(GL_MAX_LIGHTS, &max_lights);
  2120. _max_lights = max_lights;
  2121. if (GLCAT.is_debug()) {
  2122. GLCAT.debug()
  2123. << "max lights = " << _max_lights << "\n";
  2124. }
  2125. }
  2126. #endif
  2127. // Count the max number of clipping planes
  2128. _max_clip_planes = 0;
  2129. #ifdef SUPPORT_FIXED_FUNCTION
  2130. if (!core_profile) {
  2131. GLint max_clip_planes = 0;
  2132. glGetIntegerv(GL_MAX_CLIP_PLANES, &max_clip_planes);
  2133. _max_clip_planes = max_clip_planes;
  2134. if (GLCAT.is_debug()) {
  2135. GLCAT.debug()
  2136. << "max clip planes = " << _max_clip_planes << "\n";
  2137. }
  2138. }
  2139. #endif
  2140. _max_texture_stages = 1;
  2141. #ifdef SUPPORT_FIXED_FUNCTION
  2142. if (supports_multitexture && !core_profile) {
  2143. GLint max_texture_stages = 0;
  2144. glGetIntegerv(GL_MAX_TEXTURE_UNITS, &max_texture_stages);
  2145. _max_texture_stages = max_texture_stages;
  2146. if (GLCAT.is_debug()) {
  2147. GLCAT.debug()
  2148. << "max texture stages = " << _max_texture_stages << "\n";
  2149. }
  2150. }
  2151. #endif
  2152. _current_vbuffer_index = 0;
  2153. _current_ibuffer_index = 0;
  2154. _current_vao_index = 0;
  2155. _current_fbo = 0;
  2156. _auto_antialias_mode = false;
  2157. _render_mode = RenderModeAttrib::M_filled;
  2158. _point_size = 1.0f;
  2159. _point_perspective = false;
  2160. report_my_gl_errors();
  2161. #ifdef SUPPORT_FIXED_FUNCTION
  2162. if (!core_profile) {
  2163. if (gl_cheap_textures) {
  2164. GLCAT.info()
  2165. << "Setting glHint() for fastest textures.\n";
  2166. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
  2167. }
  2168. // Use per-vertex fog if per-pixel fog requires SW renderer
  2169. glHint(GL_FOG_HINT, GL_DONT_CARE);
  2170. }
  2171. #endif
  2172. #ifdef SUPPORT_FIXED_FUNCTION
  2173. if (!core_profile) {
  2174. GLint num_red_bits = 0;
  2175. glGetIntegerv(GL_RED_BITS, &num_red_bits);
  2176. if (num_red_bits < 8) {
  2177. glEnable(GL_DITHER);
  2178. _dithering_enabled = true;
  2179. if (GLCAT.is_debug()) {
  2180. GLCAT.debug()
  2181. << "frame buffer depth = " << num_red_bits
  2182. << " bits/channel, enabling dithering\n";
  2183. }
  2184. }
  2185. }
  2186. #endif
  2187. _error_count = 0;
  2188. report_my_gl_errors();
  2189. #ifndef OPENGLES
  2190. if (_gl_shadlang_ver_major >= 4 || has_extension("GL_NV_gpu_program5")) {
  2191. // gp5fp - OpenGL fragment profile for GeForce 400 Series and up
  2192. _shader_model = SM_50;
  2193. } else if (_gl_shadlang_ver_major >= 3 ||
  2194. has_extension("GL_NV_gpu_program4")) {
  2195. // gp4fp - OpenGL fragment profile for G8x (GeForce 8xxx and up)
  2196. _shader_model = SM_40;
  2197. } else if (has_extension("GL_NV_fragment_program2")) {
  2198. // fp40 - OpenGL fragment profile for NV4x (GeForce 6xxx and 7xxx
  2199. // Series, NV4x-based Quadro FX, etc.)
  2200. _shader_model = SM_30;
  2201. } else if (has_extension("GL_NV_fragment_program")) {
  2202. // fp30 - OpenGL fragment profile for NV3x (GeForce FX, Quadro FX, etc.)
  2203. _shader_model = SM_2X;
  2204. } else if (_gl_shadlang_ver_major >= 1 ||
  2205. has_extension("GL_ARB_fragment_program")) {
  2206. // This OpenGL profile corresponds to the per-fragment
  2207. // functionality introduced by GeForce FX and other DirectX 9 GPUs.
  2208. _shader_model = SM_20;
  2209. } else if (has_extension("GL_NV_texture_shader2")) {
  2210. // fp20 - OpenGL fragment profile for NV2x (GeForce3, GeForce4 Ti,
  2211. // Quadro DCC, etc.)
  2212. _shader_model = SM_11;
  2213. } else {
  2214. // No shader support
  2215. _shader_model = SM_00;
  2216. }
  2217. // DisplayInformation may have better shader model detection
  2218. {
  2219. GraphicsPipe *pipe;
  2220. DisplayInformation *display_information;
  2221. pipe = this->get_pipe();
  2222. if (pipe) {
  2223. display_information = pipe->get_display_information ();
  2224. if (display_information) {
  2225. if (display_information->get_shader_model() > _shader_model) {
  2226. _shader_model = display_information->get_shader_model();
  2227. }
  2228. }
  2229. }
  2230. }
  2231. _auto_detect_shader_model = _shader_model;
  2232. if (GLCAT.is_debug()) {
  2233. #ifdef HAVE_CG
  2234. #if CG_VERSION_NUM >= 2200
  2235. GLCAT.debug() << "Supported Cg profiles:\n";
  2236. int num_profiles = cgGetNumSupportedProfiles();
  2237. for (int i = 0; i < num_profiles; ++i) {
  2238. CGprofile profile = cgGetSupportedProfile(i);
  2239. if (cgGLIsProfileSupported(profile)) {
  2240. GLCAT.debug() << " " << cgGetProfileString(profile) << "\n";
  2241. }
  2242. }
  2243. #endif // CG_VERSION_NUM >= 2200
  2244. #if CG_VERSION_NUM >= 3100
  2245. GLCAT.debug() << "Cg GLSL version = "
  2246. << cgGLGetGLSLVersionString(cgGLDetectGLSLVersion()) << "\n";
  2247. #endif
  2248. GLCAT.debug()
  2249. << "Cg latest vertex profile = "
  2250. << cgGetProfileString(cgGLGetLatestProfile(CG_GL_VERTEX)) << "\n";
  2251. GLCAT.debug()
  2252. << "Cg latest fragment profile = "
  2253. << cgGetProfileString(cgGLGetLatestProfile(CG_GL_FRAGMENT)) << "\n";
  2254. #if CG_VERSION_NUM >= 2000
  2255. GLCAT.debug()
  2256. << "Cg latest geometry profile = "
  2257. << cgGetProfileString(cgGLGetLatestProfile(CG_GL_GEOMETRY)) << "\n";
  2258. #endif
  2259. GLCAT.debug() << "basic-shaders-only " << basic_shaders_only << "\n";
  2260. GLCAT.debug()
  2261. << "Cg active vertex profile = "
  2262. << cgGetProfileString((CGprofile)_shader_caps._active_vprofile) << "\n";
  2263. GLCAT.debug()
  2264. << "Cg active fragment profile = "
  2265. << cgGetProfileString((CGprofile)_shader_caps._active_fprofile) << "\n";
  2266. GLCAT.debug()
  2267. << "Cg active geometry profile = "
  2268. << cgGetProfileString((CGprofile)_shader_caps._active_gprofile) << "\n";
  2269. #endif // HAVE_CG
  2270. GLCAT.debug() << "shader model = " << _shader_model << "\n";
  2271. }
  2272. #endif // !OPENGLES
  2273. // OpenGL core profile requires a VAO to be bound. It's a bit silly,
  2274. // because we can just bind a VAO and then forget about it.
  2275. #if !defined(OPENGLES)
  2276. if (core_profile) {
  2277. if (_supports_vao) {
  2278. _glGenVertexArrays(1, &_current_vao_index);
  2279. _glBindVertexArray(_current_vao_index);
  2280. } else {
  2281. GLCAT.error()
  2282. << "Core profile enabled, but vertex array objects not supported!\n";
  2283. }
  2284. }
  2285. #endif
  2286. // Now that the GSG has been initialized, make it available for
  2287. // optimizations.
  2288. add_gsg(this);
  2289. }
  2290. ////////////////////////////////////////////////////////////////////
  2291. // Function: GLGraphicsStateGuardian::finish
  2292. // Access: Public, Virtual
  2293. // Description: Force the graphics card to finish drawing before
  2294. // returning. !!!!!HACK WARNING!!!!
  2295. // glfinish does not actually wait for the graphics card to finish drawing
  2296. // only for draw calls to finish. Thus flip may not happene
  2297. // immediately. Instead we read a single pixel from
  2298. // the framebuffer. This forces the graphics card to
  2299. // finish drawing the frame before returning.
  2300. ////////////////////////////////////////////////////////////////////
  2301. void CLP(GraphicsStateGuardian)::
  2302. finish() {
  2303. // Rather than call glfinish which returns immediately if
  2304. // draw commands have been submitted, we will read a single pixel
  2305. // from the frame. That will force the graphics card to finish
  2306. // drawing before it is called
  2307. char data[4];
  2308. glReadPixels(0,0,1,1,GL_RGBA,GL_UNSIGNED_BYTE,&data);
  2309. //glFinish();
  2310. }
  2311. ////////////////////////////////////////////////////////////////////
  2312. // Function: GraphicsStateGuardian::clear
  2313. // Access: Public
  2314. // Description: Clears the framebuffer within the current
  2315. // DisplayRegion, according to the flags indicated by
  2316. // the given DrawableRegion object.
  2317. //
  2318. // This does not set the DisplayRegion first. You
  2319. // should call prepare_display_region() to specify the
  2320. // region you wish the clear operation to apply to.
  2321. ////////////////////////////////////////////////////////////////////
  2322. void CLP(GraphicsStateGuardian)::
  2323. clear(DrawableRegion *clearable) {
  2324. PStatGPUTimer timer(this, _clear_pcollector);
  2325. report_my_gl_errors();
  2326. if (!clearable->is_any_clear_active()) {
  2327. return;
  2328. }
  2329. //XXX rdb: Is this line really necessary?
  2330. set_state_and_transform(RenderState::make_empty(), _internal_transform);
  2331. int mask = 0;
  2332. #ifndef OPENGLES
  2333. if (_current_fbo != 0 && _glClearBufferfv != NULL) {
  2334. // We can use glClearBuffer to clear all the color attachments,
  2335. // which protects us from the overhead of having to call set_draw_buffer
  2336. // for every single attachment.
  2337. int index = 0;
  2338. if (_current_properties->get_color_bits() > 0) {
  2339. if (_current_properties->is_stereo()) {
  2340. // Clear both left and right attachments.
  2341. if (clearable->get_clear_active(GraphicsOutput::RTP_color)) {
  2342. LColorf v = LCAST(float, clearable->get_clear_value(GraphicsOutput::RTP_color));
  2343. _glClearBufferfv(GL_COLOR, index, v.get_data());
  2344. _glClearBufferfv(GL_COLOR, index + 1, v.get_data());
  2345. }
  2346. index += 2;
  2347. } else {
  2348. if (clearable->get_clear_active(GraphicsOutput::RTP_color)) {
  2349. LColorf v = LCAST(float, clearable->get_clear_value(GraphicsOutput::RTP_color));
  2350. _glClearBufferfv(GL_COLOR, index, v.get_data());
  2351. }
  2352. ++index;
  2353. }
  2354. }
  2355. for (int i = 0; i < _current_properties->get_aux_rgba(); ++i) {
  2356. int layerid = GraphicsOutput::RTP_aux_rgba_0 + i;
  2357. if (clearable->get_clear_active(layerid)) {
  2358. LColorf v = LCAST(float, clearable->get_clear_value(layerid));
  2359. _glClearBufferfv(GL_COLOR, index, v.get_data());
  2360. }
  2361. ++index;
  2362. }
  2363. for (int i = 0; i < _current_properties->get_aux_hrgba(); ++i) {
  2364. int layerid = GraphicsOutput::RTP_aux_hrgba_0 + i;
  2365. if (clearable->get_clear_active(layerid)) {
  2366. LColorf v = LCAST(float, clearable->get_clear_value(layerid));
  2367. _glClearBufferfv(GL_COLOR, index, v.get_data());
  2368. }
  2369. ++index;
  2370. }
  2371. for (int i = 0; i < _current_properties->get_aux_float(); ++i) {
  2372. int layerid = GraphicsOutput::RTP_aux_float_0 + i;
  2373. if (clearable->get_clear_active(layerid)) {
  2374. LColorf v = LCAST(float, clearable->get_clear_value(layerid));
  2375. _glClearBufferfv(GL_COLOR, index, v.get_data());
  2376. }
  2377. ++index;
  2378. }
  2379. } else
  2380. #endif
  2381. {
  2382. if (_current_properties->get_aux_mask() != 0) {
  2383. for (int i = 0; i < _current_properties->get_aux_rgba(); ++i) {
  2384. int layerid = GraphicsOutput::RTP_aux_rgba_0 + i;
  2385. int layerbit = RenderBuffer::T_aux_rgba_0 << i;
  2386. if (clearable->get_clear_active(layerid)) {
  2387. LColor v = clearable->get_clear_value(layerid);
  2388. glClearColor(v[0], v[1], v[2], v[3]);
  2389. set_draw_buffer(layerbit);
  2390. glClear(GL_COLOR_BUFFER_BIT);
  2391. }
  2392. }
  2393. for (int i = 0; i < _current_properties->get_aux_hrgba(); ++i) {
  2394. int layerid = GraphicsOutput::RTP_aux_hrgba_0 + i;
  2395. int layerbit = RenderBuffer::T_aux_hrgba_0 << i;
  2396. if (clearable->get_clear_active(layerid)) {
  2397. LColor v = clearable->get_clear_value(layerid);
  2398. glClearColor(v[0], v[1], v[2], v[3]);
  2399. set_draw_buffer(layerbit);
  2400. glClear(GL_COLOR_BUFFER_BIT);
  2401. }
  2402. }
  2403. for (int i = 0; i < _current_properties->get_aux_float(); ++i) {
  2404. int layerid = GraphicsOutput::RTP_aux_float_0 + i;
  2405. int layerbit = RenderBuffer::T_aux_float_0 << i;
  2406. if (clearable->get_clear_active(layerid)) {
  2407. LColor v = clearable->get_clear_value(layerid);
  2408. glClearColor(v[0], v[1], v[2], v[3]);
  2409. set_draw_buffer(layerbit);
  2410. glClear(GL_COLOR_BUFFER_BIT);
  2411. }
  2412. }
  2413. // In the past, it was possible to set the draw buffer
  2414. // once in prepare_display_region and then forget about it.
  2415. // Now, with aux layers, it is necessary to occasionally
  2416. // change the draw buffer. In time, I think there will need
  2417. // to be a draw buffer attrib. Until then, this little hack
  2418. // to put things back the way they were after
  2419. // prepare_display_region will do.
  2420. set_draw_buffer(_draw_buffer_type);
  2421. }
  2422. if (_current_properties->get_color_bits() > 0) {
  2423. if (clearable->get_clear_color_active()) {
  2424. LColor v = clearable->get_clear_color();
  2425. glClearColor(v[0], v[1], v[2], v[3]);
  2426. clear_color_write_mask();
  2427. _state_mask.clear_bit(ColorWriteAttrib::get_class_slot());
  2428. mask |= GL_COLOR_BUFFER_BIT;
  2429. }
  2430. }
  2431. }
  2432. if (clearable->get_clear_depth_active()) {
  2433. #ifdef OPENGLES
  2434. glClearDepthf(clearable->get_clear_depth());
  2435. #else
  2436. glClearDepth(clearable->get_clear_depth());
  2437. #endif // OPENGLES
  2438. #ifdef GSG_VERBOSE
  2439. GLCAT.spam()
  2440. << "glDepthMask(GL_TRUE)" << endl;
  2441. #endif
  2442. glDepthMask(GL_TRUE);
  2443. _state_mask.clear_bit(DepthWriteAttrib::get_class_slot());
  2444. mask |= GL_DEPTH_BUFFER_BIT;
  2445. }
  2446. if (clearable->get_clear_stencil_active()) {
  2447. glStencilMask(~0);
  2448. glClearStencil(clearable->get_clear_stencil());
  2449. mask |= GL_STENCIL_BUFFER_BIT;
  2450. }
  2451. glClear(mask);
  2452. if (GLCAT.is_spam()) {
  2453. GLCAT.spam() << "glClear(";
  2454. if (mask & GL_COLOR_BUFFER_BIT) {
  2455. GLCAT.spam(false) << "GL_COLOR_BUFFER_BIT|";
  2456. }
  2457. if (mask & GL_DEPTH_BUFFER_BIT) {
  2458. GLCAT.spam(false) << "GL_DEPTH_BUFFER_BIT|";
  2459. }
  2460. if (mask & GL_STENCIL_BUFFER_BIT) {
  2461. GLCAT.spam(false) << "GL_STENCIL_BUFFER_BIT|";
  2462. }
  2463. #ifndef OPENGLES
  2464. if (mask & GL_ACCUM_BUFFER_BIT) {
  2465. GLCAT.spam(false) << "GL_ACCUM_BUFFER_BIT|";
  2466. }
  2467. #endif
  2468. GLCAT.spam(false) << ")" << endl;
  2469. }
  2470. report_my_gl_errors();
  2471. }
  2472. ////////////////////////////////////////////////////////////////////
  2473. // Function: GLGraphicsStateGuardian::prepare_display_region
  2474. // Access: Public, Virtual
  2475. // Description: Prepare a display region for rendering (set up
  2476. // scissor region and viewport)
  2477. ////////////////////////////////////////////////////////////////////
  2478. void CLP(GraphicsStateGuardian)::
  2479. prepare_display_region(DisplayRegionPipelineReader *dr) {
  2480. nassertv(dr != (DisplayRegionPipelineReader *)NULL);
  2481. GraphicsStateGuardian::prepare_display_region(dr);
  2482. int l, b, w, h;
  2483. dr->get_region_pixels(l, b, w, h);
  2484. _viewport_x = l;
  2485. _viewport_y = b;
  2486. _viewport_width = w;
  2487. _viewport_height = h;
  2488. GLint x = GLint(l);
  2489. GLint y = GLint(b);
  2490. GLsizei width = GLsizei(w);
  2491. GLsizei height = GLsizei(h);
  2492. _draw_buffer_type = dr->get_object()->get_draw_buffer_type() & _current_properties->get_buffer_mask() & _stereo_buffer_mask;
  2493. _draw_buffer_type |= _current_properties->get_aux_mask();
  2494. set_draw_buffer(_draw_buffer_type);
  2495. int count = dr->get_num_regions();
  2496. if (dr->get_scissor_enabled()) {
  2497. if (GLCAT.is_spam()) {
  2498. GLCAT.spam()
  2499. << "glEnable(GL_SCISSOR_TEST)\n";
  2500. }
  2501. glEnable(GL_SCISSOR_TEST);
  2502. _scissor_enabled = true;
  2503. _scissor_array.resize(count);
  2504. } else {
  2505. if (GLCAT.is_spam()) {
  2506. GLCAT.spam()
  2507. << "glDisable(GL_SCISSOR_TEST)\n";
  2508. }
  2509. glDisable(GL_SCISSOR_TEST);
  2510. _scissor_enabled = false;
  2511. _scissor_array.clear();
  2512. }
  2513. _scissor_attrib_active = false;
  2514. #ifndef OPENGLES
  2515. if (_supports_viewport_arrays) {
  2516. GLfloat *viewports = (GLfloat *)alloca(sizeof(GLfloat) * 4 * count);
  2517. // We store the scissor regions in a vector since we may need
  2518. // to switch back to it in do_issue_scissor.
  2519. for (int i = 0; i < count; ++i) {
  2520. LVecBase4i sr;
  2521. dr->get_region_pixels(i, sr[0], sr[1], sr[2], sr[3]);
  2522. GLfloat *vr = viewports + i * 4;
  2523. vr[0] = (GLfloat) sr[0];
  2524. vr[1] = (GLfloat) sr[1];
  2525. vr[2] = (GLfloat) sr[2];
  2526. vr[3] = (GLfloat) sr[3];
  2527. if (_scissor_enabled) {
  2528. _scissor_array[i] = sr;
  2529. }
  2530. }
  2531. _glViewportArrayv(0, count, viewports);
  2532. if (_scissor_enabled) {
  2533. _glScissorArrayv(0, count, _scissor_array[0].get_data());
  2534. }
  2535. if (GLCAT.is_spam()) {
  2536. GLCAT.spam()
  2537. << "glViewportArrayv(0, " << count << ", [\n";
  2538. for (int i = 0; i < count; ++i) {
  2539. GLfloat *vr = viewports + i * 4;
  2540. GLCAT.spam(false) << vr[0] << ", " << vr[1] << ", " << vr[2] << ", " << vr[3] << ",\n";
  2541. }
  2542. GLCAT.spam(false) << "])\n";
  2543. if (_scissor_enabled) {
  2544. GLCAT.spam()
  2545. << "glScissorArrayv(0, " << count << ", [\n";
  2546. for (int i = 0; i < count; ++i) {
  2547. const LVecBase4i &sr = _scissor_array[i];
  2548. GLCAT.spam(false) << sr << ",\n";
  2549. }
  2550. }
  2551. GLCAT.spam(false) << "])\n";
  2552. }
  2553. } else
  2554. #endif // OPENGLES
  2555. {
  2556. glViewport(x, y, width, height);
  2557. if (_scissor_enabled) {
  2558. glScissor(x, y, width, height);
  2559. _scissor_array.resize(1);
  2560. _scissor_array[0].set(x, y, width, height);
  2561. }
  2562. if (GLCAT.is_spam()) {
  2563. GLCAT.spam()
  2564. << "glViewport(" << x << ", " << y << ", " << width << ", " << height << ")\n";
  2565. if (dr->get_scissor_enabled()) {
  2566. GLCAT.spam()
  2567. << "glScissor(" << x << ", " << y << ", " << width << ", " << height << ")\n";
  2568. }
  2569. }
  2570. }
  2571. report_my_gl_errors();
  2572. do_point_size();
  2573. }
  2574. ////////////////////////////////////////////////////////////////////
  2575. // Function: GLGraphicsStateGuardian::clear_before_callback
  2576. // Access: Public, Virtual
  2577. // Description: Resets any non-standard graphics state that might
  2578. // give a callback apoplexy. Some drivers require that
  2579. // the graphics state be restored to neutral before
  2580. // performing certain operations. In OpenGL, for
  2581. // instance, this closes any open vertex buffers.
  2582. ////////////////////////////////////////////////////////////////////
  2583. void CLP(GraphicsStateGuardian)::
  2584. clear_before_callback() {
  2585. #ifdef SUPPORT_FIXED_FUNCTION
  2586. disable_standard_vertex_arrays();
  2587. #endif
  2588. unbind_buffers();
  2589. // Some callbacks may quite reasonably assume that the active
  2590. // texture stage is still set to stage 0. CEGUI, in particular,
  2591. // makes this assumption.
  2592. _glActiveTexture(GL_TEXTURE0);
  2593. #ifdef SUPPORT_FIXED_FUNCTION
  2594. _glClientActiveTexture(GL_TEXTURE0);
  2595. #endif
  2596. // Clear the bound sampler object, so that we do not inadvertently
  2597. // override the callback's desired sampler settings.
  2598. #ifndef OPENGLES
  2599. if (_supports_sampler_objects) {
  2600. _glBindSampler(0, 0);
  2601. }
  2602. #endif
  2603. }
  2604. ////////////////////////////////////////////////////////////////////
  2605. // Function: GLGraphicsStateGuardian::calc_projection_mat
  2606. // Access: Public, Virtual
  2607. // Description: Given a lens, calculates the appropriate projection
  2608. // matrix for use with this gsg. Note that the
  2609. // projection matrix depends a lot upon the coordinate
  2610. // system of the rendering API.
  2611. //
  2612. // The return value is a TransformState if the lens is
  2613. // acceptable, NULL if it is not.
  2614. ////////////////////////////////////////////////////////////////////
  2615. CPT(TransformState) CLP(GraphicsStateGuardian)::
  2616. calc_projection_mat(const Lens *lens) {
  2617. if (lens == (Lens *)NULL) {
  2618. return NULL;
  2619. }
  2620. if (!lens->is_linear()) {
  2621. return NULL;
  2622. }
  2623. // The projection matrix must always be right-handed Y-up, even if
  2624. // our coordinate system of choice is otherwise, because certain GL
  2625. // calls (specifically glTexGen(GL_SPHERE_MAP)) assume this kind of
  2626. // a coordinate system. Sigh. In order to implement a Z-up (or
  2627. // other arbitrary) coordinate system, we'll use a Y-up projection
  2628. // matrix, and store the conversion to our coordinate system of
  2629. // choice in the modelview matrix.
  2630. LMatrix4 result =
  2631. LMatrix4::convert_mat(_internal_coordinate_system,
  2632. lens->get_coordinate_system()) *
  2633. lens->get_projection_mat(_current_stereo_channel);
  2634. if (_scene_setup->get_inverted()) {
  2635. // If the scene is supposed to be inverted, then invert the
  2636. // projection matrix.
  2637. result *= LMatrix4::scale_mat(1.0f, -1.0f, 1.0f);
  2638. }
  2639. return TransformState::make_mat(result);
  2640. }
  2641. ////////////////////////////////////////////////////////////////////
  2642. // Function: GLGraphicsStateGuardian::prepare_lens
  2643. // Access: Public, Virtual
  2644. // Description: Makes the current lens (whichever lens was most
  2645. // recently specified with set_scene()) active, so
  2646. // that it will transform future rendered geometry.
  2647. // Normally this is only called from the draw process,
  2648. // and usually it is called by set_scene().
  2649. //
  2650. // The return value is true if the lens is acceptable,
  2651. // false if it is not.
  2652. ////////////////////////////////////////////////////////////////////
  2653. bool CLP(GraphicsStateGuardian)::
  2654. prepare_lens() {
  2655. #ifdef SUPPORT_FIXED_FUNCTION
  2656. if (GLCAT.is_spam()) {
  2657. GLCAT.spam()
  2658. << "glMatrixMode(GL_PROJECTION): " << _projection_mat->get_mat() << endl;
  2659. }
  2660. glMatrixMode(GL_PROJECTION);
  2661. GLPf(LoadMatrix)(_projection_mat->get_mat().get_data());
  2662. report_my_gl_errors();
  2663. do_point_size();
  2664. #endif
  2665. #ifndef OPENGLES_1
  2666. if (_current_shader_context) {
  2667. _current_shader_context->issue_parameters(Shader::SSD_transform);
  2668. }
  2669. #endif
  2670. return true;
  2671. }
  2672. ////////////////////////////////////////////////////////////////////
  2673. // Function: GraphicsStateGuardian::begin_frame
  2674. // Access: Public, Virtual
  2675. // Description: Called before each frame is rendered, to allow the
  2676. // GSG a chance to do any internal cleanup before
  2677. // beginning the frame.
  2678. //
  2679. // The return value is true if successful (in which case
  2680. // the frame will be drawn and end_frame() will be
  2681. // called later), or false if unsuccessful (in which
  2682. // case nothing will be drawn and end_frame() will not
  2683. // be called).
  2684. ////////////////////////////////////////////////////////////////////
  2685. bool CLP(GraphicsStateGuardian)::
  2686. begin_frame(Thread *current_thread) {
  2687. if (!GraphicsStateGuardian::begin_frame(current_thread)) {
  2688. return false;
  2689. }
  2690. _renderbuffer_residency.begin_frame(current_thread);
  2691. report_my_gl_errors();
  2692. #ifdef DO_PSTATS
  2693. _vertices_display_list_pcollector.clear_level();
  2694. _vertices_immediate_pcollector.clear_level();
  2695. _primitive_batches_display_list_pcollector.clear_level();
  2696. #endif
  2697. #ifndef NDEBUG
  2698. _show_texture_usage = false;
  2699. if (gl_show_texture_usage) {
  2700. // When this is true, then every other second, we show the usage
  2701. // textures instead of the real textures.
  2702. double now = ClockObject::get_global_clock()->get_frame_time();
  2703. int this_second = (int)floor(now);
  2704. if (this_second & 1) {
  2705. _show_texture_usage = true;
  2706. _show_texture_usage_index = this_second >> 1;
  2707. int max_size = gl_show_texture_usage_max_size;
  2708. if (max_size != _show_texture_usage_max_size) {
  2709. // Remove the cache of usage textures; we've changed the max
  2710. // size.
  2711. UsageTextures::iterator ui;
  2712. for (ui = _usage_textures.begin();
  2713. ui != _usage_textures.end();
  2714. ++ui) {
  2715. GLuint index = (*ui).second;
  2716. glDeleteTextures(1, &index);
  2717. }
  2718. _usage_textures.clear();
  2719. _show_texture_usage_max_size = max_size;
  2720. }
  2721. }
  2722. }
  2723. #endif // NDEBUG
  2724. #ifdef DO_PSTATS
  2725. /*if (_supports_timer_query) {
  2726. // Measure the difference between the OpenGL clock and the
  2727. // PStats clock.
  2728. GLint64 time_ns;
  2729. _glGetInteger64v(GL_TIMESTAMP, &time_ns);
  2730. _timer_delta = time_ns * -0.000000001;
  2731. _timer_delta += PStatClient::get_global_pstats()->get_real_time();
  2732. }*/
  2733. #endif
  2734. #ifndef OPENGLES
  2735. if (_current_properties->get_srgb_color()) {
  2736. glEnable(GL_FRAMEBUFFER_SRGB);
  2737. }
  2738. #endif
  2739. report_my_gl_errors();
  2740. return true;
  2741. }
  2742. ////////////////////////////////////////////////////////////////////
  2743. // Function: GraphicsStateGuardian::begin_scene
  2744. // Access: Public, Virtual
  2745. // Description: Called between begin_frame() and end_frame() to mark
  2746. // the beginning of drawing commands for a "scene"
  2747. // (usually a particular DisplayRegion) within a frame.
  2748. // All 3-D drawing commands, except the clear operation,
  2749. // must be enclosed within begin_scene() .. end_scene().
  2750. //
  2751. // The return value is true if successful (in which case
  2752. // the scene will be drawn and end_scene() will be
  2753. // called later), or false if unsuccessful (in which
  2754. // case nothing will be drawn and end_scene() will not
  2755. // be called).
  2756. ////////////////////////////////////////////////////////////////////
  2757. bool CLP(GraphicsStateGuardian)::
  2758. begin_scene() {
  2759. return GraphicsStateGuardian::begin_scene();
  2760. }
  2761. ////////////////////////////////////////////////////////////////////
  2762. // Function: GLGraphicsStateGuardian::end_scene
  2763. // Access: Protected, Virtual
  2764. // Description: Called between begin_frame() and end_frame() to mark
  2765. // the end of drawing commands for a "scene" (usually a
  2766. // particular DisplayRegion) within a frame. All 3-D
  2767. // drawing commands, except the clear operation, must be
  2768. // enclosed within begin_scene() .. end_scene().
  2769. ////////////////////////////////////////////////////////////////////
  2770. void CLP(GraphicsStateGuardian)::
  2771. end_scene() {
  2772. GraphicsStateGuardian::end_scene();
  2773. _dlights.clear();
  2774. report_my_gl_errors();
  2775. }
  2776. ////////////////////////////////////////////////////////////////////
  2777. // Function: GLGraphicsStateGuardian::end_frame
  2778. // Access: Public, Virtual
  2779. // Description: Called after each frame is rendered, to allow the
  2780. // GSG a chance to do any internal cleanup after
  2781. // rendering the frame, and before the window flips.
  2782. ////////////////////////////////////////////////////////////////////
  2783. void CLP(GraphicsStateGuardian)::
  2784. end_frame(Thread *current_thread) {
  2785. report_my_gl_errors();
  2786. #ifndef OPENGLES
  2787. if (_current_properties->get_srgb_color()) {
  2788. glDisable(GL_FRAMEBUFFER_SRGB);
  2789. }
  2790. #endif
  2791. #ifdef DO_PSTATS
  2792. // Check for textures, etc., that are no longer resident. These
  2793. // calls might be measurably expensive, and they don't have any
  2794. // benefit unless we are actually viewing PStats, so don't do them
  2795. // unless we're connected. That will just mean that we'll count
  2796. // everything as resident until the user connects PStats, at which
  2797. // point it will then correct the assessment. No harm done.
  2798. if (PStatClient::is_connected()) {
  2799. check_nonresident_texture(_prepared_objects->_texture_residency.get_inactive_resident());
  2800. check_nonresident_texture(_prepared_objects->_texture_residency.get_active_resident());
  2801. // OpenGL provides no methods for querying whether a buffer object
  2802. // (vertex buffer) is resident. In fact, the API appears geared
  2803. // towards the assumption that such buffers are always resident.
  2804. // OK.
  2805. }
  2806. #endif
  2807. #ifndef OPENGLES_1
  2808. // This breaks shaders across multiple regions.
  2809. if (_vertex_array_shader_context != 0) {
  2810. _vertex_array_shader_context->disable_shader_vertex_arrays();
  2811. _vertex_array_shader = (Shader *)NULL;
  2812. _vertex_array_shader_context = (ShaderContext *)NULL;
  2813. }
  2814. if (_texture_binding_shader_context != 0) {
  2815. _texture_binding_shader_context->disable_shader_texture_bindings();
  2816. _texture_binding_shader = (Shader *)NULL;
  2817. _texture_binding_shader_context = (ShaderContext *)NULL;
  2818. }
  2819. if (_current_shader_context != 0) {
  2820. _current_shader_context->unbind();
  2821. _current_shader = (Shader *)NULL;
  2822. _current_shader_context = (ShaderContext *)NULL;
  2823. }
  2824. #endif
  2825. // Calling glFlush() at the end of the frame is particularly
  2826. // necessary if this is a single-buffered visual, so that the frame
  2827. // will be finished drawing before we return to the application.
  2828. // It's not clear what effect this has on our total frame time.
  2829. //if (_force_flush || _current_properties->is_single_buffered()) {
  2830. // gl_flush();
  2831. //}
  2832. maybe_gl_finish();
  2833. GraphicsStateGuardian::end_frame(current_thread);
  2834. _renderbuffer_residency.end_frame(current_thread);
  2835. // Flush any PCollectors specific to this kind of GSG.
  2836. _primitive_batches_display_list_pcollector.flush_level();
  2837. _vertices_display_list_pcollector.flush_level();
  2838. _vertices_immediate_pcollector.flush_level();
  2839. // Now is a good time to delete any pending display lists.
  2840. #ifndef OPENGLES
  2841. #ifdef SUPPORT_FIXED_FUNCTION
  2842. if (display_lists) {
  2843. LightMutexHolder holder(_lock);
  2844. if (!_deleted_display_lists.empty()) {
  2845. DeletedNames::iterator ddli;
  2846. for (ddli = _deleted_display_lists.begin();
  2847. ddli != _deleted_display_lists.end();
  2848. ++ddli) {
  2849. if (GLCAT.is_debug()) {
  2850. GLCAT.debug()
  2851. << "releasing display list index " << (int)(*ddli) << "\n";
  2852. }
  2853. glDeleteLists((*ddli), 1);
  2854. }
  2855. _deleted_display_lists.clear();
  2856. }
  2857. }
  2858. #endif
  2859. // And deleted queries, too, unless we're using query timers
  2860. // in which case we'll need to reuse lots of them.
  2861. if (_supports_occlusion_query && !get_timer_queries_active()) {
  2862. LightMutexHolder holder(_lock);
  2863. if (!_deleted_queries.empty()) {
  2864. if (GLCAT.is_spam()) {
  2865. DeletedNames::iterator dqi;
  2866. for (dqi = _deleted_queries.begin();
  2867. dqi != _deleted_queries.end();
  2868. ++dqi) {
  2869. GLCAT.spam()
  2870. << "releasing query index " << (int)(*dqi) << "\n";
  2871. }
  2872. }
  2873. _glDeleteQueries(_deleted_queries.size(), &_deleted_queries[0]);
  2874. _deleted_queries.clear();
  2875. }
  2876. }
  2877. #endif // OPENGLES
  2878. #ifndef NDEBUG
  2879. if (_check_errors || (_supports_debug && gl_debug)) {
  2880. report_my_gl_errors();
  2881. } else {
  2882. static int frame_counter = -1;
  2883. // If _check_errors is false, we still want to check for errors
  2884. // the first few frames and once every N frames, so that we know if
  2885. // anything went wrong at all.
  2886. if (frame_counter++ <= 0) {
  2887. PStatTimer timer(_check_error_pcollector);
  2888. GLenum error_code = glGetError();
  2889. if (error_code != GL_NO_ERROR) {
  2890. int error_count = 0;
  2891. bool deactivate = !report_errors_loop(__LINE__, __FILE__, error_code, error_count);
  2892. if (error_count == 1) {
  2893. GLCAT.error()
  2894. << "An OpenGL error (" << get_error_string(error_code)
  2895. << ") has occurred.";
  2896. } else {
  2897. GLCAT.error()
  2898. << error_count << " OpenGL errors have occurred.";
  2899. }
  2900. if (_supports_debug) {
  2901. GLCAT.error(false) << " Set gl-debug #t "
  2902. << "in your PRC file to display more information.\n";
  2903. } else {
  2904. GLCAT.error(false) << " Set gl-check-errors #t "
  2905. << "in your PRC file to display more information.\n";
  2906. }
  2907. if (deactivate) {
  2908. panic_deactivate();
  2909. }
  2910. }
  2911. } else if (frame_counter > 100) {
  2912. // 100 frames have passed. Check next frame.
  2913. frame_counter = 0;
  2914. }
  2915. }
  2916. #endif
  2917. }
  2918. ////////////////////////////////////////////////////////////////////
  2919. // Function: GLGraphicsStateGuardian::begin_draw_primitives
  2920. // Access: Public, Virtual
  2921. // Description: Called before a sequence of draw_primitive()
  2922. // functions are called, this should prepare the vertex
  2923. // data for rendering. It returns true if the vertices
  2924. // are ok, false to abort this group of primitives.
  2925. ////////////////////////////////////////////////////////////////////
  2926. bool CLP(GraphicsStateGuardian)::
  2927. begin_draw_primitives(const GeomPipelineReader *geom_reader,
  2928. const GeomMunger *munger,
  2929. const GeomVertexDataPipelineReader *data_reader,
  2930. bool force) {
  2931. #ifndef NDEBUG
  2932. if (GLCAT.is_spam()) {
  2933. GLCAT.spam() << "begin_draw_primitives: " << *(data_reader->get_object()) << "\n";
  2934. }
  2935. #endif // NDEBUG
  2936. #ifndef SUPPORT_FIXED_FUNCTION
  2937. // We can't draw without a shader bound in OpenGL ES 2. This shouldn't
  2938. // happen anyway unless the default shader failed to compile somehow.
  2939. if (_current_shader_context == NULL) {
  2940. return false;
  2941. }
  2942. #endif
  2943. if (!GraphicsStateGuardian::begin_draw_primitives(geom_reader, munger, data_reader, force)) {
  2944. return false;
  2945. }
  2946. nassertr(_data_reader != (GeomVertexDataPipelineReader *)NULL, false);
  2947. _geom_display_list = 0;
  2948. if (_auto_antialias_mode) {
  2949. switch (geom_reader->get_primitive_type()) {
  2950. case GeomPrimitive::PT_polygons:
  2951. case GeomPrimitive::PT_patches:
  2952. setup_antialias_polygon();
  2953. break;
  2954. case GeomPrimitive::PT_points:
  2955. setup_antialias_point();
  2956. break;
  2957. case GeomPrimitive::PT_lines:
  2958. setup_antialias_line();
  2959. break;
  2960. case GeomPrimitive::PT_none:
  2961. break;
  2962. }
  2963. int transparency_slot = TransparencyAttrib::get_class_slot();
  2964. int color_write_slot = ColorWriteAttrib::get_class_slot();
  2965. int color_blend_slot = ColorBlendAttrib::get_class_slot();
  2966. if (!_state_mask.get_bit(transparency_slot) ||
  2967. !_state_mask.get_bit(color_write_slot) ||
  2968. !_state_mask.get_bit(color_blend_slot)) {
  2969. do_issue_blending();
  2970. _state_mask.set_bit(transparency_slot);
  2971. _state_mask.set_bit(color_write_slot);
  2972. _state_mask.set_bit(color_blend_slot);
  2973. }
  2974. }
  2975. #ifdef SUPPORT_FIXED_FUNCTION
  2976. if (_data_reader->is_vertex_transformed()) {
  2977. // If the vertex data claims to be already transformed into clip
  2978. // coordinates, wipe out the current projection and modelview
  2979. // matrix (so we don't attempt to transform it again).
  2980. glMatrixMode(GL_PROJECTION);
  2981. glPushMatrix();
  2982. glLoadIdentity();
  2983. glMatrixMode(GL_MODELVIEW);
  2984. glPushMatrix();
  2985. glLoadIdentity();
  2986. }
  2987. #endif
  2988. #if !defined(OPENGLES) && defined(SUPPORT_FIXED_FUNCTION) // Display lists not supported by OpenGL ES.
  2989. if (geom_reader->get_usage_hint() == Geom::UH_static &&
  2990. _data_reader->get_usage_hint() == Geom::UH_static &&
  2991. display_lists) {
  2992. // If the geom claims to be totally static, try to build it into
  2993. // a display list.
  2994. // Before we compile or call a display list, make sure the current
  2995. // buffers are unbound, or the nVidia drivers may crash.
  2996. unbind_buffers();
  2997. GeomContext *gc = geom_reader->prepare_now(get_prepared_objects(), this);
  2998. nassertr(gc != (GeomContext *)NULL, false);
  2999. CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc);
  3000. const CLP(GeomMunger) *gmunger = DCAST(CLP(GeomMunger), _munger);
  3001. UpdateSeq modified = max(geom_reader->get_modified(), _data_reader->get_modified());
  3002. if (ggc->get_display_list(_geom_display_list, gmunger, modified)) {
  3003. // If it hasn't been modified, just play the display list again.
  3004. if (GLCAT.is_spam()) {
  3005. GLCAT.spam()
  3006. << "calling display list " << (int)_geom_display_list << "\n";
  3007. }
  3008. glCallList(_geom_display_list);
  3009. #ifdef DO_PSTATS
  3010. _vertices_display_list_pcollector.add_level(ggc->_num_verts);
  3011. _primitive_batches_display_list_pcollector.add_level(1);
  3012. #endif
  3013. // And now we don't need to do anything else for this geom.
  3014. _geom_display_list = 0;
  3015. end_draw_primitives();
  3016. return false;
  3017. }
  3018. // Since we start this collector explicitly, we have to be sure to
  3019. // stop it again.
  3020. _load_display_list_pcollector.start();
  3021. if (GLCAT.is_debug()) {
  3022. GLCAT.debug()
  3023. << "compiling display list " << (int)_geom_display_list << "\n";
  3024. }
  3025. // If it has been modified, or this is the first time, then we
  3026. // need to build the display list up.
  3027. if (gl_compile_and_execute) {
  3028. glNewList(_geom_display_list, GL_COMPILE_AND_EXECUTE);
  3029. } else {
  3030. glNewList(_geom_display_list, GL_COMPILE);
  3031. }
  3032. #ifdef DO_PSTATS
  3033. // Count up the number of vertices used by primitives in the Geom,
  3034. // for PStats reporting.
  3035. ggc->_num_verts = 0;
  3036. for (int i = 0; i < geom_reader->get_num_primitives(); i++) {
  3037. ggc->_num_verts += geom_reader->get_primitive(i)->get_num_vertices();
  3038. }
  3039. #endif
  3040. }
  3041. #endif // OPENGLES
  3042. // Enable the appropriate vertex arrays, and disable any
  3043. // extra vertex arrays used by the previous rendering mode.
  3044. #ifdef SUPPORT_IMMEDIATE_MODE
  3045. _use_sender = !vertex_arrays;
  3046. #endif
  3047. {
  3048. //PStatGPUTimer timer(this, _vertex_array_update_pcollector);
  3049. #ifdef OPENGLES_1
  3050. if (!update_standard_vertex_arrays(force)) {
  3051. return false;
  3052. }
  3053. #else
  3054. if (_current_shader_context == 0) {
  3055. // No shader.
  3056. if (_vertex_array_shader_context != 0) {
  3057. _vertex_array_shader_context->disable_shader_vertex_arrays();
  3058. }
  3059. #ifdef SUPPORT_FIXED_FUNCTION
  3060. if (!update_standard_vertex_arrays(force)) {
  3061. return false;
  3062. }
  3063. #endif
  3064. } else {
  3065. #ifdef SUPPORT_FIXED_FUNCTION
  3066. // Shader.
  3067. if (_vertex_array_shader_context == 0 || _vertex_array_shader_context->uses_standard_vertex_arrays()) {
  3068. // Previous shader used standard arrays.
  3069. if (_current_shader_context->uses_standard_vertex_arrays()) {
  3070. // So does the current, so update them.
  3071. if (!update_standard_vertex_arrays(force)) {
  3072. return false;
  3073. }
  3074. } else {
  3075. // The current shader does not, so disable them entirely.
  3076. disable_standard_vertex_arrays();
  3077. }
  3078. }
  3079. #endif // SUPPORT_FIXED_FUNCTION
  3080. if (_current_shader_context->uses_custom_vertex_arrays()) {
  3081. // The current shader also uses custom vertex arrays.
  3082. if (!_current_shader_context->
  3083. update_shader_vertex_arrays(_vertex_array_shader_context, force)) {
  3084. return false;
  3085. }
  3086. } else {
  3087. _vertex_array_shader_context->disable_shader_vertex_arrays();
  3088. }
  3089. }
  3090. _vertex_array_shader = _current_shader;
  3091. _vertex_array_shader_context = _current_shader_context;
  3092. #endif // OPENGLES_1
  3093. }
  3094. report_my_gl_errors();
  3095. return true;
  3096. }
  3097. #ifdef SUPPORT_FIXED_FUNCTION
  3098. ////////////////////////////////////////////////////////////////////
  3099. // Function: GLGraphicsStateGuardian::update_standard_vertex_arrays
  3100. // Access: Protected
  3101. // Description: Disables any unneeded vertex arrays that
  3102. // were previously enabled, and enables any vertex
  3103. // arrays that are needed that were not previously
  3104. // enabled (or, sets up an immediate-mode sender).
  3105. // Called only from begin_draw_primitives.
  3106. // Used only when the standard (non-shader) pipeline
  3107. // is about to be used - glShaderContexts are responsible
  3108. // for setting up their own vertex arrays.
  3109. ////////////////////////////////////////////////////////////////////
  3110. bool CLP(GraphicsStateGuardian)::
  3111. update_standard_vertex_arrays(bool force) {
  3112. #ifdef SUPPORT_IMMEDIATE_MODE
  3113. if (_use_sender) {
  3114. // We must use immediate mode to render primitives.
  3115. _sender.clear();
  3116. _sender.add_column(_data_reader, InternalName::get_normal(),
  3117. NULL, NULL, GLPf(Normal3), NULL);
  3118. #ifndef NDEBUG
  3119. if (_show_texture_usage) {
  3120. // In show_texture_usage mode, all colors are white, so as not
  3121. // to contaminate the texture color.
  3122. GLPf(Color4)(1.0f, 1.0f, 1.0f, 1.0f);
  3123. } else
  3124. #endif // NDEBUG
  3125. if (!_sender.add_column(_data_reader, InternalName::get_color(),
  3126. NULL, NULL, GLPf(Color3), GLPf(Color4))) {
  3127. // If we didn't have a color column, the item color is white.
  3128. GLPf(Color4)(1.0f, 1.0f, 1.0f, 1.0f);
  3129. }
  3130. // Now set up each of the active texture coordinate stages--or at
  3131. // least those for which we're not generating texture coordinates
  3132. // automatically.
  3133. int max_stage_index = _target_texture->get_num_on_ff_stages();
  3134. int stage_index = 0;
  3135. while (stage_index < max_stage_index) {
  3136. TextureStage *stage = _target_texture->get_on_ff_stage(stage_index);
  3137. if (!_target_tex_gen->has_gen_texcoord_stage(stage)) {
  3138. // This stage is not one of the stages that doesn't need
  3139. // texcoords issued for it.
  3140. const InternalName *name = stage->get_texcoord_name();
  3141. if (stage_index == 0) {
  3142. // Use the original functions for stage 0, in case we don't
  3143. // support multitexture.
  3144. _sender.add_column(_data_reader, name,
  3145. GLPf(TexCoord1), GLPf(TexCoord2),
  3146. GLPf(TexCoord3), GLPf(TexCoord4));
  3147. } else {
  3148. // Other stages require the multitexture functions.
  3149. _sender.add_texcoord_column(_data_reader, name, stage_index,
  3150. GLf(_glMultiTexCoord1), GLf(_glMultiTexCoord2),
  3151. GLf(_glMultiTexCoord3), GLf(_glMultiTexCoord4));
  3152. }
  3153. }
  3154. ++stage_index;
  3155. }
  3156. // Be sure also to disable any texture stages we had enabled before.
  3157. while (stage_index < _last_max_stage_index) {
  3158. _glClientActiveTexture(GL_TEXTURE0 + stage_index);
  3159. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  3160. ++stage_index;
  3161. }
  3162. _last_max_stage_index = max_stage_index;
  3163. // We must add vertex last, because glVertex3f() is the key
  3164. // function call that actually issues the vertex.
  3165. _sender.add_column(_data_reader, InternalName::get_vertex(),
  3166. NULL, GLPf(Vertex2), GLPf(Vertex3), GLPf(Vertex4));
  3167. } else
  3168. #endif // SUPPORT_IMMEDIATE_MODE
  3169. {
  3170. // We may use vertex arrays or buffers to render primitives.
  3171. const GeomVertexArrayDataHandle *array_reader;
  3172. const unsigned char *client_pointer;
  3173. int num_values;
  3174. Geom::NumericType numeric_type;
  3175. int start;
  3176. int stride;
  3177. if (_data_reader->get_normal_info(array_reader, numeric_type,
  3178. start, stride)) {
  3179. if (!setup_array_data(client_pointer, array_reader, force)) {
  3180. return false;
  3181. }
  3182. glNormalPointer(get_numeric_type(numeric_type), stride,
  3183. client_pointer + start);
  3184. glEnableClientState(GL_NORMAL_ARRAY);
  3185. } else {
  3186. glDisableClientState(GL_NORMAL_ARRAY);
  3187. }
  3188. #ifndef NDEBUG
  3189. if (_show_texture_usage) {
  3190. // In show_texture_usage mode, all colors are white, so as not
  3191. // to contaminate the texture color.
  3192. glDisableClientState(GL_COLOR_ARRAY);
  3193. GLPf(Color4)(1.0f, 1.0f, 1.0f, 1.0f);
  3194. } else
  3195. #endif // NDEBUG
  3196. if (_data_reader->get_color_info(array_reader, num_values, numeric_type,
  3197. start, stride)) {
  3198. if (!setup_array_data(client_pointer, array_reader, force)) {
  3199. return false;
  3200. }
  3201. if (numeric_type == Geom::NT_packed_dabc) {
  3202. glColorPointer(GL_BGRA, GL_UNSIGNED_BYTE,
  3203. stride, client_pointer + start);
  3204. } else {
  3205. glColorPointer(num_values, get_numeric_type(numeric_type),
  3206. stride, client_pointer + start);
  3207. }
  3208. glEnableClientState(GL_COLOR_ARRAY);
  3209. } else {
  3210. glDisableClientState(GL_COLOR_ARRAY);
  3211. // Since we don't have per-vertex color, the implicit color is
  3212. // white.
  3213. GLPf(Color4)(1.0f, 1.0f, 1.0f, 1.0f);
  3214. }
  3215. // Now set up each of the active texture coordinate stages--or at
  3216. // least those for which we're not generating texture coordinates
  3217. // automatically.
  3218. int max_stage_index = _target_texture->get_num_on_ff_stages();
  3219. int stage_index = 0;
  3220. while (stage_index < max_stage_index) {
  3221. _glClientActiveTexture(GL_TEXTURE0 + stage_index);
  3222. TextureStage *stage = _target_texture->get_on_ff_stage(stage_index);
  3223. if (!_target_tex_gen->has_gen_texcoord_stage(stage)) {
  3224. // This stage is not one of the stages that doesn't need
  3225. // texcoords issued for it.
  3226. const InternalName *name = stage->get_texcoord_name();
  3227. if (_data_reader->get_array_info(name, array_reader, num_values,
  3228. numeric_type, start, stride)) {
  3229. // The vertex data does have texcoords for this stage.
  3230. if (!setup_array_data(client_pointer, array_reader, force)) {
  3231. return false;
  3232. }
  3233. glTexCoordPointer(num_values, get_numeric_type(numeric_type),
  3234. stride, client_pointer + start);
  3235. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  3236. } else {
  3237. // The vertex data doesn't have texcoords for this stage (even
  3238. // though they're needed).
  3239. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  3240. }
  3241. } else {
  3242. // No texcoords are needed for this stage.
  3243. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  3244. }
  3245. ++stage_index;
  3246. }
  3247. // Be sure also to disable any texture stages we had enabled before.
  3248. while (stage_index < _last_max_stage_index) {
  3249. _glClientActiveTexture(GL_TEXTURE0 + stage_index);
  3250. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  3251. ++stage_index;
  3252. }
  3253. _last_max_stage_index = max_stage_index;
  3254. // There's no requirement that we add vertices last, but we do
  3255. // anyway.
  3256. if (_data_reader->get_vertex_info(array_reader, num_values, numeric_type,
  3257. start, stride)) {
  3258. if (!setup_array_data(client_pointer, array_reader, force)) {
  3259. return false;
  3260. }
  3261. glVertexPointer(num_values, get_numeric_type(numeric_type),
  3262. stride, client_pointer + start);
  3263. glEnableClientState(GL_VERTEX_ARRAY);
  3264. }
  3265. }
  3266. return true;
  3267. }
  3268. #endif // SUPPORT_FIXED_FUNCTION
  3269. ////////////////////////////////////////////////////////////////////
  3270. // Function: GLGraphicsStateGuardian::unbind_buffers
  3271. // Access: Protected
  3272. // Description: Ensures the vertex and array buffers are no longer
  3273. // bound. Some graphics drivers crash if these are left
  3274. // bound indiscriminantly.
  3275. ////////////////////////////////////////////////////////////////////
  3276. void CLP(GraphicsStateGuardian)::
  3277. unbind_buffers() {
  3278. if (_current_vbuffer_index != 0) {
  3279. if (GLCAT.is_spam() && gl_debug_buffers) {
  3280. GLCAT.spam()
  3281. << "unbinding vertex buffer\n";
  3282. }
  3283. _glBindBuffer(GL_ARRAY_BUFFER, 0);
  3284. _current_vbuffer_index = 0;
  3285. }
  3286. if (_current_ibuffer_index != 0) {
  3287. if (GLCAT.is_spam() && gl_debug_buffers) {
  3288. GLCAT.spam()
  3289. << "unbinding index buffer\n";
  3290. }
  3291. _glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  3292. _current_ibuffer_index = 0;
  3293. }
  3294. #ifdef SUPPORT_FIXED_FUNCTION
  3295. disable_standard_vertex_arrays();
  3296. #endif
  3297. }
  3298. #ifdef SUPPORT_FIXED_FUNCTION
  3299. ////////////////////////////////////////////////////////////////////
  3300. // Function: GLGraphicsStateGuardian::disable_standard_vertex_arrays
  3301. // Access: Protected
  3302. // Description: Used to disable all the standard vertex arrays that
  3303. // are currently enabled. glShaderContexts are
  3304. // responsible for setting up their own vertex arrays,
  3305. // but before they can do so, the standard vertex
  3306. // arrays need to be disabled to get them "out of the
  3307. // way." Called only from begin_draw_primitives.
  3308. ////////////////////////////////////////////////////////////////////
  3309. void CLP(GraphicsStateGuardian)::
  3310. disable_standard_vertex_arrays() {
  3311. #ifdef SUPPORT_IMMEDIATE_MODE
  3312. if (_use_sender) return;
  3313. #endif
  3314. glDisableClientState(GL_NORMAL_ARRAY);
  3315. glDisableClientState(GL_COLOR_ARRAY);
  3316. GLPf(Color4)(1.0f, 1.0f, 1.0f, 1.0f);
  3317. for (int stage_index=0; stage_index < _last_max_stage_index; stage_index++) {
  3318. _glClientActiveTexture(GL_TEXTURE0 + stage_index);
  3319. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  3320. }
  3321. _last_max_stage_index = 0;
  3322. glDisableClientState(GL_VERTEX_ARRAY);
  3323. report_my_gl_errors();
  3324. }
  3325. #endif // SUPPORT_FIXED_FUNCTION
  3326. ////////////////////////////////////////////////////////////////////
  3327. // Function: GLGraphicsStateGuardian::draw_triangles
  3328. // Access: Public, Virtual
  3329. // Description: Draws a series of disconnected triangles.
  3330. ////////////////////////////////////////////////////////////////////
  3331. bool CLP(GraphicsStateGuardian)::
  3332. draw_triangles(const GeomPrimitivePipelineReader *reader, bool force) {
  3333. //PStatGPUTimer timer(this, _draw_primitive_pcollector, reader->get_current_thread());
  3334. #ifndef NDEBUG
  3335. if (GLCAT.is_spam()) {
  3336. GLCAT.spam() << "draw_triangles: " << *(reader->get_object()) << "\n";
  3337. }
  3338. #endif // NDEBUG
  3339. #ifdef SUPPORT_IMMEDIATE_MODE
  3340. if (_use_sender) {
  3341. draw_immediate_simple_primitives(reader, GL_TRIANGLES);
  3342. } else
  3343. #endif // SUPPORT_IMMEDIATE_MODE
  3344. {
  3345. int num_vertices = reader->get_num_vertices();
  3346. _vertices_tri_pcollector.add_level(num_vertices);
  3347. _primitive_batches_tri_pcollector.add_level(1);
  3348. if (reader->is_indexed()) {
  3349. const unsigned char *client_pointer;
  3350. if (!setup_primitive(client_pointer, reader, force)) {
  3351. return false;
  3352. }
  3353. #ifndef OPENGLES_1
  3354. if (_supports_geometry_instancing && _instance_count > 0) {
  3355. _glDrawElementsInstanced(GL_TRIANGLES, num_vertices,
  3356. get_numeric_type(reader->get_index_type()),
  3357. client_pointer, _instance_count);
  3358. } else
  3359. #endif
  3360. {
  3361. _glDrawRangeElements(GL_TRIANGLES,
  3362. reader->get_min_vertex(),
  3363. reader->get_max_vertex(),
  3364. num_vertices,
  3365. get_numeric_type(reader->get_index_type()),
  3366. client_pointer);
  3367. }
  3368. } else {
  3369. #ifndef OPENGLES_1
  3370. if (_supports_geometry_instancing && _instance_count > 0) {
  3371. _glDrawArraysInstanced(GL_TRIANGLES,
  3372. reader->get_first_vertex(),
  3373. num_vertices, _instance_count);
  3374. } else
  3375. #endif
  3376. {
  3377. glDrawArrays(GL_TRIANGLES,
  3378. reader->get_first_vertex(),
  3379. num_vertices);
  3380. }
  3381. }
  3382. }
  3383. report_my_gl_errors();
  3384. return true;
  3385. }
  3386. ////////////////////////////////////////////////////////////////////
  3387. // Function: GLGraphicsStateGuardian::draw_tristrips
  3388. // Access: Public, Virtual
  3389. // Description: Draws a series of triangle strips.
  3390. ////////////////////////////////////////////////////////////////////
  3391. bool CLP(GraphicsStateGuardian)::
  3392. draw_tristrips(const GeomPrimitivePipelineReader *reader, bool force) {
  3393. //PStatGPUTimer timer(this, _draw_primitive_pcollector, reader->get_current_thread());
  3394. report_my_gl_errors();
  3395. #ifndef NDEBUG
  3396. if (GLCAT.is_spam()) {
  3397. GLCAT.spam() << "draw_tristrips: " << *(reader->get_object()) << "\n";
  3398. }
  3399. #endif // NDEBUG
  3400. #ifdef SUPPORT_IMMEDIATE_MODE
  3401. if (_use_sender) {
  3402. draw_immediate_composite_primitives(reader, GL_TRIANGLE_STRIP);
  3403. } else
  3404. #endif // SUPPORT_IMMEDIATE_MODE
  3405. {
  3406. if (connect_triangle_strips && _render_mode != RenderModeAttrib::M_wireframe) {
  3407. // One long triangle strip, connected by the degenerate vertices
  3408. // that have already been set up within the primitive.
  3409. int num_vertices = reader->get_num_vertices();
  3410. _vertices_tristrip_pcollector.add_level(num_vertices);
  3411. _primitive_batches_tristrip_pcollector.add_level(1);
  3412. if (reader->is_indexed()) {
  3413. const unsigned char *client_pointer;
  3414. if (!setup_primitive(client_pointer, reader, force)) {
  3415. return false;
  3416. }
  3417. #ifndef OPENGLES_1
  3418. if (_supports_geometry_instancing && _instance_count > 0) {
  3419. _glDrawElementsInstanced(GL_TRIANGLE_STRIP, num_vertices,
  3420. get_numeric_type(reader->get_index_type()),
  3421. client_pointer, _instance_count);
  3422. } else
  3423. #endif
  3424. {
  3425. _glDrawRangeElements(GL_TRIANGLE_STRIP,
  3426. reader->get_min_vertex(),
  3427. reader->get_max_vertex(),
  3428. num_vertices,
  3429. get_numeric_type(reader->get_index_type()),
  3430. client_pointer);
  3431. }
  3432. } else {
  3433. #ifndef OPENGLES_1
  3434. if (_supports_geometry_instancing && _instance_count > 0) {
  3435. _glDrawArraysInstanced(GL_TRIANGLE_STRIP,
  3436. reader->get_first_vertex(),
  3437. num_vertices, _instance_count);
  3438. } else
  3439. #endif
  3440. {
  3441. glDrawArrays(GL_TRIANGLE_STRIP,
  3442. reader->get_first_vertex(),
  3443. num_vertices);
  3444. }
  3445. }
  3446. } else {
  3447. // Send the individual triangle strips, stepping over the
  3448. // degenerate vertices.
  3449. CPTA_int ends = reader->get_ends();
  3450. _primitive_batches_tristrip_pcollector.add_level(ends.size());
  3451. if (reader->is_indexed()) {
  3452. const unsigned char *client_pointer;
  3453. if (!setup_primitive(client_pointer, reader, force)) {
  3454. return false;
  3455. }
  3456. int index_stride = reader->get_index_stride();
  3457. GeomVertexReader mins(reader->get_mins(), 0);
  3458. GeomVertexReader maxs(reader->get_maxs(), 0);
  3459. nassertr(reader->get_mins()->get_num_rows() == (int)ends.size() &&
  3460. reader->get_maxs()->get_num_rows() == (int)ends.size(), false);
  3461. unsigned int start = 0;
  3462. for (size_t i = 0; i < ends.size(); i++) {
  3463. _vertices_tristrip_pcollector.add_level(ends[i] - start);
  3464. #ifndef OPENGLES_1
  3465. if (_supports_geometry_instancing && _instance_count > 0) {
  3466. _glDrawElementsInstanced(GL_TRIANGLE_STRIP, ends[i] - start,
  3467. get_numeric_type(reader->get_index_type()),
  3468. client_pointer + start * index_stride,
  3469. _instance_count);
  3470. } else
  3471. #endif
  3472. {
  3473. _glDrawRangeElements(GL_TRIANGLE_STRIP,
  3474. mins.get_data1i(), maxs.get_data1i(),
  3475. ends[i] - start,
  3476. get_numeric_type(reader->get_index_type()),
  3477. client_pointer + start * index_stride);
  3478. }
  3479. start = ends[i] + 2;
  3480. }
  3481. } else {
  3482. unsigned int start = 0;
  3483. int first_vertex = reader->get_first_vertex();
  3484. for (size_t i = 0; i < ends.size(); i++) {
  3485. _vertices_tristrip_pcollector.add_level(ends[i] - start);
  3486. #ifndef OPENGLES_1
  3487. if (_supports_geometry_instancing && _instance_count > 0) {
  3488. _glDrawArraysInstanced(GL_TRIANGLE_STRIP, first_vertex + start,
  3489. ends[i] - start, _instance_count);
  3490. } else
  3491. #endif
  3492. {
  3493. glDrawArrays(GL_TRIANGLE_STRIP, first_vertex + start,
  3494. ends[i] - start);
  3495. }
  3496. start = ends[i] + 2;
  3497. }
  3498. }
  3499. }
  3500. }
  3501. report_my_gl_errors();
  3502. return true;
  3503. }
  3504. ////////////////////////////////////////////////////////////////////
  3505. // Function: GLGraphicsStateGuardian::draw_trifans
  3506. // Access: Public, Virtual
  3507. // Description: Draws a series of triangle fans.
  3508. ////////////////////////////////////////////////////////////////////
  3509. bool CLP(GraphicsStateGuardian)::
  3510. draw_trifans(const GeomPrimitivePipelineReader *reader, bool force) {
  3511. //PStatGPUTimer timer(this, _draw_primitive_pcollector, reader->get_current_thread());
  3512. #ifndef NDEBUG
  3513. if (GLCAT.is_spam()) {
  3514. GLCAT.spam() << "draw_trifans: " << *(reader->get_object()) << "\n";
  3515. }
  3516. #endif // NDEBUG
  3517. #ifdef SUPPORT_IMMEDIATE_MODE
  3518. if (_use_sender) {
  3519. draw_immediate_composite_primitives(reader, GL_TRIANGLE_FAN);
  3520. } else
  3521. #endif // SUPPORT_IMMEDIATE_MODE
  3522. {
  3523. // Send the individual triangle fans. There's no connecting fans
  3524. // with degenerate vertices, so no worries about that.
  3525. CPTA_int ends = reader->get_ends();
  3526. _primitive_batches_trifan_pcollector.add_level(ends.size());
  3527. if (reader->is_indexed()) {
  3528. const unsigned char *client_pointer;
  3529. if (!setup_primitive(client_pointer, reader, force)) {
  3530. return false;
  3531. }
  3532. int index_stride = reader->get_index_stride();
  3533. GeomVertexReader mins(reader->get_mins(), 0);
  3534. GeomVertexReader maxs(reader->get_maxs(), 0);
  3535. nassertr(reader->get_mins()->get_num_rows() == (int)ends.size() &&
  3536. reader->get_maxs()->get_num_rows() == (int)ends.size(), false);
  3537. unsigned int start = 0;
  3538. for (size_t i = 0; i < ends.size(); i++) {
  3539. _vertices_trifan_pcollector.add_level(ends[i] - start);
  3540. #ifndef OPENGLES_1
  3541. if (_supports_geometry_instancing && _instance_count > 0) {
  3542. _glDrawElementsInstanced(GL_TRIANGLE_FAN, ends[i] - start,
  3543. get_numeric_type(reader->get_index_type()),
  3544. client_pointer + start * index_stride,
  3545. _instance_count);
  3546. } else
  3547. #endif
  3548. {
  3549. _glDrawRangeElements(GL_TRIANGLE_FAN,
  3550. mins.get_data1i(), maxs.get_data1i(), ends[i] - start,
  3551. get_numeric_type(reader->get_index_type()),
  3552. client_pointer + start * index_stride);
  3553. }
  3554. start = ends[i];
  3555. }
  3556. } else {
  3557. unsigned int start = 0;
  3558. int first_vertex = reader->get_first_vertex();
  3559. for (size_t i = 0; i < ends.size(); i++) {
  3560. _vertices_trifan_pcollector.add_level(ends[i] - start);
  3561. #ifndef OPENGLES_1
  3562. if (_supports_geometry_instancing && _instance_count > 0) {
  3563. _glDrawArraysInstanced(GL_TRIANGLE_FAN, first_vertex + start,
  3564. ends[i] - start, _instance_count);
  3565. } else
  3566. #endif
  3567. {
  3568. glDrawArrays(GL_TRIANGLE_FAN, first_vertex + start,
  3569. ends[i] - start);
  3570. }
  3571. start = ends[i];
  3572. }
  3573. }
  3574. }
  3575. report_my_gl_errors();
  3576. return true;
  3577. }
  3578. ////////////////////////////////////////////////////////////////////
  3579. // Function: GLGraphicsStateGuardian::draw_patches
  3580. // Access: Public, Virtual
  3581. // Description: Draws a series of "patches", which can only be
  3582. // processed by a tessellation shader.
  3583. ////////////////////////////////////////////////////////////////////
  3584. bool CLP(GraphicsStateGuardian)::
  3585. draw_patches(const GeomPrimitivePipelineReader *reader, bool force) {
  3586. //PStatGPUTimer timer(this, _draw_primitive_pcollector, reader->get_current_thread());
  3587. #ifndef NDEBUG
  3588. if (GLCAT.is_spam()) {
  3589. GLCAT.spam() << "draw_patches: " << *(reader->get_object()) << "\n";
  3590. }
  3591. #endif // NDEBUG
  3592. if (!get_supports_tessellation_shaders()) {
  3593. return false;
  3594. }
  3595. #ifndef OPENGLES
  3596. _glPatchParameteri(GL_PATCH_VERTICES, reader->get_object()->get_num_vertices_per_primitive());
  3597. #ifdef SUPPORT_IMMEDIATE_MODE
  3598. if (_use_sender) {
  3599. draw_immediate_simple_primitives(reader, GL_PATCHES);
  3600. } else
  3601. #endif // SUPPORT_IMMEDIATE_MODE
  3602. {
  3603. int num_vertices = reader->get_num_vertices();
  3604. _vertices_patch_pcollector.add_level(num_vertices);
  3605. _primitive_batches_patch_pcollector.add_level(1);
  3606. if (reader->is_indexed()) {
  3607. const unsigned char *client_pointer;
  3608. if (!setup_primitive(client_pointer, reader, force)) {
  3609. return false;
  3610. }
  3611. #ifndef OPENGLES_1
  3612. if (_supports_geometry_instancing && _instance_count > 0) {
  3613. _glDrawElementsInstanced(GL_PATCHES, num_vertices,
  3614. get_numeric_type(reader->get_index_type()),
  3615. client_pointer, _instance_count);
  3616. } else
  3617. #endif
  3618. {
  3619. _glDrawRangeElements(GL_PATCHES,
  3620. reader->get_min_vertex(),
  3621. reader->get_max_vertex(),
  3622. num_vertices,
  3623. get_numeric_type(reader->get_index_type()),
  3624. client_pointer);
  3625. }
  3626. } else {
  3627. #ifndef OPENGLES_1
  3628. if (_supports_geometry_instancing && _instance_count > 0) {
  3629. _glDrawArraysInstanced(GL_PATCHES,
  3630. reader->get_first_vertex(),
  3631. num_vertices, _instance_count);
  3632. } else
  3633. #endif
  3634. {
  3635. glDrawArrays(GL_PATCHES,
  3636. reader->get_first_vertex(),
  3637. num_vertices);
  3638. }
  3639. }
  3640. }
  3641. #endif // OPENGLES
  3642. report_my_gl_errors();
  3643. return true;
  3644. }
  3645. ////////////////////////////////////////////////////////////////////
  3646. // Function: GLGraphicsStateGuardian::draw_lines
  3647. // Access: Public, Virtual
  3648. // Description: Draws a series of disconnected line segments.
  3649. ////////////////////////////////////////////////////////////////////
  3650. bool CLP(GraphicsStateGuardian)::
  3651. draw_lines(const GeomPrimitivePipelineReader *reader, bool force) {
  3652. //PStatGPUTimer timer(this, _draw_primitive_pcollector, reader->get_current_thread());
  3653. #ifndef NDEBUG
  3654. if (GLCAT.is_spam()) {
  3655. GLCAT.spam() << "draw_lines: " << *(reader->get_object()) << "\n";
  3656. }
  3657. #endif // NDEBUG
  3658. #ifdef SUPPORT_IMMEDIATE_MODE
  3659. if (_use_sender) {
  3660. draw_immediate_simple_primitives(reader, GL_LINES);
  3661. } else
  3662. #endif // SUPPORT_IMMEDIATE_MODE
  3663. {
  3664. int num_vertices = reader->get_num_vertices();
  3665. _vertices_other_pcollector.add_level(num_vertices);
  3666. _primitive_batches_other_pcollector.add_level(1);
  3667. if (reader->is_indexed()) {
  3668. const unsigned char *client_pointer;
  3669. if (!setup_primitive(client_pointer, reader, force)) {
  3670. return false;
  3671. }
  3672. #ifndef OPENGLES_1
  3673. if (_supports_geometry_instancing && _instance_count > 0) {
  3674. _glDrawElementsInstanced(GL_LINES, num_vertices,
  3675. get_numeric_type(reader->get_index_type()),
  3676. client_pointer, _instance_count);
  3677. } else
  3678. #endif
  3679. {
  3680. _glDrawRangeElements(GL_LINES,
  3681. reader->get_min_vertex(),
  3682. reader->get_max_vertex(),
  3683. num_vertices,
  3684. get_numeric_type(reader->get_index_type()),
  3685. client_pointer);
  3686. }
  3687. } else {
  3688. #ifndef OPENGLES_1
  3689. if (_supports_geometry_instancing && _instance_count > 0) {
  3690. _glDrawArraysInstanced(GL_LINES,
  3691. reader->get_first_vertex(),
  3692. num_vertices, _instance_count);
  3693. } else
  3694. #endif
  3695. {
  3696. glDrawArrays(GL_LINES,
  3697. reader->get_first_vertex(),
  3698. num_vertices);
  3699. }
  3700. }
  3701. }
  3702. report_my_gl_errors();
  3703. return true;
  3704. }
  3705. ////////////////////////////////////////////////////////////////////
  3706. // Function: GLGraphicsStateGuardian::draw_linestrips
  3707. // Access: Public, Virtual
  3708. // Description: Draws a series of line strips.
  3709. ////////////////////////////////////////////////////////////////////
  3710. bool CLP(GraphicsStateGuardian)::
  3711. draw_linestrips(const GeomPrimitivePipelineReader *reader, bool force) {
  3712. //PStatGPUTimer timer(this, _draw_primitive_pcollector, reader->get_current_thread());
  3713. report_my_gl_errors();
  3714. #ifndef NDEBUG
  3715. if (GLCAT.is_spam()) {
  3716. GLCAT.spam() << "draw_linestrips: " << *(reader->get_object()) << "\n";
  3717. }
  3718. #endif // NDEBUG
  3719. #ifdef SUPPORT_IMMEDIATE_MODE
  3720. if (_use_sender) {
  3721. draw_immediate_composite_primitives(reader, GL_LINE_STRIP);
  3722. } else
  3723. #endif // SUPPORT_IMMEDIATE_MODE
  3724. {
  3725. if (reader->is_indexed() &&
  3726. (_supported_geom_rendering & GeomEnums::GR_strip_cut_index) != 0) {
  3727. // One long triangle strip, connected by strip cut indices.
  3728. #ifndef OPENGLES
  3729. if (_explicit_primitive_restart) {
  3730. glEnable(GL_PRIMITIVE_RESTART);
  3731. _glPrimitiveRestartIndex(reader->get_strip_cut_index());
  3732. }
  3733. #endif // !OPENGLES
  3734. int num_vertices = reader->get_num_vertices();
  3735. _vertices_other_pcollector.add_level(num_vertices);
  3736. _primitive_batches_other_pcollector.add_level(1);
  3737. const unsigned char *client_pointer;
  3738. if (!setup_primitive(client_pointer, reader, force)) {
  3739. return false;
  3740. }
  3741. #ifndef OPENGLES_1
  3742. if (_supports_geometry_instancing && _instance_count > 0) {
  3743. _glDrawElementsInstanced(GL_LINE_STRIP, num_vertices,
  3744. get_numeric_type(reader->get_index_type()),
  3745. client_pointer, _instance_count);
  3746. } else
  3747. #endif // !OPENGLES
  3748. {
  3749. _glDrawRangeElements(GL_LINE_STRIP,
  3750. reader->get_min_vertex(),
  3751. reader->get_max_vertex(),
  3752. num_vertices,
  3753. get_numeric_type(reader->get_index_type()),
  3754. client_pointer);
  3755. }
  3756. #ifndef OPENGLES
  3757. if (_explicit_primitive_restart) {
  3758. glDisable(GL_PRIMITIVE_RESTART);
  3759. }
  3760. #endif // !OPENGLES
  3761. } else {
  3762. // Send the individual line strips, stepping over the
  3763. // strip-cut indices.
  3764. CPTA_int ends = reader->get_ends();
  3765. _primitive_batches_other_pcollector.add_level(ends.size());
  3766. if (reader->is_indexed()) {
  3767. const unsigned char *client_pointer;
  3768. if (!setup_primitive(client_pointer, reader, force)) {
  3769. return false;
  3770. }
  3771. int index_stride = reader->get_index_stride();
  3772. GeomVertexReader mins(reader->get_mins(), 0);
  3773. GeomVertexReader maxs(reader->get_maxs(), 0);
  3774. nassertr(reader->get_mins()->get_num_rows() == (int)ends.size() &&
  3775. reader->get_maxs()->get_num_rows() == (int)ends.size(), false);
  3776. unsigned int start = 0;
  3777. for (size_t i = 0; i < ends.size(); i++) {
  3778. _vertices_other_pcollector.add_level(ends[i] - start);
  3779. #ifndef OPENGLES_1
  3780. if (_supports_geometry_instancing && _instance_count > 0) {
  3781. _glDrawElementsInstanced(GL_LINE_STRIP, ends[i] - start,
  3782. get_numeric_type(reader->get_index_type()),
  3783. client_pointer + start * index_stride,
  3784. _instance_count);
  3785. } else
  3786. #endif
  3787. {
  3788. _glDrawRangeElements(GL_LINE_STRIP,
  3789. mins.get_data1i(), maxs.get_data1i(),
  3790. ends[i] - start,
  3791. get_numeric_type(reader->get_index_type()),
  3792. client_pointer + start * index_stride);
  3793. }
  3794. start = ends[i] + 1;
  3795. }
  3796. } else {
  3797. unsigned int start = 0;
  3798. int first_vertex = reader->get_first_vertex();
  3799. for (size_t i = 0; i < ends.size(); i++) {
  3800. _vertices_other_pcollector.add_level(ends[i] - start);
  3801. #ifndef OPENGLES_1
  3802. if (_supports_geometry_instancing && _instance_count > 0) {
  3803. _glDrawArraysInstanced(GL_LINE_STRIP, first_vertex + start,
  3804. ends[i] - start, _instance_count);
  3805. } else
  3806. #endif
  3807. {
  3808. glDrawArrays(GL_LINE_STRIP, first_vertex + start, ends[i] - start);
  3809. }
  3810. start = ends[i] + 1;
  3811. }
  3812. }
  3813. }
  3814. }
  3815. report_my_gl_errors();
  3816. return true;
  3817. }
  3818. ////////////////////////////////////////////////////////////////////
  3819. // Function: GLGraphicsStateGuardian::draw_points
  3820. // Access: Public, Virtual
  3821. // Description: Draws a series of disconnected points.
  3822. ////////////////////////////////////////////////////////////////////
  3823. bool CLP(GraphicsStateGuardian)::
  3824. draw_points(const GeomPrimitivePipelineReader *reader, bool force) {
  3825. //PStatGPUTimer timer(this, _draw_primitive_pcollector, reader->get_current_thread());
  3826. #ifndef NDEBUG
  3827. if (GLCAT.is_spam()) {
  3828. GLCAT.spam() << "draw_points: " << *(reader->get_object()) << "\n";
  3829. }
  3830. #endif // NDEBUG
  3831. #ifdef SUPPORT_IMMEDIATE_MODE
  3832. if (_use_sender) {
  3833. draw_immediate_simple_primitives(reader, GL_POINTS);
  3834. } else
  3835. #endif // SUPPORT_IMMEDIATE_MODE
  3836. {
  3837. int num_vertices = reader->get_num_vertices();
  3838. _vertices_other_pcollector.add_level(num_vertices);
  3839. _primitive_batches_other_pcollector.add_level(1);
  3840. if (reader->is_indexed()) {
  3841. const unsigned char *client_pointer;
  3842. if (!setup_primitive(client_pointer, reader, force)) {
  3843. return false;
  3844. }
  3845. #ifndef OPENGLES_1
  3846. if (_supports_geometry_instancing && _instance_count > 0) {
  3847. _glDrawElementsInstanced(GL_POINTS, num_vertices,
  3848. get_numeric_type(reader->get_index_type()),
  3849. client_pointer, _instance_count);
  3850. } else
  3851. #endif
  3852. {
  3853. _glDrawRangeElements(GL_POINTS,
  3854. reader->get_min_vertex(),
  3855. reader->get_max_vertex(),
  3856. num_vertices,
  3857. get_numeric_type(reader->get_index_type()),
  3858. client_pointer);
  3859. }
  3860. } else {
  3861. #ifndef OPENGLES_1
  3862. if (_supports_geometry_instancing && _instance_count > 0) {
  3863. _glDrawArraysInstanced(GL_POINTS,
  3864. reader->get_first_vertex(),
  3865. num_vertices, _instance_count);
  3866. } else
  3867. #endif
  3868. {
  3869. glDrawArrays(GL_POINTS, reader->get_first_vertex(), num_vertices);
  3870. }
  3871. }
  3872. }
  3873. report_my_gl_errors();
  3874. return true;
  3875. }
  3876. ////////////////////////////////////////////////////////////////////
  3877. // Function: GLGraphicsStateGuardian::end_draw_primitives()
  3878. // Access: Public, Virtual
  3879. // Description: Called after a sequence of draw_primitive()
  3880. // functions are called, this should do whatever cleanup
  3881. // is appropriate.
  3882. ////////////////////////////////////////////////////////////////////
  3883. void CLP(GraphicsStateGuardian)::
  3884. end_draw_primitives() {
  3885. #if !defined(OPENGLES) && defined(SUPPORT_FIXED_FUNCTION) // Display lists not supported by OpenGL ES.
  3886. if (_geom_display_list != 0) {
  3887. // If we were building a display list, close it now.
  3888. glEndList();
  3889. _load_display_list_pcollector.stop();
  3890. if (!gl_compile_and_execute) {
  3891. glCallList(_geom_display_list);
  3892. }
  3893. _primitive_batches_display_list_pcollector.add_level(1);
  3894. }
  3895. _geom_display_list = 0;
  3896. #endif
  3897. #ifdef SUPPORT_FIXED_FUNCTION
  3898. if (_transform_stale) {
  3899. glMatrixMode(GL_MODELVIEW);
  3900. GLPf(LoadMatrix)(_internal_transform->get_mat().get_data());
  3901. }
  3902. if (_data_reader->is_vertex_transformed()) {
  3903. // Restore the matrices that we pushed above.
  3904. glMatrixMode(GL_PROJECTION);
  3905. glPopMatrix();
  3906. glMatrixMode(GL_MODELVIEW);
  3907. glPopMatrix();
  3908. }
  3909. #endif
  3910. GraphicsStateGuardian::end_draw_primitives();
  3911. maybe_gl_finish();
  3912. report_my_gl_errors();
  3913. }
  3914. #ifndef OPENGLES
  3915. ////////////////////////////////////////////////////////////////////
  3916. // Function: GLGraphicsStateGuardian::issue_memory_barrier
  3917. // Access: Public
  3918. // Description: Issues the given memory barriers, and clears the
  3919. // list of textures marked as incoherent for the given
  3920. // bits.
  3921. ////////////////////////////////////////////////////////////////////
  3922. void CLP(GraphicsStateGuardian)::
  3923. issue_memory_barrier(GLbitfield barriers) {
  3924. if (!gl_enable_memory_barriers || _glMemoryBarrier == NULL) {
  3925. return;
  3926. }
  3927. PStatGPUTimer timer(this, _memory_barrier_pcollector);
  3928. if (GLCAT.is_spam()) {
  3929. GLCAT.spam() << "Issuing memory barriers:";
  3930. }
  3931. _glMemoryBarrier(barriers);
  3932. // Indicate that barriers no longer need to be issued for
  3933. // the relevant lists of textures.
  3934. if (barriers & GL_TEXTURE_FETCH_BARRIER_BIT) {
  3935. _textures_needing_fetch_barrier.clear();
  3936. GLCAT.spam(false) << " texture_fetch";
  3937. }
  3938. if (barriers & GL_SHADER_IMAGE_ACCESS_BARRIER_BIT) {
  3939. _textures_needing_image_access_barrier.clear();
  3940. GLCAT.spam(false) << " shader_image_access";
  3941. }
  3942. if (barriers & GL_TEXTURE_UPDATE_BARRIER_BIT) {
  3943. _textures_needing_update_barrier.clear();
  3944. GLCAT.spam(false) << " texture_update";
  3945. }
  3946. if (barriers & GL_FRAMEBUFFER_BARRIER_BIT) {
  3947. _textures_needing_framebuffer_barrier.clear();
  3948. GLCAT.spam(false) << " framebuffer";
  3949. }
  3950. GLCAT.spam(false) << "\n";
  3951. report_my_gl_errors();
  3952. }
  3953. #endif // OPENGLES
  3954. ////////////////////////////////////////////////////////////////////
  3955. // Function: GLGraphicsStateGuardian::prepare_texture
  3956. // Access: Public, Virtual
  3957. // Description: Creates whatever structures the GSG requires to
  3958. // represent the texture internally, and returns a
  3959. // newly-allocated TextureContext object with this data.
  3960. // It is the responsibility of the calling function to
  3961. // later call release_texture() with this same pointer
  3962. // (which will also delete the pointer).
  3963. //
  3964. // This function should not be called directly to
  3965. // prepare a texture. Instead, call Texture::prepare().
  3966. ////////////////////////////////////////////////////////////////////
  3967. TextureContext *CLP(GraphicsStateGuardian)::
  3968. prepare_texture(Texture *tex, int view) {
  3969. PStatGPUTimer timer(this, _prepare_texture_pcollector);
  3970. report_my_gl_errors();
  3971. // Make sure we'll support this texture when it's rendered. Don't
  3972. // bother to prepare it if we won't.
  3973. switch (tex->get_texture_type()) {
  3974. case Texture::TT_3d_texture:
  3975. if (!_supports_3d_texture) {
  3976. GLCAT.warning()
  3977. << "3-D textures are not supported by this OpenGL driver.\n";
  3978. return NULL;
  3979. }
  3980. break;
  3981. case Texture::TT_2d_texture_array:
  3982. if (!_supports_2d_texture_array) {
  3983. GLCAT.warning()
  3984. << "2-D texture arrays are not supported by this OpenGL driver.\n";
  3985. return NULL;
  3986. }
  3987. break;
  3988. case Texture::TT_cube_map:
  3989. if (!_supports_cube_map) {
  3990. GLCAT.warning()
  3991. << "Cube map textures are not supported by this OpenGL driver.\n";
  3992. return NULL;
  3993. }
  3994. break;
  3995. case Texture::TT_buffer_texture:
  3996. if (!_supports_buffer_texture) {
  3997. GLCAT.warning()
  3998. << "Buffer textures are not supported by this OpenGL driver.\n";
  3999. return NULL;
  4000. }
  4001. break;
  4002. default:
  4003. break;
  4004. }
  4005. CLP(TextureContext) *gtc = new CLP(TextureContext)(this, _prepared_objects, tex, view);
  4006. report_my_gl_errors();
  4007. return gtc;
  4008. }
  4009. ////////////////////////////////////////////////////////////////////
  4010. // Function: GLGraphicsStateGuardian::update_texture
  4011. // Access: Public, Virtual
  4012. // Description: Ensures that the current Texture data is refreshed
  4013. // onto the GSG. This means updating the texture
  4014. // properties and/or re-uploading the texture image, if
  4015. // necessary. This should only be called within the
  4016. // draw thread.
  4017. //
  4018. // If force is true, this function will not return until
  4019. // the texture has been fully uploaded. If force is
  4020. // false, the function may choose to upload a simple
  4021. // version of the texture instead, if the texture is not
  4022. // fully resident (and if get_incomplete_render() is
  4023. // true).
  4024. ////////////////////////////////////////////////////////////////////
  4025. bool CLP(GraphicsStateGuardian)::
  4026. update_texture(TextureContext *tc, bool force) {
  4027. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  4028. if (gtc->was_image_modified() || !gtc->_has_storage) {
  4029. PStatGPUTimer timer(this, _texture_update_pcollector);
  4030. // If the texture image was modified, reload the texture.
  4031. apply_texture(tc);
  4032. Texture *tex = tc->get_texture();
  4033. if (gtc->was_properties_modified()) {
  4034. specify_texture(gtc, tex->get_default_sampler());
  4035. }
  4036. bool okflag = upload_texture(gtc, force, tex->uses_mipmaps());
  4037. if (!okflag) {
  4038. GLCAT.error()
  4039. << "Could not load " << *tex << "\n";
  4040. return false;
  4041. }
  4042. } else if (gtc->was_properties_modified()) {
  4043. PStatGPUTimer timer(this, _texture_update_pcollector);
  4044. // If only the properties have been modified, we don't necessarily
  4045. // need to reload the texture.
  4046. apply_texture(tc);
  4047. Texture *tex = tc->get_texture();
  4048. if (specify_texture(gtc, tex->get_default_sampler())) {
  4049. // Actually, looks like the texture *does* need to be reloaded.
  4050. gtc->mark_needs_reload();
  4051. bool okflag = upload_texture(gtc, force, tex->uses_mipmaps());
  4052. if (!okflag) {
  4053. GLCAT.error()
  4054. << "Could not load " << *tex << "\n";
  4055. return false;
  4056. }
  4057. } else {
  4058. // The texture didn't need reloading, but mark it fully updated
  4059. // now.
  4060. gtc->mark_loaded();
  4061. }
  4062. }
  4063. gtc->enqueue_lru(&_prepared_objects->_graphics_memory_lru);
  4064. report_my_gl_errors();
  4065. return true;
  4066. }
  4067. ////////////////////////////////////////////////////////////////////
  4068. // Function: GLGraphicsStateGuardian::release_texture
  4069. // Access: Public, Virtual
  4070. // Description: Frees the GL resources previously allocated for the
  4071. // texture. This function should never be called
  4072. // directly; instead, call Texture::release() (or simply
  4073. // let the Texture destruct).
  4074. ////////////////////////////////////////////////////////////////////
  4075. void CLP(GraphicsStateGuardian)::
  4076. release_texture(TextureContext *tc) {
  4077. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  4078. delete gtc;
  4079. }
  4080. ////////////////////////////////////////////////////////////////////
  4081. // Function: GLGraphicsStateGuardian::extract_texture_data
  4082. // Access: Public, Virtual
  4083. // Description: This method should only be called by the
  4084. // GraphicsEngine. Do not call it directly; call
  4085. // GraphicsEngine::extract_texture_data() instead.
  4086. //
  4087. // This method will be called in the draw thread to
  4088. // download the texture memory's image into its
  4089. // ram_image value. It returns true on success, false
  4090. // otherwise.
  4091. ////////////////////////////////////////////////////////////////////
  4092. bool CLP(GraphicsStateGuardian)::
  4093. extract_texture_data(Texture *tex) {
  4094. bool success = true;
  4095. // Make sure the error stack is cleared out before we begin.
  4096. report_my_gl_errors();
  4097. int num_views = tex->get_num_views();
  4098. for (int view = 0; view < num_views; ++view) {
  4099. TextureContext *tc = tex->prepare_now(view, get_prepared_objects(), this);
  4100. nassertr(tc != (TextureContext *)NULL, false);
  4101. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  4102. if (!do_extract_texture_data(gtc)) {
  4103. success = false;
  4104. }
  4105. }
  4106. return success;
  4107. }
  4108. #ifndef OPENGLES
  4109. ////////////////////////////////////////////////////////////////////
  4110. // Function: GLGraphicsStateGuardian::prepare_sampler
  4111. // Access: Public, Virtual
  4112. // Description: Creates whatever structures the GSG requires to
  4113. // represent the sampler state internally, and returns a
  4114. // newly-allocated SamplerContext object with this data.
  4115. // It is the responsibility of the calling function to
  4116. // later call release_sampler() with this same pointer
  4117. // (which will also delete the pointer).
  4118. //
  4119. // This function should not be called directly to
  4120. // prepare a sampler object. Instead, call
  4121. // SamplerState::prepare().
  4122. ////////////////////////////////////////////////////////////////////
  4123. SamplerContext *CLP(GraphicsStateGuardian)::
  4124. prepare_sampler(const SamplerState &sampler) {
  4125. nassertr(_supports_sampler_objects, NULL);
  4126. PStatGPUTimer timer(this, _prepare_sampler_pcollector);
  4127. CLP(SamplerContext) *gsc = new CLP(SamplerContext)(this, sampler);
  4128. GLuint index = gsc->_index;
  4129. // Sampler contexts are immutable in Panda, so might as well just
  4130. // initialize all the settings here.
  4131. _glSamplerParameteri(index, GL_TEXTURE_WRAP_S,
  4132. get_texture_wrap_mode(sampler.get_wrap_u()));
  4133. _glSamplerParameteri(index, GL_TEXTURE_WRAP_T,
  4134. get_texture_wrap_mode(sampler.get_wrap_v()));
  4135. _glSamplerParameteri(index, GL_TEXTURE_WRAP_R,
  4136. get_texture_wrap_mode(sampler.get_wrap_w()));
  4137. #ifdef STDFLOAT_DOUBLE
  4138. LVecBase4f fvalue = LCAST(float, sampler.get_border_color());
  4139. _glSamplerParameterfv(index, GL_TEXTURE_BORDER_COLOR, fvalue.get_data());
  4140. #else
  4141. _glSamplerParameterfv(index, GL_TEXTURE_BORDER_COLOR,
  4142. sampler.get_border_color().get_data());
  4143. #endif
  4144. SamplerState::FilterType minfilter = sampler.get_effective_minfilter();
  4145. SamplerState::FilterType magfilter = sampler.get_effective_magfilter();
  4146. bool uses_mipmaps = SamplerState::is_mipmap(minfilter) && !gl_ignore_mipmaps;
  4147. #ifndef NDEBUG
  4148. if (gl_force_mipmaps) {
  4149. minfilter = SamplerState::FT_linear_mipmap_linear;
  4150. magfilter = SamplerState::FT_linear;
  4151. uses_mipmaps = true;
  4152. }
  4153. #endif
  4154. _glSamplerParameteri(index, GL_TEXTURE_MIN_FILTER,
  4155. get_texture_filter_type(minfilter, !uses_mipmaps));
  4156. _glSamplerParameteri(index, GL_TEXTURE_MAG_FILTER,
  4157. get_texture_filter_type(magfilter, true));
  4158. // Set anisotropic filtering.
  4159. if (_supports_anisotropy) {
  4160. PN_stdfloat anisotropy = sampler.get_effective_anisotropic_degree();
  4161. anisotropy = min(anisotropy, _max_anisotropy);
  4162. anisotropy = max(anisotropy, (PN_stdfloat)1.0);
  4163. _glSamplerParameterf(index, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
  4164. }
  4165. if (_supports_shadow_filter) {
  4166. if ((sampler.get_magfilter() == SamplerState::FT_shadow) ||
  4167. (sampler.get_minfilter() == SamplerState::FT_shadow)) {
  4168. _glSamplerParameteri(index, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
  4169. _glSamplerParameteri(index, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
  4170. } else {
  4171. _glSamplerParameteri(index, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
  4172. _glSamplerParameteri(index, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
  4173. }
  4174. }
  4175. _glSamplerParameterf(index, GL_TEXTURE_MIN_LOD, sampler.get_min_lod());
  4176. _glSamplerParameterf(index, GL_TEXTURE_MAX_LOD, sampler.get_max_lod());
  4177. _glSamplerParameterf(index, GL_TEXTURE_LOD_BIAS, sampler.get_lod_bias());
  4178. gsc->enqueue_lru(&_prepared_objects->_sampler_object_lru);
  4179. report_my_gl_errors();
  4180. return gsc;
  4181. }
  4182. #endif // !OPENGLES
  4183. #ifndef OPENGLES
  4184. ////////////////////////////////////////////////////////////////////
  4185. // Function: GLGraphicsStateGuardian::release_sampler
  4186. // Access: Public, Virtual
  4187. // Description: Frees the GL resources previously allocated for the
  4188. // sampler. This function should never be called
  4189. // directly; instead, call SamplerState::release().
  4190. ////////////////////////////////////////////////////////////////////
  4191. void CLP(GraphicsStateGuardian)::
  4192. release_sampler(SamplerContext *sc) {
  4193. CLP(SamplerContext) *gsc = DCAST(CLP(SamplerContext), sc);
  4194. delete gsc;
  4195. }
  4196. #endif // !OPENGLES
  4197. ////////////////////////////////////////////////////////////////////
  4198. // Function: GLGraphicsStateGuardian::prepare_geom
  4199. // Access: Public, Virtual
  4200. // Description: Creates a new retained-mode representation of the
  4201. // given geom, and returns a newly-allocated
  4202. // GeomContext pointer to reference it. It is the
  4203. // responsibility of the calling function to later
  4204. // call release_geom() with this same pointer (which
  4205. // will also delete the pointer).
  4206. //
  4207. // This function should not be called directly to
  4208. // prepare a geom. Instead, call Geom::prepare().
  4209. ////////////////////////////////////////////////////////////////////
  4210. GeomContext *CLP(GraphicsStateGuardian)::
  4211. prepare_geom(Geom *geom) {
  4212. PStatGPUTimer timer(this, _prepare_geom_pcollector);
  4213. return new CLP(GeomContext)(geom);
  4214. }
  4215. ////////////////////////////////////////////////////////////////////
  4216. // Function: GLGraphicsStateGuardian::release_geom
  4217. // Access: Public, Virtual
  4218. // Description: Frees the GL resources previously allocated for the
  4219. // geom. This function should never be called
  4220. // directly; instead, call Geom::release() (or simply
  4221. // let the Geom destruct).
  4222. ////////////////////////////////////////////////////////////////////
  4223. void CLP(GraphicsStateGuardian)::
  4224. release_geom(GeomContext *gc) {
  4225. CLP(GeomContext) *ggc = DCAST(CLP(GeomContext), gc);
  4226. ggc->release_display_lists();
  4227. report_my_gl_errors();
  4228. delete ggc;
  4229. }
  4230. ////////////////////////////////////////////////////////////////////
  4231. // Function: GLGraphicsStateGuardian::prepare_shader
  4232. // Access: Public, Virtual
  4233. // Description:
  4234. ////////////////////////////////////////////////////////////////////
  4235. ShaderContext *CLP(GraphicsStateGuardian)::
  4236. prepare_shader(Shader *se) {
  4237. PStatGPUTimer timer(this, _prepare_shader_pcollector);
  4238. #ifndef OPENGLES_1
  4239. ShaderContext *result = NULL;
  4240. switch (se->get_language()) {
  4241. case Shader::SL_GLSL:
  4242. if (_supports_glsl) {
  4243. result = new CLP(ShaderContext)(this, se);
  4244. break;
  4245. } else {
  4246. GLCAT.error()
  4247. << "Tried to load GLSL shader, but GLSL shaders not supported.\n";
  4248. return NULL;
  4249. }
  4250. case Shader::SL_Cg:
  4251. #if defined(HAVE_CG) && !defined(OPENGLES)
  4252. if (_supports_basic_shaders) {
  4253. result = new CLP(CgShaderContext)(this, se);
  4254. break;
  4255. } else {
  4256. GLCAT.error()
  4257. << "Tried to load Cg shader, but basic shaders not supported.\n";
  4258. return NULL;
  4259. }
  4260. #elif defined(OPENGLES)
  4261. GLCAT.error()
  4262. << "Tried to load Cg shader, but Cg support is not available for OpenGL ES.\n";
  4263. return NULL;
  4264. #else
  4265. GLCAT.error()
  4266. << "Tried to load Cg shader, but Cg support not compiled in.\n";
  4267. return NULL;
  4268. #endif
  4269. default:
  4270. GLCAT.error()
  4271. << "Tried to load shader with unsupported shader language!\n";
  4272. return NULL;
  4273. }
  4274. if (result->valid()) {
  4275. return result;
  4276. }
  4277. delete result;
  4278. #endif // OPENGLES_1
  4279. return NULL;
  4280. }
  4281. ////////////////////////////////////////////////////////////////////
  4282. // Function: GLGraphicsStateGuardian::release_shader
  4283. // Access: Public, Virtual
  4284. // Description:
  4285. ////////////////////////////////////////////////////////////////////
  4286. void CLP(GraphicsStateGuardian)::
  4287. release_shader(ShaderContext *sc) {
  4288. delete sc;
  4289. }
  4290. ////////////////////////////////////////////////////////////////////
  4291. // Function: GLGraphicsStateGuardian::record_deleted_display_list
  4292. // Access: Public
  4293. // Description: This is intended to be called only from the
  4294. // GLGeomContext destructor. It saves the indicated
  4295. // display list index in the list to be deleted at the
  4296. // end of the frame.
  4297. ////////////////////////////////////////////////////////////////////
  4298. void CLP(GraphicsStateGuardian)::
  4299. record_deleted_display_list(GLuint index) {
  4300. LightMutexHolder holder(_lock);
  4301. _deleted_display_lists.push_back(index);
  4302. }
  4303. ////////////////////////////////////////////////////////////////////
  4304. // Function: GLGraphicsStateGuardian::prepare_vertex_buffer
  4305. // Access: Public, Virtual
  4306. // Description: Creates a new retained-mode representation of the
  4307. // given data, and returns a newly-allocated
  4308. // VertexBufferContext pointer to reference it. It is the
  4309. // responsibility of the calling function to later
  4310. // call release_vertex_buffer() with this same pointer (which
  4311. // will also delete the pointer).
  4312. //
  4313. // This function should not be called directly to
  4314. // prepare a buffer. Instead, call Geom::prepare().
  4315. ////////////////////////////////////////////////////////////////////
  4316. VertexBufferContext *CLP(GraphicsStateGuardian)::
  4317. prepare_vertex_buffer(GeomVertexArrayData *data) {
  4318. if (_supports_buffers) {
  4319. PStatGPUTimer timer(this, _prepare_vertex_buffer_pcollector);
  4320. CLP(VertexBufferContext) *gvbc = new CLP(VertexBufferContext)(this, _prepared_objects, data);
  4321. _glGenBuffers(1, &gvbc->_index);
  4322. if (GLCAT.is_debug() && gl_debug_buffers) {
  4323. GLCAT.debug()
  4324. << "creating vertex buffer " << (int)gvbc->_index << ": "
  4325. << data->get_num_rows() << " vertices "
  4326. << *data->get_array_format() << "\n";
  4327. }
  4328. report_my_gl_errors();
  4329. apply_vertex_buffer(gvbc, data->get_handle(), false);
  4330. return gvbc;
  4331. }
  4332. return NULL;
  4333. }
  4334. ////////////////////////////////////////////////////////////////////
  4335. // Function: GLGraphicsStateGuardian::apply_vertex_buffer
  4336. // Access: Public
  4337. // Description: Makes the data the currently available data for
  4338. // rendering.
  4339. ////////////////////////////////////////////////////////////////////
  4340. bool CLP(GraphicsStateGuardian)::
  4341. apply_vertex_buffer(VertexBufferContext *vbc,
  4342. const GeomVertexArrayDataHandle *reader, bool force) {
  4343. nassertr(_supports_buffers, false);
  4344. if (reader->get_modified() == UpdateSeq::initial()) {
  4345. // No need to re-apply.
  4346. return true;
  4347. }
  4348. CLP(VertexBufferContext) *gvbc = DCAST(CLP(VertexBufferContext), vbc);
  4349. if (_current_vbuffer_index != gvbc->_index) {
  4350. if (GLCAT.is_spam() && gl_debug_buffers) {
  4351. GLCAT.spam()
  4352. << "binding vertex buffer " << (int)gvbc->_index << "\n";
  4353. }
  4354. _glBindBuffer(GL_ARRAY_BUFFER, gvbc->_index);
  4355. _current_vbuffer_index = gvbc->_index;
  4356. gvbc->set_active(true);
  4357. }
  4358. if (gvbc->was_modified(reader)) {
  4359. int num_bytes = reader->get_data_size_bytes();
  4360. if (GLCAT.is_debug() && gl_debug_buffers) {
  4361. GLCAT.debug()
  4362. << "copying " << num_bytes
  4363. << " bytes into vertex buffer " << (int)gvbc->_index << "\n";
  4364. }
  4365. if (num_bytes != 0) {
  4366. const unsigned char *client_pointer = reader->get_read_pointer(force);
  4367. if (client_pointer == NULL) {
  4368. return false;
  4369. }
  4370. PStatGPUTimer timer(this, _load_vertex_buffer_pcollector, reader->get_current_thread());
  4371. if (gvbc->changed_size(reader) || gvbc->changed_usage_hint(reader)) {
  4372. _glBufferData(GL_ARRAY_BUFFER, num_bytes, client_pointer,
  4373. get_usage(reader->get_usage_hint()));
  4374. } else {
  4375. _glBufferSubData(GL_ARRAY_BUFFER, 0, num_bytes, client_pointer);
  4376. }
  4377. _data_transferred_pcollector.add_level(num_bytes);
  4378. }
  4379. gvbc->mark_loaded(reader);
  4380. }
  4381. gvbc->enqueue_lru(&_prepared_objects->_graphics_memory_lru);
  4382. maybe_gl_finish();
  4383. report_my_gl_errors();
  4384. return true;
  4385. }
  4386. ////////////////////////////////////////////////////////////////////
  4387. // Function: GLGraphicsStateGuardian::release_vertex_buffer
  4388. // Access: Public, Virtual
  4389. // Description: Frees the GL resources previously allocated for the
  4390. // data. This function should never be called
  4391. // directly; instead, call Data::release() (or simply
  4392. // let the Data destruct).
  4393. ////////////////////////////////////////////////////////////////////
  4394. void CLP(GraphicsStateGuardian)::
  4395. release_vertex_buffer(VertexBufferContext *vbc) {
  4396. nassertv(_supports_buffers);
  4397. CLP(VertexBufferContext) *gvbc = DCAST(CLP(VertexBufferContext), vbc);
  4398. if (GLCAT.is_debug() && gl_debug_buffers) {
  4399. GLCAT.debug()
  4400. << "deleting vertex buffer " << (int)gvbc->_index << "\n";
  4401. }
  4402. // Make sure the buffer is unbound before we delete it. Not
  4403. // strictly necessary according to the OpenGL spec, but it might
  4404. // help out a flaky driver, and we need to keep our internal state
  4405. // consistent anyway.
  4406. if (_current_vbuffer_index == gvbc->_index) {
  4407. if (GLCAT.is_spam() && gl_debug_buffers) {
  4408. GLCAT.spam()
  4409. << "unbinding vertex buffer\n";
  4410. }
  4411. _glBindBuffer(GL_ARRAY_BUFFER, 0);
  4412. _current_vbuffer_index = 0;
  4413. }
  4414. _glDeleteBuffers(1, &gvbc->_index);
  4415. report_my_gl_errors();
  4416. gvbc->_index = 0;
  4417. delete gvbc;
  4418. }
  4419. ////////////////////////////////////////////////////////////////////
  4420. // Function: GLGraphicsStateGuardian::setup_array_data
  4421. // Access: Public
  4422. // Description: Internal function to bind a buffer object for the
  4423. // indicated data array, if appropriate, or to unbind a
  4424. // buffer object if it should be rendered from client
  4425. // memory.
  4426. //
  4427. // If the buffer object is bound, this function sets
  4428. // client_pointer to NULL (representing the start of the
  4429. // buffer object in server memory); if the buffer object
  4430. // is not bound, this function sets client_pointer the
  4431. // pointer to the data array in client memory, that is,
  4432. // the data array passed in.
  4433. //
  4434. // If force is not true, the function may return false
  4435. // indicating the data is not currently available.
  4436. ////////////////////////////////////////////////////////////////////
  4437. bool CLP(GraphicsStateGuardian)::
  4438. setup_array_data(const unsigned char *&client_pointer,
  4439. const GeomVertexArrayDataHandle *array_reader,
  4440. bool force) {
  4441. if (!_supports_buffers) {
  4442. // No support for buffer objects; always render from client.
  4443. client_pointer = array_reader->get_read_pointer(force);
  4444. return (client_pointer != NULL);
  4445. }
  4446. if (!vertex_buffers || _geom_display_list != 0 ||
  4447. array_reader->get_usage_hint() < gl_min_buffer_usage_hint) {
  4448. // The array specifies client rendering only, or buffer objects
  4449. // are configured off.
  4450. if (_current_vbuffer_index != 0) {
  4451. if (GLCAT.is_spam() && gl_debug_buffers) {
  4452. GLCAT.spam()
  4453. << "unbinding vertex buffer\n";
  4454. }
  4455. _glBindBuffer(GL_ARRAY_BUFFER, 0);
  4456. _current_vbuffer_index = 0;
  4457. }
  4458. client_pointer = array_reader->get_read_pointer(force);
  4459. return (client_pointer != NULL);
  4460. }
  4461. // Prepare the buffer object and bind it.
  4462. VertexBufferContext *vbc = array_reader->prepare_now(get_prepared_objects(), this);
  4463. nassertr(vbc != (VertexBufferContext *)NULL, false);
  4464. if (!apply_vertex_buffer(vbc, array_reader, force)) {
  4465. return false;
  4466. }
  4467. // NULL is the OpenGL convention for the first byte of the buffer object.
  4468. client_pointer = NULL;
  4469. return true;
  4470. }
  4471. ////////////////////////////////////////////////////////////////////
  4472. // Function: GLGraphicsStateGuardian::prepare_index_buffer
  4473. // Access: Public, Virtual
  4474. // Description: Creates a new retained-mode representation of the
  4475. // given data, and returns a newly-allocated
  4476. // IndexBufferContext pointer to reference it. It is the
  4477. // responsibility of the calling function to later
  4478. // call release_index_buffer() with this same pointer (which
  4479. // will also delete the pointer).
  4480. //
  4481. // This function should not be called directly to
  4482. // prepare a buffer. Instead, call Geom::prepare().
  4483. ////////////////////////////////////////////////////////////////////
  4484. IndexBufferContext *CLP(GraphicsStateGuardian)::
  4485. prepare_index_buffer(GeomPrimitive *data) {
  4486. if (_supports_buffers) {
  4487. PStatGPUTimer timer(this, _prepare_index_buffer_pcollector);
  4488. CLP(IndexBufferContext) *gibc = new CLP(IndexBufferContext)(this, _prepared_objects, data);
  4489. _glGenBuffers(1, &gibc->_index);
  4490. if (GLCAT.is_debug() && gl_debug_buffers) {
  4491. GLCAT.debug()
  4492. << "creating index buffer " << (int)gibc->_index << ": "
  4493. << data->get_num_vertices() << " indices ("
  4494. << data->get_vertices()->get_array_format()->get_column(0)->get_numeric_type()
  4495. << ")\n";
  4496. }
  4497. report_my_gl_errors();
  4498. GeomPrimitivePipelineReader reader(data, Thread::get_current_thread());
  4499. apply_index_buffer(gibc, &reader, false);
  4500. return gibc;
  4501. }
  4502. return NULL;
  4503. }
  4504. ////////////////////////////////////////////////////////////////////
  4505. // Function: GLGraphicsStateGuardian::apply_index_buffer
  4506. // Access: Public
  4507. // Description: Makes the data the currently available data for
  4508. // rendering.
  4509. ////////////////////////////////////////////////////////////////////
  4510. bool CLP(GraphicsStateGuardian)::
  4511. apply_index_buffer(IndexBufferContext *ibc,
  4512. const GeomPrimitivePipelineReader *reader,
  4513. bool force) {
  4514. nassertr(_supports_buffers, false);
  4515. if (reader->get_modified() == UpdateSeq::initial()) {
  4516. // No need to re-apply.
  4517. return true;
  4518. }
  4519. CLP(IndexBufferContext) *gibc = DCAST(CLP(IndexBufferContext), ibc);
  4520. if (_current_ibuffer_index != gibc->_index) {
  4521. if (GLCAT.is_spam() && gl_debug_buffers) {
  4522. GLCAT.spam()
  4523. << "binding index buffer " << (int)gibc->_index << "\n";
  4524. }
  4525. _glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gibc->_index);
  4526. _current_ibuffer_index = gibc->_index;
  4527. gibc->set_active(true);
  4528. }
  4529. if (gibc->was_modified(reader)) {
  4530. int num_bytes = reader->get_data_size_bytes();
  4531. if (GLCAT.is_debug() && gl_debug_buffers) {
  4532. GLCAT.debug()
  4533. << "copying " << num_bytes
  4534. << " bytes into index buffer " << (int)gibc->_index << "\n";
  4535. }
  4536. if (num_bytes != 0) {
  4537. const unsigned char *client_pointer = reader->get_read_pointer(force);
  4538. if (client_pointer == NULL) {
  4539. return false;
  4540. }
  4541. PStatGPUTimer timer(this, _load_index_buffer_pcollector, reader->get_current_thread());
  4542. if (gibc->changed_size(reader) || gibc->changed_usage_hint(reader)) {
  4543. _glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_bytes, client_pointer,
  4544. get_usage(reader->get_usage_hint()));
  4545. } else {
  4546. _glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, num_bytes,
  4547. client_pointer);
  4548. }
  4549. _data_transferred_pcollector.add_level(num_bytes);
  4550. }
  4551. gibc->mark_loaded(reader);
  4552. }
  4553. gibc->enqueue_lru(&_prepared_objects->_graphics_memory_lru);
  4554. maybe_gl_finish();
  4555. report_my_gl_errors();
  4556. return true;
  4557. }
  4558. ////////////////////////////////////////////////////////////////////
  4559. // Function: GLGraphicsStateGuardian::release_index_buffer
  4560. // Access: Public, Virtual
  4561. // Description: Frees the GL resources previously allocated for the
  4562. // data. This function should never be called
  4563. // directly; instead, call Data::release() (or simply
  4564. // let the Data destruct).
  4565. ////////////////////////////////////////////////////////////////////
  4566. void CLP(GraphicsStateGuardian)::
  4567. release_index_buffer(IndexBufferContext *ibc) {
  4568. nassertv(_supports_buffers);
  4569. CLP(IndexBufferContext) *gibc = DCAST(CLP(IndexBufferContext), ibc);
  4570. if (GLCAT.is_debug() && gl_debug_buffers) {
  4571. GLCAT.debug()
  4572. << "deleting index buffer " << (int)gibc->_index << "\n";
  4573. }
  4574. // Make sure the buffer is unbound before we delete it. Not
  4575. // strictly necessary according to the OpenGL spec, but it might
  4576. // help out a flaky driver, and we need to keep our internal state
  4577. // consistent anyway.
  4578. if (_current_ibuffer_index == gibc->_index) {
  4579. if (GLCAT.is_spam() && gl_debug_buffers) {
  4580. GLCAT.spam()
  4581. << "unbinding index buffer\n";
  4582. }
  4583. _glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  4584. _current_ibuffer_index = 0;
  4585. }
  4586. _glDeleteBuffers(1, &gibc->_index);
  4587. report_my_gl_errors();
  4588. gibc->_index = 0;
  4589. delete gibc;
  4590. }
  4591. ////////////////////////////////////////////////////////////////////
  4592. // Function: GLGraphicsStateGuardian::setup_primitive
  4593. // Access: Public
  4594. // Description: Internal function to bind a buffer object for the
  4595. // indicated primitive's index list, if appropriate, or
  4596. // to unbind a buffer object if it should be rendered
  4597. // from client memory.
  4598. //
  4599. // If the buffer object is bound, this function sets
  4600. // client_pointer to NULL (representing the start of the
  4601. // buffer object in server memory); if the buffer object
  4602. // is not bound, this function sets client_pointer to to
  4603. // the data array in client memory, that is, the data
  4604. // array passed in.
  4605. //
  4606. // If force is not true, the function may return false
  4607. // indicating the data is not currently available.
  4608. ////////////////////////////////////////////////////////////////////
  4609. bool CLP(GraphicsStateGuardian)::
  4610. setup_primitive(const unsigned char *&client_pointer,
  4611. const GeomPrimitivePipelineReader *reader,
  4612. bool force) {
  4613. if (!_supports_buffers) {
  4614. // No support for buffer objects; always render from client.
  4615. client_pointer = reader->get_read_pointer(force);
  4616. return (client_pointer != NULL);
  4617. }
  4618. if (!vertex_buffers || _geom_display_list != 0 ||
  4619. reader->get_usage_hint() == Geom::UH_client) {
  4620. // The array specifies client rendering only, or buffer objects
  4621. // are configured off.
  4622. if (_current_ibuffer_index != 0) {
  4623. if (GLCAT.is_spam() && gl_debug_buffers) {
  4624. GLCAT.spam()
  4625. << "unbinding index buffer\n";
  4626. }
  4627. _glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  4628. _current_ibuffer_index = 0;
  4629. }
  4630. client_pointer = reader->get_read_pointer(force);
  4631. return (client_pointer != NULL);
  4632. }
  4633. // Prepare the buffer object and bind it.
  4634. IndexBufferContext *ibc = reader->prepare_now(get_prepared_objects(), this);
  4635. nassertr(ibc != (IndexBufferContext *)NULL, false);
  4636. if (!apply_index_buffer(ibc, reader, force)) {
  4637. return false;
  4638. }
  4639. // NULL is the OpenGL convention for the first byte of the buffer object.
  4640. client_pointer = NULL;
  4641. return true;
  4642. }
  4643. #ifndef OPENGLES
  4644. ////////////////////////////////////////////////////////////////////
  4645. // Function: GLGraphicsStateGuardian::begin_occlusion_query
  4646. // Access: Public, Virtual
  4647. // Description: Begins a new occlusion query. After this call, you
  4648. // may call begin_draw_primitives() and
  4649. // draw_triangles()/draw_whatever() repeatedly.
  4650. // Eventually, you should call end_occlusion_query()
  4651. // before the end of the frame; that will return a new
  4652. // OcclusionQueryContext object that will tell you how
  4653. // many pixels represented by the bracketed geometry
  4654. // passed the depth test.
  4655. //
  4656. // It is not valid to call begin_occlusion_query()
  4657. // between another begin_occlusion_query()
  4658. // .. end_occlusion_query() sequence.
  4659. ////////////////////////////////////////////////////////////////////
  4660. void CLP(GraphicsStateGuardian)::
  4661. begin_occlusion_query() {
  4662. nassertv(_supports_occlusion_query);
  4663. nassertv(_current_occlusion_query == (OcclusionQueryContext *)NULL);
  4664. PT(CLP(OcclusionQueryContext)) query = new CLP(OcclusionQueryContext)(this);
  4665. _glGenQueries(1, &query->_index);
  4666. if (GLCAT.is_debug()) {
  4667. GLCAT.debug()
  4668. << "beginning occlusion query index " << (int)query->_index << "\n";
  4669. }
  4670. _glBeginQuery(GL_SAMPLES_PASSED, query->_index);
  4671. _current_occlusion_query = query;
  4672. report_my_gl_errors();
  4673. }
  4674. #endif // !OPENGLES
  4675. #ifndef OPENGLES
  4676. ////////////////////////////////////////////////////////////////////
  4677. // Function: GLGraphicsStateGuardian::end_occlusion_query
  4678. // Access: Public, Virtual
  4679. // Description: Ends a previous call to begin_occlusion_query().
  4680. // This call returns the OcclusionQueryContext object
  4681. // that will (eventually) report the number of pixels
  4682. // that passed the depth test between the call to
  4683. // begin_occlusion_query() and end_occlusion_query().
  4684. ////////////////////////////////////////////////////////////////////
  4685. PT(OcclusionQueryContext) CLP(GraphicsStateGuardian)::
  4686. end_occlusion_query() {
  4687. nassertr(_current_occlusion_query != (OcclusionQueryContext *)NULL, NULL);
  4688. PT(OcclusionQueryContext) result = _current_occlusion_query;
  4689. GLuint index = DCAST(CLP(OcclusionQueryContext), result)->_index;
  4690. if (GLCAT.is_debug()) {
  4691. GLCAT.debug()
  4692. << "ending occlusion query index " << (int)index << "\n";
  4693. }
  4694. _current_occlusion_query = NULL;
  4695. _glEndQuery(GL_SAMPLES_PASSED);
  4696. // Temporary hack to try working around an apparent driver bug on
  4697. // iMacs. Occlusion queries sometimes incorrectly report 0 samples,
  4698. // unless we stall the pipe to keep fewer than a certain maximum
  4699. // number of queries pending at once.
  4700. static ConfigVariableInt limit_occlusion_queries("limit-occlusion-queries", 0);
  4701. if (limit_occlusion_queries > 0) {
  4702. if (index > (unsigned int)limit_occlusion_queries) {
  4703. PStatGPUTimer timer(this, _wait_occlusion_pcollector);
  4704. GLuint result;
  4705. _glGetQueryObjectuiv(index - (unsigned int)limit_occlusion_queries,
  4706. GL_QUERY_RESULT, &result);
  4707. }
  4708. }
  4709. report_my_gl_errors();
  4710. return result;
  4711. }
  4712. #endif // !OPENGLES
  4713. ////////////////////////////////////////////////////////////////////
  4714. // Function: GLGraphicsStateGuardian::issue_timer_query
  4715. // Access: Public, Virtual
  4716. // Description: Adds a timer query to the command stream, associated
  4717. // with the given PStats collector index.
  4718. ////////////////////////////////////////////////////////////////////
  4719. PT(TimerQueryContext) CLP(GraphicsStateGuardian)::
  4720. issue_timer_query(int pstats_index) {
  4721. #if defined(DO_PSTATS) && !defined(OPENGLES)
  4722. nassertr(_supports_timer_query, NULL);
  4723. PT(CLP(TimerQueryContext)) query;
  4724. // Hack
  4725. if (pstats_index == _command_latency_pcollector.get_index()) {
  4726. query = new CLP(LatencyQueryContext)(this, pstats_index);
  4727. } else {
  4728. query = new CLP(TimerQueryContext)(this, pstats_index);
  4729. }
  4730. if (_deleted_queries.size() >= 1) {
  4731. query->_index = _deleted_queries.back();
  4732. _deleted_queries.pop_back();
  4733. } else {
  4734. _glGenQueries(1, &query->_index);
  4735. if (GLCAT.is_spam()) {
  4736. GLCAT.spam() << "Generating query for " << pstats_index
  4737. << ": " << query->_index << "\n";
  4738. }
  4739. }
  4740. // Issue the timestamp query.
  4741. _glQueryCounter(query->_index, GL_TIMESTAMP);
  4742. if (_use_object_labels) {
  4743. // Assign a label to it based on the PStatCollector name.
  4744. const PStatClient *client = PStatClient::get_global_pstats();
  4745. string name = client->get_collector_fullname(pstats_index & 0x7fff);
  4746. _glObjectLabel(GL_QUERY, query->_index, name.size(), name.data());
  4747. }
  4748. _pending_timer_queries.push_back((TimerQueryContext *)query);
  4749. return (TimerQueryContext *)query;
  4750. #else
  4751. return NULL;
  4752. #endif
  4753. }
  4754. #ifndef OPENGLES
  4755. ////////////////////////////////////////////////////////////////////
  4756. // Function: GLGraphicsStateGuardian::dispatch_compute
  4757. // Access: Public, Virtual
  4758. // Description: Dispatches a currently bound compute shader using
  4759. // the given work group counts.
  4760. ////////////////////////////////////////////////////////////////////
  4761. void CLP(GraphicsStateGuardian)::
  4762. dispatch_compute(int num_groups_x, int num_groups_y, int num_groups_z) {
  4763. maybe_gl_finish();
  4764. PStatGPUTimer timer(this, _compute_dispatch_pcollector);
  4765. nassertv(_supports_compute_shaders);
  4766. nassertv(_current_shader_context != NULL);
  4767. _glDispatchCompute(num_groups_x, num_groups_y, num_groups_z);
  4768. maybe_gl_finish();
  4769. }
  4770. #endif // !OPENGLES
  4771. ////////////////////////////////////////////////////////////////////
  4772. // Function: GLGraphicsStateGuardian::make_geom_munger
  4773. // Access: Public, Virtual
  4774. // Description: Creates a new GeomMunger object to munge vertices
  4775. // appropriate to this GSG for the indicated state.
  4776. ////////////////////////////////////////////////////////////////////
  4777. PT(GeomMunger) CLP(GraphicsStateGuardian)::
  4778. make_geom_munger(const RenderState *state, Thread *current_thread) {
  4779. PT(CLP(GeomMunger)) munger = new CLP(GeomMunger)(this, state);
  4780. return GeomMunger::register_munger(munger, current_thread);
  4781. }
  4782. ////////////////////////////////////////////////////////////////////
  4783. // Function: GLGraphicsStateGuardian::compute_distance_to
  4784. // Access: Public, Virtual
  4785. // Description: This function will compute the distance to the
  4786. // indicated point, assumed to be in eye coordinates,
  4787. // from the camera plane. The point is assumed to be
  4788. // in the GSG's internal coordinate system.
  4789. ////////////////////////////////////////////////////////////////////
  4790. PN_stdfloat CLP(GraphicsStateGuardian)::
  4791. compute_distance_to(const LPoint3 &point) const {
  4792. return -point[2];
  4793. }
  4794. ////////////////////////////////////////////////////////////////////
  4795. // Function: GLGraphicsStateGuardian::framebuffer_copy_to_texture
  4796. // Access: Public, Virtual
  4797. // Description: Copy the pixels within the indicated display
  4798. // region from the framebuffer into texture memory.
  4799. //
  4800. // If z > -1, it is the cube map index or layer index
  4801. // into which to copy.
  4802. ////////////////////////////////////////////////////////////////////
  4803. bool CLP(GraphicsStateGuardian)::
  4804. framebuffer_copy_to_texture(Texture *tex, int view, int z,
  4805. const DisplayRegion *dr, const RenderBuffer &rb) {
  4806. nassertr(tex != NULL && dr != NULL, false);
  4807. set_read_buffer(rb._buffer_type);
  4808. clear_color_write_mask();
  4809. int xo, yo, w, h;
  4810. dr->get_region_pixels(xo, yo, w, h);
  4811. tex->set_size_padded(w, h, tex->get_z_size());
  4812. if (tex->get_compression() == Texture::CM_default) {
  4813. // Unless the user explicitly turned on texture compression, turn
  4814. // it off for the copy-to-texture case.
  4815. tex->set_compression(Texture::CM_off);
  4816. }
  4817. // Sanity check everything.
  4818. if (z >= 0) {
  4819. if (z >= tex->get_z_size()) {
  4820. // This can happen, when textures with different layer counts
  4821. // are attached to a buffer. We simply ignore this if it happens.
  4822. return false;
  4823. }
  4824. if ((w != tex->get_x_size()) ||
  4825. (h != tex->get_y_size())) {
  4826. return false;
  4827. }
  4828. if (tex->get_texture_type() == Texture::TT_cube_map) {
  4829. if (!_supports_cube_map) {
  4830. return false;
  4831. }
  4832. nassertr(z < 6, false);
  4833. if (w != h) {
  4834. return false;
  4835. }
  4836. } else if (tex->get_texture_type() == Texture::TT_3d_texture) {
  4837. if (!_supports_3d_texture) {
  4838. return false;
  4839. }
  4840. } else if (tex->get_texture_type() == Texture::TT_2d_texture_array) {
  4841. if (!_supports_2d_texture_array) {
  4842. return false;
  4843. }
  4844. } else {
  4845. GLCAT.error()
  4846. << "Don't know how to copy framebuffer to texture " << *tex << "\n";
  4847. }
  4848. } else {
  4849. nassertr(tex->get_texture_type() == Texture::TT_2d_texture, false);
  4850. }
  4851. // Match framebuffer format if necessary.
  4852. if (tex->get_match_framebuffer_format()) {
  4853. switch (tex->get_format()) {
  4854. case Texture::F_depth_component:
  4855. case Texture::F_depth_component16:
  4856. case Texture::F_depth_component24:
  4857. case Texture::F_depth_component32:
  4858. case Texture::F_depth_stencil:
  4859. // Don't remap if we're one of these special format.
  4860. break;
  4861. default:
  4862. // If the texture is a color format, we want to match the
  4863. // presence of sRGB and alpha according to the framebuffer.
  4864. if (_current_properties->get_srgb_color()) {
  4865. if (_current_properties->get_alpha_bits()) {
  4866. tex->set_format(Texture::F_srgb_alpha);
  4867. } else {
  4868. tex->set_format(Texture::F_srgb);
  4869. }
  4870. } else {
  4871. if (_current_properties->get_alpha_bits()) {
  4872. tex->set_format(Texture::F_rgba);
  4873. } else {
  4874. tex->set_format(Texture::F_rgb);
  4875. }
  4876. }
  4877. }
  4878. }
  4879. TextureContext *tc = tex->prepare_now(view, get_prepared_objects(), this);
  4880. nassertr(tc != (TextureContext *)NULL, false);
  4881. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  4882. apply_texture(gtc);
  4883. bool needs_reload = specify_texture(gtc, tex->get_default_sampler());
  4884. GLenum target = get_texture_target(tex->get_texture_type());
  4885. GLint internal_format = get_internal_image_format(tex);
  4886. int width = tex->get_x_size();
  4887. int height = tex->get_y_size();
  4888. int depth = tex->get_z_size();
  4889. bool uses_mipmaps = tex->uses_mipmaps() && !gl_ignore_mipmaps;
  4890. if (uses_mipmaps) {
  4891. if (_supports_generate_mipmap) {
  4892. #ifndef OPENGLES_2
  4893. if (_glGenerateMipmap == NULL) {
  4894. glTexParameteri(target, GL_GENERATE_MIPMAP, true);
  4895. }
  4896. #endif
  4897. } else {
  4898. // If we can't auto-generate mipmaps, do without mipmaps.
  4899. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  4900. uses_mipmaps = false;
  4901. }
  4902. }
  4903. bool new_image = needs_reload || gtc->was_image_modified();
  4904. if (z >= 0) {
  4905. if (target == GL_TEXTURE_CUBE_MAP) {
  4906. // Copy to a cube map face, which is treated as a 2D texture.
  4907. target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + z;
  4908. depth = 1;
  4909. z = -1;
  4910. // Cube map faces seem to have trouble with CopyTexSubImage, so we
  4911. // always reload the image.
  4912. new_image = true;
  4913. }
  4914. }
  4915. if (!gtc->_has_storage ||
  4916. internal_format != gtc->_internal_format ||
  4917. uses_mipmaps != gtc->_uses_mipmaps ||
  4918. width != gtc->_width ||
  4919. height != gtc->_height ||
  4920. depth != gtc->_depth) {
  4921. // If the texture properties have changed, we need to reload the
  4922. // image.
  4923. new_image = true;
  4924. }
  4925. if (new_image && gtc->_immutable) {
  4926. gtc->reset_data();
  4927. glBindTexture(target, gtc->_index);
  4928. }
  4929. #ifndef OPENGLES
  4930. if (gtc->needs_barrier(GL_TEXTURE_UPDATE_BARRIER_BIT)) {
  4931. // Make sure that any incoherent writes to this texture have been synced.
  4932. issue_memory_barrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
  4933. }
  4934. #endif
  4935. if (z >= 0) {
  4936. #ifndef OPENGLES_1
  4937. if (new_image) {
  4938. // These won't be used because we pass a NULL image, but we still
  4939. // have to specify them. Might as well use the actual values.
  4940. GLint external_format = get_external_image_format(tex);
  4941. GLint component_type = get_component_type(tex->get_component_type());
  4942. _glTexImage3D(target, 0, internal_format, width, height, depth, 0, external_format, component_type, NULL);
  4943. }
  4944. _glCopyTexSubImage3D(target, 0, 0, 0, z, xo, yo, w, h);
  4945. #endif
  4946. } else {
  4947. if (new_image) {
  4948. // We have to create a new image.
  4949. // It seems that OpenGL accepts a size higher than the framebuffer,
  4950. // but if we run into trouble we'll have to replace this with
  4951. // something smarter.
  4952. glCopyTexImage2D(target, 0, internal_format, xo, yo, width, height, 0);
  4953. } else {
  4954. // We can overlay the existing image.
  4955. glCopyTexSubImage2D(target, 0, 0, 0, xo, yo, w, h);
  4956. }
  4957. }
  4958. if (uses_mipmaps && _glGenerateMipmap != NULL) {
  4959. glEnable(target);
  4960. _glGenerateMipmap(target);
  4961. glDisable(target);
  4962. }
  4963. gtc->_has_storage = true;
  4964. gtc->_uses_mipmaps = uses_mipmaps;
  4965. gtc->_internal_format = internal_format;
  4966. gtc->_width = width;
  4967. gtc->_height = height;
  4968. gtc->_depth = depth;
  4969. gtc->mark_loaded();
  4970. gtc->enqueue_lru(&_prepared_objects->_graphics_memory_lru);
  4971. report_my_gl_errors();
  4972. // Force reload of texture state, since we've just monkeyed with it.
  4973. _state_mask.clear_bit(TextureAttrib::get_class_slot());
  4974. return true;
  4975. }
  4976. ////////////////////////////////////////////////////////////////////
  4977. // Function: GLGraphicsStateGuardian::framebuffer_copy_to_ram
  4978. // Access: Public, Virtual
  4979. // Description: Copy the pixels within the indicated display region
  4980. // from the framebuffer into system memory, not texture
  4981. // memory. Returns true on success, false on failure.
  4982. //
  4983. // This completely redefines the ram image of the
  4984. // indicated texture.
  4985. ////////////////////////////////////////////////////////////////////
  4986. bool CLP(GraphicsStateGuardian)::
  4987. framebuffer_copy_to_ram(Texture *tex, int view, int z,
  4988. const DisplayRegion *dr, const RenderBuffer &rb) {
  4989. nassertr(tex != NULL && dr != NULL, false);
  4990. set_read_buffer(rb._buffer_type);
  4991. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  4992. clear_color_write_mask();
  4993. // Bug fix for RE, RE2, and VTX - need to disable texturing in order
  4994. // for glReadPixels() to work
  4995. // NOTE: reading the depth buffer is *much* slower than reading the
  4996. // color buffer
  4997. set_state_and_transform(RenderState::make_empty(), _internal_transform);
  4998. int xo, yo, w, h;
  4999. dr->get_region_pixels(xo, yo, w, h);
  5000. Texture::ComponentType component_type = tex->get_component_type();
  5001. bool color_mode = false;
  5002. Texture::Format format = tex->get_format();
  5003. switch (format) {
  5004. case Texture::F_depth_stencil:
  5005. if (_current_properties->get_float_depth()) {
  5006. component_type = Texture::T_float;
  5007. } else {
  5008. component_type = Texture::T_unsigned_int_24_8;
  5009. }
  5010. break;
  5011. case Texture::F_depth_component:
  5012. if (_current_properties->get_float_depth()) {
  5013. component_type = Texture::T_float;
  5014. } else if (_current_properties->get_depth_bits() <= 8) {
  5015. component_type = Texture::T_unsigned_byte;
  5016. } else if (_current_properties->get_depth_bits() <= 16) {
  5017. component_type = Texture::T_unsigned_short;
  5018. } else {
  5019. component_type = Texture::T_float;
  5020. }
  5021. break;
  5022. default:
  5023. color_mode = true;
  5024. if (_current_properties->get_srgb_color()) {
  5025. if (_current_properties->get_alpha_bits()) {
  5026. format = Texture::F_srgb_alpha;
  5027. } else {
  5028. format = Texture::F_srgb;
  5029. }
  5030. } else {
  5031. if (_current_properties->get_alpha_bits()) {
  5032. format = Texture::F_rgba;
  5033. } else {
  5034. format = Texture::F_rgb;
  5035. }
  5036. }
  5037. if (_current_properties->get_float_color()) {
  5038. component_type = Texture::T_float;
  5039. } else if (_current_properties->get_color_bits() <= 24) {
  5040. component_type = Texture::T_unsigned_byte;
  5041. } else {
  5042. component_type = Texture::T_unsigned_short;
  5043. }
  5044. }
  5045. Texture::TextureType texture_type;
  5046. int z_size;
  5047. //TODO: should be extended to support 3D textures and 2D arrays.
  5048. if (z >= 0) {
  5049. texture_type = Texture::TT_cube_map;
  5050. z_size = 6;
  5051. } else {
  5052. texture_type = Texture::TT_2d_texture;
  5053. z_size = 1;
  5054. }
  5055. if (tex->get_x_size() != w || tex->get_y_size() != h ||
  5056. tex->get_z_size() != z_size ||
  5057. tex->get_component_type() != component_type ||
  5058. tex->get_format() != format ||
  5059. tex->get_texture_type() != texture_type) {
  5060. // Re-setup the texture; its properties have changed.
  5061. tex->setup_texture(texture_type, w, h, z_size,
  5062. component_type, format);
  5063. }
  5064. nassertr(z < tex->get_z_size(), false);
  5065. GLenum external_format = get_external_image_format(tex);
  5066. if (GLCAT.is_spam()) {
  5067. GLCAT.spam()
  5068. << "glReadPixels(" << xo << ", " << yo << ", " << w << ", " << h << ", ";
  5069. switch (external_format) {
  5070. case GL_DEPTH_COMPONENT:
  5071. GLCAT.spam(false) << "GL_DEPTH_COMPONENT, ";
  5072. break;
  5073. case GL_DEPTH_STENCIL:
  5074. GLCAT.spam(false) << "GL_DEPTH_STENCIL, ";
  5075. break;
  5076. case GL_RGB:
  5077. GLCAT.spam(false) << "GL_RGB, ";
  5078. break;
  5079. case GL_RGBA:
  5080. GLCAT.spam(false) << "GL_RGBA, ";
  5081. break;
  5082. #ifndef OPENGLES
  5083. case GL_BGR:
  5084. GLCAT.spam(false) << "GL_BGR, ";
  5085. break;
  5086. #endif
  5087. case GL_BGRA:
  5088. GLCAT.spam(false) << "GL_BGRA, ";
  5089. break;
  5090. default:
  5091. GLCAT.spam(false) << "unknown, ";
  5092. break;
  5093. }
  5094. switch (get_component_type(component_type)) {
  5095. case GL_UNSIGNED_BYTE:
  5096. GLCAT.spam(false) << "GL_UNSIGNED_BYTE";
  5097. break;
  5098. case GL_UNSIGNED_SHORT:
  5099. GLCAT.spam(false) << "GL_UNSIGNED_SHORT";
  5100. break;
  5101. case GL_FLOAT:
  5102. GLCAT.spam(false) << "GL_FLOAT";
  5103. break;
  5104. #ifndef OPENGLES_1
  5105. case GL_INT:
  5106. GLCAT.spam(false) << "GL_INT";
  5107. break;
  5108. #endif
  5109. default:
  5110. GLCAT.spam(false) << "unknown";
  5111. break;
  5112. }
  5113. GLCAT.spam(false)
  5114. << ")" << endl;
  5115. }
  5116. unsigned char *image_ptr = tex->modify_ram_image();
  5117. size_t image_size = tex->get_ram_image_size();
  5118. if (z >= 0 || view > 0) {
  5119. image_size = tex->get_expected_ram_page_size();
  5120. if (z >= 0) {
  5121. image_ptr += z * image_size;
  5122. }
  5123. if (view > 0) {
  5124. image_ptr += (view * tex->get_z_size()) * image_size;
  5125. }
  5126. }
  5127. glReadPixels(xo, yo, w, h, external_format,
  5128. get_component_type(component_type), image_ptr);
  5129. // We may have to reverse the byte ordering of the image if GL
  5130. // didn't do it for us.
  5131. if (color_mode && !_supports_bgr) {
  5132. PTA_uchar new_image;
  5133. const unsigned char *result =
  5134. fix_component_ordering(new_image, image_ptr, image_size,
  5135. external_format, tex);
  5136. if (result != image_ptr) {
  5137. memcpy(image_ptr, result, image_size);
  5138. }
  5139. }
  5140. report_my_gl_errors();
  5141. return true;
  5142. }
  5143. #ifdef SUPPORT_FIXED_FUNCTION
  5144. ////////////////////////////////////////////////////////////////////
  5145. // Function: GLGraphicsStateGuardian::apply_fog
  5146. // Access: Public, Virtual
  5147. // Description:
  5148. ////////////////////////////////////////////////////////////////////
  5149. void CLP(GraphicsStateGuardian)::
  5150. apply_fog(Fog *fog) {
  5151. Fog::Mode fmode = fog->get_mode();
  5152. glFogf(GL_FOG_MODE, get_fog_mode_type(fmode));
  5153. if (fmode == Fog::M_linear) {
  5154. PN_stdfloat onset, opaque;
  5155. fog->get_linear_range(onset, opaque);
  5156. glFogf(GL_FOG_START, onset);
  5157. glFogf(GL_FOG_END, opaque);
  5158. } else {
  5159. // Exponential fog is always camera-relative.
  5160. glFogf(GL_FOG_DENSITY, fog->get_exp_density());
  5161. }
  5162. call_glFogfv(GL_FOG_COLOR, fog->get_color());
  5163. report_my_gl_errors();
  5164. }
  5165. #endif // SUPPORT_FIXED_FUNCTION
  5166. ////////////////////////////////////////////////////////////////////
  5167. // Function: GLGraphicsStateGuardian::do_issue_transform
  5168. // Access: Protected
  5169. // Description: Sends the indicated transform matrix to the graphics
  5170. // API to be applied to future vertices.
  5171. //
  5172. // This transform is the internal_transform, already
  5173. // converted into the GSG's internal coordinate system.
  5174. ////////////////////////////////////////////////////////////////////
  5175. void CLP(GraphicsStateGuardian)::
  5176. do_issue_transform() {
  5177. #ifdef SUPPORT_FIXED_FUNCTION
  5178. // OpenGL ES 2 does not support glLoadMatrix.
  5179. const TransformState *transform = _internal_transform;
  5180. if (GLCAT.is_spam()) {
  5181. GLCAT.spam()
  5182. << "glLoadMatrix(GL_MODELVIEW): " << transform->get_mat() << endl;
  5183. }
  5184. DO_PSTATS_STUFF(_transform_state_pcollector.add_level(1));
  5185. glMatrixMode(GL_MODELVIEW);
  5186. GLPf(LoadMatrix)(transform->get_mat().get_data());
  5187. #endif
  5188. _transform_stale = false;
  5189. #ifndef OPENGLES_1
  5190. if (_current_shader_context) {
  5191. _current_shader_context->issue_parameters(Shader::SSD_transform);
  5192. }
  5193. #endif
  5194. report_my_gl_errors();
  5195. }
  5196. #ifdef SUPPORT_FIXED_FUNCTION
  5197. ////////////////////////////////////////////////////////////////////
  5198. // Function: GLGraphicsStateGuardian::do_issue_shade_model
  5199. // Access: Protected
  5200. // Description:
  5201. ////////////////////////////////////////////////////////////////////
  5202. void CLP(GraphicsStateGuardian)::
  5203. do_issue_shade_model() {
  5204. const ShadeModelAttrib *target_shade_model;
  5205. _target_rs->get_attrib_def(target_shade_model);
  5206. switch (target_shade_model->get_mode()) {
  5207. case ShadeModelAttrib::M_smooth:
  5208. glShadeModel(GL_SMOOTH);
  5209. _flat_shade_model = false;
  5210. break;
  5211. case ShadeModelAttrib::M_flat:
  5212. glShadeModel(GL_FLAT);
  5213. _flat_shade_model = true;
  5214. break;
  5215. }
  5216. }
  5217. #endif // SUPPORT_FIXED_FUNCTION
  5218. #ifndef OPENGLES_1
  5219. ////////////////////////////////////////////////////////////////////
  5220. // Function: GLGraphicsStateGuardian::do_issue_shader
  5221. // Access: Protected
  5222. // Description:
  5223. ////////////////////////////////////////////////////////////////////
  5224. void CLP(GraphicsStateGuardian)::
  5225. do_issue_shader(bool state_has_changed) {
  5226. ShaderContext *context = 0;
  5227. Shader *shader = (Shader *)(_target_shader->get_shader());
  5228. #ifndef SUPPORT_FIXED_FUNCTION
  5229. // If we don't have a shader, apply the default shader.
  5230. if (!shader) {
  5231. shader = _default_shader;
  5232. nassertv(shader != NULL);
  5233. }
  5234. #endif
  5235. if (shader) {
  5236. context = shader->prepare_now(get_prepared_objects(), this);
  5237. }
  5238. #ifndef SUPPORT_FIXED_FUNCTION
  5239. // If it failed, try applying the default shader.
  5240. if (shader != _default_shader && (context == 0 || !context->valid())) {
  5241. shader = _default_shader;
  5242. nassertv(shader != NULL);
  5243. context = shader->prepare_now(get_prepared_objects(), this);
  5244. }
  5245. #endif
  5246. if (context == 0 || (context->valid() == false)) {
  5247. if (_current_shader_context != 0) {
  5248. _current_shader_context->unbind();
  5249. _current_shader = 0;
  5250. _current_shader_context = 0;
  5251. }
  5252. } else {
  5253. if (context != _current_shader_context) {
  5254. // Use a completely different shader than before.
  5255. // Unbind old shader, bind the new one.
  5256. if (_current_shader_context != 0) {
  5257. _current_shader_context->unbind();
  5258. }
  5259. context->bind();
  5260. _current_shader = shader;
  5261. _current_shader_context = context;
  5262. context->issue_parameters(Shader::SSD_shaderinputs);
  5263. } else {
  5264. if (state_has_changed) {
  5265. // Use the same shader as before, but with new input arguments.
  5266. context->issue_parameters(Shader::SSD_shaderinputs);
  5267. }
  5268. }
  5269. }
  5270. report_my_gl_errors();
  5271. }
  5272. #endif // !OPENGLES_1
  5273. ////////////////////////////////////////////////////////////////////
  5274. // Function: GLGraphicsStateGuardian::do_issue_render_mode
  5275. // Access: Protected
  5276. // Description:
  5277. ////////////////////////////////////////////////////////////////////
  5278. void CLP(GraphicsStateGuardian)::
  5279. do_issue_render_mode() {
  5280. const RenderModeAttrib *target_render_mode;
  5281. _target_rs->get_attrib_def(target_render_mode);
  5282. _render_mode = target_render_mode->get_mode();
  5283. PN_stdfloat thickness = target_render_mode->get_thickness();
  5284. _point_perspective = target_render_mode->get_perspective();
  5285. #ifndef OPENGLES // glPolygonMode not supported by OpenGL ES.
  5286. switch (_render_mode) {
  5287. case RenderModeAttrib::M_unchanged:
  5288. case RenderModeAttrib::M_filled:
  5289. case RenderModeAttrib::M_filled_flat:
  5290. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  5291. break;
  5292. case RenderModeAttrib::M_wireframe:
  5293. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  5294. break;
  5295. case RenderModeAttrib::M_point:
  5296. glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  5297. break;
  5298. default:
  5299. GLCAT.error()
  5300. << "Unknown render mode " << (int)_render_mode << endl;
  5301. }
  5302. #endif // OPENGLES
  5303. // The thickness affects both the line width and the point size.
  5304. if (thickness != _point_size) {
  5305. glLineWidth(_point_size);
  5306. #ifndef OPENGLES_2
  5307. glPointSize(_point_size);
  5308. #endif
  5309. _point_size = thickness;
  5310. }
  5311. report_my_gl_errors();
  5312. do_point_size();
  5313. }
  5314. ////////////////////////////////////////////////////////////////////
  5315. // Function: GLGraphicsStateGuardian::do_issue_antialias
  5316. // Access: Protected
  5317. // Description:
  5318. ////////////////////////////////////////////////////////////////////
  5319. void CLP(GraphicsStateGuardian)::
  5320. do_issue_antialias() {
  5321. const AntialiasAttrib *target_antialias;
  5322. _target_rs->get_attrib_def(target_antialias);
  5323. if (target_antialias->get_mode_type() == AntialiasAttrib::M_auto) {
  5324. // In this special mode, we must enable antialiasing on a
  5325. // case-by-case basis, because we enable it differently for
  5326. // polygons and for points and lines.
  5327. _auto_antialias_mode = true;
  5328. } else {
  5329. // Otherwise, explicitly enable or disable according to the bits
  5330. // that are set. But if multisample is requested and supported,
  5331. // don't use the other bits at all (they will be ignored by GL
  5332. // anyway).
  5333. _auto_antialias_mode = false;
  5334. unsigned short mode = target_antialias->get_mode();
  5335. if (_supports_multisample &&
  5336. (mode & AntialiasAttrib::M_multisample) != 0) {
  5337. enable_multisample_antialias(true);
  5338. } else {
  5339. enable_multisample_antialias(false);
  5340. enable_line_smooth((mode & AntialiasAttrib::M_line) != 0);
  5341. enable_point_smooth((mode & AntialiasAttrib::M_point) != 0);
  5342. enable_polygon_smooth((mode & AntialiasAttrib::M_polygon) != 0);
  5343. }
  5344. }
  5345. #ifndef OPENGLES_2
  5346. switch (target_antialias->get_mode_quality()) {
  5347. case AntialiasAttrib::M_faster:
  5348. glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST);
  5349. glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST);
  5350. #ifndef OPENGLES
  5351. glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
  5352. #endif // OPENGLES
  5353. break;
  5354. case AntialiasAttrib::M_better:
  5355. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  5356. glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
  5357. #ifndef OPENGLES
  5358. glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
  5359. #endif // OPENGLES
  5360. break;
  5361. default:
  5362. glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  5363. glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
  5364. #ifndef OPENGLES
  5365. glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
  5366. #endif // OPENGLES
  5367. break;
  5368. }
  5369. #endif // !OPENGLES_2
  5370. report_my_gl_errors();
  5371. }
  5372. #ifdef SUPPORT_FIXED_FUNCTION // OpenGL ES 2.0 doesn't support rescaling normals.
  5373. ////////////////////////////////////////////////////////////////////
  5374. // Function: GLGraphicsStateGuardian::do_issue_rescale_normal
  5375. // Access: Protected
  5376. // Description:
  5377. ////////////////////////////////////////////////////////////////////
  5378. void CLP(GraphicsStateGuardian)::
  5379. do_issue_rescale_normal() {
  5380. RescaleNormalAttrib::Mode mode = RescaleNormalAttrib::M_none;
  5381. const RescaleNormalAttrib *target_rescale_normal;
  5382. if (_target_rs->get_attrib(target_rescale_normal)) {
  5383. mode = target_rescale_normal->get_mode();
  5384. }
  5385. switch (mode) {
  5386. case RescaleNormalAttrib::M_none:
  5387. glDisable(GL_NORMALIZE);
  5388. if (_supports_rescale_normal && support_rescale_normal) {
  5389. glDisable(GL_RESCALE_NORMAL);
  5390. }
  5391. break;
  5392. case RescaleNormalAttrib::M_rescale:
  5393. if (_supports_rescale_normal && support_rescale_normal) {
  5394. glEnable(GL_RESCALE_NORMAL);
  5395. glDisable(GL_NORMALIZE);
  5396. } else {
  5397. glEnable(GL_NORMALIZE);
  5398. }
  5399. break;
  5400. case RescaleNormalAttrib::M_normalize:
  5401. glEnable(GL_NORMALIZE);
  5402. if (_supports_rescale_normal && support_rescale_normal) {
  5403. glDisable(GL_RESCALE_NORMAL);
  5404. }
  5405. break;
  5406. default:
  5407. GLCAT.error()
  5408. << "Unknown rescale_normal mode " << (int)mode << endl;
  5409. }
  5410. report_my_gl_errors();
  5411. }
  5412. #endif // SUPPORT_FIXED_FUNCTION
  5413. // PandaCompareFunc - 1 + 0x200 === GL_NEVER, etc. order is sequential
  5414. #define PANDA_TO_GL_COMPAREFUNC(PANDACMPFUNC) (PANDACMPFUNC-1 +0x200)
  5415. ////////////////////////////////////////////////////////////////////
  5416. // Function: GLGraphicsStateGuardian::do_issue_depth_test
  5417. // Access: Protected
  5418. // Description:
  5419. ////////////////////////////////////////////////////////////////////
  5420. void CLP(GraphicsStateGuardian)::
  5421. do_issue_depth_test() {
  5422. const DepthTestAttrib *target_depth_test;
  5423. _target_rs->get_attrib_def(target_depth_test);
  5424. DepthTestAttrib::PandaCompareFunc mode = target_depth_test->get_mode();
  5425. if (mode == DepthTestAttrib::M_none) {
  5426. enable_depth_test(false);
  5427. } else {
  5428. enable_depth_test(true);
  5429. glDepthFunc(PANDA_TO_GL_COMPAREFUNC(mode));
  5430. }
  5431. report_my_gl_errors();
  5432. }
  5433. #ifdef SUPPORT_FIXED_FUNCTION
  5434. ////////////////////////////////////////////////////////////////////
  5435. // Function: GLGraphicsStateGuardian::do_issue_alpha_test
  5436. // Access: Protected
  5437. // Description:
  5438. ////////////////////////////////////////////////////////////////////
  5439. void CLP(GraphicsStateGuardian)::
  5440. do_issue_alpha_test() {
  5441. if (_target_shader->get_flag(ShaderAttrib::F_subsume_alpha_test)) {
  5442. enable_alpha_test(false);
  5443. } else {
  5444. const AlphaTestAttrib *target_alpha_test;
  5445. _target_rs->get_attrib_def(target_alpha_test);
  5446. AlphaTestAttrib::PandaCompareFunc mode = target_alpha_test->get_mode();
  5447. if (mode == AlphaTestAttrib::M_none) {
  5448. enable_alpha_test(false);
  5449. } else {
  5450. nassertv(GL_NEVER == (AlphaTestAttrib::M_never-1+0x200));
  5451. glAlphaFunc(PANDA_TO_GL_COMPAREFUNC(mode), target_alpha_test->get_reference_alpha());
  5452. enable_alpha_test(true);
  5453. }
  5454. }
  5455. }
  5456. #endif // SUPPORT_FIXED_FUNCTION
  5457. ////////////////////////////////////////////////////////////////////
  5458. // Function: GLGraphicsStateGuardian::do_issue_depth_write
  5459. // Access: Protected
  5460. // Description:
  5461. ////////////////////////////////////////////////////////////////////
  5462. void CLP(GraphicsStateGuardian)::
  5463. do_issue_depth_write() {
  5464. const DepthWriteAttrib *target_depth_write;
  5465. _target_rs->get_attrib_def(target_depth_write);
  5466. DepthWriteAttrib::Mode mode = target_depth_write->get_mode();
  5467. if (mode == DepthWriteAttrib::M_off) {
  5468. #ifdef GSG_VERBOSE
  5469. GLCAT.spam()
  5470. << "glDepthMask(GL_FALSE)" << endl;
  5471. #endif
  5472. glDepthMask(GL_FALSE);
  5473. } else {
  5474. #ifdef GSG_VERBOSE
  5475. GLCAT.spam()
  5476. << "glDepthMask(GL_TRUE)" << endl;
  5477. #endif
  5478. glDepthMask(GL_TRUE);
  5479. }
  5480. report_my_gl_errors();
  5481. }
  5482. ////////////////////////////////////////////////////////////////////
  5483. // Function: GLGraphicsStateGuardian::do_issue_cull_face
  5484. // Access: Protected
  5485. // Description:
  5486. ////////////////////////////////////////////////////////////////////
  5487. void CLP(GraphicsStateGuardian)::
  5488. do_issue_cull_face() {
  5489. const CullFaceAttrib *target_cull_face;
  5490. _target_rs->get_attrib_def(target_cull_face);
  5491. CullFaceAttrib::Mode mode = target_cull_face->get_effective_mode();
  5492. switch (mode) {
  5493. case CullFaceAttrib::M_cull_none:
  5494. glDisable(GL_CULL_FACE);
  5495. break;
  5496. case CullFaceAttrib::M_cull_clockwise:
  5497. glEnable(GL_CULL_FACE);
  5498. glCullFace(GL_BACK);
  5499. break;
  5500. case CullFaceAttrib::M_cull_counter_clockwise:
  5501. glEnable(GL_CULL_FACE);
  5502. glCullFace(GL_FRONT);
  5503. break;
  5504. default:
  5505. GLCAT.error()
  5506. << "invalid cull face mode " << (int)mode << endl;
  5507. break;
  5508. }
  5509. report_my_gl_errors();
  5510. }
  5511. #ifdef SUPPORT_FIXED_FUNCTION
  5512. ////////////////////////////////////////////////////////////////////
  5513. // Function: GLGraphicsStateGuardian::do_issue_fog
  5514. // Access: Protected
  5515. // Description:
  5516. ////////////////////////////////////////////////////////////////////
  5517. void CLP(GraphicsStateGuardian)::
  5518. do_issue_fog() {
  5519. const FogAttrib *target_fog;
  5520. _target_rs->get_attrib_def(target_fog);
  5521. if (!target_fog->is_off()) {
  5522. enable_fog(true);
  5523. Fog *fog = target_fog->get_fog();
  5524. nassertv(fog != (Fog *)NULL);
  5525. apply_fog(fog);
  5526. } else {
  5527. enable_fog(false);
  5528. }
  5529. report_my_gl_errors();
  5530. }
  5531. #endif // SUPPORT_FIXED_FUNCTION
  5532. ////////////////////////////////////////////////////////////////////
  5533. // Function: GLGraphicsStateGuardian::do_issue_depth_offset
  5534. // Access: Protected
  5535. // Description:
  5536. ////////////////////////////////////////////////////////////////////
  5537. void CLP(GraphicsStateGuardian)::
  5538. do_issue_depth_offset() {
  5539. const DepthOffsetAttrib *target_depth_offset = (const DepthOffsetAttrib *)
  5540. _target_rs->get_attrib_def(DepthOffsetAttrib::get_class_slot());
  5541. int offset = target_depth_offset->get_offset();
  5542. if (offset != 0) {
  5543. // The relationship between these two parameters is a little
  5544. // unclear and poorly explained in the GL man pages.
  5545. glPolygonOffset((GLfloat) -offset, (GLfloat) -offset);
  5546. enable_polygon_offset(true);
  5547. } else {
  5548. enable_polygon_offset(false);
  5549. }
  5550. PN_stdfloat min_value = target_depth_offset->get_min_value();
  5551. PN_stdfloat max_value = target_depth_offset->get_max_value();
  5552. #ifdef GSG_VERBOSE
  5553. GLCAT.spam()
  5554. << "glDepthRange(" << min_value << ", " << max_value << ")" << endl;
  5555. #endif
  5556. #ifdef OPENGLES
  5557. // OpenGL ES uses a single-precision call.
  5558. glDepthRangef((GLclampf)min_value, (GLclampf)max_value);
  5559. #else
  5560. // Mainline OpenGL uses a double-precision call.
  5561. glDepthRange((GLclampd)min_value, (GLclampd)max_value);
  5562. #endif // OPENGLES
  5563. report_my_gl_errors();
  5564. }
  5565. #ifdef SUPPORT_FIXED_FUNCTION
  5566. ////////////////////////////////////////////////////////////////////
  5567. // Function: GLGraphicsStateGuardian::do_issue_material
  5568. // Access: Protected
  5569. // Description:
  5570. ////////////////////////////////////////////////////////////////////
  5571. void CLP(GraphicsStateGuardian)::
  5572. do_issue_material() {
  5573. static Material empty;
  5574. const Material *material;
  5575. const MaterialAttrib *target_material;
  5576. _target_rs->get_attrib_def(target_material);
  5577. if (target_material == (MaterialAttrib *)NULL ||
  5578. target_material->is_off()) {
  5579. material = &empty;
  5580. } else {
  5581. material = target_material->get_material();
  5582. }
  5583. bool has_material_force_color = _has_material_force_color;
  5584. #ifndef NDEBUG
  5585. if (_show_texture_usage) {
  5586. // In show_texture_usage mode, all colors are white, so as not
  5587. // to contaminate the texture color. This means we disable
  5588. // lighting materials too.
  5589. material = &empty;
  5590. has_material_force_color = false;
  5591. }
  5592. #endif // NDEBUG
  5593. #ifdef OPENGLES
  5594. const GLenum face = GL_FRONT_AND_BACK;
  5595. #else
  5596. GLenum face = material->get_twoside() ? GL_FRONT_AND_BACK : GL_FRONT;
  5597. #endif
  5598. call_glMaterialfv(face, GL_SPECULAR, material->get_specular());
  5599. call_glMaterialfv(face, GL_EMISSION, material->get_emission());
  5600. glMaterialf(face, GL_SHININESS, min(material->get_shininess(), (PN_stdfloat)128.0));
  5601. if (material->has_ambient() && material->has_diffuse()) {
  5602. // The material has both an ambient and diffuse specified. This
  5603. // means we do not need glMaterialColor().
  5604. glDisable(GL_COLOR_MATERIAL);
  5605. call_glMaterialfv(face, GL_AMBIENT, material->get_ambient());
  5606. call_glMaterialfv(face, GL_DIFFUSE, material->get_diffuse());
  5607. } else if (material->has_ambient()) {
  5608. // The material specifies an ambient, but not a diffuse component.
  5609. // The diffuse component comes from the object's color.
  5610. call_glMaterialfv(face, GL_AMBIENT, material->get_ambient());
  5611. if (has_material_force_color) {
  5612. glDisable(GL_COLOR_MATERIAL);
  5613. call_glMaterialfv(face, GL_DIFFUSE, _material_force_color);
  5614. } else {
  5615. #ifndef OPENGLES
  5616. glColorMaterial(face, GL_DIFFUSE);
  5617. #endif // OPENGLES
  5618. glEnable(GL_COLOR_MATERIAL);
  5619. }
  5620. } else if (material->has_diffuse()) {
  5621. // The material specifies a diffuse, but not an ambient component.
  5622. // The ambient component comes from the object's color.
  5623. call_glMaterialfv(face, GL_DIFFUSE, material->get_diffuse());
  5624. if (has_material_force_color) {
  5625. glDisable(GL_COLOR_MATERIAL);
  5626. call_glMaterialfv(face, GL_AMBIENT, _material_force_color);
  5627. } else {
  5628. #ifndef OPENGLES
  5629. glColorMaterial(face, GL_AMBIENT);
  5630. #endif // OPENGLES
  5631. glEnable(GL_COLOR_MATERIAL);
  5632. }
  5633. } else {
  5634. // The material specifies neither a diffuse nor an ambient
  5635. // component. Both components come from the object's color.
  5636. if (has_material_force_color) {
  5637. glDisable(GL_COLOR_MATERIAL);
  5638. call_glMaterialfv(face, GL_AMBIENT, _material_force_color);
  5639. call_glMaterialfv(face, GL_DIFFUSE, _material_force_color);
  5640. } else {
  5641. #ifndef OPENGLES
  5642. glColorMaterial(face, GL_AMBIENT_AND_DIFFUSE);
  5643. #endif // OPENGLES
  5644. glEnable(GL_COLOR_MATERIAL);
  5645. }
  5646. }
  5647. #ifndef OPENGLES
  5648. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, material->get_local());
  5649. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, material->get_twoside());
  5650. if (gl_separate_specular_color) {
  5651. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
  5652. } else {
  5653. glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
  5654. }
  5655. #endif
  5656. report_my_gl_errors();
  5657. }
  5658. #endif // SUPPORT_FIXED_FUNCTION
  5659. ////////////////////////////////////////////////////////////////////
  5660. // Function: GLGraphicsStateGuardian::do_issue_blending
  5661. // Access: Protected
  5662. // Description:
  5663. ////////////////////////////////////////////////////////////////////
  5664. void CLP(GraphicsStateGuardian)::
  5665. do_issue_blending() {
  5666. // Handle the color_write attrib. If color_write is off, then
  5667. // all the other blending-related stuff doesn't matter. If the
  5668. // device doesn't support color-write, we use blending tricks
  5669. // to effectively disable color write.
  5670. const ColorWriteAttrib *target_color_write;
  5671. _target_rs->get_attrib_def(target_color_write);
  5672. unsigned int color_channels =
  5673. target_color_write->get_channels() & _color_write_mask;
  5674. if (_target_shader->get_flag(ShaderAttrib::F_disable_alpha_write)) {
  5675. color_channels &= ~(ColorWriteAttrib::C_alpha);
  5676. }
  5677. if (color_channels == ColorWriteAttrib::C_off) {
  5678. int color_write_slot = ColorWriteAttrib::get_class_slot();
  5679. enable_multisample_alpha_one(false);
  5680. enable_multisample_alpha_mask(false);
  5681. if (gl_color_mask) {
  5682. enable_blend(false);
  5683. set_color_write_mask(ColorWriteAttrib::C_off);
  5684. } else {
  5685. enable_blend(true);
  5686. _glBlendEquation(GL_FUNC_ADD);
  5687. glBlendFunc(GL_ZERO, GL_ONE);
  5688. }
  5689. return;
  5690. } else {
  5691. set_color_write_mask(color_channels);
  5692. }
  5693. const ColorBlendAttrib *target_color_blend;
  5694. _target_rs->get_attrib_def(target_color_blend);
  5695. CPT(ColorBlendAttrib) color_blend = target_color_blend;
  5696. ColorBlendAttrib::Mode color_blend_mode = target_color_blend->get_mode();
  5697. const TransparencyAttrib *target_transparency;
  5698. _target_rs->get_attrib_def(target_transparency);
  5699. TransparencyAttrib::Mode transparency_mode = target_transparency->get_mode();
  5700. _color_blend_involves_color_scale = color_blend->involves_color_scale();
  5701. // Is there a color blend set?
  5702. if (color_blend_mode != ColorBlendAttrib::M_none) {
  5703. enable_multisample_alpha_one(false);
  5704. enable_multisample_alpha_mask(false);
  5705. enable_blend(true);
  5706. _glBlendEquation(get_blend_equation_type(color_blend_mode));
  5707. glBlendFunc(get_blend_func(color_blend->get_operand_a()),
  5708. get_blend_func(color_blend->get_operand_b()));
  5709. if (_color_blend_involves_color_scale) {
  5710. // Apply the current color scale to the blend mode.
  5711. _glBlendColor(_current_color_scale[0], _current_color_scale[1],
  5712. _current_color_scale[2], _current_color_scale[3]);
  5713. } else {
  5714. LColor c = color_blend->get_color();
  5715. _glBlendColor(c[0], c[1], c[2], c[3]);
  5716. }
  5717. return;
  5718. }
  5719. // No color blend; is there a transparency set?
  5720. switch (transparency_mode) {
  5721. case TransparencyAttrib::M_none:
  5722. case TransparencyAttrib::M_binary:
  5723. break;
  5724. case TransparencyAttrib::M_alpha:
  5725. case TransparencyAttrib::M_dual:
  5726. enable_multisample_alpha_one(false);
  5727. enable_multisample_alpha_mask(false);
  5728. enable_blend(true);
  5729. _glBlendEquation(GL_FUNC_ADD);
  5730. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  5731. return;
  5732. case TransparencyAttrib::M_multisample:
  5733. // We need to enable *both* of these in M_multisample case.
  5734. enable_multisample_alpha_one(true);
  5735. enable_multisample_alpha_mask(true);
  5736. enable_blend(false);
  5737. return;
  5738. case TransparencyAttrib::M_multisample_mask:
  5739. enable_multisample_alpha_one(false);
  5740. enable_multisample_alpha_mask(true);
  5741. enable_blend(false);
  5742. return;
  5743. default:
  5744. GLCAT.error()
  5745. << "invalid transparency mode " << (int)transparency_mode << endl;
  5746. break;
  5747. }
  5748. if (_line_smooth_enabled || _point_smooth_enabled) {
  5749. // If we have either of these turned on, we also need to have
  5750. // blend mode enabled in order to see it.
  5751. enable_multisample_alpha_one(false);
  5752. enable_multisample_alpha_mask(false);
  5753. enable_blend(true);
  5754. _glBlendEquation(GL_FUNC_ADD);
  5755. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  5756. return;
  5757. }
  5758. // For best polygon smoothing, we need:
  5759. // (1) a frame buffer that supports alpha
  5760. // (2) sort polygons front-to-back
  5761. // (3) glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
  5762. //
  5763. // Since these modes have other implications for the application, we
  5764. // don't attempt to do this by default. If you really want good
  5765. // polygon smoothing (and you don't have multisample support), do
  5766. // all this yourself.
  5767. // Nothing's set, so disable blending.
  5768. enable_multisample_alpha_one(false);
  5769. enable_multisample_alpha_mask(false);
  5770. enable_blend(false);
  5771. }
  5772. #ifdef SUPPORT_FIXED_FUNCTION
  5773. ////////////////////////////////////////////////////////////////////
  5774. // Function: GLGraphicsStateGuardian::bind_light
  5775. // Access: Public, Virtual
  5776. // Description: Called the first time a particular light has been
  5777. // bound to a given id within a frame, this should set
  5778. // up the associated hardware light with the light's
  5779. // properties.
  5780. ////////////////////////////////////////////////////////////////////
  5781. void CLP(GraphicsStateGuardian)::
  5782. bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
  5783. // static PStatCollector _draw_set_state_light_bind_point_pcollector("Draw:Set State:Light:Bind:Point");
  5784. // PStatGPUTimer timer(this, _draw_set_state_light_bind_point_pcollector);
  5785. GLenum id = get_light_id(light_id);
  5786. static const LColor black(0.0f, 0.0f, 0.0f, 1.0f);
  5787. call_glLightfv(id, GL_AMBIENT, black);
  5788. call_glLightfv(id, GL_DIFFUSE, get_light_color(light_obj));
  5789. call_glLightfv(id, GL_SPECULAR, light_obj->get_specular_color());
  5790. // Position needs to specify x, y, z, and w
  5791. // w == 1 implies non-infinite position
  5792. CPT(TransformState) transform = light.get_transform(_scene_setup->get_scene_root().get_parent());
  5793. LPoint3 pos = light_obj->get_point() * transform->get_mat();
  5794. LPoint4 fpos(pos[0], pos[1], pos[2], 1.0f);
  5795. call_glLightfv(id, GL_POSITION, fpos);
  5796. // GL_SPOT_DIRECTION is not significant when cutoff == 180
  5797. // Exponent == 0 implies uniform light distribution
  5798. glLightf(id, GL_SPOT_EXPONENT, 0.0f);
  5799. // Cutoff == 180 means uniform point light source
  5800. glLightf(id, GL_SPOT_CUTOFF, 180.0f);
  5801. const LVecBase3 &att = light_obj->get_attenuation();
  5802. glLightf(id, GL_CONSTANT_ATTENUATION, att[0]);
  5803. glLightf(id, GL_LINEAR_ATTENUATION, att[1]);
  5804. glLightf(id, GL_QUADRATIC_ATTENUATION, att[2]);
  5805. report_my_gl_errors();
  5806. }
  5807. #endif // SUPPORT_FIXED_FUNCTION
  5808. #ifdef SUPPORT_FIXED_FUNCTION
  5809. ////////////////////////////////////////////////////////////////////
  5810. // Function: GLGraphicsStateGuardian::bind_light
  5811. // Access: Public, Virtual
  5812. // Description: Called the first time a particular light has been
  5813. // bound to a given id within a frame, this should set
  5814. // up the associated hardware light with the light's
  5815. // properties.
  5816. ////////////////////////////////////////////////////////////////////
  5817. void CLP(GraphicsStateGuardian)::
  5818. bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
  5819. // static PStatCollector _draw_set_state_light_bind_directional_pcollector("Draw:Set State:Light:Bind:Directional");
  5820. // PStatGPUTimer timer(this, _draw_set_state_light_bind_directional_pcollector);
  5821. pair<DirectionalLights::iterator, bool> lookup = _dlights.insert(DirectionalLights::value_type(light, DirectionalLightFrameData()));
  5822. DirectionalLightFrameData &fdata = (*lookup.first).second;
  5823. if (lookup.second) {
  5824. // The light was not computed yet this frame. Compute it now.
  5825. CPT(TransformState) transform = light.get_transform(_scene_setup->get_scene_root().get_parent());
  5826. LVector3 dir = light_obj->get_direction() * transform->get_mat();
  5827. fdata._neg_dir.set(-dir[0], -dir[1], -dir[2], 0);
  5828. }
  5829. GLenum id = get_light_id( light_id );
  5830. static const LColor black(0.0f, 0.0f, 0.0f, 1.0f);
  5831. call_glLightfv(id, GL_AMBIENT, black);
  5832. call_glLightfv(id, GL_DIFFUSE, get_light_color(light_obj));
  5833. call_glLightfv(id, GL_SPECULAR, light_obj->get_specular_color());
  5834. // Position needs to specify x, y, z, and w.
  5835. // w == 0 implies light is at infinity
  5836. call_glLightfv(id, GL_POSITION, fdata._neg_dir);
  5837. // GL_SPOT_DIRECTION is not significant when cutoff == 180
  5838. // In this case, position x, y, z specifies direction
  5839. // Exponent == 0 implies uniform light distribution
  5840. glLightf(id, GL_SPOT_EXPONENT, 0.0f);
  5841. // Cutoff == 180 means uniform point light source
  5842. glLightf(id, GL_SPOT_CUTOFF, 180.0f);
  5843. // Default attenuation values (only spotlight and point light can
  5844. // modify these)
  5845. glLightf(id, GL_CONSTANT_ATTENUATION, 1.0f);
  5846. glLightf(id, GL_LINEAR_ATTENUATION, 0.0f);
  5847. glLightf(id, GL_QUADRATIC_ATTENUATION, 0.0f);
  5848. report_my_gl_errors();
  5849. }
  5850. #endif // SUPPORT_FIXED_FUNCTION
  5851. #ifdef SUPPORT_FIXED_FUNCTION
  5852. ////////////////////////////////////////////////////////////////////
  5853. // Function: GLGraphicsStateGuardian::bind_light
  5854. // Access: Public, Virtual
  5855. // Description: Called the first time a particular light has been
  5856. // bound to a given id within a frame, this should set
  5857. // up the associated hardware light with the light's
  5858. // properties.
  5859. ////////////////////////////////////////////////////////////////////
  5860. void CLP(GraphicsStateGuardian)::
  5861. bind_light(Spotlight *light_obj, const NodePath &light, int light_id) {
  5862. // static PStatCollector _draw_set_state_light_bind_spotlight_pcollector("Draw:Set State:Light:Bind:Spotlight");
  5863. // PStatGPUTimer timer(this, _draw_set_state_light_bind_spotlight_pcollector);
  5864. Lens *lens = light_obj->get_lens();
  5865. nassertv(lens != (Lens *)NULL);
  5866. GLenum id = get_light_id(light_id);
  5867. static const LColor black(0.0f, 0.0f, 0.0f, 1.0f);
  5868. call_glLightfv(id, GL_AMBIENT, black);
  5869. call_glLightfv(id, GL_DIFFUSE, get_light_color(light_obj));
  5870. call_glLightfv(id, GL_SPECULAR, light_obj->get_specular_color());
  5871. // Position needs to specify x, y, z, and w
  5872. // w == 1 implies non-infinite position
  5873. CPT(TransformState) transform = light.get_transform(_scene_setup->get_scene_root().get_parent());
  5874. const LMatrix4 &light_mat = transform->get_mat();
  5875. LPoint3 pos = lens->get_nodal_point() * light_mat;
  5876. LVector3 dir = lens->get_view_vector() * light_mat;
  5877. LPoint4 fpos(pos[0], pos[1], pos[2], 1.0f);
  5878. call_glLightfv(id, GL_POSITION, fpos);
  5879. call_glLightfv(id, GL_SPOT_DIRECTION, dir);
  5880. glLightf(id, GL_SPOT_EXPONENT, light_obj->get_exponent());
  5881. glLightf(id, GL_SPOT_CUTOFF, lens->get_hfov() * 0.5f);
  5882. const LVecBase3 &att = light_obj->get_attenuation();
  5883. glLightf(id, GL_CONSTANT_ATTENUATION, att[0]);
  5884. glLightf(id, GL_LINEAR_ATTENUATION, att[1]);
  5885. glLightf(id, GL_QUADRATIC_ATTENUATION, att[2]);
  5886. report_my_gl_errors();
  5887. }
  5888. #endif // SUPPORT_FIXED_FUNCTION
  5889. #ifdef SUPPORT_IMMEDIATE_MODE
  5890. ////////////////////////////////////////////////////////////////////
  5891. // Function: GLGraphicsStateGuardian::draw_immediate_simple_primitives
  5892. // Access: Protected
  5893. // Description: Uses the ImmediateModeSender to draw a series of
  5894. // primitives of the indicated type.
  5895. ////////////////////////////////////////////////////////////////////
  5896. void CLP(GraphicsStateGuardian)::
  5897. draw_immediate_simple_primitives(const GeomPrimitivePipelineReader *reader, GLenum mode) {
  5898. int num_vertices = reader->get_num_vertices();
  5899. _vertices_immediate_pcollector.add_level(num_vertices);
  5900. glBegin(mode);
  5901. if (reader->is_indexed()) {
  5902. for (int v = 0; v < num_vertices; ++v) {
  5903. _sender.set_vertex(reader->get_vertex(v));
  5904. _sender.issue_vertex();
  5905. }
  5906. } else {
  5907. _sender.set_vertex(reader->get_first_vertex());
  5908. for (int v = 0; v < num_vertices; ++v) {
  5909. _sender.issue_vertex();
  5910. }
  5911. }
  5912. glEnd();
  5913. }
  5914. #endif // SUPPORT_IMMEDIATE_MODE
  5915. #ifdef SUPPORT_IMMEDIATE_MODE
  5916. ////////////////////////////////////////////////////////////////////
  5917. // Function: GLGraphicsStateGuardian::draw_immediate_composite_primitives
  5918. // Access: Protected
  5919. // Description: Uses the ImmediateModeSender to draw a series of
  5920. // primitives of the indicated type. This form is for
  5921. // primitive types like tristrips which must involve
  5922. // several begin/end groups.
  5923. ////////////////////////////////////////////////////////////////////
  5924. void CLP(GraphicsStateGuardian)::
  5925. draw_immediate_composite_primitives(const GeomPrimitivePipelineReader *reader, GLenum mode) {
  5926. int num_vertices = reader->get_num_vertices();
  5927. _vertices_immediate_pcollector.add_level(num_vertices);
  5928. CPTA_int ends = reader->get_ends();
  5929. int num_unused_vertices_per_primitive = reader->get_object()->get_num_unused_vertices_per_primitive();
  5930. if (reader->is_indexed()) {
  5931. int begin = 0;
  5932. CPTA_int::const_iterator ei;
  5933. for (ei = ends.begin(); ei != ends.end(); ++ei) {
  5934. int end = (*ei);
  5935. glBegin(mode);
  5936. for (int v = begin; v < end; ++v) {
  5937. _sender.set_vertex(reader->get_vertex(v));
  5938. _sender.issue_vertex();
  5939. }
  5940. glEnd();
  5941. begin = end + num_unused_vertices_per_primitive;
  5942. }
  5943. } else {
  5944. _sender.set_vertex(reader->get_first_vertex());
  5945. int begin = 0;
  5946. CPTA_int::const_iterator ei;
  5947. for (ei = ends.begin(); ei != ends.end(); ++ei) {
  5948. int end = (*ei);
  5949. glBegin(mode);
  5950. for (int v = begin; v < end; ++v) {
  5951. _sender.issue_vertex();
  5952. }
  5953. glEnd();
  5954. begin = end + num_unused_vertices_per_primitive;
  5955. }
  5956. }
  5957. }
  5958. #endif // SUPPORT_IMMEDIATE_MODE
  5959. ////////////////////////////////////////////////////////////////////
  5960. // Function: GLGraphicsStateGuardian::gl_flush
  5961. // Access: Protected, Virtual
  5962. // Description: Calls glFlush().
  5963. ////////////////////////////////////////////////////////////////////
  5964. void CLP(GraphicsStateGuardian)::
  5965. gl_flush() const {
  5966. PStatTimer timer(_flush_pcollector);
  5967. glFlush();
  5968. }
  5969. ////////////////////////////////////////////////////////////////////
  5970. // Function: GLGraphicsStateGuardian::gl_get_error
  5971. // Access: Protected, Virtual
  5972. // Description: Returns the result of glGetError().
  5973. ////////////////////////////////////////////////////////////////////
  5974. GLenum CLP(GraphicsStateGuardian)::
  5975. gl_get_error() const {
  5976. if (_check_errors) {
  5977. PStatTimer timer(_check_error_pcollector);
  5978. return glGetError();
  5979. } else {
  5980. return GL_NO_ERROR;
  5981. }
  5982. }
  5983. ////////////////////////////////////////////////////////////////////
  5984. // Function: GLGraphicsStateGuardian::report_errors_loop
  5985. // Access: Protected, Static
  5986. // Description: The internal implementation of report_errors().
  5987. // Don't call this function; use report_errors()
  5988. // instead. The return value is true if everything is
  5989. // ok, or false if we should shut down.
  5990. ////////////////////////////////////////////////////////////////////
  5991. bool CLP(GraphicsStateGuardian)::
  5992. report_errors_loop(int line, const char *source_file, GLenum error_code,
  5993. int &error_count) {
  5994. while ((gl_max_errors < 0 || error_count < gl_max_errors) &&
  5995. (error_code != GL_NO_ERROR)) {
  5996. GLCAT.error()
  5997. << "at " << line << " of " << source_file << " : "
  5998. << get_error_string(error_code) << "\n";
  5999. error_code = glGetError();
  6000. error_count++;
  6001. }
  6002. return (error_code == GL_NO_ERROR);
  6003. }
  6004. ////////////////////////////////////////////////////////////////////
  6005. // Function: GLGraphicsStateGuardian::get_error_string
  6006. // Access: Protected, Static
  6007. // Description: Returns an error string for an OpenGL error code.
  6008. ////////////////////////////////////////////////////////////////////
  6009. string CLP(GraphicsStateGuardian)::
  6010. get_error_string(GLenum error_code) {
  6011. // We used to use gluErrorString here, but I (rdb) took it out
  6012. // because that was really the only function we used from GLU.
  6013. // The idea with the error table was taken from SGI's sample implementation.
  6014. static const char *error_strings[GL_OUT_OF_MEMORY - GL_INVALID_ENUM + 1] = {
  6015. "invalid enumerant",
  6016. "invalid value",
  6017. "invalid operation",
  6018. "stack overflow",
  6019. "stack underflow",
  6020. "out of memory",
  6021. };
  6022. if (error_code == GL_NO_ERROR) {
  6023. return "no error";
  6024. #ifndef OPENGLES
  6025. } else if (error_code == GL_TABLE_TOO_LARGE) {
  6026. return "table too large";
  6027. #endif
  6028. } else if (error_code >= GL_INVALID_ENUM && error_code <= GL_OUT_OF_MEMORY) {
  6029. return error_strings[error_code - GL_INVALID_ENUM];
  6030. }
  6031. // Other error, somehow? Just display the error code then.
  6032. ostringstream strm;
  6033. strm << "GL error " << (int)error_code;
  6034. return strm.str();
  6035. }
  6036. ////////////////////////////////////////////////////////////////////
  6037. // Function: GLGraphicsStateGuardian::show_gl_string
  6038. // Access: Protected
  6039. // Description: Outputs the result of glGetString() on the indicated
  6040. // tag. The output string is returned.
  6041. ////////////////////////////////////////////////////////////////////
  6042. string CLP(GraphicsStateGuardian)::
  6043. show_gl_string(const string &name, GLenum id) {
  6044. string result;
  6045. const GLubyte *text = glGetString(id);
  6046. if (text == (const GLubyte *)NULL) {
  6047. GLCAT.warning()
  6048. << "Unable to query " << name << "\n";
  6049. } else {
  6050. result = (const char *)text;
  6051. if (GLCAT.is_debug()) {
  6052. GLCAT.debug()
  6053. << name << " = " << result << "\n";
  6054. }
  6055. }
  6056. return result;
  6057. }
  6058. ////////////////////////////////////////////////////////////////////
  6059. // Function: GLGraphicsStateGuardian::query_gl_version
  6060. // Access: Protected, Virtual
  6061. // Description: Queries the runtime version of OpenGL in use.
  6062. ////////////////////////////////////////////////////////////////////
  6063. void CLP(GraphicsStateGuardian)::
  6064. query_gl_version() {
  6065. _gl_vendor = show_gl_string("GL_VENDOR", GL_VENDOR);
  6066. _gl_renderer = show_gl_string("GL_RENDERER", GL_RENDERER);
  6067. _gl_version = show_gl_string("GL_VERSION", GL_VERSION);
  6068. _gl_version_major = 0;
  6069. _gl_version_minor = 0;
  6070. // This is the most preposterous driver bug: NVIDIA drivers will claim
  6071. // that the version is 1.2 as long as the process is named pview.exe!
  6072. #ifndef OPENGLES
  6073. if (_gl_version.substr(0, 10) == "1.2 NVIDIA") {
  6074. Filename exec_name = ExecutionEnvironment::get_binary_name();
  6075. if (cmp_nocase(exec_name.get_basename(), "pview.exe") == 0) {
  6076. glGetIntegerv(GL_MAJOR_VERSION, &_gl_version_major);
  6077. glGetIntegerv(GL_MINOR_VERSION, &_gl_version_minor);
  6078. if (glGetError() == GL_INVALID_ENUM) {
  6079. _gl_version_major = 1;
  6080. _gl_version_minor = 2;
  6081. GLCAT.warning()
  6082. << "Driver possibly misreported GL_VERSION! Unable to detect "
  6083. "correct OpenGL version.\n";
  6084. } else if (_gl_version_major != 1 || _gl_version_minor != 2) {
  6085. GLCAT.debug()
  6086. << "Driver misreported GL_VERSION! Correct version detected as "
  6087. << _gl_version_major << "." << _gl_version_minor << "\n";
  6088. }
  6089. return;
  6090. }
  6091. }
  6092. // If we asked for a GL 3 context, let's first try and see if we
  6093. // can use the OpenGL 3 way to query version.
  6094. if (gl_version.get_num_words() > 0 && gl_version[0] >= 3) {
  6095. glGetIntegerv(GL_MAJOR_VERSION, &_gl_version_major);
  6096. glGetIntegerv(GL_MINOR_VERSION, &_gl_version_minor);
  6097. if (_gl_version_major >= 1) {
  6098. // Fair enough, seems to check out.
  6099. if (GLCAT.is_debug()) {
  6100. GLCAT.debug()
  6101. << "Detected OpenGL version: "
  6102. << _gl_version_major << "." << _gl_version_minor << "\n";
  6103. }
  6104. return;
  6105. }
  6106. }
  6107. #endif // !OPENGLES
  6108. // Otherwise, parse the GL_VERSION string.
  6109. if (_gl_version.empty()) {
  6110. GLCAT.error() << "Unable to detect OpenGL version\n";
  6111. } else {
  6112. string input = _gl_version;
  6113. // Skip any initial words that don't begin with a digit.
  6114. while (!input.empty() && !isdigit(input[0])) {
  6115. size_t space = input.find(' ');
  6116. if (space == string::npos) {
  6117. break;
  6118. }
  6119. size_t next = space + 1;
  6120. while (next < input.length() && isspace(input[next])) {
  6121. ++next;
  6122. }
  6123. input = input.substr(next);
  6124. }
  6125. // Truncate after the first space.
  6126. size_t space = input.find(' ');
  6127. if (space != string::npos) {
  6128. input = input.substr(0, space);
  6129. }
  6130. vector_string components;
  6131. tokenize(input, components, ".");
  6132. if (components.size() >= 1) {
  6133. string_to_int(components[0], _gl_version_major);
  6134. }
  6135. if (components.size() >= 2) {
  6136. string_to_int(components[1], _gl_version_minor);
  6137. }
  6138. if (GLCAT.is_debug()) {
  6139. GLCAT.debug()
  6140. << "GL_VERSION decoded to: "
  6141. << _gl_version_major << "." << _gl_version_minor
  6142. << "\n";
  6143. }
  6144. }
  6145. }
  6146. ////////////////////////////////////////////////////////////////////
  6147. // Function: GLGraphicsStateGuardian::query_glsl_version
  6148. // Access: Protected
  6149. // Description: Queries the supported GLSL version.
  6150. ////////////////////////////////////////////////////////////////////
  6151. void CLP(GraphicsStateGuardian)::
  6152. query_glsl_version() {
  6153. _gl_shadlang_ver_major = 0;
  6154. _gl_shadlang_ver_minor = 0;
  6155. #ifndef OPENGLES
  6156. // OpenGL 2.0 introduces GLSL in the core. In 1.x, it is an extension.
  6157. if (_gl_version_major >= 2 || has_extension("GL_ARB_shading_language_100")) {
  6158. string ver = show_gl_string("GL_SHADING_LANGUAGE_VERSION", GL_SHADING_LANGUAGE_VERSION);
  6159. _gl_shadlang_ver_major = 1;
  6160. _gl_shadlang_ver_minor = (_gl_version_major >= 2) ? 1 : 0;
  6161. if (ver.empty() ||
  6162. sscanf(ver.c_str(), "%d.%d", &_gl_shadlang_ver_major,
  6163. &_gl_shadlang_ver_minor) != 2) {
  6164. GLCAT.warning() << "Invalid GL_SHADING_LANGUAGE_VERSION format.\n";
  6165. }
  6166. }
  6167. #elif !defined(OPENGLES_1)
  6168. // OpenGL ES 2.0 and above has shader support built-in.
  6169. string ver = show_gl_string("GL_SHADING_LANGUAGE_VERSION", GL_SHADING_LANGUAGE_VERSION);
  6170. _gl_shadlang_ver_major = 1;
  6171. _gl_shadlang_ver_minor = 0;
  6172. if (ver.empty() ||
  6173. sscanf(ver.c_str(), "OpenGL ES GLSL %d.%d", &_gl_shadlang_ver_major,
  6174. &_gl_shadlang_ver_minor) != 2) {
  6175. GLCAT.warning() << "Invalid GL_SHADING_LANGUAGE_VERSION format.\n";
  6176. }
  6177. #endif
  6178. #ifndef OPENGLES_1
  6179. if (GLCAT.is_debug()) {
  6180. GLCAT.debug()
  6181. << "Detected GLSL version: "
  6182. << _gl_shadlang_ver_major << "." << _gl_shadlang_ver_minor << "\n";
  6183. }
  6184. #endif
  6185. }
  6186. ////////////////////////////////////////////////////////////////////
  6187. // Function: GLGraphicsStateGuardian::save_extensions
  6188. // Access: Protected
  6189. // Description: Separates the string returned by GL_EXTENSIONS (or
  6190. // glx or wgl extensions) into its individual tokens
  6191. // and saves them in the _extensions member.
  6192. ////////////////////////////////////////////////////////////////////
  6193. void CLP(GraphicsStateGuardian)::
  6194. save_extensions(const char *extensions) {
  6195. if (extensions != (const char *)NULL) {
  6196. vector_string tokens;
  6197. extract_words(extensions, tokens);
  6198. vector_string::iterator ti;
  6199. for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
  6200. _extensions.insert(*ti);
  6201. }
  6202. }
  6203. }
  6204. ////////////////////////////////////////////////////////////////////
  6205. // Function: GLGraphicsStateGuardian::get_extra_extensions
  6206. // Access: Protected, Virtual
  6207. // Description: This may be redefined by a derived class (e.g. glx or
  6208. // wgl) to get whatever further extensions strings may
  6209. // be appropriate to that interface, in addition to the
  6210. // GL extension strings return by glGetString().
  6211. ////////////////////////////////////////////////////////////////////
  6212. void CLP(GraphicsStateGuardian)::
  6213. get_extra_extensions() {
  6214. }
  6215. ////////////////////////////////////////////////////////////////////
  6216. // Function: GLGraphicsStateGuardian::report_extensions
  6217. // Access: Protected
  6218. // Description: Outputs the list of GL extensions to notify, if debug
  6219. // mode is enabled.
  6220. ////////////////////////////////////////////////////////////////////
  6221. void CLP(GraphicsStateGuardian)::
  6222. report_extensions() const {
  6223. if (GLCAT.is_debug()) {
  6224. ostream &out = GLCAT.debug();
  6225. out << "GL Extensions:\n";
  6226. size_t maxlen = 0;
  6227. pset<string>::const_iterator ei;
  6228. for (ei = _extensions.begin(); ei != _extensions.end(); ++ei) {
  6229. size_t len = (*ei).size();
  6230. out << " " << (*ei);
  6231. // Display a second column.
  6232. if (len <= 38) {
  6233. if (++ei != _extensions.end()) {
  6234. for (int i = len; i < 38; ++i) {
  6235. out.put(' ');
  6236. }
  6237. out << ' ' << (*ei);
  6238. }
  6239. }
  6240. out.put('\n');
  6241. }
  6242. }
  6243. }
  6244. ////////////////////////////////////////////////////////////////////
  6245. // Function: GLGraphicsStateGuardian::get_extension_func
  6246. // Access: Public
  6247. // Description: Returns the pointer to the GL extension function with
  6248. // the indicated name, or NULL if the function is not
  6249. // available.
  6250. ////////////////////////////////////////////////////////////////////
  6251. void *CLP(GraphicsStateGuardian)::
  6252. get_extension_func(const char *name) {
  6253. // First, look in the static-compiled namespace. If we were
  6254. // compiled to expect at least a certain minimum runtime version of
  6255. // OpenGL, then we can expect those extension functions to be
  6256. // available at compile time. Somewhat more reliable than poking
  6257. // around in the runtime pointers.
  6258. static struct {
  6259. const char *name;
  6260. void *fptr;
  6261. } compiled_function_table[] = {
  6262. #ifdef EXPECT_GL_VERSION_1_2
  6263. { "glBlendColor", (void *)&glBlendColor },
  6264. { "glBlendEquation", (void *)&glBlendEquation },
  6265. { "glDrawRangeElements", (void *)&glDrawRangeElements },
  6266. { "glTexImage3D", (void *)&glTexImage3D },
  6267. { "glTexSubImage3D", (void *)&glTexSubImage3D },
  6268. { "glCopyTexSubImage3D", (void *)&glCopyTexSubImage3D },
  6269. #endif
  6270. #ifdef EXPECT_GL_VERSION_1_3
  6271. { "glActiveTexture", (void *)&glActiveTexture },
  6272. { "glClientActiveTexture", (void *)&glClientActiveTexture },
  6273. { "glCompressedTexImage1D", (void *)&glCompressedTexImage1D },
  6274. { "glCompressedTexImage2D", (void *)&glCompressedTexImage2D },
  6275. { "glCompressedTexImage3D", (void *)&glCompressedTexImage3D },
  6276. { "glCompressedTexSubImage1D", (void *)&glCompressedTexSubImage1D },
  6277. { "glCompressedTexSubImage2D", (void *)&glCompressedTexSubImage2D },
  6278. { "glCompressedTexSubImage3D", (void *)&glCompressedTexSubImage3D },
  6279. { "glGetCompressedTexImage", (void *)&glGetCompressedTexImage },
  6280. { "glMultiTexCoord1f", (void *)&glMultiTexCoord1f },
  6281. { "glMultiTexCoord2", (void *)&glMultiTexCoord2 },
  6282. { "glMultiTexCoord3", (void *)&glMultiTexCoord3 },
  6283. { "glMultiTexCoord4", (void *)&glMultiTexCoord4 },
  6284. #endif
  6285. #ifdef EXPECT_GL_VERSION_1_4
  6286. { "glPointParameterfv", (void *)&glPointParameterfv },
  6287. { "glSecondaryColorPointer", (void *)&glSecondaryColorPointer },
  6288. #endif
  6289. #ifdef EXPECT_GL_VERSION_1_5
  6290. { "glBeginQuery", (void *)&glBeginQuery },
  6291. { "glBindBuffer", (void *)&glBindBuffer },
  6292. { "glBufferData", (void *)&glBufferData },
  6293. { "glBufferSubData", (void *)&glBufferSubData },
  6294. { "glDeleteBuffers", (void *)&glDeleteBuffers },
  6295. { "glDeleteQueries", (void *)&glDeleteQueries },
  6296. { "glEndQuery", (void *)&glEndQuery },
  6297. { "glGenBuffers", (void *)&glGenBuffers },
  6298. { "glGenQueries", (void *)&glGenQueries },
  6299. { "glGetQueryObjectuiv", (void *)&glGetQueryObjectuiv },
  6300. { "glGetQueryiv", (void *)&glGetQueryiv },
  6301. #endif
  6302. #ifdef OPENGLES
  6303. { "glActiveTexture", (void *)&glActiveTexture },
  6304. #ifndef OPENGLES_2
  6305. { "glClientActiveTexture", (void *)&glClientActiveTexture },
  6306. #endif
  6307. { "glBindBuffer", (void *)&glBindBuffer },
  6308. { "glBufferData", (void *)&glBufferData },
  6309. { "glBufferSubData", (void *)&glBufferSubData },
  6310. { "glDeleteBuffers", (void *)&glDeleteBuffers },
  6311. { "glGenBuffers", (void *)&glGenBuffers },
  6312. #endif
  6313. { NULL, NULL }
  6314. };
  6315. int i = 0;
  6316. while (compiled_function_table[i].name != NULL) {
  6317. if (strcmp(compiled_function_table[i].name, name) == 0) {
  6318. return compiled_function_table[i].fptr;
  6319. }
  6320. ++i;
  6321. }
  6322. // If the extension function wasn't compiled in, then go get it from
  6323. // the runtime. There's a different interface for each API.
  6324. return do_get_extension_func(name);
  6325. }
  6326. ////////////////////////////////////////////////////////////////////
  6327. // Function: GLGraphicsStateGuardian::do_get_extension_func
  6328. // Access: Public, Virtual
  6329. // Description: This is the virtual implementation of
  6330. // get_extension_func(). Each API-specific GL
  6331. // implementation will map this method to the
  6332. // appropriate API call to retrieve the extension
  6333. // function pointer. Returns NULL if the function is
  6334. // not available.
  6335. ////////////////////////////////////////////////////////////////////
  6336. void *CLP(GraphicsStateGuardian)::
  6337. do_get_extension_func(const char *) {
  6338. return NULL;
  6339. }
  6340. ////////////////////////////////////////////////////////////////////
  6341. // Function: GLGraphicsStateGuardian::set_draw_buffer
  6342. // Access: Protected
  6343. // Description: Sets up the glDrawBuffer to render into the buffer
  6344. // indicated by the RenderBuffer object. This only sets
  6345. // up the color and aux bits; it does not affect the depth,
  6346. // stencil, accum layers.
  6347. ////////////////////////////////////////////////////////////////////
  6348. void CLP(GraphicsStateGuardian)::
  6349. set_draw_buffer(int rbtype) {
  6350. #ifndef OPENGLES // Draw buffers not supported by OpenGL ES.
  6351. if (_current_fbo) {
  6352. GLuint buffers[16];
  6353. int nbuffers = 0;
  6354. int index = 0;
  6355. if (_current_properties->get_color_bits() > 0) {
  6356. if (rbtype & RenderBuffer::T_left) {
  6357. buffers[nbuffers++] = GL_COLOR_ATTACHMENT0_EXT + index;
  6358. }
  6359. ++index;
  6360. if (_current_properties->is_stereo()) {
  6361. if (rbtype & RenderBuffer::T_right) {
  6362. buffers[nbuffers++] = GL_COLOR_ATTACHMENT0_EXT + index;
  6363. }
  6364. ++index;
  6365. }
  6366. }
  6367. for (int i = 0; i < _current_properties->get_aux_rgba(); ++i) {
  6368. if (rbtype & (RenderBuffer::T_aux_rgba_0 << i)) {
  6369. buffers[nbuffers++] = GL_COLOR_ATTACHMENT0_EXT + index;
  6370. }
  6371. ++index;
  6372. }
  6373. for (int i = 0; i < _current_properties->get_aux_hrgba(); ++i) {
  6374. if (rbtype & (RenderBuffer::T_aux_hrgba_0 << i)) {
  6375. buffers[nbuffers++] = GL_COLOR_ATTACHMENT0_EXT + index;
  6376. }
  6377. ++index;
  6378. }
  6379. for (int i = 0; i < _current_properties->get_aux_float(); ++i) {
  6380. if (rbtype & (RenderBuffer::T_aux_float_0 << i)) {
  6381. buffers[nbuffers++] = GL_COLOR_ATTACHMENT0_EXT + index;
  6382. }
  6383. ++index;
  6384. }
  6385. _glDrawBuffers(nbuffers, buffers);
  6386. } else {
  6387. switch (rbtype & RenderBuffer::T_color) {
  6388. case RenderBuffer::T_front:
  6389. glDrawBuffer(GL_FRONT);
  6390. break;
  6391. case RenderBuffer::T_back:
  6392. glDrawBuffer(GL_BACK);
  6393. break;
  6394. case RenderBuffer::T_right:
  6395. glDrawBuffer(GL_RIGHT);
  6396. break;
  6397. case RenderBuffer::T_left:
  6398. glDrawBuffer(GL_LEFT);
  6399. break;
  6400. case RenderBuffer::T_front_right:
  6401. nassertv(_current_properties->is_stereo());
  6402. glDrawBuffer(GL_FRONT_RIGHT);
  6403. break;
  6404. case RenderBuffer::T_front_left:
  6405. nassertv(_current_properties->is_stereo());
  6406. glDrawBuffer(GL_FRONT_LEFT);
  6407. break;
  6408. case RenderBuffer::T_back_right:
  6409. nassertv(_current_properties->is_stereo());
  6410. glDrawBuffer(GL_BACK_RIGHT);
  6411. break;
  6412. case RenderBuffer::T_back_left:
  6413. nassertv(_current_properties->is_stereo());
  6414. glDrawBuffer(GL_BACK_LEFT);
  6415. break;
  6416. default:
  6417. break;
  6418. }
  6419. }
  6420. #endif // OPENGLES
  6421. // Also ensure that any global color channels are masked out.
  6422. set_color_write_mask(_color_write_mask);
  6423. report_my_gl_errors();
  6424. }
  6425. ////////////////////////////////////////////////////////////////////
  6426. // Function: GLGraphicsStateGuardian::set_read_buffer
  6427. // Access: Protected
  6428. // Description: Sets up the glReadBuffer to render into the buffer
  6429. // indicated by the RenderBuffer object. This only sets
  6430. // up the color bits; it does not affect the depth,
  6431. // stencil, accum layers.
  6432. ////////////////////////////////////////////////////////////////////
  6433. void CLP(GraphicsStateGuardian)::
  6434. set_read_buffer(int rbtype) {
  6435. #ifndef OPENGLES // Draw buffers not supported by OpenGL ES.
  6436. if (rbtype & (RenderBuffer::T_depth | RenderBuffer::T_stencil)) {
  6437. // Special case: don't have to call ReadBuffer for these.
  6438. return;
  6439. }
  6440. if (_current_fbo) {
  6441. GLuint buffer = GL_COLOR_ATTACHMENT0_EXT;
  6442. int index = 1;
  6443. if (_current_properties->is_stereo()) {
  6444. if (rbtype & RenderBuffer::T_right) {
  6445. buffer = GL_COLOR_ATTACHMENT1_EXT;
  6446. }
  6447. ++index;
  6448. }
  6449. for (int i = 0; i < _current_properties->get_aux_rgba(); ++i) {
  6450. if (rbtype & (RenderBuffer::T_aux_rgba_0 << i)) {
  6451. buffer = GL_COLOR_ATTACHMENT0_EXT + index;
  6452. }
  6453. ++index;
  6454. }
  6455. for (int i = 0; i < _current_properties->get_aux_hrgba(); ++i) {
  6456. if (rbtype & (RenderBuffer::T_aux_hrgba_0 << i)) {
  6457. buffer = GL_COLOR_ATTACHMENT0_EXT + index;
  6458. }
  6459. ++index;
  6460. }
  6461. for (int i = 0; i < _current_properties->get_aux_float(); ++i) {
  6462. if (rbtype & (RenderBuffer::T_aux_float_0 << i)) {
  6463. buffer = GL_COLOR_ATTACHMENT0_EXT + index;
  6464. }
  6465. ++index;
  6466. }
  6467. glReadBuffer(buffer);
  6468. } else {
  6469. switch (rbtype & RenderBuffer::T_color) {
  6470. case RenderBuffer::T_front:
  6471. glReadBuffer(GL_FRONT);
  6472. break;
  6473. case RenderBuffer::T_back:
  6474. glReadBuffer(GL_BACK);
  6475. break;
  6476. case RenderBuffer::T_right:
  6477. glReadBuffer(GL_RIGHT);
  6478. break;
  6479. case RenderBuffer::T_left:
  6480. glReadBuffer(GL_LEFT);
  6481. break;
  6482. case RenderBuffer::T_front_right:
  6483. glReadBuffer(GL_FRONT_RIGHT);
  6484. break;
  6485. case RenderBuffer::T_front_left:
  6486. glReadBuffer(GL_FRONT_LEFT);
  6487. break;
  6488. case RenderBuffer::T_back_right:
  6489. glReadBuffer(GL_BACK_RIGHT);
  6490. break;
  6491. case RenderBuffer::T_back_left:
  6492. glReadBuffer(GL_BACK_LEFT);
  6493. break;
  6494. default:
  6495. break;
  6496. }
  6497. }
  6498. report_my_gl_errors();
  6499. #endif // OPENGLES
  6500. }
  6501. ////////////////////////////////////////////////////////////////////
  6502. // Function: GLGraphicsStateGuardian::get_numeric_type
  6503. // Access: Protected, Static
  6504. // Description: Maps from the Geom's internal numeric type symbols
  6505. // to GL's.
  6506. ////////////////////////////////////////////////////////////////////
  6507. GLenum CLP(GraphicsStateGuardian)::
  6508. get_numeric_type(Geom::NumericType numeric_type) {
  6509. switch (numeric_type) {
  6510. case Geom::NT_uint16:
  6511. return GL_UNSIGNED_SHORT;
  6512. case Geom::NT_uint32:
  6513. #ifndef OPENGLES_1
  6514. return GL_UNSIGNED_INT;
  6515. #else
  6516. break;
  6517. #endif
  6518. case Geom::NT_uint8:
  6519. case Geom::NT_packed_dcba:
  6520. case Geom::NT_packed_dabc:
  6521. return GL_UNSIGNED_BYTE;
  6522. case Geom::NT_float32:
  6523. return GL_FLOAT;
  6524. case Geom::NT_float64:
  6525. #ifndef OPENGLES
  6526. return GL_DOUBLE;
  6527. #else
  6528. break;
  6529. #endif
  6530. case Geom::NT_stdfloat:
  6531. // Shouldn't happen, display error.
  6532. break;
  6533. case Geom::NT_int8:
  6534. return GL_BYTE;
  6535. case Geom::NT_int16:
  6536. return GL_SHORT;
  6537. case Geom::NT_int32:
  6538. #ifndef OPENGLES_1
  6539. return GL_INT;
  6540. #else
  6541. break;
  6542. #endif
  6543. case Geom::NT_packed_ufloat:
  6544. #ifndef OPENGLES
  6545. return GL_UNSIGNED_INT_10F_11F_11F_REV;
  6546. #else
  6547. break;
  6548. #endif
  6549. }
  6550. GLCAT.error()
  6551. << "Invalid NumericType value (" << (int)numeric_type << ")\n";
  6552. return GL_UNSIGNED_BYTE;
  6553. }
  6554. ////////////////////////////////////////////////////////////////////
  6555. // Function: GLGraphicsStateGuardian::get_texture_target
  6556. // Access: Protected
  6557. // Description: Maps from the Texture's texture type symbols to
  6558. // GL's.
  6559. ////////////////////////////////////////////////////////////////////
  6560. GLenum CLP(GraphicsStateGuardian)::
  6561. get_texture_target(Texture::TextureType texture_type) const {
  6562. switch (texture_type) {
  6563. case Texture::TT_1d_texture:
  6564. // There are no 1D textures in OpenGL ES. Fall back to 2D textures.
  6565. #ifndef OPENGLES
  6566. return GL_TEXTURE_1D;
  6567. #endif
  6568. case Texture::TT_2d_texture:
  6569. return GL_TEXTURE_2D;
  6570. case Texture::TT_3d_texture:
  6571. #ifndef OPENGLES_1
  6572. if (_supports_3d_texture) {
  6573. return GL_TEXTURE_3D;
  6574. }
  6575. #endif
  6576. return GL_NONE;
  6577. case Texture::TT_2d_texture_array:
  6578. #ifndef OPENGLES
  6579. if (_supports_2d_texture_array) {
  6580. return GL_TEXTURE_2D_ARRAY_EXT;
  6581. }
  6582. #endif
  6583. return GL_NONE;
  6584. case Texture::TT_cube_map:
  6585. if (_supports_cube_map) {
  6586. return GL_TEXTURE_CUBE_MAP;
  6587. } else {
  6588. return GL_NONE;
  6589. }
  6590. case Texture::TT_buffer_texture:
  6591. #ifndef OPENGLES
  6592. if (_supports_buffer_texture) {
  6593. return GL_TEXTURE_BUFFER;
  6594. }
  6595. #endif
  6596. return GL_NONE;
  6597. }
  6598. GLCAT.error() << "Invalid Texture::TextureType value!\n";
  6599. return GL_TEXTURE_2D;
  6600. }
  6601. ////////////////////////////////////////////////////////////////////
  6602. // Function: GLGraphicsStateGuardian::get_texture_wrap_mode
  6603. // Access: Protected
  6604. // Description: Maps from the Texture's internal wrap mode symbols to
  6605. // GL's.
  6606. ////////////////////////////////////////////////////////////////////
  6607. GLenum CLP(GraphicsStateGuardian)::
  6608. get_texture_wrap_mode(SamplerState::WrapMode wm) const {
  6609. if (gl_ignore_clamp) {
  6610. return GL_REPEAT;
  6611. }
  6612. switch (wm) {
  6613. case SamplerState::WM_clamp:
  6614. return _edge_clamp;
  6615. case SamplerState::WM_repeat:
  6616. return GL_REPEAT;
  6617. case SamplerState::WM_mirror:
  6618. return _mirror_repeat;
  6619. case SamplerState::WM_mirror_once:
  6620. return _mirror_border_clamp;
  6621. case SamplerState::WM_border_color:
  6622. return _border_clamp;
  6623. case SamplerState::WM_invalid:
  6624. break;
  6625. }
  6626. GLCAT.error() << "Invalid SamplerState::WrapMode value!\n";
  6627. return _edge_clamp;
  6628. }
  6629. ////////////////////////////////////////////////////////////////////
  6630. // Function: GLGraphicsStateGuardian::get_panda_wrap_mode
  6631. // Access: Protected, Static
  6632. // Description: Maps from the GL's internal wrap mode symbols to
  6633. // Panda's.
  6634. ////////////////////////////////////////////////////////////////////
  6635. SamplerState::WrapMode CLP(GraphicsStateGuardian)::
  6636. get_panda_wrap_mode(GLenum wm) {
  6637. switch (wm) {
  6638. #ifndef OPENGLES
  6639. case GL_CLAMP:
  6640. #endif
  6641. case GL_CLAMP_TO_EDGE:
  6642. return SamplerState::WM_clamp;
  6643. #ifndef OPENGLES
  6644. case GL_CLAMP_TO_BORDER:
  6645. return SamplerState::WM_border_color;
  6646. #endif
  6647. case GL_REPEAT:
  6648. return SamplerState::WM_repeat;
  6649. #ifndef OPENGLES
  6650. case GL_MIRROR_CLAMP_EXT:
  6651. case GL_MIRROR_CLAMP_TO_EDGE_EXT:
  6652. return SamplerState::WM_mirror;
  6653. case GL_MIRROR_CLAMP_TO_BORDER_EXT:
  6654. return SamplerState::WM_mirror_once;
  6655. #endif
  6656. }
  6657. GLCAT.error() << "Unexpected GL wrap mode " << (int)wm << "\n";
  6658. return SamplerState::WM_clamp;
  6659. }
  6660. ////////////////////////////////////////////////////////////////////
  6661. // Function: GLGraphicsStateGuardian::get_texture_filter_type
  6662. // Access: Protected, Static
  6663. // Description: Maps from the Texture's internal filter type symbols
  6664. // to GL's.
  6665. ////////////////////////////////////////////////////////////////////
  6666. GLenum CLP(GraphicsStateGuardian)::
  6667. get_texture_filter_type(SamplerState::FilterType ft, bool ignore_mipmaps) {
  6668. if (gl_ignore_filters) {
  6669. return GL_NEAREST;
  6670. } else if (ignore_mipmaps) {
  6671. switch (ft) {
  6672. case SamplerState::FT_nearest_mipmap_nearest:
  6673. case SamplerState::FT_nearest:
  6674. return GL_NEAREST;
  6675. case SamplerState::FT_linear:
  6676. case SamplerState::FT_linear_mipmap_nearest:
  6677. case SamplerState::FT_nearest_mipmap_linear:
  6678. case SamplerState::FT_linear_mipmap_linear:
  6679. return GL_LINEAR;
  6680. case SamplerState::FT_shadow:
  6681. return GL_LINEAR;
  6682. case SamplerState::FT_default:
  6683. case SamplerState::FT_invalid:
  6684. break;
  6685. }
  6686. } else {
  6687. switch (ft) {
  6688. case SamplerState::FT_nearest:
  6689. return GL_NEAREST;
  6690. case SamplerState::FT_linear:
  6691. return GL_LINEAR;
  6692. case SamplerState::FT_nearest_mipmap_nearest:
  6693. return GL_NEAREST_MIPMAP_NEAREST;
  6694. case SamplerState::FT_linear_mipmap_nearest:
  6695. return GL_LINEAR_MIPMAP_NEAREST;
  6696. case SamplerState::FT_nearest_mipmap_linear:
  6697. return GL_NEAREST_MIPMAP_LINEAR;
  6698. case SamplerState::FT_linear_mipmap_linear:
  6699. return GL_LINEAR_MIPMAP_LINEAR;
  6700. case SamplerState::FT_shadow:
  6701. return GL_LINEAR;
  6702. case SamplerState::FT_default:
  6703. case SamplerState::FT_invalid:
  6704. break;
  6705. }
  6706. }
  6707. GLCAT.error() << "Invalid SamplerState::FilterType value!\n";
  6708. return GL_NEAREST;
  6709. }
  6710. ////////////////////////////////////////////////////////////////////
  6711. // Function: GLGraphicsStateGuardian::get_panda_filter_type
  6712. // Access: Protected, Static
  6713. // Description: Maps from the GL's internal filter type symbols
  6714. // to Panda's.
  6715. ////////////////////////////////////////////////////////////////////
  6716. SamplerState::FilterType CLP(GraphicsStateGuardian)::
  6717. get_panda_filter_type(GLenum ft) {
  6718. switch (ft) {
  6719. case GL_NEAREST:
  6720. return SamplerState::FT_nearest;
  6721. case GL_LINEAR:
  6722. return SamplerState::FT_linear;
  6723. case GL_NEAREST_MIPMAP_NEAREST:
  6724. return SamplerState::FT_nearest_mipmap_nearest;
  6725. case GL_LINEAR_MIPMAP_NEAREST:
  6726. return SamplerState::FT_linear_mipmap_nearest;
  6727. case GL_NEAREST_MIPMAP_LINEAR:
  6728. return SamplerState::FT_nearest_mipmap_linear;
  6729. case GL_LINEAR_MIPMAP_LINEAR:
  6730. return SamplerState::FT_linear_mipmap_linear;
  6731. }
  6732. GLCAT.error() << "Unexpected GL filter type " << (int)ft << "\n";
  6733. return SamplerState::FT_linear;
  6734. }
  6735. ////////////////////////////////////////////////////////////////////
  6736. // Function: GLGraphicsStateGuardian::get_component_type
  6737. // Access: Protected, Static
  6738. // Description: Maps from the Texture's internal ComponentType symbols
  6739. // to GL's.
  6740. ////////////////////////////////////////////////////////////////////
  6741. GLenum CLP(GraphicsStateGuardian)::
  6742. get_component_type(Texture::ComponentType component_type) {
  6743. switch (component_type) {
  6744. case Texture::T_unsigned_byte:
  6745. return GL_UNSIGNED_BYTE;
  6746. case Texture::T_unsigned_short:
  6747. return GL_UNSIGNED_SHORT;
  6748. case Texture::T_float:
  6749. return GL_FLOAT;
  6750. case Texture::T_unsigned_int_24_8:
  6751. if (_supports_depth_stencil) {
  6752. return GL_UNSIGNED_INT_24_8_EXT;
  6753. } else {
  6754. return GL_UNSIGNED_BYTE;
  6755. }
  6756. case Texture::T_int:
  6757. #ifndef OPENGLES_1
  6758. return GL_INT;
  6759. #endif
  6760. default:
  6761. GLCAT.error() << "Invalid Texture::Type value!\n";
  6762. return GL_UNSIGNED_BYTE;
  6763. }
  6764. }
  6765. ////////////////////////////////////////////////////////////////////
  6766. // Function: GLGraphicsStateGuardian::get_external_image_format
  6767. // Access: Protected
  6768. // Description: Maps from the Texture's Format symbols
  6769. // to GL's.
  6770. ////////////////////////////////////////////////////////////////////
  6771. GLint CLP(GraphicsStateGuardian)::
  6772. get_external_image_format(Texture *tex) const {
  6773. Texture::CompressionMode compression = tex->get_ram_image_compression();
  6774. Texture::Format format = tex->get_format();
  6775. if (compression != Texture::CM_off &&
  6776. get_supports_compressed_texture_format(compression)) {
  6777. switch (compression) {
  6778. case Texture::CM_on:
  6779. #ifndef OPENGLES
  6780. switch (format) {
  6781. case Texture::F_color_index:
  6782. case Texture::F_depth_component:
  6783. case Texture::F_depth_component16:
  6784. case Texture::F_depth_component24:
  6785. case Texture::F_depth_component32:
  6786. case Texture::F_depth_stencil:
  6787. // This shouldn't be possible.
  6788. nassertr(false, GL_RGB);
  6789. break;
  6790. case Texture::F_rgba:
  6791. case Texture::F_rgbm:
  6792. case Texture::F_rgba4:
  6793. case Texture::F_rgba8:
  6794. case Texture::F_rgba12:
  6795. case Texture::F_rgba16:
  6796. case Texture::F_rgba32:
  6797. case Texture::F_rgba8i:
  6798. return GL_COMPRESSED_RGBA;
  6799. case Texture::F_rgb:
  6800. case Texture::F_rgb5:
  6801. case Texture::F_rgba5:
  6802. case Texture::F_rgb8:
  6803. case Texture::F_rgb8i:
  6804. case Texture::F_rgb12:
  6805. case Texture::F_rgb332:
  6806. case Texture::F_rgb16:
  6807. case Texture::F_rgb32:
  6808. return GL_COMPRESSED_RGB;
  6809. case Texture::F_alpha:
  6810. return GL_COMPRESSED_ALPHA;
  6811. case Texture::F_red:
  6812. case Texture::F_green:
  6813. case Texture::F_blue:
  6814. case Texture::F_r8i:
  6815. case Texture::F_r16:
  6816. case Texture::F_r32:
  6817. case Texture::F_r32i:
  6818. return GL_COMPRESSED_RED;
  6819. case Texture::F_rg8i:
  6820. case Texture::F_rg16:
  6821. case Texture::F_rg32:
  6822. return GL_COMPRESSED_RG;
  6823. case Texture::F_luminance:
  6824. return GL_COMPRESSED_LUMINANCE;
  6825. case Texture::F_luminance_alpha:
  6826. case Texture::F_luminance_alphamask:
  6827. return GL_COMPRESSED_LUMINANCE_ALPHA;
  6828. case Texture::F_srgb:
  6829. return GL_COMPRESSED_SRGB;
  6830. case Texture::F_srgb_alpha:
  6831. return GL_COMPRESSED_SRGB_ALPHA;
  6832. case Texture::F_sluminance:
  6833. return GL_COMPRESSED_SLUMINANCE;
  6834. case Texture::F_sluminance_alpha:
  6835. return GL_COMPRESSED_SLUMINANCE_ALPHA;
  6836. }
  6837. #endif
  6838. break;
  6839. case Texture::CM_dxt1:
  6840. #ifndef OPENGLES
  6841. if (format == Texture::F_srgb_alpha) {
  6842. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  6843. } else if (format == Texture::F_srgb) {
  6844. return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
  6845. } else
  6846. #endif
  6847. if (Texture::has_alpha(format)) {
  6848. return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  6849. } else {
  6850. return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  6851. }
  6852. case Texture::CM_dxt3:
  6853. #ifndef OPENGLES
  6854. if (format == Texture::F_srgb || format == Texture::F_srgb_alpha) {
  6855. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  6856. }
  6857. #endif
  6858. #ifndef OPENGLES_1
  6859. return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  6860. #endif
  6861. break;
  6862. case Texture::CM_dxt5:
  6863. #ifndef OPENGLES
  6864. if (format == Texture::F_srgb || format == Texture::F_srgb_alpha) {
  6865. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  6866. }
  6867. #endif
  6868. #ifndef OPENGLES_1
  6869. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  6870. #endif
  6871. break;
  6872. case Texture::CM_fxt1:
  6873. #ifndef OPENGLES
  6874. if (Texture::has_alpha(format)) {
  6875. return GL_COMPRESSED_RGBA_FXT1_3DFX;
  6876. } else {
  6877. return GL_COMPRESSED_RGB_FXT1_3DFX;
  6878. }
  6879. #endif
  6880. break;
  6881. #ifdef OPENGLES
  6882. case Texture::CM_pvr1_2bpp:
  6883. if (Texture::has_alpha(format)) {
  6884. return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  6885. } else {
  6886. return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  6887. }
  6888. case Texture::CM_pvr1_4bpp:
  6889. if (Texture::has_alpha(format)) {
  6890. return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  6891. } else {
  6892. return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  6893. }
  6894. #else
  6895. case Texture::CM_pvr1_2bpp:
  6896. case Texture::CM_pvr1_4bpp:
  6897. break;
  6898. #endif // OPENGLES
  6899. case Texture::CM_default:
  6900. case Texture::CM_off:
  6901. case Texture::CM_dxt2:
  6902. case Texture::CM_dxt4:
  6903. // This shouldn't happen.
  6904. nassertr(false, GL_RGB);
  6905. break;
  6906. }
  6907. }
  6908. switch (format) {
  6909. #ifndef OPENGLES
  6910. case Texture::F_color_index:
  6911. return GL_COLOR_INDEX;
  6912. #endif
  6913. case Texture::F_depth_component:
  6914. case Texture::F_depth_component16:
  6915. case Texture::F_depth_component24:
  6916. case Texture::F_depth_component32:
  6917. return GL_DEPTH_COMPONENT;
  6918. case Texture::F_depth_stencil:
  6919. return _supports_depth_stencil ? GL_DEPTH_STENCIL : GL_DEPTH_COMPONENT;
  6920. #ifndef OPENGLES
  6921. case Texture::F_red:
  6922. case Texture::F_r16:
  6923. case Texture::F_r32:
  6924. return GL_RED;
  6925. case Texture::F_green:
  6926. return GL_GREEN;
  6927. case Texture::F_blue:
  6928. return GL_BLUE;
  6929. #endif
  6930. case Texture::F_alpha:
  6931. #ifdef SUPPORT_FIXED_FUNCTION
  6932. return GL_ALPHA;
  6933. #else
  6934. return GL_RED;
  6935. #endif
  6936. #ifndef OPENGLES_1
  6937. case Texture::F_rg16:
  6938. case Texture::F_rg32:
  6939. return GL_RG;
  6940. #endif
  6941. case Texture::F_rgb:
  6942. case Texture::F_rgb5:
  6943. case Texture::F_rgb8:
  6944. case Texture::F_rgb12:
  6945. case Texture::F_rgb332:
  6946. case Texture::F_rgb16:
  6947. case Texture::F_rgb32:
  6948. case Texture::F_srgb:
  6949. #ifdef OPENGLES
  6950. return GL_RGB;
  6951. #else
  6952. return _supports_bgr ? GL_BGR : GL_RGB;
  6953. #endif
  6954. case Texture::F_rgba:
  6955. case Texture::F_rgbm:
  6956. case Texture::F_rgba4:
  6957. case Texture::F_rgba5:
  6958. case Texture::F_rgba8:
  6959. case Texture::F_rgba12:
  6960. case Texture::F_rgba16:
  6961. case Texture::F_rgba32:
  6962. case Texture::F_srgb_alpha:
  6963. #ifdef OPENGLES_2
  6964. return GL_RGBA;
  6965. #else
  6966. return _supports_bgr ? GL_BGRA : GL_RGBA;
  6967. #endif
  6968. case Texture::F_luminance:
  6969. case Texture::F_sluminance:
  6970. #ifdef SUPPORT_FIXED_FUNCTION
  6971. return GL_LUMINANCE;
  6972. #else
  6973. return GL_RED;
  6974. #endif
  6975. case Texture::F_luminance_alphamask:
  6976. case Texture::F_luminance_alpha:
  6977. case Texture::F_sluminance_alpha:
  6978. #ifdef SUPPORT_FIXED_FUNCTION
  6979. return GL_LUMINANCE_ALPHA;
  6980. #else
  6981. return GL_RG;
  6982. #endif
  6983. #ifndef OPENGLES
  6984. case Texture::F_r8i:
  6985. case Texture::F_r32i:
  6986. return GL_RED_INTEGER;
  6987. case Texture::F_rg8i:
  6988. return GL_RG_INTEGER;
  6989. case Texture::F_rgb8i:
  6990. return GL_RGB_INTEGER;
  6991. case Texture::F_rgba8i:
  6992. return GL_RGBA_INTEGER;
  6993. #endif
  6994. default:
  6995. break;
  6996. }
  6997. GLCAT.error()
  6998. << "Invalid Texture::Format value in get_external_image_format(): "
  6999. << tex->get_format() << "\n";
  7000. return GL_RGB;
  7001. }
  7002. ////////////////////////////////////////////////////////////////////
  7003. // Function: GLGraphicsStateGuardian::get_internal_image_format
  7004. // Access: Protected
  7005. // Description: Maps from the Texture's Format symbols to a
  7006. // suitable internal format for GL textures.
  7007. ////////////////////////////////////////////////////////////////////
  7008. GLint CLP(GraphicsStateGuardian)::
  7009. get_internal_image_format(Texture *tex, bool force_sized) const {
  7010. Texture::CompressionMode compression = tex->get_compression();
  7011. if (compression == Texture::CM_default) {
  7012. compression = (compressed_textures) ? Texture::CM_on : Texture::CM_off;
  7013. }
  7014. Texture::Format format = tex->get_format();
  7015. if (tex->get_render_to_texture()) {
  7016. // no compression for render targets
  7017. compression = Texture::CM_off;
  7018. }
  7019. bool is_3d = (tex->get_texture_type() == Texture::TT_3d_texture ||
  7020. tex->get_texture_type() == Texture::TT_2d_texture_array);
  7021. if (get_supports_compressed_texture_format(compression)) {
  7022. switch (compression) {
  7023. case Texture::CM_on:
  7024. // The user asked for just generic compression. OpenGL supports
  7025. // requesting just generic compression, but we'd like to go ahead
  7026. // and request a specific type (if we can figure out an
  7027. // appropriate choice), since that makes saving the result as a
  7028. // pre-compressed texture more dependable--this way, we will know
  7029. // which compression algorithm was applied.
  7030. switch (format) {
  7031. case Texture::F_color_index:
  7032. case Texture::F_depth_component:
  7033. case Texture::F_depth_component16:
  7034. case Texture::F_depth_component24:
  7035. case Texture::F_depth_component32:
  7036. case Texture::F_depth_stencil:
  7037. case Texture::F_r8i:
  7038. case Texture::F_rg8i:
  7039. case Texture::F_rgb8i:
  7040. case Texture::F_rgba8i:
  7041. case Texture::F_r32i:
  7042. // Unsupported; fall through to below.
  7043. break;
  7044. case Texture::F_rgbm:
  7045. if (get_supports_compressed_texture_format(Texture::CM_dxt1) && !is_3d) {
  7046. return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  7047. }
  7048. #ifndef OPENGLES
  7049. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7050. return GL_COMPRESSED_RGBA_FXT1_3DFX;
  7051. }
  7052. return GL_COMPRESSED_RGBA;
  7053. #endif
  7054. break;
  7055. case Texture::F_rgba4:
  7056. #ifndef OPENGLES_1
  7057. if (get_supports_compressed_texture_format(Texture::CM_dxt3) && !is_3d) {
  7058. return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  7059. }
  7060. #endif
  7061. #ifndef OPENGLES
  7062. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7063. return GL_COMPRESSED_RGBA_FXT1_3DFX;
  7064. }
  7065. return GL_COMPRESSED_RGBA;
  7066. #endif
  7067. break;
  7068. case Texture::F_rgba:
  7069. case Texture::F_rgba8:
  7070. case Texture::F_rgba12:
  7071. case Texture::F_rgba16:
  7072. case Texture::F_rgba32:
  7073. #ifndef OPENGLES_1
  7074. if (get_supports_compressed_texture_format(Texture::CM_dxt5) && !is_3d) {
  7075. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  7076. }
  7077. #endif
  7078. #ifndef OPENGLES
  7079. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7080. return GL_COMPRESSED_RGBA_FXT1_3DFX;
  7081. }
  7082. return GL_COMPRESSED_RGBA;
  7083. #endif
  7084. break;
  7085. case Texture::F_rgb:
  7086. case Texture::F_rgb5:
  7087. case Texture::F_rgba5:
  7088. case Texture::F_rgb8:
  7089. case Texture::F_rgb12:
  7090. case Texture::F_rgb332:
  7091. case Texture::F_rgb16:
  7092. case Texture::F_rgb32:
  7093. if (get_supports_compressed_texture_format(Texture::CM_dxt1) && !is_3d) {
  7094. return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  7095. }
  7096. #ifndef OPENGLES
  7097. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7098. return GL_COMPRESSED_RGB_FXT1_3DFX;
  7099. }
  7100. return GL_COMPRESSED_RGB;
  7101. #endif
  7102. break;
  7103. case Texture::F_alpha:
  7104. #ifndef OPENGLES_1
  7105. if (get_supports_compressed_texture_format(Texture::CM_dxt5) && !is_3d) {
  7106. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  7107. }
  7108. #endif
  7109. #ifndef OPENGLES
  7110. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7111. return GL_COMPRESSED_RGBA_FXT1_3DFX;
  7112. }
  7113. return GL_COMPRESSED_ALPHA;
  7114. #endif
  7115. break;
  7116. case Texture::F_red:
  7117. case Texture::F_green:
  7118. case Texture::F_blue:
  7119. case Texture::F_r16:
  7120. case Texture::F_r32:
  7121. if (get_supports_compressed_texture_format(Texture::CM_dxt1) && !is_3d) {
  7122. return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  7123. }
  7124. #ifndef OPENGLES
  7125. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7126. return GL_COMPRESSED_RGB_FXT1_3DFX;
  7127. }
  7128. return GL_COMPRESSED_RED;
  7129. #endif
  7130. break;
  7131. case Texture::F_rg16:
  7132. case Texture::F_rg32:
  7133. if (get_supports_compressed_texture_format(Texture::CM_dxt1) && !is_3d) {
  7134. return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  7135. }
  7136. #ifndef OPENGLES
  7137. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7138. return GL_COMPRESSED_RGB_FXT1_3DFX;
  7139. }
  7140. return GL_COMPRESSED_RG;
  7141. #endif
  7142. break;
  7143. case Texture::F_luminance:
  7144. if (get_supports_compressed_texture_format(Texture::CM_dxt1) && !is_3d) {
  7145. return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  7146. }
  7147. #ifndef OPENGLES
  7148. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7149. return GL_COMPRESSED_RGB_FXT1_3DFX;
  7150. }
  7151. return GL_COMPRESSED_LUMINANCE;
  7152. #endif
  7153. break;
  7154. case Texture::F_luminance_alpha:
  7155. case Texture::F_luminance_alphamask:
  7156. #ifndef OPENGLES_1
  7157. if (get_supports_compressed_texture_format(Texture::CM_dxt5) && !is_3d) {
  7158. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  7159. }
  7160. #endif
  7161. #ifndef OPENGLES
  7162. if (get_supports_compressed_texture_format(Texture::CM_fxt1) && !is_3d) {
  7163. return GL_COMPRESSED_RGBA_FXT1_3DFX;
  7164. }
  7165. return GL_COMPRESSED_LUMINANCE_ALPHA;
  7166. #endif
  7167. break;
  7168. #ifndef OPENGLES
  7169. case Texture::F_srgb:
  7170. if (get_supports_compressed_texture_format(Texture::CM_dxt1) && !is_3d) {
  7171. return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
  7172. }
  7173. return GL_COMPRESSED_SRGB;
  7174. case Texture::F_srgb_alpha:
  7175. if (get_supports_compressed_texture_format(Texture::CM_dxt5) && !is_3d) {
  7176. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  7177. }
  7178. return GL_COMPRESSED_SRGB_ALPHA;
  7179. case Texture::F_sluminance:
  7180. return GL_COMPRESSED_SLUMINANCE;
  7181. case Texture::F_sluminance_alpha:
  7182. return GL_COMPRESSED_SLUMINANCE_ALPHA;
  7183. #else
  7184. // For now, we don't support compressed sRGB textures in OpenGL ES.
  7185. case Texture::F_srgb:
  7186. case Texture::F_srgb_alpha:
  7187. case Texture::F_sluminance:
  7188. case Texture::F_sluminance_alpha:
  7189. break;
  7190. #endif
  7191. }
  7192. break;
  7193. case Texture::CM_dxt1:
  7194. #ifndef OPENGLES
  7195. if (format == Texture::F_srgb_alpha) {
  7196. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  7197. } else if (format == Texture::F_srgb) {
  7198. return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
  7199. } else
  7200. #endif
  7201. if (Texture::has_alpha(format)) {
  7202. return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  7203. } else {
  7204. return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
  7205. }
  7206. case Texture::CM_dxt3:
  7207. #ifndef OPENGLES
  7208. if (format == Texture::F_srgb || format == Texture::F_srgb_alpha) {
  7209. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  7210. }
  7211. #endif
  7212. #ifndef OPENGLES_1
  7213. return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  7214. #endif
  7215. break;
  7216. case Texture::CM_dxt5:
  7217. #ifndef OPENGLES
  7218. if (format == Texture::F_srgb || format == Texture::F_srgb_alpha) {
  7219. return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  7220. }
  7221. #endif
  7222. #ifndef OPENGLES_1
  7223. return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  7224. #endif
  7225. case Texture::CM_fxt1:
  7226. #ifndef OPENGLES
  7227. if (Texture::has_alpha(format)) {
  7228. return GL_COMPRESSED_RGBA_FXT1_3DFX;
  7229. } else {
  7230. return GL_COMPRESSED_RGB_FXT1_3DFX;
  7231. }
  7232. #endif
  7233. break;
  7234. #ifdef OPENGLES
  7235. case Texture::CM_pvr1_2bpp:
  7236. if (Texture::has_alpha(format)) {
  7237. return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  7238. } else {
  7239. return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  7240. }
  7241. case Texture::CM_pvr1_4bpp:
  7242. if (Texture::has_alpha(format)) {
  7243. return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  7244. } else {
  7245. return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  7246. }
  7247. #else
  7248. case Texture::CM_pvr1_2bpp:
  7249. case Texture::CM_pvr1_4bpp:
  7250. break;
  7251. #endif
  7252. case Texture::CM_default:
  7253. case Texture::CM_off:
  7254. case Texture::CM_dxt2:
  7255. case Texture::CM_dxt4:
  7256. // No compression: fall through to below.
  7257. break;
  7258. }
  7259. }
  7260. switch (format) {
  7261. #ifndef OPENGLES
  7262. case Texture::F_color_index:
  7263. return GL_COLOR_INDEX;
  7264. #endif
  7265. case Texture::F_depth_stencil:
  7266. if (_supports_depth_stencil) {
  7267. #ifndef OPENGLES
  7268. if (tex->get_component_type() == Texture::T_float) {
  7269. return GL_DEPTH32F_STENCIL8;
  7270. } else
  7271. #endif
  7272. {
  7273. return force_sized ? GL_DEPTH24_STENCIL8 : GL_DEPTH_STENCIL;
  7274. }
  7275. }
  7276. // Fall through.
  7277. case Texture::F_depth_component:
  7278. #ifndef OPENGLES
  7279. if (tex->get_component_type() == Texture::T_float) {
  7280. return GL_DEPTH_COMPONENT32F;
  7281. } else
  7282. #endif
  7283. {
  7284. return force_sized ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT;
  7285. }
  7286. case Texture::F_depth_component16:
  7287. #ifdef OPENGLES
  7288. return GL_DEPTH_COMPONENT16_OES;
  7289. #else
  7290. return GL_DEPTH_COMPONENT16;
  7291. #endif
  7292. case Texture::F_depth_component24:
  7293. #ifdef OPENGLES
  7294. if (_supports_depth24) {
  7295. return GL_DEPTH_COMPONENT24_OES;
  7296. } else {
  7297. return GL_DEPTH_COMPONENT16_OES;
  7298. }
  7299. #else
  7300. return GL_DEPTH_COMPONENT24;
  7301. #endif
  7302. case Texture::F_depth_component32:
  7303. #ifdef OPENGLES
  7304. if (_supports_depth32) {
  7305. return GL_DEPTH_COMPONENT32_OES;
  7306. } else if (_supports_depth24) {
  7307. return GL_DEPTH_COMPONENT24_OES;
  7308. } else {
  7309. return GL_DEPTH_COMPONENT16_OES;
  7310. }
  7311. #else
  7312. if (tex->get_component_type() == Texture::T_float) {
  7313. return GL_DEPTH_COMPONENT32F;
  7314. } else {
  7315. return GL_DEPTH_COMPONENT32;
  7316. }
  7317. #endif
  7318. case Texture::F_rgba:
  7319. case Texture::F_rgbm:
  7320. #ifndef OPENGLES_1
  7321. if (tex->get_component_type() == Texture::T_float) {
  7322. return GL_RGBA16F;
  7323. } else
  7324. #endif
  7325. #ifndef OPENGLES
  7326. if (tex->get_component_type() == Texture::T_unsigned_short) {
  7327. return GL_RGBA16;
  7328. } else
  7329. #endif
  7330. {
  7331. return force_sized ? GL_RGBA8 : GL_RGBA;
  7332. }
  7333. case Texture::F_rgba4:
  7334. return GL_RGBA4;
  7335. #ifdef OPENGLES
  7336. case Texture::F_rgba8:
  7337. return GL_RGBA8_OES;
  7338. case Texture::F_rgba12:
  7339. return force_sized ? GL_RGBA8 : GL_RGBA;
  7340. #else
  7341. case Texture::F_rgba8:
  7342. return GL_RGBA8;
  7343. case Texture::F_r8i:
  7344. if (tex->get_component_type() == Texture::T_unsigned_byte) {
  7345. return GL_R8UI;
  7346. } else {
  7347. return GL_R8I;
  7348. }
  7349. case Texture::F_rg8i:
  7350. if (tex->get_component_type() == Texture::T_unsigned_byte) {
  7351. return GL_RG8UI;
  7352. } else {
  7353. return GL_RG8I;
  7354. }
  7355. case Texture::F_rgb8i:
  7356. if (tex->get_component_type() == Texture::T_unsigned_byte) {
  7357. return GL_RGB8UI;
  7358. } else {
  7359. return GL_RGB8I;
  7360. }
  7361. case Texture::F_rgba8i:
  7362. if (tex->get_component_type() == Texture::T_unsigned_byte) {
  7363. return GL_RGBA8UI;
  7364. } else {
  7365. return GL_RGBA8I;
  7366. }
  7367. case Texture::F_rgba12:
  7368. return GL_RGBA12;
  7369. #endif // OPENGLES
  7370. #ifndef OPENGLES
  7371. case Texture::F_rgba16:
  7372. if (tex->get_component_type() == Texture::T_float) {
  7373. return GL_RGBA16F;
  7374. } else {
  7375. return GL_RGBA16;
  7376. }
  7377. case Texture::F_rgba32:
  7378. return GL_RGBA32F;
  7379. #endif // OPENGLES
  7380. case Texture::F_rgb:
  7381. if (tex->get_component_type() == Texture::T_float) {
  7382. return GL_RGB16F;
  7383. } else {
  7384. return force_sized ? GL_RGB8 : GL_RGB;
  7385. }
  7386. case Texture::F_rgb5:
  7387. #ifdef OPENGLES
  7388. // Close enough.
  7389. return GL_RGB565_OES;
  7390. #else
  7391. return GL_RGB5;
  7392. #endif
  7393. case Texture::F_rgba5:
  7394. return GL_RGB5_A1;
  7395. #ifdef OPENGLES
  7396. case Texture::F_rgb8:
  7397. return GL_RGB8_OES;
  7398. case Texture::F_rgb12:
  7399. return force_sized ? GL_RGB8 : GL_RGB;
  7400. case Texture::F_rgb16:
  7401. return GL_RGB16F;
  7402. #else
  7403. case Texture::F_rgb8:
  7404. return GL_RGB8;
  7405. case Texture::F_rgb12:
  7406. return GL_RGB12;
  7407. case Texture::F_rgb16:
  7408. if (tex->get_component_type() == Texture::T_float) {
  7409. return GL_RGB16F;
  7410. } else {
  7411. return GL_RGB16;
  7412. }
  7413. #endif // OPENGLES
  7414. case Texture::F_rgb32:
  7415. return GL_RGB32F;
  7416. #ifndef OPENGLES
  7417. case Texture::F_rgb332:
  7418. return GL_R3_G3_B2;
  7419. #endif
  7420. #if defined(OPENGLES_2)
  7421. case Texture::F_r16:
  7422. return GL_R16F_EXT;
  7423. case Texture::F_rg16:
  7424. return GL_RG16F_EXT;
  7425. #elif !defined(OPENGLES_1)
  7426. case Texture::F_r16:
  7427. if (tex->get_component_type() == Texture::T_float) {
  7428. return GL_R16F;
  7429. } else {
  7430. return GL_R16;
  7431. }
  7432. case Texture::F_rg16:
  7433. if (tex->get_component_type() == Texture::T_float) {
  7434. return GL_RG16F;
  7435. } else {
  7436. return GL_RG16;
  7437. }
  7438. #endif
  7439. #ifndef OPENGLES_1
  7440. case Texture::F_r32:
  7441. return GL_R32F;
  7442. case Texture::F_rg32:
  7443. return GL_RG32F;
  7444. case Texture::F_red:
  7445. case Texture::F_green:
  7446. case Texture::F_blue:
  7447. return force_sized ? GL_R8 : GL_RED;
  7448. #endif
  7449. case Texture::F_alpha:
  7450. #ifdef SUPPORT_FIXED_FUNCTION
  7451. return force_sized ? GL_ALPHA8 : GL_ALPHA;
  7452. #else
  7453. return force_sized ? GL_R8 : GL_RED;
  7454. #endif
  7455. case Texture::F_luminance:
  7456. #ifndef OPENGLES
  7457. if (tex->get_component_type() == Texture::T_float) {
  7458. return GL_LUMINANCE16F_ARB;
  7459. } else if (tex->get_component_type() == Texture::T_unsigned_short) {
  7460. return GL_LUMINANCE16;
  7461. } else
  7462. #endif // OPENGLES
  7463. {
  7464. return force_sized ? GL_LUMINANCE8 : GL_LUMINANCE;
  7465. }
  7466. case Texture::F_luminance_alpha:
  7467. case Texture::F_luminance_alphamask:
  7468. #ifndef OPENGLES
  7469. if (tex->get_component_type() == Texture::T_float || tex->get_component_type() == Texture::T_unsigned_short) {
  7470. return GL_LUMINANCE_ALPHA16F_ARB;
  7471. } else
  7472. #endif // OPENGLES
  7473. {
  7474. return force_sized ? GL_LUMINANCE8_ALPHA8 : GL_LUMINANCE_ALPHA;
  7475. }
  7476. #ifndef OPENGLES_1
  7477. case Texture::F_srgb:
  7478. #ifndef OPENGLES
  7479. return GL_SRGB8;
  7480. #endif
  7481. case Texture::F_srgb_alpha:
  7482. return GL_SRGB8_ALPHA8;
  7483. #endif
  7484. #ifndef OPENGLES
  7485. case Texture::F_sluminance:
  7486. return GL_SLUMINANCE8;
  7487. case Texture::F_sluminance_alpha:
  7488. return GL_SLUMINANCE8_ALPHA8;
  7489. #endif
  7490. #ifndef OPENGLES
  7491. case Texture::F_r32i:
  7492. return GL_R32I;
  7493. #endif
  7494. default:
  7495. GLCAT.error()
  7496. << "Invalid image format in get_internal_image_format(): "
  7497. << (int)tex->get_format() << "\n";
  7498. return force_sized ? GL_RGB8 : GL_RGB;
  7499. }
  7500. }
  7501. ////////////////////////////////////////////////////////////////////
  7502. // Function: GLGraphicsStateGuardian::is_mipmap_filter
  7503. // Access: Protected, Static
  7504. // Description: Returns true if the indicated GL minfilter type
  7505. // represents a mipmap format, false otherwise.
  7506. ////////////////////////////////////////////////////////////////////
  7507. bool CLP(GraphicsStateGuardian)::
  7508. is_mipmap_filter(GLenum min_filter) {
  7509. switch (min_filter) {
  7510. case GL_NEAREST_MIPMAP_NEAREST:
  7511. case GL_LINEAR_MIPMAP_NEAREST:
  7512. case GL_NEAREST_MIPMAP_LINEAR:
  7513. case GL_LINEAR_MIPMAP_LINEAR:
  7514. return true;
  7515. default:
  7516. return false;
  7517. }
  7518. }
  7519. ////////////////////////////////////////////////////////////////////
  7520. // Function: GLGraphicsStateGuardian::is_compressed_format
  7521. // Access: Protected, Static
  7522. // Description: Returns true if the indicated GL internal format
  7523. // represents a compressed texture format, false
  7524. // otherwise.
  7525. ////////////////////////////////////////////////////////////////////
  7526. bool CLP(GraphicsStateGuardian)::
  7527. is_compressed_format(GLenum format) {
  7528. switch (format) {
  7529. case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  7530. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  7531. #ifdef OPENGLES
  7532. case GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  7533. case GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  7534. case GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  7535. case GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  7536. #else
  7537. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  7538. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  7539. case GL_COMPRESSED_RGB_FXT1_3DFX:
  7540. case GL_COMPRESSED_RGBA_FXT1_3DFX:
  7541. case GL_COMPRESSED_RGB:
  7542. case GL_COMPRESSED_RGBA:
  7543. case GL_COMPRESSED_ALPHA:
  7544. case GL_COMPRESSED_LUMINANCE:
  7545. case GL_COMPRESSED_LUMINANCE_ALPHA:
  7546. #endif
  7547. return true;
  7548. default:
  7549. return false;
  7550. }
  7551. }
  7552. ////////////////////////////////////////////////////////////////////
  7553. // Function: GLGraphicsStateGuardian::get_texture_apply_mode_type
  7554. // Access: Protected, Static
  7555. // Description: Maps from the texture stage's mode types
  7556. // to the corresponding OpenGL ids
  7557. ////////////////////////////////////////////////////////////////////
  7558. GLint CLP(GraphicsStateGuardian)::
  7559. get_texture_apply_mode_type(TextureStage::Mode am) {
  7560. #ifdef SUPPORT_FIXED_FUNCTION
  7561. switch (am) {
  7562. case TextureStage::M_modulate: return GL_MODULATE;
  7563. case TextureStage::M_decal: return GL_DECAL;
  7564. case TextureStage::M_blend: return GL_BLEND;
  7565. case TextureStage::M_replace: return GL_REPLACE;
  7566. case TextureStage::M_add: return GL_ADD;
  7567. case TextureStage::M_combine: return GL_COMBINE;
  7568. case TextureStage::M_blend_color_scale: return GL_BLEND;
  7569. case TextureStage::M_modulate_glow: return GL_MODULATE;
  7570. case TextureStage::M_modulate_gloss: return GL_MODULATE;
  7571. default:
  7572. // Other modes shouldn't get here. Fall through and error.
  7573. break;
  7574. }
  7575. GLCAT.error()
  7576. << "Invalid TextureStage::Mode value" << endl;
  7577. return GL_MODULATE;
  7578. #else
  7579. return 0;
  7580. #endif
  7581. }
  7582. ////////////////////////////////////////////////////////////////////
  7583. // Function: GLGraphicsStateGuardian::get_texture_combine_type
  7584. // Access: Protected, Static
  7585. // Description: Maps from the texture stage's CombineMode types
  7586. // to the corresponding OpenGL ids
  7587. ////////////////////////////////////////////////////////////////////
  7588. GLint CLP(GraphicsStateGuardian)::
  7589. get_texture_combine_type(TextureStage::CombineMode cm) {
  7590. #ifdef SUPPORT_FIXED_FUNCTION
  7591. switch (cm) {
  7592. case TextureStage::CM_undefined: // fall through
  7593. case TextureStage::CM_replace: return GL_REPLACE;
  7594. case TextureStage::CM_modulate: return GL_MODULATE;
  7595. case TextureStage::CM_add: return GL_ADD;
  7596. case TextureStage::CM_add_signed: return GL_ADD_SIGNED;
  7597. case TextureStage::CM_interpolate: return GL_INTERPOLATE;
  7598. case TextureStage::CM_subtract: return GL_SUBTRACT;
  7599. case TextureStage::CM_dot3_rgb: return GL_DOT3_RGB;
  7600. case TextureStage::CM_dot3_rgba: return GL_DOT3_RGBA;
  7601. }
  7602. GLCAT.error()
  7603. << "Invalid TextureStage::CombineMode value" << endl;
  7604. #endif
  7605. return GL_REPLACE;
  7606. }
  7607. ////////////////////////////////////////////////////////////////////
  7608. // Function: GLGraphicsStateGuardian::get_texture_src_type
  7609. // Access: Protected
  7610. // Description: Maps from the texture stage's CombineSource types
  7611. // to the corresponding OpenGL ids
  7612. ////////////////////////////////////////////////////////////////////
  7613. GLint CLP(GraphicsStateGuardian)::
  7614. get_texture_src_type(TextureStage::CombineSource cs,
  7615. int last_stage, int last_saved_result,
  7616. int this_stage) const {
  7617. #ifdef SUPPORT_FIXED_FUNCTION
  7618. switch (cs) {
  7619. case TextureStage::CS_undefined: // fall through
  7620. case TextureStage::CS_texture: return GL_TEXTURE;
  7621. case TextureStage::CS_constant: return GL_CONSTANT;
  7622. case TextureStage::CS_primary_color: return GL_PRIMARY_COLOR;
  7623. case TextureStage::CS_constant_color_scale: return GL_CONSTANT;
  7624. case TextureStage::CS_previous:
  7625. if (last_stage == this_stage - 1) {
  7626. return GL_PREVIOUS;
  7627. } else if (last_stage == -1) {
  7628. return GL_PRIMARY_COLOR;
  7629. } else if (_supports_texture_saved_result) {
  7630. return GL_TEXTURE0 + last_stage;
  7631. } else {
  7632. GLCAT.warning()
  7633. << "Current OpenGL driver does not support texture crossbar blending.\n";
  7634. return GL_PRIMARY_COLOR;
  7635. }
  7636. case TextureStage::CS_last_saved_result:
  7637. if (last_saved_result == this_stage - 1) {
  7638. return GL_PREVIOUS;
  7639. } else if (last_saved_result == -1) {
  7640. return GL_PRIMARY_COLOR;
  7641. } else if (_supports_texture_saved_result) {
  7642. return GL_TEXTURE0 + last_saved_result;
  7643. } else {
  7644. GLCAT.warning()
  7645. << "Current OpenGL driver does not support texture crossbar blending.\n";
  7646. return GL_PRIMARY_COLOR;
  7647. }
  7648. }
  7649. GLCAT.error()
  7650. << "Invalid TextureStage::CombineSource value" << endl;
  7651. #endif
  7652. return GL_TEXTURE;
  7653. }
  7654. ////////////////////////////////////////////////////////////////////
  7655. // Function: GLGraphicsStateGuardian::get_texture_operand_type
  7656. // Access: Protected, Static
  7657. // Description: Maps from the texture stage's CombineOperand types
  7658. // to the corresponding OpenGL ids
  7659. ////////////////////////////////////////////////////////////////////
  7660. GLint CLP(GraphicsStateGuardian)::
  7661. get_texture_operand_type(TextureStage::CombineOperand co) {
  7662. switch (co) {
  7663. case TextureStage::CO_undefined: // fall through
  7664. case TextureStage::CO_src_alpha: return GL_SRC_ALPHA;
  7665. case TextureStage::CO_one_minus_src_alpha: return GL_ONE_MINUS_SRC_ALPHA;
  7666. case TextureStage::CO_src_color: return GL_SRC_COLOR;
  7667. case TextureStage::CO_one_minus_src_color: return GL_ONE_MINUS_SRC_COLOR;
  7668. }
  7669. GLCAT.error()
  7670. << "Invalid TextureStage::CombineOperand value" << endl;
  7671. return GL_SRC_COLOR;
  7672. }
  7673. #ifdef SUPPORT_FIXED_FUNCTION
  7674. ////////////////////////////////////////////////////////////////////
  7675. // Function: GLGraphicsStateGuardian::get_fog_mode_type
  7676. // Access: Protected, Static
  7677. // Description: Maps from the fog types to gl version
  7678. ////////////////////////////////////////////////////////////////////
  7679. GLenum CLP(GraphicsStateGuardian)::
  7680. get_fog_mode_type(Fog::Mode m) {
  7681. switch(m) {
  7682. case Fog::M_linear: return GL_LINEAR;
  7683. case Fog::M_exponential: return GL_EXP;
  7684. case Fog::M_exponential_squared: return GL_EXP2;
  7685. /*
  7686. case Fog::M_spline: return GL_FOG_FUNC_SGIS;
  7687. */
  7688. default:
  7689. GLCAT.error() << "Invalid Fog::Mode value" << endl;
  7690. return GL_EXP;
  7691. }
  7692. }
  7693. #endif
  7694. ////////////////////////////////////////////////////////////////////
  7695. // Function: GLGraphicsStateGuardian::get_blend_equation_type
  7696. // Access: Protected, Static
  7697. // Description: Maps from ColorBlendAttrib::Mode to glBlendEquation
  7698. // value.
  7699. ////////////////////////////////////////////////////////////////////
  7700. GLenum CLP(GraphicsStateGuardian)::
  7701. get_blend_equation_type(ColorBlendAttrib::Mode mode) {
  7702. switch (mode) {
  7703. case ColorBlendAttrib::M_none:
  7704. case ColorBlendAttrib::M_add:
  7705. return GL_FUNC_ADD;
  7706. case ColorBlendAttrib::M_subtract:
  7707. return GL_FUNC_SUBTRACT;
  7708. case ColorBlendAttrib::M_inv_subtract:
  7709. return GL_FUNC_REVERSE_SUBTRACT;
  7710. #ifdef OPENGLES
  7711. case ColorBlendAttrib::M_min:
  7712. return GL_MIN_EXT;
  7713. case ColorBlendAttrib::M_max:
  7714. return GL_MAX_EXT;
  7715. #else
  7716. case ColorBlendAttrib::M_min:
  7717. return GL_MIN;
  7718. case ColorBlendAttrib::M_max:
  7719. return GL_MAX;
  7720. #endif
  7721. }
  7722. GLCAT.error()
  7723. << "Unknown color blend mode " << (int)mode << endl;
  7724. return GL_FUNC_ADD;
  7725. }
  7726. ////////////////////////////////////////////////////////////////////
  7727. // Function: GLGraphicsStateGuardian::get_blend_func
  7728. // Access: Protected, Static
  7729. // Description: Maps from ColorBlendAttrib::Operand to glBlendFunc
  7730. // value.
  7731. ////////////////////////////////////////////////////////////////////
  7732. GLenum CLP(GraphicsStateGuardian)::
  7733. get_blend_func(ColorBlendAttrib::Operand operand) {
  7734. switch (operand) {
  7735. case ColorBlendAttrib::O_zero:
  7736. return GL_ZERO;
  7737. case ColorBlendAttrib::O_one:
  7738. return GL_ONE;
  7739. case ColorBlendAttrib::O_incoming_color:
  7740. return GL_SRC_COLOR;
  7741. case ColorBlendAttrib::O_one_minus_incoming_color:
  7742. return GL_ONE_MINUS_SRC_COLOR;
  7743. case ColorBlendAttrib::O_fbuffer_color:
  7744. return GL_DST_COLOR;
  7745. case ColorBlendAttrib::O_one_minus_fbuffer_color:
  7746. return GL_ONE_MINUS_DST_COLOR;
  7747. case ColorBlendAttrib::O_incoming_alpha:
  7748. return GL_SRC_ALPHA;
  7749. case ColorBlendAttrib::O_one_minus_incoming_alpha:
  7750. return GL_ONE_MINUS_SRC_ALPHA;
  7751. case ColorBlendAttrib::O_fbuffer_alpha:
  7752. return GL_DST_ALPHA;
  7753. case ColorBlendAttrib::O_one_minus_fbuffer_alpha:
  7754. return GL_ONE_MINUS_DST_ALPHA;
  7755. #ifdef OPENGLES_1
  7756. // OpenGL ES 1 has no constant blend factor.
  7757. case ColorBlendAttrib::O_constant_color:
  7758. case ColorBlendAttrib::O_color_scale:
  7759. case ColorBlendAttrib::O_one_minus_constant_color:
  7760. case ColorBlendAttrib::O_one_minus_color_scale:
  7761. case ColorBlendAttrib::O_constant_alpha:
  7762. case ColorBlendAttrib::O_alpha_scale:
  7763. case ColorBlendAttrib::O_one_minus_constant_alpha:
  7764. case ColorBlendAttrib::O_one_minus_alpha_scale:
  7765. break;
  7766. #else
  7767. case ColorBlendAttrib::O_constant_color:
  7768. case ColorBlendAttrib::O_color_scale:
  7769. return GL_CONSTANT_COLOR;
  7770. case ColorBlendAttrib::O_one_minus_constant_color:
  7771. case ColorBlendAttrib::O_one_minus_color_scale:
  7772. return GL_ONE_MINUS_CONSTANT_COLOR;
  7773. case ColorBlendAttrib::O_constant_alpha:
  7774. case ColorBlendAttrib::O_alpha_scale:
  7775. return GL_CONSTANT_ALPHA;
  7776. case ColorBlendAttrib::O_one_minus_constant_alpha:
  7777. case ColorBlendAttrib::O_one_minus_alpha_scale:
  7778. return GL_ONE_MINUS_CONSTANT_ALPHA;
  7779. #endif
  7780. case ColorBlendAttrib::O_incoming_color_saturate:
  7781. return GL_SRC_ALPHA_SATURATE;
  7782. }
  7783. GLCAT.error()
  7784. << "Unknown color blend operand " << (int)operand << endl;
  7785. return GL_ZERO;
  7786. }
  7787. ////////////////////////////////////////////////////////////////////
  7788. // Function: GLGraphicsStateGuardian::get_usage
  7789. // Access: Public, Static
  7790. // Description: Maps from UsageHint to the GL symbol.
  7791. ////////////////////////////////////////////////////////////////////
  7792. GLenum CLP(GraphicsStateGuardian)::
  7793. get_usage(Geom::UsageHint usage_hint) {
  7794. switch (usage_hint) {
  7795. case Geom::UH_stream:
  7796. #ifdef OPENGLES
  7797. return GL_DYNAMIC_DRAW;
  7798. #else
  7799. return GL_STREAM_DRAW;
  7800. #endif // OPENGLES
  7801. case Geom::UH_static:
  7802. case Geom::UH_unspecified:
  7803. return GL_STATIC_DRAW;
  7804. case Geom::UH_dynamic:
  7805. return GL_DYNAMIC_DRAW;
  7806. case Geom::UH_client:
  7807. break;
  7808. }
  7809. GLCAT.error()
  7810. << "Unexpected usage_hint " << (int)usage_hint << endl;
  7811. return GL_STATIC_DRAW;
  7812. }
  7813. ////////////////////////////////////////////////////////////////////
  7814. // Function: GLGraphicsStateGuardian::get_compressed_format_string
  7815. // Access: Public, Static
  7816. // Description: Returns a string describing an compression format.
  7817. ////////////////////////////////////////////////////////////////////
  7818. const char *CLP(GraphicsStateGuardian)::
  7819. get_compressed_format_string(GLenum format) {
  7820. switch (format) {
  7821. case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT";
  7822. case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT";
  7823. case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT";
  7824. case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT";
  7825. case 0x86B0: return "GL_COMPRESSED_RGB_FXT1_3DFX";
  7826. case 0x86B1: return "GL_COMPRESSED_RGBA_FXT1_3DFX";
  7827. case 0x8A54: return "GL_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT";
  7828. case 0x8A55: return "GL_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT";
  7829. case 0x8A56: return "GL_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT";
  7830. case 0x8A57: return "GL_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT";
  7831. case 0x8B90: return "GL_PALETTE4_RGB8_OES";
  7832. case 0x8B91: return "GL_PALETTE4_RGBA8_OES";
  7833. case 0x8B92: return "GL_PALETTE4_R5_G6_B5_OES";
  7834. case 0x8B93: return "GL_PALETTE4_RGBA4_OES";
  7835. case 0x8B94: return "GL_PALETTE4_RGB5_A1_OES";
  7836. case 0x8B95: return "GL_PALETTE8_RGB8_OES";
  7837. case 0x8B96: return "GL_PALETTE8_RGBA8_OES";
  7838. case 0x8B97: return "GL_PALETTE8_R5_G6_B5_OES";
  7839. case 0x8B98: return "GL_PALETTE8_RGBA4_OES";
  7840. case 0x8B99: return "GL_PALETTE8_RGB5_A1_OES";
  7841. case 0x8C00: return "GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG";
  7842. case 0x8C01: return "GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG";
  7843. case 0x8C02: return "GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG";
  7844. case 0x8C03: return "GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG";
  7845. case 0x8C48: return "GL_COMPRESSED_SRGB_EXT";
  7846. case 0x8C49: return "GL_COMPRESSED_SRGB_ALPHA_EXT";
  7847. case 0x8C4A: return "GL_COMPRESSED_SLUMINANCE_EXT";
  7848. case 0x8C4B: return "GL_COMPRESSED_SLUMINANCE_ALPHA_EXT";
  7849. case 0x8C4C: return "GL_COMPRESSED_SRGB_S3TC_DXT1_EXT";
  7850. case 0x8C4D: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT";
  7851. case 0x8C4E: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT";
  7852. case 0x8C4F: return "GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT";
  7853. case 0x8C70: return "GL_COMPRESSED_LUMINANCE_LATC1_EXT";
  7854. case 0x8C71: return "GL_COMPRESSED_SIGNED_LUMINANCE_LATC1_EXT";
  7855. case 0x8C72: return "GL_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT";
  7856. case 0x8C73: return "GL_COMPRESSED_SIGNED_LUMINANCE_ALPHA_LATC2_EXT";
  7857. case 0x8DBB: return "GL_COMPRESSED_RED_RGTC1";
  7858. case 0x8DBC: return "GL_COMPRESSED_SIGNED_RED_RGTC1";
  7859. case 0x8DBD: return "GL_COMPRESSED_RG_RGTC2";
  7860. case 0x8DBE: return "GL_COMPRESSED_SIGNED_RG_RGTC2";
  7861. case 0x8E8C: return "GL_COMPRESSED_RGBA_BPTC_UNORM";
  7862. case 0x8E8D: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM";
  7863. case 0x8E8E: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT";
  7864. case 0x8E8F: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT";
  7865. case 0x9137: return "GL_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG";
  7866. case 0x9138: return "GL_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG";
  7867. case 0x9270: return "GL_COMPRESSED_R11_EAC";
  7868. case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC";
  7869. case 0x9272: return "GL_COMPRESSED_RG11_EAC";
  7870. case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC";
  7871. case 0x9274: return "GL_COMPRESSED_RGB8_ETC2";
  7872. case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2";
  7873. case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2";
  7874. case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2";
  7875. case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC";
  7876. case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC";
  7877. case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4x4_KHR";
  7878. case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5x4_KHR";
  7879. case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5x5_KHR";
  7880. case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6x5_KHR";
  7881. case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6x6_KHR";
  7882. case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8x5_KHR";
  7883. case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8x6_KHR";
  7884. case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8x8_KHR";
  7885. case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10x5_KHR";
  7886. case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10x6_KHR";
  7887. case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10x8_KHR";
  7888. case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10x10_KHR";
  7889. case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12x10_KHR";
  7890. case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12x12_KHR";
  7891. case 0x93C0: return "GL_COMPRESSED_RGBA_ASTC_3x3x3_OES";
  7892. case 0x93C1: return "GL_COMPRESSED_RGBA_ASTC_4x3x3_OES";
  7893. case 0x93C2: return "GL_COMPRESSED_RGBA_ASTC_4x4x3_OES";
  7894. case 0x93C3: return "GL_COMPRESSED_RGBA_ASTC_4x4x4_OES";
  7895. case 0x93C4: return "GL_COMPRESSED_RGBA_ASTC_5x4x4_OES";
  7896. case 0x93C5: return "GL_COMPRESSED_RGBA_ASTC_5x5x4_OES";
  7897. case 0x93C6: return "GL_COMPRESSED_RGBA_ASTC_5x5x5_OES";
  7898. case 0x93C7: return "GL_COMPRESSED_RGBA_ASTC_6x5x5_OES";
  7899. case 0x93C8: return "GL_COMPRESSED_RGBA_ASTC_6x6x5_OES";
  7900. case 0x93C9: return "GL_COMPRESSED_RGBA_ASTC_6x6x6_OES";
  7901. case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR";
  7902. case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR";
  7903. case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR";
  7904. case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR";
  7905. case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR";
  7906. case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR";
  7907. case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR";
  7908. case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR";
  7909. case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR";
  7910. case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR";
  7911. case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR";
  7912. case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR";
  7913. case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR";
  7914. case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR";
  7915. case 0x93E0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_3x3x3_OES";
  7916. case 0x93E1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x3x3_OES";
  7917. case 0x93E2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x3_OES";
  7918. case 0x93E3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4x4_OES";
  7919. case 0x93E4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4x4_OES";
  7920. case 0x93E5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x4_OES";
  7921. case 0x93E6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5x5_OES";
  7922. case 0x93E7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5x5_OES";
  7923. case 0x93E8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x5_OES";
  7924. case 0x93E9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6x6_OES";
  7925. case 0x93F0: return "GL_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV2_IMG";
  7926. case 0x93F1: return "GL_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV2_IMG";
  7927. default:
  7928. return NULL;
  7929. }
  7930. }
  7931. ////////////////////////////////////////////////////////////////////
  7932. // Function: GLGraphicsStateGuardian::get_light_color
  7933. // Access: Public
  7934. // Description: Returns the value that that should be issued as the
  7935. // light's color, as scaled by the current value of
  7936. // _light_color_scale, in the case of
  7937. // color_scale_via_lighting.
  7938. ////////////////////////////////////////////////////////////////////
  7939. LVecBase4 CLP(GraphicsStateGuardian)::
  7940. get_light_color(Light *light) const {
  7941. #ifndef NDEBUG
  7942. if (_show_texture_usage) {
  7943. // In show_texture_usage mode, all lights are white, so as not to
  7944. // contaminate the texture color.
  7945. return LVecBase4(1.0, 1.0, 1.0, 1.0);
  7946. }
  7947. #endif // NDEBUG
  7948. const LColor &c = light->get_color();
  7949. LVecBase4 light_color(c[0] * _light_color_scale[0],
  7950. c[1] * _light_color_scale[1],
  7951. c[2] * _light_color_scale[2],
  7952. c[3] * _light_color_scale[3]);
  7953. return light_color;
  7954. }
  7955. ////////////////////////////////////////////////////////////////////
  7956. // Function: GLGraphicsStateGuardian::reissue_transforms
  7957. // Access: Protected, Virtual
  7958. // Description: Called by clear_state_and_transform() to ensure that
  7959. // the current modelview and projection matrices are
  7960. // properly loaded in the graphics state, after a
  7961. // callback might have mucked them up.
  7962. ////////////////////////////////////////////////////////////////////
  7963. void CLP(GraphicsStateGuardian)::
  7964. reissue_transforms() {
  7965. prepare_lens();
  7966. do_issue_transform();
  7967. }
  7968. #ifdef SUPPORT_FIXED_FUNCTION
  7969. ////////////////////////////////////////////////////////////////////
  7970. // Function: GLGraphicsStateGuardian::enable_lighting
  7971. // Access: Protected, Virtual
  7972. // Description: Intended to be overridden by a derived class to
  7973. // enable or disable the use of lighting overall. This
  7974. // is called by do_issue_light() according to whether any
  7975. // lights are in use or not.
  7976. ////////////////////////////////////////////////////////////////////
  7977. void CLP(GraphicsStateGuardian)::
  7978. enable_lighting(bool enable) {
  7979. // static PStatCollector _draw_set_state_light_enable_lighting_pcollector("Draw:Set State:Light:Enable lighting");
  7980. // PStatGPUTimer timer(this, _draw_set_state_light_enable_lighting_pcollector);
  7981. if (enable) {
  7982. glEnable(GL_LIGHTING);
  7983. } else {
  7984. glDisable(GL_LIGHTING);
  7985. }
  7986. }
  7987. #endif // SUPPORT_FIXED_FUNCTION
  7988. #ifdef SUPPORT_FIXED_FUNCTION
  7989. ////////////////////////////////////////////////////////////////////
  7990. // Function: GLGraphicsStateGuardian::set_ambient_light
  7991. // Access: Protected, Virtual
  7992. // Description: Intended to be overridden by a derived class to
  7993. // indicate the color of the ambient light that should
  7994. // be in effect. This is called by do_issue_light() after
  7995. // all other lights have been enabled or disabled.
  7996. ////////////////////////////////////////////////////////////////////
  7997. void CLP(GraphicsStateGuardian)::
  7998. set_ambient_light(const LColor &color) {
  7999. // static PStatCollector _draw_set_state_light_ambient_pcollector("Draw:Set State:Light:Ambient");
  8000. // PStatGPUTimer timer(this, _draw_set_state_light_ambient_pcollector);
  8001. LColor c = color;
  8002. c.set(c[0] * _light_color_scale[0],
  8003. c[1] * _light_color_scale[1],
  8004. c[2] * _light_color_scale[2],
  8005. c[3] * _light_color_scale[3]);
  8006. call_glLightModelfv(GL_LIGHT_MODEL_AMBIENT, c);
  8007. }
  8008. #endif // SUPPORT_FIXED_FUNCTION
  8009. #ifdef SUPPORT_FIXED_FUNCTION
  8010. ////////////////////////////////////////////////////////////////////
  8011. // Function: GLGraphicsStateGuardian::enable_light
  8012. // Access: Protected, Virtual
  8013. // Description: Intended to be overridden by a derived class to
  8014. // enable the indicated light id. A specific Light will
  8015. // already have been bound to this id via bind_light().
  8016. ////////////////////////////////////////////////////////////////////
  8017. void CLP(GraphicsStateGuardian)::
  8018. enable_light(int light_id, bool enable) {
  8019. // static PStatCollector _draw_set_state_light_enable_light_pcollector("Draw:Set State:Light:Enable light");
  8020. // PStatGPUTimer timer(this, _draw_set_state_light_enable_light_pcollector);
  8021. if (enable) {
  8022. glEnable(get_light_id(light_id));
  8023. } else {
  8024. glDisable(get_light_id(light_id));
  8025. }
  8026. }
  8027. #endif // SUPPORT_FIXED_FUNCTION
  8028. #ifdef SUPPORT_FIXED_FUNCTION
  8029. ////////////////////////////////////////////////////////////////////
  8030. // Function: GLGraphicsStateGuardian::begin_bind_lights
  8031. // Access: Protected, Virtual
  8032. // Description: Called immediately before bind_light() is called,
  8033. // this is intended to provide the derived class a hook
  8034. // in which to set up some state (like transform) that
  8035. // might apply to several lights.
  8036. //
  8037. // The sequence is: begin_bind_lights() will be called,
  8038. // then one or more bind_light() calls, then
  8039. // end_bind_lights().
  8040. ////////////////////////////////////////////////////////////////////
  8041. void CLP(GraphicsStateGuardian)::
  8042. begin_bind_lights() {
  8043. // static PStatCollector _draw_set_state_light_begin_bind_pcollector("Draw:Set State:Light:Begin bind");
  8044. // PStatGPUTimer timer(this, _draw_set_state_light_begin_bind_pcollector);
  8045. // We need to temporarily load a new matrix so we can define the
  8046. // light in a known coordinate system. We pick the transform of the
  8047. // root. (Alternatively, we could leave the current transform where
  8048. // it is and compute the light position relative to that transform
  8049. // instead of relative to the root, by composing with the matrix
  8050. // computed by _internal_transform->invert_compose(render_transform).
  8051. // But I think loading a completely new matrix is simpler.)
  8052. CPT(TransformState) render_transform =
  8053. _cs_transform->compose(_scene_setup->get_world_transform());
  8054. glMatrixMode(GL_MODELVIEW);
  8055. glPushMatrix();
  8056. GLPf(LoadMatrix)(render_transform->get_mat().get_data());
  8057. }
  8058. #endif // SUPPORT_FIXED_FUNCTION
  8059. #ifdef SUPPORT_FIXED_FUNCTION
  8060. ////////////////////////////////////////////////////////////////////
  8061. // Function: GLGraphicsStateGuardian::end_bind_lights
  8062. // Access: Protected, Virtual
  8063. // Description: Called after before bind_light() has been called one
  8064. // or more times (but before any geometry is issued or
  8065. // additional state is changed), this is intended to
  8066. // clean up any temporary changes to the state that may
  8067. // have been made by begin_bind_lights().
  8068. ////////////////////////////////////////////////////////////////////
  8069. void CLP(GraphicsStateGuardian)::
  8070. end_bind_lights() {
  8071. // static PStatCollector _draw_set_state_light_end_bind_pcollector("Draw:Set State:Light:End bind");
  8072. // PStatGPUTimer timer(this, _draw_set_state_light_end_bind_pcollector);
  8073. glMatrixMode(GL_MODELVIEW);
  8074. glPopMatrix();
  8075. }
  8076. #endif // SUPPORT_FIXED_FUNCTION
  8077. #ifdef SUPPORT_FIXED_FUNCTION
  8078. ////////////////////////////////////////////////////////////////////
  8079. // Function: GLGraphicsStateGuardian::enable_clip_plane
  8080. // Access: Protected, Virtual
  8081. // Description: Intended to be overridden by a derived class to
  8082. // enable the indicated clip_plane id. A specific
  8083. // PlaneNode will already have been bound to this id via
  8084. // bind_clip_plane().
  8085. ////////////////////////////////////////////////////////////////////
  8086. void CLP(GraphicsStateGuardian)::
  8087. enable_clip_plane(int plane_id, bool enable) {
  8088. if (enable) {
  8089. glEnable(get_clip_plane_id(plane_id));
  8090. } else {
  8091. glDisable(get_clip_plane_id(plane_id));
  8092. }
  8093. }
  8094. #endif // SUPPORT_FIXED_FUNCTION
  8095. #ifdef SUPPORT_FIXED_FUNCTION
  8096. ////////////////////////////////////////////////////////////////////
  8097. // Function: GLGraphicsStateGuardian::begin_bind_clip_planes
  8098. // Access: Protected, Virtual
  8099. // Description: Called immediately before bind_clip_plane() is called,
  8100. // this is intended to provide the derived class a hook
  8101. // in which to set up some state (like transform) that
  8102. // might apply to several clip_planes.
  8103. //
  8104. // The sequence is: begin_bind_clip_planes() will be called,
  8105. // then one or more bind_clip_plane() calls, then
  8106. // end_bind_clip_planes().
  8107. ////////////////////////////////////////////////////////////////////
  8108. void CLP(GraphicsStateGuardian)::
  8109. begin_bind_clip_planes() {
  8110. // We need to temporarily load a new matrix so we can define the
  8111. // clip_plane in a known coordinate system. We pick the transform of the
  8112. // root. (Alternatively, we could leave the current transform where
  8113. // it is and compute the clip_plane position relative to that transform
  8114. // instead of relative to the root, by composing with the matrix
  8115. // computed by _internal_transform->invert_compose(render_transform).
  8116. // But I think loading a completely new matrix is simpler.)
  8117. CPT(TransformState) render_transform =
  8118. _cs_transform->compose(_scene_setup->get_world_transform());
  8119. glMatrixMode(GL_MODELVIEW);
  8120. glPushMatrix();
  8121. GLPf(LoadMatrix)(render_transform->get_mat().get_data());
  8122. }
  8123. #endif // SUPPORT_FIXED_FUNCTION
  8124. #ifdef SUPPORT_FIXED_FUNCTION
  8125. ////////////////////////////////////////////////////////////////////
  8126. // Function: GLGraphicsStateGuardian::bind_clip_plane
  8127. // Access: Protected, Virtual
  8128. // Description: Called the first time a particular clip_plane has been
  8129. // bound to a given id within a frame, this should set
  8130. // up the associated hardware clip_plane with the clip_plane's
  8131. // properties.
  8132. ////////////////////////////////////////////////////////////////////
  8133. void CLP(GraphicsStateGuardian)::
  8134. bind_clip_plane(const NodePath &plane, int plane_id) {
  8135. GLenum id = get_clip_plane_id(plane_id);
  8136. CPT(TransformState) transform = plane.get_transform(_scene_setup->get_scene_root().get_parent());
  8137. const PlaneNode *plane_node;
  8138. DCAST_INTO_V(plane_node, plane.node());
  8139. LPlane xformed_plane = plane_node->get_plane() * transform->get_mat();
  8140. #ifdef OPENGLES
  8141. // OpenGL ES uses a single-precision call.
  8142. LPlanef single_plane(LCAST(float, xformed_plane));
  8143. glClipPlanef(id, single_plane.get_data());
  8144. #else
  8145. // Mainline OpenGL uses a double-precision call.
  8146. LPlaned double_plane(LCAST(double, xformed_plane));
  8147. glClipPlane(id, double_plane.get_data());
  8148. #endif // OPENGLES
  8149. report_my_gl_errors();
  8150. }
  8151. #endif // SUPPORT_FIXED_FUNCTION
  8152. #ifdef SUPPORT_FIXED_FUNCTION
  8153. ////////////////////////////////////////////////////////////////////
  8154. // Function: GLGraphicsStateGuardian::end_bind_clip_planes
  8155. // Access: Protected, Virtual
  8156. // Description: Called after before bind_clip_plane() has been called one
  8157. // or more times (but before any geometry is issued or
  8158. // additional state is changed), this is intended to
  8159. // clean up any temporary changes to the state that may
  8160. // have been made by begin_bind_clip_planes().
  8161. ////////////////////////////////////////////////////////////////////
  8162. void CLP(GraphicsStateGuardian)::
  8163. end_bind_clip_planes() {
  8164. glMatrixMode(GL_MODELVIEW);
  8165. glPopMatrix();
  8166. }
  8167. #endif // SUPPORT_FIXED_FUNCTION
  8168. ////////////////////////////////////////////////////////////////////
  8169. // Function: GLGraphicsStateGuardian::set_state_and_transform
  8170. // Access: Public, Virtual
  8171. // Description: Simultaneously resets the render state and the
  8172. // transform state.
  8173. //
  8174. // This transform specified is the "internal" net
  8175. // transform, already converted into the GSG's internal
  8176. // coordinate space by composing it to
  8177. // get_cs_transform(). (Previously, this used to be the
  8178. // "external" net transform, with the assumption that
  8179. // that GSG would convert it internally, but that is no
  8180. // longer the case.)
  8181. //
  8182. // Special case: if (state==NULL), then the target
  8183. // state is already stored in _target.
  8184. ////////////////////////////////////////////////////////////////////
  8185. void CLP(GraphicsStateGuardian)::
  8186. set_state_and_transform(const RenderState *target,
  8187. const TransformState *transform) {
  8188. report_my_gl_errors();
  8189. #ifndef NDEBUG
  8190. if (gsg_cat.is_spam()) {
  8191. gsg_cat.spam() << "Setting GSG state to " << (void *)target << ":\n";
  8192. target->write(gsg_cat.spam(false), 2);
  8193. }
  8194. #endif
  8195. _state_pcollector.add_level(1);
  8196. PStatGPUTimer timer1(this, _draw_set_state_pcollector);
  8197. if (transform != _internal_transform) {
  8198. //PStatGPUTimer timer(this, _draw_set_state_transform_pcollector);
  8199. _transform_state_pcollector.add_level(1);
  8200. _internal_transform = transform;
  8201. do_issue_transform();
  8202. }
  8203. if (target == _state_rs && (_state_mask | _inv_state_mask).is_all_on()) {
  8204. return;
  8205. }
  8206. _target_rs = target;
  8207. _target_shader = (const ShaderAttrib *)
  8208. _target_rs->get_attrib_def(ShaderAttrib::get_class_slot());
  8209. #ifndef OPENGLES_1
  8210. _instance_count = _target_shader->get_instance_count();
  8211. #endif
  8212. #ifdef SUPPORT_FIXED_FUNCTION
  8213. int alpha_test_slot = AlphaTestAttrib::get_class_slot();
  8214. if (_target_rs->get_attrib(alpha_test_slot) != _state_rs->get_attrib(alpha_test_slot) ||
  8215. !_state_mask.get_bit(alpha_test_slot) ||
  8216. (_target_shader->get_flag(ShaderAttrib::F_subsume_alpha_test) !=
  8217. _state_shader->get_flag(ShaderAttrib::F_subsume_alpha_test))) {
  8218. //PStatGPUTimer timer(this, _draw_set_state_alpha_test_pcollector);
  8219. do_issue_alpha_test();
  8220. _state_mask.set_bit(alpha_test_slot);
  8221. }
  8222. #endif
  8223. int antialias_slot = AntialiasAttrib::get_class_slot();
  8224. if (_target_rs->get_attrib(antialias_slot) != _state_rs->get_attrib(antialias_slot) ||
  8225. !_state_mask.get_bit(antialias_slot)) {
  8226. //PStatGPUTimer timer(this, _draw_set_state_antialias_pcollector);
  8227. do_issue_antialias();
  8228. _state_mask.set_bit(antialias_slot);
  8229. }
  8230. int clip_plane_slot = ClipPlaneAttrib::get_class_slot();
  8231. if (_target_rs->get_attrib(clip_plane_slot) != _state_rs->get_attrib(clip_plane_slot) ||
  8232. !_state_mask.get_bit(clip_plane_slot)) {
  8233. //PStatGPUTimer timer(this, _draw_set_state_clip_plane_pcollector);
  8234. do_issue_clip_plane();
  8235. _state_mask.set_bit(clip_plane_slot);
  8236. #ifndef OPENGLES_1
  8237. if (_current_shader_context) {
  8238. _current_shader_context->issue_parameters(Shader::SSD_clip_planes);
  8239. }
  8240. #endif
  8241. }
  8242. int color_slot = ColorAttrib::get_class_slot();
  8243. int color_scale_slot = ColorScaleAttrib::get_class_slot();
  8244. if (_target_rs->get_attrib(color_slot) != _state_rs->get_attrib(color_slot) ||
  8245. _target_rs->get_attrib(color_scale_slot) != _state_rs->get_attrib(color_scale_slot) ||
  8246. !_state_mask.get_bit(color_slot) ||
  8247. !_state_mask.get_bit(color_scale_slot)) {
  8248. //PStatGPUTimer timer(this, _draw_set_state_color_pcollector);
  8249. do_issue_color();
  8250. do_issue_color_scale();
  8251. _state_mask.set_bit(color_slot);
  8252. _state_mask.set_bit(color_scale_slot);
  8253. #ifndef OPENGLES_1
  8254. if (_current_shader_context) {
  8255. _current_shader_context->issue_parameters(Shader::SSD_color);
  8256. _current_shader_context->issue_parameters(Shader::SSD_colorscale);
  8257. }
  8258. #endif
  8259. }
  8260. int cull_face_slot = CullFaceAttrib::get_class_slot();
  8261. if (_target_rs->get_attrib(cull_face_slot) != _state_rs->get_attrib(cull_face_slot) ||
  8262. !_state_mask.get_bit(cull_face_slot)) {
  8263. //PStatGPUTimer timer(this, _draw_set_state_cull_face_pcollector);
  8264. do_issue_cull_face();
  8265. _state_mask.set_bit(cull_face_slot);
  8266. }
  8267. int depth_offset_slot = DepthOffsetAttrib::get_class_slot();
  8268. if (_target_rs->get_attrib(depth_offset_slot) != _state_rs->get_attrib(depth_offset_slot) ||
  8269. !_state_mask.get_bit(depth_offset_slot)) {
  8270. //PStatGPUTimer timer(this, _draw_set_state_depth_offset_pcollector);
  8271. do_issue_depth_offset();
  8272. _state_mask.set_bit(depth_offset_slot);
  8273. }
  8274. int depth_test_slot = DepthTestAttrib::get_class_slot();
  8275. if (_target_rs->get_attrib(depth_test_slot) != _state_rs->get_attrib(depth_test_slot) ||
  8276. !_state_mask.get_bit(depth_test_slot)) {
  8277. //PStatGPUTimer timer(this, _draw_set_state_depth_test_pcollector);
  8278. do_issue_depth_test();
  8279. _state_mask.set_bit(depth_test_slot);
  8280. }
  8281. int depth_write_slot = DepthWriteAttrib::get_class_slot();
  8282. if (_target_rs->get_attrib(depth_write_slot) != _state_rs->get_attrib(depth_write_slot) ||
  8283. !_state_mask.get_bit(depth_write_slot)) {
  8284. //PStatGPUTimer timer(this, _draw_set_state_depth_write_pcollector);
  8285. do_issue_depth_write();
  8286. _state_mask.set_bit(depth_write_slot);
  8287. }
  8288. int render_mode_slot = RenderModeAttrib::get_class_slot();
  8289. if (_target_rs->get_attrib(render_mode_slot) != _state_rs->get_attrib(render_mode_slot) ||
  8290. !_state_mask.get_bit(render_mode_slot)) {
  8291. //PStatGPUTimer timer(this, _draw_set_state_render_mode_pcollector);
  8292. do_issue_render_mode();
  8293. _state_mask.set_bit(render_mode_slot);
  8294. }
  8295. #ifdef SUPPORT_FIXED_FUNCTION
  8296. int rescale_normal_slot = RescaleNormalAttrib::get_class_slot();
  8297. if (_target_rs->get_attrib(rescale_normal_slot) != _state_rs->get_attrib(rescale_normal_slot) ||
  8298. !_state_mask.get_bit(rescale_normal_slot)) {
  8299. //PStatGPUTimer timer(this, _draw_set_state_rescale_normal_pcollector);
  8300. do_issue_rescale_normal();
  8301. _state_mask.set_bit(rescale_normal_slot);
  8302. }
  8303. #endif
  8304. #ifdef SUPPORT_FIXED_FUNCTION
  8305. int shade_model_slot = ShadeModelAttrib::get_class_slot();
  8306. if (_target_rs->get_attrib(shade_model_slot) != _state_rs->get_attrib(shade_model_slot) ||
  8307. !_state_mask.get_bit(shade_model_slot)) {
  8308. //PStatGPUTimer timer(this, _draw_set_state_shade_model_pcollector);
  8309. do_issue_shade_model();
  8310. _state_mask.set_bit(shade_model_slot);
  8311. }
  8312. #endif
  8313. int transparency_slot = TransparencyAttrib::get_class_slot();
  8314. int color_write_slot = ColorWriteAttrib::get_class_slot();
  8315. int color_blend_slot = ColorBlendAttrib::get_class_slot();
  8316. if (_target_rs->get_attrib(transparency_slot) != _state_rs->get_attrib(transparency_slot) ||
  8317. _target_rs->get_attrib(color_write_slot) != _state_rs->get_attrib(color_write_slot) ||
  8318. _target_rs->get_attrib(color_blend_slot) != _state_rs->get_attrib(color_blend_slot) ||
  8319. !_state_mask.get_bit(transparency_slot) ||
  8320. !_state_mask.get_bit(color_write_slot) ||
  8321. !_state_mask.get_bit(color_blend_slot) ||
  8322. (_target_shader->get_flag(ShaderAttrib::F_disable_alpha_write) !=
  8323. _state_shader->get_flag(ShaderAttrib::F_disable_alpha_write))) {
  8324. //PStatGPUTimer timer(this, _draw_set_state_blending_pcollector);
  8325. do_issue_blending();
  8326. _state_mask.set_bit(transparency_slot);
  8327. _state_mask.set_bit(color_write_slot);
  8328. _state_mask.set_bit(color_blend_slot);
  8329. }
  8330. if (_target_shader != _state_shader) {
  8331. //PStatGPUTimer timer(this, _draw_set_state_shader_pcollector);
  8332. #ifndef OPENGLES_1
  8333. do_issue_shader(true);
  8334. #endif
  8335. _state_shader = _target_shader;
  8336. _state_mask.clear_bit(TextureAttrib::get_class_slot());
  8337. }
  8338. #ifndef SUPPORT_FIXED_FUNCTION
  8339. else { // In the case of OpenGL ES 2.x, we need to glUseShader before we draw anything.
  8340. do_issue_shader(false);
  8341. }
  8342. #endif
  8343. int texture_slot = TextureAttrib::get_class_slot();
  8344. if (_target_rs->get_attrib(texture_slot) != _state_rs->get_attrib(texture_slot) ||
  8345. !_state_mask.get_bit(texture_slot)) {
  8346. //PStatGPUTimer timer(this, _draw_set_state_texture_pcollector);
  8347. determine_target_texture();
  8348. int prev_active = _num_active_texture_stages;
  8349. do_issue_texture();
  8350. // Since the TexGen and TexMatrix states depend partly on the
  8351. // particular set of textures in use, we should force both of
  8352. // those to be reissued every time we change the texture state.
  8353. _state_mask.clear_bit(TexGenAttrib::get_class_slot());
  8354. _state_mask.clear_bit(TexMatrixAttrib::get_class_slot());
  8355. _state_texture = _target_texture;
  8356. _state_mask.set_bit(texture_slot);
  8357. }
  8358. // If one of the previously-loaded TexGen modes modified the texture
  8359. // matrix, then if either state changed, we have to change both of
  8360. // them now.
  8361. if (_tex_gen_modifies_mat) {
  8362. int tex_gen_slot = TexGenAttrib::get_class_slot();
  8363. int tex_matrix_slot = TexMatrixAttrib::get_class_slot();
  8364. if (_target_rs->get_attrib(tex_gen_slot) != _state_rs->get_attrib(tex_gen_slot) ||
  8365. _target_rs->get_attrib(tex_matrix_slot) != _state_rs->get_attrib(tex_matrix_slot) ||
  8366. !_state_mask.get_bit(tex_gen_slot) ||
  8367. !_state_mask.get_bit(tex_matrix_slot)) {
  8368. _state_mask.clear_bit(tex_gen_slot);
  8369. _state_mask.clear_bit(tex_matrix_slot);
  8370. }
  8371. }
  8372. int tex_matrix_slot = TexMatrixAttrib::get_class_slot();
  8373. if (_target_rs->get_attrib(tex_matrix_slot) != _state_rs->get_attrib(tex_matrix_slot) ||
  8374. !_state_mask.get_bit(tex_matrix_slot)) {
  8375. //PStatGPUTimer timer(this, _draw_set_state_tex_matrix_pcollector);
  8376. #ifdef SUPPORT_FIXED_FUNCTION
  8377. do_issue_tex_matrix();
  8378. #endif
  8379. _state_mask.set_bit(tex_matrix_slot);
  8380. #ifndef OPENGLES_1
  8381. if (_current_shader_context) {
  8382. _current_shader_context->issue_parameters(Shader::SSD_tex_matrix);
  8383. }
  8384. #endif
  8385. }
  8386. #ifdef SUPPORT_FIXED_FUNCTION
  8387. int tex_gen_slot = TexGenAttrib::get_class_slot();
  8388. if (_target_tex_gen != _state_tex_gen ||
  8389. !_state_mask.get_bit(tex_gen_slot)) {
  8390. //PStatGPUTimer timer(this, _draw_set_state_tex_gen_pcollector);
  8391. do_issue_tex_gen();
  8392. _state_tex_gen = _target_tex_gen;
  8393. _state_mask.set_bit(tex_gen_slot);
  8394. }
  8395. #endif
  8396. int material_slot = MaterialAttrib::get_class_slot();
  8397. if (_target_rs->get_attrib(material_slot) != _state_rs->get_attrib(material_slot) ||
  8398. !_state_mask.get_bit(material_slot)) {
  8399. //PStatGPUTimer timer(this, _draw_set_state_material_pcollector);
  8400. #ifdef SUPPORT_FIXED_FUNCTION
  8401. do_issue_material();
  8402. #endif
  8403. _state_mask.set_bit(material_slot);
  8404. #ifndef OPENGLES_1
  8405. if (_current_shader_context) {
  8406. _current_shader_context->issue_parameters(Shader::SSD_material);
  8407. }
  8408. #endif
  8409. }
  8410. int light_slot = LightAttrib::get_class_slot();
  8411. if (_target_rs->get_attrib(light_slot) != _state_rs->get_attrib(light_slot) ||
  8412. !_state_mask.get_bit(light_slot)) {
  8413. //PStatGPUTimer timer(this, _draw_set_state_light_pcollector);
  8414. #ifdef SUPPORT_FIXED_FUNCTION
  8415. do_issue_light();
  8416. #endif
  8417. _state_mask.set_bit(light_slot);
  8418. #ifndef OPENGLES_1
  8419. if (_current_shader_context) {
  8420. _current_shader_context->issue_parameters(Shader::SSD_light);
  8421. }
  8422. #endif
  8423. }
  8424. int stencil_slot = StencilAttrib::get_class_slot();
  8425. if (_target_rs->get_attrib(stencil_slot) != _state_rs->get_attrib(stencil_slot) ||
  8426. !_state_mask.get_bit(stencil_slot)) {
  8427. //PStatGPUTimer timer(this, _draw_set_state_stencil_pcollector);
  8428. do_issue_stencil();
  8429. _state_mask.set_bit(stencil_slot);
  8430. }
  8431. int fog_slot = FogAttrib::get_class_slot();
  8432. if (_target_rs->get_attrib(fog_slot) != _state_rs->get_attrib(fog_slot) ||
  8433. !_state_mask.get_bit(fog_slot)) {
  8434. //PStatGPUTimer timer(this, _draw_set_state_fog_pcollector);
  8435. #ifdef SUPPORT_FIXED_FUNCTION
  8436. do_issue_fog();
  8437. #endif
  8438. _state_mask.set_bit(fog_slot);
  8439. #ifndef OPENGLES_1
  8440. if (_current_shader_context) {
  8441. _current_shader_context->issue_parameters(Shader::SSD_fog);
  8442. }
  8443. #endif
  8444. }
  8445. int scissor_slot = ScissorAttrib::get_class_slot();
  8446. if (_target_rs->get_attrib(scissor_slot) != _state_rs->get_attrib(scissor_slot) ||
  8447. !_state_mask.get_bit(scissor_slot)) {
  8448. //PStatGPUTimer timer(this, _draw_set_state_scissor_pcollector);
  8449. do_issue_scissor();
  8450. _state_mask.set_bit(scissor_slot);
  8451. }
  8452. _state_rs = _target_rs;
  8453. maybe_gl_finish();
  8454. report_my_gl_errors();
  8455. }
  8456. ////////////////////////////////////////////////////////////////////
  8457. // Function: GLGraphicsStateGuardian::free_pointers
  8458. // Access: Protected, Virtual
  8459. // Description: Frees some memory that was explicitly allocated
  8460. // within the glgsg.
  8461. ////////////////////////////////////////////////////////////////////
  8462. void CLP(GraphicsStateGuardian)::
  8463. free_pointers() {
  8464. #if defined(HAVE_CG) && !defined(OPENGLES)
  8465. if (_cg_context != 0) {
  8466. cgDestroyContext(_cg_context);
  8467. _cg_context = 0;
  8468. }
  8469. #endif
  8470. }
  8471. ////////////////////////////////////////////////////////////////////
  8472. // Function: GLGraphicsStateGuardian::do_issue_texture
  8473. // Access: Protected, Virtual
  8474. // Description: This is called by set_state_and_transform() when
  8475. // the texture state has changed.
  8476. ////////////////////////////////////////////////////////////////////
  8477. void CLP(GraphicsStateGuardian)::
  8478. do_issue_texture() {
  8479. DO_PSTATS_STUFF(_texture_state_pcollector.add_level(1));
  8480. #ifdef OPENGLES_1
  8481. update_standard_texture_bindings();
  8482. #else
  8483. if (_current_shader_context == 0 || !_current_shader_context->uses_custom_texture_bindings()) {
  8484. // No shader, or a non-Cg shader.
  8485. if (_texture_binding_shader_context != 0) {
  8486. _texture_binding_shader_context->disable_shader_texture_bindings();
  8487. }
  8488. #ifdef SUPPORT_FIXED_FUNCTION
  8489. update_standard_texture_bindings();
  8490. #endif
  8491. } else {
  8492. if (_texture_binding_shader_context == 0) {
  8493. #ifdef SUPPORT_FIXED_FUNCTION
  8494. disable_standard_texture_bindings();
  8495. #endif
  8496. _current_shader_context->update_shader_texture_bindings(NULL);
  8497. } else {
  8498. _current_shader_context->
  8499. update_shader_texture_bindings(_texture_binding_shader_context);
  8500. }
  8501. }
  8502. _texture_binding_shader = _current_shader;
  8503. _texture_binding_shader_context = _current_shader_context;
  8504. #endif
  8505. }
  8506. #ifdef SUPPORT_FIXED_FUNCTION
  8507. ////////////////////////////////////////////////////////////////////
  8508. // Function: GLGraphicsStateGuardian::update_standard_texture_bindings
  8509. // Access: Private
  8510. // Description: Applies the appropriate set of textures for the
  8511. // current state, using the standard fixed-function
  8512. // pipeline.
  8513. ////////////////////////////////////////////////////////////////////
  8514. void CLP(GraphicsStateGuardian)::
  8515. update_standard_texture_bindings() {
  8516. #ifndef NDEBUG
  8517. if (_show_texture_usage) {
  8518. update_show_usage_texture_bindings(-1);
  8519. return;
  8520. }
  8521. #endif // NDEBUG
  8522. int num_stages = _target_texture->get_num_on_ff_stages();
  8523. #ifndef NDEBUG
  8524. // Also check the _flash_texture. If it is non-NULL, we need to
  8525. // check to see if our flash_texture is in the texture stack here.
  8526. // If so, then we need to call the special show_texture method
  8527. // instead of the normal texture stack.
  8528. if (_flash_texture != (Texture *)NULL) {
  8529. double now = ClockObject::get_global_clock()->get_frame_time();
  8530. int this_second = (int)floor(now);
  8531. if (this_second & 1) {
  8532. int show_stage_index = -1;
  8533. for (int i = 0; i < num_stages && show_stage_index < 0; ++i) {
  8534. TextureStage *stage = _target_texture->get_on_ff_stage(i);
  8535. Texture *texture = _target_texture->get_on_texture(stage);
  8536. if (texture == _flash_texture) {
  8537. show_stage_index = i;
  8538. }
  8539. }
  8540. if (show_stage_index >= 0) {
  8541. update_show_usage_texture_bindings(show_stage_index);
  8542. return;
  8543. }
  8544. }
  8545. }
  8546. #endif // NDEBUG
  8547. nassertv(num_stages <= _max_texture_stages &&
  8548. _num_active_texture_stages <= _max_texture_stages);
  8549. _texture_involves_color_scale = false;
  8550. int last_saved_result = -1;
  8551. int last_stage = -1;
  8552. int i;
  8553. for (i = 0; i < num_stages; i++) {
  8554. TextureStage *stage = _target_texture->get_on_ff_stage(i);
  8555. Texture *texture = _target_texture->get_on_texture(stage);
  8556. nassertv(texture != (Texture *)NULL);
  8557. // Issue the texture on stage i.
  8558. _glActiveTexture(GL_TEXTURE0 + i);
  8559. // First, turn off the previous texture mode.
  8560. glDisable(GL_TEXTURE_2D);
  8561. if (_supports_cube_map) {
  8562. glDisable(GL_TEXTURE_CUBE_MAP);
  8563. }
  8564. #ifndef OPENGLES
  8565. glDisable(GL_TEXTURE_1D);
  8566. if (_supports_3d_texture) {
  8567. glDisable(GL_TEXTURE_3D);
  8568. }
  8569. #endif // OPENGLES
  8570. int view = get_current_tex_view_offset() + stage->get_tex_view_offset();
  8571. TextureContext *tc = texture->prepare_now(view, _prepared_objects, this);
  8572. if (tc == (TextureContext *)NULL) {
  8573. // Something wrong with this texture; skip it.
  8574. continue;
  8575. }
  8576. // Then, turn on the current texture mode.
  8577. GLenum target = get_texture_target(texture->get_texture_type());
  8578. if (target == GL_NONE) {
  8579. // Unsupported texture mode.
  8580. continue;
  8581. }
  8582. #ifndef OPENGLES
  8583. if (target == GL_TEXTURE_2D_ARRAY_EXT) {
  8584. // Cannot be applied via the FFP.
  8585. continue;
  8586. }
  8587. #endif // OPENGLES
  8588. glEnable(target);
  8589. if (!update_texture(tc, false)) {
  8590. glDisable(target);
  8591. continue;
  8592. }
  8593. apply_texture(tc);
  8594. apply_sampler(i, _target_texture->get_on_sampler(stage), tc);
  8595. if (stage->involves_color_scale() && _color_scale_enabled) {
  8596. LColor color = stage->get_color();
  8597. color.set(color[0] * _current_color_scale[0],
  8598. color[1] * _current_color_scale[1],
  8599. color[2] * _current_color_scale[2],
  8600. color[3] * _current_color_scale[3]);
  8601. _texture_involves_color_scale = true;
  8602. call_glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, color);
  8603. } else {
  8604. call_glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, stage->get_color());
  8605. }
  8606. if (stage->get_mode() == TextureStage::M_decal) {
  8607. if (texture->get_num_components() < 3 && _supports_texture_combine) {
  8608. // Make a special case for 1- and 2-channel decal textures.
  8609. // OpenGL does not define their use with GL_DECAL for some
  8610. // reason, so implement them using the combiner instead.
  8611. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
  8612. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
  8613. glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, 1);
  8614. glTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);
  8615. glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE);
  8616. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);
  8617. glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PREVIOUS);
  8618. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);
  8619. glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB, GL_TEXTURE);
  8620. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA);
  8621. } else {
  8622. // Normal 3- and 4-channel decal textures.
  8623. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
  8624. }
  8625. } else if (stage->get_mode() == TextureStage::M_combine) {
  8626. if (!_supports_texture_combine) {
  8627. GLCAT.warning()
  8628. << "TextureStage::M_combine mode is not supported.\n";
  8629. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  8630. } else {
  8631. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
  8632. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE);
  8633. glTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE, stage->get_rgb_scale());
  8634. glTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, stage->get_alpha_scale());
  8635. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
  8636. get_texture_combine_type(stage->get_combine_rgb_mode()));
  8637. switch (stage->get_num_combine_rgb_operands()) {
  8638. case 3:
  8639. glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_RGB,
  8640. get_texture_src_type(stage->get_combine_rgb_source2(),
  8641. last_stage, last_saved_result, i));
  8642. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB,
  8643. get_texture_operand_type(stage->get_combine_rgb_operand2()));
  8644. // fall through
  8645. case 2:
  8646. glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB,
  8647. get_texture_src_type(stage->get_combine_rgb_source1(),
  8648. last_stage, last_saved_result, i));
  8649. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB,
  8650. get_texture_operand_type(stage->get_combine_rgb_operand1()));
  8651. // fall through
  8652. case 1:
  8653. glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB,
  8654. get_texture_src_type(stage->get_combine_rgb_source0(),
  8655. last_stage, last_saved_result, i));
  8656. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB,
  8657. get_texture_operand_type(stage->get_combine_rgb_operand0()));
  8658. // fall through
  8659. default:
  8660. break;
  8661. }
  8662. glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA,
  8663. get_texture_combine_type(stage->get_combine_alpha_mode()));
  8664. switch (stage->get_num_combine_alpha_operands()) {
  8665. case 3:
  8666. glTexEnvi(GL_TEXTURE_ENV, GL_SRC2_ALPHA,
  8667. get_texture_src_type(stage->get_combine_alpha_source2(),
  8668. last_stage, last_saved_result, i));
  8669. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA,
  8670. get_texture_operand_type(stage->get_combine_alpha_operand2()));
  8671. // fall through
  8672. case 2:
  8673. glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA,
  8674. get_texture_src_type(stage->get_combine_alpha_source1(),
  8675. last_stage, last_saved_result, i));
  8676. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA,
  8677. get_texture_operand_type(stage->get_combine_alpha_operand1()));
  8678. // fall through
  8679. case 1:
  8680. glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA,
  8681. get_texture_src_type(stage->get_combine_alpha_source0(),
  8682. last_stage, last_saved_result, i));
  8683. glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
  8684. get_texture_operand_type(stage->get_combine_alpha_operand0()));
  8685. // fall through
  8686. default:
  8687. break;
  8688. }
  8689. }
  8690. } else {
  8691. GLint glmode = get_texture_apply_mode_type(stage->get_mode());
  8692. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, glmode);
  8693. }
  8694. if (stage->get_saved_result()) {
  8695. // This texture's result will be "saved" for a future stage's
  8696. // input.
  8697. last_saved_result = i;
  8698. } else {
  8699. // This is a regular texture stage; it will be the "previous"
  8700. // input for the next stage.
  8701. last_stage = i;
  8702. }
  8703. }
  8704. // Disable the texture stages that are no longer used.
  8705. for (i = num_stages; i < _num_active_texture_stages; i++) {
  8706. _glActiveTexture(GL_TEXTURE0 + i);
  8707. glDisable(GL_TEXTURE_2D);
  8708. if (_supports_cube_map) {
  8709. glDisable(GL_TEXTURE_CUBE_MAP);
  8710. }
  8711. #ifndef OPENGLES
  8712. glDisable(GL_TEXTURE_1D);
  8713. if (_supports_3d_texture) {
  8714. glDisable(GL_TEXTURE_3D);
  8715. }
  8716. #endif // OPENGLES
  8717. }
  8718. // Save the count of texture stages for next time.
  8719. _num_active_texture_stages = num_stages;
  8720. report_my_gl_errors();
  8721. }
  8722. #endif // SUPPORT_FIXED_FUNCTION
  8723. ////////////////////////////////////////////////////////////////////
  8724. // Function: GLGraphicsStateGuardian::apply_white_texture
  8725. // Access: Private
  8726. // Description: Applies a white dummy texture. This is useful to
  8727. // bind to a texture slot when a texture is missing.
  8728. ////////////////////////////////////////////////////////////////////
  8729. void CLP(GraphicsStateGuardian)::
  8730. apply_white_texture() {
  8731. if (_white_texture != 0) {
  8732. glBindTexture(GL_TEXTURE_2D, _white_texture);
  8733. return;
  8734. }
  8735. glGenTextures(1, &_white_texture);
  8736. glBindTexture(GL_TEXTURE_2D, _white_texture);
  8737. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  8738. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  8739. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  8740. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  8741. unsigned char data[] = {0xff, 0xff, 0xff, 0xff};
  8742. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0,
  8743. GL_RGBA, GL_UNSIGNED_BYTE, data);
  8744. }
  8745. #ifndef NDEBUG
  8746. ////////////////////////////////////////////////////////////////////
  8747. // Function: GLGraphicsStateGuardian::update_show_usage_texture_bindings
  8748. // Access: Private
  8749. // Description: This is a special function that loads the usage
  8750. // textures in gl-show-texture-usage mode, instead of
  8751. // loading the actual used textures.
  8752. //
  8753. // If the indicated stage_index is >= 0, then it is the
  8754. // particular texture that is shown. Otherwise, the
  8755. // textures are rotated through based on
  8756. // show_texture_usage_index.
  8757. ////////////////////////////////////////////////////////////////////
  8758. void CLP(GraphicsStateGuardian)::
  8759. update_show_usage_texture_bindings(int show_stage_index) {
  8760. int num_stages = _target_texture->get_num_on_ff_stages();
  8761. nassertv(num_stages <= _max_texture_stages &&
  8762. _num_active_texture_stages <= _max_texture_stages);
  8763. _texture_involves_color_scale = false;
  8764. // First, we walk through the list of textures and pretend to render
  8765. // them all, even though we don't actually render them, just so
  8766. // Panda will keep track of the list of "active" textures correctly
  8767. // during the flash.
  8768. int i;
  8769. for (i = 0; i < num_stages; i++) {
  8770. TextureStage *stage = _target_texture->get_on_ff_stage(i);
  8771. Texture *texture = _target_texture->get_on_texture(stage);
  8772. nassertv(texture != (Texture *)NULL);
  8773. int view = get_current_tex_view_offset() + stage->get_tex_view_offset();
  8774. TextureContext *tc = texture->prepare_now(view, _prepared_objects, this);
  8775. if (tc == (TextureContext *)NULL) {
  8776. // Something wrong with this texture; skip it.
  8777. break;
  8778. }
  8779. tc->enqueue_lru(&_prepared_objects->_graphics_memory_lru);
  8780. }
  8781. #ifdef SUPPORT_FIXED_FUNCTION
  8782. // Disable all texture stages.
  8783. for (i = 0; i < _num_active_texture_stages; i++) {
  8784. _glActiveTexture(GL_TEXTURE0 + i);
  8785. #ifndef OPENGLES
  8786. glDisable(GL_TEXTURE_1D);
  8787. #endif // OPENGLES
  8788. glDisable(GL_TEXTURE_2D);
  8789. if (_supports_3d_texture) {
  8790. #ifndef OPENGLES_1
  8791. glDisable(GL_TEXTURE_3D);
  8792. #endif // OPENGLES_1
  8793. }
  8794. if (_supports_cube_map) {
  8795. glDisable(GL_TEXTURE_CUBE_MAP);
  8796. }
  8797. }
  8798. #endif
  8799. // Save the count of texture stages for next time.
  8800. _num_active_texture_stages = num_stages;
  8801. if (num_stages > 0) {
  8802. // Now, pick just one texture stage to apply.
  8803. if (show_stage_index >= 0 && show_stage_index < num_stages) {
  8804. i = show_stage_index;
  8805. } else {
  8806. i = _show_texture_usage_index % num_stages;
  8807. }
  8808. TextureStage *stage = _target_texture->get_on_ff_stage(i);
  8809. Texture *texture = _target_texture->get_on_texture(stage);
  8810. nassertv(texture != (Texture *)NULL);
  8811. // Choose the corresponding usage texture and apply it.
  8812. _glActiveTexture(GL_TEXTURE0 + i);
  8813. #ifdef SUPPORT_FIXED_FUNCTION
  8814. glEnable(GL_TEXTURE_2D);
  8815. #endif
  8816. UsageTextureKey key(texture->get_x_size(), texture->get_y_size());
  8817. UsageTextures::iterator ui = _usage_textures.find(key);
  8818. if (ui == _usage_textures.end()) {
  8819. // Need to create a new texture for this size.
  8820. GLuint index;
  8821. glGenTextures(1, &index);
  8822. glBindTexture(GL_TEXTURE_2D, index);
  8823. //TODO: this could be a lot simpler with glTexStorage2D
  8824. // followed by a call to glClearTexImage.
  8825. upload_usage_texture(texture->get_x_size(), texture->get_y_size());
  8826. _usage_textures[key] = index;
  8827. } else {
  8828. // Just bind the previously-created texture.
  8829. GLuint index = (*ui).second;
  8830. glBindTexture(GL_TEXTURE_2D, index);
  8831. }
  8832. //TODO: glBindSampler(0) ?
  8833. }
  8834. report_my_gl_errors();
  8835. }
  8836. #endif // NDEBUG
  8837. #ifndef NDEBUG
  8838. ////////////////////////////////////////////////////////////////////
  8839. // Function: GLGraphicsStateGuardian::upload_usage_texture
  8840. // Access: Protected
  8841. // Description: Uploads a special "usage" texture intended to be
  8842. // applied only in gl-show-texture-usage mode, to reveal
  8843. // where texture memory is being spent.
  8844. ////////////////////////////////////////////////////////////////////
  8845. void CLP(GraphicsStateGuardian)::
  8846. upload_usage_texture(int width, int height) {
  8847. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  8848. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  8849. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  8850. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  8851. if (GLCAT.is_debug()) {
  8852. GLCAT.debug()
  8853. << "upload_usage_texture(" << width << ", " << height << ")\n";
  8854. }
  8855. static LColor colors[3] = {
  8856. LColor(0.4, 0.5f, 0.8f, 1.0f), // mipmap 0: blue
  8857. LColor(1.0f, 1.0f, 0.0f, 1.0f), // mipmap 1: yellow
  8858. LColor(0.8f, 0.3, 0.3, 1.0f), // mipmap 2 and higher: red
  8859. };
  8860. // Allocate a temporary array large enough to contain the toplevel
  8861. // mipmap.
  8862. PN_uint32 *buffer = (PN_uint32 *)PANDA_MALLOC_ARRAY(width * height * 4);
  8863. int n = 0;
  8864. while (true) {
  8865. // Choose the color for the nth mipmap.
  8866. LColor c = colors[min(n, 2)];
  8867. // A simple union to store the colors values bytewise, and get the
  8868. // answer wordwise, independently of machine byte-ordernig.
  8869. union {
  8870. struct {
  8871. unsigned char r, g, b, a;
  8872. } b;
  8873. PN_uint32 w;
  8874. } store;
  8875. store.b.r = (unsigned char)(c[0] * 255.0f);
  8876. store.b.g = (unsigned char)(c[1] * 255.0f);
  8877. store.b.b = (unsigned char)(c[2] * 255.0f);
  8878. store.b.a = 0xff;
  8879. // Fill in the array.
  8880. int num_pixels = width * height;
  8881. for (int p = 0; p < num_pixels; ++p) {
  8882. buffer[p] = store.w;
  8883. }
  8884. glTexImage2D(GL_TEXTURE_2D, n, GL_RGBA, width, height, 0,
  8885. GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  8886. if (width == 1 && height == 1) {
  8887. // That was the last mipmap level.
  8888. break;
  8889. }
  8890. width = max(width >> 1, 1);
  8891. height = max(height >> 1, 1);
  8892. ++n;
  8893. }
  8894. PANDA_FREE_ARRAY(buffer);
  8895. }
  8896. #endif // NDEBUG
  8897. #ifdef SUPPORT_FIXED_FUNCTION
  8898. ////////////////////////////////////////////////////////////////////
  8899. // Function: GLGraphicsStateGuardian::disable_standard_texture_bindings
  8900. // Access: Private
  8901. // Description:
  8902. ////////////////////////////////////////////////////////////////////
  8903. void CLP(GraphicsStateGuardian)::
  8904. disable_standard_texture_bindings() {
  8905. // Disable the texture stages that are no longer used.
  8906. for (int i = 0; i < _num_active_texture_stages; i++) {
  8907. _glActiveTexture(GL_TEXTURE0 + i);
  8908. #ifndef OPENGLES
  8909. glDisable(GL_TEXTURE_1D);
  8910. #endif // OPENGLES
  8911. glDisable(GL_TEXTURE_2D);
  8912. if (_supports_3d_texture) {
  8913. #ifndef OPENGLES_1
  8914. glDisable(GL_TEXTURE_3D);
  8915. #endif // OPENGLES_1
  8916. }
  8917. if (_supports_cube_map) {
  8918. glDisable(GL_TEXTURE_CUBE_MAP);
  8919. }
  8920. }
  8921. _num_active_texture_stages = 0;
  8922. report_my_gl_errors();
  8923. }
  8924. #endif // SUPPORT_FIXED_FUNCTION
  8925. #ifdef SUPPORT_FIXED_FUNCTION
  8926. ////////////////////////////////////////////////////////////////////
  8927. // Function: GLGraphicsStateGuardian::do_issue_tex_matrix
  8928. // Access: Protected
  8929. // Description:
  8930. ////////////////////////////////////////////////////////////////////
  8931. void CLP(GraphicsStateGuardian)::
  8932. do_issue_tex_matrix() {
  8933. nassertv(_num_active_texture_stages <= _max_texture_stages);
  8934. for (int i = 0; i < _num_active_texture_stages; i++) {
  8935. TextureStage *stage = _target_texture->get_on_ff_stage(i);
  8936. _glActiveTexture(GL_TEXTURE0 + i);
  8937. glMatrixMode(GL_TEXTURE);
  8938. const TexMatrixAttrib *target_tex_matrix;
  8939. _target_rs->get_attrib_def(target_tex_matrix);
  8940. if (target_tex_matrix->has_stage(stage)) {
  8941. GLPf(LoadMatrix)(target_tex_matrix->get_mat(stage).get_data());
  8942. } else {
  8943. glLoadIdentity();
  8944. // For some reason, the glLoadIdentity() call doesn't work on
  8945. // my Dell laptop's IBM OpenGL driver, when used in
  8946. // conjunction with glTexGen(), below. But explicitly loading
  8947. // an identity matrix does work. But this buggy-driver
  8948. // workaround might have other performance implications, so I
  8949. // leave it out.
  8950. // GLPf(LoadMatrix)(LMatrix4::ident_mat().get_data());
  8951. }
  8952. }
  8953. report_my_gl_errors();
  8954. }
  8955. #endif // SUPPORT_FIXED_FUNCTION
  8956. #ifdef SUPPORT_FIXED_FUNCTION
  8957. ////////////////////////////////////////////////////////////////////
  8958. // Function: GLGraphicsStateGuardian::do_issue_tex_gen
  8959. // Access: Protected
  8960. // Description:
  8961. ////////////////////////////////////////////////////////////////////
  8962. void CLP(GraphicsStateGuardian)::
  8963. do_issue_tex_gen() {
  8964. bool force_normal = false;
  8965. nassertv(_num_active_texture_stages <= _max_texture_stages);
  8966. // These are passed in for the four OBJECT_PLANE or EYE_PLANE
  8967. // values; they effectively define an identity matrix that maps
  8968. // the spatial coordinates one-for-one to UV's. If you want a
  8969. // mapping other than identity, use a TexMatrixAttrib (or a
  8970. // TexProjectorEffect).
  8971. static const PN_stdfloat s_data[4] = { 1, 0, 0, 0 };
  8972. static const PN_stdfloat t_data[4] = { 0, 1, 0, 0 };
  8973. static const PN_stdfloat r_data[4] = { 0, 0, 1, 0 };
  8974. static const PN_stdfloat q_data[4] = { 0, 0, 0, 1 };
  8975. _tex_gen_modifies_mat = false;
  8976. bool got_point_sprites = false;
  8977. for (int i = 0; i < _num_active_texture_stages; i++) {
  8978. TextureStage *stage = _target_texture->get_on_ff_stage(i);
  8979. _glActiveTexture(GL_TEXTURE0 + i);
  8980. if (_supports_point_sprite) {
  8981. #ifdef OPENGLES
  8982. glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_FALSE);
  8983. #else
  8984. glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_FALSE);
  8985. #endif // OPENGLES
  8986. }
  8987. #ifndef OPENGLES // TexGen not supported by OpenGL ES.
  8988. glDisable(GL_TEXTURE_GEN_S);
  8989. glDisable(GL_TEXTURE_GEN_T);
  8990. glDisable(GL_TEXTURE_GEN_R);
  8991. glDisable(GL_TEXTURE_GEN_Q);
  8992. TexGenAttrib::Mode mode = _target_tex_gen->get_mode(stage);
  8993. switch (mode) {
  8994. case TexGenAttrib::M_off:
  8995. case TexGenAttrib::M_unused2:
  8996. break;
  8997. case TexGenAttrib::M_eye_sphere_map:
  8998. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  8999. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  9000. glEnable(GL_TEXTURE_GEN_S);
  9001. glEnable(GL_TEXTURE_GEN_T);
  9002. force_normal = true;
  9003. break;
  9004. case TexGenAttrib::M_eye_cube_map:
  9005. if (_supports_cube_map) {
  9006. // We need to rotate the normals out of GL's coordinate
  9007. // system and into the user's coordinate system. We do this
  9008. // by composing a transform onto the texture matrix.
  9009. LMatrix4 mat = _inv_cs_transform->get_mat();
  9010. mat.set_row(3, LVecBase3(0.0f, 0.0f, 0.0f));
  9011. glMatrixMode(GL_TEXTURE);
  9012. GLPf(MultMatrix)(mat.get_data());
  9013. // Now we need to reset the texture matrix next time
  9014. // around to undo this.
  9015. _tex_gen_modifies_mat = true;
  9016. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
  9017. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
  9018. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
  9019. glEnable(GL_TEXTURE_GEN_S);
  9020. glEnable(GL_TEXTURE_GEN_T);
  9021. glEnable(GL_TEXTURE_GEN_R);
  9022. force_normal = true;
  9023. }
  9024. break;
  9025. case TexGenAttrib::M_world_cube_map:
  9026. if (_supports_cube_map) {
  9027. // We dynamically transform normals from eye space to world
  9028. // space by applying the appropriate rotation transform to
  9029. // the current texture matrix. Unlike M_world_position, we
  9030. // can't achieve this effect by monkeying with the modelview
  9031. // transform, since the current modelview doesn't affect
  9032. // GL_REFLECTION_MAP.
  9033. CPT(TransformState) camera_transform = _scene_setup->get_camera_transform()->compose(_inv_cs_transform);
  9034. LMatrix4 mat = camera_transform->get_mat();
  9035. mat.set_row(3, LVecBase3(0.0f, 0.0f, 0.0f));
  9036. glMatrixMode(GL_TEXTURE);
  9037. GLPf(MultMatrix)(mat.get_data());
  9038. // Now we need to reset the texture matrix next time
  9039. // around to undo this.
  9040. _tex_gen_modifies_mat = true;
  9041. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
  9042. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
  9043. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP);
  9044. glEnable(GL_TEXTURE_GEN_S);
  9045. glEnable(GL_TEXTURE_GEN_T);
  9046. glEnable(GL_TEXTURE_GEN_R);
  9047. force_normal = true;
  9048. }
  9049. break;
  9050. case TexGenAttrib::M_eye_normal:
  9051. if (_supports_cube_map) {
  9052. // We need to rotate the normals out of GL's coordinate
  9053. // system and into the user's coordinate system. We do this
  9054. // by composing a transform onto the texture matrix.
  9055. LMatrix4 mat = _inv_cs_transform->get_mat();
  9056. mat.set_row(3, LVecBase3(0.0f, 0.0f, 0.0f));
  9057. glMatrixMode(GL_TEXTURE);
  9058. GLPf(MultMatrix)(mat.get_data());
  9059. // Now we need to reset the texture matrix next time
  9060. // around to undo this.
  9061. _tex_gen_modifies_mat = true;
  9062. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
  9063. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
  9064. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
  9065. glEnable(GL_TEXTURE_GEN_S);
  9066. glEnable(GL_TEXTURE_GEN_T);
  9067. glEnable(GL_TEXTURE_GEN_R);
  9068. force_normal = true;
  9069. }
  9070. break;
  9071. case TexGenAttrib::M_world_normal:
  9072. if (_supports_cube_map) {
  9073. // We dynamically transform normals from eye space to world
  9074. // space by applying the appropriate rotation transform to
  9075. // the current texture matrix. Unlike M_world_position, we
  9076. // can't achieve this effect by monkeying with the modelview
  9077. // transform, since the current modelview doesn't affect
  9078. // GL_NORMAL_MAP.
  9079. CPT(TransformState) camera_transform = _scene_setup->get_camera_transform()->compose(_inv_cs_transform);
  9080. LMatrix4 mat = camera_transform->get_mat();
  9081. mat.set_row(3, LVecBase3(0.0f, 0.0f, 0.0f));
  9082. glMatrixMode(GL_TEXTURE);
  9083. GLPf(MultMatrix)(mat.get_data());
  9084. // Now we need to reset the texture matrix next time
  9085. // around to undo this.
  9086. _tex_gen_modifies_mat = true;
  9087. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
  9088. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
  9089. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
  9090. glEnable(GL_TEXTURE_GEN_S);
  9091. glEnable(GL_TEXTURE_GEN_T);
  9092. glEnable(GL_TEXTURE_GEN_R);
  9093. force_normal = true;
  9094. }
  9095. break;
  9096. case TexGenAttrib::M_eye_position:
  9097. // To represent eye position correctly, we need to temporarily
  9098. // load the coordinate-system transform.
  9099. glMatrixMode(GL_MODELVIEW);
  9100. glPushMatrix();
  9101. GLPf(LoadMatrix)(_cs_transform->get_mat().get_data());
  9102. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9103. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9104. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9105. glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9106. GLPfv(TexGen)(GL_S, GL_EYE_PLANE, s_data);
  9107. GLPfv(TexGen)(GL_T, GL_EYE_PLANE, t_data);
  9108. GLPfv(TexGen)(GL_R, GL_EYE_PLANE, r_data);
  9109. GLPfv(TexGen)(GL_Q, GL_EYE_PLANE, q_data);
  9110. glEnable(GL_TEXTURE_GEN_S);
  9111. glEnable(GL_TEXTURE_GEN_T);
  9112. glEnable(GL_TEXTURE_GEN_R);
  9113. glEnable(GL_TEXTURE_GEN_Q);
  9114. glMatrixMode(GL_MODELVIEW);
  9115. glPopMatrix();
  9116. break;
  9117. case TexGenAttrib::M_world_position:
  9118. // We achieve world position coordinates by using the eye
  9119. // position mode, and loading the transform of the root
  9120. // node--thus putting the "eye" at the root.
  9121. {
  9122. glMatrixMode(GL_MODELVIEW);
  9123. glPushMatrix();
  9124. CPT(TransformState) root_transform = _cs_transform->compose(_scene_setup->get_world_transform());
  9125. GLPf(LoadMatrix)(root_transform->get_mat().get_data());
  9126. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9127. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9128. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9129. glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  9130. GLPfv(TexGen)(GL_S, GL_EYE_PLANE, s_data);
  9131. GLPfv(TexGen)(GL_T, GL_EYE_PLANE, t_data);
  9132. GLPfv(TexGen)(GL_R, GL_EYE_PLANE, r_data);
  9133. GLPfv(TexGen)(GL_Q, GL_EYE_PLANE, q_data);
  9134. glEnable(GL_TEXTURE_GEN_S);
  9135. glEnable(GL_TEXTURE_GEN_T);
  9136. glEnable(GL_TEXTURE_GEN_R);
  9137. glEnable(GL_TEXTURE_GEN_Q);
  9138. glMatrixMode(GL_MODELVIEW);
  9139. glPopMatrix();
  9140. }
  9141. break;
  9142. case TexGenAttrib::M_point_sprite:
  9143. if (_supports_point_sprite) {
  9144. #ifdef OPENGLES
  9145. glTexEnvi(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
  9146. #else
  9147. glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
  9148. #endif
  9149. got_point_sprites = true;
  9150. }
  9151. break;
  9152. case TexGenAttrib::M_constant:
  9153. // To generate a constant UV(w) coordinate everywhere, we use
  9154. // EYE_LINEAR mode, but we construct a special matrix that
  9155. // flattens the vertex position to zero and then adds our
  9156. // desired value.
  9157. {
  9158. const LTexCoord3 &v = _target_tex_gen->get_constant_value(stage);
  9159. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  9160. glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  9161. glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  9162. glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  9163. LVecBase4 s(0.0f, 0.0f, 0.0f, v[0]);
  9164. LVecBase4 t(0.0f, 0.0f, 0.0f, v[1]);
  9165. LVecBase4 r(0.0f, 0.0f, 0.0f, v[2]);
  9166. GLPfv(TexGen)(GL_S, GL_OBJECT_PLANE, s.get_data());
  9167. GLPfv(TexGen)(GL_T, GL_OBJECT_PLANE, t.get_data());
  9168. GLPfv(TexGen)(GL_R, GL_OBJECT_PLANE, r.get_data());
  9169. GLPfv(TexGen)(GL_Q, GL_OBJECT_PLANE, q_data);
  9170. glEnable(GL_TEXTURE_GEN_S);
  9171. glEnable(GL_TEXTURE_GEN_T);
  9172. glEnable(GL_TEXTURE_GEN_R);
  9173. glEnable(GL_TEXTURE_GEN_Q);
  9174. }
  9175. break;
  9176. case TexGenAttrib::M_unused:
  9177. break;
  9178. }
  9179. #endif // OPENGLES
  9180. }
  9181. if (got_point_sprites != _tex_gen_point_sprite) {
  9182. _tex_gen_point_sprite = got_point_sprites;
  9183. #ifdef OPENGLES
  9184. if (_tex_gen_point_sprite) {
  9185. glEnable(GL_POINT_SPRITE_OES);
  9186. } else {
  9187. glDisable(GL_POINT_SPRITE_OES);
  9188. }
  9189. #else
  9190. if (_tex_gen_point_sprite) {
  9191. glEnable(GL_POINT_SPRITE_ARB);
  9192. } else {
  9193. glDisable(GL_POINT_SPRITE_ARB);
  9194. }
  9195. #endif // OPENGLES
  9196. }
  9197. report_my_gl_errors();
  9198. }
  9199. #endif // SUPPORT_FIXED_FUNCTION
  9200. ////////////////////////////////////////////////////////////////////
  9201. // Function: GLGraphicsStateGuardian::specify_texture
  9202. // Access: Protected
  9203. // Description: Specifies the texture parameters. Returns true if
  9204. // the texture may need to be reloaded. Pass non-NULL
  9205. // sampler argument to use different sampler settings.
  9206. ////////////////////////////////////////////////////////////////////
  9207. bool CLP(GraphicsStateGuardian)::
  9208. specify_texture(CLP(TextureContext) *gtc, const SamplerState &sampler) {
  9209. nassertr(gtc->_handle == 0 /* can't modify tex with active handle */, false);
  9210. Texture *tex = gtc->get_texture();
  9211. GLenum target = get_texture_target(tex->get_texture_type());
  9212. if (target == GL_NONE) {
  9213. // Unsupported target (e.g. 3-d texturing on GL 1.1).
  9214. return false;
  9215. }
  9216. #ifndef OPENGLES
  9217. if (target == GL_TEXTURE_BUFFER) {
  9218. // Buffer textures may not receive texture parameters.
  9219. return false;
  9220. }
  9221. #endif // OPENGLES
  9222. // Record the active sampler settings.
  9223. gtc->_active_sampler = sampler;
  9224. glTexParameteri(target, GL_TEXTURE_WRAP_S,
  9225. get_texture_wrap_mode(sampler.get_wrap_u()));
  9226. #ifndef OPENGLES
  9227. if (target != GL_TEXTURE_1D) {
  9228. glTexParameteri(target, GL_TEXTURE_WRAP_T,
  9229. get_texture_wrap_mode(sampler.get_wrap_v()));
  9230. }
  9231. #endif
  9232. #ifdef OPENGLES_2
  9233. if (target == GL_TEXTURE_3D_OES) {
  9234. glTexParameteri(target, GL_TEXTURE_WRAP_R_OES,
  9235. get_texture_wrap_mode(sampler.get_wrap_w()));
  9236. }
  9237. #endif
  9238. #ifndef OPENGLES
  9239. if (target == GL_TEXTURE_3D) {
  9240. glTexParameteri(target, GL_TEXTURE_WRAP_R,
  9241. get_texture_wrap_mode(sampler.get_wrap_w()));
  9242. }
  9243. LColor border_color = sampler.get_border_color();
  9244. call_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border_color);
  9245. #endif // OPENGLES
  9246. SamplerState::FilterType minfilter = sampler.get_effective_minfilter();
  9247. SamplerState::FilterType magfilter = sampler.get_effective_magfilter();
  9248. bool uses_mipmaps = SamplerState::is_mipmap(minfilter) && !gl_ignore_mipmaps;
  9249. #ifndef NDEBUG
  9250. if (gl_force_mipmaps) {
  9251. minfilter = SamplerState::FT_linear_mipmap_linear;
  9252. magfilter = SamplerState::FT_linear;
  9253. uses_mipmaps = true;
  9254. }
  9255. #endif
  9256. if (!tex->might_have_ram_image()) {
  9257. // If it's a dynamically generated texture (that is, the RAM image
  9258. // isn't available so it didn't pass through the CPU), we should
  9259. // enable GL-generated mipmaps if we can.
  9260. if (!_supports_generate_mipmap) {
  9261. // However, if the GPU doesn't support mipmap generation, we
  9262. // have to turn it off.
  9263. uses_mipmaps = false;
  9264. }
  9265. }
  9266. glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
  9267. get_texture_filter_type(minfilter, !uses_mipmaps));
  9268. glTexParameteri(target, GL_TEXTURE_MAG_FILTER,
  9269. get_texture_filter_type(magfilter, true));
  9270. // Set anisotropic filtering.
  9271. if (_supports_anisotropy) {
  9272. PN_stdfloat anisotropy = sampler.get_effective_anisotropic_degree();
  9273. anisotropy = min(anisotropy, _max_anisotropy);
  9274. anisotropy = max(anisotropy, (PN_stdfloat)1.0);
  9275. glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
  9276. }
  9277. #ifndef OPENGLES_1
  9278. if (tex->get_format() == Texture::F_depth_stencil ||
  9279. tex->get_format() == Texture::F_depth_component ||
  9280. tex->get_format() == Texture::F_depth_component16 ||
  9281. tex->get_format() == Texture::F_depth_component24 ||
  9282. tex->get_format() == Texture::F_depth_component32) {
  9283. #ifdef SUPPORT_FIXED_FUNCTION
  9284. glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
  9285. #endif
  9286. if (_supports_shadow_filter) {
  9287. if ((sampler.get_magfilter() == SamplerState::FT_shadow) ||
  9288. (sampler.get_minfilter() == SamplerState::FT_shadow)) {
  9289. glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
  9290. glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
  9291. } else {
  9292. glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
  9293. glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
  9294. }
  9295. }
  9296. }
  9297. #endif
  9298. #ifndef OPENGLES
  9299. glTexParameterf(target, GL_TEXTURE_MIN_LOD, sampler.get_min_lod());
  9300. glTexParameterf(target, GL_TEXTURE_MAX_LOD, sampler.get_max_lod());
  9301. glTexParameterf(target, GL_TEXTURE_LOD_BIAS, sampler.get_lod_bias());
  9302. #endif
  9303. report_my_gl_errors();
  9304. if (uses_mipmaps && !gtc->_uses_mipmaps) {
  9305. // Suddenly we require mipmaps. This means the texture may need
  9306. // reloading.
  9307. return true;
  9308. }
  9309. return false;
  9310. }
  9311. ////////////////////////////////////////////////////////////////////
  9312. // Function: GLGraphicsStateGuardian::apply_texture
  9313. // Access: Protected
  9314. // Description: Updates OpenGL with the current information for this
  9315. // texture, and makes it the current texture available
  9316. // for rendering.
  9317. ////////////////////////////////////////////////////////////////////
  9318. bool CLP(GraphicsStateGuardian)::
  9319. apply_texture(TextureContext *tc) {
  9320. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  9321. gtc->set_active(true);
  9322. GLenum target = get_texture_target(gtc->get_texture()->get_texture_type());
  9323. if (target == GL_NONE) {
  9324. return false;
  9325. }
  9326. if (gtc->_target != target) {
  9327. // The target has changed. That means we have to re-bind a new
  9328. // texture object.
  9329. gtc->reset_data();
  9330. gtc->_target = target;
  9331. }
  9332. glBindTexture(target, gtc->_index);
  9333. report_my_gl_errors();
  9334. return true;
  9335. }
  9336. ////////////////////////////////////////////////////////////////////
  9337. // Function: GLGraphicsStateGuardian::apply_sampler
  9338. // Access: Protected
  9339. // Description: Updates OpenGL with the current information for this
  9340. // sampler, and makes it the current sampler available
  9341. // for rendering. Use NULL to unbind the sampler.
  9342. //
  9343. // If the GSG doesn't support sampler objects, the
  9344. // sampler settings are applied to the given texture
  9345. // context instead.
  9346. ////////////////////////////////////////////////////////////////////
  9347. bool CLP(GraphicsStateGuardian)::
  9348. apply_sampler(GLuint unit, const SamplerState &sampler, TextureContext *tc) {
  9349. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  9350. #ifndef OPENGLES
  9351. if (_supports_sampler_objects) {
  9352. // We support sampler objects. Prepare the sampler object and
  9353. // bind it to the indicated texture unit.
  9354. SamplerContext *sc = sampler.prepare_now(get_prepared_objects(), this);
  9355. nassertr(sc != (SamplerContext *)NULL, false);
  9356. CLP(SamplerContext) *gsc = DCAST(CLP(SamplerContext), sc);
  9357. gsc->enqueue_lru(&_prepared_objects->_sampler_object_lru);
  9358. _glBindSampler(unit, gsc->_index);
  9359. if (GLCAT.is_spam()) {
  9360. GLCAT.spam()
  9361. << "bind " << unit << " " << sampler << "\n";
  9362. }
  9363. } else
  9364. #endif // OPENGLES
  9365. {
  9366. // We don't support sampler objects. We'll have to bind the
  9367. // texture and change the texture parameters if they don't match.
  9368. if (gtc->_active_sampler != sampler) {
  9369. _glActiveTexture(GL_TEXTURE0 + unit);
  9370. apply_texture(tc);
  9371. specify_texture(gtc, sampler);
  9372. }
  9373. }
  9374. if (sampler.uses_mipmaps() && !gtc->_uses_mipmaps) {
  9375. // The texture wasn't created with mipmaps, but we are trying
  9376. // to sample it with mipmaps. We will need to reload it.
  9377. apply_texture(tc);
  9378. gtc->mark_needs_reload();
  9379. bool okflag = upload_texture(gtc, false, true);
  9380. if (!okflag) {
  9381. GLCAT.error()
  9382. << "Could not load " << *gtc->get_texture() << "\n";
  9383. return false;
  9384. }
  9385. }
  9386. report_my_gl_errors();
  9387. return true;
  9388. }
  9389. ////////////////////////////////////////////////////////////////////
  9390. // Function: GLGraphicsStateGuardian::upload_texture
  9391. // Access: Protected
  9392. // Description: Uploads the entire texture image to OpenGL, including
  9393. // all pages.
  9394. //
  9395. // The return value is true if successful, or false if
  9396. // the texture has no image.
  9397. ////////////////////////////////////////////////////////////////////
  9398. bool CLP(GraphicsStateGuardian)::
  9399. upload_texture(CLP(TextureContext) *gtc, bool force, bool uses_mipmaps) {
  9400. PStatGPUTimer timer(this, _load_texture_pcollector);
  9401. Texture *tex = gtc->get_texture();
  9402. if (_effective_incomplete_render && !force) {
  9403. bool has_image = _supports_compressed_texture ? tex->has_ram_image() : tex->has_uncompressed_ram_image();
  9404. if (!has_image && tex->might_have_ram_image() &&
  9405. tex->has_simple_ram_image() &&
  9406. !_loader.is_null()) {
  9407. // If we don't have the texture data right now, go get it, but in
  9408. // the meantime load a temporary simple image in its place.
  9409. async_reload_texture(gtc);
  9410. has_image = _supports_compressed_texture ? tex->has_ram_image() : tex->has_uncompressed_ram_image();
  9411. if (!has_image) {
  9412. if (gtc->was_simple_image_modified()) {
  9413. return upload_simple_texture(gtc);
  9414. }
  9415. return true;
  9416. }
  9417. }
  9418. }
  9419. CPTA_uchar image;
  9420. if (_supports_compressed_texture) {
  9421. image = tex->get_ram_image();
  9422. } else {
  9423. image = tex->get_uncompressed_ram_image();
  9424. }
  9425. Texture::CompressionMode image_compression;
  9426. if (image.is_null()) {
  9427. image_compression = Texture::CM_off;
  9428. } else {
  9429. image_compression = tex->get_ram_image_compression();
  9430. }
  9431. if (!get_supports_compressed_texture_format(image_compression)) {
  9432. image = tex->get_uncompressed_ram_image();
  9433. image_compression = Texture::CM_off;
  9434. }
  9435. int mipmap_bias = 0;
  9436. int width = tex->get_x_size();
  9437. int height = tex->get_y_size();
  9438. int depth = tex->get_z_size();
  9439. // If we'll use immutable texture storage, we have to pick a sized
  9440. // image format.
  9441. bool force_sized = (gl_immutable_texture_storage && _supports_tex_storage) ||
  9442. (tex->get_texture_type() == Texture::TT_buffer_texture);
  9443. GLint internal_format = get_internal_image_format(tex, force_sized);
  9444. GLint external_format = get_external_image_format(tex);
  9445. GLenum component_type = get_component_type(tex->get_component_type());
  9446. if (GLCAT.is_debug()) {
  9447. if (image.is_null()) {
  9448. GLCAT.debug()
  9449. << "loading texture with NULL image";
  9450. } else if (image_compression != Texture::CM_off) {
  9451. GLCAT.debug()
  9452. << "loading pre-compressed texture";
  9453. } else if (is_compressed_format(internal_format)) {
  9454. GLCAT.debug()
  9455. << "loading compressed texture";
  9456. } else {
  9457. GLCAT.debug()
  9458. << "loading uncompressed texture";
  9459. }
  9460. GLCAT.debug(false) << " " << tex->get_name() << "\n";
  9461. }
  9462. // Ensure that the texture fits within the GL's specified limits.
  9463. // Need to split dimensions because of texture arrays
  9464. int max_dimension_x;
  9465. int max_dimension_y;
  9466. int max_dimension_z;
  9467. switch (tex->get_texture_type()) {
  9468. case Texture::TT_3d_texture:
  9469. max_dimension_x = _max_3d_texture_dimension;
  9470. max_dimension_y = _max_3d_texture_dimension;
  9471. max_dimension_z = _max_3d_texture_dimension;
  9472. break;
  9473. case Texture::TT_cube_map:
  9474. max_dimension_x = _max_cube_map_dimension;
  9475. max_dimension_y = _max_cube_map_dimension;
  9476. max_dimension_z = 6;
  9477. break;
  9478. case Texture::TT_2d_texture_array:
  9479. max_dimension_x = _max_texture_dimension;
  9480. max_dimension_y = _max_texture_dimension;
  9481. max_dimension_z = _max_2d_texture_array_layers;
  9482. break;
  9483. case Texture::TT_buffer_texture:
  9484. max_dimension_x = _max_buffer_texture_size;
  9485. max_dimension_y = 1;
  9486. max_dimension_z = 1;
  9487. break;
  9488. default:
  9489. max_dimension_x = _max_texture_dimension;
  9490. max_dimension_y = _max_texture_dimension;
  9491. max_dimension_z = 1;
  9492. }
  9493. if (max_dimension_x == 0 || max_dimension_y == 0 || max_dimension_z == 0) {
  9494. // Guess this GL doesn't support cube mapping/3d textures/2d texture arrays.
  9495. report_my_gl_errors();
  9496. return false;
  9497. }
  9498. // If it doesn't fit, we have to reduce it on-the-fly. We do this
  9499. // by incrementing the mipmap_bias, so we're effectively loading a
  9500. // lower mipmap level. This requires generating the mipmaps on
  9501. // the CPU if they haven't already been generated. It would have
  9502. // been better if the user had specified max-texture-dimension to
  9503. // reduce the texture at load time instead; of course, the user
  9504. // doesn't always know ahead of time what the hardware limits are.
  9505. if ((max_dimension_x > 0 && max_dimension_y > 0 && max_dimension_z > 0) &&
  9506. image_compression == Texture::CM_off) {
  9507. while (tex->get_expected_mipmap_x_size(mipmap_bias) > max_dimension_x ||
  9508. tex->get_expected_mipmap_y_size(mipmap_bias) > max_dimension_y ||
  9509. tex->get_expected_mipmap_z_size(mipmap_bias) > max_dimension_z) {
  9510. ++mipmap_bias;
  9511. }
  9512. if (mipmap_bias >= tex->get_num_ram_mipmap_images()) {
  9513. // We need to generate some more mipmap images.
  9514. if (tex->has_ram_image()) {
  9515. tex->generate_ram_mipmap_images();
  9516. if (mipmap_bias >= tex->get_num_ram_mipmap_images()) {
  9517. // It didn't work. Send the smallest we've got, and hope
  9518. // for the best.
  9519. mipmap_bias = tex->get_num_ram_mipmap_images() - 1;
  9520. }
  9521. }
  9522. }
  9523. width = tex->get_expected_mipmap_x_size(mipmap_bias);
  9524. height = tex->get_expected_mipmap_y_size(mipmap_bias);
  9525. depth = tex->get_expected_mipmap_z_size(mipmap_bias);
  9526. if (mipmap_bias != 0) {
  9527. GLCAT.info()
  9528. << "Reducing image " << tex->get_name()
  9529. << " from " << tex->get_x_size() << " x " << tex->get_y_size()
  9530. << " x " << tex->get_z_size() << " to "
  9531. << width << " x " << height << " x " << depth << "\n";
  9532. }
  9533. }
  9534. if (image_compression != Texture::CM_off) {
  9535. Texture::QualityLevel quality_level = tex->get_effective_quality_level();
  9536. #ifndef OPENGLES
  9537. switch (quality_level) {
  9538. case Texture::QL_fastest:
  9539. glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST);
  9540. break;
  9541. case Texture::QL_default:
  9542. case Texture::QL_normal:
  9543. glHint(GL_TEXTURE_COMPRESSION_HINT, GL_DONT_CARE);
  9544. break;
  9545. case Texture::QL_best:
  9546. glHint(GL_TEXTURE_COMPRESSION_HINT, GL_NICEST);
  9547. break;
  9548. }
  9549. #endif
  9550. }
  9551. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  9552. GLenum target = get_texture_target(tex->get_texture_type());
  9553. uses_mipmaps = (uses_mipmaps && !gl_ignore_mipmaps) || gl_force_mipmaps;
  9554. #ifndef OPENGLES
  9555. if (target == GL_TEXTURE_BUFFER) {
  9556. // Buffer textures may not have mipmaps.
  9557. uses_mipmaps = false;
  9558. }
  9559. #endif // OPENGLES
  9560. bool needs_reload = false;
  9561. if (!gtc->_has_storage ||
  9562. gtc->_uses_mipmaps != uses_mipmaps ||
  9563. gtc->_internal_format != internal_format ||
  9564. gtc->_width != width ||
  9565. gtc->_height != height ||
  9566. gtc->_depth != depth) {
  9567. // We need to reload a new GL Texture object.
  9568. needs_reload = true;
  9569. if (_use_object_labels) {
  9570. // This seems like a good time to assign a label for the debug messages.
  9571. const string &name = tex->get_name();
  9572. _glObjectLabel(GL_TEXTURE, gtc->_index, name.size(), name.data());
  9573. }
  9574. }
  9575. if (needs_reload && gtc->_immutable) {
  9576. GLCAT.warning() << "Attempt to modify texture with immutable storage, recreating texture.\n";
  9577. gtc->reset_data();
  9578. glBindTexture(target, gtc->_index);
  9579. }
  9580. #ifndef OPENGLES
  9581. if (target == GL_TEXTURE_BUFFER) {
  9582. // Buffer textures don't support mipmappping.
  9583. gtc->_generate_mipmaps = false;
  9584. if (gtc->_buffer == 0) {
  9585. // The buffer object wasn't created yet.
  9586. _glGenBuffers(1, &gtc->_buffer);
  9587. _glBindBuffer(GL_TEXTURE_BUFFER, gtc->_buffer);
  9588. _glTexBuffer(GL_TEXTURE_BUFFER, internal_format, gtc->_buffer);
  9589. needs_reload = true;
  9590. } else {
  9591. _glBindBuffer(GL_TEXTURE_BUFFER, gtc->_buffer);
  9592. if (gtc->_internal_format != internal_format) {
  9593. _glTexBuffer(GL_TEXTURE_BUFFER, internal_format, gtc->_buffer);
  9594. }
  9595. }
  9596. } else
  9597. #endif // !OPENGLES
  9598. if (needs_reload) {
  9599. // Figure out whether mipmaps will be generated by the GPU or by
  9600. // Panda (or not at all), and how many mipmap levels should be created.
  9601. gtc->_generate_mipmaps = false;
  9602. int num_levels = 1;
  9603. CPTA_uchar image = tex->get_ram_mipmap_image(mipmap_bias);
  9604. if (image.is_null()) {
  9605. // We don't even have a RAM image, so we have no choice but to let
  9606. // mipmaps be generated on the GPU.
  9607. if (uses_mipmaps) {
  9608. if (_supports_generate_mipmap) {
  9609. num_levels = tex->get_expected_num_mipmap_levels() - mipmap_bias;
  9610. gtc->_generate_mipmaps = true;
  9611. } else {
  9612. // If it can't, do without mipmaps.
  9613. num_levels = 1;
  9614. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  9615. }
  9616. }
  9617. } else {
  9618. if (uses_mipmaps) {
  9619. num_levels = tex->get_num_ram_mipmap_images() - mipmap_bias;
  9620. if (num_levels <= 1) {
  9621. // No RAM mipmap levels available. Should we generate some?
  9622. if (!_supports_generate_mipmap || !driver_generate_mipmaps ||
  9623. image_compression != Texture::CM_off) {
  9624. // Yes, the GL can't or won't generate them, so we need to.
  9625. // Note that some drivers (nVidia) will *corrupt memory* if
  9626. // you ask them to generate mipmaps for a pre-compressed
  9627. // texture.
  9628. tex->generate_ram_mipmap_images();
  9629. num_levels = tex->get_num_ram_mipmap_images() - mipmap_bias;
  9630. }
  9631. }
  9632. if (num_levels <= 1) {
  9633. // We don't have mipmap levels in RAM. Ask the GL to generate
  9634. // them if it can.
  9635. if (_supports_generate_mipmap) {
  9636. num_levels = tex->get_expected_num_mipmap_levels() - mipmap_bias;
  9637. gtc->_generate_mipmaps = true;
  9638. } else {
  9639. // If it can't, do without mipmaps.
  9640. glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  9641. num_levels = 1;
  9642. }
  9643. }
  9644. }
  9645. }
  9646. #ifndef OPENGLES // OpenGL ES doesn't have GL_TEXTURE_MAX_LEVEL.
  9647. if (is_at_least_gl_version(1, 2)) {
  9648. // By the time we get here, we have a pretty good prediction for
  9649. // the number of mipmaps we're going to have, so tell the GL that's
  9650. // all it's going to get.
  9651. glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, num_levels - 1);
  9652. }
  9653. #endif
  9654. #ifndef OPENGLES_2
  9655. if (gtc->_generate_mipmaps && _glGenerateMipmap == NULL) {
  9656. // The old, deprecated way to generate mipmaps.
  9657. glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
  9658. }
  9659. #endif
  9660. #if !defined(SUPPORT_FIXED_FUNCTION) && !defined(OPENGLES_2)
  9661. // Do we need to apply a swizzle mask to emulate these deprecated
  9662. // texture formats?
  9663. switch (tex->get_format()) {
  9664. case Texture::F_alpha:
  9665. glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
  9666. glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
  9667. glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
  9668. glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, GL_RED);
  9669. break;
  9670. case Texture::F_luminance:
  9671. glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  9672. glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  9673. glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  9674. glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, GL_ONE);
  9675. break;
  9676. case Texture::F_luminance_alpha:
  9677. glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_RED);
  9678. glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, GL_RED);
  9679. glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_RED);
  9680. glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, GL_GREEN);
  9681. break;
  9682. default:
  9683. break;
  9684. }
  9685. #endif
  9686. // Allocate immutable storage for the texture, after which we can subload it.
  9687. // Pre-allocating storage using glTexStorage is more efficient than using glTexImage
  9688. // to load all of the individual images one by one later, but we are not allowed to
  9689. // change the texture size or number of mipmap levels after this point.
  9690. if (gl_immutable_texture_storage && _supports_tex_storage && !gtc->_has_storage) {
  9691. if (GLCAT.is_debug()) {
  9692. GLCAT.debug()
  9693. << "allocating storage for texture " << tex->get_name() << ", " << width
  9694. << " x " << height << " x " << depth << ", mipmaps " << num_levels
  9695. << ", uses_mipmaps = " << uses_mipmaps << "\n";
  9696. }
  9697. switch (tex->get_texture_type()) {
  9698. case Texture::TT_buffer_texture:
  9699. // Won't get here, but squelch compiler warning
  9700. case Texture::TT_1d_texture:
  9701. _glTexStorage1D(target, num_levels, internal_format, width);
  9702. break;
  9703. case Texture::TT_2d_texture:
  9704. case Texture::TT_cube_map:
  9705. _glTexStorage2D(target, num_levels, internal_format, width, height);
  9706. break;
  9707. case Texture::TT_3d_texture:
  9708. case Texture::TT_2d_texture_array:
  9709. _glTexStorage3D(target, num_levels, internal_format, width, height, depth);
  9710. break;
  9711. }
  9712. gtc->_has_storage = true;
  9713. gtc->_immutable = true;
  9714. gtc->_uses_mipmaps = uses_mipmaps;
  9715. gtc->_internal_format = internal_format;
  9716. gtc->_width = width;
  9717. gtc->_height = height;
  9718. gtc->_depth = depth;
  9719. needs_reload = false;
  9720. }
  9721. } else {
  9722. // Maybe we need to generate mipmaps on the CPU.
  9723. if (!image.is_null() && uses_mipmaps) {
  9724. if (tex->get_num_ram_mipmap_images() - mipmap_bias <= 1) {
  9725. // No RAM mipmap levels available. Should we generate some?
  9726. if (!_supports_generate_mipmap || !driver_generate_mipmaps ||
  9727. image_compression != Texture::CM_off) {
  9728. // Yes, the GL can't or won't generate them, so we need to.
  9729. // Note that some drivers (nVidia) will *corrupt memory* if
  9730. // you ask them to generate mipmaps for a pre-compressed
  9731. // texture.
  9732. tex->generate_ram_mipmap_images();
  9733. }
  9734. }
  9735. }
  9736. }
  9737. bool success = true;
  9738. if (tex->get_texture_type() == Texture::TT_cube_map) {
  9739. // A cube map must load six different 2-d images (which are stored
  9740. // as the six pages of the system ram image).
  9741. if (!_supports_cube_map) {
  9742. report_my_gl_errors();
  9743. return false;
  9744. }
  9745. nassertr(target == GL_TEXTURE_CUBE_MAP, false);
  9746. success = success && upload_texture_image
  9747. (gtc, needs_reload, uses_mipmaps, mipmap_bias,
  9748. GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_POSITIVE_X,
  9749. internal_format, external_format, component_type,
  9750. true, 0, image_compression);
  9751. success = success && upload_texture_image
  9752. (gtc, needs_reload, uses_mipmaps, mipmap_bias,
  9753. GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
  9754. internal_format, external_format, component_type,
  9755. true, 1, image_compression);
  9756. success = success && upload_texture_image
  9757. (gtc, needs_reload, uses_mipmaps, mipmap_bias,
  9758. GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
  9759. internal_format, external_format, component_type,
  9760. true, 2, image_compression);
  9761. success = success && upload_texture_image
  9762. (gtc, needs_reload, uses_mipmaps, mipmap_bias,
  9763. GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
  9764. internal_format, external_format, component_type,
  9765. true, 3, image_compression);
  9766. success = success && upload_texture_image
  9767. (gtc, needs_reload, uses_mipmaps, mipmap_bias,
  9768. GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
  9769. internal_format, external_format, component_type,
  9770. true, 4, image_compression);
  9771. success = success && upload_texture_image
  9772. (gtc, needs_reload, uses_mipmaps, mipmap_bias,
  9773. GL_TEXTURE_CUBE_MAP, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
  9774. internal_format, external_format, component_type,
  9775. true, 5, image_compression);
  9776. } else {
  9777. // Any other kind of texture can be loaded all at once.
  9778. success = upload_texture_image
  9779. (gtc, needs_reload, uses_mipmaps, mipmap_bias, target,
  9780. target, internal_format, external_format,
  9781. component_type, false, 0, image_compression);
  9782. }
  9783. if (gtc->_generate_mipmaps && _glGenerateMipmap != NULL &&
  9784. !image.is_null()) {
  9785. // We uploaded an image; we may need to generate mipmaps.
  9786. if (GLCAT.is_debug()) {
  9787. GLCAT.debug()
  9788. << "generating mipmaps for texture " << tex->get_name() << ", "
  9789. << width << " x " << height << " x " << depth
  9790. << ", uses_mipmaps = " << uses_mipmaps << "\n";
  9791. }
  9792. _glGenerateMipmap(target);
  9793. }
  9794. maybe_gl_finish();
  9795. if (success) {
  9796. if (needs_reload) {
  9797. gtc->_has_storage = true;
  9798. gtc->_uses_mipmaps = uses_mipmaps;
  9799. gtc->_internal_format = internal_format;
  9800. gtc->_width = width;
  9801. gtc->_height = height;
  9802. gtc->_depth = depth;
  9803. gtc->update_data_size_bytes(get_texture_memory_size(tex));
  9804. }
  9805. if (tex->get_post_load_store_cache()) {
  9806. tex->set_post_load_store_cache(false);
  9807. // OK, get the RAM image, and save it in a BamCache record.
  9808. if (do_extract_texture_data(gtc)) {
  9809. if (tex->has_ram_image()) {
  9810. BamCache *cache = BamCache::get_global_ptr();
  9811. PT(BamCacheRecord) record = cache->lookup(tex->get_fullpath(), "txo");
  9812. if (record != (BamCacheRecord *)NULL) {
  9813. record->set_data(tex, tex);
  9814. cache->store(record);
  9815. }
  9816. }
  9817. }
  9818. }
  9819. GraphicsEngine *engine = get_engine();
  9820. nassertr(engine != (GraphicsEngine *)NULL, false);
  9821. engine->texture_uploaded(tex);
  9822. gtc->mark_loaded();
  9823. report_my_gl_errors();
  9824. return true;
  9825. }
  9826. report_my_gl_errors();
  9827. return false;
  9828. }
  9829. ////////////////////////////////////////////////////////////////////
  9830. // Function: GLGraphicsStateGuardian::upload_texture_image
  9831. // Access: Protected
  9832. // Description: Loads a texture image, or one page of a cube map
  9833. // image, from system RAM to texture memory.
  9834. //
  9835. // texture_target is normally the same thing as
  9836. // page_target; both represent the GL target onto which
  9837. // the texture image is loaded, e.g. GL_TEXTURE_1D,
  9838. // GL_TEXTURE_2D, etc. The only time they may differ is
  9839. // in the case of cube mapping, in which case
  9840. // texture_target will be target for the overall
  9841. // texture, e.g. GL_TEXTURE_CUBE_MAP, and page_target
  9842. // will be the target for this particular page,
  9843. // e.g. GL_TEXTURE_CUBE_MAP_POSITIVE_X.
  9844. ////////////////////////////////////////////////////////////////////
  9845. bool CLP(GraphicsStateGuardian)::
  9846. upload_texture_image(CLP(TextureContext) *gtc, bool needs_reload,
  9847. bool uses_mipmaps, int mipmap_bias,
  9848. GLenum texture_target, GLenum page_target,
  9849. GLint internal_format,
  9850. GLint external_format, GLenum component_type,
  9851. bool one_page_only, int z,
  9852. Texture::CompressionMode image_compression) {
  9853. // Make sure the error stack is cleared out before we begin.
  9854. clear_my_gl_errors();
  9855. if (texture_target == GL_NONE) {
  9856. // Unsupported target (e.g. 3-d texturing on GL 1.1).
  9857. return false;
  9858. }
  9859. if (image_compression != Texture::CM_off && !_supports_compressed_texture) {
  9860. return false;
  9861. }
  9862. Texture *tex = gtc->get_texture();
  9863. nassertr(tex != (Texture *)NULL, false);
  9864. CPTA_uchar image = tex->get_ram_mipmap_image(mipmap_bias);
  9865. int width = tex->get_expected_mipmap_x_size(mipmap_bias);
  9866. int height = tex->get_expected_mipmap_y_size(mipmap_bias);
  9867. int depth = tex->get_expected_mipmap_z_size(mipmap_bias);
  9868. // Determine the number of images to upload.
  9869. int num_levels = 1;
  9870. if (uses_mipmaps) {
  9871. num_levels = tex->get_expected_num_mipmap_levels();
  9872. }
  9873. int num_ram_mipmap_levels = 0;
  9874. if (!image.is_null()) {
  9875. if (uses_mipmaps) {
  9876. num_ram_mipmap_levels = tex->get_num_ram_mipmap_images();
  9877. } else {
  9878. num_ram_mipmap_levels = 1;
  9879. }
  9880. }
  9881. #ifndef OPENGLES
  9882. if (needs_reload || num_ram_mipmap_levels > 0) {
  9883. // Make sure that any incoherent writes to this texture have been synced.
  9884. if (gtc->needs_barrier(GL_TEXTURE_UPDATE_BARRIER_BIT)) {
  9885. issue_memory_barrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
  9886. }
  9887. }
  9888. #endif
  9889. if (!needs_reload) {
  9890. // Try to subload the image over the existing GL Texture object,
  9891. // possibly saving on texture memory fragmentation.
  9892. if (GLCAT.is_debug()) {
  9893. if (num_ram_mipmap_levels == 0) {
  9894. if (tex->has_clear_color()) {
  9895. GLCAT.debug()
  9896. << "clearing texture " << tex->get_name() << ", "
  9897. << width << " x " << height << " x " << depth << ", z = " << z
  9898. << ", uses_mipmaps = " << uses_mipmaps << ", clear_color = "
  9899. << tex->get_clear_color() << "\n";
  9900. } else {
  9901. GLCAT.debug()
  9902. << "not loading NULL image for texture " << tex->get_name()
  9903. << ", " << width << " x " << height << " x " << depth
  9904. << ", z = " << z << ", uses_mipmaps = " << uses_mipmaps << "\n";
  9905. }
  9906. } else {
  9907. GLCAT.debug()
  9908. << "updating image data of texture " << tex->get_name()
  9909. << ", " << width << " x " << height << " x " << depth
  9910. << ", z = " << z << ", mipmaps " << num_ram_mipmap_levels
  9911. << ", uses_mipmaps = " << uses_mipmaps << "\n";
  9912. }
  9913. }
  9914. for (int n = mipmap_bias; n < num_levels; ++n) {
  9915. // we grab the mipmap pointer first, if it is NULL we grab the
  9916. // normal mipmap image pointer which is a PTA_uchar
  9917. const unsigned char *image_ptr = (unsigned char*)tex->get_ram_mipmap_pointer(n);
  9918. CPTA_uchar ptimage;
  9919. if (image_ptr == (const unsigned char *)NULL) {
  9920. ptimage = tex->get_ram_mipmap_image(n);
  9921. if (ptimage.is_null()) {
  9922. if (n < num_ram_mipmap_levels) {
  9923. // We were told we'd have this many RAM mipmap images, but
  9924. // we don't. Raise a warning.
  9925. GLCAT.warning()
  9926. << "No mipmap level " << n << " defined for " << tex->get_name()
  9927. << "\n";
  9928. break;
  9929. }
  9930. if (tex->has_clear_color()) {
  9931. // The texture has a clear color, so we should fill this mipmap
  9932. // level to a solid color.
  9933. #ifndef OPENGLES
  9934. if (_supports_clear_texture) {
  9935. // We can do that with the convenient glClearTexImage function.
  9936. string clear_data = tex->get_clear_data();
  9937. _glClearTexImage(gtc->_index, n - mipmap_bias, external_format,
  9938. component_type, (void *)clear_data.data());
  9939. continue;
  9940. }
  9941. #endif // OPENGLES
  9942. // Ask the Texture class to create the mipmap level in RAM.
  9943. // It'll fill it in with the correct clear color, which we
  9944. // can then upload.
  9945. ptimage = tex->make_ram_mipmap_image(n);
  9946. } else {
  9947. // No clear color and no more images.
  9948. break;
  9949. }
  9950. }
  9951. image_ptr = ptimage;
  9952. }
  9953. PTA_uchar bgr_image;
  9954. size_t view_size = tex->get_ram_mipmap_view_size(n);
  9955. if (image_ptr != (const unsigned char *)NULL) {
  9956. const unsigned char *orig_image_ptr = image_ptr;
  9957. image_ptr += view_size * gtc->get_view();
  9958. if (one_page_only) {
  9959. view_size = tex->get_ram_mipmap_page_size(n);
  9960. image_ptr += view_size * z;
  9961. }
  9962. nassertr(image_ptr >= orig_image_ptr && image_ptr + view_size <= orig_image_ptr + tex->get_ram_mipmap_image_size(n), false);
  9963. if (!_supports_bgr && image_compression == Texture::CM_off) {
  9964. // If the GL doesn't claim to support BGR, we may have to reverse
  9965. // the component ordering of the image.
  9966. image_ptr = fix_component_ordering(bgr_image, image_ptr, view_size,
  9967. external_format, tex);
  9968. }
  9969. }
  9970. int width = tex->get_expected_mipmap_x_size(n);
  9971. int height = tex->get_expected_mipmap_y_size(n);
  9972. int depth = tex->get_expected_mipmap_z_size(n);
  9973. #ifdef DO_PSTATS
  9974. _data_transferred_pcollector.add_level(view_size);
  9975. #endif
  9976. switch (texture_target) {
  9977. #ifndef OPENGLES_1
  9978. case GL_TEXTURE_3D:
  9979. if (_supports_3d_texture) {
  9980. if (image_compression == Texture::CM_off) {
  9981. _glTexSubImage3D(page_target, n - mipmap_bias, 0, 0, 0, width, height, depth,
  9982. external_format, component_type, image_ptr);
  9983. } else {
  9984. _glCompressedTexSubImage3D(page_target, n - mipmap_bias, 0, 0, 0, width, height, depth,
  9985. external_format, view_size, image_ptr);
  9986. }
  9987. } else {
  9988. report_my_gl_errors();
  9989. return false;
  9990. }
  9991. break;
  9992. #endif // OPENGLES_1
  9993. #ifndef OPENGLES
  9994. case GL_TEXTURE_1D:
  9995. if (image_compression == Texture::CM_off) {
  9996. glTexSubImage1D(page_target, n - mipmap_bias, 0, width,
  9997. external_format, component_type, image_ptr);
  9998. } else {
  9999. _glCompressedTexSubImage1D(page_target, n - mipmap_bias, 0, width,
  10000. external_format, view_size, image_ptr);
  10001. }
  10002. break;
  10003. #endif // OPENGLES
  10004. #ifndef OPENGLES
  10005. case GL_TEXTURE_2D_ARRAY_EXT:
  10006. if (_supports_2d_texture_array) {
  10007. if (image_compression == Texture::CM_off) {
  10008. _glTexSubImage3D(page_target, n - mipmap_bias, 0, 0, 0, width, height, depth,
  10009. external_format, component_type, image_ptr);
  10010. } else {
  10011. _glCompressedTexSubImage3D(page_target, n - mipmap_bias, 0, 0, 0, width, height, depth,
  10012. external_format, view_size, image_ptr);
  10013. }
  10014. } else {
  10015. report_my_gl_errors();
  10016. return false;
  10017. }
  10018. break;
  10019. #endif // OPENGLES
  10020. #ifndef OPENGLES
  10021. case GL_TEXTURE_BUFFER:
  10022. if (_supports_buffer_texture) {
  10023. _glBufferSubData(GL_TEXTURE_BUFFER, 0, view_size, image_ptr);
  10024. } else {
  10025. report_my_gl_errors();
  10026. return false;
  10027. }
  10028. break;
  10029. #endif // OPENGLES
  10030. default:
  10031. if (image_compression == Texture::CM_off) {
  10032. if (n==0) {
  10033. // It's unfortunate that we can't adjust the width, too,
  10034. // but TexSubImage2D doesn't accept a row-stride parameter.
  10035. height = tex->get_y_size() - tex->get_pad_y_size();
  10036. }
  10037. glTexSubImage2D(page_target, n - mipmap_bias, 0, 0, width, height,
  10038. external_format, component_type, image_ptr);
  10039. } else {
  10040. _glCompressedTexSubImage2D(page_target, n - mipmap_bias, 0, 0, width, height,
  10041. external_format, view_size, image_ptr);
  10042. }
  10043. break;
  10044. }
  10045. }
  10046. // Did that fail? If it did, we'll immediately try again, this
  10047. // time loading the texture from scratch.
  10048. GLenum error_code = gl_get_error();
  10049. if (error_code != GL_NO_ERROR) {
  10050. if (GLCAT.is_debug()) {
  10051. GLCAT.debug()
  10052. << "GL texture subload failed for " << tex->get_name()
  10053. << " : " << get_error_string(error_code) << "\n";
  10054. }
  10055. needs_reload = true;
  10056. }
  10057. }
  10058. if (needs_reload) {
  10059. // Load the image up from scratch, creating a new GL Texture
  10060. // object.
  10061. if (GLCAT.is_debug()) {
  10062. GLCAT.debug()
  10063. << "loading new texture object for " << tex->get_name() << ", " << width
  10064. << " x " << height << " x " << depth << ", z = " << z << ", mipmaps "
  10065. << num_ram_mipmap_levels << ", uses_mipmaps = " << uses_mipmaps << "\n";
  10066. }
  10067. // If there is immutable storage, this is impossible to do, and we should
  10068. // not have gotten here at all.
  10069. nassertr(!gtc->_immutable, false);
  10070. if (num_ram_mipmap_levels == 0) {
  10071. if (GLCAT.is_debug()) {
  10072. GLCAT.debug()
  10073. << " (initializing NULL image)\n";
  10074. }
  10075. if ((external_format == GL_DEPTH_STENCIL) && get_supports_depth_stencil()) {
  10076. #ifdef OPENGLES
  10077. component_type = GL_UNSIGNED_INT_24_8_OES;
  10078. #else
  10079. component_type = GL_UNSIGNED_INT_24_8_EXT;
  10080. #endif
  10081. }
  10082. }
  10083. for (int n = mipmap_bias; n < num_levels; ++n) {
  10084. const unsigned char *image_ptr = (unsigned char*)tex->get_ram_mipmap_pointer(n);
  10085. CPTA_uchar ptimage;
  10086. if (image_ptr == (const unsigned char *)NULL) {
  10087. ptimage = tex->get_ram_mipmap_image(n);
  10088. if (ptimage.is_null()) {
  10089. if (n < num_ram_mipmap_levels) {
  10090. // We were told we'd have this many RAM mipmap images, but
  10091. // we don't. Raise a warning.
  10092. GLCAT.warning()
  10093. << "No mipmap level " << n << " defined for " << tex->get_name()
  10094. << "\n";
  10095. #ifndef OPENGLES
  10096. if (is_at_least_gl_version(1, 2)) {
  10097. // Tell the GL we have no more mipmaps for it to use.
  10098. glTexParameteri(texture_target, GL_TEXTURE_MAX_LEVEL, n - mipmap_bias);
  10099. }
  10100. #endif
  10101. break;
  10102. }
  10103. if (tex->has_clear_color()) {
  10104. // Ask the Texture class to create the mipmap level in RAM.
  10105. // It'll fill it in with the correct clear color, which we
  10106. // can then upload.
  10107. ptimage = tex->make_ram_mipmap_image(n);
  10108. }
  10109. }
  10110. image_ptr = ptimage;
  10111. }
  10112. PTA_uchar bgr_image;
  10113. size_t view_size = tex->get_ram_mipmap_view_size(n);
  10114. if (image_ptr != (const unsigned char *)NULL) {
  10115. const unsigned char *orig_image_ptr = image_ptr;
  10116. image_ptr += view_size * gtc->get_view();
  10117. if (one_page_only) {
  10118. view_size = tex->get_ram_mipmap_page_size(n);
  10119. image_ptr += view_size * z;
  10120. }
  10121. nassertr(image_ptr >= orig_image_ptr && image_ptr + view_size <= orig_image_ptr + tex->get_ram_mipmap_image_size(n), false);
  10122. if (!_supports_bgr && image_compression == Texture::CM_off) {
  10123. // If the GL doesn't claim to support BGR, we may have to reverse
  10124. // the component ordering of the image.
  10125. image_ptr = fix_component_ordering(bgr_image, image_ptr, view_size,
  10126. external_format, tex);
  10127. }
  10128. }
  10129. int width = tex->get_expected_mipmap_x_size(n);
  10130. int height = tex->get_expected_mipmap_y_size(n);
  10131. int depth = tex->get_expected_mipmap_z_size(n);
  10132. #ifdef DO_PSTATS
  10133. _data_transferred_pcollector.add_level(view_size);
  10134. #endif
  10135. switch (texture_target) {
  10136. #ifndef OPENGLES // 1-d textures not supported by OpenGL ES. Fall through.
  10137. case GL_TEXTURE_1D:
  10138. if (image_compression == Texture::CM_off) {
  10139. glTexImage1D(page_target, n - mipmap_bias, internal_format,
  10140. width, 0,
  10141. external_format, component_type, image_ptr);
  10142. } else {
  10143. _glCompressedTexImage1D(page_target, n - mipmap_bias, external_format, width,
  10144. 0, view_size, image_ptr);
  10145. }
  10146. break;
  10147. #endif // OPENGLES // OpenGL ES will fall through.
  10148. #ifdef OPENGLES_2
  10149. case GL_TEXTURE_3D_OES:
  10150. #endif
  10151. #ifndef OPENGLES
  10152. case GL_TEXTURE_3D:
  10153. #endif
  10154. #ifndef OPENGLES_1
  10155. if (_supports_3d_texture) {
  10156. if (image_compression == Texture::CM_off) {
  10157. _glTexImage3D(page_target, n - mipmap_bias, internal_format,
  10158. width, height, depth, 0,
  10159. external_format, component_type, image_ptr);
  10160. } else {
  10161. _glCompressedTexImage3D(page_target, n - mipmap_bias, external_format, width,
  10162. height, depth,
  10163. 0, view_size, image_ptr);
  10164. }
  10165. } else {
  10166. report_my_gl_errors();
  10167. return false;
  10168. }
  10169. break;
  10170. #endif
  10171. #ifndef OPENGLES
  10172. case GL_TEXTURE_2D_ARRAY_EXT:
  10173. if (_supports_2d_texture_array) {
  10174. if (image_compression == Texture::CM_off) {
  10175. _glTexImage3D(page_target, n - mipmap_bias, internal_format,
  10176. width, height, depth, 0,
  10177. external_format, component_type, image_ptr);
  10178. } else {
  10179. _glCompressedTexImage3D(page_target, n - mipmap_bias, external_format, width,
  10180. height, depth,
  10181. 0, view_size, image_ptr);
  10182. }
  10183. } else {
  10184. report_my_gl_errors();
  10185. return false;
  10186. }
  10187. break;
  10188. #endif // OPENGLES
  10189. #ifndef OPENGLES
  10190. case GL_TEXTURE_BUFFER:
  10191. if (_supports_buffer_texture) {
  10192. _glBufferData(GL_TEXTURE_BUFFER, view_size, image_ptr,
  10193. get_usage(tex->get_usage_hint()));
  10194. } else {
  10195. report_my_gl_errors();
  10196. return false;
  10197. }
  10198. break;
  10199. #endif // OPENGLES
  10200. default:
  10201. if (image_compression == Texture::CM_off) {
  10202. glTexImage2D(page_target, n - mipmap_bias, internal_format,
  10203. width, height, 0,
  10204. external_format, component_type, image_ptr);
  10205. } else {
  10206. _glCompressedTexImage2D(page_target, n - mipmap_bias, external_format,
  10207. width, height, 0, view_size, image_ptr);
  10208. }
  10209. }
  10210. }
  10211. // Report the error message explicitly if the GL texture creation
  10212. // failed.
  10213. GLenum error_code = gl_get_error();
  10214. if (error_code != GL_NO_ERROR) {
  10215. GLCAT.error()
  10216. << "GL texture creation failed for " << tex->get_name()
  10217. << " : " << get_error_string(error_code) << "\n";
  10218. gtc->_has_storage = false;
  10219. return false;
  10220. }
  10221. }
  10222. report_my_gl_errors();
  10223. return true;
  10224. }
  10225. ////////////////////////////////////////////////////////////////////
  10226. // Function: GLGraphicsStateGuardian::upload_simple_texture
  10227. // Access: Protected
  10228. // Description: This is used as a standin for upload_texture
  10229. // when the texture in question is unavailable (e.g. it
  10230. // hasn't yet been loaded from disk). Until the texture
  10231. // image itself becomes available, we will render the
  10232. // texture's "simple" image--a sharply reduced version
  10233. // of the same texture.
  10234. ////////////////////////////////////////////////////////////////////
  10235. bool CLP(GraphicsStateGuardian)::
  10236. upload_simple_texture(CLP(TextureContext) *gtc) {
  10237. report_my_gl_errors();
  10238. PStatGPUTimer timer(this, _load_texture_pcollector);
  10239. Texture *tex = gtc->get_texture();
  10240. nassertr(tex != (Texture *)NULL, false);
  10241. int internal_format = GL_RGBA;
  10242. #ifdef OPENGLES_2
  10243. int external_format = GL_RGBA;
  10244. #else
  10245. int external_format = GL_BGRA;
  10246. #endif
  10247. const unsigned char *image_ptr = tex->get_simple_ram_image();
  10248. if (image_ptr == (const unsigned char *)NULL) {
  10249. return false;
  10250. }
  10251. size_t image_size = tex->get_simple_ram_image_size();
  10252. PTA_uchar bgr_image;
  10253. if (!_supports_bgr) {
  10254. // If the GL doesn't claim to support BGR, we may have to reverse
  10255. // the component ordering of the image.
  10256. external_format = GL_RGBA;
  10257. image_ptr = fix_component_ordering(bgr_image, image_ptr, image_size,
  10258. external_format, tex);
  10259. }
  10260. int width = tex->get_simple_x_size();
  10261. int height = tex->get_simple_y_size();
  10262. int component_type = GL_UNSIGNED_BYTE;
  10263. if (GLCAT.is_debug()) {
  10264. GLCAT.debug()
  10265. << "loading simple image for " << tex->get_name() << "\n";
  10266. }
  10267. #ifndef OPENGLES
  10268. // Turn off mipmaps for the simple texture.
  10269. if (tex->uses_mipmaps()) {
  10270. if (is_at_least_gl_version(1, 2)) {
  10271. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
  10272. }
  10273. }
  10274. #endif
  10275. #ifdef DO_PSTATS
  10276. _data_transferred_pcollector.add_level(image_size);
  10277. #endif
  10278. glTexImage2D(GL_TEXTURE_2D, 0, internal_format,
  10279. width, height, 0,
  10280. external_format, component_type, image_ptr);
  10281. gtc->mark_simple_loaded();
  10282. report_my_gl_errors();
  10283. return true;
  10284. }
  10285. ////////////////////////////////////////////////////////////////////
  10286. // Function: GLGraphicsStateGuardian::get_texture_memory_size
  10287. // Access: Protected
  10288. // Description: Asks OpenGL how much texture memory is consumed by
  10289. // the indicated texture (which is also the
  10290. // currently-selected texture).
  10291. ////////////////////////////////////////////////////////////////////
  10292. size_t CLP(GraphicsStateGuardian)::
  10293. get_texture_memory_size(Texture *tex) {
  10294. #ifdef OPENGLES // Texture querying not supported on OpenGL ES.
  10295. int width = tex->get_x_size();
  10296. int height = tex->get_y_size();
  10297. int depth = 1;
  10298. int scale = 1;
  10299. bool has_mipmaps = tex->uses_mipmaps();
  10300. size_t num_bytes = 2; // Temporary assumption?
  10301. #else
  10302. GLenum target = get_texture_target(tex->get_texture_type());
  10303. GLenum page_target = target;
  10304. GLint scale = 1;
  10305. if (target == GL_TEXTURE_CUBE_MAP) {
  10306. // We need a particular page to get the level parameter from.
  10307. page_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  10308. scale = 6;
  10309. } else if (target == GL_TEXTURE_BUFFER) {
  10310. // In the case of buffer textures, we provided the size to begin with,
  10311. // so no point in querying anything. Plus, glGetTexParameter is not even
  10312. // supported for buffer textures.
  10313. return tex->get_expected_ram_image_size();
  10314. }
  10315. GLint minfilter;
  10316. glGetTexParameteriv(target, GL_TEXTURE_MIN_FILTER, &minfilter);
  10317. bool has_mipmaps = is_mipmap_filter(minfilter);
  10318. clear_my_gl_errors();
  10319. GLint internal_format;
  10320. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);
  10321. if (is_compressed_format(internal_format)) {
  10322. // Try to get the compressed size.
  10323. GLint image_size;
  10324. glGetTexLevelParameteriv(page_target, 0,
  10325. GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &image_size);
  10326. GLenum error_code = gl_get_error();
  10327. if (error_code != GL_NO_ERROR) {
  10328. if (GLCAT.is_debug()) {
  10329. GLCAT.debug()
  10330. << "Couldn't get compressed size for " << tex->get_name()
  10331. << " : " << get_error_string(error_code) << "\n";
  10332. }
  10333. // Fall through to the noncompressed case.
  10334. } else {
  10335. return image_size * scale;
  10336. }
  10337. }
  10338. // OK, get the noncompressed size.
  10339. GLint red_size, green_size, blue_size, alpha_size,
  10340. luminance_size, intensity_size;
  10341. GLint depth_size = 0;
  10342. glGetTexLevelParameteriv(page_target, 0,
  10343. GL_TEXTURE_RED_SIZE, &red_size);
  10344. glGetTexLevelParameteriv(page_target, 0,
  10345. GL_TEXTURE_GREEN_SIZE, &green_size);
  10346. glGetTexLevelParameteriv(page_target, 0,
  10347. GL_TEXTURE_BLUE_SIZE, &blue_size);
  10348. glGetTexLevelParameteriv(page_target, 0,
  10349. GL_TEXTURE_ALPHA_SIZE, &alpha_size);
  10350. glGetTexLevelParameteriv(page_target, 0,
  10351. GL_TEXTURE_LUMINANCE_SIZE, &luminance_size);
  10352. glGetTexLevelParameteriv(page_target, 0,
  10353. GL_TEXTURE_INTENSITY_SIZE, &intensity_size);
  10354. if (_supports_depth_texture) {
  10355. glGetTexLevelParameteriv(page_target, 0,
  10356. GL_TEXTURE_DEPTH_SIZE, &depth_size);
  10357. }
  10358. GLint width = 1, height = 1, depth = 1;
  10359. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_WIDTH, &width);
  10360. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_HEIGHT, &height);
  10361. if (_supports_3d_texture || _supports_2d_texture_array) {
  10362. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_DEPTH, &depth);
  10363. }
  10364. report_my_gl_errors();
  10365. size_t num_bits = (red_size + green_size + blue_size + alpha_size + luminance_size + intensity_size + depth_size);
  10366. size_t num_bytes = (num_bits + 7) / 8;
  10367. #endif // OPENGLES
  10368. size_t result = num_bytes * width * height * depth * scale;
  10369. if (has_mipmaps) {
  10370. result = (result * 4) / 3;
  10371. }
  10372. return result;
  10373. }
  10374. ////////////////////////////////////////////////////////////////////
  10375. // Function: GLGraphicsStateGuardian::check_nonresident_texture
  10376. // Access: Private
  10377. // Description: Checks the list of resident texture objects to see if
  10378. // any have recently been evicted.
  10379. ////////////////////////////////////////////////////////////////////
  10380. void CLP(GraphicsStateGuardian)::
  10381. check_nonresident_texture(BufferContextChain &chain) {
  10382. #ifndef OPENGLES // Residency queries not supported by OpenGL ES.
  10383. size_t num_textures = chain.get_count();
  10384. if (num_textures == 0) {
  10385. return;
  10386. }
  10387. CLP(TextureContext) **gtc_list = (CLP(TextureContext) **)alloca(num_textures * sizeof(CLP(TextureContext) *));
  10388. GLuint *texture_list = (GLuint *)alloca(num_textures * sizeof(GLuint));
  10389. size_t ti = 0;
  10390. BufferContext *node = chain.get_first();
  10391. while (node != (BufferContext *)NULL) {
  10392. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), node);
  10393. gtc_list[ti] = gtc;
  10394. texture_list[ti] = gtc->_index;
  10395. node = node->get_next();
  10396. ++ti;
  10397. }
  10398. nassertv(ti == num_textures);
  10399. GLboolean *results = (GLboolean *)alloca(num_textures * sizeof(GLboolean));
  10400. bool all_resident = (glAreTexturesResident(num_textures, texture_list, results) != 0);
  10401. report_my_gl_errors();
  10402. if (!all_resident) {
  10403. // Some are now nonresident.
  10404. for (ti = 0; ti < num_textures; ++ti) {
  10405. if (!results[ti]) {
  10406. gtc_list[ti]->set_resident(false);
  10407. }
  10408. }
  10409. }
  10410. #endif // OPENGLES
  10411. }
  10412. ////////////////////////////////////////////////////////////////////
  10413. // Function: GLGraphicsStateGuardian::do_extract_texture_data
  10414. // Access: Protected
  10415. // Description: The internal implementation of
  10416. // extract_texture_data(), given an already-created
  10417. // TextureContext.
  10418. ////////////////////////////////////////////////////////////////////
  10419. bool CLP(GraphicsStateGuardian)::
  10420. do_extract_texture_data(CLP(TextureContext) *gtc) {
  10421. report_my_gl_errors();
  10422. GLenum target = gtc->_target;
  10423. if (target == GL_NONE) {
  10424. return false;
  10425. }
  10426. #ifndef OPENGLES
  10427. // Make sure any incoherent writes to the texture have been synced.
  10428. if (gtc->needs_barrier(GL_TEXTURE_UPDATE_BARRIER_BIT)) {
  10429. issue_memory_barrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
  10430. }
  10431. #endif
  10432. glBindTexture(target, gtc->_index);
  10433. Texture *tex = gtc->get_texture();
  10434. GLint wrap_u, wrap_v, wrap_w;
  10435. GLint minfilter, magfilter;
  10436. GLfloat border_color[4];
  10437. glGetTexParameteriv(target, GL_TEXTURE_WRAP_S, &wrap_u);
  10438. glGetTexParameteriv(target, GL_TEXTURE_WRAP_T, &wrap_v);
  10439. wrap_w = GL_REPEAT;
  10440. if (_supports_3d_texture) {
  10441. #ifdef OPENGLES_2
  10442. glGetTexParameteriv(target, GL_TEXTURE_WRAP_R_OES, &wrap_w);
  10443. #endif
  10444. #ifndef OPENGLES
  10445. glGetTexParameteriv(target, GL_TEXTURE_WRAP_R, &wrap_w);
  10446. #endif
  10447. }
  10448. if (_supports_2d_texture_array) {
  10449. #ifndef OPENGLES
  10450. glGetTexParameteriv(target, GL_TEXTURE_WRAP_R, &wrap_w);
  10451. #endif
  10452. }
  10453. glGetTexParameteriv(target, GL_TEXTURE_MIN_FILTER, &minfilter);
  10454. glGetTexParameteriv(target, GL_TEXTURE_MAG_FILTER, &magfilter);
  10455. #ifndef OPENGLES
  10456. glGetTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, border_color);
  10457. #endif
  10458. GLenum page_target = target;
  10459. if (target == GL_TEXTURE_CUBE_MAP) {
  10460. // We need a particular page to get the level parameter from.
  10461. page_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
  10462. }
  10463. GLint width = gtc->_width, height = gtc->_height, depth = gtc->_depth;
  10464. #ifndef OPENGLES
  10465. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_WIDTH, &width);
  10466. if (target != GL_TEXTURE_1D) {
  10467. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_HEIGHT, &height);
  10468. }
  10469. if (_supports_3d_texture && target == GL_TEXTURE_3D) {
  10470. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_DEPTH, &depth);
  10471. }
  10472. #ifndef OPENGLES
  10473. else if (_supports_2d_texture_array && target == GL_TEXTURE_2D_ARRAY_EXT) {
  10474. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_DEPTH, &depth);
  10475. }
  10476. #endif
  10477. else if (target == GL_TEXTURE_CUBE_MAP) {
  10478. depth = 6;
  10479. }
  10480. #endif
  10481. clear_my_gl_errors();
  10482. if (width <= 0 || height <= 0 || depth <= 0) {
  10483. GLCAT.error()
  10484. << "No texture data for " << tex->get_name() << "\n";
  10485. return false;
  10486. }
  10487. GLint internal_format = GL_RGBA;
  10488. #ifndef OPENGLES
  10489. glGetTexLevelParameteriv(page_target, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);
  10490. #endif // OPENGLES
  10491. // Make sure we were able to query those parameters properly.
  10492. GLenum error_code = gl_get_error();
  10493. if (error_code != GL_NO_ERROR) {
  10494. GLCAT.error()
  10495. << "Unable to query texture parameters for " << tex->get_name()
  10496. << " : " << get_error_string(error_code) << "\n";
  10497. return false;
  10498. }
  10499. Texture::ComponentType type = Texture::T_unsigned_byte;
  10500. Texture::Format format = Texture::F_rgb;
  10501. Texture::CompressionMode compression = Texture::CM_off;
  10502. switch (internal_format) {
  10503. #ifndef OPENGLES
  10504. case GL_COLOR_INDEX:
  10505. format = Texture::F_color_index;
  10506. break;
  10507. #endif
  10508. #if GL_DEPTH_COMPONENT != GL_DEPTH_COMPONENT24
  10509. case GL_DEPTH_COMPONENT:
  10510. #endif
  10511. case GL_DEPTH_COMPONENT16:
  10512. case GL_DEPTH_COMPONENT24:
  10513. case GL_DEPTH_COMPONENT32:
  10514. type = Texture::T_unsigned_short;
  10515. format = Texture::F_depth_component;
  10516. break;
  10517. #ifndef OPENGLES
  10518. case GL_DEPTH_COMPONENT32F:
  10519. type = Texture::T_float;
  10520. format = Texture::F_depth_component;
  10521. break;
  10522. #endif
  10523. case GL_DEPTH_STENCIL:
  10524. case GL_DEPTH24_STENCIL8:
  10525. type = Texture::T_unsigned_int_24_8;
  10526. format = Texture::F_depth_stencil;
  10527. break;
  10528. #ifndef OPENGLES
  10529. case GL_DEPTH32F_STENCIL8:
  10530. type = Texture::T_float;
  10531. format = Texture::F_depth_stencil;
  10532. break;
  10533. #endif
  10534. case GL_RGBA:
  10535. case 4:
  10536. format = Texture::F_rgba;
  10537. break;
  10538. case GL_RGBA4:
  10539. format = Texture::F_rgba4;
  10540. break;
  10541. #ifdef OPENGLES
  10542. case GL_RGBA8_OES:
  10543. format = Texture::F_rgba8;
  10544. break;
  10545. #else
  10546. case GL_RGBA8:
  10547. format = Texture::F_rgba8;
  10548. break;
  10549. #endif
  10550. #ifndef OPENGLES
  10551. case GL_RGBA12:
  10552. type = Texture::T_unsigned_short;
  10553. format = Texture::F_rgba12;
  10554. break;
  10555. #endif
  10556. case GL_RGB:
  10557. case 3:
  10558. format = Texture::F_rgb;
  10559. break;
  10560. #ifndef OPENGLES
  10561. case GL_RGB5:
  10562. format = Texture::F_rgb5;
  10563. break;
  10564. #endif
  10565. case GL_RGB5_A1:
  10566. format = Texture::F_rgba5;
  10567. break;
  10568. #ifndef OPENGLES
  10569. case GL_RGB8:
  10570. format = Texture::F_rgb8;
  10571. break;
  10572. case GL_RGB12:
  10573. format = Texture::F_rgb12;
  10574. break;
  10575. case GL_RGBA16:
  10576. format = Texture::F_rgba16;
  10577. break;
  10578. case GL_R3_G3_B2:
  10579. format = Texture::F_rgb332;
  10580. break;
  10581. case GL_R8I:
  10582. format = Texture::F_r8i;
  10583. break;
  10584. case GL_RG8I:
  10585. format = Texture::F_rg8i;
  10586. break;
  10587. case GL_RGB8I:
  10588. format = Texture::F_rgb8i;
  10589. break;
  10590. case GL_RGBA8I:
  10591. format = Texture::F_rgba8i;
  10592. break;
  10593. case GL_R8UI:
  10594. type = Texture::T_unsigned_byte;
  10595. format = Texture::F_r8i;
  10596. break;
  10597. case GL_RG8UI:
  10598. type = Texture::T_unsigned_byte;
  10599. format = Texture::F_rg8i;
  10600. break;
  10601. case GL_RGB8UI:
  10602. type = Texture::T_unsigned_byte;
  10603. format = Texture::F_rgb8i;
  10604. break;
  10605. case GL_RGBA8UI:
  10606. type = Texture::T_unsigned_byte;
  10607. format = Texture::F_rgba8i;
  10608. break;
  10609. #endif
  10610. #ifndef OPENGLES_1
  10611. case GL_RGBA16F:
  10612. type = Texture::T_float;
  10613. format = Texture::F_rgba16;
  10614. break;
  10615. case GL_RGB16F:
  10616. type = Texture::T_float;
  10617. format = Texture::F_rgb16;
  10618. break;
  10619. case GL_RG16F:
  10620. type = Texture::T_float;
  10621. format = Texture::F_rg16;
  10622. break;
  10623. case GL_R16F:
  10624. type = Texture::T_float;
  10625. format = Texture::F_r16;
  10626. break;
  10627. case GL_RGBA32F:
  10628. type = Texture::T_float;
  10629. format = Texture::F_rgba32;
  10630. break;
  10631. case GL_RGB32F:
  10632. type = Texture::T_float;
  10633. format = Texture::F_rgb32;
  10634. break;
  10635. case GL_RG32F:
  10636. type = Texture::T_float;
  10637. format = Texture::F_rg32;
  10638. break;
  10639. case GL_R32F:
  10640. type = Texture::T_float;
  10641. format = Texture::F_r32;
  10642. break;
  10643. #endif
  10644. #ifndef OPENGLES
  10645. case GL_RGB16:
  10646. type = Texture::T_unsigned_short;
  10647. format = Texture::F_rgb16;
  10648. break;
  10649. case GL_RG16:
  10650. type = Texture::T_unsigned_short;
  10651. format = Texture::F_rg16;
  10652. break;
  10653. case GL_R16:
  10654. type = Texture::T_unsigned_short;
  10655. format = Texture::F_r16;
  10656. break;
  10657. #endif
  10658. #ifdef OPENGLES_2
  10659. case GL_RED_EXT:
  10660. case GL_R8_EXT:
  10661. format = Texture::F_red;
  10662. break;
  10663. #endif
  10664. #ifndef OPENGLES
  10665. case GL_R32I:
  10666. type = Texture::T_int;
  10667. format = Texture::F_r32i;
  10668. break;
  10669. #endif
  10670. #ifndef OPENGLES
  10671. case GL_RED:
  10672. format = Texture::F_red;
  10673. break;
  10674. case GL_GREEN:
  10675. format = Texture::F_green;
  10676. break;
  10677. case GL_BLUE:
  10678. format = Texture::F_blue;
  10679. break;
  10680. #endif // OPENGLES
  10681. case GL_ALPHA:
  10682. format = Texture::F_alpha;
  10683. break;
  10684. case GL_LUMINANCE:
  10685. #ifndef OPENGLES
  10686. case GL_LUMINANCE16:
  10687. case GL_LUMINANCE16F_ARB:
  10688. #endif
  10689. case 1:
  10690. format = Texture::F_luminance;
  10691. break;
  10692. case GL_LUMINANCE_ALPHA:
  10693. #ifndef OPENGLES
  10694. case GL_LUMINANCE_ALPHA16F_ARB:
  10695. #endif
  10696. case 2:
  10697. format = Texture::F_luminance_alpha;
  10698. break;
  10699. #ifndef OPENGLES_1
  10700. case GL_SRGB:
  10701. #ifndef OPENGLES
  10702. case GL_SRGB8:
  10703. #endif
  10704. format = Texture::F_srgb;
  10705. break;
  10706. case GL_SRGB_ALPHA:
  10707. case GL_SRGB8_ALPHA8:
  10708. format = Texture::F_srgb_alpha;
  10709. break;
  10710. #endif // OPENGLES_1
  10711. #ifndef OPENGLES
  10712. case GL_SLUMINANCE:
  10713. case GL_SLUMINANCE8:
  10714. format = Texture::F_sluminance;
  10715. break;
  10716. case GL_SLUMINANCE_ALPHA:
  10717. case GL_SLUMINANCE8_ALPHA8:
  10718. format = Texture::F_sluminance_alpha;
  10719. break;
  10720. #endif // OPENGLES
  10721. #ifndef OPENGLES
  10722. case GL_COMPRESSED_RGB:
  10723. format = Texture::F_rgb;
  10724. compression = Texture::CM_on;
  10725. break;
  10726. case GL_COMPRESSED_RGBA:
  10727. format = Texture::F_rgba;
  10728. compression = Texture::CM_on;
  10729. break;
  10730. case GL_COMPRESSED_ALPHA:
  10731. format = Texture::F_alpha;
  10732. compression = Texture::CM_on;
  10733. break;
  10734. case GL_COMPRESSED_LUMINANCE:
  10735. format = Texture::F_luminance;
  10736. compression = Texture::CM_on;
  10737. break;
  10738. case GL_COMPRESSED_LUMINANCE_ALPHA:
  10739. format = Texture::F_luminance_alpha;
  10740. compression = Texture::CM_on;
  10741. break;
  10742. case GL_COMPRESSED_SRGB:
  10743. format = Texture::F_srgb;
  10744. compression = Texture::CM_on;
  10745. break;
  10746. case GL_COMPRESSED_SRGB_ALPHA:
  10747. format = Texture::F_srgb_alpha;
  10748. compression = Texture::CM_on;
  10749. break;
  10750. case GL_COMPRESSED_SLUMINANCE:
  10751. format = Texture::F_sluminance;
  10752. compression = Texture::CM_on;
  10753. break;
  10754. case GL_COMPRESSED_SLUMINANCE_ALPHA:
  10755. format = Texture::F_sluminance_alpha;
  10756. compression = Texture::CM_on;
  10757. break;
  10758. #endif
  10759. case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  10760. format = Texture::F_rgb;
  10761. compression = Texture::CM_dxt1;
  10762. break;
  10763. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  10764. format = Texture::F_rgbm;
  10765. compression = Texture::CM_dxt1;
  10766. break;
  10767. #ifndef OPENGLES
  10768. case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
  10769. format = Texture::F_srgb;
  10770. compression = Texture::CM_dxt1;
  10771. break;
  10772. case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
  10773. format = Texture::F_srgb_alpha;
  10774. compression = Texture::CM_dxt1;
  10775. break;
  10776. #endif
  10777. #ifdef OPENGLES
  10778. case GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG:
  10779. format = Texture::F_rgb;
  10780. compression = Texture::CM_pvr1_2bpp;
  10781. break;
  10782. case GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG:
  10783. format = Texture::F_rgba;
  10784. compression = Texture::CM_pvr1_2bpp;
  10785. break;
  10786. case GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG:
  10787. format = Texture::F_rgb;
  10788. compression = Texture::CM_pvr1_4bpp;
  10789. break;
  10790. case GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG:
  10791. format = Texture::F_rgba;
  10792. compression = Texture::CM_pvr1_4bpp;
  10793. break;
  10794. #else
  10795. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  10796. format = Texture::F_rgba;
  10797. compression = Texture::CM_dxt3;
  10798. break;
  10799. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  10800. format = Texture::F_rgba;
  10801. compression = Texture::CM_dxt5;
  10802. break;
  10803. case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
  10804. format = Texture::F_srgb_alpha;
  10805. compression = Texture::CM_dxt3;
  10806. break;
  10807. case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
  10808. format = Texture::F_srgb_alpha;
  10809. compression = Texture::CM_dxt5;
  10810. break;
  10811. case GL_COMPRESSED_RGB_FXT1_3DFX:
  10812. format = Texture::F_rgb;
  10813. compression = Texture::CM_fxt1;
  10814. break;
  10815. case GL_COMPRESSED_RGBA_FXT1_3DFX:
  10816. format = Texture::F_rgba;
  10817. compression = Texture::CM_fxt1;
  10818. break;
  10819. #endif
  10820. default:
  10821. GLCAT.warning()
  10822. << "Unhandled internal format for " << tex->get_name()
  10823. << " : " << hex << "0x" << internal_format << dec << "\n";
  10824. return false;
  10825. }
  10826. // We don't want to call setup_texture() again; that resets too
  10827. // much. Instead, we'll just set the individual components.
  10828. tex->set_x_size(width);
  10829. tex->set_y_size(height);
  10830. tex->set_z_size(depth);
  10831. tex->set_component_type(type);
  10832. tex->set_format(format);
  10833. tex->set_wrap_u(get_panda_wrap_mode(wrap_u));
  10834. tex->set_wrap_v(get_panda_wrap_mode(wrap_v));
  10835. tex->set_wrap_w(get_panda_wrap_mode(wrap_w));
  10836. tex->set_border_color(LColor(border_color[0], border_color[1],
  10837. border_color[2], border_color[3]));
  10838. tex->set_minfilter(get_panda_filter_type(minfilter));
  10839. // tex->set_magfilter(get_panda_filter_type(magfilter));
  10840. PTA_uchar image;
  10841. size_t page_size = 0;
  10842. if (!extract_texture_image(image, page_size, tex, target, page_target,
  10843. type, compression, 0)) {
  10844. return false;
  10845. }
  10846. tex->set_ram_image(image, compression, page_size);
  10847. if (gtc->_uses_mipmaps) {
  10848. // Also get the mipmap levels.
  10849. GLint num_expected_levels = tex->get_expected_num_mipmap_levels();
  10850. GLint highest_level = num_expected_levels;
  10851. #ifndef OPENGLES
  10852. if (is_at_least_gl_version(1, 2)) {
  10853. glGetTexParameteriv(target, GL_TEXTURE_MAX_LEVEL, &highest_level);
  10854. highest_level = min(highest_level, num_expected_levels);
  10855. }
  10856. #endif
  10857. for (int n = 1; n <= highest_level; ++n) {
  10858. if (!extract_texture_image(image, page_size, tex, target, page_target,
  10859. type, compression, n)) {
  10860. return false;
  10861. }
  10862. tex->set_ram_mipmap_image(n, image, page_size);
  10863. }
  10864. }
  10865. return true;
  10866. }
  10867. ////////////////////////////////////////////////////////////////////
  10868. // Function: GLGraphicsStateGuardian::extract_texture_image
  10869. // Access: Protected
  10870. // Description: Called from extract_texture_data(), this gets just
  10871. // the image array for a particular mipmap level (or for
  10872. // the base image).
  10873. ////////////////////////////////////////////////////////////////////
  10874. bool CLP(GraphicsStateGuardian)::
  10875. extract_texture_image(PTA_uchar &image, size_t &page_size,
  10876. Texture *tex, GLenum target, GLenum page_target,
  10877. Texture::ComponentType type,
  10878. Texture::CompressionMode compression, int n) {
  10879. #ifdef OPENGLES // Extracting texture data unsupported in OpenGL ES.
  10880. nassertr(false, false);
  10881. return false;
  10882. #else
  10883. if (target == GL_TEXTURE_CUBE_MAP) {
  10884. // A cube map, compressed or uncompressed. This we must extract
  10885. // one page at a time.
  10886. // If the cube map is compressed, we assume that all the
  10887. // compressed pages are exactly the same size. OpenGL doesn't
  10888. // make this assumption, but it happens to be true for all
  10889. // currently extant compression schemes, and it makes things
  10890. // simpler for us. (It also makes things much simpler for the
  10891. // graphics hardware, so it's likely to continue to be true for a
  10892. // while at least.)
  10893. GLenum external_format = get_external_image_format(tex);
  10894. GLenum pixel_type = get_component_type(type);
  10895. page_size = tex->get_expected_ram_mipmap_page_size(n);
  10896. if (compression != Texture::CM_off) {
  10897. GLint image_size;
  10898. glGetTexLevelParameteriv(page_target, n,
  10899. GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &image_size);
  10900. nassertr(image_size <= (int)page_size, false);
  10901. page_size = image_size;
  10902. }
  10903. image = PTA_uchar::empty_array(page_size * 6);
  10904. for (int z = 0; z < 6; ++z) {
  10905. page_target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + z;
  10906. if (compression == Texture::CM_off) {
  10907. glGetTexImage(page_target, n, external_format, pixel_type,
  10908. image.p() + z * page_size);
  10909. } else {
  10910. _glGetCompressedTexImage(page_target, 0, image.p() + z * page_size);
  10911. }
  10912. }
  10913. } else if (compression == Texture::CM_off) {
  10914. // An uncompressed 1-d, 2-d, or 3-d texture.
  10915. image = PTA_uchar::empty_array(tex->get_expected_ram_mipmap_image_size(n));
  10916. GLenum external_format = get_external_image_format(tex);
  10917. GLenum pixel_type = get_component_type(type);
  10918. glGetTexImage(target, n, external_format, pixel_type, image.p());
  10919. } else {
  10920. // A compressed 1-d, 2-d, or 3-d texture.
  10921. GLint image_size;
  10922. glGetTexLevelParameteriv(target, n, GL_TEXTURE_COMPRESSED_IMAGE_SIZE, &image_size);
  10923. page_size = image_size / tex->get_z_size();
  10924. image = PTA_uchar::empty_array(image_size);
  10925. // Some drivers (ATI!) seem to try to overstuff more bytes in the
  10926. // array than they asked us to allocate (that is, more bytes than
  10927. // GL_TEXTURE_COMPRESSED_IMAGE_SIZE), requiring us to overallocate
  10928. // and then copy the result into our final buffer. Sheesh.
  10929. // We'll only do this for small textures (the ATI bug doesn't
  10930. // *seem* to affect large textures), to save on the overhead of
  10931. // the double-copy, and reduce risk from an overly-large alloca().
  10932. #ifndef NDEBUG
  10933. static const int max_trouble_buffer = 102400;
  10934. #else
  10935. static const int max_trouble_buffer = 1024;
  10936. #endif
  10937. if (image_size < max_trouble_buffer) {
  10938. static const int extra_space = 32;
  10939. unsigned char *buffer = (unsigned char *)alloca(image_size + extra_space);
  10940. #ifndef NDEBUG
  10941. // Tag the buffer with a specific byte so we can report on
  10942. // whether that driver bug is still active.
  10943. static unsigned char keep_token = 0x00;
  10944. unsigned char token = ++keep_token;
  10945. memset(buffer + image_size, token, extra_space);
  10946. #endif
  10947. _glGetCompressedTexImage(target, n, buffer);
  10948. memcpy(image.p(), buffer, image_size);
  10949. #ifndef NDEBUG
  10950. int count = extra_space;
  10951. while (count > 0 && buffer[image_size + count - 1] == token) {
  10952. --count;
  10953. }
  10954. if (count != 0) {
  10955. GLCAT.warning()
  10956. << "GL graphics driver overfilled " << count
  10957. << " bytes into a " << image_size
  10958. << "-byte buffer provided to glGetCompressedTexImage()\n";
  10959. }
  10960. // This had better not equal the amount of buffer space we set
  10961. // aside. If it does, we assume the driver might have
  10962. // overfilled even our provided extra buffer.
  10963. nassertr(count != extra_space, true)
  10964. #endif // NDEBUG
  10965. } else {
  10966. _glGetCompressedTexImage(target, n, image.p());
  10967. }
  10968. }
  10969. // Now see if we were successful.
  10970. GLenum error_code = gl_get_error();
  10971. if (error_code != GL_NO_ERROR) {
  10972. GLCAT.error()
  10973. << "Unable to extract texture for " << *tex
  10974. << ", mipmap level " << n
  10975. << " : " << get_error_string(error_code) << "\n";
  10976. nassertr(false, false);
  10977. return false;
  10978. }
  10979. return true;
  10980. #endif // OPENGLES
  10981. }
  10982. ////////////////////////////////////////////////////////////////////
  10983. // Function: GLGraphicsStateGuardian::do_point_size
  10984. // Access: Protected
  10985. // Description: Internally sets the point size parameters after any
  10986. // of the properties have changed that might affect
  10987. // this.
  10988. ////////////////////////////////////////////////////////////////////
  10989. void CLP(GraphicsStateGuardian)::
  10990. do_point_size() {
  10991. #ifndef OPENGLES_2
  10992. if (!_point_perspective) {
  10993. // Normal, constant-sized points. Here _point_size is a width in
  10994. // pixels.
  10995. static LVecBase3f constant(1.0f, 0.0f, 0.0f);
  10996. _glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, constant.get_data());
  10997. } else {
  10998. // Perspective-sized points. Here _point_size is a width in 3-d
  10999. // units. To arrange that, we need to figure out the appropriate
  11000. // scaling factor based on the current viewport and projection
  11001. // matrix.
  11002. LVector3 height(0.0f, _point_size, 1.0f);
  11003. height = height * _projection_mat->get_mat();
  11004. height = height * _internal_transform->get_scale()[1];
  11005. PN_stdfloat s = height[1] * _viewport_height / _point_size;
  11006. if (_current_lens->is_orthographic()) {
  11007. // If we have an orthographic lens in effect, we don't actually
  11008. // apply a perspective transform: we just scale the points once,
  11009. // regardless of the distance from the camera.
  11010. LVecBase3f constant(1.0f / (s * s), 0.0f, 0.0f);
  11011. _glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, constant.get_data());
  11012. } else {
  11013. // Otherwise, we give it a true perspective adjustment.
  11014. LVecBase3f square(0.0f, 0.0f, 1.0f / (s * s));
  11015. _glPointParameterfv(GL_POINT_DISTANCE_ATTENUATION, square.get_data());
  11016. }
  11017. }
  11018. report_my_gl_errors();
  11019. #endif
  11020. }
  11021. ////////////////////////////////////////////////////////////////////
  11022. // Function: GLGraphicsStateGuardian::get_supports_cg_profile
  11023. // Access: Public, Virtual
  11024. // Description: Returns true if this particular GSG supports the
  11025. // specified Cg Shader Profile.
  11026. ////////////////////////////////////////////////////////////////////
  11027. bool CLP(GraphicsStateGuardian)::
  11028. get_supports_cg_profile(const string &name) const {
  11029. #if !defined(HAVE_CG) || defined(OPENGLES)
  11030. return false;
  11031. #else
  11032. CGprofile profile = cgGetProfile(name.c_str());
  11033. if (profile == CG_PROFILE_UNKNOWN) {
  11034. GLCAT.error() << name << ", unknown Cg-profile\n";
  11035. return false;
  11036. }
  11037. return (cgGLIsProfileSupported(profile) != 0);
  11038. #endif
  11039. }
  11040. ////////////////////////////////////////////////////////////////////
  11041. // Function: GLGraphicsStateGuardian::bind_fbo
  11042. // Access: Protected
  11043. // Description: Binds a framebuffer object.
  11044. ////////////////////////////////////////////////////////////////////
  11045. void CLP(GraphicsStateGuardian)::
  11046. bind_fbo(GLuint fbo) {
  11047. if (_current_fbo == fbo) {
  11048. return;
  11049. }
  11050. PStatGPUTimer timer(this, _fbo_bind_pcollector);
  11051. nassertv(_glBindFramebuffer != 0);
  11052. #if defined(OPENGLES_2)
  11053. _glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  11054. #elif defined(OPENGLES_1)
  11055. _glBindFramebuffer(GL_FRAMEBUFFER_OES, fbo);
  11056. #else
  11057. _glBindFramebuffer(GL_FRAMEBUFFER_EXT, fbo);
  11058. #endif
  11059. _current_fbo = fbo;
  11060. }
  11061. ////////////////////////////////////////////////////////////////////
  11062. // GL stencil code section
  11063. ////////////////////////////////////////////////////////////////////
  11064. static int gl_stencil_operations_array[] = {
  11065. GL_KEEP,
  11066. GL_ZERO,
  11067. GL_REPLACE,
  11068. #ifdef OPENGLES_1
  11069. GL_INCR_WRAP_OES,
  11070. GL_DECR_WRAP_OES,
  11071. #else
  11072. GL_INCR_WRAP,
  11073. GL_DECR_WRAP,
  11074. #endif
  11075. GL_INVERT,
  11076. GL_INCR,
  11077. GL_DECR,
  11078. };
  11079. ////////////////////////////////////////////////////////////////////
  11080. // Function: GLGraphicsStateGuardian::do_issue_stencil
  11081. // Access: Protected
  11082. // Description: Set stencil render states.
  11083. ////////////////////////////////////////////////////////////////////
  11084. void CLP(GraphicsStateGuardian)::
  11085. do_issue_stencil() {
  11086. if (!_supports_stencil) {
  11087. return;
  11088. }
  11089. const StencilAttrib *stencil;
  11090. if (_target_rs->get_attrib(stencil)) {
  11091. // DEBUG
  11092. if (false) {
  11093. GLCAT.debug() << "STENCIL STATE CHANGE\n";
  11094. GLCAT.debug() << "\n"
  11095. << "SRS_front_comparison_function " << (int)stencil->get_render_state(StencilAttrib::SRS_front_comparison_function) << "\n"
  11096. << "SRS_front_stencil_fail_operation " << (int)stencil->get_render_state(StencilAttrib::SRS_front_stencil_fail_operation) << "\n"
  11097. << "SRS_front_stencil_pass_z_fail_operation " << (int)stencil->get_render_state(StencilAttrib::SRS_front_stencil_pass_z_fail_operation) << "\n"
  11098. << "SRS_front_stencil_pass_z_pass_operation " << (int)stencil->get_render_state(StencilAttrib::SRS_front_stencil_pass_z_pass_operation) << "\n"
  11099. << "SRS_reference " << (int)stencil->get_render_state(StencilAttrib::SRS_reference) << "\n"
  11100. << "SRS_read_mask " << (int)stencil->get_render_state(StencilAttrib::SRS_read_mask) << "\n"
  11101. << "SRS_write_mask " << (int)stencil->get_render_state(StencilAttrib::SRS_write_mask) << "\n"
  11102. << "SRS_back_comparison_function " << (int)stencil->get_render_state(StencilAttrib::SRS_back_comparison_function) << "\n"
  11103. << "SRS_back_stencil_fail_operation " << (int)stencil->get_render_state(StencilAttrib::SRS_back_stencil_fail_operation) << "\n"
  11104. << "SRS_back_stencil_pass_z_fail_operation " << (int)stencil->get_render_state(StencilAttrib::SRS_back_stencil_pass_z_fail_operation) << "\n"
  11105. << "SRS_back_stencil_pass_z_pass_operation " << (int)stencil->get_render_state(StencilAttrib::SRS_back_stencil_pass_z_pass_operation) << "\n";
  11106. }
  11107. #ifndef OPENGLES
  11108. if (_supports_two_sided_stencil) {
  11109. //TODO: add support for OpenGL 2.0-style glStencilFuncSeparate.
  11110. unsigned int back_compare;
  11111. back_compare = stencil->get_render_state(StencilAttrib::SRS_back_comparison_function);
  11112. if (back_compare != RenderAttrib::M_none) {
  11113. glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);
  11114. _glActiveStencilFaceEXT(GL_BACK);
  11115. glStencilFunc(
  11116. PANDA_TO_GL_COMPAREFUNC(back_compare),
  11117. stencil->get_render_state(StencilAttrib::SRS_reference),
  11118. stencil->get_render_state(StencilAttrib::SRS_read_mask));
  11119. glStencilOp(
  11120. gl_stencil_operations_array[stencil->get_render_state(StencilAttrib::SRS_back_stencil_fail_operation)],
  11121. gl_stencil_operations_array[stencil->get_render_state(StencilAttrib::SRS_back_stencil_pass_z_fail_operation)],
  11122. gl_stencil_operations_array[stencil->get_render_state(StencilAttrib::SRS_back_stencil_pass_z_pass_operation)]
  11123. );
  11124. glStencilMask(stencil->get_render_state(StencilAttrib::SRS_write_mask));
  11125. } else {
  11126. glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
  11127. }
  11128. _glActiveStencilFaceEXT(GL_FRONT);
  11129. }
  11130. #endif // OPENGLES
  11131. unsigned int front_compare;
  11132. front_compare = stencil->get_render_state(StencilAttrib::SRS_front_comparison_function);
  11133. if (front_compare != RenderAttrib::M_none) {
  11134. glEnable(GL_STENCIL_TEST);
  11135. glStencilFunc(
  11136. PANDA_TO_GL_COMPAREFUNC(front_compare),
  11137. stencil->get_render_state(StencilAttrib::SRS_reference),
  11138. stencil->get_render_state(StencilAttrib::SRS_read_mask));
  11139. glStencilOp(
  11140. gl_stencil_operations_array[stencil->get_render_state(StencilAttrib::SRS_front_stencil_fail_operation)],
  11141. gl_stencil_operations_array[stencil->get_render_state(StencilAttrib::SRS_front_stencil_pass_z_fail_operation)],
  11142. gl_stencil_operations_array[stencil->get_render_state(StencilAttrib::SRS_front_stencil_pass_z_pass_operation)]
  11143. );
  11144. glStencilMask(stencil->get_render_state(StencilAttrib::SRS_write_mask));
  11145. } else {
  11146. glDisable(GL_STENCIL_TEST);
  11147. }
  11148. if (stencil->get_render_state(StencilAttrib::SRS_clear)) {
  11149. // clear stencil buffer
  11150. glClearStencil(stencil->get_render_state(StencilAttrib::SRS_clear_value));
  11151. glClear(GL_STENCIL_BUFFER_BIT);
  11152. }
  11153. } else {
  11154. glDisable(GL_STENCIL_TEST);
  11155. #ifndef OPENGLES
  11156. if (_supports_two_sided_stencil) {
  11157. glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
  11158. }
  11159. #endif // OPENGLES
  11160. }
  11161. }
  11162. ////////////////////////////////////////////////////////////////////
  11163. // Function: GLGraphicsStateGuardian::do_issue_scissor
  11164. // Access: Protected
  11165. // Description:
  11166. ////////////////////////////////////////////////////////////////////
  11167. void CLP(GraphicsStateGuardian)::
  11168. do_issue_scissor() {
  11169. const ScissorAttrib *target_scissor;
  11170. _target_rs->get_attrib_def(target_scissor);
  11171. if (!target_scissor->is_off()) {
  11172. // A non-off ScissorAttrib means to override the scissor setting
  11173. // that was specified by the DisplayRegion.
  11174. if (!_scissor_enabled) {
  11175. if (GLCAT.is_spam()) {
  11176. GLCAT.spam()
  11177. << "glEnable(GL_SCISSOR_TEST)\n";
  11178. }
  11179. glEnable(GL_SCISSOR_TEST);
  11180. _scissor_enabled = true;
  11181. }
  11182. const LVecBase4 &frame = target_scissor->get_frame();
  11183. int x = (int)(_viewport_x + _viewport_width * frame[0] + 0.5f);
  11184. int y = (int)(_viewport_y + _viewport_height * frame[2] + 0.5f);
  11185. int width = (int)(_viewport_width * (frame[1] - frame[0]) + 0.5f);
  11186. int height = (int)(_viewport_height * (frame[3] - frame[2]) + 0.5f);
  11187. if (GLCAT.is_spam()) {
  11188. GLCAT.spam()
  11189. << "glScissor(" << x << ", " << y << ", " << width << ", " << height << ")\n";
  11190. }
  11191. glScissor(x, y, width, height);
  11192. _scissor_attrib_active = true;
  11193. } else if (_scissor_attrib_active) {
  11194. _scissor_attrib_active = false;
  11195. if (_scissor_array.size() > 0) {
  11196. // Scissoring is enabled on the display region.
  11197. // Revert to the scissor state specified in the DisplayRegion.
  11198. #ifndef OPENGLES
  11199. if (_supports_viewport_arrays) {
  11200. _glScissorArrayv(0, _scissor_array.size(), _scissor_array[0].get_data());
  11201. } else
  11202. #endif // OPENGLES
  11203. {
  11204. const LVecBase4i sr = _scissor_array[0];
  11205. glScissor(sr[0], sr[1], sr[2], sr[3]);
  11206. }
  11207. } else if (_scissor_enabled) {
  11208. // The display region had no scissor enabled. Disable scissoring.
  11209. if (GLCAT.is_spam()) {
  11210. GLCAT.spam()
  11211. << "glDisable(GL_SCISSOR_TEST)\n";
  11212. }
  11213. glDisable(GL_SCISSOR_TEST);
  11214. _scissor_enabled = false;
  11215. }
  11216. }
  11217. }