phosphor-21x.fs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Phosphor-21x
  2. Copyright (C) 2011 caligari
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU General Public License as published by the Free
  5. Software Foundation; either version 2 of the License, or (at your option)
  6. any later version.
  7. (caligari gave their consent to have this shader distributed under the GPL
  8. in this message:
  9. http://board.byuu.org/viewtopic.php?p=36219#p36219
  10. "As I said to Hyllian by PM, I'm fine with the GPL (not really a big
  11. deal...)"
  12. )
  13. */
  14. #version 150
  15. uniform sampler2D source[];
  16. uniform vec4 sourceSize[];
  17. uniform vec4 targetSize;
  18. in Vertex {
  19. vec2 texCoord;
  20. };
  21. out vec4 fragColor;
  22. // #define TRIAD1
  23. // #define TRIAD2
  24. // #define distortion 0.15
  25. // Uncomment to use neighbours from previous and next scanlines
  26. // #define USE_ALL_NEIGHBOURS
  27. // 0.5 = same width as original pixel 1.0-1.2 seems nice
  28. #define SPOT_WIDTH 1.2
  29. // Shape of the spots 1.0 = circle, 4.0 = ellipse with width = 2*height ************/
  30. #define X_SIZE_ADJUST 2.0
  31. /******************************** To increase bloom / luminosity play with this parameter ************/
  32. #define FACTOR_ADJUST 2.5
  33. #ifdef distortion
  34. vec2 barrelDistortion(vec2 coord) {
  35. vec2 cc = coord - 0.5;
  36. float dist = dot(cc, cc);
  37. return 0.5 + cc * (1.0 + (dist + distortion * dist * dist) * distortion) / (1.0 + (0.25 + distortion * 0.25 * 0.25) * distortion);
  38. }
  39. #define TEXCOORDS barrelDistortion(texCoord * sourceSize[0] / sourceSize[0]) * sourceSize[0] / sourceSize[0]
  40. #else
  41. #define TEXCOORDS texCoord.xy
  42. #endif
  43. #define SCALE 21.0
  44. // Constants
  45. vec4 luminosity_weights = vec4( 0.2126, 0.7152, 0.0722, 0.0 ); // Y = 0.2126 R + 0.7152 G + 0.0722 B
  46. //vec4 luminosity_weights = vec4( 0.6, 0.3, 0.1, 0.0 );
  47. vec2 onex = vec2( 1.0 / sourceSize[0].x, 0.0 );
  48. #ifdef USE_ALL_NEIGHBOURS
  49. vec2 oney = vec2( 0.0, 1.0 / sourceSize[0].y);
  50. #endif
  51. float factor( float lumi, vec2 dxy)
  52. {
  53. float dist = sqrt( dxy.x * dxy.x + dxy.y * dxy.y * X_SIZE_ADJUST ) / SCALE;
  54. return (2.0 + lumi ) * (1.0 - smoothstep( 0.0, SPOT_WIDTH, dist ) ) / FACTOR_ADJUST ;
  55. }
  56. void main(void) {
  57. vec2 coords_scaled = floor( TEXCOORDS * sourceSize[0].xy * SCALE );
  58. vec2 coords_snes = floor( coords_scaled / SCALE ); //TEXCOORDS * sourceSize[0] ) ;
  59. vec2 coords_texture = ( coords_snes + vec2(0.5) ) / sourceSize[0].xy;
  60. vec2 ecart = coords_scaled - ( SCALE * coords_snes + vec2( SCALE * 0.5 - 0.5 ) ) ;
  61. vec4 color = texture(source[0], coords_texture );
  62. float luminosity = dot( color, luminosity_weights );
  63. color *= factor( luminosity, ecart );
  64. // RIGHT NEIGHBOUR
  65. vec4 pcol = texture(source[0], coords_texture + onex);
  66. luminosity = dot( pcol, luminosity_weights );
  67. color += pcol * factor( luminosity, ecart + vec2( -SCALE , 0.0) );
  68. // LEFT NEIGHBOUR
  69. pcol = texture(source[0], coords_texture - onex);
  70. luminosity = dot( pcol, luminosity_weights );
  71. color += pcol * factor( luminosity, ecart + vec2( SCALE , 0.0) );
  72. #ifdef USE_ALL_NEIGHBOURS
  73. // TOP
  74. pcol = texture(source[0], coords_texture + oney);
  75. luminosity = dot( pcol, luminosity_weights );
  76. color += pcol * factor( luminosity, ecart + vec2( 0.0, -SCALE) );
  77. // TOP-LEFT
  78. pcol = texture(source[0], coords_texture + oney - onex);
  79. luminosity = dot( pcol, luminosity_weights );
  80. color += pcol * factor( luminosity, ecart + vec2( SCALE, -SCALE) );
  81. // TOP-RIGHT
  82. pcol = texture(source[0], coords_texture + oney + onex);
  83. luminosity = dot( pcol, luminosity_weights );
  84. color += pcol * factor( luminosity, ecart + vec2( -SCALE, -SCALE) );
  85. // BOTTOM
  86. pcol = texture(source[0], coords_texture - oney);
  87. luminosity = dot( pcol, luminosity_weights );
  88. color += pcol * factor( luminosity, ecart + vec2( 0.0, SCALE) );
  89. // BOTTOM-LEFT
  90. pcol = texture(source[0], coords_texture - oney - onex);
  91. luminosity = dot( pcol, luminosity_weights );
  92. color += pcol * factor( luminosity, ecart + vec2( SCALE, SCALE) );
  93. // BOTTOM-RIGHT
  94. pcol = texture(source[0], coords_texture - oney + onex);
  95. luminosity = dot( pcol, luminosity_weights );
  96. color += pcol * factor( luminosity, ecart + vec2( -SCALE, SCALE) );
  97. #endif
  98. #ifdef TRIAD1
  99. vec2 coords_screen = floor( texCoord.xy * targetSize.xy );
  100. float modulo = mod( coords_screen.y + coords_screen.x , 3.0 );
  101. if ( modulo == 0.0 )
  102. color.rgb *= vec3(1.0,0.5,0.5);
  103. else if ( modulo <= 1.0 )
  104. color.rgb *= vec3(0.5,1.0,0.5);
  105. else
  106. color.rgb *= vec3(0.5,0.5,1.0);
  107. #endif
  108. #ifdef TRIAD2
  109. color = clamp( color, 0.0, 1.0 );
  110. vec2 coords_screen = floor( texCoord.xy * targetSize.xy );
  111. float modulo = mod( coords_screen.x , 3.0 );
  112. if ( modulo == 0.0 ) color.gb *= 0.8;
  113. else if ( modulo == 1.0 ) color.rb *= 0.8;
  114. else color.rg *= 0.8;
  115. #endif
  116. fragColor = clamp( color, 0.0, 1.0 );
  117. }