geometry-aa-last-pass.fs 287 KB

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