Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 112.67 KB | None | 0 0
  1. /*===============================================================================*\
  2. |####################### [PsxFX Shader Suite v2.00] ########################|
  3. |######################### By Asmodean ##########################|
  4. || ||
  5. || This program is free software; you can redistribute it and/or ||
  6. || modify it under the terms of the GNU General Public License ||
  7. || as published by the Free Software Foundation; either version 2 ||
  8. || of the License, or (at your option) any later version. ||
  9. || ||
  10. || This program is distributed in the hope that it will be useful, ||
  11. || but WITHOUT ANY WARRANTY; without even the implied warranty of ||
  12. || MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ||
  13. || GNU General Public License for more details. (c)2016 ||
  14. || ||
  15. |#################################################################################|
  16. \*===============================================================================*/
  17. #version 150 compatibility
  18.  
  19. #extension GL_NV_gpu_shader5 : enable
  20. #extension GL_ARB_gpu_shader5 : enable
  21. #extension GL_ARB_shader_precision : enable
  22. #extension GL_ARB_sample_shading : enable
  23. #extension GL_ARB_texture_rectangle : enable
  24. #extension GL_ARB_shader_storage_buffer_object : enable
  25. #extension GL_ARB_shader_image_load_store : enable
  26. #extension GL_ARB_bindless_texture : enable
  27. #extension GL_ARB_shading_language_420pack : enable
  28.  
  29. /*------------------------------------------------------------------------------
  30. [DEFINITIONS & ON/OFF OPTIONS]
  31. ------------------------------------------------------------------------------*/
  32.  
  33. //--------------------------#[CHOOSE EFFECTS]#--------------------------------\\
  34.  
  35. //#[ANTIALIASING TECHNIQUES] [1=ON|0=OFF] #NOTE: For best results: use an antialiasing OR filtering technique. Not both together.
  36. #define FXAA 1 //# High Quality Fast Approximate Anti-Aliasing. With optional xBR, & Bilateral sampling. Providing the benefits of 3 in 1.
  37. #define DLAA 0 //# Directionally Localized Anti-Aliasing. A soft type of Morphological Anti-Aliasing with good coverage.
  38. #define SSAA 0 //# Shader-Based Pseudo SuperSampling. A light AA that scales well with high resolutions. (2D & 3D).
  39. #define XBRV 0 //# Hyllian's xBR-lv2-3D Shader. xBR interpolation scaling. This must be enabled in the vertex shader (slv) also, for it to work.
  40. #define SFAA 0 //# Guest's Soft Filter Anti-Aliasing. This must be enabled in the vertex shader (slv) also, for it to work. (2D & 3D).
  41.  
  42. //#[FS SCALING TECHNIQUES] [1=ON|0=OFF] #NOTE: For best results: Only enable one type of filtering at one time. Use post antialiasing OR FS filtering, not both.
  43. #define BILINEAR_FILTERING 0 //# Bilinear Fullscreen Texture Filtering. BiLinear filtering - light to medium filtering of textures.[2D]
  44. #define BICUBIC_FILTERING 0 //# Bicubic Fullscreen Texture Filtering. BiCubic filtering - medium to strong filtering of textures.[2D]
  45. #define GAUSSIAN_FILTERING 0 //# Gaussian Fullscreen Texture Filtering. Gaussian filtering - strong to extra strong filtering of textures.[2D]
  46. #define BILATERAL_FILTERING 0 //# Bilateral Fullscreen Texture Filtering. Smoothes color gradient transitions and reduces dithering artifacts.[2D]
  47. #define DDTI_FILTERING 0 //# Data Dependent Triangulation Interpolation. This must be enabled in the vertex shader (slv) also, for it to work.[2D]
  48. #define BICUBLIC_SCALER 0 //# Bicubic Interpolation Scaling. Uses BCS on up scaling, and downsampling of games, for smoother scaling.
  49. #define LANCZOS_SCALER 0 //# Lanczos Interpolation Scaling. Uses Lanczos on up scaling, and downsampling of games for smoother scaling.
  50.  
  51. //#[LIGHTING & COLOUR] [1=ON|0=OFF] #NOTE: These can all be turned on & off independently of each other. [For High Dynamic Range(HDR) use Bloom & Tonemapping together]
  52. #define PHONG_SHADING 1 //# Per-Pixel Phong Shading. Simulates lighting calculations on a per-pixel basis, with attenuation (lighting enhancement).
  53. #define BLENDED_BLOOM 0 //# High Quality SP Bloom. Soft lighting with blending techniques, for a natural looking bloom.
  54. #define SCENE_TONEMAPPING 0 //# HDR Scene Tonemapping. Layered component conversion, and applies scene tone mapping.
  55. #define COLOR_CORRECTION 1 //# Component Color Correction. Colorspace conversion, with correction curves, and multiple palette types.
  56. #define CROSS_PROCESSING 0 //# Filmic Cross Processing. Alters the tone of the scene, crossing the game's color set, with another.
  57. #define NTSC_TV_EMULATION 0 //# SimoneT's TV Colors Emulation Shader. Use to emulate the original PSX color space tone, and vibrancy.
  58. #define GAMMA_CORRECTION 0 //# RGB Gamma Correction. Fixed expansion to variable compression gamma correction curve.
  59. #define PIXEL_VIBRANCE 0 //# Pixel Vibrance. Intelligently adjusts pixel vibrance depending on original color saturation.
  60. #define COLOR_GRADING 0 //# Post-Complement Colour Grading. Alters individual colour components on a scene, to manip selected colour tones.
  61. #define TEXTURE_SHARPEN 0 //# Bicubic Texture Unsharp Mask. Looks similar to a negative texture LOD bias. Enhances texture fidelity.
  62. #define CURVE_CONTRAST 0 //# S-Curve Scene Contrast Enhancement. Locally adjusts contrast using a four-point cubic bezier spline.
  63. #define CEL_SHADING 0 //# PX Cel Shading. A pixel-based cel shader, with a luminance edge filter for avoiding edge effects on menus & text.
  64. #define PAINT_SHADING 0 //# Paint Shading. Creates the effect of a painted scene. Cel shaded edges can also be enabled for a story book effect.
  65.  
  66. //#[TV EMU TECHNIQUES] [1=ON|0=OFF] #NOTE: These can all be turned on & off independently of each other. These effects are typically used to simulated older TVs/utility, etc.
  67. #define LOTTES_CRT 0 //# Timothy Lottes CRT emulation effect. Similar to scanlines, but with more control options. Ported by request.
  68. #define SCANLINES 0 //# Scanlines to simulate the look of a CRT TV. Typically suited to sprite games. Note: Works best at Native Res.
  69. #define VIGNETTE 0 //# Darkens the edges of the screen, to make it look more like it was shot with a camera lens.
  70. #define DEBANDING 0 //# Applies a debanding effect. This effect can help to minimize banding artifacts. Mileage will vary per game.
  71. #define SP_DITHERING 0 //# Subpixel Dithering to simulate more colors than your monitor can display. Smoothes gradiants, this can reduce color banding.
  72. #define PX_BORDER 0 //# Creates a pixel border as a workaround for the bright, or flickering edges that can occur on some games. (Ported by request from SFX).
  73.  
  74. /*------------------------------------------------------------------------------
  75. [EFFECT CONFIG OPTIONS]
  76. ------------------------------------------------------------------------------*/
  77.  
  78. //##[FXAA]
  79. #define FxaaSubpixMax 8.00 //[1.00 to 8.00] Amount of subpixel anti-aliasing. Higher values for a stronger effect, & edge coverage.
  80. #define FxaaQuality 4 //[1|2|3|4] Overall Fxaa quality preset (pixel coverage). 1: Low, 2: Medium, 3: High, 4: Ultra.
  81. #define BilateralSampling 0 //[0 or 1] Use Bilateral filtering within the FXAA coverage. High performance cost if BilateralRadius is set too high. (use bilateral settings)
  82. #define xBRlv2Sampling 0 //[0 or 1] Use xBR-lv2-3D within the FXAA coverage. Enables the 2D interpolation of xBR, with the 3D antialiasing of FXAA. (use xBR settings & enable in .slv)
  83. #define xBRThreshold 0.020 //[0.00 to 0.05] The detection threshold for applying xBR to 2D, and FXAA to 3D. (3D -> 0.0....0.05 -> 2D)
  84.  
  85. //##[SSAA]
  86. #define FilterWidth 1.35 //[0.50 to 2.00] Width of the SSAA filter. Raising this will increase the width of the edge filter.
  87.  
  88. //##[XBRV]
  89. #define DetectionType 3 //[1|2|3|4] The pixel corner detection method used. 1: Corner A, 2: Corner B, 3: Corner C, 4: Corner D.
  90. #define SampleScale 2.0 //[1.0 to 4.0] The xBR resolution scale of the samples. Changes the scale mod for the base res.
  91. #define ScaleFactor 3.0 //[1.0 to 5.0] The xBR scaling factor. Use to increase or decrease the scale of the xBR.
  92.  
  93. //##[BILINEAR_FILTERING]
  94. #define FilterStrength 1.00 //[0.10 to 1.50] Bilinear filtering strength. Controls the overall strength of the filtering.
  95. #define OffsetAmount 0.0 //[0.0 to 1.5] Pixel offset amount. If you want to use an st offset, 0.5 is generally recommended. 0.0 is off.
  96.  
  97. //##[BICUBIC_FILTERING]
  98. #define Interpolation Triangular //[CatMullRom, Bell, BSpline, Triangular, Cubic] Type of interpolation to use. From left to right is lighter<-->stronger filtering.
  99. #define BicubicStrength 0.75 //[0.10 to 1.50] Bicubic filtering strength. Controls the overall strength of the filtering.
  100. #define PixelOffset 0.0 //[0.0 to 1.5] Pixel offset amount. If you want to use an st offset, 0.5 is generally recommended. 0.0 is off.
  101.  
  102. //##[GAUSSIAN_FILTERING]
  103. #define FilterAmount 1.00 //[0.10 to 1.50] Gaussian filtering strength. Controls the overall strength of the filtering.
  104. #define GaussianSpread 0.75 //[0.50 to 4.00] The filtering spread & offset levels. Controls the sampling spread of the filtering.
  105.  
  106. //##[BILATERAL_FILTERING]
  107. #define BilateralRadius 2.00 //[1.00 to 8.00] Radius of the filter. Increasing the radius leads to more pixel lookups, so higher values require more performance.
  108. #define BilateralWeight 0.20 //[0.10 to 1.00] Central weight of the filter. Raising this increases the overall filter width, along with the strength.
  109.  
  110. //##[DDTI_FILTERING]
  111. #define PixelShift 0.25 //[0.10 to 1.00] Pixel offset amount. If you want to use an st offset, 0.25 is generally recommended. 0.0 is off.
  112.  
  113. //##[BLENDED_BLOOM]
  114. #define BloomType BlendGlow //[BlendGlow, BlendAddGlow, BlendAddLight, BlendScreen, BlendLuma, BlendOverlay] The type of blended bloom. Light<->Dark.
  115. #define BloomStrength 0.200 //[0.000 to 1.000] Overall strength of the bloom. You may want to readjust for each blend type.
  116. #define BlendStrength 1.000 //[0.000 to 1.000] Strength of the blending. This is a modifier based on bloom. 1.0 equates to 100% strength.
  117. #define BloomDefocus 2.000 //[1.000 to 4.000] The initial bloom defocus value. Increases the softness of light, bright objects, etc.
  118. #define BloomWidth 3.200 //[1.000 to 8.000] Width of the bloom. Adjusts the width of the spread and soft glow. Scales with BloomStrength.
  119. #define BloomReds 0.040 //[0.000 to 1.000] Red channel correction of the bloom. Raising will increase the bloom of reds.
  120. #define BloomGreens 0.030 //[0.000 to 1.000] Green channel correction of the bloom. Raising will increase the bloom of greens.
  121. #define BloomBlues 0.020 //[0.000 to 1.000] Blue channel correction of the bloom. Raising will increase the bloom of blues.
  122.  
  123. //##[SCENE_TONEMAPPING]
  124. #define TonemapType 1 //[0|1|2|3] The base tone mapping operator. 0 is LDR, 1 is HDR(original), 2 & 3 are Filmic HDR(slight grading).
  125. #define TonemapMask 0 //[0 or 1] Enables an ALU tone masking curve. Produces a nice cinematic look. Suits some games more than others.
  126. #define MaskStrength 0.10 //[0.000 to 1.000] Strength of the tone masking. Higher for a stronger effect. This is a dependency of TonemapMask.
  127. #define ToneAmount 0.320 //[0.050 to 1.000] Tonemap strength (tone correction). Higher for stronger tone mapping, lower for lighter.
  128. #define BlackLevels 0.060 //[0.000 to 1.000] Black level balance (shadow correction). Increase to deepen blacks, lower to lighten them.
  129. #define Exposure 1.000 //[0.100 to 2.000] White correction (brightness). Higher values for more scene exposure, lower for less.
  130. #define Luminance 1.000 //[0.100 to 2.000] Luminance average (luminance correction). Higher values will lower scene luminance average.
  131. #define WhitePoint 1.000 //[0.100 to 2.000] Whitepoint average (wp lum correction). Higher values will lower the maximum scene white point.
  132.  
  133. //##[COLOR_CORRECTION]
  134. #define CorrectionPalette 1 //[1|2|3|4|5] The colorspace palette type. 1: RGB, 2: YXY, 3: XYZ, 4: HSV, 5: YUV. Each one will produce a different combination of shades & hues.
  135. #define ChannelR 1.40 //[0.00 to 8.00] R(1), Y(2), X(3), H(4), Y(5) component channel varies with the colorspace used. Higher values increase correction strength.
  136. #define ChannelG 1.60 //[0.00 to 8.00] G(1), X(2), Y(3), S(4), U(5) component channel varies with the colorspace used. Higher values increase correction strength.
  137. #define ChannelB 1.80 //[0.00 to 8.00] B(1), Y(2), Z(3), V(4), V(5) component channel varies with the colorspace used. Higher values increase correction strength.
  138. #define PaletteStrength 2.00 //[0.00 to 4.00] The interpolated strength ratio between the base color, and the corrected color. Raise to increase saturation.
  139.  
  140. //##[CROSS_PROCESSING]
  141. #define FilmicProcess 1 //[1|2|3] The color conversion type for the cross process. 1: cool, 2: warm, 3: dusk. You can achieve different results with each.
  142. #define RedShift 0.50 //[0.10 to 1.00] Red color component shift of the filmic processing. Alters the red balance of the shift.
  143. #define GreenShift 0.50 //[0.10 to 1.00] Green color component shift of the filmic processing. Alters the green balance of the shift.
  144. #define BlueShift 0.50 //[0.10 to 1.00] Blue color component shift of the filmic processing. Alters the blue balance of the shift.
  145. #define ShiftRatio 0.50 //[0.10 to 2.00] The blending ratio for the base color and the color shift. Higher for a stronger effect.
  146.  
  147. //##[TEXTURE_SHARPEN]
  148. #define SharpenStrength 0.80 //[0.10 to 2.00] Strength of the texture sharpening effect. This is the maximum strength that will be used.
  149. #define SharpenClamp 0.080 //[0.005 to 0.500] Reduces the clamping/limiting on the maximum amount of sharpening each pixel recieves. Raise this to reduce the clamping.
  150. #define SharpenBias 1.40 //[0.50 to 4.00] Sharpening edge bias. Lower values for clean subtle sharpen, and higher values for a deeper textured sharpen.
  151. #define DebugSharpen 0 //[0 or 1] Visualize the sharpening effect. Useful for fine-tuning. Best to disable other effects, to see edge detection clearly.
  152.  
  153. //##[PIXEL_VIBRANCE]
  154. #define Vibrance 0.10 //[-1.00 to 1.00] Overall vibrance strength. Locally adjusts the vibrance of pixels depending on their original saturation.
  155. #define RedVibrance 1.00 //[-8.00 to 8.00] Red channel coefficient of the vibrance strength. Adjusting the vibrance of the red channel independently.
  156. #define GreenVibrance 1.00 //[-8.00 to 8.00] Green channel coefficient of the vibrance strength. Adjusting the vibrance of the green channel independently.
  157. #define BlueVibrance 1.00 //[-8.00 to 8.00] Blue channel coefficient of the vibrance strength. Adjusting the vibrance of the blue channel independently.
  158.  
  159. //##[COLOR_GRADING]
  160. #define RedGrading 1.20 //[0.00 to 3.00] Red colour grading coefficient. Adjust to influence the red channel coefficients of the grading, and highlight tones.
  161. #define GreenGrading 1.10 //[0.00 to 3.00] Green colour grading coefficient. Adjust to influence the Green channel coefficients of the grading, and highlight tones.
  162. #define BlueGrading 1.10 //[0.00 to 3.00] Blue colour grading coefficient. Adjust to influence the Blue channel coefficients of the grading, and highlight tones.
  163. #define GradingStrength 0.25 //[0.00 to 1.00] The overall max strength of the colour grading effect. Raise to increase, lower to decrease the amount.
  164. #define Correlation 1.00 //[0.10 to 1.00] Correlation between the base colour, and the grading influence. Lower = more of the scene is graded, Higher = less of the scene is graded.
  165.  
  166. //##[CEL_SHADING]
  167. #define CelStrength 1.00 //[0.00 to 4.00] Overall strength of the cel edge outline effect. Affects the overall density. 0.00: no outlines.
  168. #define EdgeFilter 1.00 //[0.10 to 2.00] Filters out fainter cel edges. Use it for balancing the cel edge density. Lower values for more of a dense cel detection.
  169. #define EdgeScale 1.50 //[1.00 to 4.00] Thickness of the cel edges. Basically a scale modifier for the resolution. Set higher when using low/native res for games.
  170. #define ColorRange 7.00 //[5.00 to 15.0] This alters the color highlight range of the cel shading. Lower values reduce the range, and higher increases it.
  171. #define Saturation 1.20 //[0.10 to 2.00] Highlighted saturation of the shaded color. Where 1.0 is base saturation. Higher for more.
  172. #define ShadedStrength 0.60 //[0.00 to 1.00] The interpolated strength value of the 'tooning' shading color conversions. Where 1.00 is 100% strength.
  173. #define UseYuvLuma 0 //[0 or 1] Uses YUV luma calculations, or base color luma calculations. Yuv luma can produce a better shaded look.
  174. #define ColorRounding 0 //[0 or 1] Uses rounding methods on colors. This can emphasise shaded toon colors. Looks good in some games, and odd in others.
  175. #define RoundingLevels 9.0 //[2.0 to 18.0] Alters the rounding levels for the shaded RGB color. Different values will produce different roundin results.
  176.  
  177. //##[PAINT_SHADING]
  178. #define PaintRadius 4 //[2 to 8] Radius of the painted effect. Increasing the radius also requires longer loops, so higher values require more performance.
  179. #define PaintStrength 1.00 //[0.00 to 1.00] The overall interpolated strength of the paint effect. Where 1.0 equates to 100% strength.
  180. #define ResScale 1.00 //[1.00 to 4.00] Scale modifier for the effect resolution. Set higher to clean up the effect when using on low/native res for games.
  181. #define UseCelEdges 1 //[0 or 1] Enables cel edges for the paint shading. This can produce a nice story book / painted cel shader effect. (use cel options)
  182.  
  183. //##[CURVE_CONTRAST]
  184. #define CurveType 0 //[0|1|2] Choose what to apply contrast to. 0 = Luma, 1 = Chroma, 2 = both Luma and Chroma. Default is 0 (Luma)
  185. #define CurvesContrast 0.35 //[0.00 to 2.00] The amount of contrast you want. Controls the overall strength of the texture sharpening.
  186.  
  187. //##[GAMMA_CORRECTION]
  188. #define Gamma 2.20 //[1.5 to 4.0] Gamma correction. Decrease for lower gamma(darker). Increase for higher gamma(brighter). (Default: 2.2)
  189.  
  190. //##[LOTTES_CRT]
  191. #define UseShadowMask 1 //[0 or 1] Enables, or disables the use of the CRT shadow mask. 0 is disabled, 1 is enabled.
  192. #define MaskingType 1 //[1|2|3|4] The type of CRT shadow masking used. 1: compressed TV style, 2: Aperture-grille, 3: Stretched VGA style, 4: VGA style.
  193. #define CRTSizeX 1024 //[128 to 2048] A workaround for arbitrary render target scaling. Either set it to a fixed width, or set it to screenSize.x
  194. #define CRTSizeY 240 //[128 to 2048] A workaround for arbitrary render target scaling. Either set it to a fixed height, or set it to screenSize.y
  195. #define ScanBrightness -6.00 //[-16.0 to 1.0] The overall brightness of the scanline effect. Lower for darker, higher for brighter.
  196. #define FilterCRTAmount -4.00 //[-4.0 to 1.0] The amount of filtering used, to replicate the TV CRT look. Lower for less, higher for more.
  197. #define HorizontalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the horizontal (x) axis of the screen. Use small increments.
  198. #define VerticalWarp 0.00 //[0.0 to 0.1] The distortion warping effect for the verticle (y) axis of the screen. Use small increments.
  199. #define MaskAmountDark 1.00 //[0.0 to 1.0] The value of the dark masking line effect used. Lower for darker lower end masking, higher for brighter.
  200. #define MaskAmountLight 1.50 //[0.0 to 2.0] The value of the light masking line effect used. Lower for darker higher end masking, higher for brighter.
  201. #define ResolutionScale 4.00 //[0.1 to 4.0] The scale of the image resolution. Lowering this can give off a nice retro TV look. Raising it can clear up the image.
  202. #define MaskResolutionScale 2.30 //[0.1 to 2.0] The scale of the CRT mask resolution. Use this for balancing the scanline mask scale for difference resolution scaling.
  203.  
  204. //##[SCANLINES]
  205. #define ScanlineType 2 //[0|1|2] The type & orientation of the scanlines. 0 is x(horizontal), 1 is y(vertical), 2 is both(xy)
  206. #define ScanlineScale 0.30 //[0.20 to 2.00] The scaling & thickness of the scanlines. Changing this can help with PCSX2 IR scaling problems.
  207. #define ScanlineIntensity 0.18 //[0.10 to 1.00] The intensity of the scanlines. Defaults: 0.18 for ScanlineType 0|1|2, 0.50 for ScanlineType 3.
  208. #define ScanlineBrightness 2.00 //[0.50 to 2.00] The brightness of the scanlines. Defaults: 2.00 for ScanlineType 0|1|2, 1.50 for ScanlineType 3.
  209.  
  210. //##[VIGNETTE]
  211. #define VignetteRatio 1.77 //[0.15 to 6.00] Sets the espect ratio of the vignette. 1.77 for 16:9, 1.60 for 16:10, 1.33 for 4:3, 1.00 for 1:1.
  212. #define VignetteRadius 1.10 //[0.50 to 3.00] Radius of the vignette effect. Lower values for stronger radial effect from center
  213. #define VignetteAmount 0.25 //[0.00 to 1.00] Strength of black edge occlusion. Increase for higher strength, decrease for lower.
  214. #define VignetteSlope 12 //[2|4|8|10|12|16] How far away from the center the vignetting will start.
  215.  
  216. //##[DEBANDING]
  217. #define DebandRadius 256.0 //[0.0 to 1024.0] Sampling radius, higher values will reduce further banding but might also reduce details.
  218. #define DebandThreshold 0.017 //[0.000 to 0.100] Threshold, higher values will reduce further banding but might also reduce details and increase noise.
  219. #define DebandSampleCount 8 //[1 to 8] Sample count, higher values are better. But also have a higher performance cost.
  220. #define DebandOffsetMode 3 //[1|2|3] 1 = cross (axis aligned, fast), 2 = diagonal (45 degrees, slower), 3 = box (fully random, slower).
  221. #define DebandDithering 3 //[0|1|2|3] Additional dithering options to smoothen the output. 0 = No dithering 1 = Ordered dithering, 2 = Random dithering, 3 = Iestyn's RGB dither (Valve).
  222.  
  223. //##[SUBPIXEL_DITHERING]
  224. #define DitherMethod 2 //[1 or 2] The type of dithering to use. 1: Ordered grid dithering(faster), 2: random dithering(higher quality).
  225. #define ShowMethod 0 //[0 or 1] Shows the dithering method, based of the type of dithering selected. Useful for debugging, and confirmation of working order.
  226.  
  227. //##[PX_BORDER]
  228. #define BorderWidth float2(0, 0) //[0 to 2048, 0 to 2048] (X,Y)-width of the border. Measured in pixels.
  229. #define BorderColor 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.
  230.  
  231. //[END OF USER OPTIONS]
  232.  
  233. /*------------------------------------------------------------------------------
  234. [GLOBALS|FUNCTIONS]
  235. ------------------------------------------------------------------------------*/
  236.  
  237. // HLSL --> GLSL
  238. #define float2 vec2
  239. #define float3 vec3
  240. #define float4 vec4
  241. #define uint2 uvec2
  242. #define uint3 uvec3
  243. #define uint4 uvec4
  244. #define int2 ivec2
  245. #define int3 ivec3
  246. #define int4 ivec4
  247. #define float1x1 mat1
  248. #define float2x2 mat2
  249. #define float3x3 mat3
  250. #define float4x4 mat4
  251. #define float4x3 mat4x3
  252. #define float3x4 mat3x4
  253. #define frac fract
  254. #define lerp mix
  255. #define fmod mod
  256. #define saturate(x) clamp(x, 0.0, 1.0)
  257. #define mul(x, y) (y * x)
  258. #define ddx dFdx
  259. #define ddy dFdy
  260. #define rsqrt inversesqrt
  261. #define SamplerState sampler2D
  262.  
  263. //OGL2 Uniforms
  264. uniform vec4 OGL2Param;
  265. uniform vec4 OGL2Size;
  266. uniform vec4 OGL2InvSize;
  267. uniform sampler2D OGL2Texture;
  268.  
  269. //VS --> PS
  270. in vec4 TexCoord;
  271. in vec4 Vertex;
  272. in vec3 eyeVect;
  273. in vec3 Normal;
  274. in vec4 rcpPos;
  275. in vec4 Position;
  276. in vec3 lightPos;
  277. in vec3 lightVect;
  278. in float Distance;
  279.  
  280. //PS --> Output
  281. out vec4 FragColor;
  282.  
  283. //Static vars
  284. float Epsilon = 1e-10;
  285. float2 screenSize = OGL2Size.xy;
  286. float2 pixelSize = OGL2InvSize.xy;
  287. float4 rcpFrame = float4(-0.50/OGL2Size.x, -0.50/OGL2Size.y, 0.50/OGL2Size.x, 0.50/OGL2Size.y);
  288. float4 rcpByTwo = float4(-2.00/OGL2Size.x, -2.00/OGL2Size.y, 2.00/OGL2Size.x, 2.00/OGL2Size.y);
  289.  
  290. //Conversion matrices
  291. float3 RGBtoXYZ(float3 RGB)
  292. {
  293. const float3x3 m = float3x3(
  294. 0.6068909, 0.1735011, 0.2003480,
  295. 0.2989164, 0.5865990, 0.1144845,
  296. 0.0000000, 0.0660957, 1.1162243);
  297.  
  298. return mul(m, RGB);
  299. }
  300.  
  301. float3 XYZtoRGB(float3 XYZ)
  302. {
  303. const float3x3 m = float3x3(
  304. 1.9099961, -0.5324542, -0.2882091,
  305. -0.9846663, 1.9991710, -0.0283082,
  306. 0.0583056, -0.1183781, 0.8975535);
  307.  
  308. return mul(m, XYZ);
  309. }
  310.  
  311. float3 XYZtoSRGB(float3 XYZ)
  312. {
  313. const float3x3 m = float3x3(
  314. 3.2404542,-1.5371385,-0.4985314,
  315. -0.9692660, 1.8760108, 0.0415560,
  316. 0.0556434,-0.2040259, 1.0572252);
  317.  
  318. return mul(m, XYZ);
  319. }
  320.  
  321. float3 RGBtoYUV(float3 RGB)
  322. {
  323. const float3x3 m = float3x3(
  324. 0.2126, 0.7152, 0.0722,
  325. -0.09991,-0.33609, 0.436,
  326. 0.615, -0.55861, -0.05639);
  327.  
  328. return mul(m, RGB);
  329. }
  330.  
  331. float3 YUVtoRGB(float3 YUV)
  332. {
  333. const float3x3 m = float3x3(
  334. 1.000, 0.000, 1.28033,
  335. 1.000,-0.21482,-0.38059,
  336. 1.000, 2.12798, 0.000);
  337.  
  338. return mul(m, YUV);
  339. }
  340.  
  341. float3 XYZtoYxy(float3 XYZ)
  342. {
  343. float w = (XYZ.r + XYZ.g + XYZ.b);
  344. float3 Yxy;
  345.  
  346. Yxy.r = XYZ.g;
  347. Yxy.g = XYZ.r / w;
  348. Yxy.b = XYZ.g / w;
  349.  
  350. return Yxy;
  351. }
  352.  
  353. float3 YxytoXYZ(float3 Yxy)
  354. {
  355. float3 XYZ;
  356. XYZ.g = Yxy.r;
  357. XYZ.r = Yxy.r * Yxy.g / Yxy.b;
  358. XYZ.b = Yxy.r * (1.0 - Yxy.g - Yxy.b) / Yxy.b;
  359.  
  360. return XYZ;
  361. }
  362.  
  363. //Average Relative Luminance (HSP Color Model)
  364. //sqrt(0.299 * R^2 + 0.587 * G^2 + 0.114 * B^2)
  365. const float3 lumCoeff = float3(0.2126729, 0.7151522, 0.0721750);
  366. float AvgLuminance(float3 color)
  367. {
  368. return sqrt(dot(color * color, lumCoeff));
  369. }
  370.  
  371. //Smoothstep alt
  372. float smootherstep(float a, float b, float x)
  373. {
  374. x = saturate((x - a) / (b - a));
  375. return x*x*x*(x*(x * 6 - 15) + 10);
  376. }
  377.  
  378. //Check for OOB rgb clipping
  379. float4 DebugClipping(float4 color)
  380. {
  381. if (color.x >= 0.99999 && color.y >= 0.99999 &&
  382. color.z >= 0.99999) color.xyz = float3(1.0f, 0.0f, 0.0f);
  383.  
  384. if (color.x <= 0.00001 && color.y <= 0.00001 &&
  385. color.z <= 0.00001) color.xyz = float3(0.0f, 0.0f, 1.0f);
  386.  
  387. return color;
  388. }
  389.  
  390. /*------------------------------------------------------------------------------
  391. [DLAA CODE SECTION]
  392. ------------------------------------------------------------------------------*/
  393.  
  394. #if DLAA == 1
  395. vec4 sampleOffset(sampler2D tex, vec2 texcoord, vec2 pixelOffset)
  396. {
  397. return texture(tex, texcoord + pixelOffset * pixelSize);
  398. }
  399.  
  400. vec3 DLAAPixelShader(vec4 color, vec2 texcoord)
  401. {
  402. vec4 SC = sampleOffset(OGL2Texture, texcoord.xy, vec2( 0.0, 0.0));
  403. vec4 HN = sampleOffset(OGL2Texture, texcoord.xy, vec2(-1.5, 0.0));
  404. vec4 HP = sampleOffset(OGL2Texture, texcoord.xy, vec2( 1.5, 0.0));
  405. vec4 VN = sampleOffset(OGL2Texture, texcoord.xy, vec2( 0.0, -1.5));
  406. vec4 VP = sampleOffset(OGL2Texture, texcoord.xy, vec2( 0.0, 1.5));
  407.  
  408. vec4 sumH = HN + HP, sumV = VN + VP;
  409. vec4 diffH = abs(sumH - (2.0 * SC)) / 2.0, diffV = abs(sumH - (2.0 * SC)) / 2.0;
  410.  
  411. float edgeH = AvgLuminance(diffH.xyz);
  412. float edgeV = AvgLuminance(diffV.xyz);
  413.  
  414. float detectH = clamp((3.0 * edgeH) - 0.1, 0.0, 1.0);
  415. float detectV = clamp((3.0 * edgeV) - 0.1, 0.0, 1.0);
  416.  
  417. vec4 averageH = (sumH + SC) / 3.0, averageV = (sumV + SC) / 3.0;
  418.  
  419. float valueH = AvgLuminance(averageH.xyz);
  420. float valueV = AvgLuminance(averageV.xyz );
  421.  
  422. float blurAmountHoriz = clamp(detectH / valueH , 0.0, 1.0);
  423. float blurAmountVert = clamp(detectV / valueV , 0.0, 1.0);
  424.  
  425. vec4 result = mix(SC, averageH, blurAmountHoriz);
  426. result = mix(result, averageV, blurAmountVert);
  427.  
  428. vec4 VNA = sampleOffset(OGL2Texture, texcoord.xy, vec2(0.0, -3.5));
  429. vec4 VNB = sampleOffset(OGL2Texture, texcoord.xy, vec2(0.0, -7.5));
  430. vec4 VPA = sampleOffset(OGL2Texture, texcoord.xy, vec2(0.0, 3.5));
  431. vec4 VPB = sampleOffset(OGL2Texture, texcoord.xy, vec2(0.0, 7.5));
  432.  
  433. vec4 HNA = sampleOffset(OGL2Texture, texcoord.xy, vec2(-3.5, 0.0));
  434. vec4 HNB = sampleOffset(OGL2Texture, texcoord.xy, vec2(-7.5, 0.0));
  435. vec4 HPA = sampleOffset(OGL2Texture, texcoord.xy, vec2( 3.5, 0.0));
  436. vec4 HPB = sampleOffset(OGL2Texture, texcoord.xy, vec2( 7.5, 0.0));
  437.  
  438. float passH = (HNB.a + HNA.a + SC.a + HPA.a + HPB.a) / 5.0;
  439. float passV = (VNB.a + VNA.a + SC.a + VPA.a + VPB.a ) / 5.0;
  440. passH = clamp(passH * 2.0 - 1.0 , 0.0, 1.0), passV = clamp(passV * 2.0 - 1.0 , 0.0, 1.0);
  441.  
  442. float longEdge = max(passH, passV);
  443.  
  444. if (longEdge > 1.0)
  445. {
  446. vec4 avgHorizLong = (HNB + HNA + SC + HPA + HPB) / 5.0;
  447. vec4 avgVertLong = (VNB + VNA + SC + VPA + VPB) / 5.0;
  448.  
  449. float valueHorizLong = AvgLuminance(avgHorizLong.xyz);
  450. float valueVertLong = AvgLuminance(avgVertLong.xyz);
  451.  
  452. vec4 SL = sampleOffset(OGL2Texture, texcoord.xy, vec2(-1.0, 0.0) );
  453. vec4 SR = sampleOffset(OGL2Texture, texcoord.xy, vec2( 1.0, 0.0) );
  454. vec4 SU = sampleOffset(OGL2Texture, texcoord.xy, vec2( 0.0, -1.0) );
  455. vec4 SD = sampleOffset(OGL2Texture, texcoord.xy, vec2( 0.0, 1.0) );
  456.  
  457. float VC = AvgLuminance(SC.xyz);
  458. float VL = AvgLuminance(SL.xyz);
  459. float VR = AvgLuminance(SR.xyz);
  460. float VT = AvgLuminance(SU.xyz);
  461. float VB = AvgLuminance(SD.xyz);
  462.  
  463. vec4 diffC = VC - vec4(VL, VT, VR, VB);
  464. float BL = clamp(0.0 + (valueVertLong - VL) / diffC.x , 0.0, 1.0);
  465. float BU = clamp(0.0 + (valueHorizLong - VT) / diffC.y , 0.0, 1.0);
  466. float BR = clamp(1.0 + (valueVertLong - VC) / diffC.z , 0.0, 1.0);
  467. float BD = clamp(1.0 + (valueHorizLong - VC) / diffC.w , 0.0, 1.0);
  468.  
  469. vec4 blurAmounts = vec4(BL, BR, BU, BD);
  470. blurAmounts = (blurAmounts == vec4(0.0, 0.0, 0.0, 0.0)) ? vec4(1.0, 1.0, 1.0, 1.0) : blurAmounts;
  471.  
  472. vec4 longBlurHoriz = mix(SL, SC, blurAmounts.x);
  473. longBlurHoriz = mix(SR, longBlurHoriz, blurAmounts.y);
  474.  
  475. vec4 longBlurVert = mix(SU, SC, blurAmounts.z);
  476. longBlurVert = mix(SD, longBlurVert, blurAmounts.w);
  477.  
  478. result = mix(result, longBlurHoriz, passV);
  479. result = mix(result, longBlurVert, passH);
  480. }
  481.  
  482. color.rgb = result.rgb;
  483.  
  484. return color.rgb;
  485. }
  486.  
  487. vec4 DLAAPass(float4 color, float2 texcoord)
  488. {
  489. color.rgb = DLAAPixelShader(color, texcoord);
  490. color.a = AvgLuminance(color.rgb);
  491.  
  492. return color;
  493. }
  494. #endif
  495.  
  496. /*------------------------------------------------------------------------------
  497. [SSAA CODE SECTION]
  498. ------------------------------------------------------------------------------*/
  499.  
  500. #if SSAA == 1
  501. vec4 SSAAPass(vec4 color, vec2 texcoord)
  502. {
  503. float t, l, r, b;
  504. vec2 w = vec2(FilterWidth) * pixelSize;
  505.  
  506. t = AvgLuminance(texture(OGL2Texture, texcoord + vec2(0.0,-1.0) * w, 0.0).xyz),
  507. l = AvgLuminance(texture(OGL2Texture, texcoord + vec2(-1.0,0.0) * w, 0.0).xyz),
  508. r = AvgLuminance(texture(OGL2Texture, texcoord + vec2(1.0, 0.0) * w, 0.0).xyz),
  509. b = AvgLuminance(texture(OGL2Texture, texcoord + vec2(0.0, 1.0) * w, 0.0).xyz);
  510.  
  511. vec2 n = vec2(-(t - b), r - l);
  512.  
  513. float nl = length(n);
  514.  
  515. if (nl < (1.0 / 16.0))
  516. return texture(OGL2Texture, texcoord, 0.0);
  517.  
  518. else
  519. {
  520. n *= pixelSize / nl;
  521.  
  522. vec4 o = texture(OGL2Texture, texcoord, 0.0),
  523. t0 = texture(OGL2Texture, texcoord + n * 0.5, 0.0) * 0.9,
  524. t1 = texture(OGL2Texture, texcoord - n * 0.5, 0.0) * 0.9,
  525. t2 = texture(OGL2Texture, texcoord + n, 0.0) * 0.75,
  526. t3 = texture(OGL2Texture, texcoord - n, 0.0) * 0.75;
  527.  
  528. color = (o + t0 + t1 + t2 + t3) / 4.3;
  529.  
  530. return color;
  531. }
  532. }
  533. #endif
  534.  
  535. /*------------------------------------------------------------------------------
  536. [SFAA AA CODE SECTION]
  537. ------------------------------------------------------------------------------*/
  538.  
  539. #if SFAA == 1
  540. // Ported from AA 1.5 by guest(r)
  541. vec3 SFAAFilter(sampler2D colorSampler, vec2 texcoord)
  542. {
  543. vec3 c11 = texture(colorSampler, gl_TexCoord[0].xy).xyz;
  544. vec3 s00 = texture(colorSampler, gl_TexCoord[1].xy).xyz;
  545. vec3 s20 = texture(colorSampler, gl_TexCoord[2].xy).xyz;
  546. vec3 s22 = texture(colorSampler, gl_TexCoord[3].xy).xyz;
  547. vec3 s02 = texture(colorSampler, gl_TexCoord[4].xy).xyz;
  548. vec3 c00 = texture(colorSampler, gl_TexCoord[5].xy).xyz;
  549. vec3 c22 = texture(colorSampler, gl_TexCoord[6].xy).xyz;
  550. vec3 c20 = texture(colorSampler, gl_TexCoord[5].zw).xyz;
  551. vec3 c02 = texture(colorSampler, gl_TexCoord[6].zw).xyz;
  552. vec3 c10 = texture(colorSampler, gl_TexCoord[1].zw).xyz;
  553. vec3 c21 = texture(colorSampler, gl_TexCoord[2].zw).xyz;
  554. vec3 c12 = texture(colorSampler, gl_TexCoord[3].zw).xyz;
  555. vec3 c01 = texture(colorSampler, gl_TexCoord[4].zw).xyz;
  556.  
  557. vec3 dt = vec3(1.0, 1.0, 1.0);
  558.  
  559. float d1 = dot(abs(c00-c22),dt) +0.001;
  560. float d2 = dot(abs(c20-c02),dt) +0.001;
  561. float hl = dot(abs(c01-c21),dt) +0.001;
  562. float vl = dot(abs(c10-c12),dt) +0.001;
  563. float m1 = dot(abs(s00-s22),dt) +0.001;
  564. float m2 = dot(abs(s02-s20),dt) +0.001;
  565.  
  566. c11 = 0.5 * (m2*(s00+s22)+m1*(s02+s20))/(m1+m2);
  567.  
  568. float k1 = hl+vl, k2 = d1+d2;
  569.  
  570. vec3 t1 = (hl*(c10+c12)+vl*(c01+c21)+k1*c11)/(3.0*(hl+vl));
  571. vec3 t2 = (d1*(c20+c02)+d2*(c00+c22)+k2*c11)/(3.0*(d1+d2));
  572.  
  573. k1 = dot(abs(t1-c11),dt) +0.001;
  574. k2 = dot(abs(t2-c11),dt) +0.001;
  575.  
  576. c11 = (k1*t2 + k2*t1)/(k1+k2);
  577.  
  578. vec3 mn1 = min(min(c00,c01),c02);
  579. vec3 mn2 = min(min(c10,c11),c12);
  580. vec3 mn3 = min(min(c20,c21),c22);
  581.  
  582. vec3 mx1 = max(max(c00,c01),c02);
  583. vec3 mx2 = max(max(c10,c11),c12);
  584. vec3 mx3 = max(max(c20,c21),c22);
  585.  
  586. mn1 = min(min(mn1,mn2),mn3);
  587. mx1 = max(max(mx1,mx2),mx3);
  588.  
  589. vec3 dif1 = abs(c11-mn1) + 0.01 * dt; dif1*=dif1; k1=dot(dif1,dt);
  590. vec3 dif2 = abs(c11-mx1) + 0.01 * dt; dif2*=dif2; k2=dot(dif2,dt);
  591.  
  592. return (k1*mx1+k2*mn1)/(k1+k2);
  593. }
  594.  
  595. float4 SFAAPass(float4 color, float2 texcoord)
  596. {
  597. color.rgb = SFAAFilter(OGL2Texture, texcoord);
  598. color.a = AvgLuminance(color.rgb);
  599.  
  600. return color;
  601. }
  602. #endif
  603.  
  604. /*------------------------------------------------------------------------------
  605. [TEXTURE FILTERING FUNCTIONS]
  606. ------------------------------------------------------------------------------*/
  607.  
  608. float BSpline(float x)
  609. {
  610. float f = x;
  611.  
  612. if (f < 0.0)
  613. {
  614. f = -f;
  615. }
  616. if (f >= 0.0 && f <= 1.0)
  617. {
  618. return (2.0 / 3.0) + (0.5) * (f* f * f) - (f*f);
  619. }
  620. else if (f > 1.0 && f <= 2.0)
  621. {
  622. return 1.0 / 6.0 * pow((2.0 - f), 3.0);
  623. }
  624. return 1.0;
  625. }
  626.  
  627. float CatMullRom(float x)
  628. {
  629. float b = 0.0;
  630. float c = 0.5;
  631. float f = x;
  632.  
  633. if (f < 0.0)
  634. {
  635. f = -f;
  636. }
  637. if (f < 1.0)
  638. {
  639. return ((12.0 - 9.0 * b - 6.0 * c) *
  640. (f * f * f) + (-18.0 + 12.0 * b + 6.0 * c) *
  641. (f * f) + (6.0 - 2.0 * b)) / 6.0;
  642. }
  643. else if (f >= 1.0 && f < 2.0)
  644. {
  645. return ((-b - 6.0 * c) * (f * f * f) +
  646. (6.0 * b + 30.0 * c) *(f *f) +
  647. (-(12.0 * b) - 48.0 * c) * f +
  648. 8.0 * b + 24.0 * c) / 6.0;
  649. }
  650. else
  651. {
  652. return 0.0;
  653. }
  654. }
  655.  
  656. float Bell(float x)
  657. {
  658. float f = (x / 2.0) * 1.5;
  659.  
  660. if (f > -1.5 && f < -0.5)
  661. {
  662. return(0.5 * pow(f + 1.5, 2.0));
  663. }
  664. else if (f > -0.5 && f < 0.5)
  665. {
  666. return 3.0 / 4.0 - (f * f);
  667. }
  668. else if ((f > 0.5 && f < 1.5))
  669. {
  670. return(0.5 * pow(f - 1.5, 2.0));
  671. }
  672. return 0.0;
  673. }
  674.  
  675. float Triangular(float x)
  676. {
  677. x = x / 2.0;
  678.  
  679. if (x < 0.0)
  680. {
  681. return (x + 1.0);
  682. }
  683. else
  684. {
  685. return (1.0 - x);
  686. }
  687. return 0.0;
  688. }
  689.  
  690. float Cubic(float coeff)
  691. {
  692. float4 n = float4(1.0, 2.0, 3.0, 4.0) - coeff;
  693. float4 s = n * n * n;
  694.  
  695. float x = s.x;
  696. float y = s.y - 4.0 * s.x;
  697. float z = s.z - 4.0 * s.y + 6.0 * s.x;
  698. float w = 6.0 - x - y - z;
  699.  
  700. return (x + y + z + w) / 4.0;
  701. }
  702.  
  703. /*------------------------------------------------------------------------------
  704. [BILINEAR FILTERING CODE SECTION]
  705. ------------------------------------------------------------------------------*/
  706.  
  707. #if BILINEAR_FILTERING == 1
  708. float4 SampleBiLinear(sampler2D texSample, float2 texcoord)
  709. {
  710. float texelSizeX = pixelSize.x;
  711. float texelSizeY = pixelSize.y;
  712.  
  713. int nX = int(texcoord.x * screenSize.x);
  714. int nY = int(texcoord.y * screenSize.y);
  715.  
  716. float2 uvCoord = float2((float(nX) + OffsetAmount) / screenSize.x, (float(nY) + OffsetAmount) / screenSize.y);
  717.  
  718. // Take nearest two data in current row.
  719. float4 SampleA = texture(texSample, uvCoord);
  720. float4 SampleB = texture(texSample, uvCoord + float2(texelSizeX, 0.0));
  721.  
  722. // Take nearest two data in bottom row.
  723. float4 SampleC = texture(texSample, uvCoord + float2(0.0, texelSizeY));
  724. float4 SampleD = texture(texSample, uvCoord + float2(texelSizeX, texelSizeY));
  725.  
  726. float LX = frac(texcoord.x * screenSize.x); //Get Interpolation factor for X direction.
  727.  
  728. // Interpolate in X direction.
  729. float4 InterpolateA = lerp(SampleA, SampleB, LX); //Top row in X direction.
  730. float4 InterpolateB = lerp(SampleC, SampleD, LX); //Bottom row in X direction.
  731.  
  732. float LY = frac(texcoord.y * screenSize.y); //Get Interpolation factor for Y direction.
  733.  
  734. return lerp(InterpolateA, InterpolateB, LY); //Interpolate in Y direction.
  735. }
  736.  
  737. float4 BiLinearPass(float4 color, float2 texcoord)
  738. {
  739. float4 bilinear = SampleBiLinear(OGL2Texture, texcoord);
  740. color = lerp(color, bilinear, FilterStrength);
  741.  
  742. return color;
  743. }
  744. #endif
  745.  
  746. /*------------------------------------------------------------------------------
  747. [BICUBIC FILTERING CODE SECTION]
  748. ------------------------------------------------------------------------------*/
  749.  
  750. #if BICUBIC_FILTERING == 1
  751. float4 BicubicFilter(sampler2D texSample, float2 texcoord)
  752. {
  753. float texelSizeX = pixelSize.x;
  754. float texelSizeY = pixelSize.y;
  755.  
  756. float4 nSum = float4(0.0, 0.0, 0.0, 0.0);
  757. float4 nDenom = float4(0.0, 0.0, 0.0, 0.0);
  758.  
  759. float a = frac(texcoord.x * screenSize.x);
  760. float b = frac(texcoord.y * screenSize.y);
  761.  
  762. int nX = int(texcoord.x * screenSize.x);
  763. int nY = int(texcoord.y * screenSize.y);
  764.  
  765. float2 uvCoord = float2(float(nX) / screenSize.x + PixelOffset / screenSize.x,
  766. float(nY) / screenSize.y + PixelOffset / screenSize.y);
  767.  
  768. for (int m = -1; m <= 2; m++) {
  769. for (int n = -1; n <= 2; n++) {
  770.  
  771. float4 Samples = texture(texSample, uvCoord +
  772. float2(texelSizeX * float(m), texelSizeY * float(n)));
  773.  
  774. float vc1 = Interpolation(float(m) - a);
  775. float4 vecCoeff1 = float4(vc1, vc1, vc1, vc1);
  776.  
  777. float vc2 = Interpolation(-(float(n) - b));
  778. float4 vecCoeff2 = float4(vc2, vc2, vc2, vc2);
  779.  
  780. nSum = nSum + (Samples * vecCoeff2 * vecCoeff1);
  781. nDenom = nDenom + (vecCoeff2 * vecCoeff1); }}
  782. return nSum / nDenom;
  783. }
  784.  
  785. float4 BiCubicPass(float4 color, float2 texcoord)
  786. {
  787. float4 bicubic = BicubicFilter(OGL2Texture, texcoord);
  788. color = lerp(color, bicubic, BicubicStrength);
  789. return color;
  790. }
  791. #endif
  792.  
  793. /*------------------------------------------------------------------------------
  794. [DDT FILTERING CODE SECTION]
  795. ------------------------------------------------------------------------------*/
  796.  
  797. #if DDTI_FILTERING == 1
  798. float3 Bilinear(float p, float q, float3 A, float3 B, float3 C, float3 D)
  799. {
  800. return ((1-p)*(1-q)*A + p*(1-q)*B + (1-p)*q*C + p*q*D);
  801. }
  802.  
  803. float3 DDTFilter(sampler2D decal, float2 texcoord)
  804. {
  805. float2 ofx = float2(PixelShift, PixelShift);
  806. float2 pos = frac(gl_TexCoord[2].xy) - ofx;
  807. float2 dir = sign(pos); // dir = pixel direction
  808.  
  809. float2 g1 = dir * gl_TexCoord[1].xy;
  810. float2 g2 = dir * gl_TexCoord[1].zw;
  811.  
  812. float3 A = texture(decal, texcoord).xyz;
  813. float3 B = texture(decal, texcoord +g1).xyz;
  814. float3 C = texture(decal, texcoord +g2).xyz;
  815. float3 D = texture(decal, texcoord +g1+g2).xyz;
  816.  
  817. float a = AvgLuminance(A);
  818. float b = AvgLuminance(B);
  819. float c = AvgLuminance(C);
  820. float d = AvgLuminance(D);
  821.  
  822. float p = abs(pos.x), q = abs(pos.y);
  823. float k = distance(pos,g1), l = distance(pos,g2);
  824. float wd1 = abs(a-d), wd2 = abs(b-c);
  825.  
  826. if (wd1 < wd2)
  827. {
  828. if (k < l)
  829. {
  830. C = A + D - B;
  831. }
  832. else
  833. {
  834. B = A + D - C;
  835. }
  836. }
  837. else if (wd1 > wd2)
  838. {
  839. D = B + C - A;
  840. }
  841.  
  842. float3 color = Bilinear(p, q, A, B, C, D);
  843.  
  844. return color;
  845. }
  846.  
  847. float4 DDTPass(float4 color, float2 texcoord)
  848. {
  849. color.rgb = DDTFilter(OGL2Texture, texcoord);
  850. color.a = AvgLuminance(color.rgb);
  851.  
  852. return color;
  853. }
  854. #endif
  855.  
  856. /*------------------------------------------------------------------------------
  857. [GAUSSIAN FILTERING CODE SECTION]
  858. ------------------------------------------------------------------------------*/
  859.  
  860. #if GAUSSIAN_FILTERING == 1
  861. float4 GaussianPass(float4 color, float2 texcoord)
  862. {
  863. if (screenSize.x < 1024 || screenSize.y < 1024)
  864. {
  865. pixelSize.x /= 2.0;
  866. pixelSize.y /= 2.0;
  867. }
  868.  
  869. float2 dx = float2(pixelSize.x * GaussianSpread, 0.0);
  870. float2 dy = float2(0.0, pixelSize.y * GaussianSpread);
  871.  
  872. float2 dx2 = 2.0 * dx;
  873. float2 dy2 = 2.0 * dy;
  874.  
  875. float4 gaussian = texture(OGL2Texture, texcoord);
  876.  
  877. gaussian += texture(OGL2Texture, texcoord - dx2 + dy2);
  878. gaussian += texture(OGL2Texture, texcoord - dx + dy2);
  879. gaussian += texture(OGL2Texture, texcoord + dy2);
  880. gaussian += texture(OGL2Texture, texcoord + dx + dy2);
  881. gaussian += texture(OGL2Texture, texcoord + dx2 + dy2);
  882.  
  883. gaussian += texture(OGL2Texture, texcoord - dx2 + dy);
  884. gaussian += texture(OGL2Texture, texcoord - dx + dy);
  885. gaussian += texture(OGL2Texture, texcoord + dy);
  886. gaussian += texture(OGL2Texture, texcoord + dx + dy);
  887. gaussian += texture(OGL2Texture, texcoord + dx2 + dy);
  888.  
  889. gaussian += texture(OGL2Texture, texcoord - dx2);
  890. gaussian += texture(OGL2Texture, texcoord - dx);
  891. gaussian += texture(OGL2Texture, texcoord + dx);
  892. gaussian += texture(OGL2Texture, texcoord + dx2);
  893.  
  894. gaussian += texture(OGL2Texture, texcoord - dx2 - dy);
  895. gaussian += texture(OGL2Texture, texcoord - dx - dy);
  896. gaussian += texture(OGL2Texture, texcoord - dy);
  897. gaussian += texture(OGL2Texture, texcoord + dx - dy);
  898. gaussian += texture(OGL2Texture, texcoord + dx2 - dy);
  899.  
  900. gaussian += texture(OGL2Texture, texcoord - dx2 - dy2);
  901. gaussian += texture(OGL2Texture, texcoord - dx - dy2);
  902. gaussian += texture(OGL2Texture, texcoord - dy2);
  903. gaussian += texture(OGL2Texture, texcoord + dx - dy2);
  904. gaussian += texture(OGL2Texture, texcoord + dx2 - dy2);
  905.  
  906. gaussian /= 25.0;
  907.  
  908. color = lerp(color, gaussian, FilterAmount);
  909.  
  910. return color;
  911. }
  912. #endif
  913.  
  914. /*------------------------------------------------------------------------------
  915. [BILATERAL FILTERING CODE SECTION]
  916. ------------------------------------------------------------------------------*/
  917.  
  918. #if BILATERAL_FILTERING || BilateralSampling == 1
  919. // Adapated from LibRetro (Bilateral v1.0 by Sp00kyFox)
  920. #define CWGHT 0.25 // color weight threshold
  921. #define TEX(dx, dy) texture(bTex, texcoord + float2(dx, dy) * pixelSize)
  922.  
  923. float Dist(float3 pt1, float3 pt2)
  924. {
  925. float3 v = pt1 - pt2;
  926. return dot(v,v);
  927. }
  928.  
  929. float sigma = BilateralRadius * BilateralRadius / 2.0;
  930. float cwght = 1.0 + CWGHT * max(1.0, 2.87029746 * sigma + 0.43165242 * BilateralRadius - 0.25219746);
  931.  
  932. float4 Weight(int i, int j, float3 org, float4x3 A)
  933. {
  934. float clr = -BilateralWeight * BilateralWeight;
  935.  
  936. float domain[13] = float[](1.0,
  937. exp( -1.0/sigma), exp( -4.0/sigma), exp( -9.0/sigma), exp( -16.0/sigma), exp( -25.0/sigma), exp( -36.0/sigma),
  938. exp(-49.0/sigma), exp(-64.0/sigma), exp(-81.0/sigma), exp(-100.0/sigma), exp(-121.0/sigma), exp(-144.0/sigma));
  939.  
  940. float4 weight = domain[i] * domain[j] * exp(
  941. float4(Dist(org, A[0]), Dist(org, A[1]), Dist(org, A[2]), Dist(org, A[3])) / clr);
  942.  
  943. return weight;
  944. }
  945.  
  946. float3 BilateralFilter(sampler2D bTex, float2 texcoord)
  947. {
  948. float4x3 A, B;
  949. float norm = cwght;
  950.  
  951. float4 WX, WY, V4 = float4(1.0, 1.0, 1.0, 1.0);
  952. float3 org = TEX(0.0, 0.0).rgb, result = cwght * org;
  953.  
  954. float steps = ceil(BilateralRadius);
  955.  
  956. for(int x = 1; x <= steps; x++)
  957. {
  958. A = float4x3(TEX( x, 0).rgb, TEX(-x, 0).rgb, TEX( 0, x).rgb, TEX( 0,-x).rgb);
  959. B = float4x3(TEX( x, x).rgb, TEX( x,-x).rgb, TEX(-x, x).rgb, TEX(-x,-x).rgb);
  960.  
  961. WX = Weight(x, 0, org, A); WY = Weight(x, x, org, B);
  962.  
  963. result += mul(WX, A) + mul(WY, B);
  964. norm += dot(WX, V4) + dot(WY, V4);
  965.  
  966. for(int y = 1; y < x; y++)
  967. {
  968. A = float4x3(TEX( x, y).rgb, TEX( x,-y).rgb, TEX(-x, y).rgb, TEX(-x,-y).rgb);
  969. B = float4x3(TEX( y, x).rgb, TEX( y,-x).rgb, TEX(-y, x).rgb, TEX(-y,-x).rgb);
  970.  
  971. WX = Weight(x, y, org, A); WY = Weight(y, x, org, B);
  972.  
  973. result += mul(WX, A) + mul(WY, B);
  974. norm += dot(WX, V4) + dot(WY, V4);
  975. }
  976. }
  977.  
  978. return result/norm;
  979. }
  980.  
  981. float4 BilateralPass(float4 color, float2 texcoord)
  982. {
  983. color.rgb = BilateralFilter(OGL2Texture, texcoord);
  984. color.a = AvgLuminance(color.rgb);
  985.  
  986. return color;
  987. }
  988. #endif
  989.  
  990. /*------------------------------------------------------------------------------
  991. [xBR LV2 3D CODE SECTION]
  992. ------------------------------------------------------------------------------*/
  993.  
  994. #if XBRV == 1 || xBRlv2Sampling == 1
  995. #define XBR_RES SampleScale
  996. #define TEX_SIZEX (1024.0 * XBR_RES)
  997. #define TEX_SIZEY (512.0 * XBR_RES)
  998.  
  999. #if DetectionType == 1
  1000. #define CORNER_A
  1001. #elif DetectionType == 2
  1002. #define CORNER_B
  1003. #elif DetectionType == 3
  1004. #define CORNER_C
  1005. #else
  1006. #define CORNER_D
  1007. #endif
  1008.  
  1009. #ifndef CORNER_A
  1010. #define SMOOTH_TIPS
  1011. #endif
  1012.  
  1013. #define XBR_SCALE ScaleFactor
  1014. #define XBR_EQ_THRESHOLD 15.0
  1015. #define lv2_cf 4.0
  1016.  
  1017. const vec3 rgbw = vec3(14.352, 28.176, 5.472);
  1018. const vec4 delta = vec4(1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE, 1.0/XBR_SCALE);
  1019. const vec4 delta_l = vec4(0.5/XBR_SCALE, 1.0/XBR_SCALE, 0.5/XBR_SCALE, 1.0/XBR_SCALE);
  1020. const vec4 delta_u = delta_l.yxwz;
  1021.  
  1022. const vec2 OGLSize = vec2(1024.0, 512.0);
  1023. const vec2 OGLInvSize = 1.0/OGLSize;
  1024.  
  1025. const vec2 dx = vec2( XBR_RES/TEX_SIZEX, 0.0);
  1026. const vec2 dy = vec2( 0.0, XBR_RES/TEX_SIZEY);
  1027.  
  1028. const vec4 Ao = vec4( 1.0, -1.0, -1.0, 1.0 );
  1029. const vec4 Bo = vec4( 1.0, 1.0, -1.0,-1.0 );
  1030. const vec4 Co = vec4( 1.5, 0.5, -0.5, 0.5 );
  1031. const vec4 Ax = vec4( 1.0, -1.0, -1.0, 1.0 );
  1032. const vec4 Bx = vec4( 0.5, 2.0, -0.5,-2.0 );
  1033. const vec4 Cx = vec4( 1.0, 1.0, -0.5, 0.0 );
  1034. const vec4 Ay = vec4( 1.0, -1.0, -1.0, 1.0 );
  1035. const vec4 By = vec4( 2.0, 0.5, -2.0,-0.5 );
  1036. const vec4 Cy = vec4( 2.0, 0.0, -1.0, 0.5 );
  1037. const vec4 Ci = vec4(0.25, 0.25, 0.25, 0.25);
  1038.  
  1039. // Difference between vector components.
  1040. vec4 df(vec4 A, vec4 B)
  1041. {
  1042. return vec4(abs(A-B));
  1043. }
  1044.  
  1045. // Compare two vectors and return their components are different.
  1046. vec4 diff(vec4 A, vec4 B)
  1047. {
  1048. return vec4(notEqual(A, B));
  1049. }
  1050.  
  1051. // Determine if two vector components are equal based on a threshold.
  1052. vec4 eq(vec4 A, vec4 B)
  1053. {
  1054. return (step(df(A, B), vec4(XBR_EQ_THRESHOLD)));
  1055. }
  1056.  
  1057. // Determine if two vector components are NOT equal based on a threshold.
  1058. vec4 neq(vec4 A, vec4 B)
  1059. {
  1060. return (vec4(1.0, 1.0, 1.0, 1.0) - eq(A, B));
  1061. }
  1062.  
  1063. // Weighted distance.
  1064. vec4 wd(vec4 a, vec4 b, vec4 c, vec4 d, vec4 e, vec4 f, vec4 g, vec4 h)
  1065. {
  1066. return (df(a,b) + df(a,c) + df(d,e) + df(d,f) + 4.0*df(g,h));
  1067. }
  1068.  
  1069. float c_df(vec3 c1, vec3 c2)
  1070. {
  1071. vec3 df = abs(c1 - c2);
  1072. return df.r + df.g + df.b;
  1073. }
  1074.  
  1075. vec3 xBRvFilter(sampler2D tex, vec2 texcoord)
  1076. {
  1077. vec4 edri, edr, edr_l, edr_u, px; // px = pixel, edr = edge detection rule
  1078. vec4 irlv0, irlv1, irlv2l, irlv2u, block_3d;
  1079. vec4 fx, fx_l, fx_u; // inequations of straight lines.
  1080.  
  1081. vec2 fp = fract(texcoord * OGLSize);
  1082. vec2 uv = (floor(texcoord * OGLSize) + vec2(0.5, 0.5))/OGLSize;
  1083.  
  1084. vec3 A1 = texture(tex, gl_TexCoord[1].xw ).xyz;
  1085. vec3 B1 = texture(tex, gl_TexCoord[1].yw ).xyz;
  1086. vec3 C1 = texture(tex, gl_TexCoord[1].zw ).xyz;
  1087. vec3 A = texture(tex, gl_TexCoord[2].xw ).xyz;
  1088. vec3 B = texture(tex, gl_TexCoord[2].yw ).xyz;
  1089. vec3 C = texture(tex, gl_TexCoord[2].zw ).xyz;
  1090. vec3 D = texture(tex, gl_TexCoord[3].xw ).xyz;
  1091. vec3 E = texture(tex, gl_TexCoord[3].yw ).xyz;
  1092. vec3 F = texture(tex, gl_TexCoord[3].zw ).xyz;
  1093. vec3 G = texture(tex, gl_TexCoord[4].xw ).xyz;
  1094. vec3 H = texture(tex, gl_TexCoord[4].yw ).xyz;
  1095. vec3 I = texture(tex, gl_TexCoord[4].zw ).xyz;
  1096. vec3 G5 = texture(tex, gl_TexCoord[5].xw ).xyz;
  1097. vec3 H5 = texture(tex, gl_TexCoord[5].yw ).xyz;
  1098. vec3 I5 = texture(tex, gl_TexCoord[5].zw ).xyz;
  1099. vec3 A0 = texture(tex, gl_TexCoord[6].xy ).xyz;
  1100. vec3 D0 = texture(tex, gl_TexCoord[6].xz ).xyz;
  1101. vec3 G0 = texture(tex, gl_TexCoord[6].xw ).xyz;
  1102. vec3 C4 = texture(tex, gl_TexCoord[7].xy ).xyz;
  1103. vec3 F4 = texture(tex, gl_TexCoord[7].xz ).xyz;
  1104. vec3 I4 = texture(tex, gl_TexCoord[7].xw ).xyz;
  1105.  
  1106. vec3 E6 = texture(tex, uv +0.25*dx +0.25*dy).xyz;
  1107. vec3 E7 = texture(tex, uv +0.25*dx -0.25*dy).xyz;
  1108. vec3 E8 = texture(tex, uv -0.25*dx -0.25*dy).xyz;
  1109. vec3 E9 = texture(tex, uv -0.25*dx +0.25*dy).xyz;
  1110.  
  1111. vec3 F6 = texture(tex, uv +dx+0.25*dx +0.25*dy).xyz;
  1112. vec3 F7 = texture(tex, uv +dx+0.25*dx -0.25*dy).xyz;
  1113. vec3 F8 = texture(tex, uv +dx-0.25*dx -0.25*dy).xyz;
  1114. vec3 F9 = texture(tex, uv +dx-0.25*dx +0.25*dy).xyz;
  1115.  
  1116. vec3 B6 = texture(tex, uv +0.25*dx +0.25*dy-dy).xyz;
  1117. vec3 B7 = texture(tex, uv +0.25*dx -0.25*dy-dy).xyz;
  1118. vec3 B8 = texture(tex, uv -0.25*dx -0.25*dy-dy).xyz;
  1119. vec3 B9 = texture(tex, uv -0.25*dx +0.25*dy-dy).xyz;
  1120.  
  1121. vec3 D6 = texture(tex, uv -dx+0.25*dx +0.25*dy).xyz;
  1122. vec3 D7 = texture(tex, uv -dx+0.25*dx -0.25*dy).xyz;
  1123. vec3 D8 = texture(tex, uv -dx-0.25*dx -0.25*dy).xyz;
  1124. vec3 D9 = texture(tex, uv -dx-0.25*dx +0.25*dy).xyz;
  1125.  
  1126. vec3 H6 = texture(tex, uv +0.25*dx +0.25*dy+dy).xyz;
  1127. vec3 H7 = texture(tex, uv +0.25*dx -0.25*dy+dy).xyz;
  1128. vec3 H8 = texture(tex, uv -0.25*dx -0.25*dy+dy).xyz;
  1129. vec3 H9 = texture(tex, uv -0.25*dx +0.25*dy+dy).xyz;
  1130.  
  1131.  
  1132. vec4 b = vec4(dot(B ,rgbw), dot(D ,rgbw), dot(H ,rgbw), dot(F ,rgbw));
  1133. vec4 c = vec4(dot(C ,rgbw), dot(A ,rgbw), dot(G ,rgbw), dot(I ,rgbw));
  1134. vec4 d = b.yzwx;
  1135. vec4 e = vec4(dot(E,rgbw));
  1136. vec4 f = b.wxyz;
  1137. vec4 g = c.zwxy;
  1138. vec4 h = b.zwxy;
  1139. vec4 i = c.wxyz;
  1140. vec4 i4 = vec4(dot(I4,rgbw), dot(C1,rgbw), dot(A0,rgbw), dot(G5,rgbw));
  1141. vec4 i5 = vec4(dot(I5,rgbw), dot(C4,rgbw), dot(A1,rgbw), dot(G0,rgbw));
  1142. vec4 h5 = vec4(dot(H5,rgbw), dot(F4,rgbw), dot(B1,rgbw), dot(D0,rgbw));
  1143. vec4 f4 = h5.yzwx;
  1144.  
  1145. vec4 f0 = vec4(dot(F6,rgbw), dot(B6,rgbw), dot(D6,rgbw), dot(H6,rgbw));
  1146. vec4 f1 = vec4(dot(F7,rgbw), dot(B7,rgbw), dot(D7,rgbw), dot(H7,rgbw));
  1147. vec4 f2 = vec4(dot(F8,rgbw), dot(B8,rgbw), dot(D8,rgbw), dot(H8,rgbw));
  1148. vec4 f3 = vec4(dot(F9,rgbw), dot(B9,rgbw), dot(D9,rgbw), dot(H9,rgbw));
  1149.  
  1150. vec4 h0 = f0.wxyz;
  1151. vec4 h1 = f1.wxyz;
  1152. vec4 h2 = f2.wxyz;
  1153. vec4 h3 = f3.wxyz;
  1154.  
  1155. vec4 e0 = vec4(dot(E6,rgbw), dot(E7,rgbw), dot(E8,rgbw), dot(E9,rgbw));
  1156.  
  1157. // These inequations define the line below which interpolation occurs.
  1158. fx = (Ao*fp.y+Bo*fp.x);
  1159. fx_l = (Ax*fp.y+Bx*fp.x);
  1160. fx_u = (Ay*fp.y+By*fp.x);
  1161.  
  1162. block_3d = vec4(equal(f0,f1))*vec4(equal(f1,f2))*vec4(equal(f2,f3))*
  1163. vec4(equal(h0,h1))*vec4(equal(h1,h2))*vec4(equal(h2,h3));
  1164.  
  1165. irlv1 = irlv0 = block_3d * diff(e,f) * diff(e,h);
  1166.  
  1167. #ifdef CORNER_B
  1168. irlv1 = (irlv0 * (neq(f,b) * neq(h,d) + eq(e,i) * neq(f,i4) * neq(h,i5) + eq(e,g) + eq(e,c)));
  1169. #endif
  1170. #ifdef CORNER_D
  1171. vec4 c1 = i4.yzwx;
  1172. vec4 g0 = i5.wxyz;
  1173. irlv1 = (irlv0 * (neq(f,b) * neq(h,d) + eq(e,i) * neq(f,i4) * neq(h,i5) + eq(e,g) + eq(e,c) ) * (
  1174. diff(f,f4) * diff(f,i) + diff(h,h5) * diff(h,i) + diff(h,g) + diff(f,c) + eq(b,c1) * eq(d,g0)));
  1175. #endif
  1176. #ifdef CORNER_C
  1177. irlv1 = (irlv0 * ( neq(f,b) * neq(f,c) + neq(h,d) * neq(h,g) + eq(e,i) * (
  1178. neq(f,f4) * neq(f,i4) + neq(h,h5) * neq(h,i5)) + eq(e,g) + eq(e,c)));
  1179. #endif
  1180.  
  1181. irlv2l = diff(e,g) * diff(d,g);
  1182. irlv2u = diff(e,c) * diff(b,c);
  1183.  
  1184. vec4 fx45i = clamp((fx + delta -Co - Ci)/(2*delta ), 0.0, 1.0);
  1185. vec4 fx45 = clamp((fx + delta -Co )/(2*delta ), 0.0, 1.0);
  1186. vec4 fx30 = clamp((fx_l + delta_l -Cx )/(2*delta_l), 0.0, 1.0);
  1187. vec4 fx60 = clamp((fx_u + delta_u -Cy )/(2*delta_u), 0.0, 1.0);
  1188.  
  1189. vec4 wd1 = wd( e, c, g, i, h5, f4, h, f);
  1190. vec4 wd2 = wd( h, d, i5, f, i4, b, e, i);
  1191.  
  1192. edri = step(wd1, wd2) * irlv0;
  1193. edr = step(wd1 + vec4(0.1, 0.1, 0.1, 0.1), wd2) * step(vec4(0.5, 0.5, 0.5, 0.5), irlv1);
  1194. edr_l = step(lv2_cf*df(f,g), df(h,c) ) * irlv2l * edr;
  1195. edr_u = step(lv2_cf*df(h,c), df(f,g) ) * irlv2u * edr;
  1196.  
  1197. fx45 = edr * fx45;
  1198. fx30 = edr_l * fx30;
  1199. fx60 = edr_u * fx60;
  1200. fx45i = edri * fx45i;
  1201.  
  1202. px = step(df(e,f), df(e,h));
  1203.  
  1204. #ifdef SMOOTH_TIPS
  1205. vec4 maximos = max(max(fx30, fx60), max(fx45, fx45i));
  1206. #endif
  1207. #ifndef SMOOTH_TIPS
  1208. vec4 maximos = max(max(fx30, fx60), fx45);
  1209. #endif
  1210.  
  1211. vec3 res1 = E;
  1212. res1 = mix(res1, mix(H, F, px.x), maximos.x);
  1213. res1 = mix(res1, mix(B, D, px.z), maximos.z);
  1214.  
  1215. vec3 res2 = E;
  1216. res2 = mix(res2, mix(F, B, px.y), maximos.y);
  1217. res2 = mix(res2, mix(D, H, px.w), maximos.w);
  1218.  
  1219. vec3 res = mix(res1, res2, step(c_df(E, res1), c_df(E, res2)));
  1220.  
  1221. return res;
  1222. }
  1223.  
  1224. float4 xBRPass(float4 color, float2 texcoord)
  1225. {
  1226. color.rgb = xBRvFilter(OGL2Texture, texcoord);
  1227. color.a = AvgLuminance(color.rgb);
  1228.  
  1229. return color;
  1230. }
  1231. #endif
  1232.  
  1233. /*------------------------------------------------------------------------------
  1234. [FXAA CODE SECTION]
  1235. ------------------------------------------------------------------------------*/
  1236.  
  1237. #if FXAA == 1
  1238. #if FxaaQuality == 4
  1239. #define FxaaEdgeThreshold 0.063
  1240. #define FxaaEdgeThresholdMin 0.000
  1241. #elif FxaaQuality == 3
  1242. #define FxaaEdgeThreshold 0.125
  1243. #define FxaaEdgeThresholdMin 0.0312
  1244. #elif FxaaQuality == 2
  1245. #define FxaaEdgeThreshold 0.166
  1246. #define FxaaEdgeThresholdMin 0.0625
  1247. #elif FxaaQuality == 1
  1248. #define FxaaEdgeThreshold 0.250
  1249. #define FxaaEdgeThresholdMin 0.0833
  1250. #endif
  1251.  
  1252. uniform float maxSpan = FxaaSubpixMax;
  1253. uniform float mulReduce = FxaaEdgeThreshold;
  1254. uniform float minReduce = FxaaEdgeThresholdMin;
  1255.  
  1256. vec3 FxaaPixelShader(vec2 texcoord, vec4 pos, vec2 texelStep)
  1257. {
  1258. vec3 rgbM = texture(OGL2Texture, texcoord).rgb;
  1259.  
  1260. // Sampling neighbour texels. Offsets are adapted to OpenGL texture coordinates.
  1261. vec3 rgbNW = textureOffset(OGL2Texture, pos.zw, ivec2(0, 0)).rgb;
  1262. vec3 rgbNE = textureOffset(OGL2Texture, pos.zw, ivec2(1, 0)).rgb;
  1263. vec3 rgbSW = textureOffset(OGL2Texture, pos.zw, ivec2(0, 1)).rgb;
  1264. vec3 rgbSE = textureOffset(OGL2Texture, pos.zw, ivec2(1, 1)).rgb;
  1265.  
  1266. // Convert from RGB to luma.
  1267. float lumaNW = AvgLuminance(rgbNW);
  1268. float lumaNE = AvgLuminance(rgbNE);
  1269. float lumaSW = AvgLuminance(rgbSW);
  1270. float lumaSE = AvgLuminance(rgbSE);
  1271. float lumaM = AvgLuminance(rgbM);
  1272.  
  1273. // Gather minimum and maximum luma.
  1274. float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
  1275. float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
  1276.  
  1277. // Sampling is done along the gradient.
  1278. vec2 direction;
  1279. direction.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
  1280. direction.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
  1281.  
  1282. // Sampling step distance depends on the luma: brighter areas are sharper than dark areas.
  1283. float samplingDirectionReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * 0.25 * mulReduce, minReduce);
  1284.  
  1285. // Factor for norming the sampling direction plus adding the brightness influence.
  1286. float minSamplingDirectionFactor = 1.0 / (min(abs(direction.x), abs(direction.y)) + samplingDirectionReduce);
  1287.  
  1288. // Calculate final sampling direction vector by reducing, clamping to a range and finally adapting to the texture size.
  1289. direction = clamp(direction * minSamplingDirectionFactor, vec2(-maxSpan, -maxSpan), vec2(maxSpan, maxSpan)) * texelStep;
  1290.  
  1291. // Inner samples.
  1292. #if BilateralSampling == 1
  1293. vec3 rgbSampleNeg = BilateralFilter(OGL2Texture, texcoord + direction * (1.0/3.0 - 0.5)).rgb;
  1294. vec3 rgbSamplePos = BilateralFilter(OGL2Texture, texcoord + direction * (2.0/3.0 - 0.5)).rgb;
  1295.  
  1296. #else
  1297. vec3 rgbSampleNeg = texture(OGL2Texture, texcoord + direction * (1.0/3.0 - 0.5)).rgb;
  1298. vec3 rgbSamplePos = texture(OGL2Texture, texcoord + direction * (2.0/3.0 - 0.5)).rgb;
  1299. #endif
  1300.  
  1301. vec3 rgbTwoTab = (rgbSamplePos + rgbSampleNeg) * 0.5;
  1302.  
  1303. // Outer samples.
  1304. vec3 rgbSampleNegOuter = texture(OGL2Texture, texcoord + direction * (0.0/3.0 - 0.5)).rgb;
  1305. vec3 rgbSamplePosOuter = texture(OGL2Texture, texcoord + direction * (3.0/3.0 - 0.5)).rgb;
  1306.  
  1307. vec3 rgbFourTab = (rgbSamplePosOuter + rgbSampleNegOuter) * 0.25 + rgbTwoTab * 0.5;
  1308.  
  1309. // Calculate luma for checking against the minimum and maximum value.
  1310. float lumaFourTab = AvgLuminance(rgbFourTab);
  1311.  
  1312. // Are outer samples of the tab beyond the edge ...
  1313. if (lumaFourTab < lumaMin || lumaFourTab > lumaMax) return rgbTwoTab; // two samples.
  1314. else return rgbFourTab; // four samples.
  1315. }
  1316.  
  1317. #if xBRlv2Sampling == 1
  1318. float diff2D(vec2 coord)
  1319. {
  1320. const vec3 dt = vec3(1.0, 1.0, 1.0);
  1321.  
  1322. vec3 c00 = texture(OGL2Texture, coord + 0.25 * (-dx-dy)).xyz;
  1323. vec3 c20 = texture(OGL2Texture, coord + 0.25 * ( dx-dy)).xyz;
  1324. vec3 c02 = texture(OGL2Texture, coord + 0.25 * (-dx+dy)).xyz;
  1325. vec3 c22 = texture(OGL2Texture, coord + 0.25 * ( dx+dy)).xyz;
  1326.  
  1327. vec3 mn = min(min(c00, c20), min(c02, c22));
  1328. vec3 mx = max(max(c00, c20), max(c02, c22));
  1329.  
  1330. return dot(mx-mn, dt);
  1331. }
  1332.  
  1333. vec3 MergeXBR(vec3 color, vec2 texcoord)
  1334. {
  1335. const float xbrthreshold = xBRThreshold;
  1336.  
  1337. vec2 fp = fract(texcoord * OGLSize);
  1338. vec2 tex = texcoord - fp * OGLInvSize + 0.5 * OGLInvSize;
  1339. vec2 locator = sign(fp - vec2(0.5000000001, 0.5000000001)); //fractional driver bug workaround?
  1340.  
  1341. float maxdiff = max(max(diff2D(tex+locator*dx), diff2D(tex+locator*dy)), diff2D(tex));
  1342.  
  1343. if (maxdiff <= xbrthreshold) { color = xBRvFilter(OGL2Texture, texcoord);}
  1344. else { color = FxaaPixelShader(texcoord, rcpPos, pixelSize); }
  1345.  
  1346. return color;
  1347. }
  1348. #endif
  1349.  
  1350. vec4 FXAAPass(float4 Fxaa, vec2 texcoord)
  1351. {
  1352. #if xBRlv2Sampling == 1
  1353. Fxaa.rgb = MergeXBR(Fxaa.rgb, texcoord);
  1354. #else
  1355. Fxaa.rgb = FxaaPixelShader(texcoord, rcpPos, pixelSize);
  1356. #endif
  1357. Fxaa.a = AvgLuminance(Fxaa.rgb);
  1358.  
  1359. return Fxaa;
  1360. }
  1361. #endif
  1362.  
  1363. /*------------------------------------------------------------------------------
  1364. [BICUBIC SCALER CODE SECTION]
  1365. ------------------------------------------------------------------------------*/
  1366.  
  1367. #if BICUBLIC_SCALER == 1
  1368. float4 BicubicScaler(sampler2D tex, float2 uv, float2 texSize)
  1369. {
  1370. float2 inputSize = float2(1.0/texSize.x, 1.0/texSize.y);
  1371.  
  1372. float2 coord_hg = uv * texSize - 0.5;
  1373. float2 index = floor(coord_hg);
  1374. float2 f = coord_hg - index;
  1375.  
  1376. mat4 M = mat4( -1.0, 3.0,-3.0, 1.0, 3.0,-6.0, 3.0, 0.0,
  1377. -3.0, 0.0, 3.0, 0.0, 1.0, 4.0, 1.0, 0.0 );
  1378. M /= 6.0;
  1379.  
  1380. float4 wx = mul(float4(f.x*f.x*f.x, f.x*f.x, f.x, 1.0), M);
  1381. float4 wy = mul(float4(f.y*f.y*f.y, f.y*f.y, f.y, 1.0), M);
  1382. float2 w0 = float2(wx.x, wy.x);
  1383. float2 w1 = float2(wx.y, wy.y);
  1384. float2 w2 = float2(wx.z, wy.z);
  1385. float2 w3 = float2(wx.w, wy.w);
  1386.  
  1387. float2 g0 = w0 + w1;
  1388. float2 g1 = w2 + w3;
  1389. float2 h0 = w1 / g0 - 1.0;
  1390. float2 h1 = w3 / g1 + 1.0;
  1391.  
  1392. float2 coord00 = index + h0;
  1393. float2 coord10 = index + float2(h1.x, h0.y);
  1394. float2 coord01 = index + float2(h0.x, h1.y);
  1395. float2 coord11 = index + h1;
  1396.  
  1397. coord00 = (coord00 + 0.5) * inputSize;
  1398. coord10 = (coord10 + 0.5) * inputSize;
  1399. coord01 = (coord01 + 0.5) * inputSize;
  1400. coord11 = (coord11 + 0.5) * inputSize;
  1401.  
  1402. float4 tex00 = texture(tex, coord00);
  1403. float4 tex10 = texture(tex, coord10);
  1404. float4 tex01 = texture(tex, coord01);
  1405. float4 tex11 = texture(tex, coord11);
  1406.  
  1407. tex00 = lerp(tex01, tex00, float4(g0.y, g0.y, g0.y, g0.y));
  1408. tex10 = lerp(tex11, tex10, float4(g0.y, g0.y, g0.y, g0.y));
  1409.  
  1410. float4 res = lerp(tex10, tex00, float4(g0.x, g0.x, g0.x, g0.x));
  1411.  
  1412. return res;
  1413. }
  1414.  
  1415. float4 BiCubicScalerPass(float4 color, float2 texcoord)
  1416. {
  1417. color = BicubicScaler(OGL2Texture, texcoord, screenSize);
  1418. return color;
  1419. }
  1420. #endif
  1421.  
  1422. /*------------------------------------------------------------------------------
  1423. [LANCZOS SCALER CODE SECTION]
  1424. ------------------------------------------------------------------------------*/
  1425.  
  1426. #if LANCZOS_SCALER == 1
  1427. float3 PixelPos(float xpos, float ypos)
  1428. {
  1429. return texture(OGL2Texture, float2(xpos, ypos)).rgb;
  1430. }
  1431.  
  1432. float4 WeightQuad(float x)
  1433. {
  1434. #define FIX(c) max(abs(c), 1e-5);
  1435. const float PI = 3.1415926535897932384626433832795;
  1436.  
  1437. float4 weight = FIX(PI * float4(1.0 + x, x, 1.0 - x, 2.0 - x));
  1438. float4 ret = sin(weight) * sin(weight / 2.0) / (weight * weight);
  1439.  
  1440. return ret / dot(ret, float4(1.0, 1.0, 1.0, 1.0));
  1441. }
  1442.  
  1443. float3 LineRun(float ypos, float4 xpos, float4 linetaps)
  1444. {
  1445. return mul(linetaps, float4x3(
  1446. PixelPos(xpos.x, ypos),
  1447. PixelPos(xpos.y, ypos),
  1448. PixelPos(xpos.z, ypos),
  1449. PixelPos(xpos.w, ypos)));
  1450. }
  1451.  
  1452. float4 LanczosScaler(float2 texcoord, float2 inputSize)
  1453. {
  1454. float2 stepxy = float2(1.0/inputSize.x, 1.0/inputSize.y);
  1455. float2 pos = texcoord + stepxy;
  1456. float2 f = frac(pos / stepxy);
  1457.  
  1458. float2 xystart = (-2.0 - f) * stepxy + pos;
  1459. float4 xpos = float4(xystart.x,
  1460. xystart.x + stepxy.x,
  1461. xystart.x + stepxy.x * 2.0,
  1462. xystart.x + stepxy.x * 3.0);
  1463.  
  1464. float4 linetaps = WeightQuad(f.x);
  1465. float4 columntaps = WeightQuad(f.y);
  1466.  
  1467. // final sum and weight normalization
  1468. return float4(mul(columntaps, float4x3(
  1469. LineRun(xystart.y, xpos, linetaps),
  1470. LineRun(xystart.y + stepxy.y, xpos, linetaps),
  1471. LineRun(xystart.y + stepxy.y * 2.0, xpos, linetaps),
  1472. LineRun(xystart.y + stepxy.y * 3.0, xpos, linetaps))), 1.0);
  1473. }
  1474.  
  1475. float4 LanczosScalerPass(float4 color, float2 texcoord)
  1476. {
  1477. color = LanczosScaler(texcoord, screenSize);
  1478. return color;
  1479. }
  1480. #endif
  1481.  
  1482. /*------------------------------------------------------------------------------
  1483. [GAMMA CORRECTION CODE SECTION]
  1484. ------------------------------------------------------------------------------*/
  1485.  
  1486. float3 EncodeGamma(float3 color, float gamma)
  1487. {
  1488. color = saturate(color);
  1489. color.r = (color.r <= 0.0404482362771082) ?
  1490. color.r / 12.92 : pow((color.r + 0.055) / 1.055, gamma);
  1491. color.g = (color.g <= 0.0404482362771082) ?
  1492. color.g / 12.92 : pow((color.g + 0.055) / 1.055, gamma);
  1493. color.b = (color.b <= 0.0404482362771082) ?
  1494. color.b / 12.92 : pow((color.b + 0.055) / 1.055, gamma);
  1495.  
  1496. return color;
  1497. }
  1498.  
  1499. float3 DecodeGamma(float3 color, float gamma)
  1500. {
  1501. color = saturate(color);
  1502. color.r = (color.r <= 0.00313066844250063) ?
  1503. color.r * 12.92 : 1.055 * pow(color.r, 1.0 / gamma) - 0.055;
  1504. color.g = (color.g <= 0.00313066844250063) ?
  1505. color.g * 12.92 : 1.055 * pow(color.g, 1.0 / gamma) - 0.055;
  1506. color.b = (color.b <= 0.00313066844250063) ?
  1507. color.b * 12.92 : 1.055 * pow(color.b, 1.0 / gamma) - 0.055;
  1508.  
  1509. return color;
  1510. }
  1511.  
  1512. #if GAMMA_CORRECTION == 1
  1513. float4 GammaPass(float4 color, float2 texcoord)
  1514. {
  1515. const float GammaConst = 2.233333;
  1516. color.rgb = EncodeGamma(color.rgb, GammaConst);
  1517. color.rgb = DecodeGamma(color.rgb, float(Gamma));
  1518. color.a = AvgLuminance(color.rgb);
  1519.  
  1520. return color;
  1521. }
  1522. #endif
  1523. /*------------------------------------------------------------------------------
  1524. [TEXTURE SHARPEN CODE SECTION]
  1525. ------------------------------------------------------------------------------*/
  1526.  
  1527. #if TEXTURE_SHARPEN == 1
  1528. float4 SampleBicubic(sampler2D texSample, float2 texcoord)
  1529. {
  1530. float texelSizeX = pixelSize.x * float(SharpenBias);
  1531. float texelSizeY = pixelSize.y * float(SharpenBias);
  1532.  
  1533. float4 nSum = float4(0.0, 0.0, 0.0, 0.0);
  1534. float4 nDenom = float4(0.0, 0.0, 0.0, 0.0);
  1535.  
  1536. float a = frac(texcoord.x * screenSize.x);
  1537. float b = frac(texcoord.y * screenSize.y);
  1538.  
  1539. int nX = int(texcoord.x * screenSize.x);
  1540. int nY = int(texcoord.y * screenSize.y);
  1541.  
  1542. float2 uvCoord = float2(float(nX) / screenSize.x, float(nY) / screenSize.y);
  1543.  
  1544. for (int m = -1; m <= 2; m++) {
  1545. for (int n = -1; n <= 2; n++) {
  1546.  
  1547. float4 Samples = texture(texSample, uvCoord +
  1548. float2(texelSizeX * float(m), texelSizeY * float(n)));
  1549.  
  1550. float vc1 = Cubic(float(m) - a);
  1551. float4 vecCoeff1 = float4(vc1, vc1, vc1, vc1);
  1552.  
  1553. float vc2 = Cubic(-(float(n) - b));
  1554. float4 vecCoeff2 = float4(vc2, vc2, vc2, vc2);
  1555.  
  1556. nSum = nSum + (Samples * vecCoeff2 * vecCoeff1);
  1557. nDenom = nDenom + (vecCoeff2 * vecCoeff1); }}
  1558.  
  1559. return nSum / nDenom;
  1560. }
  1561.  
  1562. float4 TexSharpenPass(float4 color, float2 texcoord)
  1563. {
  1564. float3 calcSharpen = lumCoeff * float(SharpenStrength);
  1565.  
  1566. float4 blurredColor = SampleBicubic(OGL2Texture, texcoord);
  1567. float3 sharpenedColor = (color.rgb - blurredColor.rgb);
  1568.  
  1569. float sharpenLuma = dot(sharpenedColor, calcSharpen);
  1570. sharpenLuma = clamp(sharpenLuma, -float(SharpenClamp), float(SharpenClamp));
  1571.  
  1572. color.rgb = color.rgb + sharpenLuma;
  1573. color.a = AvgLuminance(color.rgb);
  1574.  
  1575. #if DebugSharpen == 1
  1576. color = saturate(0.5f + float4(sharpenLuma * 4));
  1577. #endif
  1578.  
  1579. return saturate(color);
  1580. }
  1581. #endif
  1582.  
  1583. /*------------------------------------------------------------------------------
  1584. [PIXEL VIBRANCE CODE SECTION]
  1585. ------------------------------------------------------------------------------*/
  1586.  
  1587. #if PIXEL_VIBRANCE == 1
  1588. float4 VibrancePass(float4 color, float2 texcoord)
  1589. {
  1590. float vib = Vibrance;
  1591.  
  1592. float lum = AvgLuminance(color.rgb);
  1593. float3 luma = float3(lum, lum, lum);
  1594.  
  1595. float colorMax = max(color.r, max(color.g, color.b));
  1596. float colorMin = min(color.r, min(color.g, color.b));
  1597.  
  1598. float colorSaturation = colorMax - colorMin;
  1599. float3 colorCoeff = float3(RedVibrance * vib, GreenVibrance * vib, BlueVibrance * vib);
  1600.  
  1601. color.rgb = lerp(luma, color.rgb, (1.0 + (colorCoeff * (1.0 - (sign(colorCoeff) * colorSaturation)))));
  1602. color.a = AvgLuminance(color.rgb);
  1603.  
  1604. return saturate(color); //Debug: return colorSaturation.xxxx;
  1605. }
  1606. #endif
  1607.  
  1608. /*------------------------------------------------------------------------------
  1609. [BLENDED BLOOM CODE SECTION]
  1610. ------------------------------------------------------------------------------*/
  1611.  
  1612. #if BLENDED_BLOOM == 1
  1613. float3 BlendAddLight(float3 bloom, float3 blend)
  1614. {
  1615. return saturate(bloom + blend);
  1616. }
  1617.  
  1618. float3 BlendScreen(float3 bloom, float3 blend)
  1619. {
  1620. return (bloom + blend) - (bloom * blend);
  1621. }
  1622.  
  1623. float3 BlendGlow(float3 bloom, float3 blend)
  1624. {
  1625. float glow = AvgLuminance(bloom);
  1626. return lerp((bloom + blend) - (bloom * blend),
  1627. (blend + blend) - (blend * blend), glow);
  1628. }
  1629.  
  1630. float3 BlendAddGlow(float3 bloom, float3 blend)
  1631. {
  1632. float addglow = smootherstep(0.0, 1.0, AvgLuminance(bloom));
  1633. return lerp(saturate(bloom + blend),
  1634. (blend + blend) - (blend * blend), addglow);
  1635. }
  1636.  
  1637. float3 BlendLuma(float3 bloom, float3 blend)
  1638. {
  1639. float lumavg = smootherstep(0.0, 1.0, AvgLuminance(bloom + blend));
  1640. return lerp((bloom * blend), (1.0 -
  1641. ((1.0 - bloom) * (1.0 - blend))), lumavg);
  1642. }
  1643.  
  1644. float3 BlendOverlay(float3 bloom, float3 blend)
  1645. {
  1646. float3 overlay = step(0.5, bloom);
  1647. return lerp((bloom * blend * 2.0), (1.0 - (2.0 *
  1648. (1.0 - bloom) * (1.0 - blend))), overlay);
  1649. }
  1650.  
  1651. float3 BloomCorrection(float3 color)
  1652. {
  1653. float3 bloom = color;
  1654.  
  1655. bloom.r = 2.0 / 3.0 * (1.0 - (bloom.r * bloom.r));
  1656. bloom.g = 2.0 / 3.0 * (1.0 - (bloom.g * bloom.g));
  1657. bloom.b = 2.0 / 3.0 * (1.0 - (bloom.b * bloom.b));
  1658.  
  1659. bloom.r = saturate(color.r + float(BloomReds) * bloom.r);
  1660. bloom.g = saturate(color.g + float(BloomGreens) * bloom.g);
  1661. bloom.b = saturate(color.b + float(BloomBlues) * bloom.b);
  1662.  
  1663. color = bloom;
  1664.  
  1665. return color;
  1666. }
  1667.  
  1668. float4 DefocusFilter(sampler2D tex, float2 texcoord, float2 defocus)
  1669. {
  1670. defocus = clamp(defocus, 1.0, 1.5);
  1671. float2 texel = pixelSize * defocus;
  1672.  
  1673. float4 sampleA = texture(tex, texcoord + float2(0.5, 0.5) * texel);
  1674. float4 sampleB = texture(tex, texcoord + float2(-0.5, 0.5) * texel);
  1675. float4 sampleC = texture(tex, texcoord + float2(0.5, -0.5) * texel);
  1676. float4 sampleD = texture(tex, texcoord + float2(-0.5, -0.5) * texel);
  1677.  
  1678. float fx = frac(texcoord.x * screenSize.x);
  1679. float fy = frac(texcoord.y * screenSize.y);
  1680.  
  1681. float4 interpolateA = lerp(sampleA, sampleB, fx);
  1682. float4 interpolateB = lerp(sampleC, sampleD, fx);
  1683.  
  1684. return lerp(interpolateA, interpolateB, fy);
  1685. }
  1686.  
  1687. float4 BloomPass(float4 color, float2 texcoord)
  1688. {
  1689. float anflare = 4.0;
  1690. float width = BloomWidth;
  1691.  
  1692. float2 defocus = float2(BloomDefocus, BloomDefocus);
  1693. float4 bloom = DefocusFilter(OGL2Texture, texcoord, defocus);
  1694.  
  1695. float2 dx = float2(pixelSize.x * width, 0.0);
  1696. float2 dy = float2(0.0, pixelSize.y * width);
  1697.  
  1698. float2 mdx = float2(dx.x * defocus.x, 0.0);
  1699. float2 mdy = float2(0.0, dy.y * defocus.y);
  1700.  
  1701. float4 blend = bloom * 0.22520613262190495;
  1702.  
  1703. blend += 0.002589001911021066 * texture(OGL2Texture, texcoord - mdx + mdy);
  1704. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord - dx + mdy);
  1705. blend += 0.024146616900339800 * texture(OGL2Texture, texcoord + mdy);
  1706. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord + dx + mdy);
  1707. blend += 0.002589001911021066 * texture(OGL2Texture, texcoord + mdx + mdy);
  1708.  
  1709. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord - mdx + dy);
  1710. blend += 0.044875475183061630 * texture(OGL2Texture, texcoord - dx + dy);
  1711. blend += 0.100529757860782610 * texture(OGL2Texture, texcoord + dy);
  1712. blend += 0.044875475183061630 * texture(OGL2Texture, texcoord + dx + dy);
  1713. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord + mdx + dy);
  1714.  
  1715. blend += 0.024146616900339800 * texture(OGL2Texture, texcoord - mdx);
  1716. blend += 0.100529757860782610 * texture(OGL2Texture, texcoord - dx);
  1717. blend += 0.100529757860782610 * texture(OGL2Texture, texcoord + dx);
  1718. blend += 0.024146616900339800 * texture(OGL2Texture, texcoord + mdx);
  1719.  
  1720. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord - mdx - dy);
  1721. blend += 0.044875475183061630 * texture(OGL2Texture, texcoord - dx - dy);
  1722. blend += 0.100529757860782610 * texture(OGL2Texture, texcoord - dy);
  1723. blend += 0.044875475183061630 * texture(OGL2Texture, texcoord + dx - dy);
  1724. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord + mdx - dy);
  1725.  
  1726. blend += 0.002589001911021066 * texture(OGL2Texture, texcoord - mdx - mdy);
  1727. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord - dx - mdy);
  1728. blend += 0.024146616900339800 * texture(OGL2Texture, texcoord - mdy);
  1729. blend += 0.010778807494659370 * texture(OGL2Texture, texcoord + dx - mdy);
  1730. blend += 0.002589001911021066 * texture(OGL2Texture, texcoord + mdx - mdy);
  1731. blend = lerp(color, blend, float(BlendStrength));
  1732.  
  1733. bloom.xyz = BloomType(bloom.xyz, blend.xyz);
  1734. bloom.xyz = BloomCorrection(bloom.xyz);
  1735.  
  1736. color.w = AvgLuminance(color.xyz);
  1737. bloom.w = AvgLuminance(bloom.xyz);
  1738. bloom.w *= anflare;
  1739.  
  1740. color = lerp(color, bloom, float(BloomStrength));
  1741.  
  1742. return color;
  1743. }
  1744. #endif
  1745.  
  1746. /*------------------------------------------------------------------------------
  1747. [SCENE TONE MAPPING CODE SECTION]
  1748. ------------------------------------------------------------------------------*/
  1749.  
  1750. #if SCENE_TONEMAPPING == 1
  1751. float3 ScaleLuminance(float3 x)
  1752. {
  1753. float W = 1.02; // WhitePoint Scale
  1754. float L = 0.16; // Luminance Scale
  1755. float C = 1.02; // Compression Scale
  1756.  
  1757. float N = clamp(0.82 + ToneAmount, 1.0, 2.0);
  1758. float K = (N - L * C) / C;
  1759.  
  1760. float3 tone = L * C + (1.0 - L * C) * (1.0 + K * (x - L) /
  1761. ((W - L) * (W - L))) * (x - L) / (x - L + K);
  1762.  
  1763. float3 color;
  1764. color.r = (x.r > L) ? tone.r : C * x.r;
  1765. color.g = (x.g > L) ? tone.g : C * x.g;
  1766. color.b = (x.b > L) ? tone.b : C * x.b;
  1767.  
  1768. return color;
  1769. }
  1770.  
  1771. float3 TmMask(float3 color)
  1772. {
  1773. float3 tone = color;
  1774.  
  1775. float highTone = 6.2; float greyTone = 0.4;
  1776. float midTone = 1.62; float lowTone = 0.06;
  1777.  
  1778. tone.r = (tone.r * (highTone * tone.r + greyTone))/(
  1779. tone.r * (highTone * tone.r + midTone) + lowTone);
  1780. tone.g = (tone.g * (highTone * tone.g + greyTone))/(
  1781. tone.g * (highTone * tone.g + midTone) + lowTone);
  1782. tone.b = (tone.b * (highTone * tone.b + greyTone))/(
  1783. tone.b * (highTone * tone.b + midTone) + lowTone);
  1784.  
  1785. const float gamma = 2.42;
  1786. tone = EncodeGamma(tone, gamma);
  1787.  
  1788. color = lerp(color, tone, float(MaskStrength));
  1789.  
  1790. return color;
  1791. }
  1792.  
  1793. float3 TmCurve(float3 color)
  1794. {
  1795. float3 T = color;
  1796.  
  1797. float tnamn = ToneAmount;
  1798. float blevel = length(T);
  1799. float bmask = pow(blevel, 0.02);
  1800.  
  1801. float A = 0.100; float B = 0.300;
  1802. float C = 0.100; float D = tnamn;
  1803. float E = 0.020; float F = 0.300;
  1804.  
  1805. float W = 1.000;
  1806.  
  1807. T.r = ((T.r*(A*T.r + C*B) + D*E) / (T.r*(A*T.r + B) + D*F)) - E / F;
  1808. T.g = ((T.g*(A*T.g + C*B) + D*E) / (T.g*(A*T.g + B) + D*F)) - E / F;
  1809. T.b = ((T.b*(A*T.b + C*B) + D*E) / (T.b*(A*T.b + B) + D*F)) - E / F;
  1810.  
  1811. float denom = ((W*(A*W + C*B) + D*E) / (W*(A*W + B) + D*F)) - E / F;
  1812.  
  1813. float3 black = float3(bmask, bmask, bmask);
  1814. float3 white = float3(denom, denom, denom);
  1815.  
  1816. T = T / white;
  1817. T = T * black;
  1818.  
  1819. color = saturate(T);
  1820.  
  1821. return color;
  1822. }
  1823.  
  1824. float4 TonemapPass(float4 color, float2 texcoord)
  1825. {
  1826. float L = Luminance;
  1827.  
  1828. float3 tonemap = color.rgb;
  1829. float3 luma = float3(L, L, L);
  1830.  
  1831. float blackLevel = length(tonemap);
  1832. tonemap = ScaleLuminance(tonemap);
  1833.  
  1834. float luminanceAverage = AvgLuminance(luma);
  1835.  
  1836. if (TonemapMask == 1) { tonemap = TmMask(tonemap); }
  1837. if (TonemapType == 1) { tonemap = TmCurve(tonemap); }
  1838.  
  1839. // RGB -> XYZ conversion
  1840. float3 XYZ = RGBtoXYZ(tonemap);
  1841.  
  1842. // XYZ -> Yxy conversion
  1843. float3 Yxy;
  1844.  
  1845. Yxy.r = XYZ.g; // copy luminance Y
  1846. Yxy.g = XYZ.r / (XYZ.r + XYZ.g + XYZ.b); // x = X / (X + Y + Z)
  1847. Yxy.b = XYZ.g / (XYZ.r + XYZ.g + XYZ.b); // y = Y / (X + Y + Z)
  1848.  
  1849. // (Wt) Tone mapped scaling of the initial wp before input modifiers
  1850. float Wt = saturate(Yxy.r / AvgLuminance(XYZ));
  1851.  
  1852. if (TonemapType == 2) { Yxy.r = TmCurve(Yxy).r; }
  1853.  
  1854. // (Lp) Map average luminance to the middlegrey zone by scaling pixel luminance
  1855. float Lp = Yxy.r * float(Exposure) / (luminanceAverage + Epsilon);
  1856.  
  1857. // (Wp) White point calculated, based on the toned white, and input modifier
  1858. float Wp = dot(abs(Wt), float(WhitePoint));
  1859.  
  1860. // (Ld) Scale all luminance within a displayable range of 0 to 1
  1861. Yxy.r = (Lp * (1.0 + Lp / (Wp * Wp))) / (1.0 + Lp);
  1862.  
  1863. // Yxy -> XYZ conversion
  1864. XYZ.r = Yxy.r * Yxy.g / Yxy.b; // X = Y * x / y
  1865. XYZ.g = Yxy.r; // copy luminance Y
  1866. XYZ.b = Yxy.r * (1.0 - Yxy.g - Yxy.b) / Yxy.b; // Z = Y * (1-x-y) / y
  1867.  
  1868. if (TonemapType == 3) { XYZ = TmCurve(XYZ); }
  1869.  
  1870. // XYZ -> RGB conversion
  1871. tonemap = XYZtoRGB(XYZ);
  1872.  
  1873. float shadowmask = pow(saturate(blackLevel), float(BlackLevels));
  1874. tonemap = tonemap * float3(shadowmask, shadowmask, shadowmask);
  1875.  
  1876. color.rgb = tonemap;
  1877. color.a = AvgLuminance(color.rgb);
  1878.  
  1879. return color;
  1880. }
  1881. #endif
  1882.  
  1883. /*------------------------------------------------------------------------------
  1884. [CROSS PROCESSING CODE SECTION]
  1885. ------------------------------------------------------------------------------*/
  1886.  
  1887. #if CROSS_PROCESSING == 1
  1888. float3 CrossShift(float3 color)
  1889. {
  1890. float3 cross;
  1891.  
  1892. float2 CrossMatrix[3] = float2[](
  1893. float2 (0.960, 0.040 * color.x),
  1894. float2 (0.980, 0.020 * color.y),
  1895. float2 (0.970, 0.030 * color.z) );
  1896.  
  1897. cross.x = float(RedShift) * CrossMatrix[0].x + CrossMatrix[0].y;
  1898. cross.y = float(GreenShift) * CrossMatrix[1].x + CrossMatrix[1].y;
  1899. cross.z = float(BlueShift) * CrossMatrix[2].x + CrossMatrix[2].y;
  1900.  
  1901. float lum = AvgLuminance(color);
  1902. float3 black = float3(0.0, 0.0, 0.0);
  1903. float3 white = float3(1.0, 1.0, 1.0);
  1904.  
  1905. cross = lerp(black, cross, saturate(lum * 2.0));
  1906. cross = lerp(cross, white, saturate(lum - 0.5) * 2.0);
  1907. color = lerp(color, cross, saturate(lum * float(ShiftRatio)));
  1908.  
  1909. return color;
  1910. }
  1911.  
  1912. float4 CrossPass(float4 color, float2 texcoord)
  1913. {
  1914. #if FilmicProcess == 1
  1915. color.rgb = CrossShift(color.rgb);
  1916.  
  1917. #elif FilmicProcess == 2
  1918. float3 XYZ = RGBtoXYZ(color.rgb);
  1919. float3 Yxy = XYZtoYxy(XYZ);
  1920.  
  1921. Yxy = CrossShift(Yxy);
  1922. XYZ = YxytoXYZ(Yxy);
  1923.  
  1924. color.rgb = XYZtoRGB(XYZ);
  1925.  
  1926. #elif FilmicProcess == 3
  1927. float3 XYZ = RGBtoXYZ(color.rgb);
  1928. float3 Yxy = XYZtoYxy(XYZ);
  1929.  
  1930. XYZ = YxytoXYZ(Yxy);
  1931. XYZ = CrossShift(XYZ);
  1932.  
  1933. color.rgb = XYZtoRGB(XYZ);
  1934. #endif
  1935.  
  1936. color.a = AvgLuminance(color.rgb);
  1937.  
  1938. return saturate(color);
  1939. }
  1940. #endif
  1941.  
  1942. /*------------------------------------------------------------------------------
  1943. [COLOR CORRECTION CODE SECTION]
  1944. ------------------------------------------------------------------------------*/
  1945.  
  1946. // Converting pure hue to RGB
  1947. float3 HUEtoRGB(float H)
  1948. {
  1949. float R = abs(H * 6.0 - 3.0) - 1.0;
  1950. float G = 2.0 - abs(H * 6.0 - 2.0);
  1951. float B = 2.0 - abs(H * 6.0 - 4.0);
  1952.  
  1953. return saturate(float3(R, G, B));
  1954. }
  1955.  
  1956. // Converting RGB to hue/chroma/value
  1957. float3 RGBtoHCV(float3 RGB)
  1958. {
  1959. float4 BG = float4(RGB.bg,-1.0, 2.0 / 3.0);
  1960. float4 GB = float4(RGB.gb, 0.0,-1.0 / 3.0);
  1961.  
  1962. float4 P = (RGB.g < RGB.b) ? BG : GB;
  1963.  
  1964. float4 XY = float4(P.xyw, RGB.r);
  1965. float4 YZ = float4(RGB.r, P.yzx);
  1966.  
  1967. float4 Q = (RGB.r < P.x) ? XY : YZ;
  1968.  
  1969. float C = Q.x - min(Q.w, Q.y);
  1970. float H = abs((Q.w - Q.y) / (6.0 * C + Epsilon) + Q.z);
  1971.  
  1972. return float3(H, C, Q.x);
  1973. }
  1974.  
  1975. // Converting RGB to HSV
  1976. float3 RGBtoHSV(float3 RGB)
  1977. {
  1978. float3 HCV = RGBtoHCV(RGB);
  1979. float S = HCV.y / (HCV.z + Epsilon);
  1980.  
  1981. return float3(HCV.x, S, HCV.z);
  1982. }
  1983.  
  1984. // Converting HSV to RGB
  1985. float3 HSVtoRGB(float3 HSV)
  1986. {
  1987. float3 RGB = HUEtoRGB(HSV.x);
  1988. return ((RGB - 1.0) * HSV.y + 1.0) * HSV.z;
  1989. }
  1990.  
  1991. #if COLOR_CORRECTION == 1
  1992. // Pre correction color mask
  1993. float3 PreCorrection(float3 color)
  1994. {
  1995. float3 RGB = color;
  1996.  
  1997. RGB.r = 2.0 / 3.0 * (1.0 - (RGB.r * RGB.r));
  1998. RGB.g = 2.0 / 3.0 * (1.0 - (RGB.g * RGB.g));
  1999. RGB.b = 2.0 / 3.0 * (1.0 - (RGB.b * RGB.b));
  2000.  
  2001. RGB.r = saturate(color.r + (float(ChannelR) / 200.0) * RGB.r);
  2002. RGB.g = saturate(color.g + (float(ChannelG) / 200.0) * RGB.g);
  2003. RGB.b = saturate(color.b + (float(ChannelB) / 200.0) * RGB.b);
  2004.  
  2005. color = saturate(RGB);
  2006.  
  2007. return color;
  2008. }
  2009.  
  2010. float3 ColorCorrection(float3 color)
  2011. {
  2012. float X = 1.0 / (1.0 + exp(float(ChannelR) / 2.0));
  2013. float Y = 1.0 / (1.0 + exp(float(ChannelG) / 2.0));
  2014. float Z = 1.0 / (1.0 + exp(float(ChannelB) / 2.0));
  2015.  
  2016. color.r = (1.0 / (1.0 + exp(float(-ChannelR) * (color.r - 0.5))) - X) / (1.0 - 2.0 * X);
  2017. color.g = (1.0 / (1.0 + exp(float(-ChannelG) * (color.g - 0.5))) - Y) / (1.0 - 2.0 * Y);
  2018. color.b = (1.0 / (1.0 + exp(float(-ChannelB) * (color.b - 0.5))) - Z) / (1.0 - 2.0 * Z);
  2019.  
  2020. return saturate(color);
  2021. }
  2022.  
  2023. float4 CorrectionPass(float4 color, float2 texcoord)
  2024. {
  2025. float3 colorspace = PreCorrection(color.rgb);
  2026.  
  2027. #if CorrectionPalette == 1
  2028. colorspace = ColorCorrection(colorspace);
  2029.  
  2030. #elif CorrectionPalette == 2
  2031. float3 XYZ = RGBtoXYZ(colorspace);
  2032. float3 Yxy = XYZtoYxy(XYZ);
  2033.  
  2034. Yxy = ColorCorrection(Yxy);
  2035. XYZ = YxytoXYZ(Yxy);
  2036. colorspace = XYZtoRGB(XYZ);
  2037.  
  2038. #elif CorrectionPalette == 3
  2039. float3 XYZ = RGBtoXYZ(colorspace);
  2040. float3 Yxy = XYZtoYxy(XYZ);
  2041.  
  2042. XYZ = YxytoXYZ(Yxy);
  2043. XYZ = ColorCorrection(XYZ);
  2044. colorspace = XYZtoRGB(XYZ);
  2045.  
  2046. #elif CorrectionPalette == 4
  2047. float3 hsv = RGBtoHSV(colorspace);
  2048. hsv = ColorCorrection(hsv);
  2049. colorspace = HSVtoRGB(hsv);
  2050.  
  2051. #elif CorrectionPalette == 5
  2052. float3 yuv = RGBtoYUV(colorspace);
  2053. yuv = ColorCorrection(yuv);
  2054. colorspace = YUVtoRGB(yuv);
  2055. #endif
  2056.  
  2057. color.rgb = lerp(color.rgb, colorspace, float(PaletteStrength));
  2058. color.a = AvgLuminance(color.rgb);
  2059.  
  2060. return color;
  2061. }
  2062. #endif
  2063.  
  2064.  
  2065. /*------------------------------------------------------------------------------
  2066. [TV COLORS EMU CODE SECTION]
  2067. ------------------------------------------------------------------------------*/
  2068.  
  2069. #if NTSC_TV_EMULATION == 1
  2070. // conversion from NTSC RGB Reference White D65 ( color space used by NA/Japan TV's ) to XYZ
  2071. vec3 NTSC(vec3 c)
  2072. {
  2073. vec3 v = vec3(pow(c.r, 2.2), pow(c.g, 2.2), pow(c.b, 2.2)); //Inverse Companding
  2074. return RGBtoXYZ(v);
  2075. }
  2076.  
  2077. // conversion from XYZ to sRGB Reference White D65 ( color space used by windows )
  2078. vec3 sRGB(vec3 c)
  2079. {
  2080. vec3 v = XYZtoSRGB(c);
  2081. v = DecodeGamma(v, 2.4); //Companding
  2082.  
  2083. return v;
  2084. }
  2085.  
  2086. // NTSC RGB to sRGB
  2087. vec3 NTSCtoSRGB(vec3 c)
  2088. {
  2089. return sRGB(NTSC(c));
  2090. }
  2091.  
  2092. // Ported from SimoneT TV colors emulation 1.0 Shader
  2093. vec4 TvColorsPass(vec4 color, vec2 texcoord)
  2094. {
  2095. color.rgb = NTSCtoSRGB(color.rgb);
  2096. color.a = AvgLuminance(color.rgb);
  2097.  
  2098. return color;
  2099. }
  2100. #endif
  2101.  
  2102. /*------------------------------------------------------------------------------
  2103. [S-CURVE CONTRAST CODE SECTION]
  2104. ------------------------------------------------------------------------------*/
  2105.  
  2106. #if CURVE_CONTRAST == 1
  2107. float4 ContrastPass(float4 color, float2 texcoord)
  2108. {
  2109. float CurveBlend = CurvesContrast;
  2110.  
  2111. #if CurveType != 2
  2112. float lum = AvgLuminance(color.rgb);
  2113. float3 luma = float3(lum, lum, lum);
  2114. float3 chroma = color.rgb - luma;
  2115. #endif
  2116.  
  2117. #if CurveType == 2
  2118. float3 x = color.rgb;
  2119. #elif (CurveType == 1)
  2120. float3 x = chroma;
  2121. x = x * 0.5 + 0.5;
  2122. #else
  2123. float3 x = luma;
  2124. #endif
  2125.  
  2126. //S-Curve - Cubic Bezier spline
  2127. float3 a = float3(0.00, 0.00, 0.00); //start point
  2128. float3 b = float3(0.25, 0.25, 0.25); //control point 1
  2129. float3 c = float3(0.85, 0.85, 0.85); //control point 2
  2130. float3 d = float3(1.00, 1.00, 1.00); //endpoint
  2131.  
  2132. float3 ab = lerp(a, b, x); //point between a and b (green)
  2133. float3 bc = lerp(b, c, x); //point between b and c (green)
  2134. float3 cd = lerp(c, d, x); //point between c and d (green)
  2135. float3 abbc = lerp(ab, bc, x); //point between ab and bc (blue)
  2136. float3 bccd = lerp(bc, cd, x); //point between bc and cd (blue)
  2137. float3 dest = lerp(abbc, bccd, x); //point on the bezier-curve (black)
  2138.  
  2139. x = dest;
  2140.  
  2141. #if CurveType == 0 //Only Luma
  2142. x = lerp(luma, x, CurveBlend);
  2143. color.rgb = x + chroma;
  2144. #elif (CurveType == 1) //Only Chroma
  2145. x = x * 2 - 1;
  2146. float3 LColor = luma + x;
  2147. color.rgb = lerp(color.rgb, LColor, CurveBlend);
  2148. #elif (CurveType == 2) //Both Luma and Chroma
  2149. float3 LColor = x;
  2150. color.rgb = lerp(color.rgb, LColor, CurveBlend);
  2151. #endif
  2152.  
  2153. color.a = AvgLuminance(color.rgb);
  2154.  
  2155. return saturate(color);
  2156. }
  2157. #endif
  2158.  
  2159. /*------------------------------------------------------------------------------
  2160. [CEL SHADING CODE SECTION]
  2161. ------------------------------------------------------------------------------*/
  2162.  
  2163. #if CEL_SHADING == 1
  2164. float3 CelColor(in float3 RGB)
  2165. {
  2166. float3 HSV;
  2167. float CR = 1.0 / ColorRange, MS = Saturation, ML;
  2168.  
  2169. HSV = RGBtoHSV(RGB);
  2170.  
  2171. ML = fmod(HSV[2], CR);
  2172. HSV[1] *= MS; HSV[2] += ((CR * (HSV[2] + 0.6)) - ML);
  2173. HSV[2] = clamp(HSV[2], float(int(HSV[2] / CR) - 1) * CR, float(int(HSV[2] / CR) + 1) * CR);
  2174.  
  2175. RGB = HSVtoRGB(HSV);
  2176.  
  2177. return RGB;
  2178. }
  2179.  
  2180. float4 CelPass(float4 color, float2 texcoord)
  2181. {
  2182. const int NUM = 9;
  2183.  
  2184. float3 yuv, sum = color.rgb;
  2185. float2 width = float2(0.1, 0.1);
  2186. float rounding = RoundingLevels;
  2187.  
  2188. float lum[NUM];
  2189. float3 col[NUM];
  2190. float2 set[NUM] = float2[](
  2191. float2(-0.0078125, -0.0078125),
  2192. float2(0.00, -0.0078125),
  2193. float2(0.0078125, -0.0078125),
  2194. float2(-0.0078125, 0.00),
  2195. float2(0.00, 0.00),
  2196. float2(0.0078125, 0.00),
  2197. float2(-0.0078125, 0.0078125),
  2198. float2(0.00, 0.0078125),
  2199. float2(0.0078125, 0.0078125) );
  2200.  
  2201. for (int i = 0; i < NUM; i++)
  2202. {
  2203. col[i] = texture(OGL2Texture, texcoord + set[i] * width).rgb;
  2204. col[i] = CelColor(col[i]);
  2205.  
  2206. #if ColorRounding == 1
  2207. col[i].r = round(col[i].r * rounding) / rounding;
  2208. col[i].g = round(col[i].g * rounding) / rounding;
  2209. col[i].b = round(col[i].b * rounding) / rounding;
  2210. #endif
  2211.  
  2212. lum[i] = AvgLuminance(col[i].xyz);
  2213. yuv = RGBtoYUV(col[i]);
  2214.  
  2215. #if UseYuvLuma == 1
  2216. yuv.r = round(yuv.r * rounding) / rounding;
  2217. #endif
  2218.  
  2219. yuv = YUVtoRGB(yuv);
  2220. sum += yuv;
  2221. }
  2222.  
  2223. float3 shaded = sum / NUM;
  2224. float3 shadedColor = lerp(color.rgb, shaded, ShadedStrength);
  2225.  
  2226. float cs; float4 offset; float4 pos0 = texcoord.xyxy;
  2227. float px = textureSize(OGL2Texture, 0).x * float(EdgeScale);
  2228. float py = textureSize(OGL2Texture, 0).y * float(EdgeScale);
  2229.  
  2230. offset.xy = -(offset.zw = float2(1.0/px, 0.0));
  2231. float4 pos1 = pos0 + offset;
  2232.  
  2233. offset.xy = -(offset.zw = float2(0.0, 1.0/py));
  2234. float4 pos2 = pos0 + offset;
  2235.  
  2236. float4 pos3 = pos1 + 2.0 * offset;
  2237.  
  2238. float3 c0 = texture(OGL2Texture, pos3.xy).rgb;
  2239. float3 c1 = texture(OGL2Texture, pos2.xy).rgb;
  2240. float3 c2 = texture(OGL2Texture, pos3.zy).rgb;
  2241. float3 c3 = texture(OGL2Texture, pos1.xy).rgb;
  2242. float3 c5 = texture(OGL2Texture, pos1.zw).rgb;
  2243. float3 c6 = texture(OGL2Texture, pos3.xw).rgb;
  2244. float3 c7 = texture(OGL2Texture, pos2.zw).rgb;
  2245. float3 c8 = texture(OGL2Texture, pos3.zw).rgb;
  2246.  
  2247. float3 o = float3(1.0, 1.0, 1.0);
  2248. float3 h = float3(0.02, 0.02, 0.02);
  2249.  
  2250. float3 hz = h; float k = 0.02; float kz = EdgeFilter / 100.0;
  2251. float3 cz = (color.rgb + h) / (dot(o, color.rgb) + k);
  2252.  
  2253. hz = (cz - ((c0 + h) / (dot(o, c0) + k))); cs = kz / (dot(hz, hz) + kz);
  2254. hz = (cz - ((c1 + h) / (dot(o, c1) + k))); cs += kz / (dot(hz, hz) + kz);
  2255. hz = (cz - ((c2 + h) / (dot(o, c2) + k))); cs += kz / (dot(hz, hz) + kz);
  2256. hz = (cz - ((c3 + h) / (dot(o, c3) + k))); cs += kz / (dot(hz, hz) + kz);
  2257. hz = (cz - ((c5 + h) / (dot(o, c5) + k))); cs += kz / (dot(hz, hz) + kz);
  2258. hz = (cz - ((c6 + h) / (dot(o, c6) + k))); cs += kz / (dot(hz, hz) + kz);
  2259. hz = (cz - ((c7 + h) / (dot(o, c7) + k))); cs += kz / (dot(hz, hz) + kz);
  2260. hz = (cz - ((c8 + h) / (dot(o, c8) + k))); cs += kz / (dot(hz, hz) + kz);
  2261.  
  2262. cs /= 8.0;
  2263.  
  2264. color.rgb = lerp(color.rgb, shadedColor, AvgLuminance(color.rgb)) * pow(cs, CelStrength);
  2265. color.a = AvgLuminance(color.rgb);
  2266.  
  2267. return color;
  2268. }
  2269. #endif
  2270.  
  2271. /*------------------------------------------------------------------------------
  2272. [PAINT SHADING CODE SECTION]
  2273. ------------------------------------------------------------------------------*/
  2274.  
  2275. #if PAINT_SHADING == 1
  2276. float3 PaintShading(float3 color, float2 texcoord)
  2277. {
  2278. float px = textureSize(OGL2Texture, 0).x * float(ResScale);
  2279. float py = textureSize(OGL2Texture, 0).y * float(ResScale);
  2280.  
  2281. float3 m0, m1, m2, m3, k0, k1, k2, k3, shade; int j, i;
  2282. float n = float((PaintRadius + 1.0) * (PaintRadius + 1.0));
  2283.  
  2284. for (j = int(-PaintRadius); j <= 0; ++j) {
  2285. for (i = int(-PaintRadius); i <= 0; ++i) {
  2286.  
  2287. shade = texture(OGL2Texture, texcoord + float2(i, j) / float2(px, py)).rgb;
  2288. m0 += shade; k0 += shade * shade; }}
  2289.  
  2290. for (j = int(-PaintRadius); j <= 0; ++j) {
  2291. for (i = 0; i <= int(PaintRadius); ++i) {
  2292.  
  2293. shade = texture(OGL2Texture, texcoord + float2(i, j) / float2(px, py)).rgb;
  2294. m1 += shade; k1 += shade * shade; }}
  2295.  
  2296. for (j = 0; j <= int(PaintRadius); ++j) {
  2297. for (i = 0; i <= int(PaintRadius); ++i) {
  2298.  
  2299. shade = texture(OGL2Texture, texcoord + float2(i, j) / float2(px, py)).rgb;
  2300. m2 += shade; k2 += shade * shade; }}
  2301.  
  2302. float min_sigma2 = 1e+2;
  2303. m0 /= n; k0 = abs(k0 / n - m0 * m0);
  2304.  
  2305. float sigma2 = k0.r + k0.g + k0.b;
  2306. if (sigma2 < min_sigma2) {
  2307. min_sigma2 = sigma2; color = m0; }
  2308.  
  2309. m1 /= n; k1 = abs(k1 / n - m1 * m1);
  2310. sigma2 = k1.r + k1.g + k1.b;
  2311.  
  2312. if (sigma2 < min_sigma2) {
  2313. min_sigma2 = sigma2; color = m1; }
  2314.  
  2315. m2 /= n; k2 = abs(k2 / n - m2 * m2);
  2316. sigma2 = k2.r + k2.g + k2.b;
  2317.  
  2318. if (sigma2 < min_sigma2) {
  2319. min_sigma2 = sigma2; color = m2; }
  2320.  
  2321. #if UseCelEdges == 1
  2322. float cs; float4 offset; float4 pos0 = texcoord.xyxy;
  2323. float ppx = textureSize(OGL2Texture, 0).x * float(EdgeScale);
  2324. float ppy = textureSize(OGL2Texture, 0).y * float(EdgeScale);
  2325.  
  2326. offset.xy = -(offset.zw = float2(1.0/ppx, 0.0));
  2327. float4 pos1 = pos0 + offset;
  2328.  
  2329. offset.xy = -(offset.zw = float2(0.0, 1.0/ppy));
  2330. float4 pos2 = pos0 + offset;
  2331.  
  2332. float4 pos3 = pos1 + 2.0 * offset;
  2333.  
  2334. float3 c0 = texture(OGL2Texture, pos3.xy).rgb;
  2335. float3 c1 = texture(OGL2Texture, pos2.xy).rgb;
  2336. float3 c2 = texture(OGL2Texture, pos3.zy).rgb;
  2337. float3 c3 = texture(OGL2Texture, pos1.xy).rgb;
  2338. float3 c5 = texture(OGL2Texture, pos1.zw).rgb;
  2339. float3 c6 = texture(OGL2Texture, pos3.xw).rgb;
  2340. float3 c7 = texture(OGL2Texture, pos2.zw).rgb;
  2341. float3 c8 = texture(OGL2Texture, pos3.zw).rgb;
  2342.  
  2343. float3 o = float3(1.0, 1.0, 1.0);
  2344. float3 h = float3(0.02, 0.02, 0.02);
  2345.  
  2346. float3 hz = h; float k = 0.02; float kz = EdgeFilter / 100.0;
  2347. float3 cz = (color.rgb + h) / (dot(o, color.rgb) + k);
  2348.  
  2349. hz = (cz - ((c0 + h) / (dot(o, c0) + k))); cs = kz / (dot(hz, hz) + kz);
  2350. hz = (cz - ((c1 + h) / (dot(o, c1) + k))); cs += kz / (dot(hz, hz) + kz);
  2351. hz = (cz - ((c2 + h) / (dot(o, c2) + k))); cs += kz / (dot(hz, hz) + kz);
  2352. hz = (cz - ((c3 + h) / (dot(o, c3) + k))); cs += kz / (dot(hz, hz) + kz);
  2353. hz = (cz - ((c5 + h) / (dot(o, c5) + k))); cs += kz / (dot(hz, hz) + kz);
  2354. hz = (cz - ((c6 + h) / (dot(o, c6) + k))); cs += kz / (dot(hz, hz) + kz);
  2355. hz = (cz - ((c7 + h) / (dot(o, c7) + k))); cs += kz / (dot(hz, hz) + kz);
  2356. hz = (cz - ((c8 + h) / (dot(o, c8) + k))); cs += kz / (dot(hz, hz) + kz);
  2357.  
  2358. cs /= 8.0;
  2359.  
  2360. color.rgb *= pow(cs, CelStrength);
  2361. #endif
  2362.  
  2363. return color;
  2364. }
  2365.  
  2366. float4 PaintPass(float4 color, float2 texcoord)
  2367. {
  2368. float3 paint = PaintShading(color.rgb, texcoord);
  2369. color.rgb = lerp(color.rgb, paint, float(PaintStrength));
  2370. color.a = AvgLuminance(color.rgb);
  2371.  
  2372. return color;
  2373. }
  2374. #endif
  2375.  
  2376.  
  2377. /*------------------------------------------------------------------------------
  2378. [COLOR GRADING CODE SECTION]
  2379. ------------------------------------------------------------------------------*/
  2380.  
  2381. #if COLOR_GRADING == 1
  2382. float RGBCVtoHUE(float3 RGB, float C, float V)
  2383. {
  2384. float3 Delta = (V - RGB) / C;
  2385.  
  2386. Delta.rgb -= Delta.brg;
  2387. Delta.rgb += float3(2.0, 4.0, 6.0);
  2388. Delta.brg = step(V, RGB) * Delta.brg;
  2389.  
  2390. float H;
  2391. H = max(Delta.r, max(Delta.g, Delta.b));
  2392. return frac(H / 6);
  2393. }
  2394.  
  2395. float3 HSVComplement(float3 HSV)
  2396. {
  2397. float3 complement = HSV;
  2398. complement.x -= 0.5;
  2399.  
  2400. if (complement.x < 0.0) { complement.x += 1.0; }
  2401. return(complement);
  2402. }
  2403.  
  2404. float HueLerp(float h1, float h2, float v)
  2405. {
  2406. float d = abs(h1 - h2);
  2407.  
  2408. if (d <= 0.5)
  2409. { return lerp(h1, h2, v); }
  2410. else if (h1 < h2)
  2411. { return frac(lerp((h1 + 1.0), h2, v)); }
  2412. else
  2413. { return frac(lerp(h1, (h2 + 1.0), v)); }
  2414. }
  2415.  
  2416. float4 ColorGrading(float4 color, float2 texcoord)
  2417. {
  2418. float3 guide = float3(RedGrading, GreenGrading, BlueGrading);
  2419. float amount = GradingStrength;
  2420. float correlation = Correlation;
  2421. float concentration = 2.00;
  2422.  
  2423. float3 colorHSV = RGBtoHSV(color.rgb);
  2424. float3 huePoleA = RGBtoHSV(guide);
  2425. float3 huePoleB = HSVComplement(huePoleA);
  2426.  
  2427. float dist1 = abs(colorHSV.x - huePoleA.x); if (dist1 > 0.5) dist1 = 1.0 - dist1;
  2428. float dist2 = abs(colorHSV.x - huePoleB.x); if (dist2 > 0.5) dist2 = 1.0 - dist2;
  2429.  
  2430. float descent = smoothstep(0.0, correlation, colorHSV.y);
  2431.  
  2432. float3 HSVColor = colorHSV;
  2433.  
  2434. if (dist1 < dist2)
  2435. {
  2436. float c = descent * amount * (1.0 - pow((dist1 * 2.0), 1.0 / concentration));
  2437. HSVColor.x = HueLerp(colorHSV.x, huePoleA.x, c);
  2438. HSVColor.y = lerp(colorHSV.y, huePoleA.y, c);
  2439. }
  2440. else
  2441. {
  2442. float c = descent * amount * (1.0 - pow((dist2 * 2.0), 1.0 / concentration));
  2443. HSVColor.x = HueLerp(colorHSV.x, huePoleB.x, c);
  2444. HSVColor.y = lerp(colorHSV.y, huePoleB.y, c);
  2445. }
  2446.  
  2447. color.rgb = HSVtoRGB(HSVColor);
  2448. color.a = AvgLuminance(color.rgb);
  2449.  
  2450. return saturate(color);
  2451. }
  2452. #endif
  2453.  
  2454. /*------------------------------------------------------------------------------
  2455. [SCANLINES CODE SECTION]
  2456. ------------------------------------------------------------------------------*/
  2457.  
  2458. #if SCANLINES == 1
  2459. float4 ScanlinesPass(float4 color, float2 texcoord)
  2460. {
  2461. float4 intensity;
  2462. float4 fragcoord = gl_FragCoord;
  2463.  
  2464. #if ScanlineType == 0
  2465. if (frac(fragcoord.y * 0.25) > ScanlineScale)
  2466. #elif (ScanlineType == 1)
  2467. if (frac(fragcoord.x * 0.25) > ScanlineScale)
  2468. #elif (ScanlineType == 2)
  2469. if (frac(fragcoord.x * 0.25) > ScanlineScale && frac(fragcoord.y * 0.5) > ScanlineScale)
  2470. #endif
  2471. {
  2472. intensity = float4(0.0, 0.0, 0.0, 0.0);
  2473. }
  2474. else
  2475. {
  2476. intensity = smoothstep(0.2, ScanlineBrightness, color) + normalize(float4(color.xyz, AvgLuminance(color.xyz)));
  2477. }
  2478.  
  2479. float level = (4.0 - texcoord.x) * ScanlineIntensity;
  2480.  
  2481. color = intensity * (0.5 - level) + color * 1.1;
  2482.  
  2483. return color;
  2484. }
  2485. #endif
  2486.  
  2487. /*------------------------------------------------------------------------------
  2488. [VIGNETTE CODE SECTION]
  2489. ------------------------------------------------------------------------------*/
  2490.  
  2491. #if VIGNETTE == 1
  2492. float4 VignettePass(float4 color, float2 texcoord)
  2493. {
  2494. const float2 VignetteCenter = float2(0.5, 0.5);
  2495. float2 tc = texcoord - VignetteCenter;
  2496.  
  2497. tc *= float2((screenSize.x / screenSize.y), VignetteRatio);
  2498. tc /= VignetteRadius;
  2499.  
  2500. float v = dot(tc, tc);
  2501.  
  2502. color.rgb *= (1.0 + pow(v, VignetteSlope * 0.25) * -VignetteAmount);
  2503.  
  2504. return color;
  2505. }
  2506. #endif
  2507.  
  2508. /*------------------------------------------------------------------------------
  2509. [SUBPIXEL DITHERING CODE SECTION]
  2510. ------------------------------------------------------------------------------*/
  2511.  
  2512. #if SP_DITHERING == 1
  2513. float Random(float2 texcoord)
  2514. {
  2515. float seed = dot(texcoord, float2(12.9898, 78.233));
  2516. float sine = sin(seed);
  2517. float noise = frac(sine * 43758.5453);
  2518.  
  2519. return noise;
  2520. }
  2521.  
  2522. float4 DitherPass(float4 color, float2 texcoord)
  2523. {
  2524. float ditherBits = 8.0;
  2525.  
  2526. #if DitherMethod == 2 //random dithering
  2527.  
  2528. float noise = Random(gl_FragCoord.xy);
  2529. float ditherShift = (1.0 / (pow(2.0, ditherBits) - 1.0));
  2530. float ditherHalfShift = (ditherShift * 0.5);
  2531. ditherShift = ditherShift * noise - ditherHalfShift;
  2532.  
  2533. color.rgb += float3(-ditherShift, ditherShift, -ditherShift);
  2534.  
  2535. #if ShowMethod == 1
  2536. color.rgb = float3(noise);
  2537. #endif
  2538.  
  2539. #elif DitherMethod == 1 //ordered dithering
  2540.  
  2541. float2 ditherSize = float2(1.0 / 16.0, 10.0 / 36.0);
  2542. float gridPosition = fract(dot(gl_FragCoord.xy, ditherSize) + 0.25);
  2543. float ditherShift = (0.25) * (1.0 / (pow(2.0, ditherBits) - 1.0));
  2544.  
  2545. float3 RGBShift = float3(ditherShift, -ditherShift, ditherShift);
  2546. RGBShift = lerp(2.0 * RGBShift, -2.0 * RGBShift, gridPosition);
  2547.  
  2548. color.rgb += RGBShift;
  2549.  
  2550. #if ShowMethod == 1
  2551. color.rgb = float3(gridPosition);
  2552. #endif
  2553. #endif
  2554.  
  2555. color.a = AvgLuminance(color.rgb);
  2556.  
  2557. return color;
  2558. }
  2559. #endif
  2560.  
  2561. /*------------------------------------------------------------------------------
  2562. [DEBAND CODE SECTION]
  2563. ------------------------------------------------------------------------------*/
  2564.  
  2565. #if DEBANDING == 1
  2566. //Deband ported from gedosato
  2567. #define DEBAND_SKIP_THRESHOLD_TEST 0 //[0:1] 1 = Skip threshold to see the unfiltered sampling pattern
  2568. #define DEBAND_OUTPUT_BOOST 1.0 //[-2.0:2.0] Default = 1.0. Any value other than the default activates debug mode.
  2569. #define DEBAND_OUTPUT_OFFSET 0.0 //[-1.0:3.0] Default = 0.0. Any value other than the default activates debug mode.
  2570.  
  2571. float rand(float2 pos)
  2572. {
  2573. return frac(sin(dot(pos, float2(12.9898, 78.233))) * 43758.5453);
  2574. }
  2575.  
  2576. bool is_within_threshold(float3 original, float3 other)
  2577. {
  2578. bvec3 cond = notEqual(max(abs(original - other) - DebandThreshold, float3(0.0, 0.0, 0.0)), float3(0.0, 0.0, 0.0));
  2579. return !any(cond);
  2580. }
  2581.  
  2582. float4 DebandPass(float4 color, float2 texcoord)
  2583. {
  2584. float2 step = pixelSize * DebandRadius;
  2585. float2 halfstep = step * 0.5;
  2586.  
  2587. // Compute additional sample positions
  2588. float2 seed = texcoord;
  2589. #if (DebandOffsetMode == 1)
  2590. float2 offset = float2(rand(seed), 0.0);
  2591. #elif(DebandOffsetMode == 2)
  2592. float2 offset = float2(rand(seed).xx);
  2593. #elif(DebandOffsetMode == 3)
  2594. float2 offset = float2(rand(seed), rand(seed + float2(0.1, 0.2)));
  2595. #endif
  2596.  
  2597. float2 on[8] = float2[](
  2598. float2( offset.x, offset.y) * step,
  2599. float2( offset.y, -offset.x) * step,
  2600. float2(-offset.x, -offset.y) * step,
  2601. float2(-offset.y, offset.x) * step,
  2602. float2( offset.x, offset.y) * halfstep,
  2603. float2( offset.y, -offset.x) * halfstep,
  2604. float2(-offset.x, -offset.y) * halfstep,
  2605. float2(-offset.y, offset.x) * halfstep );
  2606.  
  2607. float3 col0 = color.rgb;
  2608. float4 accu = float4(col0, 1.0);
  2609.  
  2610. for(int i=0; i < int(DebandSampleCount); i++)
  2611. {
  2612. float4 cn = float4(texture(OGL2Texture, texcoord + on[i]).rgb, 1.0);
  2613.  
  2614. #if (DEBAND_SKIP_THRESHOLD_TEST == 0)
  2615. if(is_within_threshold(col0, cn.rgb))
  2616. #endif
  2617.  
  2618. accu += cn;
  2619. }
  2620.  
  2621. accu.rgb /= accu.a;
  2622.  
  2623. // Boost to make it easier to inspect the effect's output
  2624. if(DEBAND_OUTPUT_OFFSET != 0.0 || DEBAND_OUTPUT_BOOST != 1.0)
  2625. {
  2626. accu.rgb -= DEBAND_OUTPUT_OFFSET;
  2627. accu.rgb *= DEBAND_OUTPUT_BOOST;
  2628. }
  2629.  
  2630. // Additional dithering
  2631. #if (DebandDithering == 1)
  2632. //Ordered dithering
  2633. float dither_bit = 8.0;
  2634. float grid_position = frac( dot(texcoord,(screenSize * float2(1.0/16.0,10.0/36.0))) + 0.25 );
  2635. float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0));
  2636. float3 dither_shift_RGB = float3(dither_shift, -dither_shift, dither_shift);
  2637. dither_shift_RGB = lerp(2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position);
  2638. accu.rgb += dither_shift_RGB;
  2639. #elif (DebandDithering == 2)
  2640. //Random dithering
  2641. float dither_bit = 8.0;
  2642. float sine = sin(dot(texcoord, float2(12.9898,78.233)));
  2643. float noise = frac(sine * 43758.5453 + texcoord.x);
  2644. float dither_shift = (1.0 / (pow(2,dither_bit) - 1.0));
  2645. float dither_shift_half = (dither_shift * 0.5);
  2646. dither_shift = dither_shift * noise - dither_shift_half;
  2647. accu.rgb += float3(-dither_shift, dither_shift, -dither_shift);
  2648. #elif (DebandDithering == 3)
  2649. float3 vDither = float3(dot(float2(171.0, 231.0), texcoord * screenSize)).xxx;
  2650. vDither.rgb = frac( vDither.rgb / float3( 103.0, 71.0, 97.0 ) ) - float3(0.5, 0.5, 0.5);
  2651. accu.rgb += (vDither.rgb / 255.0);
  2652. #endif
  2653.  
  2654. return accu;
  2655. }
  2656. #endif
  2657.  
  2658. /*------------------------------------------------------------------------------
  2659. [LOTTES CRT CODE SECTION]
  2660. ------------------------------------------------------------------------------*/
  2661.  
  2662. #if LOTTES_CRT == 1
  2663. float ToLinear1(float c)
  2664. {
  2665. c = saturate(c);
  2666. return(c <= 0.04045) ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
  2667. }
  2668.  
  2669. float3 ToLinear(float3 c)
  2670. {
  2671. return float3(ToLinear1(c.r), ToLinear1(c.g), ToLinear1(c.b));
  2672. }
  2673.  
  2674. float ToSrgb1(float c)
  2675. {
  2676. c = saturate(c);
  2677. return(c < 0.0031308 ? c * 12.92 : 1.055 * pow(c, 0.41666) -0.055);
  2678. }
  2679.  
  2680. float3 ToSrgb(float3 c)
  2681. {
  2682. return float3(ToSrgb1(c.r), ToSrgb1(c.g), ToSrgb1(c.b));
  2683. }
  2684.  
  2685. float3 Fetch(float2 pos, float2 off)
  2686. {
  2687. float2 res = (screenSize * ResolutionScale);
  2688. pos = round(pos * res + off) / res;
  2689. if(max(abs(pos.x - 0.5), abs(pos.y - 0.5)) > 0.5) { return float3(0.0, 0.0, 0.0); }
  2690.  
  2691. return ToLinear(texture(OGL2Texture, pos.xy).rgb);
  2692. }
  2693.  
  2694. float2 Dist(float2 pos)
  2695. {
  2696. float2 crtRes = float2(CRTSizeX, CRTSizeY);
  2697. float2 res = (crtRes * MaskResolutionScale);
  2698. pos = (pos * res);
  2699.  
  2700. return -((pos - floor(pos)) - float2(0.5, 0.5));
  2701. }
  2702.  
  2703. float Gaus(float pos, float scale)
  2704. {
  2705. return exp2(scale * pos * pos);
  2706. }
  2707.  
  2708. float3 Horz3(float2 pos, float off)
  2709. {
  2710. float3 b = Fetch(pos,float2(-1.0, off));
  2711. float3 c = Fetch(pos,float2( 0.0, off));
  2712. float3 d = Fetch(pos,float2( 1.0, off));
  2713. float dst = Dist(pos).x;
  2714.  
  2715. // Convert distance to weight.
  2716. float scale = FilterCRTAmount;
  2717. float wb = Gaus(dst-1.0, scale);
  2718. float wc = Gaus(dst+0.0, scale);
  2719. float wd = Gaus(dst+1.0, scale);
  2720.  
  2721. return (b*wb+c*wc+d*wd)/(wb+wc+wd);
  2722. }
  2723.  
  2724. float3 Horz5(float2 pos, float off)
  2725. {
  2726. float3 a = Fetch(pos, float2(-2.0, off));
  2727. float3 b = Fetch(pos, float2(-1.0, off));
  2728. float3 c = Fetch(pos, float2( 0.0, off));
  2729. float3 d = Fetch(pos, float2( 1.0, off));
  2730. float3 e = Fetch(pos, float2( 2.0, off));
  2731. float dst = Dist(pos).x;
  2732.  
  2733. // Convert distance to weight.
  2734. float scale = FilterCRTAmount;
  2735.  
  2736. float wa = Gaus(dst-2.0, scale);
  2737. float wb = Gaus(dst-1.0, scale);
  2738. float wc = Gaus(dst+0.0, scale);
  2739. float wd = Gaus(dst+1.0, scale);
  2740. float we = Gaus(dst+2.0, scale);
  2741.  
  2742. return (a*wa+b*wb+c*wc+d*wd+e*we)/(wa+wb+wc+wd+we);
  2743. }
  2744.  
  2745. // Return scanline weight.
  2746. float Scan(float2 pos, float off)
  2747. {
  2748. float dst = Dist(pos).y;
  2749. return Gaus(dst+off, ScanBrightness);
  2750. }
  2751.  
  2752. float3 Tri(float2 pos)
  2753. {
  2754. float3 a = Horz3(pos,-1.0);
  2755. float3 b = Horz5(pos, 0.0);
  2756. float3 c = Horz3(pos, 1.0);
  2757.  
  2758. float wa = Scan(pos,-1.0);
  2759. float wb = Scan(pos, 0.0);
  2760. float wc = Scan(pos, 1.0);
  2761.  
  2762. return a*wa+b*wb+c*wc;
  2763. }
  2764.  
  2765. float2 Warp(float2 pos)
  2766. {
  2767. pos = pos * 2.0-1.0;
  2768. pos *= float2(1.0 + (pos.y*pos.y) * (HorizontalWarp), 1.0 + (pos.x*pos.x) * (VerticalWarp));
  2769. return pos * 0.5 + 0.5;
  2770. }
  2771.  
  2772. float3 Mask(float2 pos)
  2773. {
  2774. #if MaskingType == 1
  2775. // Very compressed TV style shadow mask.
  2776. float lines = MaskAmountLight;
  2777. float odd = 0.0;
  2778.  
  2779. if(frac(pos.x/6.0) < 0.5) odd = 1.0;
  2780. if (frac((pos.y + odd) / 2.0) < 0.5) lines = MaskAmountDark;
  2781. pos.x = frac(pos.x/3.0);
  2782. float3 mask = float3(MaskAmountDark, MaskAmountDark, MaskAmountDark);
  2783.  
  2784. if(pos.x < 0.333) mask.r = MaskAmountLight;
  2785. else if(pos.x < 0.666)mask.g = MaskAmountLight;
  2786. else mask.b = MaskAmountLight;
  2787. mask *= lines;
  2788.  
  2789. return mask;
  2790.  
  2791. #elif MaskingType == 2
  2792. // Aperture-grille.
  2793. pos.x = frac(pos.x/3.0);
  2794. float3 mask = float3(MaskAmountDark, MaskAmountDark, MaskAmountDark);
  2795.  
  2796. if(pos.x < 0.333)mask.r = MaskAmountLight;
  2797. else if(pos.x < 0.666)mask.g = MaskAmountLight;
  2798. else mask.b = MaskAmountLight;
  2799.  
  2800. return mask;
  2801.  
  2802. #elif MaskingType == 3
  2803. // Stretched VGA style shadow mask (same as prior shaders).
  2804. pos.x += pos.y*3.0;
  2805. float3 mask = float3(MaskAmountDark, MaskAmountDark, MaskAmountDark);
  2806. pos.x = frac(pos.x/6.0);
  2807.  
  2808. if(pos.x < 0.333)mask.r = MaskAmountLight;
  2809. else if(pos.x < 0.666)mask.g = MaskAmountLight;
  2810. else mask.b = MaskAmountLight;
  2811.  
  2812. return mask;
  2813.  
  2814. #else
  2815. // VGA style shadow mask.
  2816. pos.xy = floor(pos.xy*float2(1.0, 0.5));
  2817. pos.x += pos.y*3.0;
  2818.  
  2819. float3 mask = float3(MaskAmountDark, MaskAmountDark, MaskAmountDark);
  2820. pos.x = frac(pos.x/6.0);
  2821.  
  2822. if(pos.x < 0.333)mask.r = MaskAmountLight;
  2823. else if(pos.x < 0.666)mask.g = MaskAmountLight;
  2824. else mask.b= MaskAmountLight;
  2825. return mask;
  2826. #endif
  2827. }
  2828.  
  2829. float4 LottesCRTPass(float4 color, float2 texcoord)
  2830. {
  2831. float4 fragcoord = gl_FragCoord;
  2832. float2 inSize = textureSize(OGL2Texture, 0);
  2833. float2 pos = Warp(texcoord.xy * (inSize/screenSize)) * (screenSize/inSize);
  2834.  
  2835. #if UseShadowMask == 0
  2836. color.rgb = Tri(pos);
  2837. #else
  2838. color.rgb = Tri(pos);
  2839. color.rgb *= Mask(floor(texcoord.xy*(inSize.xy/screenSize)*screenSize.xy) + float2(0.5,0.5));
  2840. #endif
  2841. color.rgb = ToSrgb(color.rgb);
  2842. color.a = 1.0;
  2843.  
  2844. return color;
  2845. }
  2846. #endif
  2847.  
  2848. /*------------------------------------------------------------------------------
  2849. [PX BORDER CODE SECTION]
  2850. ------------------------------------------------------------------------------*/
  2851.  
  2852. float4 BorderPass(float4 colorInput, float2 tex)
  2853. {
  2854. float3 border_color_float = BorderColor / 255.0;
  2855.  
  2856. float2 border = (pixelSize.xy * BorderWidth);
  2857. float2 within_border = saturate((-tex * tex + tex) - (-border * border + border));
  2858.  
  2859. bvec2 cond = notEqual(within_border, vec2(0.0f, 0.0f));
  2860. colorInput.rgb = all(cond) ? colorInput.rgb : border_color_float;
  2861.  
  2862. return colorInput;
  2863.  
  2864. }
  2865.  
  2866. /*------------------------------------------------------------------------------
  2867. [PHONG SHADING CODE SECTION]
  2868. ------------------------------------------------------------------------------*/
  2869.  
  2870. #if PHONG_SHADING == 1
  2871. struct material
  2872. {
  2873. vec4 ambient;
  2874. vec4 diffuse;
  2875. vec4 specular;
  2876. float shininess;
  2877. vec4 sceneColor;
  2878. };
  2879.  
  2880. material frontMaterial = material
  2881. (
  2882. gl_FrontMaterial.ambient,
  2883. gl_FrontMaterial.diffuse,
  2884. gl_FrontMaterial.specular,
  2885. gl_FrontMaterial.shininess,
  2886. gl_FrontLightModelProduct.sceneColor
  2887. );
  2888.  
  2889. struct lightSource
  2890. {
  2891. vec4 ambient;
  2892. vec4 diffuse;
  2893. vec4 specular;
  2894. float constantAttenuation;
  2895. float linearAttenuation;
  2896. float quadraticAttenuation;
  2897. };
  2898.  
  2899. lightSource light0 = lightSource
  2900. (
  2901. gl_LightSource[0].ambient,
  2902. gl_LightSource[0].diffuse,
  2903. gl_LightSource[0].specular,
  2904. gl_LightSource[0].constantAttenuation,
  2905. gl_LightSource[0].linearAttenuation,
  2906. gl_LightSource[0].quadraticAttenuation
  2907. );
  2908.  
  2909. float CalcAttenuation(float dist)
  2910. {
  2911. float consAtten = light0.constantAttenuation;
  2912. float lineAtten = light0.linearAttenuation;
  2913. float quadAtten = light0.quadraticAttenuation;
  2914.  
  2915. float atten = (1.0 / (consAtten + lineAtten * dist + quadAtten * dist * dist));
  2916.  
  2917. return atten;
  2918. }
  2919.  
  2920. vec4 PhongPass(vec4 color, vec2 texcoord)
  2921. {
  2922. // setup input params
  2923. vec3 eyeVector = normalize(eyeVect);
  2924. vec3 normalVector = normalize(Normal);
  2925.  
  2926. // calculate reflection
  2927. vec3 reflection = reflect(-lightVect, normalVector);
  2928.  
  2929. // calculate attenuation
  2930. float attenuation = CalcAttenuation(Distance);
  2931.  
  2932. float nDotL = max(dot(normalVector, lightVect), 0.0); // normal, position
  2933. float eDotR = max(dot(eyeVector, reflection), 0.0); // eyeVec, reflect
  2934.  
  2935. // calculate scene ambient:
  2936. vec4 scene = gl_LightModel.ambient * frontMaterial.ambient;
  2937. vec3 ambient = vec3(scene + light0.ambient + frontMaterial.ambient);
  2938.  
  2939. // calculate mat diffuse:
  2940. vec3 diffuse = vec3(attenuation) * vec3(light0.diffuse * frontMaterial.diffuse) * nDotL;
  2941. diffuse = clamp(diffuse, 0.0, 1.0);
  2942.  
  2943. // calculate specular highlights:
  2944. vec3 specular = vec3(attenuation) * vec3(light0.specular * frontMaterial.specular) *
  2945. pow(eDotR, frontMaterial.shininess); specular = clamp(specular, 0.0, 1.0);
  2946.  
  2947. // calculate phong lighting:
  2948. vec3 phong = ambient + diffuse + specular;
  2949.  
  2950. color.xyz *= phong;
  2951. color.w = AvgLuminance(color.xyz);
  2952.  
  2953. return color;
  2954. }
  2955. #endif
  2956.  
  2957. /*------------------------------------------------------------------------------
  2958. [MAIN() & COMBINE PASS CODE SECTION]
  2959. ------------------------------------------------------------------------------*/
  2960.  
  2961. void main()
  2962. {
  2963. float2 texcoord = TexCoord.xy;
  2964. float4 color = texture(OGL2Texture, texcoord);
  2965.  
  2966. #if BILINEAR_FILTERING == 1
  2967. color = BiLinearPass(color, texcoord);
  2968. #endif
  2969.  
  2970. #if GAUSSIAN_FILTERING == 1
  2971. color = GaussianPass(color, texcoord);
  2972. #endif
  2973.  
  2974. #if BICUBIC_FILTERING == 1
  2975. color = BiCubicPass(color, texcoord);
  2976. #endif
  2977.  
  2978. #if BILATERAL_FILTERING == 1
  2979. color = BilateralPass(color, texcoord);
  2980. #endif
  2981.  
  2982. #if DDTI_FILTERING == 1
  2983. color = DDTPass(color, texcoord);
  2984. #endif
  2985.  
  2986. #if BICUBLIC_SCALER == 1
  2987. color = BiCubicScalerPass(color, texcoord);
  2988. #endif
  2989.  
  2990. #if LANCZOS_SCALER == 1
  2991. color = LanczosScalerPass(color, texcoord);
  2992. #endif
  2993.  
  2994. #if DLAA == 1
  2995. color = DLAAPass(color, texcoord);
  2996. #endif
  2997.  
  2998. #if SSAA == 1
  2999. color = SSAAPass(color, texcoord);
  3000. #endif
  3001.  
  3002. #if SFAA == 1
  3003. color = SFAAPass(color, texcoord);
  3004. #endif
  3005.  
  3006. #if FXAA == 1
  3007. color = FXAAPass(color, texcoord);
  3008. #endif
  3009.  
  3010. #if XBRV == 1
  3011. color = xBRPass(color, texcoord);
  3012. #endif
  3013.  
  3014. #if LOTTES_CRT == 1
  3015. color = LottesCRTPass(color, texcoord);
  3016. #endif
  3017.  
  3018. #if PAINT_SHADING == 1
  3019. color = PaintPass(color, texcoord);
  3020. #endif
  3021.  
  3022. #if TEXTURE_SHARPEN == 1
  3023. color = TexSharpenPass(color, texcoord);
  3024. #endif
  3025.  
  3026. #if PHONG_SHADING == 1
  3027. color = PhongPass(color, texcoord);
  3028. #endif
  3029.  
  3030. #if GAMMA_CORRECTION == 1
  3031. color = GammaPass(color, texcoord);
  3032. #endif
  3033.  
  3034. #if PIXEL_VIBRANCE == 1
  3035. color = VibrancePass(color, texcoord);
  3036. #endif
  3037.  
  3038. #if CEL_SHADING == 1
  3039. color = CelPass(color, texcoord);
  3040. #endif
  3041.  
  3042. #if COLOR_GRADING == 1
  3043. color = ColorGrading(color, texcoord);
  3044. #endif
  3045.  
  3046. #if NTSC_TV_EMULATION == 1
  3047. color = TvColorsPass(color, texcoord);
  3048. #endif
  3049.  
  3050. #if COLOR_CORRECTION == 1
  3051. color = CorrectionPass(color, texcoord);
  3052. #endif
  3053.  
  3054. #if CROSS_PROCESSING == 1
  3055. color = CrossPass(color, texcoord);
  3056. #endif
  3057.  
  3058. #if BLENDED_BLOOM == 1
  3059. color = BloomPass(color, texcoord);
  3060. #endif
  3061.  
  3062. #if SCENE_TONEMAPPING == 1
  3063. color = TonemapPass(color, texcoord);
  3064. #endif
  3065.  
  3066. #if CURVE_CONTRAST == 1
  3067. color = ContrastPass(color, texcoord);
  3068. #endif
  3069.  
  3070. #if VIGNETTE == 1
  3071. color = VignettePass(color, texcoord);
  3072. #endif
  3073.  
  3074. #if SCANLINES == 1
  3075. color = ScanlinesPass(color, texcoord);
  3076. #endif
  3077.  
  3078. #if SP_DITHERING == 1
  3079. color = DitherPass(color, texcoord);
  3080. #endif
  3081.  
  3082. #if DEBANDING == 1
  3083. color = DebandPass(color, texcoord);
  3084. #endif
  3085.  
  3086. #if PX_BORDER == 1
  3087. color = BorderPass(color, texcoord);
  3088. #endif
  3089.  
  3090. FragColor = color;
  3091. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement