Advertisement
Uhyve

PCSX2 CRT Shader V2

Aug 31st, 2012
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 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. // Mess with these top defines to get a CRT pattern that works for your monitor, it may
  7. // take some trial and error. This is what I settled on for a 1920x1080 monitor.
  8. #define ResX 512.0
  9. #define ResY 360.0
  10. #define PixelX 1.0/ResX
  11. #define PixelY 1.0/ResY
  12. #define Wasteful
  13. #define Smooth
  14. #define Curved
  15. #define fCurviness 0.05
  16. //#define SplitRGB
  17. //#define Vignette
  18.  
  19. float2 radialDistortion(float2 coord)
  20. {
  21.     float2 cc = coord - 0.5;
  22.     float dist = dot(cc, cc) * fCurviness;             
  23.     return (coord + cc * (1.0 + dist) * dist);
  24. }
  25.  
  26. struct PS_INPUT
  27. {
  28.     float4 p : SV_Position;
  29.     float2 t : TEXCOORD0;
  30. };
  31.  
  32. struct PS_OUTPUT
  33. {
  34.     float4 c : SV_Target0;
  35. };
  36.  
  37.  
  38. PS_OUTPUT ps_main(PS_INPUT input)
  39. {
  40.     PS_OUTPUT output;  
  41.  
  42.     #ifdef Curved
  43.         input.t = radialDistortion(input.t);
  44.  
  45.         // Again wasteful, but for some reason, I couldn't set BORDER as a samplerstate
  46.         if( input.t.x > 1.0 || input.t.x < 0.0 || input.t.y > 1.0 || input.t.y < 0.0 )
  47.         {
  48.             output.c = float4(0.0,0.0,0.0,1.0);
  49.             return output;
  50.         }
  51.     #endif
  52.  
  53.     float2 vPixelPos;
  54.     vPixelPos.x = (input.t.x * ResX) + 0.5;
  55.     vPixelPos.y = (input.t.y * ResY) + 0.5;
  56.    
  57.     float2 vPixel = round(vPixelPos);
  58.     float2 vPixelDist = vPixelPos - vPixel;
  59.     float fMaxDist = 1.0;
  60.  
  61.     #ifdef Wasteful
  62.         input.t.x = vPixel.x / ResX;
  63.         input.t.y = vPixel.y / ResY;
  64.     #endif
  65.  
  66.     float4 color = Texture.Sample(TextureSampler, input.t);
  67.    
  68.     #ifdef Smooth
  69.         float2 coord = input.t;
  70.         float fXDiff = PixelX * 0.33;
  71.         float fYDiff = PixelY * 0.33;
  72.         coord.x -= fXDiff;
  73.         coord.y -= fYDiff;
  74.         color += Texture.Sample(TextureSampler, coord);
  75.         coord.x += fXDiff;
  76.         color += Texture.Sample(TextureSampler, coord);
  77.         coord.x += fXDiff;
  78.         color += Texture.Sample(TextureSampler, coord);
  79.         coord.y += fYDiff;
  80.         color += Texture.Sample(TextureSampler, coord);
  81.         coord.y += fYDiff;
  82.         color += Texture.Sample(TextureSampler, coord);
  83.         coord.x -= fXDiff;
  84.         color += Texture.Sample(TextureSampler, coord);
  85.         coord.x -= fXDiff;
  86.         color += Texture.Sample(TextureSampler, coord);
  87.         coord.y -= fYDiff;
  88.         color += Texture.Sample(TextureSampler, coord);
  89.         color /= 9;
  90.     #endif
  91.  
  92.     #ifdef Vignette
  93.         color *= saturate(1.3 - (pow(input.t.x-0.5,2) + pow(input.t.y-0.5,2)) );
  94.     #endif
  95.  
  96.     // Shame this doesn't work, not entirely sure why either. Should split the RGB slightly.
  97.     #if SplitRGB
  98.         float fTexelPos = (vPixelDist.x + 1.0) * 0.7854;
  99.         color.x *= 1.0 - sin(fTexelPos+0.00) * 0.25;
  100.         color.y *= 1.0 - sin(fTexelPos+0.52) * 0.25;
  101.         color.z *= 1.0 - sin(fTexelPos+1.04) * 0.25;
  102.         color.xyz *= 1.5;
  103.     #endif
  104.  
  105.     float fPixelDist = saturate( fMaxDist - sqrt(pow(abs(vPixelDist.x),2) + pow(abs(vPixelDist.y),2)) );
  106.    
  107.     color.xyz *= fPixelDist;
  108.     output.c = color;
  109.     return output;
  110. }
  111.  
  112. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement