cgwg-scanlines-interlaced.fs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #version 150
  2. //#define INTERLACED
  3. uniform sampler2D source[];
  4. uniform vec4 sourceSize[];
  5. uniform vec4 targetSize;
  6. uniform int phase;
  7. in Vertex {
  8. vec2 texCoord;
  9. };
  10. out vec4 fragColor;
  11. // Macros.
  12. #define FIX(c) max(abs(c), 1e-5);
  13. #define PI 3.141592653589
  14. #define ilfac vec2(1.0, floor(sourceSize[0].y / 200.0))
  15. #define one (ilfac / sourceSize[0].xy)
  16. #define mod_factor (texCoord.x * targetSize.x)
  17. // Settings //
  18. // gamma of simulated CRT
  19. #define CRTgamma 2.4
  20. // gamma of display monitor (typically 2.2 is correct)
  21. #define monitorgamma 2.2
  22. #define OVERSAMPLE
  23. //#define LINEAR_PROCESSING
  24. // END Settings //
  25. #ifdef LINEAR_PROCESSING
  26. # define TEX2D(c) pow(texture(source[0], (c)), vec4(CRTgamma))
  27. #else
  28. # define TEX2D(c) texture(source[0], (c))
  29. #endif
  30. // Calculate the influence of a scanline on the current pixel.
  31. //
  32. // 'distance' is the distance in texture coordinates from the current
  33. // pixel to the scanline in question.
  34. // 'color' is the colour of the scanline at the horizontal location of
  35. // the current pixel.
  36. vec4 scanlineWeights(float distance, vec4 color)
  37. {
  38. // "wid" controls the width of the scanline beam, for each RGB channel
  39. // The "weights" lines basically specify the formula that gives
  40. // you the profile of the beam, i.e. the intensity as
  41. // a function of distance from the vertical center of the
  42. // scanline. In this case, it is gaussian if width=2, and
  43. // becomes nongaussian for larger widths. Ideally this should
  44. // be normalized so that the integral across the beam is
  45. // independent of its width. That is, for a narrower beam
  46. // "weights" should have a higher peak at the center of the
  47. // scanline than for a wider beam.
  48. #ifdef USEGAUSSIAN
  49. vec4 wid = 0.3 + 0.1 * pow(color, vec4(3.0));
  50. vec4 weights = vec4(distance / wid);
  51. return 0.4 * exp(-weights * weights) / wid;
  52. #else
  53. vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0));
  54. vec4 weights = vec4(distance / 0.3);
  55. return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
  56. #endif
  57. }
  58. void main() {
  59. vec2 xy = texCoord;
  60. // Of all the pixels that are mapped onto the texel we are
  61. // currently rendering, which pixel are we currently rendering?
  62. #ifdef INTERLACED
  63. vec2 ilvec = vec2(0.0,ilfac.y > 1.5 ? mod(float(phase),2.0) : 0.0);
  64. #else
  65. vec2 ilvec = vec2(0.0,ilfac.y);
  66. #endif
  67. vec2 ratio_scale = (xy * sourceSize[0].xy - vec2(0.4999) + ilvec)/ilfac;
  68. #ifdef OVERSAMPLE
  69. float filter_ = fwidth(ratio_scale.y);
  70. #endif
  71. vec2 uv_ratio = fract(ratio_scale);
  72. // Snap to the center of the underlying texel.
  73. xy = (floor(ratio_scale)*ilfac + vec2(0.4999) - ilvec) / sourceSize[0].xy;
  74. // Calculate Lanczos scaling coefficients describing the effect
  75. // of various neighbour texels in a scanline on the current
  76. // pixel.
  77. vec4 coeffs = PI * vec4(1.0 + uv_ratio.x, uv_ratio.x, 1.0 - uv_ratio.x, 2.0 - uv_ratio.x);
  78. // Prevent division by zero.
  79. coeffs = FIX(coeffs);
  80. // Lanczos2 kernel.
  81. coeffs = 2.0 * sin(coeffs) * sin(coeffs / 2.0) / (coeffs * coeffs);
  82. // Normalize.
  83. coeffs /= dot(coeffs, vec4(1.0));
  84. // Calculate the effective colour of the current and next
  85. // scanlines at the horizontal location of the current pixel,
  86. // using the Lanczos coefficients above.
  87. vec4 col = clamp(mat4(
  88. TEX2D(xy + vec2(-one.x, 0.0)),
  89. TEX2D(xy),
  90. TEX2D(xy + vec2(one.x, 0.0)),
  91. TEX2D(xy + vec2(2.0 * one.x, 0.0))) * coeffs,
  92. 0.0, 1.0);
  93. vec4 col2 = clamp(mat4(
  94. TEX2D(xy + vec2(-one.x, one.y)),
  95. TEX2D(xy + vec2(0.0, one.y)),
  96. TEX2D(xy + one),
  97. TEX2D(xy + vec2(2.0 * one.x, one.y))) * coeffs,
  98. 0.0, 1.0);
  99. #ifndef LINEAR_PROCESSING
  100. col = pow(col , vec4(CRTgamma));
  101. col2 = pow(col2, vec4(CRTgamma));
  102. #endif
  103. // Calculate the influence of the current and next scanlines on
  104. // the current pixel.
  105. vec4 weights = scanlineWeights(uv_ratio.y, col);
  106. vec4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
  107. #ifdef OVERSAMPLE
  108. uv_ratio.y =uv_ratio.y+1.0/3.0*filter_;
  109. weights = (weights+scanlineWeights(uv_ratio.y, col))/3.0;
  110. weights2=(weights2+scanlineWeights(abs(1.0-uv_ratio.y), col2))/3.0;
  111. uv_ratio.y =uv_ratio.y-2.0/3.0*filter_;
  112. weights=weights+scanlineWeights(abs(uv_ratio.y), col)/3.0;
  113. weights2=weights2+scanlineWeights(abs(1.0-uv_ratio.y), col2)/3.0;
  114. #endif
  115. vec3 mul_res = (col * weights + col2 * weights2).rgb;
  116. // Convert the image gamma for display on our output device.
  117. mul_res = pow(mul_res, vec3(1.0 / monitorgamma));
  118. // Color the texel.
  119. fragColor = vec4(mul_res, 1.0);
  120. }