bloom-vertical.fs 252 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824
  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. in Vertex {
  22. vec2 vTexCoord;
  23. vec2 tex_uv;
  24. vec2 bloom_dxdy;
  25. float bloom_sigma_runtime;
  26. };
  27. out vec4 FragColor;
  28. // USER SETTINGS BLOCK //
  29. #define crt_gamma 2.500000
  30. #define lcd_gamma 2.200000
  31. #define levels_contrast 1.0
  32. #define halation_weight 0.0
  33. #define diffusion_weight 0.075
  34. #define bloom_underestimate_levels 0.8
  35. #define bloom_excess 0.000000
  36. #define beam_min_sigma 0.020000
  37. #define beam_max_sigma 0.300000
  38. #define beam_spot_power 0.330000
  39. #define beam_min_shape 2.000000
  40. #define beam_max_shape 4.000000
  41. #define beam_shape_power 0.250000
  42. #define beam_horiz_filter 0.000000
  43. #define beam_horiz_sigma 0.35
  44. #define beam_horiz_linear_rgb_weight 1.000000
  45. #define convergence_offset_x_r -0.000000
  46. #define convergence_offset_x_g 0.000000
  47. #define convergence_offset_x_b 0.000000
  48. #define convergence_offset_y_r 0.000000
  49. #define convergence_offset_y_g -0.000000
  50. #define convergence_offset_y_b 0.000000
  51. #define mask_type 1.000000
  52. #define mask_sample_mode_desired 0.000000
  53. #define mask_specify_num_triads 0.000000
  54. #define mask_triad_size_desired 3.000000
  55. #define mask_num_triads_desired 480.000000
  56. #define aa_subpixel_r_offset_x_runtime -0.0
  57. #define aa_subpixel_r_offset_y_runtime 0.000000
  58. #define aa_cubic_c 0.500000
  59. #define aa_gauss_sigma 0.500000
  60. #define geom_mode_runtime 0.000000
  61. #define geom_radius 2.000000
  62. #define geom_view_dist 2.000000
  63. #define geom_tilt_angle_x 0.000000
  64. #define geom_tilt_angle_y 0.000000
  65. #define geom_aspect_ratio_x 432.000000
  66. #define geom_aspect_ratio_y 329.000000
  67. #define geom_overscan_x 1.000000
  68. #define geom_overscan_y 1.000000
  69. #define border_size 0.015
  70. #define border_darkness 2.0
  71. #define border_compress 2.500000
  72. #define interlace_bff 0.000000
  73. #define interlace_1080i 0.000000
  74. // END USER SETTINGS BLOCK //
  75. // compatibility macros for transparently converting HLSLisms into GLSLisms
  76. #define mul(a,b) (b*a)
  77. #define lerp(a,b,c) mix(a,b,c)
  78. #define saturate(c) clamp(c, 0.0, 1.0)
  79. #define frac(x) (fract(x))
  80. #define float2 vec2
  81. #define float3 vec3
  82. #define float4 vec4
  83. #define bool2 bvec2
  84. #define bool3 bvec3
  85. #define bool4 bvec4
  86. #define float2x2 mat2x2
  87. #define float3x3 mat3x3
  88. #define float4x4 mat4x4
  89. #define float4x3 mat4x3
  90. #define float2x4 mat2x4
  91. #define IN params
  92. #define texture_size sourceSize[0].xy
  93. #define video_size sourceSize[0].xy
  94. #define output_size targetSize.xy
  95. #define frame_count phase
  96. #define static
  97. #define inline
  98. #define const
  99. #define fmod(x,y) mod(x,y)
  100. #define ddx(c) dFdx(c)
  101. #define ddy(c) dFdy(c)
  102. #define atan2(x,y) atan(y,x)
  103. #define rsqrt(c) inversesqrt(c)
  104. #define MASKED_SCANLINEStexture source[0]
  105. #define MASKED_SCANLINEStexture_size sourceSize[0].xy
  106. #define MASKED_SCANLINESvideo_size sourceSize[0].xy
  107. #define BLOOM_APPROXtexture source[3]
  108. #define BLOOM_APPROXtexture_size sourceSize[3].xy
  109. #define BLOOM_APPROXvideo_size sourceSize[3].xy
  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. float bloom_approx_scale_x = targetSize.y / sourceSize[0].y;
  122. const float max_viewport_size_x = 1080.0*1024.0*(4.0/3.0);
  123. const float bloom_diff_thresh_ = 1.0/256.0;
  124. ////////////////////////////// FRAGMENT INCLUDES //////////////////////////////
  125. //#include "bloom-functions.h"
  126. //////////////////////////// BEGIN BLOOM-FUNCTIONS ///////////////////////////
  127. #ifndef BLOOM_FUNCTIONS_H
  128. #define BLOOM_FUNCTIONS_H
  129. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  130. // crt-royale: A full-featured CRT shader, with cheese.
  131. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  132. //
  133. // This program is free software; you can redistribute it and/or modify it
  134. // under the terms of the GNU General Public License as published by the Free
  135. // Software Foundation; either version 2 of the License, or any later version.
  136. //
  137. // This program is distributed in the hope that it will be useful, but WITHOUT
  138. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  139. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  140. // more details.
  141. //
  142. // You should have received a copy of the GNU General Public License along with
  143. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  144. // Place, Suite 330, Boston, MA 02111-1307 USA
  145. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  146. // These utility functions and constants help several passes determine the
  147. // size and center texel weight of the phosphor bloom in a uniform manner.
  148. ////////////////////////////////// INCLUDES //////////////////////////////////
  149. // We need to calculate the correct blur sigma using some .cgp constants:
  150. //#include "../user-settings.h"
  151. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  152. #ifndef USER_SETTINGS_H
  153. #define USER_SETTINGS_H
  154. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  155. // The Cg compiler uses different "profiles" with different capabilities.
  156. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  157. // require higher profiles like fp30 or fp40. The shader can't detect profile
  158. // or driver capabilities, so instead you must comment or uncomment the lines
  159. // below with "//" before "#define." Disable an option if you get compilation
  160. // errors resembling those listed. Generally speaking, all of these options
  161. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  162. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  163. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  164. // Among other things, derivatives help us fix anisotropic filtering artifacts
  165. // with curved manually tiled phosphor mask coords. Related errors:
  166. // error C3004: function "float2 ddx(float2);" not supported in this profile
  167. // error C3004: function "float2 ddy(float2);" not supported in this profile
  168. //#define DRIVERS_ALLOW_DERIVATIVES
  169. // Fine derivatives: Unsupported on older ATI cards.
  170. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  171. // fast single-pass blur operations. If your card uses coarse derivatives and
  172. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  173. #ifdef DRIVERS_ALLOW_DERIVATIVES
  174. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  175. #endif
  176. // Dynamic looping: Requires an fp30 or newer profile.
  177. // This makes phosphor mask resampling faster in some cases. Related errors:
  178. // error C5013: profile does not support "for" statements and "for" could not
  179. // be unrolled
  180. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  181. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  182. // Using one static loop avoids overhead if the user is right, but if the user
  183. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  184. // binary search can potentially save some iterations. However, it may fail:
  185. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  186. // needed to compile program
  187. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  188. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  189. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  190. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  191. // this profile
  192. //#define DRIVERS_ALLOW_TEX2DLOD
  193. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  194. // artifacts from anisotropic filtering and mipmapping. Related errors:
  195. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  196. // in this profile
  197. //#define DRIVERS_ALLOW_TEX2DBIAS
  198. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  199. // impose stricter limitations on register counts and instructions. Enable
  200. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  201. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  202. // to compile program.
  203. // Enabling integrated graphics compatibility mode will automatically disable:
  204. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  205. // (This may be reenabled in a later release.)
  206. // 2.) RUNTIME_GEOMETRY_MODE
  207. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  208. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  209. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  210. // To disable a #define option, turn its line into a comment with "//."
  211. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  212. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  213. // many of the options in this file and allow real-time tuning, but many of
  214. // them are slower. Disabling them and using this text file will boost FPS.
  215. #define RUNTIME_SHADER_PARAMS_ENABLE
  216. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  217. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  218. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  219. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  220. #define RUNTIME_ANTIALIAS_WEIGHTS
  221. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  222. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  223. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  224. // parameters? This will require more math or dynamic branching.
  225. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  226. // Specify the tilt at runtime? This makes things about 3% slower.
  227. #define RUNTIME_GEOMETRY_TILT
  228. // Specify the geometry mode at runtime?
  229. #define RUNTIME_GEOMETRY_MODE
  230. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  231. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  232. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  233. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  234. // PHOSPHOR MASK:
  235. // Manually resize the phosphor mask for best results (slower)? Disabling this
  236. // removes the option to do so, but it may be faster without dynamic branches.
  237. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  238. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  239. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  240. // Larger blurs are expensive, but we need them to blur larger triads. We can
  241. // detect the right blur if the triad size is static or our profile allows
  242. // dynamic branches, but otherwise we use the largest blur the user indicates
  243. // they might need:
  244. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  245. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  246. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  247. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  248. // Here's a helpful chart:
  249. // MaxTriadSize BlurSize MinTriadCountsByResolution
  250. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  251. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  252. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  253. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  254. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  255. /////////////////////////////// USER PARAMETERS //////////////////////////////
  256. // Note: Many of these static parameters are overridden by runtime shader
  257. // parameters when those are enabled. However, many others are static codepath
  258. // options that were cleaner or more convert to code as static constants.
  259. // GAMMA:
  260. static const float crt_gamma_static = 2.5; // range [1, 5]
  261. static const float lcd_gamma_static = 2.2; // range [1, 5]
  262. // LEVELS MANAGEMENT:
  263. // Control the final multiplicative image contrast:
  264. static const float levels_contrast_static = 1.0; // range [0, 4)
  265. // We auto-dim to avoid clipping between passes and restore brightness
  266. // later. Control the dim factor here: Lower values clip less but crush
  267. // blacks more (static only for now).
  268. 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
  269. // HALATION/DIFFUSION/BLOOM:
  270. // Halation weight: How much energy should be lost to electrons bounding
  271. // around under the CRT glass and exciting random phosphors?
  272. static const float halation_weight_static = 0.0; // range [0, 1]
  273. // Refractive diffusion weight: How much light should spread/diffuse from
  274. // refracting through the CRT glass?
  275. static const float diffusion_weight_static = 0.075; // range [0, 1]
  276. // Underestimate brightness: Bright areas bloom more, but we can base the
  277. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  278. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  279. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  280. // Blur all colors more than necessary for a softer phosphor bloom?
  281. static const float bloom_excess_static = 0.0; // range [0, 1]
  282. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  283. // blurred resize of the input (convergence offsets are applied as well).
  284. // There are three filter options (static option only for now):
  285. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  286. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  287. // and beam_max_sigma is low.
  288. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  289. // always uses a static sigma regardless of beam_max_sigma or
  290. // mask_num_triads_desired.
  291. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  292. // These options are more pronounced for the fast, unbloomed shader version.
  293. #ifndef RADEON_FIX
  294. static const float bloom_approx_filter_static = 2.0;
  295. #else
  296. static const float bloom_approx_filter_static = 1.0;
  297. #endif
  298. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  299. // How many scanlines should contribute light to each pixel? Using more
  300. // scanlines is slower (especially for a generalized Gaussian) but less
  301. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  302. // max_beam_sigma at which the closest unused weight is guaranteed <
  303. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  304. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  305. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  306. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  307. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  308. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  309. static const float beam_num_scanlines = 3.0; // range [2, 6]
  310. // A generalized Gaussian beam varies shape with color too, now just width.
  311. // It's slower but more flexible (static option only for now).
  312. static const bool beam_generalized_gaussian = true;
  313. // What kind of scanline antialiasing do you want?
  314. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  315. // Integrals are slow (especially for generalized Gaussians) and rarely any
  316. // better than 3x antialiasing (static option only for now).
  317. static const float beam_antialias_level = 1.0; // range [0, 2]
  318. // Min/max standard deviations for scanline beams: Higher values widen and
  319. // soften scanlines. Depending on other options, low min sigmas can alias.
  320. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  321. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  322. // Beam width varies as a function of color: A power function (0) is more
  323. // configurable, but a spherical function (1) gives the widest beam
  324. // variability without aliasing (static option only for now).
  325. static const float beam_spot_shape_function = 0.0;
  326. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  327. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  328. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  329. // Generalized Gaussian max shape parameters: Higher values give flatter
  330. // scanline plateaus and steeper dropoffs, simultaneously widening and
  331. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  332. // values > ~40.0 cause artifacts with integrals.
  333. static const float beam_min_shape_static = 2.0; // range [2, 32]
  334. static const float beam_max_shape_static = 4.0; // range [2, 32]
  335. // Generalized Gaussian shape power: Affects how quickly the distribution
  336. // changes shape from Gaussian to steep/plateaued as color increases from 0
  337. // to 1.0. Higher powers appear softer for most colors, and lower powers
  338. // appear sharper for most colors.
  339. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  340. // What filter should be used to sample scanlines horizontally?
  341. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  342. static const float beam_horiz_filter_static = 0.0;
  343. // Standard deviation for horizontal Gaussian resampling:
  344. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  345. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  346. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  347. // limiting circuitry in some CRT's), or a weighted avg.?
  348. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  349. // Simulate scanline misconvergence? This needs 3x horizontal texture
  350. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  351. // later passes (static option only for now).
  352. static const bool beam_misconvergence = true;
  353. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  354. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  355. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  356. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  357. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  358. // Detect interlacing (static option only for now)?
  359. static const bool interlace_detect = true;
  360. // Assume 1080-line sources are interlaced?
  361. static const bool interlace_1080i_static = false;
  362. // For interlaced sources, assume TFF (top-field first) or BFF order?
  363. // (Whether this matters depends on the nature of the interlaced input.)
  364. static const bool interlace_bff_static = false;
  365. // ANTIALIASING:
  366. // What AA level do you want for curvature/overscan/subpixels? Options:
  367. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  368. // (Static option only for now)
  369. static const float aa_level = 12.0; // range [0, 24]
  370. // What antialiasing filter do you want (static option only)? Options:
  371. // 0: Box (separable), 1: Box (cylindrical),
  372. // 2: Tent (separable), 3: Tent (cylindrical),
  373. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  374. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  375. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  376. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  377. static const float aa_filter = 6.0; // range [0, 9]
  378. // Flip the sample grid on odd/even frames (static option only for now)?
  379. static const bool aa_temporal = false;
  380. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  381. // the blue offset is the negative r offset; range [0, 0.5]
  382. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  383. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  384. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  385. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  386. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  387. // 4.) C = 0.0 is a soft spline filter.
  388. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  389. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  390. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  391. // PHOSPHOR MASK:
  392. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  393. static const float mask_type_static = 1.0; // range [0, 2]
  394. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  395. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  396. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  397. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  398. // is halfway decent with LUT mipmapping but atrocious without it.
  399. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  400. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  401. // This mode reuses the same masks, so triads will be enormous unless
  402. // you change the mask LUT filenames in your .cgp file.
  403. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  404. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  405. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  406. // will always be used to calculate the full bloom sigma statically.
  407. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  408. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  409. // triads) will be rounded to the nearest integer tile size and clamped to
  410. // obey minimum size constraints (imposed to reduce downsize taps) and
  411. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  412. // To increase the size limit, double the viewport-relative scales for the
  413. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  414. // range [1, mask_texture_small_size/mask_triads_per_tile]
  415. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  416. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  417. // final size will be rounded and constrained as above); default 480.0
  418. static const float mask_num_triads_desired_static = 480.0;
  419. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  420. // more samples and avoid moire a bit better, but some is unavoidable
  421. // depending on the destination size (static option for now).
  422. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  423. // The mask is resized using a variable number of taps in each dimension,
  424. // but some Cg profiles always fetch a constant number of taps no matter
  425. // what (no dynamic branching). We can limit the maximum number of taps if
  426. // we statically limit the minimum phosphor triad size. Larger values are
  427. // faster, but the limit IS enforced (static option only, forever);
  428. // range [1, mask_texture_small_size/mask_triads_per_tile]
  429. // TODO: Make this 1.0 and compensate with smarter sampling!
  430. static const float mask_min_allowed_triad_size = 2.0;
  431. // GEOMETRY:
  432. // Geometry mode:
  433. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  434. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  435. static const float geom_mode_static = 0.0; // range [0, 3]
  436. // Radius of curvature: Measured in units of your viewport's diagonal size.
  437. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  438. // View dist is the distance from the player to their physical screen, in
  439. // units of the viewport's diagonal size. It controls the field of view.
  440. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  441. // Tilt angle in radians (clockwise around up and right vectors):
  442. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  443. // Aspect ratio: When the true viewport size is unknown, this value is used
  444. // to help convert between the phosphor triad size and count, along with
  445. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  446. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  447. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  448. // default (256/224)*(54/47) = 1.313069909 (see below)
  449. static const float geom_aspect_ratio_static = 1.313069909;
  450. // Before getting into overscan, here's some general aspect ratio info:
  451. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  452. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  453. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  454. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  455. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  456. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  457. // a.) Enable Retroarch's "Crop Overscan"
  458. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  459. // Real consoles use horizontal black padding in the signal, but emulators
  460. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  461. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  462. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  463. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  464. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  465. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  466. // without doing a. or b., but horizontal image borders will be tighter
  467. // than vertical ones, messing up curvature and overscan. Fixing the
  468. // padding first corrects this.
  469. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  470. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  471. // above: Values < 1.0 zoom out; range (0, inf)
  472. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  473. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  474. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  475. // with strong curvature (static option only for now).
  476. static const bool geom_force_correct_tangent_matrix = true;
  477. // BORDERS:
  478. // Rounded border size in texture uv coords:
  479. static const float border_size_static = 0.015; // range [0, 0.5]
  480. // Border darkness: Moderate values darken the border smoothly, and high
  481. // values make the image very dark just inside the border:
  482. static const float border_darkness_static = 2.0; // range [0, inf)
  483. // Border compression: High numbers compress border transitions, narrowing
  484. // the dark border area.
  485. static const float border_compress_static = 2.5; // range [1, inf)
  486. #endif // USER_SETTINGS_H
  487. //////////////////////////// END USER-SETTINGS //////////////////////////
  488. //#include "derived-settings-and-constants.h"
  489. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  490. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  491. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  492. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  493. // crt-royale: A full-featured CRT shader, with cheese.
  494. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  495. //
  496. // This program is free software; you can redistribute it and/or modify it
  497. // under the terms of the GNU General Public License as published by the Free
  498. // Software Foundation; either version 2 of the License, or any later version.
  499. //
  500. // This program is distributed in the hope that it will be useful, but WITHOUT
  501. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  502. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  503. // more details.
  504. //
  505. // You should have received a copy of the GNU General Public License along with
  506. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  507. // Place, Suite 330, Boston, MA 02111-1307 USA
  508. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  509. // These macros and constants can be used across the whole codebase.
  510. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  511. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  512. //#include "../user-settings.h"
  513. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  514. #ifndef USER_SETTINGS_H
  515. #define USER_SETTINGS_H
  516. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  517. // The Cg compiler uses different "profiles" with different capabilities.
  518. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  519. // require higher profiles like fp30 or fp40. The shader can't detect profile
  520. // or driver capabilities, so instead you must comment or uncomment the lines
  521. // below with "//" before "#define." Disable an option if you get compilation
  522. // errors resembling those listed. Generally speaking, all of these options
  523. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  524. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  525. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  526. // Among other things, derivatives help us fix anisotropic filtering artifacts
  527. // with curved manually tiled phosphor mask coords. Related errors:
  528. // error C3004: function "float2 ddx(float2);" not supported in this profile
  529. // error C3004: function "float2 ddy(float2);" not supported in this profile
  530. //#define DRIVERS_ALLOW_DERIVATIVES
  531. // Fine derivatives: Unsupported on older ATI cards.
  532. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  533. // fast single-pass blur operations. If your card uses coarse derivatives and
  534. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  535. #ifdef DRIVERS_ALLOW_DERIVATIVES
  536. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  537. #endif
  538. // Dynamic looping: Requires an fp30 or newer profile.
  539. // This makes phosphor mask resampling faster in some cases. Related errors:
  540. // error C5013: profile does not support "for" statements and "for" could not
  541. // be unrolled
  542. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  543. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  544. // Using one static loop avoids overhead if the user is right, but if the user
  545. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  546. // binary search can potentially save some iterations. However, it may fail:
  547. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  548. // needed to compile program
  549. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  550. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  551. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  552. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  553. // this profile
  554. //#define DRIVERS_ALLOW_TEX2DLOD
  555. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  556. // artifacts from anisotropic filtering and mipmapping. Related errors:
  557. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  558. // in this profile
  559. //#define DRIVERS_ALLOW_TEX2DBIAS
  560. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  561. // impose stricter limitations on register counts and instructions. Enable
  562. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  563. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  564. // to compile program.
  565. // Enabling integrated graphics compatibility mode will automatically disable:
  566. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  567. // (This may be reenabled in a later release.)
  568. // 2.) RUNTIME_GEOMETRY_MODE
  569. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  570. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  571. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  572. // To disable a #define option, turn its line into a comment with "//."
  573. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  574. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  575. // many of the options in this file and allow real-time tuning, but many of
  576. // them are slower. Disabling them and using this text file will boost FPS.
  577. #define RUNTIME_SHADER_PARAMS_ENABLE
  578. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  579. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  580. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  581. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  582. #define RUNTIME_ANTIALIAS_WEIGHTS
  583. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  584. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  585. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  586. // parameters? This will require more math or dynamic branching.
  587. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  588. // Specify the tilt at runtime? This makes things about 3% slower.
  589. #define RUNTIME_GEOMETRY_TILT
  590. // Specify the geometry mode at runtime?
  591. #define RUNTIME_GEOMETRY_MODE
  592. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  593. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  594. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  595. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  596. // PHOSPHOR MASK:
  597. // Manually resize the phosphor mask for best results (slower)? Disabling this
  598. // removes the option to do so, but it may be faster without dynamic branches.
  599. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  600. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  601. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  602. // Larger blurs are expensive, but we need them to blur larger triads. We can
  603. // detect the right blur if the triad size is static or our profile allows
  604. // dynamic branches, but otherwise we use the largest blur the user indicates
  605. // they might need:
  606. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  607. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  608. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  609. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  610. // Here's a helpful chart:
  611. // MaxTriadSize BlurSize MinTriadCountsByResolution
  612. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  613. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  614. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  615. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  616. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  617. /////////////////////////////// USER PARAMETERS //////////////////////////////
  618. // Note: Many of these static parameters are overridden by runtime shader
  619. // parameters when those are enabled. However, many others are static codepath
  620. // options that were cleaner or more convert to code as static constants.
  621. // GAMMA:
  622. static const float crt_gamma_static = 2.5; // range [1, 5]
  623. static const float lcd_gamma_static = 2.2; // range [1, 5]
  624. // LEVELS MANAGEMENT:
  625. // Control the final multiplicative image contrast:
  626. static const float levels_contrast_static = 1.0; // range [0, 4)
  627. // We auto-dim to avoid clipping between passes and restore brightness
  628. // later. Control the dim factor here: Lower values clip less but crush
  629. // blacks more (static only for now).
  630. 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
  631. // HALATION/DIFFUSION/BLOOM:
  632. // Halation weight: How much energy should be lost to electrons bounding
  633. // around under the CRT glass and exciting random phosphors?
  634. static const float halation_weight_static = 0.0; // range [0, 1]
  635. // Refractive diffusion weight: How much light should spread/diffuse from
  636. // refracting through the CRT glass?
  637. static const float diffusion_weight_static = 0.075; // range [0, 1]
  638. // Underestimate brightness: Bright areas bloom more, but we can base the
  639. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  640. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  641. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  642. // Blur all colors more than necessary for a softer phosphor bloom?
  643. static const float bloom_excess_static = 0.0; // range [0, 1]
  644. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  645. // blurred resize of the input (convergence offsets are applied as well).
  646. // There are three filter options (static option only for now):
  647. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  648. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  649. // and beam_max_sigma is low.
  650. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  651. // always uses a static sigma regardless of beam_max_sigma or
  652. // mask_num_triads_desired.
  653. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  654. // These options are more pronounced for the fast, unbloomed shader version.
  655. #ifndef RADEON_FIX
  656. static const float bloom_approx_filter_static = 2.0;
  657. #else
  658. static const float bloom_approx_filter_static = 1.0;
  659. #endif
  660. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  661. // How many scanlines should contribute light to each pixel? Using more
  662. // scanlines is slower (especially for a generalized Gaussian) but less
  663. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  664. // max_beam_sigma at which the closest unused weight is guaranteed <
  665. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  666. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  667. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  668. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  669. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  670. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  671. static const float beam_num_scanlines = 3.0; // range [2, 6]
  672. // A generalized Gaussian beam varies shape with color too, now just width.
  673. // It's slower but more flexible (static option only for now).
  674. static const bool beam_generalized_gaussian = true;
  675. // What kind of scanline antialiasing do you want?
  676. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  677. // Integrals are slow (especially for generalized Gaussians) and rarely any
  678. // better than 3x antialiasing (static option only for now).
  679. static const float beam_antialias_level = 1.0; // range [0, 2]
  680. // Min/max standard deviations for scanline beams: Higher values widen and
  681. // soften scanlines. Depending on other options, low min sigmas can alias.
  682. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  683. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  684. // Beam width varies as a function of color: A power function (0) is more
  685. // configurable, but a spherical function (1) gives the widest beam
  686. // variability without aliasing (static option only for now).
  687. static const float beam_spot_shape_function = 0.0;
  688. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  689. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  690. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  691. // Generalized Gaussian max shape parameters: Higher values give flatter
  692. // scanline plateaus and steeper dropoffs, simultaneously widening and
  693. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  694. // values > ~40.0 cause artifacts with integrals.
  695. static const float beam_min_shape_static = 2.0; // range [2, 32]
  696. static const float beam_max_shape_static = 4.0; // range [2, 32]
  697. // Generalized Gaussian shape power: Affects how quickly the distribution
  698. // changes shape from Gaussian to steep/plateaued as color increases from 0
  699. // to 1.0. Higher powers appear softer for most colors, and lower powers
  700. // appear sharper for most colors.
  701. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  702. // What filter should be used to sample scanlines horizontally?
  703. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  704. static const float beam_horiz_filter_static = 0.0;
  705. // Standard deviation for horizontal Gaussian resampling:
  706. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  707. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  708. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  709. // limiting circuitry in some CRT's), or a weighted avg.?
  710. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  711. // Simulate scanline misconvergence? This needs 3x horizontal texture
  712. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  713. // later passes (static option only for now).
  714. static const bool beam_misconvergence = true;
  715. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  716. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  717. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  718. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  719. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  720. // Detect interlacing (static option only for now)?
  721. static const bool interlace_detect = true;
  722. // Assume 1080-line sources are interlaced?
  723. static const bool interlace_1080i_static = false;
  724. // For interlaced sources, assume TFF (top-field first) or BFF order?
  725. // (Whether this matters depends on the nature of the interlaced input.)
  726. static const bool interlace_bff_static = false;
  727. // ANTIALIASING:
  728. // What AA level do you want for curvature/overscan/subpixels? Options:
  729. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  730. // (Static option only for now)
  731. static const float aa_level = 12.0; // range [0, 24]
  732. // What antialiasing filter do you want (static option only)? Options:
  733. // 0: Box (separable), 1: Box (cylindrical),
  734. // 2: Tent (separable), 3: Tent (cylindrical),
  735. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  736. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  737. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  738. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  739. static const float aa_filter = 6.0; // range [0, 9]
  740. // Flip the sample grid on odd/even frames (static option only for now)?
  741. static const bool aa_temporal = false;
  742. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  743. // the blue offset is the negative r offset; range [0, 0.5]
  744. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  745. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  746. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  747. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  748. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  749. // 4.) C = 0.0 is a soft spline filter.
  750. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  751. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  752. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  753. // PHOSPHOR MASK:
  754. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  755. static const float mask_type_static = 1.0; // range [0, 2]
  756. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  757. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  758. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  759. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  760. // is halfway decent with LUT mipmapping but atrocious without it.
  761. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  762. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  763. // This mode reuses the same masks, so triads will be enormous unless
  764. // you change the mask LUT filenames in your .cgp file.
  765. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  766. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  767. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  768. // will always be used to calculate the full bloom sigma statically.
  769. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  770. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  771. // triads) will be rounded to the nearest integer tile size and clamped to
  772. // obey minimum size constraints (imposed to reduce downsize taps) and
  773. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  774. // To increase the size limit, double the viewport-relative scales for the
  775. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  776. // range [1, mask_texture_small_size/mask_triads_per_tile]
  777. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  778. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  779. // final size will be rounded and constrained as above); default 480.0
  780. static const float mask_num_triads_desired_static = 480.0;
  781. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  782. // more samples and avoid moire a bit better, but some is unavoidable
  783. // depending on the destination size (static option for now).
  784. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  785. // The mask is resized using a variable number of taps in each dimension,
  786. // but some Cg profiles always fetch a constant number of taps no matter
  787. // what (no dynamic branching). We can limit the maximum number of taps if
  788. // we statically limit the minimum phosphor triad size. Larger values are
  789. // faster, but the limit IS enforced (static option only, forever);
  790. // range [1, mask_texture_small_size/mask_triads_per_tile]
  791. // TODO: Make this 1.0 and compensate with smarter sampling!
  792. static const float mask_min_allowed_triad_size = 2.0;
  793. // GEOMETRY:
  794. // Geometry mode:
  795. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  796. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  797. static const float geom_mode_static = 0.0; // range [0, 3]
  798. // Radius of curvature: Measured in units of your viewport's diagonal size.
  799. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  800. // View dist is the distance from the player to their physical screen, in
  801. // units of the viewport's diagonal size. It controls the field of view.
  802. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  803. // Tilt angle in radians (clockwise around up and right vectors):
  804. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  805. // Aspect ratio: When the true viewport size is unknown, this value is used
  806. // to help convert between the phosphor triad size and count, along with
  807. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  808. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  809. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  810. // default (256/224)*(54/47) = 1.313069909 (see below)
  811. static const float geom_aspect_ratio_static = 1.313069909;
  812. // Before getting into overscan, here's some general aspect ratio info:
  813. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  814. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  815. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  816. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  817. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  818. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  819. // a.) Enable Retroarch's "Crop Overscan"
  820. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  821. // Real consoles use horizontal black padding in the signal, but emulators
  822. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  823. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  824. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  825. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  826. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  827. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  828. // without doing a. or b., but horizontal image borders will be tighter
  829. // than vertical ones, messing up curvature and overscan. Fixing the
  830. // padding first corrects this.
  831. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  832. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  833. // above: Values < 1.0 zoom out; range (0, inf)
  834. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  835. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  836. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  837. // with strong curvature (static option only for now).
  838. static const bool geom_force_correct_tangent_matrix = true;
  839. // BORDERS:
  840. // Rounded border size in texture uv coords:
  841. static const float border_size_static = 0.015; // range [0, 0.5]
  842. // Border darkness: Moderate values darken the border smoothly, and high
  843. // values make the image very dark just inside the border:
  844. static const float border_darkness_static = 2.0; // range [0, inf)
  845. // Border compression: High numbers compress border transitions, narrowing
  846. // the dark border area.
  847. static const float border_compress_static = 2.5; // range [1, inf)
  848. #endif // USER_SETTINGS_H
  849. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  850. //#include "user-cgp-constants.h"
  851. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  852. #ifndef USER_CGP_CONSTANTS_H
  853. #define USER_CGP_CONSTANTS_H
  854. // IMPORTANT:
  855. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  856. // (or whatever related .cgp file you're using). If they aren't, you're likely
  857. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  858. // set directly in the .cgp file to make things easier, but...they can't.
  859. // PASS SCALES AND RELATED CONSTANTS:
  860. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  861. // this shader: One does a viewport-scale bloom, and the other skips it. The
  862. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  863. static const float bloom_approx_size_x = 320.0;
  864. static const float bloom_approx_size_x_for_fake = 400.0;
  865. // Copy the viewport-relative scales of the phosphor mask resize passes
  866. // (MASK_RESIZE and the pass immediately preceding it):
  867. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  868. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  869. static const float geom_max_aspect_ratio = 4.0/3.0;
  870. // PHOSPHOR MASK TEXTURE CONSTANTS:
  871. // Set the following constants to reflect the properties of the phosphor mask
  872. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  873. // based on user settings, then repeats a single tile until filling the screen.
  874. // The shader must know the input texture size (default 64x64), and to manually
  875. // resize, it must also know the horizontal triads per tile (default 8).
  876. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  877. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  878. static const float mask_triads_per_tile = 8.0;
  879. // We need the average brightness of the phosphor mask to compensate for the
  880. // dimming it causes. The following four values are roughly correct for the
  881. // masks included with the shader. Update the value for any LUT texture you
  882. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  883. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  884. //#define PHOSPHOR_MASK_GRILLE14
  885. static const float mask_grille14_avg_color = 50.6666666/255.0;
  886. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  887. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  888. static const float mask_grille15_avg_color = 53.0/255.0;
  889. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  890. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  891. static const float mask_slot_avg_color = 46.0/255.0;
  892. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  893. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  894. static const float mask_shadow_avg_color = 41.0/255.0;
  895. // TileableLinearShadowMask*.png
  896. // TileableLinearShadowMaskEDP*.png
  897. #ifdef PHOSPHOR_MASK_GRILLE14
  898. static const float mask_grille_avg_color = mask_grille14_avg_color;
  899. #else
  900. static const float mask_grille_avg_color = mask_grille15_avg_color;
  901. #endif
  902. #endif // USER_CGP_CONSTANTS_H
  903. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  904. //////////////////////////////// END INCLUDES ////////////////////////////////
  905. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  906. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  907. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  908. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  909. #ifndef SIMULATE_CRT_ON_LCD
  910. #define SIMULATE_CRT_ON_LCD
  911. #endif
  912. // Manually tiling a manually resized texture creates texture coord derivative
  913. // discontinuities and confuses anisotropic filtering, causing discolored tile
  914. // seams in the phosphor mask. Workarounds:
  915. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  916. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  917. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  918. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  919. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  920. // (Same-pass curvature isn't used but could be in the future...maybe.)
  921. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  922. // border padding to the resized mask FBO, but it works with same-pass
  923. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  924. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  925. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  926. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  927. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  928. // Also, manually resampling the phosphor mask is slightly blurrier with
  929. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  930. // creates artifacts, but only with the fully bloomed shader.) The difference
  931. // is subtle with small triads, but you can fix it for a small cost.
  932. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  933. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  934. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  935. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  936. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  937. // #defined by either user-settings.h or a wrapper .cg that #includes the
  938. // current .cg pass.)
  939. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  940. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  941. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  942. #endif
  943. #ifdef RUNTIME_GEOMETRY_MODE
  944. #undef RUNTIME_GEOMETRY_MODE
  945. #endif
  946. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  947. // inferior in most cases, so replace 2.0 with 0.0:
  948. static const float bloom_approx_filter =
  949. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  950. #else
  951. static const float bloom_approx_filter = bloom_approx_filter_static;
  952. #endif
  953. // Disable slow runtime paths if static parameters are used. Most of these
  954. // won't be a problem anyway once the params are disabled, but some will.
  955. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  956. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  957. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  958. #endif
  959. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  960. #undef RUNTIME_ANTIALIAS_WEIGHTS
  961. #endif
  962. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  963. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  964. #endif
  965. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  966. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  967. #endif
  968. #ifdef RUNTIME_GEOMETRY_TILT
  969. #undef RUNTIME_GEOMETRY_TILT
  970. #endif
  971. #ifdef RUNTIME_GEOMETRY_MODE
  972. #undef RUNTIME_GEOMETRY_MODE
  973. #endif
  974. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  975. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  976. #endif
  977. #endif
  978. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  979. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  980. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  981. #endif
  982. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  983. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  984. #endif
  985. // Rule out unavailable anisotropic compatibility strategies:
  986. #ifndef DRIVERS_ALLOW_DERIVATIVES
  987. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  988. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  989. #endif
  990. #endif
  991. #ifndef DRIVERS_ALLOW_TEX2DLOD
  992. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  993. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  994. #endif
  995. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  996. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  997. #endif
  998. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  999. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  1000. #endif
  1001. #endif
  1002. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  1003. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1004. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1005. #endif
  1006. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1007. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1008. #endif
  1009. #endif
  1010. // Prioritize anisotropic tiling compatibility strategies by performance and
  1011. // disable unused strategies. This concentrates all the nesting in one place.
  1012. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1013. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1014. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1015. #endif
  1016. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1017. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1018. #endif
  1019. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1020. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1021. #endif
  1022. #else
  1023. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1024. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1025. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1026. #endif
  1027. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1028. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1029. #endif
  1030. #else
  1031. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  1032. // flat texture coords in the same pass, but that's all we use.
  1033. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1034. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1035. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1036. #endif
  1037. #endif
  1038. #endif
  1039. #endif
  1040. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  1041. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  1042. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1043. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1044. #endif
  1045. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1046. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1047. #endif
  1048. // Prioritize anisotropic resampling compatibility strategies the same way:
  1049. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1050. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1051. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1052. #endif
  1053. #endif
  1054. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  1055. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  1056. // should: It gives us more options for using fewer samples.
  1057. #ifdef DRIVERS_ALLOW_TEX2DLOD
  1058. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1059. // TODO: Take advantage of this!
  1060. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  1061. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  1062. #else
  1063. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1064. #endif
  1065. #else
  1066. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1067. #endif
  1068. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  1069. // main_fragment, or a static alias of one of the above. This makes it hard
  1070. // to select the phosphor mask at runtime: We can't even assign to a uniform
  1071. // global in the vertex shader or select a sampler2D in the vertex shader and
  1072. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  1073. // because it just gives us the input texture or a black screen. However, we
  1074. // can get around these limitations by calling tex2D three times with different
  1075. // uniform samplers (or resizing the phosphor mask three times altogether).
  1076. // With dynamic branches, we can process only one of these branches on top of
  1077. // quickly discarding fragments we don't need (cgc seems able to overcome
  1078. // limigations around dependent texture fetches inside of branches). Without
  1079. // dynamic branches, we have to process every branch for every fragment...which
  1080. // is slower. Runtime sampling mode selection is slower without dynamic
  1081. // branches as well. Let the user's static #defines decide if it's worth it.
  1082. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1083. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1084. #else
  1085. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1086. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1087. #endif
  1088. #endif
  1089. // We need to render some minimum number of tiles in the resize passes.
  1090. // We need at least 1.0 just to repeat a single tile, and we need extra
  1091. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  1092. // antialiasing, same-pass curvature (not currently used), etc. First
  1093. // determine how many border texels and tiles we need, based on how the result
  1094. // will be sampled:
  1095. #ifdef GEOMETRY_EARLY
  1096. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  1097. // Most antialiasing filters have a base radius of 4.0 pixels:
  1098. static const float max_aa_base_pixel_border = 4.0 +
  1099. max_subpixel_offset;
  1100. #else
  1101. static const float max_aa_base_pixel_border = 0.0;
  1102. #endif
  1103. // Anisotropic filtering adds about 0.5 to the pixel border:
  1104. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1105. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  1106. #else
  1107. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  1108. #endif
  1109. // Fixing discontinuities adds 1.0 more to the pixel border:
  1110. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1111. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  1112. #else
  1113. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  1114. #endif
  1115. // Convert the pixel border to an integer texel border. Assume same-pass
  1116. // curvature about triples the texel frequency:
  1117. #ifdef GEOMETRY_EARLY
  1118. static const float max_mask_texel_border =
  1119. ceil(max_tiled_pixel_border * 3.0);
  1120. #else
  1121. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  1122. #endif
  1123. // Convert the texel border to a tile border using worst-case assumptions:
  1124. static const float max_mask_tile_border = max_mask_texel_border/
  1125. (mask_min_allowed_triad_size * mask_triads_per_tile);
  1126. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  1127. // the starting texel (inside borders) for sampling it.
  1128. #ifndef GEOMETRY_EARLY
  1129. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1130. // Special case: Render two tiles without borders. Anisotropic
  1131. // filtering doesn't seem to be a problem here.
  1132. static const float mask_resize_num_tiles = 1.0 + 1.0;
  1133. static const float mask_start_texels = 0.0;
  1134. #else
  1135. static const float mask_resize_num_tiles = 1.0 +
  1136. 2.0 * max_mask_tile_border;
  1137. static const float mask_start_texels = max_mask_texel_border;
  1138. #endif
  1139. #else
  1140. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  1141. static const float mask_start_texels = max_mask_texel_border;
  1142. #endif
  1143. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  1144. // mask_resize_viewport_scale. This limits the maximum final triad size.
  1145. // Estimate the minimum number of triads we can split the screen into in each
  1146. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  1147. static const float mask_resize_num_triads =
  1148. mask_resize_num_tiles * mask_triads_per_tile;
  1149. static const float2 min_allowed_viewport_triads =
  1150. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  1151. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  1152. static const float pi = 3.141592653589;
  1153. // We often want to find the location of the previous texel, e.g.:
  1154. // const float2 curr_texel = uv * texture_size;
  1155. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  1156. // const float2 prev_texel_uv = prev_texel / texture_size;
  1157. // However, many GPU drivers round incorrectly around exact texel locations.
  1158. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  1159. // require this value to be farther from 0.5 than others; define it here.
  1160. // const float2 prev_texel =
  1161. // floor(curr_texel - float2(under_half)) + float2(0.5);
  1162. static const float under_half = 0.4995;
  1163. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  1164. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  1165. //#include "../../../../include/blur-functions.h"
  1166. //////////////////////////// BEGIN BLUR-FUNCTIONS ///////////////////////////
  1167. #ifndef BLUR_FUNCTIONS_H
  1168. #define BLUR_FUNCTIONS_H
  1169. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  1170. // Copyright (C) 2014 TroggleMonkey
  1171. //
  1172. // Permission is hereby granted, free of charge, to any person obtaining a copy
  1173. // of this software and associated documentation files (the "Software"), to
  1174. // deal in the Software without restriction, including without limitation the
  1175. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1176. // sell copies of the Software, and to permit persons to whom the Software is
  1177. // furnished to do so, subject to the following conditions:
  1178. //
  1179. // The above copyright notice and this permission notice shall be included in
  1180. // all copies or substantial portions of the Software.
  1181. //
  1182. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1183. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1184. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1185. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1186. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1187. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1188. // IN THE SOFTWARE.
  1189. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  1190. // This file provides reusable one-pass and separable (two-pass) blurs.
  1191. // Requires: All blurs share these requirements (dxdy requirement is split):
  1192. // 1.) All requirements of gamma-management.h must be satisfied!
  1193. // 2.) filter_linearN must == "true" in your .cgp preset unless
  1194. // you're using tex2DblurNresize at 1x scale.
  1195. // 3.) mipmap_inputN must == "true" in your .cgp preset if
  1196. // IN.output_size < IN.video_size.
  1197. // 4.) IN.output_size == IN.video_size / pow(2, M), where M is some
  1198. // positive integer. tex2Dblur*resize can resize arbitrarily
  1199. // (and the blur will be done after resizing), but arbitrary
  1200. // resizes "fail" with other blurs due to the way they mix
  1201. // static weights with bilinear sample exploitation.
  1202. // 5.) In general, dxdy should contain the uv pixel spacing:
  1203. // dxdy = (IN.video_size/IN.output_size)/IN.texture_size
  1204. // 6.) For separable blurs (tex2DblurNresize and tex2DblurNfast),
  1205. // zero out the dxdy component in the unblurred dimension:
  1206. // dxdy = float2(dxdy.x, 0.0) or float2(0.0, dxdy.y)
  1207. // Many blurs share these requirements:
  1208. // 1.) One-pass blurs require scale_xN == scale_yN or scales > 1.0,
  1209. // or they will blur more in the lower-scaled dimension.
  1210. // 2.) One-pass shared sample blurs require ddx(), ddy(), and
  1211. // tex2Dlod() to be supported by the current Cg profile, and
  1212. // the drivers must support high-quality derivatives.
  1213. // 3.) One-pass shared sample blurs require:
  1214. // tex_uv.w == log2(IN.video_size/IN.output_size).y;
  1215. // Non-wrapper blurs share this requirement:
  1216. // 1.) sigma is the intended standard deviation of the blur
  1217. // Wrapper blurs share this requirement, which is automatically
  1218. // met (unless OVERRIDE_BLUR_STD_DEVS is #defined; see below):
  1219. // 1.) blurN_std_dev must be global static const float values
  1220. // specifying standard deviations for Nx blurs in units
  1221. // of destination pixels
  1222. // Optional: 1.) The including file (or an earlier included file) may
  1223. // optionally #define USE_BINOMIAL_BLUR_STD_DEVS to replace
  1224. // default standard deviations with those matching a binomial
  1225. // distribution. (See below for details/properties.)
  1226. // 2.) The including file (or an earlier included file) may
  1227. // optionally #define OVERRIDE_BLUR_STD_DEVS and override:
  1228. // static const float blur3_std_dev
  1229. // static const float blur4_std_dev
  1230. // static const float blur5_std_dev
  1231. // static const float blur6_std_dev
  1232. // static const float blur7_std_dev
  1233. // static const float blur8_std_dev
  1234. // static const float blur9_std_dev
  1235. // static const float blur10_std_dev
  1236. // static const float blur11_std_dev
  1237. // static const float blur12_std_dev
  1238. // static const float blur17_std_dev
  1239. // static const float blur25_std_dev
  1240. // static const float blur31_std_dev
  1241. // static const float blur43_std_dev
  1242. // 3.) The including file (or an earlier included file) may
  1243. // optionally #define OVERRIDE_ERROR_BLURRING and override:
  1244. // static const float error_blurring
  1245. // This tuning value helps mitigate weighting errors from one-
  1246. // pass shared-sample blurs sharing bilinear samples between
  1247. // fragments. Values closer to 0.0 have "correct" blurriness
  1248. // but allow more artifacts, and values closer to 1.0 blur away
  1249. // artifacts by sampling closer to halfway between texels.
  1250. // UPDATE 6/21/14: The above static constants may now be overridden
  1251. // by non-static uniform constants. This permits exposing blur
  1252. // standard deviations as runtime GUI shader parameters. However,
  1253. // using them keeps weights from being statically computed, and the
  1254. // speed hit depends on the blur: On my machine, uniforms kill over
  1255. // 53% of the framerate with tex2Dblur12x12shared, but they only
  1256. // drop the framerate by about 18% with tex2Dblur11fast.
  1257. // Quality and Performance Comparisons:
  1258. // For the purposes of the following discussion, "no sRGB" means
  1259. // GAMMA_ENCODE_EVERY_FBO is #defined, and "sRGB" means it isn't.
  1260. // 1.) tex2DblurNfast is always faster than tex2DblurNresize.
  1261. // 2.) tex2DblurNresize functions are the only ones that can arbitrarily resize
  1262. // well, because they're the only ones that don't exploit bilinear samples.
  1263. // This also means they're the only functions which can be truly gamma-
  1264. // correct without linear (or sRGB FBO) input, but only at 1x scale.
  1265. // 3.) One-pass shared sample blurs only have a speed advantage without sRGB.
  1266. // They also have some inaccuracies due to their shared-[bilinear-]sample
  1267. // design, which grow increasingly bothersome for smaller blurs and higher-
  1268. // frequency source images (relative to their resolution). I had high
  1269. // hopes for them, but their most realistic use case is limited to quickly
  1270. // reblurring an already blurred input at full resolution. Otherwise:
  1271. // a.) If you're blurring a low-resolution source, you want a better blur.
  1272. // b.) If you're blurring a lower mipmap, you want a better blur.
  1273. // c.) If you're blurring a high-resolution, high-frequency source, you
  1274. // want a better blur.
  1275. // 4.) The one-pass blurs without shared samples grow slower for larger blurs,
  1276. // but they're competitive with separable blurs at 5x5 and smaller, and
  1277. // even tex2Dblur7x7 isn't bad if you're wanting to conserve passes.
  1278. // Here are some framerates from a GeForce 8800GTS. The first pass resizes to
  1279. // viewport size (4x in this test) and linearizes for sRGB codepaths, and the
  1280. // remaining passes perform 6 full blurs. Mipmapped tests are performed at the
  1281. // same scale, so they just measure the cost of mipmapping each FBO (only every
  1282. // other FBO is mipmapped for separable blurs, to mimic realistic usage).
  1283. // Mipmap Neither sRGB+Mipmap sRGB Function
  1284. // 76.0 92.3 131.3 193.7 tex2Dblur3fast
  1285. // 63.2 74.4 122.4 175.5 tex2Dblur3resize
  1286. // 93.7 121.2 159.3 263.2 tex2Dblur3x3
  1287. // 59.7 68.7 115.4 162.1 tex2Dblur3x3resize
  1288. // 63.2 74.4 122.4 175.5 tex2Dblur5fast
  1289. // 49.3 54.8 100.0 132.7 tex2Dblur5resize
  1290. // 59.7 68.7 115.4 162.1 tex2Dblur5x5
  1291. // 64.9 77.2 99.1 137.2 tex2Dblur6x6shared
  1292. // 55.8 63.7 110.4 151.8 tex2Dblur7fast
  1293. // 39.8 43.9 83.9 105.8 tex2Dblur7resize
  1294. // 40.0 44.2 83.2 104.9 tex2Dblur7x7
  1295. // 56.4 65.5 71.9 87.9 tex2Dblur8x8shared
  1296. // 49.3 55.1 99.9 132.5 tex2Dblur9fast
  1297. // 33.3 36.2 72.4 88.0 tex2Dblur9resize
  1298. // 27.8 29.7 61.3 72.2 tex2Dblur9x9
  1299. // 37.2 41.1 52.6 60.2 tex2Dblur10x10shared
  1300. // 44.4 49.5 91.3 117.8 tex2Dblur11fast
  1301. // 28.8 30.8 63.6 75.4 tex2Dblur11resize
  1302. // 33.6 36.5 40.9 45.5 tex2Dblur12x12shared
  1303. // TODO: Fill in benchmarks for new untested blurs.
  1304. // tex2Dblur17fast
  1305. // tex2Dblur25fast
  1306. // tex2Dblur31fast
  1307. // tex2Dblur43fast
  1308. // tex2Dblur3x3resize
  1309. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  1310. // Set static standard deviations, but allow users to override them with their
  1311. // own constants (even non-static uniforms if they're okay with the speed hit):
  1312. #ifndef OVERRIDE_BLUR_STD_DEVS
  1313. // blurN_std_dev values are specified in terms of dxdy strides.
  1314. #ifdef USE_BINOMIAL_BLUR_STD_DEVS
  1315. // By request, we can define standard deviations corresponding to a
  1316. // binomial distribution with p = 0.5 (related to Pascal's triangle).
  1317. // This distribution works such that blurring multiple times should
  1318. // have the same result as a single larger blur. These values are
  1319. // larger than default for blurs up to 6x and smaller thereafter.
  1320. static const float blur3_std_dev = 0.84931640625;
  1321. static const float blur4_std_dev = 0.84931640625;
  1322. static const float blur5_std_dev = 1.0595703125;
  1323. static const float blur6_std_dev = 1.06591796875;
  1324. static const float blur7_std_dev = 1.17041015625;
  1325. static const float blur8_std_dev = 1.1720703125;
  1326. static const float blur9_std_dev = 1.2259765625;
  1327. static const float blur10_std_dev = 1.21982421875;
  1328. static const float blur11_std_dev = 1.25361328125;
  1329. static const float blur12_std_dev = 1.2423828125;
  1330. static const float blur17_std_dev = 1.27783203125;
  1331. static const float blur25_std_dev = 1.2810546875;
  1332. static const float blur31_std_dev = 1.28125;
  1333. static const float blur43_std_dev = 1.28125;
  1334. #else
  1335. // The defaults are the largest values that keep the largest unused
  1336. // blur term on each side <= 1.0/256.0. (We could get away with more
  1337. // or be more conservative, but this compromise is pretty reasonable.)
  1338. static const float blur3_std_dev = 0.62666015625;
  1339. static const float blur4_std_dev = 0.66171875;
  1340. static const float blur5_std_dev = 0.9845703125;
  1341. static const float blur6_std_dev = 1.02626953125;
  1342. static const float blur7_std_dev = 1.36103515625;
  1343. static const float blur8_std_dev = 1.4080078125;
  1344. static const float blur9_std_dev = 1.7533203125;
  1345. static const float blur10_std_dev = 1.80478515625;
  1346. static const float blur11_std_dev = 2.15986328125;
  1347. static const float blur12_std_dev = 2.215234375;
  1348. static const float blur17_std_dev = 3.45535583496;
  1349. static const float blur25_std_dev = 5.3409576416;
  1350. static const float blur31_std_dev = 6.86488037109;
  1351. static const float blur43_std_dev = 10.1852050781;
  1352. #endif // USE_BINOMIAL_BLUR_STD_DEVS
  1353. #endif // OVERRIDE_BLUR_STD_DEVS
  1354. #ifndef OVERRIDE_ERROR_BLURRING
  1355. // error_blurring should be in [0.0, 1.0]. Higher values reduce ringing
  1356. // in shared-sample blurs but increase blurring and feature shifting.
  1357. static const float error_blurring = 0.5;
  1358. #endif
  1359. ////////////////////////////////// INCLUDES //////////////////////////////////
  1360. // gamma-management.h relies on pass-specific settings to guide its behavior:
  1361. // FIRST_PASS, LAST_PASS, GAMMA_ENCODE_EVERY_FBO, etc. See it for details.
  1362. //#include "gamma-management.h"
  1363. //////////////////////////// BEGIN GAMMA-MANAGEMENT //////////////////////////
  1364. #ifndef GAMMA_MANAGEMENT_H
  1365. #define GAMMA_MANAGEMENT_H
  1366. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  1367. // Copyright (C) 2014 TroggleMonkey
  1368. //
  1369. // Permission is hereby granted, free of charge, to any person obtaining a copy
  1370. // of this software and associated documentation files (the "Software"), to
  1371. // deal in the Software without restriction, including without limitation the
  1372. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1373. // sell copies of the Software, and to permit persons to whom the Software is
  1374. // furnished to do so, subject to the following conditions:
  1375. //
  1376. // The above copyright notice and this permission notice shall be included in
  1377. // all copies or substantial portions of the Software.
  1378. //
  1379. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1380. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1381. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1382. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1383. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1384. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1385. // IN THE SOFTWARE.
  1386. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  1387. // This file provides gamma-aware tex*D*() and encode_output() functions.
  1388. // Requires: Before #include-ing this file, the including file must #define
  1389. // the following macros when applicable and follow their rules:
  1390. // 1.) #define FIRST_PASS if this is the first pass.
  1391. // 2.) #define LAST_PASS if this is the last pass.
  1392. // 3.) If sRGB is available, set srgb_framebufferN = "true" for
  1393. // every pass except the last in your .cgp preset.
  1394. // 4.) If sRGB isn't available but you want gamma-correctness with
  1395. // no banding, #define GAMMA_ENCODE_EVERY_FBO each pass.
  1396. // 5.) #define SIMULATE_CRT_ON_LCD if desired (precedence over 5-7)
  1397. // 6.) #define SIMULATE_GBA_ON_LCD if desired (precedence over 6-7)
  1398. // 7.) #define SIMULATE_LCD_ON_CRT if desired (precedence over 7)
  1399. // 8.) #define SIMULATE_GBA_ON_CRT if desired (precedence over -)
  1400. // If an option in [5, 8] is #defined in the first or last pass, it
  1401. // should be #defined for both. It shouldn't make a difference
  1402. // whether it's #defined for intermediate passes or not.
  1403. // Optional: The including file (or an earlier included file) may optionally
  1404. // #define a number of macros indicating it will override certain
  1405. // macros and associated constants are as follows:
  1406. // static constants with either static or uniform constants. The
  1407. // 1.) OVERRIDE_STANDARD_GAMMA: The user must first define:
  1408. // static const float ntsc_gamma
  1409. // static const float pal_gamma
  1410. // static const float crt_reference_gamma_high
  1411. // static const float crt_reference_gamma_low
  1412. // static const float lcd_reference_gamma
  1413. // static const float crt_office_gamma
  1414. // static const float lcd_office_gamma
  1415. // 2.) OVERRIDE_DEVICE_GAMMA: The user must first define:
  1416. // static const float crt_gamma
  1417. // static const float gba_gamma
  1418. // static const float lcd_gamma
  1419. // 3.) OVERRIDE_FINAL_GAMMA: The user must first define:
  1420. // static const float input_gamma
  1421. // static const float intermediate_gamma
  1422. // static const float output_gamma
  1423. // (intermediate_gamma is for GAMMA_ENCODE_EVERY_FBO.)
  1424. // 4.) OVERRIDE_ALPHA_ASSUMPTIONS: The user must first define:
  1425. // static const bool assume_opaque_alpha
  1426. // The gamma constant overrides must be used in every pass or none,
  1427. // and OVERRIDE_FINAL_GAMMA bypasses all of the SIMULATE* macros.
  1428. // OVERRIDE_ALPHA_ASSUMPTIONS may be set on a per-pass basis.
  1429. // Usage: After setting macros appropriately, ignore gamma correction and
  1430. // replace all tex*D*() calls with equivalent gamma-aware
  1431. // tex*D*_linearize calls, except:
  1432. // 1.) When you read an LUT, use regular tex*D or a gamma-specified
  1433. // function, depending on its gamma encoding:
  1434. // tex*D*_linearize_gamma (takes a runtime gamma parameter)
  1435. // 2.) If you must read pass0's original input in a later pass, use
  1436. // tex2D_linearize_ntsc_gamma. If you want to read pass0's
  1437. // input with gamma-corrected bilinear filtering, consider
  1438. // creating a first linearizing pass and reading from the input
  1439. // of pass1 later.
  1440. // Then, return encode_output(color) from every fragment shader.
  1441. // Finally, use the global gamma_aware_bilinear boolean if you want
  1442. // to statically branch based on whether bilinear filtering is
  1443. // gamma-correct or not (e.g. for placing Gaussian blur samples).
  1444. //
  1445. // Detailed Policy:
  1446. // tex*D*_linearize() functions enforce a consistent gamma-management policy
  1447. // based on the FIRST_PASS and GAMMA_ENCODE_EVERY_FBO settings. They assume
  1448. // their input texture has the same encoding characteristics as the input for
  1449. // the current pass (which doesn't apply to the exceptions listed above).
  1450. // Similarly, encode_output() enforces a policy based on the LAST_PASS and
  1451. // GAMMA_ENCODE_EVERY_FBO settings. Together, they result in one of the
  1452. // following two pipelines.
  1453. // Typical pipeline with intermediate sRGB framebuffers:
  1454. // linear_color = pow(pass0_encoded_color, input_gamma);
  1455. // intermediate_output = linear_color; // Automatic sRGB encoding
  1456. // linear_color = intermediate_output; // Automatic sRGB decoding
  1457. // final_output = pow(intermediate_output, 1.0/output_gamma);
  1458. // Typical pipeline without intermediate sRGB framebuffers:
  1459. // linear_color = pow(pass0_encoded_color, input_gamma);
  1460. // intermediate_output = pow(linear_color, 1.0/intermediate_gamma);
  1461. // linear_color = pow(intermediate_output, intermediate_gamma);
  1462. // final_output = pow(intermediate_output, 1.0/output_gamma);
  1463. // Using GAMMA_ENCODE_EVERY_FBO is much slower, but it's provided as a way to
  1464. // easily get gamma-correctness without banding on devices where sRGB isn't
  1465. // supported.
  1466. //
  1467. // Use This Header to Maximize Code Reuse:
  1468. // The purpose of this header is to provide a consistent interface for texture
  1469. // reads and output gamma-encoding that localizes and abstracts away all the
  1470. // annoying details. This greatly reduces the amount of code in each shader
  1471. // pass that depends on the pass number in the .cgp preset or whether sRGB
  1472. // FBO's are being used: You can trivially change the gamma behavior of your
  1473. // whole pass by commenting or uncommenting 1-3 #defines. To reuse the same
  1474. // code in your first, Nth, and last passes, you can even put it all in another
  1475. // header file and #include it from skeleton .cg files that #define the
  1476. // appropriate pass-specific settings.
  1477. //
  1478. // Rationale for Using Three Macros:
  1479. // This file uses GAMMA_ENCODE_EVERY_FBO instead of an opposite macro like
  1480. // SRGB_PIPELINE to ensure sRGB is assumed by default, which hopefully imposes
  1481. // a lower maintenance burden on each pass. At first glance it seems we could
  1482. // accomplish everything with two macros: GAMMA_CORRECT_IN / GAMMA_CORRECT_OUT.
  1483. // This works for simple use cases where input_gamma == output_gamma, but it
  1484. // breaks down for more complex scenarios like CRT simulation, where the pass
  1485. // number determines the gamma encoding of the input and output.
  1486. /////////////////////////////// BASE CONSTANTS ///////////////////////////////
  1487. // Set standard gamma constants, but allow users to override them:
  1488. #ifndef OVERRIDE_STANDARD_GAMMA
  1489. // Standard encoding gammas:
  1490. static const float ntsc_gamma = 2.2; // Best to use NTSC for PAL too?
  1491. static const float pal_gamma = 2.8; // Never actually 2.8 in practice
  1492. // Typical device decoding gammas (only use for emulating devices):
  1493. // CRT/LCD reference gammas are higher than NTSC and Rec.709 video standard
  1494. // gammas: The standards purposely undercorrected for an analog CRT's
  1495. // assumed 2.5 reference display gamma to maintain contrast in assumed
  1496. // [dark] viewing conditions: http://www.poynton.com/PDFs/GammaFAQ.pdf
  1497. // These unstated assumptions about display gamma and perceptual rendering
  1498. // intent caused a lot of confusion, and more modern CRT's seemed to target
  1499. // NTSC 2.2 gamma with circuitry. LCD displays seem to have followed suit
  1500. // (they struggle near black with 2.5 gamma anyway), especially PC/laptop
  1501. // displays designed to view sRGB in bright environments. (Standards are
  1502. // also in flux again with BT.1886, but it's underspecified for displays.)
  1503. static const float crt_reference_gamma_high = 2.5; // In (2.35, 2.55)
  1504. static const float crt_reference_gamma_low = 2.35; // In (2.35, 2.55)
  1505. static const float lcd_reference_gamma = 2.5; // To match CRT
  1506. static const float crt_office_gamma = 2.2; // Circuitry-adjusted for NTSC
  1507. static const float lcd_office_gamma = 2.2; // Approximates sRGB
  1508. #endif // OVERRIDE_STANDARD_GAMMA
  1509. // Assuming alpha == 1.0 might make it easier for users to avoid some bugs,
  1510. // but only if they're aware of it.
  1511. #ifndef OVERRIDE_ALPHA_ASSUMPTIONS
  1512. static const bool assume_opaque_alpha = false;
  1513. #endif
  1514. /////////////////////// DERIVED CONSTANTS AS FUNCTIONS ///////////////////////
  1515. // gamma-management.h should be compatible with overriding gamma values with
  1516. // runtime user parameters, but we can only define other global constants in
  1517. // terms of static constants, not uniform user parameters. To get around this
  1518. // limitation, we need to define derived constants using functions.
  1519. // Set device gamma constants, but allow users to override them:
  1520. #ifdef OVERRIDE_DEVICE_GAMMA
  1521. // The user promises to globally define the appropriate constants:
  1522. inline float get_crt_gamma() { return crt_gamma; }
  1523. inline float get_gba_gamma() { return gba_gamma; }
  1524. inline float get_lcd_gamma() { return lcd_gamma; }
  1525. #else
  1526. inline float get_crt_gamma() { return crt_reference_gamma_high; }
  1527. inline float get_gba_gamma() { return 3.5; } // Game Boy Advance; in (3.0, 4.0)
  1528. inline float get_lcd_gamma() { return lcd_office_gamma; }
  1529. #endif // OVERRIDE_DEVICE_GAMMA
  1530. // Set decoding/encoding gammas for the first/lass passes, but allow overrides:
  1531. #ifdef OVERRIDE_FINAL_GAMMA
  1532. // The user promises to globally define the appropriate constants:
  1533. inline float get_intermediate_gamma() { return intermediate_gamma; }
  1534. inline float get_input_gamma() { return input_gamma; }
  1535. inline float get_output_gamma() { return output_gamma; }
  1536. #else
  1537. // If we gamma-correct every pass, always use ntsc_gamma between passes to
  1538. // ensure middle passes don't need to care if anything is being simulated:
  1539. inline float get_intermediate_gamma() { return ntsc_gamma; }
  1540. #ifdef SIMULATE_CRT_ON_LCD
  1541. inline float get_input_gamma() { return get_crt_gamma(); }
  1542. inline float get_output_gamma() { return get_lcd_gamma(); }
  1543. #else
  1544. #ifdef SIMULATE_GBA_ON_LCD
  1545. inline float get_input_gamma() { return get_gba_gamma(); }
  1546. inline float get_output_gamma() { return get_lcd_gamma(); }
  1547. #else
  1548. #ifdef SIMULATE_LCD_ON_CRT
  1549. inline float get_input_gamma() { return get_lcd_gamma(); }
  1550. inline float get_output_gamma() { return get_crt_gamma(); }
  1551. #else
  1552. #ifdef SIMULATE_GBA_ON_CRT
  1553. inline float get_input_gamma() { return get_gba_gamma(); }
  1554. inline float get_output_gamma() { return get_crt_gamma(); }
  1555. #else // Don't simulate anything:
  1556. inline float get_input_gamma() { return ntsc_gamma; }
  1557. inline float get_output_gamma() { return ntsc_gamma; }
  1558. #endif // SIMULATE_GBA_ON_CRT
  1559. #endif // SIMULATE_LCD_ON_CRT
  1560. #endif // SIMULATE_GBA_ON_LCD
  1561. #endif // SIMULATE_CRT_ON_LCD
  1562. #endif // OVERRIDE_FINAL_GAMMA
  1563. // Set decoding/encoding gammas for the current pass. Use static constants for
  1564. // linearize_input and gamma_encode_output, because they aren't derived, and
  1565. // they let the compiler do dead-code elimination.
  1566. #ifndef GAMMA_ENCODE_EVERY_FBO
  1567. #ifdef FIRST_PASS
  1568. static const bool linearize_input = true;
  1569. inline float get_pass_input_gamma() { return get_input_gamma(); }
  1570. #else
  1571. static const bool linearize_input = false;
  1572. inline float get_pass_input_gamma() { return 1.0; }
  1573. #endif
  1574. #ifdef LAST_PASS
  1575. static const bool gamma_encode_output = true;
  1576. inline float get_pass_output_gamma() { return get_output_gamma(); }
  1577. #else
  1578. static const bool gamma_encode_output = false;
  1579. inline float get_pass_output_gamma() { return 1.0; }
  1580. #endif
  1581. #else
  1582. static const bool linearize_input = true;
  1583. static const bool gamma_encode_output = true;
  1584. #ifdef FIRST_PASS
  1585. inline float get_pass_input_gamma() { return get_input_gamma(); }
  1586. #else
  1587. inline float get_pass_input_gamma() { return get_intermediate_gamma(); }
  1588. #endif
  1589. #ifdef LAST_PASS
  1590. inline float get_pass_output_gamma() { return get_output_gamma(); }
  1591. #else
  1592. inline float get_pass_output_gamma() { return get_intermediate_gamma(); }
  1593. #endif
  1594. #endif
  1595. // Users might want to know if bilinear filtering will be gamma-correct:
  1596. static const bool gamma_aware_bilinear = !linearize_input;
  1597. ////////////////////// COLOR ENCODING/DECODING FUNCTIONS /////////////////////
  1598. inline float4 encode_output(const float4 color)
  1599. {
  1600. if(gamma_encode_output)
  1601. {
  1602. if(assume_opaque_alpha)
  1603. {
  1604. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), 1.0);
  1605. }
  1606. else
  1607. {
  1608. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), color.a);
  1609. }
  1610. }
  1611. else
  1612. {
  1613. return color;
  1614. }
  1615. }
  1616. inline float4 decode_input(const float4 color)
  1617. {
  1618. if(linearize_input)
  1619. {
  1620. if(assume_opaque_alpha)
  1621. {
  1622. return float4(pow(color.rgb, float3(get_pass_input_gamma())), 1.0);
  1623. }
  1624. else
  1625. {
  1626. return float4(pow(color.rgb, float3(get_pass_input_gamma())), color.a);
  1627. }
  1628. }
  1629. else
  1630. {
  1631. return color;
  1632. }
  1633. }
  1634. inline float4 decode_gamma_input(const float4 color, const float3 gamma)
  1635. {
  1636. if(assume_opaque_alpha)
  1637. {
  1638. return float4(pow(color.rgb, gamma), 1.0);
  1639. }
  1640. else
  1641. {
  1642. return float4(pow(color.rgb, gamma), color.a);
  1643. }
  1644. }
  1645. //TODO/FIXME: I have no idea why replacing the lookup wrappers with this macro fixes the blurs being offset ¯\_(ツ)_/¯
  1646. //#define tex2D_linearize(C, D) decode_input(vec4(COMPAT_TEXTURE(C, D)))
  1647. // EDIT: it's the 'const' in front of the coords that's doing it
  1648. /////////////////////////// TEXTURE LOOKUP WRAPPERS //////////////////////////
  1649. // "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  1650. // Provide a wide array of linearizing texture lookup wrapper functions. The
  1651. // Cg shader spec Retroarch uses only allows for 2D textures, but 1D and 3D
  1652. // lookups are provided for completeness in case that changes someday. Nobody
  1653. // is likely to use the *fetch and *proj functions, but they're included just
  1654. // in case. The only tex*D texture sampling functions omitted are:
  1655. // - tex*Dcmpbias
  1656. // - tex*Dcmplod
  1657. // - tex*DARRAY*
  1658. // - tex*DMS*
  1659. // - Variants returning integers
  1660. // Standard line length restrictions are ignored below for vertical brevity.
  1661. /*
  1662. // tex1D:
  1663. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords)
  1664. { return decode_input(tex1D(tex, tex_coords)); }
  1665. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords)
  1666. { return decode_input(tex1D(tex, tex_coords)); }
  1667. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const int texel_off)
  1668. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  1669. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  1670. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  1671. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy)
  1672. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  1673. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy)
  1674. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  1675. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy, const int texel_off)
  1676. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  1677. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy, const int texel_off)
  1678. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  1679. // tex1Dbias:
  1680. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords)
  1681. { return decode_input(tex1Dbias(tex, tex_coords)); }
  1682. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  1683. { return decode_input(tex1Dbias(tex, tex_coords, texel_off)); }
  1684. // tex1Dfetch:
  1685. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords)
  1686. { return decode_input(tex1Dfetch(tex, tex_coords)); }
  1687. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords, const int texel_off)
  1688. { return decode_input(tex1Dfetch(tex, tex_coords, texel_off)); }
  1689. // tex1Dlod:
  1690. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords)
  1691. { return decode_input(tex1Dlod(tex, tex_coords)); }
  1692. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  1693. { return decode_input(tex1Dlod(tex, tex_coords, texel_off)); }
  1694. // tex1Dproj:
  1695. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords)
  1696. { return decode_input(tex1Dproj(tex, tex_coords)); }
  1697. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords)
  1698. { return decode_input(tex1Dproj(tex, tex_coords)); }
  1699. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  1700. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  1701. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords, const int texel_off)
  1702. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  1703. */
  1704. // tex2D:
  1705. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords)
  1706. { return decode_input(COMPAT_TEXTURE(tex, tex_coords)); }
  1707. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords)
  1708. { return decode_input(COMPAT_TEXTURE(tex, tex_coords.xy)); }
  1709. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords, int texel_off)
  1710. { return decode_input(textureLod(tex, tex_coords, texel_off)); }
  1711. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords, int texel_off)
  1712. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  1713. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy)
  1714. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  1715. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy)
  1716. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  1717. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  1718. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  1719. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  1720. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  1721. // tex2Dbias:
  1722. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords)
  1723. //{ return decode_input(tex2Dbias(tex, tex_coords)); }
  1724. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  1725. //{ return decode_input(tex2Dbias(tex, tex_coords, texel_off)); }
  1726. // tex2Dfetch:
  1727. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords)
  1728. //{ return decode_input(tex2Dfetch(tex, tex_coords)); }
  1729. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords, const int texel_off)
  1730. //{ return decode_input(tex2Dfetch(tex, tex_coords, texel_off)); }
  1731. // tex2Dlod:
  1732. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords)
  1733. { return decode_input(textureLod(tex, tex_coords.xy, 0.0)); }
  1734. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords, int texel_off)
  1735. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  1736. /*
  1737. // tex2Dproj:
  1738. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords)
  1739. { return decode_input(tex2Dproj(tex, tex_coords)); }
  1740. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords)
  1741. { return decode_input(tex2Dproj(tex, tex_coords)); }
  1742. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords, const int texel_off)
  1743. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  1744. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  1745. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  1746. */
  1747. /*
  1748. // tex3D:
  1749. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords)
  1750. { return decode_input(tex3D(tex, tex_coords)); }
  1751. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const int texel_off)
  1752. { return decode_input(tex3D(tex, tex_coords, texel_off)); }
  1753. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy)
  1754. { return decode_input(tex3D(tex, tex_coords, dx, dy)); }
  1755. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy, const int texel_off)
  1756. { return decode_input(tex3D(tex, tex_coords, dx, dy, texel_off)); }
  1757. // tex3Dbias:
  1758. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords)
  1759. { return decode_input(tex3Dbias(tex, tex_coords)); }
  1760. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  1761. { return decode_input(tex3Dbias(tex, tex_coords, texel_off)); }
  1762. // tex3Dfetch:
  1763. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords)
  1764. { return decode_input(tex3Dfetch(tex, tex_coords)); }
  1765. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords, const int texel_off)
  1766. { return decode_input(tex3Dfetch(tex, tex_coords, texel_off)); }
  1767. // tex3Dlod:
  1768. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords)
  1769. { return decode_input(tex3Dlod(tex, tex_coords)); }
  1770. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  1771. { return decode_input(tex3Dlod(tex, tex_coords, texel_off)); }
  1772. // tex3Dproj:
  1773. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords)
  1774. { return decode_input(tex3Dproj(tex, tex_coords)); }
  1775. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  1776. { return decode_input(tex3Dproj(tex, tex_coords, texel_off)); }
  1777. /////////*
  1778. // NONSTANDARD "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  1779. // This narrow selection of nonstandard tex2D* functions can be useful:
  1780. // tex2Dlod0: Automatically fill in the tex2D LOD parameter for mip level 0.
  1781. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords)
  1782. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0))); }
  1783. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords, const int texel_off)
  1784. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0), texel_off)); }
  1785. // MANUALLY LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  1786. // Provide a narrower selection of tex2D* wrapper functions that decode an
  1787. // input sample with a specified gamma value. These are useful for reading
  1788. // LUT's and for reading the input of pass0 in a later pass.
  1789. // tex2D:
  1790. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float3 gamma)
  1791. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords), gamma); }
  1792. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float3 gamma)
  1793. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords.xy), gamma); }
  1794. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const int texel_off, const float3 gamma)
  1795. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  1796. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const int texel_off, const float3 gamma)
  1797. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  1798. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  1799. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  1800. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  1801. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  1802. //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)
  1803. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  1804. //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)
  1805. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  1806. /*
  1807. // tex2Dbias:
  1808. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const float3 gamma)
  1809. { return decode_gamma_input(tex2Dbias(tex, tex_coords), gamma); }
  1810. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const int texel_off, const float3 gamma)
  1811. { return decode_gamma_input(tex2Dbias(tex, tex_coords, texel_off), gamma); }
  1812. // tex2Dfetch:
  1813. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const float3 gamma)
  1814. { return decode_gamma_input(tex2Dfetch(tex, tex_coords), gamma); }
  1815. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const int texel_off, const float3 gamma)
  1816. { return decode_gamma_input(tex2Dfetch(tex, tex_coords, texel_off), gamma); }
  1817. */
  1818. // tex2Dlod:
  1819. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, float3 gamma)
  1820. { return decode_gamma_input(textureLod(tex, tex_coords.xy, 0.0), gamma); }
  1821. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, int texel_off, float3 gamma)
  1822. { return decode_gamma_input(textureLod(tex, tex_coords.xy, texel_off), gamma); }
  1823. #endif // GAMMA_MANAGEMENT_H
  1824. //////////////////////////// END GAMMA-MANAGEMENT //////////////////////////
  1825. //#include "quad-pixel-communication.h"
  1826. /////////////////////// BEGIN QUAD-PIXEL-COMMUNICATION //////////////////////
  1827. #ifndef QUAD_PIXEL_COMMUNICATION_H
  1828. #define QUAD_PIXEL_COMMUNICATION_H
  1829. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  1830. // Copyright (C) 2014 TroggleMonkey*
  1831. //
  1832. // Permission is hereby granted, free of charge, to any person obtaining a copy
  1833. // of this software and associated documentation files (the "Software"), to
  1834. // deal in the Software without restriction, including without limitation the
  1835. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  1836. // sell copies of the Software, and to permit persons to whom the Software is
  1837. // furnished to do so, subject to the following conditions:
  1838. //
  1839. // The above copyright notice and this permission notice shall be included in
  1840. // all copies or substantial portions of the Software.
  1841. //
  1842. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1843. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1844. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1845. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1846. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  1847. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  1848. // IN THE SOFTWARE.
  1849. ///////////////////////////////// DISCLAIMER /////////////////////////////////
  1850. // *This code was inspired by "Shader Amortization using Pixel Quad Message
  1851. // Passing" by Eric Penner, published in GPU Pro 2, Chapter VI.2. My intent
  1852. // is not to plagiarize his fundamentally similar code and assert my own
  1853. // copyright, but the algorithmic helper functions require so little code that
  1854. // implementations can't vary by much except bugfixes and conventions. I just
  1855. // wanted to license my own particular code here to avoid ambiguity and make it
  1856. // clear that as far as I'm concerned, people can do as they please with it.
  1857. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  1858. // Given screen pixel numbers, derive a "quad vector" describing a fragment's
  1859. // position in its 2x2 pixel quad. Given that vector, obtain the values of any
  1860. // variable at neighboring fragments.
  1861. // Requires: Using this file in general requires:
  1862. // 1.) ddx() and ddy() are present in the current Cg profile.
  1863. // 2.) The GPU driver is using fine/high-quality derivatives.
  1864. // Functions will give incorrect results if this is not true,
  1865. // so a test function is included.
  1866. ///////////////////// QUAD-PIXEL COMMUNICATION PRIMITIVES ////////////////////
  1867. float4 get_quad_vector_naive(float4 output_pixel_num_wrt_uvxy)
  1868. {
  1869. // Requires: Two measures of the current fragment's output pixel number
  1870. // in the range ([0, IN.output_size.x), [0, IN.output_size.y)):
  1871. // 1.) output_pixel_num_wrt_uvxy.xy increase with uv coords.
  1872. // 2.) output_pixel_num_wrt_uvxy.zw increase with screen xy.
  1873. // Returns: Two measures of the fragment's position in its 2x2 quad:
  1874. // 1.) The .xy components are its 2x2 placement with respect to
  1875. // uv direction (the origin (0, 0) is at the top-left):
  1876. // top-left = (-1.0, -1.0) top-right = ( 1.0, -1.0)
  1877. // bottom-left = (-1.0, 1.0) bottom-right = ( 1.0, 1.0)
  1878. // You need this to arrange/weight shared texture samples.
  1879. // 2.) The .zw components are its 2x2 placement with respect to
  1880. // screen xy direction (IN.position); the origin varies.
  1881. // quad_gather needs this measure to work correctly.
  1882. // Note: quad_vector.zw = quad_vector.xy * float2(
  1883. // ddx(output_pixel_num_wrt_uvxy.x),
  1884. // ddy(output_pixel_num_wrt_uvxy.y));
  1885. // Caveats: This function assumes the GPU driver always starts 2x2 pixel
  1886. // quads at even pixel numbers. This assumption can be wrong
  1887. // for odd output resolutions (nondeterministically so).
  1888. float4 pixel_odd = frac(output_pixel_num_wrt_uvxy * 0.5) * 2.0;
  1889. float4 quad_vector = pixel_odd * 2.0 - float4(1.0);
  1890. return quad_vector;
  1891. }
  1892. float4 get_quad_vector(float4 output_pixel_num_wrt_uvxy)
  1893. {
  1894. // Requires: Same as get_quad_vector_naive() (see that first).
  1895. // Returns: Same as get_quad_vector_naive() (see that first), but it's
  1896. // correct even if the 2x2 pixel quad starts at an odd pixel,
  1897. // which can occur at odd resolutions.
  1898. float4 quad_vector_guess =
  1899. get_quad_vector_naive(output_pixel_num_wrt_uvxy);
  1900. // If quad_vector_guess.zw doesn't increase with screen xy, we know
  1901. // the 2x2 pixel quad starts at an odd pixel:
  1902. float2 odd_start_mirror = 0.5 * float2(ddx(quad_vector_guess.z),
  1903. ddy(quad_vector_guess.w));
  1904. return quad_vector_guess * odd_start_mirror.xyxy;
  1905. }
  1906. float4 get_quad_vector(float2 output_pixel_num_wrt_uv)
  1907. {
  1908. // Requires: 1.) ddx() and ddy() are present in the current Cg profile.
  1909. // 2.) output_pixel_num_wrt_uv must increase with uv coords and
  1910. // measure the current fragment's output pixel number in:
  1911. // ([0, IN.output_size.x), [0, IN.output_size.y))
  1912. // Returns: Same as get_quad_vector_naive() (see that first), but it's
  1913. // correct even if the 2x2 pixel quad starts at an odd pixel,
  1914. // which can occur at odd resolutions.
  1915. // Caveats: This function requires less information than the version
  1916. // taking a float4, but it's potentially slower.
  1917. // Do screen coords increase with or against uv? Get the direction
  1918. // with respect to (uv.x, uv.y) for (screen.x, screen.y) in {-1, 1}.
  1919. float2 screen_uv_mirror = float2(ddx(output_pixel_num_wrt_uv.x),
  1920. ddy(output_pixel_num_wrt_uv.y));
  1921. float2 pixel_odd_wrt_uv = frac(output_pixel_num_wrt_uv * 0.5) * 2.0;
  1922. float2 quad_vector_uv_guess = (pixel_odd_wrt_uv - float2(0.5)) * 2.0;
  1923. float2 quad_vector_screen_guess = quad_vector_uv_guess * screen_uv_mirror;
  1924. // If quad_vector_screen_guess doesn't increase with screen xy, we know
  1925. // the 2x2 pixel quad starts at an odd pixel:
  1926. float2 odd_start_mirror = 0.5 * float2(ddx(quad_vector_screen_guess.x),
  1927. ddy(quad_vector_screen_guess.y));
  1928. float4 quad_vector_guess = float4(
  1929. quad_vector_uv_guess, quad_vector_screen_guess);
  1930. return quad_vector_guess * odd_start_mirror.xyxy;
  1931. }
  1932. void quad_gather(float4 quad_vector, float4 curr,
  1933. out float4 adjx, out float4 adjy, out float4 diag)
  1934. {
  1935. // Requires: 1.) ddx() and ddy() are present in the current Cg profile.
  1936. // 2.) The GPU driver is using fine/high-quality derivatives.
  1937. // 3.) quad_vector describes the current fragment's location in
  1938. // its 2x2 pixel quad using get_quad_vector()'s conventions.
  1939. // 4.) curr is any vector you wish to get neighboring values of.
  1940. // Returns: Values of an input vector (curr) at neighboring fragments
  1941. // adjacent x, adjacent y, and diagonal (via out parameters).
  1942. adjx = curr - ddx(curr) * quad_vector.z;
  1943. adjy = curr - ddy(curr) * quad_vector.w;
  1944. diag = adjx - ddy(adjx) * quad_vector.w;
  1945. }
  1946. void quad_gather(float4 quad_vector, float3 curr,
  1947. out float3 adjx, out float3 adjy, out float3 diag)
  1948. {
  1949. // Float3 version
  1950. adjx = curr - ddx(curr) * quad_vector.z;
  1951. adjy = curr - ddy(curr) * quad_vector.w;
  1952. diag = adjx - ddy(adjx) * quad_vector.w;
  1953. }
  1954. void quad_gather(float4 quad_vector, float2 curr,
  1955. out float2 adjx, out float2 adjy, out float2 diag)
  1956. {
  1957. // Float2 version
  1958. adjx = curr - ddx(curr) * quad_vector.z;
  1959. adjy = curr - ddy(curr) * quad_vector.w;
  1960. diag = adjx - ddy(adjx) * quad_vector.w;
  1961. }
  1962. float4 quad_gather(float4 quad_vector, float curr)
  1963. {
  1964. // Float version:
  1965. // Returns: return.x == current
  1966. // return.y == adjacent x
  1967. // return.z == adjacent y
  1968. // return.w == diagonal
  1969. float4 all = float4(curr);
  1970. all.y = all.x - ddx(all.x) * quad_vector.z;
  1971. all.zw = all.xy - ddy(all.xy) * quad_vector.w;
  1972. return all;
  1973. }
  1974. float4 quad_gather_sum(float4 quad_vector, float4 curr)
  1975. {
  1976. // Requires: Same as quad_gather()
  1977. // Returns: Sum of an input vector (curr) at all fragments in a quad.
  1978. float4 adjx, adjy, diag;
  1979. quad_gather(quad_vector, curr, adjx, adjy, diag);
  1980. return (curr + adjx + adjy + diag);
  1981. }
  1982. float3 quad_gather_sum(float4 quad_vector, float3 curr)
  1983. {
  1984. // Float3 version:
  1985. float3 adjx, adjy, diag;
  1986. quad_gather(quad_vector, curr, adjx, adjy, diag);
  1987. return (curr + adjx + adjy + diag);
  1988. }
  1989. float2 quad_gather_sum(float4 quad_vector, float2 curr)
  1990. {
  1991. // Float2 version:
  1992. float2 adjx, adjy, diag;
  1993. quad_gather(quad_vector, curr, adjx, adjy, diag);
  1994. return (curr + adjx + adjy + diag);
  1995. }
  1996. float quad_gather_sum(float4 quad_vector, float curr)
  1997. {
  1998. // Float version:
  1999. float4 all_values = quad_gather(quad_vector, curr);
  2000. return (all_values.x + all_values.y + all_values.z + all_values.w);
  2001. }
  2002. bool fine_derivatives_working(float4 quad_vector, float4 curr)
  2003. {
  2004. // Requires: 1.) ddx() and ddy() are present in the current Cg profile.
  2005. // 2.) quad_vector describes the current fragment's location in
  2006. // its 2x2 pixel quad using get_quad_vector()'s conventions.
  2007. // 3.) curr must be a test vector with non-constant derivatives
  2008. // (its value should change nonlinearly across fragments).
  2009. // Returns: true if fine/hybrid/high-quality derivatives are used, or
  2010. // false if coarse derivatives are used or inconclusive
  2011. // Usage: Test whether quad-pixel communication is working!
  2012. // Method: We can confirm fine derivatives are used if the following
  2013. // holds (ever, for any value at any fragment):
  2014. // (ddy(curr) != ddy(adjx)) or (ddx(curr) != ddx(adjy))
  2015. // The more values we test (e.g. test a float4 two ways), the
  2016. // easier it is to demonstrate fine derivatives are working.
  2017. // TODO: Check for floating point exact comparison issues!
  2018. float4 ddx_curr = ddx(curr);
  2019. float4 ddy_curr = ddy(curr);
  2020. float4 adjx = curr - ddx_curr * quad_vector.z;
  2021. float4 adjy = curr - ddy_curr * quad_vector.w;
  2022. bool ddy_different = any(bool4(ddy_curr.x != ddy(adjx).x, ddy_curr.y != ddy(adjx).y, ddy_curr.z != ddy(adjx).z, ddy_curr.w != ddy(adjx).w));
  2023. bool ddx_different = any(bool4(ddx_curr.x != ddx(adjy).x, ddx_curr.y != ddx(adjy).y, ddx_curr.z != ddx(adjy).z, ddx_curr.w != ddx(adjy).w));
  2024. return any(bool2(ddy_different, ddx_different));
  2025. }
  2026. bool fine_derivatives_working_fast(float4 quad_vector, float curr)
  2027. {
  2028. // Requires: Same as fine_derivatives_working()
  2029. // Returns: Same as fine_derivatives_working()
  2030. // Usage: This is faster than fine_derivatives_working() but more
  2031. // likely to return false negatives, so it's less useful for
  2032. // offline testing/debugging. It's also useless as the basis
  2033. // for dynamic runtime branching as of May 2014: Derivatives
  2034. // (and quad-pixel communication) are currently disallowed in
  2035. // branches. However, future GPU's may allow you to use them
  2036. // in dynamic branches if you promise the branch condition
  2037. // evaluates the same for every fragment in the quad (and/or if
  2038. // the driver enforces that promise by making a single fragment
  2039. // control branch decisions). If that ever happens, this
  2040. // version may become a more economical choice.
  2041. float ddx_curr = ddx(curr);
  2042. float ddy_curr = ddy(curr);
  2043. float adjx = curr - ddx_curr * quad_vector.z;
  2044. return (ddy_curr != ddy(adjx));
  2045. }
  2046. #endif // QUAD_PIXEL_COMMUNICATION_H
  2047. //////////////////////// END QUAD-PIXEL-COMMUNICATION ///////////////////////
  2048. //#include "special-functions.h"
  2049. /////////////////////////// BEGIN SPECIAL-FUNCTIONS //////////////////////////
  2050. #ifndef SPECIAL_FUNCTIONS_H
  2051. #define SPECIAL_FUNCTIONS_H
  2052. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  2053. // Copyright (C) 2014 TroggleMonkey
  2054. //
  2055. // Permission is hereby granted, free of charge, to any person obtaining a copy
  2056. // of this software and associated documentation files (the "Software"), to
  2057. // deal in the Software without restriction, including without limitation the
  2058. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2059. // sell copies of the Software, and to permit persons to whom the Software is
  2060. // furnished to do so, subject to the following conditions:
  2061. //
  2062. // The above copyright notice and this permission notice shall be included in
  2063. // all copies or substantial portions of the Software.
  2064. //
  2065. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2066. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2067. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2068. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2069. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2070. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2071. // IN THE SOFTWARE.
  2072. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  2073. // This file implements the following mathematical special functions:
  2074. // 1.) erf() = 2/sqrt(pi) * indefinite_integral(e**(-x**2))
  2075. // 2.) gamma(s), a real-numbered extension of the integer factorial function
  2076. // It also implements normalized_ligamma(s, z), a normalized lower incomplete
  2077. // gamma function for s < 0.5 only. Both gamma() and normalized_ligamma() can
  2078. // be called with an _impl suffix to use an implementation version with a few
  2079. // extra precomputed parameters (which may be useful for the caller to reuse).
  2080. // See below for details.
  2081. //
  2082. // Design Rationale:
  2083. // Pretty much every line of code in this file is duplicated four times for
  2084. // different input types (float4/float3/float2/float). This is unfortunate,
  2085. // but Cg doesn't allow function templates. Macros would be far less verbose,
  2086. // but they would make the code harder to document and read. I don't expect
  2087. // these functions will require a whole lot of maintenance changes unless
  2088. // someone ever has need for more robust incomplete gamma functions, so code
  2089. // duplication seems to be the lesser evil in this case.
  2090. /////////////////////////// GAUSSIAN ERROR FUNCTION //////////////////////////
  2091. float4 erf6(float4 x)
  2092. {
  2093. // Requires: x is the standard parameter to erf().
  2094. // Returns: Return an Abramowitz/Stegun approximation of erf(), where:
  2095. // erf(x) = 2/sqrt(pi) * integral(e**(-x**2))
  2096. // This approximation has a max absolute error of 2.5*10**-5
  2097. // with solid numerical robustness and efficiency. See:
  2098. // https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions
  2099. static const float4 one = float4(1.0);
  2100. const float4 sign_x = sign(x);
  2101. const float4 t = one/(one + 0.47047*abs(x));
  2102. const float4 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  2103. exp(-(x*x));
  2104. return result * sign_x;
  2105. }
  2106. float3 erf6(const float3 x)
  2107. {
  2108. // Float3 version:
  2109. static const float3 one = float3(1.0);
  2110. const float3 sign_x = sign(x);
  2111. const float3 t = one/(one + 0.47047*abs(x));
  2112. const float3 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  2113. exp(-(x*x));
  2114. return result * sign_x;
  2115. }
  2116. float2 erf6(const float2 x)
  2117. {
  2118. // Float2 version:
  2119. static const float2 one = float2(1.0);
  2120. const float2 sign_x = sign(x);
  2121. const float2 t = one/(one + 0.47047*abs(x));
  2122. const float2 result = one - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  2123. exp(-(x*x));
  2124. return result * sign_x;
  2125. }
  2126. float erf6(const float x)
  2127. {
  2128. // Float version:
  2129. const float sign_x = sign(x);
  2130. const float t = 1.0/(1.0 + 0.47047*abs(x));
  2131. const float result = 1.0 - t*(0.3480242 + t*(-0.0958798 + t*0.7478556))*
  2132. exp(-(x*x));
  2133. return result * sign_x;
  2134. }
  2135. float4 erft(const float4 x)
  2136. {
  2137. // Requires: x is the standard parameter to erf().
  2138. // Returns: Approximate erf() with the hyperbolic tangent. The error is
  2139. // visually noticeable, but it's blazing fast and perceptually
  2140. // close...at least on ATI hardware. See:
  2141. // http://www.maplesoft.com/applications/view.aspx?SID=5525&view=html
  2142. // Warning: Only use this if your hardware drivers correctly implement
  2143. // tanh(): My nVidia 8800GTS returns garbage output.
  2144. return tanh(1.202760580 * x);
  2145. }
  2146. float3 erft(const float3 x)
  2147. {
  2148. // Float3 version:
  2149. return tanh(1.202760580 * x);
  2150. }
  2151. float2 erft(const float2 x)
  2152. {
  2153. // Float2 version:
  2154. return tanh(1.202760580 * x);
  2155. }
  2156. float erft(const float x)
  2157. {
  2158. // Float version:
  2159. return tanh(1.202760580 * x);
  2160. }
  2161. inline float4 erf(const float4 x)
  2162. {
  2163. // Requires: x is the standard parameter to erf().
  2164. // Returns: Some approximation of erf(x), depending on user settings.
  2165. #ifdef ERF_FAST_APPROXIMATION
  2166. return erft(x);
  2167. #else
  2168. return erf6(x);
  2169. #endif
  2170. }
  2171. inline float3 erf(const float3 x)
  2172. {
  2173. // Float3 version:
  2174. #ifdef ERF_FAST_APPROXIMATION
  2175. return erft(x);
  2176. #else
  2177. return erf6(x);
  2178. #endif
  2179. }
  2180. inline float2 erf(const float2 x)
  2181. {
  2182. // Float2 version:
  2183. #ifdef ERF_FAST_APPROXIMATION
  2184. return erft(x);
  2185. #else
  2186. return erf6(x);
  2187. #endif
  2188. }
  2189. inline float erf(const float x)
  2190. {
  2191. // Float version:
  2192. #ifdef ERF_FAST_APPROXIMATION
  2193. return erft(x);
  2194. #else
  2195. return erf6(x);
  2196. #endif
  2197. }
  2198. /////////////////////////// COMPLETE GAMMA FUNCTION //////////////////////////
  2199. float4 gamma_impl(const float4 s, const float4 s_inv)
  2200. {
  2201. // Requires: 1.) s is the standard parameter to the gamma function, and
  2202. // it should lie in the [0, 36] range.
  2203. // 2.) s_inv = 1.0/s. This implementation function requires
  2204. // the caller to precompute this value, giving users the
  2205. // opportunity to reuse it.
  2206. // Returns: Return approximate gamma function (real-numbered factorial)
  2207. // output using the Lanczos approximation with two coefficients
  2208. // calculated using Paul Godfrey's method here:
  2209. // http://my.fit.edu/~gabdo/gamma.txt
  2210. // An optimal g value for s in [0, 36] is ~1.12906830989, with
  2211. // a maximum relative error of 0.000463 for 2**16 equally
  2212. // evals. We could use three coeffs (0.0000346 error) without
  2213. // hurting latency, but this allows more parallelism with
  2214. // outside instructions.
  2215. static const float4 g = float4(1.12906830989);
  2216. static const float4 c0 = float4(0.8109119309638332633713423362694399653724431);
  2217. static const float4 c1 = float4(0.4808354605142681877121661197951496120000040);
  2218. static const float4 e = float4(2.71828182845904523536028747135266249775724709);
  2219. const float4 sph = s + float4(0.5);
  2220. const float4 lanczos_sum = c0 + c1/(s + float4(1.0));
  2221. const float4 base = (sph + g)/e; // or (s + g + float4(0.5))/e
  2222. // gamma(s + 1) = base**sph * lanczos_sum; divide by s for gamma(s).
  2223. // This has less error for small s's than (s -= 1.0) at the beginning.
  2224. return (pow(base, sph) * lanczos_sum) * s_inv;
  2225. }
  2226. float3 gamma_impl(const float3 s, const float3 s_inv)
  2227. {
  2228. // Float3 version:
  2229. static const float3 g = float3(1.12906830989);
  2230. static const float3 c0 = float3(0.8109119309638332633713423362694399653724431);
  2231. static const float3 c1 = float3(0.4808354605142681877121661197951496120000040);
  2232. static const float3 e = float3(2.71828182845904523536028747135266249775724709);
  2233. const float3 sph = s + float3(0.5);
  2234. const float3 lanczos_sum = c0 + c1/(s + float3(1.0));
  2235. const float3 base = (sph + g)/e;
  2236. return (pow(base, sph) * lanczos_sum) * s_inv;
  2237. }
  2238. float2 gamma_impl(const float2 s, const float2 s_inv)
  2239. {
  2240. // Float2 version:
  2241. static const float2 g = float2(1.12906830989);
  2242. static const float2 c0 = float2(0.8109119309638332633713423362694399653724431);
  2243. static const float2 c1 = float2(0.4808354605142681877121661197951496120000040);
  2244. static const float2 e = float2(2.71828182845904523536028747135266249775724709);
  2245. const float2 sph = s + float2(0.5);
  2246. const float2 lanczos_sum = c0 + c1/(s + float2(1.0));
  2247. const float2 base = (sph + g)/e;
  2248. return (pow(base, sph) * lanczos_sum) * s_inv;
  2249. }
  2250. float gamma_impl(const float s, const float s_inv)
  2251. {
  2252. // Float version:
  2253. static const float g = 1.12906830989;
  2254. static const float c0 = 0.8109119309638332633713423362694399653724431;
  2255. static const float c1 = 0.4808354605142681877121661197951496120000040;
  2256. static const float e = 2.71828182845904523536028747135266249775724709;
  2257. const float sph = s + 0.5;
  2258. const float lanczos_sum = c0 + c1/(s + 1.0);
  2259. const float base = (sph + g)/e;
  2260. return (pow(base, sph) * lanczos_sum) * s_inv;
  2261. }
  2262. float4 gamma(const float4 s)
  2263. {
  2264. // Requires: s is the standard parameter to the gamma function, and it
  2265. // should lie in the [0, 36] range.
  2266. // Returns: Return approximate gamma function output with a maximum
  2267. // relative error of 0.000463. See gamma_impl for details.
  2268. return gamma_impl(s, float4(1.0)/s);
  2269. }
  2270. float3 gamma(const float3 s)
  2271. {
  2272. // Float3 version:
  2273. return gamma_impl(s, float3(1.0)/s);
  2274. }
  2275. float2 gamma(const float2 s)
  2276. {
  2277. // Float2 version:
  2278. return gamma_impl(s, float2(1.0)/s);
  2279. }
  2280. float gamma(const float s)
  2281. {
  2282. // Float version:
  2283. return gamma_impl(s, 1.0/s);
  2284. }
  2285. //////////////// INCOMPLETE GAMMA FUNCTIONS (RESTRICTED INPUT) ///////////////
  2286. // Lower incomplete gamma function for small s and z (implementation):
  2287. float4 ligamma_small_z_impl(const float4 s, const float4 z, const float4 s_inv)
  2288. {
  2289. // Requires: 1.) s < ~0.5
  2290. // 2.) z <= ~0.775075
  2291. // 3.) s_inv = 1.0/s (precomputed for outside reuse)
  2292. // Returns: A series representation for the lower incomplete gamma
  2293. // function for small s and small z (4 terms).
  2294. // The actual "rolled up" summation looks like:
  2295. // last_sign = 1.0; last_pow = 1.0; last_factorial = 1.0;
  2296. // sum = last_sign * last_pow / ((s + k) * last_factorial)
  2297. // for(int i = 0; i < 4; ++i)
  2298. // {
  2299. // last_sign *= -1.0; last_pow *= z; last_factorial *= i;
  2300. // sum += last_sign * last_pow / ((s + k) * last_factorial);
  2301. // }
  2302. // Unrolled, constant-unfolded and arranged for madds and parallelism:
  2303. const float4 scale = pow(z, s);
  2304. float4 sum = s_inv; // Summation iteration 0 result
  2305. // Summation iterations 1, 2, and 3:
  2306. const float4 z_sq = z*z;
  2307. const float4 denom1 = s + float4(1.0);
  2308. const float4 denom2 = 2.0*s + float4(4.0);
  2309. const float4 denom3 = 6.0*s + float4(18.0);
  2310. //float4 denom4 = 24.0*s + float4(96.0);
  2311. sum -= z/denom1;
  2312. sum += z_sq/denom2;
  2313. sum -= z * z_sq/denom3;
  2314. //sum += z_sq * z_sq / denom4;
  2315. // Scale and return:
  2316. return scale * sum;
  2317. }
  2318. float3 ligamma_small_z_impl(const float3 s, const float3 z, const float3 s_inv)
  2319. {
  2320. // Float3 version:
  2321. const float3 scale = pow(z, s);
  2322. float3 sum = s_inv;
  2323. const float3 z_sq = z*z;
  2324. const float3 denom1 = s + float3(1.0);
  2325. const float3 denom2 = 2.0*s + float3(4.0);
  2326. const float3 denom3 = 6.0*s + float3(18.0);
  2327. sum -= z/denom1;
  2328. sum += z_sq/denom2;
  2329. sum -= z * z_sq/denom3;
  2330. return scale * sum;
  2331. }
  2332. float2 ligamma_small_z_impl(const float2 s, const float2 z, const float2 s_inv)
  2333. {
  2334. // Float2 version:
  2335. const float2 scale = pow(z, s);
  2336. float2 sum = s_inv;
  2337. const float2 z_sq = z*z;
  2338. const float2 denom1 = s + float2(1.0);
  2339. const float2 denom2 = 2.0*s + float2(4.0);
  2340. const float2 denom3 = 6.0*s + float2(18.0);
  2341. sum -= z/denom1;
  2342. sum += z_sq/denom2;
  2343. sum -= z * z_sq/denom3;
  2344. return scale * sum;
  2345. }
  2346. float ligamma_small_z_impl(const float s, const float z, const float s_inv)
  2347. {
  2348. // Float version:
  2349. const float scale = pow(z, s);
  2350. float sum = s_inv;
  2351. const float z_sq = z*z;
  2352. const float denom1 = s + 1.0;
  2353. const float denom2 = 2.0*s + 4.0;
  2354. const float denom3 = 6.0*s + 18.0;
  2355. sum -= z/denom1;
  2356. sum += z_sq/denom2;
  2357. sum -= z * z_sq/denom3;
  2358. return scale * sum;
  2359. }
  2360. // Upper incomplete gamma function for small s and large z (implementation):
  2361. float4 uigamma_large_z_impl(const float4 s, const float4 z)
  2362. {
  2363. // Requires: 1.) s < ~0.5
  2364. // 2.) z > ~0.775075
  2365. // Returns: Gauss's continued fraction representation for the upper
  2366. // incomplete gamma function (4 terms).
  2367. // The "rolled up" continued fraction looks like this. The denominator
  2368. // is truncated, and it's calculated "from the bottom up:"
  2369. // denom = float4('inf');
  2370. // float4 one = float4(1.0);
  2371. // for(int i = 4; i > 0; --i)
  2372. // {
  2373. // denom = ((i * 2.0) - one) + z - s + (i * (s - i))/denom;
  2374. // }
  2375. // Unrolled and constant-unfolded for madds and parallelism:
  2376. const float4 numerator = pow(z, s) * exp(-z);
  2377. float4 denom = float4(7.0) + z - s;
  2378. denom = float4(5.0) + z - s + (3.0*s - float4(9.0))/denom;
  2379. denom = float4(3.0) + z - s + (2.0*s - float4(4.0))/denom;
  2380. denom = float4(1.0) + z - s + (s - float4(1.0))/denom;
  2381. return numerator / denom;
  2382. }
  2383. float3 uigamma_large_z_impl(const float3 s, const float3 z)
  2384. {
  2385. // Float3 version:
  2386. const float3 numerator = pow(z, s) * exp(-z);
  2387. float3 denom = float3(7.0) + z - s;
  2388. denom = float3(5.0) + z - s + (3.0*s - float3(9.0))/denom;
  2389. denom = float3(3.0) + z - s + (2.0*s - float3(4.0))/denom;
  2390. denom = float3(1.0) + z - s + (s - float3(1.0))/denom;
  2391. return numerator / denom;
  2392. }
  2393. float2 uigamma_large_z_impl(const float2 s, const float2 z)
  2394. {
  2395. // Float2 version:
  2396. const float2 numerator = pow(z, s) * exp(-z);
  2397. float2 denom = float2(7.0) + z - s;
  2398. denom = float2(5.0) + z - s + (3.0*s - float2(9.0))/denom;
  2399. denom = float2(3.0) + z - s + (2.0*s - float2(4.0))/denom;
  2400. denom = float2(1.0) + z - s + (s - float2(1.0))/denom;
  2401. return numerator / denom;
  2402. }
  2403. float uigamma_large_z_impl(const float s, const float z)
  2404. {
  2405. // Float version:
  2406. const float numerator = pow(z, s) * exp(-z);
  2407. float denom = 7.0 + z - s;
  2408. denom = 5.0 + z - s + (3.0*s - 9.0)/denom;
  2409. denom = 3.0 + z - s + (2.0*s - 4.0)/denom;
  2410. denom = 1.0 + z - s + (s - 1.0)/denom;
  2411. return numerator / denom;
  2412. }
  2413. // Normalized lower incomplete gamma function for small s (implementation):
  2414. float4 normalized_ligamma_impl(const float4 s, const float4 z,
  2415. const float4 s_inv, const float4 gamma_s_inv)
  2416. {
  2417. // Requires: 1.) s < ~0.5
  2418. // 2.) s_inv = 1/s (precomputed for outside reuse)
  2419. // 3.) gamma_s_inv = 1/gamma(s) (precomputed for outside reuse)
  2420. // Returns: Approximate the normalized lower incomplete gamma function
  2421. // for s < 0.5. Since we only care about s < 0.5, we only need
  2422. // to evaluate two branches (not four) based on z. Each branch
  2423. // uses four terms, with a max relative error of ~0.00182. The
  2424. // branch threshold and specifics were adapted for fewer terms
  2425. // from Gil/Segura/Temme's paper here:
  2426. // http://oai.cwi.nl/oai/asset/20433/20433B.pdf
  2427. // Evaluate both branches: Real branches test slower even when available.
  2428. static const float4 thresh = float4(0.775075);
  2429. bool4 z_is_large;
  2430. z_is_large.x = z.x > thresh.x;
  2431. z_is_large.y = z.y > thresh.y;
  2432. z_is_large.z = z.z > thresh.z;
  2433. z_is_large.w = z.w > thresh.w;
  2434. const float4 large_z = float4(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  2435. const float4 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  2436. // Combine the results from both branches:
  2437. bool4 inverse_z_is_large = not(z_is_large);
  2438. return large_z * float4(z_is_large) + small_z * float4(inverse_z_is_large);
  2439. }
  2440. float3 normalized_ligamma_impl(const float3 s, const float3 z,
  2441. const float3 s_inv, const float3 gamma_s_inv)
  2442. {
  2443. // Float3 version:
  2444. static const float3 thresh = float3(0.775075);
  2445. bool3 z_is_large;
  2446. z_is_large.x = z.x > thresh.x;
  2447. z_is_large.y = z.y > thresh.y;
  2448. z_is_large.z = z.z > thresh.z;
  2449. const float3 large_z = float3(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  2450. const float3 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  2451. bool3 inverse_z_is_large = not(z_is_large);
  2452. return large_z * float3(z_is_large) + small_z * float3(inverse_z_is_large);
  2453. }
  2454. float2 normalized_ligamma_impl(const float2 s, const float2 z,
  2455. const float2 s_inv, const float2 gamma_s_inv)
  2456. {
  2457. // Float2 version:
  2458. static const float2 thresh = float2(0.775075);
  2459. bool2 z_is_large;
  2460. z_is_large.x = z.x > thresh.x;
  2461. z_is_large.y = z.y > thresh.y;
  2462. const float2 large_z = float2(1.0) - uigamma_large_z_impl(s, z) * gamma_s_inv;
  2463. const float2 small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  2464. bool2 inverse_z_is_large = not(z_is_large);
  2465. return large_z * float2(z_is_large) + small_z * float2(inverse_z_is_large);
  2466. }
  2467. float normalized_ligamma_impl(const float s, const float z,
  2468. const float s_inv, const float gamma_s_inv)
  2469. {
  2470. // Float version:
  2471. static const float thresh = 0.775075;
  2472. const bool z_is_large = z > thresh;
  2473. const float large_z = 1.0 - uigamma_large_z_impl(s, z) * gamma_s_inv;
  2474. const float small_z = ligamma_small_z_impl(s, z, s_inv) * gamma_s_inv;
  2475. return large_z * float(z_is_large) + small_z * float(!z_is_large);
  2476. }
  2477. // Normalized lower incomplete gamma function for small s:
  2478. float4 normalized_ligamma(const float4 s, const float4 z)
  2479. {
  2480. // Requires: s < ~0.5
  2481. // Returns: Approximate the normalized lower incomplete gamma function
  2482. // for s < 0.5. See normalized_ligamma_impl() for details.
  2483. const float4 s_inv = float4(1.0)/s;
  2484. const float4 gamma_s_inv = float4(1.0)/gamma_impl(s, s_inv);
  2485. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  2486. }
  2487. float3 normalized_ligamma(const float3 s, const float3 z)
  2488. {
  2489. // Float3 version:
  2490. const float3 s_inv = float3(1.0)/s;
  2491. const float3 gamma_s_inv = float3(1.0)/gamma_impl(s, s_inv);
  2492. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  2493. }
  2494. float2 normalized_ligamma(const float2 s, const float2 z)
  2495. {
  2496. // Float2 version:
  2497. const float2 s_inv = float2(1.0)/s;
  2498. const float2 gamma_s_inv = float2(1.0)/gamma_impl(s, s_inv);
  2499. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  2500. }
  2501. float normalized_ligamma(const float s, const float z)
  2502. {
  2503. // Float version:
  2504. const float s_inv = 1.0/s;
  2505. const float gamma_s_inv = 1.0/gamma_impl(s, s_inv);
  2506. return normalized_ligamma_impl(s, z, s_inv, gamma_s_inv);
  2507. }
  2508. #endif // SPECIAL_FUNCTIONS_H
  2509. //////////////////////////// END SPECIAL-FUNCTIONS ///////////////////////////
  2510. //////////////////////////////// END INCLUDES ////////////////////////////////
  2511. /////////////////////////////////// HELPERS //////////////////////////////////
  2512. inline float4 uv2_to_uv4(float2 tex_uv)
  2513. {
  2514. // Make a float2 uv offset safe for adding to float4 tex2Dlod coords:
  2515. return float4(tex_uv, 0.0, 0.0);
  2516. }
  2517. // Make a length squared helper macro (for usage with static constants):
  2518. #define LENGTH_SQ(vec) (dot(vec, vec))
  2519. inline float get_fast_gaussian_weight_sum_inv(const float sigma)
  2520. {
  2521. // We can use the Gaussian integral to calculate the asymptotic weight for
  2522. // the center pixel. Since the unnormalized center pixel weight is 1.0,
  2523. // the normalized weight is the same as the weight sum inverse. Given a
  2524. // large enough blur (9+), the asymptotic weight sum is close and faster:
  2525. // center_weight = 0.5 *
  2526. // (erf(0.5/(sigma*sqrt(2.0))) - erf(-0.5/(sigma*sqrt(2.0))))
  2527. // erf(-x) == -erf(x), so we get 0.5 * (2.0 * erf(blah blah)):
  2528. // However, we can get even faster results with curve-fitting. These are
  2529. // also closer than the asymptotic results, because they were constructed
  2530. // from 64 blurs sizes from [3, 131) and 255 equally-spaced sigmas from
  2531. // (0, blurN_std_dev), so the results for smaller sigmas are biased toward
  2532. // smaller blurs. The max error is 0.0031793913.
  2533. // Relative FPS: 134.3 with erf, 135.8 with curve-fitting.
  2534. //static const float temp = 0.5/sqrt(2.0);
  2535. //return erf(temp/sigma);
  2536. return min(exp(exp(0.348348412457428/
  2537. (sigma - 0.0860587260734721))), 0.399334576340352/sigma);
  2538. }
  2539. //////////////////// ARBITRARILY RESIZABLE SEPARABLE BLURS ///////////////////
  2540. float3 tex2Dblur11resize(const sampler2D tex, const float2 tex_uv,
  2541. const float2 dxdy, const float sigma)
  2542. {
  2543. // Requires: Global requirements must be met (see file description).
  2544. // Returns: A 1D 11x Gaussian blurred texture lookup using a 11-tap blur.
  2545. // It may be mipmapped depending on settings and dxdy.
  2546. // Calculate Gaussian blur kernel weights and a normalization factor for
  2547. // distances of 0-4, ignoring constant factors (since we're normalizing).
  2548. const float denom_inv = 0.5/(sigma*sigma);
  2549. const float w0 = 1.0;
  2550. const float w1 = exp(-1.0 * denom_inv);
  2551. const float w2 = exp(-4.0 * denom_inv);
  2552. const float w3 = exp(-9.0 * denom_inv);
  2553. const float w4 = exp(-16.0 * denom_inv);
  2554. const float w5 = exp(-25.0 * denom_inv);
  2555. const float weight_sum_inv = 1.0 /
  2556. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5));
  2557. // Statically normalize weights, sum weighted samples, and return. Blurs are
  2558. // currently optimized for dynamic weights.
  2559. float3 sum = float3(0.0,0.0,0.0);
  2560. sum += w5 * tex2D_linearize(tex, tex_uv - 5.0 * dxdy).rgb;
  2561. sum += w4 * tex2D_linearize(tex, tex_uv - 4.0 * dxdy).rgb;
  2562. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  2563. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  2564. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  2565. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2566. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  2567. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  2568. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  2569. sum += w4 * tex2D_linearize(tex, tex_uv + 4.0 * dxdy).rgb;
  2570. sum += w5 * tex2D_linearize(tex, tex_uv + 5.0 * dxdy).rgb;
  2571. return sum * weight_sum_inv;
  2572. }
  2573. float3 tex2Dblur9resize(const sampler2D tex, const float2 tex_uv,
  2574. const float2 dxdy, const float sigma)
  2575. {
  2576. // Requires: Global requirements must be met (see file description).
  2577. // Returns: A 1D 9x Gaussian blurred texture lookup using a 9-tap blur.
  2578. // It may be mipmapped depending on settings and dxdy.
  2579. // First get the texel weights and normalization factor as above.
  2580. const float denom_inv = 0.5/(sigma*sigma);
  2581. const float w0 = 1.0;
  2582. const float w1 = exp(-1.0 * denom_inv);
  2583. const float w2 = exp(-4.0 * denom_inv);
  2584. const float w3 = exp(-9.0 * denom_inv);
  2585. const float w4 = exp(-16.0 * denom_inv);
  2586. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  2587. // Statically normalize weights, sum weighted samples, and return:
  2588. float3 sum = float3(0.0,0.0,0.0);
  2589. sum += w4 * tex2D_linearize(tex, tex_uv - 4.0 * dxdy).rgb;
  2590. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  2591. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  2592. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  2593. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2594. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  2595. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  2596. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  2597. sum += w4 * tex2D_linearize(tex, tex_uv + 4.0 * dxdy).rgb;
  2598. return sum * weight_sum_inv;
  2599. }
  2600. float3 tex2Dblur7resize(const sampler2D tex, const float2 tex_uv,
  2601. const float2 dxdy, const float sigma)
  2602. {
  2603. // Requires: Global requirements must be met (see file description).
  2604. // Returns: A 1D 7x Gaussian blurred texture lookup using a 7-tap blur.
  2605. // It may be mipmapped depending on settings and dxdy.
  2606. // First get the texel weights and normalization factor as above.
  2607. const float denom_inv = 0.5/(sigma*sigma);
  2608. const float w0 = 1.0;
  2609. const float w1 = exp(-1.0 * denom_inv);
  2610. const float w2 = exp(-4.0 * denom_inv);
  2611. const float w3 = exp(-9.0 * denom_inv);
  2612. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3));
  2613. // Statically normalize weights, sum weighted samples, and return:
  2614. float3 sum = float3(0.0,0.0,0.0);
  2615. sum += w3 * tex2D_linearize(tex, tex_uv - 3.0 * dxdy).rgb;
  2616. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  2617. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  2618. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2619. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  2620. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  2621. sum += w3 * tex2D_linearize(tex, tex_uv + 3.0 * dxdy).rgb;
  2622. return sum * weight_sum_inv;
  2623. }
  2624. float3 tex2Dblur5resize(const sampler2D tex, const float2 tex_uv,
  2625. const float2 dxdy, const float sigma)
  2626. {
  2627. // Requires: Global requirements must be met (see file description).
  2628. // Returns: A 1D 5x Gaussian blurred texture lookup using a 5-tap blur.
  2629. // It may be mipmapped depending on settings and dxdy.
  2630. // First get the texel weights and normalization factor as above.
  2631. const float denom_inv = 0.5/(sigma*sigma);
  2632. const float w0 = 1.0;
  2633. const float w1 = exp(-1.0 * denom_inv);
  2634. const float w2 = exp(-4.0 * denom_inv);
  2635. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2));
  2636. // Statically normalize weights, sum weighted samples, and return:
  2637. float3 sum = float3(0.0,0.0,0.0);
  2638. sum += w2 * tex2D_linearize(tex, tex_uv - 2.0 * dxdy).rgb;
  2639. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  2640. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2641. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  2642. sum += w2 * tex2D_linearize(tex, tex_uv + 2.0 * dxdy).rgb;
  2643. return sum * weight_sum_inv;
  2644. }
  2645. float3 tex2Dblur3resize(const sampler2D tex, const float2 tex_uv,
  2646. const float2 dxdy, const float sigma)
  2647. {
  2648. // Requires: Global requirements must be met (see file description).
  2649. // Returns: A 1D 3x Gaussian blurred texture lookup using a 3-tap blur.
  2650. // It may be mipmapped depending on settings and dxdy.
  2651. // First get the texel weights and normalization factor as above.
  2652. const float denom_inv = 0.5/(sigma*sigma);
  2653. const float w0 = 1.0;
  2654. const float w1 = exp(-1.0 * denom_inv);
  2655. const float weight_sum_inv = 1.0 / (w0 + 2.0 * w1);
  2656. // Statically normalize weights, sum weighted samples, and return:
  2657. float3 sum = float3(0.0,0.0,0.0);
  2658. sum += w1 * tex2D_linearize(tex, tex_uv - 1.0 * dxdy).rgb;
  2659. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2660. sum += w1 * tex2D_linearize(tex, tex_uv + 1.0 * dxdy).rgb;
  2661. return sum * weight_sum_inv;
  2662. }
  2663. /////////////////////////// FAST SEPARABLE BLURS ///////////////////////////
  2664. float3 tex2Dblur11fast(const sampler2D tex, const float2 tex_uv,
  2665. const float2 dxdy, const float sigma)
  2666. {
  2667. // Requires: 1.) Global requirements must be met (see file description).
  2668. // 2.) filter_linearN must = "true" in your .cgp file.
  2669. // 3.) For gamma-correct bilinear filtering, global
  2670. // gamma_aware_bilinear == true (from gamma-management.h)
  2671. // Returns: A 1D 11x Gaussian blurred texture lookup using 6 linear
  2672. // taps. It may be mipmapped depending on settings and dxdy.
  2673. // First get the texel weights and normalization factor as above.
  2674. const float denom_inv = 0.5/(sigma*sigma);
  2675. const float w0 = 1.0;
  2676. const float w1 = exp(-1.0 * denom_inv);
  2677. const float w2 = exp(-4.0 * denom_inv);
  2678. const float w3 = exp(-9.0 * denom_inv);
  2679. const float w4 = exp(-16.0 * denom_inv);
  2680. const float w5 = exp(-25.0 * denom_inv);
  2681. const float weight_sum_inv = 1.0 /
  2682. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5));
  2683. // Calculate combined weights and linear sample ratios between texel pairs.
  2684. // The center texel (with weight w0) is used twice, so halve its weight.
  2685. const float w01 = w0 * 0.5 + w1;
  2686. const float w23 = w2 + w3;
  2687. const float w45 = w4 + w5;
  2688. const float w01_ratio = w1/w01;
  2689. const float w23_ratio = w3/w23;
  2690. const float w45_ratio = w5/w45;
  2691. // Statically normalize weights, sum weighted samples, and return:
  2692. float3 sum = float3(0.0,0.0,0.0);
  2693. sum += w45 * tex2D_linearize(tex, tex_uv - (4.0 + w45_ratio) * dxdy).rgb;
  2694. sum += w23 * tex2D_linearize(tex, tex_uv - (2.0 + w23_ratio) * dxdy).rgb;
  2695. sum += w01 * tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb;
  2696. sum += w01 * tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb;
  2697. sum += w23 * tex2D_linearize(tex, tex_uv + (2.0 + w23_ratio) * dxdy).rgb;
  2698. sum += w45 * tex2D_linearize(tex, tex_uv + (4.0 + w45_ratio) * dxdy).rgb;
  2699. return sum * weight_sum_inv;
  2700. }
  2701. float3 tex2Dblur9fast(const sampler2D tex, const float2 tex_uv,
  2702. const float2 dxdy, const float sigma)
  2703. {
  2704. // Requires: Same as tex2Dblur11()
  2705. // Returns: A 1D 9x Gaussian blurred texture lookup using 1 nearest
  2706. // neighbor and 4 linear taps. It may be mipmapped depending
  2707. // on settings and dxdy.
  2708. // First get the texel weights and normalization factor as above.
  2709. const float denom_inv = 0.5/(sigma*sigma);
  2710. const float w0 = 1.0;
  2711. const float w1 = exp(-1.0 * denom_inv);
  2712. const float w2 = exp(-4.0 * denom_inv);
  2713. const float w3 = exp(-9.0 * denom_inv);
  2714. const float w4 = exp(-16.0 * denom_inv);
  2715. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  2716. // Calculate combined weights and linear sample ratios between texel pairs.
  2717. const float w12 = w1 + w2;
  2718. const float w34 = w3 + w4;
  2719. const float w12_ratio = w2/w12;
  2720. const float w34_ratio = w4/w34;
  2721. // Statically normalize weights, sum weighted samples, and return:
  2722. float3 sum = float3(0.0,0.0,0.0);
  2723. sum += w34 * tex2D_linearize(tex, tex_uv - (3.0 + w34_ratio) * dxdy).rgb;
  2724. sum += w12 * tex2D_linearize(tex, tex_uv - (1.0 + w12_ratio) * dxdy).rgb;
  2725. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2726. sum += w12 * tex2D_linearize(tex, tex_uv + (1.0 + w12_ratio) * dxdy).rgb;
  2727. sum += w34 * tex2D_linearize(tex, tex_uv + (3.0 + w34_ratio) * dxdy).rgb;
  2728. return sum * weight_sum_inv;
  2729. }
  2730. float3 tex2Dblur7fast(const sampler2D tex, const float2 tex_uv,
  2731. const float2 dxdy, const float sigma)
  2732. {
  2733. // Requires: Same as tex2Dblur11()
  2734. // Returns: A 1D 7x Gaussian blurred texture lookup using 4 linear
  2735. // taps. It may be mipmapped depending on settings and dxdy.
  2736. // First get the texel weights and normalization factor as above.
  2737. const float denom_inv = 0.5/(sigma*sigma);
  2738. const float w0 = 1.0;
  2739. const float w1 = exp(-1.0 * denom_inv);
  2740. const float w2 = exp(-4.0 * denom_inv);
  2741. const float w3 = exp(-9.0 * denom_inv);
  2742. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3));
  2743. // Calculate combined weights and linear sample ratios between texel pairs.
  2744. // The center texel (with weight w0) is used twice, so halve its weight.
  2745. const float w01 = w0 * 0.5 + w1;
  2746. const float w23 = w2 + w3;
  2747. const float w01_ratio = w1/w01;
  2748. const float w23_ratio = w3/w23;
  2749. // Statically normalize weights, sum weighted samples, and return:
  2750. float3 sum = float3(0.0,0.0,0.0);
  2751. sum += w23 * tex2D_linearize(tex, tex_uv - (2.0 + w23_ratio) * dxdy).rgb;
  2752. sum += w01 * tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb;
  2753. sum += w01 * tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb;
  2754. sum += w23 * tex2D_linearize(tex, tex_uv + (2.0 + w23_ratio) * dxdy).rgb;
  2755. return sum * weight_sum_inv;
  2756. }
  2757. float3 tex2Dblur5fast(const sampler2D tex, const float2 tex_uv,
  2758. const float2 dxdy, const float sigma)
  2759. {
  2760. // Requires: Same as tex2Dblur11()
  2761. // Returns: A 1D 5x Gaussian blurred texture lookup using 1 nearest
  2762. // neighbor and 2 linear taps. It may be mipmapped depending
  2763. // on settings and dxdy.
  2764. // First get the texel weights and normalization factor as above.
  2765. const float denom_inv = 0.5/(sigma*sigma);
  2766. const float w0 = 1.0;
  2767. const float w1 = exp(-1.0 * denom_inv);
  2768. const float w2 = exp(-4.0 * denom_inv);
  2769. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2));
  2770. // Calculate combined weights and linear sample ratios between texel pairs.
  2771. const float w12 = w1 + w2;
  2772. const float w12_ratio = w2/w12;
  2773. // Statically normalize weights, sum weighted samples, and return:
  2774. float3 sum = float3(0.0,0.0,0.0);
  2775. sum += w12 * tex2D_linearize(tex, tex_uv - (1.0 + w12_ratio) * dxdy).rgb;
  2776. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2777. sum += w12 * tex2D_linearize(tex, tex_uv + (1.0 + w12_ratio) * dxdy).rgb;
  2778. return sum * weight_sum_inv;
  2779. }
  2780. float3 tex2Dblur3fast(const sampler2D tex, const float2 tex_uv,
  2781. const float2 dxdy, const float sigma)
  2782. {
  2783. // Requires: Same as tex2Dblur11()
  2784. // Returns: A 1D 3x Gaussian blurred texture lookup using 2 linear
  2785. // taps. It may be mipmapped depending on settings and dxdy.
  2786. // First get the texel weights and normalization factor as above.
  2787. const float denom_inv = 0.5/(sigma*sigma);
  2788. const float w0 = 1.0;
  2789. const float w1 = exp(-1.0 * denom_inv);
  2790. const float weight_sum_inv = 1.0 / (w0 + 2.0 * w1);
  2791. // Calculate combined weights and linear sample ratios between texel pairs.
  2792. // The center texel (with weight w0) is used twice, so halve its weight.
  2793. const float w01 = w0 * 0.5 + w1;
  2794. const float w01_ratio = w1/w01;
  2795. // Weights for all samples are the same, so just average them:
  2796. return 0.5 * (
  2797. tex2D_linearize(tex, tex_uv - w01_ratio * dxdy).rgb +
  2798. tex2D_linearize(tex, tex_uv + w01_ratio * dxdy).rgb);
  2799. }
  2800. //////////////////////////// HUGE SEPARABLE BLURS ////////////////////////////
  2801. // Huge separable blurs come only in "fast" versions.
  2802. float3 tex2Dblur43fast(const sampler2D tex, const float2 tex_uv,
  2803. const float2 dxdy, const float sigma)
  2804. {
  2805. // Requires: Same as tex2Dblur11()
  2806. // Returns: A 1D 43x Gaussian blurred texture lookup using 22 linear
  2807. // taps. It may be mipmapped depending on settings and dxdy.
  2808. // First get the texel weights and normalization factor as above.
  2809. const float denom_inv = 0.5/(sigma*sigma);
  2810. const float w0 = 1.0;
  2811. const float w1 = exp(-1.0 * denom_inv);
  2812. const float w2 = exp(-4.0 * denom_inv);
  2813. const float w3 = exp(-9.0 * denom_inv);
  2814. const float w4 = exp(-16.0 * denom_inv);
  2815. const float w5 = exp(-25.0 * denom_inv);
  2816. const float w6 = exp(-36.0 * denom_inv);
  2817. const float w7 = exp(-49.0 * denom_inv);
  2818. const float w8 = exp(-64.0 * denom_inv);
  2819. const float w9 = exp(-81.0 * denom_inv);
  2820. const float w10 = exp(-100.0 * denom_inv);
  2821. const float w11 = exp(-121.0 * denom_inv);
  2822. const float w12 = exp(-144.0 * denom_inv);
  2823. const float w13 = exp(-169.0 * denom_inv);
  2824. const float w14 = exp(-196.0 * denom_inv);
  2825. const float w15 = exp(-225.0 * denom_inv);
  2826. const float w16 = exp(-256.0 * denom_inv);
  2827. const float w17 = exp(-289.0 * denom_inv);
  2828. const float w18 = exp(-324.0 * denom_inv);
  2829. const float w19 = exp(-361.0 * denom_inv);
  2830. const float w20 = exp(-400.0 * denom_inv);
  2831. const float w21 = exp(-441.0 * denom_inv);
  2832. //const float weight_sum_inv = 1.0 /
  2833. // (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 +
  2834. // w12 + w13 + w14 + w15 + w16 + w17 + w18 + w19 + w20 + w21));
  2835. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  2836. // Calculate combined weights and linear sample ratios between texel pairs.
  2837. // The center texel (with weight w0) is used twice, so halve its weight.
  2838. const float w0_1 = w0 * 0.5 + w1;
  2839. const float w2_3 = w2 + w3;
  2840. const float w4_5 = w4 + w5;
  2841. const float w6_7 = w6 + w7;
  2842. const float w8_9 = w8 + w9;
  2843. const float w10_11 = w10 + w11;
  2844. const float w12_13 = w12 + w13;
  2845. const float w14_15 = w14 + w15;
  2846. const float w16_17 = w16 + w17;
  2847. const float w18_19 = w18 + w19;
  2848. const float w20_21 = w20 + w21;
  2849. const float w0_1_ratio = w1/w0_1;
  2850. const float w2_3_ratio = w3/w2_3;
  2851. const float w4_5_ratio = w5/w4_5;
  2852. const float w6_7_ratio = w7/w6_7;
  2853. const float w8_9_ratio = w9/w8_9;
  2854. const float w10_11_ratio = w11/w10_11;
  2855. const float w12_13_ratio = w13/w12_13;
  2856. const float w14_15_ratio = w15/w14_15;
  2857. const float w16_17_ratio = w17/w16_17;
  2858. const float w18_19_ratio = w19/w18_19;
  2859. const float w20_21_ratio = w21/w20_21;
  2860. // Statically normalize weights, sum weighted samples, and return:
  2861. float3 sum = float3(0.0,0.0,0.0);
  2862. sum += w20_21 * tex2D_linearize(tex, tex_uv - (20.0 + w20_21_ratio) * dxdy).rgb;
  2863. sum += w18_19 * tex2D_linearize(tex, tex_uv - (18.0 + w18_19_ratio) * dxdy).rgb;
  2864. sum += w16_17 * tex2D_linearize(tex, tex_uv - (16.0 + w16_17_ratio) * dxdy).rgb;
  2865. sum += w14_15 * tex2D_linearize(tex, tex_uv - (14.0 + w14_15_ratio) * dxdy).rgb;
  2866. sum += w12_13 * tex2D_linearize(tex, tex_uv - (12.0 + w12_13_ratio) * dxdy).rgb;
  2867. sum += w10_11 * tex2D_linearize(tex, tex_uv - (10.0 + w10_11_ratio) * dxdy).rgb;
  2868. sum += w8_9 * tex2D_linearize(tex, tex_uv - (8.0 + w8_9_ratio) * dxdy).rgb;
  2869. sum += w6_7 * tex2D_linearize(tex, tex_uv - (6.0 + w6_7_ratio) * dxdy).rgb;
  2870. sum += w4_5 * tex2D_linearize(tex, tex_uv - (4.0 + w4_5_ratio) * dxdy).rgb;
  2871. sum += w2_3 * tex2D_linearize(tex, tex_uv - (2.0 + w2_3_ratio) * dxdy).rgb;
  2872. sum += w0_1 * tex2D_linearize(tex, tex_uv - w0_1_ratio * dxdy).rgb;
  2873. sum += w0_1 * tex2D_linearize(tex, tex_uv + w0_1_ratio * dxdy).rgb;
  2874. sum += w2_3 * tex2D_linearize(tex, tex_uv + (2.0 + w2_3_ratio) * dxdy).rgb;
  2875. sum += w4_5 * tex2D_linearize(tex, tex_uv + (4.0 + w4_5_ratio) * dxdy).rgb;
  2876. sum += w6_7 * tex2D_linearize(tex, tex_uv + (6.0 + w6_7_ratio) * dxdy).rgb;
  2877. sum += w8_9 * tex2D_linearize(tex, tex_uv + (8.0 + w8_9_ratio) * dxdy).rgb;
  2878. sum += w10_11 * tex2D_linearize(tex, tex_uv + (10.0 + w10_11_ratio) * dxdy).rgb;
  2879. sum += w12_13 * tex2D_linearize(tex, tex_uv + (12.0 + w12_13_ratio) * dxdy).rgb;
  2880. sum += w14_15 * tex2D_linearize(tex, tex_uv + (14.0 + w14_15_ratio) * dxdy).rgb;
  2881. sum += w16_17 * tex2D_linearize(tex, tex_uv + (16.0 + w16_17_ratio) * dxdy).rgb;
  2882. sum += w18_19 * tex2D_linearize(tex, tex_uv + (18.0 + w18_19_ratio) * dxdy).rgb;
  2883. sum += w20_21 * tex2D_linearize(tex, tex_uv + (20.0 + w20_21_ratio) * dxdy).rgb;
  2884. return sum * weight_sum_inv;
  2885. }
  2886. float3 tex2Dblur31fast(const sampler2D tex, const float2 tex_uv,
  2887. const float2 dxdy, const float sigma)
  2888. {
  2889. // Requires: Same as tex2Dblur11()
  2890. // Returns: A 1D 31x Gaussian blurred texture lookup using 16 linear
  2891. // taps. It may be mipmapped depending on settings and dxdy.
  2892. // First get the texel weights and normalization factor as above.
  2893. const float denom_inv = 0.5/(sigma*sigma);
  2894. const float w0 = 1.0;
  2895. const float w1 = exp(-1.0 * denom_inv);
  2896. const float w2 = exp(-4.0 * denom_inv);
  2897. const float w3 = exp(-9.0 * denom_inv);
  2898. const float w4 = exp(-16.0 * denom_inv);
  2899. const float w5 = exp(-25.0 * denom_inv);
  2900. const float w6 = exp(-36.0 * denom_inv);
  2901. const float w7 = exp(-49.0 * denom_inv);
  2902. const float w8 = exp(-64.0 * denom_inv);
  2903. const float w9 = exp(-81.0 * denom_inv);
  2904. const float w10 = exp(-100.0 * denom_inv);
  2905. const float w11 = exp(-121.0 * denom_inv);
  2906. const float w12 = exp(-144.0 * denom_inv);
  2907. const float w13 = exp(-169.0 * denom_inv);
  2908. const float w14 = exp(-196.0 * denom_inv);
  2909. const float w15 = exp(-225.0 * denom_inv);
  2910. //const float weight_sum_inv = 1.0 /
  2911. // (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 +
  2912. // w9 + w10 + w11 + w12 + w13 + w14 + w15));
  2913. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  2914. // Calculate combined weights and linear sample ratios between texel pairs.
  2915. // The center texel (with weight w0) is used twice, so halve its weight.
  2916. const float w0_1 = w0 * 0.5 + w1;
  2917. const float w2_3 = w2 + w3;
  2918. const float w4_5 = w4 + w5;
  2919. const float w6_7 = w6 + w7;
  2920. const float w8_9 = w8 + w9;
  2921. const float w10_11 = w10 + w11;
  2922. const float w12_13 = w12 + w13;
  2923. const float w14_15 = w14 + w15;
  2924. const float w0_1_ratio = w1/w0_1;
  2925. const float w2_3_ratio = w3/w2_3;
  2926. const float w4_5_ratio = w5/w4_5;
  2927. const float w6_7_ratio = w7/w6_7;
  2928. const float w8_9_ratio = w9/w8_9;
  2929. const float w10_11_ratio = w11/w10_11;
  2930. const float w12_13_ratio = w13/w12_13;
  2931. const float w14_15_ratio = w15/w14_15;
  2932. // Statically normalize weights, sum weighted samples, and return:
  2933. float3 sum = float3(0.0,0.0,0.0);
  2934. sum += w14_15 * tex2D_linearize(tex, tex_uv - (14.0 + w14_15_ratio) * dxdy).rgb;
  2935. sum += w12_13 * tex2D_linearize(tex, tex_uv - (12.0 + w12_13_ratio) * dxdy).rgb;
  2936. sum += w10_11 * tex2D_linearize(tex, tex_uv - (10.0 + w10_11_ratio) * dxdy).rgb;
  2937. sum += w8_9 * tex2D_linearize(tex, tex_uv - (8.0 + w8_9_ratio) * dxdy).rgb;
  2938. sum += w6_7 * tex2D_linearize(tex, tex_uv - (6.0 + w6_7_ratio) * dxdy).rgb;
  2939. sum += w4_5 * tex2D_linearize(tex, tex_uv - (4.0 + w4_5_ratio) * dxdy).rgb;
  2940. sum += w2_3 * tex2D_linearize(tex, tex_uv - (2.0 + w2_3_ratio) * dxdy).rgb;
  2941. sum += w0_1 * tex2D_linearize(tex, tex_uv - w0_1_ratio * dxdy).rgb;
  2942. sum += w0_1 * tex2D_linearize(tex, tex_uv + w0_1_ratio * dxdy).rgb;
  2943. sum += w2_3 * tex2D_linearize(tex, tex_uv + (2.0 + w2_3_ratio) * dxdy).rgb;
  2944. sum += w4_5 * tex2D_linearize(tex, tex_uv + (4.0 + w4_5_ratio) * dxdy).rgb;
  2945. sum += w6_7 * tex2D_linearize(tex, tex_uv + (6.0 + w6_7_ratio) * dxdy).rgb;
  2946. sum += w8_9 * tex2D_linearize(tex, tex_uv + (8.0 + w8_9_ratio) * dxdy).rgb;
  2947. sum += w10_11 * tex2D_linearize(tex, tex_uv + (10.0 + w10_11_ratio) * dxdy).rgb;
  2948. sum += w12_13 * tex2D_linearize(tex, tex_uv + (12.0 + w12_13_ratio) * dxdy).rgb;
  2949. sum += w14_15 * tex2D_linearize(tex, tex_uv + (14.0 + w14_15_ratio) * dxdy).rgb;
  2950. return sum * weight_sum_inv;
  2951. }
  2952. float3 tex2Dblur25fast(const sampler2D tex, const float2 tex_uv,
  2953. const float2 dxdy, const float sigma)
  2954. {
  2955. // Requires: Same as tex2Dblur11()
  2956. // Returns: A 1D 25x Gaussian blurred texture lookup using 1 nearest
  2957. // neighbor and 12 linear taps. It may be mipmapped depending
  2958. // on settings and dxdy.
  2959. // First get the texel weights and normalization factor as above.
  2960. const float denom_inv = 0.5/(sigma*sigma);
  2961. const float w0 = 1.0;
  2962. const float w1 = exp(-1.0 * denom_inv);
  2963. const float w2 = exp(-4.0 * denom_inv);
  2964. const float w3 = exp(-9.0 * denom_inv);
  2965. const float w4 = exp(-16.0 * denom_inv);
  2966. const float w5 = exp(-25.0 * denom_inv);
  2967. const float w6 = exp(-36.0 * denom_inv);
  2968. const float w7 = exp(-49.0 * denom_inv);
  2969. const float w8 = exp(-64.0 * denom_inv);
  2970. const float w9 = exp(-81.0 * denom_inv);
  2971. const float w10 = exp(-100.0 * denom_inv);
  2972. const float w11 = exp(-121.0 * denom_inv);
  2973. const float w12 = exp(-144.0 * denom_inv);
  2974. //const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  2975. // w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12));
  2976. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  2977. // Calculate combined weights and linear sample ratios between texel pairs.
  2978. const float w1_2 = w1 + w2;
  2979. const float w3_4 = w3 + w4;
  2980. const float w5_6 = w5 + w6;
  2981. const float w7_8 = w7 + w8;
  2982. const float w9_10 = w9 + w10;
  2983. const float w11_12 = w11 + w12;
  2984. const float w1_2_ratio = w2/w1_2;
  2985. const float w3_4_ratio = w4/w3_4;
  2986. const float w5_6_ratio = w6/w5_6;
  2987. const float w7_8_ratio = w8/w7_8;
  2988. const float w9_10_ratio = w10/w9_10;
  2989. const float w11_12_ratio = w12/w11_12;
  2990. // Statically normalize weights, sum weighted samples, and return:
  2991. float3 sum = float3(0.0,0.0,0.0);
  2992. sum += w11_12 * tex2D_linearize(tex, tex_uv - (11.0 + w11_12_ratio) * dxdy).rgb;
  2993. sum += w9_10 * tex2D_linearize(tex, tex_uv - (9.0 + w9_10_ratio) * dxdy).rgb;
  2994. sum += w7_8 * tex2D_linearize(tex, tex_uv - (7.0 + w7_8_ratio) * dxdy).rgb;
  2995. sum += w5_6 * tex2D_linearize(tex, tex_uv - (5.0 + w5_6_ratio) * dxdy).rgb;
  2996. sum += w3_4 * tex2D_linearize(tex, tex_uv - (3.0 + w3_4_ratio) * dxdy).rgb;
  2997. sum += w1_2 * tex2D_linearize(tex, tex_uv - (1.0 + w1_2_ratio) * dxdy).rgb;
  2998. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  2999. sum += w1_2 * tex2D_linearize(tex, tex_uv + (1.0 + w1_2_ratio) * dxdy).rgb;
  3000. sum += w3_4 * tex2D_linearize(tex, tex_uv + (3.0 + w3_4_ratio) * dxdy).rgb;
  3001. sum += w5_6 * tex2D_linearize(tex, tex_uv + (5.0 + w5_6_ratio) * dxdy).rgb;
  3002. sum += w7_8 * tex2D_linearize(tex, tex_uv + (7.0 + w7_8_ratio) * dxdy).rgb;
  3003. sum += w9_10 * tex2D_linearize(tex, tex_uv + (9.0 + w9_10_ratio) * dxdy).rgb;
  3004. sum += w11_12 * tex2D_linearize(tex, tex_uv + (11.0 + w11_12_ratio) * dxdy).rgb;
  3005. return sum * weight_sum_inv;
  3006. }
  3007. float3 tex2Dblur17fast(const sampler2D tex, const float2 tex_uv,
  3008. const float2 dxdy, const float sigma)
  3009. {
  3010. // Requires: Same as tex2Dblur11()
  3011. // Returns: A 1D 17x Gaussian blurred texture lookup using 1 nearest
  3012. // neighbor and 8 linear taps. It may be mipmapped depending
  3013. // on settings and dxdy.
  3014. // First get the texel weights and normalization factor as above.
  3015. const float denom_inv = 0.5/(sigma*sigma);
  3016. const float w0 = 1.0;
  3017. const float w1 = exp(-1.0 * denom_inv);
  3018. const float w2 = exp(-4.0 * denom_inv);
  3019. const float w3 = exp(-9.0 * denom_inv);
  3020. const float w4 = exp(-16.0 * denom_inv);
  3021. const float w5 = exp(-25.0 * denom_inv);
  3022. const float w6 = exp(-36.0 * denom_inv);
  3023. const float w7 = exp(-49.0 * denom_inv);
  3024. const float w8 = exp(-64.0 * denom_inv);
  3025. //const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  3026. // w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8));
  3027. const float weight_sum_inv = get_fast_gaussian_weight_sum_inv(sigma);
  3028. // Calculate combined weights and linear sample ratios between texel pairs.
  3029. const float w1_2 = w1 + w2;
  3030. const float w3_4 = w3 + w4;
  3031. const float w5_6 = w5 + w6;
  3032. const float w7_8 = w7 + w8;
  3033. const float w1_2_ratio = w2/w1_2;
  3034. const float w3_4_ratio = w4/w3_4;
  3035. const float w5_6_ratio = w6/w5_6;
  3036. const float w7_8_ratio = w8/w7_8;
  3037. // Statically normalize weights, sum weighted samples, and return:
  3038. float3 sum = float3(0.0,0.0,0.0);
  3039. sum += w7_8 * tex2D_linearize(tex, tex_uv - (7.0 + w7_8_ratio) * dxdy).rgb;
  3040. sum += w5_6 * tex2D_linearize(tex, tex_uv - (5.0 + w5_6_ratio) * dxdy).rgb;
  3041. sum += w3_4 * tex2D_linearize(tex, tex_uv - (3.0 + w3_4_ratio) * dxdy).rgb;
  3042. sum += w1_2 * tex2D_linearize(tex, tex_uv - (1.0 + w1_2_ratio) * dxdy).rgb;
  3043. sum += w0 * tex2D_linearize(tex, tex_uv).rgb;
  3044. sum += w1_2 * tex2D_linearize(tex, tex_uv + (1.0 + w1_2_ratio) * dxdy).rgb;
  3045. sum += w3_4 * tex2D_linearize(tex, tex_uv + (3.0 + w3_4_ratio) * dxdy).rgb;
  3046. sum += w5_6 * tex2D_linearize(tex, tex_uv + (5.0 + w5_6_ratio) * dxdy).rgb;
  3047. sum += w7_8 * tex2D_linearize(tex, tex_uv + (7.0 + w7_8_ratio) * dxdy).rgb;
  3048. return sum * weight_sum_inv;
  3049. }
  3050. //////////////////// ARBITRARILY RESIZABLE ONE-PASS BLURS ////////////////////
  3051. float3 tex2Dblur3x3resize(const sampler2D tex, const float2 tex_uv,
  3052. const float2 dxdy, const float sigma)
  3053. {
  3054. // Requires: Global requirements must be met (see file description).
  3055. // Returns: A 3x3 Gaussian blurred mipmapped texture lookup of the
  3056. // resized input.
  3057. // Description:
  3058. // This is the only arbitrarily resizable one-pass blur; tex2Dblur5x5resize
  3059. // would perform like tex2Dblur9x9, MUCH slower than tex2Dblur5resize.
  3060. const float denom_inv = 0.5/(sigma*sigma);
  3061. // Load each sample. We need all 3x3 samples. Quad-pixel communication
  3062. // won't help either: This should perform like tex2Dblur5x5, but sharing a
  3063. // 4x4 sample field would perform more like tex2Dblur8x8shared (worse).
  3064. const float2 sample4_uv = tex_uv;
  3065. const float2 dx = float2(dxdy.x, 0.0);
  3066. const float2 dy = float2(0.0, dxdy.y);
  3067. const float2 sample1_uv = sample4_uv - dy;
  3068. const float2 sample7_uv = sample4_uv + dy;
  3069. const float3 sample0 = tex2D_linearize(tex, sample1_uv - dx).rgb;
  3070. const float3 sample1 = tex2D_linearize(tex, sample1_uv).rgb;
  3071. const float3 sample2 = tex2D_linearize(tex, sample1_uv + dx).rgb;
  3072. const float3 sample3 = tex2D_linearize(tex, sample4_uv - dx).rgb;
  3073. const float3 sample4 = tex2D_linearize(tex, sample4_uv).rgb;
  3074. const float3 sample5 = tex2D_linearize(tex, sample4_uv + dx).rgb;
  3075. const float3 sample6 = tex2D_linearize(tex, sample7_uv - dx).rgb;
  3076. const float3 sample7 = tex2D_linearize(tex, sample7_uv).rgb;
  3077. const float3 sample8 = tex2D_linearize(tex, sample7_uv + dx).rgb;
  3078. // Statically compute Gaussian sample weights:
  3079. const float w4 = 1.0;
  3080. const float w1_3_5_7 = exp(-LENGTH_SQ(float2(1.0, 0.0)) * denom_inv);
  3081. const float w0_2_6_8 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  3082. const float weight_sum_inv = 1.0/(w4 + 4.0 * (w1_3_5_7 + w0_2_6_8));
  3083. // Weight and sum the samples:
  3084. const float3 sum = w4 * sample4 +
  3085. w1_3_5_7 * (sample1 + sample3 + sample5 + sample7) +
  3086. w0_2_6_8 * (sample0 + sample2 + sample6 + sample8);
  3087. return sum * weight_sum_inv;
  3088. }
  3089. //////////////////////////// FASTER ONE-PASS BLURS ///////////////////////////
  3090. float3 tex2Dblur9x9(const sampler2D tex, const float2 tex_uv,
  3091. const float2 dxdy, const float sigma)
  3092. {
  3093. // Perform a 1-pass 9x9 blur with 5x5 bilinear samples.
  3094. // Requires: Same as tex2Dblur9()
  3095. // Returns: A 9x9 Gaussian blurred mipmapped texture lookup composed of
  3096. // 5x5 carefully selected bilinear samples.
  3097. // Description:
  3098. // Perform a 1-pass 9x9 blur with 5x5 bilinear samples. Adjust the
  3099. // bilinear sample location to reflect the true Gaussian weights for each
  3100. // underlying texel. The following diagram illustrates the relative
  3101. // locations of bilinear samples. Each sample with the same number has the
  3102. // same weight (notice the symmetry). The letters a, b, c, d distinguish
  3103. // quadrants, and the letters U, D, L, R, C (up, down, left, right, center)
  3104. // distinguish 1D directions along the line containing the pixel center:
  3105. // 6a 5a 2U 5b 6b
  3106. // 4a 3a 1U 3b 4b
  3107. // 2L 1L 0C 1R 2R
  3108. // 4c 3c 1D 3d 4d
  3109. // 6c 5c 2D 5d 6d
  3110. // The following diagram illustrates the underlying equally spaced texels,
  3111. // named after the sample that accesses them and subnamed by their location
  3112. // within their 2x2, 2x1, 1x2, or 1x1 texel block:
  3113. // 6a4 6a3 5a4 5a3 2U2 5b3 5b4 6b3 6b4
  3114. // 6a2 6a1 5a2 5a1 2U1 5b1 5b2 6b1 6b2
  3115. // 4a4 4a3 3a4 3a3 1U2 3b3 3b4 4b3 4b4
  3116. // 4a2 4a1 3a2 3a1 1U1 3b1 3b2 4b1 4b2
  3117. // 2L2 2L1 1L2 1L1 0C1 1R1 1R2 2R1 2R2
  3118. // 4c2 4c1 3c2 3c1 1D1 3d1 3d2 4d1 4d2
  3119. // 4c4 4c3 3c4 3c3 1D2 3d3 3d4 4d3 4d4
  3120. // 6c2 6c1 5c2 5c1 2D1 5d1 5d2 6d1 6d2
  3121. // 6c4 6c3 5c4 5c3 2D2 5d3 5d4 6d3 6d4
  3122. // Note there is only one C texel and only two texels for each U, D, L, or
  3123. // R sample. The center sample is effectively a nearest neighbor sample,
  3124. // and the U/D/L/R samples use 1D linear filtering. All other texels are
  3125. // read with bilinear samples somewhere within their 2x2 texel blocks.
  3126. // COMPUTE TEXTURE COORDS:
  3127. // Statically compute sampling offsets within each 2x2 texel block, based
  3128. // on 1D sampling ratios between texels [1, 2] and [3, 4] texels away from
  3129. // the center, and reuse them independently for both dimensions. Compute
  3130. // these offsets based on the relative 1D Gaussian weights of the texels
  3131. // in question. (w1off means "Gaussian weight for the texel 1.0 texels
  3132. // away from the pixel center," etc.).
  3133. const float denom_inv = 0.5/(sigma*sigma);
  3134. const float w1off = exp(-1.0 * denom_inv);
  3135. const float w2off = exp(-4.0 * denom_inv);
  3136. const float w3off = exp(-9.0 * denom_inv);
  3137. const float w4off = exp(-16.0 * denom_inv);
  3138. const float texel1to2ratio = w2off/(w1off + w2off);
  3139. const float texel3to4ratio = w4off/(w3off + w4off);
  3140. // Statically compute texel offsets from the fragment center to each
  3141. // bilinear sample in the bottom-right quadrant, including x-axis-aligned:
  3142. const float2 sample1R_texel_offset = float2(1.0, 0.0) + float2(texel1to2ratio, 0.0);
  3143. const float2 sample2R_texel_offset = float2(3.0, 0.0) + float2(texel3to4ratio, 0.0);
  3144. const float2 sample3d_texel_offset = float2(1.0, 1.0) + float2(texel1to2ratio, texel1to2ratio);
  3145. const float2 sample4d_texel_offset = float2(3.0, 1.0) + float2(texel3to4ratio, texel1to2ratio);
  3146. const float2 sample5d_texel_offset = float2(1.0, 3.0) + float2(texel1to2ratio, texel3to4ratio);
  3147. const float2 sample6d_texel_offset = float2(3.0, 3.0) + float2(texel3to4ratio, texel3to4ratio);
  3148. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  3149. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  3150. // Read underscores as "and."
  3151. const float w1R1 = w1off;
  3152. const float w1R2 = w2off;
  3153. const float w2R1 = w3off;
  3154. const float w2R2 = w4off;
  3155. const float w3d1 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  3156. const float w3d2_3d3 = exp(-LENGTH_SQ(float2(2.0, 1.0)) * denom_inv);
  3157. const float w3d4 = exp(-LENGTH_SQ(float2(2.0, 2.0)) * denom_inv);
  3158. const float w4d1_5d1 = exp(-LENGTH_SQ(float2(3.0, 1.0)) * denom_inv);
  3159. const float w4d2_5d3 = exp(-LENGTH_SQ(float2(4.0, 1.0)) * denom_inv);
  3160. const float w4d3_5d2 = exp(-LENGTH_SQ(float2(3.0, 2.0)) * denom_inv);
  3161. const float w4d4_5d4 = exp(-LENGTH_SQ(float2(4.0, 2.0)) * denom_inv);
  3162. const float w6d1 = exp(-LENGTH_SQ(float2(3.0, 3.0)) * denom_inv);
  3163. const float w6d2_6d3 = exp(-LENGTH_SQ(float2(4.0, 3.0)) * denom_inv);
  3164. const float w6d4 = exp(-LENGTH_SQ(float2(4.0, 4.0)) * denom_inv);
  3165. // Statically add texel weights in each sample to get sample weights:
  3166. const float w0 = 1.0;
  3167. const float w1 = w1R1 + w1R2;
  3168. const float w2 = w2R1 + w2R2;
  3169. const float w3 = w3d1 + 2.0 * w3d2_3d3 + w3d4;
  3170. const float w4 = w4d1_5d1 + w4d2_5d3 + w4d3_5d2 + w4d4_5d4;
  3171. const float w5 = w4;
  3172. const float w6 = w6d1 + 2.0 * w6d2_6d3 + w6d4;
  3173. // Get the weight sum inverse (normalization factor):
  3174. const float weight_sum_inv =
  3175. 1.0/(w0 + 4.0 * (w1 + w2 + w3 + w4 + w5 + w6));
  3176. // LOAD TEXTURE SAMPLES:
  3177. // Load all 25 samples (1 nearest, 8 linear, 16 bilinear) using symmetry:
  3178. const float2 mirror_x = float2(-1.0, 1.0);
  3179. const float2 mirror_y = float2(1.0, -1.0);
  3180. const float2 mirror_xy = float2(-1.0, -1.0);
  3181. const float2 dxdy_mirror_x = dxdy * mirror_x;
  3182. const float2 dxdy_mirror_y = dxdy * mirror_y;
  3183. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  3184. // Sampling order doesn't seem to affect performance, so just be clear:
  3185. const float3 sample0C = tex2D_linearize(tex, tex_uv).rgb;
  3186. const float3 sample1R = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset).rgb;
  3187. const float3 sample1D = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset.yx).rgb;
  3188. const float3 sample1L = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset).rgb;
  3189. const float3 sample1U = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset.yx).rgb;
  3190. const float3 sample2R = tex2D_linearize(tex, tex_uv + dxdy * sample2R_texel_offset).rgb;
  3191. const float3 sample2D = tex2D_linearize(tex, tex_uv + dxdy * sample2R_texel_offset.yx).rgb;
  3192. const float3 sample2L = tex2D_linearize(tex, tex_uv - dxdy * sample2R_texel_offset).rgb;
  3193. const float3 sample2U = tex2D_linearize(tex, tex_uv - dxdy * sample2R_texel_offset.yx).rgb;
  3194. const float3 sample3d = tex2D_linearize(tex, tex_uv + dxdy * sample3d_texel_offset).rgb;
  3195. const float3 sample3c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample3d_texel_offset).rgb;
  3196. const float3 sample3b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample3d_texel_offset).rgb;
  3197. const float3 sample3a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample3d_texel_offset).rgb;
  3198. const float3 sample4d = tex2D_linearize(tex, tex_uv + dxdy * sample4d_texel_offset).rgb;
  3199. const float3 sample4c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample4d_texel_offset).rgb;
  3200. const float3 sample4b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample4d_texel_offset).rgb;
  3201. const float3 sample4a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample4d_texel_offset).rgb;
  3202. const float3 sample5d = tex2D_linearize(tex, tex_uv + dxdy * sample5d_texel_offset).rgb;
  3203. const float3 sample5c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample5d_texel_offset).rgb;
  3204. const float3 sample5b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample5d_texel_offset).rgb;
  3205. const float3 sample5a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample5d_texel_offset).rgb;
  3206. const float3 sample6d = tex2D_linearize(tex, tex_uv + dxdy * sample6d_texel_offset).rgb;
  3207. const float3 sample6c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample6d_texel_offset).rgb;
  3208. const float3 sample6b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample6d_texel_offset).rgb;
  3209. const float3 sample6a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample6d_texel_offset).rgb;
  3210. // SUM WEIGHTED SAMPLES:
  3211. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  3212. float3 sum = w0 * sample0C;
  3213. sum += w1 * (sample1R + sample1D + sample1L + sample1U);
  3214. sum += w2 * (sample2R + sample2D + sample2L + sample2U);
  3215. sum += w3 * (sample3d + sample3c + sample3b + sample3a);
  3216. sum += w4 * (sample4d + sample4c + sample4b + sample4a);
  3217. sum += w5 * (sample5d + sample5c + sample5b + sample5a);
  3218. sum += w6 * (sample6d + sample6c + sample6b + sample6a);
  3219. return sum * weight_sum_inv;
  3220. }
  3221. float3 tex2Dblur7x7(const sampler2D tex, const float2 tex_uv,
  3222. const float2 dxdy, const float sigma)
  3223. {
  3224. // Perform a 1-pass 7x7 blur with 5x5 bilinear samples.
  3225. // Requires: Same as tex2Dblur9()
  3226. // Returns: A 7x7 Gaussian blurred mipmapped texture lookup composed of
  3227. // 4x4 carefully selected bilinear samples.
  3228. // Description:
  3229. // First see the descriptions for tex2Dblur9x9() and tex2Dblur7(). This
  3230. // blur mixes concepts from both. The sample layout is as follows:
  3231. // 4a 3a 3b 4b
  3232. // 2a 1a 1b 2b
  3233. // 2c 1c 1d 2d
  3234. // 4c 3c 3d 4d
  3235. // The texel layout is as follows. Note that samples 3a/3b, 1a/1b, 1c/1d,
  3236. // and 3c/3d share a vertical column of texels, and samples 2a/2c, 1a/1c,
  3237. // 1b/1d, and 2b/2d share a horizontal row of texels (all sample1's share
  3238. // the center texel):
  3239. // 4a4 4a3 3a4 3ab3 3b4 4b3 4b4
  3240. // 4a2 4a1 3a2 3ab1 3b2 4b1 4b2
  3241. // 2a4 2a3 1a4 1ab3 1b4 2b3 2b4
  3242. // 2ac2 2ac1 1ac2 1* 1bd2 2bd1 2bd2
  3243. // 2c4 2c3 1c4 1cd3 1d4 2d3 2d4
  3244. // 4c2 4c1 3c2 3cd1 3d2 4d1 4d2
  3245. // 4c4 4c3 3c4 3cd3 3d4 4d3 4d4
  3246. // COMPUTE TEXTURE COORDS:
  3247. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  3248. const float denom_inv = 0.5/(sigma*sigma);
  3249. const float w0off = 1.0;
  3250. const float w1off = exp(-1.0 * denom_inv);
  3251. const float w2off = exp(-4.0 * denom_inv);
  3252. const float w3off = exp(-9.0 * denom_inv);
  3253. const float texel0to1ratio = w1off/(w0off * 0.5 + w1off);
  3254. const float texel2to3ratio = w3off/(w2off + w3off);
  3255. // Statically compute texel offsets from the fragment center to each
  3256. // bilinear sample in the bottom-right quadrant, including axis-aligned:
  3257. const float2 sample1d_texel_offset = float2(texel0to1ratio, texel0to1ratio);
  3258. const float2 sample2d_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  3259. const float2 sample3d_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  3260. const float2 sample4d_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  3261. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  3262. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  3263. // Read underscores as "and."
  3264. const float w1abcd = 1.0;
  3265. const float w1bd2_1cd3 = exp(-LENGTH_SQ(float2(1.0, 0.0)) * denom_inv);
  3266. const float w2bd1_3cd1 = exp(-LENGTH_SQ(float2(2.0, 0.0)) * denom_inv);
  3267. const float w2bd2_3cd2 = exp(-LENGTH_SQ(float2(3.0, 0.0)) * denom_inv);
  3268. const float w1d4 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  3269. const float w2d3_3d2 = exp(-LENGTH_SQ(float2(2.0, 1.0)) * denom_inv);
  3270. const float w2d4_3d4 = exp(-LENGTH_SQ(float2(3.0, 1.0)) * denom_inv);
  3271. const float w4d1 = exp(-LENGTH_SQ(float2(2.0, 2.0)) * denom_inv);
  3272. const float w4d2_4d3 = exp(-LENGTH_SQ(float2(3.0, 2.0)) * denom_inv);
  3273. const float w4d4 = exp(-LENGTH_SQ(float2(3.0, 3.0)) * denom_inv);
  3274. // Statically add texel weights in each sample to get sample weights.
  3275. // Split weights for shared texels between samples sharing them:
  3276. const float w1 = w1abcd * 0.25 + w1bd2_1cd3 + w1d4;
  3277. const float w2_3 = (w2bd1_3cd1 + w2bd2_3cd2) * 0.5 + w2d3_3d2 + w2d4_3d4;
  3278. const float w4 = w4d1 + 2.0 * w4d2_4d3 + w4d4;
  3279. // Get the weight sum inverse (normalization factor):
  3280. const float weight_sum_inv =
  3281. 1.0/(4.0 * (w1 + 2.0 * w2_3 + w4));
  3282. // LOAD TEXTURE SAMPLES:
  3283. // Load all 16 samples using symmetry:
  3284. const float2 mirror_x = float2(-1.0, 1.0);
  3285. const float2 mirror_y = float2(1.0, -1.0);
  3286. const float2 mirror_xy = float2(-1.0, -1.0);
  3287. const float2 dxdy_mirror_x = dxdy * mirror_x;
  3288. const float2 dxdy_mirror_y = dxdy * mirror_y;
  3289. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  3290. const float3 sample1a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample1d_texel_offset).rgb;
  3291. const float3 sample2a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample2d_texel_offset).rgb;
  3292. const float3 sample3a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample3d_texel_offset).rgb;
  3293. const float3 sample4a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample4d_texel_offset).rgb;
  3294. const float3 sample1b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample1d_texel_offset).rgb;
  3295. const float3 sample2b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample2d_texel_offset).rgb;
  3296. const float3 sample3b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample3d_texel_offset).rgb;
  3297. const float3 sample4b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample4d_texel_offset).rgb;
  3298. const float3 sample1c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample1d_texel_offset).rgb;
  3299. const float3 sample2c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample2d_texel_offset).rgb;
  3300. const float3 sample3c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample3d_texel_offset).rgb;
  3301. const float3 sample4c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample4d_texel_offset).rgb;
  3302. const float3 sample1d = tex2D_linearize(tex, tex_uv + dxdy * sample1d_texel_offset).rgb;
  3303. const float3 sample2d = tex2D_linearize(tex, tex_uv + dxdy * sample2d_texel_offset).rgb;
  3304. const float3 sample3d = tex2D_linearize(tex, tex_uv + dxdy * sample3d_texel_offset).rgb;
  3305. const float3 sample4d = tex2D_linearize(tex, tex_uv + dxdy * sample4d_texel_offset).rgb;
  3306. // SUM WEIGHTED SAMPLES:
  3307. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  3308. float3 sum = float3(0.0,0.0,0.0);
  3309. sum += w1 * (sample1a + sample1b + sample1c + sample1d);
  3310. sum += w2_3 * (sample2a + sample2b + sample2c + sample2d);
  3311. sum += w2_3 * (sample3a + sample3b + sample3c + sample3d);
  3312. sum += w4 * (sample4a + sample4b + sample4c + sample4d);
  3313. return sum * weight_sum_inv;
  3314. }
  3315. float3 tex2Dblur5x5(const sampler2D tex, const float2 tex_uv,
  3316. const float2 dxdy, const float sigma)
  3317. {
  3318. // Perform a 1-pass 5x5 blur with 3x3 bilinear samples.
  3319. // Requires: Same as tex2Dblur9()
  3320. // Returns: A 5x5 Gaussian blurred mipmapped texture lookup composed of
  3321. // 3x3 carefully selected bilinear samples.
  3322. // Description:
  3323. // First see the description for tex2Dblur9x9(). This blur uses the same
  3324. // concept and sample/texel locations except on a smaller scale. Samples:
  3325. // 2a 1U 2b
  3326. // 1L 0C 1R
  3327. // 2c 1D 2d
  3328. // Texels:
  3329. // 2a4 2a3 1U2 2b3 2b4
  3330. // 2a2 2a1 1U1 2b1 2b2
  3331. // 1L2 1L1 0C1 1R1 1R2
  3332. // 2c2 2c1 1D1 2d1 2d2
  3333. // 2c4 2c3 1D2 2d3 2d4
  3334. // COMPUTE TEXTURE COORDS:
  3335. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  3336. const float denom_inv = 0.5/(sigma*sigma);
  3337. const float w1off = exp(-1.0 * denom_inv);
  3338. const float w2off = exp(-4.0 * denom_inv);
  3339. const float texel1to2ratio = w2off/(w1off + w2off);
  3340. // Statically compute texel offsets from the fragment center to each
  3341. // bilinear sample in the bottom-right quadrant, including x-axis-aligned:
  3342. const float2 sample1R_texel_offset = float2(1.0, 0.0) + float2(texel1to2ratio, 0.0);
  3343. const float2 sample2d_texel_offset = float2(1.0, 1.0) + float2(texel1to2ratio, texel1to2ratio);
  3344. // CALCULATE KERNEL WEIGHTS FOR ALL SAMPLES:
  3345. // Statically compute Gaussian texel weights for the bottom-right quadrant.
  3346. // Read underscores as "and."
  3347. const float w1R1 = w1off;
  3348. const float w1R2 = w2off;
  3349. const float w2d1 = exp(-LENGTH_SQ(float2(1.0, 1.0)) * denom_inv);
  3350. const float w2d2_3 = exp(-LENGTH_SQ(float2(2.0, 1.0)) * denom_inv);
  3351. const float w2d4 = exp(-LENGTH_SQ(float2(2.0, 2.0)) * denom_inv);
  3352. // Statically add texel weights in each sample to get sample weights:
  3353. const float w0 = 1.0;
  3354. const float w1 = w1R1 + w1R2;
  3355. const float w2 = w2d1 + 2.0 * w2d2_3 + w2d4;
  3356. // Get the weight sum inverse (normalization factor):
  3357. const float weight_sum_inv = 1.0/(w0 + 4.0 * (w1 + w2));
  3358. // LOAD TEXTURE SAMPLES:
  3359. // Load all 9 samples (1 nearest, 4 linear, 4 bilinear) using symmetry:
  3360. const float2 mirror_x = float2(-1.0, 1.0);
  3361. const float2 mirror_y = float2(1.0, -1.0);
  3362. const float2 mirror_xy = float2(-1.0, -1.0);
  3363. const float2 dxdy_mirror_x = dxdy * mirror_x;
  3364. const float2 dxdy_mirror_y = dxdy * mirror_y;
  3365. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  3366. const float3 sample0C = tex2D_linearize(tex, tex_uv).rgb;
  3367. const float3 sample1R = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset).rgb;
  3368. const float3 sample1D = tex2D_linearize(tex, tex_uv + dxdy * sample1R_texel_offset.yx).rgb;
  3369. const float3 sample1L = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset).rgb;
  3370. const float3 sample1U = tex2D_linearize(tex, tex_uv - dxdy * sample1R_texel_offset.yx).rgb;
  3371. const float3 sample2d = tex2D_linearize(tex, tex_uv + dxdy * sample2d_texel_offset).rgb;
  3372. const float3 sample2c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample2d_texel_offset).rgb;
  3373. const float3 sample2b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample2d_texel_offset).rgb;
  3374. const float3 sample2a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample2d_texel_offset).rgb;
  3375. // SUM WEIGHTED SAMPLES:
  3376. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  3377. float3 sum = w0 * sample0C;
  3378. sum += w1 * (sample1R + sample1D + sample1L + sample1U);
  3379. sum += w2 * (sample2a + sample2b + sample2c + sample2d);
  3380. return sum * weight_sum_inv;
  3381. }
  3382. float3 tex2Dblur3x3(const sampler2D tex, const float2 tex_uv,
  3383. const float2 dxdy, const float sigma)
  3384. {
  3385. // Perform a 1-pass 3x3 blur with 5x5 bilinear samples.
  3386. // Requires: Same as tex2Dblur9()
  3387. // Returns: A 3x3 Gaussian blurred mipmapped texture lookup composed of
  3388. // 2x2 carefully selected bilinear samples.
  3389. // Description:
  3390. // First see the descriptions for tex2Dblur9x9() and tex2Dblur7(). This
  3391. // blur mixes concepts from both. The sample layout is as follows:
  3392. // 0a 0b
  3393. // 0c 0d
  3394. // The texel layout is as follows. Note that samples 0a/0b and 0c/0d share
  3395. // a vertical column of texels, and samples 0a/0c and 0b/0d share a
  3396. // horizontal row of texels (all samples share the center texel):
  3397. // 0a3 0ab2 0b3
  3398. // 0ac1 0*0 0bd1
  3399. // 0c3 0cd2 0d3
  3400. // COMPUTE TEXTURE COORDS:
  3401. // Statically compute bilinear sampling offsets (details in tex2Dblur9x9).
  3402. const float denom_inv = 0.5/(sigma*sigma);
  3403. const float w0off = 1.0;
  3404. const float w1off = exp(-1.0 * denom_inv);
  3405. const float texel0to1ratio = w1off/(w0off * 0.5 + w1off);
  3406. // Statically compute texel offsets from the fragment center to each
  3407. // bilinear sample in the bottom-right quadrant, including axis-aligned:
  3408. const float2 sample0d_texel_offset = float2(texel0to1ratio, texel0to1ratio);
  3409. // LOAD TEXTURE SAMPLES:
  3410. // Load all 4 samples using symmetry:
  3411. const float2 mirror_x = float2(-1.0, 1.0);
  3412. const float2 mirror_y = float2(1.0, -1.0);
  3413. const float2 mirror_xy = float2(-1.0, -1.0);
  3414. const float2 dxdy_mirror_x = dxdy * mirror_x;
  3415. const float2 dxdy_mirror_y = dxdy * mirror_y;
  3416. const float2 dxdy_mirror_xy = dxdy * mirror_xy;
  3417. const float3 sample0a = tex2D_linearize(tex, tex_uv + dxdy_mirror_xy * sample0d_texel_offset).rgb;
  3418. const float3 sample0b = tex2D_linearize(tex, tex_uv + dxdy_mirror_y * sample0d_texel_offset).rgb;
  3419. const float3 sample0c = tex2D_linearize(tex, tex_uv + dxdy_mirror_x * sample0d_texel_offset).rgb;
  3420. const float3 sample0d = tex2D_linearize(tex, tex_uv + dxdy * sample0d_texel_offset).rgb;
  3421. // SUM WEIGHTED SAMPLES:
  3422. // Weights for all samples are the same, so just average them:
  3423. return 0.25 * (sample0a + sample0b + sample0c + sample0d);
  3424. }
  3425. ////////////////// LINEAR ONE-PASS BLURS WITH SHARED SAMPLES /////////////////
  3426. float3 tex2Dblur12x12shared(const sampler2D tex,
  3427. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  3428. const float sigma)
  3429. {
  3430. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  3431. // Requires: 1.) Same as tex2Dblur9()
  3432. // 2.) ddx() and ddy() are present in the current Cg profile.
  3433. // 3.) The GPU driver is using fine/high-quality derivatives.
  3434. // 4.) quad_vector *correctly* describes the current fragment's
  3435. // location in its pixel quad, by the conventions noted in
  3436. // get_quad_vector[_naive].
  3437. // 5.) tex_uv.w = log2(IN.video_size/IN.output_size).y
  3438. // 6.) tex2Dlod() is present in the current Cg profile.
  3439. // Optional: Tune artifacts vs. excessive blurriness with the global
  3440. // float error_blurring.
  3441. // Returns: A blurred texture lookup using a "virtual" 12x12 Gaussian
  3442. // blur (a 6x6 blur of carefully selected bilinear samples)
  3443. // of the given mip level. There will be subtle inaccuracies,
  3444. // especially for small or high-frequency detailed sources.
  3445. // Description:
  3446. // Perform a 1-pass blur with shared texture lookups across a pixel quad.
  3447. // We'll get neighboring samples with high-quality ddx/ddy derivatives, as
  3448. // in GPU Pro 2, Chapter VI.2, "Shader Amortization using Pixel Quad
  3449. // Message Passing" by Eric Penner.
  3450. //
  3451. // Our "virtual" 12x12 blur will be comprised of ((6 - 1)^2)/4 + 3 = 12
  3452. // bilinear samples, where bilinear sampling positions are computed from
  3453. // the relative Gaussian weights of the 4 surrounding texels. The catch is
  3454. // that the appropriate texel weights and sample coords differ for each
  3455. // fragment, but we're reusing most of the same samples across a quad of
  3456. // destination fragments. (We do use unique coords for the four nearest
  3457. // samples at each fragment.) Mixing bilinear filtering and sample-sharing
  3458. // therefore introduces some error into the weights, and this can get nasty
  3459. // when the source image is small or high-frequency. Computing bilinear
  3460. // ratios based on weights at the sample field center results in sharpening
  3461. // and ringing artifacts, but we can move samples closer to halfway between
  3462. // texels to try blurring away the error (which can move features around by
  3463. // a texel or so). Tune this with the global float "error_blurring".
  3464. //
  3465. // The pixel quad's sample field covers 12x12 texels, accessed through 6x6
  3466. // bilinear (2x2 texel) taps. Each fragment depends on a window of 10x10
  3467. // texels (5x5 bilinear taps), and each fragment is responsible for loading
  3468. // a 6x6 texel quadrant as a 3x3 block of bilinear taps, plus 3 more taps
  3469. // to use unique bilinear coords for sample0* for each fragment. This
  3470. // diagram illustrates the relative locations of bilinear samples 1-9 for
  3471. // each quadrant a, b, c, d (note samples will not be equally spaced):
  3472. // 8a 7a 6a 6b 7b 8b
  3473. // 5a 4a 3a 3b 4b 5b
  3474. // 2a 1a 0a 0b 1b 2b
  3475. // 2c 1c 0c 0d 1d 2d
  3476. // 5c 4c 3c 3d 4d 5d
  3477. // 8c 7c 6c 6d 7d 8d
  3478. // The following diagram illustrates the underlying equally spaced texels,
  3479. // named after the sample that accesses them and subnamed by their location
  3480. // within their 2x2 texel block:
  3481. // 8a3 8a2 7a3 7a2 6a3 6a2 6b2 6b3 7b2 7b3 8b2 8b3
  3482. // 8a1 8a0 7a1 7a0 6a1 6a0 6b0 6b1 7b0 7b1 8b0 8b1
  3483. // 5a3 5a2 4a3 4a2 3a3 3a2 3b2 3b3 4b2 4b3 5b2 5b3
  3484. // 5a1 5a0 4a1 4a0 3a1 3a0 3b0 3b1 4b0 4b1 5b0 5b1
  3485. // 2a3 2a2 1a3 1a2 0a3 0a2 0b2 0b3 1b2 1b3 2b2 2b3
  3486. // 2a1 2a0 1a1 1a0 0a1 0a0 0b0 0b1 1b0 1b1 2b0 2b1
  3487. // 2c1 2c0 1c1 1c0 0c1 0c0 0d0 0d1 1d0 1d1 2d0 2d1
  3488. // 2c3 2c2 1c3 1c2 0c3 0c2 0d2 0d3 1d2 1d3 2d2 2d3
  3489. // 5c1 5c0 4c1 4c0 3c1 3c0 3d0 3d1 4d0 4d1 5d0 5d1
  3490. // 5c3 5c2 4c3 4c2 3c3 3c2 3d2 3d3 4d2 4d3 5d2 5d3
  3491. // 8c1 8c0 7c1 7c0 6c1 6c0 6d0 6d1 7d0 7d1 8d0 8d1
  3492. // 8c3 8c2 7c3 7c2 6c3 6c2 6d2 6d3 7d2 7d3 8d2 8d3
  3493. // With this symmetric arrangement, we don't have to know which absolute
  3494. // quadrant a sample lies in to assign kernel weights; it's enough to know
  3495. // the sample number and the relative quadrant of the sample (relative to
  3496. // the current quadrant):
  3497. // {current, adjacent x, adjacent y, diagonal}
  3498. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3499. // Statically compute sampling offsets within each 2x2 texel block, based
  3500. // on appropriate 1D Gaussian sampling ratio between texels [0, 1], [2, 3],
  3501. // and [4, 5] away from the fragment, and reuse them independently for both
  3502. // dimensions. Use the sample field center as the estimated destination,
  3503. // but nudge the result closer to halfway between texels to blur error.
  3504. const float denom_inv = 0.5/(sigma*sigma);
  3505. const float w0off = 1.0;
  3506. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  3507. const float w1off = exp(-(1.0*1.0) * denom_inv);
  3508. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  3509. const float w2off = exp(-(2.0*2.0) * denom_inv);
  3510. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  3511. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  3512. const float w4_5off = exp(-(4.5*4.5) * denom_inv);
  3513. const float w5_5off = exp(-(5.5*5.5) * denom_inv);
  3514. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  3515. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  3516. const float texel4to5ratio = lerp(w5_5off/(w4_5off + w5_5off), 0.5, error_blurring);
  3517. // We don't share sample0*, so use the nearest destination fragment:
  3518. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  3519. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  3520. // Statically compute texel offsets from the bottom-right fragment to each
  3521. // bilinear sample in the bottom-right quadrant:
  3522. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  3523. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  3524. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  3525. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  3526. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  3527. const float2 sample2_texel_offset = float2(4.0, 0.0) + float2(texel4to5ratio, texel0to1ratio);
  3528. const float2 sample3_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  3529. const float2 sample4_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  3530. const float2 sample5_texel_offset = float2(4.0, 2.0) + float2(texel4to5ratio, texel2to3ratio);
  3531. const float2 sample6_texel_offset = float2(0.0, 4.0) + float2(texel0to1ratio, texel4to5ratio);
  3532. const float2 sample7_texel_offset = float2(2.0, 4.0) + float2(texel2to3ratio, texel4to5ratio);
  3533. const float2 sample8_texel_offset = float2(4.0, 4.0) + float2(texel4to5ratio, texel4to5ratio);
  3534. // CALCULATE KERNEL WEIGHTS:
  3535. // Statically compute bilinear sample weights at each destination fragment
  3536. // based on the sum of their 4 underlying texel weights. Assume a same-
  3537. // resolution blur, so each symmetrically named sample weight will compute
  3538. // the same at every fragment in the pixel quad: We can therefore compute
  3539. // texel weights based only on the bottom-right quadrant (fragment at 0d0).
  3540. // Too avoid too much boilerplate code, use a macro to get all 4 texel
  3541. // weights for a bilinear sample based on the offset of its top-left texel:
  3542. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  3543. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  3544. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  3545. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  3546. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  3547. const float w8diag = GET_TEXEL_QUAD_WEIGHTS(-6.0, -6.0);
  3548. const float w7diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -6.0);
  3549. const float w6diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -6.0);
  3550. const float w6adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -6.0);
  3551. const float w7adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -6.0);
  3552. const float w8adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -6.0);
  3553. const float w5diag = GET_TEXEL_QUAD_WEIGHTS(-6.0, -4.0);
  3554. const float w4diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -4.0);
  3555. const float w3diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -4.0);
  3556. const float w3adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -4.0);
  3557. const float w4adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -4.0);
  3558. const float w5adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -4.0);
  3559. const float w2diag = GET_TEXEL_QUAD_WEIGHTS(-6.0, -2.0);
  3560. const float w1diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -2.0);
  3561. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  3562. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  3563. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  3564. const float w2adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -2.0);
  3565. const float w2adjx = GET_TEXEL_QUAD_WEIGHTS(-6.0, 0.0);
  3566. const float w1adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 0.0);
  3567. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  3568. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  3569. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  3570. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 0.0);
  3571. const float w5adjx = GET_TEXEL_QUAD_WEIGHTS(-6.0, 2.0);
  3572. const float w4adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 2.0);
  3573. const float w3adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  3574. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  3575. const float w4curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  3576. const float w5curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 2.0);
  3577. const float w8adjx = GET_TEXEL_QUAD_WEIGHTS(-6.0, 4.0);
  3578. const float w7adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 4.0);
  3579. const float w6adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 4.0);
  3580. const float w6curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 4.0);
  3581. const float w7curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 4.0);
  3582. const float w8curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 4.0);
  3583. #undef GET_TEXEL_QUAD_WEIGHTS
  3584. // Statically pack weights for runtime:
  3585. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  3586. const float4 w1 = float4(w1curr, w1adjx, w1adjy, w1diag);
  3587. const float4 w2 = float4(w2curr, w2adjx, w2adjy, w2diag);
  3588. const float4 w3 = float4(w3curr, w3adjx, w3adjy, w3diag);
  3589. const float4 w4 = float4(w4curr, w4adjx, w4adjy, w4diag);
  3590. const float4 w5 = float4(w5curr, w5adjx, w5adjy, w5diag);
  3591. const float4 w6 = float4(w6curr, w6adjx, w6adjy, w6diag);
  3592. const float4 w7 = float4(w7curr, w7adjx, w7adjy, w7diag);
  3593. const float4 w8 = float4(w8curr, w8adjx, w8adjy, w8diag);
  3594. // Get the weight sum inverse (normalization factor):
  3595. const float4 weight_sum4 = w0 + w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8;
  3596. const float2 weight_sum2 = weight_sum4.xy + weight_sum4.zw;
  3597. const float weight_sum = weight_sum2.x + weight_sum2.y;
  3598. const float weight_sum_inv = 1.0/(weight_sum);
  3599. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3600. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  3601. const float2 dxdy_curr = dxdy * quad_vector.xy;
  3602. // Load bilinear samples for the current quadrant (for this fragment):
  3603. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  3604. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  3605. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  3606. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  3607. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  3608. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  3609. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  3610. const float3 sample4curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample4_texel_offset)).rgb;
  3611. const float3 sample5curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample5_texel_offset)).rgb;
  3612. const float3 sample6curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample6_texel_offset)).rgb;
  3613. const float3 sample7curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample7_texel_offset)).rgb;
  3614. const float3 sample8curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample8_texel_offset)).rgb;
  3615. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  3616. // Fetch the samples from other fragments in the 2x2 quad:
  3617. float3 sample1adjx, sample1adjy, sample1diag;
  3618. float3 sample2adjx, sample2adjy, sample2diag;
  3619. float3 sample3adjx, sample3adjy, sample3diag;
  3620. float3 sample4adjx, sample4adjy, sample4diag;
  3621. float3 sample5adjx, sample5adjy, sample5diag;
  3622. float3 sample6adjx, sample6adjy, sample6diag;
  3623. float3 sample7adjx, sample7adjy, sample7diag;
  3624. float3 sample8adjx, sample8adjy, sample8diag;
  3625. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  3626. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  3627. quad_gather(quad_vector, sample3curr, sample3adjx, sample3adjy, sample3diag);
  3628. quad_gather(quad_vector, sample4curr, sample4adjx, sample4adjy, sample4diag);
  3629. quad_gather(quad_vector, sample5curr, sample5adjx, sample5adjy, sample5diag);
  3630. quad_gather(quad_vector, sample6curr, sample6adjx, sample6adjy, sample6diag);
  3631. quad_gather(quad_vector, sample7curr, sample7adjx, sample7adjy, sample7diag);
  3632. quad_gather(quad_vector, sample8curr, sample8adjx, sample8adjy, sample8diag);
  3633. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  3634. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  3635. // weights to obtain a weighted result:
  3636. float3 sum = float3(0.0,0.0,0.0);
  3637. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  3638. sum += mul(w1, float4x3(sample1curr, sample1adjx, sample1adjy, sample1diag));
  3639. sum += mul(w2, float4x3(sample2curr, sample2adjx, sample2adjy, sample2diag));
  3640. sum += mul(w3, float4x3(sample3curr, sample3adjx, sample3adjy, sample3diag));
  3641. sum += mul(w4, float4x3(sample4curr, sample4adjx, sample4adjy, sample4diag));
  3642. sum += mul(w5, float4x3(sample5curr, sample5adjx, sample5adjy, sample5diag));
  3643. sum += mul(w6, float4x3(sample6curr, sample6adjx, sample6adjy, sample6diag));
  3644. sum += mul(w7, float4x3(sample7curr, sample7adjx, sample7adjy, sample7diag));
  3645. sum += mul(w8, float4x3(sample8curr, sample8adjx, sample8adjy, sample8diag));
  3646. return sum * weight_sum_inv;
  3647. }
  3648. float3 tex2Dblur10x10shared(const sampler2D tex,
  3649. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  3650. const float sigma)
  3651. {
  3652. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  3653. // Requires: Same as tex2Dblur12x12shared()
  3654. // Returns: A blurred texture lookup using a "virtual" 10x10 Gaussian
  3655. // blur (a 5x5 blur of carefully selected bilinear samples)
  3656. // of the given mip level. There will be subtle inaccuracies,
  3657. // especially for small or high-frequency detailed sources.
  3658. // Description:
  3659. // First see the description for tex2Dblur12x12shared(). This
  3660. // function shares the same concept and sample placement, but each fragment
  3661. // only uses 25 of the 36 samples taken across the pixel quad (to cover a
  3662. // 5x5 sample area, or 10x10 texel area), and it uses a lower standard
  3663. // deviation to compensate. Thanks to symmetry, the 11 omitted samples
  3664. // are always the "same:"
  3665. // 8adjx, 2adjx, 5adjx,
  3666. // 6adjy, 7adjy, 8adjy,
  3667. // 2diag, 5diag, 6diag, 7diag, 8diag
  3668. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3669. // Statically compute bilinear sampling offsets (details in tex2Dblur12x12shared).
  3670. const float denom_inv = 0.5/(sigma*sigma);
  3671. const float w0off = 1.0;
  3672. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  3673. const float w1off = exp(-(1.0*1.0) * denom_inv);
  3674. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  3675. const float w2off = exp(-(2.0*2.0) * denom_inv);
  3676. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  3677. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  3678. const float w4_5off = exp(-(4.5*4.5) * denom_inv);
  3679. const float w5_5off = exp(-(5.5*5.5) * denom_inv);
  3680. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  3681. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  3682. const float texel4to5ratio = lerp(w5_5off/(w4_5off + w5_5off), 0.5, error_blurring);
  3683. // We don't share sample0*, so use the nearest destination fragment:
  3684. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  3685. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  3686. // Statically compute texel offsets from the bottom-right fragment to each
  3687. // bilinear sample in the bottom-right quadrant:
  3688. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  3689. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  3690. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  3691. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  3692. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  3693. const float2 sample2_texel_offset = float2(4.0, 0.0) + float2(texel4to5ratio, texel0to1ratio);
  3694. const float2 sample3_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  3695. const float2 sample4_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  3696. const float2 sample5_texel_offset = float2(4.0, 2.0) + float2(texel4to5ratio, texel2to3ratio);
  3697. const float2 sample6_texel_offset = float2(0.0, 4.0) + float2(texel0to1ratio, texel4to5ratio);
  3698. const float2 sample7_texel_offset = float2(2.0, 4.0) + float2(texel2to3ratio, texel4to5ratio);
  3699. const float2 sample8_texel_offset = float2(4.0, 4.0) + float2(texel4to5ratio, texel4to5ratio);
  3700. // CALCULATE KERNEL WEIGHTS:
  3701. // Statically compute bilinear sample weights at each destination fragment
  3702. // from the sum of their 4 texel weights (details in tex2Dblur12x12shared).
  3703. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  3704. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  3705. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  3706. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  3707. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  3708. // We only need 25 of the 36 sample weights. Skip the following weights:
  3709. // 8adjx, 2adjx, 5adjx,
  3710. // 6adjy, 7adjy, 8adjy,
  3711. // 2diag, 5diag, 6diag, 7diag, 8diag
  3712. const float w4diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -4.0);
  3713. const float w3diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -4.0);
  3714. const float w3adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -4.0);
  3715. const float w4adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -4.0);
  3716. const float w5adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -4.0);
  3717. const float w1diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -2.0);
  3718. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  3719. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  3720. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  3721. const float w2adjy = GET_TEXEL_QUAD_WEIGHTS(4.0, -2.0);
  3722. const float w1adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 0.0);
  3723. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  3724. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  3725. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  3726. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 0.0);
  3727. const float w4adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 2.0);
  3728. const float w3adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  3729. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  3730. const float w4curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  3731. const float w5curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 2.0);
  3732. const float w7adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 4.0);
  3733. const float w6adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 4.0);
  3734. const float w6curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 4.0);
  3735. const float w7curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 4.0);
  3736. const float w8curr = GET_TEXEL_QUAD_WEIGHTS(4.0, 4.0);
  3737. #undef GET_TEXEL_QUAD_WEIGHTS
  3738. // Get the weight sum inverse (normalization factor):
  3739. const float weight_sum_inv = 1.0/(w0curr + w1curr + w2curr + w3curr +
  3740. w4curr + w5curr + w6curr + w7curr + w8curr +
  3741. w0adjx + w1adjx + w3adjx + w4adjx + w6adjx + w7adjx +
  3742. w0adjy + w1adjy + w2adjy + w3adjy + w4adjy + w5adjy +
  3743. w0diag + w1diag + w3diag + w4diag);
  3744. // Statically pack most weights for runtime. Note the mixed packing:
  3745. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  3746. const float4 w1 = float4(w1curr, w1adjx, w1adjy, w1diag);
  3747. const float4 w3 = float4(w3curr, w3adjx, w3adjy, w3diag);
  3748. const float4 w4 = float4(w4curr, w4adjx, w4adjy, w4diag);
  3749. const float4 w2and5 = float4(w2curr, w2adjy, w5curr, w5adjy);
  3750. const float4 w6and7 = float4(w6curr, w6adjx, w7curr, w7adjx);
  3751. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3752. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  3753. const float2 dxdy_curr = dxdy * quad_vector.xy;
  3754. // Load bilinear samples for the current quadrant (for this fragment):
  3755. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  3756. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  3757. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  3758. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  3759. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  3760. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  3761. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  3762. const float3 sample4curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample4_texel_offset)).rgb;
  3763. const float3 sample5curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample5_texel_offset)).rgb;
  3764. const float3 sample6curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample6_texel_offset)).rgb;
  3765. const float3 sample7curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample7_texel_offset)).rgb;
  3766. const float3 sample8curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample8_texel_offset)).rgb;
  3767. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  3768. // Fetch the samples from other fragments in the 2x2 quad in order of need:
  3769. float3 sample1adjx, sample1adjy, sample1diag;
  3770. float3 sample2adjx, sample2adjy, sample2diag;
  3771. float3 sample3adjx, sample3adjy, sample3diag;
  3772. float3 sample4adjx, sample4adjy, sample4diag;
  3773. float3 sample5adjx, sample5adjy, sample5diag;
  3774. float3 sample6adjx, sample6adjy, sample6diag;
  3775. float3 sample7adjx, sample7adjy, sample7diag;
  3776. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  3777. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  3778. quad_gather(quad_vector, sample3curr, sample3adjx, sample3adjy, sample3diag);
  3779. quad_gather(quad_vector, sample4curr, sample4adjx, sample4adjy, sample4diag);
  3780. quad_gather(quad_vector, sample5curr, sample5adjx, sample5adjy, sample5diag);
  3781. quad_gather(quad_vector, sample6curr, sample6adjx, sample6adjy, sample6diag);
  3782. quad_gather(quad_vector, sample7curr, sample7adjx, sample7adjy, sample7diag);
  3783. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  3784. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  3785. // weights to obtain a weighted result. First do the simple ones:
  3786. float3 sum = float3(0.0,0.0,0.0);
  3787. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  3788. sum += mul(w1, float4x3(sample1curr, sample1adjx, sample1adjy, sample1diag));
  3789. sum += mul(w3, float4x3(sample3curr, sample3adjx, sample3adjy, sample3diag));
  3790. sum += mul(w4, float4x3(sample4curr, sample4adjx, sample4adjy, sample4diag));
  3791. // Now do the mixed-sample ones:
  3792. sum += mul(w2and5, float4x3(sample2curr, sample2adjy, sample5curr, sample5adjy));
  3793. sum += mul(w6and7, float4x3(sample6curr, sample6adjx, sample7curr, sample7adjx));
  3794. sum += w8curr * sample8curr;
  3795. // Normalize the sum (so the weights add to 1.0) and return:
  3796. return sum * weight_sum_inv;
  3797. }
  3798. float3 tex2Dblur8x8shared(const sampler2D tex,
  3799. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  3800. const float sigma)
  3801. {
  3802. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  3803. // Requires: Same as tex2Dblur12x12shared()
  3804. // Returns: A blurred texture lookup using a "virtual" 8x8 Gaussian
  3805. // blur (a 4x4 blur of carefully selected bilinear samples)
  3806. // of the given mip level. There will be subtle inaccuracies,
  3807. // especially for small or high-frequency detailed sources.
  3808. // Description:
  3809. // First see the description for tex2Dblur12x12shared(). This function
  3810. // shares the same concept and a similar sample placement, except each
  3811. // quadrant contains 4x4 texels and 2x2 samples instead of 6x6 and 3x3
  3812. // respectively. There could be a total of 16 samples, 4 of which each
  3813. // fragment is responsible for, but each fragment loads 0a/0b/0c/0d with
  3814. // its own offset to reduce shared sample artifacts, bringing the sample
  3815. // count for each fragment to 7. Sample placement:
  3816. // 3a 2a 2b 3b
  3817. // 1a 0a 0b 1b
  3818. // 1c 0c 0d 1d
  3819. // 3c 2c 2d 3d
  3820. // Texel placement:
  3821. // 3a3 3a2 2a3 2a2 2b2 2b3 3b2 3b3
  3822. // 3a1 3a0 2a1 2a0 2b0 2b1 3b0 3b1
  3823. // 1a3 1a2 0a3 0a2 0b2 0b3 1b2 1b3
  3824. // 1a1 1a0 0a1 0a0 0b0 0b1 1b0 1b1
  3825. // 1c1 1c0 0c1 0c0 0d0 0d1 1d0 1d1
  3826. // 1c3 1c2 0c3 0c2 0d2 0d3 1d2 1d3
  3827. // 3c1 3c0 2c1 2c0 2d0 2d1 3d0 4d1
  3828. // 3c3 3c2 2c3 2c2 2d2 2d3 3d2 4d3
  3829. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3830. // Statically compute bilinear sampling offsets (details in tex2Dblur12x12shared).
  3831. const float denom_inv = 0.5/(sigma*sigma);
  3832. const float w0off = 1.0;
  3833. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  3834. const float w1off = exp(-(1.0*1.0) * denom_inv);
  3835. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  3836. const float w2off = exp(-(2.0*2.0) * denom_inv);
  3837. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  3838. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  3839. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  3840. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  3841. // We don't share sample0*, so use the nearest destination fragment:
  3842. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  3843. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  3844. // Statically compute texel offsets from the bottom-right fragment to each
  3845. // bilinear sample in the bottom-right quadrant:
  3846. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  3847. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  3848. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  3849. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  3850. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  3851. const float2 sample2_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  3852. const float2 sample3_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  3853. // CALCULATE KERNEL WEIGHTS:
  3854. // Statically compute bilinear sample weights at each destination fragment
  3855. // from the sum of their 4 texel weights (details in tex2Dblur12x12shared).
  3856. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  3857. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  3858. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  3859. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  3860. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  3861. const float w3diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -4.0);
  3862. const float w2diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -4.0);
  3863. const float w2adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -4.0);
  3864. const float w3adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -4.0);
  3865. const float w1diag = GET_TEXEL_QUAD_WEIGHTS(-4.0, -2.0);
  3866. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  3867. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  3868. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  3869. const float w1adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 0.0);
  3870. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  3871. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  3872. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  3873. const float w3adjx = GET_TEXEL_QUAD_WEIGHTS(-4.0, 2.0);
  3874. const float w2adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  3875. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  3876. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  3877. #undef GET_TEXEL_QUAD_WEIGHTS
  3878. // Statically pack weights for runtime:
  3879. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  3880. const float4 w1 = float4(w1curr, w1adjx, w1adjy, w1diag);
  3881. const float4 w2 = float4(w2curr, w2adjx, w2adjy, w2diag);
  3882. const float4 w3 = float4(w3curr, w3adjx, w3adjy, w3diag);
  3883. // Get the weight sum inverse (normalization factor):
  3884. const float4 weight_sum4 = w0 + w1 + w2 + w3;
  3885. const float2 weight_sum2 = weight_sum4.xy + weight_sum4.zw;
  3886. const float weight_sum = weight_sum2.x + weight_sum2.y;
  3887. const float weight_sum_inv = 1.0/(weight_sum);
  3888. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3889. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  3890. const float2 dxdy_curr = dxdy * quad_vector.xy;
  3891. // Load bilinear samples for the current quadrant (for this fragment):
  3892. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  3893. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  3894. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  3895. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  3896. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  3897. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  3898. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  3899. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  3900. // Fetch the samples from other fragments in the 2x2 quad:
  3901. float3 sample1adjx, sample1adjy, sample1diag;
  3902. float3 sample2adjx, sample2adjy, sample2diag;
  3903. float3 sample3adjx, sample3adjy, sample3diag;
  3904. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  3905. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  3906. quad_gather(quad_vector, sample3curr, sample3adjx, sample3adjy, sample3diag);
  3907. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  3908. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  3909. // weights to obtain a weighted result:
  3910. float3 sum = float3(0.0,0.0,0.0);
  3911. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  3912. sum += mul(w1, float4x3(sample1curr, sample1adjx, sample1adjy, sample1diag));
  3913. sum += mul(w2, float4x3(sample2curr, sample2adjx, sample2adjy, sample2diag));
  3914. sum += mul(w3, float4x3(sample3curr, sample3adjx, sample3adjy, sample3diag));
  3915. return sum * weight_sum_inv;
  3916. }
  3917. float3 tex2Dblur6x6shared(const sampler2D tex,
  3918. const float4 tex_uv, const float2 dxdy, const float4 quad_vector,
  3919. const float sigma)
  3920. {
  3921. // Perform a 1-pass mipmapped blur with shared samples across a pixel quad.
  3922. // Requires: Same as tex2Dblur12x12shared()
  3923. // Returns: A blurred texture lookup using a "virtual" 6x6 Gaussian
  3924. // blur (a 3x3 blur of carefully selected bilinear samples)
  3925. // of the given mip level. There will be some inaccuracies,subtle inaccuracies,
  3926. // especially for small or high-frequency detailed sources.
  3927. // Description:
  3928. // First see the description for tex2Dblur8x8shared(). This
  3929. // function shares the same concept and sample placement, but each fragment
  3930. // only uses 9 of the 16 samples taken across the pixel quad (to cover a
  3931. // 3x3 sample area, or 6x6 texel area), and it uses a lower standard
  3932. // deviation to compensate. Thanks to symmetry, the 7 omitted samples
  3933. // are always the "same:"
  3934. // 1adjx, 3adjx
  3935. // 2adjy, 3adjy
  3936. // 1diag, 2diag, 3diag
  3937. // COMPUTE COORDS FOR TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3938. // Statically compute bilinear sampling offsets (details in tex2Dblur12x12shared).
  3939. const float denom_inv = 0.5/(sigma*sigma);
  3940. const float w0off = 1.0;
  3941. const float w0_5off = exp(-(0.5*0.5) * denom_inv);
  3942. const float w1off = exp(-(1.0*1.0) * denom_inv);
  3943. const float w1_5off = exp(-(1.5*1.5) * denom_inv);
  3944. const float w2off = exp(-(2.0*2.0) * denom_inv);
  3945. const float w2_5off = exp(-(2.5*2.5) * denom_inv);
  3946. const float w3_5off = exp(-(3.5*3.5) * denom_inv);
  3947. const float texel0to1ratio = lerp(w1_5off/(w0_5off + w1_5off), 0.5, error_blurring);
  3948. const float texel2to3ratio = lerp(w3_5off/(w2_5off + w3_5off), 0.5, error_blurring);
  3949. // We don't share sample0*, so use the nearest destination fragment:
  3950. const float texel0to1ratio_nearest = w1off/(w0off + w1off);
  3951. const float texel1to2ratio_nearest = w2off/(w1off + w2off);
  3952. // Statically compute texel offsets from the bottom-right fragment to each
  3953. // bilinear sample in the bottom-right quadrant:
  3954. const float2 sample0curr_texel_offset = float2(0.0, 0.0) + float2(texel0to1ratio_nearest, texel0to1ratio_nearest);
  3955. const float2 sample0adjx_texel_offset = float2(-1.0, 0.0) + float2(-texel1to2ratio_nearest, texel0to1ratio_nearest);
  3956. const float2 sample0adjy_texel_offset = float2(0.0, -1.0) + float2(texel0to1ratio_nearest, -texel1to2ratio_nearest);
  3957. const float2 sample0diag_texel_offset = float2(-1.0, -1.0) + float2(-texel1to2ratio_nearest, -texel1to2ratio_nearest);
  3958. const float2 sample1_texel_offset = float2(2.0, 0.0) + float2(texel2to3ratio, texel0to1ratio);
  3959. const float2 sample2_texel_offset = float2(0.0, 2.0) + float2(texel0to1ratio, texel2to3ratio);
  3960. const float2 sample3_texel_offset = float2(2.0, 2.0) + float2(texel2to3ratio, texel2to3ratio);
  3961. // CALCULATE KERNEL WEIGHTS:
  3962. // Statically compute bilinear sample weights at each destination fragment
  3963. // from the sum of their 4 texel weights (details in tex2Dblur12x12shared).
  3964. #define GET_TEXEL_QUAD_WEIGHTS(xoff, yoff) \
  3965. (exp(-LENGTH_SQ(float2(xoff, yoff)) * denom_inv) + \
  3966. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff)) * denom_inv) + \
  3967. exp(-LENGTH_SQ(float2(xoff, yoff + 1.0)) * denom_inv) + \
  3968. exp(-LENGTH_SQ(float2(xoff + 1.0, yoff + 1.0)) * denom_inv))
  3969. // We only need 9 of the 16 sample weights. Skip the following weights:
  3970. // 1adjx, 3adjx
  3971. // 2adjy, 3adjy
  3972. // 1diag, 2diag, 3diag
  3973. const float w0diag = GET_TEXEL_QUAD_WEIGHTS(-2.0, -2.0);
  3974. const float w0adjy = GET_TEXEL_QUAD_WEIGHTS(0.0, -2.0);
  3975. const float w1adjy = GET_TEXEL_QUAD_WEIGHTS(2.0, -2.0);
  3976. const float w0adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 0.0);
  3977. const float w0curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 0.0);
  3978. const float w1curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 0.0);
  3979. const float w2adjx = GET_TEXEL_QUAD_WEIGHTS(-2.0, 2.0);
  3980. const float w2curr = GET_TEXEL_QUAD_WEIGHTS(0.0, 2.0);
  3981. const float w3curr = GET_TEXEL_QUAD_WEIGHTS(2.0, 2.0);
  3982. #undef GET_TEXEL_QUAD_WEIGHTS
  3983. // Get the weight sum inverse (normalization factor):
  3984. const float weight_sum_inv = 1.0/(w0curr + w1curr + w2curr + w3curr +
  3985. w0adjx + w2adjx + w0adjy + w1adjy + w0diag);
  3986. // Statically pack some weights for runtime:
  3987. const float4 w0 = float4(w0curr, w0adjx, w0adjy, w0diag);
  3988. // LOAD TEXTURE SAMPLES THIS FRAGMENT IS RESPONSIBLE FOR:
  3989. // Get a uv vector from texel 0q0 of this quadrant to texel 0q3:
  3990. const float2 dxdy_curr = dxdy * quad_vector.xy;
  3991. // Load bilinear samples for the current quadrant (for this fragment):
  3992. const float3 sample0curr = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0curr_texel_offset).rgb;
  3993. const float3 sample0adjx = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjx_texel_offset).rgb;
  3994. const float3 sample0adjy = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0adjy_texel_offset).rgb;
  3995. const float3 sample0diag = tex2D_linearize(tex, tex_uv.xy + dxdy_curr * sample0diag_texel_offset).rgb;
  3996. const float3 sample1curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample1_texel_offset)).rgb;
  3997. const float3 sample2curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample2_texel_offset)).rgb;
  3998. const float3 sample3curr = tex2Dlod_linearize(tex, tex_uv + uv2_to_uv4(dxdy_curr * sample3_texel_offset)).rgb;
  3999. // GATHER NEIGHBORING SAMPLES AND SUM WEIGHTED SAMPLES:
  4000. // Fetch the samples from other fragments in the 2x2 quad:
  4001. float3 sample1adjx, sample1adjy, sample1diag;
  4002. float3 sample2adjx, sample2adjy, sample2diag;
  4003. quad_gather(quad_vector, sample1curr, sample1adjx, sample1adjy, sample1diag);
  4004. quad_gather(quad_vector, sample2curr, sample2adjx, sample2adjy, sample2diag);
  4005. // Statically normalize weights (so total = 1.0), and sum weighted samples.
  4006. // Fill each row of a matrix with an rgb sample and pre-multiply by the
  4007. // weights to obtain a weighted result for sample1*, and handle the rest
  4008. // of the weights more directly/verbosely:
  4009. float3 sum = float3(0.0,0.0,0.0);
  4010. sum += mul(w0, float4x3(sample0curr, sample0adjx, sample0adjy, sample0diag));
  4011. sum += w1curr * sample1curr + w1adjy * sample1adjy + w2curr * sample2curr +
  4012. w2adjx * sample2adjx + w3curr * sample3curr;
  4013. return sum * weight_sum_inv;
  4014. }
  4015. /////////////////////// MAX OPTIMAL SIGMA BLUR WRAPPERS //////////////////////
  4016. // The following blurs are static wrappers around the dynamic blurs above.
  4017. // HOPEFULLY, the compiler will be smart enough to do constant-folding.
  4018. // Resizable separable blurs:
  4019. inline float3 tex2Dblur11resize(const sampler2D tex, const float2 tex_uv,
  4020. const float2 dxdy)
  4021. {
  4022. return tex2Dblur11resize(tex, tex_uv, dxdy, blur11_std_dev);
  4023. }
  4024. inline float3 tex2Dblur9resize(const sampler2D tex, const float2 tex_uv,
  4025. const float2 dxdy)
  4026. {
  4027. return tex2Dblur9resize(tex, tex_uv, dxdy, blur9_std_dev);
  4028. }
  4029. inline float3 tex2Dblur7resize(const sampler2D tex, const float2 tex_uv,
  4030. const float2 dxdy)
  4031. {
  4032. return tex2Dblur7resize(tex, tex_uv, dxdy, blur7_std_dev);
  4033. }
  4034. inline float3 tex2Dblur5resize(const sampler2D tex, const float2 tex_uv,
  4035. const float2 dxdy)
  4036. {
  4037. return tex2Dblur5resize(tex, tex_uv, dxdy, blur5_std_dev);
  4038. }
  4039. inline float3 tex2Dblur3resize(const sampler2D tex, const float2 tex_uv,
  4040. const float2 dxdy)
  4041. {
  4042. return tex2Dblur3resize(tex, tex_uv, dxdy, blur3_std_dev);
  4043. }
  4044. // Fast separable blurs:
  4045. inline float3 tex2Dblur11fast(const sampler2D tex, const float2 tex_uv,
  4046. const float2 dxdy)
  4047. {
  4048. return tex2Dblur11fast(tex, tex_uv, dxdy, blur11_std_dev);
  4049. }
  4050. inline float3 tex2Dblur9fast(const sampler2D tex, const float2 tex_uv,
  4051. const float2 dxdy)
  4052. {
  4053. return tex2Dblur9fast(tex, tex_uv, dxdy, blur9_std_dev);
  4054. }
  4055. inline float3 tex2Dblur7fast(const sampler2D tex, const float2 tex_uv,
  4056. const float2 dxdy)
  4057. {
  4058. return tex2Dblur7fast(tex, tex_uv, dxdy, blur7_std_dev);
  4059. }
  4060. inline float3 tex2Dblur5fast(const sampler2D tex, const float2 tex_uv,
  4061. const float2 dxdy)
  4062. {
  4063. return tex2Dblur5fast(tex, tex_uv, dxdy, blur5_std_dev);
  4064. }
  4065. inline float3 tex2Dblur3fast(const sampler2D tex, const float2 tex_uv,
  4066. const float2 dxdy)
  4067. {
  4068. return tex2Dblur3fast(tex, tex_uv, dxdy, blur3_std_dev);
  4069. }
  4070. // Huge, "fast" separable blurs:
  4071. inline float3 tex2Dblur43fast(const sampler2D tex, const float2 tex_uv,
  4072. const float2 dxdy)
  4073. {
  4074. return tex2Dblur43fast(tex, tex_uv, dxdy, blur43_std_dev);
  4075. }
  4076. inline float3 tex2Dblur31fast(const sampler2D tex, const float2 tex_uv,
  4077. const float2 dxdy)
  4078. {
  4079. return tex2Dblur31fast(tex, tex_uv, dxdy, blur31_std_dev);
  4080. }
  4081. inline float3 tex2Dblur25fast(const sampler2D tex, const float2 tex_uv,
  4082. const float2 dxdy)
  4083. {
  4084. return tex2Dblur25fast(tex, tex_uv, dxdy, blur25_std_dev);
  4085. }
  4086. inline float3 tex2Dblur17fast(const sampler2D tex, const float2 tex_uv,
  4087. const float2 dxdy)
  4088. {
  4089. return tex2Dblur17fast(tex, tex_uv, dxdy, blur17_std_dev);
  4090. }
  4091. // Resizable one-pass blurs:
  4092. inline float3 tex2Dblur3x3resize(const sampler2D tex, const float2 tex_uv,
  4093. const float2 dxdy)
  4094. {
  4095. return tex2Dblur3x3resize(tex, tex_uv, dxdy, blur3_std_dev);
  4096. }
  4097. // "Fast" one-pass blurs:
  4098. inline float3 tex2Dblur9x9(const sampler2D tex, const float2 tex_uv,
  4099. const float2 dxdy)
  4100. {
  4101. return tex2Dblur9x9(tex, tex_uv, dxdy, blur9_std_dev);
  4102. }
  4103. inline float3 tex2Dblur7x7(const sampler2D tex, const float2 tex_uv,
  4104. const float2 dxdy)
  4105. {
  4106. return tex2Dblur7x7(tex, tex_uv, dxdy, blur7_std_dev);
  4107. }
  4108. inline float3 tex2Dblur5x5(const sampler2D tex, const float2 tex_uv,
  4109. const float2 dxdy)
  4110. {
  4111. return tex2Dblur5x5(tex, tex_uv, dxdy, blur5_std_dev);
  4112. }
  4113. inline float3 tex2Dblur3x3(const sampler2D tex, const float2 tex_uv,
  4114. const float2 dxdy)
  4115. {
  4116. return tex2Dblur3x3(tex, tex_uv, dxdy, blur3_std_dev);
  4117. }
  4118. // "Fast" shared-sample one-pass blurs:
  4119. inline float3 tex2Dblur12x12shared(const sampler2D tex,
  4120. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  4121. {
  4122. return tex2Dblur12x12shared(tex, tex_uv, dxdy, quad_vector, blur12_std_dev);
  4123. }
  4124. inline float3 tex2Dblur10x10shared(const sampler2D tex,
  4125. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  4126. {
  4127. return tex2Dblur10x10shared(tex, tex_uv, dxdy, quad_vector, blur10_std_dev);
  4128. }
  4129. inline float3 tex2Dblur8x8shared(const sampler2D tex,
  4130. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  4131. {
  4132. return tex2Dblur8x8shared(tex, tex_uv, dxdy, quad_vector, blur8_std_dev);
  4133. }
  4134. inline float3 tex2Dblur6x6shared(const sampler2D tex,
  4135. const float4 tex_uv, const float2 dxdy, const float4 quad_vector)
  4136. {
  4137. return tex2Dblur6x6shared(tex, tex_uv, dxdy, quad_vector, blur6_std_dev);
  4138. }
  4139. #endif // BLUR_FUNCTIONS_H
  4140. //////////////////////////// END BLUR-FUNCTIONS ///////////////////////////
  4141. /////////////////////////////// BLOOM CONSTANTS //////////////////////////////
  4142. // Compute constants with manual inlines of the functions below:
  4143. static const float bloom_diff_thresh = 1.0/256.0;
  4144. /////////////////////////////////// HELPERS //////////////////////////////////
  4145. inline float get_min_sigma_to_blur_triad(const float triad_size,
  4146. const float thresh)
  4147. {
  4148. // Requires: 1.) triad_size is the final phosphor triad size in pixels
  4149. // 2.) thresh is the max desired pixel difference in the
  4150. // blurred triad (e.g. 1.0/256.0).
  4151. // Returns: Return the minimum sigma that will fully blur a phosphor
  4152. // triad on the screen to an even color, within thresh.
  4153. // This closed-form function was found by curve-fitting data.
  4154. // Estimate: max error = ~0.086036, mean sq. error = ~0.0013387:
  4155. return -0.05168 + 0.6113*triad_size -
  4156. 1.122*triad_size*sqrt(0.000416 + thresh);
  4157. // Estimate: max error = ~0.16486, mean sq. error = ~0.0041041:
  4158. //return 0.5985*triad_size - triad_size*sqrt(thresh)
  4159. }
  4160. inline float get_absolute_scale_blur_sigma(const float thresh)
  4161. {
  4162. // Requires: 1.) min_expected_triads must be a global float. The number
  4163. // of horizontal phosphor triads in the final image must be
  4164. // >= min_allowed_viewport_triads.x for realistic results.
  4165. // 2.) bloom_approx_scale_x must be a global float equal to the
  4166. // absolute horizontal scale of BLOOM_APPROX.
  4167. // 3.) bloom_approx_scale_x/min_allowed_viewport_triads.x
  4168. // should be <= 1.1658025090 to keep the final result <
  4169. // 0.62666015625 (the largest sigma ensuring the largest
  4170. // unused texel weight stays < 1.0/256.0 for a 3x3 blur).
  4171. // 4.) thresh is the max desired pixel difference in the
  4172. // blurred triad (e.g. 1.0/256.0).
  4173. // Returns: Return the minimum Gaussian sigma that will blur the pass
  4174. // output as much as it would have taken to blur away
  4175. // bloom_approx_scale_x horizontal phosphor triads.
  4176. // Description:
  4177. // BLOOM_APPROX should look like a downscaled phosphor blur. Ideally, we'd
  4178. // use the same blur sigma as the actual phosphor bloom and scale it down
  4179. // to the current resolution with (bloom_approx_scale_x/viewport_size_x), but
  4180. // we don't know the viewport size in this pass. Instead, we'll blur as
  4181. // much as it would take to blur away min_allowed_viewport_triads.x. This
  4182. // will blur "more than necessary" if the user actually uses more triads,
  4183. // but that's not terrible either, because blurring a constant fraction of
  4184. // the viewport may better resemble a true optical bloom anyway (since the
  4185. // viewport will generally be about the same fraction of each player's
  4186. // field of view, regardless of screen size and resolution).
  4187. // Assume an extremely large viewport size for asymptotic results.
  4188. return bloom_approx_scale_x/max_viewport_size_x *
  4189. get_min_sigma_to_blur_triad(
  4190. max_viewport_size_x/min_allowed_viewport_triads.x, thresh);
  4191. }
  4192. inline float get_center_weight(const float sigma)
  4193. {
  4194. // Given a Gaussian blur sigma, get the blur weight for the center texel.
  4195. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  4196. return get_fast_gaussian_weight_sum_inv(sigma);
  4197. #else
  4198. const float denom_inv = 0.5/(sigma*sigma);
  4199. const float w0 = 1.0;
  4200. const float w1 = exp(-1.0 * denom_inv);
  4201. const float w2 = exp(-4.0 * denom_inv);
  4202. const float w3 = exp(-9.0 * denom_inv);
  4203. const float w4 = exp(-16.0 * denom_inv);
  4204. const float w5 = exp(-25.0 * denom_inv);
  4205. const float w6 = exp(-36.0 * denom_inv);
  4206. const float w7 = exp(-49.0 * denom_inv);
  4207. const float w8 = exp(-64.0 * denom_inv);
  4208. const float w9 = exp(-81.0 * denom_inv);
  4209. const float w10 = exp(-100.0 * denom_inv);
  4210. const float w11 = exp(-121.0 * denom_inv);
  4211. const float w12 = exp(-144.0 * denom_inv);
  4212. const float w13 = exp(-169.0 * denom_inv);
  4213. const float w14 = exp(-196.0 * denom_inv);
  4214. const float w15 = exp(-225.0 * denom_inv);
  4215. const float w16 = exp(-256.0 * denom_inv);
  4216. const float w17 = exp(-289.0 * denom_inv);
  4217. const float w18 = exp(-324.0 * denom_inv);
  4218. const float w19 = exp(-361.0 * denom_inv);
  4219. const float w20 = exp(-400.0 * denom_inv);
  4220. const float w21 = exp(-441.0 * denom_inv);
  4221. // Note: If the implementation uses a smaller blur than the max allowed,
  4222. // the worst case scenario is that the center weight will be overestimated,
  4223. // so we'll put a bit more energy into the brightpass...no huge deal.
  4224. // Then again, if the implementation uses a larger blur than the max
  4225. // "allowed" because of dynamic branching, the center weight could be
  4226. // underestimated, which is more of a problem...consider always using
  4227. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  4228. // 43x blur:
  4229. const float weight_sum_inv = 1.0 /
  4230. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 +
  4231. w11 + w12 + w13 + w14 + w15 + w16 + w17 + w18 + w19 + w20 + w21));
  4232. #else
  4233. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  4234. // 31x blur:
  4235. const float weight_sum_inv = 1.0 /
  4236. (w0 + 2.0 * (w1 + w2 + w3 + w4 + w5 + w6 + w7 +
  4237. w8 + w9 + w10 + w11 + w12 + w13 + w14 + w15));
  4238. #else
  4239. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  4240. // 25x blur:
  4241. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  4242. w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8 + w9 + w10 + w11 + w12));
  4243. #else
  4244. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  4245. // 17x blur:
  4246. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (
  4247. w1 + w2 + w3 + w4 + w5 + w6 + w7 + w8));
  4248. #else
  4249. // 9x blur:
  4250. const float weight_sum_inv = 1.0 / (w0 + 2.0 * (w1 + w2 + w3 + w4));
  4251. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  4252. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  4253. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  4254. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  4255. const float center_weight = weight_sum_inv * weight_sum_inv;
  4256. return center_weight;
  4257. #endif
  4258. }
  4259. inline float3 tex2DblurNfast(const sampler2D texture, const float2 tex_uv,
  4260. const float2 dxdy, const float sigma)
  4261. {
  4262. // If sigma is static, we can safely branch and use the smallest blur
  4263. // that's big enough. Ignore #define hints, because we'll only use a
  4264. // large blur if we actually need it, and the branches cost nothing.
  4265. #ifndef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  4266. #define PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  4267. #else
  4268. // It's still worth branching if the profile supports dynamic branches:
  4269. // It's much faster than using a hugely excessive blur, but each branch
  4270. // eats ~1% FPS.
  4271. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  4272. #define PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  4273. #endif
  4274. #endif
  4275. // Failed optimization notes:
  4276. // I originally created a same-size mipmapped 5-tap separable blur10 that
  4277. // could handle any sigma by reaching into lower mip levels. It was
  4278. // as fast as blur25fast for runtime sigmas and a tad faster than
  4279. // blur31fast for static sigmas, but mipmapping two viewport-size passes
  4280. // ate 10% of FPS across all codepaths, so it wasn't worth it.
  4281. #ifdef PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  4282. if(sigma <= blur9_std_dev)
  4283. {
  4284. return tex2Dblur9fast(texture, tex_uv, dxdy, sigma);
  4285. }
  4286. else if(sigma <= blur17_std_dev)
  4287. {
  4288. return tex2Dblur17fast(texture, tex_uv, dxdy, sigma);
  4289. }
  4290. else if(sigma <= blur25_std_dev)
  4291. {
  4292. return tex2Dblur25fast(texture, tex_uv, dxdy, sigma);
  4293. }
  4294. else if(sigma <= blur31_std_dev)
  4295. {
  4296. return tex2Dblur31fast(texture, tex_uv, dxdy, sigma);
  4297. }
  4298. else
  4299. {
  4300. return tex2Dblur43fast(texture, tex_uv, dxdy, sigma);
  4301. }
  4302. #else
  4303. // If we can't afford to branch, we can only guess at what blur
  4304. // size we need. Therefore, use the largest blur allowed.
  4305. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  4306. return tex2Dblur43fast(texture, tex_uv, dxdy, sigma);
  4307. #else
  4308. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  4309. return tex2Dblur31fast(texture, tex_uv, dxdy, sigma);
  4310. #else
  4311. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  4312. return tex2Dblur25fast(texture, tex_uv, dxdy, sigma);
  4313. #else
  4314. #ifdef PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  4315. return tex2Dblur17fast(texture, tex_uv, dxdy, sigma);
  4316. #else
  4317. return tex2Dblur9fast(texture, tex_uv, dxdy, sigma);
  4318. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  4319. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  4320. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  4321. #endif // PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  4322. #endif // PHOSPHOR_BLOOM_BRANCH_FOR_BLUR_SIZE
  4323. }
  4324. inline float get_bloom_approx_sigma(const float output_size_x_runtime,
  4325. const float estimated_viewport_size_x)
  4326. {
  4327. // Requires: 1.) output_size_x_runtime == BLOOM_APPROX.output_size.x.
  4328. // This is included for dynamic codepaths just in case the
  4329. // following two globals are incorrect:
  4330. // 2.) bloom_approx_size_x_for_skip should == the same
  4331. // if PHOSPHOR_BLOOM_FAKE is #defined
  4332. // 3.) bloom_approx_size_x should == the same otherwise
  4333. // Returns: For gaussian4x4, return a dynamic small bloom sigma that's
  4334. // as close to optimal as possible given available information.
  4335. // For blur3x3, return the a static small bloom sigma that
  4336. // works well for typical cases. Otherwise, we're using simple
  4337. // bilinear filtering, so use static calculations.
  4338. // Assume the default static value. This is a compromise that ensures
  4339. // typical triads are blurred, even if unusually large ones aren't.
  4340. static const float mask_num_triads_static =
  4341. max(min_allowed_viewport_triads.x, mask_num_triads_desired_static);
  4342. const float mask_num_triads_from_size =
  4343. estimated_viewport_size_x/mask_triad_size_desired;
  4344. const float mask_num_triads_runtime = max(min_allowed_viewport_triads.x,
  4345. lerp(mask_num_triads_from_size, mask_num_triads_desired,
  4346. mask_specify_num_triads));
  4347. // Assume an extremely large viewport size for asymptotic results:
  4348. static const float max_viewport_size_x = 1080.0*1024.0*(4.0/3.0);
  4349. if(bloom_approx_filter > 1.5) // 4x4 true Gaussian resize
  4350. {
  4351. // Use the runtime num triads and output size:
  4352. const float asymptotic_triad_size =
  4353. max_viewport_size_x/mask_num_triads_runtime;
  4354. const float asymptotic_sigma = get_min_sigma_to_blur_triad(
  4355. asymptotic_triad_size, bloom_diff_thresh);
  4356. const float bloom_approx_sigma =
  4357. asymptotic_sigma * output_size_x_runtime/max_viewport_size_x;
  4358. // The BLOOM_APPROX input has to be ORIG_LINEARIZED to avoid moire, but
  4359. // account for the Gaussian scanline sigma from the last pass too.
  4360. // The bloom will be too wide horizontally but tall enough vertically.
  4361. return length(float2(bloom_approx_sigma, beam_max_sigma));
  4362. }
  4363. else // 3x3 blur resize (the bilinear resize doesn't need a sigma)
  4364. {
  4365. // We're either using blur3x3 or bilinear filtering. The biggest
  4366. // reason to choose blur3x3 is to avoid dynamic weights, so use a
  4367. // static calculation.
  4368. #ifdef PHOSPHOR_BLOOM_FAKE
  4369. static const float output_size_x_static =
  4370. bloom_approx_size_x_for_fake;
  4371. #else
  4372. static const float output_size_x_static = bloom_approx_size_x;
  4373. #endif
  4374. static const float asymptotic_triad_size =
  4375. max_viewport_size_x/mask_num_triads_static;
  4376. const float asymptotic_sigma = get_min_sigma_to_blur_triad(
  4377. asymptotic_triad_size, bloom_diff_thresh);
  4378. const float bloom_approx_sigma =
  4379. asymptotic_sigma * output_size_x_static/max_viewport_size_x;
  4380. // The BLOOM_APPROX input has to be ORIG_LINEARIZED to avoid moire, but
  4381. // try accounting for the Gaussian scanline sigma from the last pass
  4382. // too; use the static default value:
  4383. return length(float2(bloom_approx_sigma, beam_max_sigma_static));
  4384. }
  4385. }
  4386. inline float get_final_bloom_sigma(const float bloom_sigma_runtime)
  4387. {
  4388. // Requires: 1.) bloom_sigma_runtime is a precalculated sigma that's
  4389. // optimal for the [known] triad size.
  4390. // 2.) Call this from a fragment shader (not a vertex shader),
  4391. // or blurring with static sigmas won't be constant-folded.
  4392. // Returns: Return the optimistic static sigma if the triad size is
  4393. // known at compile time. Otherwise return the optimal runtime
  4394. // sigma (10% slower) or an implementation-specific compromise
  4395. // between an optimistic or pessimistic static sigma.
  4396. // Notes: Call this from the fragment shader, NOT the vertex shader,
  4397. // so static sigmas can be constant-folded!
  4398. const float bloom_sigma_optimistic = get_min_sigma_to_blur_triad(
  4399. mask_triad_size_desired_static, bloom_diff_thresh);
  4400. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  4401. return bloom_sigma_runtime;
  4402. #else
  4403. // Overblurring looks as bad as underblurring, so assume average-size
  4404. // triads, not worst-case huge triads:
  4405. return bloom_sigma_optimistic;
  4406. #endif
  4407. }
  4408. #endif // BLOOM_FUNCTIONS_H
  4409. //////////////////////////// END BLOOM-FUNCTIONS ///////////////////////////
  4410. /////////////////////////// END FRAGMENT-INCLUDES //////////////////////////
  4411. void main() {
  4412. // Blur the brightpass horizontally with a 9/17/25/43x blur:
  4413. const float bloom_sigma = get_final_bloom_sigma(bloom_sigma_runtime);
  4414. const float3 color = tex2DblurNfast(input_texture, tex_uv,
  4415. bloom_dxdy, bloom_sigma);
  4416. // Encode and output the blurred image:
  4417. FragColor = encode_output(float4(color, 1.0));
  4418. }