mask-resize-horizontal.fs 170 KB

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