scanlines-vertical-interlacing.vs 303 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830
  1. #version 150
  2. in vec4 position;
  3. in vec2 texCoord;
  4. out Vertex {
  5. vec2 vTexCoord;
  6. vec2 uv_step;
  7. vec2 il_step_multiple;
  8. float pixel_height_in_scanlines;
  9. };
  10. uniform vec4 targetSize;
  11. uniform vec4 sourceSize[];
  12. // USER SETTINGS BLOCK //
  13. #define crt_gamma 2.500000
  14. #define lcd_gamma 2.200000
  15. #define levels_contrast 1.0
  16. #define halation_weight 0.0
  17. #define diffusion_weight 0.075
  18. #define bloom_underestimate_levels 0.8
  19. #define bloom_excess 0.000000
  20. #define beam_min_sigma 0.020000
  21. #define beam_max_sigma 0.300000
  22. #define beam_spot_power 0.330000
  23. #define beam_min_shape 2.000000
  24. #define beam_max_shape 4.000000
  25. #define beam_shape_power 0.250000
  26. #define beam_horiz_filter 0.000000
  27. #define beam_horiz_sigma 0.35
  28. #define beam_horiz_linear_rgb_weight 1.000000
  29. #define convergence_offset_x_r -0.000000
  30. #define convergence_offset_x_g 0.000000
  31. #define convergence_offset_x_b 0.000000
  32. #define convergence_offset_y_r 0.000000
  33. #define convergence_offset_y_g -0.000000
  34. #define convergence_offset_y_b 0.000000
  35. #define mask_type 1.000000
  36. #define mask_sample_mode_desired 0.000000
  37. #define mask_specify_num_triads 0.000000
  38. #define mask_triad_size_desired 3.000000
  39. #define mask_num_triads_desired 480.000000
  40. #define aa_subpixel_r_offset_x_runtime -0.0
  41. #define aa_subpixel_r_offset_y_runtime 0.000000
  42. #define aa_cubic_c 0.500000
  43. #define aa_gauss_sigma 0.500000
  44. #define geom_mode_runtime 2.000000
  45. #define geom_radius 2.000000
  46. #define geom_view_dist 2.000000
  47. #define geom_tilt_angle_x 0.000000
  48. #define geom_tilt_angle_y 0.000000
  49. #define geom_aspect_ratio_x 432.000000
  50. #define geom_aspect_ratio_y 329.000000
  51. #define geom_overscan_x 1.000000
  52. #define geom_overscan_y 1.000000
  53. #define border_size 0.015
  54. #define border_darkness 2.0
  55. #define border_compress 2.500000
  56. #define interlace_bff 0.000000
  57. #define interlace_1080i 0.000000
  58. // END USER SETTINGS BLOCK //
  59. // compatibility macros for transparently converting HLSLisms into GLSLisms
  60. #define mul(a,b) (b*a)
  61. #define lerp(a,b,c) mix(a,b,c)
  62. #define saturate(c) clamp(c, 0.0, 1.0)
  63. #define frac(x) (fract(x))
  64. #define float2 vec2
  65. #define float3 vec3
  66. #define float4 vec4
  67. #define bool2 bvec2
  68. #define bool3 bvec3
  69. #define bool4 bvec4
  70. #define float2x2 mat2x2
  71. #define float3x3 mat3x3
  72. #define float4x4 mat4x4
  73. #define float4x3 mat4x3
  74. #define float2x4 mat2x4
  75. #define IN params
  76. #define texture_size sourceSize[0].xy
  77. #define video_size sourceSize[0].xy
  78. #define output_size targetSize.xy
  79. #define frame_count phase
  80. #define static
  81. #define inline
  82. #define const
  83. #define fmod(x,y) mod(x,y)
  84. #define ddx(c) dFdx(c)
  85. #define ddy(c) dFdy(c)
  86. #define atan2(x,y) atan(y,x)
  87. #define rsqrt(c) inversesqrt(c)
  88. #define input_texture source[0]
  89. #if defined(GL_ES)
  90. #define COMPAT_PRECISION mediump
  91. #else
  92. #define COMPAT_PRECISION
  93. #endif
  94. #if __VERSION__ >= 130
  95. #define COMPAT_TEXTURE texture
  96. #else
  97. #define COMPAT_TEXTURE texture2D
  98. #endif
  99. ////////////////////////////////// INCLUDES //////////////////////////////////
  100. //#include "../user-settings.h"
  101. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  102. #ifndef USER_SETTINGS_H
  103. #define USER_SETTINGS_H
  104. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  105. // The Cg compiler uses different "profiles" with different capabilities.
  106. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  107. // require higher profiles like fp30 or fp40. The shader can't detect profile
  108. // or driver capabilities, so instead you must comment or uncomment the lines
  109. // below with "//" before "#define." Disable an option if you get compilation
  110. // errors resembling those listed. Generally speaking, all of these options
  111. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  112. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  113. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  114. // Among other things, derivatives help us fix anisotropic filtering artifacts
  115. // with curved manually tiled phosphor mask coords. Related errors:
  116. // error C3004: function "float2 ddx(float2);" not supported in this profile
  117. // error C3004: function "float2 ddy(float2);" not supported in this profile
  118. //#define DRIVERS_ALLOW_DERIVATIVES
  119. // Fine derivatives: Unsupported on older ATI cards.
  120. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  121. // fast single-pass blur operations. If your card uses coarse derivatives and
  122. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  123. #ifdef DRIVERS_ALLOW_DERIVATIVES
  124. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  125. #endif
  126. // Dynamic looping: Requires an fp30 or newer profile.
  127. // This makes phosphor mask resampling faster in some cases. Related errors:
  128. // error C5013: profile does not support "for" statements and "for" could not
  129. // be unrolled
  130. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  131. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  132. // Using one static loop avoids overhead if the user is right, but if the user
  133. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  134. // binary search can potentially save some iterations. However, it may fail:
  135. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  136. // needed to compile program
  137. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  138. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  139. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  140. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  141. // this profile
  142. //#define DRIVERS_ALLOW_TEX2DLOD
  143. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  144. // artifacts from anisotropic filtering and mipmapping. Related errors:
  145. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  146. // in this profile
  147. //#define DRIVERS_ALLOW_TEX2DBIAS
  148. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  149. // impose stricter limitations on register counts and instructions. Enable
  150. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  151. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  152. // to compile program.
  153. // Enabling integrated graphics compatibility mode will automatically disable:
  154. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  155. // (This may be reenabled in a later release.)
  156. // 2.) RUNTIME_GEOMETRY_MODE
  157. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  158. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  159. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  160. // To disable a #define option, turn its line into a comment with "//."
  161. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  162. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  163. // many of the options in this file and allow real-time tuning, but many of
  164. // them are slower. Disabling them and using this text file will boost FPS.
  165. #define RUNTIME_SHADER_PARAMS_ENABLE
  166. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  167. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  168. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  169. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  170. #define RUNTIME_ANTIALIAS_WEIGHTS
  171. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  172. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  173. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  174. // parameters? This will require more math or dynamic branching.
  175. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  176. // Specify the tilt at runtime? This makes things about 3% slower.
  177. #define RUNTIME_GEOMETRY_TILT
  178. // Specify the geometry mode at runtime?
  179. #define RUNTIME_GEOMETRY_MODE
  180. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  181. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  182. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  183. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  184. // PHOSPHOR MASK:
  185. // Manually resize the phosphor mask for best results (slower)? Disabling this
  186. // removes the option to do so, but it may be faster without dynamic branches.
  187. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  188. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  189. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  190. // Larger blurs are expensive, but we need them to blur larger triads. We can
  191. // detect the right blur if the triad size is static or our profile allows
  192. // dynamic branches, but otherwise we use the largest blur the user indicates
  193. // they might need:
  194. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  195. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  196. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  197. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  198. // Here's a helpful chart:
  199. // MaxTriadSize BlurSize MinTriadCountsByResolution
  200. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  201. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  202. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  203. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  204. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  205. /////////////////////////////// USER PARAMETERS //////////////////////////////
  206. // Note: Many of these static parameters are overridden by runtime shader
  207. // parameters when those are enabled. However, many others are static codepath
  208. // options that were cleaner or more convert to code as static constants.
  209. // GAMMA:
  210. static const float crt_gamma_static = 2.5; // range [1, 5]
  211. static const float lcd_gamma_static = 2.2; // range [1, 5]
  212. // LEVELS MANAGEMENT:
  213. // Control the final multiplicative image contrast:
  214. static const float levels_contrast_static = 1.0; // range [0, 4)
  215. // We auto-dim to avoid clipping between passes and restore brightness
  216. // later. Control the dim factor here: Lower values clip less but crush
  217. // blacks more (static only for now).
  218. 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
  219. // HALATION/DIFFUSION/BLOOM:
  220. // Halation weight: How much energy should be lost to electrons bounding
  221. // around under the CRT glass and exciting random phosphors?
  222. static const float halation_weight_static = 0.0; // range [0, 1]
  223. // Refractive diffusion weight: How much light should spread/diffuse from
  224. // refracting through the CRT glass?
  225. static const float diffusion_weight_static = 0.075; // range [0, 1]
  226. // Underestimate brightness: Bright areas bloom more, but we can base the
  227. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  228. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  229. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  230. // Blur all colors more than necessary for a softer phosphor bloom?
  231. static const float bloom_excess_static = 0.0; // range [0, 1]
  232. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  233. // blurred resize of the input (convergence offsets are applied as well).
  234. // There are three filter options (static option only for now):
  235. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  236. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  237. // and beam_max_sigma is low.
  238. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  239. // always uses a static sigma regardless of beam_max_sigma or
  240. // mask_num_triads_desired.
  241. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  242. // These options are more pronounced for the fast, unbloomed shader version.
  243. #ifndef RADEON_FIX
  244. static const float bloom_approx_filter_static = 2.0;
  245. #else
  246. static const float bloom_approx_filter_static = 1.0;
  247. #endif
  248. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  249. // How many scanlines should contribute light to each pixel? Using more
  250. // scanlines is slower (especially for a generalized Gaussian) but less
  251. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  252. // max_beam_sigma at which the closest unused weight is guaranteed <
  253. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  254. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  255. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  256. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  257. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  258. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  259. static const float beam_num_scanlines = 3.0; // range [2, 6]
  260. // A generalized Gaussian beam varies shape with color too, now just width.
  261. // It's slower but more flexible (static option only for now).
  262. static const bool beam_generalized_gaussian = true;
  263. // What kind of scanline antialiasing do you want?
  264. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  265. // Integrals are slow (especially for generalized Gaussians) and rarely any
  266. // better than 3x antialiasing (static option only for now).
  267. static const float beam_antialias_level = 1.0; // range [0, 2]
  268. // Min/max standard deviations for scanline beams: Higher values widen and
  269. // soften scanlines. Depending on other options, low min sigmas can alias.
  270. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  271. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  272. // Beam width varies as a function of color: A power function (0) is more
  273. // configurable, but a spherical function (1) gives the widest beam
  274. // variability without aliasing (static option only for now).
  275. static const float beam_spot_shape_function = 0.0;
  276. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  277. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  278. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  279. // Generalized Gaussian max shape parameters: Higher values give flatter
  280. // scanline plateaus and steeper dropoffs, simultaneously widening and
  281. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  282. // values > ~40.0 cause artifacts with integrals.
  283. static const float beam_min_shape_static = 2.0; // range [2, 32]
  284. static const float beam_max_shape_static = 4.0; // range [2, 32]
  285. // Generalized Gaussian shape power: Affects how quickly the distribution
  286. // changes shape from Gaussian to steep/plateaued as color increases from 0
  287. // to 1.0. Higher powers appear softer for most colors, and lower powers
  288. // appear sharper for most colors.
  289. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  290. // What filter should be used to sample scanlines horizontally?
  291. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  292. static const float beam_horiz_filter_static = 0.0;
  293. // Standard deviation for horizontal Gaussian resampling:
  294. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  295. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  296. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  297. // limiting circuitry in some CRT's), or a weighted avg.?
  298. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  299. // Simulate scanline misconvergence? This needs 3x horizontal texture
  300. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  301. // later passes (static option only for now).
  302. static const bool beam_misconvergence = true;
  303. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  304. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  305. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  306. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  307. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  308. // Detect interlacing (static option only for now)?
  309. static const bool interlace_detect = true;
  310. // Assume 1080-line sources are interlaced?
  311. static const bool interlace_1080i_static = false;
  312. // For interlaced sources, assume TFF (top-field first) or BFF order?
  313. // (Whether this matters depends on the nature of the interlaced input.)
  314. static const bool interlace_bff_static = false;
  315. // ANTIALIASING:
  316. // What AA level do you want for curvature/overscan/subpixels? Options:
  317. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  318. // (Static option only for now)
  319. static const float aa_level = 12.0; // range [0, 24]
  320. // What antialiasing filter do you want (static option only)? Options:
  321. // 0: Box (separable), 1: Box (cylindrical),
  322. // 2: Tent (separable), 3: Tent (cylindrical),
  323. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  324. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  325. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  326. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  327. static const float aa_filter = 6.0; // range [0, 9]
  328. // Flip the sample grid on odd/even frames (static option only for now)?
  329. static const bool aa_temporal = false;
  330. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  331. // the blue offset is the negative r offset; range [0, 0.5]
  332. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  333. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  334. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  335. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  336. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  337. // 4.) C = 0.0 is a soft spline filter.
  338. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  339. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  340. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  341. // PHOSPHOR MASK:
  342. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  343. static const float mask_type_static = 1.0; // range [0, 2]
  344. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  345. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  346. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  347. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  348. // is halfway decent with LUT mipmapping but atrocious without it.
  349. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  350. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  351. // This mode reuses the same masks, so triads will be enormous unless
  352. // you change the mask LUT filenames in your .cgp file.
  353. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  354. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  355. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  356. // will always be used to calculate the full bloom sigma statically.
  357. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  358. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  359. // triads) will be rounded to the nearest integer tile size and clamped to
  360. // obey minimum size constraints (imposed to reduce downsize taps) and
  361. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  362. // To increase the size limit, double the viewport-relative scales for the
  363. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  364. // range [1, mask_texture_small_size/mask_triads_per_tile]
  365. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  366. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  367. // final size will be rounded and constrained as above); default 480.0
  368. static const float mask_num_triads_desired_static = 480.0;
  369. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  370. // more samples and avoid moire a bit better, but some is unavoidable
  371. // depending on the destination size (static option for now).
  372. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  373. // The mask is resized using a variable number of taps in each dimension,
  374. // but some Cg profiles always fetch a constant number of taps no matter
  375. // what (no dynamic branching). We can limit the maximum number of taps if
  376. // we statically limit the minimum phosphor triad size. Larger values are
  377. // faster, but the limit IS enforced (static option only, forever);
  378. // range [1, mask_texture_small_size/mask_triads_per_tile]
  379. // TODO: Make this 1.0 and compensate with smarter sampling!
  380. static const float mask_min_allowed_triad_size = 2.0;
  381. // GEOMETRY:
  382. // Geometry mode:
  383. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  384. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  385. static const float geom_mode_static = 0.0; // range [0, 3]
  386. // Radius of curvature: Measured in units of your viewport's diagonal size.
  387. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  388. // View dist is the distance from the player to their physical screen, in
  389. // units of the viewport's diagonal size. It controls the field of view.
  390. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  391. // Tilt angle in radians (clockwise around up and right vectors):
  392. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  393. // Aspect ratio: When the true viewport size is unknown, this value is used
  394. // to help convert between the phosphor triad size and count, along with
  395. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  396. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  397. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  398. // default (256/224)*(54/47) = 1.313069909 (see below)
  399. static const float geom_aspect_ratio_static = 1.313069909;
  400. // Before getting into overscan, here's some general aspect ratio info:
  401. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  402. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  403. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  404. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  405. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  406. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  407. // a.) Enable Retroarch's "Crop Overscan"
  408. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  409. // Real consoles use horizontal black padding in the signal, but emulators
  410. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  411. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  412. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  413. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  414. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  415. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  416. // without doing a. or b., but horizontal image borders will be tighter
  417. // than vertical ones, messing up curvature and overscan. Fixing the
  418. // padding first corrects this.
  419. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  420. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  421. // above: Values < 1.0 zoom out; range (0, inf)
  422. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  423. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  424. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  425. // with strong curvature (static option only for now).
  426. static const bool geom_force_correct_tangent_matrix = true;
  427. // BORDERS:
  428. // Rounded border size in texture uv coords:
  429. static const float border_size_static = 0.015; // range [0, 0.5]
  430. // Border darkness: Moderate values darken the border smoothly, and high
  431. // values make the image very dark just inside the border:
  432. static const float border_darkness_static = 2.0; // range [0, inf)
  433. // Border compression: High numbers compress border transitions, narrowing
  434. // the dark border area.
  435. static const float border_compress_static = 2.5; // range [1, inf)
  436. #endif // USER_SETTINGS_H
  437. //////////////////////////// END USER-SETTINGS //////////////////////////
  438. //#include "derived-settings-and-constants.h"
  439. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  440. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  441. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  442. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  443. // crt-royale: A full-featured CRT shader, with cheese.
  444. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  445. //
  446. // This program is free software; you can redistribute it and/or modify it
  447. // under the terms of the GNU General Public License as published by the Free
  448. // Software Foundation; either version 2 of the License, or any later version.
  449. //
  450. // This program is distributed in the hope that it will be useful, but WITHOUT
  451. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  452. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  453. // more details.
  454. //
  455. // You should have received a copy of the GNU General Public License along with
  456. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  457. // Place, Suite 330, Boston, MA 02111-1307 USA
  458. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  459. // These macros and constants can be used across the whole codebase.
  460. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  461. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  462. //#include "../user-settings.h"
  463. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  464. #ifndef USER_SETTINGS_H
  465. #define USER_SETTINGS_H
  466. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  467. // The Cg compiler uses different "profiles" with different capabilities.
  468. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  469. // require higher profiles like fp30 or fp40. The shader can't detect profile
  470. // or driver capabilities, so instead you must comment or uncomment the lines
  471. // below with "//" before "#define." Disable an option if you get compilation
  472. // errors resembling those listed. Generally speaking, all of these options
  473. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  474. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  475. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  476. // Among other things, derivatives help us fix anisotropic filtering artifacts
  477. // with curved manually tiled phosphor mask coords. Related errors:
  478. // error C3004: function "float2 ddx(float2);" not supported in this profile
  479. // error C3004: function "float2 ddy(float2);" not supported in this profile
  480. //#define DRIVERS_ALLOW_DERIVATIVES
  481. // Fine derivatives: Unsupported on older ATI cards.
  482. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  483. // fast single-pass blur operations. If your card uses coarse derivatives and
  484. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  485. #ifdef DRIVERS_ALLOW_DERIVATIVES
  486. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  487. #endif
  488. // Dynamic looping: Requires an fp30 or newer profile.
  489. // This makes phosphor mask resampling faster in some cases. Related errors:
  490. // error C5013: profile does not support "for" statements and "for" could not
  491. // be unrolled
  492. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  493. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  494. // Using one static loop avoids overhead if the user is right, but if the user
  495. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  496. // binary search can potentially save some iterations. However, it may fail:
  497. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  498. // needed to compile program
  499. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  500. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  501. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  502. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  503. // this profile
  504. //#define DRIVERS_ALLOW_TEX2DLOD
  505. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  506. // artifacts from anisotropic filtering and mipmapping. Related errors:
  507. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  508. // in this profile
  509. //#define DRIVERS_ALLOW_TEX2DBIAS
  510. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  511. // impose stricter limitations on register counts and instructions. Enable
  512. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  513. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  514. // to compile program.
  515. // Enabling integrated graphics compatibility mode will automatically disable:
  516. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  517. // (This may be reenabled in a later release.)
  518. // 2.) RUNTIME_GEOMETRY_MODE
  519. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  520. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  521. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  522. // To disable a #define option, turn its line into a comment with "//."
  523. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  524. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  525. // many of the options in this file and allow real-time tuning, but many of
  526. // them are slower. Disabling them and using this text file will boost FPS.
  527. #define RUNTIME_SHADER_PARAMS_ENABLE
  528. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  529. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  530. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  531. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  532. #define RUNTIME_ANTIALIAS_WEIGHTS
  533. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  534. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  535. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  536. // parameters? This will require more math or dynamic branching.
  537. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  538. // Specify the tilt at runtime? This makes things about 3% slower.
  539. #define RUNTIME_GEOMETRY_TILT
  540. // Specify the geometry mode at runtime?
  541. #define RUNTIME_GEOMETRY_MODE
  542. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  543. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  544. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  545. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  546. // PHOSPHOR MASK:
  547. // Manually resize the phosphor mask for best results (slower)? Disabling this
  548. // removes the option to do so, but it may be faster without dynamic branches.
  549. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  550. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  551. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  552. // Larger blurs are expensive, but we need them to blur larger triads. We can
  553. // detect the right blur if the triad size is static or our profile allows
  554. // dynamic branches, but otherwise we use the largest blur the user indicates
  555. // they might need:
  556. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  557. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  558. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  559. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  560. // Here's a helpful chart:
  561. // MaxTriadSize BlurSize MinTriadCountsByResolution
  562. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  563. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  564. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  565. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  566. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  567. /////////////////////////////// USER PARAMETERS //////////////////////////////
  568. // Note: Many of these static parameters are overridden by runtime shader
  569. // parameters when those are enabled. However, many others are static codepath
  570. // options that were cleaner or more convert to code as static constants.
  571. // GAMMA:
  572. static const float crt_gamma_static = 2.5; // range [1, 5]
  573. static const float lcd_gamma_static = 2.2; // range [1, 5]
  574. // LEVELS MANAGEMENT:
  575. // Control the final multiplicative image contrast:
  576. static const float levels_contrast_static = 1.0; // range [0, 4)
  577. // We auto-dim to avoid clipping between passes and restore brightness
  578. // later. Control the dim factor here: Lower values clip less but crush
  579. // blacks more (static only for now).
  580. 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
  581. // HALATION/DIFFUSION/BLOOM:
  582. // Halation weight: How much energy should be lost to electrons bounding
  583. // around under the CRT glass and exciting random phosphors?
  584. static const float halation_weight_static = 0.0; // range [0, 1]
  585. // Refractive diffusion weight: How much light should spread/diffuse from
  586. // refracting through the CRT glass?
  587. static const float diffusion_weight_static = 0.075; // range [0, 1]
  588. // Underestimate brightness: Bright areas bloom more, but we can base the
  589. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  590. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  591. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  592. // Blur all colors more than necessary for a softer phosphor bloom?
  593. static const float bloom_excess_static = 0.0; // range [0, 1]
  594. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  595. // blurred resize of the input (convergence offsets are applied as well).
  596. // There are three filter options (static option only for now):
  597. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  598. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  599. // and beam_max_sigma is low.
  600. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  601. // always uses a static sigma regardless of beam_max_sigma or
  602. // mask_num_triads_desired.
  603. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  604. // These options are more pronounced for the fast, unbloomed shader version.
  605. #ifndef RADEON_FIX
  606. static const float bloom_approx_filter_static = 2.0;
  607. #else
  608. static const float bloom_approx_filter_static = 1.0;
  609. #endif
  610. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  611. // How many scanlines should contribute light to each pixel? Using more
  612. // scanlines is slower (especially for a generalized Gaussian) but less
  613. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  614. // max_beam_sigma at which the closest unused weight is guaranteed <
  615. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  616. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  617. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  618. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  619. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  620. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  621. static const float beam_num_scanlines = 3.0; // range [2, 6]
  622. // A generalized Gaussian beam varies shape with color too, now just width.
  623. // It's slower but more flexible (static option only for now).
  624. static const bool beam_generalized_gaussian = true;
  625. // What kind of scanline antialiasing do you want?
  626. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  627. // Integrals are slow (especially for generalized Gaussians) and rarely any
  628. // better than 3x antialiasing (static option only for now).
  629. static const float beam_antialias_level = 1.0; // range [0, 2]
  630. // Min/max standard deviations for scanline beams: Higher values widen and
  631. // soften scanlines. Depending on other options, low min sigmas can alias.
  632. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  633. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  634. // Beam width varies as a function of color: A power function (0) is more
  635. // configurable, but a spherical function (1) gives the widest beam
  636. // variability without aliasing (static option only for now).
  637. static const float beam_spot_shape_function = 0.0;
  638. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  639. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  640. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  641. // Generalized Gaussian max shape parameters: Higher values give flatter
  642. // scanline plateaus and steeper dropoffs, simultaneously widening and
  643. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  644. // values > ~40.0 cause artifacts with integrals.
  645. static const float beam_min_shape_static = 2.0; // range [2, 32]
  646. static const float beam_max_shape_static = 4.0; // range [2, 32]
  647. // Generalized Gaussian shape power: Affects how quickly the distribution
  648. // changes shape from Gaussian to steep/plateaued as color increases from 0
  649. // to 1.0. Higher powers appear softer for most colors, and lower powers
  650. // appear sharper for most colors.
  651. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  652. // What filter should be used to sample scanlines horizontally?
  653. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  654. static const float beam_horiz_filter_static = 0.0;
  655. // Standard deviation for horizontal Gaussian resampling:
  656. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  657. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  658. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  659. // limiting circuitry in some CRT's), or a weighted avg.?
  660. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  661. // Simulate scanline misconvergence? This needs 3x horizontal texture
  662. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  663. // later passes (static option only for now).
  664. static const bool beam_misconvergence = true;
  665. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  666. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  667. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  668. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  669. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  670. // Detect interlacing (static option only for now)?
  671. static const bool interlace_detect = true;
  672. // Assume 1080-line sources are interlaced?
  673. static const bool interlace_1080i_static = false;
  674. // For interlaced sources, assume TFF (top-field first) or BFF order?
  675. // (Whether this matters depends on the nature of the interlaced input.)
  676. static const bool interlace_bff_static = false;
  677. // ANTIALIASING:
  678. // What AA level do you want for curvature/overscan/subpixels? Options:
  679. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  680. // (Static option only for now)
  681. static const float aa_level = 12.0; // range [0, 24]
  682. // What antialiasing filter do you want (static option only)? Options:
  683. // 0: Box (separable), 1: Box (cylindrical),
  684. // 2: Tent (separable), 3: Tent (cylindrical),
  685. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  686. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  687. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  688. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  689. static const float aa_filter = 6.0; // range [0, 9]
  690. // Flip the sample grid on odd/even frames (static option only for now)?
  691. static const bool aa_temporal = false;
  692. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  693. // the blue offset is the negative r offset; range [0, 0.5]
  694. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  695. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  696. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  697. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  698. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  699. // 4.) C = 0.0 is a soft spline filter.
  700. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  701. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  702. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  703. // PHOSPHOR MASK:
  704. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  705. static const float mask_type_static = 1.0; // range [0, 2]
  706. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  707. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  708. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  709. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  710. // is halfway decent with LUT mipmapping but atrocious without it.
  711. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  712. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  713. // This mode reuses the same masks, so triads will be enormous unless
  714. // you change the mask LUT filenames in your .cgp file.
  715. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  716. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  717. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  718. // will always be used to calculate the full bloom sigma statically.
  719. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  720. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  721. // triads) will be rounded to the nearest integer tile size and clamped to
  722. // obey minimum size constraints (imposed to reduce downsize taps) and
  723. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  724. // To increase the size limit, double the viewport-relative scales for the
  725. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  726. // range [1, mask_texture_small_size/mask_triads_per_tile]
  727. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  728. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  729. // final size will be rounded and constrained as above); default 480.0
  730. static const float mask_num_triads_desired_static = 480.0;
  731. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  732. // more samples and avoid moire a bit better, but some is unavoidable
  733. // depending on the destination size (static option for now).
  734. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  735. // The mask is resized using a variable number of taps in each dimension,
  736. // but some Cg profiles always fetch a constant number of taps no matter
  737. // what (no dynamic branching). We can limit the maximum number of taps if
  738. // we statically limit the minimum phosphor triad size. Larger values are
  739. // faster, but the limit IS enforced (static option only, forever);
  740. // range [1, mask_texture_small_size/mask_triads_per_tile]
  741. // TODO: Make this 1.0 and compensate with smarter sampling!
  742. static const float mask_min_allowed_triad_size = 2.0;
  743. // GEOMETRY:
  744. // Geometry mode:
  745. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  746. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  747. static const float geom_mode_static = 0.0; // range [0, 3]
  748. // Radius of curvature: Measured in units of your viewport's diagonal size.
  749. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  750. // View dist is the distance from the player to their physical screen, in
  751. // units of the viewport's diagonal size. It controls the field of view.
  752. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  753. // Tilt angle in radians (clockwise around up and right vectors):
  754. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  755. // Aspect ratio: When the true viewport size is unknown, this value is used
  756. // to help convert between the phosphor triad size and count, along with
  757. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  758. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  759. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  760. // default (256/224)*(54/47) = 1.313069909 (see below)
  761. static const float geom_aspect_ratio_static = 1.313069909;
  762. // Before getting into overscan, here's some general aspect ratio info:
  763. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  764. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  765. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  766. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  767. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  768. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  769. // a.) Enable Retroarch's "Crop Overscan"
  770. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  771. // Real consoles use horizontal black padding in the signal, but emulators
  772. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  773. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  774. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  775. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  776. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  777. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  778. // without doing a. or b., but horizontal image borders will be tighter
  779. // than vertical ones, messing up curvature and overscan. Fixing the
  780. // padding first corrects this.
  781. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  782. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  783. // above: Values < 1.0 zoom out; range (0, inf)
  784. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  785. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  786. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  787. // with strong curvature (static option only for now).
  788. static const bool geom_force_correct_tangent_matrix = true;
  789. // BORDERS:
  790. // Rounded border size in texture uv coords:
  791. static const float border_size_static = 0.015; // range [0, 0.5]
  792. // Border darkness: Moderate values darken the border smoothly, and high
  793. // values make the image very dark just inside the border:
  794. static const float border_darkness_static = 2.0; // range [0, inf)
  795. // Border compression: High numbers compress border transitions, narrowing
  796. // the dark border area.
  797. static const float border_compress_static = 2.5; // range [1, inf)
  798. #endif // USER_SETTINGS_H
  799. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  800. //#include "user-cgp-constants.h"
  801. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  802. #ifndef USER_CGP_CONSTANTS_H
  803. #define USER_CGP_CONSTANTS_H
  804. // IMPORTANT:
  805. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  806. // (or whatever related .cgp file you're using). If they aren't, you're likely
  807. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  808. // set directly in the .cgp file to make things easier, but...they can't.
  809. // PASS SCALES AND RELATED CONSTANTS:
  810. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  811. // this shader: One does a viewport-scale bloom, and the other skips it. The
  812. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  813. static const float bloom_approx_size_x = 320.0;
  814. static const float bloom_approx_size_x_for_fake = 400.0;
  815. // Copy the viewport-relative scales of the phosphor mask resize passes
  816. // (MASK_RESIZE and the pass immediately preceding it):
  817. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  818. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  819. static const float geom_max_aspect_ratio = 4.0/3.0;
  820. // PHOSPHOR MASK TEXTURE CONSTANTS:
  821. // Set the following constants to reflect the properties of the phosphor mask
  822. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  823. // based on user settings, then repeats a single tile until filling the screen.
  824. // The shader must know the input texture size (default 64x64), and to manually
  825. // resize, it must also know the horizontal triads per tile (default 8).
  826. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  827. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  828. static const float mask_triads_per_tile = 8.0;
  829. // We need the average brightness of the phosphor mask to compensate for the
  830. // dimming it causes. The following four values are roughly correct for the
  831. // masks included with the shader. Update the value for any LUT texture you
  832. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  833. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  834. //#define PHOSPHOR_MASK_GRILLE14
  835. static const float mask_grille14_avg_color = 50.6666666/255.0;
  836. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  837. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  838. static const float mask_grille15_avg_color = 53.0/255.0;
  839. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  840. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  841. static const float mask_slot_avg_color = 46.0/255.0;
  842. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  843. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  844. static const float mask_shadow_avg_color = 41.0/255.0;
  845. // TileableLinearShadowMask*.png
  846. // TileableLinearShadowMaskEDP*.png
  847. #ifdef PHOSPHOR_MASK_GRILLE14
  848. static const float mask_grille_avg_color = mask_grille14_avg_color;
  849. #else
  850. static const float mask_grille_avg_color = mask_grille15_avg_color;
  851. #endif
  852. #endif // USER_CGP_CONSTANTS_H
  853. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  854. //////////////////////////////// END INCLUDES ////////////////////////////////
  855. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  856. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  857. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  858. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  859. #ifndef SIMULATE_CRT_ON_LCD
  860. #define SIMULATE_CRT_ON_LCD
  861. #endif
  862. // Manually tiling a manually resized texture creates texture coord derivative
  863. // discontinuities and confuses anisotropic filtering, causing discolored tile
  864. // seams in the phosphor mask. Workarounds:
  865. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  866. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  867. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  868. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  869. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  870. // (Same-pass curvature isn't used but could be in the future...maybe.)
  871. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  872. // border padding to the resized mask FBO, but it works with same-pass
  873. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  874. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  875. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  876. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  877. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  878. // Also, manually resampling the phosphor mask is slightly blurrier with
  879. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  880. // creates artifacts, but only with the fully bloomed shader.) The difference
  881. // is subtle with small triads, but you can fix it for a small cost.
  882. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  883. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  884. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  885. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  886. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  887. // #defined by either user-settings.h or a wrapper .cg that #includes the
  888. // current .cg pass.)
  889. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  890. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  891. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  892. #endif
  893. #ifdef RUNTIME_GEOMETRY_MODE
  894. #undef RUNTIME_GEOMETRY_MODE
  895. #endif
  896. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  897. // inferior in most cases, so replace 2.0 with 0.0:
  898. static const float bloom_approx_filter =
  899. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  900. #else
  901. static const float bloom_approx_filter = bloom_approx_filter_static;
  902. #endif
  903. // Disable slow runtime paths if static parameters are used. Most of these
  904. // won't be a problem anyway once the params are disabled, but some will.
  905. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  906. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  907. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  908. #endif
  909. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  910. #undef RUNTIME_ANTIALIAS_WEIGHTS
  911. #endif
  912. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  913. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  914. #endif
  915. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  916. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  917. #endif
  918. #ifdef RUNTIME_GEOMETRY_TILT
  919. #undef RUNTIME_GEOMETRY_TILT
  920. #endif
  921. #ifdef RUNTIME_GEOMETRY_MODE
  922. #undef RUNTIME_GEOMETRY_MODE
  923. #endif
  924. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  925. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  926. #endif
  927. #endif
  928. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  929. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  930. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  931. #endif
  932. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  933. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  934. #endif
  935. // Rule out unavailable anisotropic compatibility strategies:
  936. #ifndef DRIVERS_ALLOW_DERIVATIVES
  937. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  938. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  939. #endif
  940. #endif
  941. #ifndef DRIVERS_ALLOW_TEX2DLOD
  942. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  943. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  944. #endif
  945. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  946. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  947. #endif
  948. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  949. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  950. #endif
  951. #endif
  952. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  953. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  954. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  955. #endif
  956. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  957. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  958. #endif
  959. #endif
  960. // Prioritize anisotropic tiling compatibility strategies by performance and
  961. // disable unused strategies. This concentrates all the nesting in one place.
  962. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  963. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  964. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  965. #endif
  966. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  967. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  968. #endif
  969. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  970. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  971. #endif
  972. #else
  973. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  974. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  975. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  976. #endif
  977. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  978. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  979. #endif
  980. #else
  981. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  982. // flat texture coords in the same pass, but that's all we use.
  983. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  984. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  985. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  986. #endif
  987. #endif
  988. #endif
  989. #endif
  990. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  991. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  992. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  993. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  994. #endif
  995. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  996. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  997. #endif
  998. // Prioritize anisotropic resampling compatibility strategies the same way:
  999. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1000. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1001. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1002. #endif
  1003. #endif
  1004. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  1005. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  1006. // should: It gives us more options for using fewer samples.
  1007. #ifdef DRIVERS_ALLOW_TEX2DLOD
  1008. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1009. // TODO: Take advantage of this!
  1010. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  1011. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  1012. #else
  1013. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1014. #endif
  1015. #else
  1016. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1017. #endif
  1018. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  1019. // main_fragment, or a static alias of one of the above. This makes it hard
  1020. // to select the phosphor mask at runtime: We can't even assign to a uniform
  1021. // global in the vertex shader or select a sampler2D in the vertex shader and
  1022. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  1023. // because it just gives us the input texture or a black screen. However, we
  1024. // can get around these limitations by calling tex2D three times with different
  1025. // uniform samplers (or resizing the phosphor mask three times altogether).
  1026. // With dynamic branches, we can process only one of these branches on top of
  1027. // quickly discarding fragments we don't need (cgc seems able to overcome
  1028. // limigations around dependent texture fetches inside of branches). Without
  1029. // dynamic branches, we have to process every branch for every fragment...which
  1030. // is slower. Runtime sampling mode selection is slower without dynamic
  1031. // branches as well. Let the user's static #defines decide if it's worth it.
  1032. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1033. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1034. #else
  1035. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1036. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1037. #endif
  1038. #endif
  1039. // We need to render some minimum number of tiles in the resize passes.
  1040. // We need at least 1.0 just to repeat a single tile, and we need extra
  1041. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  1042. // antialiasing, same-pass curvature (not currently used), etc. First
  1043. // determine how many border texels and tiles we need, based on how the result
  1044. // will be sampled:
  1045. #ifdef GEOMETRY_EARLY
  1046. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  1047. // Most antialiasing filters have a base radius of 4.0 pixels:
  1048. static const float max_aa_base_pixel_border = 4.0 +
  1049. max_subpixel_offset;
  1050. #else
  1051. static const float max_aa_base_pixel_border = 0.0;
  1052. #endif
  1053. // Anisotropic filtering adds about 0.5 to the pixel border:
  1054. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1055. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  1056. #else
  1057. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  1058. #endif
  1059. // Fixing discontinuities adds 1.0 more to the pixel border:
  1060. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1061. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  1062. #else
  1063. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  1064. #endif
  1065. // Convert the pixel border to an integer texel border. Assume same-pass
  1066. // curvature about triples the texel frequency:
  1067. #ifdef GEOMETRY_EARLY
  1068. static const float max_mask_texel_border =
  1069. ceil(max_tiled_pixel_border * 3.0);
  1070. #else
  1071. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  1072. #endif
  1073. // Convert the texel border to a tile border using worst-case assumptions:
  1074. static const float max_mask_tile_border = max_mask_texel_border/
  1075. (mask_min_allowed_triad_size * mask_triads_per_tile);
  1076. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  1077. // the starting texel (inside borders) for sampling it.
  1078. #ifndef GEOMETRY_EARLY
  1079. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1080. // Special case: Render two tiles without borders. Anisotropic
  1081. // filtering doesn't seem to be a problem here.
  1082. static const float mask_resize_num_tiles = 1.0 + 1.0;
  1083. static const float mask_start_texels = 0.0;
  1084. #else
  1085. static const float mask_resize_num_tiles = 1.0 +
  1086. 2.0 * max_mask_tile_border;
  1087. static const float mask_start_texels = max_mask_texel_border;
  1088. #endif
  1089. #else
  1090. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  1091. static const float mask_start_texels = max_mask_texel_border;
  1092. #endif
  1093. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  1094. // mask_resize_viewport_scale. This limits the maximum final triad size.
  1095. // Estimate the minimum number of triads we can split the screen into in each
  1096. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  1097. static const float mask_resize_num_triads =
  1098. mask_resize_num_tiles * mask_triads_per_tile;
  1099. static const float2 min_allowed_viewport_triads =
  1100. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  1101. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  1102. static const float pi = 3.141592653589;
  1103. // We often want to find the location of the previous texel, e.g.:
  1104. // const float2 curr_texel = uv * texture_size;
  1105. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  1106. // const float2 prev_texel_uv = prev_texel / texture_size;
  1107. // However, many GPU drivers round incorrectly around exact texel locations.
  1108. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  1109. // require this value to be farther from 0.5 than others; define it here.
  1110. // const float2 prev_texel =
  1111. // floor(curr_texel - float2(under_half)) + float2(0.5);
  1112. static const float under_half = 0.4995;
  1113. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  1114. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  1115. //#include "bind-shader-params.h"
  1116. ///////////////////////////// BEGIN BIND-SHADER-PARAMS ////////////////////////////
  1117. #ifndef BIND_SHADER_PARAMS_H
  1118. #define BIND_SHADER_PARAMS_H
  1119. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1120. // crt-royale: A full-featured CRT shader, with cheese.
  1121. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1122. //
  1123. // This program is free software; you can redistribute it and/or modify it
  1124. // under the terms of the GNU General Public License as published by the Free
  1125. // Software Foundation; either version 2 of the License, or any later version.
  1126. //
  1127. // This program is distributed in the hope that it will be useful, but WITHOUT
  1128. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1129. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1130. // more details.
  1131. //
  1132. // You should have received a copy of the GNU General Public License along with
  1133. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1134. // Place, Suite 330, Boston, MA 02111-1307 USA
  1135. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  1136. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1137. //#include "../user-settings.h"
  1138. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1139. #ifndef USER_SETTINGS_H
  1140. #define USER_SETTINGS_H
  1141. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1142. // The Cg compiler uses different "profiles" with different capabilities.
  1143. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1144. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1145. // or driver capabilities, so instead you must comment or uncomment the lines
  1146. // below with "//" before "#define." Disable an option if you get compilation
  1147. // errors resembling those listed. Generally speaking, all of these options
  1148. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1149. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1150. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1151. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1152. // with curved manually tiled phosphor mask coords. Related errors:
  1153. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1154. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1155. //#define DRIVERS_ALLOW_DERIVATIVES
  1156. // Fine derivatives: Unsupported on older ATI cards.
  1157. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1158. // fast single-pass blur operations. If your card uses coarse derivatives and
  1159. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1160. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1161. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1162. #endif
  1163. // Dynamic looping: Requires an fp30 or newer profile.
  1164. // This makes phosphor mask resampling faster in some cases. Related errors:
  1165. // error C5013: profile does not support "for" statements and "for" could not
  1166. // be unrolled
  1167. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1168. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1169. // Using one static loop avoids overhead if the user is right, but if the user
  1170. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1171. // binary search can potentially save some iterations. However, it may fail:
  1172. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1173. // needed to compile program
  1174. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1175. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1176. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1177. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1178. // this profile
  1179. //#define DRIVERS_ALLOW_TEX2DLOD
  1180. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1181. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1182. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1183. // in this profile
  1184. //#define DRIVERS_ALLOW_TEX2DBIAS
  1185. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1186. // impose stricter limitations on register counts and instructions. Enable
  1187. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1188. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1189. // to compile program.
  1190. // Enabling integrated graphics compatibility mode will automatically disable:
  1191. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1192. // (This may be reenabled in a later release.)
  1193. // 2.) RUNTIME_GEOMETRY_MODE
  1194. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1195. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1196. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1197. // To disable a #define option, turn its line into a comment with "//."
  1198. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1199. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1200. // many of the options in this file and allow real-time tuning, but many of
  1201. // them are slower. Disabling them and using this text file will boost FPS.
  1202. #define RUNTIME_SHADER_PARAMS_ENABLE
  1203. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1204. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1205. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1206. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1207. #define RUNTIME_ANTIALIAS_WEIGHTS
  1208. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1209. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1210. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1211. // parameters? This will require more math or dynamic branching.
  1212. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1213. // Specify the tilt at runtime? This makes things about 3% slower.
  1214. #define RUNTIME_GEOMETRY_TILT
  1215. // Specify the geometry mode at runtime?
  1216. #define RUNTIME_GEOMETRY_MODE
  1217. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1218. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1219. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1220. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1221. // PHOSPHOR MASK:
  1222. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1223. // removes the option to do so, but it may be faster without dynamic branches.
  1224. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1225. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1226. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1227. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1228. // detect the right blur if the triad size is static or our profile allows
  1229. // dynamic branches, but otherwise we use the largest blur the user indicates
  1230. // they might need:
  1231. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1232. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1233. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1234. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1235. // Here's a helpful chart:
  1236. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1237. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1238. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1239. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1240. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1241. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1242. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1243. // Note: Many of these static parameters are overridden by runtime shader
  1244. // parameters when those are enabled. However, many others are static codepath
  1245. // options that were cleaner or more convert to code as static constants.
  1246. // GAMMA:
  1247. static const float crt_gamma_static = 2.5; // range [1, 5]
  1248. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1249. // LEVELS MANAGEMENT:
  1250. // Control the final multiplicative image contrast:
  1251. static const float levels_contrast_static = 1.0; // range [0, 4)
  1252. // We auto-dim to avoid clipping between passes and restore brightness
  1253. // later. Control the dim factor here: Lower values clip less but crush
  1254. // blacks more (static only for now).
  1255. 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
  1256. // HALATION/DIFFUSION/BLOOM:
  1257. // Halation weight: How much energy should be lost to electrons bounding
  1258. // around under the CRT glass and exciting random phosphors?
  1259. static const float halation_weight_static = 0.0; // range [0, 1]
  1260. // Refractive diffusion weight: How much light should spread/diffuse from
  1261. // refracting through the CRT glass?
  1262. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1263. // Underestimate brightness: Bright areas bloom more, but we can base the
  1264. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1265. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1266. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1267. // Blur all colors more than necessary for a softer phosphor bloom?
  1268. static const float bloom_excess_static = 0.0; // range [0, 1]
  1269. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1270. // blurred resize of the input (convergence offsets are applied as well).
  1271. // There are three filter options (static option only for now):
  1272. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1273. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1274. // and beam_max_sigma is low.
  1275. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1276. // always uses a static sigma regardless of beam_max_sigma or
  1277. // mask_num_triads_desired.
  1278. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1279. // These options are more pronounced for the fast, unbloomed shader version.
  1280. #ifndef RADEON_FIX
  1281. static const float bloom_approx_filter_static = 2.0;
  1282. #else
  1283. static const float bloom_approx_filter_static = 1.0;
  1284. #endif
  1285. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1286. // How many scanlines should contribute light to each pixel? Using more
  1287. // scanlines is slower (especially for a generalized Gaussian) but less
  1288. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1289. // max_beam_sigma at which the closest unused weight is guaranteed <
  1290. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1291. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1292. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1293. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1294. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1295. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1296. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1297. // A generalized Gaussian beam varies shape with color too, now just width.
  1298. // It's slower but more flexible (static option only for now).
  1299. static const bool beam_generalized_gaussian = true;
  1300. // What kind of scanline antialiasing do you want?
  1301. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1302. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1303. // better than 3x antialiasing (static option only for now).
  1304. static const float beam_antialias_level = 1.0; // range [0, 2]
  1305. // Min/max standard deviations for scanline beams: Higher values widen and
  1306. // soften scanlines. Depending on other options, low min sigmas can alias.
  1307. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1308. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1309. // Beam width varies as a function of color: A power function (0) is more
  1310. // configurable, but a spherical function (1) gives the widest beam
  1311. // variability without aliasing (static option only for now).
  1312. static const float beam_spot_shape_function = 0.0;
  1313. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1314. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1315. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1316. // Generalized Gaussian max shape parameters: Higher values give flatter
  1317. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1318. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1319. // values > ~40.0 cause artifacts with integrals.
  1320. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1321. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1322. // Generalized Gaussian shape power: Affects how quickly the distribution
  1323. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1324. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1325. // appear sharper for most colors.
  1326. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1327. // What filter should be used to sample scanlines horizontally?
  1328. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1329. static const float beam_horiz_filter_static = 0.0;
  1330. // Standard deviation for horizontal Gaussian resampling:
  1331. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1332. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1333. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1334. // limiting circuitry in some CRT's), or a weighted avg.?
  1335. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1336. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1337. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1338. // later passes (static option only for now).
  1339. static const bool beam_misconvergence = true;
  1340. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1341. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1342. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1343. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1344. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1345. // Detect interlacing (static option only for now)?
  1346. static const bool interlace_detect = true;
  1347. // Assume 1080-line sources are interlaced?
  1348. static const bool interlace_1080i_static = false;
  1349. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1350. // (Whether this matters depends on the nature of the interlaced input.)
  1351. static const bool interlace_bff_static = false;
  1352. // ANTIALIASING:
  1353. // What AA level do you want for curvature/overscan/subpixels? Options:
  1354. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1355. // (Static option only for now)
  1356. static const float aa_level = 12.0; // range [0, 24]
  1357. // What antialiasing filter do you want (static option only)? Options:
  1358. // 0: Box (separable), 1: Box (cylindrical),
  1359. // 2: Tent (separable), 3: Tent (cylindrical),
  1360. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1361. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1362. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1363. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1364. static const float aa_filter = 6.0; // range [0, 9]
  1365. // Flip the sample grid on odd/even frames (static option only for now)?
  1366. static const bool aa_temporal = false;
  1367. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1368. // the blue offset is the negative r offset; range [0, 0.5]
  1369. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1370. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1371. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1372. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1373. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1374. // 4.) C = 0.0 is a soft spline filter.
  1375. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1376. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1377. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1378. // PHOSPHOR MASK:
  1379. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1380. static const float mask_type_static = 1.0; // range [0, 2]
  1381. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1382. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1383. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1384. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1385. // is halfway decent with LUT mipmapping but atrocious without it.
  1386. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1387. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1388. // This mode reuses the same masks, so triads will be enormous unless
  1389. // you change the mask LUT filenames in your .cgp file.
  1390. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1391. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1392. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1393. // will always be used to calculate the full bloom sigma statically.
  1394. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1395. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1396. // triads) will be rounded to the nearest integer tile size and clamped to
  1397. // obey minimum size constraints (imposed to reduce downsize taps) and
  1398. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1399. // To increase the size limit, double the viewport-relative scales for the
  1400. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1401. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1402. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1403. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1404. // final size will be rounded and constrained as above); default 480.0
  1405. static const float mask_num_triads_desired_static = 480.0;
  1406. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1407. // more samples and avoid moire a bit better, but some is unavoidable
  1408. // depending on the destination size (static option for now).
  1409. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1410. // The mask is resized using a variable number of taps in each dimension,
  1411. // but some Cg profiles always fetch a constant number of taps no matter
  1412. // what (no dynamic branching). We can limit the maximum number of taps if
  1413. // we statically limit the minimum phosphor triad size. Larger values are
  1414. // faster, but the limit IS enforced (static option only, forever);
  1415. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1416. // TODO: Make this 1.0 and compensate with smarter sampling!
  1417. static const float mask_min_allowed_triad_size = 2.0;
  1418. // GEOMETRY:
  1419. // Geometry mode:
  1420. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1421. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1422. static const float geom_mode_static = 0.0; // range [0, 3]
  1423. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1424. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1425. // View dist is the distance from the player to their physical screen, in
  1426. // units of the viewport's diagonal size. It controls the field of view.
  1427. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1428. // Tilt angle in radians (clockwise around up and right vectors):
  1429. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1430. // Aspect ratio: When the true viewport size is unknown, this value is used
  1431. // to help convert between the phosphor triad size and count, along with
  1432. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1433. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1434. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1435. // default (256/224)*(54/47) = 1.313069909 (see below)
  1436. static const float geom_aspect_ratio_static = 1.313069909;
  1437. // Before getting into overscan, here's some general aspect ratio info:
  1438. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1439. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1440. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1441. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1442. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1443. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1444. // a.) Enable Retroarch's "Crop Overscan"
  1445. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1446. // Real consoles use horizontal black padding in the signal, but emulators
  1447. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1448. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1449. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1450. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1451. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1452. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1453. // without doing a. or b., but horizontal image borders will be tighter
  1454. // than vertical ones, messing up curvature and overscan. Fixing the
  1455. // padding first corrects this.
  1456. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1457. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1458. // above: Values < 1.0 zoom out; range (0, inf)
  1459. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1460. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1461. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1462. // with strong curvature (static option only for now).
  1463. static const bool geom_force_correct_tangent_matrix = true;
  1464. // BORDERS:
  1465. // Rounded border size in texture uv coords:
  1466. static const float border_size_static = 0.015; // range [0, 0.5]
  1467. // Border darkness: Moderate values darken the border smoothly, and high
  1468. // values make the image very dark just inside the border:
  1469. static const float border_darkness_static = 2.0; // range [0, inf)
  1470. // Border compression: High numbers compress border transitions, narrowing
  1471. // the dark border area.
  1472. static const float border_compress_static = 2.5; // range [1, inf)
  1473. #endif // USER_SETTINGS_H
  1474. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1475. //#include "derived-settings-and-constants.h"
  1476. ///////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  1477. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  1478. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  1479. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1480. // crt-royale: A full-featured CRT shader, with cheese.
  1481. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1482. //
  1483. // This program is free software; you can redistribute it and/or modify it
  1484. // under the terms of the GNU General Public License as published by the Free
  1485. // Software Foundation; either version 2 of the License, or any later version.
  1486. //
  1487. // This program is distributed in the hope that it will be useful, but WITHOUT
  1488. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1489. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1490. // more details.
  1491. //
  1492. // You should have received a copy of the GNU General Public License along with
  1493. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1494. // Place, Suite 330, Boston, MA 02111-1307 USA
  1495. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  1496. // These macros and constants can be used across the whole codebase.
  1497. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  1498. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1499. //#include "../user-settings.h"
  1500. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1501. #ifndef USER_SETTINGS_H
  1502. #define USER_SETTINGS_H
  1503. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1504. // The Cg compiler uses different "profiles" with different capabilities.
  1505. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1506. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1507. // or driver capabilities, so instead you must comment or uncomment the lines
  1508. // below with "//" before "#define." Disable an option if you get compilation
  1509. // errors resembling those listed. Generally speaking, all of these options
  1510. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1511. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1512. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1513. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1514. // with curved manually tiled phosphor mask coords. Related errors:
  1515. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1516. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1517. //#define DRIVERS_ALLOW_DERIVATIVES
  1518. // Fine derivatives: Unsupported on older ATI cards.
  1519. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1520. // fast single-pass blur operations. If your card uses coarse derivatives and
  1521. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1522. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1523. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1524. #endif
  1525. // Dynamic looping: Requires an fp30 or newer profile.
  1526. // This makes phosphor mask resampling faster in some cases. Related errors:
  1527. // error C5013: profile does not support "for" statements and "for" could not
  1528. // be unrolled
  1529. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1530. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1531. // Using one static loop avoids overhead if the user is right, but if the user
  1532. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1533. // binary search can potentially save some iterations. However, it may fail:
  1534. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1535. // needed to compile program
  1536. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1537. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1538. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1539. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1540. // this profile
  1541. //#define DRIVERS_ALLOW_TEX2DLOD
  1542. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1543. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1544. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1545. // in this profile
  1546. //#define DRIVERS_ALLOW_TEX2DBIAS
  1547. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1548. // impose stricter limitations on register counts and instructions. Enable
  1549. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1550. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1551. // to compile program.
  1552. // Enabling integrated graphics compatibility mode will automatically disable:
  1553. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1554. // (This may be reenabled in a later release.)
  1555. // 2.) RUNTIME_GEOMETRY_MODE
  1556. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1557. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1558. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1559. // To disable a #define option, turn its line into a comment with "//."
  1560. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1561. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1562. // many of the options in this file and allow real-time tuning, but many of
  1563. // them are slower. Disabling them and using this text file will boost FPS.
  1564. #define RUNTIME_SHADER_PARAMS_ENABLE
  1565. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1566. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1567. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1568. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1569. #define RUNTIME_ANTIALIAS_WEIGHTS
  1570. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1571. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1572. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1573. // parameters? This will require more math or dynamic branching.
  1574. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1575. // Specify the tilt at runtime? This makes things about 3% slower.
  1576. #define RUNTIME_GEOMETRY_TILT
  1577. // Specify the geometry mode at runtime?
  1578. #define RUNTIME_GEOMETRY_MODE
  1579. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1580. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1581. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1582. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1583. // PHOSPHOR MASK:
  1584. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1585. // removes the option to do so, but it may be faster without dynamic branches.
  1586. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1587. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1588. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1589. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1590. // detect the right blur if the triad size is static or our profile allows
  1591. // dynamic branches, but otherwise we use the largest blur the user indicates
  1592. // they might need:
  1593. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1594. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1595. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1596. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1597. // Here's a helpful chart:
  1598. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1599. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1600. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1601. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1602. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1603. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1604. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1605. // Note: Many of these static parameters are overridden by runtime shader
  1606. // parameters when those are enabled. However, many others are static codepath
  1607. // options that were cleaner or more convert to code as static constants.
  1608. // GAMMA:
  1609. static const float crt_gamma_static = 2.5; // range [1, 5]
  1610. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1611. // LEVELS MANAGEMENT:
  1612. // Control the final multiplicative image contrast:
  1613. static const float levels_contrast_static = 1.0; // range [0, 4)
  1614. // We auto-dim to avoid clipping between passes and restore brightness
  1615. // later. Control the dim factor here: Lower values clip less but crush
  1616. // blacks more (static only for now).
  1617. 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
  1618. // HALATION/DIFFUSION/BLOOM:
  1619. // Halation weight: How much energy should be lost to electrons bounding
  1620. // around under the CRT glass and exciting random phosphors?
  1621. static const float halation_weight_static = 0.0; // range [0, 1]
  1622. // Refractive diffusion weight: How much light should spread/diffuse from
  1623. // refracting through the CRT glass?
  1624. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1625. // Underestimate brightness: Bright areas bloom more, but we can base the
  1626. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1627. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1628. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1629. // Blur all colors more than necessary for a softer phosphor bloom?
  1630. static const float bloom_excess_static = 0.0; // range [0, 1]
  1631. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1632. // blurred resize of the input (convergence offsets are applied as well).
  1633. // There are three filter options (static option only for now):
  1634. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1635. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1636. // and beam_max_sigma is low.
  1637. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1638. // always uses a static sigma regardless of beam_max_sigma or
  1639. // mask_num_triads_desired.
  1640. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1641. // These options are more pronounced for the fast, unbloomed shader version.
  1642. #ifndef RADEON_FIX
  1643. static const float bloom_approx_filter_static = 2.0;
  1644. #else
  1645. static const float bloom_approx_filter_static = 1.0;
  1646. #endif
  1647. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1648. // How many scanlines should contribute light to each pixel? Using more
  1649. // scanlines is slower (especially for a generalized Gaussian) but less
  1650. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1651. // max_beam_sigma at which the closest unused weight is guaranteed <
  1652. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1653. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1654. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1655. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1656. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1657. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1658. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1659. // A generalized Gaussian beam varies shape with color too, now just width.
  1660. // It's slower but more flexible (static option only for now).
  1661. static const bool beam_generalized_gaussian = true;
  1662. // What kind of scanline antialiasing do you want?
  1663. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1664. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1665. // better than 3x antialiasing (static option only for now).
  1666. static const float beam_antialias_level = 1.0; // range [0, 2]
  1667. // Min/max standard deviations for scanline beams: Higher values widen and
  1668. // soften scanlines. Depending on other options, low min sigmas can alias.
  1669. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1670. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1671. // Beam width varies as a function of color: A power function (0) is more
  1672. // configurable, but a spherical function (1) gives the widest beam
  1673. // variability without aliasing (static option only for now).
  1674. static const float beam_spot_shape_function = 0.0;
  1675. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1676. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1677. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1678. // Generalized Gaussian max shape parameters: Higher values give flatter
  1679. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1680. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1681. // values > ~40.0 cause artifacts with integrals.
  1682. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1683. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1684. // Generalized Gaussian shape power: Affects how quickly the distribution
  1685. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1686. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1687. // appear sharper for most colors.
  1688. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1689. // What filter should be used to sample scanlines horizontally?
  1690. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1691. static const float beam_horiz_filter_static = 0.0;
  1692. // Standard deviation for horizontal Gaussian resampling:
  1693. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1694. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1695. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1696. // limiting circuitry in some CRT's), or a weighted avg.?
  1697. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1698. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1699. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1700. // later passes (static option only for now).
  1701. static const bool beam_misconvergence = true;
  1702. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1703. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1704. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1705. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1706. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1707. // Detect interlacing (static option only for now)?
  1708. static const bool interlace_detect = true;
  1709. // Assume 1080-line sources are interlaced?
  1710. static const bool interlace_1080i_static = false;
  1711. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1712. // (Whether this matters depends on the nature of the interlaced input.)
  1713. static const bool interlace_bff_static = false;
  1714. // ANTIALIASING:
  1715. // What AA level do you want for curvature/overscan/subpixels? Options:
  1716. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1717. // (Static option only for now)
  1718. static const float aa_level = 12.0; // range [0, 24]
  1719. // What antialiasing filter do you want (static option only)? Options:
  1720. // 0: Box (separable), 1: Box (cylindrical),
  1721. // 2: Tent (separable), 3: Tent (cylindrical),
  1722. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1723. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1724. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1725. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1726. static const float aa_filter = 6.0; // range [0, 9]
  1727. // Flip the sample grid on odd/even frames (static option only for now)?
  1728. static const bool aa_temporal = false;
  1729. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1730. // the blue offset is the negative r offset; range [0, 0.5]
  1731. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1732. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1733. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1734. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1735. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1736. // 4.) C = 0.0 is a soft spline filter.
  1737. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1738. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1739. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1740. // PHOSPHOR MASK:
  1741. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1742. static const float mask_type_static = 1.0; // range [0, 2]
  1743. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1744. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1745. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1746. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1747. // is halfway decent with LUT mipmapping but atrocious without it.
  1748. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1749. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1750. // This mode reuses the same masks, so triads will be enormous unless
  1751. // you change the mask LUT filenames in your .cgp file.
  1752. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1753. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1754. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1755. // will always be used to calculate the full bloom sigma statically.
  1756. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1757. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1758. // triads) will be rounded to the nearest integer tile size and clamped to
  1759. // obey minimum size constraints (imposed to reduce downsize taps) and
  1760. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1761. // To increase the size limit, double the viewport-relative scales for the
  1762. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1763. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1764. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1765. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1766. // final size will be rounded and constrained as above); default 480.0
  1767. static const float mask_num_triads_desired_static = 480.0;
  1768. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1769. // more samples and avoid moire a bit better, but some is unavoidable
  1770. // depending on the destination size (static option for now).
  1771. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1772. // The mask is resized using a variable number of taps in each dimension,
  1773. // but some Cg profiles always fetch a constant number of taps no matter
  1774. // what (no dynamic branching). We can limit the maximum number of taps if
  1775. // we statically limit the minimum phosphor triad size. Larger values are
  1776. // faster, but the limit IS enforced (static option only, forever);
  1777. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1778. // TODO: Make this 1.0 and compensate with smarter sampling!
  1779. static const float mask_min_allowed_triad_size = 2.0;
  1780. // GEOMETRY:
  1781. // Geometry mode:
  1782. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1783. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1784. static const float geom_mode_static = 0.0; // range [0, 3]
  1785. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1786. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1787. // View dist is the distance from the player to their physical screen, in
  1788. // units of the viewport's diagonal size. It controls the field of view.
  1789. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1790. // Tilt angle in radians (clockwise around up and right vectors):
  1791. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1792. // Aspect ratio: When the true viewport size is unknown, this value is used
  1793. // to help convert between the phosphor triad size and count, along with
  1794. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1795. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1796. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1797. // default (256/224)*(54/47) = 1.313069909 (see below)
  1798. static const float geom_aspect_ratio_static = 1.313069909;
  1799. // Before getting into overscan, here's some general aspect ratio info:
  1800. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1801. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1802. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1803. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1804. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1805. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1806. // a.) Enable Retroarch's "Crop Overscan"
  1807. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1808. // Real consoles use horizontal black padding in the signal, but emulators
  1809. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1810. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1811. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1812. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1813. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1814. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1815. // without doing a. or b., but horizontal image borders will be tighter
  1816. // than vertical ones, messing up curvature and overscan. Fixing the
  1817. // padding first corrects this.
  1818. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1819. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1820. // above: Values < 1.0 zoom out; range (0, inf)
  1821. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1822. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1823. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1824. // with strong curvature (static option only for now).
  1825. static const bool geom_force_correct_tangent_matrix = true;
  1826. // BORDERS:
  1827. // Rounded border size in texture uv coords:
  1828. static const float border_size_static = 0.015; // range [0, 0.5]
  1829. // Border darkness: Moderate values darken the border smoothly, and high
  1830. // values make the image very dark just inside the border:
  1831. static const float border_darkness_static = 2.0; // range [0, inf)
  1832. // Border compression: High numbers compress border transitions, narrowing
  1833. // the dark border area.
  1834. static const float border_compress_static = 2.5; // range [1, inf)
  1835. #endif // USER_SETTINGS_H
  1836. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1837. //#include "user-cgp-constants.h"
  1838. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  1839. #ifndef USER_CGP_CONSTANTS_H
  1840. #define USER_CGP_CONSTANTS_H
  1841. // IMPORTANT:
  1842. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  1843. // (or whatever related .cgp file you're using). If they aren't, you're likely
  1844. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  1845. // set directly in the .cgp file to make things easier, but...they can't.
  1846. // PASS SCALES AND RELATED CONSTANTS:
  1847. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  1848. // this shader: One does a viewport-scale bloom, and the other skips it. The
  1849. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  1850. static const float bloom_approx_size_x = 320.0;
  1851. static const float bloom_approx_size_x_for_fake = 400.0;
  1852. // Copy the viewport-relative scales of the phosphor mask resize passes
  1853. // (MASK_RESIZE and the pass immediately preceding it):
  1854. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  1855. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  1856. static const float geom_max_aspect_ratio = 4.0/3.0;
  1857. // PHOSPHOR MASK TEXTURE CONSTANTS:
  1858. // Set the following constants to reflect the properties of the phosphor mask
  1859. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  1860. // based on user settings, then repeats a single tile until filling the screen.
  1861. // The shader must know the input texture size (default 64x64), and to manually
  1862. // resize, it must also know the horizontal triads per tile (default 8).
  1863. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  1864. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  1865. static const float mask_triads_per_tile = 8.0;
  1866. // We need the average brightness of the phosphor mask to compensate for the
  1867. // dimming it causes. The following four values are roughly correct for the
  1868. // masks included with the shader. Update the value for any LUT texture you
  1869. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  1870. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  1871. //#define PHOSPHOR_MASK_GRILLE14
  1872. static const float mask_grille14_avg_color = 50.6666666/255.0;
  1873. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  1874. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  1875. static const float mask_grille15_avg_color = 53.0/255.0;
  1876. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  1877. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  1878. static const float mask_slot_avg_color = 46.0/255.0;
  1879. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  1880. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  1881. static const float mask_shadow_avg_color = 41.0/255.0;
  1882. // TileableLinearShadowMask*.png
  1883. // TileableLinearShadowMaskEDP*.png
  1884. #ifdef PHOSPHOR_MASK_GRILLE14
  1885. static const float mask_grille_avg_color = mask_grille14_avg_color;
  1886. #else
  1887. static const float mask_grille_avg_color = mask_grille15_avg_color;
  1888. #endif
  1889. #endif // USER_CGP_CONSTANTS_H
  1890. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  1891. //////////////////////////////// END INCLUDES ////////////////////////////////
  1892. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  1893. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  1894. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  1895. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  1896. #ifndef SIMULATE_CRT_ON_LCD
  1897. #define SIMULATE_CRT_ON_LCD
  1898. #endif
  1899. // Manually tiling a manually resized texture creates texture coord derivative
  1900. // discontinuities and confuses anisotropic filtering, causing discolored tile
  1901. // seams in the phosphor mask. Workarounds:
  1902. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  1903. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  1904. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  1905. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  1906. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  1907. // (Same-pass curvature isn't used but could be in the future...maybe.)
  1908. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  1909. // border padding to the resized mask FBO, but it works with same-pass
  1910. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  1911. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  1912. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  1913. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  1914. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  1915. // Also, manually resampling the phosphor mask is slightly blurrier with
  1916. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  1917. // creates artifacts, but only with the fully bloomed shader.) The difference
  1918. // is subtle with small triads, but you can fix it for a small cost.
  1919. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1920. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  1921. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  1922. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  1923. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  1924. // #defined by either user-settings.h or a wrapper .cg that #includes the
  1925. // current .cg pass.)
  1926. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1927. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  1928. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  1929. #endif
  1930. #ifdef RUNTIME_GEOMETRY_MODE
  1931. #undef RUNTIME_GEOMETRY_MODE
  1932. #endif
  1933. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  1934. // inferior in most cases, so replace 2.0 with 0.0:
  1935. static const float bloom_approx_filter =
  1936. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  1937. #else
  1938. static const float bloom_approx_filter = bloom_approx_filter_static;
  1939. #endif
  1940. // Disable slow runtime paths if static parameters are used. Most of these
  1941. // won't be a problem anyway once the params are disabled, but some will.
  1942. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  1943. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1944. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1945. #endif
  1946. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  1947. #undef RUNTIME_ANTIALIAS_WEIGHTS
  1948. #endif
  1949. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1950. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1951. #endif
  1952. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1953. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1954. #endif
  1955. #ifdef RUNTIME_GEOMETRY_TILT
  1956. #undef RUNTIME_GEOMETRY_TILT
  1957. #endif
  1958. #ifdef RUNTIME_GEOMETRY_MODE
  1959. #undef RUNTIME_GEOMETRY_MODE
  1960. #endif
  1961. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1962. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1963. #endif
  1964. #endif
  1965. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  1966. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1967. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1968. #endif
  1969. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1970. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1971. #endif
  1972. // Rule out unavailable anisotropic compatibility strategies:
  1973. #ifndef DRIVERS_ALLOW_DERIVATIVES
  1974. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1975. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1976. #endif
  1977. #endif
  1978. #ifndef DRIVERS_ALLOW_TEX2DLOD
  1979. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1980. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1981. #endif
  1982. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1983. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1984. #endif
  1985. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  1986. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  1987. #endif
  1988. #endif
  1989. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  1990. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1991. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1992. #endif
  1993. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1994. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1995. #endif
  1996. #endif
  1997. // Prioritize anisotropic tiling compatibility strategies by performance and
  1998. // disable unused strategies. This concentrates all the nesting in one place.
  1999. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2000. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2001. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2002. #endif
  2003. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2004. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2005. #endif
  2006. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2007. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2008. #endif
  2009. #else
  2010. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2011. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2012. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2013. #endif
  2014. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2015. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2016. #endif
  2017. #else
  2018. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  2019. // flat texture coords in the same pass, but that's all we use.
  2020. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2021. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2022. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2023. #endif
  2024. #endif
  2025. #endif
  2026. #endif
  2027. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  2028. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  2029. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2030. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2031. #endif
  2032. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2033. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2034. #endif
  2035. // Prioritize anisotropic resampling compatibility strategies the same way:
  2036. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2037. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2038. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2039. #endif
  2040. #endif
  2041. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  2042. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  2043. // should: It gives us more options for using fewer samples.
  2044. #ifdef DRIVERS_ALLOW_TEX2DLOD
  2045. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2046. // TODO: Take advantage of this!
  2047. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  2048. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  2049. #else
  2050. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2051. #endif
  2052. #else
  2053. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2054. #endif
  2055. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  2056. // main_fragment, or a static alias of one of the above. This makes it hard
  2057. // to select the phosphor mask at runtime: We can't even assign to a uniform
  2058. // global in the vertex shader or select a sampler2D in the vertex shader and
  2059. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  2060. // because it just gives us the input texture or a black screen. However, we
  2061. // can get around these limitations by calling tex2D three times with different
  2062. // uniform samplers (or resizing the phosphor mask three times altogether).
  2063. // With dynamic branches, we can process only one of these branches on top of
  2064. // quickly discarding fragments we don't need (cgc seems able to overcome
  2065. // limigations around dependent texture fetches inside of branches). Without
  2066. // dynamic branches, we have to process every branch for every fragment...which
  2067. // is slower. Runtime sampling mode selection is slower without dynamic
  2068. // branches as well. Let the user's static #defines decide if it's worth it.
  2069. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2070. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2071. #else
  2072. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2073. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2074. #endif
  2075. #endif
  2076. // We need to render some minimum number of tiles in the resize passes.
  2077. // We need at least 1.0 just to repeat a single tile, and we need extra
  2078. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  2079. // antialiasing, same-pass curvature (not currently used), etc. First
  2080. // determine how many border texels and tiles we need, based on how the result
  2081. // will be sampled:
  2082. #ifdef GEOMETRY_EARLY
  2083. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  2084. // Most antialiasing filters have a base radius of 4.0 pixels:
  2085. static const float max_aa_base_pixel_border = 4.0 +
  2086. max_subpixel_offset;
  2087. #else
  2088. static const float max_aa_base_pixel_border = 0.0;
  2089. #endif
  2090. // Anisotropic filtering adds about 0.5 to the pixel border:
  2091. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2092. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  2093. #else
  2094. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  2095. #endif
  2096. // Fixing discontinuities adds 1.0 more to the pixel border:
  2097. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2098. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  2099. #else
  2100. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  2101. #endif
  2102. // Convert the pixel border to an integer texel border. Assume same-pass
  2103. // curvature about triples the texel frequency:
  2104. #ifdef GEOMETRY_EARLY
  2105. static const float max_mask_texel_border =
  2106. ceil(max_tiled_pixel_border * 3.0);
  2107. #else
  2108. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  2109. #endif
  2110. // Convert the texel border to a tile border using worst-case assumptions:
  2111. static const float max_mask_tile_border = max_mask_texel_border/
  2112. (mask_min_allowed_triad_size * mask_triads_per_tile);
  2113. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  2114. // the starting texel (inside borders) for sampling it.
  2115. #ifndef GEOMETRY_EARLY
  2116. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2117. // Special case: Render two tiles without borders. Anisotropic
  2118. // filtering doesn't seem to be a problem here.
  2119. static const float mask_resize_num_tiles = 1.0 + 1.0;
  2120. static const float mask_start_texels = 0.0;
  2121. #else
  2122. static const float mask_resize_num_tiles = 1.0 +
  2123. 2.0 * max_mask_tile_border;
  2124. static const float mask_start_texels = max_mask_texel_border;
  2125. #endif
  2126. #else
  2127. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  2128. static const float mask_start_texels = max_mask_texel_border;
  2129. #endif
  2130. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  2131. // mask_resize_viewport_scale. This limits the maximum final triad size.
  2132. // Estimate the minimum number of triads we can split the screen into in each
  2133. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  2134. static const float mask_resize_num_triads =
  2135. mask_resize_num_tiles * mask_triads_per_tile;
  2136. static const float2 min_allowed_viewport_triads =
  2137. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  2138. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  2139. static const float pi = 3.141592653589;
  2140. // We often want to find the location of the previous texel, e.g.:
  2141. // const float2 curr_texel = uv * texture_size;
  2142. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  2143. // const float2 prev_texel_uv = prev_texel / texture_size;
  2144. // However, many GPU drivers round incorrectly around exact texel locations.
  2145. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  2146. // require this value to be farther from 0.5 than others; define it here.
  2147. // const float2 prev_texel =
  2148. // floor(curr_texel - float2(under_half)) + float2(0.5);
  2149. static const float under_half = 0.4995;
  2150. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  2151. //////////////////// END DERIVED-SETTINGS-AND-CONSTANTS /////////////////////
  2152. //////////////////////////////// END INCLUDES ////////////////////////////////
  2153. // Override some parameters for gamma-management.h and tex2Dantialias.h:
  2154. #define OVERRIDE_DEVICE_GAMMA
  2155. static const float gba_gamma = 3.5; // Irrelevant but necessary to define.
  2156. #define ANTIALIAS_OVERRIDE_BASICS
  2157. #define ANTIALIAS_OVERRIDE_PARAMETERS
  2158. // Provide accessors for vector constants that pack scalar uniforms:
  2159. inline float2 get_aspect_vector(const float geom_aspect_ratio)
  2160. {
  2161. // Get an aspect ratio vector. Enforce geom_max_aspect_ratio, and prevent
  2162. // the absolute scale from affecting the uv-mapping for curvature:
  2163. const float geom_clamped_aspect_ratio =
  2164. min(geom_aspect_ratio, geom_max_aspect_ratio);
  2165. const float2 geom_aspect =
  2166. normalize(float2(geom_clamped_aspect_ratio, 1.0));
  2167. return geom_aspect;
  2168. }
  2169. inline float2 get_geom_overscan_vector()
  2170. {
  2171. return float2(geom_overscan_x, geom_overscan_y);
  2172. }
  2173. inline float2 get_geom_tilt_angle_vector()
  2174. {
  2175. return float2(geom_tilt_angle_x, geom_tilt_angle_y);
  2176. }
  2177. inline float3 get_convergence_offsets_x_vector()
  2178. {
  2179. return float3(convergence_offset_x_r, convergence_offset_x_g,
  2180. convergence_offset_x_b);
  2181. }
  2182. inline float3 get_convergence_offsets_y_vector()
  2183. {
  2184. return float3(convergence_offset_y_r, convergence_offset_y_g,
  2185. convergence_offset_y_b);
  2186. }
  2187. inline float2 get_convergence_offsets_r_vector()
  2188. {
  2189. return float2(convergence_offset_x_r, convergence_offset_y_r);
  2190. }
  2191. inline float2 get_convergence_offsets_g_vector()
  2192. {
  2193. return float2(convergence_offset_x_g, convergence_offset_y_g);
  2194. }
  2195. inline float2 get_convergence_offsets_b_vector()
  2196. {
  2197. return float2(convergence_offset_x_b, convergence_offset_y_b);
  2198. }
  2199. inline float2 get_aa_subpixel_r_offset()
  2200. {
  2201. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  2202. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2203. // WARNING: THIS IS EXTREMELY EXPENSIVE.
  2204. return float2(aa_subpixel_r_offset_x_runtime,
  2205. aa_subpixel_r_offset_y_runtime);
  2206. #else
  2207. return aa_subpixel_r_offset_static;
  2208. #endif
  2209. #else
  2210. return aa_subpixel_r_offset_static;
  2211. #endif
  2212. }
  2213. // Provide accessors settings which still need "cooking:"
  2214. inline float get_mask_amplify()
  2215. {
  2216. static const float mask_grille_amplify = 1.0/mask_grille_avg_color;
  2217. static const float mask_slot_amplify = 1.0/mask_slot_avg_color;
  2218. static const float mask_shadow_amplify = 1.0/mask_shadow_avg_color;
  2219. return mask_type < 0.5 ? mask_grille_amplify :
  2220. mask_type < 1.5 ? mask_slot_amplify :
  2221. mask_shadow_amplify;
  2222. }
  2223. inline float get_mask_sample_mode()
  2224. {
  2225. #ifdef RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2226. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2227. return mask_sample_mode_desired;
  2228. #else
  2229. return clamp(mask_sample_mode_desired, 1.0, 2.0);
  2230. #endif
  2231. #else
  2232. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2233. return mask_sample_mode_static;
  2234. #else
  2235. return clamp(mask_sample_mode_static, 1.0, 2.0);
  2236. #endif
  2237. #endif
  2238. }
  2239. #endif // BIND_SHADER_PARAMS_H
  2240. //////////////////////////// END BIND-SHADER-PARAMS ///////////////////////////
  2241. //#include "scanline-functions.h"
  2242. ///////////////////////////// BEGIN SCANLINE-FUNCTIONS ////////////////////////////
  2243. #ifndef SCANLINE_FUNCTIONS_H
  2244. #define SCANLINE_FUNCTIONS_H
  2245. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  2246. // crt-royale: A full-featured CRT shader, with cheese.
  2247. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  2248. //
  2249. // This program is free software; you can redistribute it and/or modify it
  2250. // under the terms of the GNU General Public License as published by the Free
  2251. // Software Foundation; either version 2 of the License, or any later version.
  2252. //
  2253. // This program is distributed in the hope that it will be useful, but WITHOUT
  2254. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  2255. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  2256. // more details.
  2257. //
  2258. // You should have received a copy of the GNU General Public License along with
  2259. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  2260. // Place, Suite 330, Boston, MA 02111-1307 USA
  2261. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  2262. //#include "../user-settings.h"
  2263. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  2264. #ifndef USER_SETTINGS_H
  2265. #define USER_SETTINGS_H
  2266. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  2267. // The Cg compiler uses different "profiles" with different capabilities.
  2268. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  2269. // require higher profiles like fp30 or fp40. The shader can't detect profile
  2270. // or driver capabilities, so instead you must comment or uncomment the lines
  2271. // below with "//" before "#define." Disable an option if you get compilation
  2272. // errors resembling those listed. Generally speaking, all of these options
  2273. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  2274. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  2275. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  2276. // Among other things, derivatives help us fix anisotropic filtering artifacts
  2277. // with curved manually tiled phosphor mask coords. Related errors:
  2278. // error C3004: function "float2 ddx(float2);" not supported in this profile
  2279. // error C3004: function "float2 ddy(float2);" not supported in this profile
  2280. //#define DRIVERS_ALLOW_DERIVATIVES
  2281. // Fine derivatives: Unsupported on older ATI cards.
  2282. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  2283. // fast single-pass blur operations. If your card uses coarse derivatives and
  2284. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  2285. #ifdef DRIVERS_ALLOW_DERIVATIVES
  2286. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  2287. #endif
  2288. // Dynamic looping: Requires an fp30 or newer profile.
  2289. // This makes phosphor mask resampling faster in some cases. Related errors:
  2290. // error C5013: profile does not support "for" statements and "for" could not
  2291. // be unrolled
  2292. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2293. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  2294. // Using one static loop avoids overhead if the user is right, but if the user
  2295. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  2296. // binary search can potentially save some iterations. However, it may fail:
  2297. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  2298. // needed to compile program
  2299. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  2300. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  2301. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  2302. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  2303. // this profile
  2304. //#define DRIVERS_ALLOW_TEX2DLOD
  2305. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  2306. // artifacts from anisotropic filtering and mipmapping. Related errors:
  2307. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  2308. // in this profile
  2309. //#define DRIVERS_ALLOW_TEX2DBIAS
  2310. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  2311. // impose stricter limitations on register counts and instructions. Enable
  2312. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  2313. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  2314. // to compile program.
  2315. // Enabling integrated graphics compatibility mode will automatically disable:
  2316. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  2317. // (This may be reenabled in a later release.)
  2318. // 2.) RUNTIME_GEOMETRY_MODE
  2319. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  2320. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  2321. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  2322. // To disable a #define option, turn its line into a comment with "//."
  2323. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  2324. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  2325. // many of the options in this file and allow real-time tuning, but many of
  2326. // them are slower. Disabling them and using this text file will boost FPS.
  2327. #define RUNTIME_SHADER_PARAMS_ENABLE
  2328. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  2329. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  2330. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  2331. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  2332. #define RUNTIME_ANTIALIAS_WEIGHTS
  2333. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  2334. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2335. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  2336. // parameters? This will require more math or dynamic branching.
  2337. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  2338. // Specify the tilt at runtime? This makes things about 3% slower.
  2339. #define RUNTIME_GEOMETRY_TILT
  2340. // Specify the geometry mode at runtime?
  2341. #define RUNTIME_GEOMETRY_MODE
  2342. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  2343. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  2344. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  2345. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2346. // PHOSPHOR MASK:
  2347. // Manually resize the phosphor mask for best results (slower)? Disabling this
  2348. // removes the option to do so, but it may be faster without dynamic branches.
  2349. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  2350. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  2351. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  2352. // Larger blurs are expensive, but we need them to blur larger triads. We can
  2353. // detect the right blur if the triad size is static or our profile allows
  2354. // dynamic branches, but otherwise we use the largest blur the user indicates
  2355. // they might need:
  2356. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  2357. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  2358. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  2359. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  2360. // Here's a helpful chart:
  2361. // MaxTriadSize BlurSize MinTriadCountsByResolution
  2362. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2363. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2364. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2365. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2366. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2367. /////////////////////////////// USER PARAMETERS //////////////////////////////
  2368. // Note: Many of these static parameters are overridden by runtime shader
  2369. // parameters when those are enabled. However, many others are static codepath
  2370. // options that were cleaner or more convert to code as static constants.
  2371. // GAMMA:
  2372. static const float crt_gamma_static = 2.5; // range [1, 5]
  2373. static const float lcd_gamma_static = 2.2; // range [1, 5]
  2374. // LEVELS MANAGEMENT:
  2375. // Control the final multiplicative image contrast:
  2376. static const float levels_contrast_static = 1.0; // range [0, 4)
  2377. // We auto-dim to avoid clipping between passes and restore brightness
  2378. // later. Control the dim factor here: Lower values clip less but crush
  2379. // blacks more (static only for now).
  2380. 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
  2381. // HALATION/DIFFUSION/BLOOM:
  2382. // Halation weight: How much energy should be lost to electrons bounding
  2383. // around under the CRT glass and exciting random phosphors?
  2384. static const float halation_weight_static = 0.0; // range [0, 1]
  2385. // Refractive diffusion weight: How much light should spread/diffuse from
  2386. // refracting through the CRT glass?
  2387. static const float diffusion_weight_static = 0.075; // range [0, 1]
  2388. // Underestimate brightness: Bright areas bloom more, but we can base the
  2389. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  2390. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  2391. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  2392. // Blur all colors more than necessary for a softer phosphor bloom?
  2393. static const float bloom_excess_static = 0.0; // range [0, 1]
  2394. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  2395. // blurred resize of the input (convergence offsets are applied as well).
  2396. // There are three filter options (static option only for now):
  2397. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  2398. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  2399. // and beam_max_sigma is low.
  2400. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  2401. // always uses a static sigma regardless of beam_max_sigma or
  2402. // mask_num_triads_desired.
  2403. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  2404. // These options are more pronounced for the fast, unbloomed shader version.
  2405. #ifndef RADEON_FIX
  2406. static const float bloom_approx_filter_static = 2.0;
  2407. #else
  2408. static const float bloom_approx_filter_static = 1.0;
  2409. #endif
  2410. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  2411. // How many scanlines should contribute light to each pixel? Using more
  2412. // scanlines is slower (especially for a generalized Gaussian) but less
  2413. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  2414. // max_beam_sigma at which the closest unused weight is guaranteed <
  2415. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  2416. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  2417. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  2418. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  2419. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  2420. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  2421. static const float beam_num_scanlines = 3.0; // range [2, 6]
  2422. // A generalized Gaussian beam varies shape with color too, now just width.
  2423. // It's slower but more flexible (static option only for now).
  2424. static const bool beam_generalized_gaussian = true;
  2425. // What kind of scanline antialiasing do you want?
  2426. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  2427. // Integrals are slow (especially for generalized Gaussians) and rarely any
  2428. // better than 3x antialiasing (static option only for now).
  2429. static const float beam_antialias_level = 1.0; // range [0, 2]
  2430. // Min/max standard deviations for scanline beams: Higher values widen and
  2431. // soften scanlines. Depending on other options, low min sigmas can alias.
  2432. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  2433. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  2434. // Beam width varies as a function of color: A power function (0) is more
  2435. // configurable, but a spherical function (1) gives the widest beam
  2436. // variability without aliasing (static option only for now).
  2437. static const float beam_spot_shape_function = 0.0;
  2438. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  2439. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  2440. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  2441. // Generalized Gaussian max shape parameters: Higher values give flatter
  2442. // scanline plateaus and steeper dropoffs, simultaneously widening and
  2443. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  2444. // values > ~40.0 cause artifacts with integrals.
  2445. static const float beam_min_shape_static = 2.0; // range [2, 32]
  2446. static const float beam_max_shape_static = 4.0; // range [2, 32]
  2447. // Generalized Gaussian shape power: Affects how quickly the distribution
  2448. // changes shape from Gaussian to steep/plateaued as color increases from 0
  2449. // to 1.0. Higher powers appear softer for most colors, and lower powers
  2450. // appear sharper for most colors.
  2451. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  2452. // What filter should be used to sample scanlines horizontally?
  2453. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  2454. static const float beam_horiz_filter_static = 0.0;
  2455. // Standard deviation for horizontal Gaussian resampling:
  2456. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  2457. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  2458. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  2459. // limiting circuitry in some CRT's), or a weighted avg.?
  2460. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  2461. // Simulate scanline misconvergence? This needs 3x horizontal texture
  2462. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  2463. // later passes (static option only for now).
  2464. static const bool beam_misconvergence = true;
  2465. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  2466. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  2467. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  2468. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  2469. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  2470. // Detect interlacing (static option only for now)?
  2471. static const bool interlace_detect = true;
  2472. // Assume 1080-line sources are interlaced?
  2473. static const bool interlace_1080i_static = false;
  2474. // For interlaced sources, assume TFF (top-field first) or BFF order?
  2475. // (Whether this matters depends on the nature of the interlaced input.)
  2476. static const bool interlace_bff_static = false;
  2477. // ANTIALIASING:
  2478. // What AA level do you want for curvature/overscan/subpixels? Options:
  2479. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  2480. // (Static option only for now)
  2481. static const float aa_level = 12.0; // range [0, 24]
  2482. // What antialiasing filter do you want (static option only)? Options:
  2483. // 0: Box (separable), 1: Box (cylindrical),
  2484. // 2: Tent (separable), 3: Tent (cylindrical),
  2485. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  2486. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  2487. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  2488. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  2489. static const float aa_filter = 6.0; // range [0, 9]
  2490. // Flip the sample grid on odd/even frames (static option only for now)?
  2491. static const bool aa_temporal = false;
  2492. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  2493. // the blue offset is the negative r offset; range [0, 0.5]
  2494. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  2495. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  2496. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  2497. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  2498. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  2499. // 4.) C = 0.0 is a soft spline filter.
  2500. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  2501. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  2502. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  2503. // PHOSPHOR MASK:
  2504. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  2505. static const float mask_type_static = 1.0; // range [0, 2]
  2506. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  2507. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  2508. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  2509. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  2510. // is halfway decent with LUT mipmapping but atrocious without it.
  2511. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  2512. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  2513. // This mode reuses the same masks, so triads will be enormous unless
  2514. // you change the mask LUT filenames in your .cgp file.
  2515. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  2516. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  2517. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  2518. // will always be used to calculate the full bloom sigma statically.
  2519. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  2520. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  2521. // triads) will be rounded to the nearest integer tile size and clamped to
  2522. // obey minimum size constraints (imposed to reduce downsize taps) and
  2523. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  2524. // To increase the size limit, double the viewport-relative scales for the
  2525. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  2526. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2527. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  2528. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  2529. // final size will be rounded and constrained as above); default 480.0
  2530. static const float mask_num_triads_desired_static = 480.0;
  2531. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  2532. // more samples and avoid moire a bit better, but some is unavoidable
  2533. // depending on the destination size (static option for now).
  2534. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  2535. // The mask is resized using a variable number of taps in each dimension,
  2536. // but some Cg profiles always fetch a constant number of taps no matter
  2537. // what (no dynamic branching). We can limit the maximum number of taps if
  2538. // we statically limit the minimum phosphor triad size. Larger values are
  2539. // faster, but the limit IS enforced (static option only, forever);
  2540. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2541. // TODO: Make this 1.0 and compensate with smarter sampling!
  2542. static const float mask_min_allowed_triad_size = 2.0;
  2543. // GEOMETRY:
  2544. // Geometry mode:
  2545. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  2546. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  2547. static const float geom_mode_static = 0.0; // range [0, 3]
  2548. // Radius of curvature: Measured in units of your viewport's diagonal size.
  2549. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  2550. // View dist is the distance from the player to their physical screen, in
  2551. // units of the viewport's diagonal size. It controls the field of view.
  2552. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  2553. // Tilt angle in radians (clockwise around up and right vectors):
  2554. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  2555. // Aspect ratio: When the true viewport size is unknown, this value is used
  2556. // to help convert between the phosphor triad size and count, along with
  2557. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  2558. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  2559. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  2560. // default (256/224)*(54/47) = 1.313069909 (see below)
  2561. static const float geom_aspect_ratio_static = 1.313069909;
  2562. // Before getting into overscan, here's some general aspect ratio info:
  2563. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  2564. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  2565. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  2566. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  2567. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  2568. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  2569. // a.) Enable Retroarch's "Crop Overscan"
  2570. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  2571. // Real consoles use horizontal black padding in the signal, but emulators
  2572. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  2573. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  2574. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  2575. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  2576. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  2577. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  2578. // without doing a. or b., but horizontal image borders will be tighter
  2579. // than vertical ones, messing up curvature and overscan. Fixing the
  2580. // padding first corrects this.
  2581. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  2582. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  2583. // above: Values < 1.0 zoom out; range (0, inf)
  2584. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  2585. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  2586. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  2587. // with strong curvature (static option only for now).
  2588. static const bool geom_force_correct_tangent_matrix = true;
  2589. // BORDERS:
  2590. // Rounded border size in texture uv coords:
  2591. static const float border_size_static = 0.015; // range [0, 0.5]
  2592. // Border darkness: Moderate values darken the border smoothly, and high
  2593. // values make the image very dark just inside the border:
  2594. static const float border_darkness_static = 2.0; // range [0, inf)
  2595. // Border compression: High numbers compress border transitions, narrowing
  2596. // the dark border area.
  2597. static const float border_compress_static = 2.5; // range [1, inf)
  2598. #endif // USER_SETTINGS_H
  2599. //////////////////////////// END USER-SETTINGS //////////////////////////
  2600. //#include "derived-settings-and-constants.h"
  2601. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  2602. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  2603. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  2604. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  2605. // crt-royale: A full-featured CRT shader, with cheese.
  2606. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  2607. //
  2608. // This program is free software; you can redistribute it and/or modify it
  2609. // under the terms of the GNU General Public License as published by the Free
  2610. // Software Foundation; either version 2 of the License, or any later version.
  2611. //
  2612. // This program is distributed in the hope that it will be useful, but WITHOUT
  2613. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  2614. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  2615. // more details.
  2616. //
  2617. // You should have received a copy of the GNU General Public License along with
  2618. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  2619. // Place, Suite 330, Boston, MA 02111-1307 USA
  2620. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  2621. // These macros and constants can be used across the whole codebase.
  2622. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  2623. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  2624. //#include "../user-settings.h"
  2625. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  2626. #ifndef USER_SETTINGS_H
  2627. #define USER_SETTINGS_H
  2628. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  2629. // The Cg compiler uses different "profiles" with different capabilities.
  2630. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  2631. // require higher profiles like fp30 or fp40. The shader can't detect profile
  2632. // or driver capabilities, so instead you must comment or uncomment the lines
  2633. // below with "//" before "#define." Disable an option if you get compilation
  2634. // errors resembling those listed. Generally speaking, all of these options
  2635. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  2636. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  2637. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  2638. // Among other things, derivatives help us fix anisotropic filtering artifacts
  2639. // with curved manually tiled phosphor mask coords. Related errors:
  2640. // error C3004: function "float2 ddx(float2);" not supported in this profile
  2641. // error C3004: function "float2 ddy(float2);" not supported in this profile
  2642. //#define DRIVERS_ALLOW_DERIVATIVES
  2643. // Fine derivatives: Unsupported on older ATI cards.
  2644. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  2645. // fast single-pass blur operations. If your card uses coarse derivatives and
  2646. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  2647. #ifdef DRIVERS_ALLOW_DERIVATIVES
  2648. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  2649. #endif
  2650. // Dynamic looping: Requires an fp30 or newer profile.
  2651. // This makes phosphor mask resampling faster in some cases. Related errors:
  2652. // error C5013: profile does not support "for" statements and "for" could not
  2653. // be unrolled
  2654. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2655. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  2656. // Using one static loop avoids overhead if the user is right, but if the user
  2657. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  2658. // binary search can potentially save some iterations. However, it may fail:
  2659. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  2660. // needed to compile program
  2661. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  2662. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  2663. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  2664. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  2665. // this profile
  2666. //#define DRIVERS_ALLOW_TEX2DLOD
  2667. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  2668. // artifacts from anisotropic filtering and mipmapping. Related errors:
  2669. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  2670. // in this profile
  2671. //#define DRIVERS_ALLOW_TEX2DBIAS
  2672. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  2673. // impose stricter limitations on register counts and instructions. Enable
  2674. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  2675. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  2676. // to compile program.
  2677. // Enabling integrated graphics compatibility mode will automatically disable:
  2678. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  2679. // (This may be reenabled in a later release.)
  2680. // 2.) RUNTIME_GEOMETRY_MODE
  2681. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  2682. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  2683. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  2684. // To disable a #define option, turn its line into a comment with "//."
  2685. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  2686. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  2687. // many of the options in this file and allow real-time tuning, but many of
  2688. // them are slower. Disabling them and using this text file will boost FPS.
  2689. #define RUNTIME_SHADER_PARAMS_ENABLE
  2690. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  2691. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  2692. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  2693. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  2694. #define RUNTIME_ANTIALIAS_WEIGHTS
  2695. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  2696. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2697. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  2698. // parameters? This will require more math or dynamic branching.
  2699. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  2700. // Specify the tilt at runtime? This makes things about 3% slower.
  2701. #define RUNTIME_GEOMETRY_TILT
  2702. // Specify the geometry mode at runtime?
  2703. #define RUNTIME_GEOMETRY_MODE
  2704. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  2705. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  2706. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  2707. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2708. // PHOSPHOR MASK:
  2709. // Manually resize the phosphor mask for best results (slower)? Disabling this
  2710. // removes the option to do so, but it may be faster without dynamic branches.
  2711. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  2712. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  2713. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  2714. // Larger blurs are expensive, but we need them to blur larger triads. We can
  2715. // detect the right blur if the triad size is static or our profile allows
  2716. // dynamic branches, but otherwise we use the largest blur the user indicates
  2717. // they might need:
  2718. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  2719. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  2720. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  2721. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  2722. // Here's a helpful chart:
  2723. // MaxTriadSize BlurSize MinTriadCountsByResolution
  2724. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2725. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2726. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2727. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2728. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  2729. /////////////////////////////// USER PARAMETERS //////////////////////////////
  2730. // Note: Many of these static parameters are overridden by runtime shader
  2731. // parameters when those are enabled. However, many others are static codepath
  2732. // options that were cleaner or more convert to code as static constants.
  2733. // GAMMA:
  2734. static const float crt_gamma_static = 2.5; // range [1, 5]
  2735. static const float lcd_gamma_static = 2.2; // range [1, 5]
  2736. // LEVELS MANAGEMENT:
  2737. // Control the final multiplicative image contrast:
  2738. static const float levels_contrast_static = 1.0; // range [0, 4)
  2739. // We auto-dim to avoid clipping between passes and restore brightness
  2740. // later. Control the dim factor here: Lower values clip less but crush
  2741. // blacks more (static only for now).
  2742. 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
  2743. // HALATION/DIFFUSION/BLOOM:
  2744. // Halation weight: How much energy should be lost to electrons bounding
  2745. // around under the CRT glass and exciting random phosphors?
  2746. static const float halation_weight_static = 0.0; // range [0, 1]
  2747. // Refractive diffusion weight: How much light should spread/diffuse from
  2748. // refracting through the CRT glass?
  2749. static const float diffusion_weight_static = 0.075; // range [0, 1]
  2750. // Underestimate brightness: Bright areas bloom more, but we can base the
  2751. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  2752. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  2753. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  2754. // Blur all colors more than necessary for a softer phosphor bloom?
  2755. static const float bloom_excess_static = 0.0; // range [0, 1]
  2756. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  2757. // blurred resize of the input (convergence offsets are applied as well).
  2758. // There are three filter options (static option only for now):
  2759. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  2760. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  2761. // and beam_max_sigma is low.
  2762. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  2763. // always uses a static sigma regardless of beam_max_sigma or
  2764. // mask_num_triads_desired.
  2765. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  2766. // These options are more pronounced for the fast, unbloomed shader version.
  2767. #ifndef RADEON_FIX
  2768. static const float bloom_approx_filter_static = 2.0;
  2769. #else
  2770. static const float bloom_approx_filter_static = 1.0;
  2771. #endif
  2772. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  2773. // How many scanlines should contribute light to each pixel? Using more
  2774. // scanlines is slower (especially for a generalized Gaussian) but less
  2775. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  2776. // max_beam_sigma at which the closest unused weight is guaranteed <
  2777. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  2778. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  2779. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  2780. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  2781. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  2782. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  2783. static const float beam_num_scanlines = 3.0; // range [2, 6]
  2784. // A generalized Gaussian beam varies shape with color too, now just width.
  2785. // It's slower but more flexible (static option only for now).
  2786. static const bool beam_generalized_gaussian = true;
  2787. // What kind of scanline antialiasing do you want?
  2788. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  2789. // Integrals are slow (especially for generalized Gaussians) and rarely any
  2790. // better than 3x antialiasing (static option only for now).
  2791. static const float beam_antialias_level = 1.0; // range [0, 2]
  2792. // Min/max standard deviations for scanline beams: Higher values widen and
  2793. // soften scanlines. Depending on other options, low min sigmas can alias.
  2794. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  2795. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  2796. // Beam width varies as a function of color: A power function (0) is more
  2797. // configurable, but a spherical function (1) gives the widest beam
  2798. // variability without aliasing (static option only for now).
  2799. static const float beam_spot_shape_function = 0.0;
  2800. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  2801. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  2802. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  2803. // Generalized Gaussian max shape parameters: Higher values give flatter
  2804. // scanline plateaus and steeper dropoffs, simultaneously widening and
  2805. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  2806. // values > ~40.0 cause artifacts with integrals.
  2807. static const float beam_min_shape_static = 2.0; // range [2, 32]
  2808. static const float beam_max_shape_static = 4.0; // range [2, 32]
  2809. // Generalized Gaussian shape power: Affects how quickly the distribution
  2810. // changes shape from Gaussian to steep/plateaued as color increases from 0
  2811. // to 1.0. Higher powers appear softer for most colors, and lower powers
  2812. // appear sharper for most colors.
  2813. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  2814. // What filter should be used to sample scanlines horizontally?
  2815. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  2816. static const float beam_horiz_filter_static = 0.0;
  2817. // Standard deviation for horizontal Gaussian resampling:
  2818. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  2819. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  2820. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  2821. // limiting circuitry in some CRT's), or a weighted avg.?
  2822. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  2823. // Simulate scanline misconvergence? This needs 3x horizontal texture
  2824. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  2825. // later passes (static option only for now).
  2826. static const bool beam_misconvergence = true;
  2827. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  2828. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  2829. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  2830. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  2831. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  2832. // Detect interlacing (static option only for now)?
  2833. static const bool interlace_detect = true;
  2834. // Assume 1080-line sources are interlaced?
  2835. static const bool interlace_1080i_static = false;
  2836. // For interlaced sources, assume TFF (top-field first) or BFF order?
  2837. // (Whether this matters depends on the nature of the interlaced input.)
  2838. static const bool interlace_bff_static = false;
  2839. // ANTIALIASING:
  2840. // What AA level do you want for curvature/overscan/subpixels? Options:
  2841. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  2842. // (Static option only for now)
  2843. static const float aa_level = 12.0; // range [0, 24]
  2844. // What antialiasing filter do you want (static option only)? Options:
  2845. // 0: Box (separable), 1: Box (cylindrical),
  2846. // 2: Tent (separable), 3: Tent (cylindrical),
  2847. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  2848. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  2849. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  2850. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  2851. static const float aa_filter = 6.0; // range [0, 9]
  2852. // Flip the sample grid on odd/even frames (static option only for now)?
  2853. static const bool aa_temporal = false;
  2854. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  2855. // the blue offset is the negative r offset; range [0, 0.5]
  2856. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  2857. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  2858. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  2859. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  2860. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  2861. // 4.) C = 0.0 is a soft spline filter.
  2862. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  2863. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  2864. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  2865. // PHOSPHOR MASK:
  2866. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  2867. static const float mask_type_static = 1.0; // range [0, 2]
  2868. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  2869. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  2870. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  2871. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  2872. // is halfway decent with LUT mipmapping but atrocious without it.
  2873. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  2874. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  2875. // This mode reuses the same masks, so triads will be enormous unless
  2876. // you change the mask LUT filenames in your .cgp file.
  2877. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  2878. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  2879. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  2880. // will always be used to calculate the full bloom sigma statically.
  2881. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  2882. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  2883. // triads) will be rounded to the nearest integer tile size and clamped to
  2884. // obey minimum size constraints (imposed to reduce downsize taps) and
  2885. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  2886. // To increase the size limit, double the viewport-relative scales for the
  2887. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  2888. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2889. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  2890. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  2891. // final size will be rounded and constrained as above); default 480.0
  2892. static const float mask_num_triads_desired_static = 480.0;
  2893. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  2894. // more samples and avoid moire a bit better, but some is unavoidable
  2895. // depending on the destination size (static option for now).
  2896. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  2897. // The mask is resized using a variable number of taps in each dimension,
  2898. // but some Cg profiles always fetch a constant number of taps no matter
  2899. // what (no dynamic branching). We can limit the maximum number of taps if
  2900. // we statically limit the minimum phosphor triad size. Larger values are
  2901. // faster, but the limit IS enforced (static option only, forever);
  2902. // range [1, mask_texture_small_size/mask_triads_per_tile]
  2903. // TODO: Make this 1.0 and compensate with smarter sampling!
  2904. static const float mask_min_allowed_triad_size = 2.0;
  2905. // GEOMETRY:
  2906. // Geometry mode:
  2907. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  2908. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  2909. static const float geom_mode_static = 0.0; // range [0, 3]
  2910. // Radius of curvature: Measured in units of your viewport's diagonal size.
  2911. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  2912. // View dist is the distance from the player to their physical screen, in
  2913. // units of the viewport's diagonal size. It controls the field of view.
  2914. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  2915. // Tilt angle in radians (clockwise around up and right vectors):
  2916. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  2917. // Aspect ratio: When the true viewport size is unknown, this value is used
  2918. // to help convert between the phosphor triad size and count, along with
  2919. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  2920. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  2921. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  2922. // default (256/224)*(54/47) = 1.313069909 (see below)
  2923. static const float geom_aspect_ratio_static = 1.313069909;
  2924. // Before getting into overscan, here's some general aspect ratio info:
  2925. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  2926. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  2927. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  2928. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  2929. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  2930. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  2931. // a.) Enable Retroarch's "Crop Overscan"
  2932. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  2933. // Real consoles use horizontal black padding in the signal, but emulators
  2934. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  2935. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  2936. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  2937. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  2938. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  2939. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  2940. // without doing a. or b., but horizontal image borders will be tighter
  2941. // than vertical ones, messing up curvature and overscan. Fixing the
  2942. // padding first corrects this.
  2943. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  2944. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  2945. // above: Values < 1.0 zoom out; range (0, inf)
  2946. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  2947. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  2948. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  2949. // with strong curvature (static option only for now).
  2950. static const bool geom_force_correct_tangent_matrix = true;
  2951. // BORDERS:
  2952. // Rounded border size in texture uv coords:
  2953. static const float border_size_static = 0.015; // range [0, 0.5]
  2954. // Border darkness: Moderate values darken the border smoothly, and high
  2955. // values make the image very dark just inside the border:
  2956. static const float border_darkness_static = 2.0; // range [0, inf)
  2957. // Border compression: High numbers compress border transitions, narrowing
  2958. // the dark border area.
  2959. static const float border_compress_static = 2.5; // range [1, inf)
  2960. #endif // USER_SETTINGS_H
  2961. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  2962. //#include "user-cgp-constants.h"
  2963. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  2964. #ifndef USER_CGP_CONSTANTS_H
  2965. #define USER_CGP_CONSTANTS_H
  2966. // IMPORTANT:
  2967. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  2968. // (or whatever related .cgp file you're using). If they aren't, you're likely
  2969. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  2970. // set directly in the .cgp file to make things easier, but...they can't.
  2971. // PASS SCALES AND RELATED CONSTANTS:
  2972. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  2973. // this shader: One does a viewport-scale bloom, and the other skips it. The
  2974. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  2975. static const float bloom_approx_size_x = 320.0;
  2976. static const float bloom_approx_size_x_for_fake = 400.0;
  2977. // Copy the viewport-relative scales of the phosphor mask resize passes
  2978. // (MASK_RESIZE and the pass immediately preceding it):
  2979. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  2980. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  2981. static const float geom_max_aspect_ratio = 4.0/3.0;
  2982. // PHOSPHOR MASK TEXTURE CONSTANTS:
  2983. // Set the following constants to reflect the properties of the phosphor mask
  2984. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  2985. // based on user settings, then repeats a single tile until filling the screen.
  2986. // The shader must know the input texture size (default 64x64), and to manually
  2987. // resize, it must also know the horizontal triads per tile (default 8).
  2988. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  2989. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  2990. static const float mask_triads_per_tile = 8.0;
  2991. // We need the average brightness of the phosphor mask to compensate for the
  2992. // dimming it causes. The following four values are roughly correct for the
  2993. // masks included with the shader. Update the value for any LUT texture you
  2994. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  2995. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  2996. //#define PHOSPHOR_MASK_GRILLE14
  2997. static const float mask_grille14_avg_color = 50.6666666/255.0;
  2998. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  2999. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  3000. static const float mask_grille15_avg_color = 53.0/255.0;
  3001. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  3002. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  3003. static const float mask_slot_avg_color = 46.0/255.0;
  3004. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  3005. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  3006. static const float mask_shadow_avg_color = 41.0/255.0;
  3007. // TileableLinearShadowMask*.png
  3008. // TileableLinearShadowMaskEDP*.png
  3009. #ifdef PHOSPHOR_MASK_GRILLE14
  3010. static const float mask_grille_avg_color = mask_grille14_avg_color;
  3011. #else
  3012. static const float mask_grille_avg_color = mask_grille15_avg_color;
  3013. #endif
  3014. #endif // USER_CGP_CONSTANTS_H
  3015. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  3016. //////////////////////////////// END INCLUDES ////////////////////////////////
  3017. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  3018. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  3019. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  3020. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  3021. #ifndef SIMULATE_CRT_ON_LCD
  3022. #define SIMULATE_CRT_ON_LCD
  3023. #endif
  3024. // Manually tiling a manually resized texture creates texture coord derivative
  3025. // discontinuities and confuses anisotropic filtering, causing discolored tile
  3026. // seams in the phosphor mask. Workarounds:
  3027. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  3028. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  3029. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  3030. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  3031. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  3032. // (Same-pass curvature isn't used but could be in the future...maybe.)
  3033. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  3034. // border padding to the resized mask FBO, but it works with same-pass
  3035. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  3036. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  3037. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  3038. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  3039. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  3040. // Also, manually resampling the phosphor mask is slightly blurrier with
  3041. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  3042. // creates artifacts, but only with the fully bloomed shader.) The difference
  3043. // is subtle with small triads, but you can fix it for a small cost.
  3044. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3045. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  3046. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  3047. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  3048. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  3049. // #defined by either user-settings.h or a wrapper .cg that #includes the
  3050. // current .cg pass.)
  3051. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  3052. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  3053. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  3054. #endif
  3055. #ifdef RUNTIME_GEOMETRY_MODE
  3056. #undef RUNTIME_GEOMETRY_MODE
  3057. #endif
  3058. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  3059. // inferior in most cases, so replace 2.0 with 0.0:
  3060. static const float bloom_approx_filter =
  3061. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  3062. #else
  3063. static const float bloom_approx_filter = bloom_approx_filter_static;
  3064. #endif
  3065. // Disable slow runtime paths if static parameters are used. Most of these
  3066. // won't be a problem anyway once the params are disabled, but some will.
  3067. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  3068. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  3069. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  3070. #endif
  3071. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  3072. #undef RUNTIME_ANTIALIAS_WEIGHTS
  3073. #endif
  3074. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  3075. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  3076. #endif
  3077. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  3078. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  3079. #endif
  3080. #ifdef RUNTIME_GEOMETRY_TILT
  3081. #undef RUNTIME_GEOMETRY_TILT
  3082. #endif
  3083. #ifdef RUNTIME_GEOMETRY_MODE
  3084. #undef RUNTIME_GEOMETRY_MODE
  3085. #endif
  3086. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3087. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3088. #endif
  3089. #endif
  3090. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  3091. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3092. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3093. #endif
  3094. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3095. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3096. #endif
  3097. // Rule out unavailable anisotropic compatibility strategies:
  3098. #ifndef DRIVERS_ALLOW_DERIVATIVES
  3099. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3100. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3101. #endif
  3102. #endif
  3103. #ifndef DRIVERS_ALLOW_TEX2DLOD
  3104. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3105. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3106. #endif
  3107. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3108. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3109. #endif
  3110. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  3111. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  3112. #endif
  3113. #endif
  3114. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  3115. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3116. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3117. #endif
  3118. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3119. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3120. #endif
  3121. #endif
  3122. // Prioritize anisotropic tiling compatibility strategies by performance and
  3123. // disable unused strategies. This concentrates all the nesting in one place.
  3124. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3125. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3126. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3127. #endif
  3128. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3129. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3130. #endif
  3131. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3132. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3133. #endif
  3134. #else
  3135. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3136. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3137. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3138. #endif
  3139. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3140. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3141. #endif
  3142. #else
  3143. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  3144. // flat texture coords in the same pass, but that's all we use.
  3145. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3146. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3147. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3148. #endif
  3149. #endif
  3150. #endif
  3151. #endif
  3152. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  3153. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  3154. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  3155. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3156. #endif
  3157. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  3158. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3159. #endif
  3160. // Prioritize anisotropic resampling compatibility strategies the same way:
  3161. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3162. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3163. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  3164. #endif
  3165. #endif
  3166. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  3167. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  3168. // should: It gives us more options for using fewer samples.
  3169. #ifdef DRIVERS_ALLOW_TEX2DLOD
  3170. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  3171. // TODO: Take advantage of this!
  3172. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  3173. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  3174. #else
  3175. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  3176. #endif
  3177. #else
  3178. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  3179. #endif
  3180. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  3181. // main_fragment, or a static alias of one of the above. This makes it hard
  3182. // to select the phosphor mask at runtime: We can't even assign to a uniform
  3183. // global in the vertex shader or select a sampler2D in the vertex shader and
  3184. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  3185. // because it just gives us the input texture or a black screen. However, we
  3186. // can get around these limitations by calling tex2D three times with different
  3187. // uniform samplers (or resizing the phosphor mask three times altogether).
  3188. // With dynamic branches, we can process only one of these branches on top of
  3189. // quickly discarding fragments we don't need (cgc seems able to overcome
  3190. // limigations around dependent texture fetches inside of branches). Without
  3191. // dynamic branches, we have to process every branch for every fragment...which
  3192. // is slower. Runtime sampling mode selection is slower without dynamic
  3193. // branches as well. Let the user's static #defines decide if it's worth it.
  3194. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  3195. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3196. #else
  3197. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3198. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  3199. #endif
  3200. #endif
  3201. // We need to render some minimum number of tiles in the resize passes.
  3202. // We need at least 1.0 just to repeat a single tile, and we need extra
  3203. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  3204. // antialiasing, same-pass curvature (not currently used), etc. First
  3205. // determine how many border texels and tiles we need, based on how the result
  3206. // will be sampled:
  3207. #ifdef GEOMETRY_EARLY
  3208. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  3209. // Most antialiasing filters have a base radius of 4.0 pixels:
  3210. static const float max_aa_base_pixel_border = 4.0 +
  3211. max_subpixel_offset;
  3212. #else
  3213. static const float max_aa_base_pixel_border = 0.0;
  3214. #endif
  3215. // Anisotropic filtering adds about 0.5 to the pixel border:
  3216. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  3217. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  3218. #else
  3219. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  3220. #endif
  3221. // Fixing discontinuities adds 1.0 more to the pixel border:
  3222. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3223. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  3224. #else
  3225. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  3226. #endif
  3227. // Convert the pixel border to an integer texel border. Assume same-pass
  3228. // curvature about triples the texel frequency:
  3229. #ifdef GEOMETRY_EARLY
  3230. static const float max_mask_texel_border =
  3231. ceil(max_tiled_pixel_border * 3.0);
  3232. #else
  3233. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  3234. #endif
  3235. // Convert the texel border to a tile border using worst-case assumptions:
  3236. static const float max_mask_tile_border = max_mask_texel_border/
  3237. (mask_min_allowed_triad_size * mask_triads_per_tile);
  3238. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  3239. // the starting texel (inside borders) for sampling it.
  3240. #ifndef GEOMETRY_EARLY
  3241. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3242. // Special case: Render two tiles without borders. Anisotropic
  3243. // filtering doesn't seem to be a problem here.
  3244. static const float mask_resize_num_tiles = 1.0 + 1.0;
  3245. static const float mask_start_texels = 0.0;
  3246. #else
  3247. static const float mask_resize_num_tiles = 1.0 +
  3248. 2.0 * max_mask_tile_border;
  3249. static const float mask_start_texels = max_mask_texel_border;
  3250. #endif
  3251. #else
  3252. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  3253. static const float mask_start_texels = max_mask_texel_border;
  3254. #endif
  3255. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  3256. // mask_resize_viewport_scale. This limits the maximum final triad size.
  3257. // Estimate the minimum number of triads we can split the screen into in each
  3258. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  3259. static const float mask_resize_num_triads =
  3260. mask_resize_num_tiles * mask_triads_per_tile;
  3261. static const float2 min_allowed_viewport_triads =
  3262. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  3263. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  3264. static const float pi = 3.141592653589;
  3265. // We often want to find the location of the previous texel, e.g.:
  3266. // const float2 curr_texel = uv * texture_size;
  3267. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  3268. // const float2 prev_texel_uv = prev_texel / texture_size;
  3269. // However, many GPU drivers round incorrectly around exact texel locations.
  3270. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  3271. // require this value to be farther from 0.5 than others; define it here.
  3272. // const float2 prev_texel =
  3273. // floor(curr_texel - float2(under_half)) + float2(0.5);
  3274. static const float under_half = 0.4995;
  3275. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  3276. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  3277. //#include "../../../../include/special-functions.h"
  3278. /////////////////////////// BEGIN SPECIAL-FUNCTIONS //////////////////////////
  3279. #ifndef SPECIAL_FUNCTIONS_H
  3280. #define SPECIAL_FUNCTIONS_H
  3281. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3282. // Copyright (C) 2014 TroggleMonkey
  3283. //
  3284. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3285. // of this software and associated documentation files (the "Software"), to
  3286. // deal in the Software without restriction, including without limitation the
  3287. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  3288. // sell copies of the Software, and to permit persons to whom the Software is
  3289. // furnished to do so, subject to the following conditions:
  3290. //
  3291. // The above copyright notice and this permission notice shall be included in
  3292. // all copies or substantial portions of the Software.
  3293. //
  3294. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3295. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3296. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3297. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3298. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3299. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3300. // IN THE SOFTWARE.
  3301. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  3302. // This file implements the following mathematical special functions:
  3303. // 1.) erf() = 2/sqrt(pi) * indefinite_integral(e**(-x**2))
  3304. // 2.) gamma(s), a real-numbered extension of the integer factorial function
  3305. // It also implements normalized_ligamma(s, z), a normalized lower incomplete
  3306. // gamma function for s < 0.5 only. Both gamma() and normalized_ligamma() can
  3307. // be called with an _impl suffix to use an implementation version with a few
  3308. // extra precomputed parameters (which may be useful for the caller to reuse).
  3309. // See below for details.
  3310. //
  3311. // Design Rationale:
  3312. // Pretty much every line of code in this file is duplicated four times for
  3313. // different input types (float4/float3/float2/float). This is unfortunate,
  3314. // but Cg doesn't allow function templates. Macros would be far less verbose,
  3315. // but they would make the code harder to document and read. I don't expect
  3316. // these functions will require a whole lot of maintenance changes unless
  3317. // someone ever has need for more robust incomplete gamma functions, so code
  3318. // duplication seems to be the lesser evil in this case.
  3319. /////////////////////////// GAUSSIAN ERROR FUNCTION //////////////////////////
  3320. float4 erf6(float4 x)
  3321. {
  3322. // Requires: x is the standard parameter to erf().
  3323. // Returns: Return an Abramowitz/Stegun approximation of erf(), where:
  3324. // erf(x) = 2/sqrt(pi) * integral(e**(-x**2))
  3325. // This approximation has a max absolute error of 2.5*10**-5
  3326. // with solid numerical robustness and efficiency. See:
  3327. // https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions
  3328. static const float4 one = float4(1.0);
  3329. const float4 sign_x = sign(x);
  3330. const float4 t = one/(one + 0.47047*abs(x));
  3331. const float4 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3332. exp(-(x*x));
  3333. return result * sign_x;
  3334. }
  3335. float3 erf6(const float3 x)
  3336. {
  3337. // Float3 version:
  3338. static const float3 one = float3(1.0);
  3339. const float3 sign_x = sign(x);
  3340. const float3 t = one/(one + 0.47047*abs(x));
  3341. const float3 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3342. exp(-(x*x));
  3343. return result * sign_x;
  3344. }
  3345. float2 erf6(const float2 x)
  3346. {
  3347. // Float2 version:
  3348. static const float2 one = float2(1.0);
  3349. const float2 sign_x = sign(x);
  3350. const float2 t = one/(one + 0.47047*abs(x));
  3351. const float2 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3352. exp(-(x*x));
  3353. return result * sign_x;
  3354. }
  3355. float erf6(const float x)
  3356. {
  3357. // Float version:
  3358. const float sign_x = sign(x);
  3359. const float t = 1.0/(1.0 + 0.47047*abs(x));
  3360. const float result = 1.0 - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  3361. exp(-(x*x));
  3362. return result * sign_x;
  3363. }
  3364. float4 erft(const float4 x)
  3365. {
  3366. // Requires: x is the standard parameter to erf().
  3367. // Returns: Approximate erf() with the hyperbolic tangent. The error is
  3368. // visually noticeable, but it's blazing fast and perceptually
  3369. // close...at least on ATI hardware. See:
  3370. // http://www.maplesoft.com/applications/view.aspx?SID=5525&view=html
  3371. // Warning: Only use this if your hardware drivers correctly implement
  3372. // tanh(): My nVidia 8800GTS returns garbage output.
  3373. return tanh(1.202760580 * x);
  3374. }
  3375. float3 erft(const float3 x)
  3376. {
  3377. // Float3 version:
  3378. return tanh(1.202760580 * x);
  3379. }
  3380. float2 erft(const float2 x)
  3381. {
  3382. // Float2 version:
  3383. return tanh(1.202760580 * x);
  3384. }
  3385. float erft(const float x)
  3386. {
  3387. // Float version:
  3388. return tanh(1.202760580 * x);
  3389. }
  3390. inline float4 erf(const float4 x)
  3391. {
  3392. // Requires: x is the standard parameter to erf().
  3393. // Returns: Some approximation of erf(x), depending on user settings.
  3394. #ifdef ERF_FAST_APPROXIMATION
  3395. return erft(x);
  3396. #else
  3397. return erf6(x);
  3398. #endif
  3399. }
  3400. inline float3 erf(const float3 x)
  3401. {
  3402. // Float3 version:
  3403. #ifdef ERF_FAST_APPROXIMATION
  3404. return erft(x);
  3405. #else
  3406. return erf6(x);
  3407. #endif
  3408. }
  3409. inline float2 erf(const float2 x)
  3410. {
  3411. // Float2 version:
  3412. #ifdef ERF_FAST_APPROXIMATION
  3413. return erft(x);
  3414. #else
  3415. return erf6(x);
  3416. #endif
  3417. }
  3418. inline float erf(const float x)
  3419. {
  3420. // Float version:
  3421. #ifdef ERF_FAST_APPROXIMATION
  3422. return erft(x);
  3423. #else
  3424. return erf6(x);
  3425. #endif
  3426. }
  3427. /////////////////////////// COMPLETE GAMMA FUNCTION //////////////////////////
  3428. float4 gamma_impl(const float4 s, const float4 s_inv)
  3429. {
  3430. // Requires: 1.) s is the standard parameter to the gamma function, and
  3431. // it should lie in the [0, 36] range.
  3432. // 2.) s_inv = 1.0/s. This implementation function requires
  3433. // the caller to precompute this value, giving users the
  3434. // opportunity to reuse it.
  3435. // Returns: Return approximate gamma function (real-numbered factorial)
  3436. // output using the Lanczos approximation with two coefficients
  3437. // calculated using Paul Godfrey's method here:
  3438. // http://my.fit.edu/~gabdo/gamma.txt
  3439. // An optimal g value for s in [0, 36] is ~1.12906830989, with
  3440. // a maximum relative error of 0.000463 for 2**16 equally
  3441. // evals. We could use three coeffs (0.0000346 error) without
  3442. // hurting latency, but this allows more parallelism with
  3443. // outside instructions.
  3444. static const float4 g = float4(1.12906830989);
  3445. static const float4 c0 = float4(0.8109119309638332633713423362694399653724431);
  3446. static const float4 c1 = float4(0.4808354605142681877121661197951496120000040);
  3447. static const float4 e = float4(2.71828182845904523536028747135266249775724709);
  3448. const float4 sph = s + float4(0.5);
  3449. const float4 lanczos_sum = c0 + c1/(s + float4(1.0));
  3450. const float4 base = (sph + g)/e; // or (s + g + float4(0.5))/e
  3451. // gamma(s + 1) = base**sph * lanczos_sum; divide by s for gamma(s).
  3452. // This has less error for small s's than (s -= 1.0) at the beginning.
  3453. return (pow(base, sph) * lanczos_sum) * s_inv;
  3454. }
  3455. float3 gamma_impl(const float3 s, const float3 s_inv)
  3456. {
  3457. // Float3 version:
  3458. static const float3 g = float3(1.12906830989);
  3459. static const float3 c0 = float3(0.8109119309638332633713423362694399653724431);
  3460. static const float3 c1 = float3(0.4808354605142681877121661197951496120000040);
  3461. static const float3 e = float3(2.71828182845904523536028747135266249775724709);
  3462. const float3 sph = s + float3(0.5);
  3463. const float3 lanczos_sum = c0 + c1/(s + float3(1.0));
  3464. const float3 base = (sph + g)/e;
  3465. return (pow(base, sph) * lanczos_sum) * s_inv;
  3466. }
  3467. float2 gamma_impl(const float2 s, const float2 s_inv)
  3468. {
  3469. // Float2 version:
  3470. static const float2 g = float2(1.12906830989);
  3471. static const float2 c0 = float2(0.8109119309638332633713423362694399653724431);
  3472. static const float2 c1 = float2(0.4808354605142681877121661197951496120000040);
  3473. static const float2 e = float2(2.71828182845904523536028747135266249775724709);
  3474. const float2 sph = s + float2(0.5);
  3475. const float2 lanczos_sum = c0 + c1/(s + float2(1.0));
  3476. const float2 base = (sph + g)/e;
  3477. return (pow(base, sph) * lanczos_sum) * s_inv;
  3478. }
  3479. float gamma_impl(const float s, const float s_inv)
  3480. {
  3481. // Float version:
  3482. static const float g = 1.12906830989;
  3483. static const float c0 = 0.8109119309638332633713423362694399653724431;
  3484. static const float c1 = 0.4808354605142681877121661197951496120000040;
  3485. static const float e = 2.71828182845904523536028747135266249775724709;
  3486. const float sph = s + 0.5;
  3487. const float lanczos_sum = c0 + c1/(s + 1.0);
  3488. const float base = (sph + g)/e;
  3489. return (pow(base, sph) * lanczos_sum) * s_inv;
  3490. }
  3491. float4 gamma(const float4 s)
  3492. {
  3493. // Requires: s is the standard parameter to the gamma function, and it
  3494. // should lie in the [0, 36] range.
  3495. // Returns: Return approximate gamma function output with a maximum
  3496. // relative error of 0.000463. See gamma_impl for details.
  3497. return gamma_impl(s, float4(1.0)/s);
  3498. }
  3499. float3 gamma(const float3 s)
  3500. {
  3501. // Float3 version:
  3502. return gamma_impl(s, float3(1.0)/s);
  3503. }
  3504. float2 gamma(const float2 s)
  3505. {
  3506. // Float2 version:
  3507. return gamma_impl(s, float2(1.0)/s);
  3508. }
  3509. float gamma(const float s)
  3510. {
  3511. // Float version:
  3512. return gamma_impl(s, 1.0/s);
  3513. }
  3514. //////////////// INCOMPLETE GAMMA FUNCTIONS (RESTRICTED INPUT) ///////////////
  3515. // Lower incomplete gamma function for small s and z (implementation):
  3516. float4 ligamma_small_z_impl(const float4 s, const float4 z, const float4 s_inv)
  3517. {
  3518. // Requires: 1.) s < ~0.5
  3519. // 2.) z <= ~0.775075
  3520. // 3.) s_inv = 1.0/s (precomputed for outside reuse)
  3521. // Returns: A series representation for the lower incomplete gamma
  3522. // function for small s and small z (4 terms).
  3523. // The actual "rolled up" summation looks like:
  3524. // last_sign = 1.0; last_pow = 1.0; last_factorial = 1.0;
  3525. // sum = last_sign * last_pow / ((s + k) * last_factorial)
  3526. // for(int i = 0; i < 4; ++i)
  3527. // {
  3528. // last_sign *= -1.0; last_pow *= z; last_factorial *= i;
  3529. // sum += last_sign * last_pow / ((s + k) * last_factorial);
  3530. // }
  3531. // Unrolled, constant-unfolded and arranged for madds and parallelism:
  3532. const float4 scale = pow(z, s);
  3533. float4 sum = s_inv; // Summation iteration 0 result
  3534. // Summation iterations 1, 2, and 3:
  3535. const float4 z_sq = z*z;
  3536. const float4 denom1 = s + float4(1.0);
  3537. const float4 denom2 = 2.0*s + float4(4.0);
  3538. const float4 denom3 = 6.0*s + float4(18.0);
  3539. //float4 denom4 = 24.0*s + float4(96.0);
  3540. sum -= z/denom1;
  3541. sum += z_sq/denom2;
  3542. sum -= z * z_sq/denom3;
  3543. //sum += z_sq * z_sq / denom4;
  3544. // Scale and return:
  3545. return scale * sum;
  3546. }
  3547. float3 ligamma_small_z_impl(const float3 s, const float3 z, const float3 s_inv)
  3548. {
  3549. // Float3 version:
  3550. const float3 scale = pow(z, s);
  3551. float3 sum = s_inv;
  3552. const float3 z_sq = z*z;
  3553. const float3 denom1 = s + float3(1.0);
  3554. const float3 denom2 = 2.0*s + float3(4.0);
  3555. const float3 denom3 = 6.0*s + float3(18.0);
  3556. sum -= z/denom1;
  3557. sum += z_sq/denom2;
  3558. sum -= z * z_sq/denom3;
  3559. return scale * sum;
  3560. }
  3561. float2 ligamma_small_z_impl(const float2 s, const float2 z, const float2 s_inv)
  3562. {
  3563. // Float2 version:
  3564. const float2 scale = pow(z, s);
  3565. float2 sum = s_inv;
  3566. const float2 z_sq = z*z;
  3567. const float2 denom1 = s + float2(1.0);
  3568. const float2 denom2 = 2.0*s + float2(4.0);
  3569. const float2 denom3 = 6.0*s + float2(18.0);
  3570. sum -= z/denom1;
  3571. sum += z_sq/denom2;
  3572. sum -= z * z_sq/denom3;
  3573. return scale * sum;
  3574. }
  3575. float ligamma_small_z_impl(const float s, const float z, const float s_inv)
  3576. {
  3577. // Float version:
  3578. const float scale = pow(z, s);
  3579. float sum = s_inv;
  3580. const float z_sq = z*z;
  3581. const float denom1 = s + 1.0;
  3582. const float denom2 = 2.0*s + 4.0;
  3583. const float denom3 = 6.0*s + 18.0;
  3584. sum -= z/denom1;
  3585. sum += z_sq/denom2;
  3586. sum -= z * z_sq/denom3;
  3587. return scale * sum;
  3588. }
  3589. // Upper incomplete gamma function for small s and large z (implementation):
  3590. float4 uigamma_large_z_impl(const float4 s, const float4 z)
  3591. {
  3592. // Requires: 1.) s < ~0.5
  3593. // 2.) z > ~0.775075
  3594. // Returns: Gauss's continued fraction representation for the upper
  3595. // incomplete gamma function (4 terms).
  3596. // The "rolled up" continued fraction looks like this. The denominator
  3597. // is truncated, and it's calculated "from the bottom up:"
  3598. // denom = float4('inf');
  3599. // float4 one = float4(1.0);
  3600. // for(int i = 4; i > 0; --i)
  3601. // {
  3602. // denom = ((i * 2.0) - one) + z - s + (i * (s - i))/denom;
  3603. // }
  3604. // Unrolled and constant-unfolded for madds and parallelism:
  3605. const float4 numerator = pow(z, s) * exp(-z);
  3606. float4 denom = float4(7.0) + z - s;
  3607. denom = float4(5.0) + z - s + (3.0*s - float4(9.0))/denom;
  3608. denom = float4(3.0) + z - s + (2.0*s - float4(4.0))/denom;
  3609. denom = float4(1.0) + z - s + (s - float4(1.0))/denom;
  3610. return numerator / denom;
  3611. }
  3612. float3 uigamma_large_z_impl(const float3 s, const float3 z)
  3613. {
  3614. // Float3 version:
  3615. const float3 numerator = pow(z, s) * exp(-z);
  3616. float3 denom = float3(7.0) + z - s;
  3617. denom = float3(5.0) + z - s + (3.0*s - float3(9.0))/denom;
  3618. denom = float3(3.0) + z - s + (2.0*s - float3(4.0))/denom;
  3619. denom = float3(1.0) + z - s + (s - float3(1.0))/denom;
  3620. return numerator / denom;
  3621. }
  3622. float2 uigamma_large_z_impl(const float2 s, const float2 z)
  3623. {
  3624. // Float2 version:
  3625. const float2 numerator = pow(z, s) * exp(-z);
  3626. float2 denom = float2(7.0) + z - s;
  3627. denom = float2(5.0) + z - s + (3.0*s - float2(9.0))/denom;
  3628. denom = float2(3.0) + z - s + (2.0*s - float2(4.0))/denom;
  3629. denom = float2(1.0) + z - s + (s - float2(1.0))/denom;
  3630. return numerator / denom;
  3631. }
  3632. float uigamma_large_z_impl(const float s, const float z)
  3633. {
  3634. // Float version:
  3635. const float numerator = pow(z, s) * exp(-z);
  3636. float denom = 7.0 + z - s;
  3637. denom = 5.0 + z - s + (3.0*s - 9.0)/denom;
  3638. denom = 3.0 + z - s + (2.0*s - 4.0)/denom;
  3639. denom = 1.0 + z - s + (s - 1.0)/denom;
  3640. return numerator / denom;
  3641. }
  3642. // Normalized lower incomplete gamma function for small s (implementation):
  3643. float4 normalized_ligamma_impl(const float4 s, const float4 z,
  3644. const float4 s_inv, const float4 gamma_s_inv)
  3645. {
  3646. // Requires: 1.) s < ~0.5
  3647. // 2.) s_inv = 1/s (precomputed for outside reuse)
  3648. // 3.) gamma_s_inv = 1/gamma(s) (precomputed for outside reuse)
  3649. // Returns: Approximate the normalized lower incomplete gamma function
  3650. // for s < 0.5. Since we only care about s < 0.5, we only need
  3651. // to evaluate two branches (not four) based on z. Each branch
  3652. // uses four terms, with a max relative error of ~0.00182. The
  3653. // branch threshold and specifics were adapted for fewer terms
  3654. // from Gil/Segura/Temme's paper here:
  3655. // http://oai.cwi.nl/oai/asset/20433/20433B.pdf
  3656. // Evaluate both branches: Real branches test slower even when available.
  3657. static const float4 thresh = float4(0.775075);
  3658. bool4 z_is_large;
  3659. z_is_large.x = z.x > thresh.x;
  3660. z_is_large.y = z.y > thresh.y;
  3661. z_is_large.z = z.z > thresh.z;
  3662. z_is_large.w = z.w > thresh.w;
  3663. const float4 large_z = float4(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3664. const float4 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3665. // Combine the results from both branches:
  3666. bool4 inverse_z_is_large = not(z_is_large);
  3667. return large_z * float4(z_is_large) + small_z * float4(inverse_z_is_large);
  3668. }
  3669. float3 normalized_ligamma_impl(const float3 s, const float3 z,
  3670. const float3 s_inv, const float3 gamma_s_inv)
  3671. {
  3672. // Float3 version:
  3673. static const float3 thresh = float3(0.775075);
  3674. bool3 z_is_large;
  3675. z_is_large.x = z.x > thresh.x;
  3676. z_is_large.y = z.y > thresh.y;
  3677. z_is_large.z = z.z > thresh.z;
  3678. const float3 large_z = float3(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3679. const float3 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3680. bool3 inverse_z_is_large = not(z_is_large);
  3681. return large_z * float3(z_is_large) + small_z * float3(inverse_z_is_large);
  3682. }
  3683. float2 normalized_ligamma_impl(const float2 s, const float2 z,
  3684. const float2 s_inv, const float2 gamma_s_inv)
  3685. {
  3686. // Float2 version:
  3687. static const float2 thresh = float2(0.775075);
  3688. bool2 z_is_large;
  3689. z_is_large.x = z.x > thresh.x;
  3690. z_is_large.y = z.y > thresh.y;
  3691. const float2 large_z = float2(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3692. const float2 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3693. bool2 inverse_z_is_large = not(z_is_large);
  3694. return large_z * float2(z_is_large) + small_z * float2(inverse_z_is_large);
  3695. }
  3696. float normalized_ligamma_impl(const float s, const float z,
  3697. const float s_inv, const float gamma_s_inv)
  3698. {
  3699. // Float version:
  3700. static const float thresh = 0.775075;
  3701. const bool z_is_large = z > thresh;
  3702. const float large_z = 1.0 - uigamma_large_z_impl(s, z) * gamma_s_inv;
  3703. const float small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  3704. return large_z * float(z_is_large) + small_z * float(!z_is_large);
  3705. }
  3706. // Normalized lower incomplete gamma function for small s:
  3707. float4 normalized_ligamma(const float4 s, const float4 z)
  3708. {
  3709. // Requires: s < ~0.5
  3710. // Returns: Approximate the normalized lower incomplete gamma function
  3711. // for s < 0.5. See normalized_ligamma_impl() for details.
  3712. const float4 s_inv = float4(1.0)/s;
  3713. const float4 gamma_s_inv = float4(1.0)/gamma_impl(s, s_inv);
  3714. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3715. }
  3716. float3 normalized_ligamma(const float3 s, const float3 z)
  3717. {
  3718. // Float3 version:
  3719. const float3 s_inv = float3(1.0)/s;
  3720. const float3 gamma_s_inv = float3(1.0)/gamma_impl(s, s_inv);
  3721. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3722. }
  3723. float2 normalized_ligamma(const float2 s, const float2 z)
  3724. {
  3725. // Float2 version:
  3726. const float2 s_inv = float2(1.0)/s;
  3727. const float2 gamma_s_inv = float2(1.0)/gamma_impl(s, s_inv);
  3728. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3729. }
  3730. float normalized_ligamma(const float s, const float z)
  3731. {
  3732. // Float version:
  3733. const float s_inv = 1.0/s;
  3734. const float gamma_s_inv = 1.0/gamma_impl(s, s_inv);
  3735. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  3736. }
  3737. #endif // SPECIAL_FUNCTIONS_H
  3738. //////////////////////////// END SPECIAL-FUNCTIONS ///////////////////////////
  3739. //#include "../../../../include/gamma-management.h"
  3740. //////////////////////////// BEGIN GAMMA-MANAGEMENT //////////////////////////
  3741. #ifndef GAMMA_MANAGEMENT_H
  3742. #define GAMMA_MANAGEMENT_H
  3743. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  3744. // Copyright (C) 2014 TroggleMonkey
  3745. //
  3746. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3747. // of this software and associated documentation files (the "Software"), to
  3748. // deal in the Software without restriction, including without limitation the
  3749. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  3750. // sell copies of the Software, and to permit persons to whom the Software is
  3751. // furnished to do so, subject to the following conditions:
  3752. //
  3753. // The above copyright notice and this permission notice shall be included in
  3754. // all copies or substantial portions of the Software.
  3755. //
  3756. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3757. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3758. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3759. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3760. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3761. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3762. // IN THE SOFTWARE.
  3763. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  3764. // This file provides gamma-aware tex*D*() and encode_output() functions.
  3765. // Requires: Before #include-ing this file, the including file must #define
  3766. // the following macros when applicable and follow their rules:
  3767. // 1.) #define FIRST_PASS if this is the first pass.
  3768. // 2.) #define LAST_PASS if this is the last pass.
  3769. // 3.) If sRGB is available, set srgb_framebufferN = "true" for
  3770. // every pass except the last in your .cgp preset.
  3771. // 4.) If sRGB isn't available but you want gamma-correctness with
  3772. // no banding, #define GAMMA_ENCODE_EVERY_FBO each pass.
  3773. // 5.) #define SIMULATE_CRT_ON_LCD if desired (precedence over 5-7)
  3774. // 6.) #define SIMULATE_GBA_ON_LCD if desired (precedence over 6-7)
  3775. // 7.) #define SIMULATE_LCD_ON_CRT if desired (precedence over 7)
  3776. // 8.) #define SIMULATE_GBA_ON_CRT if desired (precedence over -)
  3777. // If an option in [5, 8] is #defined in the first or last pass, it
  3778. // should be #defined for both. It shouldn't make a difference
  3779. // whether it's #defined for intermediate passes or not.
  3780. // Optional: The including file (or an earlier included file) may optionally
  3781. // #define a number of macros indicating it will override certain
  3782. // macros and associated constants are as follows:
  3783. // static constants with either static or uniform constants. The
  3784. // 1.) OVERRIDE_STANDARD_GAMMA: The user must first define:
  3785. // static const float ntsc_gamma
  3786. // static const float pal_gamma
  3787. // static const float crt_reference_gamma_high
  3788. // static const float crt_reference_gamma_low
  3789. // static const float lcd_reference_gamma
  3790. // static const float crt_office_gamma
  3791. // static const float lcd_office_gamma
  3792. // 2.) OVERRIDE_DEVICE_GAMMA: The user must first define:
  3793. // static const float crt_gamma
  3794. // static const float gba_gamma
  3795. // static const float lcd_gamma
  3796. // 3.) OVERRIDE_FINAL_GAMMA: The user must first define:
  3797. // static const float input_gamma
  3798. // static const float intermediate_gamma
  3799. // static const float output_gamma
  3800. // (intermediate_gamma is for GAMMA_ENCODE_EVERY_FBO.)
  3801. // 4.) OVERRIDE_ALPHA_ASSUMPTIONS: The user must first define:
  3802. // static const bool assume_opaque_alpha
  3803. // The gamma constant overrides must be used in every pass or none,
  3804. // and OVERRIDE_FINAL_GAMMA bypasses all of the SIMULATE* macros.
  3805. // OVERRIDE_ALPHA_ASSUMPTIONS may be set on a per-pass basis.
  3806. // Usage: After setting macros appropriately, ignore gamma correction and
  3807. // replace all tex*D*() calls with equivalent gamma-aware
  3808. // tex*D*_linearize calls, except:
  3809. // 1.) When you read an LUT, use regular tex*D or a gamma-specified
  3810. // function, depending on its gamma encoding:
  3811. // tex*D*_linearize_gamma (takes a runtime gamma parameter)
  3812. // 2.) If you must read pass0's original input in a later pass, use
  3813. // tex2D_linearize_ntsc_gamma. If you want to read pass0's
  3814. // input with gamma-corrected bilinear filtering, consider
  3815. // creating a first linearizing pass and reading from the input
  3816. // of pass1 later.
  3817. // Then, return encode_output(color) from every fragment shader.
  3818. // Finally, use the global gamma_aware_bilinear boolean if you want
  3819. // to statically branch based on whether bilinear filtering is
  3820. // gamma-correct or not (e.g. for placing Gaussian blur samples).
  3821. //
  3822. // Detailed Policy:
  3823. // tex*D*_linearize() functions enforce a consistent gamma-management policy
  3824. // based on the FIRST_PASS and GAMMA_ENCODE_EVERY_FBO settings. They assume
  3825. // their input texture has the same encoding characteristics as the input for
  3826. // the current pass (which doesn't apply to the exceptions listed above).
  3827. // Similarly, encode_output() enforces a policy based on the LAST_PASS and
  3828. // GAMMA_ENCODE_EVERY_FBO settings. Together, they result in one of the
  3829. // following two pipelines.
  3830. // Typical pipeline with intermediate sRGB framebuffers:
  3831. // linear_color = pow(pass0_encoded_color, input_gamma);
  3832. // intermediate_output = linear_color; // Automatic sRGB encoding
  3833. // linear_color = intermediate_output; // Automatic sRGB decoding
  3834. // final_output = pow(intermediate_output, 1.0/output_gamma);
  3835. // Typical pipeline without intermediate sRGB framebuffers:
  3836. // linear_color = pow(pass0_encoded_color, input_gamma);
  3837. // intermediate_output = pow(linear_color, 1.0/intermediate_gamma);
  3838. // linear_color = pow(intermediate_output, intermediate_gamma);
  3839. // final_output = pow(intermediate_output, 1.0/output_gamma);
  3840. // Using GAMMA_ENCODE_EVERY_FBO is much slower, but it's provided as a way to
  3841. // easily get gamma-correctness without banding on devices where sRGB isn't
  3842. // supported.
  3843. //
  3844. // Use This Header to Maximize Code Reuse:
  3845. // The purpose of this header is to provide a consistent interface for texture
  3846. // reads and output gamma-encoding that localizes and abstracts away all the
  3847. // annoying details. This greatly reduces the amount of code in each shader
  3848. // pass that depends on the pass number in the .cgp preset or whether sRGB
  3849. // FBO's are being used: You can trivially change the gamma behavior of your
  3850. // whole pass by commenting or uncommenting 1-3 #defines. To reuse the same
  3851. // code in your first, Nth, and last passes, you can even put it all in another
  3852. // header file and #include it from skeleton .cg files that #define the
  3853. // appropriate pass-specific settings.
  3854. //
  3855. // Rationale for Using Three Macros:
  3856. // This file uses GAMMA_ENCODE_EVERY_FBO instead of an opposite macro like
  3857. // SRGB_PIPELINE to ensure sRGB is assumed by default, which hopefully imposes
  3858. // a lower maintenance burden on each pass. At first glance it seems we could
  3859. // accomplish everything with two macros: GAMMA_CORRECT_IN / GAMMA_CORRECT_OUT.
  3860. // This works for simple use cases where input_gamma == output_gamma, but it
  3861. // breaks down for more complex scenarios like CRT simulation, where the pass
  3862. // number determines the gamma encoding of the input and output.
  3863. /////////////////////////////// BASE CONSTANTS ///////////////////////////////
  3864. // Set standard gamma constants, but allow users to override them:
  3865. #ifndef OVERRIDE_STANDARD_GAMMA
  3866. // Standard encoding gammas:
  3867. static const float ntsc_gamma = 2.2; // Best to use NTSC for PAL too?
  3868. static const float pal_gamma = 2.8; // Never actually 2.8 in practice
  3869. // Typical device decoding gammas (only use for emulating devices):
  3870. // CRT/LCD reference gammas are higher than NTSC and Rec.709 video standard
  3871. // gammas: The standards purposely undercorrected for an analog CRT's
  3872. // assumed 2.5 reference display gamma to maintain contrast in assumed
  3873. // [dark] viewing conditions: http://www.poynton.com/PDFs/GammaFAQ.pdf
  3874. // These unstated assumptions about display gamma and perceptual rendering
  3875. // intent caused a lot of confusion, and more modern CRT's seemed to target
  3876. // NTSC 2.2 gamma with circuitry. LCD displays seem to have followed suit
  3877. // (they struggle near black with 2.5 gamma anyway), especially PC/laptop
  3878. // displays designed to view sRGB in bright environments. (Standards are
  3879. // also in flux again with BT.1886, but it's underspecified for displays.)
  3880. static const float crt_reference_gamma_high = 2.5; // In (2.35, 2.55)
  3881. static const float crt_reference_gamma_low = 2.35; // In (2.35, 2.55)
  3882. static const float lcd_reference_gamma = 2.5; // To match CRT
  3883. static const float crt_office_gamma = 2.2; // Circuitry-adjusted for NTSC
  3884. static const float lcd_office_gamma = 2.2; // Approximates sRGB
  3885. #endif // OVERRIDE_STANDARD_GAMMA
  3886. // Assuming alpha == 1.0 might make it easier for users to avoid some bugs,
  3887. // but only if they're aware of it.
  3888. #ifndef OVERRIDE_ALPHA_ASSUMPTIONS
  3889. static const bool assume_opaque_alpha = false;
  3890. #endif
  3891. /////////////////////// DERIVED CONSTANTS AS FUNCTIONS ///////////////////////
  3892. // gamma-management.h should be compatible with overriding gamma values with
  3893. // runtime user parameters, but we can only define other global constants in
  3894. // terms of static constants, not uniform user parameters. To get around this
  3895. // limitation, we need to define derived constants using functions.
  3896. // Set device gamma constants, but allow users to override them:
  3897. #ifdef OVERRIDE_DEVICE_GAMMA
  3898. // The user promises to globally define the appropriate constants:
  3899. inline float get_crt_gamma() { return crt_gamma; }
  3900. inline float get_gba_gamma() { return gba_gamma; }
  3901. inline float get_lcd_gamma() { return lcd_gamma; }
  3902. #else
  3903. inline float get_crt_gamma() { return crt_reference_gamma_high; }
  3904. inline float get_gba_gamma() { return 3.5; } // Game Boy Advance; in (3.0, 4.0)
  3905. inline float get_lcd_gamma() { return lcd_office_gamma; }
  3906. #endif // OVERRIDE_DEVICE_GAMMA
  3907. // Set decoding/encoding gammas for the first/lass passes, but allow overrides:
  3908. #ifdef OVERRIDE_FINAL_GAMMA
  3909. // The user promises to globally define the appropriate constants:
  3910. inline float get_intermediate_gamma() { return intermediate_gamma; }
  3911. inline float get_input_gamma() { return input_gamma; }
  3912. inline float get_output_gamma() { return output_gamma; }
  3913. #else
  3914. // If we gamma-correct every pass, always use ntsc_gamma between passes to
  3915. // ensure middle passes don't need to care if anything is being simulated:
  3916. inline float get_intermediate_gamma() { return ntsc_gamma; }
  3917. #ifdef SIMULATE_CRT_ON_LCD
  3918. inline float get_input_gamma() { return get_crt_gamma(); }
  3919. inline float get_output_gamma() { return get_lcd_gamma(); }
  3920. #else
  3921. #ifdef SIMULATE_GBA_ON_LCD
  3922. inline float get_input_gamma() { return get_gba_gamma(); }
  3923. inline float get_output_gamma() { return get_lcd_gamma(); }
  3924. #else
  3925. #ifdef SIMULATE_LCD_ON_CRT
  3926. inline float get_input_gamma() { return get_lcd_gamma(); }
  3927. inline float get_output_gamma() { return get_crt_gamma(); }
  3928. #else
  3929. #ifdef SIMULATE_GBA_ON_CRT
  3930. inline float get_input_gamma() { return get_gba_gamma(); }
  3931. inline float get_output_gamma() { return get_crt_gamma(); }
  3932. #else // Don't simulate anything:
  3933. inline float get_input_gamma() { return ntsc_gamma; }
  3934. inline float get_output_gamma() { return ntsc_gamma; }
  3935. #endif // SIMULATE_GBA_ON_CRT
  3936. #endif // SIMULATE_LCD_ON_CRT
  3937. #endif // SIMULATE_GBA_ON_LCD
  3938. #endif // SIMULATE_CRT_ON_LCD
  3939. #endif // OVERRIDE_FINAL_GAMMA
  3940. // Set decoding/encoding gammas for the current pass. Use static constants for
  3941. // linearize_input and gamma_encode_output, because they aren't derived, and
  3942. // they let the compiler do dead-code elimination.
  3943. #ifndef GAMMA_ENCODE_EVERY_FBO
  3944. #ifdef FIRST_PASS
  3945. static const bool linearize_input = true;
  3946. inline float get_pass_input_gamma() { return get_input_gamma(); }
  3947. #else
  3948. static const bool linearize_input = false;
  3949. inline float get_pass_input_gamma() { return 1.0; }
  3950. #endif
  3951. #ifdef LAST_PASS
  3952. static const bool gamma_encode_output = true;
  3953. inline float get_pass_output_gamma() { return get_output_gamma(); }
  3954. #else
  3955. static const bool gamma_encode_output = false;
  3956. inline float get_pass_output_gamma() { return 1.0; }
  3957. #endif
  3958. #else
  3959. static const bool linearize_input = true;
  3960. static const bool gamma_encode_output = true;
  3961. #ifdef FIRST_PASS
  3962. inline float get_pass_input_gamma() { return get_input_gamma(); }
  3963. #else
  3964. inline float get_pass_input_gamma() { return get_intermediate_gamma(); }
  3965. #endif
  3966. #ifdef LAST_PASS
  3967. inline float get_pass_output_gamma() { return get_output_gamma(); }
  3968. #else
  3969. inline float get_pass_output_gamma() { return get_intermediate_gamma(); }
  3970. #endif
  3971. #endif
  3972. // Users might want to know if bilinear filtering will be gamma-correct:
  3973. static const bool gamma_aware_bilinear = !linearize_input;
  3974. ////////////////////// COLOR ENCODING/DECODING FUNCTIONS /////////////////////
  3975. inline float4 encode_output(const float4 color)
  3976. {
  3977. if(gamma_encode_output)
  3978. {
  3979. if(assume_opaque_alpha)
  3980. {
  3981. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), 1.0);
  3982. }
  3983. else
  3984. {
  3985. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), color.a);
  3986. }
  3987. }
  3988. else
  3989. {
  3990. return color;
  3991. }
  3992. }
  3993. inline float4 decode_input(const float4 color)
  3994. {
  3995. if(linearize_input)
  3996. {
  3997. if(assume_opaque_alpha)
  3998. {
  3999. return float4(pow(color.rgb, float3(get_pass_input_gamma())), 1.0);
  4000. }
  4001. else
  4002. {
  4003. return float4(pow(color.rgb, float3(get_pass_input_gamma())), color.a);
  4004. }
  4005. }
  4006. else
  4007. {
  4008. return color;
  4009. }
  4010. }
  4011. inline float4 decode_gamma_input(const float4 color, const float3 gamma)
  4012. {
  4013. if(assume_opaque_alpha)
  4014. {
  4015. return float4(pow(color.rgb, gamma), 1.0);
  4016. }
  4017. else
  4018. {
  4019. return float4(pow(color.rgb, gamma), color.a);
  4020. }
  4021. }
  4022. //TODO/FIXME: I have no idea why replacing the lookup wrappers with this macro fixes the blurs being offset ¯\_(ツ)_/¯
  4023. //#define tex2D_linearize(C, D) decode_input(vec4(texture(C, D)))
  4024. // EDIT: it's the 'const' in front of the coords that's doing it
  4025. /////////////////////////// TEXTURE LOOKUP WRAPPERS //////////////////////////
  4026. // "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  4027. // Provide a wide array of linearizing texture lookup wrapper functions. The
  4028. // Cg shader spec Retroarch uses only allows for 2D textures, but 1D and 3D
  4029. // lookups are provided for completeness in case that changes someday. Nobody
  4030. // is likely to use the *fetch and *proj functions, but they're included just
  4031. // in case. The only tex*D texture sampling functions omitted are:
  4032. // - tex*Dcmpbias
  4033. // - tex*Dcmplod
  4034. // - tex*DARRAY*
  4035. // - tex*DMS*
  4036. // - Variants returning integers
  4037. // Standard line length restrictions are ignored below for vertical brevity.
  4038. /*
  4039. // tex1D:
  4040. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords)
  4041. { return decode_input(tex1D(tex, tex_coords)); }
  4042. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords)
  4043. { return decode_input(tex1D(tex, tex_coords)); }
  4044. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const int texel_off)
  4045. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  4046. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  4047. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  4048. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy)
  4049. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  4050. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy)
  4051. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  4052. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy, const int texel_off)
  4053. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  4054. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy, const int texel_off)
  4055. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  4056. // tex1Dbias:
  4057. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords)
  4058. { return decode_input(tex1Dbias(tex, tex_coords)); }
  4059. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  4060. { return decode_input(tex1Dbias(tex, tex_coords, texel_off)); }
  4061. // tex1Dfetch:
  4062. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords)
  4063. { return decode_input(tex1Dfetch(tex, tex_coords)); }
  4064. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords, const int texel_off)
  4065. { return decode_input(tex1Dfetch(tex, tex_coords, texel_off)); }
  4066. // tex1Dlod:
  4067. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords)
  4068. { return decode_input(tex1Dlod(tex, tex_coords)); }
  4069. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  4070. { return decode_input(tex1Dlod(tex, tex_coords, texel_off)); }
  4071. // tex1Dproj:
  4072. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords)
  4073. { return decode_input(tex1Dproj(tex, tex_coords)); }
  4074. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords)
  4075. { return decode_input(tex1Dproj(tex, tex_coords)); }
  4076. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  4077. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  4078. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords, const int texel_off)
  4079. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  4080. */
  4081. // tex2D:
  4082. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords)
  4083. { return decode_input(COMPAT_TEXTURE(tex, tex_coords)); }
  4084. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords)
  4085. { return decode_input(COMPAT_TEXTURE(tex, tex_coords.xy)); }
  4086. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords, int texel_off)
  4087. { return decode_input(textureLod(tex, tex_coords, texel_off)); }
  4088. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords, int texel_off)
  4089. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  4090. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy)
  4091. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  4092. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy)
  4093. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  4094. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  4095. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  4096. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  4097. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  4098. // tex2Dbias:
  4099. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords)
  4100. //{ return decode_input(tex2Dbias(tex, tex_coords)); }
  4101. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  4102. //{ return decode_input(tex2Dbias(tex, tex_coords, texel_off)); }
  4103. // tex2Dfetch:
  4104. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords)
  4105. //{ return decode_input(tex2Dfetch(tex, tex_coords)); }
  4106. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords, const int texel_off)
  4107. //{ return decode_input(tex2Dfetch(tex, tex_coords, texel_off)); }
  4108. // tex2Dlod:
  4109. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords)
  4110. { return decode_input(textureLod(tex, tex_coords.xy, 0.0)); }
  4111. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords, int texel_off)
  4112. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  4113. /*
  4114. // tex2Dproj:
  4115. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords)
  4116. { return decode_input(tex2Dproj(tex, tex_coords)); }
  4117. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords)
  4118. { return decode_input(tex2Dproj(tex, tex_coords)); }
  4119. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords, const int texel_off)
  4120. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  4121. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  4122. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  4123. */
  4124. /*
  4125. // tex3D:
  4126. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords)
  4127. { return decode_input(tex3D(tex, tex_coords)); }
  4128. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const int texel_off)
  4129. { return decode_input(tex3D(tex, tex_coords, texel_off)); }
  4130. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy)
  4131. { return decode_input(tex3D(tex, tex_coords, dx, dy)); }
  4132. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy, const int texel_off)
  4133. { return decode_input(tex3D(tex, tex_coords, dx, dy, texel_off)); }
  4134. // tex3Dbias:
  4135. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords)
  4136. { return decode_input(tex3Dbias(tex, tex_coords)); }
  4137. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  4138. { return decode_input(tex3Dbias(tex, tex_coords, texel_off)); }
  4139. // tex3Dfetch:
  4140. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords)
  4141. { return decode_input(tex3Dfetch(tex, tex_coords)); }
  4142. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords, const int texel_off)
  4143. { return decode_input(tex3Dfetch(tex, tex_coords, texel_off)); }
  4144. // tex3Dlod:
  4145. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords)
  4146. { return decode_input(tex3Dlod(tex, tex_coords)); }
  4147. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  4148. { return decode_input(tex3Dlod(tex, tex_coords, texel_off)); }
  4149. // tex3Dproj:
  4150. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords)
  4151. { return decode_input(tex3Dproj(tex, tex_coords)); }
  4152. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  4153. { return decode_input(tex3Dproj(tex, tex_coords, texel_off)); }
  4154. /////////*
  4155. // NONSTANDARD "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  4156. // This narrow selection of nonstandard tex2D* functions can be useful:
  4157. // tex2Dlod0: Automatically fill in the tex2D LOD parameter for mip level 0.
  4158. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords)
  4159. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0))); }
  4160. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords, const int texel_off)
  4161. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0), texel_off)); }
  4162. // MANUALLY LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  4163. // Provide a narrower selection of tex2D* wrapper functions that decode an
  4164. // input sample with a specified gamma value. These are useful for reading
  4165. // LUT's and for reading the input of pass0 in a later pass.
  4166. // tex2D:
  4167. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float3 gamma)
  4168. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords), gamma); }
  4169. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float3 gamma)
  4170. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords.xy), gamma); }
  4171. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const int texel_off, const float3 gamma)
  4172. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  4173. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const int texel_off, const float3 gamma)
  4174. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  4175. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  4176. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  4177. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  4178. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  4179. //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)
  4180. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  4181. //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)
  4182. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  4183. /*
  4184. // tex2Dbias:
  4185. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const float3 gamma)
  4186. { return decode_gamma_input(tex2Dbias(tex, tex_coords), gamma); }
  4187. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const int texel_off, const float3 gamma)
  4188. { return decode_gamma_input(tex2Dbias(tex, tex_coords, texel_off), gamma); }
  4189. // tex2Dfetch:
  4190. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const float3 gamma)
  4191. { return decode_gamma_input(tex2Dfetch(tex, tex_coords), gamma); }
  4192. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const int texel_off, const float3 gamma)
  4193. { return decode_gamma_input(tex2Dfetch(tex, tex_coords, texel_off), gamma); }
  4194. */
  4195. // tex2Dlod:
  4196. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, float3 gamma)
  4197. { return decode_gamma_input(textureLod(tex, tex_coords.xy, 0.0), gamma); }
  4198. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, int texel_off, float3 gamma)
  4199. { return decode_gamma_input(textureLod(tex, tex_coords.xy, texel_off), gamma); }
  4200. #endif // GAMMA_MANAGEMENT_H
  4201. //////////////////////////// END GAMMA-MANAGEMENT //////////////////////////
  4202. //////////////////////////////// END INCLUDES ////////////////////////////////
  4203. ///////////////////////////// SCANLINE FUNCTIONS /////////////////////////////
  4204. inline float3 get_gaussian_sigma(const float3 color, const float sigma_range)
  4205. {
  4206. // Requires: Globals:
  4207. // 1.) beam_min_sigma and beam_max_sigma are global floats
  4208. // containing the desired minimum and maximum beam standard
  4209. // deviations, for dim and bright colors respectively.
  4210. // 2.) beam_max_sigma must be > 0.0
  4211. // 3.) beam_min_sigma must be in (0.0, beam_max_sigma]
  4212. // 4.) beam_spot_power must be defined as a global float.
  4213. // Parameters:
  4214. // 1.) color is the underlying source color along a scanline
  4215. // 2.) sigma_range = beam_max_sigma - beam_min_sigma; we take
  4216. // sigma_range as a parameter to avoid repeated computation
  4217. // when beam_{min, max}_sigma are runtime shader parameters
  4218. // Optional: Users may set beam_spot_shape_function to 1 to define the
  4219. // inner f(color) subfunction (see below) as:
  4220. // f(color) = sqrt(1.0 - (color - 1.0)*(color - 1.0))
  4221. // Otherwise (technically, if beam_spot_shape_function < 0.5):
  4222. // f(color) = pow(color, beam_spot_power)
  4223. // Returns: The standard deviation of the Gaussian beam for "color:"
  4224. // sigma = beam_min_sigma + sigma_range * f(color)
  4225. // Details/Discussion:
  4226. // The beam's spot shape vaguely resembles an aspect-corrected f() in the
  4227. // range [0, 1] (not quite, but it's related). f(color) = color makes
  4228. // spots look like diamonds, and a spherical function or cube balances
  4229. // between variable width and a soft/realistic shape. A beam_spot_power
  4230. // > 1.0 can produce an ugly spot shape and more initial clipping, but the
  4231. // final shape also differs based on the horizontal resampling filter and
  4232. // the phosphor bloom. For instance, resampling horizontally in nonlinear
  4233. // light and/or with a sharp (e.g. Lanczos) filter will sharpen the spot
  4234. // shape, but a sixth root is still quite soft. A power function (default
  4235. // 1.0/3.0 beam_spot_power) is most flexible, but a fixed spherical curve
  4236. // has the highest variability without an awful spot shape.
  4237. //
  4238. // beam_min_sigma affects scanline sharpness/aliasing in dim areas, and its
  4239. // difference from beam_max_sigma affects beam width variability. It only
  4240. // affects clipping [for pure Gaussians] if beam_spot_power > 1.0 (which is
  4241. // a conservative estimate for a more complex constraint).
  4242. //
  4243. // beam_max_sigma affects clipping and increasing scanline width/softness
  4244. // as color increases. The wider this is, the more scanlines need to be
  4245. // evaluated to avoid distortion. For a pure Gaussian, the max_beam_sigma
  4246. // at which the first unused scanline always has a weight < 1.0/255.0 is:
  4247. // num scanlines = 2, max_beam_sigma = 0.2089; distortions begin ~0.34
  4248. // num scanlines = 3, max_beam_sigma = 0.3879; distortions begin ~0.52
  4249. // num scanlines = 4, max_beam_sigma = 0.5723; distortions begin ~0.70
  4250. // num scanlines = 5, max_beam_sigma = 0.7591; distortions begin ~0.89
  4251. // num scanlines = 6, max_beam_sigma = 0.9483; distortions begin ~1.08
  4252. // Generalized Gaussians permit more leeway here as steepness increases.
  4253. if(beam_spot_shape_function < 0.5)
  4254. {
  4255. // Use a power function:
  4256. return float3(beam_min_sigma) + sigma_range *
  4257. pow(color, float3(beam_spot_power));
  4258. }
  4259. else
  4260. {
  4261. // Use a spherical function:
  4262. const float3 color_minus_1 = color - float3(1.0);
  4263. return float3(beam_min_sigma) + sigma_range *
  4264. sqrt(float3(1.0) - color_minus_1*color_minus_1);
  4265. }
  4266. }
  4267. inline float3 get_generalized_gaussian_beta(const float3 color,
  4268. const float shape_range)
  4269. {
  4270. // Requires: Globals:
  4271. // 1.) beam_min_shape and beam_max_shape are global floats
  4272. // containing the desired min/max generalized Gaussian
  4273. // beta parameters, for dim and bright colors respectively.
  4274. // 2.) beam_max_shape must be >= 2.0
  4275. // 3.) beam_min_shape must be in [2.0, beam_max_shape]
  4276. // 4.) beam_shape_power must be defined as a global float.
  4277. // Parameters:
  4278. // 1.) color is the underlying source color along a scanline
  4279. // 2.) shape_range = beam_max_shape - beam_min_shape; we take
  4280. // shape_range as a parameter to avoid repeated computation
  4281. // when beam_{min, max}_shape are runtime shader parameters
  4282. // Returns: The type-I generalized Gaussian "shape" parameter beta for
  4283. // the given color.
  4284. // Details/Discussion:
  4285. // Beta affects the scanline distribution as follows:
  4286. // a.) beta < 2.0 narrows the peak to a spike with a discontinuous slope
  4287. // b.) beta == 2.0 just degenerates to a Gaussian
  4288. // c.) beta > 2.0 flattens and widens the peak, then drops off more steeply
  4289. // than a Gaussian. Whereas high sigmas widen and soften peaks, high
  4290. // beta widen and sharpen peaks at the risk of aliasing.
  4291. // Unlike high beam_spot_powers, high beam_shape_powers actually soften shape
  4292. // transitions, whereas lower ones sharpen them (at the risk of aliasing).
  4293. return beam_min_shape + shape_range * pow(color, float3(beam_shape_power));
  4294. }
  4295. float3 scanline_gaussian_integral_contrib(const float3 dist,
  4296. const float3 color, const float pixel_height, const float sigma_range)
  4297. {
  4298. // Requires: 1.) dist is the distance of the [potentially separate R/G/B]
  4299. // point(s) from a scanline in units of scanlines, where
  4300. // 1.0 means the sample point straddles the next scanline.
  4301. // 2.) color is the underlying source color along a scanline.
  4302. // 3.) pixel_height is the output pixel height in scanlines.
  4303. // 4.) Requirements of get_gaussian_sigma() must be met.
  4304. // Returns: Return a scanline's light output over a given pixel.
  4305. // Details:
  4306. // The CRT beam profile follows a roughly Gaussian distribution which is
  4307. // wider for bright colors than dark ones. The integral over the full
  4308. // range of a Gaussian function is always 1.0, so we can vary the beam
  4309. // with a standard deviation without affecting brightness. 'x' = distance:
  4310. // gaussian sample = 1/(sigma*sqrt(2*pi)) * e**(-(x**2)/(2*sigma**2))
  4311. // gaussian integral = 0.5 (1.0 + erf(x/(sigma * sqrt(2))))
  4312. // Use a numerical approximation of the "error function" (the Gaussian
  4313. // indefinite integral) to find the definite integral of the scanline's
  4314. // average brightness over a given pixel area. Even if curved coords were
  4315. // used in this pass, a flat scalar pixel height works almost as well as a
  4316. // pixel height computed from a full pixel-space to scanline-space matrix.
  4317. const float3 sigma = get_gaussian_sigma(color, sigma_range);
  4318. const float3 ph_offset = float3(pixel_height * 0.5);
  4319. const float3 denom_inv = 1.0/(sigma*sqrt(2.0));
  4320. const float3 integral_high = erf((dist + ph_offset)*denom_inv);
  4321. const float3 integral_low = erf((dist - ph_offset)*denom_inv);
  4322. return color * 0.5*(integral_high - integral_low)/pixel_height;
  4323. }
  4324. float3 scanline_generalized_gaussian_integral_contrib(float3 dist,
  4325. float3 color, float pixel_height, float sigma_range,
  4326. float shape_range)
  4327. {
  4328. // Requires: 1.) Requirements of scanline_gaussian_integral_contrib()
  4329. // must be met.
  4330. // 2.) Requirements of get_gaussian_sigma() must be met.
  4331. // 3.) Requirements of get_generalized_gaussian_beta() must be
  4332. // met.
  4333. // Returns: Return a scanline's light output over a given pixel.
  4334. // A generalized Gaussian distribution allows the shape (beta) to vary
  4335. // as well as the width (alpha). "gamma" refers to the gamma function:
  4336. // generalized sample =
  4337. // beta/(2*alpha*gamma(1/beta)) * e**(-(|x|/alpha)**beta)
  4338. // ligamma(s, z) is the lower incomplete gamma function, for which we only
  4339. // implement two of four branches (because we keep 1/beta <= 0.5):
  4340. // generalized integral = 0.5 + 0.5* sign(x) *
  4341. // ligamma(1/beta, (|x|/alpha)**beta)/gamma(1/beta)
  4342. // See get_generalized_gaussian_beta() for a discussion of beta.
  4343. // We base alpha on the intended Gaussian sigma, but it only strictly
  4344. // models models standard deviation at beta == 2, because the standard
  4345. // deviation depends on both alpha and beta (keeping alpha independent is
  4346. // faster and preserves intuitive behavior and a full spectrum of results).
  4347. const float3 alpha = sqrt(2.0) * get_gaussian_sigma(color, sigma_range);
  4348. const float3 beta = get_generalized_gaussian_beta(color, shape_range);
  4349. const float3 alpha_inv = float3(1.0)/alpha;
  4350. const float3 s = float3(1.0)/beta;
  4351. const float3 ph_offset = float3(pixel_height * 0.5);
  4352. // Pass beta to gamma_impl to avoid repeated divides. Similarly pass
  4353. // beta (i.e. 1/s) and 1/gamma(s) to normalized_ligamma_impl.
  4354. const float3 gamma_s_inv = float3(1.0)/gamma_impl(s, beta);
  4355. const float3 dist1 = dist + ph_offset;
  4356. const float3 dist0 = dist - ph_offset;
  4357. const float3 integral_high = sign(dist1) * normalized_ligamma_impl(
  4358. s, pow(abs(dist1)*alpha_inv, beta), beta, gamma_s_inv);
  4359. const float3 integral_low = sign(dist0) * normalized_ligamma_impl(
  4360. s, pow(abs(dist0)*alpha_inv, beta), beta, gamma_s_inv);
  4361. return color * 0.5*(integral_high - integral_low)/pixel_height;
  4362. }
  4363. float3 scanline_gaussian_sampled_contrib(const float3 dist, const float3 color,
  4364. const float pixel_height, const float sigma_range)
  4365. {
  4366. // See scanline_gaussian integral_contrib() for detailed comments!
  4367. // gaussian sample = 1/(sigma*sqrt(2*pi)) * e**(-(x**2)/(2*sigma**2))
  4368. const float3 sigma = get_gaussian_sigma(color, sigma_range);
  4369. // Avoid repeated divides:
  4370. const float3 sigma_inv = float3(1.0)/sigma;
  4371. const float3 inner_denom_inv = 0.5 * sigma_inv * sigma_inv;
  4372. const float3 outer_denom_inv = sigma_inv/sqrt(2.0*pi);
  4373. if(beam_antialias_level > 0.5)
  4374. {
  4375. // Sample 1/3 pixel away in each direction as well:
  4376. const float3 sample_offset = float3(pixel_height/3.0);
  4377. const float3 dist2 = dist + sample_offset;
  4378. const float3 dist3 = abs(dist - sample_offset);
  4379. // Average three pure Gaussian samples:
  4380. const float3 scale = color/3.0 * outer_denom_inv;
  4381. const float3 weight1 = exp(-(dist*dist)*inner_denom_inv);
  4382. const float3 weight2 = exp(-(dist2*dist2)*inner_denom_inv);
  4383. const float3 weight3 = exp(-(dist3*dist3)*inner_denom_inv);
  4384. return scale * (weight1 + weight2 + weight3);
  4385. }
  4386. else
  4387. {
  4388. return color*exp(-(dist*dist)*inner_denom_inv)*outer_denom_inv;
  4389. }
  4390. }
  4391. float3 scanline_generalized_gaussian_sampled_contrib(float3 dist,
  4392. float3 color, float pixel_height, float sigma_range,
  4393. float shape_range)
  4394. {
  4395. // See scanline_generalized_gaussian_integral_contrib() for details!
  4396. // generalized sample =
  4397. // beta/(2*alpha*gamma(1/beta)) * e**(-(|x|/alpha)**beta)
  4398. const float3 alpha = sqrt(2.0) * get_gaussian_sigma(color, sigma_range);
  4399. const float3 beta = get_generalized_gaussian_beta(color, shape_range);
  4400. // Avoid repeated divides:
  4401. const float3 alpha_inv = float3(1.0)/alpha;
  4402. const float3 beta_inv = float3(1.0)/beta;
  4403. const float3 scale = color * beta * 0.5 * alpha_inv /
  4404. gamma_impl(beta_inv, beta);
  4405. if(beam_antialias_level > 0.5)
  4406. {
  4407. // Sample 1/3 pixel closer to and farther from the scanline too.
  4408. const float3 sample_offset = float3(pixel_height/3.0);
  4409. const float3 dist2 = dist + sample_offset;
  4410. const float3 dist3 = abs(dist - sample_offset);
  4411. // Average three generalized Gaussian samples:
  4412. const float3 weight1 = exp(-pow(abs(dist*alpha_inv), beta));
  4413. const float3 weight2 = exp(-pow(abs(dist2*alpha_inv), beta));
  4414. const float3 weight3 = exp(-pow(abs(dist3*alpha_inv), beta));
  4415. return scale/3.0 * (weight1 + weight2 + weight3);
  4416. }
  4417. else
  4418. {
  4419. return scale * exp(-pow(abs(dist*alpha_inv), beta));
  4420. }
  4421. }
  4422. inline float3 scanline_contrib(float3 dist, float3 color,
  4423. float pixel_height, const float sigma_range, const float shape_range)
  4424. {
  4425. // Requires: 1.) Requirements of scanline_gaussian_integral_contrib()
  4426. // must be met.
  4427. // 2.) Requirements of get_gaussian_sigma() must be met.
  4428. // 3.) Requirements of get_generalized_gaussian_beta() must be
  4429. // met.
  4430. // Returns: Return a scanline's light output over a given pixel, using
  4431. // a generalized or pure Gaussian distribution and sampling or
  4432. // integrals as desired by user codepath choices.
  4433. if(beam_generalized_gaussian)
  4434. {
  4435. if(beam_antialias_level > 1.5)
  4436. {
  4437. return scanline_generalized_gaussian_integral_contrib(
  4438. dist, color, pixel_height, sigma_range, shape_range);
  4439. }
  4440. else
  4441. {
  4442. return scanline_generalized_gaussian_sampled_contrib(
  4443. dist, color, pixel_height, sigma_range, shape_range);
  4444. }
  4445. }
  4446. else
  4447. {
  4448. if(beam_antialias_level > 1.5)
  4449. {
  4450. return scanline_gaussian_integral_contrib(
  4451. dist, color, pixel_height, sigma_range);
  4452. }
  4453. else
  4454. {
  4455. return scanline_gaussian_sampled_contrib(
  4456. dist, color, pixel_height, sigma_range);
  4457. }
  4458. }
  4459. }
  4460. inline float3 get_raw_interpolated_color(const float3 color0,
  4461. const float3 color1, const float3 color2, const float3 color3,
  4462. const float4 weights)
  4463. {
  4464. // Use max to avoid bizarre artifacts from negative colors:
  4465. return max(mul(weights, float4x3(color0, color1, color2, color3)), 0.0);
  4466. }
  4467. float3 get_interpolated_linear_color(const float3 color0, const float3 color1,
  4468. const float3 color2, const float3 color3, const float4 weights)
  4469. {
  4470. // Requires: 1.) Requirements of include/gamma-management.h must be met:
  4471. // intermediate_gamma must be globally defined, and input
  4472. // colors are interpreted as linear RGB unless you #define
  4473. // GAMMA_ENCODE_EVERY_FBO (in which case they are
  4474. // interpreted as gamma-encoded with intermediate_gamma).
  4475. // 2.) color0-3 are colors sampled from a texture with tex2D().
  4476. // They are interpreted as defined in requirement 1.
  4477. // 3.) weights contains weights for each color, summing to 1.0.
  4478. // 4.) beam_horiz_linear_rgb_weight must be defined as a global
  4479. // float in [0.0, 1.0] describing how much blending should
  4480. // be done in linear RGB (rest is gamma-corrected RGB).
  4481. // 5.) RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE must be #defined
  4482. // if beam_horiz_linear_rgb_weight is anything other than a
  4483. // static constant, or we may try branching at runtime
  4484. // without dynamic branches allowed (slow).
  4485. // Returns: Return an interpolated color lookup between the four input
  4486. // colors based on the weights in weights. The final color will
  4487. // be a linear RGB value, but the blending will be done as
  4488. // indicated above.
  4489. const float intermediate_gamma = get_intermediate_gamma();
  4490. // Branch if beam_horiz_linear_rgb_weight is static (for free) or if the
  4491. // profile allows dynamic branches (faster than computing extra pows):
  4492. #ifndef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  4493. #define SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4494. #else
  4495. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  4496. #define SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4497. #endif
  4498. #endif
  4499. #ifdef SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4500. // beam_horiz_linear_rgb_weight is static, so we can branch:
  4501. #ifdef GAMMA_ENCODE_EVERY_FBO
  4502. const float3 gamma_mixed_color = pow(get_raw_interpolated_color(
  4503. color0, color1, color2, color3, weights), float3(intermediate_gamma));
  4504. if(beam_horiz_linear_rgb_weight > 0.0)
  4505. {
  4506. const float3 linear_mixed_color = get_raw_interpolated_color(
  4507. pow(color0, float3(intermediate_gamma)),
  4508. pow(color1, float3(intermediate_gamma)),
  4509. pow(color2, float3(intermediate_gamma)),
  4510. pow(color3, float3(intermediate_gamma)),
  4511. weights);
  4512. return lerp(gamma_mixed_color, linear_mixed_color,
  4513. beam_horiz_linear_rgb_weight);
  4514. }
  4515. else
  4516. {
  4517. return gamma_mixed_color;
  4518. }
  4519. #else
  4520. const float3 linear_mixed_color = get_raw_interpolated_color(
  4521. color0, color1, color2, color3, weights);
  4522. if(beam_horiz_linear_rgb_weight < 1.0)
  4523. {
  4524. const float3 gamma_mixed_color = get_raw_interpolated_color(
  4525. pow(color0, float3(1.0/intermediate_gamma)),
  4526. pow(color1, float3(1.0/intermediate_gamma)),
  4527. pow(color2, float3(1.0/intermediate_gamma)),
  4528. pow(color3, float3(1.0/intermediate_gamma)),
  4529. weights);
  4530. return lerp(gamma_mixed_color, linear_mixed_color,
  4531. beam_horiz_linear_rgb_weight);
  4532. }
  4533. else
  4534. {
  4535. return linear_mixed_color;
  4536. }
  4537. #endif // GAMMA_ENCODE_EVERY_FBO
  4538. #else
  4539. #ifdef GAMMA_ENCODE_EVERY_FBO
  4540. // Inputs: color0-3 are colors in gamma-encoded RGB.
  4541. const float3 gamma_mixed_color = pow(get_raw_interpolated_color(
  4542. color0, color1, color2, color3, weights), intermediate_gamma);
  4543. const float3 linear_mixed_color = get_raw_interpolated_color(
  4544. pow(color0, float3(intermediate_gamma)),
  4545. pow(color1, float3(intermediate_gamma)),
  4546. pow(color2, float3(intermediate_gamma)),
  4547. pow(color3, float3(intermediate_gamma)),
  4548. weights);
  4549. return lerp(gamma_mixed_color, linear_mixed_color,
  4550. beam_horiz_linear_rgb_weight);
  4551. #else
  4552. // Inputs: color0-3 are colors in linear RGB.
  4553. const float3 linear_mixed_color = get_raw_interpolated_color(
  4554. color0, color1, color2, color3, weights);
  4555. const float3 gamma_mixed_color = get_raw_interpolated_color(
  4556. pow(color0, float3(1.0/intermediate_gamma)),
  4557. pow(color1, float3(1.0/intermediate_gamma)),
  4558. pow(color2, float3(1.0/intermediate_gamma)),
  4559. pow(color3, float3(1.0/intermediate_gamma)),
  4560. weights);
  4561. // wtf fixme
  4562. // const float beam_horiz_linear_rgb_weight1 = 1.0;
  4563. return lerp(gamma_mixed_color, linear_mixed_color,
  4564. beam_horiz_linear_rgb_weight);
  4565. #endif // GAMMA_ENCODE_EVERY_FBO
  4566. #endif // SCANLINES_BRANCH_FOR_LINEAR_RGB_WEIGHT
  4567. }
  4568. float3 get_scanline_color(const sampler2D tex, const float2 scanline_uv,
  4569. const float2 uv_step_x, const float4 weights)
  4570. {
  4571. // Requires: 1.) scanline_uv must be vertically snapped to the caller's
  4572. // desired line or scanline and horizontally snapped to the
  4573. // texel just left of the output pixel (color1)
  4574. // 2.) uv_step_x must contain the horizontal uv distance
  4575. // between texels.
  4576. // 3.) weights must contain interpolation filter weights for
  4577. // color0, color1, color2, and color3, where color1 is just
  4578. // left of the output pixel.
  4579. // Returns: Return a horizontally interpolated texture lookup using 2-4
  4580. // nearby texels, according to weights and the conventions of
  4581. // get_interpolated_linear_color().
  4582. // We can ignore the outside texture lookups for Quilez resampling.
  4583. const float3 color1 = COMPAT_TEXTURE(tex, scanline_uv).rgb;
  4584. const float3 color2 = COMPAT_TEXTURE(tex, scanline_uv + uv_step_x).rgb;
  4585. float3 color0 = float3(0.0);
  4586. float3 color3 = float3(0.0);
  4587. if(beam_horiz_filter > 0.5)
  4588. {
  4589. color0 = COMPAT_TEXTURE(tex, scanline_uv - uv_step_x).rgb;
  4590. color3 = COMPAT_TEXTURE(tex, scanline_uv + 2.0 * uv_step_x).rgb;
  4591. }
  4592. // Sample the texture as-is, whether it's linear or gamma-encoded:
  4593. // get_interpolated_linear_color() will handle the difference.
  4594. return get_interpolated_linear_color(color0, color1, color2, color3, weights);
  4595. }
  4596. float3 sample_single_scanline_horizontal(const sampler2D tex,
  4597. const float2 tex_uv, const float2 tex_size,
  4598. const float2 texture_size_inv)
  4599. {
  4600. // TODO: Add function requirements.
  4601. // Snap to the previous texel and get sample dists from 2/4 nearby texels:
  4602. const float2 curr_texel = tex_uv * tex_size;
  4603. // Use under_half to fix a rounding bug right around exact texel locations.
  4604. const float2 prev_texel =
  4605. floor(curr_texel - float2(under_half)) + float2(0.5);
  4606. const float2 prev_texel_hor = float2(prev_texel.x, curr_texel.y);
  4607. const float2 prev_texel_hor_uv = prev_texel_hor * texture_size_inv;
  4608. const float prev_dist = curr_texel.x - prev_texel_hor.x;
  4609. const float4 sample_dists = float4(1.0 + prev_dist, prev_dist,
  4610. 1.0 - prev_dist, 2.0 - prev_dist);
  4611. // Get Quilez, Lanczos2, or Gaussian resize weights for 2/4 nearby texels:
  4612. float4 weights;
  4613. if(beam_horiz_filter < 0.5)
  4614. {
  4615. // Quilez:
  4616. const float x = sample_dists.y;
  4617. const float w2 = x*x*x*(x*(x*6.0 - 15.0) + 10.0);
  4618. weights = float4(0.0, 1.0 - w2, w2, 0.0);
  4619. }
  4620. else if(beam_horiz_filter < 1.5)
  4621. {
  4622. // Gaussian:
  4623. float inner_denom_inv = 1.0/(2.0*beam_horiz_sigma*beam_horiz_sigma);
  4624. weights = exp(-(sample_dists*sample_dists)*inner_denom_inv);
  4625. }
  4626. else
  4627. {
  4628. // Lanczos2:
  4629. const float4 pi_dists = FIX_ZERO(sample_dists * pi);
  4630. weights = 2.0 * sin(pi_dists) * sin(pi_dists * 0.5) /
  4631. (pi_dists * pi_dists);
  4632. }
  4633. // Ensure the weight sum == 1.0:
  4634. const float4 final_weights = weights/dot(weights, float4(1.0));
  4635. // Get the interpolated horizontal scanline color:
  4636. const float2 uv_step_x = float2(texture_size_inv.x, 0.0);
  4637. return get_scanline_color(
  4638. tex, prev_texel_hor_uv, uv_step_x, final_weights);
  4639. }
  4640. float3 sample_rgb_scanline_horizontal(const sampler2D tex,
  4641. const float2 tex_uv, const float2 tex_size,
  4642. const float2 texture_size_inv)
  4643. {
  4644. // TODO: Add function requirements.
  4645. // Rely on a helper to make convergence easier.
  4646. if(beam_misconvergence)
  4647. {
  4648. const float3 convergence_offsets_rgb =
  4649. get_convergence_offsets_x_vector();
  4650. const float3 offset_u_rgb =
  4651. convergence_offsets_rgb * texture_size_inv.xxx;
  4652. const float2 scanline_uv_r = tex_uv - float2(offset_u_rgb.r, 0.0);
  4653. const float2 scanline_uv_g = tex_uv - float2(offset_u_rgb.g, 0.0);
  4654. const float2 scanline_uv_b = tex_uv - float2(offset_u_rgb.b, 0.0);
  4655. const float3 sample_r = sample_single_scanline_horizontal(
  4656. tex, scanline_uv_r, tex_size, texture_size_inv);
  4657. const float3 sample_g = sample_single_scanline_horizontal(
  4658. tex, scanline_uv_g, tex_size, texture_size_inv);
  4659. const float3 sample_b = sample_single_scanline_horizontal(
  4660. tex, scanline_uv_b, tex_size, texture_size_inv);
  4661. return float3(sample_r.r, sample_g.g, sample_b.b);
  4662. }
  4663. else
  4664. {
  4665. return sample_single_scanline_horizontal(tex, tex_uv, tex_size,
  4666. texture_size_inv);
  4667. }
  4668. }
  4669. float2 get_last_scanline_uv(const float2 tex_uv, const float2 tex_size,
  4670. const float2 texture_size_inv, const float2 il_step_multiple,
  4671. const float frame_count, out float dist)
  4672. {
  4673. // Compute texture coords for the last/upper scanline, accounting for
  4674. // interlacing: With interlacing, only consider even/odd scanlines every
  4675. // other frame. Top-field first (TFF) order puts even scanlines on even
  4676. // frames, and BFF order puts them on odd frames. Texels are centered at:
  4677. // frac(tex_uv * tex_size) == x.5
  4678. // Caution: If these coordinates ever seem incorrect, first make sure it's
  4679. // not because anisotropic filtering is blurring across field boundaries.
  4680. // Note: TFF/BFF won't matter for sources that double-weave or similar.
  4681. // wtf fixme
  4682. // const float interlace_bff1 = 1.0;
  4683. const float field_offset = floor(il_step_multiple.y * 0.75) *
  4684. fmod(frame_count + float(interlace_bff), 2.0);
  4685. const float2 curr_texel = tex_uv * tex_size;
  4686. // Use under_half to fix a rounding bug right around exact texel locations.
  4687. const float2 prev_texel_num = floor(curr_texel - float2(under_half));
  4688. const float wrong_field = fmod(
  4689. prev_texel_num.y + field_offset, il_step_multiple.y);
  4690. const float2 scanline_texel_num = prev_texel_num - float2(0.0, wrong_field);
  4691. // Snap to the center of the previous scanline in the current field:
  4692. const float2 scanline_texel = scanline_texel_num + float2(0.5);
  4693. const float2 scanline_uv = scanline_texel * texture_size_inv;
  4694. // Save the sample's distance from the scanline, in units of scanlines:
  4695. dist = (curr_texel.y - scanline_texel.y)/il_step_multiple.y;
  4696. return scanline_uv;
  4697. }
  4698. inline bool is_interlaced(float num_lines)
  4699. {
  4700. // Detect interlacing based on the number of lines in the source.
  4701. if(interlace_detect)
  4702. {
  4703. // NTSC: 525 lines, 262.5/field; 486 active (2 half-lines), 243/field
  4704. // NTSC Emulators: Typically 224 or 240 lines
  4705. // PAL: 625 lines, 312.5/field; 576 active (typical), 288/field
  4706. // PAL Emulators: ?
  4707. // ATSC: 720p, 1080i, 1080p
  4708. // Where do we place our cutoffs? Assumptions:
  4709. // 1.) We only need to care about active lines.
  4710. // 2.) Anything > 288 and <= 576 lines is probably interlaced.
  4711. // 3.) Anything > 576 lines is probably not interlaced...
  4712. // 4.) ...except 1080 lines, which is a crapshoot (user decision).
  4713. // 5.) Just in case the main program uses calculated video sizes,
  4714. // we should nudge the float thresholds a bit.
  4715. const bool sd_interlace = ((num_lines > 288.5) && (num_lines < 576.5));
  4716. const bool hd_interlace = bool(interlace_1080i) ?
  4717. ((num_lines > 1079.5) && (num_lines < 1080.5)) :
  4718. false;
  4719. return (sd_interlace || hd_interlace);
  4720. }
  4721. else
  4722. {
  4723. return false;
  4724. }
  4725. }
  4726. #endif // SCANLINE_FUNCTIONS_H
  4727. ///////////////////////////// END SCANLINE-FUNCTIONS ////////////////////////////
  4728. //#include "../../../../include/gamma-management.h"
  4729. //////////////////////////// BEGIN GAMMA-MANAGEMENT //////////////////////////
  4730. #ifndef GAMMA_MANAGEMENT_H
  4731. #define GAMMA_MANAGEMENT_H
  4732. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  4733. // Copyright (C) 2014 TroggleMonkey
  4734. //
  4735. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4736. // of this software and associated documentation files (the "Software"), to
  4737. // deal in the Software without restriction, including without limitation the
  4738. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  4739. // sell copies of the Software, and to permit persons to whom the Software is
  4740. // furnished to do so, subject to the following conditions:
  4741. //
  4742. // The above copyright notice and this permission notice shall be included in
  4743. // all copies or substantial portions of the Software.
  4744. //
  4745. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  4746. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  4747. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  4748. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  4749. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  4750. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  4751. // IN THE SOFTWARE.
  4752. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  4753. // This file provides gamma-aware tex*D*() and encode_output() functions.
  4754. // Requires: Before #include-ing this file, the including file must #define
  4755. // the following macros when applicable and follow their rules:
  4756. // 1.) #define FIRST_PASS if this is the first pass.
  4757. // 2.) #define LAST_PASS if this is the last pass.
  4758. // 3.) If sRGB is available, set srgb_framebufferN = "true" for
  4759. // every pass except the last in your .cgp preset.
  4760. // 4.) If sRGB isn't available but you want gamma-correctness with
  4761. // no banding, #define GAMMA_ENCODE_EVERY_FBO each pass.
  4762. // 5.) #define SIMULATE_CRT_ON_LCD if desired (precedence over 5-7)
  4763. // 6.) #define SIMULATE_GBA_ON_LCD if desired (precedence over 6-7)
  4764. // 7.) #define SIMULATE_LCD_ON_CRT if desired (precedence over 7)
  4765. // 8.) #define SIMULATE_GBA_ON_CRT if desired (precedence over -)
  4766. // If an option in [5, 8] is #defined in the first or last pass, it
  4767. // should be #defined for both. It shouldn't make a difference
  4768. // whether it's #defined for intermediate passes or not.
  4769. // Optional: The including file (or an earlier included file) may optionally
  4770. // #define a number of macros indicating it will override certain
  4771. // macros and associated constants are as follows:
  4772. // static constants with either static or uniform constants. The
  4773. // 1.) OVERRIDE_STANDARD_GAMMA: The user must first define:
  4774. // static const float ntsc_gamma
  4775. // static const float pal_gamma
  4776. // static const float crt_reference_gamma_high
  4777. // static const float crt_reference_gamma_low
  4778. // static const float lcd_reference_gamma
  4779. // static const float crt_office_gamma
  4780. // static const float lcd_office_gamma
  4781. // 2.) OVERRIDE_DEVICE_GAMMA: The user must first define:
  4782. // static const float crt_gamma
  4783. // static const float gba_gamma
  4784. // static const float lcd_gamma
  4785. // 3.) OVERRIDE_FINAL_GAMMA: The user must first define:
  4786. // static const float input_gamma
  4787. // static const float intermediate_gamma
  4788. // static const float output_gamma
  4789. // (intermediate_gamma is for GAMMA_ENCODE_EVERY_FBO.)
  4790. // 4.) OVERRIDE_ALPHA_ASSUMPTIONS: The user must first define:
  4791. // static const bool assume_opaque_alpha
  4792. // The gamma constant overrides must be used in every pass or none,
  4793. // and OVERRIDE_FINAL_GAMMA bypasses all of the SIMULATE* macros.
  4794. // OVERRIDE_ALPHA_ASSUMPTIONS may be set on a per-pass basis.
  4795. // Usage: After setting macros appropriately, ignore gamma correction and
  4796. // replace all tex*D*() calls with equivalent gamma-aware
  4797. // tex*D*_linearize calls, except:
  4798. // 1.) When you read an LUT, use regular tex*D or a gamma-specified
  4799. // function, depending on its gamma encoding:
  4800. // tex*D*_linearize_gamma (takes a runtime gamma parameter)
  4801. // 2.) If you must read pass0's original input in a later pass, use
  4802. // tex2D_linearize_ntsc_gamma. If you want to read pass0's
  4803. // input with gamma-corrected bilinear filtering, consider
  4804. // creating a first linearizing pass and reading from the input
  4805. // of pass1 later.
  4806. // Then, return encode_output(color) from every fragment shader.
  4807. // Finally, use the global gamma_aware_bilinear boolean if you want
  4808. // to statically branch based on whether bilinear filtering is
  4809. // gamma-correct or not (e.g. for placing Gaussian blur samples).
  4810. //
  4811. // Detailed Policy:
  4812. // tex*D*_linearize() functions enforce a consistent gamma-management policy
  4813. // based on the FIRST_PASS and GAMMA_ENCODE_EVERY_FBO settings. They assume
  4814. // their input texture has the same encoding characteristics as the input for
  4815. // the current pass (which doesn't apply to the exceptions listed above).
  4816. // Similarly, encode_output() enforces a policy based on the LAST_PASS and
  4817. // GAMMA_ENCODE_EVERY_FBO settings. Together, they result in one of the
  4818. // following two pipelines.
  4819. // Typical pipeline with intermediate sRGB framebuffers:
  4820. // linear_color = pow(pass0_encoded_color, input_gamma);
  4821. // intermediate_output = linear_color; // Automatic sRGB encoding
  4822. // linear_color = intermediate_output; // Automatic sRGB decoding
  4823. // final_output = pow(intermediate_output, 1.0/output_gamma);
  4824. // Typical pipeline without intermediate sRGB framebuffers:
  4825. // linear_color = pow(pass0_encoded_color, input_gamma);
  4826. // intermediate_output = pow(linear_color, 1.0/intermediate_gamma);
  4827. // linear_color = pow(intermediate_output, intermediate_gamma);
  4828. // final_output = pow(intermediate_output, 1.0/output_gamma);
  4829. // Using GAMMA_ENCODE_EVERY_FBO is much slower, but it's provided as a way to
  4830. // easily get gamma-correctness without banding on devices where sRGB isn't
  4831. // supported.
  4832. //
  4833. // Use This Header to Maximize Code Reuse:
  4834. // The purpose of this header is to provide a consistent interface for texture
  4835. // reads and output gamma-encoding that localizes and abstracts away all the
  4836. // annoying details. This greatly reduces the amount of code in each shader
  4837. // pass that depends on the pass number in the .cgp preset or whether sRGB
  4838. // FBO's are being used: You can trivially change the gamma behavior of your
  4839. // whole pass by commenting or uncommenting 1-3 #defines. To reuse the same
  4840. // code in your first, Nth, and last passes, you can even put it all in another
  4841. // header file and #include it from skeleton .cg files that #define the
  4842. // appropriate pass-specific settings.
  4843. //
  4844. // Rationale for Using Three Macros:
  4845. // This file uses GAMMA_ENCODE_EVERY_FBO instead of an opposite macro like
  4846. // SRGB_PIPELINE to ensure sRGB is assumed by default, which hopefully imposes
  4847. // a lower maintenance burden on each pass. At first glance it seems we could
  4848. // accomplish everything with two macros: GAMMA_CORRECT_IN / GAMMA_CORRECT_OUT.
  4849. // This works for simple use cases where input_gamma == output_gamma, but it
  4850. // breaks down for more complex scenarios like CRT simulation, where the pass
  4851. // number determines the gamma encoding of the input and output.
  4852. /////////////////////////////// BASE CONSTANTS ///////////////////////////////
  4853. // Set standard gamma constants, but allow users to override them:
  4854. #ifndef OVERRIDE_STANDARD_GAMMA
  4855. // Standard encoding gammas:
  4856. static const float ntsc_gamma = 2.2; // Best to use NTSC for PAL too?
  4857. static const float pal_gamma = 2.8; // Never actually 2.8 in practice
  4858. // Typical device decoding gammas (only use for emulating devices):
  4859. // CRT/LCD reference gammas are higher than NTSC and Rec.709 video standard
  4860. // gammas: The standards purposely undercorrected for an analog CRT's
  4861. // assumed 2.5 reference display gamma to maintain contrast in assumed
  4862. // [dark] viewing conditions: http://www.poynton.com/PDFs/GammaFAQ.pdf
  4863. // These unstated assumptions about display gamma and perceptual rendering
  4864. // intent caused a lot of confusion, and more modern CRT's seemed to target
  4865. // NTSC 2.2 gamma with circuitry. LCD displays seem to have followed suit
  4866. // (they struggle near black with 2.5 gamma anyway), especially PC/laptop
  4867. // displays designed to view sRGB in bright environments. (Standards are
  4868. // also in flux again with BT.1886, but it's underspecified for displays.)
  4869. static const float crt_reference_gamma_high = 2.5; // In (2.35, 2.55)
  4870. static const float crt_reference_gamma_low = 2.35; // In (2.35, 2.55)
  4871. static const float lcd_reference_gamma = 2.5; // To match CRT
  4872. static const float crt_office_gamma = 2.2; // Circuitry-adjusted for NTSC
  4873. static const float lcd_office_gamma = 2.2; // Approximates sRGB
  4874. #endif // OVERRIDE_STANDARD_GAMMA
  4875. // Assuming alpha == 1.0 might make it easier for users to avoid some bugs,
  4876. // but only if they're aware of it.
  4877. #ifndef OVERRIDE_ALPHA_ASSUMPTIONS
  4878. static const bool assume_opaque_alpha = false;
  4879. #endif
  4880. /////////////////////// DERIVED CONSTANTS AS FUNCTIONS ///////////////////////
  4881. // gamma-management.h should be compatible with overriding gamma values with
  4882. // runtime user parameters, but we can only define other global constants in
  4883. // terms of static constants, not uniform user parameters. To get around this
  4884. // limitation, we need to define derived constants using functions.
  4885. // Set device gamma constants, but allow users to override them:
  4886. #ifdef OVERRIDE_DEVICE_GAMMA
  4887. // The user promises to globally define the appropriate constants:
  4888. inline float get_crt_gamma() { return crt_gamma; }
  4889. inline float get_gba_gamma() { return gba_gamma; }
  4890. inline float get_lcd_gamma() { return lcd_gamma; }
  4891. #else
  4892. inline float get_crt_gamma() { return crt_reference_gamma_high; }
  4893. inline float get_gba_gamma() { return 3.5; } // Game Boy Advance; in (3.0, 4.0)
  4894. inline float get_lcd_gamma() { return lcd_office_gamma; }
  4895. #endif // OVERRIDE_DEVICE_GAMMA
  4896. // Set decoding/encoding gammas for the first/lass passes, but allow overrides:
  4897. #ifdef OVERRIDE_FINAL_GAMMA
  4898. // The user promises to globally define the appropriate constants:
  4899. inline float get_intermediate_gamma() { return intermediate_gamma; }
  4900. inline float get_input_gamma() { return input_gamma; }
  4901. inline float get_output_gamma() { return output_gamma; }
  4902. #else
  4903. // If we gamma-correct every pass, always use ntsc_gamma between passes to
  4904. // ensure middle passes don't need to care if anything is being simulated:
  4905. inline float get_intermediate_gamma() { return ntsc_gamma; }
  4906. #ifdef SIMULATE_CRT_ON_LCD
  4907. inline float get_input_gamma() { return get_crt_gamma(); }
  4908. inline float get_output_gamma() { return get_lcd_gamma(); }
  4909. #else
  4910. #ifdef SIMULATE_GBA_ON_LCD
  4911. inline float get_input_gamma() { return get_gba_gamma(); }
  4912. inline float get_output_gamma() { return get_lcd_gamma(); }
  4913. #else
  4914. #ifdef SIMULATE_LCD_ON_CRT
  4915. inline float get_input_gamma() { return get_lcd_gamma(); }
  4916. inline float get_output_gamma() { return get_crt_gamma(); }
  4917. #else
  4918. #ifdef SIMULATE_GBA_ON_CRT
  4919. inline float get_input_gamma() { return get_gba_gamma(); }
  4920. inline float get_output_gamma() { return get_crt_gamma(); }
  4921. #else // Don't simulate anything:
  4922. inline float get_input_gamma() { return ntsc_gamma; }
  4923. inline float get_output_gamma() { return ntsc_gamma; }
  4924. #endif // SIMULATE_GBA_ON_CRT
  4925. #endif // SIMULATE_LCD_ON_CRT
  4926. #endif // SIMULATE_GBA_ON_LCD
  4927. #endif // SIMULATE_CRT_ON_LCD
  4928. #endif // OVERRIDE_FINAL_GAMMA
  4929. // Set decoding/encoding gammas for the current pass. Use static constants for
  4930. // linearize_input and gamma_encode_output, because they aren't derived, and
  4931. // they let the compiler do dead-code elimination.
  4932. #ifndef GAMMA_ENCODE_EVERY_FBO
  4933. #ifdef FIRST_PASS
  4934. static const bool linearize_input = true;
  4935. inline float get_pass_input_gamma() { return get_input_gamma(); }
  4936. #else
  4937. static const bool linearize_input = false;
  4938. inline float get_pass_input_gamma() { return 1.0; }
  4939. #endif
  4940. #ifdef LAST_PASS
  4941. static const bool gamma_encode_output = true;
  4942. inline float get_pass_output_gamma() { return get_output_gamma(); }
  4943. #else
  4944. static const bool gamma_encode_output = false;
  4945. inline float get_pass_output_gamma() { return 1.0; }
  4946. #endif
  4947. #else
  4948. static const bool linearize_input = true;
  4949. static const bool gamma_encode_output = true;
  4950. #ifdef FIRST_PASS
  4951. inline float get_pass_input_gamma() { return get_input_gamma(); }
  4952. #else
  4953. inline float get_pass_input_gamma() { return get_intermediate_gamma(); }
  4954. #endif
  4955. #ifdef LAST_PASS
  4956. inline float get_pass_output_gamma() { return get_output_gamma(); }
  4957. #else
  4958. inline float get_pass_output_gamma() { return get_intermediate_gamma(); }
  4959. #endif
  4960. #endif
  4961. // Users might want to know if bilinear filtering will be gamma-correct:
  4962. static const bool gamma_aware_bilinear = !linearize_input;
  4963. ////////////////////// COLOR ENCODING/DECODING FUNCTIONS /////////////////////
  4964. inline float4 encode_output(const float4 color)
  4965. {
  4966. if(gamma_encode_output)
  4967. {
  4968. if(assume_opaque_alpha)
  4969. {
  4970. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), 1.0);
  4971. }
  4972. else
  4973. {
  4974. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), color.a);
  4975. }
  4976. }
  4977. else
  4978. {
  4979. return color;
  4980. }
  4981. }
  4982. inline float4 decode_input(const float4 color)
  4983. {
  4984. if(linearize_input)
  4985. {
  4986. if(assume_opaque_alpha)
  4987. {
  4988. return float4(pow(color.rgb, float3(get_pass_input_gamma())), 1.0);
  4989. }
  4990. else
  4991. {
  4992. return float4(pow(color.rgb, float3(get_pass_input_gamma())), color.a);
  4993. }
  4994. }
  4995. else
  4996. {
  4997. return color;
  4998. }
  4999. }
  5000. inline float4 decode_gamma_input(const float4 color, const float3 gamma)
  5001. {
  5002. if(assume_opaque_alpha)
  5003. {
  5004. return float4(pow(color.rgb, gamma), 1.0);
  5005. }
  5006. else
  5007. {
  5008. return float4(pow(color.rgb, gamma), color.a);
  5009. }
  5010. }
  5011. //TODO/FIXME: I have no idea why replacing the lookup wrappers with this macro fixes the blurs being offset ¯\_(ツ)_/¯
  5012. //#define tex2D_linearize(C, D) decode_input(vec4(COMPAT_TEXTURE(C, D)))
  5013. // EDIT: it's the 'const' in front of the coords that's doing it
  5014. /////////////////////////// TEXTURE LOOKUP WRAPPERS //////////////////////////
  5015. // "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  5016. // Provide a wide array of linearizing texture lookup wrapper functions. The
  5017. // Cg shader spec Retroarch uses only allows for 2D textures, but 1D and 3D
  5018. // lookups are provided for completeness in case that changes someday. Nobody
  5019. // is likely to use the *fetch and *proj functions, but they're included just
  5020. // in case. The only tex*D texture sampling functions omitted are:
  5021. // - tex*Dcmpbias
  5022. // - tex*Dcmplod
  5023. // - tex*DARRAY*
  5024. // - tex*DMS*
  5025. // - Variants returning integers
  5026. // Standard line length restrictions are ignored below for vertical brevity.
  5027. /*
  5028. // tex1D:
  5029. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords)
  5030. { return decode_input(tex1D(tex, tex_coords)); }
  5031. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords)
  5032. { return decode_input(tex1D(tex, tex_coords)); }
  5033. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const int texel_off)
  5034. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  5035. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  5036. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  5037. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy)
  5038. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  5039. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy)
  5040. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  5041. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy, const int texel_off)
  5042. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  5043. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy, const int texel_off)
  5044. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  5045. // tex1Dbias:
  5046. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords)
  5047. { return decode_input(tex1Dbias(tex, tex_coords)); }
  5048. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  5049. { return decode_input(tex1Dbias(tex, tex_coords, texel_off)); }
  5050. // tex1Dfetch:
  5051. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords)
  5052. { return decode_input(tex1Dfetch(tex, tex_coords)); }
  5053. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords, const int texel_off)
  5054. { return decode_input(tex1Dfetch(tex, tex_coords, texel_off)); }
  5055. // tex1Dlod:
  5056. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords)
  5057. { return decode_input(tex1Dlod(tex, tex_coords)); }
  5058. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  5059. { return decode_input(tex1Dlod(tex, tex_coords, texel_off)); }
  5060. // tex1Dproj:
  5061. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords)
  5062. { return decode_input(tex1Dproj(tex, tex_coords)); }
  5063. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords)
  5064. { return decode_input(tex1Dproj(tex, tex_coords)); }
  5065. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  5066. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  5067. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords, const int texel_off)
  5068. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  5069. */
  5070. // tex2D:
  5071. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords)
  5072. { return decode_input(COMPAT_TEXTURE(tex, tex_coords)); }
  5073. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords)
  5074. { return decode_input(COMPAT_TEXTURE(tex, tex_coords.xy)); }
  5075. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords, int texel_off)
  5076. { return decode_input(textureLod(tex, tex_coords, texel_off)); }
  5077. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords, int texel_off)
  5078. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  5079. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy)
  5080. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  5081. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy)
  5082. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  5083. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  5084. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  5085. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  5086. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  5087. // tex2Dbias:
  5088. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords)
  5089. //{ return decode_input(tex2Dbias(tex, tex_coords)); }
  5090. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  5091. //{ return decode_input(tex2Dbias(tex, tex_coords, texel_off)); }
  5092. // tex2Dfetch:
  5093. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords)
  5094. //{ return decode_input(tex2Dfetch(tex, tex_coords)); }
  5095. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords, const int texel_off)
  5096. //{ return decode_input(tex2Dfetch(tex, tex_coords, texel_off)); }
  5097. // tex2Dlod:
  5098. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords)
  5099. { return decode_input(textureLod(tex, tex_coords.xy, 0.0)); }
  5100. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords, int texel_off)
  5101. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  5102. /*
  5103. // tex2Dproj:
  5104. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords)
  5105. { return decode_input(tex2Dproj(tex, tex_coords)); }
  5106. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords)
  5107. { return decode_input(tex2Dproj(tex, tex_coords)); }
  5108. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords, const int texel_off)
  5109. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  5110. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  5111. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  5112. */
  5113. /*
  5114. // tex3D:
  5115. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords)
  5116. { return decode_input(tex3D(tex, tex_coords)); }
  5117. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const int texel_off)
  5118. { return decode_input(tex3D(tex, tex_coords, texel_off)); }
  5119. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy)
  5120. { return decode_input(tex3D(tex, tex_coords, dx, dy)); }
  5121. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy, const int texel_off)
  5122. { return decode_input(tex3D(tex, tex_coords, dx, dy, texel_off)); }
  5123. // tex3Dbias:
  5124. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords)
  5125. { return decode_input(tex3Dbias(tex, tex_coords)); }
  5126. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  5127. { return decode_input(tex3Dbias(tex, tex_coords, texel_off)); }
  5128. // tex3Dfetch:
  5129. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords)
  5130. { return decode_input(tex3Dfetch(tex, tex_coords)); }
  5131. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords, const int texel_off)
  5132. { return decode_input(tex3Dfetch(tex, tex_coords, texel_off)); }
  5133. // tex3Dlod:
  5134. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords)
  5135. { return decode_input(tex3Dlod(tex, tex_coords)); }
  5136. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  5137. { return decode_input(tex3Dlod(tex, tex_coords, texel_off)); }
  5138. // tex3Dproj:
  5139. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords)
  5140. { return decode_input(tex3Dproj(tex, tex_coords)); }
  5141. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  5142. { return decode_input(tex3Dproj(tex, tex_coords, texel_off)); }
  5143. /////////*
  5144. // NONSTANDARD "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  5145. // This narrow selection of nonstandard tex2D* functions can be useful:
  5146. // tex2Dlod0: Automatically fill in the tex2D LOD parameter for mip level 0.
  5147. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords)
  5148. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0))); }
  5149. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords, const int texel_off)
  5150. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0), texel_off)); }
  5151. // MANUALLY LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  5152. // Provide a narrower selection of tex2D* wrapper functions that decode an
  5153. // input sample with a specified gamma value. These are useful for reading
  5154. // LUT's and for reading the input of pass0 in a later pass.
  5155. // tex2D:
  5156. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float3 gamma)
  5157. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords), gamma); }
  5158. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float3 gamma)
  5159. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords.xy), gamma); }
  5160. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const int texel_off, const float3 gamma)
  5161. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  5162. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const int texel_off, const float3 gamma)
  5163. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  5164. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  5165. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  5166. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  5167. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  5168. //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)
  5169. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  5170. //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)
  5171. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  5172. /*
  5173. // tex2Dbias:
  5174. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const float3 gamma)
  5175. { return decode_gamma_input(tex2Dbias(tex, tex_coords), gamma); }
  5176. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const int texel_off, const float3 gamma)
  5177. { return decode_gamma_input(tex2Dbias(tex, tex_coords, texel_off), gamma); }
  5178. // tex2Dfetch:
  5179. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const float3 gamma)
  5180. { return decode_gamma_input(tex2Dfetch(tex, tex_coords), gamma); }
  5181. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const int texel_off, const float3 gamma)
  5182. { return decode_gamma_input(tex2Dfetch(tex, tex_coords, texel_off), gamma); }
  5183. */
  5184. // tex2Dlod:
  5185. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, float3 gamma)
  5186. { return decode_gamma_input(textureLod(tex, tex_coords.xy, 0.0), gamma); }
  5187. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, int texel_off, float3 gamma)
  5188. { return decode_gamma_input(textureLod(tex, tex_coords.xy, texel_off), gamma); }
  5189. #endif // GAMMA_MANAGEMENT_H
  5190. //////////////////////////// END GAMMA-MANAGEMENT //////////////////////////
  5191. #undef COMPAT_PRECISION
  5192. #undef COMPAT_TEXTURE
  5193. void main() {
  5194. gl_Position = position;
  5195. vTexCoord = texCoord * 1.0001;
  5196. // Detect interlacing: il_step_multiple indicates the step multiple between
  5197. // lines: 1 is for progressive sources, and 2 is for interlaced sources.
  5198. float2 video_size_ = video_size.xy;
  5199. const float y_step = 1.0 + float(is_interlaced(video_size_.y));
  5200. il_step_multiple = float2(1.0, y_step);
  5201. // Get the uv tex coords step between one texel (x) and scanline (y):
  5202. uv_step = il_step_multiple / texture_size;
  5203. // If shader parameters are used, {min, max}_{sigma, shape} are runtime
  5204. // values. Compute {sigma, shape}_range outside of scanline_contrib() so
  5205. // they aren't computed once per scanline (6 times per fragment and up to
  5206. // 18 times per vertex):
  5207. // TODO/FIXME: if these aren't used, why are they calculated? commenting for now
  5208. // const floatsigma_range = max(beam_max_sigma, beam_min_sigma) -
  5209. // beam_min_sigma;
  5210. // const float shape_range = max(beam_max_shape, beam_min_shape) -
  5211. // beam_min_shape;
  5212. // We need the pixel height in scanlines for antialiased/integral sampling:
  5213. const float ph = (video_size_.y / output_size.y) /
  5214. il_step_multiple.y;
  5215. pixel_height_in_scanlines = ph;
  5216. }