Advertisement
Uhyve

PCSX2 CRT Shader V3

Sep 1st, 2012
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #ifdef SHADER_MODEL // make safe to include in resource file to enforce dependency
  2.  
  3. Texture2D Texture;
  4. SamplerState TextureSampler;
  5.  
  6. // Technically, I think the wasteful behavior is more accurate, but it looks fine
  7. // without it as long as smooth is on.
  8. //#define Wasteful
  9. #define Smooth
  10. #define Curved
  11. #define Illuminate
  12. //#define Scanlines
  13. //#define SplitRGB
  14. //#define Vignette
  15.  
  16. // This controls how many pixels are made. I'd suggest using your monitors native
  17. // resolution divided by 3-4ish (this is set up for 1080p). Also depends on what
  18. // scale you're using in GSdx.
  19. #define ResX 512.0
  20. #define ResY 360.0
  21. #define PixelX 1.0/ResX
  22. #define PixelY 1.0/ResY
  23. #define Strength 1.0
  24. #define fCurviness 0.05
  25. #define fDarkStrength 1.5
  26.  
  27. #ifdef Curved
  28. float2 radialDistortion(float2 coord)
  29. {
  30.     float2 cc = coord - 0.5;
  31.     float dist = dot(cc, cc) * fCurviness;             
  32.     return (coord + cc * (1.0 + dist) * dist);
  33. }
  34. #endif
  35.  
  36. struct PS_INPUT
  37. {
  38.     float4 p : SV_Position;
  39.     float2 t : TEXCOORD0;
  40. };
  41.  
  42. struct PS_OUTPUT
  43. {
  44.     float4 c : SV_Target0;
  45. };
  46.  
  47. PS_OUTPUT ps_main(PS_INPUT input)
  48. {
  49.     PS_OUTPUT output;  
  50.  
  51.     #ifdef Curved
  52.     input.t = radialDistortion(input.t);
  53.  
  54.     // Again wasteful, but for some reason, I couldn't set BORDER as a samplerstate
  55.     if( input.t.x > 1.0 || input.t.x < 0.0 || input.t.y > 1.0 || input.t.y < 0.0 )
  56.     {
  57.         output.c = float4(0.0,0.0,0.0,1.0);
  58.         return output;
  59.     }
  60.     #endif
  61.  
  62.     float2 vPixelPos;
  63.     vPixelPos.x = (input.t.x * ResX) + 0.5;
  64.     vPixelPos.y = (input.t.y * ResY) + 0.5;
  65.    
  66.     float2 vPixel = round(vPixelPos);
  67.     float2 vPixelDist = vPixelPos - vPixel;
  68.  
  69.     #ifdef Scanlines
  70.     vPixelDist.x = 0.0;
  71.     #endif
  72.  
  73.     #ifdef Wasteful
  74.     input.t.x = vPixel.x / ResX;
  75.     input.t.y = vPixel.y / ResY;
  76.     #endif
  77.  
  78.     float4 color = Texture.Sample(TextureSampler, input.t);
  79.    
  80.     #ifdef Smooth
  81.     float2 coord = input.t;
  82.     float fXDiff = PixelX * 0.33;
  83.     float fYDiff = PixelY * 0.33;
  84.     coord.x -= fXDiff;
  85.     coord.y -= fYDiff;
  86.     color += Texture.Sample(TextureSampler, coord);
  87.     coord.x += fXDiff;
  88.     color += Texture.Sample(TextureSampler, coord);
  89.     coord.x += fXDiff;
  90.     color += Texture.Sample(TextureSampler, coord);
  91.     coord.y += fYDiff;
  92.     color += Texture.Sample(TextureSampler, coord);
  93.     coord.y += fYDiff;
  94.     color += Texture.Sample(TextureSampler, coord);
  95.     coord.x -= fXDiff;
  96.     color += Texture.Sample(TextureSampler, coord);
  97.     coord.x -= fXDiff;
  98.     color += Texture.Sample(TextureSampler, coord);
  99.     coord.y -= fYDiff;
  100.     color += Texture.Sample(TextureSampler, coord);
  101.     color /= 9;
  102.     #endif
  103.  
  104.     #ifdef Vignette
  105.     color *= saturate(1.3 - (pow(input.t.x-0.5,2) + pow(input.t.y-0.5,2)) );
  106.     #endif
  107.  
  108.     // Shame this doesn't work, not entirely sure why either. Should split the RGB slightly.
  109.     // Lol, just figured it out, so dumb, hah.
  110.     #if SplitRGB
  111.     float fTexelPos = (vPixelDist.x + 1.0) * 0.7854;
  112.     color.x *= 1.0 - sin(fTexelPos+0.00) * 0.25;
  113.     color.y *= 1.0 - sin(fTexelPos+0.52) * 0.25;
  114.     color.z *= 1.0 - sin(fTexelPos+1.04) * 0.25;
  115.     color.xyz *= 1.5;
  116.     #endif
  117.  
  118.     float fSize = Strength;
  119.  
  120.     #ifdef Illuminate
  121.     float fDarkness = 1.0 - ((color.x + color.y + color.z) / 3.0);
  122.     fSize *= (fDarkness * fDarkStrength) + 0.2;
  123.     #endif
  124.  
  125.     vPixelDist *= fSize;
  126.     float fPixelDist = saturate( 1.0 - sqrt( pow( abs(vPixelDist.x),2 ) + pow( abs(vPixelDist.y),2 ) ) * 0.8 );
  127.  
  128.     color.xyz *= fPixelDist;
  129.     output.c = color;
  130.     return output;
  131. }
  132.  
  133. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement