mask-resize-vertical.vs 171 KB

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