geometry-aa-last-pass.vs 287 KB

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