Guest User

Untitled

a guest
Oct 26th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 74.91 KB | None | 0 0
  1. // Post processing effect file
  2. // most shader code taken from SweetFX 1.5.1
  3.  
  4. // -------------------- Settings -----------------------------------------------
  5.  
  6. // Set to 1 for ON or 0 for OFF
  7. #define USE_BLOOM             0 //[0 or 1] Bloom : Makes bright lights bleed their light into their surroundings (relatively high performance cost)
  8. #define USE_HDR               0 //[0 or 1] HDR : Not actual HDR - It just tries to mimic an HDR look (relatively high performance cost)
  9. #define USE_LUMASHARPEN       1 //[0 or 1] LumaSharpen
  10. #define USE_VIBRANCE          0 //[0 or 1] Vibrance : Intelligently saturates (or desaturates if you use negative values) the pixels depending on their original saturation.
  11. #define USE_TONEMAP           0 //[0 or 1] Tonemap : Adjust gamma, exposure, saturation, bleach and defog. (may cause clipping)
  12. #define USE_CA                0 //[0 or 1] Chromatic aberration. You can only use Chromatic Aberration OR the Explosion Shader. Not both at the same time.
  13. #define USE_CURVES            0 //[0 or 1] Curves : Contrast adjustments using S-curves.
  14. #define USE_DITHER            0 //[0 or 1] Dither : Applies dithering to simulate more colors than your monitor can display. This lessens banding artifacts (mostly caused by Vignette)
  15. #define USE_ADVANCED_CRT      0 //[0 or 1] Advanced CRT : Simulates an old CRT TV display. Set gaussian blur along with it to get a halation effect
  16. #define USE_TECHNICOLOR       0 //[0 or 1] TECHNICOLOR : Attempts to mimic the look of an old movie using the Technicolor three-strip color process (Techicolor Process 4)
  17. #define USE_DPX               0 //[0 or 1] Cineon DPX : Should make the image look like it's been converted to DXP Cineon - basically it's another movie-like look similar to technicolor.
  18. #define USE_GRAIN             0 //[0 or 1] Film Grain: Adds an active graininess to the image.  Helps with banding and can add the illusion of detail
  19. #define USE_BORDER            0 //[0 or 1] Border : Makes the screenedge black as a workaround for the bright edge that forcing some AA modes sometimes causes.
  20. #define USE_SPLITSCREEN       0 //[0 or 1] Splitscreen : Enables the before-and-after splitscreen comparison mode.
  21. #define USE_SINCITY           0 //[0 or 1] SinCity : Make the game look like the movie Sin City. Use with Tonemap gamma, default value of 0.6.
  22. #define USE_LOTTES_CRT        0 //[0 or 1] Timothy Lottes' CRT (http://tinyurl.com/od5oyxn http://tinyurl.com/qboke3o)
  23. #define USE_CARTOON           1 //[0 or 1] Cartoon : "Toon"s the image(Interferes with SMAA, CRT, Bloom, HDR and Lumasharpen).
  24. #define USE_MONOCHROME        0 //[0 or 1] Monochrome : Monochrome makes the colors disappear.
  25.  
  26. // Bloom settings
  27.     #define BloomThreshold 20.25 // [0.00 to 50.00] Threshold for what is a bright light (that causes bloom) and what isn't.
  28.     #define BloomPower 1.900     // [0.00 to  8.00] Strength of the bloom
  29.     #define BloomWidth 0.035     // [0.00 to  1.00] Width of the bloom
  30.  
  31. // HDR settings
  32.     #define HDRPower 1.65        // [0.00 to 8.00] Strangely lowering this makes the image brighter
  33.     #define radius2  1.050        // [0.00 to 8.00] Raising this seems to make the effect stronger and also brighter
  34.  
  35. // LumaSharpen settings
  36.     #define sharp_strength 0.35   // [0.10 to 3.00] Strength of the sharpening
  37.     #define sharp_clamp    0.035  // [0.00 to 1.00] Limits maximum amount of sharpening a pixel recieves - Default is 0.035
  38.     #define pattern 2             // [1|2|3|4] Choose a sample pattern. 1 = Fast, 2 = Normal, 3 = Wider, 4 = Pyramid shaped.
  39.  
  40. // Vibrance settings
  41.     #define Vibrance     0.08     // [-1.00 to 1.00] Intelligently saturates (or desaturates if you use negative values) the pixels depending on their original saturation.
  42.     #define Vibrance_RGB_balance float3(1.00, 1.00, 1.00) // [-10.00 to 10.00,-10.00 to 10.00,-10.00 to 10.00] A per channel multiplier to the Vibrance strength so you can give more boost to certain colors over others
  43.  
  44. // Tonemap settings
  45.     #define Gamma 1.0                      //[0.000 to 2.000] Adjust midtones
  46.     #define Exposure 0.0                   //[-1.000 to 1.000] Adjust exposure
  47.     #define Saturation 0.0                 //[-1.000 to 1.000] Adjust saturation
  48.     #define Bleach 0.0                     //[0.000 to 1.000] Brightens the shadows and fades the colors
  49.     #define Defog 1.0                      //[0.000 to 1.000] How much of the color tint to remove
  50.     #define FogColor float3(0.0, 0.0, 0.0) //[0.00 to 2.55, 0.00 to 2.55, 0.00 to 2.55] What color to remove - default is blue
  51.  
  52. // Chromatic Abberation settings
  53.     #define outfocus  0.016                     //[0.00 to 1.000] How strong the effect should be.
  54.  
  55. // Curves settings
  56.     #define Curves_mode     2   //[0|1|2] Choose what to apply contrast to. 0 = Luma, 1 = Chroma, 2 = both Luma and Chroma. Default is 0 (Luma)
  57.     #define Curves_contrast 0.65 //[-1.00 to 1.00] The amount of contrast you want
  58.  
  59. // -- Advanced curve settings --
  60.     #define Curves_formula     3 //[1|2|3|4|5|6|7|8|9|10] The contrast s-curve you want to use.
  61.         //1 = Sine, 2 = Abs split, 3 = Smoothstep, 4 = Exp formula, 5 = Simplified Catmull-Rom (0,0,1,1), 6 = Perlins Smootherstep
  62.         //Note that Technicolor Cinestyle is practically identical to Sine, but runs slower. In fact I think the difference might only be due to rounding errors.
  63.         //I prefer 2 myself, but 3 is a nice alternative with a little more effect (but harsher on the highlight and shadows) and it's the fastest formula.
  64.  
  65. // Dither settings
  66.     #define dither_method      2  //[1 or 2] 1 = Ordered dithering (very good and very fast), 2 = Random dithering (different but slightly slower dithering)
  67.         //Note that the patterns used by Dither, makes an image harder to compress.
  68.         //This can make your screenshots and video recordings take up more space.
  69.  
  70. // Advanced CRT settings
  71.     #define CRTAmount            1.00    //[0.00 to 1.00]  Amount of CRT effect you want
  72.  
  73.     #define CRTResolutionX       (SCREEN_SIZE.x)  / 4    //[1 to 2048]     Original input width of the game.  Default is 1/4 horizontal screen resolution
  74.     #define CRTResolutionY       (SCREEN_SIZE.y) / 4    //[1 to 2048]     Original input height of the game.  Default is 1/4 vertical screen resolution
  75.     #define CRTgamma             2.2     //[0.0 to 4.0]    Gamma of simulated CRT (default 2.2)
  76.     #define CRTmonitorgamma      2.4     //[0.0 to 4.0]    Gamma of display monitor (typically 2.2 is correct)
  77.     #define CRTBrightness        1.2     //[1.0 to 3.0]    Used to boost brightness a little. Default is 1.0
  78.     #define CRTScanlineIntensity 2.0     //[2.0 to 4.0]    Scanlines intensity (use integer values preferably). Default is 2.0
  79.     #define CRTScanlineGaussian  1       //[0 or 1]        Use the "new nongaussian scanlines bloom effect". Default is on
  80.  
  81.     #define CRTCurvature         1       //[[0 or 1]          "Barrel effect" enabled (1) or off (0)
  82.     #define CRTCurvatureRadius   2.0     //[0.0 to 2.0]       Curvature Radius (only effective when Curvature is enabled). Default is 1.5
  83.     #define CRTCornerSize        0.0100  //[0.0000 to 0.0020] Higher values, more rounded corner. Default is 0.001
  84.     #define CRTDistance          2.00    //[0.00 to 4.00]     Simulated distance from viewer to monitor. Default is 2.00
  85.     #define CRTAngleX            0.00    //[-0.20 to 0.20]    Tilt angle in radians (X coordinates)
  86.     #define CRTAngleY           -0.15    //[-0.20 to 0.20]    Tilt angle in radians (Y coordinates). (Value of -0.15 gives the 'arcade tilt' look)
  87.     #define CRTOverScan          1.00    //[1.00 to 1.10]     Overscan (e.g. 1.02 for 2% overscan). Default is 1.01
  88.     #define CRTOversample        0       //[0 or 1]           Enable 3x oversampling of the beam profile (warning : performance hit)
  89.    
  90. // ----Advanced Advanced CRT settings
  91.     //Amount of R,G,B tint for even scanlines, and amount of R,G,B tint for the odd scanlines. 1.0 is unchanged color
  92.     #define R1          1       //[0.0 to 1.0]   Amount of red color for even scanlines
  93.     #define G1          0.7     //[0.0 to 1.0]   Amount of green color for even scanlines
  94.     #define B1          1       //[0.0 to 1.0]   Amount of blue color for even scanlines
  95.    
  96.     #define R2          0.7     //[0.0 to 1.0]   Amount of red color for odd scanlines
  97.     #define G2          1.0     //[0.0 to 1.0]   Amount of green color for odd scanlines
  98.     #define B2          0.7     //[0.0 to 1.0]   Amount of blue color for odd scanlines
  99.    
  100. // Technicolor settings
  101.     #define TechniAmount 0.46        //[0.00 to 1.00]
  102.     #define TechniPower  4.0         //[0.00 to 8.00]
  103.     #define redNegativeAmount   0.74 //[0.00 to 1.00]
  104.     #define greenNegativeAmount 0.83 //[0.00 to 1.00]
  105.     #define blueNegativeAmount  0.9  //[0.00 to 1.00]
  106.    
  107. // Cineon DPX settings
  108.     #define Red   9.0  //[1.0 to 15.0]
  109.     #define Green 9.0  //[1.0 to 15.0]
  110.     #define Blue  9.0  //[1.0 to 15.0]
  111.  
  112.     #define ColorGamma    1.0  //[0.1 to 2.5] Adjusts the colorfulness of the effect in a manner similar to Vibrance. 1.0 is neutral.
  113.     #define DPXSaturation 1.0  //[0.0 to 8.0] Adjust saturation of the effect. 1.0 is neutral.
  114.  
  115.     #define RedC   0.33  //[0.60 to 0.20]
  116.     #define GreenC 0.36  //[0.60 to 0.20]
  117.     #define BlueC  0.35  //[0.60 to 0.20]
  118.  
  119.     #define Blend 0.23    //[0.00 to 1.00] How strong the effect should be
  120.    
  121. // Grain Settings
  122.     #define GrainPower   0.07 //[0.00 to 1.00] Intensity of applied grain
  123.     #define GrainColored   0 //[0 or 1] Whether grain should be colored or not
  124.     #define GrainColorAmount   0.6//[0.00 to 1.00] Amount of color to add to grain
  125.     #define GrainSize   1.6//[1.50 to 2.50] Size of individual pieces of grain
  126.     #define GrainLuma   1.0//[0.00 to 1.00] Grain brightness
  127.    
  128. // Border settings
  129.     #define border_width float2(0,0)        //[0 to 2048, 0 to 2048] (X,Y)-width of the border. Measured in pixels. If this is set to 0,0 then the border_ratio will be used instead
  130.     #define border_ratio float(0.75 / 1.0)  //[0.1000 to 10.0000] Set the desired ratio for the visible area. You MUST use floating point - Integers do not work right.
  131.     //Examples that work: (1680.0 / 1050.0), (16.0 / 10.0), (1.6) Examples that does NOT work right: (1680 / 1050), (16 / 10)
  132.     #define border_color float3(0, 0, 0)    //[0 to 255, 0 to 255, 0 to 255] What color the border should be. In integer RGB colors, meaning 0,0,0 is black and 255,255,255 is full white.
  133.  
  134. // Splitscreen settings
  135.     #define splitscreen_mode   1  //[1|2|3|4|5|6]  1 = Vertical 50/50 split, 2 = Vertical 25/50/25 split, 3 = Vertical 50/50 angled split,
  136.         // 4 = Horizontal 50/50 split, 5 = Horizontal 25/50/25 split, 6 = Curvy vertical 50/50 split
  137.  
  138. // SinCity settings
  139.     #define sincity_strength  1.00 //[0.00 to 1.00] This setting is a placeholder and does nothing at the moment.
  140.    
  141. // Cartoon settings                          
  142.     #define CartoonPower      10.0  //[0.1 to 10.0] Amount of effect you want.
  143.     #define CartoonEdgeSlope  0.8  //[0.1 to 8.0]  Raise this to filter out fainter edges. You might need to increase the power to compensate. Whole numbers are faster.
  144.     #define CartoonColor        -1.0  //[-1.0 OR 1.0] Sets border color to either black or white.  -1 is black, 1 is white.  
  145.     #define CartoonThickness 1.5  //[0 and above]  Sets border thickness.  Looks best at values around 1.0 - 2.0.  At higher values, sharpening helps visuals look better.  
  146.    
  147. // Monochrome settings                    
  148.     #define Monochrome_conversion_values  float3(0.18,0.41,0.41)  //[-1.00 to 1.00] Percentage of RGB to include (should sum up to 1.00)
  149.    
  150. // -------------------- Interface -----------------------------------------------
  151.  
  152. texture2D thisframeTex;
  153.  
  154. static float2 rcpres = PIXEL_SIZE;
  155.  
  156. const float timer;
  157.  
  158. #ifndef USE_SRGB
  159. #define USE_SRGB true
  160. #endif
  161.  
  162. sampler s0 = sampler_state
  163. {
  164.     texture = <thisframeTex>;
  165.     AddressU = CLAMP;
  166.     AddressV = CLAMP;
  167.     MINFILTER = LINEAR;
  168.     MAGFILTER = LINEAR;
  169.     SRGBTexture = USE_SRGB;
  170. };
  171.  
  172. struct VSOUT
  173. {
  174.     float4 vertPos : POSITION;
  175.     float2 UVCoord : TEXCOORD0;
  176. };
  177.  
  178. struct VSIN
  179. {
  180.     float4 vertPos : POSITION0;
  181.     float2 UVCoord : TEXCOORD0;
  182. };
  183.  
  184. VSOUT FrameVS(VSIN IN)
  185. {
  186.     VSOUT OUT;
  187.     OUT.vertPos = IN.vertPos;
  188.     OUT.UVCoord = IN.UVCoord;
  189.     return OUT;
  190. }
  191.  
  192. // -------------------- Effects -----------------------------------------------
  193.  
  194. /*
  195.    _____________________
  196.  
  197.      LumaSharpen 1.4.1
  198.    _____________________
  199.  
  200.   by Christian Cann Schuldt Jensen ~ CeeJay.dk
  201.  
  202.   It blurs the original pixel with the surrounding pixels and then subtracts this blur to sharpen the image.
  203.   It does this in luma to avoid color artifacts and allows limiting the maximum sharpening to avoid or lessen halo artifacts.
  204.  
  205.   This is similar to using Unsharp Mask in Photoshop.
  206.  
  207.   Compiles with 3.0
  208. */
  209.  
  210. #ifndef offset_bias
  211. #define offset_bias 1.0
  212. #endif
  213.  
  214. #define px (rcpres.x)
  215. #define py (rcpres.y)
  216.  
  217.    /*-----------------------------------------------------------.
  218.   /                      Developer settings                     /
  219.   '-----------------------------------------------------------*/
  220. #define CoefLuma float3(0.2126, 0.7152, 0.0722)      // BT.709 & sRBG luma coefficient (Monitors and HD Television)
  221. //#define CoefLuma float3(0.299, 0.587, 0.114)       // BT.601 luma coefficient (SD Television)
  222. //#define CoefLuma float3(1.0/3.0, 1.0/3.0, 1.0/3.0) // Equal weight coefficient
  223.  
  224.    /*-----------------------------------------------------------.
  225.   /                          Main code                          /
  226.   '-----------------------------------------------------------*/
  227.  
  228. float4 LumaSharpenPass(float4 inputcolor, float2 tex )
  229. {
  230.   // -- Get the original pixel --
  231.   float3 ori = tex2D(s0, tex).rgb;       // ori = original pixel
  232.  
  233.   // -- Combining the strength and luma multipliers --
  234.   float3 sharp_strength_luma = (CoefLuma * sharp_strength); //I'll be combining even more multipliers with it later on
  235.  
  236.    /*-----------------------------------------------------------.
  237.   /                       Sampling patterns                     /
  238.   '-----------------------------------------------------------*/
  239.   //   [ NW,   , NE ] Each texture lookup (except ori)
  240.   //   [   ,ori,    ] samples 4 pixels
  241.   //   [ SW,   , SE ]
  242.  
  243.   // -- Pattern 1 -- A (fast) 7 tap gaussian using only 2+1 texture fetches.
  244.   #if pattern == 1
  245.  
  246.     // -- Gaussian filter --
  247.     //   [ 1/9, 2/9,    ]     [ 1 , 2 ,   ]
  248.     //   [ 2/9, 8/9, 2/9]  =  [ 2 , 8 , 2 ]
  249.     //   [    , 2/9, 1/9]     [   , 2 , 1 ]
  250.  
  251.     float3 blur_ori = tex2D(s0, tex + (float2(px,py) / 3.0) * offset_bias).rgb;  // North West
  252.     blur_ori += tex2D(s0, tex + (float2(-px,-py) / 3.0) * offset_bias).rgb; // South East
  253.  
  254.     //blur_ori += tex2D(s0, tex + float2(px,py) / 3.0 * offset_bias); // North East
  255.     //blur_ori += tex2D(s0, tex + float2(-px,-py) / 3.0 * offset_bias); // South West
  256.  
  257.     blur_ori /= 2;  //Divide by the number of texture fetches
  258.  
  259.     sharp_strength_luma *= 1.5; // Adjust strength to aproximate the strength of pattern 2
  260.  
  261.   #endif
  262.  
  263.   // -- Pattern 2 -- A 9 tap gaussian using 4+1 texture fetches.
  264.   #if pattern == 2
  265.  
  266.     // -- Gaussian filter --
  267.     //   [ .25, .50, .25]     [ 1 , 2 , 1 ]
  268.     //   [ .50,   1, .50]  =  [ 2 , 4 , 2 ]
  269.     //   [ .25, .50, .25]     [ 1 , 2 , 1 ]
  270.  
  271.  
  272.     float3 blur_ori = tex2D(s0, tex + float2(px,-py) * 0.5 * offset_bias).rgb; // South East
  273.     blur_ori += tex2D(s0, tex + float2(-px,-py) * 0.5 * offset_bias).rgb;  // South West
  274.     blur_ori += tex2D(s0, tex + float2(px,py) * 0.5 * offset_bias).rgb; // North East
  275.     blur_ori += tex2D(s0, tex + float2(-px,py) * 0.5 * offset_bias).rgb; // North West
  276.  
  277.     blur_ori *= 0.25;  // ( /= 4) Divide by the number of texture fetches
  278.  
  279.   #endif
  280.  
  281.   // -- Pattern 3 -- An experimental 17 tap gaussian using 4+1 texture fetches.
  282.   #if pattern == 3
  283.  
  284.     // -- Gaussian filter --
  285.     //   [   , 4 , 6 ,   ,   ]
  286.     //   [   ,16 ,24 ,16 , 4 ]
  287.     //   [ 6 ,24 ,   ,24 , 6 ]
  288.     //   [ 4 ,16 ,24 ,16 ,   ]
  289.     //   [   ,   , 6 , 4 ,   ]
  290.  
  291.     float3 blur_ori = tex2D(s0, tex + float2(0.4*px,-1.2*py)* offset_bias).rgb;  // South South East
  292.     blur_ori += tex2D(s0, tex + float2(-1.2*px,-0.4*py) * offset_bias).rgb; // West South West
  293.     blur_ori += tex2D(s0, tex + float2(1.2*px,0.4*py) * offset_bias).rgb; // East North East
  294.     blur_ori += tex2D(s0, tex + float2(-0.4*px,1.2*py) * offset_bias).rgb; // North North West
  295.  
  296.     blur_ori *= 0.25;  // ( /= 4) Divide by the number of texture fetches
  297.  
  298.     sharp_strength_luma *= 0.51;
  299.   #endif
  300.  
  301.   // -- Pattern 4 -- A 9 tap high pass (pyramid filter) using 4+1 texture fetches.
  302.   #if pattern == 4
  303.  
  304.     // -- Gaussian filter --
  305.     //   [ .50, .50, .50]     [ 1 , 1 , 1 ]
  306.     //   [ .50,    , .50]  =  [ 1 ,   , 1 ]
  307.     //   [ .50, .50, .50]     [ 1 , 1 , 1 ]
  308.  
  309.     float3 blur_ori = tex2D(s0, tex + float2(0.5 * px,-py * offset_bias)).rgb;  // South South East
  310.     blur_ori += tex2D(s0, tex + float2(offset_bias * -px,0.5 * -py)).rgb; // West South West
  311.     blur_ori += tex2D(s0, tex + float2(offset_bias * px,0.5 * py)).rgb; // East North East
  312.     blur_ori += tex2D(s0, tex + float2(0.5 * -px,py * offset_bias)).rgb; // North North West
  313.  
  314.     //blur_ori += (2 * ori); // Probably not needed. Only serves to lessen the effect.
  315.  
  316.     blur_ori /= 4.0;  //Divide by the number of texture fetches
  317.  
  318.     sharp_strength_luma *= 0.666; // Adjust strength to aproximate the strength of pattern 2
  319.   #endif
  320.  
  321.   // -- Pattern 8 -- A (slower) 9 tap gaussian using 9 texture fetches.
  322.   #if pattern == 8
  323.  
  324.     // -- Gaussian filter --
  325.     //   [ 1 , 2 , 1 ]
  326.     //   [ 2 , 4 , 2 ]
  327.     //   [ 1 , 2 , 1 ]
  328.  
  329.     half3 blur_ori = tex2D(s0, tex + float2(-px,py) * offset_bias).rgb; // North West
  330.     blur_ori += tex2D(s0, tex + float2(px,-py) * offset_bias).rgb;     // South East
  331.     blur_ori += tex2D(s0, tex + float2(-px,-py)  * offset_bias).rgb;  // South West
  332.     blur_ori += tex2D(s0, tex + float2(px,py) * offset_bias).rgb;    // North East
  333.  
  334.     half3 blur_ori2 = tex2D(s0, tex + float2(0,py) * offset_bias).rgb; // North
  335.     blur_ori2 += tex2D(s0, tex + float2(0,-py) * offset_bias).rgb;    // South
  336.     blur_ori2 += tex2D(s0, tex + float2(-px,0) * offset_bias).rgb;   // West
  337.     blur_ori2 += tex2D(s0, tex + float2(px,0) * offset_bias).rgb;   // East
  338.     blur_ori2 *= 2.0;
  339.  
  340.     blur_ori += blur_ori2;
  341.     blur_ori += (ori * 4); // Probably not needed. Only serves to lessen the effect.
  342.  
  343.     // dot()s with gaussian strengths here?
  344.  
  345.     blur_ori /= 16.0;  //Divide by the number of texture fetches
  346.  
  347.     //sharp_strength_luma *= 0.75; // Adjust strength to aproximate the strength of pattern 2
  348.   #endif
  349.  
  350.   // -- Pattern 9 -- A (slower) 9 tap high pass using 9 texture fetches.
  351.   #if pattern == 9
  352.  
  353.     // -- Gaussian filter --
  354.     //   [ 1 , 1 , 1 ]
  355.     //   [ 1 , 1 , 1 ]
  356.     //   [ 1 , 1 , 1 ]
  357.  
  358.     float3 blur_ori = tex2D(s0, tex + float2(-px,py) * offset_bias).rgb; // North West
  359.     blur_ori += tex2D(s0, tex + float2(px,-py) * offset_bias).rgb;     // South East
  360.     blur_ori += tex2D(s0, tex + float2(-px,-py)  * offset_bias).rgb;  // South West
  361.     blur_ori += tex2D(s0, tex + float2(px,py) * offset_bias).rgb;    // North East
  362.  
  363.     blur_ori += ori.rgb; // Probably not needed. Only serves to lessen the effect.
  364.  
  365.     blur_ori += tex2D(s0, tex + float2(0,py) * offset_bias).rgb;    // North
  366.     blur_ori += tex2D(s0, tex + float2(0,-py) * offset_bias).rgb;  // South
  367.     blur_ori += tex2D(s0, tex + float2(-px,0) * offset_bias).rgb; // West
  368.     blur_ori += tex2D(s0, tex + float2(px,0) * offset_bias).rgb; // East
  369.  
  370.     blur_ori /= 9;  //Divide by the number of texture fetches
  371.  
  372.     //sharp_strength_luma *= (8.0/9.0); // Adjust strength to aproximate the strength of pattern 2
  373.   #endif
  374.  
  375.  
  376.    /*-----------------------------------------------------------.
  377.   /                            Sharpen                          /
  378.   '-----------------------------------------------------------*/
  379.  
  380.   // -- Calculate the sharpening --
  381.   float3 sharp = ori - blur_ori;  //Subtracting the blurred image from the original image
  382.  
  383.   #if 0 //New experimental limiter .. not yet finished
  384.     float sharp_luma = dot(sharp, sharp_strength_luma); //Calculate the luma
  385.     sharp_luma = (abs(sharp_luma)*8.0) * exp(1.0-(abs(sharp_luma)*8.0)) * sign(sharp_luma) / 16.0; //I should probably move the strength modifier here
  386.  
  387.   #elif 0 //SweetFX 1.4 code
  388.     // -- Adjust strength of the sharpening --
  389.     float sharp_luma = dot(sharp, sharp_strength_luma); //Calculate the luma and adjust the strength
  390.  
  391.     // -- Clamping the maximum amount of sharpening to prevent halo artifacts --
  392.     sharp_luma = clamp(sharp_luma, -sharp_clamp, sharp_clamp);  //TODO Try a curve function instead of a clamp
  393.  
  394.   #else //SweetFX 1.5.1 code
  395.     // -- Adjust strength of the sharpening and clamp it--
  396.     float4 sharp_strength_luma_clamp = float4(sharp_strength_luma * (0.5 / sharp_clamp),0.5); //Roll part of the clamp into the dot
  397.  
  398.     //sharp_luma = saturate((0.5 / sharp_clamp) * sharp_luma + 0.5); //scale up and clamp
  399.     float sharp_luma = saturate(dot(float4(sharp,1.0), sharp_strength_luma_clamp)); //Calculate the luma, adjust the strength, scale up and clamp
  400.     sharp_luma = (sharp_clamp * 2.0) * sharp_luma - sharp_clamp; //scale down
  401.   #endif
  402.  
  403.   // -- Combining the values to get the final sharpened pixel   --
  404.   //float4 done = ori + sharp_luma;    // Add the sharpening to the original.
  405.   inputcolor.rgb = inputcolor.rgb + sharp_luma;    // Add the sharpening to the input color.
  406.  
  407.    /*-----------------------------------------------------------.
  408.   /                     Returning the output                    /
  409.   '-----------------------------------------------------------*/
  410.   #ifndef show_sharpen
  411.   #define show_sharpen 0
  412.   #endif
  413.   #if show_sharpen == 1
  414.     //inputcolor.rgb = abs(sharp * 4.0);
  415.     inputcolor.rgb = saturate(0.5 + (sharp_luma * 4)).rrr;
  416.   #endif
  417.  
  418.   return saturate(inputcolor);
  419.  
  420. }
  421.  
  422. // -------------------- Bloom -----------------------------------------------
  423.  
  424. float4 BloomPass(float4 ColorInput2, float2 Tex)
  425. {
  426.     float3 BlurColor2 = 0;
  427.     float3 Blurtemp = 0;
  428.     float MaxDistance = 8*BloomWidth;
  429.     float CurDistance = 0;
  430.    
  431.     float Samplecount = 25.0;
  432.    
  433.     float2 blurtempvalue = Tex * rcpres * BloomWidth;
  434.    
  435.     float2 BloomSample = float2(2.5,-2.5);
  436.     float2 BloomSampleValue;
  437.    
  438.     for(BloomSample.x = (2.5); BloomSample.x > -2.0; BloomSample.x = BloomSample.x - 1.0) // runs 5 times
  439.     {
  440.         BloomSampleValue.x = BloomSample.x * blurtempvalue.x;
  441.         float2 distancetemp = BloomSample.x * BloomSample.x * BloomWidth;
  442.        
  443.         for(BloomSample.y = (- 2.5); BloomSample.y < 2.0; BloomSample.y = BloomSample.y + 1.0) // runs 5 ( * 5) times
  444.         {
  445.             distancetemp.y = BloomSample.y * BloomSample.y;
  446.             CurDistance = (distancetemp.y * BloomWidth) + distancetemp.x;
  447.            
  448.             BloomSampleValue.y = BloomSample.y * blurtempvalue.y;
  449.             Blurtemp.rgb = tex2D(s0, float2(Tex + BloomSampleValue)).rgb;
  450.  
  451.             BlurColor2.rgb += lerp(Blurtemp.rgb,ColorInput2.rgb, sqrt(CurDistance / MaxDistance));
  452.         }
  453.     }
  454.     BlurColor2.rgb = (BlurColor2.rgb / (Samplecount - (BloomPower - BloomThreshold*5)));
  455.     float Bloomamount = (dot(ColorInput2.rgb,float3(0.299f, 0.587f, 0.114f)));
  456.     float3 BlurColor = BlurColor2.rgb * (BloomPower + 4.0);
  457.  
  458.     ColorInput2.rgb = lerp(ColorInput2.rgb,BlurColor.rgb, Bloomamount);
  459.     return saturate(ColorInput2);
  460. }
  461.  
  462. // -------------------- HDR -----------------------------------------------
  463.  
  464. float4 HDRPass(float4 colorInput, float2 Tex)
  465. {
  466.     float3 c_center = tex2D(s0, Tex).rgb;
  467.    
  468.     float radius1 = 0.793;
  469.     float3 bloom_sum1 = tex2D(s0, Tex + float2(1.5, -1.5) * radius1).rgb;
  470.     bloom_sum1 += tex2D(s0, Tex + float2(-1.5, -1.5) * radius1).rgb;
  471.     bloom_sum1 += tex2D(s0, Tex + float2(1.5, 1.5) * radius1).rgb;
  472.     bloom_sum1 += tex2D(s0, Tex + float2(-1.5, 1.5) * radius1).rgb;
  473.    
  474.     bloom_sum1 += tex2D(s0, Tex + float2(0, -2.5) * radius1).rgb;
  475.     bloom_sum1 += tex2D(s0, Tex + float2(0, 2.5) * radius1).rgb;
  476.     bloom_sum1 += tex2D(s0, Tex + float2(-2.5, 0) * radius1).rgb;
  477.     bloom_sum1 += tex2D(s0, Tex + float2(2.5, 0) * radius1).rgb;
  478.    
  479.     bloom_sum1 *= 0.005;
  480.    
  481.     float3 bloom_sum2 = tex2D(s0, Tex + float2(1.5, -1.5) * radius2).rgb;
  482.     bloom_sum2 += tex2D(s0, Tex + float2(-1.5, -1.5) * radius2).rgb;
  483.     bloom_sum2 += tex2D(s0, Tex + float2(1.5, 1.5) * radius2).rgb;
  484.     bloom_sum2 += tex2D(s0, Tex + float2(-1.5, 1.5) * radius2).rgb;
  485.  
  486.  
  487.     bloom_sum2 += tex2D(s0, Tex + float2(0, -2.5) * radius2).rgb;  
  488.     bloom_sum2 += tex2D(s0, Tex + float2(0, 2.5) * radius2).rgb;
  489.     bloom_sum2 += tex2D(s0, Tex + float2(-2.5, 0) * radius2).rgb;
  490.     bloom_sum2 += tex2D(s0, Tex + float2(2.5, 0) * radius2).rgb;
  491.  
  492.     bloom_sum2 *= 0.010;
  493.    
  494.     float dist = radius2 - radius1;
  495.    
  496.     float3 HDR = (c_center + (bloom_sum2 - bloom_sum1)) * dist;
  497.     float3 blend = HDR + colorInput.rgb;
  498.     colorInput.rgb = HDR + pow(blend, HDRPower); // pow - don't use fractions for HDRpower
  499.    
  500.     return saturate(colorInput);
  501. }
  502.  
  503. // -------------------- Vibrance -----------------------------------------------
  504.  
  505. float4 VibrancePass(float4 colorInput)
  506. {  
  507.     #define Vibrance_coeff float3(Vibrance_RGB_balance * Vibrance)
  508.  
  509.     float4 color = colorInput; //original input color
  510.     float3 lumCoeff = float3(0.212656, 0.715158, 0.072186);  //Values to calculate luma with
  511.  
  512.     float luma = dot(lumCoeff, color.rgb); //calculate luma (grey)
  513.  
  514.     float max_color = max(colorInput.r, max(colorInput.g,colorInput.b)); //Find the strongest color
  515.     float min_color = min(colorInput.r, min(colorInput.g,colorInput.b)); //Find the weakest color
  516.  
  517.     float color_saturation = max_color - min_color; //The difference between the two is the saturation
  518.  
  519.     color.rgb = lerp(luma, color.rgb, (1.0 + (Vibrance_coeff * (1.0 - (sign(Vibrance_coeff) * color_saturation)))));
  520.     return color;
  521. }
  522.  
  523. // -------------------------Tonemap--------------------------------------------
  524.  
  525. float4 TonemapPass(float4 colorInput)
  526. {
  527.     float3 color = colorInput.rgb;
  528.  
  529.     color = saturate(color - Defog * FogColor); // Defog
  530.    
  531.     color *= pow(2.0f, Exposure); // Exposure
  532.    
  533.     color = pow(color, Gamma);    // Gamma -- roll into the first gamma correction in main.h ?
  534.  
  535.     //#define BlueShift 0.00    //Blueshift
  536.     //float4 d = color * float4(1.05f, 0.97f, 1.27f, color.a);
  537.     //color = lerp(color, d, BlueShift);
  538.    
  539.     float3 lumCoeff = float3(0.2126, 0.7152, 0.0722);
  540.     float lum = dot(lumCoeff, color.rgb);
  541.    
  542.     float3 blend = lum.rrr; //dont use float3
  543.    
  544.     float L = saturate( 10.0 * (lum - 0.45) );
  545.    
  546.     float3 result1 = 2.0f * color.rgb * blend;
  547.     float3 result2 = 1.0f - 2.0f * (1.0f - blend) * (1.0f - color.rgb);
  548.    
  549.     float3 newColor = lerp(result1, result2, L);
  550.     //float A2 = Bleach * color.rgb; //why use a float for A2 here and then multiply by color.rgb (a float3)?
  551.     float3 A2 = Bleach * color.rgb; //
  552.     float3 mixRGB = (A2 * newColor.r, A2 * newColor.g, A2*newColor.b);
  553.    
  554.     color.rgb += ((1.0f - A2) * mixRGB);
  555.    
  556.     //float3 middlegray = float(color.r + color.g + color.b) / 3;
  557.     float3 middlegray = dot(color,(1.0/3.0)); //1fps slower than the original on nvidia, 2 fps faster on AMD
  558.    
  559.     float3 diffcolor = color - middlegray; //float 3 here
  560.     colorInput.rgb = (color + diffcolor * Saturation)/(1+(diffcolor*Saturation)); //saturation
  561.    
  562.     return colorInput;
  563. }
  564.  
  565.  
  566. // ------------------------- Chromatic Abberation --------------------------------------------
  567.  
  568. #define CHROMA_POW      35.0
  569.  
  570. float3 fvChroma = float3(0.995, 1.000, 1.005);
  571.  
  572. float4 ChromaticAberrationFocus(float2 tex, float outOfFocus)
  573. {
  574.     float3 chroma = pow(fvChroma, CHROMA_POW * outOfFocus);
  575.  
  576.     float2 tr = ((2.0 * tex - 1.0) * chroma.r) * 0.5 + 0.5;
  577.     float2 tg = ((2.0 * tex - 1.0) * chroma.g) * 0.5 + 0.5;
  578.     float2 tb = ((2.0 * tex - 1.0) * chroma.b) * 0.5 + 0.5;
  579.    
  580.     float3 color = float3(tex2D(s0, tr).r, tex2D(s0, tg).g, tex2D(s0, tb).b) * (1.0 - outOfFocus);
  581.    
  582.     return float4(color, 1.0);
  583. }
  584.  
  585. float4 CAPass(float4 colorInput, float2 tex)
  586. {
  587.     return ChromaticAberrationFocus(tex, outfocus);
  588. }
  589.  
  590. // ------------------------- Curves --------------------------------------------
  591.  
  592. float4 CurvesPass( float4 colorInput )
  593. {
  594.   float3 lumCoeff = float3(0.2126, 0.7152, 0.0722);  //Values to calculate luma with
  595.   float Curves_contrast_blend = Curves_contrast;
  596.   float PI = acos(-1); //3.1415926589
  597.  
  598. // --------------------------------- Separation of Luma and Chroma
  599.  
  600.   // -- Calculate Luma and Chroma if needed --
  601.   #if Curves_mode != 2
  602.  
  603.     //calculate luma (grey)
  604.     float luma = dot(lumCoeff, colorInput.rgb);
  605.  
  606.     //calculate chroma
  607.       float3 chroma = colorInput.rgb - luma;
  608.   #endif
  609.  
  610.   // -- Which value to put through the contrast formula? --
  611.   // I name it x because makes it easier to copy-paste to Graphtoy or Wolfram Alpha or another graphing program
  612.   #if Curves_mode == 2
  613.       float3 x = colorInput.rgb; //if the curve should be applied to both Luma and Chroma
  614.     #elif Curves_mode == 1
  615.       float3 x = chroma; //if the curve should be applied to Chroma
  616.       x = x * 0.5 + 0.5; //adjust range of Chroma from -1 -> 1 to 0 -> 1
  617.   #else // Curves_mode == 0
  618.     float x = luma; //if the curve should be applied to Luma
  619.   #endif
  620.  
  621. // --------------------------------- Contrast formulas
  622.  
  623.   // -- Curve 1 --
  624.   #if Curves_formula == 1
  625.     x = sin(PI * 0.5 * x); // Sin - 721 amd fps, +vign 536 nv
  626.     x *= x;
  627.    
  628.     //x = 0.5 - 0.5*cos(PI*x);
  629.     //x = 0.5 * -sin(PI * -x + (PI*0.5)) + 0.5;
  630.   #endif
  631.  
  632.   // -- Curve 2 --
  633.   #if Curves_formula == 2
  634.     x = x - 0.5;  
  635.     x = ( x / (0.5 + abs(x)) ) + 0.5;
  636.    
  637.     //x = ( (x - 0.5) / (0.5 + abs(x-0.5)) ) + 0.5;
  638.   #endif
  639.  
  640.   // -- Curve 3 --
  641.   #if Curves_formula == 3
  642.     //x = smoothstep(0.0,1.0,x); //smoothstep
  643.     x = x*x*(3.0-2.0*x); //faster smoothstep alternative - 776 amd fps, +vign 536 nv
  644.     //x = x - 2.0 * (x - 1.0) * x* (x- 0.5);  //2.0 is contrast. Range is 0.0 to 2.0
  645.   #endif
  646.  
  647.   // -- Curve 4 --
  648.   #if Curves_formula == 4
  649.     x = (1.0524 * exp(6.0 * x) - 1.05248) / (20.0855 + exp(6.0 * x)); //exp formula
  650.   #endif
  651.  
  652.   // -- Curve 5 --
  653.   #if Curves_formula == 5
  654.     //x = 0.5 * (x + 3.0 * x * x - 2.0 * x * x * x); //a simplified catmull-rom (0,0,1,1) - btw smoothstep can also be expressed as a simplified catmull-rom using (1,0,1,0)
  655.     //x = (0.5 * x) + (1.5 -x) * x*x; //estrin form - faster version
  656.     x = x * (x * (1.5-x) + 0.5); //horner form - fastest version
  657.  
  658.     Curves_contrast_blend = Curves_contrast * 2.0; //I multiply by two to give it a strength closer to the other curves.
  659.   #endif
  660.  
  661.     // -- Curve 6 --
  662.   #if Curves_formula == 6
  663.     x = x*x*x*(x*(x*6.0 - 15.0) + 10.0); //Perlins smootherstep
  664.     #endif
  665.  
  666.     // -- Curve 7 --
  667.   #if Curves_formula == 7
  668.     //x = ((x-0.5) / ((0.5/(4.0/3.0)) + abs((x-0.5)*1.25))) + 0.5;
  669.     x = x - 0.5;
  670.     x = x / ((abs(x)*1.25) + 0.375 ) + 0.5;
  671.     //x = ( (x-0.5) / ((abs(x-0.5)*1.25) + (0.5/(4.0/3.0))) ) + 0.5;
  672.   #endif
  673.  
  674.   // -- Curve 8 --
  675.   #if Curves_formula == 8
  676.     x = (x * (x * (x * (x * (x * (x * (1.6 * x - 7.2) + 10.8) - 4.2) - 3.6) + 2.7) - 1.8) + 2.7) * x * x; //Techicolor Cinestyle - almost identical to curve 1
  677.   #endif
  678.  
  679.   // -- Curve 9 --
  680.   #if Curves_formula == 9
  681.     x =  -0.5 * (x*2.0-1.0) * (abs(x*2.0-1.0)-2.0) + 0.5; //parabola
  682.   #endif
  683.  
  684.   // -- Curve 10 --
  685.   #if Curves_formula == 10 //Half-circles
  686.  
  687.     #if Curves_mode == 0
  688.       float xstep = step(x,0.5);
  689.         float xstep_shift = (xstep - 0.5);
  690.         float shifted_x = x + xstep_shift;
  691.     #else
  692.       float3 xstep = step(x,0.5);
  693.         float3 xstep_shift = (xstep - 0.5);
  694.         float3 shifted_x = x + xstep_shift;
  695.     #endif
  696.  
  697.     x = abs(xstep - sqrt(-shifted_x * shifted_x + shifted_x) ) - xstep_shift;
  698.  
  699.   //x = abs(step(x,0.5)-sqrt(-(x+step(x,0.5)-0.5)*(x+step(x,0.5)-0.5)+(x+step(x,0.5)-0.5)))-(step(x,0.5)-0.5); //single line version of the above
  700.    
  701.   //x = 0.5 + (sign(x-0.5)) * sqrt(0.25-(x-trunc(x*2))*(x-trunc(x*2))); //worse
  702.  
  703.   /* // if/else - even worse
  704.   if (x-0.5)
  705.   x = 0.5-sqrt(0.25-x*x);
  706.   else
  707.   x = 0.5+sqrt(0.25-(x-1)*(x-1));
  708.     */
  709.  
  710.   //x = (abs(step(0.5,x)-clamp( 1-sqrt(1-abs(step(0.5,x)- frac(x*2%1)) * abs(step(0.5,x)- frac(x*2%1))),0 ,1))+ step(0.5,x) )*0.5; //worst so far
  711.    
  712.     //TODO: Check if I could use an abs split instead of step. It might be more efficient
  713.    
  714.     Curves_contrast_blend = Curves_contrast * 0.5; //I divide by two to give it a strength closer to the other curves.
  715.   #endif
  716.  
  717.   // -- Curve 11 --
  718.   #if Curves_formula == 11 //Cubic catmull
  719.     float a = 1.00; //control point 1
  720.     float b = 0.00; //start point
  721.     float c = 1.00; //endpoint
  722.     float d = 0.20; //control point 2
  723.     x = 0.5 * ((-a + 3*b -3*c + d)*x*x*x + (2*a -5*b + 4*c - d)*x*x + (-a+c)*x + 2*b); //A customizable cubic catmull-rom spline
  724.   #endif
  725.  
  726.   // -- Curve 12 --
  727.   #if Curves_formula == 12 //Cubic Bezier spline
  728.     float a = 0.00; //start point
  729.     float b = 0.00; //control point 1
  730.     float c = 1.00; //control point 2
  731.     float d = 1.00; //endpoint
  732.  
  733.     float r  = (1-x);
  734.     float r2 = r*r;
  735.     float r3 = r2 * r;
  736.     float x2 = x*x;
  737.     float x3 = x2*x;
  738.     //x = dot(float4(a,b,c,d),float4(r3,3*r2*x,3*r*x2,x3));
  739.  
  740.     //x = a * r*r*r + r * (3 * b * r * x + 3 * c * x*x) + d * x*x*x;
  741.     //x = a*(1-x)*(1-x)*(1-x) +(1-x) * (3*b * (1-x) * x + 3 * c * x*x) + d * x*x*x;
  742.     x = a*(1-x)*(1-x)*(1-x) + 3*b*(1-x)*(1-x)*x + 3*c*(1-x)*x*x + d*x*x*x;
  743.   #endif
  744.  
  745.   // -- Curve 13 --
  746.   #if Curves_formula == 13 //Cubic Bezier spline - alternative implementation.
  747.     float3 a = float3(0.00,0.00,0.00); //start point
  748.     float3 b = float3(0.25,0.15,0.85); //control point 1
  749.     float3 c = float3(0.75,0.85,0.15); //control point 2
  750.     float3 d = float3(1.00,1.00,1.00); //endpoint
  751.  
  752.     float3 ab = lerp(a,b,x);           // point between a and b
  753.     float3 bc = lerp(b,c,x);           // point between b and c
  754.     float3 cd = lerp(c,d,x);           // point between c and d
  755.     float3 abbc = lerp(ab,bc,x);       // point between ab and bc
  756.     float3 bccd = lerp(bc,cd,x);       // point between bc and cd
  757.     float3 dest = lerp(abbc,bccd,x);   // point on the bezier-curve
  758.     x = dest;
  759.   #endif
  760.  
  761.   // -- Curve 14 --
  762.   #if Curves_formula == 14
  763.     x = 1.0 / (1.0 + exp(-(x * 10.0 - 5.0))); //alternative exp formula
  764.   #endif
  765.  
  766. // ---------------------------------Joining of Luma and Chroma
  767.  
  768.   #if Curves_mode == 2 //Both Luma and Chroma
  769.     float3 color = x;  //if the curve should be applied to both Luma and Chroma
  770.     colorInput.rgb = lerp(colorInput.rgb, color, Curves_contrast_blend); //Blend by Curves_contrast
  771.  
  772.   #elif Curves_mode == 1 //Only Chroma
  773.     x = x * 2.0 - 1.0; //adjust the Chroma range back to -1 -> 1
  774.     float3 color = luma + x; //Luma + Chroma
  775.     colorInput.rgb = lerp(colorInput.rgb, color, Curves_contrast_blend); //Blend by Curves_contrast
  776.  
  777.   #else // Curves_mode == 0 //Only Luma
  778.     x = lerp(luma, x, Curves_contrast_blend); //Blend by Curves_contrast
  779.     colorInput.rgb = x + chroma; //Luma + Chroma
  780.  
  781.   #endif
  782.  
  783.   //Return the result
  784.   return colorInput;
  785. }
  786.  
  787. // -------------------------Dither--------------------------------------------
  788.  
  789. #ifndef dither_method
  790.   #define dither_method 1
  791. #endif
  792.  
  793. float4 DitherPass( float4 colorInput, float2 tex )
  794. {
  795.    float3 color = colorInput.rgb;
  796.  
  797.    float dither_bit  = 8.0;  //Number of bits per channel. Should be 8 for most monitors.
  798.    
  799.    //color = (tex.x*0.3+0.1); //draw a gradient for testing.
  800.    //#define dither_method 2 //override method for testing purposes
  801.  
  802.   /*------------------------.
  803.   | :: Ordered Dithering :: |
  804.   '------------------------*/
  805.    #if dither_method == 1 // Ordered dithering
  806.      //Calculate grid position
  807.      float grid_position = frac( dot(tex, (SCREEN_SIZE * float2(1.0/16.0,10.0/36.0)  )+(0.25) ) );
  808.  
  809.      //Calculate how big the shift should be
  810.      float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0));
  811.  
  812.      //Shift the individual colors differently, thus making it even harder to see the dithering pattern
  813.      float3 dither_shift_RGB = float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  814.  
  815.      //modify shift acording to grid position.
  816.      dither_shift_RGB = lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  817.  
  818.      //shift the color by dither_shift
  819.      color.rgb += dither_shift_RGB;
  820.  
  821.  
  822.   /*-----------------------.
  823.   | :: Random Dithering :: |
  824.   '-----------------------*/
  825.    #elif dither_method == 2 //Random dithering
  826.  
  827.      //Pseudo Random Number Generator
  828.      // -- PRNG 1 - Reference --
  829.      float seed = dot(tex, float2(12.9898,78.233)); //I could add more salt here if I wanted to
  830.      float sine = sin(seed); //cos also works well. Sincos too if you want 2D noise.
  831.      float noise = frac(sine * 43758.5453 + tex.x); //tex.x is just some additional salt - it can be taken out.
  832.  
  833.      //Calculate how big the shift should be
  834.      float dither_shift = (1.0 / (pow(2,dither_bit) - 1.0)); // Using noise to determine shift. Will be 1/255 if set to 8-bit.
  835.      float dither_shift_half = (dither_shift * 0.5); // The noise should vary between +- 0.5
  836.      dither_shift = dither_shift * noise - dither_shift_half; // MAD
  837.  
  838.      //shift the color by dither_shift
  839.      color.rgb += float3(-dither_shift, dither_shift, -dither_shift); //subpixel dithering
  840.  
  841.   /*--------------------.
  842.   | :: New Dithering :: |
  843.   '--------------------*/
  844.   //#define dither_method 3
  845.    #elif dither_method == 3 // New Ordered dithering
  846.  
  847.      //Calculate grid position
  848.      float grid_position = frac(dot(tex,(SCREEN_SIZE) * float2(0.75,0.5) /*+ (0.00025)*/)); //(0.6,0.8) is good too - TODO : experiment with values
  849.  
  850.      //Calculate how big the shift should be
  851.      float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0)); // 0.25 seems good both when using math and when eyeballing it. So does 0.75 btw.
  852.      dither_shift = lerp(2.0 * dither_shift, -2.0 * dither_shift, grid_position); //shift acording to grid position.
  853.  
  854.      //shift the color by dither_shift
  855.      color.rgb += float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  856.  
  857.   /*-------------------------.
  858.   | :: A Dither Dithering :: |
  859.   '-------------------------*/
  860.   //#define dither_method 4
  861.   #elif dither_method == 4 // New Ordered dithering
  862.  
  863.   #define dither_pattern 11
  864.   #define dither_levels 254
  865.  
  866.   float x=tex.x * SCREEN_SIZE.x;// * 1.31;
  867.   float y=tex.y * SCREEN_SIZE.y;// * 1.31;
  868.  
  869.   //Calculate grid position
  870.   float c = frac(dot(tex,(SCREEN_SIZE) * float2(1.0/4.0,3.0/4.0) + (0.00025) )); //the + (0.00025) part is to avoid errors with the floating point math
  871.  
  872.   float mask;
  873.  
  874.   #if dither_pattern == 1
  875.     mask = ((x ^ y * 149) * 1234 & 511)/511.0; //requires bitwise XOR - doesn't work
  876.   #elif dither_pattern == 2
  877.     mask = (((x+c*17) ^ y * 149) * 1234 & 511)/511.0; //requires bitwise XOR - doesn't work
  878.   #elif dither_pattern == 3
  879.     mask = 256.0 * frac(((x + y * 237) * 119)/ 256.0 ) / 255.0 ;//1.00392 * frac(0.464844 * (x + 237.0 * y)); //256.0 * frac(((x + y * 237) * 119)/ 256.0 ) / 255.0
  880.   #elif dither_pattern == 4
  881.     mask = (256.0 * frac((((x+c*67.0) + y * 236.0) * 119.0) / 256.0)) / 255.0; //& 255 = 256 * frac(x / 256)
  882.   #elif dither_pattern == 5
  883.     mask = 0.5;
  884.   #elif dither_pattern == 6
  885.     mask = frac( dot(tex, float2(12.9898,78.233)) * 927.5453 );
  886.   #elif dither_pattern == 7
  887.     mask = frac( dot(tex, (SCREEN_SIZE * float2(1.0/7.0,9.0/17.0))+(0.00025) ) );
  888.   #elif dither_pattern == 8
  889.     mask = frac( dot(tex, (SCREEN_SIZE * float2(5.0/7.0,3.0/17.0))+(0.00025) ) );
  890.   #elif dither_pattern == 9
  891.     mask = frac( dot(tex, (SCREEN_SIZE * float2(1.0/4.0,3.0/5.0))+(0.000025) ) );
  892.   #elif dither_pattern == 10
  893.     mask = frac( dot(tex, (SCREEN_SIZE * float2(1.0/87.0,1.0/289.0))+(0.000025) ) ); //stylish pattern - but bad for dithering
  894.   #elif dither_pattern == 11
  895.     //mask = frac( dot(tex, (SCREEN_SIZE * float2(1.0/(floor(tex.y*10.0)/100.+16.0),87.0/289.0))+(0.000025) ) ); //
  896.     //mask = frac( dot(float4(tex,tex), float4((SCREEN_SIZE * float2(0.666/16.0,6.66/36.)),(SCREEN_SIZE * float2(0.3344/16.0,3.34/36.)) ) ) ); //
  897.       mask = frac( dot(tex, (SCREEN_SIZE * float2(1.0/16.0,10.0/36.0)  )+(0.25) ) ); //
  898. //(floor(tex.y*10.0)/100.0 + 3.0)
  899.   #else
  900.     //return input;
  901.   #endif
  902.  
  903.   color.rgb = floor(dither_levels * color.rgb + mask) / dither_levels;
  904.   color.rgb = mask.xxx;
  905.  
  906.   /*---------------------------------------.
  907.   | :: New Dithering - grid experiments :: |
  908.   '---------------------------------------*/
  909.   //#define dither_method 5
  910.    #elif dither_method == 5 // New Ordered dithering
  911.  
  912.      //Calculate grid position
  913.      float grid_position = frac(dot(tex,floor(SCREEN_SIZE * float2(-0.5,-0.9) ) /*- (0.00025)*/ )); //(0.6,0.8) is good too - TODO : experiment with values
  914.  
  915.      //Calculate grid position
  916.      grid_position = frac(dot(tex,floor(SCREEN_SIZE * float2(0.4,0.70)) /*+ grid_position*/ /*+ (0.00025)*/ )); //
  917.  
  918.      //Calculate how big the shift should be
  919.      float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0)); // 0.25 seems good both when using math and when eyeballing it. So does 0.75 btw.
  920.      dither_shift = lerp(2.0 * dither_shift, -2.0 * dither_shift, grid_position); //shift acording to grid position.
  921.  
  922.      //dither_shift = (2.0 * dither_shift) * grid_position + (2.0 * dither_shift) * grid_position;
  923.      //dither_shift = 4.0 * dither_shift * grid_position;
  924.  
  925.      //shift the color by dither_shift
  926.      //color.rgb += lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  927.  
  928.      color.rgb += float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  929.  
  930.   /*-------------------.
  931.   | :: Checkerboard :: |
  932.   '-------------------*/
  933.    #elif dither_method == 6 // Checkerboard Ordered dithering
  934.      //Calculate grid position
  935.      float grid_position = frac(dot(tex, SCREEN_SIZE * 0.5) + 0.25); //returns 0.25 and 0.75
  936.  
  937.      //Calculate how big the shift should be
  938.      float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0)); // 0.25 seems good both when using math and when eyeballing it. So does 0.75 btw.
  939.  
  940.      //Shift the individual colors differently, thus making it even harder to see the dithering pattern
  941.      float3 dither_shift_RGB = float3(dither_shift, -dither_shift, dither_shift); //subpixel dithering
  942.  
  943.      //modify shift acording to grid position.
  944.      dither_shift_RGB = lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  945.  
  946.      //shift the color by dither_shift
  947.      //color.rgb += lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position); //shift acording to grid position.
  948.      color.rgb += dither_shift_RGB;
  949.    
  950.    #endif
  951.  
  952.   /*---------------------------.
  953.   | :: Returning the output :: |
  954.   '---------------------------*/
  955.  
  956.    //color = (tex.x / 2.0); //draw a undithered gradient for testing.
  957.  
  958.    colorInput.rgb = color.rgb;
  959.  
  960.    return colorInput;
  961. }
  962.  
  963. // CRT shader
  964. //
  965. // Copyright (C) 2010-2012 cgwg, Themaister and DOLLS
  966. //
  967. // This program is free software; you can redistribute it and/or modify it
  968. // under the terms of the GNU General Public License as published by the Free
  969. // Software Foundation; either version 2 of the License, or (at your option)
  970. // any later version.
  971.  
  972. // Comment the next line to disable interpolation in linear gamma (and gain speed).
  973. //#define LINEAR_PROCESSING
  974.  
  975. // aspect ratio
  976. #define aspect float2(1.0, 0.75)
  977.  
  978. // Precalculate a bunch of useful values we'll need in the fragment
  979. // shader.
  980. #define sinangle sin(float2(CRTAngleX, CRTAngleY))
  981. #define cosangle cos(float2(CRTAngleX, CRTAngleY))
  982. #define stretch maxscale()
  983.  
  984. // Macros.
  985. #define FIX(c) max(abs(c), 1e-5);
  986. float PI = acos(-1); //#define PI 3.141592653589
  987.  
  988. // The size of one texel, in texture-coordinates.
  989. #define coone 1.0 / rubyTextureSize
  990.  
  991. #define mod_factor tex.x * rubyTextureSize.x * rubyOutputSize.x / rubyInputSize.x
  992.  
  993. #ifdef LINEAR_PROCESSING
  994. #       define TEX2D(c) pow(tex2D(s0, (c)), CRTgamma)
  995. #else
  996. #       define TEX2D(c) tex2D(s0, (c))
  997. #endif
  998.  
  999. float intersect(float2 xy)
  1000. {
  1001.   float A = dot(xy,xy) + CRTDistance * CRTDistance;
  1002.   float B = 2.0 * (CRTCurvatureRadius * (dot(xy, sinangle) - CRTDistance * cosangle.x * cosangle.y) - CRTDistance * CRTDistance);
  1003.   float C = CRTDistance * CRTDistance + 2.0 * CRTCurvatureRadius * CRTDistance * cosangle.x * cosangle.y;
  1004.   return (-B - sqrt(B * B -4.0 * A * C)) / (2.0 * A);
  1005. }
  1006.  
  1007. float2 bkwtrans(float2 xy)
  1008. {
  1009.   float c = intersect(xy);
  1010.   float2 _point = float2(c, c) * xy;
  1011.   _point -= float2(-CRTCurvatureRadius, -CRTCurvatureRadius) * sinangle;
  1012.   _point /= float2(CRTCurvatureRadius, CRTCurvatureRadius);
  1013.   float2 tang = sinangle / cosangle;
  1014.   float2 poc = _point / cosangle;
  1015.   float A = dot(tang, tang) + 1.0;
  1016.   float B = -2.0 * dot(poc, tang);
  1017.   float C = dot(poc, poc) - 1.0;
  1018.   float a = (-B + sqrt(B * B -4.0 * A * C)) / (2.0 * A);
  1019.   float2 uv = (_point - a * sinangle) / cosangle;
  1020.   float r = FIX(CRTCurvatureRadius * acos(a));
  1021.   return uv * r / sin(r / CRTCurvatureRadius);
  1022. }
  1023.  
  1024. float2 fwtrans(float2 uv)
  1025. {
  1026.   float r = FIX(sqrt(dot(uv, uv)));
  1027.   uv *= sin(r / CRTCurvatureRadius) / r;
  1028.   float x = 1.0 - cos(r / CRTCurvatureRadius);
  1029.   float D = CRTDistance / CRTCurvatureRadius + x * cosangle.x * cosangle.y + dot(uv, sinangle);
  1030.   return CRTDistance * (uv * cosangle - x * sinangle) / D;
  1031. }
  1032.  
  1033. float3 maxscale()
  1034. {
  1035.   float2 c = bkwtrans(-CRTCurvatureRadius * sinangle / (1.0 + CRTCurvatureRadius / CRTDistance * cosangle.x * cosangle.y));
  1036.   float2 a = float2(0.5, 0.5) * aspect;
  1037.   float2 lo = float2(fwtrans(float2(-a.x, c.y)).x,
  1038.                      fwtrans(float2(c.x,-a.y)).y) / aspect;
  1039.   float2 hi = float2(fwtrans(float2(+a.x, c.y)).x,
  1040.                      fwtrans(float2(c.x, +a.y)).y) / aspect;
  1041.   return float3((hi + lo) * aspect * 0.5, max(hi.x - lo.x, hi.y - lo.y));
  1042. }
  1043.  
  1044. float2 transform(float2 coord, float2 textureSize, float2 inputSize)
  1045. {
  1046.   coord *= textureSize / inputSize;
  1047.   coord = (coord - 0.5) * aspect * stretch.z + stretch.xy;
  1048.   return (bkwtrans(coord) / float2(CRTOverScan, CRTOverScan) / aspect + 0.5) * inputSize / textureSize;
  1049. }
  1050.  
  1051. float corner(float2 coord, float2 textureSize, float2 inputSize)
  1052. {
  1053.   coord *= textureSize / inputSize;
  1054.   coord = (coord - 0.5) * float2(CRTOverScan, CRTOverScan) + 0.5;
  1055.   coord = min(coord, 1.0 - coord) * aspect;
  1056.   float2 cdist = float2(CRTCornerSize, CRTCornerSize);
  1057.   coord = (cdist - min(coord, cdist));
  1058.   float dist = sqrt(dot(coord, coord));
  1059.   return clamp((cdist.x-dist) * 1000.0, 0.0, 1.0);
  1060. }
  1061.  
  1062. // Calculate the influence of a scanline on the current pixel.
  1063. //
  1064. // 'distance' is the distance in texture coordinates from the current
  1065. // pixel to the scanline in question.
  1066. // 'color' is the colour of the scanline at the horizontal location of
  1067. // the current pixel.
  1068. float4 scanlineWeights(float distance, float4 color)
  1069. {
  1070.   // "wid" controls the width of the scanline beam, for each RGB channel
  1071.   // The "weights" lines basically specify the formula that gives
  1072.   // you the profile of the beam, i.e. the intensity as
  1073.   // a function of distance from the vertical center of the
  1074.   // scanline. In this case, it is gaussian if width=2, and
  1075.   // becomes nongaussian for larger widths. Ideally this should
  1076.   // be normalized so that the integral across the beam is
  1077.   // independent of its width. That is, for a narrower beam
  1078.   // "weights" should have a higher peak at the center of the
  1079.   // scanline than for a wider beam.
  1080. #if CRTScanlineGaussian == 0
  1081.     float4 wid = 0.3 + 0.1 * pow(color, 3.0);
  1082.     float4 weights = float4(distance / wid);
  1083.     return 0.4 * exp(-weights * weights) / wid;
  1084. #else
  1085.     float4 wid = 2.0 + 2.0 * pow(color, 4.0);
  1086.     float calcdistance = distance / 0.3; // Optimization  ?
  1087.     //float4 weights = float4(distance / 0.3, distance / 0.3, distance / 0.3, distance / 0.3);
  1088.     float4 weights = float4(calcdistance, calcdistance, calcdistance, calcdistance);
  1089.     return 1.4 * exp(-pow(weights * rsqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
  1090. #endif
  1091. }
  1092.  
  1093. float4 AdvancedCRTPass( float4 colorInput, float2 tex )
  1094. {
  1095.     // Here's a helpful diagram to keep in mind while trying to
  1096.     // understand the code:
  1097.     //
  1098.     //  |      |      |      |      |
  1099.     // -------------------------------
  1100.     //  |      |      |      |      |
  1101.     //  |  01  |  11  |  21  |  31  | <-- current scanline
  1102.     //  |      | @    |      |      |
  1103.     // -------------------------------
  1104.     //  |      |      |      |      |
  1105.     //  |  02  |  12  |  22  |  32  | <-- next scanline
  1106.     //  |      |      |      |      |
  1107.     // -------------------------------
  1108.     //  |      |      |      |      |
  1109.     //
  1110.     // Each character-cell represents a pixel on the output
  1111.     // surface, "@" represents the current pixel (always somewhere
  1112.     // in the bottom half of the current scan-line, or the top-half
  1113.     // of the next scanline). The grid of lines represents the
  1114.     // edges of the texels of the underlying texture.
  1115.  
  1116.     float2 Resolution = float2(CRTResolutionX, CRTResolutionY);
  1117.     float2 rubyTextureSize = Resolution;
  1118.     float2 rubyInputSize = Resolution;
  1119.     float2 rubyOutputSize = SCREEN_SIZE;
  1120.    
  1121. #if CRTCurvature == 1
  1122.     float2 xy = transform(tex, rubyTextureSize, rubyInputSize);
  1123. #else
  1124.     float2 xy = tex;
  1125. #endif
  1126.     float cval = corner(xy, rubyTextureSize, rubyInputSize);
  1127.    
  1128.     // Of all the pixels that are mapped onto the texel we are
  1129.     // currently rendering, which pixel are we currently rendering?
  1130.     float2 ratio_scale = xy * rubyTextureSize - 0.5;
  1131.    
  1132. #if CRTOversample == 1
  1133.     float filter = fwidth(ratio_scale.y);
  1134. #endif  
  1135.     float2 uv_ratio = frac(ratio_scale);
  1136.  
  1137.     // Snap to the center of the underlying texel.
  1138.     xy = (floor(ratio_scale) + 0.5) / rubyTextureSize;
  1139.  
  1140.     // Calculate Lanczos scaling coefficients describing the effect
  1141.     // of various neighbour texels in a scanline on the current
  1142.     // pixel.
  1143.     float4 coeffs = PI * float4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x);
  1144.  
  1145.     // Prevent division by zero.
  1146.     coeffs = FIX(coeffs);
  1147.  
  1148.     // Lanczos2 kernel.
  1149.     coeffs = 2.0 * sin(coeffs) * sin(coeffs / 2.0) / (coeffs * coeffs);
  1150.  
  1151.     // Normalize.
  1152.     coeffs /= dot(coeffs, 1.0);
  1153.    
  1154.     // Calculate the effective colour of the current and next
  1155.     // scanlines at the horizontal location of the current pixel,
  1156.     // using the Lanczos coefficients above.
  1157.     float4 col  = clamp(mul(coeffs, float4x4(
  1158.             TEX2D(xy + float2(-coone.x, 0.0)),
  1159.             TEX2D(xy),
  1160.             TEX2D(xy + float2(coone.x, 0.0)),
  1161.             TEX2D(xy + float2(2.0 * coone.x, 0.0)))),
  1162.             0.0, 1.0);
  1163.     float4 col2 = clamp(mul(coeffs, float4x4(
  1164.             TEX2D(xy + float2(-coone.x, coone.y)),
  1165.             TEX2D(xy + float2(0.0, coone.y)),
  1166.             TEX2D(xy + coone),
  1167.             TEX2D(xy + float2(2.0 * coone.x, coone.y)))),
  1168.             0.0, 1.0);
  1169.  
  1170. #ifndef LINEAR_PROCESSING
  1171.     col  = pow(col , CRTgamma);
  1172.     col2 = pow(col2, CRTgamma);
  1173. #endif
  1174.  
  1175.     // Calculate the influence of the current and next scanlines on
  1176.     // the current pixel.
  1177.     float4 weights  = scanlineWeights(uv_ratio.y, col);
  1178.     float4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
  1179.    
  1180. #if CRTOversample == 1
  1181.     uv_ratio.y = uv_ratio.y + 1.0 / 3.0 * filter;
  1182.     weights = (weights + scanlineWeights(uv_ratio.y, col)) / 3.0;
  1183.     weights2 = (weights2 + scanlineWeights(abs(1.0 - uv_ratio.y), col2)) / 3.0;
  1184.     uv_ratio.y = uv_ratio.y - 2.0 / 3.0 * filter;
  1185.     weights = weights + scanlineWeights(abs(uv_ratio.y), col) / 3.0;
  1186.     weights2 = weights2 + scanlineWeights(abs(1.0 - uv_ratio.y), col2) / 3.0;
  1187. #endif
  1188.    
  1189.     float3 mul_res  = (col * weights + col2 * weights2).rgb * float3(cval, cval, cval);
  1190.  
  1191.     // dot-mask emulation:
  1192.     // Output pixels are alternately tinted green and magenta.
  1193.     float3 dotMaskWeights = lerp(float3(R1, G1, B1),
  1194.                                  float3(R2, G2, B2),
  1195.                                  floor(mod_factor % CRTScanlineIntensity));
  1196.  
  1197.     mul_res *= dotMaskWeights * float3(0.83, 0.83, 0.83) * CRTBrightness;
  1198.  
  1199.     // Convert the image gamma for display on our output device.
  1200.     mul_res = pow(mul_res, 1.0 / CRTmonitorgamma);
  1201.  
  1202.     //return saturate(lerp(colorInput, float4(mul_res, 1.0), CRTAmount));
  1203.     colorInput.rgb = lerp(colorInput.rgb, mul_res, CRTAmount);
  1204.     return saturate(colorInput);
  1205. }
  1206.  
  1207. // ------------------------- Technicolor --------------------------------------------
  1208.  
  1209. #define cyanfilter float3(0.0, 1.30, 1.0)
  1210. #define magentafilter float3(1.0, 0.0, 1.05)
  1211. #define yellowfilter float3(1.6, 1.6, 0.05)
  1212.  
  1213. #define redorangefilter float2(1.05, 0.620) //RG_
  1214. #define greenfilter float2(0.30, 1.0)       //RG_
  1215. #define magentafilter2 magentafilter.rb     //R_B
  1216.  
  1217. float4 TechnicolorPass( float4 colorInput )
  1218. {
  1219.     float3 tcol = colorInput.rgb;
  1220.    
  1221.   float2 rednegative_mul   = tcol.rg * (1.0 / (redNegativeAmount * TechniPower));
  1222.     float2 greennegative_mul = tcol.rg * (1.0 / (greenNegativeAmount * TechniPower));
  1223.     float2 bluenegative_mul  = tcol.rb * (1.0 / (blueNegativeAmount * TechniPower));
  1224.    
  1225.   float rednegative   = dot( redorangefilter, rednegative_mul );
  1226.     float greennegative = dot( greenfilter, greennegative_mul );
  1227.     float bluenegative  = dot( magentafilter2, bluenegative_mul );
  1228.    
  1229.     float3 redoutput   = rednegative.rrr + cyanfilter;
  1230.     float3 greenoutput = greennegative.rrr + magentafilter;
  1231.     float3 blueoutput  = bluenegative.rrr + yellowfilter;
  1232.    
  1233.     float3 result = redoutput * greenoutput * blueoutput;
  1234.     colorInput.rgb = lerp(tcol, result, TechniAmount);
  1235.     return colorInput;
  1236. }
  1237.  
  1238. // ------------------------- DPX/Cineon --------------------------------------------
  1239.  
  1240. static float3x3 RGB =
  1241. {
  1242. 2.67147117265996,-1.26723605786241,-0.410995602172227,
  1243. -1.02510702934664,1.98409116241089,0.0439502493584124,
  1244. 0.0610009456429445,-0.223670750812863,1.15902104167061
  1245. };
  1246.  
  1247. static float3x3 XYZ =
  1248. {
  1249. 0.500303383543316,0.338097573222739,0.164589779545857,
  1250. 0.257968894274758,0.676195259144706,0.0658358459823868,
  1251. 0.0234517888692628,0.1126992737203,0.866839673124201
  1252. };
  1253.  
  1254. float4 DPXPass(float4 InputColor) : COLOR0 {
  1255.  
  1256.     float DPXContrast = 0.1;
  1257.  
  1258.     float DPXGamma = 1.0;
  1259.  
  1260.     float RedCurve = Red;
  1261.     float GreenCurve = Green;
  1262.     float BlueCurve = Blue;
  1263.  
  1264.     float3 B = InputColor.rgb;
  1265.     //float3 Bn = B; // I used InputColor.rgb instead.
  1266.  
  1267.     B = pow(B, 1.0/DPXGamma);
  1268.  
  1269.     B.r = pow(B.r, 1.00);
  1270.     B.g = pow(B.g, 1.00);
  1271.     B.b = pow(B.b, 1.00);
  1272.  
  1273.         B = (B * (1.0 - DPXContrast)) + DPXContrast / 2.0;
  1274.    
  1275.     B.r = (1.0 /(1.0 + exp(- RedCurve * (B.r - RedC))) - (1.0 / (1.0 + exp(RedCurve / 2.0))))/(1.0 - 2.0 * (1.0 / (1.0 + exp(RedCurve / 2.0))));                
  1276.     B.g = (1.0 /(1.0 + exp(- GreenCurve * (B.g - GreenC))) - (1.0 / (1.0 + exp(GreenCurve / 2.0))))/(1.0 - 2.0 * (1.0 / (1.0 + exp(GreenCurve / 2.0))));                
  1277.     B.b = (1.0 /(1.0 + exp(- BlueCurve * (B.b - BlueC))) - (1.0 / (1.0 + exp(BlueCurve / 2.0))))/(1.0 - 2.0 * (1.0 / (1.0 + exp(BlueCurve / 2.0))));                    
  1278.  
  1279.         //TODO use faster code for conversion between RGB/HSV  -  see http://www.chilliant.com/rgb2hsv.html
  1280.        float value = max(max(B.r, B.g), B.b);
  1281.        float3 color = B / value;
  1282.    
  1283.        color = pow(color, 1.0/ColorGamma);
  1284.    
  1285.        float3 c0 = color * value;
  1286.  
  1287.        c0 = mul(XYZ, c0);
  1288.  
  1289.        float luma = dot(c0, float3(0.30, 0.59, 0.11)); //Use BT 709 instead?
  1290.        float3 chroma = c0 - luma;
  1291.  
  1292.        c0 = luma + chroma * DPXSaturation;
  1293.        c0 = mul(RGB, c0);
  1294.    
  1295.     InputColor.rgb = lerp(InputColor.rgb, c0, Blend); //as long as Blend is always 0 we don't really need to lerp. The compiler *should* be smart enough to optimize this though (check to be sure)
  1296.  
  1297.     return InputColor;
  1298. }
  1299.  
  1300. /* ------------------------- Grain --------------------------------------------
  1301.  
  1302. Film Grain post-process shader v1.1
  1303. Martins Upitis (martinsh) devlog-martinsh.blogspot.com
  1304. 2013
  1305. --------------------------
  1306. This work is licensed under a Creative Commons Attribution 3.0 Unported License.
  1307. So you are free to share, modify and adapt it for your needs, and even use it for commercial use.
  1308. I would also love to hear about a project you are using it.
  1309.  
  1310. Have fun,
  1311. Martins
  1312. ---------------------------
  1313. Ported to GeDoSaTo by Angelo Gonzalez
  1314. ---------------------------
  1315.  
  1316. Perlin noise shader by toneburst:
  1317. http://machinesdontcare.wordpress.com/2009/06/25/3d-perlin-noise-sphere-vertex-shader-sourcecode/
  1318. */
  1319.  
  1320. static const float permTexUnit = 1.0/256.0;        // Perm texture texel-size
  1321. static const float permTexUnitHalf = 0.5/256.0;    // Half perm texture texel-size
  1322.  
  1323. float width = SCREEN_SIZE.x;
  1324. float height = SCREEN_SIZE.y;
  1325.  
  1326. static const float grainamount = GrainPower;
  1327. float colored = GrainColored;
  1328. float coloramount = GrainColorAmount;
  1329. float grainsize = GrainSize;
  1330. float lumamount = GrainLuma;
  1331.  
  1332. float fract (float inp)
  1333. {
  1334.     return (inp-floor(inp));
  1335. }
  1336.  
  1337. float4 mix (float x, float y, float a)
  1338. {
  1339.     return x * (1 - a) + y * a;
  1340. }
  1341.    
  1342. //a random texture generator, but you can also use a pre-computed perturbation texture
  1343. float4 rnm(in float2 tc)
  1344. {
  1345.     float noise =  sin(dot(tc + float2(timer,timer),float2(12.9898,78.233))) * 43758.5453;
  1346.  
  1347.     float noiseR =  frac(noise)*2.0-1.0;
  1348.     float noiseG =  frac(noise*1.2154)*2.0-1.0;
  1349.     float noiseB =  frac(noise*1.3453)*2.0-1.0;
  1350.     float noiseA =  frac(noise*1.3647)*2.0-1.0;
  1351.    
  1352.     return float4(noiseR,noiseG,noiseB,noiseA);
  1353. }
  1354.  
  1355. float fade(in float t) {
  1356.     return t*t*t*(t*(t*6.0-15.0)+10.0);
  1357. }
  1358.  
  1359. float pnoise3D(in float3 p)
  1360. {
  1361.     float3 pi = permTexUnit*floor(p)+permTexUnitHalf; // Integer part, scaled so +1 moves permTexUnit texel
  1362.     // and offset 1/2 texel to sample texel centers
  1363.     float3 pf = frac(p);     // Fractional part for interpolation
  1364.  
  1365.     // Noise contributions from (x=0, y=0), z=0 and z=1
  1366.     float perm00 = rnm(pi.xy).a ;
  1367.     float3  grad000 = rnm(float2(perm00, pi.z)).rgb * 4.0 - 1.0;
  1368.     float n000 = dot(grad000, pf);
  1369.     float3  grad001 = rnm(float2(perm00, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  1370.     float n001 = dot(grad001, pf - float3(0.0, 0.0, 1.0));
  1371.  
  1372.     // Noise contributions from (x=0, y=1), z=0 and z=1
  1373.     float perm01 = rnm(pi.xy + float2(0.0, permTexUnit)).a ;
  1374.     float3  grad010 = rnm(float2(perm01, pi.z)).rgb * 4.0 - 1.0;
  1375.     float n010 = dot(grad010, pf - float3(0.0, 1.0, 0.0));
  1376.     float3  grad011 = rnm(float2(perm01, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  1377.     float n011 = dot(grad011, pf - float3(0.0, 1.0, 1.0));
  1378.  
  1379.     // Noise contributions from (x=1, y=0), z=0 and z=1
  1380.     float perm10 = rnm(pi.xy + float2(permTexUnit, 0.0)).a ;
  1381.     float3  grad100 = rnm(float2(perm10, pi.z)).rgb * 4.0 - 1.0;
  1382.     float n100 = dot(grad100, pf - float3(1.0, 0.0, 0.0));
  1383.     float3  grad101 = rnm(float2(perm10, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  1384.     float n101 = dot(grad101, pf - float3(1.0, 0.0, 1.0));
  1385.  
  1386.     // Noise contributions from (x=1, y=1), z=0 and z=1
  1387.     float perm11 = rnm(pi.xy + float2(permTexUnit, permTexUnit)).a ;
  1388.     float3  grad110 = rnm(float2(perm11, pi.z)).rgb * 4.0 - 1.0;
  1389.     float n110 = dot(grad110, pf - float3(1.0, 1.0, 0.0));
  1390.     float3  grad111 = rnm(float2(perm11, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
  1391.     float n111 = dot(grad111, pf - float3(1.0, 1.0, 1.0));
  1392.  
  1393.     // Blend contributions along x
  1394.     float4 n_x = lerp(float4(n000, n001, n010, n011), float4(n100, n101, n110, n111), fade(pf.x));
  1395.  
  1396.     // Blend contributions along y
  1397.     float2 n_xy = lerp(n_x.xy, n_x.zw, fade(pf.y));
  1398.  
  1399.     // Blend contributions along z
  1400.     float n_xyz = lerp(n_xy.x, n_xy.y, fade(pf.z));
  1401.  
  1402.     // We're done, return the final noise value.
  1403.     return n_xyz;
  1404. }
  1405.  
  1406. //2d coordinate orientation thing
  1407. float2 coordRot(in float2 tc, in float angle)
  1408. {
  1409.     float aspectr = width/height;
  1410.     float rotX = ((tc.x*2.0-1.0)*aspectr*cos(angle)) - ((tc.y*2.0-1.0)*sin(angle));
  1411.     float rotY = ((tc.y*2.0-1.0)*cos(angle)) + ((tc.x*2.0-1.0)*aspectr*sin(angle));
  1412.     rotX = ((rotX/aspectr)*0.5+0.5);
  1413.     rotY = rotY*0.5+0.5;
  1414.     return float2(rotX,rotY);
  1415. }
  1416.  
  1417. float4 GrainPass( float4 colorInput, float2 tex )
  1418. {
  1419.     float3 rotOffset = float3(1.425,3.892,5.835); //rotation offset values  
  1420.     float2 rotCoordsR = coordRot(tex, timer + rotOffset.x);
  1421.     float2 rot = rotCoordsR*float2(width/grainsize,height/grainsize);
  1422.     float pNoise = pnoise3D(float3(rot.x,rot.y,0.0));
  1423.     float3 noise = float3(pNoise, pNoise, pNoise);
  1424.  
  1425.     if (colored == 1)
  1426.     {
  1427.         float2 rotCoordsG = coordRot(tex, timer + rotOffset.y);
  1428.         float2 rotCoordsB = coordRot(tex, timer + rotOffset.z);
  1429.         noise.g = lerp(noise.r,pnoise3D(float3(rotCoordsG*float2(width/grainsize,height/grainsize),1.0)),coloramount);
  1430.         noise.b = lerp(noise.r,pnoise3D(float3(rotCoordsB*float2(width/grainsize,height/grainsize),2.0)),coloramount);
  1431.     }
  1432.    
  1433.     float3 col = colorInput.rgb;
  1434.  
  1435.     //noisiness response curve based on scene luminance
  1436.     float3 lumcoeff = float3(0.299,0.587,0.114);
  1437.     float luminance = lerp(0.0,dot(col, lumcoeff),lumamount);
  1438.     float lum = smoothstep(0.2,0.0,luminance);
  1439.     lum += luminance;
  1440.    
  1441.     float2 thepow = pow(lum, 4.0);
  1442.    
  1443.     noise = lerp(noise,float3(0.0, 0.0, 0.0),pow(lum,4.0));
  1444.     col += noise*grainamount;
  1445.    
  1446.     return float4(col,1.0);
  1447. }
  1448.  
  1449. // ------------------------- Border --------------------------------------------
  1450.  
  1451. #ifndef border_width
  1452.   #define border_width float2(1,0)
  1453. #endif
  1454.  
  1455. #ifndef border_color
  1456.   #define border_color float3(0, 0, 0)
  1457. #endif
  1458.  
  1459. #define screen_ratio (SCREEN_SIZE.x / SCREEN_SIZE.y)
  1460.  
  1461. float4 BorderPass( float4 colorInput, float2 tex )
  1462. {
  1463.   float3 border_color_float = border_color / 255.0;
  1464.  
  1465.   float2 border_width_variable = border_width;
  1466.  
  1467.   // -- calculate the right border_width for a given border_ratio --
  1468.   if (!any(border_width)) //if border_width is not used
  1469.     if (screen_ratio < border_ratio)
  1470.       border_width_variable = float2(0.0, (SCREEN_SIZE.y - (SCREEN_SIZE.x / border_ratio)) * 0.5);
  1471.     else
  1472.       border_width_variable = float2((SCREEN_SIZE.x - (SCREEN_SIZE.y * border_ratio)) * 0.5, 0.0);
  1473.  
  1474.   float2 border = (PIXEL_SIZE * border_width_variable); //Translate integer pixel width to floating point
  1475.  
  1476.   float2 within_border = saturate((-tex * tex + tex) - (-border * border + border)); //becomes positive when inside the border and 0 when outside
  1477.  
  1478.   colorInput.rgb = all(within_border) ?  colorInput.rgb : border_color_float ; //if the pixel is within the border use the original color, if not use the border_color
  1479.  
  1480.   return colorInput; //return the pixel
  1481. }
  1482.  
  1483.  
  1484. // ------------------------- Splitscreen --------------------------------------------
  1485.  
  1486. float4 SplitscreenPass( float4 colorInput, float2 tex )
  1487. {
  1488.   // -- Vertical 50/50 split --
  1489.   #if splitscreen_mode == 1
  1490.       return (tex.x < 0.5) ? tex2D(s0, tex) : colorInput;
  1491.   #endif
  1492.  
  1493.   // -- Vertical 25/50/25 split --
  1494.     #if splitscreen_mode == 2
  1495.     //Calculate the distance from center
  1496.     float distance = abs(tex.x - 0.5);
  1497.    
  1498.     //Further than 1/4 away from center?
  1499.     distance = saturate(distance - 0.25);
  1500.    
  1501.     return distance ? tex2D(s0, tex) : colorInput;
  1502.     #endif
  1503.  
  1504.   // -- Vertical 50/50 angled split --
  1505.     #if splitscreen_mode == 3
  1506.       //Calculate the distance from center
  1507.     float distance = ((tex.x - 3.0/8.0) + (tex.y * 0.25));
  1508.    
  1509.     //Further than 1/4 away from center?
  1510.     distance = saturate(distance - 0.25);
  1511.    
  1512.     return distance ? colorInput : tex2D(s0, tex);
  1513.     #endif
  1514.  
  1515.   // -- Horizontal 50/50 split --
  1516.   #if splitscreen_mode == 4
  1517.       return (tex.y < 0.5) ? tex2D(s0, tex) : colorInput;
  1518.   #endif
  1519.    
  1520.   // -- Horizontal 25/50/25 split --
  1521.   #if splitscreen_mode == 5
  1522.     //Calculate the distance from center
  1523.     float distance = abs(tex.y - 0.5);
  1524.    
  1525.     //Further than 1/4 away from center?
  1526.     distance = saturate(distance - 0.25);
  1527.    
  1528.     return distance ? tex2D(s0, tex) : colorInput;
  1529.   #endif
  1530.  
  1531.   // -- Vertical 50/50 curvy split --
  1532.     #if splitscreen_mode == 6
  1533.     //Calculate the distance from center
  1534.     float distance = (tex.x - 0.25) + (sin(tex.y * 10)*0.10);
  1535.    
  1536.     //Further than 1/4 away from center?
  1537.     distance = saturate(distance - 0.25);
  1538.    
  1539.     return distance ? colorInput : tex2D(s0, tex);
  1540.     #endif
  1541.  
  1542. }
  1543.  
  1544. // ------------------------- SinCity --------------------------------------------
  1545. //  Original code by /r/TerrifyingCookie that was then ported to SweetFX by /r/PrydeRage.
  1546.  
  1547. float4 SinCityPass( float4 colorInput )
  1548. {
  1549.         colorInput = pow(colorInput, .45f);
  1550.         float3 bwcolor = dot(colorInput.rgb, 1.f.xxx) * 0.33333f;
  1551.         float weight = smoothstep(0.1f, 0.25f, colorInput.r - bwcolor);
  1552.         bwcolor = pow(bwcolor * 1.1f, 2.f);
  1553.         float3 colorout = lerp(bwcolor, colorInput * float3(0.5f, 3.5f, 0.5f), weight);
  1554.         return pow(float4(colorout, 1.f), 2.2f);
  1555. }
  1556.  
  1557. //--------------------- Cartoon -------------------------------------------------
  1558.  
  1559. float4 CartoonPass( float4 colorInput, float2 Tex )
  1560. {
  1561.   float3 CoefLuma2 = float3(0.2126, 0.7152, 0.0722);  //Values to calculate luma with
  1562.  
  1563.   float diff1 = dot(CoefLuma2,tex2D(s0, Tex + PIXEL_SIZE).rgb);
  1564.   diff1 = dot(float4(CoefLuma2,-1.0),float4(tex2D(s0, Tex - PIXEL_SIZE).rgb , diff1));
  1565.  
  1566.   float diff2 = dot(CoefLuma2,tex2D(s0, Tex +float2(PIXEL_SIZE.x,-PIXEL_SIZE.y)).rgb);
  1567.   diff2 = dot(float4(CoefLuma2,-1.0),float4(tex2D(s0, Tex +float2(-PIXEL_SIZE.x*CartoonThickness,PIXEL_SIZE.y*CartoonThickness)).rgb , diff2));
  1568.    
  1569.   float edge = dot(float2(diff1,diff2),float2(diff1,diff2));
  1570.  
  1571.   colorInput.rgb =  pow(edge,CartoonEdgeSlope) * (CartoonColor*CartoonPower) + colorInput.rgb;
  1572.    
  1573.   return saturate(colorInput);
  1574. }
  1575.  
  1576. //------------------------ Monochrome -----------------------------------
  1577.  
  1578. //  by Christian Cann Schuldt Jensen ~ CeeJay.dk
  1579. //      - Monochrome removes color and makes everything black and white.
  1580.  
  1581. float4 MonochromePass( float4 colorInput )
  1582. {
  1583.     colorInput.rgb = dot(Monochrome_conversion_values, colorInput.rgb);
  1584.    
  1585.     return saturate(colorInput);
  1586. }
  1587.  
  1588.  
  1589. // ------------------------- Timothy Lottes' CRT --------------------------------
  1590.  
  1591. // Emulated input resolution.
  1592. #if 0
  1593.   // Fix resolution to set amount.
  1594.   float2 res = float2(320.0/1.0, 160.0/1.0);
  1595. #else
  1596.   // Optimize for resize.
  1597.   float2 res = SCREEN_SIZE / 3.0; // <-- original implementation : SCREEN_SIZE / 6.0; (probably hardcoded for ShaderToy)
  1598. #endif
  1599.  
  1600. // Hardness of scanline.
  1601. //  -8.0 = soft
  1602. // -16.0 = medium
  1603. float hardScan=-10.0;
  1604.  
  1605. // Hardness of pixels in scanline.
  1606. // -2.0 = soft
  1607. // -4.0 = hard
  1608. float hardPix=-4.0;
  1609.  
  1610. // Hardness of short vertical bloom.
  1611. //  -1.0 = wide to the point of clipping (bad)
  1612. //  -1.5 = wide
  1613. //  -4.0 = not very wide at all
  1614. float hardBloomScan=-2.0;
  1615.  
  1616. // Hardness of short horizontal bloom.
  1617. //  -0.5 = wide to the point of clipping (bad)
  1618. //  -1.0 = wide
  1619. //  -2.0 = not very wide at all
  1620. float hardBloomPix=-1.5;
  1621.  
  1622. // Amount of small bloom effect.
  1623. //  1.0/1.0 = only bloom
  1624. //  1.0/16.0 = what I think is a good amount of small bloom
  1625. //  0.0     = no bloom
  1626. float bloomAmount=1.0/4.0;
  1627.  
  1628. // Display warp.
  1629. // 0.0 = none
  1630. // 1.0/8.0 = extreme
  1631. float2 warp=float2(1.0/64.0,1.0/24.0);
  1632.  
  1633. // Amount of shadow mask.
  1634. float maskDark=0.5;
  1635. float maskLight=1.5;
  1636.  
  1637. //------------------------------------------------------------------------
  1638.  
  1639. // sRGB to Linear.
  1640. // Assuing using sRGB typed textures this should not be needed.
  1641. float ToLinear1(float c){return(c<=0.04045)?c/12.92:pow((c+0.055)/1.055,2.4);}
  1642. float3 ToLinear(float3 c){return float3(ToLinear1(c.r),ToLinear1(c.g),ToLinear1(c.b));}
  1643.  
  1644. // Linear to sRGB.
  1645. // Assuing using sRGB typed textures this should not be needed.
  1646. float ToSrgb1(float c){return(c<0.0031308?c*12.92:1.055*pow(c,0.41666)-0.055);}
  1647. float3 ToSrgb(float3 c){return float3(ToSrgb1(c.r),ToSrgb1(c.g),ToSrgb1(c.b));}
  1648.  
  1649. // Testing only, something to help generate a dark signal for bloom test.
  1650. // Set to zero, or remove Test() if using this shader.
  1651. #if 1
  1652.  float3 Test(float3 c){return c*(1.0/64.0)+c*c*c;}
  1653. #else
  1654.  float3 Test(float3 c){return c;}
  1655. #endif
  1656.  
  1657. // Nearest emulated sample given floating point position and texel offset.
  1658. // Also zero's off screen.
  1659. float3 Fetch(float2 pos,float2 off){
  1660.   pos=floor(pos*res+off)/res;
  1661.   if(max(abs(pos.x-0.5),abs(pos.y-0.5))>0.5)return float3(0.0,0.0,0.0);
  1662.   return Test(ToLinear(tex2D(s0,pos.xy).rgb));} // return Test(ToLinear(tex2D(s0,pos.xy,-16.0).rgb));}
  1663.  
  1664. // Distance in emulated pixels to nearest texel.
  1665. float2 Dist(float2 pos){pos=pos*res;return -((pos-floor(pos))-float2(0.5, 0.5));}
  1666.    
  1667. // 1D Gaussian.
  1668. float Gaus(float pos,float scale){return exp2(scale*pos*pos);}
  1669.  
  1670. // 3-tap Gaussian filter along horz line.
  1671. float3 Horz3(float2 pos,float off){
  1672.   float3 b=Fetch(pos,float2(-1.0,off));
  1673.   float3 c=Fetch(pos,float2( 0.0,off));
  1674.   float3 d=Fetch(pos,float2( 1.0,off));
  1675.   float dst=Dist(pos).x;
  1676.   // Convert distance to weight.
  1677.   float scale=hardPix;
  1678.   float wb=Gaus(dst-1.0,scale);
  1679.   float wc=Gaus(dst+0.0,scale);
  1680.   float wd=Gaus(dst+1.0,scale);
  1681.   // Return filtered sample.
  1682.   return (b*wb+c*wc+d*wd)/(wb+wc+wd);}
  1683.  
  1684. // 5-tap Gaussian filter along horz line.
  1685. float3 Horz5(float2 pos,float off){
  1686.   float3 a=Fetch(pos,float2(-2.0,off));
  1687.   float3 b=Fetch(pos,float2(-1.0,off));
  1688.   float3 c=Fetch(pos,float2( 0.0,off));
  1689.   float3 d=Fetch(pos,float2( 1.0,off));
  1690.   float3 e=Fetch(pos,float2( 2.0,off));
  1691.   float dst=Dist(pos).x;
  1692.   // Convert distance to weight.
  1693.   float scale=hardPix;
  1694.   float wa=Gaus(dst-2.0,scale);
  1695.   float wb=Gaus(dst-1.0,scale);
  1696.   float wc=Gaus(dst+0.0,scale);
  1697.   float wd=Gaus(dst+1.0,scale);
  1698.   float we=Gaus(dst+2.0,scale);
  1699.   // Return filtered sample.
  1700.   return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);}
  1701.  
  1702. // 7-tap Gaussian filter along horz line.
  1703. float3 Horz7(float2 pos,float off){
  1704.   float3 a=Fetch(pos,float2(-3.0,off));
  1705.   float3 b=Fetch(pos,float2(-2.0,off));
  1706.   float3 c=Fetch(pos,float2(-1.0,off));
  1707.   float3 d=Fetch(pos,float2( 0.0,off));
  1708.   float3 e=Fetch(pos,float2( 1.0,off));
  1709.   float3 f=Fetch(pos,float2( 2.0,off));
  1710.   float3 g=Fetch(pos,float2( 3.0,off));
  1711.   float dst=Dist(pos).x;
  1712.   // Convert distance to weight.
  1713.   float scale=hardBloomPix;
  1714.   float wa=Gaus(dst-3.0,scale);
  1715.   float wb=Gaus(dst-2.0,scale);
  1716.   float wc=Gaus(dst-1.0,scale);
  1717.   float wd=Gaus(dst+0.0,scale);
  1718.   float we=Gaus(dst+1.0,scale);
  1719.   float wf=Gaus(dst+2.0,scale);
  1720.   float wg=Gaus(dst+3.0,scale);
  1721.   // Return filtered sample.
  1722.   return (a*wa+b*wb+c*wc+d*wd+e*we+f*wf+g*wg)/(wa+wb+wc+wd+we+wf+wg);}
  1723.  
  1724. // Return scanline weight.
  1725. float Scan(float2 pos,float off){
  1726.   float dst=Dist(pos).y;
  1727.   return Gaus(dst+off,hardScan);}
  1728.  
  1729. // Return scanline weight for bloom.
  1730. float BloomScan(float2 pos,float off){
  1731.   float dst=Dist(pos).y;
  1732.   return Gaus(dst+off,hardBloomScan);}
  1733.  
  1734. // Allow nearest three lines to effect pixel.
  1735. float3 Tri(float2 pos){
  1736.   float3 a=Horz3(pos,-1.0);
  1737.   float3 b=Horz5(pos, 0.0);
  1738.   float3 c=Horz3(pos, 1.0);
  1739.   float wa=Scan(pos,-1.0);
  1740.   float wb=Scan(pos, 0.0);
  1741.   float wc=Scan(pos, 1.0);
  1742.   return a*wa+b*wb+c*wc;}
  1743.  
  1744. // Small bloom.
  1745. float3 Bloom(float2 pos){
  1746.   float3 a=Horz5(pos,-2.0);
  1747.   float3 b=Horz7(pos,-1.0);
  1748.   float3 c=Horz7(pos, 0.0);
  1749.   float3 d=Horz7(pos, 1.0);
  1750.   float3 e=Horz5(pos, 2.0);
  1751.   float wa=BloomScan(pos,-2.0);
  1752.   float wb=BloomScan(pos,-1.0);
  1753.   float wc=BloomScan(pos, 0.0);
  1754.   float wd=BloomScan(pos, 1.0);
  1755.   float we=BloomScan(pos, 2.0);
  1756.   return a*wa+b*wb+c*wc+d*wd+e*we;}
  1757.  
  1758. // Distortion of scanlines, and end of screen alpha.
  1759. float2 Warp(float2 pos){
  1760.   pos=pos*2.0-1.0;    
  1761.   pos*=float2(1.0+(pos.y*pos.y)*warp.x,1.0+(pos.x*pos.x)*warp.y);
  1762.   return pos*0.5+0.5;}
  1763.  
  1764. #if 0
  1765. // Very compressed TV style shadow mask.
  1766. float3 Mask(float2 pos){
  1767.   float line=maskLight;
  1768.   float odd=0.0;
  1769.   if(frac(pos.x/6.0)<0.5)odd=1.0;
  1770.   if(frac((pos.y+odd)/2.0)<0.5)line=maskDark;  
  1771.   pos.x=frac(pos.x/3.0);
  1772.   float3 mask=float3(maskDark,maskDark,maskDark);
  1773.   if(pos.x<0.333)mask.r=maskLight;
  1774.   else if(pos.x<0.666)mask.g=maskLight;
  1775.   else mask.b=maskLight;
  1776.   mask*=line;
  1777.   return mask;}        
  1778. #endif
  1779.  
  1780. #if 1
  1781. // Aperture-grille.
  1782. float3 Mask(float2 pos){
  1783.   pos.x=frac(pos.x/3.0);
  1784.   float3 mask=float3(maskDark,maskDark,maskDark);
  1785.   if(pos.x<0.333)mask.r=maskLight;
  1786.   else if(pos.x<0.666)mask.g=maskLight;
  1787.   else mask.b=maskLight;
  1788.   return mask;}        
  1789. #endif
  1790.  
  1791. #if 0
  1792. // Stretched VGA style shadow mask (same as prior shaders).
  1793. float3 Mask(float2 pos){
  1794.   pos.x+=pos.y*3.0;
  1795.   float3 mask=float3(maskDark,maskDark,maskDark);
  1796.   pos.x=frac(pos.x/6.0);
  1797.   if(pos.x<0.333)mask.r=maskLight;
  1798.   else if(pos.x<0.666)mask.g=maskLight;
  1799.   else mask.b=maskLight;
  1800.   return mask;}    
  1801. #endif
  1802.  
  1803. #if 0
  1804. // VGA style shadow mask.
  1805. float3 Mask(float2 pos){
  1806.   pos.xy=floor(pos.xy*float2(1.0,0.5));
  1807.   pos.x+=pos.y*3.0;
  1808.   float3 mask=float3(maskDark,maskDark,maskDark);
  1809.   pos.x=frac(pos.x/6.0);
  1810.   if(pos.x<0.333)mask.r=maskLight;
  1811.   else if(pos.x<0.666)mask.g=maskLight;
  1812.   else mask.b=maskLight;
  1813.   return mask;}    
  1814. #endif
  1815.  
  1816.  
  1817. // Draw dividing bars.
  1818. float Bar(float pos,float bar){pos-=bar;return pos*pos<4.0?0.0:1.0;}
  1819.  
  1820. float4 CRTLottesPass( float4 colorInput, float2 tex )
  1821. {
  1822.   float4 outColor;
  1823.   float2 pos=Warp(tex);
  1824.   outColor.rgb = Tri(pos)*Mask(tex * SCREEN_SIZE); // Mask(IN.UVCoord * SCREEN_SIZE.xy);
  1825.   #if 0
  1826.     // Normalized exposure.
  1827.     outColor.rgb = lerp(outColor.rgb,Bloom(pos),bloomAmount);
  1828.   #else
  1829.     // Additive bloom.
  1830.     outColor.rgb += Bloom(pos)*bloomAmount;
  1831.   #endif
  1832.   outColor.a=1.0;
  1833.   outColor.rgb = ToSrgb(outColor.rgb);
  1834.  
  1835.   return outColor;
  1836. }
  1837.  
  1838. // -------------------- Main -----------------------------------------------
  1839.  
  1840. float4 postProcessing(VSOUT IN) : COLOR0
  1841. {
  1842.     float2 tex = IN.UVCoord;
  1843.     float4 c0 = tex2D(s0, tex);
  1844.    
  1845. #if (USE_BLOOM == 1)
  1846.     c0 = BloomPass(c0, tex);
  1847. #endif
  1848.    
  1849. #if (USE_HDR == 1)
  1850.     c0 = HDRPass(c0, tex);
  1851. #endif
  1852.  
  1853. #if (USE_VIBRANCE == 1)
  1854.     c0 = VibrancePass(c0);
  1855. #endif
  1856.  
  1857. #if (USE_TONEMAP == 1)
  1858.     c0 = TonemapPass(c0);
  1859. #endif
  1860.  
  1861. #if (USE_CA == 1)
  1862.     c0 = CAPass(c0, tex);
  1863. #endif
  1864.  
  1865. #if (USE_CURVES == 1)
  1866.     c0 = CurvesPass(c0);
  1867. #endif
  1868.  
  1869. #if (USE_ADVANCED_CRT == 1)
  1870.     c0 = AdvancedCRTPass(c0, tex);
  1871. #endif
  1872.  
  1873. #if (USE_LOTTES_CRT == 1)
  1874.     c0 = CRTLottesPass(c0, tex);
  1875. #endif
  1876.  
  1877. #if (USE_DITHER == 1)
  1878.     c0 = DitherPass(c0, tex);
  1879. #endif
  1880.  
  1881. #if (USE_TECHNICOLOR == 1)
  1882.     c0 = TechnicolorPass(c0);
  1883. #endif
  1884.  
  1885. #if (USE_DPX == 1)
  1886.     c0 = DPXPass(c0);
  1887. #endif
  1888.  
  1889. #if (USE_GRAIN == 1)
  1890.     c0 = GrainPass(c0, tex);
  1891. #endif
  1892.  
  1893. #if (USE_BORDER == 1)
  1894.     c0 = BorderPass(c0, tex);
  1895. #endif
  1896.  
  1897. #if (USE_SPLITSCREEN == 1)
  1898.     c0 = SplitscreenPass(c0, tex);
  1899. #endif
  1900.  
  1901. #if (USE_SINCITY == 1)
  1902.     c0 = SinCityPass(c0);
  1903. #endif
  1904. #if (USE_CARTOON == 1)
  1905.     c0 = CartoonPass(c0, tex);
  1906. #endif
  1907.  
  1908. #if (USE_LUMASHARPEN == 1)
  1909.     c0 = LumaSharpenPass(c0, tex);
  1910. #endif
  1911.  
  1912. #if (USE_MONOCHROME == 1)
  1913.     c0 = MonochromePass(c0);
  1914. #endif
  1915.  
  1916.     c0.w = 1.0;
  1917.     return saturate(c0);
  1918. }
  1919.  
  1920. technique t0
  1921. {
  1922.     pass P0
  1923.     {
  1924.         VertexShader = compile vs_3_0 FrameVS();
  1925.         PixelShader = compile ps_3_0 postProcessing();
  1926.         ZEnable = false;        
  1927.         SRGBWriteEnable = USE_SRGB;
  1928.         AlphaBlendEnable = false;
  1929.         AlphaTestEnable = false;
  1930.         ColorWriteEnable = RED|GREEN|BLUE|ALPHA;
  1931.     }
  1932. }
Add Comment
Please, Sign In to add comment