bloom-horizontal-reconstitute.fs 385 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240
  1. #version 150
  2. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  3. // crt-royale: A full-featured CRT shader, with cheese.
  4. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  5. //
  6. // This program is free software; you can redistribute it and/or modify it
  7. // under the terms of the GNU General Public License as published by the Free
  8. // Software Foundation; either version 2 of the License, or any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful, but WITHOUT
  11. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. // more details.
  14. //
  15. // You should have received a copy of the GNU General Public License along with
  16. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. // Place, Suite 330, Boston, MA 02111-1307 USA
  18. uniform sampler2D source[];
  19. uniform vec4 sourceSize[];
  20. uniform vec4 targetSize;
  21. // Dunno why this stuff didn't want to function in the vertex, but whatever
  22. in Vertex {
  23. vec2 vTexCoord;
  24. // vec2 video_uv;
  25. // vec2 scanline_tex_uv;
  26. // vec2 halation_tex_uv;
  27. // vec2 brightpass_tex_uv;
  28. // vec2 bloom_tex_uv;
  29. vec2 bloom_dxdy;
  30. float bloom_sigma_runtime;
  31. };
  32. vec2 video_uv = vTexCoord;
  33. vec2 scanline_tex_uv = vTexCoord;
  34. vec2 halation_tex_uv = vTexCoord;
  35. vec2 brightpass_tex_uv = vTexCoord;
  36. vec2 bloom_tex_uv = vTexCoord;
  37. out vec4 FragColor;
  38. // USER SETTINGS BLOCK //
  39. #define crt_gamma 2.500000
  40. #define lcd_gamma 2.200000
  41. #define levels_contrast 1.0
  42. #define halation_weight 0.0
  43. #define diffusion_weight 0.075
  44. #define bloom_underestimate_levels 0.8
  45. #define bloom_excess 0.000000
  46. #define beam_min_sigma 0.020000
  47. #define beam_max_sigma 0.300000
  48. #define beam_spot_power 0.330000
  49. #define beam_min_shape 2.000000
  50. #define beam_max_shape 4.000000
  51. #define beam_shape_power 0.250000
  52. #define beam_horiz_filter 0.000000
  53. #define beam_horiz_sigma 0.35
  54. #define beam_horiz_linear_rgb_weight 1.000000
  55. #define convergence_offset_x_r -0.000000
  56. #define convergence_offset_x_g 0.000000
  57. #define convergence_offset_x_b 0.000000
  58. #define convergence_offset_y_r 0.000000
  59. #define convergence_offset_y_g -0.000000
  60. #define convergence_offset_y_b 0.000000
  61. #define mask_type 1.000000
  62. #define mask_sample_mode_desired 0.000000
  63. #define mask_specify_num_triads 0.000000
  64. #define mask_triad_size_desired 3.000000
  65. #define mask_num_triads_desired 480.000000
  66. #define aa_subpixel_r_offset_x_runtime -0.0
  67. #define aa_subpixel_r_offset_y_runtime 0.000000
  68. #define aa_cubic_c 0.500000
  69. #define aa_gauss_sigma 0.500000
  70. #define geom_mode_runtime 0.000000
  71. #define geom_radius 2.000000
  72. #define geom_view_dist 2.000000
  73. #define geom_tilt_angle_x 0.000000
  74. #define geom_tilt_angle_y 0.000000
  75. #define geom_aspect_ratio_x 432.000000
  76. #define geom_aspect_ratio_y 329.000000
  77. #define geom_overscan_x 1.000000
  78. #define geom_overscan_y 1.000000
  79. #define border_size 0.015
  80. #define border_darkness 2.0
  81. #define border_compress 2.500000
  82. #define interlace_bff 0.000000
  83. #define interlace_1080i 0.000000
  84. // END USER SETTINGS BLOCK //
  85. // compatibility macros for transparently converting HLSLisms into GLSLisms
  86. #define mul(a,b) (b*a)
  87. #define lerp(a,b,c) mix(a,b,c)
  88. #define saturate(c) clamp(c, 0.0, 1.0)
  89. #define frac(x) (fract(x))
  90. #define float2 vec2
  91. #define float3 vec3
  92. #define float4 vec4
  93. #define bool2 bvec2
  94. #define bool3 bvec3
  95. #define bool4 bvec4
  96. #define float2x2 mat2x2
  97. #define float3x3 mat3x3
  98. #define float4x4 mat4x4
  99. #define float4x3 mat4x3
  100. #define float2x4 mat2x4
  101. #define IN params
  102. #define texture_size sourceSize[0].xy
  103. #define video_size sourceSize[0].xy
  104. #define output_size targetSize.xy
  105. #define frame_count phase
  106. #define static
  107. #define inline
  108. #define const
  109. #define fmod(x,y) mod(x,y)
  110. #define ddx(c) dFdx(c)
  111. #define ddy(c) dFdy(c)
  112. #define atan2(x,y) atan(y,x)
  113. #define rsqrt(c) inversesqrt(c)
  114. #define bloom_texture source[0]
  115. #define MASKED_SCANLINEStexture source[2]
  116. #define MASKED_SCANLINEStexture_size sourceSize[2].xy
  117. #define MASKED_SCANLINESvideo_size sourceSize[2].xy
  118. #define HALATION_BLURtexture source[5]
  119. #define HALATION_BLURtexture_size sourceSize[5].xy
  120. #define HALATION_BLURvideo_size sourceSize[5].xy
  121. #define BRIGHTPASStexture source[1]
  122. #define BRIGHTPASStexture_size sourceSize[1].xy
  123. #define BRIGHTPASSvideo_size sourceSize[1].xy
  124. #if defined(GL_ES)
  125. #define COMPAT_PRECISION mediump
  126. #else
  127. #define COMPAT_PRECISION
  128. #endif
  129. #if __VERSION__ >= 130
  130. #define COMPAT_TEXTURE texture
  131. #else
  132. #define COMPAT_TEXTURE texture2D
  133. #endif
  134. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  135. //#include "../user-settings.h"
  136. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  137. #ifndef USER_SETTINGS_H
  138. #define USER_SETTINGS_H
  139. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  140. // The Cg compiler uses different "profiles" with different capabilities.
  141. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  142. // require higher profiles like fp30 or fp40. The shader can't detect profile
  143. // or driver capabilities, so instead you must comment or uncomment the lines
  144. // below with "//" before "#define." Disable an option if you get compilation
  145. // errors resembling those listed. Generally speaking, all of these options
  146. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  147. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  148. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  149. // Among other things, derivatives help us fix anisotropic filtering artifacts
  150. // with curved manually tiled phosphor mask coords. Related errors:
  151. // error C3004: function "float2 ddx(float2);" not supported in this profile
  152. // error C3004: function "float2 ddy(float2);" not supported in this profile
  153. //#define DRIVERS_ALLOW_DERIVATIVES
  154. // Fine derivatives: Unsupported on older ATI cards.
  155. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  156. // fast single-pass blur operations. If your card uses coarse derivatives and
  157. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  158. #ifdef DRIVERS_ALLOW_DERIVATIVES
  159. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  160. #endif
  161. // Dynamic looping: Requires an fp30 or newer profile.
  162. // This makes phosphor mask resampling faster in some cases. Related errors:
  163. // error C5013: profile does not support "for" statements and "for" could not
  164. // be unrolled
  165. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  166. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  167. // Using one static loop avoids overhead if the user is right, but if the user
  168. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  169. // binary search can potentially save some iterations. However, it may fail:
  170. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  171. // needed to compile program
  172. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  173. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  174. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  175. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  176. // this profile
  177. //#define DRIVERS_ALLOW_TEX2DLOD
  178. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  179. // artifacts from anisotropic filtering and mipmapping. Related errors:
  180. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  181. // in this profile
  182. //#define DRIVERS_ALLOW_TEX2DBIAS
  183. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  184. // impose stricter limitations on register counts and instructions. Enable
  185. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  186. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  187. // to compile program.
  188. // Enabling integrated graphics compatibility mode will automatically disable:
  189. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  190. // (This may be reenabled in a later release.)
  191. // 2.) RUNTIME_GEOMETRY_MODE
  192. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  193. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  194. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  195. // To disable a #define option, turn its line into a comment with "//."
  196. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  197. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  198. // many of the options in this file and allow real-time tuning, but many of
  199. // them are slower. Disabling them and using this text file will boost FPS.
  200. #define RUNTIME_SHADER_PARAMS_ENABLE
  201. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  202. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  203. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  204. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  205. #define RUNTIME_ANTIALIAS_WEIGHTS
  206. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  207. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  208. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  209. // parameters? This will require more math or dynamic branching.
  210. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  211. // Specify the tilt at runtime? This makes things about 3% slower.
  212. #define RUNTIME_GEOMETRY_TILT
  213. // Specify the geometry mode at runtime?
  214. #define RUNTIME_GEOMETRY_MODE
  215. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  216. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  217. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  218. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  219. // PHOSPHOR MASK:
  220. // Manually resize the phosphor mask for best results (slower)? Disabling this
  221. // removes the option to do so, but it may be faster without dynamic branches.
  222. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  223. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  224. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  225. // Larger blurs are expensive, but we need them to blur larger triads. We can
  226. // detect the right blur if the triad size is static or our profile allows
  227. // dynamic branches, but otherwise we use the largest blur the user indicates
  228. // they might need:
  229. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  230. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  231. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  232. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  233. // Here's a helpful chart:
  234. // MaxTriadSize BlurSize MinTriadCountsByResolution
  235. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  236. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  237. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  238. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  239. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  240. /////////////////////////////// USER PARAMETERS //////////////////////////////
  241. // Note: Many of these static parameters are overridden by runtime shader
  242. // parameters when those are enabled. However, many others are static codepath
  243. // options that were cleaner or more convert to code as static constants.
  244. // GAMMA:
  245. static const float crt_gamma_static = 2.5; // range [1, 5]
  246. static const float lcd_gamma_static = 2.2; // range [1, 5]
  247. // LEVELS MANAGEMENT:
  248. // Control the final multiplicative image contrast:
  249. static const float levels_contrast_static = 1.0; // range [0, 4)
  250. // We auto-dim to avoid clipping between passes and restore brightness
  251. // later. Control the dim factor here: Lower values clip less but crush
  252. // blacks more (static only for now).
  253. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  254. // HALATION/DIFFUSION/BLOOM:
  255. // Halation weight: How much energy should be lost to electrons bounding
  256. // around under the CRT glass and exciting random phosphors?
  257. static const float halation_weight_static = 0.0; // range [0, 1]
  258. // Refractive diffusion weight: How much light should spread/diffuse from
  259. // refracting through the CRT glass?
  260. static const float diffusion_weight_static = 0.075; // range [0, 1]
  261. // Underestimate brightness: Bright areas bloom more, but we can base the
  262. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  263. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  264. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  265. // Blur all colors more than necessary for a softer phosphor bloom?
  266. static const float bloom_excess_static = 0.0; // range [0, 1]
  267. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  268. // blurred resize of the input (convergence offsets are applied as well).
  269. // There are three filter options (static option only for now):
  270. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  271. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  272. // and beam_max_sigma is low.
  273. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  274. // always uses a static sigma regardless of beam_max_sigma or
  275. // mask_num_triads_desired.
  276. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  277. // These options are more pronounced for the fast, unbloomed shader version.
  278. #ifndef RADEON_FIX
  279. static const float bloom_approx_filter_static = 2.0;
  280. #else
  281. static const float bloom_approx_filter_static = 1.0;
  282. #endif
  283. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  284. // How many scanlines should contribute light to each pixel? Using more
  285. // scanlines is slower (especially for a generalized Gaussian) but less
  286. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  287. // max_beam_sigma at which the closest unused weight is guaranteed <
  288. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  289. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  290. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  291. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  292. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  293. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  294. static const float beam_num_scanlines = 3.0; // range [2, 6]
  295. // A generalized Gaussian beam varies shape with color too, now just width.
  296. // It's slower but more flexible (static option only for now).
  297. static const bool beam_generalized_gaussian = true;
  298. // What kind of scanline antialiasing do you want?
  299. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  300. // Integrals are slow (especially for generalized Gaussians) and rarely any
  301. // better than 3x antialiasing (static option only for now).
  302. static const float beam_antialias_level = 1.0; // range [0, 2]
  303. // Min/max standard deviations for scanline beams: Higher values widen and
  304. // soften scanlines. Depending on other options, low min sigmas can alias.
  305. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  306. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  307. // Beam width varies as a function of color: A power function (0) is more
  308. // configurable, but a spherical function (1) gives the widest beam
  309. // variability without aliasing (static option only for now).
  310. static const float beam_spot_shape_function = 0.0;
  311. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  312. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  313. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  314. // Generalized Gaussian max shape parameters: Higher values give flatter
  315. // scanline plateaus and steeper dropoffs, simultaneously widening and
  316. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  317. // values > ~40.0 cause artifacts with integrals.
  318. static const float beam_min_shape_static = 2.0; // range [2, 32]
  319. static const float beam_max_shape_static = 4.0; // range [2, 32]
  320. // Generalized Gaussian shape power: Affects how quickly the distribution
  321. // changes shape from Gaussian to steep/plateaued as color increases from 0
  322. // to 1.0. Higher powers appear softer for most colors, and lower powers
  323. // appear sharper for most colors.
  324. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  325. // What filter should be used to sample scanlines horizontally?
  326. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  327. static const float beam_horiz_filter_static = 0.0;
  328. // Standard deviation for horizontal Gaussian resampling:
  329. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  330. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  331. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  332. // limiting circuitry in some CRT's), or a weighted avg.?
  333. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  334. // Simulate scanline misconvergence? This needs 3x horizontal texture
  335. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  336. // later passes (static option only for now).
  337. static const bool beam_misconvergence = true;
  338. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  339. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  340. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  341. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  342. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  343. // Detect interlacing (static option only for now)?
  344. static const bool interlace_detect = true;
  345. // Assume 1080-line sources are interlaced?
  346. static const bool interlace_1080i_static = false;
  347. // For interlaced sources, assume TFF (top-field first) or BFF order?
  348. // (Whether this matters depends on the nature of the interlaced input.)
  349. static const bool interlace_bff_static = false;
  350. // ANTIALIASING:
  351. // What AA level do you want for curvature/overscan/subpixels? Options:
  352. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  353. // (Static option only for now)
  354. static const float aa_level = 12.0; // range [0, 24]
  355. // What antialiasing filter do you want (static option only)? Options:
  356. // 0: Box (separable), 1: Box (cylindrical),
  357. // 2: Tent (separable), 3: Tent (cylindrical),
  358. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  359. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  360. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  361. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  362. static const float aa_filter = 6.0; // range [0, 9]
  363. // Flip the sample grid on odd/even frames (static option only for now)?
  364. static const bool aa_temporal = false;
  365. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  366. // the blue offset is the negative r offset; range [0, 0.5]
  367. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  368. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  369. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  370. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  371. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  372. // 4.) C = 0.0 is a soft spline filter.
  373. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  374. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  375. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  376. // PHOSPHOR MASK:
  377. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  378. static const float mask_type_static = 1.0; // range [0, 2]
  379. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  380. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  381. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  382. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  383. // is halfway decent with LUT mipmapping but atrocious without it.
  384. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  385. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  386. // This mode reuses the same masks, so triads will be enormous unless
  387. // you change the mask LUT filenames in your .cgp file.
  388. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  389. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  390. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  391. // will always be used to calculate the full bloom sigma statically.
  392. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  393. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  394. // triads) will be rounded to the nearest integer tile size and clamped to
  395. // obey minimum size constraints (imposed to reduce downsize taps) and
  396. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  397. // To increase the size limit, double the viewport-relative scales for the
  398. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  399. // range [1, mask_texture_small_size/mask_triads_per_tile]
  400. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  401. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  402. // final size will be rounded and constrained as above); default 480.0
  403. static const float mask_num_triads_desired_static = 480.0;
  404. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  405. // more samples and avoid moire a bit better, but some is unavoidable
  406. // depending on the destination size (static option for now).
  407. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  408. // The mask is resized using a variable number of taps in each dimension,
  409. // but some Cg profiles always fetch a constant number of taps no matter
  410. // what (no dynamic branching). We can limit the maximum number of taps if
  411. // we statically limit the minimum phosphor triad size. Larger values are
  412. // faster, but the limit IS enforced (static option only, forever);
  413. // range [1, mask_texture_small_size/mask_triads_per_tile]
  414. // TODO: Make this 1.0 and compensate with smarter sampling!
  415. static const float mask_min_allowed_triad_size = 2.0;
  416. // GEOMETRY:
  417. // Geometry mode:
  418. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  419. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  420. static const float geom_mode_static = 0.0; // range [0, 3]
  421. // Radius of curvature: Measured in units of your viewport's diagonal size.
  422. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  423. // View dist is the distance from the player to their physical screen, in
  424. // units of the viewport's diagonal size. It controls the field of view.
  425. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  426. // Tilt angle in radians (clockwise around up and right vectors):
  427. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  428. // Aspect ratio: When the true viewport size is unknown, this value is used
  429. // to help convert between the phosphor triad size and count, along with
  430. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  431. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  432. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  433. // default (256/224)*(54/47) = 1.313069909 (see below)
  434. static const float geom_aspect_ratio_static = 1.313069909;
  435. // Before getting into overscan, here's some general aspect ratio info:
  436. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  437. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  438. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  439. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  440. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  441. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  442. // a.) Enable Retroarch's "Crop Overscan"
  443. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  444. // Real consoles use horizontal black padding in the signal, but emulators
  445. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  446. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  447. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  448. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  449. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  450. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  451. // without doing a. or b., but horizontal image borders will be tighter
  452. // than vertical ones, messing up curvature and overscan. Fixing the
  453. // padding first corrects this.
  454. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  455. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  456. // above: Values < 1.0 zoom out; range (0, inf)
  457. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  458. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  459. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  460. // with strong curvature (static option only for now).
  461. static const bool geom_force_correct_tangent_matrix = true;
  462. // BORDERS:
  463. // Rounded border size in texture uv coords:
  464. static const float border_size_static = 0.015; // range [0, 0.5]
  465. // Border darkness: Moderate values darken the border smoothly, and high
  466. // values make the image very dark just inside the border:
  467. static const float border_darkness_static = 2.0; // range [0, inf)
  468. // Border compression: High numbers compress border transitions, narrowing
  469. // the dark border area.
  470. static const float border_compress_static = 2.5; // range [1, inf)
  471. #endif // USER_SETTINGS_H
  472. //////////////////////////// END USER-SETTINGS //////////////////////////
  473. //#include "derived-settings-and-constants.h"
  474. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  475. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  476. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  477. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  478. // crt-royale: A full-featured CRT shader, with cheese.
  479. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  480. //
  481. // This program is free software; you can redistribute it and/or modify it
  482. // under the terms of the GNU General Public License as published by the Free
  483. // Software Foundation; either version 2 of the License, or any later version.
  484. //
  485. // This program is distributed in the hope that it will be useful, but WITHOUT
  486. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  487. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  488. // more details.
  489. //
  490. // You should have received a copy of the GNU General Public License along with
  491. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  492. // Place, Suite 330, Boston, MA 02111-1307 USA
  493. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  494. // These macros and constants can be used across the whole codebase.
  495. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  496. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  497. //#include "../user-settings.h"
  498. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  499. #ifndef USER_SETTINGS_H
  500. #define USER_SETTINGS_H
  501. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  502. // The Cg compiler uses different "profiles" with different capabilities.
  503. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  504. // require higher profiles like fp30 or fp40. The shader can't detect profile
  505. // or driver capabilities, so instead you must comment or uncomment the lines
  506. // below with "//" before "#define." Disable an option if you get compilation
  507. // errors resembling those listed. Generally speaking, all of these options
  508. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  509. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  510. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  511. // Among other things, derivatives help us fix anisotropic filtering artifacts
  512. // with curved manually tiled phosphor mask coords. Related errors:
  513. // error C3004: function "float2 ddx(float2);" not supported in this profile
  514. // error C3004: function "float2 ddy(float2);" not supported in this profile
  515. //#define DRIVERS_ALLOW_DERIVATIVES
  516. // Fine derivatives: Unsupported on older ATI cards.
  517. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  518. // fast single-pass blur operations. If your card uses coarse derivatives and
  519. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  520. #ifdef DRIVERS_ALLOW_DERIVATIVES
  521. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  522. #endif
  523. // Dynamic looping: Requires an fp30 or newer profile.
  524. // This makes phosphor mask resampling faster in some cases. Related errors:
  525. // error C5013: profile does not support "for" statements and "for" could not
  526. // be unrolled
  527. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  528. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  529. // Using one static loop avoids overhead if the user is right, but if the user
  530. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  531. // binary search can potentially save some iterations. However, it may fail:
  532. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  533. // needed to compile program
  534. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  535. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  536. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  537. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  538. // this profile
  539. //#define DRIVERS_ALLOW_TEX2DLOD
  540. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  541. // artifacts from anisotropic filtering and mipmapping. Related errors:
  542. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  543. // in this profile
  544. //#define DRIVERS_ALLOW_TEX2DBIAS
  545. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  546. // impose stricter limitations on register counts and instructions. Enable
  547. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  548. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  549. // to compile program.
  550. // Enabling integrated graphics compatibility mode will automatically disable:
  551. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  552. // (This may be reenabled in a later release.)
  553. // 2.) RUNTIME_GEOMETRY_MODE
  554. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  555. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  556. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  557. // To disable a #define option, turn its line into a comment with "//."
  558. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  559. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  560. // many of the options in this file and allow real-time tuning, but many of
  561. // them are slower. Disabling them and using this text file will boost FPS.
  562. #define RUNTIME_SHADER_PARAMS_ENABLE
  563. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  564. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  565. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  566. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  567. #define RUNTIME_ANTIALIAS_WEIGHTS
  568. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  569. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  570. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  571. // parameters? This will require more math or dynamic branching.
  572. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  573. // Specify the tilt at runtime? This makes things about 3% slower.
  574. #define RUNTIME_GEOMETRY_TILT
  575. // Specify the geometry mode at runtime?
  576. #define RUNTIME_GEOMETRY_MODE
  577. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  578. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  579. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  580. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  581. // PHOSPHOR MASK:
  582. // Manually resize the phosphor mask for best results (slower)? Disabling this
  583. // removes the option to do so, but it may be faster without dynamic branches.
  584. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  585. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  586. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  587. // Larger blurs are expensive, but we need them to blur larger triads. We can
  588. // detect the right blur if the triad size is static or our profile allows
  589. // dynamic branches, but otherwise we use the largest blur the user indicates
  590. // they might need:
  591. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  592. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  593. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  594. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  595. // Here's a helpful chart:
  596. // MaxTriadSize BlurSize MinTriadCountsByResolution
  597. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  598. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  599. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  600. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  601. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  602. /////////////////////////////// USER PARAMETERS //////////////////////////////
  603. // Note: Many of these static parameters are overridden by runtime shader
  604. // parameters when those are enabled. However, many others are static codepath
  605. // options that were cleaner or more convert to code as static constants.
  606. // GAMMA:
  607. static const float crt_gamma_static = 2.5; // range [1, 5]
  608. static const float lcd_gamma_static = 2.2; // range [1, 5]
  609. // LEVELS MANAGEMENT:
  610. // Control the final multiplicative image contrast:
  611. static const float levels_contrast_static = 1.0; // range [0, 4)
  612. // We auto-dim to avoid clipping between passes and restore brightness
  613. // later. Control the dim factor here: Lower values clip less but crush
  614. // blacks more (static only for now).
  615. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  616. // HALATION/DIFFUSION/BLOOM:
  617. // Halation weight: How much energy should be lost to electrons bounding
  618. // around under the CRT glass and exciting random phosphors?
  619. static const float halation_weight_static = 0.0; // range [0, 1]
  620. // Refractive diffusion weight: How much light should spread/diffuse from
  621. // refracting through the CRT glass?
  622. static const float diffusion_weight_static = 0.075; // range [0, 1]
  623. // Underestimate brightness: Bright areas bloom more, but we can base the
  624. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  625. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  626. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  627. // Blur all colors more than necessary for a softer phosphor bloom?
  628. static const float bloom_excess_static = 0.0; // range [0, 1]
  629. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  630. // blurred resize of the input (convergence offsets are applied as well).
  631. // There are three filter options (static option only for now):
  632. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  633. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  634. // and beam_max_sigma is low.
  635. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  636. // always uses a static sigma regardless of beam_max_sigma or
  637. // mask_num_triads_desired.
  638. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  639. // These options are more pronounced for the fast, unbloomed shader version.
  640. #ifndef RADEON_FIX
  641. static const float bloom_approx_filter_static = 2.0;
  642. #else
  643. static const float bloom_approx_filter_static = 1.0;
  644. #endif
  645. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  646. // How many scanlines should contribute light to each pixel? Using more
  647. // scanlines is slower (especially for a generalized Gaussian) but less
  648. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  649. // max_beam_sigma at which the closest unused weight is guaranteed <
  650. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  651. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  652. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  653. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  654. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  655. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  656. static const float beam_num_scanlines = 3.0; // range [2, 6]
  657. // A generalized Gaussian beam varies shape with color too, now just width.
  658. // It's slower but more flexible (static option only for now).
  659. static const bool beam_generalized_gaussian = true;
  660. // What kind of scanline antialiasing do you want?
  661. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  662. // Integrals are slow (especially for generalized Gaussians) and rarely any
  663. // better than 3x antialiasing (static option only for now).
  664. static const float beam_antialias_level = 1.0; // range [0, 2]
  665. // Min/max standard deviations for scanline beams: Higher values widen and
  666. // soften scanlines. Depending on other options, low min sigmas can alias.
  667. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  668. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  669. // Beam width varies as a function of color: A power function (0) is more
  670. // configurable, but a spherical function (1) gives the widest beam
  671. // variability without aliasing (static option only for now).
  672. static const float beam_spot_shape_function = 0.0;
  673. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  674. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  675. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  676. // Generalized Gaussian max shape parameters: Higher values give flatter
  677. // scanline plateaus and steeper dropoffs, simultaneously widening and
  678. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  679. // values > ~40.0 cause artifacts with integrals.
  680. static const float beam_min_shape_static = 2.0; // range [2, 32]
  681. static const float beam_max_shape_static = 4.0; // range [2, 32]
  682. // Generalized Gaussian shape power: Affects how quickly the distribution
  683. // changes shape from Gaussian to steep/plateaued as color increases from 0
  684. // to 1.0. Higher powers appear softer for most colors, and lower powers
  685. // appear sharper for most colors.
  686. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  687. // What filter should be used to sample scanlines horizontally?
  688. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  689. static const float beam_horiz_filter_static = 0.0;
  690. // Standard deviation for horizontal Gaussian resampling:
  691. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  692. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  693. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  694. // limiting circuitry in some CRT's), or a weighted avg.?
  695. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  696. // Simulate scanline misconvergence? This needs 3x horizontal texture
  697. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  698. // later passes (static option only for now).
  699. static const bool beam_misconvergence = true;
  700. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  701. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  702. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  703. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  704. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  705. // Detect interlacing (static option only for now)?
  706. static const bool interlace_detect = true;
  707. // Assume 1080-line sources are interlaced?
  708. static const bool interlace_1080i_static = false;
  709. // For interlaced sources, assume TFF (top-field first) or BFF order?
  710. // (Whether this matters depends on the nature of the interlaced input.)
  711. static const bool interlace_bff_static = false;
  712. // ANTIALIASING:
  713. // What AA level do you want for curvature/overscan/subpixels? Options:
  714. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  715. // (Static option only for now)
  716. static const float aa_level = 12.0; // range [0, 24]
  717. // What antialiasing filter do you want (static option only)? Options:
  718. // 0: Box (separable), 1: Box (cylindrical),
  719. // 2: Tent (separable), 3: Tent (cylindrical),
  720. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  721. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  722. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  723. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  724. static const float aa_filter = 6.0; // range [0, 9]
  725. // Flip the sample grid on odd/even frames (static option only for now)?
  726. static const bool aa_temporal = false;
  727. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  728. // the blue offset is the negative r offset; range [0, 0.5]
  729. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  730. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  731. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  732. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  733. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  734. // 4.) C = 0.0 is a soft spline filter.
  735. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  736. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  737. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  738. // PHOSPHOR MASK:
  739. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  740. static const float mask_type_static = 1.0; // range [0, 2]
  741. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  742. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  743. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  744. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  745. // is halfway decent with LUT mipmapping but atrocious without it.
  746. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  747. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  748. // This mode reuses the same masks, so triads will be enormous unless
  749. // you change the mask LUT filenames in your .cgp file.
  750. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  751. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  752. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  753. // will always be used to calculate the full bloom sigma statically.
  754. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  755. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  756. // triads) will be rounded to the nearest integer tile size and clamped to
  757. // obey minimum size constraints (imposed to reduce downsize taps) and
  758. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  759. // To increase the size limit, double the viewport-relative scales for the
  760. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  761. // range [1, mask_texture_small_size/mask_triads_per_tile]
  762. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  763. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  764. // final size will be rounded and constrained as above); default 480.0
  765. static const float mask_num_triads_desired_static = 480.0;
  766. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  767. // more samples and avoid moire a bit better, but some is unavoidable
  768. // depending on the destination size (static option for now).
  769. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  770. // The mask is resized using a variable number of taps in each dimension,
  771. // but some Cg profiles always fetch a constant number of taps no matter
  772. // what (no dynamic branching). We can limit the maximum number of taps if
  773. // we statically limit the minimum phosphor triad size. Larger values are
  774. // faster, but the limit IS enforced (static option only, forever);
  775. // range [1, mask_texture_small_size/mask_triads_per_tile]
  776. // TODO: Make this 1.0 and compensate with smarter sampling!
  777. static const float mask_min_allowed_triad_size = 2.0;
  778. // GEOMETRY:
  779. // Geometry mode:
  780. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  781. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  782. static const float geom_mode_static = 0.0; // range [0, 3]
  783. // Radius of curvature: Measured in units of your viewport's diagonal size.
  784. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  785. // View dist is the distance from the player to their physical screen, in
  786. // units of the viewport's diagonal size. It controls the field of view.
  787. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  788. // Tilt angle in radians (clockwise around up and right vectors):
  789. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  790. // Aspect ratio: When the true viewport size is unknown, this value is used
  791. // to help convert between the phosphor triad size and count, along with
  792. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  793. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  794. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  795. // default (256/224)*(54/47) = 1.313069909 (see below)
  796. static const float geom_aspect_ratio_static = 1.313069909;
  797. // Before getting into overscan, here's some general aspect ratio info:
  798. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  799. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  800. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  801. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  802. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  803. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  804. // a.) Enable Retroarch's "Crop Overscan"
  805. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  806. // Real consoles use horizontal black padding in the signal, but emulators
  807. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  808. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  809. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  810. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  811. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  812. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  813. // without doing a. or b., but horizontal image borders will be tighter
  814. // than vertical ones, messing up curvature and overscan. Fixing the
  815. // padding first corrects this.
  816. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  817. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  818. // above: Values < 1.0 zoom out; range (0, inf)
  819. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  820. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  821. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  822. // with strong curvature (static option only for now).
  823. static const bool geom_force_correct_tangent_matrix = true;
  824. // BORDERS:
  825. // Rounded border size in texture uv coords:
  826. static const float border_size_static = 0.015; // range [0, 0.5]
  827. // Border darkness: Moderate values darken the border smoothly, and high
  828. // values make the image very dark just inside the border:
  829. static const float border_darkness_static = 2.0; // range [0, inf)
  830. // Border compression: High numbers compress border transitions, narrowing
  831. // the dark border area.
  832. static const float border_compress_static = 2.5; // range [1, inf)
  833. #endif // USER_SETTINGS_H
  834. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  835. //#include "user-cgp-constants.h"
  836. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  837. #ifndef USER_CGP_CONSTANTS_H
  838. #define USER_CGP_CONSTANTS_H
  839. // IMPORTANT:
  840. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  841. // (or whatever related .cgp file you're using). If they aren't, you're likely
  842. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  843. // set directly in the .cgp file to make things easier, but...they can't.
  844. // PASS SCALES AND RELATED CONSTANTS:
  845. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  846. // this shader: One does a viewport-scale bloom, and the other skips it. The
  847. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  848. static const float bloom_approx_size_x = 320.0;
  849. static const float bloom_approx_size_x_for_fake = 400.0;
  850. // Copy the viewport-relative scales of the phosphor mask resize passes
  851. // (MASK_RESIZE and the pass immediately preceding it):
  852. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  853. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  854. static const float geom_max_aspect_ratio = 4.0/3.0;
  855. // PHOSPHOR MASK TEXTURE CONSTANTS:
  856. // Set the following constants to reflect the properties of the phosphor mask
  857. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  858. // based on user settings, then repeats a single tile until filling the screen.
  859. // The shader must know the input texture size (default 64x64), and to manually
  860. // resize, it must also know the horizontal triads per tile (default 8).
  861. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  862. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  863. static const float mask_triads_per_tile = 8.0;
  864. // We need the average brightness of the phosphor mask to compensate for the
  865. // dimming it causes. The following four values are roughly correct for the
  866. // masks included with the shader. Update the value for any LUT texture you
  867. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  868. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  869. //#define PHOSPHOR_MASK_GRILLE14
  870. static const float mask_grille14_avg_color = 50.6666666/255.0;
  871. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  872. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  873. static const float mask_grille15_avg_color = 53.0/255.0;
  874. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  875. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  876. static const float mask_slot_avg_color = 46.0/255.0;
  877. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  878. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  879. static const float mask_shadow_avg_color = 41.0/255.0;
  880. // TileableLinearShadowMask*.png
  881. // TileableLinearShadowMaskEDP*.png
  882. #ifdef PHOSPHOR_MASK_GRILLE14
  883. static const float mask_grille_avg_color = mask_grille14_avg_color;
  884. #else
  885. static const float mask_grille_avg_color = mask_grille15_avg_color;
  886. #endif
  887. #endif // USER_CGP_CONSTANTS_H
  888. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  889. //////////////////////////////// END INCLUDES ////////////////////////////////
  890. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  891. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  892. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  893. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  894. #ifndef SIMULATE_CRT_ON_LCD
  895. #define SIMULATE_CRT_ON_LCD
  896. #endif
  897. // Manually tiling a manually resized texture creates texture coord derivative
  898. // discontinuities and confuses anisotropic filtering, causing discolored tile
  899. // seams in the phosphor mask. Workarounds:
  900. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  901. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  902. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  903. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  904. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  905. // (Same-pass curvature isn't used but could be in the future...maybe.)
  906. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  907. // border padding to the resized mask FBO, but it works with same-pass
  908. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  909. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  910. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  911. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  912. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  913. // Also, manually resampling the phosphor mask is slightly blurrier with
  914. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  915. // creates artifacts, but only with the fully bloomed shader.) The difference
  916. // is subtle with small triads, but you can fix it for a small cost.
  917. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  918. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  919. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  920. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  921. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  922. // #defined by either user-settings.h or a wrapper .cg that #includes the
  923. // current .cg pass.)
  924. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  925. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  926. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  927. #endif
  928. #ifdef RUNTIME_GEOMETRY_MODE
  929. #undef RUNTIME_GEOMETRY_MODE
  930. #endif
  931. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  932. // inferior in most cases, so replace 2.0 with 0.0:
  933. static const float bloom_approx_filter =
  934. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  935. #else
  936. static const float bloom_approx_filter = bloom_approx_filter_static;
  937. #endif
  938. // Disable slow runtime paths if static parameters are used. Most of these
  939. // won't be a problem anyway once the params are disabled, but some will.
  940. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  941. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  942. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  943. #endif
  944. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  945. #undef RUNTIME_ANTIALIAS_WEIGHTS
  946. #endif
  947. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  948. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  949. #endif
  950. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  951. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  952. #endif
  953. #ifdef RUNTIME_GEOMETRY_TILT
  954. #undef RUNTIME_GEOMETRY_TILT
  955. #endif
  956. #ifdef RUNTIME_GEOMETRY_MODE
  957. #undef RUNTIME_GEOMETRY_MODE
  958. #endif
  959. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  960. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  961. #endif
  962. #endif
  963. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  964. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  965. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  966. #endif
  967. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  968. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  969. #endif
  970. // Rule out unavailable anisotropic compatibility strategies:
  971. #ifndef DRIVERS_ALLOW_DERIVATIVES
  972. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  973. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  974. #endif
  975. #endif
  976. #ifndef DRIVERS_ALLOW_TEX2DLOD
  977. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  978. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  979. #endif
  980. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  981. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  982. #endif
  983. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  984. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  985. #endif
  986. #endif
  987. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  988. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  989. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  990. #endif
  991. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  992. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  993. #endif
  994. #endif
  995. // Prioritize anisotropic tiling compatibility strategies by performance and
  996. // disable unused strategies. This concentrates all the nesting in one place.
  997. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  998. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  999. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1000. #endif
  1001. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1002. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1003. #endif
  1004. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1005. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1006. #endif
  1007. #else
  1008. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1009. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1010. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1011. #endif
  1012. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1013. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1014. #endif
  1015. #else
  1016. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  1017. // flat texture coords in the same pass, but that's all we use.
  1018. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1019. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1020. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1021. #endif
  1022. #endif
  1023. #endif
  1024. #endif
  1025. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  1026. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  1027. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1028. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1029. #endif
  1030. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1031. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1032. #endif
  1033. // Prioritize anisotropic resampling compatibility strategies the same way:
  1034. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1035. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1036. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1037. #endif
  1038. #endif
  1039. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  1040. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  1041. // should: It gives us more options for using fewer samples.
  1042. #ifdef DRIVERS_ALLOW_TEX2DLOD
  1043. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1044. // TODO: Take advantage of this!
  1045. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  1046. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  1047. #else
  1048. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1049. #endif
  1050. #else
  1051. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1052. #endif
  1053. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  1054. // main_fragment, or a static alias of one of the above. This makes it hard
  1055. // to select the phosphor mask at runtime: We can't even assign to a uniform
  1056. // global in the vertex shader or select a sampler2D in the vertex shader and
  1057. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  1058. // because it just gives us the input texture or a black screen. However, we
  1059. // can get around these limitations by calling tex2D three times with different
  1060. // uniform samplers (or resizing the phosphor mask three times altogether).
  1061. // With dynamic branches, we can process only one of these branches on top of
  1062. // quickly discarding fragments we don't need (cgc seems able to overcome
  1063. // limigations around dependent texture fetches inside of branches). Without
  1064. // dynamic branches, we have to process every branch for every fragment...which
  1065. // is slower. Runtime sampling mode selection is slower without dynamic
  1066. // branches as well. Let the user's static #defines decide if it's worth it.
  1067. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1068. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1069. #else
  1070. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1071. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1072. #endif
  1073. #endif
  1074. // We need to render some minimum number of tiles in the resize passes.
  1075. // We need at least 1.0 just to repeat a single tile, and we need extra
  1076. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  1077. // antialiasing, same-pass curvature (not currently used), etc. First
  1078. // determine how many border texels and tiles we need, based on how the result
  1079. // will be sampled:
  1080. #ifdef GEOMETRY_EARLY
  1081. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  1082. // Most antialiasing filters have a base radius of 4.0 pixels:
  1083. static const float max_aa_base_pixel_border = 4.0 +
  1084. max_subpixel_offset;
  1085. #else
  1086. static const float max_aa_base_pixel_border = 0.0;
  1087. #endif
  1088. // Anisotropic filtering adds about 0.5 to the pixel border:
  1089. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1090. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  1091. #else
  1092. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  1093. #endif
  1094. // Fixing discontinuities adds 1.0 more to the pixel border:
  1095. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1096. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  1097. #else
  1098. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  1099. #endif
  1100. // Convert the pixel border to an integer texel border. Assume same-pass
  1101. // curvature about triples the texel frequency:
  1102. #ifdef GEOMETRY_EARLY
  1103. static const float max_mask_texel_border =
  1104. ceil(max_tiled_pixel_border * 3.0);
  1105. #else
  1106. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  1107. #endif
  1108. // Convert the texel border to a tile border using worst-case assumptions:
  1109. static const float max_mask_tile_border = max_mask_texel_border/
  1110. (mask_min_allowed_triad_size * mask_triads_per_tile);
  1111. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  1112. // the starting texel (inside borders) for sampling it.
  1113. #ifndef GEOMETRY_EARLY
  1114. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1115. // Special case: Render two tiles without borders. Anisotropic
  1116. // filtering doesn't seem to be a problem here.
  1117. static const float mask_resize_num_tiles = 1.0 + 1.0;
  1118. static const float mask_start_texels = 0.0;
  1119. #else
  1120. static const float mask_resize_num_tiles = 1.0 +
  1121. 2.0 * max_mask_tile_border;
  1122. static const float mask_start_texels = max_mask_texel_border;
  1123. #endif
  1124. #else
  1125. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  1126. static const float mask_start_texels = max_mask_texel_border;
  1127. #endif
  1128. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  1129. // mask_resize_viewport_scale. This limits the maximum final triad size.
  1130. // Estimate the minimum number of triads we can split the screen into in each
  1131. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  1132. static const float mask_resize_num_triads =
  1133. mask_resize_num_tiles * mask_triads_per_tile;
  1134. static const float2 min_allowed_viewport_triads =
  1135. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  1136. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  1137. static const float pi = 3.141592653589;
  1138. // We often want to find the location of the previous texel, e.g.:
  1139. // const float2 curr_texel = uv * texture_size;
  1140. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  1141. // const float2 prev_texel_uv = prev_texel / texture_size;
  1142. // However, many GPU drivers round incorrectly around exact texel locations.
  1143. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  1144. // require this value to be farther from 0.5 than others; define it here.
  1145. // const float2 prev_texel =
  1146. // floor(curr_texel - float2(under_half)) + float2(0.5);
  1147. static const float under_half = 0.4995;
  1148. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  1149. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  1150. //#include "bind-shader-params.h"
  1151. ///////////////////////////// BEGIN BIND-SHADER-PARAMS ////////////////////////////
  1152. #ifndef BIND_SHADER_PARAMS_H
  1153. #define BIND_SHADER_PARAMS_H
  1154. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1155. // crt-royale: A full-featured CRT shader, with cheese.
  1156. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1157. //
  1158. // This program is free software; you can redistribute it and/or modify it
  1159. // under the terms of the GNU General Public License as published by the Free
  1160. // Software Foundation; either version 2 of the License, or any later version.
  1161. //
  1162. // This program is distributed in the hope that it will be useful, but WITHOUT
  1163. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1164. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1165. // more details.
  1166. //
  1167. // You should have received a copy of the GNU General Public License along with
  1168. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1169. // Place, Suite 330, Boston, MA 02111-1307 USA
  1170. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  1171. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1172. //#include "../user-settings.h"
  1173. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1174. #ifndef USER_SETTINGS_H
  1175. #define USER_SETTINGS_H
  1176. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1177. // The Cg compiler uses different "profiles" with different capabilities.
  1178. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1179. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1180. // or driver capabilities, so instead you must comment or uncomment the lines
  1181. // below with "//" before "#define." Disable an option if you get compilation
  1182. // errors resembling those listed. Generally speaking, all of these options
  1183. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1184. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1185. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1186. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1187. // with curved manually tiled phosphor mask coords. Related errors:
  1188. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1189. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1190. //#define DRIVERS_ALLOW_DERIVATIVES
  1191. // Fine derivatives: Unsupported on older ATI cards.
  1192. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1193. // fast single-pass blur operations. If your card uses coarse derivatives and
  1194. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1195. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1196. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1197. #endif
  1198. // Dynamic looping: Requires an fp30 or newer profile.
  1199. // This makes phosphor mask resampling faster in some cases. Related errors:
  1200. // error C5013: profile does not support "for" statements and "for" could not
  1201. // be unrolled
  1202. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1203. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1204. // Using one static loop avoids overhead if the user is right, but if the user
  1205. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1206. // binary search can potentially save some iterations. However, it may fail:
  1207. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1208. // needed to compile program
  1209. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1210. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1211. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1212. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1213. // this profile
  1214. //#define DRIVERS_ALLOW_TEX2DLOD
  1215. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1216. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1217. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1218. // in this profile
  1219. //#define DRIVERS_ALLOW_TEX2DBIAS
  1220. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1221. // impose stricter limitations on register counts and instructions. Enable
  1222. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1223. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1224. // to compile program.
  1225. // Enabling integrated graphics compatibility mode will automatically disable:
  1226. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1227. // (This may be reenabled in a later release.)
  1228. // 2.) RUNTIME_GEOMETRY_MODE
  1229. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1230. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1231. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1232. // To disable a #define option, turn its line into a comment with "//."
  1233. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1234. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1235. // many of the options in this file and allow real-time tuning, but many of
  1236. // them are slower. Disabling them and using this text file will boost FPS.
  1237. #define RUNTIME_SHADER_PARAMS_ENABLE
  1238. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1239. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1240. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1241. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1242. #define RUNTIME_ANTIALIAS_WEIGHTS
  1243. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1244. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1245. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1246. // parameters? This will require more math or dynamic branching.
  1247. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1248. // Specify the tilt at runtime? This makes things about 3% slower.
  1249. #define RUNTIME_GEOMETRY_TILT
  1250. // Specify the geometry mode at runtime?
  1251. #define RUNTIME_GEOMETRY_MODE
  1252. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1253. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1254. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1255. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1256. // PHOSPHOR MASK:
  1257. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1258. // removes the option to do so, but it may be faster without dynamic branches.
  1259. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1260. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1261. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1262. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1263. // detect the right blur if the triad size is static or our profile allows
  1264. // dynamic branches, but otherwise we use the largest blur the user indicates
  1265. // they might need:
  1266. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1267. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1268. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1269. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1270. // Here's a helpful chart:
  1271. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1272. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1273. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1274. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1275. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1276. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1277. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1278. // Note: Many of these static parameters are overridden by runtime shader
  1279. // parameters when those are enabled. However, many others are static codepath
  1280. // options that were cleaner or more convert to code as static constants.
  1281. // GAMMA:
  1282. static const float crt_gamma_static = 2.5; // range [1, 5]
  1283. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1284. // LEVELS MANAGEMENT:
  1285. // Control the final multiplicative image contrast:
  1286. static const float levels_contrast_static = 1.0; // range [0, 4)
  1287. // We auto-dim to avoid clipping between passes and restore brightness
  1288. // later. Control the dim factor here: Lower values clip less but crush
  1289. // blacks more (static only for now).
  1290. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  1291. // HALATION/DIFFUSION/BLOOM:
  1292. // Halation weight: How much energy should be lost to electrons bounding
  1293. // around under the CRT glass and exciting random phosphors?
  1294. static const float halation_weight_static = 0.0; // range [0, 1]
  1295. // Refractive diffusion weight: How much light should spread/diffuse from
  1296. // refracting through the CRT glass?
  1297. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1298. // Underestimate brightness: Bright areas bloom more, but we can base the
  1299. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1300. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1301. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1302. // Blur all colors more than necessary for a softer phosphor bloom?
  1303. static const float bloom_excess_static = 0.0; // range [0, 1]
  1304. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1305. // blurred resize of the input (convergence offsets are applied as well).
  1306. // There are three filter options (static option only for now):
  1307. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1308. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1309. // and beam_max_sigma is low.
  1310. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1311. // always uses a static sigma regardless of beam_max_sigma or
  1312. // mask_num_triads_desired.
  1313. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1314. // These options are more pronounced for the fast, unbloomed shader version.
  1315. #ifndef RADEON_FIX
  1316. static const float bloom_approx_filter_static = 2.0;
  1317. #else
  1318. static const float bloom_approx_filter_static = 1.0;
  1319. #endif
  1320. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1321. // How many scanlines should contribute light to each pixel? Using more
  1322. // scanlines is slower (especially for a generalized Gaussian) but less
  1323. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1324. // max_beam_sigma at which the closest unused weight is guaranteed <
  1325. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1326. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1327. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1328. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1329. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1330. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1331. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1332. // A generalized Gaussian beam varies shape with color too, now just width.
  1333. // It's slower but more flexible (static option only for now).
  1334. static const bool beam_generalized_gaussian = true;
  1335. // What kind of scanline antialiasing do you want?
  1336. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1337. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1338. // better than 3x antialiasing (static option only for now).
  1339. static const float beam_antialias_level = 1.0; // range [0, 2]
  1340. // Min/max standard deviations for scanline beams: Higher values widen and
  1341. // soften scanlines. Depending on other options, low min sigmas can alias.
  1342. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1343. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1344. // Beam width varies as a function of color: A power function (0) is more
  1345. // configurable, but a spherical function (1) gives the widest beam
  1346. // variability without aliasing (static option only for now).
  1347. static const float beam_spot_shape_function = 0.0;
  1348. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1349. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1350. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1351. // Generalized Gaussian max shape parameters: Higher values give flatter
  1352. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1353. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1354. // values > ~40.0 cause artifacts with integrals.
  1355. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1356. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1357. // Generalized Gaussian shape power: Affects how quickly the distribution
  1358. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1359. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1360. // appear sharper for most colors.
  1361. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1362. // What filter should be used to sample scanlines horizontally?
  1363. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1364. static const float beam_horiz_filter_static = 0.0;
  1365. // Standard deviation for horizontal Gaussian resampling:
  1366. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1367. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1368. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1369. // limiting circuitry in some CRT's), or a weighted avg.?
  1370. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1371. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1372. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1373. // later passes (static option only for now).
  1374. static const bool beam_misconvergence = true;
  1375. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1376. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1377. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1378. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1379. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1380. // Detect interlacing (static option only for now)?
  1381. static const bool interlace_detect = true;
  1382. // Assume 1080-line sources are interlaced?
  1383. static const bool interlace_1080i_static = false;
  1384. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1385. // (Whether this matters depends on the nature of the interlaced input.)
  1386. static const bool interlace_bff_static = false;
  1387. // ANTIALIASING:
  1388. // What AA level do you want for curvature/overscan/subpixels? Options:
  1389. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1390. // (Static option only for now)
  1391. static const float aa_level = 12.0; // range [0, 24]
  1392. // What antialiasing filter do you want (static option only)? Options:
  1393. // 0: Box (separable), 1: Box (cylindrical),
  1394. // 2: Tent (separable), 3: Tent (cylindrical),
  1395. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1396. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1397. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1398. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1399. static const float aa_filter = 6.0; // range [0, 9]
  1400. // Flip the sample grid on odd/even frames (static option only for now)?
  1401. static const bool aa_temporal = false;
  1402. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1403. // the blue offset is the negative r offset; range [0, 0.5]
  1404. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1405. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1406. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1407. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1408. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1409. // 4.) C = 0.0 is a soft spline filter.
  1410. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1411. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1412. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1413. // PHOSPHOR MASK:
  1414. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1415. static const float mask_type_static = 1.0; // range [0, 2]
  1416. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1417. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1418. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1419. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1420. // is halfway decent with LUT mipmapping but atrocious without it.
  1421. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1422. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1423. // This mode reuses the same masks, so triads will be enormous unless
  1424. // you change the mask LUT filenames in your .cgp file.
  1425. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1426. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1427. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1428. // will always be used to calculate the full bloom sigma statically.
  1429. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1430. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1431. // triads) will be rounded to the nearest integer tile size and clamped to
  1432. // obey minimum size constraints (imposed to reduce downsize taps) and
  1433. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1434. // To increase the size limit, double the viewport-relative scales for the
  1435. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1436. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1437. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1438. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1439. // final size will be rounded and constrained as above); default 480.0
  1440. static const float mask_num_triads_desired_static = 480.0;
  1441. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1442. // more samples and avoid moire a bit better, but some is unavoidable
  1443. // depending on the destination size (static option for now).
  1444. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1445. // The mask is resized using a variable number of taps in each dimension,
  1446. // but some Cg profiles always fetch a constant number of taps no matter
  1447. // what (no dynamic branching). We can limit the maximum number of taps if
  1448. // we statically limit the minimum phosphor triad size. Larger values are
  1449. // faster, but the limit IS enforced (static option only, forever);
  1450. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1451. // TODO: Make this 1.0 and compensate with smarter sampling!
  1452. static const float mask_min_allowed_triad_size = 2.0;
  1453. // GEOMETRY:
  1454. // Geometry mode:
  1455. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1456. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1457. static const float geom_mode_static = 0.0; // range [0, 3]
  1458. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1459. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1460. // View dist is the distance from the player to their physical screen, in
  1461. // units of the viewport's diagonal size. It controls the field of view.
  1462. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1463. // Tilt angle in radians (clockwise around up and right vectors):
  1464. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1465. // Aspect ratio: When the true viewport size is unknown, this value is used
  1466. // to help convert between the phosphor triad size and count, along with
  1467. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1468. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1469. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1470. // default (256/224)*(54/47) = 1.313069909 (see below)
  1471. static const float geom_aspect_ratio_static = 1.313069909;
  1472. // Before getting into overscan, here's some general aspect ratio info:
  1473. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1474. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1475. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1476. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1477. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1478. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1479. // a.) Enable Retroarch's "Crop Overscan"
  1480. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1481. // Real consoles use horizontal black padding in the signal, but emulators
  1482. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1483. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1484. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1485. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1486. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1487. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1488. // without doing a. or b., but horizontal image borders will be tighter
  1489. // than vertical ones, messing up curvature and overscan. Fixing the
  1490. // padding first corrects this.
  1491. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1492. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1493. // above: Values < 1.0 zoom out; range (0, inf)
  1494. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1495. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1496. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1497. // with strong curvature (static option only for now).
  1498. static const bool geom_force_correct_tangent_matrix = true;
  1499. // BORDERS:
  1500. // Rounded border size in texture uv coords:
  1501. static const float border_size_static = 0.015; // range [0, 0.5]
  1502. // Border darkness: Moderate values darken the border smoothly, and high
  1503. // values make the image very dark just inside the border:
  1504. static const float border_darkness_static = 2.0; // range [0, inf)
  1505. // Border compression: High numbers compress border transitions, narrowing
  1506. // the dark border area.
  1507. static const float border_compress_static = 2.5; // range [1, inf)
  1508. #endif // USER_SETTINGS_H
  1509. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1510. //#include "derived-settings-and-constants.h"
  1511. ///////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  1512. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  1513. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  1514. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1515. // crt-royale: A full-featured CRT shader, with cheese.
  1516. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1517. //
  1518. // This program is free software; you can redistribute it and/or modify it
  1519. // under the terms of the GNU General Public License as published by the Free
  1520. // Software Foundation; either version 2 of the License, or any later version.
  1521. //
  1522. // This program is distributed in the hope that it will be useful, but WITHOUT
  1523. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1524. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1525. // more details.
  1526. //
  1527. // You should have received a copy of the GNU General Public License along with
  1528. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1529. // Place, Suite 330, Boston, MA 02111-1307 USA
  1530. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  1531. // These macros and constants can be used across the whole codebase.
  1532. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  1533. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1534. //#include "../user-settings.h"
  1535. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1536. #ifndef USER_SETTINGS_H
  1537. #define USER_SETTINGS_H
  1538. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1539. // The Cg compiler uses different "profiles" with different capabilities.
  1540. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1541. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1542. // or driver capabilities, so instead you must comment or uncomment the lines
  1543. // below with "//" before "#define." Disable an option if you get compilation
  1544. // errors resembling those listed. Generally speaking, all of these options
  1545. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1546. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1547. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1548. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1549. // with curved manually tiled phosphor mask coords. Related errors:
  1550. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1551. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1552. //#define DRIVERS_ALLOW_DERIVATIVES
  1553. // Fine derivatives: Unsupported on older ATI cards.
  1554. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1555. // fast single-pass blur operations. If your card uses coarse derivatives and
  1556. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1557. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1558. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1559. #endif
  1560. // Dynamic looping: Requires an fp30 or newer profile.
  1561. // This makes phosphor mask resampling faster in some cases. Related errors:
  1562. // error C5013: profile does not support "for" statements and "for" could not
  1563. // be unrolled
  1564. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1565. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1566. // Using one static loop avoids overhead if the user is right, but if the user
  1567. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1568. // binary search can potentially save some iterations. However, it may fail:
  1569. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1570. // needed to compile program
  1571. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1572. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1573. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1574. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1575. // this profile
  1576. //#define DRIVERS_ALLOW_TEX2DLOD
  1577. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1578. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1579. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1580. // in this profile
  1581. //#define DRIVERS_ALLOW_TEX2DBIAS
  1582. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1583. // impose stricter limitations on register counts and instructions. Enable
  1584. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1585. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1586. // to compile program.
  1587. // Enabling integrated graphics compatibility mode will automatically disable:
  1588. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1589. // (This may be reenabled in a later release.)
  1590. // 2.) RUNTIME_GEOMETRY_MODE
  1591. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1592. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1593. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1594. // To disable a #define option, turn its line into a comment with "//."
  1595. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1596. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1597. // many of the options in this file and allow real-time tuning, but many of
  1598. // them are slower. Disabling them and using this text file will boost FPS.
  1599. #define RUNTIME_SHADER_PARAMS_ENABLE
  1600. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1601. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1602. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1603. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1604. #define RUNTIME_ANTIALIAS_WEIGHTS
  1605. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1606. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1607. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1608. // parameters? This will require more math or dynamic branching.
  1609. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1610. // Specify the tilt at runtime? This makes things about 3% slower.
  1611. #define RUNTIME_GEOMETRY_TILT
  1612. // Specify the geometry mode at runtime?
  1613. #define RUNTIME_GEOMETRY_MODE
  1614. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1615. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1616. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1617. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1618. // PHOSPHOR MASK:
  1619. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1620. // removes the option to do so, but it may be faster without dynamic branches.
  1621. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1622. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1623. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1624. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1625. // detect the right blur if the triad size is static or our profile allows
  1626. // dynamic branches, but otherwise we use the largest blur the user indicates
  1627. // they might need:
  1628. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1629. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1630. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1631. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1632. // Here's a helpful chart:
  1633. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1634. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1635. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1636. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1637. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1638. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1639. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1640. // Note: Many of these static parameters are overridden by runtime shader
  1641. // parameters when those are enabled. However, many others are static codepath
  1642. // options that were cleaner or more convert to code as static constants.
  1643. // GAMMA:
  1644. static const float crt_gamma_static = 2.5; // range [1, 5]
  1645. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1646. // LEVELS MANAGEMENT:
  1647. // Control the final multiplicative image contrast:
  1648. static const float levels_contrast_static = 1.0; // range [0, 4)
  1649. // We auto-dim to avoid clipping between passes and restore brightness
  1650. // later. Control the dim factor here: Lower values clip less but crush
  1651. // blacks more (static only for now).
  1652. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  1653. // HALATION/DIFFUSION/BLOOM:
  1654. // Halation weight: How much energy should be lost to electrons bounding
  1655. // around under the CRT glass and exciting random phosphors?
  1656. static const float halation_weight_static = 0.0; // range [0, 1]
  1657. // Refractive diffusion weight: How much light should spread/diffuse from
  1658. // refracting through the CRT glass?
  1659. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1660. // Underestimate brightness: Bright areas bloom more, but we can base the
  1661. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1662. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1663. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1664. // Blur all colors more than necessary for a softer phosphor bloom?
  1665. static const float bloom_excess_static = 0.0; // range [0, 1]
  1666. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1667. // blurred resize of the input (convergence offsets are applied as well).
  1668. // There are three filter options (static option only for now):
  1669. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1670. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1671. // and beam_max_sigma is low.
  1672. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1673. // always uses a static sigma regardless of beam_max_sigma or
  1674. // mask_num_triads_desired.
  1675. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1676. // These options are more pronounced for the fast, unbloomed shader version.
  1677. #ifndef RADEON_FIX
  1678. static const float bloom_approx_filter_static = 2.0;
  1679. #else
  1680. static const float bloom_approx_filter_static = 1.0;
  1681. #endif
  1682. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1683. // How many scanlines should contribute light to each pixel? Using more
  1684. // scanlines is slower (especially for a generalized Gaussian) but less
  1685. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1686. // max_beam_sigma at which the closest unused weight is guaranteed <
  1687. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1688. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1689. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1690. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1691. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1692. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1693. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1694. // A generalized Gaussian beam varies shape with color too, now just width.
  1695. // It's slower but more flexible (static option only for now).
  1696. static const bool beam_generalized_gaussian = true;
  1697. // What kind of scanline antialiasing do you want?
  1698. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1699. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1700. // better than 3x antialiasing (static option only for now).
  1701. static const float beam_antialias_level = 1.0; // range [0, 2]
  1702. // Min/max standard deviations for scanline beams: Higher values widen and
  1703. // soften scanlines. Depending on other options, low min sigmas can alias.
  1704. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1705. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1706. // Beam width varies as a function of color: A power function (0) is more
  1707. // configurable, but a spherical function (1) gives the widest beam
  1708. // variability without aliasing (static option only for now).
  1709. static const float beam_spot_shape_function = 0.0;
  1710. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1711. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1712. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1713. // Generalized Gaussian max shape parameters: Higher values give flatter
  1714. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1715. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1716. // values > ~40.0 cause artifacts with integrals.
  1717. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1718. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1719. // Generalized Gaussian shape power: Affects how quickly the distribution
  1720. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1721. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1722. // appear sharper for most colors.
  1723. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1724. // What filter should be used to sample scanlines horizontally?
  1725. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1726. static const float beam_horiz_filter_static = 0.0;
  1727. // Standard deviation for horizontal Gaussian resampling:
  1728. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1729. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1730. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1731. // limiting circuitry in some CRT's), or a weighted avg.?
  1732. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1733. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1734. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1735. // later passes (static option only for now).
  1736. static const bool beam_misconvergence = true;
  1737. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1738. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1739. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1740. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1741. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1742. // Detect interlacing (static option only for now)?
  1743. static const bool interlace_detect = true;
  1744. // Assume 1080-line sources are interlaced?
  1745. static const bool interlace_1080i_static = false;
  1746. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1747. // (Whether this matters depends on the nature of the interlaced input.)
  1748. static const bool interlace_bff_static = false;
  1749. // ANTIALIASING:
  1750. // What AA level do you want for curvature/overscan/subpixels? Options:
  1751. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1752. // (Static option only for now)
  1753. static const float aa_level = 12.0; // range [0, 24]
  1754. // What antialiasing filter do you want (static option only)? Options:
  1755. // 0: Box (separable), 1: Box (cylindrical),
  1756. // 2: Tent (separable), 3: Tent (cylindrical),
  1757. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1758. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1759. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1760. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1761. static const float aa_filter = 6.0; // range [0, 9]
  1762. // Flip the sample grid on odd/even frames (static option only for now)?
  1763. static const bool aa_temporal = false;
  1764. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1765. // the blue offset is the negative r offset; range [0, 0.5]
  1766. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1767. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1768. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1769. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1770. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1771. // 4.) C = 0.0 is a soft spline filter.
  1772. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1773. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1774. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1775. // PHOSPHOR MASK:
  1776. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1777. static const float mask_type_static = 1.0; // range [0, 2]
  1778. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1779. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1780. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1781. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1782. // is halfway decent with LUT mipmapping but atrocious without it.
  1783. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1784. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1785. // This mode reuses the same masks, so triads will be enormous unless
  1786. // you change the mask LUT filenames in your .cgp file.
  1787. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1788. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1789. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1790. // will always be used to calculate the full bloom sigma statically.
  1791. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1792. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1793. // triads) will be rounded to the nearest integer tile size and clamped to
  1794. // obey minimum size constraints (imposed to reduce downsize taps) and
  1795. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1796. // To increase the size limit, double the viewport-relative scales for the
  1797. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1798. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1799. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1800. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1801. // final size will be rounded and constrained as above); default 480.0
  1802. static const float mask_num_triads_desired_static = 480.0;
  1803. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1804. // more samples and avoid moire a bit better, but some is unavoidable
  1805. // depending on the destination size (static option for now).
  1806. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1807. // The mask is resized using a variable number of taps in each dimension,
  1808. // but some Cg profiles always fetch a constant number of taps no matter
  1809. // what (no dynamic branching). We can limit the maximum number of taps if
  1810. // we statically limit the minimum phosphor triad size. Larger values are
  1811. // faster, but the limit IS enforced (static option only, forever);
  1812. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1813. // TODO: Make this 1.0 and compensate with smarter sampling!
  1814. static const float mask_min_allowed_triad_size = 2.0;
  1815. // GEOMETRY:
  1816. // Geometry mode:
  1817. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1818. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1819. static const float geom_mode_static = 0.0; // range [0, 3]
  1820. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1821. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1822. // View dist is the distance from the player to their physical screen, in
  1823. // units of the viewport's diagonal size. It controls the field of view.
  1824. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1825. // Tilt angle in radians (clockwise around up and right vectors):
  1826. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1827. // Aspect ratio: When the true viewport size is unknown, this value is used
  1828. // to help convert between the phosphor triad size and count, along with
  1829. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1830. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1831. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1832. // default (256/224)*(54/47) = 1.313069909 (see below)
  1833. static const float geom_aspect_ratio_static = 1.313069909;
  1834. // Before getting into overscan, here's some general aspect ratio info:
  1835. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1836. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1837. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1838. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1839. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1840. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1841. // a.) Enable Retroarch's "Crop Overscan"
  1842. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1843. // Real consoles use horizontal black padding in the signal, but emulators
  1844. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1845. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1846. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1847. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1848. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1849. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1850. // without doing a. or b., but horizontal image borders will be tighter
  1851. // than vertical ones, messing up curvature and overscan. Fixing the
  1852. // padding first corrects this.
  1853. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1854. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1855. // above: Values < 1.0 zoom out; range (0, inf)
  1856. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1857. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1858. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1859. // with strong curvature (static option only for now).
  1860. static const bool geom_force_correct_tangent_matrix = true;
  1861. // BORDERS:
  1862. // Rounded border size in texture uv coords:
  1863. static const float border_size_static = 0.015; // range [0, 0.5]
  1864. // Border darkness: Moderate values darken the border smoothly, and high
  1865. // values make the image very dark just inside the border:
  1866. static const float border_darkness_static = 2.0; // range [0, inf)
  1867. // Border compression: High numbers compress border transitions, narrowing
  1868. // the dark border area.
  1869. static const float border_compress_static = 2.5; // range [1, inf)
  1870. #endif // USER_SETTINGS_H
  1871. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1872. //#include "user-cgp-constants.h"
  1873. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  1874. #ifndef USER_CGP_CONSTANTS_H
  1875. #define USER_CGP_CONSTANTS_H
  1876. // IMPORTANT:
  1877. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  1878. // (or whatever related .cgp file you're using). If they aren't, you're likely
  1879. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  1880. // set directly in the .cgp file to make things easier, but...they can't.
  1881. // PASS SCALES AND RELATED CONSTANTS:
  1882. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  1883. // this shader: One does a viewport-scale bloom, and the other skips it. The
  1884. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  1885. static const float bloom_approx_size_x = 320.0;
  1886. static const float bloom_approx_size_x_for_fake = 400.0;
  1887. // Copy the viewport-relative scales of the phosphor mask resize passes
  1888. // (MASK_RESIZE and the pass immediately preceding it):
  1889. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  1890. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  1891. static const float geom_max_aspect_ratio = 4.0/3.0;
  1892. // PHOSPHOR MASK TEXTURE CONSTANTS:
  1893. // Set the following constants to reflect the properties of the phosphor mask
  1894. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  1895. // based on user settings, then repeats a single tile until filling the screen.
  1896. // The shader must know the input texture size (default 64x64), and to manually
  1897. // resize, it must also know the horizontal triads per tile (default 8).
  1898. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  1899. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  1900. static const float mask_triads_per_tile = 8.0;
  1901. // We need the average brightness of the phosphor mask to compensate for the
  1902. // dimming it causes. The following four values are roughly correct for the
  1903. // masks included with the shader. Update the value for any LUT texture you
  1904. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  1905. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  1906. //#define PHOSPHOR_MASK_GRILLE14
  1907. static const float mask_grille14_avg_color = 50.6666666/255.0;
  1908. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  1909. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  1910. static const float mask_grille15_avg_color = 53.0/255.0;
  1911. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  1912. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  1913. static const float mask_slot_avg_color = 46.0/255.0;
  1914. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  1915. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  1916. static const float mask_shadow_avg_color = 41.0/255.0;
  1917. // TileableLinearShadowMask*.png
  1918. // TileableLinearShadowMaskEDP*.png
  1919. #ifdef PHOSPHOR_MASK_GRILLE14
  1920. static const float mask_grille_avg_color = mask_grille14_avg_color;
  1921. #else
  1922. static const float mask_grille_avg_color = mask_grille15_avg_color;
  1923. #endif
  1924. #endif // USER_CGP_CONSTANTS_H
  1925. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  1926. //////////////////////////////// END INCLUDES ////////////////////////////////
  1927. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  1928. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  1929. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  1930. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  1931. #ifndef SIMULATE_CRT_ON_LCD
  1932. #define SIMULATE_CRT_ON_LCD
  1933. #endif
  1934. // Manually tiling a manually resized texture creates texture coord derivative
  1935. // discontinuities and confuses anisotropic filtering, causing discolored tile
  1936. // seams in the phosphor mask. Workarounds:
  1937. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  1938. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  1939. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  1940. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  1941. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  1942. // (Same-pass curvature isn't used but could be in the future...maybe.)
  1943. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  1944. // border padding to the resized mask FBO, but it works with same-pass
  1945. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  1946. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  1947. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  1948. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  1949. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  1950. // Also, manually resampling the phosphor mask is slightly blurrier with
  1951. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  1952. // creates artifacts, but only with the fully bloomed shader.) The difference
  1953. // is subtle with small triads, but you can fix it for a small cost.
  1954. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1955. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  1956. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  1957. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  1958. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  1959. // #defined by either user-settings.h or a wrapper .cg that #includes the
  1960. // current .cg pass.)
  1961. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1962. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  1963. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  1964. #endif
  1965. #ifdef RUNTIME_GEOMETRY_MODE
  1966. #undef RUNTIME_GEOMETRY_MODE
  1967. #endif
  1968. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  1969. // inferior in most cases, so replace 2.0 with 0.0:
  1970. static const float bloom_approx_filter =
  1971. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  1972. #else
  1973. static const float bloom_approx_filter = bloom_approx_filter_static;
  1974. #endif
  1975. // Disable slow runtime paths if static parameters are used. Most of these
  1976. // won't be a problem anyway once the params are disabled, but some will.
  1977. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  1978. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1979. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1980. #endif
  1981. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  1982. #undef RUNTIME_ANTIALIAS_WEIGHTS
  1983. #endif
  1984. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1985. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1986. #endif
  1987. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1988. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1989. #endif
  1990. #ifdef RUNTIME_GEOMETRY_TILT
  1991. #undef RUNTIME_GEOMETRY_TILT
  1992. #endif
  1993. #ifdef RUNTIME_GEOMETRY_MODE
  1994. #undef RUNTIME_GEOMETRY_MODE
  1995. #endif
  1996. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1997. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1998. #endif
  1999. #endif
  2000. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  2001. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2002. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2003. #endif
  2004. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2005. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2006. #endif
  2007. // Rule out unavailable anisotropic compatibility strategies:
  2008. #ifndef DRIVERS_ALLOW_DERIVATIVES
  2009. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2010. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2011. #endif
  2012. #endif
  2013. #ifndef DRIVERS_ALLOW_TEX2DLOD
  2014. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2015. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2016. #endif
  2017. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2018. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2019. #endif
  2020. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  2021. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  2022. #endif
  2023. #endif
  2024. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  2025. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2026. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2027. #endif
  2028. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2029. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2030. #endif
  2031. #endif
  2032. // Prioritize anisotropic tiling compatibility strategies by performance and
  2033. // disable unused strategies. This concentrates all the nesting in one place.
  2034. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2035. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2036. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2037. #endif
  2038. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2039. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2040. #endif
  2041. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2042. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2043. #endif
  2044. #else
  2045. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2046. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2047. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2048. #endif
  2049. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2050. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2051. #endif
  2052. #else
  2053. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  2054. // flat texture coords in the same pass, but that's all we use.
  2055. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2056. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2057. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2058. #endif
  2059. #endif
  2060. #endif
  2061. #endif
  2062. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  2063. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  2064. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2065. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2066. #endif
  2067. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2068. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2069. #endif
  2070. // Prioritize anisotropic resampling compatibility strategies the same way:
  2071. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2072. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2073. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2074. #endif
  2075. #endif
  2076. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  2077. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  2078. // should: It gives us more options for using fewer samples.
  2079. #ifdef DRIVERS_ALLOW_TEX2DLOD
  2080. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2081. // TODO: Take advantage of this!
  2082. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  2083. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  2084. #else
  2085. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2086. #endif
  2087. #else
  2088. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2089. #endif
  2090. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  2091. // main_fragment, or a static alias of one of the above. This makes it hard
  2092. // to select the phosphor mask at runtime: We can't even assign to a uniform
  2093. // global in the vertex shader or select a sampler2D in the vertex shader and
  2094. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  2095. // because it just gives us the input texture or a black screen. However, we
  2096. // can get around these limitations by calling tex2D three times with different
  2097. // uniform samplers (or resizing the phosphor mask three times altogether).
  2098. // With dynamic branches, we can process only one of these branches on top of
  2099. // quickly discarding fragments we don't need (cgc seems able to overcome
  2100. // limigations around dependent texture fetches inside of branches). Without
  2101. // dynamic branches, we have to process every branch for every fragment...which
  2102. // is slower. Runtime sampling mode selection is slower without dynamic
  2103. // branches as well. Let the user's static #defines decide if it's worth it.
  2104. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2105. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2106. #else
  2107. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2108. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2109. #endif
  2110. #endif
  2111. // We need to render some minimum number of tiles in the resize passes.
  2112. // We need at least 1.0 just to repeat a single tile, and we need extra
  2113. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  2114. // antialiasing, same-pass curvature (not currently used), etc. First
  2115. // determine how many border texels and tiles we need, based on how the result
  2116. // will be sampled:
  2117. #ifdef GEOMETRY_EARLY
  2118. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  2119. // Most antialiasing filters have a base radius of 4.0 pixels:
  2120. static const float max_aa_base_pixel_border = 4.0 +
  2121. max_subpixel_offset;
  2122. #else
  2123. static const float max_aa_base_pixel_border = 0.0;
  2124. #endif
  2125. // Anisotropic filtering adds about 0.5 to the pixel border:
  2126. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2127. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  2128. #else
  2129. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  2130. #endif
  2131. // Fixing discontinuities adds 1.0 more to the pixel border:
  2132. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2133. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  2134. #else
  2135. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  2136. #endif
  2137. // Convert the pixel border to an integer texel border. Assume same-pass
  2138. // curvature about triples the texel frequency:
  2139. #ifdef GEOMETRY_EARLY
  2140. static const float max_mask_texel_border =
  2141. ceil(max_tiled_pixel_border * 3.0);
  2142. #else
  2143. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  2144. #endif
  2145. // Convert the texel border to a tile border using worst-case assumptions:
  2146. static const float max_mask_tile_border = max_mask_texel_border/
  2147. (mask_min_allowed_triad_size * mask_triads_per_tile);
  2148. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  2149. // the starting texel (inside borders) for sampling it.
  2150. #ifndef GEOMETRY_EARLY
  2151. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2152. // Special case: Render two tiles without borders. Anisotropic
  2153. // filtering doesn't seem to be a problem here.
  2154. static const float mask_resize_num_tiles = 1.0 + 1.0;
  2155. static const float mask_start_texels = 0.0;
  2156. #else
  2157. static const float mask_resize_num_tiles = 1.0 +
  2158. 2.0 * max_mask_tile_border;
  2159. static const float mask_start_texels = max_mask_texel_border;
  2160. #endif
  2161. #else
  2162. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  2163. static const float mask_start_texels = max_mask_texel_border;
  2164. #endif
  2165. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  2166. // mask_resize_viewport_scale. This limits the maximum final triad size.
  2167. // Estimate the minimum number of triads we can split the screen into in each
  2168. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  2169. static const float mask_resize_num_triads =
  2170. mask_resize_num_tiles * mask_triads_per_tile;
  2171. static const float2 min_allowed_viewport_triads =
  2172. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  2173. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  2174. static const float pi = 3.141592653589;
  2175. // We often want to find the location of the previous texel, e.g.:
  2176. // const float2 curr_texel = uv * texture_size;
  2177. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  2178. // const float2 prev_texel_uv = prev_texel / texture_size;
  2179. // However, many GPU drivers round incorrectly around exact texel locations.
  2180. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  2181. // require this value to be farther from 0.5 than others; define it here.
  2182. // const float2 prev_texel =
  2183. // floor(curr_texel - float2(under_half)) + float2(0.5);
  2184. static const float under_half = 0.4995;
  2185. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  2186. //////////////////// END DERIVED-SETTINGS-AND-CONSTANTS /////////////////////
  2187. //////////////////////////////// END INCLUDES ////////////////////////////////
  2188. // Override some parameters for gamma-management.h and tex2Dantialias.h:
  2189. #define OVERRIDE_DEVICE_GAMMA
  2190. static const float gba_gamma = 3.5; // Irrelevant but necessary to define.
  2191. #define ANTIALIAS_OVERRIDE_BASICS
  2192. #define ANTIALIAS_OVERRIDE_PARAMETERS
  2193. // Provide accessors for vector constants that pack scalar uniforms:
  2194. inline float2 get_aspect_vector(const float geom_aspect_ratio)
  2195. {
  2196. // Get an aspect ratio vector. Enforce geom_max_aspect_ratio, and prevent
  2197. // the absolute scale from affecting the uv-mapping for curvature:
  2198. const float geom_clamped_aspect_ratio =
  2199. min(geom_aspect_ratio, geom_max_aspect_ratio);
  2200. const float2 geom_aspect =
  2201. normalize(float2(geom_clamped_aspect_ratio, 1.0));
  2202. return geom_aspect;
  2203. }
  2204. inline float2 get_geom_overscan_vector()
  2205. {
  2206. return float2(geom_overscan_x, geom_overscan_y);
  2207. }
  2208. inline float2 get_geom_tilt_angle_vector()
  2209. {
  2210. return float2(geom_tilt_angle_x, geom_tilt_angle_y);
  2211. }
  2212. inline float3 get_convergence_offsets_x_vector()
  2213. {
  2214. return float3(convergence_offset_x_r, convergence_offset_x_g,
  2215. convergence_offset_x_b);
  2216. }
  2217. inline float3 get_convergence_offsets_y_vector()
  2218. {
  2219. return float3(convergence_offset_y_r, convergence_offset_y_g,
  2220. convergence_offset_y_b);
  2221. }
  2222. inline float2 get_convergence_offsets_r_vector()
  2223. {
  2224. return float2(convergence_offset_x_r, convergence_offset_y_r);
  2225. }
  2226. inline float2 get_convergence_offsets_g_vector()
  2227. {
  2228. return float2(convergence_offset_x_g, convergence_offset_y_g);
  2229. }
  2230. inline float2 get_convergence_offsets_b_vector()
  2231. {
  2232. return float2(convergence_offset_x_b, convergence_offset_y_b);
  2233. }
  2234. inline float2 get_aa_subpixel_r_offset()
  2235. {
  2236. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  2237. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2238. // WARNING: THIS IS EXTREMELY EXPENSIVE.
  2239. return float2(aa_subpixel_r_offset_x_runtime,
  2240. aa_subpixel_r_offset_y_runtime);
  2241. #else
  2242. return aa_subpixel_r_offset_static;
  2243. #endif
  2244. #else
  2245. return aa_subpixel_r_offset_static;
  2246. #endif
  2247. }
  2248. // Provide accessors settings which still need "cooking:"
  2249. inline float get_mask_amplify()
  2250. {
  2251. static const float mask_grille_amplify = 1.0/mask_grille_avg_color;
  2252. static const float mask_slot_amplify = 1.0/mask_slot_avg_color;
  2253. static const float mask_shadow_amplify = 1.0/mask_shadow_avg_color;
  2254. return mask_type < 0.5 ? mask_grille_amplify :
  2255. mask_type < 1.5 ? mask_slot_amplify :
  2256. mask_shadow_amplify;
  2257. }
  2258. inline float get_mask_sample_mode()
  2259. {
  2260. #ifdef RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2261. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2262. return mask_sample_mode_desired;
  2263. #else
  2264. return clamp(mask_sample_mode_desired, 1.0, 2.0);
  2265. #endif
  2266. #else
  2267. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2268. return mask_sample_mode_static;
  2269. #else
  2270. return clamp(mask_sample_mode_static, 1.0, 2.0);
  2271. #endif
  2272. #endif
  2273. }
  2274. #endif // BIND_SHADER_PARAMS_H
  2275. //////////////////////////// END BIND-SHADER-PARAMS ///////////////////////////
  2276. float bloom_approx_scale_x = targetSize.x / sourceSize[0].y;
  2277. const float max_viewport_size_x = 1080.0*1024.0*(4.0/3.0);
  2278. const float bloom_diff_thresh_ = 1.0/256.0;
  2279. /////////////////////////// BEGIN FRAGMENT-INCLUDES ///////////////////////////
  2280. //#include "bloom-functions.h"
  2281. //////////////////////////// BEGIN BLOOM-FUNCTIONS ///////////////////////////
  2282. #ifndef BLOOM_FUNCTIONS_H
  2283. #define BLOOM_FUNCTIONS_H
  2284. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  2285. // crt-royale: A full-featured CRT shader, with cheese.
  2286. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  2287. //
  2288. // This program is free software; you can redistribute it and/or modify it
  2289. // under the terms of the GNU General Public License as published by the Free
  2290. // Software Foundation; either version 2 of the License, or any later version.
  2291. //
  2292. // This program is distributed in the hope that it will be useful, but WITHOUT
  2293. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  2294. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  2295. // more details.
  2296. //
  2297. // You should have received a copy of the GNU General Public License along with
  2298. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  2299. // Place, Suite 330, Boston, MA 02111-1307 USA
  2300. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  2301. // These utility functions and constants help several passes determine the
  2302. // size and center texel weight of the phosphor bloom in a uniform manner.
  2303. ////////////////////////////////// INCLUDES //////////////////////////////////
  2304. // We need to calculate the correct blur sigma using some .cgp constants:
  2305. //#include "../user-settings.h"
  2306. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  2307. #ifndef USER_SETTINGS_H
  2308. #define USER_SETTINGS_H
  2309. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  2310. // The Cg compiler uses different "profiles" with different capabilities.
  2311. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  2312. // require higher profiles like fp30 or fp40. The shader can't detect profile
  2313. // or driver capabilities, so instead you must comment or uncomment the lines
  2314. // below with "//" before "#define." Disable an option if you get compilation
  2315. // errors resembling those listed. Generally speaking, all of these options
  2316. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  2317. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  2318. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  2319. // Among other things, derivatives help us fix anisotropic filtering artifacts
  2320. // with curved manually tiled phosphor mask coords. Related errors:
  2321. // error C3004: function "float2 ddx(float2);" not supported in this profile
  2322. // error C3004: function "float2 ddy(float2);" not supported in this profile
  2323. //#define DRIVERS_ALLOW_DERIVATIVES
  2324. // Fine derivatives: Unsupported on older ATI cards.
  2325. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  2326. // fast single-pass blur operations. If your card uses coarse derivatives and
  2327. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  2328. #ifdef DRIVERS_ALLOW_DERIVATIVES
  2329. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  2330. #endif
  2331. // Dynamic looping: Requires an fp30 or newer profile.
  2332. // This makes phosphor mask resampling faster in some cases. Related errors:
  2333. // error C5013: profile does not support "for" statements and "for" could not
  2334. // be unrolled
  2335. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2336. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  2337. // Using one static loop avoids overhead if the user is right, but if the user
  2338. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  2339. // binary search can potentially save some iterations. However, it may fail:
  2340. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  2341. // needed to compile program
  2342. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  2343. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  2344. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  2345. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  2346. // this profile
  2347. //#define DRIVERS_ALLOW_TEX2DLOD
  2348. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  2349. // artifacts from anisotropic filtering and mipmapping. Related errors:
  2350. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  2351. // in this profile
  2352. //#define DRIVERS_ALLOW_TEX2DBIAS
  2353. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  2354. // impose stricter limitations on register counts and instructions. Enable
  2355. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  2356. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  2357. // to compile program.
  2358. // Enabling integrated graphics compatibility mode will automatically disable:
  2359. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  2360. // (This may be reenabled in a later release.)
  2361. // 2.) RUNTIME_GEOMETRY_MODE
  2362. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  2363. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  2364. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  2365. // To disable a #define option, turn its line into a comment with "//."
  2366. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  2367. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  2368. // many of the options in this file and allow real-time tuning, but many of
  2369. // them are slower. Disabling them and using this text file will boost FPS.
  2370. #define RUNTIME_SHADER_PARAMS_ENABLE
  2371. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  2372. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  2373. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  2374. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  2375. #define RUNTIME_ANTIALIAS_WEIGHTS
  2376. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  2377. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2378. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  2379. // parameters? This will require more math or dynamic branching.
  2380. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  2381. // Specify the tilt at runtime? This makes things about 3% slower.
  2382. #define RUNTIME_GEOMETRY_TILT
  2383. // Specify the geometry mode at runtime?
  2384. #define RUNTIME_GEOMETRY_MODE
  2385. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  2386. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  2387. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  2388. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2389. // PHOSPHOR MASK:
  2390. // Manually resize the phosphor mask for best results (slower)? Disabling this
  2391. // removes the option to do so, but it may be faster without dynamic branches.
  2392. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  2393. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  2394. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  2395. // Larger blurs are expensive, but we need them to blur larger triads. We can
  2396. // detect the right blur if the triad size is static or our profile allows
  2397. // dynamic branches, but otherwise we use the largest blur the user indicates
  2398. // they might need:
  2399. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  2400. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  2401. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  2402. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  2403. // Here's a helpful chart:
  2404. // MaxTriadSize BlurSize MinTriadCountsByResolution
  2405. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2406. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2407. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2408. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2409. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2410. /////////////////////////////// USER PARAMETERS //////////////////////////////
  2411. // Note: Many of these static parameters are overridden by runtime shader
  2412. // parameters when those are enabled. However, many others are static codepath
  2413. // options that were cleaner or more convert to code as static constants.
  2414. // GAMMA:
  2415. static const float crt_gamma_static = 2.5; // range [1, 5]
  2416. static const float lcd_gamma_static = 2.2; // range [1, 5]
  2417. // LEVELS MANAGEMENT:
  2418. // Control the final multiplicative image contrast:
  2419. static const float levels_contrast_static = 1.0; // range [0, 4)
  2420. // We auto-dim to avoid clipping between passes and restore brightness
  2421. // later. Control the dim factor here: Lower values clip less but crush
  2422. // blacks more (static only for now).
  2423. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  2424. // HALATION/DIFFUSION/BLOOM:
  2425. // Halation weight: How much energy should be lost to electrons bounding
  2426. // around under the CRT glass and exciting random phosphors?
  2427. static const float halation_weight_static = 0.0; // range [0, 1]
  2428. // Refractive diffusion weight: How much light should spread/diffuse from
  2429. // refracting through the CRT glass?
  2430. static const float diffusion_weight_static = 0.075; // range [0, 1]
  2431. // Underestimate brightness: Bright areas bloom more, but we can base the
  2432. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  2433. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  2434. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  2435. // Blur all colors more than necessary for a softer phosphor bloom?
  2436. static const float bloom_excess_static = 0.0; // range [0, 1]
  2437. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  2438. // blurred resize of the input (convergence offsets are applied as well).
  2439. // There are three filter options (static option only for now):
  2440. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  2441. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  2442. // and beam_max_sigma is low.
  2443. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  2444. // always uses a static sigma regardless of beam_max_sigma or
  2445. // mask_num_triads_desired.
  2446. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  2447. // These options are more pronounced for the fast, unbloomed shader version.
  2448. #ifndef RADEON_FIX
  2449. static const float bloom_approx_filter_static = 2.0;
  2450. #else
  2451. static const float bloom_approx_filter_static = 1.0;
  2452. #endif
  2453. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  2454. // How many scanlines should contribute light to each pixel? Using more
  2455. // scanlines is slower (especially for a generalized Gaussian) but less
  2456. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  2457. // max_beam_sigma at which the closest unused weight is guaranteed <
  2458. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  2459. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  2460. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  2461. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  2462. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  2463. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  2464. static const float beam_num_scanlines = 3.0; // range [2, 6]
  2465. // A generalized Gaussian beam varies shape with color too, now just width.
  2466. // It's slower but more flexible (static option only for now).
  2467. static const bool beam_generalized_gaussian = true;
  2468. // What kind of scanline antialiasing do you want?
  2469. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  2470. // Integrals are slow (especially for generalized Gaussians) and rarely any
  2471. // better than 3x antialiasing (static option only for now).
  2472. static const float beam_antialias_level = 1.0; // range [0, 2]
  2473. // Min/max standard deviations for scanline beams: Higher values widen and
  2474. // soften scanlines. Depending on other options, low min sigmas can alias.
  2475. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  2476. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  2477. // Beam width varies as a function of color: A power function (0) is more
  2478. // configurable, but a spherical function (1) gives the widest beam
  2479. // variability without aliasing (static option only for now).
  2480. static const float beam_spot_shape_function = 0.0;
  2481. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  2482. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  2483. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  2484. // Generalized Gaussian max shape parameters: Higher values give flatter
  2485. // scanline plateaus and steeper dropoffs, simultaneously widening and
  2486. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  2487. // values > ~40.0 cause artifacts with integrals.
  2488. static const float beam_min_shape_static = 2.0; // range [2, 32]
  2489. static const float beam_max_shape_static = 4.0; // range [2, 32]
  2490. // Generalized Gaussian shape power: Affects how quickly the distribution
  2491. // changes shape from Gaussian to steep/plateaued as color increases from 0
  2492. // to 1.0. Higher powers appear softer for most colors, and lower powers
  2493. // appear sharper for most colors.
  2494. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  2495. // What filter should be used to sample scanlines horizontally?
  2496. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  2497. static const float beam_horiz_filter_static = 0.0;
  2498. // Standard deviation for horizontal Gaussian resampling:
  2499. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  2500. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  2501. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  2502. // limiting circuitry in some CRT's), or a weighted avg.?
  2503. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  2504. // Simulate scanline misconvergence? This needs 3x horizontal texture
  2505. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  2506. // later passes (static option only for now).
  2507. static const bool beam_misconvergence = true;
  2508. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  2509. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  2510. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  2511. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  2512. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  2513. // Detect interlacing (static option only for now)?
  2514. static const bool interlace_detect = true;
  2515. // Assume 1080-line sources are interlaced?
  2516. static const bool interlace_1080i_static = false;
  2517. // For interlaced sources, assume TFF (top-field first) or BFF order?
  2518. // (Whether this matters depends on the nature of the interlaced input.)
  2519. static const bool interlace_bff_static = false;
  2520. // ANTIALIASING:
  2521. // What AA level do you want for curvature/overscan/subpixels? Options:
  2522. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  2523. // (Static option only for now)
  2524. static const float aa_level = 12.0; // range [0, 24]
  2525. // What antialiasing filter do you want (static option only)? Options:
  2526. // 0: Box (separable), 1: Box (cylindrical),
  2527. // 2: Tent (separable), 3: Tent (cylindrical),
  2528. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  2529. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  2530. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  2531. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  2532. static const float aa_filter = 6.0; // range [0, 9]
  2533. // Flip the sample grid on odd/even frames (static option only for now)?
  2534. static const bool aa_temporal = false;
  2535. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  2536. // the blue offset is the negative r offset; range [0, 0.5]
  2537. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  2538. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  2539. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  2540. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  2541. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  2542. // 4.) C = 0.0 is a soft spline filter.
  2543. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  2544. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  2545. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  2546. // PHOSPHOR MASK:
  2547. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  2548. static const float mask_type_static = 1.0; // range [0, 2]
  2549. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  2550. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  2551. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  2552. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  2553. // is halfway decent with LUT mipmapping but atrocious without it.
  2554. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  2555. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  2556. // This mode reuses the same masks, so triads will be enormous unless
  2557. // you change the mask LUT filenames in your .cgp file.
  2558. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  2559. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  2560. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  2561. // will always be used to calculate the full bloom sigma statically.
  2562. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  2563. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  2564. // triads) will be rounded to the nearest integer tile size and clamped to
  2565. // obey minimum size constraints (imposed to reduce downsize taps) and
  2566. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  2567. // To increase the size limit, double the viewport-relative scales for the
  2568. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  2569. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2570. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  2571. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  2572. // final size will be rounded and constrained as above); default 480.0
  2573. static const float mask_num_triads_desired_static = 480.0;
  2574. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  2575. // more samples and avoid moire a bit better, but some is unavoidable
  2576. // depending on the destination size (static option for now).
  2577. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  2578. // The mask is resized using a variable number of taps in each dimension,
  2579. // but some Cg profiles always fetch a constant number of taps no matter
  2580. // what (no dynamic branching). We can limit the maximum number of taps if
  2581. // we statically limit the minimum phosphor triad size. Larger values are
  2582. // faster, but the limit IS enforced (static option only, forever);
  2583. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2584. // TODO: Make this 1.0 and compensate with smarter sampling!
  2585. static const float mask_min_allowed_triad_size = 2.0;
  2586. // GEOMETRY:
  2587. // Geometry mode:
  2588. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  2589. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  2590. static const float geom_mode_static = 0.0; // range [0, 3]
  2591. // Radius of curvature: Measured in units of your viewport's diagonal size.
  2592. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  2593. // View dist is the distance from the player to their physical screen, in
  2594. // units of the viewport's diagonal size. It controls the field of view.
  2595. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  2596. // Tilt angle in radians (clockwise around up and right vectors):
  2597. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  2598. // Aspect ratio: When the true viewport size is unknown, this value is used
  2599. // to help convert between the phosphor triad size and count, along with
  2600. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  2601. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  2602. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  2603. // default (256/224)*(54/47) = 1.313069909 (see below)
  2604. static const float geom_aspect_ratio_static = 1.313069909;
  2605. // Before getting into overscan, here's some general aspect ratio info:
  2606. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  2607. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  2608. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  2609. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  2610. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  2611. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  2612. // a.) Enable Retroarch's "Crop Overscan"
  2613. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  2614. // Real consoles use horizontal black padding in the signal, but emulators
  2615. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  2616. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  2617. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  2618. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  2619. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  2620. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  2621. // without doing a. or b., but horizontal image borders will be tighter
  2622. // than vertical ones, messing up curvature and overscan. Fixing the
  2623. // padding first corrects this.
  2624. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  2625. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  2626. // above: Values < 1.0 zoom out; range (0, inf)
  2627. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  2628. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  2629. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  2630. // with strong curvature (static option only for now).
  2631. static const bool geom_force_correct_tangent_matrix = true;
  2632. // BORDERS:
  2633. // Rounded border size in texture uv coords:
  2634. static const float border_size_static = 0.015; // range [0, 0.5]
  2635. // Border darkness: Moderate values darken the border smoothly, and high
  2636. // values make the image very dark just inside the border:
  2637. static const float border_darkness_static = 2.0; // range [0, inf)
  2638. // Border compression: High numbers compress border transitions, narrowing
  2639. // the dark border area.
  2640. static const float border_compress_static = 2.5; // range [1, inf)
  2641. #endif // USER_SETTINGS_H
  2642. //////////////////////////// END USER-SETTINGS //////////////////////////
  2643. //#include "derived-settings-and-constants.h"
  2644. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  2645. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  2646. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  2647. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  2648. // crt-royale: A full-featured CRT shader, with cheese.
  2649. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  2650. //
  2651. // This program is free software; you can redistribute it and/or modify it
  2652. // under the terms of the GNU General Public License as published by the Free
  2653. // Software Foundation; either version 2 of the License, or any later version.
  2654. //
  2655. // This program is distributed in the hope that it will be useful, but WITHOUT
  2656. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  2657. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  2658. // more details.
  2659. //
  2660. // You should have received a copy of the GNU General Public License along with
  2661. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  2662. // Place, Suite 330, Boston, MA 02111-1307 USA
  2663. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  2664. // These macros and constants can be used across the whole codebase.
  2665. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  2666. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  2667. //#include "../user-settings.h"
  2668. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  2669. #ifndef USER_SETTINGS_H
  2670. #define USER_SETTINGS_H
  2671. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  2672. // The Cg compiler uses different "profiles" with different capabilities.
  2673. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  2674. // require higher profiles like fp30 or fp40. The shader can't detect profile
  2675. // or driver capabilities, so instead you must comment or uncomment the lines
  2676. // below with "//" before "#define." Disable an option if you get compilation
  2677. // errors resembling those listed. Generally speaking, all of these options
  2678. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  2679. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  2680. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  2681. // Among other things, derivatives help us fix anisotropic filtering artifacts
  2682. // with curved manually tiled phosphor mask coords. Related errors:
  2683. // error C3004: function "float2 ddx(float2);" not supported in this profile
  2684. // error C3004: function "float2 ddy(float2);" not supported in this profile
  2685. //#define DRIVERS_ALLOW_DERIVATIVES
  2686. // Fine derivatives: Unsupported on older ATI cards.
  2687. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  2688. // fast single-pass blur operations. If your card uses coarse derivatives and
  2689. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  2690. #ifdef DRIVERS_ALLOW_DERIVATIVES
  2691. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  2692. #endif
  2693. // Dynamic looping: Requires an fp30 or newer profile.
  2694. // This makes phosphor mask resampling faster in some cases. Related errors:
  2695. // error C5013: profile does not support "for" statements and "for" could not
  2696. // be unrolled
  2697. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2698. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  2699. // Using one static loop avoids overhead if the user is right, but if the user
  2700. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  2701. // binary search can potentially save some iterations. However, it may fail:
  2702. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  2703. // needed to compile program
  2704. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  2705. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  2706. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  2707. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  2708. // this profile
  2709. //#define DRIVERS_ALLOW_TEX2DLOD
  2710. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  2711. // artifacts from anisotropic filtering and mipmapping. Related errors:
  2712. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  2713. // in this profile
  2714. //#define DRIVERS_ALLOW_TEX2DBIAS
  2715. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  2716. // impose stricter limitations on register counts and instructions. Enable
  2717. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  2718. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  2719. // to compile program.
  2720. // Enabling integrated graphics compatibility mode will automatically disable:
  2721. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  2722. // (This may be reenabled in a later release.)
  2723. // 2.) RUNTIME_GEOMETRY_MODE
  2724. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  2725. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  2726. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  2727. // To disable a #define option, turn its line into a comment with "//."
  2728. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  2729. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  2730. // many of the options in this file and allow real-time tuning, but many of
  2731. // them are slower. Disabling them and using this text file will boost FPS.
  2732. #define RUNTIME_SHADER_PARAMS_ENABLE
  2733. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  2734. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  2735. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  2736. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  2737. #define RUNTIME_ANTIALIAS_WEIGHTS
  2738. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  2739. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2740. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  2741. // parameters? This will require more math or dynamic branching.
  2742. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  2743. // Specify the tilt at runtime? This makes things about 3% slower.
  2744. #define RUNTIME_GEOMETRY_TILT
  2745. // Specify the geometry mode at runtime?
  2746. #define RUNTIME_GEOMETRY_MODE
  2747. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  2748. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  2749. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  2750. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2751. // PHOSPHOR MASK:
  2752. // Manually resize the phosphor mask for best results (slower)? Disabling this
  2753. // removes the option to do so, but it may be faster without dynamic branches.
  2754. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  2755. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  2756. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  2757. // Larger blurs are expensive, but we need them to blur larger triads. We can
  2758. // detect the right blur if the triad size is static or our profile allows
  2759. // dynamic branches, but otherwise we use the largest blur the user indicates
  2760. // they might need:
  2761. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  2762. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  2763. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  2764. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  2765. // Here's a helpful chart:
  2766. // MaxTriadSize BlurSize MinTriadCountsByResolution
  2767. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2768. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2769. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2770. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2771. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2772. /////////////////////////////// USER PARAMETERS //////////////////////////////
  2773. // Note: Many of these static parameters are overridden by runtime shader
  2774. // parameters when those are enabled. However, many others are static codepath
  2775. // options that were cleaner or more convert to code as static constants.
  2776. // GAMMA:
  2777. static const float crt_gamma_static = 2.5; // range [1, 5]
  2778. static const float lcd_gamma_static = 2.2; // range [1, 5]
  2779. // LEVELS MANAGEMENT:
  2780. // Control the final multiplicative image contrast:
  2781. static const float levels_contrast_static = 1.0; // range [0, 4)
  2782. // We auto-dim to avoid clipping between passes and restore brightness
  2783. // later. Control the dim factor here: Lower values clip less but crush
  2784. // blacks more (static only for now).
  2785. static const float levels_autodim_temp = 0.5; // range (0, 1] default is 0.5 but that was unnecessarily dark for me, so I set it to 1.0
  2786. // HALATION/DIFFUSION/BLOOM:
  2787. // Halation weight: How much energy should be lost to electrons bounding
  2788. // around under the CRT glass and exciting random phosphors?
  2789. static const float halation_weight_static = 0.0; // range [0, 1]
  2790. // Refractive diffusion weight: How much light should spread/diffuse from
  2791. // refracting through the CRT glass?
  2792. static const float diffusion_weight_static = 0.075; // range [0, 1]
  2793. // Underestimate brightness: Bright areas bloom more, but we can base the
  2794. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  2795. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  2796. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  2797. // Blur all colors more than necessary for a softer phosphor bloom?
  2798. static const float bloom_excess_static = 0.0; // range [0, 1]
  2799. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  2800. // blurred resize of the input (convergence offsets are applied as well).
  2801. // There are three filter options (static option only for now):
  2802. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  2803. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  2804. // and beam_max_sigma is low.
  2805. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  2806. // always uses a static sigma regardless of beam_max_sigma or
  2807. // mask_num_triads_desired.
  2808. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  2809. // These options are more pronounced for the fast, unbloomed shader version.
  2810. #ifndef RADEON_FIX
  2811. static const float bloom_approx_filter_static = 2.0;
  2812. #else
  2813. static const float bloom_approx_filter_static = 1.0;
  2814. #endif
  2815. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  2816. // How many scanlines should contribute light to each pixel? Using more
  2817. // scanlines is slower (especially for a generalized Gaussian) but less
  2818. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  2819. // max_beam_sigma at which the closest unused weight is guaranteed <
  2820. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  2821. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  2822. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  2823. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  2824. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  2825. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  2826. static const float beam_num_scanlines = 3.0; // range [2, 6]
  2827. // A generalized Gaussian beam varies shape with color too, now just width.
  2828. // It's slower but more flexible (static option only for now).
  2829. static const bool beam_generalized_gaussian = true;
  2830. // What kind of scanline antialiasing do you want?
  2831. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  2832. // Integrals are slow (especially for generalized Gaussians) and rarely any
  2833. // better than 3x antialiasing (static option only for now).
  2834. static const float beam_antialias_level = 1.0; // range [0, 2]
  2835. // Min/max standard deviations for scanline beams: Higher values widen and
  2836. // soften scanlines. Depending on other options, low min sigmas can alias.
  2837. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  2838. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  2839. // Beam width varies as a function of color: A power function (0) is more
  2840. // configurable, but a spherical function (1) gives the widest beam
  2841. // variability without aliasing (static option only for now).
  2842. static const float beam_spot_shape_function = 0.0;
  2843. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  2844. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  2845. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  2846. // Generalized Gaussian max shape parameters: Higher values give flatter
  2847. // scanline plateaus and steeper dropoffs, simultaneously widening and
  2848. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  2849. // values > ~40.0 cause artifacts with integrals.
  2850. static const float beam_min_shape_static = 2.0; // range [2, 32]
  2851. static const float beam_max_shape_static = 4.0; // range [2, 32]
  2852. // Generalized Gaussian shape power: Affects how quickly the distribution
  2853. // changes shape from Gaussian to steep/plateaued as color increases from 0
  2854. // to 1.0. Higher powers appear softer for most colors, and lower powers
  2855. // appear sharper for most colors.
  2856. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  2857. // What filter should be used to sample scanlines horizontally?
  2858. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  2859. static const float beam_horiz_filter_static = 0.0;
  2860. // Standard deviation for horizontal Gaussian resampling:
  2861. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  2862. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  2863. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  2864. // limiting circuitry in some CRT's), or a weighted avg.?
  2865. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  2866. // Simulate scanline misconvergence? This needs 3x horizontal texture
  2867. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  2868. // later passes (static option only for now).
  2869. static const bool beam_misconvergence = true;
  2870. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  2871. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  2872. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  2873. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  2874. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  2875. // Detect interlacing (static option only for now)?
  2876. static const bool interlace_detect = true;
  2877. // Assume 1080-line sources are interlaced?
  2878. static const bool interlace_1080i_static = false;
  2879. // For interlaced sources, assume TFF (top-field first) or BFF order?
  2880. // (Whether this matters depends on the nature of the interlaced input.)
  2881. static const bool interlace_bff_static = false;
  2882. // ANTIALIASING:
  2883. // What AA level do you want for curvature/overscan/subpixels? Options:
  2884. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  2885. // (Static option only for now)
  2886. static const float aa_level = 12.0; // range [0, 24]
  2887. // What antialiasing filter do you want (static option only)? Options:
  2888. // 0: Box (separable), 1: Box (cylindrical),
  2889. // 2: Tent (separable), 3: Tent (cylindrical),
  2890. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  2891. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  2892. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  2893. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  2894. static const float aa_filter = 6.0; // range [0, 9]
  2895. // Flip the sample grid on odd/even frames (static option only for now)?
  2896. static const bool aa_temporal = false;
  2897. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  2898. // the blue offset is the negative r offset; range [0, 0.5]
  2899. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  2900. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  2901. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  2902. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  2903. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  2904. // 4.) C = 0.0 is a soft spline filter.
  2905. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  2906. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  2907. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  2908. // PHOSPHOR MASK:
  2909. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  2910. static const float mask_type_static = 1.0; // range [0, 2]
  2911. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  2912. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  2913. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  2914. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  2915. // is halfway decent with LUT mipmapping but atrocious without it.
  2916. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  2917. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  2918. // This mode reuses the same masks, so triads will be enormous unless
  2919. // you change the mask LUT filenames in your .cgp file.
  2920. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  2921. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  2922. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  2923. // will always be used to calculate the full bloom sigma statically.
  2924. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  2925. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  2926. // triads) will be rounded to the nearest integer tile size and clamped to
  2927. // obey minimum size constraints (imposed to reduce downsize taps) and
  2928. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  2929. // To increase the size limit, double the viewport-relative scales for the
  2930. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  2931. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2932. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  2933. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  2934. // final size will be rounded and constrained as above); default 480.0
  2935. static const float mask_num_triads_desired_static = 480.0;
  2936. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  2937. // more samples and avoid moire a bit better, but some is unavoidable
  2938. // depending on the destination size (static option for now).
  2939. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  2940. // The mask is resized using a variable number of taps in each dimension,
  2941. // but some Cg profiles always fetch a constant number of taps no matter
  2942. // what (no dynamic branching). We can limit the maximum number of taps if
  2943. // we statically limit the minimum phosphor triad size. Larger values are
  2944. // faster, but the limit IS enforced (static option only, forever);
  2945. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2946. // TODO: Make this 1.0 and compensate with smarter sampling!
  2947. static const float mask_min_allowed_triad_size = 2.0;
  2948. // GEOMETRY:
  2949. // Geometry mode:
  2950. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  2951. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  2952. static const float geom_mode_static = 0.0; // range [0, 3]
  2953. // Radius of curvature: Measured in units of your viewport's diagonal size.
  2954. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  2955. // View dist is the distance from the player to their physical screen, in
  2956. // units of the viewport's diagonal size. It controls the field of view.
  2957. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  2958. // Tilt angle in radians (clockwise around up and right vectors):
  2959. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  2960. // Aspect ratio: When the true viewport size is unknown, this value is used
  2961. // to help convert between the phosphor triad size and count, along with
  2962. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  2963. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  2964. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  2965. // default (256/224)*(54/47) = 1.313069909 (see below)
  2966. static const float geom_aspect_ratio_static = 1.313069909;
  2967. // Before getting into overscan, here's some general aspect ratio info:
  2968. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  2969. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  2970. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  2971. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  2972. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  2973. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  2974. // a.) Enable Retroarch's "Crop Overscan"
  2975. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  2976. // Real consoles use horizontal black padding in the signal, but emulators
  2977. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  2978. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  2979. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  2980. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  2981. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  2982. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  2983. // without doing a. or b., but horizontal image borders will be tighter
  2984. // than vertical ones, messing up curvature and overscan. Fixing the
  2985. // padding first corrects this.
  2986. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  2987. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  2988. // above: Values < 1.0 zoom out; range (0, inf)
  2989. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  2990. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  2991. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  2992. // with strong curvature (static option only for now).
  2993. static const bool geom_force_correct_tangent_matrix = true;
  2994. // BORDERS:
  2995. // Rounded border size in texture uv coords:
  2996. static const float border_size_static = 0.015; // range [0, 0.5]
  2997. // Border darkness: Moderate values darken the border smoothly, and high
  2998. // values make the image very dark just inside the border:
  2999. static const float border_darkness_static = 2.0; // range [0, inf)
  3000. // Border compression: High numbers compress border transitions, narrowing
  3001. // the dark border area.
  3002. static const float border_compress_static = 2.5; // range [1, inf)
  3003. #endif // USER_SETTINGS_H
  3004. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  3005. //#include "user-cgp-constants.h"
  3006. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  3007. #ifndef USER_CGP_CONSTANTS_H
  3008. #define USER_CGP_CONSTANTS_H
  3009. // IMPORTANT:
  3010. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  3011. // (or whatever related .cgp file you're using). If they aren't, you're likely
  3012. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  3013. // set directly in the .cgp file to make things easier, but...they can't.
  3014. // PASS SCALES AND RELATED CONSTANTS:
  3015. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  3016. // this shader: One does a viewport-scale bloom, and the other skips it. The
  3017. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  3018. static const float bloom_approx_size_x = 320.0;
  3019. static const float bloom_approx_size_x_for_fake = 400.0;
  3020. // Copy the viewport-relative scales of the phosphor mask resize passes
  3021. // (MASK_RESIZE and the pass immediately preceding it):
  3022. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  3023. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  3024. static const float geom_max_aspect_ratio = 4.0/3.0;
  3025. // PHOSPHOR MASK TEXTURE CONSTANTS:
  3026. // Set the following constants to reflect the properties of the phosphor mask
  3027. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  3028. // based on user settings, then repeats a single tile until filling the screen.
  3029. // The shader must know the input texture size (default 64x64), and to manually
  3030. // resize, it must also know the horizontal triads per tile (default 8).
  3031. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  3032. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  3033. static const float mask_triads_per_tile = 8.0;
  3034. // We need the average brightness of the phosphor mask to compensate for the
  3035. // dimming it causes. The following four values are roughly correct for the
  3036. // masks included with the shader. Update the value for any LUT texture you
  3037. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  3038. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  3039. //#define PHOSPHOR_MASK_GRILLE14
  3040. static const float mask_grille14_avg_color = 50.6666666/255.0;
  3041. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  3042. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  3043. static const float mask_grille15_avg_color = 53.0/255.0;
  3044. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  3045. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  3046. static const float mask_slot_avg_color = 46.0/255.0;
  3047. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  3048. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  3049. static const float mask_shadow_avg_color = 41.0/255.0;
  3050. // TileableLinearShadowMask*.png
  3051. // TileableLinearShadowMaskEDP*.png
  3052. #ifdef PHOSPHOR_MASK_GRILLE14
  3053. static const float mask_grille_avg_color = mask_grille14_avg_color;
  3054. #else
  3055. static const float mask_grille_avg_color = mask_grille15_avg_color;
  3056. #endif
  3057. #endif // USER_CGP_CONSTANTS_H
  3058. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  3059. //////////////////////////////// END INCLUDES ////////////////////////////////
  3060. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  3061. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  3062. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  3063. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  3064. #ifndef SIMULATE_CRT_ON_LCD
  3065. #define SIMULATE_CRT_ON_LCD
  3066. #endif
  3067. // Manually tiling a manually resized texture creates texture coord derivative
  3068. // discontinuities and confuses anisotropic filtering, causing discolored tile
  3069. // seams in the phosphor mask. Workarounds:
  3070. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  3071. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  3072. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  3073. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  3074. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  3075. // (Same-pass curvature isn't used but could be in the future...maybe.)
  3076. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  3077. // border padding to the resized mask FBO, but it works with same-pass
  3078. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  3079. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  3080. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  3081. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  3082. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  3083. // Also, manually resampling the phosphor mask is slightly blurrier with
  3084. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  3085. // creates artifacts, but only with the fully bloomed shader.) The difference
  3086. // is subtle with small triads, but you can fix it for a small cost.
  3087. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3088. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  3089. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  3090. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  3091. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  3092. // #defined by either user-settings.h or a wrapper .cg that #includes the
  3093. // current .cg pass.)
  3094. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  3095. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  3096. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  3097. #endif
  3098. #ifdef RUNTIME_GEOMETRY_MODE
  3099. #undef RUNTIME_GEOMETRY_MODE
  3100. #endif
  3101. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  3102. // inferior in most cases, so replace 2.0 with 0.0:
  3103. static const float bloom_approx_filter =
  3104. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  3105. #else
  3106. static const float bloom_approx_filter = bloom_approx_filter_static;
  3107. #endif
  3108. // Disable slow runtime paths if static parameters are used. Most of these
  3109. // won't be a problem anyway once the params are disabled, but some will.
  3110. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  3111. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  3112. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  3113. #endif
  3114. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  3115. #undef RUNTIME_ANTIALIAS_WEIGHTS
  3116. #endif
  3117. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  3118. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  3119. #endif
  3120. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  3121. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  3122. #endif
  3123. #ifdef RUNTIME_GEOMETRY_TILT
  3124. #undef RUNTIME_GEOMETRY_TILT
  3125. #endif
  3126. #ifdef RUNTIME_GEOMETRY_MODE
  3127. #undef RUNTIME_GEOMETRY_MODE
  3128. #endif
  3129. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3130. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3131. #endif
  3132. #endif
  3133. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  3134. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3135. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3136. #endif
  3137. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3138. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3139. #endif
  3140. // Rule out unavailable anisotropic compatibility strategies:
  3141. #ifndef DRIVERS_ALLOW_DERIVATIVES
  3142. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3143. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3144. #endif
  3145. #endif
  3146. #ifndef DRIVERS_ALLOW_TEX2DLOD
  3147. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3148. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3149. #endif
  3150. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3151. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3152. #endif
  3153. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  3154. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  3155. #endif
  3156. #endif
  3157. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  3158. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3159. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3160. #endif
  3161. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3162. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3163. #endif
  3164. #endif
  3165. // Prioritize anisotropic tiling compatibility strategies by performance and
  3166. // disable unused strategies. This concentrates all the nesting in one place.
  3167. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3168. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3169. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3170. #endif
  3171. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3172. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3173. #endif
  3174. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3175. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3176. #endif
  3177. #else
  3178. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3179. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3180. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3181. #endif
  3182. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3183. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3184. #endif
  3185. #else
  3186. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  3187. // flat texture coords in the same pass, but that's all we use.
  3188. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3189. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3190. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3191. #endif
  3192. #endif
  3193. #endif
  3194. #endif
  3195. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  3196. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  3197. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3198. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3199. #endif
  3200. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3201. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3202. #endif
  3203. // Prioritize anisotropic resampling compatibility strategies the same way:
  3204. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3205. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3206. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3207. #endif
  3208. #endif
  3209. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  3210. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  3211. // should: It gives us more options for using fewer samples.
  3212. #ifdef DRIVERS_ALLOW_TEX2DLOD
  3213. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3214. // TODO: Take advantage of this!
  3215. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  3216. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  3217. #else
  3218. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  3219. #endif
  3220. #else
  3221. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  3222. #endif
  3223. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  3224. // main_fragment, or a static alias of one of the above. This makes it hard
  3225. // to select the phosphor mask at runtime: We can't even assign to a uniform
  3226. // global in the vertex shader or select a sampler2D in the vertex shader and
  3227. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  3228. // because it just gives us the input texture or a black screen. However, we
  3229. // can get around these limitations by calling tex2D three times with different
  3230. // uniform samplers (or resizing the phosphor mask three times altogether).
  3231. // With dynamic branches, we can process only one of these branches on top of
  3232. // quickly discarding fragments we don't need (cgc seems able to overcome
  3233. // limigations around dependent texture fetches inside of branches). Without
  3234. // dynamic branches, we have to process every branch for every fragment...which
  3235. // is slower. Runtime sampling mode selection is slower without dynamic
  3236. // branches as well. Let the user's static #defines decide if it's worth it.
  3237. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  3238. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3239. #else
  3240. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3241. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3242. #endif
  3243. #endif
  3244. // We need to render some minimum number of tiles in the resize passes.
  3245. // We need at least 1.0 just to repeat a single tile, and we need extra
  3246. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  3247. // antialiasing, same-pass curvature (not currently used), etc. First
  3248. // determine how many border texels and tiles we need, based on how the result
  3249. // will be sampled:
  3250. #ifdef GEOMETRY_EARLY
  3251. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  3252. // Most antialiasing filters have a base radius of 4.0 pixels:
  3253. static const float max_aa_base_pixel_border = 4.0 +
  3254. max_subpixel_offset;
  3255. #else
  3256. static const float max_aa_base_pixel_border = 0.0;
  3257. #endif
  3258. // Anisotropic filtering adds about 0.5 to the pixel border:
  3259. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3260. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  3261. #else
  3262. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  3263. #endif
  3264. // Fixing discontinuities adds 1.0 more to the pixel border:
  3265. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3266. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  3267. #else
  3268. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  3269. #endif
  3270. // Convert the pixel border to an integer texel border. Assume same-pass
  3271. // curvature about triples the texel frequency:
  3272. #ifdef GEOMETRY_EARLY
  3273. static const float max_mask_texel_border =
  3274. ceil(max_tiled_pixel_border * 3.0);
  3275. #else
  3276. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  3277. #endif
  3278. // Convert the texel border to a tile border using worst-case assumptions:
  3279. static const float max_mask_tile_border = max_mask_texel_border/
  3280. (mask_min_allowed_triad_size * mask_triads_per_tile);
  3281. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  3282. // the starting texel (inside borders) for sampling it.
  3283. #ifndef GEOMETRY_EARLY
  3284. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3285. // Special case: Render two tiles without borders. Anisotropic
  3286. // filtering doesn't seem to be a problem here.
  3287. static const float mask_resize_num_tiles = 1.0 + 1.0;
  3288. static const float mask_start_texels = 0.0;
  3289. #else
  3290. static const float mask_resize_num_tiles = 1.0 +
  3291. 2.0 * max_mask_tile_border;
  3292. static const float mask_start_texels = max_mask_texel_border;
  3293. #endif
  3294. #else
  3295. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  3296. static const float mask_start_texels = max_mask_texel_border;
  3297. #endif
  3298. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  3299. // mask_resize_viewport_scale. This limits the maximum final triad size.
  3300. // Estimate the minimum number of triads we can split the screen into in each
  3301. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  3302. static const float mask_resize_num_triads =
  3303. mask_resize_num_tiles * mask_triads_per_tile;
  3304. static const float2 min_allowed_viewport_triads =
  3305. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  3306. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  3307. static const float pi = 3.141592653589;
  3308. // We often want to find the location of the previous texel, e.g.:
  3309. // const float2 curr_texel = uv * texture_size;
  3310. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  3311. // const float2 prev_texel_uv = prev_texel / texture_size;
  3312. // However, many GPU drivers round incorrectly around exact texel locations.
  3313. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  3314. // require this value to be farther from 0.5 than others; define it here.
  3315. // const float2 prev_texel =
  3316. // floor(curr_texel - float2(under_half)) + float2(0.5);
  3317. static const float under_half = 0.4995;
  3318. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  3319. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  3320. //#include "../../../../include/blur-functions.h"
  3321. //////////////////////////// BEGIN BLUR-FUNCTIONS ///////////////////////////
  3322. #ifndef BLUR_FUNCTIONS_H
  3323. #define BLUR_FUNCTIONS_H
  3324. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3325. // Copyright (C) 2014 TroggleMonkey
  3326. //
  3327. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3328. // of this software and associated documentation files (the "Software"), to
  3329. // deal in the Software without restriction, including without limitation the
  3330. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  3331. // sell copies of the Software, and to permit persons to whom the Software is
  3332. // furnished to do so, subject to the following conditions:
  3333. //
  3334. // The above copyright notice and this permission notice shall be included in
  3335. // all copies or substantial portions of the Software.
  3336. //
  3337. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3338. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3339. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3340. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3341. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3342. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3343. // IN THE SOFTWARE.
  3344. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  3345. // This file provides reusable one-pass and separable (two-pass) blurs.
  3346. // Requires: All blurs share these requirements (dxdy requirement is split):
  3347. // 1.) All requirements of gamma-management.h must be satisfied!
  3348. // 2.) filter_linearN must == "true" in your .cgp preset unless
  3349. // you're using tex2DblurNresize at 1x scale.
  3350. // 3.) mipmap_inputN must == "true" in your .cgp preset if
  3351. // output_size < video_size.
  3352. // 4.) output_size == video_size / pow(2, M), where M is some
  3353. // positive integer. tex2Dblur*resize can resize arbitrarily
  3354. // (and the blur will be done after resizing), but arbitrary
  3355. // resizes "fail" with other blurs due to the way they mix
  3356. // static weights with bilinear sample exploitation.
  3357. // 5.) In general, dxdy should contain the uv pixel spacing:
  3358. // dxdy = (video_size/output_size)/texture_size
  3359. // 6.) For separable blurs (tex2DblurNresize and tex2DblurNfast),
  3360. // zero out the dxdy component in the unblurred dimension:
  3361. // dxdy = float2(dxdy.x, 0.0) or float2(0.0, dxdy.y)
  3362. // Many blurs share these requirements:
  3363. // 1.) One-pass blurs require scale_xN == scale_yN or scales > 1.0,
  3364. // or they will blur more in the lower-scaled dimension.
  3365. // 2.) One-pass shared sample blurs require ddx(), ddy(), and
  3366. // tex2Dlod() to be supported by the current Cg profile, and
  3367. // the drivers must support high-quality derivatives.
  3368. // 3.) One-pass shared sample blurs require:
  3369. // tex_uv.w == log2(video_size/output_size).y;
  3370. // Non-wrapper blurs share this requirement:
  3371. // 1.) sigma is the intended standard deviation of the blur
  3372. // Wrapper blurs share this requirement, which is automatically
  3373. // met (unless OVERRIDE_BLUR_STD_DEVS is #defined; see below):
  3374. // 1.) blurN_std_dev must be global static const float values
  3375. // specifying standard deviations for Nx blurs in units
  3376. // of destination pixels
  3377. // Optional: 1.) The including file (or an earlier included file) may
  3378. // optionally #define USE_BINOMIAL_BLUR_STD_DEVS to replace
  3379. // default standard deviations with those matching a binomial
  3380. // distribution. (See below for details/properties.)
  3381. // 2.) The including file (or an earlier included file) may
  3382. // optionally #define OVERRIDE_BLUR_STD_DEVS and override:
  3383. // static const float blur3_std_dev
  3384. // static const float blur4_std_dev
  3385. // static const float blur5_std_dev
  3386. // static const float blur6_std_dev
  3387. // static const float blur7_std_dev
  3388. // static const float blur8_std_dev
  3389. // static const float blur9_std_dev
  3390. // static const float blur10_std_dev
  3391. // static const float blur11_std_dev
  3392. // static const float blur12_std_dev
  3393. // static const float blur17_std_dev
  3394. // static const float blur25_std_dev
  3395. // static const float blur31_std_dev
  3396. // static const float blur43_std_dev
  3397. // 3.) The including file (or an earlier included file) may
  3398. // optionally #define OVERRIDE_ERROR_BLURRING and override:
  3399. // static const float error_blurring
  3400. // This tuning value helps mitigate weighting errors from one-
  3401. // pass shared-sample blurs sharing bilinear samples between
  3402. // fragments. Values closer to 0.0 have "correct" blurriness
  3403. // but allow more artifacts, and values closer to 1.0 blur away
  3404. // artifacts by sampling closer to halfway between texels.
  3405. // UPDATE 6/21/14: The above static constants may now be overridden
  3406. // by non-static uniform constants. This permits exposing blur
  3407. // standard deviations as runtime GUI shader parameters. However,
  3408. // using them keeps weights from being statically computed, and the
  3409. // speed hit depends on the blur: On my machine, uniforms kill over
  3410. // 53% of the framerate with tex2Dblur12x12shared, but they only
  3411. // drop the framerate by about 18% with tex2Dblur11fast.
  3412. // Quality and Performance Comparisons:
  3413. // For the purposes of the following discussion, "no sRGB" means
  3414. // GAMMA_ENCODE_EVERY_FBO is #defined, and "sRGB" means it isn't.
  3415. // 1.) tex2DblurNfast is always faster than tex2DblurNresize.
  3416. // 2.) tex2DblurNresize functions are the only ones that can arbitrarily resize
  3417. // well, because they're the only ones that don't exploit bilinear samples.
  3418. // This also means they're the only functions which can be truly gamma-
  3419. // correct without linear (or sRGB FBO) input, but only at 1x scale.
  3420. // 3.) One-pass shared sample blurs only have a speed advantage without sRGB.
  3421. // They also have some inaccuracies due to their shared-[bilinear-]sample
  3422. // design, which grow increasingly bothersome for smaller blurs and higher-
  3423. // frequency source images (relative to their resolution). I had high
  3424. // hopes for them, but their most realistic use case is limited to quickly
  3425. // reblurring an already blurred input at full resolution. Otherwise:
  3426. // a.) If you're blurring a low-resolution source, you want a better blur.
  3427. // b.) If you're blurring a lower mipmap, you want a better blur.
  3428. // c.) If you're blurring a high-resolution, high-frequency source, you
  3429. // want a better blur.
  3430. // 4.) The one-pass blurs without shared samples grow slower for larger blurs,
  3431. // but they're competitive with separable blurs at 5x5 and smaller, and
  3432. // even tex2Dblur7x7 isn't bad if you're wanting to conserve passes.
  3433. // Here are some framerates from a GeForce 8800GTS. The first pass resizes to
  3434. // viewport size (4x in this test) and linearizes for sRGB codepaths, and the
  3435. // remaining passes perform 6 full blurs. Mipmapped tests are performed at the
  3436. // same scale, so they just measure the cost of mipmapping each FBO (only every
  3437. // other FBO is mipmapped for separable blurs, to mimic realistic usage).
  3438. // Mipmap Neither sRGB+Mipmap sRGB Function
  3439. // 76.0 92.3 131.3 193.7 tex2Dblur3fast
  3440. // 63.2 74.4 122.4 175.5 tex2Dblur3resize
  3441. // 93.7 121.2 159.3 263.2 tex2Dblur3x3
  3442. // 59.7 68.7 115.4 162.1 tex2Dblur3x3resize
  3443. // 63.2 74.4 122.4 175.5 tex2Dblur5fast
  3444. // 49.3 54.8 100.0 132.7 tex2Dblur5resize
  3445. // 59.7 68.7 115.4 162.1 tex2Dblur5x5
  3446. // 64.9 77.2 99.1 137.2 tex2Dblur6x6shared
  3447. // 55.8 63.7 110.4 151.8 tex2Dblur7fast
  3448. // 39.8 43.9 83.9 105.8 tex2Dblur7resize
  3449. // 40.0 44.2 83.2 104.9 tex2Dblur7x7
  3450. // 56.4 65.5 71.9 87.9 tex2Dblur8x8shared
  3451. // 49.3 55.1 99.9 132.5 tex2Dblur9fast
  3452. // 33.3 36.2 72.4 88.0 tex2Dblur9resize
  3453. // 27.8 29.7 61.3 72.2 tex2Dblur9x9
  3454. // 37.2 41.1 52.6 60.2 tex2Dblur10x10shared
  3455. // 44.4 49.5 91.3 117.8 tex2Dblur11fast
  3456. // 28.8 30.8 63.6 75.4 tex2Dblur11resize
  3457. // 33.6 36.5 40.9 45.5 tex2Dblur12x12shared
  3458. // TODO: Fill in benchmarks for new untested blurs.
  3459. // tex2Dblur17fast
  3460. // tex2Dblur25fast
  3461. // tex2Dblur31fast
  3462. // tex2Dblur43fast
  3463. // tex2Dblur3x3resize
  3464. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  3465. // Set static standard deviations, but allow users to override them with their
  3466. // own constants (even non-static uniforms if they're okay with the speed hit):
  3467. #ifndef OVERRIDE_BLUR_STD_DEVS
  3468. // blurN_std_dev values are specified in terms of dxdy strides.
  3469. #ifdef USE_BINOMIAL_BLUR_STD_DEVS
  3470. // By request, we can define standard deviations corresponding to a
  3471. // binomial distribution with p = 0.5 (related to Pascal's triangle).
  3472. // This distribution works such that blurring multiple times should
  3473. // have the same result as a single larger blur. These values are
  3474. // larger than default for blurs up to 6x and smaller thereafter.
  3475. static const float blur3_std_dev = 0.84931640625;
  3476. static const float blur4_std_dev = 0.84931640625;
  3477. static const float blur5_std_dev = 1.0595703125;
  3478. static const float blur6_std_dev = 1.06591796875;
  3479. static const float blur7_std_dev = 1.17041015625;
  3480. static const float blur8_std_dev = 1.1720703125;
  3481. static const float blur9_std_dev = 1.2259765625;
  3482. static const float blur10_std_dev = 1.21982421875;
  3483. static const float blur11_std_dev = 1.25361328125;
  3484. static const float blur12_std_dev = 1.2423828125;
  3485. static const float blur17_std_dev = 1.27783203125;
  3486. static const float blur25_std_dev = 1.2810546875;
  3487. static const float blur31_std_dev = 1.28125;
  3488. static const float blur43_std_dev = 1.28125;
  3489. #else
  3490. // The defaults are the largest values that keep the largest unused
  3491. // blur term on each side <= 1.0/256.0. (We could get away with more
  3492. // or be more conservative, but this compromise is pretty reasonable.)
  3493. static const float blur3_std_dev = 0.62666015625;
  3494. static const float blur4_std_dev = 0.66171875;
  3495. static const float blur5_std_dev = 0.9845703125;
  3496. static const float blur6_std_dev = 1.02626953125;
  3497. static const float blur7_std_dev = 1.36103515625;
  3498. static const float blur8_std_dev = 1.4080078125;
  3499. static const float blur9_std_dev = 1.7533203125;
  3500. static const float blur10_std_dev = 1.80478515625;
  3501. static const float blur11_std_dev = 2.15986328125;
  3502. static const float blur12_std_dev = 2.215234375;
  3503. static const float blur17_std_dev = 3.45535583496;
  3504. static const float blur25_std_dev = 5.3409576416;
  3505. static const float blur31_std_dev = 6.86488037109;
  3506. static const float blur43_std_dev = 10.1852050781;
  3507. #endif // USE_BINOMIAL_BLUR_STD_DEVS
  3508. #endif // OVERRIDE_BLUR_STD_DEVS
  3509. #ifndef OVERRIDE_ERROR_BLURRING
  3510. // error_blurring should be in [0.0, 1.0]. Higher values reduce ringing
  3511. // in shared-sample blurs but increase blurring and feature shifting.
  3512. static const float error_blurring = 0.5;
  3513. #endif
  3514. ////////////////////////////////// INCLUDES //////////////////////////////////
  3515. // gamma-management.h relies on pass-specific settings to guide its behavior:
  3516. // FIRST_PASS, LAST_PASS, GAMMA_ENCODE_EVERY_FBO, etc. See it for details.
  3517. //#include "gamma-management.h"
  3518. //////////////////////////// BEGIN GAMMA-MANAGEMENT //////////////////////////
  3519. #ifndef GAMMA_MANAGEMENT_H
  3520. #define GAMMA_MANAGEMENT_H
  3521. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3522. // Copyright (C) 2014 TroggleMonkey
  3523. //
  3524. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3525. // of this software and associated documentation files (the "Software"), to
  3526. // deal in the Software without restriction, including without limitation the
  3527. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  3528. // sell copies of the Software, and to permit persons to whom the Software is
  3529. // furnished to do so, subject to the following conditions:
  3530. //
  3531. // The above copyright notice and this permission notice shall be included in
  3532. // all copies or substantial portions of the Software.
  3533. //
  3534. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3535. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3536. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3537. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3538. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3539. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3540. // IN THE SOFTWARE.
  3541. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  3542. // This file provides gamma-aware tex*D*() and encode_output() functions.
  3543. // Requires: Before #include-ing this file, the including file must #define
  3544. // the following macros when applicable and follow their rules:
  3545. // 1.) #define FIRST_PASS if this is the first pass.
  3546. // 2.) #define LAST_PASS if this is the last pass.
  3547. // 3.) If sRGB is available, set srgb_framebufferN = "true" for
  3548. // every pass except the last in your .cgp preset.
  3549. // 4.) If sRGB isn't available but you want gamma-correctness with
  3550. // no banding, #define GAMMA_ENCODE_EVERY_FBO each pass.
  3551. // 5.) #define SIMULATE_CRT_ON_LCD if desired (precedence over 5-7)
  3552. // 6.) #define SIMULATE_GBA_ON_LCD if desired (precedence over 6-7)
  3553. // 7.) #define SIMULATE_LCD_ON_CRT if desired (precedence over 7)
  3554. // 8.) #define SIMULATE_GBA_ON_CRT if desired (precedence over -)
  3555. // If an option in [5, 8] is #defined in the first or last pass, it
  3556. // should be #defined for both. It shouldn't make a difference
  3557. // whether it's #defined for intermediate passes or not.
  3558. // Optional: The including file (or an earlier included file) may optionally
  3559. // #define a number of macros indicating it will override certain
  3560. // macros and associated constants are as follows:
  3561. // static constants with either static or uniform constants. The
  3562. // 1.) OVERRIDE_STANDARD_GAMMA: The user must first define:
  3563. // static const float ntsc_gamma
  3564. // static const float pal_gamma
  3565. // static const float crt_reference_gamma_high
  3566. // static const float crt_reference_gamma_low
  3567. // static const float lcd_reference_gamma
  3568. // static const float crt_office_gamma
  3569. // static const float lcd_office_gamma
  3570. // 2.) OVERRIDE_DEVICE_GAMMA: The user must first define:
  3571. // static const float crt_gamma
  3572. // static const float gba_gamma
  3573. // static const float lcd_gamma
  3574. // 3.) OVERRIDE_FINAL_GAMMA: The user must first define:
  3575. // static const float input_gamma
  3576. // static const float intermediate_gamma
  3577. // static const float output_gamma
  3578. // (intermediate_gamma is for GAMMA_ENCODE_EVERY_FBO.)
  3579. // 4.) OVERRIDE_ALPHA_ASSUMPTIONS: The user must first define:
  3580. // static const bool assume_opaque_alpha
  3581. // The gamma constant overrides must be used in every pass or none,
  3582. // and OVERRIDE_FINAL_GAMMA bypasses all of the SIMULATE* macros.
  3583. // OVERRIDE_ALPHA_ASSUMPTIONS may be set on a per-pass basis.
  3584. // Usage: After setting macros appropriately, ignore gamma correction and
  3585. // replace all tex*D*() calls with equivalent gamma-aware
  3586. // tex*D*_linearize calls, except:
  3587. // 1.) When you read an LUT, use regular tex*D or a gamma-specified
  3588. // function, depending on its gamma encoding:
  3589. // tex*D*_linearize_gamma (takes a runtime gamma parameter)
  3590. // 2.) If you must read pass0's original input in a later pass, use
  3591. // tex2D_linearize_ntsc_gamma. If you want to read pass0's
  3592. // input with gamma-corrected bilinear filtering, consider
  3593. // creating a first linearizing pass and reading from the input
  3594. // of pass1 later.
  3595. // Then, return encode_output(color) from every fragment shader.
  3596. // Finally, use the global gamma_aware_bilinear boolean if you want
  3597. // to statically branch based on whether bilinear filtering is
  3598. // gamma-correct or not (e.g. for placing Gaussian blur samples).
  3599. //
  3600. // Detailed Policy:
  3601. // tex*D*_linearize() functions enforce a consistent gamma-management policy
  3602. // based on the FIRST_PASS and GAMMA_ENCODE_EVERY_FBO settings. They assume
  3603. // their input texture has the same encoding characteristics as the input for
  3604. // the current pass (which doesn't apply to the exceptions listed above).
  3605. // Similarly, encode_output() enforces a policy based on the LAST_PASS and
  3606. // GAMMA_ENCODE_EVERY_FBO settings. Together, they result in one of the
  3607. // following two pipelines.
  3608. // Typical pipeline with intermediate sRGB framebuffers:
  3609. // linear_color = pow(pass0_encoded_color, input_gamma);
  3610. // intermediate_output = linear_color; // Automatic sRGB encoding
  3611. // linear_color = intermediate_output; // Automatic sRGB decoding
  3612. // final_output = pow(intermediate_output, 1.0/output_gamma);
  3613. // Typical pipeline without intermediate sRGB framebuffers:
  3614. // linear_color = pow(pass0_encoded_color, input_gamma);
  3615. // intermediate_output = pow(linear_color, 1.0/intermediate_gamma);
  3616. // linear_color = pow(intermediate_output, intermediate_gamma);
  3617. // final_output = pow(intermediate_output, 1.0/output_gamma);
  3618. // Using GAMMA_ENCODE_EVERY_FBO is much slower, but it's provided as a way to
  3619. // easily get gamma-correctness without banding on devices where sRGB isn't
  3620. // supported.
  3621. //
  3622. // Use This Header to Maximize Code Reuse:
  3623. // The purpose of this header is to provide a consistent interface for texture
  3624. // reads and output gamma-encoding that localizes and abstracts away all the
  3625. // annoying details. This greatly reduces the amount of code in each shader
  3626. // pass that depends on the pass number in the .cgp preset or whether sRGB
  3627. // FBO's are being used: You can trivially change the gamma behavior of your
  3628. // whole pass by commenting or uncommenting 1-3 #defines. To reuse the same
  3629. // code in your first, Nth, and last passes, you can even put it all in another
  3630. // header file and #include it from skeleton .cg files that #define the
  3631. // appropriate pass-specific settings.
  3632. //
  3633. // Rationale for Using Three Macros:
  3634. // This file uses GAMMA_ENCODE_EVERY_FBO instead of an opposite macro like
  3635. // SRGB_PIPELINE to ensure sRGB is assumed by default, which hopefully imposes
  3636. // a lower maintenance burden on each pass. At first glance it seems we could
  3637. // accomplish everything with two macros: GAMMA_CORRECT_IN / GAMMA_CORRECT_OUT.
  3638. // This works for simple use cases where input_gamma == output_gamma, but it
  3639. // breaks down for more complex scenarios like CRT simulation, where the pass
  3640. // number determines the gamma encoding of the input and output.
  3641. /////////////////////////////// BASE CONSTANTS ///////////////////////////////
  3642. // Set standard gamma constants, but allow users to override them:
  3643. #ifndef OVERRIDE_STANDARD_GAMMA
  3644. // Standard encoding gammas:
  3645. static const float ntsc_gamma = 2.2; // Best to use NTSC for PAL too?
  3646. static const float pal_gamma = 2.8; // Never actually 2.8 in practice
  3647. // Typical device decoding gammas (only use for emulating devices):
  3648. // CRT/LCD reference gammas are higher than NTSC and Rec.709 video standard
  3649. // gammas: The standards purposely undercorrected for an analog CRT's
  3650. // assumed 2.5 reference display gamma to maintain contrast in assumed
  3651. // [dark] viewing conditions: http://www.poynton.com/PDFs/GammaFAQ.pdf
  3652. // These unstated assumptions about display gamma and perceptual rendering
  3653. // intent caused a lot of confusion, and more modern CRT's seemed to target
  3654. // NTSC 2.2 gamma with circuitry. LCD displays seem to have followed suit
  3655. // (they struggle near black with 2.5 gamma anyway), especially PC/laptop
  3656. // displays designed to view sRGB in bright environments. (Standards are
  3657. // also in flux again with BT.1886, but it's underspecified for displays.)
  3658. static const float crt_reference_gamma_high = 2.5; // In (2.35, 2.55)
  3659. static const float crt_reference_gamma_low = 2.35; // In (2.35, 2.55)
  3660. static const float lcd_reference_gamma = 2.5; // To match CRT
  3661. static const float crt_office_gamma = 2.2; // Circuitry-adjusted for NTSC
  3662. static const float lcd_office_gamma = 2.2; // Approximates sRGB
  3663. #endif // OVERRIDE_STANDARD_GAMMA
  3664. // Assuming alpha == 1.0 might make it easier for users to avoid some bugs,
  3665. // but only if they're aware of it.
  3666. #ifndef OVERRIDE_ALPHA_ASSUMPTIONS
  3667. static const bool assume_opaque_alpha = false;
  3668. #endif
  3669. /////////////////////// DERIVED CONSTANTS AS FUNCTIONS ///////////////////////
  3670. // gamma-management.h should be compatible with overriding gamma values with
  3671. // runtime user parameters, but we can only define other global constants in
  3672. // terms of static constants, not uniform user parameters. To get around this
  3673. // limitation, we need to define derived constants using functions.
  3674. // Set device gamma constants, but allow users to override them:
  3675. #ifdef OVERRIDE_DEVICE_GAMMA
  3676. // The user promises to globally define the appropriate constants:
  3677. inline float get_crt_gamma() { return crt_gamma; }
  3678. inline float get_gba_gamma() { return gba_gamma; }
  3679. inline float get_lcd_gamma() { return lcd_gamma; }
  3680. #else
  3681. inline float get_crt_gamma() { return crt_reference_gamma_high; }
  3682. inline float get_gba_gamma() { return 3.5; } // Game Boy Advance; in (3.0, 4.0)
  3683. inline float get_lcd_gamma() { return lcd_office_gamma; }
  3684. #endif // OVERRIDE_DEVICE_GAMMA
  3685. // Set decoding/encoding gammas for the first/lass passes, but allow overrides:
  3686. #ifdef OVERRIDE_FINAL_GAMMA
  3687. // The user promises to globally define the appropriate constants:
  3688. inline float get_intermediate_gamma() { return intermediate_gamma; }
  3689. inline float get_input_gamma() { return input_gamma; }
  3690. inline float get_output_gamma() { return output_gamma; }
  3691. #else
  3692. // If we gamma-correct every pass, always use ntsc_gamma between passes to
  3693. // ensure middle passes don't need to care if anything is being simulated:
  3694. inline float get_intermediate_gamma() { return ntsc_gamma; }
  3695. #ifdef SIMULATE_CRT_ON_LCD
  3696. inline float get_input_gamma() { return get_crt_gamma(); }
  3697. inline float get_output_gamma() { return get_lcd_gamma(); }
  3698. #else
  3699. #ifdef SIMULATE_GBA_ON_LCD
  3700. inline float get_input_gamma() { return get_gba_gamma(); }
  3701. inline float get_output_gamma() { return get_lcd_gamma(); }
  3702. #else
  3703. #ifdef SIMULATE_LCD_ON_CRT
  3704. inline float get_input_gamma() { return get_lcd_gamma(); }
  3705. inline float get_output_gamma() { return get_crt_gamma(); }
  3706. #else
  3707. #ifdef SIMULATE_GBA_ON_CRT
  3708. inline float get_input_gamma() { return get_gba_gamma(); }
  3709. inline float get_output_gamma() { return get_crt_gamma(); }
  3710. #else // Don't simulate anything:
  3711. inline float get_input_gamma() { return ntsc_gamma; }
  3712. inline float get_output_gamma() { return ntsc_gamma; }
  3713. #endif // SIMULATE_GBA_ON_CRT
  3714. #endif // SIMULATE_LCD_ON_CRT
  3715. #endif // SIMULATE_GBA_ON_LCD
  3716. #endif // SIMULATE_CRT_ON_LCD
  3717. #endif // OVERRIDE_FINAL_GAMMA
  3718. // Set decoding/encoding gammas for the current pass. Use static constants for
  3719. // linearize_input and gamma_encode_output, because they aren't derived, and
  3720. // they let the compiler do dead-code elimination.
  3721. #ifndef GAMMA_ENCODE_EVERY_FBO
  3722. #ifdef FIRST_PASS
  3723. static const bool linearize_input = true;
  3724. inline float get_pass_input_gamma() { return get_input_gamma(); }
  3725. #else
  3726. static const bool linearize_input = false;
  3727. inline float get_pass_input_gamma() { return 1.0; }
  3728. #endif
  3729. #ifdef LAST_PASS
  3730. static const bool gamma_encode_output = true;
  3731. inline float get_pass_output_gamma() { return get_output_gamma(); }
  3732. #else
  3733. static const bool gamma_encode_output = false;
  3734. inline float get_pass_output_gamma() { return 1.0; }
  3735. #endif
  3736. #else
  3737. static const bool linearize_input = true;
  3738. static const bool gamma_encode_output = true;
  3739. #ifdef FIRST_PASS
  3740. inline float get_pass_input_gamma() { return get_input_gamma(); }
  3741. #else
  3742. inline float get_pass_input_gamma() { return get_intermediate_gamma(); }
  3743. #endif
  3744. #ifdef LAST_PASS
  3745. inline float get_pass_output_gamma() { return get_output_gamma(); }
  3746. #else
  3747. inline float get_pass_output_gamma() { return get_intermediate_gamma(); }
  3748. #endif
  3749. #endif
  3750. // Users might want to know if bilinear filtering will be gamma-correct:
  3751. static const bool gamma_aware_bilinear = !linearize_input;
  3752. ////////////////////// COLOR ENCODING/DECODING FUNCTIONS /////////////////////
  3753. inline float4 encode_output(const float4 color)
  3754. {
  3755. if(gamma_encode_output)
  3756. {
  3757. if(assume_opaque_alpha)
  3758. {
  3759. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), 1.0);
  3760. }
  3761. else
  3762. {
  3763. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), color.a);
  3764. }
  3765. }
  3766. else
  3767. {
  3768. return color;
  3769. }
  3770. }
  3771. inline float4 decode_input(const float4 color)
  3772. {
  3773. if(linearize_input)
  3774. {
  3775. if(assume_opaque_alpha)
  3776. {
  3777. return float4(pow(color.rgb, float3(get_pass_input_gamma())), 1.0);
  3778. }
  3779. else
  3780. {
  3781. return float4(pow(color.rgb, float3(get_pass_input_gamma())), color.a);
  3782. }
  3783. }
  3784. else
  3785. {
  3786. return color;
  3787. }
  3788. }
  3789. inline float4 decode_gamma_input(const float4 color, const float3 gamma)
  3790. {
  3791. if(assume_opaque_alpha)
  3792. {
  3793. return float4(pow(color.rgb, gamma), 1.0);
  3794. }
  3795. else
  3796. {
  3797. return float4(pow(color.rgb, gamma), color.a);
  3798. }
  3799. }
  3800. //TODO/FIXME: I have no idea why replacing the lookup wrappers with this macro fixes the blurs being offset ¯\_(ツ)_/¯
  3801. //#define tex2D_linearize(C, D) decode_input(vec4(COMPAT_TEXTURE(C, D)))
  3802. // EDIT: it's the 'const' in front of the coords that's doing it
  3803. /////////////////////////// TEXTURE LOOKUP WRAPPERS //////////////////////////
  3804. // "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  3805. // Provide a wide array of linearizing texture lookup wrapper functions. The
  3806. // Cg shader spec Retroarch uses only allows for 2D textures, but 1D and 3D
  3807. // lookups are provided for completeness in case that changes someday. Nobody
  3808. // is likely to use the *fetch and *proj functions, but they're included just
  3809. // in case. The only tex*D texture sampling functions omitted are:
  3810. // - tex*Dcmpbias
  3811. // - tex*Dcmplod
  3812. // - tex*DARRAY*
  3813. // - tex*DMS*
  3814. // - Variants returning integers
  3815. // Standard line length restrictions are ignored below for vertical brevity.
  3816. /*
  3817. // tex1D:
  3818. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords)
  3819. { return decode_input(tex1D(tex, tex_coords)); }
  3820. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords)
  3821. { return decode_input(tex1D(tex, tex_coords)); }
  3822. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const int texel_off)
  3823. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  3824. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  3825. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  3826. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy)
  3827. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  3828. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy)
  3829. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  3830. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy, const int texel_off)
  3831. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  3832. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy, const int texel_off)
  3833. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  3834. // tex1Dbias:
  3835. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords)
  3836. { return decode_input(tex1Dbias(tex, tex_coords)); }
  3837. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  3838. { return decode_input(tex1Dbias(tex, tex_coords, texel_off)); }
  3839. // tex1Dfetch:
  3840. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords)
  3841. { return decode_input(tex1Dfetch(tex, tex_coords)); }
  3842. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords, const int texel_off)
  3843. { return decode_input(tex1Dfetch(tex, tex_coords, texel_off)); }
  3844. // tex1Dlod:
  3845. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords)
  3846. { return decode_input(tex1Dlod(tex, tex_coords)); }
  3847. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  3848. { return decode_input(tex1Dlod(tex, tex_coords, texel_off)); }
  3849. // tex1Dproj:
  3850. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords)
  3851. { return decode_input(tex1Dproj(tex, tex_coords)); }
  3852. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords)
  3853. { return decode_input(tex1Dproj(tex, tex_coords)); }
  3854. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  3855. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  3856. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords, const int texel_off)
  3857. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  3858. */
  3859. // tex2D:
  3860. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords)
  3861. { return decode_input(COMPAT_TEXTURE(tex, tex_coords)); }
  3862. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords)
  3863. { return decode_input(COMPAT_TEXTURE(tex, tex_coords.xy)); }
  3864. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords, int texel_off)
  3865. { return decode_input(textureLod(tex, tex_coords, texel_off)); }
  3866. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords, int texel_off)
  3867. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  3868. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy)
  3869. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  3870. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy)
  3871. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  3872. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  3873. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  3874. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  3875. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  3876. // tex2Dbias:
  3877. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords)
  3878. //{ return decode_input(tex2Dbias(tex, tex_coords)); }
  3879. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  3880. //{ return decode_input(tex2Dbias(tex, tex_coords, texel_off)); }
  3881. // tex2Dfetch:
  3882. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords)
  3883. //{ return decode_input(tex2Dfetch(tex, tex_coords)); }
  3884. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords, const int texel_off)
  3885. //{ return decode_input(tex2Dfetch(tex, tex_coords, texel_off)); }
  3886. // tex2Dlod:
  3887. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords)
  3888. { return decode_input(textureLod(tex, tex_coords.xy, 0.0)); }
  3889. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords, int texel_off)
  3890. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  3891. /*
  3892. // tex2Dproj:
  3893. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords)
  3894. { return decode_input(tex2Dproj(tex, tex_coords)); }
  3895. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords)
  3896. { return decode_input(tex2Dproj(tex, tex_coords)); }
  3897. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords, const int texel_off)
  3898. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  3899. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  3900. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  3901. */
  3902. /*
  3903. // tex3D:
  3904. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords)
  3905. { return decode_input(tex3D(tex, tex_coords)); }
  3906. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const int texel_off)
  3907. { return decode_input(tex3D(tex, tex_coords, texel_off)); }
  3908. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy)
  3909. { return decode_input(tex3D(tex, tex_coords, dx, dy)); }
  3910. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy, const int texel_off)
  3911. { return decode_input(tex3D(tex, tex_coords, dx, dy, texel_off)); }
  3912. // tex3Dbias:
  3913. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords)
  3914. { return decode_input(tex3Dbias(tex, tex_coords)); }
  3915. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  3916. { return decode_input(tex3Dbias(tex, tex_coords, texel_off)); }
  3917. // tex3Dfetch:
  3918. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords)
  3919. { return decode_input(tex3Dfetch(tex, tex_coords)); }
  3920. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords, const int texel_off)
  3921. { return decode_input(tex3Dfetch(tex, tex_coords, texel_off)); }
  3922. // tex3Dlod:
  3923. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords)
  3924. { return decode_input(tex3Dlod(tex, tex_coords)); }
  3925. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  3926. { return decode_input(tex3Dlod(tex, tex_coords, texel_off)); }
  3927. // tex3Dproj:
  3928. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords)
  3929. { return decode_input(tex3Dproj(tex, tex_coords)); }
  3930. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  3931. { return decode_input(tex3Dproj(tex, tex_coords, texel_off)); }
  3932. /////////*
  3933. // NONSTANDARD "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  3934. // This narrow selection of nonstandard tex2D* functions can be useful:
  3935. // tex2Dlod0: Automatically fill in the tex2D LOD parameter for mip level 0.
  3936. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords)
  3937. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0))); }
  3938. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords, const int texel_off)
  3939. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0), texel_off)); }
  3940. // MANUALLY LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  3941. // Provide a narrower selection of tex2D* wrapper functions that decode an
  3942. // input sample with a specified gamma value. These are useful for reading
  3943. // LUT's and for reading the input of pass0 in a later pass.
  3944. // tex2D:
  3945. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float3 gamma)
  3946. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords), gamma); }
  3947. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float3 gamma)
  3948. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords.xy), gamma); }
  3949. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const int texel_off, const float3 gamma)
  3950. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  3951. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const int texel_off, const float3 gamma)
  3952. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  3953. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  3954. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  3955. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  3956. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  3957. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off, const float3 gamma)
  3958. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  3959. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off, const float3 gamma)
  3960. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  3961. /*
  3962. // tex2Dbias:
  3963. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const float3 gamma)
  3964. { return decode_gamma_input(tex2Dbias(tex, tex_coords), gamma); }
  3965. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const int texel_off, const float3 gamma)
  3966. { return decode_gamma_input(tex2Dbias(tex, tex_coords, texel_off), gamma); }
  3967. // tex2Dfetch:
  3968. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const float3 gamma)
  3969. { return decode_gamma_input(tex2Dfetch(tex, tex_coords), gamma); }
  3970. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const int texel_off, const float3 gamma)
  3971. { return decode_gamma_input(tex2Dfetch(tex, tex_coords, texel_off), gamma); }
  3972. */
  3973. // tex2Dlod:
  3974. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, float3 gamma)
  3975. { return decode_gamma_input(textureLod(tex, tex_coords.xy, 0.0), gamma); }
  3976. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, int texel_off, float3 gamma)
  3977. { return decode_gamma_input(textureLod(tex, tex_coords.xy, texel_off), gamma); }
  3978. #endif // GAMMA_MANAGEMENT_H
  3979. //////////////////////////// END GAMMA-MANAGEMENT //////////////////////////
  3980. //#include "quad-pixel-communication.h"
  3981. /////////////////////// BEGIN QUAD-PIXEL-COMMUNICATION //////////////////////
  3982. #ifndef QUAD_PIXEL_COMMUNICATION_H
  3983. #define QUAD_PIXEL_COMMUNICATION_H
  3984. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3985. // Copyright (C) 2014 TroggleMonkey*
  3986. //
  3987. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3988. // of this software and associated documentation files (the "Software"), to
  3989. // deal in the Software without restriction, including without limitation the
  3990. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  3991. // sell copies of the Software, and to permit persons to whom the Software is
  3992. // furnished to do so, subject to the following conditions:
  3993. //
  3994. // The above copyright notice and this permission notice shall be included in
  3995. // all copies or substantial portions of the Software.
  3996. //
  3997. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3998. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3999. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  4000. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  4001. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  4002. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  4003. // IN THE SOFTWARE.
  4004. ///////////////////////////////// DISCLAIMER /////////////////////////////////
  4005. // *This code was inspired by "Shader Amortization using Pixel Quad Message
  4006. // Passing" by Eric Penner, published in GPU Pro 2, Chapter VI.2. My intent
  4007. // is not to plagiarize his fundamentally similar code and assert my own
  4008. // copyright, but the algorithmic helper functions require so little code that
  4009. // implementations can't vary by much except bugfixes and conventions. I just
  4010. // wanted to license my own particular code here to avoid ambiguity and make it
  4011. // clear that as far as I'm concerned, people can do as they please with it.
  4012. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  4013. // Given screen pixel numbers, derive a "quad vector" describing a fragment's
  4014. // position in its 2x2 pixel quad. Given that vector, obtain the values of any
  4015. // variable at neighboring fragments.
  4016. // Requires: Using this file in general requires:
  4017. // 1.) ddx() and ddy() are present in the current Cg profile.
  4018. // 2.) The GPU driver is using fine/high-quality derivatives.
  4019. // Functions will give incorrect results if this is not true,
  4020. // so a test function is included.
  4021. ///////////////////// QUAD-PIXEL COMMUNICATION PRIMITIVES ////////////////////
  4022. float4 get_quad_vector_naive(float4 output_pixel_num_wrt_uvxy)
  4023. {
  4024. // Requires: Two measures of the current fragment's output pixel number
  4025. // in the range ([0, output_size.x), [0, output_size.y)):
  4026. // 1.) output_pixel_num_wrt_uvxy.xy increase with uv coords.
  4027. // 2.) output_pixel_num_wrt_uvxy.zw increase with screen xy.
  4028. // Returns: Two measures of the fragment's position in its 2x2 quad:
  4029. // 1.) The .xy components are its 2x2 placement with respect to
  4030. // uv direction (the origin (0, 0) is at the top-left):
  4031. // top-left = (-1.0, -1.0) top-right = ( 1.0, -1.0)
  4032. // bottom-left = (-1.0, 1.0) bottom-right = ( 1.0, 1.0)
  4033. // You need this to arrange/weight shared texture samples.
  4034. // 2.) The .zw components are its 2x2 placement with respect to
  4035. // screen xy direction (position); the origin varies.
  4036. // quad_gather needs this measure to work correctly.
  4037. // Note: quad_vector.zw = quad_vector.xy * float2(
  4038. // ddx(output_pixel_num_wrt_uvxy.x),
  4039. // ddy(output_pixel_num_wrt_uvxy.y));
  4040. // Caveats: This function assumes the GPU driver always starts 2x2 pixel
  4041. // quads at even pixel numbers. This assumption can be wrong
  4042. // for odd output resolutions (nondeterministically so).
  4043. float4 pixel_odd = frac(output_pixel_num_wrt_uvxy * 0.5) * 2.0;
  4044. float4 quad_vector = pixel_odd * 2.0 - float4(1.0);
  4045. return quad_vector;
  4046. }
  4047. float4 get_quad_vector(float4 output_pixel_num_wrt_uvxy)
  4048. {
  4049. // Requires: Same as get_quad_vector_naive() (see that first).
  4050. // Returns: Same as get_quad_vector_naive() (see that first), but it's
  4051. // correct even if the 2x2 pixel quad starts at an odd pixel,
  4052. // which can occur at odd resolutions.
  4053. float4 quad_vector_guess =
  4054. get_quad_vector_naive(output_pixel_num_wrt_uvxy);
  4055. // If quad_vector_guess.zw doesn't increase with screen xy, we know
  4056. // the 2x2 pixel quad starts at an odd pixel:
  4057. float2 odd_start_mirror = 0.5 * float2(ddx(quad_vector_guess.z),
  4058. ddy(quad_vector_guess.w));
  4059. return quad_vector_guess * odd_start_mirror.xyxy;
  4060. }
  4061. float4 get_quad_vector(float2 output_pixel_num_wrt_uv)
  4062. {
  4063. // Requires: 1.) ddx() and ddy() are present in the current Cg profile.
  4064. // 2.) output_pixel_num_wrt_uv must increase with uv coords and
  4065. // measure the current fragment's output pixel number in:
  4066. // ([0, output_size.x), [0, output_size.y))
  4067. // Returns: Same as get_quad_vector_naive() (see that first), but it's
  4068. // correct even if the 2x2 pixel quad starts at an odd pixel,
  4069. // which can occur at odd resolutions.
  4070. // Caveats: This function requires less information than the version
  4071. // taking a float4, but it's potentially slower.
  4072. // Do screen coords increase with or against uv? Get the direction
  4073. // with respect to (uv.x, uv.y) for (screen.x, screen.y) in {-1, 1}.
  4074. float2 screen_uv_mirror = float2(ddx(output_pixel_num_wrt_uv.x),
  4075. ddy(output_pixel_num_wrt_uv.y));
  4076. float2 pixel_odd_wrt_uv = frac(output_pixel_num_wrt_uv * 0.5) * 2.0;
  4077. float2 quad_vector_uv_guess = (pixel_odd_wrt_uv - float2(0.5)) * 2.0;
  4078. float2 quad_vector_screen_guess = quad_vector_uv_guess * screen_uv_mirror;
  4079. // If quad_vector_screen_guess doesn't increase with screen xy, we know
  4080. // the 2x2 pixel quad starts at an odd pixel:
  4081. float2 odd_start_mirror = 0.5 * float2(ddx(quad_vector_screen_guess.x),
  4082. ddy(quad_vector_screen_guess.y));
  4083. float4 quad_vector_guess = float4(
  4084. quad_vector_uv_guess, quad_vector_screen_guess);
  4085. return quad_vector_guess * odd_start_mirror.xyxy;
  4086. }
  4087. void quad_gather(float4 quad_vector, float4 curr,
  4088. out float4 adjx, out float4 adjy, out float4 diag)
  4089. {
  4090. // Requires: 1.) ddx() and ddy() are present in the current Cg profile.
  4091. // 2.) The GPU driver is using fine/high-quality derivatives.
  4092. // 3.) quad_vector describes the current fragment's location in
  4093. // its 2x2 pixel quad using get_quad_vector()'s conventions.
  4094. // 4.) curr is any vector you wish to get neighboring values of.
  4095. // Returns: Values of an input vector (curr) at neighboring fragments
  4096. // adjacent x, adjacent y, and diagonal (via out parameters).
  4097. adjx = curr - ddx(curr) * quad_vector.z;
  4098. adjy = curr - ddy(curr) * quad_vector.w;
  4099. diag = adjx - ddy(adjx) * quad_vector.w;
  4100. }
  4101. void quad_gather(float4 quad_vector, float3 curr,
  4102. out float3 adjx, out float3 adjy, out float3 diag)
  4103. {
  4104. // Float3 version
  4105. adjx = curr - ddx(curr) * quad_vector.z;
  4106. adjy = curr - ddy(curr) * quad_vector.w;
  4107. diag = adjx - ddy(adjx) * quad_vector.w;
  4108. }
  4109. void quad_gather(float4 quad_vector, float2 curr,
  4110. out float2 adjx, out float2 adjy, out float2 diag)
  4111. {
  4112. // Float2 version
  4113. adjx = curr - ddx(curr) * quad_vector.z;
  4114. adjy = curr - ddy(curr) * quad_vector.w;
  4115. diag = adjx - ddy(adjx) * quad_vector.w;
  4116. }
  4117. float4 quad_gather(float4 quad_vector, float curr)
  4118. {
  4119. // Float version:
  4120. // Returns: return.x == current
  4121. // return.y == adjacent x
  4122. // return.z == adjacent y
  4123. // return.w == diagonal
  4124. float4 all = float4(curr);
  4125. all.y = all.x - ddx(all.x) * quad_vector.z;
  4126. all.zw = all.xy - ddy(all.xy) * quad_vector.w;
  4127. return all;
  4128. }
  4129. float4 quad_gather_sum(float4 quad_vector, float4 curr)
  4130. {
  4131. // Requires: Same as quad_gather()
  4132. // Returns: Sum of an input vector (curr) at all fragments in a quad.
  4133. float4 adjx, adjy, diag;
  4134. quad_gather(quad_vector, curr, adjx, adjy, diag);
  4135. return (curr + adjx + adjy + diag);
  4136. }
  4137. float3 quad_gather_sum(float4 quad_vector, float3 curr)
  4138. {
  4139. // Float3 version:
  4140. float3 adjx, adjy, diag;
  4141. quad_gather(quad_vector, curr, adjx, adjy, diag);
  4142. return (curr + adjx + adjy + diag);
  4143. }
  4144. float2 quad_gather_sum(float4 quad_vector, float2 curr)
  4145. {
  4146. // Float2 version:
  4147. float2 adjx, adjy, diag;
  4148. quad_gather(quad_vector, curr, adjx, adjy, diag);
  4149. return (curr + adjx + adjy + diag);
  4150. }
  4151. float quad_gather_sum(float4 quad_vector, float curr)
  4152. {
  4153. // Float version:
  4154. float4 all_values = quad_gather(quad_vector, curr);
  4155. return (all_values.x + all_values.y + all_values.z + all_values.w);
  4156. }
  4157. bool fine_derivatives_working(float4 quad_vector, float4 curr)
  4158. {
  4159. // Requires: 1.) ddx() and ddy() are present in the current Cg profile.
  4160. // 2.) quad_vector describes the current fragment's location in
  4161. // its 2x2 pixel quad using get_quad_vector()'s conventions.
  4162. // 3.) curr must be a test vector with non-constant derivatives
  4163. // (its value should change nonlinearly across fragments).
  4164. // Returns: true if fine/hybrid/high-quality derivatives are used, or
  4165. // false if coarse derivatives are used or inconclusive
  4166. // Usage: Test whether quad-pixel communication is working!
  4167. // Method: We can confirm fine derivatives are used if the following
  4168. // holds (ever, for any value at any fragment):
  4169. // (ddy(curr) != ddy(adjx)) or (ddx(curr) != ddx(adjy))
  4170. // The more values we test (e.g. test a float4 two ways), the
  4171. // easier it is to demonstrate fine derivatives are working.
  4172. // TODO: Check for floating point exact comparison issues!
  4173. float4 ddx_curr = ddx(curr);
  4174. float4 ddy_curr = ddy(curr);
  4175. float4 adjx = curr - ddx_curr * quad_vector.z;
  4176. float4 adjy = curr - ddy_curr * quad_vector.w;
  4177. bool ddy_different = any(bool4(ddy_curr.x != ddy(adjx).x, ddy_curr.y != ddy(adjx).y, ddy_curr.z != ddy(adjx).z, ddy_curr.w != ddy(adjx).w));
  4178. bool ddx_different = any(bool4(ddx_curr.x != ddx(adjy).x, ddx_curr.y != ddx(adjy).y, ddx_curr.z != ddx(adjy).z, ddx_curr.w != ddx(adjy).w));
  4179. return any(bool2(ddy_different, ddx_different));
  4180. }
  4181. bool fine_derivatives_working_fast(float4 quad_vector, float curr)
  4182. {
  4183. // Requires: Same as fine_derivatives_working()
  4184. // Returns: Same as fine_derivatives_working()
  4185. // Usage: This is faster than fine_derivatives_working() but more
  4186. // likely to return false negatives, so it's less useful for
  4187. // offline testing/debugging. It's also useless as the basis
  4188. // for dynamic runtime branching as of May 2014: Derivatives
  4189. // (and quad-pixel communication) are currently disallowed in
  4190. // branches. However, future GPU's may allow you to use them
  4191. // in dynamic branches if you promise the branch condition
  4192. // evaluates the same for every fragment in the quad (and/or if
  4193. // the driver enforces that promise by making a single fragment
  4194. // control branch decisions). If that ever happens, this
  4195. // version may become a more economical choice.
  4196. float ddx_curr = ddx(curr);
  4197. float ddy_curr = ddy(curr);
  4198. float adjx = curr - ddx_curr * quad_vector.z;
  4199. return (ddy_curr != ddy(adjx));
  4200. }
  4201. #endif // QUAD_PIXEL_COMMUNICATION_H
  4202. //////////////////////// END QUAD-PIXEL-COMMUNICATION ///////////////////////
  4203. //#include "special-functions.h"
  4204. /////////////////////////// BEGIN SPECIAL-FUNCTIONS //////////////////////////
  4205. #ifndef SPECIAL_FUNCTIONS_H
  4206. #define SPECIAL_FUNCTIONS_H
  4207. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  4208. // Copyright (C) 2014 TroggleMonkey
  4209. //
  4210. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4211. // of this software and associated documentation files (the "Software"), to
  4212. // deal in the Software without restriction, including without limitation the
  4213. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  4214. // sell copies of the Software, and to permit persons to whom the Software is
  4215. // furnished to do so, subject to the following conditions:
  4216. //
  4217. // The above copyright notice and this permission notice shall be included in
  4218. // all copies or substantial portions of the Software.
  4219. //
  4220. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  4221. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  4222. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  4223. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  4224. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  4225. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  4226. // IN THE SOFTWARE.
  4227. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  4228. // This file implements the following mathematical special functions:
  4229. // 1.) erf() = 2/sqrt(pi) * indefinite_integral(e**(-x**2))
  4230. // 2.) gamma(s), a real-numbered extension of the integer factorial function
  4231. // It also implements normalized_ligamma(s, z), a normalized lower incomplete
  4232. // gamma function for s < 0.5 only. Both gamma() and normalized_ligamma() can
  4233. // be called with an _impl suffix to use an implementation version with a few
  4234. // extra precomputed parameters (which may be useful for the caller to reuse).
  4235. // See below for details.
  4236. //
  4237. // Design Rationale:
  4238. // Pretty much every line of code in this file is duplicated four times for
  4239. // different input types (float4/float3/float2/float). This is unfortunate,
  4240. // but Cg doesn't allow function templates. Macros would be far less verbose,
  4241. // but they would make the code harder to document and read. I don't expect
  4242. // these functions will require a whole lot of maintenance changes unless
  4243. // someone ever has need for more robust incomplete gamma functions, so code
  4244. // duplication seems to be the lesser evil in this case.
  4245. /////////////////////////// GAUSSIAN ERROR FUNCTION //////////////////////////
  4246. float4 erf6(float4 x)
  4247. {
  4248. // Requires: x is the standard parameter to erf().
  4249. // Returns: Return an Abramowitz/Stegun approximation of erf(), where:
  4250. // erf(x) = 2/sqrt(pi) * integral(e**(-x**2))
  4251. // This approximation has a max absolute error of 2.5*10**-5
  4252. // with solid numerical robustness and efficiency. See:
  4253. // https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions
  4254. static const float4 one = float4(1.0);
  4255. const float4 sign_x = sign(x);
  4256. const float4 t = one/(one + 0.47047*abs(x));
  4257. const float4 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  4258. exp(-(x*x));
  4259. return result * sign_x;
  4260. }
  4261. float3 erf6(const float3 x)
  4262. {
  4263. // Float3 version:
  4264. static const float3 one = float3(1.0);
  4265. const float3 sign_x = sign(x);
  4266. const float3 t = one/(one + 0.47047*abs(x));
  4267. const float3 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  4268. exp(-(x*x));
  4269. return result * sign_x;
  4270. }
  4271. float2 erf6(const float2 x)
  4272. {
  4273. // Float2 version:
  4274. static const float2 one = float2(1.0);
  4275. const float2 sign_x = sign(x);
  4276. const float2 t = one/(one + 0.47047*abs(x));
  4277. const float2 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  4278. exp(-(x*x));
  4279. return result * sign_x;
  4280. }
  4281. float erf6(const float x)
  4282. {
  4283. // Float version:
  4284. const float sign_x = sign(x);
  4285. const float t = 1.0/(1.0 + 0.47047*abs(x));
  4286. const float result = 1.0 - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  4287. exp(-(x*x));
  4288. return result * sign_x;
  4289. }
  4290. float4 erft(const float4 x)
  4291. {
  4292. // Requires: x is the standard parameter to erf().
  4293. // Returns: Approximate erf() with the hyperbolic tangent. The error is
  4294. // visually noticeable, but it's blazing fast and perceptually
  4295. // close...at least on ATI hardware. See:
  4296. // http://www.maplesoft.com/applications/view.aspx?SID=5525&view=html
  4297. // Warning: Only use this if your hardware drivers correctly implement
  4298. // tanh(): My nVidia 8800GTS returns garbage output.
  4299. return tanh(1.202760580 * x);
  4300. }
  4301. float3 erft(const float3 x)
  4302. {
  4303. // Float3 version:
  4304. return tanh(1.202760580 * x);
  4305. }
  4306. float2 erft(const float2 x)
  4307. {
  4308. // Float2 version:
  4309. return tanh(1.202760580 * x);
  4310. }
  4311. float erft(const float x)
  4312. {
  4313. // Float version:
  4314. return tanh(1.202760580 * x);
  4315. }
  4316. inline float4 erf(const float4 x)
  4317. {
  4318. // Requires: x is the standard parameter to erf().
  4319. // Returns: Some approximation of erf(x), depending on user settings.
  4320. #ifdef ERF_FAST_APPROXIMATION
  4321. return erft(x);
  4322. #else
  4323. return erf6(x);
  4324. #endif
  4325. }
  4326. inline float3 erf(const float3 x)
  4327. {
  4328. // Float3 version:
  4329. #ifdef ERF_FAST_APPROXIMATION
  4330. return erft(x);
  4331. #else
  4332. return erf6(x);
  4333. #endif
  4334. }
  4335. inline float2 erf(const float2 x)
  4336. {
  4337. // Float2 version:
  4338. #ifdef ERF_FAST_APPROXIMATION
  4339. return erft(x);
  4340. #else
  4341. return erf6(x);
  4342. #endif
  4343. }
  4344. inline float erf(const float x)
  4345. {
  4346. // Float version:
  4347. #ifdef ERF_FAST_APPROXIMATION
  4348. return erft(x);
  4349. #else
  4350. return erf6(x);
  4351. #endif
  4352. }
  4353. /////////////////////////// COMPLETE GAMMA FUNCTION //////////////////////////
  4354. float4 gamma_impl(const float4 s, const float4 s_inv)
  4355. {
  4356. // Requires: 1.) s is the standard parameter to the gamma function, and
  4357. // it should lie in the [0, 36] range.
  4358. // 2.) s_inv = 1.0/s. This implementation function requires
  4359. // the caller to precompute this value, giving users the
  4360. // opportunity to reuse it.
  4361. // Returns: Return approximate gamma function (real-numbered factorial)
  4362. // output using the Lanczos approximation with two coefficients
  4363. // calculated using Paul Godfrey's method here:
  4364. // http://my.fit.edu/~gabdo/gamma.txt
  4365. // An optimal g value for s in [0, 36] is ~1.12906830989, with
  4366. // a maximum relative error of 0.000463 for 2**16 equally
  4367. // evals. We could use three coeffs (0.0000346 error) without
  4368. // hurting latency, but this allows more parallelism with
  4369. // outside instructions.
  4370. static const float4 g = float4(1.12906830989);
  4371. static const float4 c0 = float4(0.8109119309638332633713423362694399653724431);
  4372. static const float4 c1 = float4(0.4808354605142681877121661197951496120000040);
  4373. static const float4 e = float4(2.71828182845904523536028747135266249775724709);
  4374. const float4 sph = s + float4(0.5);
  4375. const float4 lanczos_sum = c0 + c1/(s + float4(1.0));
  4376. const float4 base = (sph + g)/e; // or (s + g + float4(0.5))/e
  4377. // gamma(s + 1) = base**sph * lanczos_sum; divide by s for gamma(s).
  4378. // This has less error for small s's than (s -= 1.0) at the beginning.
  4379. return (pow(base, sph) * lanczos_sum) * s_inv;
  4380. }
  4381. float3 gamma_impl(const float3 s, const float3 s_inv)
  4382. {
  4383. // Float3 version:
  4384. static const float3 g = float3(1.12906830989);
  4385. static const float3 c0 = float3(0.8109119309638332633713423362694399653724431);
  4386. static const float3 c1 = float3(0.4808354605142681877121661197951496120000040);
  4387. static const float3 e = float3(2.71828182845904523536028747135266249775724709);
  4388. const float3 sph = s + float3(0.5);
  4389. const float3 lanczos_sum = c0 + c1/(s + float3(1.0));
  4390. const float3 base = (sph + g)/e;
  4391. return (pow(base, sph) * lanczos_sum) * s_inv;
  4392. }
  4393. float2 gamma_impl(const float2 s, const float2 s_inv)
  4394. {
  4395. // Float2 version:
  4396. static const float2 g = float2(1.12906830989);
  4397. static const float2 c0 = float2(0.8109119309638332633713423362694399653724431);
  4398. static const float2 c1 = float2(0.4808354605142681877121661197951496120000040);
  4399. static const float2 e = float2(2.71828182845904523536028747135266249775724709);
  4400. const float2 sph = s + float2(0.5);
  4401. const float2 lanczos_sum = c0 + c1/(s + float2(1.0));
  4402. const float2 base = (sph + g)/e;
  4403. return (pow(base, sph) * lanczos_sum) * s_inv;
  4404. }
  4405. float gamma_impl(const float s, const float s_inv)
  4406. {
  4407. // Float version:
  4408. static const float g = 1.12906830989;
  4409. static const float c0 = 0.8109119309638332633713423362694399653724431;
  4410. static const float c1 = 0.4808354605142681877121661197951496120000040;
  4411. static const float e = 2.71828182845904523536028747135266249775724709;
  4412. const float sph = s + 0.5;
  4413. const float lanczos_sum = c0 + c1/(s + 1.0);
  4414. const float base = (sph + g)/e;
  4415. return (pow(base, sph) * lanczos_sum) * s_inv;
  4416. }
  4417. float4 gamma(const float4 s)
  4418. {
  4419. // Requires: s is the standard parameter to the gamma function, and it
  4420. // should lie in the [0, 36] range.
  4421. // Returns: Return approximate gamma function output with a maximum
  4422. // relative error of 0.000463. See gamma_impl for details.
  4423. return gamma_impl(s, float4(1.0)/s);
  4424. }
  4425. float3 gamma(const float3 s)
  4426. {
  4427. // Float3 version:
  4428. return gamma_impl(s, float3(1.0)/s);
  4429. }
  4430. float2 gamma(const float2 s)
  4431. {
  4432. // Float2 version:
  4433. return gamma_impl(s, float2(1.0)/s);
  4434. }
  4435. float gamma(const float s)
  4436. {
  4437. // Float version:
  4438. return gamma_impl(s, 1.0/s);
  4439. }
  4440. //////////////// INCOMPLETE GAMMA FUNCTIONS (RESTRICTED INPUT) ///////////////
  4441. // Lower incomplete gamma function for small s and z (implementation):
  4442. float4 ligamma_small_z_impl(const float4 s, const float4 z, const float4 s_inv)
  4443. {
  4444. // Requires: 1.) s < ~0.5
  4445. // 2.) z <= ~0.775075
  4446. // 3.) s_inv = 1.0/s (precomputed for outside reuse)
  4447. // Returns: A series representation for the lower incomplete gamma
  4448. // function for small s and small z (4 terms).
  4449. // The actual "rolled up" summation looks like:
  4450. // last_sign = 1.0; last_pow = 1.0; last_factorial = 1.0;
  4451. // sum = last_sign * last_pow / ((s + k) * last_factorial)
  4452. // for(int i = 0; i < 4; ++i)
  4453. // {
  4454. // last_sign *= -1.0; last_pow *= z; last_factorial *= i;
  4455. // sum += last_sign * last_pow / ((s + k) * last_factorial);
  4456. // }
  4457. // Unrolled, constant-unfolded and arranged for madds and parallelism:
  4458. const float4 scale = pow(z, s);
  4459. float4 sum = s_inv; // Summation iteration 0 result
  4460. // Summation iterations 1, 2, and 3:
  4461. const float4 z_sq = z*z;
  4462. const float4 denom1 = s + float4(1.0);
  4463. const float4 denom2 = 2.0*s + float4(4.0);
  4464. const float4 denom3 = 6.0*s + float4(18.0);
  4465. //float4 denom4 = 24.0*s + float4(96.0);
  4466. sum -= z/denom1;
  4467. sum += z_sq/denom2;
  4468. sum -= z * z_sq/denom3;
  4469. //sum += z_sq * z_sq / denom4;
  4470. // Scale and return:
  4471. return scale * sum;
  4472. }
  4473. float3 ligamma_small_z_impl(const float3 s, const float3 z, const float3 s_inv)
  4474. {
  4475. // Float3 version:
  4476. const float3 scale = pow(z, s);
  4477. float3 sum = s_inv;
  4478. const float3 z_sq = z*z;
  4479. const float3 denom1 = s + float3(1.0);
  4480. const float3 denom2 = 2.0*s + float3(4.0);
  4481. const float3 denom3 = 6.0*s + float3(18.0);
  4482. sum -= z/denom1;
  4483. sum += z_sq/denom2;
  4484. sum -= z * z_sq/denom3;
  4485. return scale * sum;
  4486. }
  4487. float2 ligamma_small_z_impl(const float2 s, const float2 z, const float2 s_inv)
  4488. {
  4489. // Float2 version:
  4490. const float2 scale = pow(z, s);
  4491. float2 sum = s_inv;
  4492. const float2 z_sq = z*z;
  4493. const float2 denom1 = s + float2(1.0);
  4494. const float2 denom2 = 2.0*s + float2(4.0);
  4495. const float2 denom3 = 6.0*s + float2(18.0);
  4496. sum -= z/denom1;
  4497. sum += z_sq/denom2;
  4498. sum -= z * z_sq/denom3;
  4499. return scale * sum;
  4500. }
  4501. float ligamma_small_z_impl(const float s, const float z, const float s_inv)
  4502. {
  4503. // Float version:
  4504. const float scale = pow(z, s);
  4505. float sum = s_inv;
  4506. const float z_sq = z*z;
  4507. const float denom1 = s + 1.0;
  4508. const float denom2 = 2.0*s + 4.0;
  4509. const float denom3 = 6.0*s + 18.0;
  4510. sum -= z/denom1;
  4511. sum += z_sq/denom2;
  4512. sum -= z * z_sq/denom3;
  4513. return scale * sum;
  4514. }
  4515. // Upper incomplete gamma function for small s and large z (implementation):
  4516. float4 uigamma_large_z_impl(const float4 s, const float4 z)
  4517. {
  4518. // Requires: 1.) s < ~0.5
  4519. // 2.) z > ~0.775075
  4520. // Returns: Gauss's continued fraction representation for the upper
  4521. // incomplete gamma function (4 terms).
  4522. // The "rolled up" continued fraction looks like this. The denominator
  4523. // is truncated, and it's calculated "from the bottom up:"
  4524. // denom = float4('inf');
  4525. // float4 one = float4(1.0);
  4526. // for(int i = 4; i > 0; --i)
  4527. // {
  4528. // denom = ((i * 2.0) - one) + z - s + (i * (s - i))/denom;
  4529. // }
  4530. // Unrolled and constant-unfolded for madds and parallelism:
  4531. const float4 numerator = pow(z, s) * exp(-z);
  4532. float4 denom = float4(7.0) + z - s;
  4533. denom = float4(5.0) + z - s + (3.0*s - float4(9.0))/denom;
  4534. denom = float4(3.0) + z - s + (2.0*s - float4(4.0))/denom;
  4535. denom = float4(1.0) + z - s + (s - float4(1.0))/denom;
  4536. return numerator / denom;
  4537. }
  4538. float3 uigamma_large_z_impl(const float3 s, const float3 z)
  4539. {
  4540. // Float3 version:
  4541. const float3 numerator = pow(z, s) * exp(-z);
  4542. float3 denom = float3(7.0) + z - s;
  4543. denom = float3(5.0) + z - s + (3.0*s - float3(9.0))/denom;
  4544. denom = float3(3.0) + z - s + (2.0*s - float3(4.0))/denom;
  4545. denom = float3(1.0) + z - s + (s - float3(1.0))/denom;
  4546. return numerator / denom;
  4547. }
  4548. float2 uigamma_large_z_impl(const float2 s, const float2 z)
  4549. {
  4550. // Float2 version:
  4551. const float2 numerator = pow(z, s) * exp(-z);
  4552. float2 denom = float2(7.0) + z - s;
  4553. denom = float2(5.0) + z - s + (3.0*s - float2(9.0))/denom;
  4554. denom = float2(3.0) + z - s + (2.0*s - float2(4.0))/denom;
  4555. denom = float2(1.0) + z - s + (s - float2(1.0))/denom;
  4556. return numerator / denom;
  4557. }
  4558. float uigamma_large_z_impl(const float s, const float z)
  4559. {
  4560. // Float version:
  4561. const float numerator = pow(z, s) * exp(-z);
  4562. float denom = 7.0 + z - s;
  4563. denom = 5.0 + z - s + (3.0*s - 9.0)/denom;
  4564. denom = 3.0 + z - s + (2.0*s - 4.0)/denom;
  4565. denom = 1.0 + z - s + (s - 1.0)/denom;
  4566. return numerator / denom;
  4567. }
  4568. // Normalized lower incomplete gamma function for small s (implementation):
  4569. float4 normalized_ligamma_impl(const float4 s, const float4 z,
  4570. const float4 s_inv, const float4 gamma_s_inv)
  4571. {
  4572. // Requires: 1.) s < ~0.5
  4573. // 2.) s_inv = 1/s (precomputed for outside reuse)
  4574. // 3.) gamma_s_inv = 1/gamma(s) (precomputed for outside reuse)
  4575. // Returns: Approximate the normalized lower incomplete gamma function
  4576. // for s < 0.5. Since we only care about s < 0.5, we only need
  4577. // to evaluate two branches (not four) based on z. Each branch
  4578. // uses four terms, with a max relative error of ~0.00182. The
  4579. // branch threshold and specifics were adapted for fewer terms
  4580. // from Gil/Segura/Temme's paper here:
  4581. // http://oai.cwi.nl/oai/asset/20433/20433B.pdf
  4582. // Evaluate both branches: Real branches test slower even when available.
  4583. static const float4 thresh = float4(0.775075);
  4584. bool4 z_is_large;
  4585. z_is_large.x = z.x > thresh.x;
  4586. z_is_large.y = z.y > thresh.y;
  4587. z_is_large.z = z.z > thresh.z;
  4588. z_is_large.w = z.w > thresh.w;
  4589. const float4 large_z = float4(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  4590. const float4 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  4591. // Combine the results from both branches:
  4592. bool4 inverse_z_is_large = not(z_is_large);
  4593. return large_z * float4(z_is_large) + small_z * float4(inverse_z_is_large);
  4594. }
  4595. float3 normalized_ligamma_impl(const float3 s, const float3 z,
  4596. const float3 s_inv, const float3 gamma_s_inv)
  4597. {
  4598. // Float3 version:
  4599. static const float3 thresh = float3(0.775075);
  4600. bool3 z_is_large;
  4601. z_is_large.x = z.x > thresh.x;
  4602. z_is_large.y = z.y > thresh.y;
  4603. z_is_large.z = z.z > thresh.z;
  4604. const float3 large_z = float3(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  4605. const float3 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  4606. bool3 inverse_z_is_large = not(z_is_large);
  4607. return large_z * float3(z_is_large) + small_z * float3(inverse_z_is_large);
  4608. }
  4609. float2 normalized_ligamma_impl(const float2 s, const float2 z,
  4610. const float2 s_inv, const float2 gamma_s_inv)
  4611. {
  4612. // Float2 version:
  4613. static const float2 thresh = float2(0.775075);
  4614. bool2 z_is_large;
  4615. z_is_large.x = z.x > thresh.x;
  4616. z_is_large.y = z.y > thresh.y;
  4617. const float2 large_z = float2(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  4618. const float2 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  4619. bool2 inverse_z_is_large = not(z_is_large);
  4620. return large_z * float2(z_is_large) + small_z * float2(inverse_z_is_large);
  4621. }
  4622. float normalized_ligamma_impl(const float s, const float z,
  4623. const float s_inv, const float gamma_s_inv)
  4624. {
  4625. // Float version:
  4626. static const float thresh = 0.775075;
  4627. const bool z_is_large = z > thresh;
  4628. const float large_z = 1.0 - uigamma_large_z_impl(s, z) * gamma_s_inv;
  4629. const float small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  4630. return large_z * float(z_is_large) + small_z * float(!z_is_large);
  4631. }
  4632. // Normalized lower incomplete gamma function for small s:
  4633. float4 normalized_ligamma(const float4 s, const float4 z)
  4634. {
  4635. // Requires: s < ~0.5
  4636. // Returns: Approximate the normalized lower incomplete gamma function
  4637. // for s < 0.5. See normalized_ligamma_impl() for details.
  4638. const float4 s_inv = float4(1.0)/s;
  4639. const float4 gamma_s_inv = float4(1.0)/gamma_impl(s, s_inv);
  4640. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  4641. }
  4642. float3 normalized_ligamma(const float3 s, const float3 z)
  4643. {
  4644. // Float3 version:
  4645. const float3 s_inv = float3(1.0)/s;
  4646. const float3 gamma_s_inv = float3(1.0)/gamma_impl(s, s_inv);
  4647. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  4648. }
  4649. float2 normalized_ligamma(const float2 s, const float2 z)
  4650. {
  4651. // Float2 version:
  4652. const float2 s_inv = float2(1.0)/s;
  4653. const float2 gamma_s_inv = float2(1.0)/gamma_impl(s, s_inv);
  4654. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  4655. }
  4656. float normalized_ligamma(const float s, const float z)
  4657. {
  4658. // Float version:
  4659. const float s_inv = 1.0/s;
  4660. const float gamma_s_inv = 1.0/gamma_impl(s, s_inv);
  4661. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  4662. }
  4663. #endif // SPECIAL_FUNCTIONS_H
  4664. //////////////////////////// END SPECIAL-FUNCTIONS ///////////////////////////
  4665. //////////////////////////////// END INCLUDES ////////////////////////////////
  4666. /////////////////////////////////// HELPERS //////////////////////////////////
  4667. inline float4 uv2_to_uv4(float2 tex_uv)
  4668. {
  4669. // Make a float2 uv offset safe for adding to float4 tex2Dlod coords:
  4670. return float4(tex_uv, 0.0, 0.0);
  4671. }
  4672. // Make a length squared helper macro (for usage with static constants):
  4673. #define LENGTH_SQ(vec) (dot(vec, vec))
  4674. inline float get_fast_gaussian_weight_sum_inv(const float sigma)
  4675. {
  4676. // We can use the Gaussian integral to calculate the asymptotic weight for
  4677. // the center pixel. Since the unnormalized center pixel weight is 1.0,
  4678. // the normalized weight is the same as the weight sum inverse. Given a
  4679. // large enough blur (9+), the asymptotic weight sum is close and faster:
  4680. // center_weight = 0.5 *
  4681. // (erf(0.5/(sigma*sqrt(2.0))) - erf(-0.5/(sigma*sqrt(2.0))))
  4682. // erf(-x) == -erf(x), so we get 0.5 * (2.0 * erf(blah blah)):
  4683. // However, we can get even faster results with curve-fitting. These are
  4684. // also closer than the asymptotic results, because they were constructed
  4685. // from 64 blurs sizes from [3, 131) and 255 equally-spaced sigmas from
  4686. // (0, blurN_std_dev), so the results for smaller sigmas are biased toward
  4687. // smaller blurs. The max error is 0.0031793913.
  4688. // Relative FPS: 134.3 with erf, 135.8 with curve-fitting.
  4689. //static const float temp = 0.5/sqrt(2.0);
  4690. //return erf(temp/sigma);
  4691. return min(exp(exp(0.348348412457428/
  4692. (sigma - 0.0860587260734721))), 0.399334576340352/sigma);
  4693. }
  4694. //////////////////// ARBITRARILY RESIZABLE SEPARABLE BLURS ///////////////////
  4695. float3 tex2Dblur11resize(const sampler2D tex, const float2 tex_uv,
  4696. const float2 dxdy, const float sigma)
  4697. {
  4698. // Requires: Global requirements must be met (see file description).
  4699. // Returns: A 1D 11x Gaussian blurred texture lookup using a 11-tap blur.
  4700. // It may be mipmapped depending on settings and dxdy.
  4701. // Calculate Gaussian blur kernel weights and a normalization factor for
  4702. // distances of 0-4, ignoring constant factors (since we're normalizing).
  4703. const float denom_inv = 0.5/(sigma*sigma);
  4704. const float w0 = 1.0;
  4705. const float w1 = exp(-1.0 * denom_inv);
  4706. const float w2 = exp(-4.0 * denom_inv);
  4707. const float w3 = exp(-9.0 * denom_inv);
  4708. const float w4 = exp(-16.0 * denom_inv);
  4709. const float w5 = exp(-25.0 * denom_inv);
  4710. const float weight_sum_inv = 1.0 /
  4711. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5));
  4712. // Statically normalize weights, sum weighted samples, and return. Blurs are
  4713. // currently optimized for dynamic weights.
  4714. float3 sum = float3(0.0,0.0,0.0);
  4715. sum += w5 * tex2D_linearize(tex, tex_uv - 5.0 * dxdy).rgb;
  4716. sum += w4 * tex2D_linearize(tex, tex_uv - 4.0 * dxdy).rgb;
  4717. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  4718. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  4719. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  4720. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  4721. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  4722. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  4723. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  4724. sum += w4 * tex2D_linearize(tex, tex_uv + 4.0 * dxdy).rgb;
  4725. sum += w5 * tex2D_linearize(tex, tex_uv + 5.0 * dxdy).rgb;
  4726. return sum * weight_sum_inv;
  4727. }
  4728. float3 tex2Dblur9resize(const sampler2D tex, const float2 tex_uv,
  4729. const float2 dxdy, const float sigma)
  4730. {
  4731. // Requires: Global requirements must be met (see file description).
  4732. // Returns: A 1D 9x Gaussian blurred texture lookup using a 9-tap blur.
  4733. // It may be mipmapped depending on settings and dxdy.
  4734. // First get the texel weights and normalization factor as above.
  4735. const float denom_inv = 0.5/(sigma*sigma);
  4736. const float w0 = 1.0;
  4737. const float w1 = exp(-1.0 * denom_inv);
  4738. const float w2 = exp(-4.0 * denom_inv);
  4739. const float w3 = exp(-9.0 * denom_inv);
  4740. const float w4 = exp(-16.0 * denom_inv);
  4741. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  4742. // Statically normalize weights, sum weighted samples, and return:
  4743. float3 sum = float3(0.0,0.0,0.0);
  4744. sum += w4 * tex2D_linearize(tex, tex_uv - 4.0 * dxdy).rgb;
  4745. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  4746. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  4747. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  4748. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  4749. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  4750. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  4751. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  4752. sum += w4 * tex2D_linearize(tex, tex_uv + 4.0 * dxdy).rgb;
  4753. return sum * weight_sum_inv;
  4754. }
  4755. float3 tex2Dblur7resize(const sampler2D tex, const float2 tex_uv,
  4756. const float2 dxdy, const float sigma)
  4757. {
  4758. // Requires: Global requirements must be met (see file description).
  4759. // Returns: A 1D 7x Gaussian blurred texture lookup using a 7-tap blur.
  4760. // It may be mipmapped depending on settings and dxdy.
  4761. // First get the texel weights and normalization factor as above.
  4762. const float denom_inv = 0.5/(sigma*sigma);
  4763. const float w0 = 1.0;
  4764. const float w1 = exp(-1.0 * denom_inv);
  4765. const float w2 = exp(-4.0 * denom_inv);
  4766. const float w3 = exp(-9.0 * denom_inv);
  4767. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3));
  4768. // Statically normalize weights, sum weighted samples, and return:
  4769. float3 sum = float3(0.0,0.0,0.0);
  4770. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  4771. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  4772. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  4773. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  4774. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  4775. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  4776. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  4777. return sum * weight_sum_inv;
  4778. }
  4779. float3 tex2Dblur5resize(const sampler2D tex, const float2 tex_uv,
  4780. const float2 dxdy, const float sigma)
  4781. {
  4782. // Requires: Global requirements must be met (see file description).
  4783. // Returns: A 1D 5x Gaussian blurred texture lookup using a 5-tap blur.
  4784. // It may be mipmapped depending on settings and dxdy.
  4785. // First get the texel weights and normalization factor as above.
  4786. const float denom_inv = 0.5/(sigma*sigma);
  4787. const float w0 = 1.0;
  4788. const float w1 = exp(-1.0 * denom_inv);
  4789. const float w2 = exp(-4.0 * denom_inv);
  4790. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2));
  4791. // Statically normalize weights, sum weighted samples, and return:
  4792. float3 sum = float3(0.0,0.0,0.0);
  4793. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  4794. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  4795. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  4796. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  4797. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  4798. return sum * weight_sum_inv;
  4799. }
  4800. float3 tex2Dblur3resize(const sampler2D tex, const float2 tex_uv,
  4801. const float2 dxdy, const float sigma)
  4802. {
  4803. // Requires: Global requirements must be met (see file description).
  4804. // Returns: A 1D 3x Gaussian blurred texture lookup using a 3-tap blur.
  4805. // It may be mipmapped depending on settings and dxdy.
  4806. // First get the texel weights and normalization factor as above.
  4807. const float denom_inv = 0.5/(sigma*sigma);
  4808. const float w0 = 1.0;
  4809. const float w1 = exp(-1.0 * denom_inv);
  4810. const float weight_sum_inv = 1.0 / (w0 + 2.0 * w1);
  4811. // Statically normalize weights, sum weighted samples, and return:
  4812. float3 sum = float3(0.0,0.0,0.0);
  4813. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  4814. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  4815. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  4816. return sum * weight_sum_inv;
  4817. }
  4818. /////////////////////////// FAST SEPARABLE BLURS ///////////////////////////
  4819. float3 tex2Dblur11fast(const sampler2D tex, const float2 tex_uv,
  4820. const float2 dxdy, const float sigma)
  4821. {
  4822. // Requires: 1.) Global requirements must be met (see file description).
  4823. // 2.) filter_linearN must = "true" in your .cgp file.
  4824. // 3.) For gamma-correct bilinear filtering, global
  4825. // gamma_aware_bilinear == true (from gamma-management.h)
  4826. // Returns: A 1D 11x Gaussian blurred texture lookup using 6 linear
  4827. // taps. It may be mipmapped depending on settings and dxdy.
  4828. // First get the texel weights and normalization factor as above.
  4829. const float denom_inv = 0.5/(sigma*sigma);
  4830. const float w0 = 1.0;
  4831. const float w1 = exp(-1.0 * denom_inv);
  4832. const float w2 = exp(-4.0 * denom_inv);
  4833. const float w3 = exp(-9.0 * denom_inv);
  4834. const float w4 = exp(-16.0 * denom_inv);
  4835. const float w5 = exp(-25.0 * denom_inv);
  4836. const float weight_sum_inv = 1.0 /
  4837. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5));
  4838. // Calculate combined weights and linear sample ratios between texel pairs.
  4839. // The center texel (with weight w0) is used twice, so halve its weight.
  4840. const float w01 = w0 * 0.5 + w1;
  4841. const float w23 = w2 + w3;
  4842. const float w45 = w4 + w5;
  4843. const float w01_ratio = w1/w01;
  4844. const float w23_ratio = w3/w23;
  4845. const float w45_ratio = w5/w45;
  4846. // Statically normalize weights, sum weighted samples, and return:
  4847. float3 sum = float3(0.0,0.0,0.0);
  4848. sum += w45 * tex2D_linearize(tex, tex_uv - (4.0 + w45_ratio) * dxdy).rgb;
  4849. sum += w23 * tex2D_linearize(tex, tex_uv - (2.0 + w23_ratio) * dxdy).rgb;
  4850. sum += w01 * tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb;
  4851. sum += w01 * tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb;
  4852. sum += w23 * tex2D_linearize(tex, tex_uv + (2.0 + w23_ratio) * dxdy).rgb;
  4853. sum += w45 * tex2D_linearize(tex, tex_uv + (4.0 + w45_ratio) * dxdy).rgb;
  4854. return sum * weight_sum_inv;
  4855. }
  4856. float3 tex2Dblur9fast(const sampler2D tex, const float2 tex_uv,
  4857. const float2 dxdy, const float sigma)
  4858. {
  4859. // Requires: Same as tex2Dblur11()
  4860. // Returns: A 1D 9x Gaussian blurred texture lookup using 1 nearest
  4861. // neighbor and 4 linear taps. It may be mipmapped depending
  4862. // on settings and dxdy.
  4863. // First get the texel weights and normalization factor as above.
  4864. const float denom_inv = 0.5/(sigma*sigma);
  4865. const float w0 = 1.0;
  4866. const float w1 = exp(-1.0 * denom_inv);
  4867. const float w2 = exp(-4.0 * denom_inv);
  4868. const float w3 = exp(-9.0 * denom_inv);
  4869. const float w4 = exp(-16.0 * denom_inv);
  4870. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  4871. // Calculate combined weights and linear sample ratios between texel pairs.
  4872. const float w12 = w1 + w2;
  4873. const float w34 = w3 + w4;
  4874. const float w12_ratio = w2/w12;
  4875. const float w34_ratio = w4/w34;
  4876. // Statically normalize weights, sum weighted samples, and return:
  4877. float3 sum = float3(0.0,0.0,0.0);
  4878. sum += w34 * tex2D_linearize(tex, tex_uv - (3.0 + w34_ratio) * dxdy).rgb;
  4879. sum += w12 * tex2D_linearize(tex, tex_uv - (1.0 + w12_ratio) * dxdy).rgb;
  4880. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  4881. sum += w12 * tex2D_linearize(tex, tex_uv + (1.0 + w12_ratio) * dxdy).rgb;
  4882. sum += w34 * tex2D_linearize(tex, tex_uv + (3.0 + w34_ratio) * dxdy).rgb;
  4883. return sum * weight_sum_inv;
  4884. }
  4885. float3 tex2Dblur7fast(const sampler2D tex, const float2 tex_uv,
  4886. const float2 dxdy, const float sigma)
  4887. {
  4888. // Requires: Same as tex2Dblur11()
  4889. // Returns: A 1D 7x Gaussian blurred texture lookup using 4 linear
  4890. // taps. It may be mipmapped depending on settings and dxdy.
  4891. // First get the texel weights and normalization factor as above.
  4892. const float denom_inv = 0.5/(sigma*sigma);
  4893. const float w0 = 1.0;
  4894. const float w1 = exp(-1.0 * denom_inv);
  4895. const float w2 = exp(-4.0 * denom_inv);
  4896. const float w3 = exp(-9.0 * denom_inv);
  4897. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3));
  4898. // Calculate combined weights and linear sample ratios between texel pairs.
  4899. // The center texel (with weight w0) is used twice, so halve its weight.
  4900. const float w01 = w0 * 0.5 + w1;
  4901. const float w23 = w2 + w3;
  4902. const float w01_ratio = w1/w01;
  4903. const float w23_ratio = w3/w23;
  4904. // Statically normalize weights, sum weighted samples, and return:
  4905. float3 sum = float3(0.0,0.0,0.0);
  4906. sum += w23 * tex2D_linearize(tex, tex_uv - (2.0 + w23_ratio) * dxdy).rgb;
  4907. sum += w01 * tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb;
  4908. sum += w01 * tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb;
  4909. sum += w23 * tex2D_linearize(tex, tex_uv + (2.0 + w23_ratio) * dxdy).rgb;
  4910. return sum * weight_sum_inv;
  4911. }
  4912. float3 tex2Dblur5fast(const sampler2D tex, const float2 tex_uv,
  4913. const float2 dxdy, const float sigma)
  4914. {
  4915. // Requires: Same as tex2Dblur11()
  4916. // Returns: A 1D 5x Gaussian blurred texture lookup using 1 nearest
  4917. // neighbor and 2 linear taps. It may be mipmapped depending
  4918. // on settings and dxdy.
  4919. // First get the texel weights and normalization factor as above.
  4920. const float denom_inv = 0.5/(sigma*sigma);
  4921. const float w0 = 1.0;
  4922. const float w1 = exp(-1.0 * denom_inv);
  4923. const float w2 = exp(-4.0 * denom_inv);
  4924. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2));
  4925. // Calculate combined weights and linear sample ratios between texel pairs.
  4926. const float w12 = w1 + w2;
  4927. const float w12_ratio = w2/w12;
  4928. // Statically normalize weights, sum weighted samples, and return:
  4929. float3 sum = float3(0.0,0.0,0.0);
  4930. sum += w12 * tex2D_linearize(tex, tex_uv - (1.0 + w12_ratio) * dxdy).rgb;
  4931. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  4932. sum += w12 * tex2D_linearize(tex, tex_uv + (1.0 + w12_ratio) * dxdy).rgb;
  4933. return sum * weight_sum_inv;
  4934. }
  4935. float3 tex2Dblur3fast(const sampler2D tex, const float2 tex_uv,
  4936. const float2 dxdy, const float sigma)
  4937. {
  4938. // Requires: Same as tex2Dblur11()
  4939. // Returns: A 1D 3x Gaussian blurred texture lookup using 2 linear
  4940. // taps. It may be mipmapped depending on settings and dxdy.
  4941. // First get the texel weights and normalization factor as above.
  4942. const float denom_inv = 0.5/(sigma*sigma);
  4943. const float w0 = 1.0;
  4944. const float w1 = exp(-1.0 * denom_inv);
  4945. const float weight_sum_inv = 1.0 / (w0 + 2.0 * w1);
  4946. // Calculate combined weights and linear sample ratios between texel pairs.
  4947. // The center texel (with weight w0) is used twice, so halve its weight.
  4948. const float w01 = w0 * 0.5 + w1;
  4949. const float w01_ratio = w1/w01;
  4950. // Weights for all samples are the same, so just average them:
  4951. return 0.5 * (
  4952. tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb +
  4953. tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb);
  4954. }
  4955. //////////////////////////// HUGE SEPARABLE BLURS ////////////////////////////
  4956. // Huge separable blurs come only in "fast" versions.
  4957. float3 tex2Dblur43fast(const sampler2D tex, const float2 tex_uv,
  4958. const float2 dxdy, const float sigma)
  4959. {
  4960. // Requires: Same as tex2Dblur11()
  4961. // Returns: A 1D 43x Gaussian blurred texture lookup using 22 linear
  4962. // taps. It may be mipmapped depending on settings and dxdy.
  4963. // First get the texel weights and normalization factor as above.
  4964. const float denom_inv = 0.5/(sigma*sigma);
  4965. const float w0 = 1.0;
  4966. const float w1 = exp(-1.0 * denom_inv);
  4967. const float w2 = exp(-4.0 * denom_inv);
  4968. const float w3 = exp(-9.0 * denom_inv);
  4969. const float w4 = exp(-16.0 * denom_inv);
  4970. const float w5 = exp(-25.0 * denom_inv);
  4971. const float w6 = exp(-36.0 * denom_inv);
  4972. const float w7 = exp(-49.0 * denom_inv);
  4973. const float w8 = exp(-64.0 * denom_inv);
  4974. const float w9 = exp(-81.0 * denom_inv);
  4975. const float w10 = exp(-100.0 * denom_inv);
  4976. const float w11 = exp(-121.0 * denom_inv);
  4977. const float w12 = exp(-144.0 * denom_inv);
  4978. const float w13 = exp(-169.0 * denom_inv);
  4979. const float w14 = exp(-196.0 * denom_inv);
  4980. const float w15 = exp(-225.0 * denom_inv);
  4981. const float w16 = exp(-256.0 * denom_inv);
  4982. const float w17 = exp(-289.0 * denom_inv);
  4983. const float w18 = exp(-324.0 * denom_inv);
  4984. const float w19 = exp(-361.0 * denom_inv);
  4985. const float w20 = exp(-400.0 * denom_inv);
  4986. const float w21 = exp(-441.0 * denom_inv);
  4987. //const float weight_sum_inv = 1.0 /
  4988. // (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 +
  4989. // w12 + w13 + w14 + w15 + w16 + w17 + w18 + w19 + w20 + w21));
  4990. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  4991. // Calculate combined weights and linear sample ratios between texel pairs.
  4992. // The center texel (with weight w0) is used twice, so halve its weight.
  4993. const float w0_1 = w0 * 0.5 + w1;
  4994. const float w2_3 = w2 + w3;
  4995. const float w4_5 = w4 + w5;
  4996. const float w6_7 = w6 + w7;
  4997. const float w8_9 = w8 + w9;
  4998. const float w10_11 = w10 + w11;
  4999. const float w12_13 = w12 + w13;
  5000. const float w14_15 = w14 + w15;
  5001. const float w16_17 = w16 + w17;
  5002. const float w18_19 = w18 + w19;
  5003. const float w20_21 = w20 + w21;
  5004. const float w0_1_ratio = w1/w0_1;
  5005. const float w2_3_ratio = w3/w2_3;
  5006. const float w4_5_ratio = w5/w4_5;
  5007. const float w6_7_ratio = w7/w6_7;
  5008. const float w8_9_ratio = w9/w8_9;
  5009. const float w10_11_ratio = w11/w10_11;
  5010. const float w12_13_ratio = w13/w12_13;
  5011. const float w14_15_ratio = w15/w14_15;
  5012. const float w16_17_ratio = w17/w16_17;
  5013. const float w18_19_ratio = w19/w18_19;
  5014. const float w20_21_ratio = w21/w20_21;
  5015. // Statically normalize weights, sum weighted samples, and return:
  5016. float3 sum = float3(0.0,0.0,0.0);
  5017. sum += w20_21 * tex2D_linearize(tex, tex_uv - (20.0 + w20_21_ratio) * dxdy).rgb;
  5018. sum += w18_19 * tex2D_linearize(tex, tex_uv - (18.0 + w18_19_ratio) * dxdy).rgb;
  5019. sum += w16_17 * tex2D_linearize(tex, tex_uv - (16.0 + w16_17_ratio) * dxdy).rgb;
  5020. sum += w14_15 * tex2D_linearize(tex, tex_uv - (14.0 + w14_15_ratio) * dxdy).rgb;
  5021. sum += w12_13 * tex2D_linearize(tex, tex_uv - (12.0 + w12_13_ratio) * dxdy).rgb;
  5022. sum += w10_11 * tex2D_linearize(tex, tex_uv - (10.0 + w10_11_ratio) * dxdy).rgb;
  5023. sum += w8_9 * tex2D_linearize(tex, tex_uv - (8.0 + w8_9_ratio) * dxdy).rgb;
  5024. sum += w6_7 * tex2D_linearize(tex, tex_uv - (6.0 + w6_7_ratio) * dxdy).rgb;
  5025. sum += w4_5 * tex2D_linearize(tex, tex_uv - (4.0 + w4_5_ratio) * dxdy).rgb;
  5026. sum += w2_3 * tex2D_linearize(tex, tex_uv - (2.0 + w2_3_ratio) * dxdy).rgb;
  5027. sum += w0_1 * tex2D_linearize(tex, tex_uv - w0_1_ratio * dxdy).rgb;
  5028. sum += w0_1 * tex2D_linearize(tex, tex_uv + w0_1_ratio * dxdy).rgb;
  5029. sum += w2_3 * tex2D_linearize(tex, tex_uv + (2.0 + w2_3_ratio) * dxdy).rgb;
  5030. sum += w4_5 * tex2D_linearize(tex, tex_uv + (4.0 + w4_5_ratio) * dxdy).rgb;
  5031. sum += w6_7 * tex2D_linearize(tex, tex_uv + (6.0 + w6_7_ratio) * dxdy).rgb;
  5032. sum += w8_9 * tex2D_linearize(tex, tex_uv + (8.0 + w8_9_ratio) * dxdy).rgb;
  5033. sum += w10_11 * tex2D_linearize(tex, tex_uv + (10.0 + w10_11_ratio) * dxdy).rgb;
  5034. sum += w12_13 * tex2D_linearize(tex, tex_uv + (12.0 + w12_13_ratio) * dxdy).rgb;
  5035. sum += w14_15 * tex2D_linearize(tex, tex_uv + (14.0 + w14_15_ratio) * dxdy).rgb;
  5036. sum += w16_17 * tex2D_linearize(tex, tex_uv + (16.0 + w16_17_ratio) * dxdy).rgb;
  5037. sum += w18_19 * tex2D_linearize(tex, tex_uv + (18.0 + w18_19_ratio) * dxdy).rgb;
  5038. sum += w20_21 * tex2D_linearize(tex, tex_uv + (20.0 + w20_21_ratio) * dxdy).rgb;
  5039. return sum * weight_sum_inv;
  5040. }
  5041. float3 tex2Dblur31fast(const sampler2D tex, const float2 tex_uv,
  5042. const float2 dxdy, const float sigma)
  5043. {
  5044. // Requires: Same as tex2Dblur11()
  5045. // Returns: A 1D 31x Gaussian blurred texture lookup using 16 linear
  5046. // taps. It may be mipmapped depending on settings and dxdy.
  5047. // First get the texel weights and normalization factor as above.
  5048. const float denom_inv = 0.5/(sigma*sigma);
  5049. const float w0 = 1.0;
  5050. const float w1 = exp(-1.0 * denom_inv);
  5051. const float w2 = exp(-4.0 * denom_inv);
  5052. const float w3 = exp(-9.0 * denom_inv);
  5053. const float w4 = exp(-16.0 * denom_inv);
  5054. const float w5 = exp(-25.0 * denom_inv);
  5055. const float w6 = exp(-36.0 * denom_inv);
  5056. const float w7 = exp(-49.0 * denom_inv);
  5057. const float w8 = exp(-64.0 * denom_inv);
  5058. const float w9 = exp(-81.0 * denom_inv);
  5059. const float w10 = exp(-100.0 * denom_inv);
  5060. const float w11 = exp(-121.0 * denom_inv);
  5061. const float w12 = exp(-144.0 * denom_inv);
  5062. const float w13 = exp(-169.0 * denom_inv);
  5063. const float w14 = exp(-196.0 * denom_inv);
  5064. const float w15 = exp(-225.0 * denom_inv);
  5065. //const float weight_sum_inv = 1.0 /
  5066. // (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 +
  5067. // w9 + w10 + w11 + w12 + w13 + w14 + w15));
  5068. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  5069. // Calculate combined weights and linear sample ratios between texel pairs.
  5070. // The center texel (with weight w0) is used twice, so halve its weight.
  5071. const float w0_1 = w0 * 0.5 + w1;
  5072. const float w2_3 = w2 + w3;
  5073. const float w4_5 = w4 + w5;
  5074. const float w6_7 = w6 + w7;
  5075. const float w8_9 = w8 + w9;
  5076. const float w10_11 = w10 + w11;
  5077. const float w12_13 = w12 + w13;
  5078. const float w14_15 = w14 + w15;
  5079. const float w0_1_ratio = w1/w0_1;
  5080. const float w2_3_ratio = w3/w2_3;
  5081. const float w4_5_ratio = w5/w4_5;
  5082. const float w6_7_ratio = w7/w6_7;
  5083. const float w8_9_ratio = w9/w8_9;
  5084. const float w10_11_ratio = w11/w10_11;
  5085. const float w12_13_ratio = w13/w12_13;
  5086. const float w14_15_ratio = w15/w14_15;
  5087. // Statically normalize weights, sum weighted samples, and return:
  5088. float3 sum = float3(0.0,0.0,0.0);
  5089. sum += w14_15 * tex2D_linearize(tex, tex_uv - (14.0 + w14_15_ratio) * dxdy).rgb;
  5090. sum += w12_13 * tex2D_linearize(tex, tex_uv - (12.0 + w12_13_ratio) * dxdy).rgb;
  5091. sum += w10_11 * tex2D_linearize(tex, tex_uv - (10.0 + w10_11_ratio) * dxdy).rgb;
  5092. sum += w8_9 * tex2D_linearize(tex, tex_uv - (8.0 + w8_9_ratio) * dxdy).rgb;
  5093. sum += w6_7 * tex2D_linearize(tex, tex_uv - (6.0 + w6_7_ratio) * dxdy).rgb;
  5094. sum += w4_5 * tex2D_linearize(tex, tex_uv - (4.0 + w4_5_ratio) * dxdy).rgb;
  5095. sum += w2_3 * tex2D_linearize(tex, tex_uv - (2.0 + w2_3_ratio) * dxdy).rgb;
  5096. sum += w0_1 * tex2D_linearize(tex, tex_uv - w0_1_ratio * dxdy).rgb;
  5097. sum += w0_1 * tex2D_linearize(tex, tex_uv + w0_1_ratio * dxdy).rgb;
  5098. sum += w2_3 * tex2D_linearize(tex, tex_uv + (2.0 + w2_3_ratio) * dxdy).rgb;
  5099. sum += w4_5 * tex2D_linearize(tex, tex_uv + (4.0 + w4_5_ratio) * dxdy).rgb;
  5100. sum += w6_7 * tex2D_linearize(tex, tex_uv + (6.0 + w6_7_ratio) * dxdy).rgb;
  5101. sum += w8_9 * tex2D_linearize(tex, tex_uv + (8.0 + w8_9_ratio) * dxdy).rgb;
  5102. sum += w10_11 * tex2D_linearize(tex, tex_uv + (10.0 + w10_11_ratio) * dxdy).rgb;
  5103. sum += w12_13 * tex2D_linearize(tex, tex_uv + (12.0 + w12_13_ratio) * dxdy).rgb;
  5104. sum += w14_15 * tex2D_linearize(tex, tex_uv + (14.0 + w14_15_ratio) * dxdy).rgb;
  5105. return sum * weight_sum_inv;
  5106. }
  5107. float3 tex2Dblur25fast(const sampler2D tex, const float2 tex_uv,
  5108. const float2 dxdy, const float sigma)
  5109. {
  5110. // Requires: Same as tex2Dblur11()
  5111. // Returns: A 1D 25x Gaussian blurred texture lookup using 1 nearest
  5112. // neighbor and 12 linear taps. It may be mipmapped depending
  5113. // on settings and dxdy.
  5114. // First get the texel weights and normalization factor as above.
  5115. const float denom_inv = 0.5/(sigma*sigma);
  5116. const float w0 = 1.0;
  5117. const float w1 = exp(-1.0 * denom_inv);
  5118. const float w2 = exp(-4.0 * denom_inv);
  5119. const float w3 = exp(-9.0 * denom_inv);
  5120. const float w4 = exp(-16.0 * denom_inv);
  5121. const float w5 = exp(-25.0 * denom_inv);
  5122. const float w6 = exp(-36.0 * denom_inv);
  5123. const float w7 = exp(-49.0 * denom_inv);
  5124. const float w8 = exp(-64.0 * denom_inv);
  5125. const float w9 = exp(-81.0 * denom_inv);
  5126. const float w10 = exp(-100.0 * denom_inv);
  5127. const float w11 = exp(-121.0 * denom_inv);
  5128. const float w12 = exp(-144.0 * denom_inv);
  5129. //const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  5130. // w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12));
  5131. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  5132. // Calculate combined weights and linear sample ratios between texel pairs.
  5133. const float w1_2 = w1 + w2;
  5134. const float w3_4 = w3 + w4;
  5135. const float w5_6 = w5 + w6;
  5136. const float w7_8 = w7 + w8;
  5137. const float w9_10 = w9 + w10;
  5138. const float w11_12 = w11 + w12;
  5139. const float w1_2_ratio = w2/w1_2;
  5140. const float w3_4_ratio = w4/w3_4;
  5141. const float w5_6_ratio = w6/w5_6;
  5142. const float w7_8_ratio = w8/w7_8;
  5143. const float w9_10_ratio = w10/w9_10;
  5144. const float w11_12_ratio = w12/w11_12;
  5145. // Statically normalize weights, sum weighted samples, and return:
  5146. float3 sum = float3(0.0,0.0,0.0);
  5147. sum += w11_12 * tex2D_linearize(tex, tex_uv - (11.0 + w11_12_ratio) * dxdy).rgb;
  5148. sum += w9_10 * tex2D_linearize(tex, tex_uv - (9.0 + w9_10_ratio) * dxdy).rgb;
  5149. sum += w7_8 * tex2D_linearize(tex, tex_uv - (7.0 + w7_8_ratio) * dxdy).rgb;
  5150. sum += w5_6 * tex2D_linearize(tex, tex_uv - (5.0 + w5_6_ratio) * dxdy).rgb;
  5151. sum += w3_4 * tex2D_linearize(tex, tex_uv - (3.0 + w3_4_ratio) * dxdy).rgb;
  5152. sum += w1_2 * tex2D_linearize(tex, tex_uv - (1.0 + w1_2_ratio) * dxdy).rgb;
  5153. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  5154. sum += w1_2 * tex2D_linearize(tex, tex_uv + (1.0 + w1_2_ratio) * dxdy).rgb;
  5155. sum += w3_4 * tex2D_linearize(tex, tex_uv + (3.0 + w3_4_ratio) * dxdy).rgb;
  5156. sum += w5_6 * tex2D_linearize(tex, tex_uv + (5.0 + w5_6_ratio) * dxdy).rgb;
  5157. sum += w7_8 * tex2D_linearize(tex, tex_uv + (7.0 + w7_8_ratio) * dxdy).rgb;
  5158. sum += w9_10 * tex2D_linearize(tex, tex_uv + (9.0 + w9_10_ratio) * dxdy).rgb;
  5159. sum += w11_12 * tex2D_linearize(tex, tex_uv + (11.0 + w11_12_ratio) * dxdy).rgb;
  5160. return sum * weight_sum_inv;
  5161. }
  5162. float3 tex2Dblur17fast(const sampler2D tex, const float2 tex_uv,
  5163. const float2 dxdy, const float sigma)
  5164. {
  5165. // Requires: Same as tex2Dblur11()
  5166. // Returns: A 1D 17x Gaussian blurred texture lookup using 1 nearest
  5167. // neighbor and 8 linear taps. It may be mipmapped depending
  5168. // on settings and dxdy.
  5169. // First get the texel weights and normalization factor as above.
  5170. const float denom_inv = 0.5/(sigma*sigma);
  5171. const float w0 = 1.0;
  5172. const float w1 = exp(-1.0 * denom_inv);
  5173. const float w2 = exp(-4.0 * denom_inv);
  5174. const float w3 = exp(-9.0 * denom_inv);
  5175. const float w4 = exp(-16.0 * denom_inv);
  5176. const float w5 = exp(-25.0 * denom_inv);
  5177. const float w6 = exp(-36.0 * denom_inv);
  5178. const float w7 = exp(-49.0 * denom_inv);
  5179. const float w8 = exp(-64.0 * denom_inv);
  5180. //const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  5181. // w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8));
  5182. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  5183. // Calculate combined weights and linear sample ratios between texel pairs.
  5184. const float w1_2 = w1 + w2;
  5185. const float w3_4 = w3 + w4;
  5186. const float w5_6 = w5 + w6;
  5187. const float w7_8 = w7 + w8;
  5188. const float w1_2_ratio = w2/w1_2;
  5189. const float w3_4_ratio = w4/w3_4;
  5190. const float w5_6_ratio = w6/w5_6;
  5191. const float w7_8_ratio = w8/w7_8;
  5192. // Statically normalize weights, sum weighted samples, and return:
  5193. float3 sum = float3(0.0,0.0,0.0);
  5194. sum += w7_8 * tex2D_linearize(tex, tex_uv - (7.0 + w7_8_ratio) * dxdy).rgb;
  5195. sum += w5_6 * tex2D_linearize(tex, tex_uv - (5.0 + w5_6_ratio) * dxdy).rgb;
  5196. sum += w3_4 * tex2D_linearize(tex, tex_uv - (3.0 + w3_4_ratio) * dxdy).rgb;
  5197. sum += w1_2 * tex2D_linearize(tex, tex_uv - (1.0 + w1_2_ratio) * dxdy).rgb;
  5198. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  5199. sum += w1_2 * tex2D_linearize(tex, tex_uv + (1.0 + w1_2_ratio) * dxdy).rgb;
  5200. sum += w3_4 * tex2D_linearize(tex, tex_uv + (3.0 + w3_4_ratio) * dxdy).rgb;
  5201. sum += w5_6 * tex2D_linearize(tex, tex_uv + (5.0 + w5_6_ratio) * dxdy).rgb;
  5202. sum += w7_8 * tex2D_linearize(tex, tex_uv + (7.0 + w7_8_ratio) * dxdy).rgb;
  5203. return sum * weight_sum_inv;
  5204. }
  5205. //////////////////// ARBITRARILY RESIZABLE ONE-PASS BLURS ////////////////////
  5206. float3 tex2Dblur3x3resize(const sampler2D tex, const float2 tex_uv,
  5207. const float2 dxdy, const float sigma)
  5208. {
  5209. // Requires: Global requirements must be met (see file description).
  5210. // Returns: A 3x3 Gaussian blurred mipmapped texture lookup of the
  5211. // resized input.
  5212. // Description:
  5213. // This is the only arbitrarily resizable one-pass blur; tex2Dblur5x5resize
  5214. // would perform like tex2Dblur9x9, MUCH slower than tex2Dblur5resize.
  5215. const float denom_inv = 0.5/(sigma*sigma);
  5216. // Load each sample. We need all 3x3 samples. Quad-pixel communication
  5217. // won't help either: This should perform like tex2Dblur5x5, but sharing a
  5218. // 4x4 sample field would perform more like tex2Dblur8x8shared (worse).
  5219. const float2 sample4_uv = tex_uv;
  5220. const float2 dx = float2(dxdy.x, 0.0);
  5221. const float2 dy = float2(0.0, dxdy.y);
  5222. const float2 sample1_uv = sample4_uv - dy;
  5223. const float2 sample7_uv = sample4_uv + dy;
  5224. const float3 sample0 = tex2D_linearize(tex, sample1_uv - dx).rgb;
  5225. const float3 sample1 = tex2D_linearize(tex, sample1_uv).rgb;
  5226. const float3 sample2 = tex2D_linearize(tex, sample1_uv + dx).rgb;
  5227. const float3 sample3 = tex2D_linearize(tex, sample4_uv - dx).rgb;
  5228. const float3 sample4 = tex2D_linearize(tex, sample4_uv).rgb;
  5229. const float3 sample5 = tex2D_linearize(tex, sample4_uv + dx).rgb;
  5230. const float3 sample6 = tex2D_linearize(tex, sample7_uv - dx).rgb;
  5231. const float3 sample7 = tex2D_linearize(tex, sample7_uv).rgb;
  5232. const float3 sample8 = tex2D_linearize(tex, sample7_uv + dx).rgb;
  5233. // Statically compute Gaussian sample weights:
  5234. const float w4 = 1.0;
  5235. const float w1_3_5_7 = exp(-LENGTH_SQ(float2(1.0, 0.0)) * denom_inv);
  5236. const float w0_2_6_8 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  5237. const float weight_sum_inv = 1.0/(w4 + 4.0 * (w1_3_5_7 + w0_2_6_8));
  5238. // Weight and sum the samples:
  5239. const float3 sum = w4 * sample4 +
  5240. w1_3_5_7 * (sample1 + sample3 + sample5 + sample7) +
  5241. w0_2_6_8 * (sample0 + sample2 + sample6 + sample8);
  5242. return sum * weight_sum_inv;
  5243. }
  5244. //////////////////////////// FASTER ONE-PASS BLURS ///////////////////////////
  5245. float3 tex2Dblur9x9(const sampler2D tex, const float2 tex_uv,
  5246. const float2 dxdy, const float sigma)
  5247. {
  5248. // Perform a 1-pass 9x9 blur with 5x5 bilinear samples.
  5249. // Requires: Same as tex2Dblur9()
  5250. // Returns: A 9x9 Gaussian blurred mipmapped texture lookup composed of
  5251. // 5x5 carefully selected bilinear samples.
  5252. // Description:
  5253. // Perform a 1-pass 9x9 blur with 5x5 bilinear samples. Adjust the
  5254. // bilinear sample location to reflect the true Gaussian weights for each
  5255. // underlying texel. The following diagram illustrates the relative
  5256. // locations of bilinear samples. Each sample with the same number has the
  5257. // same weight (notice the symmetry). The letters a, b, c, d distinguish
  5258. // quadrants, and the letters U, D, L, R, C (up, down, left, right, center)
  5259. // distinguish 1D directions along the line containing the pixel center:
  5260. // 6a 5a 2U 5b 6b
  5261. // 4a 3a 1U 3b 4b
  5262. // 2L 1L 0C 1R 2R
  5263. // 4c 3c 1D 3d 4d
  5264. // 6c 5c 2D 5d 6d
  5265. // The following diagram illustrates the underlying equally spaced texels,
  5266. // named after the sample that accesses them and subnamed by their location
  5267. // within their 2x2, 2x1, 1x2, or 1x1 texel block:
  5268. // 6a4 6a3 5a4 5a3 2U2 5b3 5b4 6b3 6b4
  5269. // 6a2 6a1 5a2 5a1 2U1 5b1 5b2 6b1 6b2
  5270. // 4a4 4a3 3a4 3a3 1U2 3b3 3b4 4b3 4b4
  5271. // 4a2 4a1 3a2 3a1 1U1 3b1 3b2 4b1 4b2
  5272. // 2L2 2L1 1L2 1L1 0C1 1R1 1R2 2R1 2R2
  5273. // 4c2 4c1 3c2 3c1 1D1 3d1 3d2 4d1 4d2
  5274. // 4c4 4c3 3c4 3c3 1D2 3d3 3d4 4d3 4d4
  5275. // 6c2 6c1 5c2 5c1 2D1 5d1 5d2 6d1 6d2
  5276. // 6c4 6c3 5c4 5c3 2D2 5d3 5d4 6d3 6d4
  5277. // Note there is only one C texel and only two texels for each U, D, L, or
  5278. // R sample. The center sample is effectively a nearest neighbor sample,
  5279. // and the U/D/L/R samples use 1D linear filtering. All other texels are
  5280. // read with bilinear samples somewhere within their 2x2 texel blocks.
  5281. // COMPUTE TEXTURE COORDS:
  5282. // Statically compute sampling offsets within each 2x2 texel block, based
  5283. // on 1D sampling ratios between texels [1, 2] and [3, 4] texels away from
  5284. // the center, and reuse them independently for both dimensions. Compute
  5285. // these offsets based on the relative 1D Gaussian weights of the texels
  5286. // in question. (w1off means "Gaussian weight for the texel 1.0 texels
  5287. // away from the pixel center," etc.).
  5288. const float denom_inv = 0.5/(sigma*sigma);
  5289. const float w1off = exp(-1.0 * denom_inv);
  5290. const float w2off = exp(-4.0 * denom_inv);
  5291. const float w3off = exp(-9.0 * denom_inv);
  5292. const float w4off = exp(-16.0 * denom_inv);
  5293. const float texel1to2ratio = w2off/(w1off + w2off);
  5294. const float texel3to4ratio = w4off/(w3off + w4off);
  5295. // Statically compute texel offsets from the fragment center to each
  5296. // bilinear sample in the bottom-right quadrant, including x-axis-aligned:
  5297. const float2 sample1R_texel_offset = float2(1.0, 0.0) + float2(texel1to2ratio, 0.0);
  5298. const float2 sample2R_texel_offset = float2(3.0, 0.0) + float2(texel3to4ratio, 0.0);
  5299. const float2 sample3d_texel_offset = float2(1.0, 1.0) + float2(texel1to2ratio, texel1to2ratio);
  5300. const float2 sample4d_texel_offset = float2(3.0, 1.0) + float2(texel3to4ratio, texel1to2ratio);
  5301. const float2 sample5d_texel_offset = float2(1.0, 3.0) + float2(texel1to2ratio, texel3to4ratio);
  5302. const float2 sample6d_texel_offset = float2(3.0, 3.0) + float2(texel3to4ratio, texel3to4ratio);
  5303. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  5304. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  5305. // Read underscores as "and."
  5306. const float w1R1 = w1off;
  5307. const float w1R2 = w2off;
  5308. const float w2R1 = w3off;
  5309. const float w2R2 = w4off;
  5310. const float w3d1 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  5311. const float w3d2_3d3 = exp(-LENGTH_SQ(float2(2.0, 1.0)) * denom_inv);
  5312. const float w3d4 = exp(-LENGTH_SQ(float2(2.0, 2.0)) * denom_inv);
  5313. const float w4d1_5d1 = exp(-LENGTH_SQ(float2(3.0, 1.0)) * denom_inv);
  5314. const float w4d2_5d3 = exp(-LENGTH_SQ(float2(4.0, 1.0)) * denom_inv);
  5315. const float w4d3_5d2 = exp(-LENGTH_SQ(float2(3.0, 2.0)) * denom_inv);
  5316. const float w4d4_5d4 = exp(-LENGTH_SQ(float2(4.0, 2.0)) * denom_inv);
  5317. const float w6d1 = exp(-LENGTH_SQ(float2(3.0, 3.0)) * denom_inv);
  5318. const float w6d2_6d3 = exp(-LENGTH_SQ(float2(4.0, 3.0)) * denom_inv);
  5319. const float w6d4 = exp(-LENGTH_SQ(float2(4.0, 4.0)) * denom_inv);
  5320. // Statically add texel weights in each sample to get sample weights:
  5321. const float w0 = 1.0;
  5322. const float w1 = w1R1 + w1R2;
  5323. const float w2 = w2R1 + w2R2;
  5324. const float w3 = w3d1 + 2.0 * w3d2_3d3 + w3d4;
  5325. const float w4 = w4d1_5d1 + w4d2_5d3 + w4d3_5d2 + w4d4_5d4;
  5326. const float w5 = w4;
  5327. const float w6 = w6d1 + 2.0 * w6d2_6d3 + w6d4;
  5328. // Get the weight sum inverse (normalization factor):
  5329. const float weight_sum_inv =
  5330. 1.0/(w0 + 4.0 * (w1 + w2 + w3 + w4 + w5 + w6));
  5331. // LOAD TEXTURE SAMPLES:
  5332. // Load all 25 samples (1 nearest, 8 linear, 16 bilinear) using symmetry:
  5333. const float2 mirror_x = float2(-1.0, 1.0);
  5334. const float2 mirror_y = float2(1.0, -1.0);
  5335. const float2 mirror_xy = float2(-1.0, -1.0);
  5336. const float2 dxdy_mirror_x = dxdy * mirror_x;
  5337. const float2 dxdy_mirror_y = dxdy * mirror_y;
  5338. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  5339. // Sampling order doesn't seem to affect performance, so just be clear:
  5340. const float3 sample0C = tex2D_linearize(tex, tex_uv).rgb;
  5341. const float3 sample1R = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset).rgb;
  5342. const float3 sample1D = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset.yx).rgb;
  5343. const float3 sample1L = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset).rgb;
  5344. const float3 sample1U = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset.yx).rgb;
  5345. const float3 sample2R = tex2D_linearize(tex, tex_uv + dxdy * sample2R_texel_offset).rgb;
  5346. const float3 sample2D = tex2D_linearize(tex, tex_uv + dxdy * sample2R_texel_offset.yx).rgb;
  5347. const float3 sample2L = tex2D_linearize(tex, tex_uv - dxdy * sample2R_texel_offset).rgb;
  5348. const float3 sample2U = tex2D_linearize(tex, tex_uv - dxdy * sample2R_texel_offset.yx).rgb;
  5349. const float3 sample3d = tex2D_linearize(tex, tex_uv + dxdy * sample3d_texel_offset).rgb;
  5350. const float3 sample3c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample3d_texel_offset).rgb;
  5351. const float3 sample3b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample3d_texel_offset).rgb;
  5352. const float3 sample3a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample3d_texel_offset).rgb;
  5353. const float3 sample4d = tex2D_linearize(tex, tex_uv + dxdy * sample4d_texel_offset).rgb;
  5354. const float3 sample4c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample4d_texel_offset).rgb;
  5355. const float3 sample4b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample4d_texel_offset).rgb;
  5356. const float3 sample4a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample4d_texel_offset).rgb;
  5357. const float3 sample5d = tex2D_linearize(tex, tex_uv + dxdy * sample5d_texel_offset).rgb;
  5358. const float3 sample5c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample5d_texel_offset).rgb;
  5359. const float3 sample5b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample5d_texel_offset).rgb;
  5360. const float3 sample5a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample5d_texel_offset).rgb;
  5361. const float3 sample6d = tex2D_linearize(tex, tex_uv + dxdy * sample6d_texel_offset).rgb;
  5362. const float3 sample6c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample6d_texel_offset).rgb;
  5363. const float3 sample6b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample6d_texel_offset).rgb;
  5364. const float3 sample6a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample6d_texel_offset).rgb;
  5365. // SUM WEIGHTED SAMPLES:
  5366. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  5367. float3 sum = w0 * sample0C;
  5368. sum += w1 * (sample1R + sample1D + sample1L + sample1U);
  5369. sum += w2 * (sample2R + sample2D + sample2L + sample2U);
  5370. sum += w3 * (sample3d + sample3c + sample3b + sample3a);
  5371. sum += w4 * (sample4d + sample4c + sample4b + sample4a);
  5372. sum += w5 * (sample5d + sample5c + sample5b + sample5a);
  5373. sum += w6 * (sample6d + sample6c + sample6b + sample6a);
  5374. return sum * weight_sum_inv;
  5375. }
  5376. float3 tex2Dblur7x7(const sampler2D tex, const float2 tex_uv,
  5377. const float2 dxdy, const float sigma)
  5378. {
  5379. // Perform a 1-pass 7x7 blur with 5x5 bilinear samples.
  5380. // Requires: Same as tex2Dblur9()
  5381. // Returns: A 7x7 Gaussian blurred mipmapped texture lookup composed of
  5382. // 4x4 carefully selected bilinear samples.
  5383. // Description:
  5384. // First see the descriptions for tex2Dblur9x9() and tex2Dblur7(). This
  5385. // blur mixes concepts from both. The sample layout is as follows:
  5386. // 4a 3a 3b 4b
  5387. // 2a 1a 1b 2b
  5388. // 2c 1c 1d 2d
  5389. // 4c 3c 3d 4d
  5390. // The texel layout is as follows. Note that samples 3a/3b, 1a/1b, 1c/1d,
  5391. // and 3c/3d share a vertical column of texels, and samples 2a/2c, 1a/1c,
  5392. // 1b/1d, and 2b/2d share a horizontal row of texels (all sample1's share
  5393. // the center texel):
  5394. // 4a4 4a3 3a4 3ab3 3b4 4b3 4b4
  5395. // 4a2 4a1 3a2 3ab1 3b2 4b1 4b2
  5396. // 2a4 2a3 1a4 1ab3 1b4 2b3 2b4
  5397. // 2ac2 2ac1 1ac2 1* 1bd2 2bd1 2bd2
  5398. // 2c4 2c3 1c4 1cd3 1d4 2d3 2d4
  5399. // 4c2 4c1 3c2 3cd1 3d2 4d1 4d2
  5400. // 4c4 4c3 3c4 3cd3 3d4 4d3 4d4
  5401. // COMPUTE TEXTURE COORDS:
  5402. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  5403. const float denom_inv = 0.5/(sigma*sigma);
  5404. const float w0off = 1.0;
  5405. const float w1off = exp(-1.0 * denom_inv);
  5406. const float w2off = exp(-4.0 * denom_inv);
  5407. const float w3off = exp(-9.0 * denom_inv);
  5408. const float texel0to1ratio = w1off/(w0off * 0.5 + w1off);
  5409. const float texel2to3ratio = w3off/(w2off + w3off);
  5410. // Statically compute texel offsets from the fragment center to each
  5411. // bilinear sample in the bottom-right quadrant, including axis-aligned:
  5412. const float2 sample1d_texel_offset = float2(texel0to1ratio, texel0to1ratio);
  5413. const float2 sample2d_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  5414. const float2 sample3d_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  5415. const float2 sample4d_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  5416. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  5417. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  5418. // Read underscores as "and."
  5419. const float w1abcd = 1.0;
  5420. const float w1bd2_1cd3 = exp(-LENGTH_SQ(float2(1.0, 0.0)) * denom_inv);
  5421. const float w2bd1_3cd1 = exp(-LENGTH_SQ(float2(2.0, 0.0)) * denom_inv);
  5422. const float w2bd2_3cd2 = exp(-LENGTH_SQ(float2(3.0, 0.0)) * denom_inv);
  5423. const float w1d4 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  5424. const float w2d3_3d2 = exp(-LENGTH_SQ(float2(2.0, 1.0)) * denom_inv);
  5425. const float w2d4_3d4 = exp(-LENGTH_SQ(float2(3.0, 1.0)) * denom_inv);
  5426. const float w4d1 = exp(-LENGTH_SQ(float2(2.0, 2.0)) * denom_inv);
  5427. const float w4d2_4d3 = exp(-LENGTH_SQ(float2(3.0, 2.0)) * denom_inv);
  5428. const float w4d4 = exp(-LENGTH_SQ(float2(3.0, 3.0)) * denom_inv);
  5429. // Statically add texel weights in each sample to get sample weights.
  5430. // Split weights for shared texels between samples sharing them:
  5431. const float w1 = w1abcd * 0.25 + w1bd2_1cd3 + w1d4;
  5432. const float w2_3 = (w2bd1_3cd1 + w2bd2_3cd2) * 0.5 + w2d3_3d2 + w2d4_3d4;
  5433. const float w4 = w4d1 + 2.0 * w4d2_4d3 + w4d4;
  5434. // Get the weight sum inverse (normalization factor):
  5435. const float weight_sum_inv =
  5436. 1.0/(4.0 * (w1 + 2.0 * w2_3 + w4));
  5437. // LOAD TEXTURE SAMPLES:
  5438. // Load all 16 samples using symmetry:
  5439. const float2 mirror_x = float2(-1.0, 1.0);
  5440. const float2 mirror_y = float2(1.0, -1.0);
  5441. const float2 mirror_xy = float2(-1.0, -1.0);
  5442. const float2 dxdy_mirror_x = dxdy * mirror_x;
  5443. const float2 dxdy_mirror_y = dxdy * mirror_y;
  5444. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  5445. const float3 sample1a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample1d_texel_offset).rgb;
  5446. const float3 sample2a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample2d_texel_offset).rgb;
  5447. const float3 sample3a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample3d_texel_offset).rgb;
  5448. const float3 sample4a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample4d_texel_offset).rgb;
  5449. const float3 sample1b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample1d_texel_offset).rgb;
  5450. const float3 sample2b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample2d_texel_offset).rgb;
  5451. const float3 sample3b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample3d_texel_offset).rgb;
  5452. const float3 sample4b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample4d_texel_offset).rgb;
  5453. const float3 sample1c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample1d_texel_offset).rgb;
  5454. const float3 sample2c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample2d_texel_offset).rgb;
  5455. const float3 sample3c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample3d_texel_offset).rgb;
  5456. const float3 sample4c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample4d_texel_offset).rgb;
  5457. const float3 sample1d = tex2D_linearize(tex, tex_uv + dxdy * sample1d_texel_offset).rgb;
  5458. const float3 sample2d = tex2D_linearize(tex, tex_uv + dxdy * sample2d_texel_offset).rgb;
  5459. const float3 sample3d = tex2D_linearize(tex, tex_uv + dxdy * sample3d_texel_offset).rgb;
  5460. const float3 sample4d = tex2D_linearize(tex, tex_uv + dxdy * sample4d_texel_offset).rgb;
  5461. // SUM WEIGHTED SAMPLES:
  5462. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  5463. float3 sum = float3(0.0,0.0,0.0);
  5464. sum += w1 * (sample1a + sample1b + sample1c + sample1d);
  5465. sum += w2_3 * (sample2a + sample2b + sample2c + sample2d);
  5466. sum += w2_3 * (sample3a + sample3b + sample3c + sample3d);
  5467. sum += w4 * (sample4a + sample4b + sample4c + sample4d);
  5468. return sum * weight_sum_inv;
  5469. }
  5470. float3 tex2Dblur5x5(const sampler2D tex, const float2 tex_uv,
  5471. const float2 dxdy, const float sigma)
  5472. {
  5473. // Perform a 1-pass 5x5 blur with 3x3 bilinear samples.
  5474. // Requires: Same as tex2Dblur9()
  5475. // Returns: A 5x5 Gaussian blurred mipmapped texture lookup composed of
  5476. // 3x3 carefully selected bilinear samples.
  5477. // Description:
  5478. // First see the description for tex2Dblur9x9(). This blur uses the same
  5479. // concept and sample/texel locations except on a smaller scale. Samples:
  5480. // 2a 1U 2b
  5481. // 1L 0C 1R
  5482. // 2c 1D 2d
  5483. // Texels:
  5484. // 2a4 2a3 1U2 2b3 2b4
  5485. // 2a2 2a1 1U1 2b1 2b2
  5486. // 1L2 1L1 0C1 1R1 1R2
  5487. // 2c2 2c1 1D1 2d1 2d2
  5488. // 2c4 2c3 1D2 2d3 2d4
  5489. // COMPUTE TEXTURE COORDS:
  5490. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  5491. const float denom_inv = 0.5/(sigma*sigma);
  5492. const float w1off = exp(-1.0 * denom_inv);
  5493. const float w2off = exp(-4.0 * denom_inv);
  5494. const float texel1to2ratio = w2off/(w1off + w2off);
  5495. // Statically compute texel offsets from the fragment center to each
  5496. // bilinear sample in the bottom-right quadrant, including x-axis-aligned:
  5497. const float2 sample1R_texel_offset = float2(1.0, 0.0) + float2(texel1to2ratio, 0.0);
  5498. const float2 sample2d_texel_offset = float2(1.0, 1.0) + float2(texel1to2ratio, texel1to2ratio);
  5499. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  5500. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  5501. // Read underscores as "and."
  5502. const float w1R1 = w1off;
  5503. const float w1R2 = w2off;
  5504. const float w2d1 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  5505. const float w2d2_3 = exp(-LENGTH_SQ(float2(2.0, 1.0)) * denom_inv);
  5506. const float w2d4 = exp(-LENGTH_SQ(float2(2.0, 2.0)) * denom_inv);
  5507. // Statically add texel weights in each sample to get sample weights:
  5508. const float w0 = 1.0;
  5509. const float w1 = w1R1 + w1R2;
  5510. const float w2 = w2d1 + 2.0 * w2d2_3 + w2d4;
  5511. // Get the weight sum inverse (normalization factor):
  5512. const float weight_sum_inv = 1.0/(w0 + 4.0 * (w1 + w2));
  5513. // LOAD TEXTURE SAMPLES:
  5514. // Load all 9 samples (1 nearest, 4 linear, 4 bilinear) using symmetry:
  5515. const float2 mirror_x = float2(-1.0, 1.0);
  5516. const float2 mirror_y = float2(1.0, -1.0);
  5517. const float2 mirror_xy = float2(-1.0, -1.0);
  5518. const float2 dxdy_mirror_x = dxdy * mirror_x;
  5519. const float2 dxdy_mirror_y = dxdy * mirror_y;
  5520. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  5521. const float3 sample0C = tex2D_linearize(tex, tex_uv).rgb;
  5522. const float3 sample1R = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset).rgb;
  5523. const float3 sample1D = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset.yx).rgb;
  5524. const float3 sample1L = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset).rgb;
  5525. const float3 sample1U = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset.yx).rgb;
  5526. const float3 sample2d = tex2D_linearize(tex, tex_uv + dxdy * sample2d_texel_offset).rgb;
  5527. const float3 sample2c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample2d_texel_offset).rgb;
  5528. const float3 sample2b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample2d_texel_offset).rgb;
  5529. const float3 sample2a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample2d_texel_offset).rgb;
  5530. // SUM WEIGHTED SAMPLES:
  5531. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  5532. float3 sum = w0 * sample0C;
  5533. sum += w1 * (sample1R + sample1D + sample1L + sample1U);
  5534. sum += w2 * (sample2a + sample2b + sample2c + sample2d);
  5535. return sum * weight_sum_inv;
  5536. }
  5537. float3 tex2Dblur3x3(const sampler2D tex, const float2 tex_uv,
  5538. const float2 dxdy, const float sigma)
  5539. {
  5540. // Perform a 1-pass 3x3 blur with 5x5 bilinear samples.
  5541. // Requires: Same as tex2Dblur9()
  5542. // Returns: A 3x3 Gaussian blurred mipmapped texture lookup composed of
  5543. // 2x2 carefully selected bilinear samples.
  5544. // Description:
  5545. // First see the descriptions for tex2Dblur9x9() and tex2Dblur7(). This
  5546. // blur mixes concepts from both. The sample layout is as follows:
  5547. // 0a 0b
  5548. // 0c 0d
  5549. // The texel layout is as follows. Note that samples 0a/0b and 0c/0d share
  5550. // a vertical column of texels, and samples 0a/0c and 0b/0d share a
  5551. // horizontal row of texels (all samples share the center texel):
  5552. // 0a3 0ab2 0b3
  5553. // 0ac1 0*0 0bd1
  5554. // 0c3 0cd2 0d3
  5555. // COMPUTE TEXTURE COORDS:
  5556. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  5557. const float denom_inv = 0.5/(sigma*sigma);
  5558. const float w0off = 1.0;
  5559. const float w1off = exp(-1.0 * denom_inv);
  5560. const float texel0to1ratio = w1off/(w0off * 0.5 + w1off);
  5561. // Statically compute texel offsets from the fragment center to each
  5562. // bilinear sample in the bottom-right quadrant, including axis-aligned:
  5563. const float2 sample0d_texel_offset = float2(texel0to1ratio, texel0to1ratio);
  5564. // LOAD TEXTURE SAMPLES:
  5565. // Load all 4 samples using symmetry:
  5566. const float2 mirror_x = float2(-1.0, 1.0);
  5567. const float2 mirror_y = float2(1.0, -1.0);
  5568. const float2 mirror_xy = float2(-1.0, -1.0);
  5569. const float2 dxdy_mirror_x = dxdy * mirror_x;
  5570. const float2 dxdy_mirror_y = dxdy * mirror_y;
  5571. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  5572. const float3 sample0a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample0d_texel_offset).rgb;
  5573. const float3 sample0b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample0d_texel_offset).rgb;
  5574. const float3 sample0c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample0d_texel_offset).rgb;
  5575. const float3 sample0d = tex2D_linearize(tex, tex_uv + dxdy * sample0d_texel_offset).rgb;
  5576. // SUM WEIGHTED SAMPLES:
  5577. // Weights for all samples are the same, so just average them:
  5578. return 0.25 * (sample0a + sample0b + sample0c + sample0d);
  5579. }
  5580. ////////////////// LINEAR ONE-PASS BLURS WITH SHARED SAMPLES /////////////////
  5581. float3 tex2Dblur12x12shared(const sampler2D tex,
  5582. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  5583. const float sigma)
  5584. {
  5585. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  5586. // Requires: 1.) Same as tex2Dblur9()
  5587. // 2.) ddx() and ddy() are present in the current Cg profile.
  5588. // 3.) The GPU driver is using fine/high-quality derivatives.
  5589. // 4.) quad_vector *correctly* describes the current fragment's
  5590. // location in its pixel quad, by the conventions noted in
  5591. // get_quad_vector[_naive].
  5592. // 5.) tex_uv.w = log2(video_size/output_size).y
  5593. // 6.) tex2Dlod() is present in the current Cg profile.
  5594. // Optional: Tune artifacts vs. excessive blurriness with the global
  5595. // float error_blurring.
  5596. // Returns: A blurred texture lookup using a "virtual" 12x12 Gaussian
  5597. // blur (a 6x6 blur of carefully selected bilinear samples)
  5598. // of the given mip level. There will be subtle inaccuracies,
  5599. // especially for small or high-frequency detailed sources.
  5600. // Description:
  5601. // Perform a 1-pass blur with shared texture lookups across a pixel quad.
  5602. // We'll get neighboring samples with high-quality ddx/ddy derivatives, as
  5603. // in GPU Pro 2, Chapter VI.2, "Shader Amortization using Pixel Quad
  5604. // Message Passing" by Eric Penner.
  5605. //
  5606. // Our "virtual" 12x12 blur will be comprised of ((6 - 1)^2)/4 + 3 = 12
  5607. // bilinear samples, where bilinear sampling positions are computed from
  5608. // the relative Gaussian weights of the 4 surrounding texels. The catch is
  5609. // that the appropriate texel weights and sample coords differ for each
  5610. // fragment, but we're reusing most of the same samples across a quad of
  5611. // destination fragments. (We do use unique coords for the four nearest
  5612. // samples at each fragment.) Mixing bilinear filtering and sample-sharing
  5613. // therefore introduces some error into the weights, and this can get nasty
  5614. // when the source image is small or high-frequency. Computing bilinear
  5615. // ratios based on weights at the sample field center results in sharpening
  5616. // and ringing artifacts, but we can move samples closer to halfway between
  5617. // texels to try blurring away the error (which can move features around by
  5618. // a texel or so). Tune this with the global float "error_blurring".
  5619. //
  5620. // The pixel quad's sample field covers 12x12 texels, accessed through 6x6
  5621. // bilinear (2x2 texel) taps. Each fragment depends on a window of 10x10
  5622. // texels (5x5 bilinear taps), and each fragment is responsible for loading
  5623. // a 6x6 texel quadrant as a 3x3 block of bilinear taps, plus 3 more taps
  5624. // to use unique bilinear coords for sample0* for each fragment. This
  5625. // diagram illustrates the relative locations of bilinear samples 1-9 for
  5626. // each quadrant a, b, c, d (note samples will not be equally spaced):
  5627. // 8a 7a 6a 6b 7b 8b
  5628. // 5a 4a 3a 3b 4b 5b
  5629. // 2a 1a 0a 0b 1b 2b
  5630. // 2c 1c 0c 0d 1d 2d
  5631. // 5c 4c 3c 3d 4d 5d
  5632. // 8c 7c 6c 6d 7d 8d
  5633. // The following diagram illustrates the underlying equally spaced texels,
  5634. // named after the sample that accesses them and subnamed by their location
  5635. // within their 2x2 texel block:
  5636. // 8a3 8a2 7a3 7a2 6a3 6a2 6b2 6b3 7b2 7b3 8b2 8b3
  5637. // 8a1 8a0 7a1 7a0 6a1 6a0 6b0 6b1 7b0 7b1 8b0 8b1
  5638. // 5a3 5a2 4a3 4a2 3a3 3a2 3b2 3b3 4b2 4b3 5b2 5b3
  5639. // 5a1 5a0 4a1 4a0 3a1 3a0 3b0 3b1 4b0 4b1 5b0 5b1
  5640. // 2a3 2a2 1a3 1a2 0a3 0a2 0b2 0b3 1b2 1b3 2b2 2b3
  5641. // 2a1 2a0 1a1 1a0 0a1 0a0 0b0 0b1 1b0 1b1 2b0 2b1
  5642. // 2c1 2c0 1c1 1c0 0c1 0c0 0d0 0d1 1d0 1d1 2d0 2d1
  5643. // 2c3 2c2 1c3 1c2 0c3 0c2 0d2 0d3 1d2 1d3 2d2 2d3
  5644. // 5c1 5c0 4c1 4c0 3c1 3c0 3d0 3d1 4d0 4d1 5d0 5d1
  5645. // 5c3 5c2 4c3 4c2 3c3 3c2 3d2 3d3 4d2 4d3 5d2 5d3
  5646. // 8c1 8c0 7c1 7c0 6c1 6c0 6d0 6d1 7d0 7d1 8d0 8d1
  5647. // 8c3 8c2 7c3 7c2 6c3 6c2 6d2 6d3 7d2 7d3 8d2 8d3
  5648. // With this symmetric arrangement, we don't have to know which absolute
  5649. // quadrant a sample lies in to assign kernel weights; it's enough to know
  5650. // the sample number and the relative quadrant of the sample (relative to
  5651. // the current quadrant):
  5652. // {current, adjacent x, adjacent y, diagonal}
  5653. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  5654. // Statically compute sampling offsets within each 2x2 texel block, based
  5655. // on appropriate 1D Gaussian sampling ratio between texels [0, 1], [2, 3],
  5656. // and [4, 5] away from the fragment, and reuse them independently for both
  5657. // dimensions. Use the sample field center as the estimated destination,
  5658. // but nudge the result closer to halfway between texels to blur error.
  5659. const float denom_inv = 0.5/(sigma*sigma);
  5660. const float w0off = 1.0;
  5661. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  5662. const float w1off = exp(-(1.0*1.0) * denom_inv);
  5663. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  5664. const float w2off = exp(-(2.0*2.0) * denom_inv);
  5665. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  5666. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  5667. const float w4_5off = exp(-(4.5*4.5) * denom_inv);
  5668. const float w5_5off = exp(-(5.5*5.5) * denom_inv);
  5669. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  5670. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  5671. const float texel4to5ratio = lerp(w5_5off/(w4_5off + w5_5off), 0.5, error_blurring);
  5672. // We don't share sample0*, so use the nearest destination fragment:
  5673. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  5674. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  5675. // Statically compute texel offsets from the bottom-right fragment to each
  5676. // bilinear sample in the bottom-right quadrant:
  5677. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  5678. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  5679. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  5680. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  5681. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  5682. const float2 sample2_texel_offset = float2(4.0, 0.0) + float2(texel4to5ratio, texel0to1ratio);
  5683. const float2 sample3_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  5684. const float2 sample4_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  5685. const float2 sample5_texel_offset = float2(4.0, 2.0) + float2(texel4to5ratio, texel2to3ratio);
  5686. const float2 sample6_texel_offset = float2(0.0, 4.0) + float2(texel0to1ratio, texel4to5ratio);
  5687. const float2 sample7_texel_offset = float2(2.0, 4.0) + float2(texel2to3ratio, texel4to5ratio);
  5688. const float2 sample8_texel_offset = float2(4.0, 4.0) + float2(texel4to5ratio, texel4to5ratio);
  5689. // CALCULATE KERNEL WEIGHTS:
  5690. // Statically compute bilinear sample weights at each destination fragment
  5691. // based on the sum of their 4 underlying texel weights. Assume a same-
  5692. // resolution blur, so each symmetrically named sample weight will compute
  5693. // the same at every fragment in the pixel quad: We can therefore compute
  5694. // texel weights based only on the bottom-right quadrant (fragment at 0d0).
  5695. // Too avoid too much boilerplate code, use a macro to get all 4 texel
  5696. // weights for a bilinear sample based on the offset of its top-left texel:
  5697. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  5698. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  5699. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  5700. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  5701. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  5702. const float w8diag = GET_TEXEL_QUAD_WEIGHTS(-6.0, -6.0);
  5703. const float w7diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -6.0);
  5704. const float w6diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -6.0);
  5705. const float w6adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -6.0);
  5706. const float w7adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -6.0);
  5707. const float w8adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -6.0);
  5708. const float w5diag = GET_TEXEL_QUAD_WEIGHTS(-6.0, -4.0);
  5709. const float w4diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -4.0);
  5710. const float w3diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -4.0);
  5711. const float w3adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -4.0);
  5712. const float w4adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -4.0);
  5713. const float w5adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -4.0);
  5714. const float w2diag = GET_TEXEL_QUAD_WEIGHTS(-6.0, -2.0);
  5715. const float w1diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -2.0);
  5716. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  5717. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  5718. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  5719. const float w2adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -2.0);
  5720. const float w2adjx = GET_TEXEL_QUAD_WEIGHTS(-6.0, 0.0);
  5721. const float w1adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 0.0);
  5722. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  5723. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  5724. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  5725. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 0.0);
  5726. const float w5adjx = GET_TEXEL_QUAD_WEIGHTS(-6.0, 2.0);
  5727. const float w4adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 2.0);
  5728. const float w3adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  5729. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  5730. const float w4curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  5731. const float w5curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 2.0);
  5732. const float w8adjx = GET_TEXEL_QUAD_WEIGHTS(-6.0, 4.0);
  5733. const float w7adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 4.0);
  5734. const float w6adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 4.0);
  5735. const float w6curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 4.0);
  5736. const float w7curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 4.0);
  5737. const float w8curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 4.0);
  5738. #undef GET_TEXEL_QUAD_WEIGHTS
  5739. // Statically pack weights for runtime:
  5740. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  5741. const float4 w1 = float4(w1curr, w1adjx, w1adjy, w1diag);
  5742. const float4 w2 = float4(w2curr, w2adjx, w2adjy, w2diag);
  5743. const float4 w3 = float4(w3curr, w3adjx, w3adjy, w3diag);
  5744. const float4 w4 = float4(w4curr, w4adjx, w4adjy, w4diag);
  5745. const float4 w5 = float4(w5curr, w5adjx, w5adjy, w5diag);
  5746. const float4 w6 = float4(w6curr, w6adjx, w6adjy, w6diag);
  5747. const float4 w7 = float4(w7curr, w7adjx, w7adjy, w7diag);
  5748. const float4 w8 = float4(w8curr, w8adjx, w8adjy, w8diag);
  5749. // Get the weight sum inverse (normalization factor):
  5750. const float4 weight_sum4 = w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8;
  5751. const float2 weight_sum2 = weight_sum4.xy + weight_sum4.zw;
  5752. const float weight_sum = weight_sum2.x + weight_sum2.y;
  5753. const float weight_sum_inv = 1.0/(weight_sum);
  5754. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  5755. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  5756. const float2 dxdy_curr = dxdy * quad_vector.xy;
  5757. // Load bilinear samples for the current quadrant (for this fragment):
  5758. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  5759. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  5760. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  5761. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  5762. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  5763. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  5764. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  5765. const float3 sample4curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample4_texel_offset)).rgb;
  5766. const float3 sample5curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample5_texel_offset)).rgb;
  5767. const float3 sample6curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample6_texel_offset)).rgb;
  5768. const float3 sample7curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample7_texel_offset)).rgb;
  5769. const float3 sample8curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample8_texel_offset)).rgb;
  5770. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  5771. // Fetch the samples from other fragments in the 2x2 quad:
  5772. float3 sample1adjx, sample1adjy, sample1diag;
  5773. float3 sample2adjx, sample2adjy, sample2diag;
  5774. float3 sample3adjx, sample3adjy, sample3diag;
  5775. float3 sample4adjx, sample4adjy, sample4diag;
  5776. float3 sample5adjx, sample5adjy, sample5diag;
  5777. float3 sample6adjx, sample6adjy, sample6diag;
  5778. float3 sample7adjx, sample7adjy, sample7diag;
  5779. float3 sample8adjx, sample8adjy, sample8diag;
  5780. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  5781. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  5782. quad_gather(quad_vector, sample3curr, sample3adjx, sample3adjy, sample3diag);
  5783. quad_gather(quad_vector, sample4curr, sample4adjx, sample4adjy, sample4diag);
  5784. quad_gather(quad_vector, sample5curr, sample5adjx, sample5adjy, sample5diag);
  5785. quad_gather(quad_vector, sample6curr, sample6adjx, sample6adjy, sample6diag);
  5786. quad_gather(quad_vector, sample7curr, sample7adjx, sample7adjy, sample7diag);
  5787. quad_gather(quad_vector, sample8curr, sample8adjx, sample8adjy, sample8diag);
  5788. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  5789. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  5790. // weights to obtain a weighted result:
  5791. float3 sum = float3(0.0,0.0,0.0);
  5792. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  5793. sum += mul(w1, float4x3(sample1curr, sample1adjx, sample1adjy, sample1diag));
  5794. sum += mul(w2, float4x3(sample2curr, sample2adjx, sample2adjy, sample2diag));
  5795. sum += mul(w3, float4x3(sample3curr, sample3adjx, sample3adjy, sample3diag));
  5796. sum += mul(w4, float4x3(sample4curr, sample4adjx, sample4adjy, sample4diag));
  5797. sum += mul(w5, float4x3(sample5curr, sample5adjx, sample5adjy, sample5diag));
  5798. sum += mul(w6, float4x3(sample6curr, sample6adjx, sample6adjy, sample6diag));
  5799. sum += mul(w7, float4x3(sample7curr, sample7adjx, sample7adjy, sample7diag));
  5800. sum += mul(w8, float4x3(sample8curr, sample8adjx, sample8adjy, sample8diag));
  5801. return sum * weight_sum_inv;
  5802. }
  5803. float3 tex2Dblur10x10shared(const sampler2D tex,
  5804. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  5805. const float sigma)
  5806. {
  5807. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  5808. // Requires: Same as tex2Dblur12x12shared()
  5809. // Returns: A blurred texture lookup using a "virtual" 10x10 Gaussian
  5810. // blur (a 5x5 blur of carefully selected bilinear samples)
  5811. // of the given mip level. There will be subtle inaccuracies,
  5812. // especially for small or high-frequency detailed sources.
  5813. // Description:
  5814. // First see the description for tex2Dblur12x12shared(). This
  5815. // function shares the same concept and sample placement, but each fragment
  5816. // only uses 25 of the 36 samples taken across the pixel quad (to cover a
  5817. // 5x5 sample area, or 10x10 texel area), and it uses a lower standard
  5818. // deviation to compensate. Thanks to symmetry, the 11 omitted samples
  5819. // are always the "same:"
  5820. // 8adjx, 2adjx, 5adjx,
  5821. // 6adjy, 7adjy, 8adjy,
  5822. // 2diag, 5diag, 6diag, 7diag, 8diag
  5823. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  5824. // Statically compute bilinear sampling offsets (details in tex2Dblur12x12shared).
  5825. const float denom_inv = 0.5/(sigma*sigma);
  5826. const float w0off = 1.0;
  5827. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  5828. const float w1off = exp(-(1.0*1.0) * denom_inv);
  5829. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  5830. const float w2off = exp(-(2.0*2.0) * denom_inv);
  5831. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  5832. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  5833. const float w4_5off = exp(-(4.5*4.5) * denom_inv);
  5834. const float w5_5off = exp(-(5.5*5.5) * denom_inv);
  5835. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  5836. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  5837. const float texel4to5ratio = lerp(w5_5off/(w4_5off + w5_5off), 0.5, error_blurring);
  5838. // We don't share sample0*, so use the nearest destination fragment:
  5839. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  5840. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  5841. // Statically compute texel offsets from the bottom-right fragment to each
  5842. // bilinear sample in the bottom-right quadrant:
  5843. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  5844. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  5845. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  5846. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  5847. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  5848. const float2 sample2_texel_offset = float2(4.0, 0.0) + float2(texel4to5ratio, texel0to1ratio);
  5849. const float2 sample3_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  5850. const float2 sample4_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  5851. const float2 sample5_texel_offset = float2(4.0, 2.0) + float2(texel4to5ratio, texel2to3ratio);
  5852. const float2 sample6_texel_offset = float2(0.0, 4.0) + float2(texel0to1ratio, texel4to5ratio);
  5853. const float2 sample7_texel_offset = float2(2.0, 4.0) + float2(texel2to3ratio, texel4to5ratio);
  5854. const float2 sample8_texel_offset = float2(4.0, 4.0) + float2(texel4to5ratio, texel4to5ratio);
  5855. // CALCULATE KERNEL WEIGHTS:
  5856. // Statically compute bilinear sample weights at each destination fragment
  5857. // from the sum of their 4 texel weights (details in tex2Dblur12x12shared).
  5858. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  5859. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  5860. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  5861. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  5862. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  5863. // We only need 25 of the 36 sample weights. Skip the following weights:
  5864. // 8adjx, 2adjx, 5adjx,
  5865. // 6adjy, 7adjy, 8adjy,
  5866. // 2diag, 5diag, 6diag, 7diag, 8diag
  5867. const float w4diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -4.0);
  5868. const float w3diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -4.0);
  5869. const float w3adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -4.0);
  5870. const float w4adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -4.0);
  5871. const float w5adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -4.0);
  5872. const float w1diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -2.0);
  5873. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  5874. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  5875. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  5876. const float w2adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -2.0);
  5877. const float w1adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 0.0);
  5878. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  5879. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  5880. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  5881. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 0.0);
  5882. const float w4adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 2.0);
  5883. const float w3adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  5884. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  5885. const float w4curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  5886. const float w5curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 2.0);
  5887. const float w7adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 4.0);
  5888. const float w6adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 4.0);
  5889. const float w6curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 4.0);
  5890. const float w7curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 4.0);
  5891. const float w8curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 4.0);
  5892. #undef GET_TEXEL_QUAD_WEIGHTS
  5893. // Get the weight sum inverse (normalization factor):
  5894. const float weight_sum_inv = 1.0/(w0curr + w1curr + w2curr + w3curr +
  5895. w4curr + w5curr + w6curr + w7curr + w8curr +
  5896. w0adjx + w1adjx + w3adjx + w4adjx + w6adjx + w7adjx +
  5897. w0adjy + w1adjy + w2adjy + w3adjy + w4adjy + w5adjy +
  5898. w0diag + w1diag + w3diag + w4diag);
  5899. // Statically pack most weights for runtime. Note the mixed packing:
  5900. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  5901. const float4 w1 = float4(w1curr, w1adjx, w1adjy, w1diag);
  5902. const float4 w3 = float4(w3curr, w3adjx, w3adjy, w3diag);
  5903. const float4 w4 = float4(w4curr, w4adjx, w4adjy, w4diag);
  5904. const float4 w2and5 = float4(w2curr, w2adjy, w5curr, w5adjy);
  5905. const float4 w6and7 = float4(w6curr, w6adjx, w7curr, w7adjx);
  5906. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  5907. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  5908. const float2 dxdy_curr = dxdy * quad_vector.xy;
  5909. // Load bilinear samples for the current quadrant (for this fragment):
  5910. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  5911. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  5912. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  5913. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  5914. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  5915. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  5916. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  5917. const float3 sample4curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample4_texel_offset)).rgb;
  5918. const float3 sample5curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample5_texel_offset)).rgb;
  5919. const float3 sample6curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample6_texel_offset)).rgb;
  5920. const float3 sample7curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample7_texel_offset)).rgb;
  5921. const float3 sample8curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample8_texel_offset)).rgb;
  5922. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  5923. // Fetch the samples from other fragments in the 2x2 quad in order of need:
  5924. float3 sample1adjx, sample1adjy, sample1diag;
  5925. float3 sample2adjx, sample2adjy, sample2diag;
  5926. float3 sample3adjx, sample3adjy, sample3diag;
  5927. float3 sample4adjx, sample4adjy, sample4diag;
  5928. float3 sample5adjx, sample5adjy, sample5diag;
  5929. float3 sample6adjx, sample6adjy, sample6diag;
  5930. float3 sample7adjx, sample7adjy, sample7diag;
  5931. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  5932. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  5933. quad_gather(quad_vector, sample3curr, sample3adjx, sample3adjy, sample3diag);
  5934. quad_gather(quad_vector, sample4curr, sample4adjx, sample4adjy, sample4diag);
  5935. quad_gather(quad_vector, sample5curr, sample5adjx, sample5adjy, sample5diag);
  5936. quad_gather(quad_vector, sample6curr, sample6adjx, sample6adjy, sample6diag);
  5937. quad_gather(quad_vector, sample7curr, sample7adjx, sample7adjy, sample7diag);
  5938. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  5939. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  5940. // weights to obtain a weighted result. First do the simple ones:
  5941. float3 sum = float3(0.0,0.0,0.0);
  5942. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  5943. sum += mul(w1, float4x3(sample1curr, sample1adjx, sample1adjy, sample1diag));
  5944. sum += mul(w3, float4x3(sample3curr, sample3adjx, sample3adjy, sample3diag));
  5945. sum += mul(w4, float4x3(sample4curr, sample4adjx, sample4adjy, sample4diag));
  5946. // Now do the mixed-sample ones:
  5947. sum += mul(w2and5, float4x3(sample2curr, sample2adjy, sample5curr, sample5adjy));
  5948. sum += mul(w6and7, float4x3(sample6curr, sample6adjx, sample7curr, sample7adjx));
  5949. sum += w8curr * sample8curr;
  5950. // Normalize the sum (so the weights add to 1.0) and return:
  5951. return sum * weight_sum_inv;
  5952. }
  5953. float3 tex2Dblur8x8shared(const sampler2D tex,
  5954. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  5955. const float sigma)
  5956. {
  5957. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  5958. // Requires: Same as tex2Dblur12x12shared()
  5959. // Returns: A blurred texture lookup using a "virtual" 8x8 Gaussian
  5960. // blur (a 4x4 blur of carefully selected bilinear samples)
  5961. // of the given mip level. There will be subtle inaccuracies,
  5962. // especially for small or high-frequency detailed sources.
  5963. // Description:
  5964. // First see the description for tex2Dblur12x12shared(). This function
  5965. // shares the same concept and a similar sample placement, except each
  5966. // quadrant contains 4x4 texels and 2x2 samples instead of 6x6 and 3x3
  5967. // respectively. There could be a total of 16 samples, 4 of which each
  5968. // fragment is responsible for, but each fragment loads 0a/0b/0c/0d with
  5969. // its own offset to reduce shared sample artifacts, bringing the sample
  5970. // count for each fragment to 7. Sample placement:
  5971. // 3a 2a 2b 3b
  5972. // 1a 0a 0b 1b
  5973. // 1c 0c 0d 1d
  5974. // 3c 2c 2d 3d
  5975. // Texel placement:
  5976. // 3a3 3a2 2a3 2a2 2b2 2b3 3b2 3b3
  5977. // 3a1 3a0 2a1 2a0 2b0 2b1 3b0 3b1
  5978. // 1a3 1a2 0a3 0a2 0b2 0b3 1b2 1b3
  5979. // 1a1 1a0 0a1 0a0 0b0 0b1 1b0 1b1
  5980. // 1c1 1c0 0c1 0c0 0d0 0d1 1d0 1d1
  5981. // 1c3 1c2 0c3 0c2 0d2 0d3 1d2 1d3
  5982. // 3c1 3c0 2c1 2c0 2d0 2d1 3d0 4d1
  5983. // 3c3 3c2 2c3 2c2 2d2 2d3 3d2 4d3
  5984. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  5985. // Statically compute bilinear sampling offsets (details in tex2Dblur12x12shared).
  5986. const float denom_inv = 0.5/(sigma*sigma);
  5987. const float w0off = 1.0;
  5988. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  5989. const float w1off = exp(-(1.0*1.0) * denom_inv);
  5990. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  5991. const float w2off = exp(-(2.0*2.0) * denom_inv);
  5992. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  5993. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  5994. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  5995. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  5996. // We don't share sample0*, so use the nearest destination fragment:
  5997. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  5998. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  5999. // Statically compute texel offsets from the bottom-right fragment to each
  6000. // bilinear sample in the bottom-right quadrant:
  6001. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  6002. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  6003. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  6004. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  6005. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  6006. const float2 sample2_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  6007. const float2 sample3_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  6008. // CALCULATE KERNEL WEIGHTS:
  6009. // Statically compute bilinear sample weights at each destination fragment
  6010. // from the sum of their 4 texel weights (details in tex2Dblur12x12shared).
  6011. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  6012. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  6013. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  6014. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  6015. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  6016. const float w3diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -4.0);
  6017. const float w2diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -4.0);
  6018. const float w2adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -4.0);
  6019. const float w3adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -4.0);
  6020. const float w1diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -2.0);
  6021. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  6022. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  6023. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  6024. const float w1adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 0.0);
  6025. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  6026. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  6027. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  6028. const float w3adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 2.0);
  6029. const float w2adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  6030. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  6031. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  6032. #undef GET_TEXEL_QUAD_WEIGHTS
  6033. // Statically pack weights for runtime:
  6034. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  6035. const float4 w1 = float4(w1curr, w1adjx, w1adjy, w1diag);
  6036. const float4 w2 = float4(w2curr, w2adjx, w2adjy, w2diag);
  6037. const float4 w3 = float4(w3curr, w3adjx, w3adjy, w3diag);
  6038. // Get the weight sum inverse (normalization factor):
  6039. const float4 weight_sum4 = w0 + w1 + w2 + w3;
  6040. const float2 weight_sum2 = weight_sum4.xy + weight_sum4.zw;
  6041. const float weight_sum = weight_sum2.x + weight_sum2.y;
  6042. const float weight_sum_inv = 1.0/(weight_sum);
  6043. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  6044. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  6045. const float2 dxdy_curr = dxdy * quad_vector.xy;
  6046. // Load bilinear samples for the current quadrant (for this fragment):
  6047. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  6048. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  6049. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  6050. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  6051. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  6052. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  6053. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  6054. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  6055. // Fetch the samples from other fragments in the 2x2 quad:
  6056. float3 sample1adjx, sample1adjy, sample1diag;
  6057. float3 sample2adjx, sample2adjy, sample2diag;
  6058. float3 sample3adjx, sample3adjy, sample3diag;
  6059. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  6060. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  6061. quad_gather(quad_vector, sample3curr, sample3adjx, sample3adjy, sample3diag);
  6062. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  6063. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  6064. // weights to obtain a weighted result:
  6065. float3 sum = float3(0.0,0.0,0.0);
  6066. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  6067. sum += mul(w1, float4x3(sample1curr, sample1adjx, sample1adjy, sample1diag));
  6068. sum += mul(w2, float4x3(sample2curr, sample2adjx, sample2adjy, sample2diag));
  6069. sum += mul(w3, float4x3(sample3curr, sample3adjx, sample3adjy, sample3diag));
  6070. return sum * weight_sum_inv;
  6071. }
  6072. float3 tex2Dblur6x6shared(const sampler2D tex,
  6073. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  6074. const float sigma)
  6075. {
  6076. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  6077. // Requires: Same as tex2Dblur12x12shared()
  6078. // Returns: A blurred texture lookup using a "virtual" 6x6 Gaussian
  6079. // blur (a 3x3 blur of carefully selected bilinear samples)
  6080. // of the given mip level. There will be some inaccuracies,subtle inaccuracies,
  6081. // especially for small or high-frequency detailed sources.
  6082. // Description:
  6083. // First see the description for tex2Dblur8x8shared(). This
  6084. // function shares the same concept and sample placement, but each fragment
  6085. // only uses 9 of the 16 samples taken across the pixel quad (to cover a
  6086. // 3x3 sample area, or 6x6 texel area), and it uses a lower standard
  6087. // deviation to compensate. Thanks to symmetry, the 7 omitted samples
  6088. // are always the "same:"
  6089. // 1adjx, 3adjx
  6090. // 2adjy, 3adjy
  6091. // 1diag, 2diag, 3diag
  6092. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  6093. // Statically compute bilinear sampling offsets (details in tex2Dblur12x12shared).
  6094. const float denom_inv = 0.5/(sigma*sigma);
  6095. const float w0off = 1.0;
  6096. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  6097. const float w1off = exp(-(1.0*1.0) * denom_inv);
  6098. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  6099. const float w2off = exp(-(2.0*2.0) * denom_inv);
  6100. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  6101. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  6102. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  6103. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  6104. // We don't share sample0*, so use the nearest destination fragment:
  6105. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  6106. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  6107. // Statically compute texel offsets from the bottom-right fragment to each
  6108. // bilinear sample in the bottom-right quadrant:
  6109. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  6110. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  6111. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  6112. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  6113. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  6114. const float2 sample2_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  6115. const float2 sample3_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  6116. // CALCULATE KERNEL WEIGHTS:
  6117. // Statically compute bilinear sample weights at each destination fragment
  6118. // from the sum of their 4 texel weights (details in tex2Dblur12x12shared).
  6119. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  6120. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  6121. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  6122. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  6123. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  6124. // We only need 9 of the 16 sample weights. Skip the following weights:
  6125. // 1adjx, 3adjx
  6126. // 2adjy, 3adjy
  6127. // 1diag, 2diag, 3diag
  6128. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  6129. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  6130. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  6131. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  6132. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  6133. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  6134. const float w2adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  6135. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  6136. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  6137. #undef GET_TEXEL_QUAD_WEIGHTS
  6138. // Get the weight sum inverse (normalization factor):
  6139. const float weight_sum_inv = 1.0/(w0curr + w1curr + w2curr + w3curr +
  6140. w0adjx + w2adjx + w0adjy + w1adjy + w0diag);
  6141. // Statically pack some weights for runtime:
  6142. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  6143. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  6144. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  6145. const float2 dxdy_curr = dxdy * quad_vector.xy;
  6146. // Load bilinear samples for the current quadrant (for this fragment):
  6147. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  6148. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  6149. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  6150. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  6151. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  6152. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  6153. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  6154. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  6155. // Fetch the samples from other fragments in the 2x2 quad:
  6156. float3 sample1adjx, sample1adjy, sample1diag;
  6157. float3 sample2adjx, sample2adjy, sample2diag;
  6158. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  6159. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  6160. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  6161. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  6162. // weights to obtain a weighted result for sample1*, and handle the rest
  6163. // of the weights more directly/verbosely:
  6164. float3 sum = float3(0.0,0.0,0.0);
  6165. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  6166. sum += w1curr * sample1curr + w1adjy * sample1adjy + w2curr * sample2curr +
  6167. w2adjx * sample2adjx + w3curr * sample3curr;
  6168. return sum * weight_sum_inv;
  6169. }
  6170. /////////////////////// MAX OPTIMAL SIGMA BLUR WRAPPERS //////////////////////
  6171. // The following blurs are static wrappers around the dynamic blurs above.
  6172. // HOPEFULLY, the compiler will be smart enough to do constant-folding.
  6173. // Resizable separable blurs:
  6174. inline float3 tex2Dblur11resize(const sampler2D tex, const float2 tex_uv,
  6175. const float2 dxdy)
  6176. {
  6177. return tex2Dblur11resize(tex, tex_uv, dxdy, blur11_std_dev);
  6178. }
  6179. inline float3 tex2Dblur9resize(const sampler2D tex, const float2 tex_uv,
  6180. const float2 dxdy)
  6181. {
  6182. return tex2Dblur9resize(tex, tex_uv, dxdy, blur9_std_dev);
  6183. }
  6184. inline float3 tex2Dblur7resize(const sampler2D tex, const float2 tex_uv,
  6185. const float2 dxdy)
  6186. {
  6187. return tex2Dblur7resize(tex, tex_uv, dxdy, blur7_std_dev);
  6188. }
  6189. inline float3 tex2Dblur5resize(const sampler2D tex, const float2 tex_uv,
  6190. const float2 dxdy)
  6191. {
  6192. return tex2Dblur5resize(tex, tex_uv, dxdy, blur5_std_dev);
  6193. }
  6194. inline float3 tex2Dblur3resize(const sampler2D tex, const float2 tex_uv,
  6195. const float2 dxdy)
  6196. {
  6197. return tex2Dblur3resize(tex, tex_uv, dxdy, blur3_std_dev);
  6198. }
  6199. // Fast separable blurs:
  6200. inline float3 tex2Dblur11fast(const sampler2D tex, const float2 tex_uv,
  6201. const float2 dxdy)
  6202. {
  6203. return tex2Dblur11fast(tex, tex_uv, dxdy, blur11_std_dev);
  6204. }
  6205. inline float3 tex2Dblur9fast(const sampler2D tex, const float2 tex_uv,
  6206. const float2 dxdy)
  6207. {
  6208. return tex2Dblur9fast(tex, tex_uv, dxdy, blur9_std_dev);
  6209. }
  6210. inline float3 tex2Dblur7fast(const sampler2D tex, const float2 tex_uv,
  6211. const float2 dxdy)
  6212. {
  6213. return tex2Dblur7fast(tex, tex_uv, dxdy, blur7_std_dev);
  6214. }
  6215. inline float3 tex2Dblur5fast(const sampler2D tex, const float2 tex_uv,
  6216. const float2 dxdy)
  6217. {
  6218. return tex2Dblur5fast(tex, tex_uv, dxdy, blur5_std_dev);
  6219. }
  6220. inline float3 tex2Dblur3fast(const sampler2D tex, const float2 tex_uv,
  6221. const float2 dxdy)
  6222. {
  6223. return tex2Dblur3fast(tex, tex_uv, dxdy, blur3_std_dev);
  6224. }
  6225. // Huge, "fast" separable blurs:
  6226. inline float3 tex2Dblur43fast(const sampler2D tex, const float2 tex_uv,
  6227. const float2 dxdy)
  6228. {
  6229. return tex2Dblur43fast(tex, tex_uv, dxdy, blur43_std_dev);
  6230. }
  6231. inline float3 tex2Dblur31fast(const sampler2D tex, const float2 tex_uv,
  6232. const float2 dxdy)
  6233. {
  6234. return tex2Dblur31fast(tex, tex_uv, dxdy, blur31_std_dev);
  6235. }
  6236. inline float3 tex2Dblur25fast(const sampler2D tex, const float2 tex_uv,
  6237. const float2 dxdy)
  6238. {
  6239. return tex2Dblur25fast(tex, tex_uv, dxdy, blur25_std_dev);
  6240. }
  6241. inline float3 tex2Dblur17fast(const sampler2D tex, const float2 tex_uv,
  6242. const float2 dxdy)
  6243. {
  6244. return tex2Dblur17fast(tex, tex_uv, dxdy, blur17_std_dev);
  6245. }
  6246. // Resizable one-pass blurs:
  6247. inline float3 tex2Dblur3x3resize(const sampler2D tex, const float2 tex_uv,
  6248. const float2 dxdy)
  6249. {
  6250. return tex2Dblur3x3resize(tex, tex_uv, dxdy, blur3_std_dev);
  6251. }
  6252. // "Fast" one-pass blurs:
  6253. inline float3 tex2Dblur9x9(const sampler2D tex, const float2 tex_uv,
  6254. const float2 dxdy)
  6255. {
  6256. return tex2Dblur9x9(tex, tex_uv, dxdy, blur9_std_dev);
  6257. }
  6258. inline float3 tex2Dblur7x7(const sampler2D tex, const float2 tex_uv,
  6259. const float2 dxdy)
  6260. {
  6261. return tex2Dblur7x7(tex, tex_uv, dxdy, blur7_std_dev);
  6262. }
  6263. inline float3 tex2Dblur5x5(const sampler2D tex, const float2 tex_uv,
  6264. const float2 dxdy)
  6265. {
  6266. return tex2Dblur5x5(tex, tex_uv, dxdy, blur5_std_dev);
  6267. }
  6268. inline float3 tex2Dblur3x3(const sampler2D tex, const float2 tex_uv,
  6269. const float2 dxdy)
  6270. {
  6271. return tex2Dblur3x3(tex, tex_uv, dxdy, blur3_std_dev);
  6272. }
  6273. // "Fast" shared-sample one-pass blurs:
  6274. inline float3 tex2Dblur12x12shared(const sampler2D tex,
  6275. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  6276. {
  6277. return tex2Dblur12x12shared(tex, tex_uv, dxdy, quad_vector, blur12_std_dev);
  6278. }
  6279. inline float3 tex2Dblur10x10shared(const sampler2D tex,
  6280. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  6281. {
  6282. return tex2Dblur10x10shared(tex, tex_uv, dxdy, quad_vector, blur10_std_dev);
  6283. }
  6284. inline float3 tex2Dblur8x8shared(const sampler2D tex,
  6285. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  6286. {
  6287. return tex2Dblur8x8shared(tex, tex_uv, dxdy, quad_vector, blur8_std_dev);
  6288. }
  6289. inline float3 tex2Dblur6x6shared(const sampler2D tex,
  6290. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  6291. {
  6292. return tex2Dblur6x6shared(tex, tex_uv, dxdy, quad_vector, blur6_std_dev);
  6293. }
  6294. #endif // BLUR_FUNCTIONS_H
  6295. //////////////////////////// END BLUR-FUNCTIONS ///////////////////////////
  6296. /////////////////////////////// BLOOM CONSTANTS //////////////////////////////
  6297. // Compute constants with manual inlines of the functions below:
  6298. static const float bloom_diff_thresh = 1.0/256.0;
  6299. /////////////////////////////////// HELPERS //////////////////////////////////
  6300. inline float get_min_sigma_to_blur_triad(const float triad_size,
  6301. const float thresh)
  6302. {
  6303. // Requires: 1.) triad_size is the final phosphor triad size in pixels
  6304. // 2.) thresh is the max desired pixel difference in the
  6305. // blurred triad (e.g. 1.0/256.0).
  6306. // Returns: Return the minimum sigma that will fully blur a phosphor
  6307. // triad on the screen to an even color, within thresh.
  6308. // This closed-form function was found by curve-fitting data.
  6309. // Estimate: max error = ~0.086036, mean sq. error = ~0.0013387:
  6310. return -0.05168 + 0.6113*triad_size -
  6311. 1.122*triad_size*sqrt(0.000416 + thresh);
  6312. // Estimate: max error = ~0.16486, mean sq. error = ~0.0041041:
  6313. //return 0.5985*triad_size - triad_size*sqrt(thresh)
  6314. }
  6315. inline float get_absolute_scale_blur_sigma(const float thresh)
  6316. {
  6317. // Requires: 1.) min_expected_triads must be a global float. The number
  6318. // of horizontal phosphor triads in the final image must be
  6319. // >= min_allowed_viewport_triads.x for realistic results.
  6320. // 2.) bloom_approx_scale_x must be a global float equal to the
  6321. // absolute horizontal scale of BLOOM_APPROX.
  6322. // 3.) bloom_approx_scale_x/min_allowed_viewport_triads.x
  6323. // should be <= 1.1658025090 to keep the final result <
  6324. // 0.62666015625 (the largest sigma ensuring the largest
  6325. // unused texel weight stays < 1.0/256.0 for a 3x3 blur).
  6326. // 4.) thresh is the max desired pixel difference in the
  6327. // blurred triad (e.g. 1.0/256.0).
  6328. // Returns: Return the minimum Gaussian sigma that will blur the pass
  6329. // output as much as it would have taken to blur away
  6330. // bloom_approx_scale_x horizontal phosphor triads.
  6331. // Description:
  6332. // BLOOM_APPROX should look like a downscaled phosphor blur. Ideally, we'd
  6333. // use the same blur sigma as the actual phosphor bloom and scale it down
  6334. // to the current resolution with (bloom_approx_scale_x/viewport_size_x), but
  6335. // we don't know the viewport size in this pass. Instead, we'll blur as
  6336. // much as it would take to blur away min_allowed_viewport_triads.x. This
  6337. // will blur "more than necessary" if the user actually uses more triads,
  6338. // but that's not terrible either, because blurring a constant fraction of
  6339. // the viewport may better resemble a true optical bloom anyway (since the
  6340. // viewport will generally be about the same fraction of each player's
  6341. // field of view, regardless of screen size and resolution).
  6342. // Assume an extremely large viewport size for asymptotic results.
  6343. return bloom_approx_scale_x/max_viewport_size_x *
  6344. get_min_sigma_to_blur_triad(
  6345. max_viewport_size_x/min_allowed_viewport_triads.x, thresh);
  6346. }
  6347. inline float get_center_weight(const float sigma)
  6348. {
  6349. // Given a Gaussian blur sigma, get the blur weight for the center texel.
  6350. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  6351. return get_fast_gaussian_weight_sum_inv(sigma);
  6352. #else
  6353. const float denom_inv = 0.5/(sigma*sigma);
  6354. const float w0 = 1.0;
  6355. const float w1 = exp(-1.0 * denom_inv);
  6356. const float w2 = exp(-4.0 * denom_inv);
  6357. const float w3 = exp(-9.0 * denom_inv);
  6358. const float w4 = exp(-16.0 * denom_inv);
  6359. const float w5 = exp(-25.0 * denom_inv);
  6360. const float w6 = exp(-36.0 * denom_inv);
  6361. const float w7 = exp(-49.0 * denom_inv);
  6362. const float w8 = exp(-64.0 * denom_inv);
  6363. const float w9 = exp(-81.0 * denom_inv);
  6364. const float w10 = exp(-100.0 * denom_inv);
  6365. const float w11 = exp(-121.0 * denom_inv);
  6366. const float w12 = exp(-144.0 * denom_inv);
  6367. const float w13 = exp(-169.0 * denom_inv);
  6368. const float w14 = exp(-196.0 * denom_inv);
  6369. const float w15 = exp(-225.0 * denom_inv);
  6370. const float w16 = exp(-256.0 * denom_inv);
  6371. const float w17 = exp(-289.0 * denom_inv);
  6372. const float w18 = exp(-324.0 * denom_inv);
  6373. const float w19 = exp(-361.0 * denom_inv);
  6374. const float w20 = exp(-400.0 * denom_inv);
  6375. const float w21 = exp(-441.0 * denom_inv);
  6376. // Note: If the implementation uses a smaller blur than the max allowed,
  6377. // the worst case scenario is that the center weight will be overestimated,
  6378. // so we'll put a bit more energy into the brightpass...no huge deal.
  6379. // Then again, if the implementation uses a larger blur than the max
  6380. // "allowed" because of dynamic branching, the center weight could be
  6381. // underestimated, which is more of a problem...consider always using
  6382. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  6383. // 43x blur:
  6384. const float weight_sum_inv = 1.0 /
  6385. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 +
  6386. w11 + w12 + w13 + w14 + w15 + w16 + w17 + w18 + w19 + w20 + w21));
  6387. #else
  6388. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  6389. // 31x blur:
  6390. const float weight_sum_inv = 1.0 /
  6391. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 +
  6392. w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15));
  6393. #else
  6394. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  6395. // 25x blur:
  6396. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  6397. w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12));
  6398. #else
  6399. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  6400. // 17x blur:
  6401. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  6402. w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8));
  6403. #else
  6404. // 9x blur:
  6405. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  6406. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  6407. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  6408. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  6409. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  6410. const float center_weight = weight_sum_inv * weight_sum_inv;
  6411. return center_weight;
  6412. #endif
  6413. }
  6414. inline float3 tex2DblurNfast(const sampler2D texture, const float2 tex_uv,
  6415. const float2 dxdy, const float sigma)
  6416. {
  6417. // If sigma is static, we can safely branch and use the smallest blur
  6418. // that's big enough. Ignore #define hints, because we'll only use a
  6419. // large blur if we actually need it, and the branches cost nothing.
  6420. #ifndef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  6421. #define PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  6422. #else
  6423. // It's still worth branching if the profile supports dynamic branches:
  6424. // It's much faster than using a hugely excessive blur, but each branch
  6425. // eats ~1% FPS.
  6426. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  6427. #define PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  6428. #endif
  6429. #endif
  6430. // Failed optimization notes:
  6431. // I originally created a same-size mipmapped 5-tap separable blur10 that
  6432. // could handle any sigma by reaching into lower mip levels. It was
  6433. // as fast as blur25fast for runtime sigmas and a tad faster than
  6434. // blur31fast for static sigmas, but mipmapping two viewport-size passes
  6435. // ate 10% of FPS across all codepaths, so it wasn't worth it.
  6436. #ifdef PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  6437. if(sigma <= blur9_std_dev)
  6438. {
  6439. return tex2Dblur9fast(texture, tex_uv, dxdy, sigma);
  6440. }
  6441. else if(sigma <= blur17_std_dev)
  6442. {
  6443. return tex2Dblur17fast(texture, tex_uv, dxdy, sigma);
  6444. }
  6445. else if(sigma <= blur25_std_dev)
  6446. {
  6447. return tex2Dblur25fast(texture, tex_uv, dxdy, sigma);
  6448. }
  6449. else if(sigma <= blur31_std_dev)
  6450. {
  6451. return tex2Dblur31fast(texture, tex_uv, dxdy, sigma);
  6452. }
  6453. else
  6454. {
  6455. return tex2Dblur43fast(texture, tex_uv, dxdy, sigma);
  6456. }
  6457. #else
  6458. // If we can't afford to branch, we can only guess at what blur
  6459. // size we need. Therefore, use the largest blur allowed.
  6460. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  6461. return tex2Dblur43fast(texture, tex_uv, dxdy, sigma);
  6462. #else
  6463. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  6464. return tex2Dblur31fast(texture, tex_uv, dxdy, sigma);
  6465. #else
  6466. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  6467. return tex2Dblur25fast(texture, tex_uv, dxdy, sigma);
  6468. #else
  6469. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  6470. return tex2Dblur17fast(texture, tex_uv, dxdy, sigma);
  6471. #else
  6472. return tex2Dblur9fast(texture, tex_uv, dxdy, sigma);
  6473. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  6474. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  6475. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  6476. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  6477. #endif // PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  6478. }
  6479. inline float get_bloom_approx_sigma(const float output_size_x_runtime,
  6480. const float estimated_viewport_size_x)
  6481. {
  6482. // Requires: 1.) output_size_x_runtime == BLOOM_APPROX.output_size.x.
  6483. // This is included for dynamic codepaths just in case the
  6484. // following two globals are incorrect:
  6485. // 2.) bloom_approx_size_x_for_skip should == the same
  6486. // if PHOSPHOR_BLOOM_FAKE is #defined
  6487. // 3.) bloom_approx_size_x should == the same otherwise
  6488. // Returns: For gaussian4x4, return a dynamic small bloom sigma that's
  6489. // as close to optimal as possible given available information.
  6490. // For blur3x3, return the a static small bloom sigma that
  6491. // works well for typical cases. Otherwise, we're using simple
  6492. // bilinear filtering, so use static calculations.
  6493. // Assume the default static value. This is a compromise that ensures
  6494. // typical triads are blurred, even if unusually large ones aren't.
  6495. static const float mask_num_triads_static =
  6496. max(min_allowed_viewport_triads.x, mask_num_triads_desired_static);
  6497. const float mask_num_triads_from_size =
  6498. estimated_viewport_size_x/mask_triad_size_desired;
  6499. const float mask_num_triads_runtime = max(min_allowed_viewport_triads.x,
  6500. lerp(mask_num_triads_from_size, mask_num_triads_desired,
  6501. mask_specify_num_triads));
  6502. // Assume an extremely large viewport size for asymptotic results:
  6503. static const float max_viewport_size_x = 1080.0*1024.0*(4.0/3.0);
  6504. if(bloom_approx_filter > 1.5) // 4x4 true Gaussian resize
  6505. {
  6506. // Use the runtime num triads and output size:
  6507. const float asymptotic_triad_size =
  6508. max_viewport_size_x/mask_num_triads_runtime;
  6509. const float asymptotic_sigma = get_min_sigma_to_blur_triad(
  6510. asymptotic_triad_size, bloom_diff_thresh);
  6511. const float bloom_approx_sigma =
  6512. asymptotic_sigma * output_size_x_runtime/max_viewport_size_x;
  6513. // The BLOOM_APPROX input has to be ORIG_LINEARIZED to avoid moire, but
  6514. // account for the Gaussian scanline sigma from the last pass too.
  6515. // The bloom will be too wide horizontally but tall enough vertically.
  6516. return length(float2(bloom_approx_sigma, beam_max_sigma));
  6517. }
  6518. else // 3x3 blur resize (the bilinear resize doesn't need a sigma)
  6519. {
  6520. // We're either using blur3x3 or bilinear filtering. The biggest
  6521. // reason to choose blur3x3 is to avoid dynamic weights, so use a
  6522. // static calculation.
  6523. #ifdef PHOSPHOR_BLOOM_FAKE
  6524. static const float output_size_x_static =
  6525. bloom_approx_size_x_for_fake;
  6526. #else
  6527. static const float output_size_x_static = bloom_approx_size_x;
  6528. #endif
  6529. static const float asymptotic_triad_size =
  6530. max_viewport_size_x/mask_num_triads_static;
  6531. const float asymptotic_sigma = get_min_sigma_to_blur_triad(
  6532. asymptotic_triad_size, bloom_diff_thresh);
  6533. const float bloom_approx_sigma =
  6534. asymptotic_sigma * output_size_x_static/max_viewport_size_x;
  6535. // The BLOOM_APPROX input has to be ORIG_LINEARIZED to avoid moire, but
  6536. // try accounting for the Gaussian scanline sigma from the last pass
  6537. // too; use the static default value:
  6538. return length(float2(bloom_approx_sigma, beam_max_sigma_static));
  6539. }
  6540. }
  6541. inline float get_final_bloom_sigma(const float bloom_sigma_runtime)
  6542. {
  6543. // Requires: 1.) bloom_sigma_runtime is a precalculated sigma that's
  6544. // optimal for the [known] triad size.
  6545. // 2.) Call this from a fragment shader (not a vertex shader),
  6546. // or blurring with static sigmas won't be constant-folded.
  6547. // Returns: Return the optimistic static sigma if the triad size is
  6548. // known at compile time. Otherwise return the optimal runtime
  6549. // sigma (10% slower) or an implementation-specific compromise
  6550. // between an optimistic or pessimistic static sigma.
  6551. // Notes: Call this from the fragment shader, NOT the vertex shader,
  6552. // so static sigmas can be constant-folded!
  6553. const float bloom_sigma_optimistic = get_min_sigma_to_blur_triad(
  6554. mask_triad_size_desired_static, bloom_diff_thresh);
  6555. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  6556. return bloom_sigma_runtime;
  6557. #else
  6558. // Overblurring looks as bad as underblurring, so assume average-size
  6559. // triads, not worst-case huge triads:
  6560. return bloom_sigma_optimistic;
  6561. #endif
  6562. }
  6563. #endif // BLOOM_FUNCTIONS_H
  6564. //////////////////////////// END BLOOM-FUNCTIONS ///////////////////////////
  6565. /////////////////////////// END FRAGMENT-INCLUDES //////////////////////////
  6566. void main() {
  6567. // Blur the vertically blurred brightpass horizontally by 9/17/25/43x:
  6568. const float bloom_sigma = get_final_bloom_sigma(bloom_sigma_runtime);
  6569. const float3 blurred_brightpass = tex2DblurNfast(bloom_texture,
  6570. bloom_tex_uv, bloom_dxdy, bloom_sigma);
  6571. // Sample the masked scanlines. Alpha contains the auto-dim factor:
  6572. const float3 intensity_dim =
  6573. tex2D_linearize(MASKED_SCANLINEStexture, scanline_tex_uv).rgb;
  6574. const float auto_dim_factor = levels_autodim_temp;
  6575. const float undim_factor = 1.0/auto_dim_factor;
  6576. // Calculate the mask dimpass, add it to the blurred brightpass, and
  6577. // undim (from scanline auto-dim) and amplify (from mask dim) the result:
  6578. const float mask_amplify = get_mask_amplify();
  6579. const float3 brightpass = tex2D_linearize(BRIGHTPASStexture,
  6580. brightpass_tex_uv).rgb;
  6581. const float3 dimpass = intensity_dim - brightpass;
  6582. const float3 phosphor_bloom = (dimpass + blurred_brightpass) *
  6583. mask_amplify * undim_factor * levels_contrast;
  6584. // Sample the halation texture, and let some light bleed into refractive
  6585. // diffusion. Conceptually this occurs before the phosphor bloom, but
  6586. // adding it in earlier passes causes black crush in the diffusion colors.
  6587. const float3 diffusion_color = levels_contrast * tex2D_linearize(
  6588. HALATION_BLURtexture, halation_tex_uv).rgb;
  6589. const float3 final_bloom = lerp(phosphor_bloom,
  6590. diffusion_color, diffusion_weight);
  6591. // Encode and output the bloomed image:
  6592. FragColor = encode_output(float4(final_bloom, 1.0));
  6593. }