geometry-aa-last-pass.fs 287 KB

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