Guest User

CRTEasymode

a guest
Mar 30th, 2017
319
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. CRT Shader by EasyMode
  3. License: GPL
  4.  
  5. A flat CRT shader ideally for 1080p or higher displays.
  6.  
  7. Recommended Settings:
  8.  
  9. Video
  10. - Aspect Ratio: 4:3
  11. - Integer Scale: Off
  12.  
  13. Shader
  14. - Filter: Nearest
  15. - Scale: Don't Care
  16.  
  17. Example RGB Mask Parameter Settings:
  18.  
  19. Aperture Grille (Default)
  20. - Dot Width: 1
  21. - Dot Height: 1
  22. - Stagger: 0
  23.  
  24. Lottes' Shadow Mask
  25. - Dot Width: 2
  26. - Dot Height: 1
  27. - Stagger: 3
  28. */
  29.  
  30. #include "ReShade.fxh"
  31.  
  32. uniform float texture_sizeX <
  33. ui_type = "drag";
  34. ui_min = 1.0;
  35. ui_max = BUFFER_WIDTH;
  36. ui_label = "Screen Width [CRT-Easymode]";
  37. > = 320.0;
  38.  
  39. uniform float texture_sizeY <
  40. ui_type = "drag";
  41. ui_min = 1.0;
  42. ui_max = BUFFER_HEIGHT;
  43. ui_label = "Screen Height [CRT-Easymode]";
  44. > = 240.0;
  45.  
  46. uniform float SHARPNESS_H <
  47. ui_type = "drag";
  48. ui_min = 0.0;
  49. ui_max = 1.0;
  50. ui_step = 0.05;
  51. ui_label = "Sharpness Horizontal [CRT-Easymode]";
  52. > = 0.5;
  53.  
  54. uniform float SHARPNESS_V <
  55. ui_type = "drag";
  56. ui_min = 0.0;
  57. ui_max = 1.0;
  58. ui_step = 0.05;
  59. ui_label = "Sharpness Vertical [CRT-Easymode]";
  60. > = 1.0;
  61.  
  62. uniform float MASK_STRENGTH <
  63. ui_type = "drag";
  64. ui_min = 0.0;
  65. ui_max = 1.0;
  66. ui_step = 0.01;
  67. ui_label = "Mask Strength [CRT-Easymode]";
  68. > = 0.3;
  69.  
  70. uniform float MASK_DOT_WIDTH <
  71. ui_type = "drag";
  72. ui_min = 1.0;
  73. ui_max = 100.0;
  74. ui_step = 1.0;
  75. ui_label = "Mask Dot Width [CRT-Easymode]";
  76. > = 1.0;
  77.  
  78. uniform float MASK_DOT_HEIGHT <
  79. ui_type = "drag";
  80. ui_min = 1.0;
  81. ui_max = 100.0;
  82. ui_step = 1.0;
  83. ui_label = "Mask Dot Height [CRT-Easymode]";
  84. > = 1.0;
  85.  
  86. uniform float MASK_STAGGER <
  87. ui_type = "drag";
  88. ui_min = 0.0;
  89. ui_max = 100.0;
  90. ui_step = 1.0;
  91. ui_label = "Mask Stagger [CRT-Easymode]";
  92. > = 0.0;
  93.  
  94. uniform float MASK_SIZE <
  95. ui_type = "drag";
  96. ui_min = 1.0;
  97. ui_max = 100.0;
  98. ui_step = 1.0;
  99. ui_label = "Mask Size [CRT-Easymode]";
  100. > = 1.0;
  101.  
  102. uniform float SCANLINE_STRENGTH <
  103. ui_type = "drag";
  104. ui_min = 0.0;
  105. ui_max = 1.0;
  106. ui_step = 0.05;
  107. ui_label = "Scanline Strength [CRT-Easymode]";
  108. > = 1.0;
  109.  
  110. uniform float SCANLINE_BEAM_WIDTH_MIN <
  111. ui_type = "drag";
  112. ui_min = 0.5;
  113. ui_max = 5.0;
  114. ui_step = 0.5;
  115. ui_label = "Scanline Beam Width M [CRT-Easymode]";
  116. > = 1.5;
  117.  
  118. uniform float SCANLINE_BEAM_WIDTH_MAX <
  119. ui_type = "drag";
  120. ui_min = 0.5;
  121. ui_max = 5.0;
  122. ui_step = 0.5;
  123. ui_label = "Scanline Beam Width Max. [CRT-Easymode]";
  124. > = 1.5;
  125.  
  126. uniform float SCANLINE_BRIGHT_MIN <
  127. ui_type = "drag";
  128. ui_min = 0.0;
  129. ui_max = 1.0;
  130. ui_step = 0.05;
  131. ui_label = "Scanline Brightness M [CRT-Easymode]";
  132. > = 0.35;
  133.  
  134. uniform float SCANLINE_BRIGHT_MAX <
  135. ui_type = "drag";
  136. ui_min = 0.0;
  137. ui_max = 1.0;
  138. ui_step = 0.05;
  139. ui_label = "Scanline Brightness Max. [CRT-Easymode]";
  140. > = 0.65;
  141.  
  142. uniform float SCANLINE_CUTOFF <
  143. ui_type = "drag";
  144. ui_min = 1.0;
  145. ui_max = 1000.0;
  146. ui_step = 1.0;
  147. ui_label = "Scanline Cutoff [CRT-Easymode]";
  148. > = 400.0;
  149.  
  150. uniform float GAMMA_INPUT <
  151. ui_type = "drag";
  152. ui_min = 0.1;
  153. ui_max = 5.0;
  154. ui_step = 0.1;
  155. ui_label = "Gamma Input [CRT-Easymode]";
  156. > = 2.0;
  157.  
  158. uniform float GAMMA_OUTPUT <
  159. ui_type = "drag";
  160. ui_min = 0.1;
  161. ui_max = 5.0;
  162. ui_step = 0.1;
  163. ui_label = "Gamma Output [CRT-Easymode]";
  164. > = 1.8;
  165.  
  166. uniform float BRIGHT_BOOST <
  167. ui_type = "drag";
  168. ui_min = 1.0;
  169. ui_max = 2.0;
  170. ui_step = 0.01;
  171. ui_label = "Brightness Boost [CRT-Easymode]";
  172. > = 1.2;
  173.  
  174. uniform int DILATION <
  175. ui_type = "drag";
  176. ui_min = 0.0;
  177. ui_max = 1.0;
  178. ui_step = 1.0;
  179. ui_label = "Dilation [CRT-Easymode]";
  180. > = 1.0;
  181.  
  182. float mod(float x, float y)
  183. {
  184. return x - y * floor(x/y);
  185. }
  186.  
  187. #define FIX(c) max(abs(c), 1e-5)
  188. #define PI 3.141592653589
  189. #define TEX2D(c) dilate(tex2D(ReShade::BackBuffer, c))
  190.  
  191. uniform bool ENABLE_LANCZOS <
  192. ui_type = "combo";
  193. ui_label = "Enable Lanczos [CRT-Easymode]";
  194. > = true;
  195.  
  196. float4 dilate(float4 col)
  197. {
  198. float4 x = lerp(float4(1.0,1.0,1.0,1.0), col, DILATION);
  199.  
  200. return col * x;
  201. }
  202.  
  203. float curve_distance(float x, float sharp)
  204. {
  205.  
  206. /*
  207. apply half-circle s-curve to distance for sharper (more pixelated) interpolation
  208. single line formula for Graph Toy:
  209. 0.5 - sqrt(0.25 - (x - step(0.5, x)) * (x - step(0.5, x))) * sign(0.5 - x)
  210. */
  211.  
  212. float x_step = step(0.5, x);
  213. float curve = 0.5 - sqrt(0.25 - (x - x_step) * (x - x_step)) * sign(0.5 - x);
  214.  
  215. return lerp(x, curve, sharp);
  216. }
  217.  
  218. float4x4 get_color_matrix(sampler2D tex, float2 co, float2 dx)
  219. {
  220. return float4x4(TEX2D(co - dx), TEX2D(co), TEX2D(co + dx), TEX2D(co + 2.0 * dx));
  221. }
  222.  
  223. float3 filter_lanczos(float4 coeffs, float4x4 color_matrix)
  224. {
  225. float4 col = mul(coeffs, color_matrix);
  226. float4 sample_min = min(color_matrix[1], color_matrix[2]);
  227. float4 sample_max = max(color_matrix[1], color_matrix[2]);
  228.  
  229. col = clamp(col, sample_min, sample_max);
  230.  
  231. return col.rgb;
  232. }
  233.  
  234. float4 PS_CRTEasymode(float4 vpos : SV_Position, float2 coords : TexCoord) : SV_Target
  235. {
  236. float2 dx = float2(1.0 / texture_sizeX, 0.0);
  237. float2 dy = float2(0.0, 1.0 / texture_sizeY);
  238. float2 pix_co = coords * float2(texture_sizeX,texture_sizeY) - float2(0.5, 0.5);
  239. float2 tex_co = (floor(pix_co) + float2(0.5, 0.5)) / float2(texture_sizeX,texture_sizeY);
  240. float2 dist = frac(pix_co);
  241. float curve_x;
  242. float3 col, col2;
  243.  
  244. if(ENABLE_LANCZOS){
  245. curve_x = curve_distance(dist.x, SHARPNESS_H * SHARPNESS_H);
  246.  
  247. float4 coeffs = PI * float4(1.0 + curve_x, curve_x, 1.0 - curve_x, 2.0 - curve_x);
  248.  
  249. coeffs = FIX(coeffs);
  250. coeffs = 2.0 * sin(coeffs) * sin(coeffs / 2.0) / (coeffs * coeffs);
  251. coeffs /= dot(coeffs, float4(1.0,1.0,1.0,1.0));
  252.  
  253. col = filter_lanczos(coeffs, get_color_matrix(ReShade::BackBuffer, tex_co, dx));
  254. col2 = filter_lanczos(coeffs, get_color_matrix(ReShade::BackBuffer, tex_co + dy, dx));
  255. } else {
  256. curve_x = curve_distance(dist.x, SHARPNESS_H);
  257.  
  258. col = lerp(TEX2D(tex_co).rgb, TEX2D(tex_co + dx).rgb, curve_x);
  259. col2 = lerp(TEX2D(tex_co + dy).rgb, TEX2D(tex_co + dx + dy).rgb, curve_x);
  260. }
  261.  
  262. col = lerp(col, col2, curve_distance(dist.y, SHARPNESS_V));
  263. col = pow(col, float3(GAMMA_INPUT / (DILATION + 1.0),GAMMA_INPUT / (DILATION + 1.0),GAMMA_INPUT / (DILATION + 1.0)));
  264.  
  265. float luma = dot(float3(0.2126, 0.7152, 0.0722), col);
  266. float bright = (max(col.r, max(col.g, col.b)) + luma) / 2.0;
  267. float scan_bright = clamp(bright, SCANLINE_BRIGHT_MIN, SCANLINE_BRIGHT_MAX);
  268. float scan_beam = clamp(bright * SCANLINE_BEAM_WIDTH_MAX, SCANLINE_BEAM_WIDTH_MIN, SCANLINE_BEAM_WIDTH_MAX);
  269. float scan_weight = 1.0 - pow(cos(coords.y * 2.0 * PI * texture_sizeY) * 0.5 + 0.5, scan_beam) * SCANLINE_STRENGTH;
  270.  
  271. float mask = 1.0 - MASK_STRENGTH;
  272. float2 mod_fac = floor(coords * ReShade::ScreenSize * float2(texture_sizeX, texture_sizeY) / (float2(texture_sizeX,texture_sizeY) * float2(MASK_SIZE, MASK_DOT_HEIGHT * MASK_SIZE)));
  273. int dot_no = int(mod((mod_fac.x + mod(mod_fac.y, 2.0) * MASK_STAGGER) / MASK_DOT_WIDTH, 3.0));
  274. float3 mask_weight;
  275.  
  276. if (dot_no == 0) mask_weight = float3(1.0, mask, mask);
  277. else if (dot_no == 1) mask_weight = float3(mask, 1.0, mask);
  278. else mask_weight = float3(mask, mask, 1.0);
  279.  
  280. if (texture_sizeY >= SCANLINE_CUTOFF) scan_weight = 1.0;
  281.  
  282. col2 = col.rgb;
  283. col *= float3(scan_weight,scan_weight,scan_weight);
  284. col = lerp(col, col2, scan_bright);
  285. col *= mask_weight;
  286. col = pow(col, float3(1.0 / GAMMA_OUTPUT,1.0 / GAMMA_OUTPUT,1.0 / GAMMA_OUTPUT));
  287.  
  288. return float4(col * BRIGHT_BOOST, 1.0);
  289. }
  290.  
  291. technique EasymodeCRT {
  292. pass CRT_Easymode {
  293. VertexShader=PostProcessVS;
  294. PixelShader=PS_CRTEasymode;
  295. }
  296. }
RAW Paste Data