bloom-approx.vs 305 KB

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