mask-resize-horizontal.vs 172 KB

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