AdvancedCRT.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // CRT shader
  2. //
  3. // Copyright (C) 2010-2012 cgwg, Themaister and DOLLS
  4. //
  5. // This program is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU General Public License as published by the Free
  7. // Software Foundation; either version 2 of the License, or (at your option)
  8. // any later version.
  9. #define d CRTDistance
  10. #define R CRTCurvatureRadius
  11. // Comment the next line to disable interpolation in linear gamma (and gain speed).
  12. //#define LINEAR_PROCESSING
  13. // aspect ratio
  14. #define aspect float2(1.0, 0.75)
  15. // Precalculate a bunch of useful values we'll need in the fragment
  16. // shader.
  17. #define sinangle sin(float2(CRTAngleX, CRTAngleY))
  18. #define cosangle cos(float2(CRTAngleX, CRTAngleY))
  19. #define stretch maxscale()
  20. // Macros.
  21. #define FIX(c) max(abs(c), 1e-5);
  22. float PI = acos(-1); //#define PI 3.141592653589
  23. // The size of one texel, in texture-coordinates.
  24. #define coone 1.0 / rubyTextureSize
  25. #define mod_factor tex.x * rubyTextureSize.x * rubyOutputSize.x / rubyInputSize.x
  26. #ifdef LINEAR_PROCESSING
  27. # define TEX2D(c) pow(myTex2D(s0, (c)), CRTgamma)
  28. #else
  29. # define TEX2D(c) myTex2D(s0, (c))
  30. #endif
  31. float intersect(float2 xy)
  32. {
  33. float A = dot(xy,xy) + (d * d);
  34. float B = 2.0 * (R * (dot(xy, sinangle) - d * cosangle.x * cosangle.y) - d * d);
  35. float C = d * d + 2.0 * R * d * cosangle.x * cosangle.y; //all constants
  36. return (-B - sqrt(B * B -4.0 * A * C)) / (2.0 * A);
  37. }
  38. float2 bkwtrans(float2 xy)
  39. {
  40. float c = intersect(xy);
  41. float2 _point = float2(c, c) * xy;
  42. _point -= float2(-R, -R) * sinangle;
  43. _point /= float2(R, R);
  44. float2 tang = sinangle / cosangle;
  45. float2 poc = _point / cosangle;
  46. float A = dot(tang, tang) + 1.0;
  47. float B = -2.0 * dot(poc, tang);
  48. float C = dot(poc, poc) - 1.0;
  49. float a = (-B + sqrt(B * B -4.0 * A * C)) / (2.0 * A);
  50. float2 uv = (_point - a * sinangle) / cosangle;
  51. float r = R * acos(a);
  52. return uv * r / sin(r / R);
  53. }
  54. float2 fwtrans(float2 uv)
  55. {
  56. float r = FIX(sqrt(dot(uv, uv)));
  57. uv *= sin(r / R) / r;
  58. float x = 1.0 - cos(r / R);
  59. float D = d / R + x * cosangle.x * cosangle.y + dot(uv, sinangle);
  60. return d * (uv * cosangle - x * sinangle) / D;
  61. }
  62. float3 maxscale()
  63. {
  64. float2 c = bkwtrans(-R * sinangle / (1.0 + R / d * cosangle.x * cosangle.y));
  65. float2 a = float2(0.5, 0.5) * aspect;
  66. float2 lo = float2(fwtrans(float2(-a.x, c.y)).x,
  67. fwtrans(float2(c.x,-a.y)).y) / aspect;
  68. float2 hi = float2(fwtrans(float2(+a.x, c.y)).x,
  69. fwtrans(float2(c.x, +a.y)).y) / aspect;
  70. return float3((hi + lo) * aspect * 0.5, max(hi.x - lo.x, hi.y - lo.y));
  71. }
  72. float2 transform(float2 coord, float2 textureSize, float2 inputSize)
  73. {
  74. coord *= textureSize / inputSize;
  75. coord = (coord - 0.5) * aspect * stretch.z + stretch.xy;
  76. return (bkwtrans(coord) / float2(CRTOverScan, CRTOverScan) / aspect + 0.5) * inputSize / textureSize;
  77. }
  78. float corner(float2 coord, float2 textureSize, float2 inputSize)
  79. {
  80. coord *= textureSize / inputSize;
  81. coord = (coord - 0.5) * float2(CRTOverScan, CRTOverScan) + 0.5;
  82. coord = min(coord, 1.0 - coord) * aspect;
  83. float2 cdist = float2(CRTCornerSize, CRTCornerSize);
  84. coord = (cdist - min(coord, cdist));
  85. float dist = sqrt(dot(coord, coord));
  86. return clamp((cdist.x-dist) * 1000.0, 0.0, 1.0);
  87. }
  88. // Calculate the influence of a scanline on the current pixel.
  89. //
  90. // 'distance' is the distance in texture coordinates from the current
  91. // pixel to the scanline in question.
  92. // 'color' is the colour of the scanline at the horizontal location of
  93. // the current pixel.
  94. float4 scanlineWeights(float distance, float4 color)
  95. {
  96. // "wid" controls the width of the scanline beam, for each RGB channel
  97. // The "weights" lines basically specify the formula that gives
  98. // you the profile of the beam, i.e. the intensity as
  99. // a function of distance from the vertical center of the
  100. // scanline. In this case, it is gaussian if width=2, and
  101. // becomes nongaussian for larger widths. Ideally this should
  102. // be normalized so that the integral across the beam is
  103. // independent of its width. That is, for a narrower beam
  104. // "weights" should have a higher peak at the center of the
  105. // scanline than for a wider beam.
  106. #if CRTScanlineGaussian == 0
  107. float4 wid = 0.3 + 0.1 * pow(color, 3.0);
  108. float4 weights = float4(distance / wid);
  109. return 0.4 * exp(-weights * weights) / wid;
  110. #else
  111. float4 wid = 2.0 + 2.0 * pow(color, 4.0);
  112. float calcdistance = distance / 0.3; // Optimization ?
  113. //float4 weights = float4(distance / 0.3, distance / 0.3, distance / 0.3, distance / 0.3);
  114. float4 weights = float4(calcdistance, calcdistance, calcdistance, calcdistance);
  115. return 1.4 * exp(-pow(weights * rsqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
  116. #endif
  117. }
  118. float4 AdvancedCRTPass( float4 colorInput, float2 tex )
  119. {
  120. // Here's a helpful diagram to keep in mind while trying to
  121. // understand the code:
  122. //
  123. // | | | | |
  124. // -------------------------------
  125. // | | | | |
  126. // | 01 | 11 | 21 | 31 | <-- current scanline
  127. // | | @ | | |
  128. // -------------------------------
  129. // | | | | |
  130. // | 02 | 12 | 22 | 32 | <-- next scanline
  131. // | | | | |
  132. // -------------------------------
  133. // | | | | |
  134. //
  135. // Each character-cell represents a pixel on the output
  136. // surface, "@" represents the current pixel (always somewhere
  137. // in the bottom half of the current scan-line, or the top-half
  138. // of the next scanline). The grid of lines represents the
  139. // edges of the texels of the underlying texture.
  140. float Input_ratio = ceil(256 * CRTResolution);
  141. float2 Resolution = float2(Input_ratio, Input_ratio);
  142. float2 rubyTextureSize = Resolution;
  143. float2 rubyInputSize = Resolution;
  144. float2 rubyOutputSize = screen_size;
  145. #if CRTCurvature == 1
  146. float2 xy = transform(tex, rubyTextureSize, rubyInputSize);
  147. #else
  148. float2 xy = tex;
  149. #endif
  150. float cval = corner(xy, rubyTextureSize, rubyInputSize);
  151. // Of all the pixels that are mapped onto the texel we are
  152. // currently rendering, which pixel are we currently rendering?
  153. float2 ratio_scale = xy * rubyTextureSize - 0.5;
  154. #if CRTOversample == 1
  155. float filter = fwidth(ratio_scale.y);
  156. #endif
  157. float2 uv_ratio = frac(ratio_scale);
  158. // Snap to the center of the underlying texel.
  159. xy = (floor(ratio_scale) + 0.5) / rubyTextureSize;
  160. // Calculate Lanczos scaling coefficients describing the effect
  161. // of various neighbour texels in a scanline on the current
  162. // pixel.
  163. float4 coeffs = PI * float4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x);
  164. // Prevent division by zero.
  165. coeffs = FIX(coeffs);
  166. // Lanczos2 kernel.
  167. coeffs = 2.0 * sin(coeffs) * sin(coeffs / 2.0) / (coeffs * coeffs);
  168. // Normalize.
  169. coeffs /= dot(coeffs, 1.0);
  170. // Calculate the effective colour of the current and next
  171. // scanlines at the horizontal location of the current pixel,
  172. // using the Lanczos coefficients above.
  173. float4 col = clamp(mul(coeffs, float4x4(
  174. TEX2D(xy + float2(-coone.x, 0.0)),
  175. TEX2D(xy),
  176. TEX2D(xy + float2(coone.x, 0.0)),
  177. TEX2D(xy + float2(2.0 * coone.x, 0.0)))),
  178. 0.0, 1.0);
  179. float4 col2 = clamp(mul(coeffs, float4x4(
  180. TEX2D(xy + float2(-coone.x, coone.y)),
  181. TEX2D(xy + float2(0.0, coone.y)),
  182. TEX2D(xy + coone),
  183. TEX2D(xy + float2(2.0 * coone.x, coone.y)))),
  184. 0.0, 1.0);
  185. #ifndef LINEAR_PROCESSING
  186. col = pow(col , CRTgamma);
  187. col2 = pow(col2, CRTgamma);
  188. #endif
  189. // Calculate the influence of the current and next scanlines on
  190. // the current pixel.
  191. float4 weights = scanlineWeights(uv_ratio.y, col);
  192. float4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
  193. #if CRTOversample == 1
  194. uv_ratio.y = uv_ratio.y + 1.0 / 3.0 * filter;
  195. weights = (weights + scanlineWeights(uv_ratio.y, col)) / 3.0;
  196. weights2 = (weights2 + scanlineWeights(abs(1.0 - uv_ratio.y), col2)) / 3.0;
  197. uv_ratio.y = uv_ratio.y - 2.0 / 3.0 * filter;
  198. weights = weights + scanlineWeights(abs(uv_ratio.y), col) / 3.0;
  199. weights2 = weights2 + scanlineWeights(abs(1.0 - uv_ratio.y), col2) / 3.0;
  200. #endif
  201. float3 mul_res = (col * weights + col2 * weights2).rgb * float3(cval, cval, cval);
  202. // dot-mask emulation:
  203. // Output pixels are alternately tinted green and magenta.
  204. float3 dotMaskWeights = lerp(float3(1.0, 0.7, 1.0),
  205. float3(0.7, 1.0, 0.7),
  206. floor(mod_factor % CRTScanlineIntensity));
  207. mul_res *= dotMaskWeights * float3(0.83, 0.83, 1.0) * CRTBrightness;
  208. // Convert the image gamma for display on our output device.
  209. mul_res = pow(mul_res, 1.0 / CRTmonitorgamma);
  210. //return saturate(lerp(colorInput, float4(mul_res, 1.0), CRTAmount));
  211. colorInput.rgb = lerp(colorInput.rgb, mul_res, CRTAmount);
  212. return saturate(colorInput);
  213. }