bloom-vertical.vs 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792
  1. #version 150
  2. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  3. // crt-royale: A full-featured CRT shader, with cheese.
  4. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  5. //
  6. // This program is free software; you can redistribute it and/or modify it
  7. // under the terms of the GNU General Public License as published by the Free
  8. // Software Foundation; either version 2 of the License, or any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful, but WITHOUT
  11. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. // more details.
  14. //
  15. // You should have received a copy of the GNU General Public License along with
  16. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  17. // Place, Suite 330, Boston, MA 02111-1307 USA
  18. in vec4 position;
  19. in vec2 texCoord;
  20. out Vertex {
  21. vec2 vTexCoord;
  22. vec2 tex_uv;
  23. vec2 bloom_dxdy;
  24. float bloom_sigma_runtime;
  25. };
  26. uniform vec4 targetSize;
  27. uniform vec4 sourceSize[];
  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. #if defined(GL_ES)
  111. #define COMPAT_PRECISION mediump
  112. #else
  113. #define COMPAT_PRECISION
  114. #endif
  115. #if __VERSION__ >= 130
  116. #define COMPAT_TEXTURE texture
  117. #else
  118. #define COMPAT_TEXTURE texture2D
  119. #endif
  120. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  121. //#include "../user-settings.h"
  122. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  123. #ifndef USER_SETTINGS_H
  124. #define USER_SETTINGS_H
  125. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  126. // The Cg compiler uses different "profiles" with different capabilities.
  127. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  128. // require higher profiles like fp30 or fp40. The shader can't detect profile
  129. // or driver capabilities, so instead you must comment or uncomment the lines
  130. // below with "//" before "#define." Disable an option if you get compilation
  131. // errors resembling those listed. Generally speaking, all of these options
  132. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  133. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  134. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  135. // Among other things, derivatives help us fix anisotropic filtering artifacts
  136. // with curved manually tiled phosphor mask coords. Related errors:
  137. // error C3004: function "float2 ddx(float2);" not supported in this profile
  138. // error C3004: function "float2 ddy(float2);" not supported in this profile
  139. //#define DRIVERS_ALLOW_DERIVATIVES
  140. // Fine derivatives: Unsupported on older ATI cards.
  141. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  142. // fast single-pass blur operations. If your card uses coarse derivatives and
  143. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  144. #ifdef DRIVERS_ALLOW_DERIVATIVES
  145. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  146. #endif
  147. // Dynamic looping: Requires an fp30 or newer profile.
  148. // This makes phosphor mask resampling faster in some cases. Related errors:
  149. // error C5013: profile does not support "for" statements and "for" could not
  150. // be unrolled
  151. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  152. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  153. // Using one static loop avoids overhead if the user is right, but if the user
  154. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  155. // binary search can potentially save some iterations. However, it may fail:
  156. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  157. // needed to compile program
  158. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  159. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  160. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  161. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  162. // this profile
  163. //#define DRIVERS_ALLOW_TEX2DLOD
  164. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  165. // artifacts from anisotropic filtering and mipmapping. Related errors:
  166. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  167. // in this profile
  168. //#define DRIVERS_ALLOW_TEX2DBIAS
  169. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  170. // impose stricter limitations on register counts and instructions. Enable
  171. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  172. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  173. // to compile program.
  174. // Enabling integrated graphics compatibility mode will automatically disable:
  175. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  176. // (This may be reenabled in a later release.)
  177. // 2.) RUNTIME_GEOMETRY_MODE
  178. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  179. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  180. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  181. // To disable a #define option, turn its line into a comment with "//."
  182. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  183. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  184. // many of the options in this file and allow real-time tuning, but many of
  185. // them are slower. Disabling them and using this text file will boost FPS.
  186. #define RUNTIME_SHADER_PARAMS_ENABLE
  187. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  188. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  189. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  190. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  191. #define RUNTIME_ANTIALIAS_WEIGHTS
  192. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  193. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  194. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  195. // parameters? This will require more math or dynamic branching.
  196. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  197. // Specify the tilt at runtime? This makes things about 3% slower.
  198. #define RUNTIME_GEOMETRY_TILT
  199. // Specify the geometry mode at runtime?
  200. #define RUNTIME_GEOMETRY_MODE
  201. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  202. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  203. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  204. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  205. // PHOSPHOR MASK:
  206. // Manually resize the phosphor mask for best results (slower)? Disabling this
  207. // removes the option to do so, but it may be faster without dynamic branches.
  208. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  209. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  210. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  211. // Larger blurs are expensive, but we need them to blur larger triads. We can
  212. // detect the right blur if the triad size is static or our profile allows
  213. // dynamic branches, but otherwise we use the largest blur the user indicates
  214. // they might need:
  215. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  216. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  217. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  218. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  219. // Here's a helpful chart:
  220. // MaxTriadSize BlurSize MinTriadCountsByResolution
  221. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  222. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  223. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  224. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  225. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  226. /////////////////////////////// USER PARAMETERS //////////////////////////////
  227. // Note: Many of these static parameters are overridden by runtime shader
  228. // parameters when those are enabled. However, many others are static codepath
  229. // options that were cleaner or more convert to code as static constants.
  230. // GAMMA:
  231. static const float crt_gamma_static = 2.5; // range [1, 5]
  232. static const float lcd_gamma_static = 2.2; // range [1, 5]
  233. // LEVELS MANAGEMENT:
  234. // Control the final multiplicative image contrast:
  235. static const float levels_contrast_static = 1.0; // range [0, 4)
  236. // We auto-dim to avoid clipping between passes and restore brightness
  237. // later. Control the dim factor here: Lower values clip less but crush
  238. // blacks more (static only for now).
  239. 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
  240. // HALATION/DIFFUSION/BLOOM:
  241. // Halation weight: How much energy should be lost to electrons bounding
  242. // around under the CRT glass and exciting random phosphors?
  243. static const float halation_weight_static = 0.0; // range [0, 1]
  244. // Refractive diffusion weight: How much light should spread/diffuse from
  245. // refracting through the CRT glass?
  246. static const float diffusion_weight_static = 0.075; // range [0, 1]
  247. // Underestimate brightness: Bright areas bloom more, but we can base the
  248. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  249. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  250. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  251. // Blur all colors more than necessary for a softer phosphor bloom?
  252. static const float bloom_excess_static = 0.0; // range [0, 1]
  253. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  254. // blurred resize of the input (convergence offsets are applied as well).
  255. // There are three filter options (static option only for now):
  256. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  257. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  258. // and beam_max_sigma is low.
  259. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  260. // always uses a static sigma regardless of beam_max_sigma or
  261. // mask_num_triads_desired.
  262. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  263. // These options are more pronounced for the fast, unbloomed shader version.
  264. #ifndef RADEON_FIX
  265. static const float bloom_approx_filter_static = 2.0;
  266. #else
  267. static const float bloom_approx_filter_static = 1.0;
  268. #endif
  269. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  270. // How many scanlines should contribute light to each pixel? Using more
  271. // scanlines is slower (especially for a generalized Gaussian) but less
  272. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  273. // max_beam_sigma at which the closest unused weight is guaranteed <
  274. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  275. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  276. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  277. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  278. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  279. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  280. static const float beam_num_scanlines = 3.0; // range [2, 6]
  281. // A generalized Gaussian beam varies shape with color too, now just width.
  282. // It's slower but more flexible (static option only for now).
  283. static const bool beam_generalized_gaussian = true;
  284. // What kind of scanline antialiasing do you want?
  285. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  286. // Integrals are slow (especially for generalized Gaussians) and rarely any
  287. // better than 3x antialiasing (static option only for now).
  288. static const float beam_antialias_level = 1.0; // range [0, 2]
  289. // Min/max standard deviations for scanline beams: Higher values widen and
  290. // soften scanlines. Depending on other options, low min sigmas can alias.
  291. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  292. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  293. // Beam width varies as a function of color: A power function (0) is more
  294. // configurable, but a spherical function (1) gives the widest beam
  295. // variability without aliasing (static option only for now).
  296. static const float beam_spot_shape_function = 0.0;
  297. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  298. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  299. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  300. // Generalized Gaussian max shape parameters: Higher values give flatter
  301. // scanline plateaus and steeper dropoffs, simultaneously widening and
  302. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  303. // values > ~40.0 cause artifacts with integrals.
  304. static const float beam_min_shape_static = 2.0; // range [2, 32]
  305. static const float beam_max_shape_static = 4.0; // range [2, 32]
  306. // Generalized Gaussian shape power: Affects how quickly the distribution
  307. // changes shape from Gaussian to steep/plateaued as color increases from 0
  308. // to 1.0. Higher powers appear softer for most colors, and lower powers
  309. // appear sharper for most colors.
  310. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  311. // What filter should be used to sample scanlines horizontally?
  312. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  313. static const float beam_horiz_filter_static = 0.0;
  314. // Standard deviation for horizontal Gaussian resampling:
  315. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  316. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  317. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  318. // limiting circuitry in some CRT's), or a weighted avg.?
  319. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  320. // Simulate scanline misconvergence? This needs 3x horizontal texture
  321. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  322. // later passes (static option only for now).
  323. static const bool beam_misconvergence = true;
  324. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  325. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  326. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  327. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  328. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  329. // Detect interlacing (static option only for now)?
  330. static const bool interlace_detect = true;
  331. // Assume 1080-line sources are interlaced?
  332. static const bool interlace_1080i_static = false;
  333. // For interlaced sources, assume TFF (top-field first) or BFF order?
  334. // (Whether this matters depends on the nature of the interlaced input.)
  335. static const bool interlace_bff_static = false;
  336. // ANTIALIASING:
  337. // What AA level do you want for curvature/overscan/subpixels? Options:
  338. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  339. // (Static option only for now)
  340. static const float aa_level = 12.0; // range [0, 24]
  341. // What antialiasing filter do you want (static option only)? Options:
  342. // 0: Box (separable), 1: Box (cylindrical),
  343. // 2: Tent (separable), 3: Tent (cylindrical),
  344. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  345. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  346. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  347. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  348. static const float aa_filter = 6.0; // range [0, 9]
  349. // Flip the sample grid on odd/even frames (static option only for now)?
  350. static const bool aa_temporal = false;
  351. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  352. // the blue offset is the negative r offset; range [0, 0.5]
  353. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  354. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  355. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  356. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  357. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  358. // 4.) C = 0.0 is a soft spline filter.
  359. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  360. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  361. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  362. // PHOSPHOR MASK:
  363. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  364. static const float mask_type_static = 1.0; // range [0, 2]
  365. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  366. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  367. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  368. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  369. // is halfway decent with LUT mipmapping but atrocious without it.
  370. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  371. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  372. // This mode reuses the same masks, so triads will be enormous unless
  373. // you change the mask LUT filenames in your .cgp file.
  374. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  375. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  376. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  377. // will always be used to calculate the full bloom sigma statically.
  378. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  379. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  380. // triads) will be rounded to the nearest integer tile size and clamped to
  381. // obey minimum size constraints (imposed to reduce downsize taps) and
  382. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  383. // To increase the size limit, double the viewport-relative scales for the
  384. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  385. // range [1, mask_texture_small_size/mask_triads_per_tile]
  386. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  387. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  388. // final size will be rounded and constrained as above); default 480.0
  389. static const float mask_num_triads_desired_static = 480.0;
  390. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  391. // more samples and avoid moire a bit better, but some is unavoidable
  392. // depending on the destination size (static option for now).
  393. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  394. // The mask is resized using a variable number of taps in each dimension,
  395. // but some Cg profiles always fetch a constant number of taps no matter
  396. // what (no dynamic branching). We can limit the maximum number of taps if
  397. // we statically limit the minimum phosphor triad size. Larger values are
  398. // faster, but the limit IS enforced (static option only, forever);
  399. // range [1, mask_texture_small_size/mask_triads_per_tile]
  400. // TODO: Make this 1.0 and compensate with smarter sampling!
  401. static const float mask_min_allowed_triad_size = 2.0;
  402. // GEOMETRY:
  403. // Geometry mode:
  404. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  405. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  406. static const float geom_mode_static = 0.0; // range [0, 3]
  407. // Radius of curvature: Measured in units of your viewport's diagonal size.
  408. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  409. // View dist is the distance from the player to their physical screen, in
  410. // units of the viewport's diagonal size. It controls the field of view.
  411. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  412. // Tilt angle in radians (clockwise around up and right vectors):
  413. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  414. // Aspect ratio: When the true viewport size is unknown, this value is used
  415. // to help convert between the phosphor triad size and count, along with
  416. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  417. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  418. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  419. // default (256/224)*(54/47) = 1.313069909 (see below)
  420. static const float geom_aspect_ratio_static = 1.313069909;
  421. // Before getting into overscan, here's some general aspect ratio info:
  422. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  423. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  424. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  425. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  426. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  427. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  428. // a.) Enable Retroarch's "Crop Overscan"
  429. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  430. // Real consoles use horizontal black padding in the signal, but emulators
  431. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  432. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  433. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  434. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  435. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  436. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  437. // without doing a. or b., but horizontal image borders will be tighter
  438. // than vertical ones, messing up curvature and overscan. Fixing the
  439. // padding first corrects this.
  440. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  441. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  442. // above: Values < 1.0 zoom out; range (0, inf)
  443. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  444. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  445. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  446. // with strong curvature (static option only for now).
  447. static const bool geom_force_correct_tangent_matrix = true;
  448. // BORDERS:
  449. // Rounded border size in texture uv coords:
  450. static const float border_size_static = 0.015; // range [0, 0.5]
  451. // Border darkness: Moderate values darken the border smoothly, and high
  452. // values make the image very dark just inside the border:
  453. static const float border_darkness_static = 2.0; // range [0, inf)
  454. // Border compression: High numbers compress border transitions, narrowing
  455. // the dark border area.
  456. static const float border_compress_static = 2.5; // range [1, inf)
  457. #endif // USER_SETTINGS_H
  458. //////////////////////////// END USER-SETTINGS //////////////////////////
  459. //#include "derived-settings-and-constants.h"
  460. //////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  461. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  462. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  463. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  464. // crt-royale: A full-featured CRT shader, with cheese.
  465. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  466. //
  467. // This program is free software; you can redistribute it and/or modify it
  468. // under the terms of the GNU General Public License as published by the Free
  469. // Software Foundation; either version 2 of the License, or any later version.
  470. //
  471. // This program is distributed in the hope that it will be useful, but WITHOUT
  472. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  473. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  474. // more details.
  475. //
  476. // You should have received a copy of the GNU General Public License along with
  477. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  478. // Place, Suite 330, Boston, MA 02111-1307 USA
  479. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  480. // These macros and constants can be used across the whole codebase.
  481. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  482. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  483. //#include "../user-settings.h"
  484. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  485. #ifndef USER_SETTINGS_H
  486. #define USER_SETTINGS_H
  487. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  488. // The Cg compiler uses different "profiles" with different capabilities.
  489. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  490. // require higher profiles like fp30 or fp40. The shader can't detect profile
  491. // or driver capabilities, so instead you must comment or uncomment the lines
  492. // below with "//" before "#define." Disable an option if you get compilation
  493. // errors resembling those listed. Generally speaking, all of these options
  494. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  495. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  496. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  497. // Among other things, derivatives help us fix anisotropic filtering artifacts
  498. // with curved manually tiled phosphor mask coords. Related errors:
  499. // error C3004: function "float2 ddx(float2);" not supported in this profile
  500. // error C3004: function "float2 ddy(float2);" not supported in this profile
  501. //#define DRIVERS_ALLOW_DERIVATIVES
  502. // Fine derivatives: Unsupported on older ATI cards.
  503. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  504. // fast single-pass blur operations. If your card uses coarse derivatives and
  505. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  506. #ifdef DRIVERS_ALLOW_DERIVATIVES
  507. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  508. #endif
  509. // Dynamic looping: Requires an fp30 or newer profile.
  510. // This makes phosphor mask resampling faster in some cases. Related errors:
  511. // error C5013: profile does not support "for" statements and "for" could not
  512. // be unrolled
  513. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  514. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  515. // Using one static loop avoids overhead if the user is right, but if the user
  516. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  517. // binary search can potentially save some iterations. However, it may fail:
  518. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  519. // needed to compile program
  520. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  521. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  522. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  523. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  524. // this profile
  525. //#define DRIVERS_ALLOW_TEX2DLOD
  526. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  527. // artifacts from anisotropic filtering and mipmapping. Related errors:
  528. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  529. // in this profile
  530. //#define DRIVERS_ALLOW_TEX2DBIAS
  531. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  532. // impose stricter limitations on register counts and instructions. Enable
  533. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  534. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  535. // to compile program.
  536. // Enabling integrated graphics compatibility mode will automatically disable:
  537. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  538. // (This may be reenabled in a later release.)
  539. // 2.) RUNTIME_GEOMETRY_MODE
  540. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  541. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  542. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  543. // To disable a #define option, turn its line into a comment with "//."
  544. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  545. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  546. // many of the options in this file and allow real-time tuning, but many of
  547. // them are slower. Disabling them and using this text file will boost FPS.
  548. #define RUNTIME_SHADER_PARAMS_ENABLE
  549. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  550. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  551. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  552. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  553. #define RUNTIME_ANTIALIAS_WEIGHTS
  554. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  555. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  556. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  557. // parameters? This will require more math or dynamic branching.
  558. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  559. // Specify the tilt at runtime? This makes things about 3% slower.
  560. #define RUNTIME_GEOMETRY_TILT
  561. // Specify the geometry mode at runtime?
  562. #define RUNTIME_GEOMETRY_MODE
  563. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  564. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  565. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  566. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  567. // PHOSPHOR MASK:
  568. // Manually resize the phosphor mask for best results (slower)? Disabling this
  569. // removes the option to do so, but it may be faster without dynamic branches.
  570. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  571. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  572. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  573. // Larger blurs are expensive, but we need them to blur larger triads. We can
  574. // detect the right blur if the triad size is static or our profile allows
  575. // dynamic branches, but otherwise we use the largest blur the user indicates
  576. // they might need:
  577. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  578. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  579. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  580. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  581. // Here's a helpful chart:
  582. // MaxTriadSize BlurSize MinTriadCountsByResolution
  583. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  584. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  585. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  586. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  587. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  588. /////////////////////////////// USER PARAMETERS //////////////////////////////
  589. // Note: Many of these static parameters are overridden by runtime shader
  590. // parameters when those are enabled. However, many others are static codepath
  591. // options that were cleaner or more convert to code as static constants.
  592. // GAMMA:
  593. static const float crt_gamma_static = 2.5; // range [1, 5]
  594. static const float lcd_gamma_static = 2.2; // range [1, 5]
  595. // LEVELS MANAGEMENT:
  596. // Control the final multiplicative image contrast:
  597. static const float levels_contrast_static = 1.0; // range [0, 4)
  598. // We auto-dim to avoid clipping between passes and restore brightness
  599. // later. Control the dim factor here: Lower values clip less but crush
  600. // blacks more (static only for now).
  601. 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
  602. // HALATION/DIFFUSION/BLOOM:
  603. // Halation weight: How much energy should be lost to electrons bounding
  604. // around under the CRT glass and exciting random phosphors?
  605. static const float halation_weight_static = 0.0; // range [0, 1]
  606. // Refractive diffusion weight: How much light should spread/diffuse from
  607. // refracting through the CRT glass?
  608. static const float diffusion_weight_static = 0.075; // range [0, 1]
  609. // Underestimate brightness: Bright areas bloom more, but we can base the
  610. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  611. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  612. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  613. // Blur all colors more than necessary for a softer phosphor bloom?
  614. static const float bloom_excess_static = 0.0; // range [0, 1]
  615. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  616. // blurred resize of the input (convergence offsets are applied as well).
  617. // There are three filter options (static option only for now):
  618. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  619. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  620. // and beam_max_sigma is low.
  621. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  622. // always uses a static sigma regardless of beam_max_sigma or
  623. // mask_num_triads_desired.
  624. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  625. // These options are more pronounced for the fast, unbloomed shader version.
  626. #ifndef RADEON_FIX
  627. static const float bloom_approx_filter_static = 2.0;
  628. #else
  629. static const float bloom_approx_filter_static = 1.0;
  630. #endif
  631. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  632. // How many scanlines should contribute light to each pixel? Using more
  633. // scanlines is slower (especially for a generalized Gaussian) but less
  634. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  635. // max_beam_sigma at which the closest unused weight is guaranteed <
  636. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  637. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  638. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  639. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  640. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  641. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  642. static const float beam_num_scanlines = 3.0; // range [2, 6]
  643. // A generalized Gaussian beam varies shape with color too, now just width.
  644. // It's slower but more flexible (static option only for now).
  645. static const bool beam_generalized_gaussian = true;
  646. // What kind of scanline antialiasing do you want?
  647. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  648. // Integrals are slow (especially for generalized Gaussians) and rarely any
  649. // better than 3x antialiasing (static option only for now).
  650. static const float beam_antialias_level = 1.0; // range [0, 2]
  651. // Min/max standard deviations for scanline beams: Higher values widen and
  652. // soften scanlines. Depending on other options, low min sigmas can alias.
  653. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  654. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  655. // Beam width varies as a function of color: A power function (0) is more
  656. // configurable, but a spherical function (1) gives the widest beam
  657. // variability without aliasing (static option only for now).
  658. static const float beam_spot_shape_function = 0.0;
  659. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  660. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  661. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  662. // Generalized Gaussian max shape parameters: Higher values give flatter
  663. // scanline plateaus and steeper dropoffs, simultaneously widening and
  664. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  665. // values > ~40.0 cause artifacts with integrals.
  666. static const float beam_min_shape_static = 2.0; // range [2, 32]
  667. static const float beam_max_shape_static = 4.0; // range [2, 32]
  668. // Generalized Gaussian shape power: Affects how quickly the distribution
  669. // changes shape from Gaussian to steep/plateaued as color increases from 0
  670. // to 1.0. Higher powers appear softer for most colors, and lower powers
  671. // appear sharper for most colors.
  672. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  673. // What filter should be used to sample scanlines horizontally?
  674. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  675. static const float beam_horiz_filter_static = 0.0;
  676. // Standard deviation for horizontal Gaussian resampling:
  677. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  678. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  679. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  680. // limiting circuitry in some CRT's), or a weighted avg.?
  681. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  682. // Simulate scanline misconvergence? This needs 3x horizontal texture
  683. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  684. // later passes (static option only for now).
  685. static const bool beam_misconvergence = true;
  686. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  687. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  688. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  689. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  690. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  691. // Detect interlacing (static option only for now)?
  692. static const bool interlace_detect = true;
  693. // Assume 1080-line sources are interlaced?
  694. static const bool interlace_1080i_static = false;
  695. // For interlaced sources, assume TFF (top-field first) or BFF order?
  696. // (Whether this matters depends on the nature of the interlaced input.)
  697. static const bool interlace_bff_static = false;
  698. // ANTIALIASING:
  699. // What AA level do you want for curvature/overscan/subpixels? Options:
  700. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  701. // (Static option only for now)
  702. static const float aa_level = 12.0; // range [0, 24]
  703. // What antialiasing filter do you want (static option only)? Options:
  704. // 0: Box (separable), 1: Box (cylindrical),
  705. // 2: Tent (separable), 3: Tent (cylindrical),
  706. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  707. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  708. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  709. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  710. static const float aa_filter = 6.0; // range [0, 9]
  711. // Flip the sample grid on odd/even frames (static option only for now)?
  712. static const bool aa_temporal = false;
  713. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  714. // the blue offset is the negative r offset; range [0, 0.5]
  715. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  716. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  717. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  718. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  719. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  720. // 4.) C = 0.0 is a soft spline filter.
  721. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  722. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  723. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  724. // PHOSPHOR MASK:
  725. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  726. static const float mask_type_static = 1.0; // range [0, 2]
  727. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  728. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  729. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  730. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  731. // is halfway decent with LUT mipmapping but atrocious without it.
  732. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  733. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  734. // This mode reuses the same masks, so triads will be enormous unless
  735. // you change the mask LUT filenames in your .cgp file.
  736. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  737. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  738. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  739. // will always be used to calculate the full bloom sigma statically.
  740. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  741. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  742. // triads) will be rounded to the nearest integer tile size and clamped to
  743. // obey minimum size constraints (imposed to reduce downsize taps) and
  744. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  745. // To increase the size limit, double the viewport-relative scales for the
  746. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  747. // range [1, mask_texture_small_size/mask_triads_per_tile]
  748. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  749. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  750. // final size will be rounded and constrained as above); default 480.0
  751. static const float mask_num_triads_desired_static = 480.0;
  752. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  753. // more samples and avoid moire a bit better, but some is unavoidable
  754. // depending on the destination size (static option for now).
  755. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  756. // The mask is resized using a variable number of taps in each dimension,
  757. // but some Cg profiles always fetch a constant number of taps no matter
  758. // what (no dynamic branching). We can limit the maximum number of taps if
  759. // we statically limit the minimum phosphor triad size. Larger values are
  760. // faster, but the limit IS enforced (static option only, forever);
  761. // range [1, mask_texture_small_size/mask_triads_per_tile]
  762. // TODO: Make this 1.0 and compensate with smarter sampling!
  763. static const float mask_min_allowed_triad_size = 2.0;
  764. // GEOMETRY:
  765. // Geometry mode:
  766. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  767. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  768. static const float geom_mode_static = 0.0; // range [0, 3]
  769. // Radius of curvature: Measured in units of your viewport's diagonal size.
  770. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  771. // View dist is the distance from the player to their physical screen, in
  772. // units of the viewport's diagonal size. It controls the field of view.
  773. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  774. // Tilt angle in radians (clockwise around up and right vectors):
  775. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  776. // Aspect ratio: When the true viewport size is unknown, this value is used
  777. // to help convert between the phosphor triad size and count, along with
  778. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  779. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  780. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  781. // default (256/224)*(54/47) = 1.313069909 (see below)
  782. static const float geom_aspect_ratio_static = 1.313069909;
  783. // Before getting into overscan, here's some general aspect ratio info:
  784. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  785. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  786. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  787. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  788. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  789. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  790. // a.) Enable Retroarch's "Crop Overscan"
  791. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  792. // Real consoles use horizontal black padding in the signal, but emulators
  793. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  794. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  795. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  796. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  797. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  798. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  799. // without doing a. or b., but horizontal image borders will be tighter
  800. // than vertical ones, messing up curvature and overscan. Fixing the
  801. // padding first corrects this.
  802. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  803. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  804. // above: Values < 1.0 zoom out; range (0, inf)
  805. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  806. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  807. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  808. // with strong curvature (static option only for now).
  809. static const bool geom_force_correct_tangent_matrix = true;
  810. // BORDERS:
  811. // Rounded border size in texture uv coords:
  812. static const float border_size_static = 0.015; // range [0, 0.5]
  813. // Border darkness: Moderate values darken the border smoothly, and high
  814. // values make the image very dark just inside the border:
  815. static const float border_darkness_static = 2.0; // range [0, inf)
  816. // Border compression: High numbers compress border transitions, narrowing
  817. // the dark border area.
  818. static const float border_compress_static = 2.5; // range [1, inf)
  819. #endif // USER_SETTINGS_H
  820. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  821. //#include "user-cgp-constants.h"
  822. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  823. #ifndef USER_CGP_CONSTANTS_H
  824. #define USER_CGP_CONSTANTS_H
  825. // IMPORTANT:
  826. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  827. // (or whatever related .cgp file you're using). If they aren't, you're likely
  828. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  829. // set directly in the .cgp file to make things easier, but...they can't.
  830. // PASS SCALES AND RELATED CONSTANTS:
  831. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  832. // this shader: One does a viewport-scale bloom, and the other skips it. The
  833. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  834. static const float bloom_approx_size_x = 320.0;
  835. static const float bloom_approx_size_x_for_fake = 400.0;
  836. // Copy the viewport-relative scales of the phosphor mask resize passes
  837. // (MASK_RESIZE and the pass immediately preceding it):
  838. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  839. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  840. static const float geom_max_aspect_ratio = 4.0/3.0;
  841. // PHOSPHOR MASK TEXTURE CONSTANTS:
  842. // Set the following constants to reflect the properties of the phosphor mask
  843. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  844. // based on user settings, then repeats a single tile until filling the screen.
  845. // The shader must know the input texture size (default 64x64), and to manually
  846. // resize, it must also know the horizontal triads per tile (default 8).
  847. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  848. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  849. static const float mask_triads_per_tile = 8.0;
  850. // We need the average brightness of the phosphor mask to compensate for the
  851. // dimming it causes. The following four values are roughly correct for the
  852. // masks included with the shader. Update the value for any LUT texture you
  853. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  854. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  855. //#define PHOSPHOR_MASK_GRILLE14
  856. static const float mask_grille14_avg_color = 50.6666666/255.0;
  857. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  858. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  859. static const float mask_grille15_avg_color = 53.0/255.0;
  860. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  861. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  862. static const float mask_slot_avg_color = 46.0/255.0;
  863. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  864. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  865. static const float mask_shadow_avg_color = 41.0/255.0;
  866. // TileableLinearShadowMask*.png
  867. // TileableLinearShadowMaskEDP*.png
  868. #ifdef PHOSPHOR_MASK_GRILLE14
  869. static const float mask_grille_avg_color = mask_grille14_avg_color;
  870. #else
  871. static const float mask_grille_avg_color = mask_grille15_avg_color;
  872. #endif
  873. #endif // USER_CGP_CONSTANTS_H
  874. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  875. //////////////////////////////// END INCLUDES ////////////////////////////////
  876. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  877. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  878. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  879. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  880. #ifndef SIMULATE_CRT_ON_LCD
  881. #define SIMULATE_CRT_ON_LCD
  882. #endif
  883. // Manually tiling a manually resized texture creates texture coord derivative
  884. // discontinuities and confuses anisotropic filtering, causing discolored tile
  885. // seams in the phosphor mask. Workarounds:
  886. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  887. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  888. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  889. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  890. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  891. // (Same-pass curvature isn't used but could be in the future...maybe.)
  892. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  893. // border padding to the resized mask FBO, but it works with same-pass
  894. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  895. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  896. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  897. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  898. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  899. // Also, manually resampling the phosphor mask is slightly blurrier with
  900. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  901. // creates artifacts, but only with the fully bloomed shader.) The difference
  902. // is subtle with small triads, but you can fix it for a small cost.
  903. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  904. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  905. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  906. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  907. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  908. // #defined by either user-settings.h or a wrapper .cg that #includes the
  909. // current .cg pass.)
  910. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  911. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  912. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  913. #endif
  914. #ifdef RUNTIME_GEOMETRY_MODE
  915. #undef RUNTIME_GEOMETRY_MODE
  916. #endif
  917. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  918. // inferior in most cases, so replace 2.0 with 0.0:
  919. static const float bloom_approx_filter =
  920. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  921. #else
  922. static const float bloom_approx_filter = bloom_approx_filter_static;
  923. #endif
  924. // Disable slow runtime paths if static parameters are used. Most of these
  925. // won't be a problem anyway once the params are disabled, but some will.
  926. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  927. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  928. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  929. #endif
  930. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  931. #undef RUNTIME_ANTIALIAS_WEIGHTS
  932. #endif
  933. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  934. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  935. #endif
  936. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  937. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  938. #endif
  939. #ifdef RUNTIME_GEOMETRY_TILT
  940. #undef RUNTIME_GEOMETRY_TILT
  941. #endif
  942. #ifdef RUNTIME_GEOMETRY_MODE
  943. #undef RUNTIME_GEOMETRY_MODE
  944. #endif
  945. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  946. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  947. #endif
  948. #endif
  949. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  950. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  951. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  952. #endif
  953. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  954. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  955. #endif
  956. // Rule out unavailable anisotropic compatibility strategies:
  957. #ifndef DRIVERS_ALLOW_DERIVATIVES
  958. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  959. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  960. #endif
  961. #endif
  962. #ifndef DRIVERS_ALLOW_TEX2DLOD
  963. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  964. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  965. #endif
  966. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  967. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  968. #endif
  969. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  970. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  971. #endif
  972. #endif
  973. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  974. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  975. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  976. #endif
  977. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  978. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  979. #endif
  980. #endif
  981. // Prioritize anisotropic tiling compatibility strategies by performance and
  982. // disable unused strategies. This concentrates all the nesting in one place.
  983. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  984. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  985. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  986. #endif
  987. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  988. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  989. #endif
  990. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  991. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  992. #endif
  993. #else
  994. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  995. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  996. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  997. #endif
  998. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  999. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1000. #endif
  1001. #else
  1002. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  1003. // flat texture coords in the same pass, but that's all we use.
  1004. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1005. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1006. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1007. #endif
  1008. #endif
  1009. #endif
  1010. #endif
  1011. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  1012. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  1013. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1014. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1015. #endif
  1016. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1017. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1018. #endif
  1019. // Prioritize anisotropic resampling compatibility strategies the same way:
  1020. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1021. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1022. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1023. #endif
  1024. #endif
  1025. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  1026. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  1027. // should: It gives us more options for using fewer samples.
  1028. #ifdef DRIVERS_ALLOW_TEX2DLOD
  1029. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1030. // TODO: Take advantage of this!
  1031. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  1032. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  1033. #else
  1034. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1035. #endif
  1036. #else
  1037. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  1038. #endif
  1039. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  1040. // main_fragment, or a static alias of one of the above. This makes it hard
  1041. // to select the phosphor mask at runtime: We can't even assign to a uniform
  1042. // global in the vertex shader or select a sampler2D in the vertex shader and
  1043. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  1044. // because it just gives us the input texture or a black screen. However, we
  1045. // can get around these limitations by calling tex2D three times with different
  1046. // uniform samplers (or resizing the phosphor mask three times altogether).
  1047. // With dynamic branches, we can process only one of these branches on top of
  1048. // quickly discarding fragments we don't need (cgc seems able to overcome
  1049. // limigations around dependent texture fetches inside of branches). Without
  1050. // dynamic branches, we have to process every branch for every fragment...which
  1051. // is slower. Runtime sampling mode selection is slower without dynamic
  1052. // branches as well. Let the user's static #defines decide if it's worth it.
  1053. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1054. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1055. #else
  1056. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1057. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1058. #endif
  1059. #endif
  1060. // We need to render some minimum number of tiles in the resize passes.
  1061. // We need at least 1.0 just to repeat a single tile, and we need extra
  1062. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  1063. // antialiasing, same-pass curvature (not currently used), etc. First
  1064. // determine how many border texels and tiles we need, based on how the result
  1065. // will be sampled:
  1066. #ifdef GEOMETRY_EARLY
  1067. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  1068. // Most antialiasing filters have a base radius of 4.0 pixels:
  1069. static const float max_aa_base_pixel_border = 4.0 +
  1070. max_subpixel_offset;
  1071. #else
  1072. static const float max_aa_base_pixel_border = 0.0;
  1073. #endif
  1074. // Anisotropic filtering adds about 0.5 to the pixel border:
  1075. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  1076. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  1077. #else
  1078. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  1079. #endif
  1080. // Fixing discontinuities adds 1.0 more to the pixel border:
  1081. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1082. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  1083. #else
  1084. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  1085. #endif
  1086. // Convert the pixel border to an integer texel border. Assume same-pass
  1087. // curvature about triples the texel frequency:
  1088. #ifdef GEOMETRY_EARLY
  1089. static const float max_mask_texel_border =
  1090. ceil(max_tiled_pixel_border * 3.0);
  1091. #else
  1092. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  1093. #endif
  1094. // Convert the texel border to a tile border using worst-case assumptions:
  1095. static const float max_mask_tile_border = max_mask_texel_border/
  1096. (mask_min_allowed_triad_size * mask_triads_per_tile);
  1097. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  1098. // the starting texel (inside borders) for sampling it.
  1099. #ifndef GEOMETRY_EARLY
  1100. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  1101. // Special case: Render two tiles without borders. Anisotropic
  1102. // filtering doesn't seem to be a problem here.
  1103. static const float mask_resize_num_tiles = 1.0 + 1.0;
  1104. static const float mask_start_texels = 0.0;
  1105. #else
  1106. static const float mask_resize_num_tiles = 1.0 +
  1107. 2.0 * max_mask_tile_border;
  1108. static const float mask_start_texels = max_mask_texel_border;
  1109. #endif
  1110. #else
  1111. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  1112. static const float mask_start_texels = max_mask_texel_border;
  1113. #endif
  1114. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  1115. // mask_resize_viewport_scale. This limits the maximum final triad size.
  1116. // Estimate the minimum number of triads we can split the screen into in each
  1117. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  1118. static const float mask_resize_num_triads =
  1119. mask_resize_num_tiles * mask_triads_per_tile;
  1120. static const float2 min_allowed_viewport_triads =
  1121. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  1122. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  1123. static const float pi = 3.141592653589;
  1124. // We often want to find the location of the previous texel, e.g.:
  1125. // const float2 curr_texel = uv * texture_size;
  1126. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  1127. // const float2 prev_texel_uv = prev_texel / texture_size;
  1128. // However, many GPU drivers round incorrectly around exact texel locations.
  1129. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  1130. // require this value to be farther from 0.5 than others; define it here.
  1131. // const float2 prev_texel =
  1132. // floor(curr_texel - float2(under_half)) + float2(0.5);
  1133. static const float under_half = 0.4995;
  1134. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  1135. ///////////////////////////// END DERIVED-SETTINGS-AND-CONSTANTS ////////////////////////////
  1136. //#include "bind-shader-params.h"
  1137. ///////////////////////////// BEGIN BIND-SHADER-PARAMS ////////////////////////////
  1138. #ifndef BIND_SHADER_PARAMS_H
  1139. #define BIND_SHADER_PARAMS_H
  1140. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1141. // crt-royale: A full-featured CRT shader, with cheese.
  1142. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1143. //
  1144. // This program is free software; you can redistribute it and/or modify it
  1145. // under the terms of the GNU General Public License as published by the Free
  1146. // Software Foundation; either version 2 of the License, or any later version.
  1147. //
  1148. // This program is distributed in the hope that it will be useful, but WITHOUT
  1149. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1150. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1151. // more details.
  1152. //
  1153. // You should have received a copy of the GNU General Public License along with
  1154. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1155. // Place, Suite 330, Boston, MA 02111-1307 USA
  1156. ///////////////////////////// SETTINGS MANAGEMENT ////////////////////////////
  1157. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1158. //#include "../user-settings.h"
  1159. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1160. #ifndef USER_SETTINGS_H
  1161. #define USER_SETTINGS_H
  1162. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1163. // The Cg compiler uses different "profiles" with different capabilities.
  1164. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1165. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1166. // or driver capabilities, so instead you must comment or uncomment the lines
  1167. // below with "//" before "#define." Disable an option if you get compilation
  1168. // errors resembling those listed. Generally speaking, all of these options
  1169. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1170. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1171. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1172. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1173. // with curved manually tiled phosphor mask coords. Related errors:
  1174. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1175. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1176. //#define DRIVERS_ALLOW_DERIVATIVES
  1177. // Fine derivatives: Unsupported on older ATI cards.
  1178. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1179. // fast single-pass blur operations. If your card uses coarse derivatives and
  1180. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1181. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1182. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1183. #endif
  1184. // Dynamic looping: Requires an fp30 or newer profile.
  1185. // This makes phosphor mask resampling faster in some cases. Related errors:
  1186. // error C5013: profile does not support "for" statements and "for" could not
  1187. // be unrolled
  1188. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1189. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1190. // Using one static loop avoids overhead if the user is right, but if the user
  1191. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1192. // binary search can potentially save some iterations. However, it may fail:
  1193. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1194. // needed to compile program
  1195. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1196. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1197. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1198. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1199. // this profile
  1200. //#define DRIVERS_ALLOW_TEX2DLOD
  1201. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1202. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1203. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1204. // in this profile
  1205. //#define DRIVERS_ALLOW_TEX2DBIAS
  1206. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1207. // impose stricter limitations on register counts and instructions. Enable
  1208. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1209. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1210. // to compile program.
  1211. // Enabling integrated graphics compatibility mode will automatically disable:
  1212. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1213. // (This may be reenabled in a later release.)
  1214. // 2.) RUNTIME_GEOMETRY_MODE
  1215. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1216. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1217. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1218. // To disable a #define option, turn its line into a comment with "//."
  1219. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1220. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1221. // many of the options in this file and allow real-time tuning, but many of
  1222. // them are slower. Disabling them and using this text file will boost FPS.
  1223. #define RUNTIME_SHADER_PARAMS_ENABLE
  1224. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1225. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1226. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1227. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1228. #define RUNTIME_ANTIALIAS_WEIGHTS
  1229. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1230. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1231. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1232. // parameters? This will require more math or dynamic branching.
  1233. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1234. // Specify the tilt at runtime? This makes things about 3% slower.
  1235. #define RUNTIME_GEOMETRY_TILT
  1236. // Specify the geometry mode at runtime?
  1237. #define RUNTIME_GEOMETRY_MODE
  1238. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1239. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1240. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1241. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1242. // PHOSPHOR MASK:
  1243. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1244. // removes the option to do so, but it may be faster without dynamic branches.
  1245. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1246. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1247. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1248. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1249. // detect the right blur if the triad size is static or our profile allows
  1250. // dynamic branches, but otherwise we use the largest blur the user indicates
  1251. // they might need:
  1252. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1253. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1254. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1255. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1256. // Here's a helpful chart:
  1257. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1258. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1259. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1260. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1261. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1262. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1263. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1264. // Note: Many of these static parameters are overridden by runtime shader
  1265. // parameters when those are enabled. However, many others are static codepath
  1266. // options that were cleaner or more convert to code as static constants.
  1267. // GAMMA:
  1268. static const float crt_gamma_static = 2.5; // range [1, 5]
  1269. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1270. // LEVELS MANAGEMENT:
  1271. // Control the final multiplicative image contrast:
  1272. static const float levels_contrast_static = 1.0; // range [0, 4)
  1273. // We auto-dim to avoid clipping between passes and restore brightness
  1274. // later. Control the dim factor here: Lower values clip less but crush
  1275. // blacks more (static only for now).
  1276. 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
  1277. // HALATION/DIFFUSION/BLOOM:
  1278. // Halation weight: How much energy should be lost to electrons bounding
  1279. // around under the CRT glass and exciting random phosphors?
  1280. static const float halation_weight_static = 0.0; // range [0, 1]
  1281. // Refractive diffusion weight: How much light should spread/diffuse from
  1282. // refracting through the CRT glass?
  1283. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1284. // Underestimate brightness: Bright areas bloom more, but we can base the
  1285. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1286. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1287. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1288. // Blur all colors more than necessary for a softer phosphor bloom?
  1289. static const float bloom_excess_static = 0.0; // range [0, 1]
  1290. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1291. // blurred resize of the input (convergence offsets are applied as well).
  1292. // There are three filter options (static option only for now):
  1293. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1294. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1295. // and beam_max_sigma is low.
  1296. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1297. // always uses a static sigma regardless of beam_max_sigma or
  1298. // mask_num_triads_desired.
  1299. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1300. // These options are more pronounced for the fast, unbloomed shader version.
  1301. #ifndef RADEON_FIX
  1302. static const float bloom_approx_filter_static = 2.0;
  1303. #else
  1304. static const float bloom_approx_filter_static = 1.0;
  1305. #endif
  1306. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1307. // How many scanlines should contribute light to each pixel? Using more
  1308. // scanlines is slower (especially for a generalized Gaussian) but less
  1309. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1310. // max_beam_sigma at which the closest unused weight is guaranteed <
  1311. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1312. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1313. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1314. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1315. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1316. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1317. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1318. // A generalized Gaussian beam varies shape with color too, now just width.
  1319. // It's slower but more flexible (static option only for now).
  1320. static const bool beam_generalized_gaussian = true;
  1321. // What kind of scanline antialiasing do you want?
  1322. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1323. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1324. // better than 3x antialiasing (static option only for now).
  1325. static const float beam_antialias_level = 1.0; // range [0, 2]
  1326. // Min/max standard deviations for scanline beams: Higher values widen and
  1327. // soften scanlines. Depending on other options, low min sigmas can alias.
  1328. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1329. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1330. // Beam width varies as a function of color: A power function (0) is more
  1331. // configurable, but a spherical function (1) gives the widest beam
  1332. // variability without aliasing (static option only for now).
  1333. static const float beam_spot_shape_function = 0.0;
  1334. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1335. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1336. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1337. // Generalized Gaussian max shape parameters: Higher values give flatter
  1338. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1339. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1340. // values > ~40.0 cause artifacts with integrals.
  1341. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1342. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1343. // Generalized Gaussian shape power: Affects how quickly the distribution
  1344. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1345. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1346. // appear sharper for most colors.
  1347. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1348. // What filter should be used to sample scanlines horizontally?
  1349. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1350. static const float beam_horiz_filter_static = 0.0;
  1351. // Standard deviation for horizontal Gaussian resampling:
  1352. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1353. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1354. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1355. // limiting circuitry in some CRT's), or a weighted avg.?
  1356. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1357. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1358. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1359. // later passes (static option only for now).
  1360. static const bool beam_misconvergence = true;
  1361. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1362. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1363. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1364. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1365. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1366. // Detect interlacing (static option only for now)?
  1367. static const bool interlace_detect = true;
  1368. // Assume 1080-line sources are interlaced?
  1369. static const bool interlace_1080i_static = false;
  1370. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1371. // (Whether this matters depends on the nature of the interlaced input.)
  1372. static const bool interlace_bff_static = false;
  1373. // ANTIALIASING:
  1374. // What AA level do you want for curvature/overscan/subpixels? Options:
  1375. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1376. // (Static option only for now)
  1377. static const float aa_level = 12.0; // range [0, 24]
  1378. // What antialiasing filter do you want (static option only)? Options:
  1379. // 0: Box (separable), 1: Box (cylindrical),
  1380. // 2: Tent (separable), 3: Tent (cylindrical),
  1381. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1382. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1383. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1384. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1385. static const float aa_filter = 6.0; // range [0, 9]
  1386. // Flip the sample grid on odd/even frames (static option only for now)?
  1387. static const bool aa_temporal = false;
  1388. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1389. // the blue offset is the negative r offset; range [0, 0.5]
  1390. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1391. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1392. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1393. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1394. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1395. // 4.) C = 0.0 is a soft spline filter.
  1396. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1397. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1398. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1399. // PHOSPHOR MASK:
  1400. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1401. static const float mask_type_static = 1.0; // range [0, 2]
  1402. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1403. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1404. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1405. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1406. // is halfway decent with LUT mipmapping but atrocious without it.
  1407. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1408. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1409. // This mode reuses the same masks, so triads will be enormous unless
  1410. // you change the mask LUT filenames in your .cgp file.
  1411. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1412. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1413. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1414. // will always be used to calculate the full bloom sigma statically.
  1415. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1416. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1417. // triads) will be rounded to the nearest integer tile size and clamped to
  1418. // obey minimum size constraints (imposed to reduce downsize taps) and
  1419. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1420. // To increase the size limit, double the viewport-relative scales for the
  1421. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1422. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1423. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1424. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1425. // final size will be rounded and constrained as above); default 480.0
  1426. static const float mask_num_triads_desired_static = 480.0;
  1427. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1428. // more samples and avoid moire a bit better, but some is unavoidable
  1429. // depending on the destination size (static option for now).
  1430. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1431. // The mask is resized using a variable number of taps in each dimension,
  1432. // but some Cg profiles always fetch a constant number of taps no matter
  1433. // what (no dynamic branching). We can limit the maximum number of taps if
  1434. // we statically limit the minimum phosphor triad size. Larger values are
  1435. // faster, but the limit IS enforced (static option only, forever);
  1436. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1437. // TODO: Make this 1.0 and compensate with smarter sampling!
  1438. static const float mask_min_allowed_triad_size = 2.0;
  1439. // GEOMETRY:
  1440. // Geometry mode:
  1441. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1442. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1443. static const float geom_mode_static = 0.0; // range [0, 3]
  1444. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1445. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1446. // View dist is the distance from the player to their physical screen, in
  1447. // units of the viewport's diagonal size. It controls the field of view.
  1448. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1449. // Tilt angle in radians (clockwise around up and right vectors):
  1450. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1451. // Aspect ratio: When the true viewport size is unknown, this value is used
  1452. // to help convert between the phosphor triad size and count, along with
  1453. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1454. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1455. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1456. // default (256/224)*(54/47) = 1.313069909 (see below)
  1457. static const float geom_aspect_ratio_static = 1.313069909;
  1458. // Before getting into overscan, here's some general aspect ratio info:
  1459. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1460. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1461. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1462. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1463. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1464. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1465. // a.) Enable Retroarch's "Crop Overscan"
  1466. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1467. // Real consoles use horizontal black padding in the signal, but emulators
  1468. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1469. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1470. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1471. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1472. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1473. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1474. // without doing a. or b., but horizontal image borders will be tighter
  1475. // than vertical ones, messing up curvature and overscan. Fixing the
  1476. // padding first corrects this.
  1477. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1478. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1479. // above: Values < 1.0 zoom out; range (0, inf)
  1480. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1481. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1482. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1483. // with strong curvature (static option only for now).
  1484. static const bool geom_force_correct_tangent_matrix = true;
  1485. // BORDERS:
  1486. // Rounded border size in texture uv coords:
  1487. static const float border_size_static = 0.015; // range [0, 0.5]
  1488. // Border darkness: Moderate values darken the border smoothly, and high
  1489. // values make the image very dark just inside the border:
  1490. static const float border_darkness_static = 2.0; // range [0, inf)
  1491. // Border compression: High numbers compress border transitions, narrowing
  1492. // the dark border area.
  1493. static const float border_compress_static = 2.5; // range [1, inf)
  1494. #endif // USER_SETTINGS_H
  1495. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1496. //#include "derived-settings-and-constants.h"
  1497. ///////////////////// BEGIN DERIVED-SETTINGS-AND-CONSTANTS ////////////////////
  1498. #ifndef DERIVED_SETTINGS_AND_CONSTANTS_H
  1499. #define DERIVED_SETTINGS_AND_CONSTANTS_H
  1500. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  1501. // crt-royale: A full-featured CRT shader, with cheese.
  1502. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  1503. //
  1504. // This program is free software; you can redistribute it and/or modify it
  1505. // under the terms of the GNU General Public License as published by the Free
  1506. // Software Foundation; either version 2 of the License, or any later version.
  1507. //
  1508. // This program is distributed in the hope that it will be useful, but WITHOUT
  1509. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  1510. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  1511. // more details.
  1512. //
  1513. // You should have received a copy of the GNU General Public License along with
  1514. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  1515. // Place, Suite 330, Boston, MA 02111-1307 USA
  1516. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  1517. // These macros and constants can be used across the whole codebase.
  1518. // Unlike the values in user-settings.cgh, end users shouldn't modify these.
  1519. /////////////////////////////// BEGIN INCLUDES ///////////////////////////////
  1520. //#include "../user-settings.h"
  1521. ///////////////////////////// BEGIN USER-SETTINGS ////////////////////////////
  1522. #ifndef USER_SETTINGS_H
  1523. #define USER_SETTINGS_H
  1524. ///////////////////////////// DRIVER CAPABILITIES ////////////////////////////
  1525. // The Cg compiler uses different "profiles" with different capabilities.
  1526. // This shader requires a Cg compilation profile >= arbfp1, but a few options
  1527. // require higher profiles like fp30 or fp40. The shader can't detect profile
  1528. // or driver capabilities, so instead you must comment or uncomment the lines
  1529. // below with "//" before "#define." Disable an option if you get compilation
  1530. // errors resembling those listed. Generally speaking, all of these options
  1531. // will run on nVidia cards, but only DRIVERS_ALLOW_TEX2DBIAS (if that) is
  1532. // likely to run on ATI/AMD, due to the Cg compiler's profile limitations.
  1533. // Derivatives: Unsupported on fp20, ps_1_1, ps_1_2, ps_1_3, and arbfp1.
  1534. // Among other things, derivatives help us fix anisotropic filtering artifacts
  1535. // with curved manually tiled phosphor mask coords. Related errors:
  1536. // error C3004: function "float2 ddx(float2);" not supported in this profile
  1537. // error C3004: function "float2 ddy(float2);" not supported in this profile
  1538. //#define DRIVERS_ALLOW_DERIVATIVES
  1539. // Fine derivatives: Unsupported on older ATI cards.
  1540. // Fine derivatives enable 2x2 fragment block communication, letting us perform
  1541. // fast single-pass blur operations. If your card uses coarse derivatives and
  1542. // these are enabled, blurs could look broken. Derivatives are a prerequisite.
  1543. #ifdef DRIVERS_ALLOW_DERIVATIVES
  1544. #define DRIVERS_ALLOW_FINE_DERIVATIVES
  1545. #endif
  1546. // Dynamic looping: Requires an fp30 or newer profile.
  1547. // This makes phosphor mask resampling faster in some cases. Related errors:
  1548. // error C5013: profile does not support "for" statements and "for" could not
  1549. // be unrolled
  1550. //#define DRIVERS_ALLOW_DYNAMIC_BRANCHES
  1551. // Without DRIVERS_ALLOW_DYNAMIC_BRANCHES, we need to use unrollable loops.
  1552. // Using one static loop avoids overhead if the user is right, but if the user
  1553. // is wrong (loops are allowed), breaking a loop into if-blocked pieces with a
  1554. // binary search can potentially save some iterations. However, it may fail:
  1555. // error C6001: Temporary register limit of 32 exceeded; 35 registers
  1556. // needed to compile program
  1557. //#define ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  1558. // tex2Dlod: Requires an fp40 or newer profile. This can be used to disable
  1559. // anisotropic filtering, thereby fixing related artifacts. Related errors:
  1560. // error C3004: function "float4 tex2Dlod(sampler2D, float4);" not supported in
  1561. // this profile
  1562. //#define DRIVERS_ALLOW_TEX2DLOD
  1563. // tex2Dbias: Requires an fp30 or newer profile. This can be used to alleviate
  1564. // artifacts from anisotropic filtering and mipmapping. Related errors:
  1565. // error C3004: function "float4 tex2Dbias(sampler2D, float4);" not supported
  1566. // in this profile
  1567. //#define DRIVERS_ALLOW_TEX2DBIAS
  1568. // Integrated graphics compatibility: Integrated graphics like Intel HD 4000
  1569. // impose stricter limitations on register counts and instructions. Enable
  1570. // INTEGRATED_GRAPHICS_COMPATIBILITY_MODE if you still see error C6001 or:
  1571. // error C6002: Instruction limit of 1024 exceeded: 1523 instructions needed
  1572. // to compile program.
  1573. // Enabling integrated graphics compatibility mode will automatically disable:
  1574. // 1.) PHOSPHOR_MASK_MANUALLY_RESIZE: The phosphor mask will be softer.
  1575. // (This may be reenabled in a later release.)
  1576. // 2.) RUNTIME_GEOMETRY_MODE
  1577. // 3.) The high-quality 4x4 Gaussian resize for the bloom approximation
  1578. //#define INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1579. //////////////////////////// USER CODEPATH OPTIONS ///////////////////////////
  1580. // To disable a #define option, turn its line into a comment with "//."
  1581. // RUNTIME VS. COMPILE-TIME OPTIONS (Major Performance Implications):
  1582. // Enable runtime shader parameters in the Retroarch (etc.) GUI? They override
  1583. // many of the options in this file and allow real-time tuning, but many of
  1584. // them are slower. Disabling them and using this text file will boost FPS.
  1585. #define RUNTIME_SHADER_PARAMS_ENABLE
  1586. // Specify the phosphor bloom sigma at runtime? This option is 10% slower, but
  1587. // it's the only way to do a wide-enough full bloom with a runtime dot pitch.
  1588. #define RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1589. // Specify antialiasing weight parameters at runtime? (Costs ~20% with cubics)
  1590. #define RUNTIME_ANTIALIAS_WEIGHTS
  1591. // Specify subpixel offsets at runtime? (WARNING: EXTREMELY EXPENSIVE!)
  1592. //#define RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1593. // Make beam_horiz_filter and beam_horiz_linear_rgb_weight into runtime shader
  1594. // parameters? This will require more math or dynamic branching.
  1595. #define RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1596. // Specify the tilt at runtime? This makes things about 3% slower.
  1597. #define RUNTIME_GEOMETRY_TILT
  1598. // Specify the geometry mode at runtime?
  1599. #define RUNTIME_GEOMETRY_MODE
  1600. // Specify the phosphor mask type (aperture grille, slot mask, shadow mask) and
  1601. // mode (Lanczos-resize, hardware resize, or tile 1:1) at runtime, even without
  1602. // dynamic branches? This is cheap if mask_resize_viewport_scale is small.
  1603. #define FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1604. // PHOSPHOR MASK:
  1605. // Manually resize the phosphor mask for best results (slower)? Disabling this
  1606. // removes the option to do so, but it may be faster without dynamic branches.
  1607. #define PHOSPHOR_MASK_MANUALLY_RESIZE
  1608. // If we sinc-resize the mask, should we Lanczos-window it (slower but better)?
  1609. #define PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  1610. // Larger blurs are expensive, but we need them to blur larger triads. We can
  1611. // detect the right blur if the triad size is static or our profile allows
  1612. // dynamic branches, but otherwise we use the largest blur the user indicates
  1613. // they might need:
  1614. #define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_3_PIXELS
  1615. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_6_PIXELS
  1616. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_9_PIXELS
  1617. //#define PHOSPHOR_BLOOM_TRIADS_LARGER_THAN_12_PIXELS
  1618. // Here's a helpful chart:
  1619. // MaxTriadSize BlurSize MinTriadCountsByResolution
  1620. // 3.0 9.0 480/640/960/1920 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1621. // 6.0 17.0 240/320/480/960 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1622. // 9.0 25.0 160/213/320/640 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1623. // 12.0 31.0 120/160/240/480 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1624. // 18.0 43.0 80/107/160/320 triads at 1080p/1440p/2160p/4320p, 4:3 aspect
  1625. /////////////////////////////// USER PARAMETERS //////////////////////////////
  1626. // Note: Many of these static parameters are overridden by runtime shader
  1627. // parameters when those are enabled. However, many others are static codepath
  1628. // options that were cleaner or more convert to code as static constants.
  1629. // GAMMA:
  1630. static const float crt_gamma_static = 2.5; // range [1, 5]
  1631. static const float lcd_gamma_static = 2.2; // range [1, 5]
  1632. // LEVELS MANAGEMENT:
  1633. // Control the final multiplicative image contrast:
  1634. static const float levels_contrast_static = 1.0; // range [0, 4)
  1635. // We auto-dim to avoid clipping between passes and restore brightness
  1636. // later. Control the dim factor here: Lower values clip less but crush
  1637. // blacks more (static only for now).
  1638. 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
  1639. // HALATION/DIFFUSION/BLOOM:
  1640. // Halation weight: How much energy should be lost to electrons bounding
  1641. // around under the CRT glass and exciting random phosphors?
  1642. static const float halation_weight_static = 0.0; // range [0, 1]
  1643. // Refractive diffusion weight: How much light should spread/diffuse from
  1644. // refracting through the CRT glass?
  1645. static const float diffusion_weight_static = 0.075; // range [0, 1]
  1646. // Underestimate brightness: Bright areas bloom more, but we can base the
  1647. // bloom brightpass on a lower brightness to sharpen phosphors, or a higher
  1648. // brightness to soften them. Low values clip, but >= 0.8 looks okay.
  1649. static const float bloom_underestimate_levels_static = 0.8; // range [0, 5]
  1650. // Blur all colors more than necessary for a softer phosphor bloom?
  1651. static const float bloom_excess_static = 0.0; // range [0, 1]
  1652. // The BLOOM_APPROX pass approximates a phosphor blur early on with a small
  1653. // blurred resize of the input (convergence offsets are applied as well).
  1654. // There are three filter options (static option only for now):
  1655. // 0.) Bilinear resize: A fast, close approximation to a 4x4 resize
  1656. // if min_allowed_viewport_triads and the BLOOM_APPROX resolution are sane
  1657. // and beam_max_sigma is low.
  1658. // 1.) 3x3 resize blur: Medium speed, soft/smeared from bilinear blurring,
  1659. // always uses a static sigma regardless of beam_max_sigma or
  1660. // mask_num_triads_desired.
  1661. // 2.) True 4x4 Gaussian resize: Slowest, technically correct.
  1662. // These options are more pronounced for the fast, unbloomed shader version.
  1663. #ifndef RADEON_FIX
  1664. static const float bloom_approx_filter_static = 2.0;
  1665. #else
  1666. static const float bloom_approx_filter_static = 1.0;
  1667. #endif
  1668. // ELECTRON BEAM SCANLINE DISTRIBUTION:
  1669. // How many scanlines should contribute light to each pixel? Using more
  1670. // scanlines is slower (especially for a generalized Gaussian) but less
  1671. // distorted with larger beam sigmas (especially for a pure Gaussian). The
  1672. // max_beam_sigma at which the closest unused weight is guaranteed <
  1673. // 1.0/255.0 (for a 3x antialiased pure Gaussian) is:
  1674. // 2 scanlines: max_beam_sigma = 0.2089; distortions begin ~0.34; 141.7 FPS pure, 131.9 FPS generalized
  1675. // 3 scanlines, max_beam_sigma = 0.3879; distortions begin ~0.52; 137.5 FPS pure; 123.8 FPS generalized
  1676. // 4 scanlines, max_beam_sigma = 0.5723; distortions begin ~0.70; 134.7 FPS pure; 117.2 FPS generalized
  1677. // 5 scanlines, max_beam_sigma = 0.7591; distortions begin ~0.89; 131.6 FPS pure; 112.1 FPS generalized
  1678. // 6 scanlines, max_beam_sigma = 0.9483; distortions begin ~1.08; 127.9 FPS pure; 105.6 FPS generalized
  1679. static const float beam_num_scanlines = 3.0; // range [2, 6]
  1680. // A generalized Gaussian beam varies shape with color too, now just width.
  1681. // It's slower but more flexible (static option only for now).
  1682. static const bool beam_generalized_gaussian = true;
  1683. // What kind of scanline antialiasing do you want?
  1684. // 0: Sample weights at 1x; 1: Sample weights at 3x; 2: Compute an integral
  1685. // Integrals are slow (especially for generalized Gaussians) and rarely any
  1686. // better than 3x antialiasing (static option only for now).
  1687. static const float beam_antialias_level = 1.0; // range [0, 2]
  1688. // Min/max standard deviations for scanline beams: Higher values widen and
  1689. // soften scanlines. Depending on other options, low min sigmas can alias.
  1690. static const float beam_min_sigma_static = 0.02; // range (0, 1]
  1691. static const float beam_max_sigma_static = 0.3; // range (0, 1]
  1692. // Beam width varies as a function of color: A power function (0) is more
  1693. // configurable, but a spherical function (1) gives the widest beam
  1694. // variability without aliasing (static option only for now).
  1695. static const float beam_spot_shape_function = 0.0;
  1696. // Spot shape power: Powers <= 1 give smoother spot shapes but lower
  1697. // sharpness. Powers >= 1.0 are awful unless mix/max sigmas are close.
  1698. static const float beam_spot_power_static = 1.0/3.0; // range (0, 16]
  1699. // Generalized Gaussian max shape parameters: Higher values give flatter
  1700. // scanline plateaus and steeper dropoffs, simultaneously widening and
  1701. // sharpening scanlines at the cost of aliasing. 2.0 is pure Gaussian, and
  1702. // values > ~40.0 cause artifacts with integrals.
  1703. static const float beam_min_shape_static = 2.0; // range [2, 32]
  1704. static const float beam_max_shape_static = 4.0; // range [2, 32]
  1705. // Generalized Gaussian shape power: Affects how quickly the distribution
  1706. // changes shape from Gaussian to steep/plateaued as color increases from 0
  1707. // to 1.0. Higher powers appear softer for most colors, and lower powers
  1708. // appear sharper for most colors.
  1709. static const float beam_shape_power_static = 1.0/4.0; // range (0, 16]
  1710. // What filter should be used to sample scanlines horizontally?
  1711. // 0: Quilez (fast), 1: Gaussian (configurable), 2: Lanczos2 (sharp)
  1712. static const float beam_horiz_filter_static = 0.0;
  1713. // Standard deviation for horizontal Gaussian resampling:
  1714. static const float beam_horiz_sigma_static = 0.35; // range (0, 2/3]
  1715. // Do horizontal scanline sampling in linear RGB (correct light mixing),
  1716. // gamma-encoded RGB (darker, hard spot shape, may better match bandwidth-
  1717. // limiting circuitry in some CRT's), or a weighted avg.?
  1718. static const float beam_horiz_linear_rgb_weight_static = 1.0; // range [0, 1]
  1719. // Simulate scanline misconvergence? This needs 3x horizontal texture
  1720. // samples and 3x texture samples of BLOOM_APPROX and HALATION_BLUR in
  1721. // later passes (static option only for now).
  1722. static const bool beam_misconvergence = true;
  1723. // Convergence offsets in x/y directions for R/G/B scanline beams in units
  1724. // of scanlines. Positive offsets go right/down; ranges [-2, 2]
  1725. static const float2 convergence_offsets_r_static = float2(0.1, 0.2);
  1726. static const float2 convergence_offsets_g_static = float2(0.3, 0.4);
  1727. static const float2 convergence_offsets_b_static = float2(0.5, 0.6);
  1728. // Detect interlacing (static option only for now)?
  1729. static const bool interlace_detect = true;
  1730. // Assume 1080-line sources are interlaced?
  1731. static const bool interlace_1080i_static = false;
  1732. // For interlaced sources, assume TFF (top-field first) or BFF order?
  1733. // (Whether this matters depends on the nature of the interlaced input.)
  1734. static const bool interlace_bff_static = false;
  1735. // ANTIALIASING:
  1736. // What AA level do you want for curvature/overscan/subpixels? Options:
  1737. // 0x (none), 1x (sample subpixels), 4x, 5x, 6x, 7x, 8x, 12x, 16x, 20x, 24x
  1738. // (Static option only for now)
  1739. static const float aa_level = 12.0; // range [0, 24]
  1740. // What antialiasing filter do you want (static option only)? Options:
  1741. // 0: Box (separable), 1: Box (cylindrical),
  1742. // 2: Tent (separable), 3: Tent (cylindrical),
  1743. // 4: Gaussian (separable), 5: Gaussian (cylindrical),
  1744. // 6: Cubic* (separable), 7: Cubic* (cylindrical, poor)
  1745. // 8: Lanczos Sinc (separable), 9: Lanczos Jinc (cylindrical, poor)
  1746. // * = Especially slow with RUNTIME_ANTIALIAS_WEIGHTS
  1747. static const float aa_filter = 6.0; // range [0, 9]
  1748. // Flip the sample grid on odd/even frames (static option only for now)?
  1749. static const bool aa_temporal = false;
  1750. // Use RGB subpixel offsets for antialiasing? The pixel is at green, and
  1751. // the blue offset is the negative r offset; range [0, 0.5]
  1752. static const float2 aa_subpixel_r_offset_static = float2(-1.0/3.0, 0.0);//float2(0.0);
  1753. // Cubics: See http://www.imagemagick.org/Usage/filter/#mitchell
  1754. // 1.) "Keys cubics" with B = 1 - 2C are considered the highest quality.
  1755. // 2.) C = 0.5 (default) is Catmull-Rom; higher C's apply sharpening.
  1756. // 3.) C = 1.0/3.0 is the Mitchell-Netravali filter.
  1757. // 4.) C = 0.0 is a soft spline filter.
  1758. static const float aa_cubic_c_static = 0.5; // range [0, 4]
  1759. // Standard deviation for Gaussian antialiasing: Try 0.5/aa_pixel_diameter.
  1760. static const float aa_gauss_sigma_static = 0.5; // range [0.0625, 1.0]
  1761. // PHOSPHOR MASK:
  1762. // Mask type: 0 = aperture grille, 1 = slot mask, 2 = EDP shadow mask
  1763. static const float mask_type_static = 1.0; // range [0, 2]
  1764. // We can sample the mask three ways. Pick 2/3 from: Pretty/Fast/Flexible.
  1765. // 0.) Sinc-resize to the desired dot pitch manually (pretty/slow/flexible).
  1766. // This requires PHOSPHOR_MASK_MANUALLY_RESIZE to be #defined.
  1767. // 1.) Hardware-resize to the desired dot pitch (ugly/fast/flexible). This
  1768. // is halfway decent with LUT mipmapping but atrocious without it.
  1769. // 2.) Tile it without resizing at a 1:1 texel:pixel ratio for flat coords
  1770. // (pretty/fast/inflexible). Each input LUT has a fixed dot pitch.
  1771. // This mode reuses the same masks, so triads will be enormous unless
  1772. // you change the mask LUT filenames in your .cgp file.
  1773. static const float mask_sample_mode_static = 0.0; // range [0, 2]
  1774. // Prefer setting the triad size (0.0) or number on the screen (1.0)?
  1775. // If RUNTIME_PHOSPHOR_BLOOM_SIGMA isn't #defined, the specified triad size
  1776. // will always be used to calculate the full bloom sigma statically.
  1777. static const float mask_specify_num_triads_static = 0.0; // range [0, 1]
  1778. // Specify the phosphor triad size, in pixels. Each tile (usually with 8
  1779. // triads) will be rounded to the nearest integer tile size and clamped to
  1780. // obey minimum size constraints (imposed to reduce downsize taps) and
  1781. // maximum size constraints (imposed to have a sane MASK_RESIZE FBO size).
  1782. // To increase the size limit, double the viewport-relative scales for the
  1783. // two MASK_RESIZE passes in crt-royale.cgp and user-cgp-contants.h.
  1784. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1785. static const float mask_triad_size_desired_static = 24.0 / 8.0;
  1786. // If mask_specify_num_triads is 1.0/true, we'll go by this instead (the
  1787. // final size will be rounded and constrained as above); default 480.0
  1788. static const float mask_num_triads_desired_static = 480.0;
  1789. // How many lobes should the sinc/Lanczos resizer use? More lobes require
  1790. // more samples and avoid moire a bit better, but some is unavoidable
  1791. // depending on the destination size (static option for now).
  1792. static const float mask_sinc_lobes = 3.0; // range [2, 4]
  1793. // The mask is resized using a variable number of taps in each dimension,
  1794. // but some Cg profiles always fetch a constant number of taps no matter
  1795. // what (no dynamic branching). We can limit the maximum number of taps if
  1796. // we statically limit the minimum phosphor triad size. Larger values are
  1797. // faster, but the limit IS enforced (static option only, forever);
  1798. // range [1, mask_texture_small_size/mask_triads_per_tile]
  1799. // TODO: Make this 1.0 and compensate with smarter sampling!
  1800. static const float mask_min_allowed_triad_size = 2.0;
  1801. // GEOMETRY:
  1802. // Geometry mode:
  1803. // 0: Off (default), 1: Spherical mapping (like cgwg's),
  1804. // 2: Alt. spherical mapping (more bulbous), 3: Cylindrical/Trinitron
  1805. static const float geom_mode_static = 0.0; // range [0, 3]
  1806. // Radius of curvature: Measured in units of your viewport's diagonal size.
  1807. static const float geom_radius_static = 2.0; // range [1/(2*pi), 1024]
  1808. // View dist is the distance from the player to their physical screen, in
  1809. // units of the viewport's diagonal size. It controls the field of view.
  1810. static const float geom_view_dist_static = 2.0; // range [0.5, 1024]
  1811. // Tilt angle in radians (clockwise around up and right vectors):
  1812. static const float2 geom_tilt_angle_static = float2(0.0, 0.0); // range [-pi, pi]
  1813. // Aspect ratio: When the true viewport size is unknown, this value is used
  1814. // to help convert between the phosphor triad size and count, along with
  1815. // the mask_resize_viewport_scale constant from user-cgp-constants.h. Set
  1816. // this equal to Retroarch's display aspect ratio (DAR) for best results;
  1817. // range [1, geom_max_aspect_ratio from user-cgp-constants.h];
  1818. // default (256/224)*(54/47) = 1.313069909 (see below)
  1819. static const float geom_aspect_ratio_static = 1.313069909;
  1820. // Before getting into overscan, here's some general aspect ratio info:
  1821. // - DAR = display aspect ratio = SAR * PAR; as in your Retroarch setting
  1822. // - SAR = storage aspect ratio = DAR / PAR; square pixel emulator frame AR
  1823. // - PAR = pixel aspect ratio = DAR / SAR; holds regardless of cropping
  1824. // Geometry processing has to "undo" the screen-space 2D DAR to calculate
  1825. // 3D view vectors, then reapplies the aspect ratio to the simulated CRT in
  1826. // uv-space. To ensure the source SAR is intended for a ~4:3 DAR, either:
  1827. // a.) Enable Retroarch's "Crop Overscan"
  1828. // b.) Readd horizontal padding: Set overscan to e.g. N*(1.0, 240.0/224.0)
  1829. // Real consoles use horizontal black padding in the signal, but emulators
  1830. // often crop this without cropping the vertical padding; a 256x224 [S]NES
  1831. // frame (8:7 SAR) is intended for a ~4:3 DAR, but a 256x240 frame is not.
  1832. // The correct [S]NES PAR is 54:47, found by blargg and NewRisingSun:
  1833. // http://board.zsnes.com/phpBB3/viewtopic.php?f=22&t=11928&start=50
  1834. // http://forums.nesdev.com/viewtopic.php?p=24815#p24815
  1835. // For flat output, it's okay to set DAR = [existing] SAR * [correct] PAR
  1836. // without doing a. or b., but horizontal image borders will be tighter
  1837. // than vertical ones, messing up curvature and overscan. Fixing the
  1838. // padding first corrects this.
  1839. // Overscan: Amount to "zoom in" before cropping. You can zoom uniformly
  1840. // or adjust x/y independently to e.g. readd horizontal padding, as noted
  1841. // above: Values < 1.0 zoom out; range (0, inf)
  1842. static const float2 geom_overscan_static = float2(1.0, 1.0);// * 1.005 * (1.0, 240/224.0)
  1843. // Compute a proper pixel-space to texture-space matrix even without ddx()/
  1844. // ddy()? This is ~8.5% slower but improves antialiasing/subpixel filtering
  1845. // with strong curvature (static option only for now).
  1846. static const bool geom_force_correct_tangent_matrix = true;
  1847. // BORDERS:
  1848. // Rounded border size in texture uv coords:
  1849. static const float border_size_static = 0.015; // range [0, 0.5]
  1850. // Border darkness: Moderate values darken the border smoothly, and high
  1851. // values make the image very dark just inside the border:
  1852. static const float border_darkness_static = 2.0; // range [0, inf)
  1853. // Border compression: High numbers compress border transitions, narrowing
  1854. // the dark border area.
  1855. static const float border_compress_static = 2.5; // range [1, inf)
  1856. #endif // USER_SETTINGS_H
  1857. ///////////////////////////// END USER-SETTINGS ////////////////////////////
  1858. //#include "user-cgp-constants.h"
  1859. ///////////////////////// BEGIN USER-CGP-CONSTANTS /////////////////////////
  1860. #ifndef USER_CGP_CONSTANTS_H
  1861. #define USER_CGP_CONSTANTS_H
  1862. // IMPORTANT:
  1863. // These constants MUST be set appropriately for the settings in crt-royale.cgp
  1864. // (or whatever related .cgp file you're using). If they aren't, you're likely
  1865. // to get artifacts, the wrong phosphor mask size, etc. I wish these could be
  1866. // set directly in the .cgp file to make things easier, but...they can't.
  1867. // PASS SCALES AND RELATED CONSTANTS:
  1868. // Copy the absolute scale_x for BLOOM_APPROX. There are two major versions of
  1869. // this shader: One does a viewport-scale bloom, and the other skips it. The
  1870. // latter benefits from a higher bloom_approx_scale_x, so save both separately:
  1871. static const float bloom_approx_size_x = 320.0;
  1872. static const float bloom_approx_size_x_for_fake = 400.0;
  1873. // Copy the viewport-relative scales of the phosphor mask resize passes
  1874. // (MASK_RESIZE and the pass immediately preceding it):
  1875. static const float2 mask_resize_viewport_scale = float2(0.0625, 0.0625);
  1876. // Copy the geom_max_aspect_ratio used to calculate the MASK_RESIZE scales, etc.:
  1877. static const float geom_max_aspect_ratio = 4.0/3.0;
  1878. // PHOSPHOR MASK TEXTURE CONSTANTS:
  1879. // Set the following constants to reflect the properties of the phosphor mask
  1880. // texture named in crt-royale.cgp. The shader optionally resizes a mask tile
  1881. // based on user settings, then repeats a single tile until filling the screen.
  1882. // The shader must know the input texture size (default 64x64), and to manually
  1883. // resize, it must also know the horizontal triads per tile (default 8).
  1884. static const float2 mask_texture_small_size = float2(64.0, 64.0);
  1885. static const float2 mask_texture_large_size = float2(512.0, 512.0);
  1886. static const float mask_triads_per_tile = 8.0;
  1887. // We need the average brightness of the phosphor mask to compensate for the
  1888. // dimming it causes. The following four values are roughly correct for the
  1889. // masks included with the shader. Update the value for any LUT texture you
  1890. // change. [Un]comment "#define PHOSPHOR_MASK_GRILLE14" depending on whether
  1891. // the loaded aperture grille uses 14-pixel or 15-pixel stripes (default 15).
  1892. //#define PHOSPHOR_MASK_GRILLE14
  1893. static const float mask_grille14_avg_color = 50.6666666/255.0;
  1894. // TileableLinearApertureGrille14Wide7d33Spacing*.png
  1895. // TileableLinearApertureGrille14Wide10And6Spacing*.png
  1896. static const float mask_grille15_avg_color = 53.0/255.0;
  1897. // TileableLinearApertureGrille15Wide6d33Spacing*.png
  1898. // TileableLinearApertureGrille15Wide8And5d5Spacing*.png
  1899. static const float mask_slot_avg_color = 46.0/255.0;
  1900. // TileableLinearSlotMask15Wide9And4d5Horizontal8VerticalSpacing*.png
  1901. // TileableLinearSlotMaskTall15Wide9And4d5Horizontal9d14VerticalSpacing*.png
  1902. static const float mask_shadow_avg_color = 41.0/255.0;
  1903. // TileableLinearShadowMask*.png
  1904. // TileableLinearShadowMaskEDP*.png
  1905. #ifdef PHOSPHOR_MASK_GRILLE14
  1906. static const float mask_grille_avg_color = mask_grille14_avg_color;
  1907. #else
  1908. static const float mask_grille_avg_color = mask_grille15_avg_color;
  1909. #endif
  1910. #endif // USER_CGP_CONSTANTS_H
  1911. ////////////////////////// END USER-CGP-CONSTANTS //////////////////////////
  1912. //////////////////////////////// END INCLUDES ////////////////////////////////
  1913. /////////////////////////////// FIXED SETTINGS ///////////////////////////////
  1914. // Avoid dividing by zero; using a macro overloads for float, float2, etc.:
  1915. #define FIX_ZERO(c) (max(abs(c), 0.0000152587890625)) // 2^-16
  1916. // Ensure the first pass decodes CRT gamma and the last encodes LCD gamma.
  1917. #ifndef SIMULATE_CRT_ON_LCD
  1918. #define SIMULATE_CRT_ON_LCD
  1919. #endif
  1920. // Manually tiling a manually resized texture creates texture coord derivative
  1921. // discontinuities and confuses anisotropic filtering, causing discolored tile
  1922. // seams in the phosphor mask. Workarounds:
  1923. // a.) Using tex2Dlod disables anisotropic filtering for tiled masks. It's
  1924. // downgraded to tex2Dbias without DRIVERS_ALLOW_TEX2DLOD #defined and
  1925. // disabled without DRIVERS_ALLOW_TEX2DBIAS #defined either.
  1926. // b.) "Tile flat twice" requires drawing two full tiles without border padding
  1927. // to the resized mask FBO, and it's incompatible with same-pass curvature.
  1928. // (Same-pass curvature isn't used but could be in the future...maybe.)
  1929. // c.) "Fix discontinuities" requires derivatives and drawing one tile with
  1930. // border padding to the resized mask FBO, but it works with same-pass
  1931. // curvature. It's disabled without DRIVERS_ALLOW_DERIVATIVES #defined.
  1932. // Precedence: a, then, b, then c (if multiple strategies are #defined).
  1933. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD // 129.7 FPS, 4x, flat; 101.8 at fullscreen
  1934. #define ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE // 128.1 FPS, 4x, flat; 101.5 at fullscreen
  1935. #define ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES // 124.4 FPS, 4x, flat; 97.4 at fullscreen
  1936. // Also, manually resampling the phosphor mask is slightly blurrier with
  1937. // anisotropic filtering. (Resampling with mipmapping is even worse: It
  1938. // creates artifacts, but only with the fully bloomed shader.) The difference
  1939. // is subtle with small triads, but you can fix it for a small cost.
  1940. //#define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1941. ////////////////////////////// DERIVED SETTINGS //////////////////////////////
  1942. // Intel HD 4000 GPU's can't handle manual mask resizing (for now), setting the
  1943. // geometry mode at runtime, or a 4x4 true Gaussian resize. Disable
  1944. // incompatible settings ASAP. (INTEGRATED_GRAPHICS_COMPATIBILITY_MODE may be
  1945. // #defined by either user-settings.h or a wrapper .cg that #includes the
  1946. // current .cg pass.)
  1947. #ifdef INTEGRATED_GRAPHICS_COMPATIBILITY_MODE
  1948. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  1949. #undef PHOSPHOR_MASK_MANUALLY_RESIZE
  1950. #endif
  1951. #ifdef RUNTIME_GEOMETRY_MODE
  1952. #undef RUNTIME_GEOMETRY_MODE
  1953. #endif
  1954. // Mode 2 (4x4 Gaussian resize) won't work, and mode 1 (3x3 blur) is
  1955. // inferior in most cases, so replace 2.0 with 0.0:
  1956. static const float bloom_approx_filter =
  1957. bloom_approx_filter_static > 1.5 ? 0.0 : bloom_approx_filter_static;
  1958. #else
  1959. static const float bloom_approx_filter = bloom_approx_filter_static;
  1960. #endif
  1961. // Disable slow runtime paths if static parameters are used. Most of these
  1962. // won't be a problem anyway once the params are disabled, but some will.
  1963. #ifndef RUNTIME_SHADER_PARAMS_ENABLE
  1964. #ifdef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1965. #undef RUNTIME_PHOSPHOR_BLOOM_SIGMA
  1966. #endif
  1967. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  1968. #undef RUNTIME_ANTIALIAS_WEIGHTS
  1969. #endif
  1970. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1971. #undef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  1972. #endif
  1973. #ifdef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1974. #undef RUNTIME_SCANLINES_HORIZ_FILTER_COLORSPACE
  1975. #endif
  1976. #ifdef RUNTIME_GEOMETRY_TILT
  1977. #undef RUNTIME_GEOMETRY_TILT
  1978. #endif
  1979. #ifdef RUNTIME_GEOMETRY_MODE
  1980. #undef RUNTIME_GEOMETRY_MODE
  1981. #endif
  1982. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1983. #undef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  1984. #endif
  1985. #endif
  1986. // Make tex2Dbias a backup for tex2Dlod for wider compatibility.
  1987. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  1988. #define ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  1989. #endif
  1990. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  1991. #define ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  1992. #endif
  1993. // Rule out unavailable anisotropic compatibility strategies:
  1994. #ifndef DRIVERS_ALLOW_DERIVATIVES
  1995. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1996. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  1997. #endif
  1998. #endif
  1999. #ifndef DRIVERS_ALLOW_TEX2DLOD
  2000. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2001. #undef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2002. #endif
  2003. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2004. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2005. #endif
  2006. #ifdef ANTIALIAS_DISABLE_ANISOTROPIC
  2007. #undef ANTIALIAS_DISABLE_ANISOTROPIC
  2008. #endif
  2009. #endif
  2010. #ifndef DRIVERS_ALLOW_TEX2DBIAS
  2011. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2012. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2013. #endif
  2014. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2015. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2016. #endif
  2017. #endif
  2018. // Prioritize anisotropic tiling compatibility strategies by performance and
  2019. // disable unused strategies. This concentrates all the nesting in one place.
  2020. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2021. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2022. #undef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2023. #endif
  2024. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2025. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2026. #endif
  2027. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2028. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2029. #endif
  2030. #else
  2031. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2032. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2033. #undef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2034. #endif
  2035. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2036. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2037. #endif
  2038. #else
  2039. // ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE is only compatible with
  2040. // flat texture coords in the same pass, but that's all we use.
  2041. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2042. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2043. #undef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2044. #endif
  2045. #endif
  2046. #endif
  2047. #endif
  2048. // The tex2Dlod and tex2Dbias strategies share a lot in common, and we can
  2049. // reduce some #ifdef nesting in the next section by essentially OR'ing them:
  2050. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DLOD
  2051. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2052. #endif
  2053. #ifdef ANISOTROPIC_TILING_COMPAT_TEX2DBIAS
  2054. #define ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2055. #endif
  2056. // Prioritize anisotropic resampling compatibility strategies the same way:
  2057. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2058. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2059. #undef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2060. #endif
  2061. #endif
  2062. /////////////////////// DERIVED PHOSPHOR MASK CONSTANTS //////////////////////
  2063. // If we can use the large mipmapped LUT without mipmapping artifacts, we
  2064. // should: It gives us more options for using fewer samples.
  2065. #ifdef DRIVERS_ALLOW_TEX2DLOD
  2066. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2067. // TODO: Take advantage of this!
  2068. #define PHOSPHOR_MASK_RESIZE_MIPMAPPED_LUT
  2069. static const float2 mask_resize_src_lut_size = mask_texture_large_size;
  2070. #else
  2071. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2072. #endif
  2073. #else
  2074. static const float2 mask_resize_src_lut_size = mask_texture_small_size;
  2075. #endif
  2076. // tex2D's sampler2D parameter MUST be a uniform global, a uniform input to
  2077. // main_fragment, or a static alias of one of the above. This makes it hard
  2078. // to select the phosphor mask at runtime: We can't even assign to a uniform
  2079. // global in the vertex shader or select a sampler2D in the vertex shader and
  2080. // pass it to the fragment shader (even with explicit TEXUNIT# bindings),
  2081. // because it just gives us the input texture or a black screen. However, we
  2082. // can get around these limitations by calling tex2D three times with different
  2083. // uniform samplers (or resizing the phosphor mask three times altogether).
  2084. // With dynamic branches, we can process only one of these branches on top of
  2085. // quickly discarding fragments we don't need (cgc seems able to overcome
  2086. // limigations around dependent texture fetches inside of branches). Without
  2087. // dynamic branches, we have to process every branch for every fragment...which
  2088. // is slower. Runtime sampling mode selection is slower without dynamic
  2089. // branches as well. Let the user's static #defines decide if it's worth it.
  2090. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2091. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2092. #else
  2093. #ifdef FORCE_RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2094. #define RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2095. #endif
  2096. #endif
  2097. // We need to render some minimum number of tiles in the resize passes.
  2098. // We need at least 1.0 just to repeat a single tile, and we need extra
  2099. // padding beyond that for anisotropic filtering, discontinuitity fixing,
  2100. // antialiasing, same-pass curvature (not currently used), etc. First
  2101. // determine how many border texels and tiles we need, based on how the result
  2102. // will be sampled:
  2103. #ifdef GEOMETRY_EARLY
  2104. static const float max_subpixel_offset = aa_subpixel_r_offset_static.x;
  2105. // Most antialiasing filters have a base radius of 4.0 pixels:
  2106. static const float max_aa_base_pixel_border = 4.0 +
  2107. max_subpixel_offset;
  2108. #else
  2109. static const float max_aa_base_pixel_border = 0.0;
  2110. #endif
  2111. // Anisotropic filtering adds about 0.5 to the pixel border:
  2112. #ifndef ANISOTROPIC_TILING_COMPAT_TEX2DLOD_FAMILY
  2113. static const float max_aniso_pixel_border = max_aa_base_pixel_border + 0.5;
  2114. #else
  2115. static const float max_aniso_pixel_border = max_aa_base_pixel_border;
  2116. #endif
  2117. // Fixing discontinuities adds 1.0 more to the pixel border:
  2118. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  2119. static const float max_tiled_pixel_border = max_aniso_pixel_border + 1.0;
  2120. #else
  2121. static const float max_tiled_pixel_border = max_aniso_pixel_border;
  2122. #endif
  2123. // Convert the pixel border to an integer texel border. Assume same-pass
  2124. // curvature about triples the texel frequency:
  2125. #ifdef GEOMETRY_EARLY
  2126. static const float max_mask_texel_border =
  2127. ceil(max_tiled_pixel_border * 3.0);
  2128. #else
  2129. static const float max_mask_texel_border = ceil(max_tiled_pixel_border);
  2130. #endif
  2131. // Convert the texel border to a tile border using worst-case assumptions:
  2132. static const float max_mask_tile_border = max_mask_texel_border/
  2133. (mask_min_allowed_triad_size * mask_triads_per_tile);
  2134. // Finally, set the number of resized tiles to render to MASK_RESIZE, and set
  2135. // the starting texel (inside borders) for sampling it.
  2136. #ifndef GEOMETRY_EARLY
  2137. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  2138. // Special case: Render two tiles without borders. Anisotropic
  2139. // filtering doesn't seem to be a problem here.
  2140. static const float mask_resize_num_tiles = 1.0 + 1.0;
  2141. static const float mask_start_texels = 0.0;
  2142. #else
  2143. static const float mask_resize_num_tiles = 1.0 +
  2144. 2.0 * max_mask_tile_border;
  2145. static const float mask_start_texels = max_mask_texel_border;
  2146. #endif
  2147. #else
  2148. static const float mask_resize_num_tiles = 1.0 + 2.0*max_mask_tile_border;
  2149. static const float mask_start_texels = max_mask_texel_border;
  2150. #endif
  2151. // We have to fit mask_resize_num_tiles into an FBO with a viewport scale of
  2152. // mask_resize_viewport_scale. This limits the maximum final triad size.
  2153. // Estimate the minimum number of triads we can split the screen into in each
  2154. // dimension (we'll be as correct as mask_resize_viewport_scale is):
  2155. static const float mask_resize_num_triads =
  2156. mask_resize_num_tiles * mask_triads_per_tile;
  2157. static const float2 min_allowed_viewport_triads =
  2158. float2(mask_resize_num_triads) / mask_resize_viewport_scale;
  2159. //////////////////////// COMMON MATHEMATICAL CONSTANTS ///////////////////////
  2160. static const float pi = 3.141592653589;
  2161. // We often want to find the location of the previous texel, e.g.:
  2162. // const float2 curr_texel = uv * texture_size;
  2163. // const float2 prev_texel = floor(curr_texel - float2(0.5)) + float2(0.5);
  2164. // const float2 prev_texel_uv = prev_texel / texture_size;
  2165. // However, many GPU drivers round incorrectly around exact texel locations.
  2166. // We need to subtract a little less than 0.5 before flooring, and some GPU's
  2167. // require this value to be farther from 0.5 than others; define it here.
  2168. // const float2 prev_texel =
  2169. // floor(curr_texel - float2(under_half)) + float2(0.5);
  2170. static const float under_half = 0.4995;
  2171. #endif // DERIVED_SETTINGS_AND_CONSTANTS_H
  2172. //////////////////// END DERIVED-SETTINGS-AND-CONSTANTS /////////////////////
  2173. //////////////////////////////// END INCLUDES ////////////////////////////////
  2174. // Override some parameters for gamma-management.h and tex2Dantialias.h:
  2175. #define OVERRIDE_DEVICE_GAMMA
  2176. static const float gba_gamma = 3.5; // Irrelevant but necessary to define.
  2177. #define ANTIALIAS_OVERRIDE_BASICS
  2178. #define ANTIALIAS_OVERRIDE_PARAMETERS
  2179. // Provide accessors for vector constants that pack scalar uniforms:
  2180. inline float2 get_aspect_vector(const float geom_aspect_ratio)
  2181. {
  2182. // Get an aspect ratio vector. Enforce geom_max_aspect_ratio, and prevent
  2183. // the absolute scale from affecting the uv-mapping for curvature:
  2184. const float geom_clamped_aspect_ratio =
  2185. min(geom_aspect_ratio, geom_max_aspect_ratio);
  2186. const float2 geom_aspect =
  2187. normalize(float2(geom_clamped_aspect_ratio, 1.0));
  2188. return geom_aspect;
  2189. }
  2190. inline float2 get_geom_overscan_vector()
  2191. {
  2192. return float2(geom_overscan_x, geom_overscan_y);
  2193. }
  2194. inline float2 get_geom_tilt_angle_vector()
  2195. {
  2196. return float2(geom_tilt_angle_x, geom_tilt_angle_y);
  2197. }
  2198. inline float3 get_convergence_offsets_x_vector()
  2199. {
  2200. return float3(convergence_offset_x_r, convergence_offset_x_g,
  2201. convergence_offset_x_b);
  2202. }
  2203. inline float3 get_convergence_offsets_y_vector()
  2204. {
  2205. return float3(convergence_offset_y_r, convergence_offset_y_g,
  2206. convergence_offset_y_b);
  2207. }
  2208. inline float2 get_convergence_offsets_r_vector()
  2209. {
  2210. return float2(convergence_offset_x_r, convergence_offset_y_r);
  2211. }
  2212. inline float2 get_convergence_offsets_g_vector()
  2213. {
  2214. return float2(convergence_offset_x_g, convergence_offset_y_g);
  2215. }
  2216. inline float2 get_convergence_offsets_b_vector()
  2217. {
  2218. return float2(convergence_offset_x_b, convergence_offset_y_b);
  2219. }
  2220. inline float2 get_aa_subpixel_r_offset()
  2221. {
  2222. #ifdef RUNTIME_ANTIALIAS_WEIGHTS
  2223. #ifdef RUNTIME_ANTIALIAS_SUBPIXEL_OFFSETS
  2224. // WARNING: THIS IS EXTREMELY EXPENSIVE.
  2225. return float2(aa_subpixel_r_offset_x_runtime,
  2226. aa_subpixel_r_offset_y_runtime);
  2227. #else
  2228. return aa_subpixel_r_offset_static;
  2229. #endif
  2230. #else
  2231. return aa_subpixel_r_offset_static;
  2232. #endif
  2233. }
  2234. // Provide accessors settings which still need "cooking:"
  2235. inline float get_mask_amplify()
  2236. {
  2237. static const float mask_grille_amplify = 1.0/mask_grille_avg_color;
  2238. static const float mask_slot_amplify = 1.0/mask_slot_avg_color;
  2239. static const float mask_shadow_amplify = 1.0/mask_shadow_avg_color;
  2240. return mask_type < 0.5 ? mask_grille_amplify :
  2241. mask_type < 1.5 ? mask_slot_amplify :
  2242. mask_shadow_amplify;
  2243. }
  2244. inline float get_mask_sample_mode()
  2245. {
  2246. #ifdef RUNTIME_PHOSPHOR_MASK_MODE_TYPE_SELECT
  2247. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2248. return mask_sample_mode_desired;
  2249. #else
  2250. return clamp(mask_sample_mode_desired, 1.0, 2.0);
  2251. #endif
  2252. #else
  2253. #ifdef PHOSPHOR_MASK_MANUALLY_RESIZE
  2254. return mask_sample_mode_static;
  2255. #else
  2256. return clamp(mask_sample_mode_static, 1.0, 2.0);
  2257. #endif
  2258. #endif
  2259. }
  2260. #endif // BIND_SHADER_PARAMS_H
  2261. //////////////////////////// END BIND-SHADER-PARAMS ///////////////////////////
  2262. /////////////////////////////// VERTEX INCLUDES ///////////////////////////////
  2263. //#include "../../../../include/gamma-management.h"
  2264. //////////////////////////// BEGIN GAMMA-MANAGEMENT //////////////////////////
  2265. #ifndef GAMMA_MANAGEMENT_H
  2266. #define GAMMA_MANAGEMENT_H
  2267. ///////////////////////////////// MIT LICENSE ////////////////////////////////
  2268. // Copyright (C) 2014 TroggleMonkey
  2269. //
  2270. // Permission is hereby granted, free of charge, to any person obtaining a copy
  2271. // of this software and associated documentation files (the "Software"), to
  2272. // deal in the Software without restriction, including without limitation the
  2273. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  2274. // sell copies of the Software, and to permit persons to whom the Software is
  2275. // furnished to do so, subject to the following conditions:
  2276. //
  2277. // The above copyright notice and this permission notice shall be included in
  2278. // all copies or substantial portions of the Software.
  2279. //
  2280. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  2281. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  2282. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  2283. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  2284. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  2285. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  2286. // IN THE SOFTWARE.
  2287. ///////////////////////////////// DESCRIPTION ////////////////////////////////
  2288. // This file provides gamma-aware tex*D*() and encode_output() functions.
  2289. // Requires: Before #include-ing this file, the including file must #define
  2290. // the following macros when applicable and follow their rules:
  2291. // 1.) #define FIRST_PASS if this is the first pass.
  2292. // 2.) #define LAST_PASS if this is the last pass.
  2293. // 3.) If sRGB is available, set srgb_framebufferN = "true" for
  2294. // every pass except the last in your .cgp preset.
  2295. // 4.) If sRGB isn't available but you want gamma-correctness with
  2296. // no banding, #define GAMMA_ENCODE_EVERY_FBO each pass.
  2297. // 5.) #define SIMULATE_CRT_ON_LCD if desired (precedence over 5-7)
  2298. // 6.) #define SIMULATE_GBA_ON_LCD if desired (precedence over 6-7)
  2299. // 7.) #define SIMULATE_LCD_ON_CRT if desired (precedence over 7)
  2300. // 8.) #define SIMULATE_GBA_ON_CRT if desired (precedence over -)
  2301. // If an option in [5, 8] is #defined in the first or last pass, it
  2302. // should be #defined for both. It shouldn't make a difference
  2303. // whether it's #defined for intermediate passes or not.
  2304. // Optional: The including file (or an earlier included file) may optionally
  2305. // #define a number of macros indicating it will override certain
  2306. // macros and associated constants are as follows:
  2307. // static constants with either static or uniform constants. The
  2308. // 1.) OVERRIDE_STANDARD_GAMMA: The user must first define:
  2309. // static const float ntsc_gamma
  2310. // static const float pal_gamma
  2311. // static const float crt_reference_gamma_high
  2312. // static const float crt_reference_gamma_low
  2313. // static const float lcd_reference_gamma
  2314. // static const float crt_office_gamma
  2315. // static const float lcd_office_gamma
  2316. // 2.) OVERRIDE_DEVICE_GAMMA: The user must first define:
  2317. // static const float crt_gamma
  2318. // static const float gba_gamma
  2319. // static const float lcd_gamma
  2320. // 3.) OVERRIDE_FINAL_GAMMA: The user must first define:
  2321. // static const float input_gamma
  2322. // static const float intermediate_gamma
  2323. // static const float output_gamma
  2324. // (intermediate_gamma is for GAMMA_ENCODE_EVERY_FBO.)
  2325. // 4.) OVERRIDE_ALPHA_ASSUMPTIONS: The user must first define:
  2326. // static const bool assume_opaque_alpha
  2327. // The gamma constant overrides must be used in every pass or none,
  2328. // and OVERRIDE_FINAL_GAMMA bypasses all of the SIMULATE* macros.
  2329. // OVERRIDE_ALPHA_ASSUMPTIONS may be set on a per-pass basis.
  2330. // Usage: After setting macros appropriately, ignore gamma correction and
  2331. // replace all tex*D*() calls with equivalent gamma-aware
  2332. // tex*D*_linearize calls, except:
  2333. // 1.) When you read an LUT, use regular tex*D or a gamma-specified
  2334. // function, depending on its gamma encoding:
  2335. // tex*D*_linearize_gamma (takes a runtime gamma parameter)
  2336. // 2.) If you must read pass0's original input in a later pass, use
  2337. // tex2D_linearize_ntsc_gamma. If you want to read pass0's
  2338. // input with gamma-corrected bilinear filtering, consider
  2339. // creating a first linearizing pass and reading from the input
  2340. // of pass1 later.
  2341. // Then, return encode_output(color) from every fragment shader.
  2342. // Finally, use the global gamma_aware_bilinear boolean if you want
  2343. // to statically branch based on whether bilinear filtering is
  2344. // gamma-correct or not (e.g. for placing Gaussian blur samples).
  2345. //
  2346. // Detailed Policy:
  2347. // tex*D*_linearize() functions enforce a consistent gamma-management policy
  2348. // based on the FIRST_PASS and GAMMA_ENCODE_EVERY_FBO settings. They assume
  2349. // their input texture has the same encoding characteristics as the input for
  2350. // the current pass (which doesn't apply to the exceptions listed above).
  2351. // Similarly, encode_output() enforces a policy based on the LAST_PASS and
  2352. // GAMMA_ENCODE_EVERY_FBO settings. Together, they result in one of the
  2353. // following two pipelines.
  2354. // Typical pipeline with intermediate sRGB framebuffers:
  2355. // linear_color = pow(pass0_encoded_color, input_gamma);
  2356. // intermediate_output = linear_color; // Automatic sRGB encoding
  2357. // linear_color = intermediate_output; // Automatic sRGB decoding
  2358. // final_output = pow(intermediate_output, 1.0/output_gamma);
  2359. // Typical pipeline without intermediate sRGB framebuffers:
  2360. // linear_color = pow(pass0_encoded_color, input_gamma);
  2361. // intermediate_output = pow(linear_color, 1.0/intermediate_gamma);
  2362. // linear_color = pow(intermediate_output, intermediate_gamma);
  2363. // final_output = pow(intermediate_output, 1.0/output_gamma);
  2364. // Using GAMMA_ENCODE_EVERY_FBO is much slower, but it's provided as a way to
  2365. // easily get gamma-correctness without banding on devices where sRGB isn't
  2366. // supported.
  2367. //
  2368. // Use This Header to Maximize Code Reuse:
  2369. // The purpose of this header is to provide a consistent interface for texture
  2370. // reads and output gamma-encoding that localizes and abstracts away all the
  2371. // annoying details. This greatly reduces the amount of code in each shader
  2372. // pass that depends on the pass number in the .cgp preset or whether sRGB
  2373. // FBO's are being used: You can trivially change the gamma behavior of your
  2374. // whole pass by commenting or uncommenting 1-3 #defines. To reuse the same
  2375. // code in your first, Nth, and last passes, you can even put it all in another
  2376. // header file and #include it from skeleton .cg files that #define the
  2377. // appropriate pass-specific settings.
  2378. //
  2379. // Rationale for Using Three Macros:
  2380. // This file uses GAMMA_ENCODE_EVERY_FBO instead of an opposite macro like
  2381. // SRGB_PIPELINE to ensure sRGB is assumed by default, which hopefully imposes
  2382. // a lower maintenance burden on each pass. At first glance it seems we could
  2383. // accomplish everything with two macros: GAMMA_CORRECT_IN / GAMMA_CORRECT_OUT.
  2384. // This works for simple use cases where input_gamma == output_gamma, but it
  2385. // breaks down for more complex scenarios like CRT simulation, where the pass
  2386. // number determines the gamma encoding of the input and output.
  2387. /////////////////////////////// BASE CONSTANTS ///////////////////////////////
  2388. // Set standard gamma constants, but allow users to override them:
  2389. #ifndef OVERRIDE_STANDARD_GAMMA
  2390. // Standard encoding gammas:
  2391. static const float ntsc_gamma = 2.2; // Best to use NTSC for PAL too?
  2392. static const float pal_gamma = 2.8; // Never actually 2.8 in practice
  2393. // Typical device decoding gammas (only use for emulating devices):
  2394. // CRT/LCD reference gammas are higher than NTSC and Rec.709 video standard
  2395. // gammas: The standards purposely undercorrected for an analog CRT's
  2396. // assumed 2.5 reference display gamma to maintain contrast in assumed
  2397. // [dark] viewing conditions: http://www.poynton.com/PDFs/GammaFAQ.pdf
  2398. // These unstated assumptions about display gamma and perceptual rendering
  2399. // intent caused a lot of confusion, and more modern CRT's seemed to target
  2400. // NTSC 2.2 gamma with circuitry. LCD displays seem to have followed suit
  2401. // (they struggle near black with 2.5 gamma anyway), especially PC/laptop
  2402. // displays designed to view sRGB in bright environments. (Standards are
  2403. // also in flux again with BT.1886, but it's underspecified for displays.)
  2404. static const float crt_reference_gamma_high = 2.5; // In (2.35, 2.55)
  2405. static const float crt_reference_gamma_low = 2.35; // In (2.35, 2.55)
  2406. static const float lcd_reference_gamma = 2.5; // To match CRT
  2407. static const float crt_office_gamma = 2.2; // Circuitry-adjusted for NTSC
  2408. static const float lcd_office_gamma = 2.2; // Approximates sRGB
  2409. #endif // OVERRIDE_STANDARD_GAMMA
  2410. // Assuming alpha == 1.0 might make it easier for users to avoid some bugs,
  2411. // but only if they're aware of it.
  2412. #ifndef OVERRIDE_ALPHA_ASSUMPTIONS
  2413. static const bool assume_opaque_alpha = false;
  2414. #endif
  2415. /////////////////////// DERIVED CONSTANTS AS FUNCTIONS ///////////////////////
  2416. // gamma-management.h should be compatible with overriding gamma values with
  2417. // runtime user parameters, but we can only define other global constants in
  2418. // terms of static constants, not uniform user parameters. To get around this
  2419. // limitation, we need to define derived constants using functions.
  2420. // Set device gamma constants, but allow users to override them:
  2421. #ifdef OVERRIDE_DEVICE_GAMMA
  2422. // The user promises to globally define the appropriate constants:
  2423. inline float get_crt_gamma() { return crt_gamma; }
  2424. inline float get_gba_gamma() { return gba_gamma; }
  2425. inline float get_lcd_gamma() { return lcd_gamma; }
  2426. #else
  2427. inline float get_crt_gamma() { return crt_reference_gamma_high; }
  2428. inline float get_gba_gamma() { return 3.5; } // Game Boy Advance; in (3.0, 4.0)
  2429. inline float get_lcd_gamma() { return lcd_office_gamma; }
  2430. #endif // OVERRIDE_DEVICE_GAMMA
  2431. // Set decoding/encoding gammas for the first/lass passes, but allow overrides:
  2432. #ifdef OVERRIDE_FINAL_GAMMA
  2433. // The user promises to globally define the appropriate constants:
  2434. inline float get_intermediate_gamma() { return intermediate_gamma; }
  2435. inline float get_input_gamma() { return input_gamma; }
  2436. inline float get_output_gamma() { return output_gamma; }
  2437. #else
  2438. // If we gamma-correct every pass, always use ntsc_gamma between passes to
  2439. // ensure middle passes don't need to care if anything is being simulated:
  2440. inline float get_intermediate_gamma() { return ntsc_gamma; }
  2441. #ifdef SIMULATE_CRT_ON_LCD
  2442. inline float get_input_gamma() { return get_crt_gamma(); }
  2443. inline float get_output_gamma() { return get_lcd_gamma(); }
  2444. #else
  2445. #ifdef SIMULATE_GBA_ON_LCD
  2446. inline float get_input_gamma() { return get_gba_gamma(); }
  2447. inline float get_output_gamma() { return get_lcd_gamma(); }
  2448. #else
  2449. #ifdef SIMULATE_LCD_ON_CRT
  2450. inline float get_input_gamma() { return get_lcd_gamma(); }
  2451. inline float get_output_gamma() { return get_crt_gamma(); }
  2452. #else
  2453. #ifdef SIMULATE_GBA_ON_CRT
  2454. inline float get_input_gamma() { return get_gba_gamma(); }
  2455. inline float get_output_gamma() { return get_crt_gamma(); }
  2456. #else // Don't simulate anything:
  2457. inline float get_input_gamma() { return ntsc_gamma; }
  2458. inline float get_output_gamma() { return ntsc_gamma; }
  2459. #endif // SIMULATE_GBA_ON_CRT
  2460. #endif // SIMULATE_LCD_ON_CRT
  2461. #endif // SIMULATE_GBA_ON_LCD
  2462. #endif // SIMULATE_CRT_ON_LCD
  2463. #endif // OVERRIDE_FINAL_GAMMA
  2464. // Set decoding/encoding gammas for the current pass. Use static constants for
  2465. // linearize_input and gamma_encode_output, because they aren't derived, and
  2466. // they let the compiler do dead-code elimination.
  2467. #ifndef GAMMA_ENCODE_EVERY_FBO
  2468. #ifdef FIRST_PASS
  2469. static const bool linearize_input = true;
  2470. inline float get_pass_input_gamma() { return get_input_gamma(); }
  2471. #else
  2472. static const bool linearize_input = false;
  2473. inline float get_pass_input_gamma() { return 1.0; }
  2474. #endif
  2475. #ifdef LAST_PASS
  2476. static const bool gamma_encode_output = true;
  2477. inline float get_pass_output_gamma() { return get_output_gamma(); }
  2478. #else
  2479. static const bool gamma_encode_output = false;
  2480. inline float get_pass_output_gamma() { return 1.0; }
  2481. #endif
  2482. #else
  2483. static const bool linearize_input = true;
  2484. static const bool gamma_encode_output = true;
  2485. #ifdef FIRST_PASS
  2486. inline float get_pass_input_gamma() { return get_input_gamma(); }
  2487. #else
  2488. inline float get_pass_input_gamma() { return get_intermediate_gamma(); }
  2489. #endif
  2490. #ifdef LAST_PASS
  2491. inline float get_pass_output_gamma() { return get_output_gamma(); }
  2492. #else
  2493. inline float get_pass_output_gamma() { return get_intermediate_gamma(); }
  2494. #endif
  2495. #endif
  2496. // Users might want to know if bilinear filtering will be gamma-correct:
  2497. static const bool gamma_aware_bilinear = !linearize_input;
  2498. ////////////////////// COLOR ENCODING/DECODING FUNCTIONS /////////////////////
  2499. inline float4 encode_output(const float4 color)
  2500. {
  2501. if(gamma_encode_output)
  2502. {
  2503. if(assume_opaque_alpha)
  2504. {
  2505. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), 1.0);
  2506. }
  2507. else
  2508. {
  2509. return float4(pow(color.rgb, float3(1.0/get_pass_output_gamma())), color.a);
  2510. }
  2511. }
  2512. else
  2513. {
  2514. return color;
  2515. }
  2516. }
  2517. inline float4 decode_input(const float4 color)
  2518. {
  2519. if(linearize_input)
  2520. {
  2521. if(assume_opaque_alpha)
  2522. {
  2523. return float4(pow(color.rgb, float3(get_pass_input_gamma())), 1.0);
  2524. }
  2525. else
  2526. {
  2527. return float4(pow(color.rgb, float3(get_pass_input_gamma())), color.a);
  2528. }
  2529. }
  2530. else
  2531. {
  2532. return color;
  2533. }
  2534. }
  2535. inline float4 decode_gamma_input(const float4 color, const float3 gamma)
  2536. {
  2537. if(assume_opaque_alpha)
  2538. {
  2539. return float4(pow(color.rgb, gamma), 1.0);
  2540. }
  2541. else
  2542. {
  2543. return float4(pow(color.rgb, gamma), color.a);
  2544. }
  2545. }
  2546. //TODO/FIXME: I have no idea why replacing the lookup wrappers with this macro fixes the blurs being offset ¯\_(ツ)_/¯
  2547. //#define tex2D_linearize(C, D) decode_input(vec4(COMPAT_TEXTURE(C, D)))
  2548. // EDIT: it's the 'const' in front of the coords that's doing it
  2549. /////////////////////////// TEXTURE LOOKUP WRAPPERS //////////////////////////
  2550. // "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  2551. // Provide a wide array of linearizing texture lookup wrapper functions. The
  2552. // Cg shader spec Retroarch uses only allows for 2D textures, but 1D and 3D
  2553. // lookups are provided for completeness in case that changes someday. Nobody
  2554. // is likely to use the *fetch and *proj functions, but they're included just
  2555. // in case. The only tex*D texture sampling functions omitted are:
  2556. // - tex*Dcmpbias
  2557. // - tex*Dcmplod
  2558. // - tex*DARRAY*
  2559. // - tex*DMS*
  2560. // - Variants returning integers
  2561. // Standard line length restrictions are ignored below for vertical brevity.
  2562. /*
  2563. // tex1D:
  2564. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords)
  2565. { return decode_input(tex1D(tex, tex_coords)); }
  2566. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords)
  2567. { return decode_input(tex1D(tex, tex_coords)); }
  2568. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const int texel_off)
  2569. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  2570. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  2571. { return decode_input(tex1D(tex, tex_coords, texel_off)); }
  2572. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy)
  2573. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  2574. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy)
  2575. { return decode_input(tex1D(tex, tex_coords, dx, dy)); }
  2576. inline float4 tex1D_linearize(const sampler1D tex, const float tex_coords, const float dx, const float dy, const int texel_off)
  2577. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  2578. inline float4 tex1D_linearize(const sampler1D tex, const float2 tex_coords, const float dx, const float dy, const int texel_off)
  2579. { return decode_input(tex1D(tex, tex_coords, dx, dy, texel_off)); }
  2580. // tex1Dbias:
  2581. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords)
  2582. { return decode_input(tex1Dbias(tex, tex_coords)); }
  2583. inline float4 tex1Dbias_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  2584. { return decode_input(tex1Dbias(tex, tex_coords, texel_off)); }
  2585. // tex1Dfetch:
  2586. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords)
  2587. { return decode_input(tex1Dfetch(tex, tex_coords)); }
  2588. inline float4 tex1Dfetch_linearize(const sampler1D tex, const int4 tex_coords, const int texel_off)
  2589. { return decode_input(tex1Dfetch(tex, tex_coords, texel_off)); }
  2590. // tex1Dlod:
  2591. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords)
  2592. { return decode_input(tex1Dlod(tex, tex_coords)); }
  2593. inline float4 tex1Dlod_linearize(const sampler1D tex, const float4 tex_coords, const int texel_off)
  2594. { return decode_input(tex1Dlod(tex, tex_coords, texel_off)); }
  2595. // tex1Dproj:
  2596. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords)
  2597. { return decode_input(tex1Dproj(tex, tex_coords)); }
  2598. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords)
  2599. { return decode_input(tex1Dproj(tex, tex_coords)); }
  2600. inline float4 tex1Dproj_linearize(const sampler1D tex, const float2 tex_coords, const int texel_off)
  2601. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  2602. inline float4 tex1Dproj_linearize(const sampler1D tex, const float3 tex_coords, const int texel_off)
  2603. { return decode_input(tex1Dproj(tex, tex_coords, texel_off)); }
  2604. */
  2605. // tex2D:
  2606. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords)
  2607. { return decode_input(COMPAT_TEXTURE(tex, tex_coords)); }
  2608. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords)
  2609. { return decode_input(COMPAT_TEXTURE(tex, tex_coords.xy)); }
  2610. inline float4 tex2D_linearize(const sampler2D tex, float2 tex_coords, int texel_off)
  2611. { return decode_input(textureLod(tex, tex_coords, texel_off)); }
  2612. inline float4 tex2D_linearize(const sampler2D tex, float3 tex_coords, int texel_off)
  2613. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  2614. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy)
  2615. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  2616. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy)
  2617. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy)); }
  2618. //inline float4 tex2D_linearize(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  2619. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  2620. //inline float4 tex2D_linearize(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const int texel_off)
  2621. //{ return decode_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off)); }
  2622. // tex2Dbias:
  2623. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords)
  2624. //{ return decode_input(tex2Dbias(tex, tex_coords)); }
  2625. //inline float4 tex2Dbias_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  2626. //{ return decode_input(tex2Dbias(tex, tex_coords, texel_off)); }
  2627. // tex2Dfetch:
  2628. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords)
  2629. //{ return decode_input(tex2Dfetch(tex, tex_coords)); }
  2630. //inline float4 tex2Dfetch_linearize(const sampler2D tex, const int4 tex_coords, const int texel_off)
  2631. //{ return decode_input(tex2Dfetch(tex, tex_coords, texel_off)); }
  2632. // tex2Dlod:
  2633. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords)
  2634. { return decode_input(textureLod(tex, tex_coords.xy, 0.0)); }
  2635. inline float4 tex2Dlod_linearize(const sampler2D tex, float4 tex_coords, int texel_off)
  2636. { return decode_input(textureLod(tex, tex_coords.xy, texel_off)); }
  2637. /*
  2638. // tex2Dproj:
  2639. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords)
  2640. { return decode_input(tex2Dproj(tex, tex_coords)); }
  2641. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords)
  2642. { return decode_input(tex2Dproj(tex, tex_coords)); }
  2643. inline float4 tex2Dproj_linearize(const sampler2D tex, const float3 tex_coords, const int texel_off)
  2644. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  2645. inline float4 tex2Dproj_linearize(const sampler2D tex, const float4 tex_coords, const int texel_off)
  2646. { return decode_input(tex2Dproj(tex, tex_coords, texel_off)); }
  2647. */
  2648. /*
  2649. // tex3D:
  2650. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords)
  2651. { return decode_input(tex3D(tex, tex_coords)); }
  2652. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const int texel_off)
  2653. { return decode_input(tex3D(tex, tex_coords, texel_off)); }
  2654. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy)
  2655. { return decode_input(tex3D(tex, tex_coords, dx, dy)); }
  2656. inline float4 tex3D_linearize(const sampler3D tex, const float3 tex_coords, const float3 dx, const float3 dy, const int texel_off)
  2657. { return decode_input(tex3D(tex, tex_coords, dx, dy, texel_off)); }
  2658. // tex3Dbias:
  2659. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords)
  2660. { return decode_input(tex3Dbias(tex, tex_coords)); }
  2661. inline float4 tex3Dbias_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  2662. { return decode_input(tex3Dbias(tex, tex_coords, texel_off)); }
  2663. // tex3Dfetch:
  2664. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords)
  2665. { return decode_input(tex3Dfetch(tex, tex_coords)); }
  2666. inline float4 tex3Dfetch_linearize(const sampler3D tex, const int4 tex_coords, const int texel_off)
  2667. { return decode_input(tex3Dfetch(tex, tex_coords, texel_off)); }
  2668. // tex3Dlod:
  2669. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords)
  2670. { return decode_input(tex3Dlod(tex, tex_coords)); }
  2671. inline float4 tex3Dlod_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  2672. { return decode_input(tex3Dlod(tex, tex_coords, texel_off)); }
  2673. // tex3Dproj:
  2674. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords)
  2675. { return decode_input(tex3Dproj(tex, tex_coords)); }
  2676. inline float4 tex3Dproj_linearize(const sampler3D tex, const float4 tex_coords, const int texel_off)
  2677. { return decode_input(tex3Dproj(tex, tex_coords, texel_off)); }
  2678. /////////*
  2679. // NONSTANDARD "SMART" LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  2680. // This narrow selection of nonstandard tex2D* functions can be useful:
  2681. // tex2Dlod0: Automatically fill in the tex2D LOD parameter for mip level 0.
  2682. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords)
  2683. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0))); }
  2684. //inline float4 tex2Dlod0_linearize(const sampler2D tex, const float2 tex_coords, const int texel_off)
  2685. //{ return decode_input(tex2Dlod(tex, float4(tex_coords, 0.0, 0.0), texel_off)); }
  2686. // MANUALLY LINEARIZING TEXTURE LOOKUP FUNCTIONS:
  2687. // Provide a narrower selection of tex2D* wrapper functions that decode an
  2688. // input sample with a specified gamma value. These are useful for reading
  2689. // LUT's and for reading the input of pass0 in a later pass.
  2690. // tex2D:
  2691. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float3 gamma)
  2692. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords), gamma); }
  2693. inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float3 gamma)
  2694. { return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords.xy), gamma); }
  2695. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const int texel_off, const float3 gamma)
  2696. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  2697. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const int texel_off, const float3 gamma)
  2698. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, texel_off), gamma); }
  2699. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float2 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  2700. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  2701. //inline float4 tex2D_linearize_gamma(const sampler2D tex, const float3 tex_coords, const float2 dx, const float2 dy, const float3 gamma)
  2702. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy), gamma); }
  2703. //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)
  2704. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  2705. //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)
  2706. //{ return decode_gamma_input(COMPAT_TEXTURE(tex, tex_coords, dx, dy, texel_off), gamma); }
  2707. /*
  2708. // tex2Dbias:
  2709. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const float3 gamma)
  2710. { return decode_gamma_input(tex2Dbias(tex, tex_coords), gamma); }
  2711. inline float4 tex2Dbias_linearize_gamma(const sampler2D tex, const float4 tex_coords, const int texel_off, const float3 gamma)
  2712. { return decode_gamma_input(tex2Dbias(tex, tex_coords, texel_off), gamma); }
  2713. // tex2Dfetch:
  2714. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const float3 gamma)
  2715. { return decode_gamma_input(tex2Dfetch(tex, tex_coords), gamma); }
  2716. inline float4 tex2Dfetch_linearize_gamma(const sampler2D tex, const int4 tex_coords, const int texel_off, const float3 gamma)
  2717. { return decode_gamma_input(tex2Dfetch(tex, tex_coords, texel_off), gamma); }
  2718. */
  2719. // tex2Dlod:
  2720. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, float3 gamma)
  2721. { return decode_gamma_input(textureLod(tex, tex_coords.xy, 0.0), gamma); }
  2722. inline float4 tex2Dlod_linearize_gamma(const sampler2D tex, float4 tex_coords, int texel_off, float3 gamma)
  2723. { return decode_gamma_input(textureLod(tex, tex_coords.xy, texel_off), gamma); }
  2724. #endif // GAMMA_MANAGEMENT_H
  2725. //////////////////////////// END GAMMA-MANAGEMENT //////////////////////////
  2726. //#include "phosphor-mask-resizing.h"
  2727. //////////////////////// BEGIN PHOSPHOR-MASK-RESIZING ////////////////////////
  2728. #ifndef PHOSPHOR_MASK_RESIZING_H
  2729. #define PHOSPHOR_MASK_RESIZING_H
  2730. ///////////////////////////// GPL LICENSE NOTICE /////////////////////////////
  2731. // crt-royale: A full-featured CRT shader, with cheese.
  2732. // Copyright (C) 2014 TroggleMonkey <trogglemonkey@gmx.com>
  2733. //
  2734. // This program is free software; you can redistribute it and/or modify it
  2735. // under the terms of the GNU General Public License as published by the Free
  2736. // Software Foundation; either version 2 of the License, or any later version.
  2737. //
  2738. // This program is distributed in the hope that it will be useful, but WITHOUT
  2739. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  2740. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  2741. // more details.
  2742. //
  2743. // You should have received a copy of the GNU General Public License along with
  2744. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  2745. // Place, Suite 330, Boston, MA 02111-1307 USA
  2746. ////////////////////////////////// INCLUDES //////////////////////////////////
  2747. //#include "../user-settings.h"
  2748. //#include "derived-settings-and-constants.h"
  2749. ///////////////////////////// CODEPATH SELECTION /////////////////////////////
  2750. // Choose a looping strategy based on what's allowed:
  2751. // Dynamic loops not allowed: Use a flat static loop.
  2752. // Dynamic loops accomodated: Coarsely branch around static loops.
  2753. // Dynamic loops assumed allowed: Use a flat dynamic loop.
  2754. #ifndef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2755. #ifdef ACCOMODATE_POSSIBLE_DYNAMIC_LOOPS
  2756. #define BREAK_LOOPS_INTO_PIECES
  2757. #else
  2758. #define USE_SINGLE_STATIC_LOOP
  2759. #endif
  2760. #endif // No else needed: Dynamic loops assumed.
  2761. ////////////////////////////////// CONSTANTS /////////////////////////////////
  2762. // The larger the resized tile, the fewer samples we'll need for downsizing.
  2763. // See if we can get a static min tile size > mask_min_allowed_tile_size:
  2764. static const float mask_min_allowed_tile_size = ceil(
  2765. mask_min_allowed_triad_size * mask_triads_per_tile);
  2766. static const float mask_min_expected_tile_size =
  2767. mask_min_allowed_tile_size;
  2768. // Limit the number of sinc resize taps by the maximum minification factor:
  2769. static const float pi_over_lobes = pi/mask_sinc_lobes;
  2770. static const float max_sinc_resize_samples_float = 2.0 * mask_sinc_lobes *
  2771. mask_resize_src_lut_size.x/mask_min_expected_tile_size;
  2772. // Vectorized loops sample in multiples of 4. Round up to be safe:
  2773. static const float max_sinc_resize_samples_m4 = ceil(
  2774. max_sinc_resize_samples_float * 0.25) * 4.0;
  2775. ///////////////////////// RESAMPLING FUNCTION HELPERS ////////////////////////
  2776. inline float get_dynamic_loop_size(const float magnification_scale)
  2777. {
  2778. // Requires: The following global constants must be defined:
  2779. // 1.) mask_sinc_lobes
  2780. // 2.) max_sinc_resize_samples_m4
  2781. // Returns: The minimum number of texture samples for a correct downsize
  2782. // at magnification_scale.
  2783. // We're downsizing, so the filter is sized across 2*lobes output pixels
  2784. // (not 2*lobes input texels). This impacts distance measurements and the
  2785. // minimum number of input samples needed.
  2786. const float min_samples_float = 2.0 * mask_sinc_lobes / magnification_scale;
  2787. const float min_samples_m4 = ceil(min_samples_float * 0.25) * 4.0;
  2788. #ifdef DRIVERS_ALLOW_DYNAMIC_BRANCHES
  2789. const float max_samples_m4 = max_sinc_resize_samples_m4;
  2790. #else // ifdef BREAK_LOOPS_INTO_PIECES
  2791. // Simulating loops with branches imposes a 128-sample limit.
  2792. const float max_samples_m4 = min(128.0, max_sinc_resize_samples_m4);
  2793. #endif
  2794. return min(min_samples_m4, max_samples_m4);
  2795. }
  2796. float2 get_first_texel_tile_uv_and_dist(const float2 tex_uv,
  2797. const float2 tex_size, const float dr,
  2798. const float input_tiles_per_texture_r, const float samples,
  2799. static const bool vertical)
  2800. {
  2801. // Requires: 1.) dr == du == 1.0/texture_size.x or
  2802. // dr == dv == 1.0/texture_size.y
  2803. // (whichever direction we're resampling in).
  2804. // It's a scalar to save register space.
  2805. // 2.) input_tiles_per_texture_r is the number of input tiles
  2806. // that can fit in the input texture in the direction we're
  2807. // resampling this pass.
  2808. // 3.) vertical indicates whether we're resampling vertically
  2809. // this pass (or horizontally).
  2810. // Returns: Pack and return the first sample's tile_uv coord in [0, 1]
  2811. // and its texel distance from the destination pixel, in the
  2812. // resized dimension only.
  2813. // We'll start with the topmost or leftmost sample and work down or right,
  2814. // so get the first sample location and distance. Modify both dimensions
  2815. // as if we're doing a one-pass 2D resize; we'll throw away the unneeded
  2816. // (and incorrect) dimension at the end.
  2817. const float2 curr_texel = tex_uv * tex_size;
  2818. const float2 prev_texel =
  2819. floor(curr_texel - float2(under_half)) + float2(0.5);
  2820. const float2 first_texel = prev_texel - float2(samples/2.0 - 1.0);
  2821. const float2 first_texel_uv_wrap_2D = first_texel * dr;
  2822. const float2 first_texel_dist_2D = curr_texel - first_texel;
  2823. // Convert from tex_uv to tile_uv coords so we can sub fracs for fmods.
  2824. const float2 first_texel_tile_uv_wrap_2D =
  2825. first_texel_uv_wrap_2D * input_tiles_per_texture_r;
  2826. // Project wrapped coordinates to the [0, 1] range. We'll do this with all
  2827. // samples,but the first texel is special, since it might be negative.
  2828. const float2 coord_negative =
  2829. float2((first_texel_tile_uv_wrap_2D.x < 0.),(first_texel_tile_uv_wrap_2D.y < 0.));
  2830. const float2 first_texel_tile_uv_2D =
  2831. frac(first_texel_tile_uv_wrap_2D) + coord_negative;
  2832. // Pack the first texel's tile_uv coord and texel distance in 1D:
  2833. const float2 tile_u_and_dist =
  2834. float2(first_texel_tile_uv_2D.x, first_texel_dist_2D.x);
  2835. const float2 tile_v_and_dist =
  2836. float2(first_texel_tile_uv_2D.y, first_texel_dist_2D.y);
  2837. return vertical ? tile_v_and_dist : tile_u_and_dist;
  2838. //return lerp(tile_u_and_dist, tile_v_and_dist, float(vertical));
  2839. }
  2840. inline float4 tex2Dlod0try(const sampler2D tex, const float2 tex_uv)
  2841. {
  2842. // Mipmapping and anisotropic filtering get confused by sinc-resampling.
  2843. // One [slow] workaround is to select the lowest mip level:
  2844. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DLOD
  2845. return textureLod(tex, float4(tex_uv, 0.0, 0.0).xy);
  2846. #else
  2847. #ifdef ANISOTROPIC_RESAMPLING_COMPAT_TEX2DBIAS
  2848. return tex2Dbias(tex, float4(tex_uv, 0.0, -16.0));
  2849. #else
  2850. return texture(tex, tex_uv);
  2851. #endif
  2852. #endif
  2853. }
  2854. ////////////////////////////// LOOP BODY MACROS //////////////////////////////
  2855. // Using inline functions can exceed the temporary register limit, so we're
  2856. // stuck with #define macros (I'm TRULY sorry). They're declared here instead
  2857. // of above to be closer to the actual invocation sites. Steps:
  2858. // 1.) Get the exact texel location.
  2859. // 2.) Sample the phosphor mask (already assumed encoded in linear RGB).
  2860. // 3.) Get the distance from the current pixel and sinc weight:
  2861. // sinc(dist) = sin(pi * dist)/(pi * dist)
  2862. // We can also use the slower/smoother Lanczos instead:
  2863. // L(x) = sinc(dist) * sinc(dist / lobes)
  2864. // 4.) Accumulate the weight sum in weights, and accumulate the weighted texels
  2865. // in pixel_color (we'll normalize outside the loop at the end).
  2866. // We vectorize the loop to help reduce the Lanczos window's cost.
  2867. // The r coord is the coord in the dimension we're resizing along (u or v),
  2868. // and first_texel_tile_uv_rrrr is a float4 of the first texel's u or v
  2869. // tile_uv coord in [0, 1]. tex_uv_r will contain the tile_uv u or v coord
  2870. // for four new texel samples.
  2871. #define CALCULATE_R_COORD_FOR_4_SAMPLES \
  2872. const float4 true_i = float4(i_base + i) + float4(0.0, 1.0, 2.0, 3.0); \
  2873. const float4 tile_uv_r = frac( \
  2874. first_texel_tile_uv_rrrr + true_i * tile_dr); \
  2875. const float4 tex_uv_r = tile_uv_r * tile_size_uv_r;
  2876. #ifdef PHOSPHOR_MASK_RESIZE_LANCZOS_WINDOW
  2877. #define CALCULATE_SINC_RESAMPLE_WEIGHTS \
  2878. const float4 pi_dist_over_lobes = pi_over_lobes * dist; \
  2879. const float4 weights = min(sin(pi_dist) * sin(pi_dist_over_lobes) /\
  2880. (pi_dist*pi_dist_over_lobes), float4(1.0));
  2881. #else
  2882. #define CALCULATE_SINC_RESAMPLE_WEIGHTS \
  2883. const float4 weights = min(sin(pi_dist)/pi_dist, float4(1.0));
  2884. #endif
  2885. #define UPDATE_COLOR_AND_WEIGHT_SUMS \
  2886. const float4 dist = magnification_scale * \
  2887. abs(first_dist_unscaled - true_i); \
  2888. const float4 pi_dist = pi * dist; \
  2889. CALCULATE_SINC_RESAMPLE_WEIGHTS; \
  2890. pixel_color += new_sample0 * weights.xxx; \
  2891. pixel_color += new_sample1 * weights.yyy; \
  2892. pixel_color += new_sample2 * weights.zzz; \
  2893. pixel_color += new_sample3 * weights.www; \
  2894. weight_sum += weights;
  2895. #define VERTICAL_SINC_RESAMPLE_LOOP_BODY \
  2896. CALCULATE_R_COORD_FOR_4_SAMPLES; \
  2897. const float3 new_sample0 = tex2Dlod0try(tex, \
  2898. float2(tex_uv.x, tex_uv_r.x)).rgb; \
  2899. const float3 new_sample1 = tex2Dlod0try(tex, \
  2900. float2(tex_uv.x, tex_uv_r.y)).rgb; \
  2901. const float3 new_sample2 = tex2Dlod0try(tex, \
  2902. float2(tex_uv.x, tex_uv_r.z)).rgb; \
  2903. const float3 new_sample3 = tex2Dlod0try(tex, \
  2904. float2(tex_uv.x, tex_uv_r.w)).rgb; \
  2905. UPDATE_COLOR_AND_WEIGHT_SUMS;
  2906. #define HORIZONTAL_SINC_RESAMPLE_LOOP_BODY \
  2907. CALCULATE_R_COORD_FOR_4_SAMPLES; \
  2908. const float3 new_sample0 = tex2Dlod0try(tex, \
  2909. float2(tex_uv_r.x, tex_uv.y)).rgb; \
  2910. const float3 new_sample1 = tex2Dlod0try(tex, \
  2911. float2(tex_uv_r.y, tex_uv.y)).rgb; \
  2912. const float3 new_sample2 = tex2Dlod0try(tex, \
  2913. float2(tex_uv_r.z, tex_uv.y)).rgb; \
  2914. const float3 new_sample3 = tex2Dlod0try(tex, \
  2915. float2(tex_uv_r.w, tex_uv.y)).rgb; \
  2916. UPDATE_COLOR_AND_WEIGHT_SUMS;
  2917. //////////////////////////// RESAMPLING FUNCTIONS ////////////////////////////
  2918. float3 downsample_vertical_sinc_tiled(const sampler2D tex,
  2919. const float2 tex_uv, const float2 tex_size, static const float dr,
  2920. const float magnification_scale, static const float tile_size_uv_r)
  2921. {
  2922. // Requires: 1.) dr == du == 1.0/texture_size.x or
  2923. // dr == dv == 1.0/texture_size.y
  2924. // (whichever direction we're resampling in).
  2925. // It's a scalar to save register space.
  2926. // 2.) tile_size_uv_r is the number of texels an input tile
  2927. // takes up in the input texture, in the direction we're
  2928. // resampling this pass.
  2929. // 3.) magnification_scale must be <= 1.0.
  2930. // Returns: Return a [Lanczos] sinc-resampled pixel of a vertically
  2931. // downsized input tile embedded in an input texture. (The
  2932. // vertical version is special-cased though: It assumes the
  2933. // tile size equals the [static] texture size, since it's used
  2934. // on an LUT texture input containing one tile. For more
  2935. // generic use, eliminate the "static" in the parameters.)
  2936. // The "r" in "dr," "tile_size_uv_r," etc. refers to the dimension
  2937. // we're resizing along, e.g. "dy" in this case.
  2938. #ifdef USE_SINGLE_STATIC_LOOP
  2939. // A static loop can be faster, but it might blur too much from using
  2940. // more samples than it should.
  2941. static const int samples = int(max_sinc_resize_samples_m4);
  2942. #else
  2943. const int samples = int(get_dynamic_loop_size(magnification_scale));
  2944. #endif
  2945. // Get the first sample location (scalar tile uv coord along the resized
  2946. // dimension) and distance from the output location (in texels):
  2947. static const float input_tiles_per_texture_r = 1.0/tile_size_uv_r;
  2948. // true = vertical resize:
  2949. const float2 first_texel_tile_r_and_dist = get_first_texel_tile_uv_and_dist(
  2950. tex_uv, tex_size, dr, input_tiles_per_texture_r, samples, true);
  2951. const float4 first_texel_tile_uv_rrrr = first_texel_tile_r_and_dist.xxxx;
  2952. const float4 first_dist_unscaled = first_texel_tile_r_and_dist.yyyy;
  2953. // Get the tile sample offset:
  2954. static const float tile_dr = dr * input_tiles_per_texture_r;
  2955. // Sum up each weight and weighted sample color, varying the looping
  2956. // strategy based on our expected dynamic loop capabilities. See the
  2957. // loop body macros above.
  2958. int i_base = 0;
  2959. float4 weight_sum = float4(0.0);
  2960. float3 pixel_color = float3(0.0);
  2961. static const int i_step = 4;
  2962. #ifdef BREAK_LOOPS_INTO_PIECES
  2963. if(samples - i_base >= 64)
  2964. {
  2965. for(int i = 0; i < 64; i += i_step)
  2966. {
  2967. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  2968. }
  2969. i_base += 64;
  2970. }
  2971. if(samples - i_base >= 32)
  2972. {
  2973. for(int i = 0; i < 32; i += i_step)
  2974. {
  2975. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  2976. }
  2977. i_base += 32;
  2978. }
  2979. if(samples - i_base >= 16)
  2980. {
  2981. for(int i = 0; i < 16; i += i_step)
  2982. {
  2983. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  2984. }
  2985. i_base += 16;
  2986. }
  2987. if(samples - i_base >= 8)
  2988. {
  2989. for(int i = 0; i < 8; i += i_step)
  2990. {
  2991. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  2992. }
  2993. i_base += 8;
  2994. }
  2995. if(samples - i_base >= 4)
  2996. {
  2997. for(int i = 0; i < 4; i += i_step)
  2998. {
  2999. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  3000. }
  3001. i_base += 4;
  3002. }
  3003. // Do another 4-sample block for a total of 128 max samples.
  3004. if(samples - i_base > 0)
  3005. {
  3006. for(int i = 0; i < 4; i += i_step)
  3007. {
  3008. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  3009. }
  3010. }
  3011. #else
  3012. for(int i = 0; i < samples; i += i_step)
  3013. {
  3014. VERTICAL_SINC_RESAMPLE_LOOP_BODY;
  3015. }
  3016. #endif
  3017. // Normalize so the weight_sum == 1.0, and return:
  3018. const float2 weight_sum_reduce = weight_sum.xy + weight_sum.zw;
  3019. const float3 scalar_weight_sum = float3(weight_sum_reduce.x +
  3020. weight_sum_reduce.y);
  3021. return (pixel_color/scalar_weight_sum);
  3022. }
  3023. float3 downsample_horizontal_sinc_tiled(const sampler2D tex,
  3024. const float2 tex_uv, const float2 tex_size, const float dr,
  3025. const float magnification_scale, const float tile_size_uv_r)
  3026. {
  3027. // Differences from downsample_horizontal_sinc_tiled:
  3028. // 1.) The dr and tile_size_uv_r parameters are not static consts.
  3029. // 2.) The "vertical" parameter to get_first_texel_tile_uv_and_dist is
  3030. // set to false instead of true.
  3031. // 3.) The horizontal version of the loop body is used.
  3032. // TODO: If we can get guaranteed compile-time dead code elimination,
  3033. // we can combine the vertical/horizontal downsampling functions by:
  3034. // 1.) Add an extra static const bool parameter called "vertical."
  3035. // 2.) Supply it with the result of get_first_texel_tile_uv_and_dist().
  3036. // 3.) Use a conditional assignment in the loop body macro. This is the
  3037. // tricky part: We DO NOT want to incur the extra conditional
  3038. // assignment in the inner loop at runtime!
  3039. // The "r" in "dr," "tile_size_uv_r," etc. refers to the dimension
  3040. // we're resizing along, e.g. "dx" in this case.
  3041. #ifdef USE_SINGLE_STATIC_LOOP
  3042. // If we have to load all samples, we might as well use them.
  3043. static const int samples = int(max_sinc_resize_samples_m4);
  3044. #else
  3045. const int samples = int(get_dynamic_loop_size(magnification_scale));
  3046. #endif
  3047. // Get the first sample location (scalar tile uv coord along resized
  3048. // dimension) and distance from the output location (in texels):
  3049. const float input_tiles_per_texture_r = 1.0/tile_size_uv_r;
  3050. // false = horizontal resize:
  3051. const float2 first_texel_tile_r_and_dist = get_first_texel_tile_uv_and_dist(
  3052. tex_uv, tex_size, dr, input_tiles_per_texture_r, samples, false);
  3053. const float4 first_texel_tile_uv_rrrr = first_texel_tile_r_and_dist.xxxx;
  3054. const float4 first_dist_unscaled = first_texel_tile_r_and_dist.yyyy;
  3055. // Get the tile sample offset:
  3056. const float tile_dr = dr * input_tiles_per_texture_r;
  3057. // Sum up each weight and weighted sample color, varying the looping
  3058. // strategy based on our expected dynamic loop capabilities. See the
  3059. // loop body macros above.
  3060. int i_base = 0;
  3061. float4 weight_sum = float4(0.0);
  3062. float3 pixel_color = float3(0.0);
  3063. static const int i_step = 4;
  3064. #ifdef BREAK_LOOPS_INTO_PIECES
  3065. if(samples - i_base >= 64)
  3066. {
  3067. for(int i = 0; i < 64; i += i_step)
  3068. {
  3069. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  3070. }
  3071. i_base += 64;
  3072. }
  3073. if(samples - i_base >= 32)
  3074. {
  3075. for(int i = 0; i < 32; i += i_step)
  3076. {
  3077. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  3078. }
  3079. i_base += 32;
  3080. }
  3081. if(samples - i_base >= 16)
  3082. {
  3083. for(int i = 0; i < 16; i += i_step)
  3084. {
  3085. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  3086. }
  3087. i_base += 16;
  3088. }
  3089. if(samples - i_base >= 8)
  3090. {
  3091. for(int i = 0; i < 8; i += i_step)
  3092. {
  3093. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  3094. }
  3095. i_base += 8;
  3096. }
  3097. if(samples - i_base >= 4)
  3098. {
  3099. for(int i = 0; i < 4; i += i_step)
  3100. {
  3101. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  3102. }
  3103. i_base += 4;
  3104. }
  3105. // Do another 4-sample block for a total of 128 max samples.
  3106. if(samples - i_base > 0)
  3107. {
  3108. for(int i = 0; i < 4; i += i_step)
  3109. {
  3110. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  3111. }
  3112. }
  3113. #else
  3114. for(int i = 0; i < samples; i += i_step)
  3115. {
  3116. HORIZONTAL_SINC_RESAMPLE_LOOP_BODY;
  3117. }
  3118. #endif
  3119. // Normalize so the weight_sum == 1.0, and return:
  3120. const float2 weight_sum_reduce = weight_sum.xy + weight_sum.zw;
  3121. const float3 scalar_weight_sum = float3(weight_sum_reduce.x +
  3122. weight_sum_reduce.y);
  3123. return (pixel_color/scalar_weight_sum);
  3124. }
  3125. //////////////////////////// TILE SIZE CALCULATION ///////////////////////////
  3126. float2 get_resized_mask_tile_size(const float2 estimated_viewport_size,
  3127. const float2 estimated_mask_resize_output_size,
  3128. const bool solemnly_swear_same_inputs_for_every_pass)
  3129. {
  3130. // Requires: The following global constants must be defined according to
  3131. // certain constraints:
  3132. // 1.) mask_resize_num_triads: Must be high enough that our
  3133. // mask sampling method won't have artifacts later
  3134. // (long story; see derived-settings-and-constants.h)
  3135. // 2.) mask_resize_src_lut_size: Texel size of our mask LUT
  3136. // 3.) mask_triads_per_tile: Num horizontal triads in our LUT
  3137. // 4.) mask_min_allowed_triad_size: User setting (the more
  3138. // restrictive it is, the faster the resize will go)
  3139. // 5.) mask_min_allowed_tile_size_x < mask_resize_src_lut_size.x
  3140. // 6.) mask_triad_size_desired_{runtime, static}
  3141. // 7.) mask_num_triads_desired_{runtime, static}
  3142. // 8.) mask_specify_num_triads must be 0.0/1.0 (false/true)
  3143. // The function parameters must be defined as follows:
  3144. // 1.) estimated_viewport_size == (final viewport size);
  3145. // If mask_specify_num_triads is 1.0/true and the viewport
  3146. // estimate is wrong, the number of triads will differ from
  3147. // the user's preference by about the same factor.
  3148. // 2.) estimated_mask_resize_output_size: Must equal the
  3149. // output size of the MASK_RESIZE pass.
  3150. // Exception: The x component may be estimated garbage if
  3151. // and only if the caller throws away the x result.
  3152. // 3.) solemnly_swear_same_inputs_for_every_pass: Set to false,
  3153. // unless you can guarantee that every call across every
  3154. // pass will use the same sizes for the other parameters.
  3155. // When calling this across multiple passes, always use the
  3156. // same y viewport size/scale, and always use the same x
  3157. // viewport size/scale when using the x result.
  3158. // Returns: Return the final size of a manually resized mask tile, after
  3159. // constraining the desired size to avoid artifacts. Under
  3160. // unusual circumstances, tiles may become stretched vertically
  3161. // (see wall of text below).
  3162. // Stated tile properties must be correct:
  3163. static const float tile_aspect_ratio_inv =
  3164. mask_resize_src_lut_size.y/mask_resize_src_lut_size.x;
  3165. static const float tile_aspect_ratio = 1.0/tile_aspect_ratio_inv;
  3166. static const float2 tile_aspect = float2(1.0, tile_aspect_ratio_inv);
  3167. // If mask_specify_num_triads is 1.0/true and estimated_viewport_size.x is
  3168. // wrong, the user preference will be misinterpreted:
  3169. const float desired_tile_size_x = mask_triads_per_tile * lerp(
  3170. mask_triad_size_desired,
  3171. estimated_viewport_size.x / mask_num_triads_desired,
  3172. mask_specify_num_triads);
  3173. if(get_mask_sample_mode() > 0.5)
  3174. {
  3175. // We don't need constraints unless we're sampling MASK_RESIZE.
  3176. return desired_tile_size_x * tile_aspect;
  3177. }
  3178. // Make sure we're not upsizing:
  3179. const float temp_tile_size_x =
  3180. min(desired_tile_size_x, mask_resize_src_lut_size.x);
  3181. // Enforce min_tile_size and max_tile_size in both dimensions:
  3182. const float2 temp_tile_size = temp_tile_size_x * tile_aspect;
  3183. static const float2 min_tile_size =
  3184. mask_min_allowed_tile_size * tile_aspect;
  3185. const float2 max_tile_size =
  3186. estimated_mask_resize_output_size / mask_resize_num_tiles;
  3187. const float2 clamped_tile_size =
  3188. clamp(temp_tile_size, min_tile_size, max_tile_size);
  3189. // Try to maintain tile_aspect_ratio. This is the tricky part:
  3190. // If we're currently resizing in the y dimension, the x components
  3191. // could be MEANINGLESS. (If estimated_mask_resize_output_size.x is
  3192. // bogus, then so is max_tile_size.x and clamped_tile_size.x.)
  3193. // We can't adjust the y size based on clamped_tile_size.x. If it
  3194. // clamps when it shouldn't, it won't clamp again when later passes
  3195. // call this function with the correct sizes, and the discrepancy will
  3196. // break the sampling coords in MASKED_SCANLINES. Instead, we'll limit
  3197. // the x size based on the y size, but not vice versa, unless the
  3198. // caller swears the parameters were the same (correct) in every pass.
  3199. // As a result, triads could appear vertically stretched if:
  3200. // a.) mask_resize_src_lut_size.x > mask_resize_src_lut_size.y: Wide
  3201. // LUT's might clamp x more than y (all provided LUT's are square)
  3202. // b.) true_viewport_size.x < true_viewport_size.y: The user is playing
  3203. // with a vertically oriented screen (not accounted for anyway)
  3204. // c.) mask_resize_viewport_scale.x < masked_resize_viewport_scale.y:
  3205. // Viewport scales are equal by default.
  3206. // If any of these are the case, you can fix the stretching by setting:
  3207. // mask_resize_viewport_scale.x = mask_resize_viewport_scale.y *
  3208. // (1.0 / min_expected_aspect_ratio) *
  3209. // (mask_resize_src_lut_size.x / mask_resize_src_lut_size.y)
  3210. const float x_tile_size_from_y =
  3211. clamped_tile_size.y * tile_aspect_ratio;
  3212. const float y_tile_size_from_x = lerp(clamped_tile_size.y,
  3213. clamped_tile_size.x * tile_aspect_ratio_inv,
  3214. float(solemnly_swear_same_inputs_for_every_pass));
  3215. const float2 reclamped_tile_size = float2(
  3216. min(clamped_tile_size.x, x_tile_size_from_y),
  3217. min(clamped_tile_size.y, y_tile_size_from_x));
  3218. // We need integer tile sizes in both directions for tiled sampling to
  3219. // work correctly. Use floor (to make sure we don't round up), but be
  3220. // careful to avoid a rounding bug where floor decreases whole numbers:
  3221. const float2 final_resized_tile_size =
  3222. floor(reclamped_tile_size + float2(FIX_ZERO(0.0)));
  3223. return final_resized_tile_size;
  3224. }
  3225. ///////////////////////// FINAL MASK SAMPLING HELPERS ////////////////////////
  3226. float4 get_mask_sampling_parameters(const float2 mask_resize_texture_size,
  3227. const float2 mask_resize_video_size, const float2 true_viewport_size,
  3228. out float2 mask_tiles_per_screen)
  3229. {
  3230. // Requires: 1.) Requirements of get_resized_mask_tile_size() must be
  3231. // met, particularly regarding global constants.
  3232. // The function parameters must be defined as follows:
  3233. // 1.) mask_resize_texture_size == MASK_RESIZE.texture_size
  3234. // if get_mask_sample_mode() is 0 (otherwise anything)
  3235. // 2.) mask_resize_video_size == MASK_RESIZE.video_size
  3236. // if get_mask_sample_mode() is 0 (otherwise anything)
  3237. // 3.) true_viewport_size == IN.output_size for a pass set to
  3238. // 1.0 viewport scale (i.e. it must be correct)
  3239. // Returns: Return a float4 containing:
  3240. // xy: tex_uv coords for the start of the mask tile
  3241. // zw: tex_uv size of the mask tile from start to end
  3242. // mask_tiles_per_screen is an out parameter containing the
  3243. // number of mask tiles that will fit on the screen.
  3244. // First get the final resized tile size. The viewport size and mask
  3245. // resize viewport scale must be correct, but don't solemnly swear they
  3246. // were correct in both mask resize passes unless you know it's true.
  3247. // (We can better ensure a correct tile aspect ratio if the parameters are
  3248. // guaranteed correct in all passes...but if we lie, we'll get inconsistent
  3249. // sizes across passes, resulting in broken texture coordinates.)
  3250. const float mask_sample_mode = get_mask_sample_mode();
  3251. const float2 mask_resize_tile_size = get_resized_mask_tile_size(
  3252. true_viewport_size, mask_resize_video_size, false);
  3253. if(mask_sample_mode < 0.5)
  3254. {
  3255. // Sample MASK_RESIZE: The resized tile is a fraction of the texture
  3256. // size and starts at a nonzero offset to allow for border texels:
  3257. const float2 mask_tile_uv_size = mask_resize_tile_size /
  3258. mask_resize_texture_size;
  3259. const float2 skipped_tiles = mask_start_texels/mask_resize_tile_size;
  3260. const float2 mask_tile_start_uv = skipped_tiles * mask_tile_uv_size;
  3261. // mask_tiles_per_screen must be based on the *true* viewport size:
  3262. mask_tiles_per_screen = true_viewport_size / mask_resize_tile_size;
  3263. return float4(mask_tile_start_uv, mask_tile_uv_size);
  3264. }
  3265. else
  3266. {
  3267. // If we're tiling at the original size (1:1 pixel:texel), redefine a
  3268. // "tile" to be the full texture containing many triads. Otherwise,
  3269. // we're hardware-resampling an LUT, and the texture truly contains a
  3270. // single unresized phosphor mask tile anyway.
  3271. static const float2 mask_tile_uv_size = float2(1.0);
  3272. static const float2 mask_tile_start_uv = float2(0.0);
  3273. if(mask_sample_mode > 1.5)
  3274. {
  3275. // Repeat the full LUT at a 1:1 pixel:texel ratio without resizing:
  3276. mask_tiles_per_screen = true_viewport_size/mask_texture_large_size;
  3277. }
  3278. else
  3279. {
  3280. // Hardware-resize the original LUT:
  3281. mask_tiles_per_screen = true_viewport_size / mask_resize_tile_size;
  3282. }
  3283. return float4(mask_tile_start_uv, mask_tile_uv_size);
  3284. }
  3285. }
  3286. /*
  3287. float2 fix_tiling_discontinuities_normalized(const float2 tile_uv,
  3288. float2 duv_dx, float2 duv_dy)
  3289. {
  3290. // Requires: 1.) duv_dx == ddx(tile_uv)
  3291. // 2.) duv_dy == ddy(tile_uv)
  3292. // 3.) tile_uv contains tile-relative uv coords in [0, 1],
  3293. // such that (0.5, 0.5) is the center of a tile, etc.
  3294. // ("Tile" can mean texture, the video embedded in the
  3295. // texture, or some other "tile" embedded in a texture.)
  3296. // Returns: Return new tile_uv coords that contain no discontinuities
  3297. // across a 2x2 pixel quad.
  3298. // Description:
  3299. // When uv coords wrap from 1.0 to 0.0, they create a discontinuity in the
  3300. // derivatives, which we assume happened if the absolute difference between
  3301. // any fragment in a 2x2 block is > ~half a tile. If the current block has
  3302. // a u or v discontinuity and the current fragment is in the first half of
  3303. // the tile along that axis (i.e. it wrapped from 1.0 to 0.0), add a tile
  3304. // to that coord to make the 2x2 block continuous. (It will now have a
  3305. // coord > 1.0 in the padding area beyond the tile.) This function takes
  3306. // derivatives as parameters so the caller can reuse them.
  3307. // In case we're using high-quality (nVidia-style) derivatives, ensure
  3308. // diagonically opposite fragments see each other for correctness:
  3309. duv_dx = abs(duv_dx) + abs(ddy(duv_dx));
  3310. duv_dy = abs(duv_dy) + abs(ddx(duv_dy));
  3311. const float2 pixel_in_first_half_tile = float2((tile_uv.x < 0.5),(tile_uv.y < 0.5));
  3312. const float2 jump_exists = float2(((duv_dx + duv_dy).x > 0.5),((duv_dx + duv_dy).y > 0.5));
  3313. return tile_uv + jump_exists * pixel_in_first_half_tile;
  3314. }
  3315. */
  3316. float2 convert_phosphor_tile_uv_wrap_to_tex_uv(const float2 tile_uv_wrap,
  3317. const float4 mask_tile_start_uv_and_size)
  3318. {
  3319. // Requires: 1.) tile_uv_wrap contains tile-relative uv coords, where the
  3320. // tile spans from [0, 1], such that (0.5, 0.5) is at the
  3321. // tile center. The input coords can range from [0, inf],
  3322. // and their fractional parts map to a repeated tile.
  3323. // ("Tile" can mean texture, the video embedded in the
  3324. // texture, or some other "tile" embedded in a texture.)
  3325. // 2.) mask_tile_start_uv_and_size.xy contains tex_uv coords
  3326. // for the start of the embedded tile in the full texture.
  3327. // 3.) mask_tile_start_uv_and_size.zw contains the [fractional]
  3328. // tex_uv size of the embedded tile in the full texture.
  3329. // Returns: Return tex_uv coords (used for texture sampling)
  3330. // corresponding to tile_uv_wrap.
  3331. if(get_mask_sample_mode() < 0.5)
  3332. {
  3333. // Manually repeat the resized mask tile to fill the screen:
  3334. // First get fractional tile_uv coords. Using frac/fmod on coords
  3335. // confuses anisotropic filtering; fix it as user options dictate.
  3336. // derived-settings-and-constants.h disables incompatible options.
  3337. #ifdef ANISOTROPIC_TILING_COMPAT_TILE_FLAT_TWICE
  3338. float2 tile_uv = frac(tile_uv_wrap * 0.5) * 2.0;
  3339. #else
  3340. float2 tile_uv = frac(tile_uv_wrap);
  3341. #endif
  3342. #ifdef ANISOTROPIC_TILING_COMPAT_FIX_DISCONTINUITIES
  3343. const float2 tile_uv_dx = ddx(tile_uv);
  3344. const float2 tile_uv_dy = ddy(tile_uv);
  3345. tile_uv = fix_tiling_discontinuities_normalized(tile_uv,
  3346. tile_uv_dx, tile_uv_dy);
  3347. #endif
  3348. // The tile is embedded in a padded FBO, and it may start at a
  3349. // nonzero offset if border texels are used to avoid artifacts:
  3350. const float2 mask_tex_uv = mask_tile_start_uv_and_size.xy +
  3351. tile_uv * mask_tile_start_uv_and_size.zw;
  3352. return mask_tex_uv;
  3353. }
  3354. else
  3355. {
  3356. // Sample from the input phosphor mask texture with hardware tiling.
  3357. // If we're tiling at the original size (mode 2), the "tile" is the
  3358. // whole texture, and it contains a large number of triads mapped with
  3359. // a 1:1 pixel:texel ratio. OTHERWISE, the texture contains a single
  3360. // unresized tile. tile_uv_wrap already has correct coords for both!
  3361. return tile_uv_wrap;
  3362. }
  3363. }
  3364. #endif // PHOSPHOR_MASK_RESIZING_H
  3365. ///////////////////////// END PHOSPHOR-MASK-RESIZING /////////////////////////
  3366. ///////////////////////////// END VERTEX-INCLUDES ////////////////////////////
  3367. #undef COMPAT_PRECISION
  3368. #undef COMPAT_TEXTURE
  3369. float bloom_approx_scale_x = targetSize.y / sourceSize[0].y;
  3370. const float max_viewport_size_x = 1080.0*1024.0*(4.0/3.0);
  3371. const float bloom_diff_thresh_ = 1.0/256.0;
  3372. // copied from bloom-functions.h
  3373. inline float get_min_sigma_to_blur_triad(const float triad_size,
  3374. const float thresh)
  3375. {
  3376. // Requires: 1.) triad_size is the final phosphor triad size in pixels
  3377. // 2.) thresh is the max desired pixel difference in the
  3378. // blurred triad (e.g. 1.0/256.0).
  3379. // Returns: Return the minimum sigma that will fully blur a phosphor
  3380. // triad on the screen to an even color, within thresh.
  3381. // This closed-form function was found by curve-fitting data.
  3382. // Estimate: max error = ~0.086036, mean sq. error = ~0.0013387:
  3383. return -0.05168 + 0.6113*triad_size -
  3384. 1.122*triad_size*sqrt(0.000416 + thresh);
  3385. // Estimate: max error = ~0.16486, mean sq. error = ~0.0041041:
  3386. //return 0.5985*triad_size - triad_size*sqrt(thresh)
  3387. }
  3388. void main() {
  3389. gl_Position = position;
  3390. vTexCoord = texCoord;
  3391. tex_uv = vTexCoord.xy * 1.0001;
  3392. // Get the uv sample distance between output pixels. Calculate dxdy like
  3393. // blurs/vertex-shader-blur-fast-vertical.h.
  3394. const float2 dxdy_scale = video_size/output_size;
  3395. const float2 dxdy = dxdy_scale/texture_size;
  3396. // This blur is vertical-only, so zero out the vertical offset:
  3397. bloom_dxdy = float2(0.0, dxdy.y);
  3398. // Calculate a runtime bloom_sigma in case it's needed:
  3399. const float mask_tile_size_x = get_resized_mask_tile_size(
  3400. output_size, output_size * mask_resize_viewport_scale, false).x;
  3401. bloom_sigma_runtime = get_min_sigma_to_blur_triad(
  3402. mask_tile_size_x / mask_triads_per_tile, bloom_diff_thresh_);
  3403. }