SHARE
TWEET

NTSC_MAME.fx

a guest Mar 8th, 2017 235 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ReShade.fxh"
  2.  
  3. //-----------------------------------------------------------------------------
  4. // NTSC Pixel Shader
  5. //-----------------------------------------------------------------------------
  6.  
  7. uniform float AValue = 0.5f;
  8. uniform float BValue = 0.5f;
  9. uniform float CCValue = 3.5795454f;
  10. uniform float OValue = 0.0f;
  11. uniform float PValue = 1.0f;
  12. uniform float ScanTime = 52.6f;
  13.  
  14. uniform float NotchHalfWidth = 1.0f;
  15. uniform float YFreqResponse = 6.0f;
  16. uniform float IFreqResponse = 1.2f;
  17. uniform float QFreqResponse = 0.6f;
  18.  
  19. uniform float SignalOffset = 0.0f;
  20.  
  21. //-----------------------------------------------------------------------------
  22. // Constants
  23. //-----------------------------------------------------------------------------
  24.  
  25. static const float PI = 3.1415927f;
  26. static const float PI2 = PI * 2.0f;
  27.  
  28. static const float4 YDot = float4(0.299f, 0.587f, 0.114f, 0.0f);
  29. static const float4 IDot = float4(0.595716f, -0.274453f, -0.321263f, 0.0f);
  30. static const float4 QDot = float4(0.211456f, -0.522591f, 0.311135f, 0.0f);
  31.  
  32. static const float3 RDot = float3(1.0f, 0.956f, 0.621f);
  33. static const float3 GDot = float3(1.0f, -0.272f, -0.647f);
  34. static const float3 BDot = float3(1.0f, -1.106f, 1.703f);
  35.  
  36. static const float4 OffsetX = float4(0.0f, 0.25f, 0.50f, 0.75f);
  37. static const float4 NotchOffset = float4(0.0f, 1.0f, 2.0f, 3.0f);
  38.  
  39. static const int SampleCount = 64;
  40. static const int HalfSampleCount = SampleCount / 2;
  41.  
  42. float4 GetCompositeYIQ(float2 TexCoord)
  43. {
  44.     float2 PValueSourceTexel = float2(PValue / ReShade::ScreenSize.x, 0.0f);
  45.  
  46.     float2 C0 = TexCoord + PValueSourceTexel * OffsetX.x;
  47.     float2 C1 = TexCoord + PValueSourceTexel * OffsetX.y;
  48.     float2 C2 = TexCoord + PValueSourceTexel * OffsetX.z;
  49.     float2 C3 = TexCoord + PValueSourceTexel * OffsetX.w;
  50.     float4 Cx = float4(C0.x, C1.x, C2.x, C3.x);
  51.     float4 Cy = float4(C0.y, C1.y, C2.y, C3.y);
  52.     float4 Texel0 = tex2D(ReShade::BackBuffer, C0);
  53.     float4 Texel1 = tex2D(ReShade::BackBuffer, C1);
  54.     float4 Texel2 = tex2D(ReShade::BackBuffer, C2);
  55.     float4 Texel3 = tex2D(ReShade::BackBuffer, C3);
  56.  
  57.     float4 HPosition = Cx;
  58.     float4 VPosition = Cy;
  59.  
  60.     float4 Y = float4(dot(Texel0, YDot), dot(Texel1, YDot), dot(Texel2, YDot), dot(Texel3, YDot));
  61.     float4 I = float4(dot(Texel0, IDot), dot(Texel1, IDot), dot(Texel2, IDot), dot(Texel3, IDot));
  62.     float4 Q = float4(dot(Texel0, QDot), dot(Texel1, QDot), dot(Texel2, QDot), dot(Texel3, QDot));
  63.  
  64.     float W = PI2 * CCValue * ScanTime;
  65.     float WoPI = W / PI;
  66.  
  67.     float HOffset = (BValue + SignalOffset) / WoPI;
  68.     float VScale = (AValue * ReShade::ScreenSize.y) / WoPI;
  69.  
  70.     float4 T = HPosition + HOffset + VPosition * VScale;
  71.     float4 TW = T * W;
  72.  
  73.     float4 CompositeYIQ = Y + I * cos(TW) + Q * sin(TW);
  74.  
  75.     return CompositeYIQ;
  76. }
  77.  
  78. float4 PS_NTSC(float4 pos : SV_Position, float2 texcoord : TEXCOORD0) : SV_Target
  79. {
  80.     float4 BaseTexel = tex2D(ReShade::BackBuffer, texcoord);
  81.  
  82.     float TimePerSample = ScanTime / (ReShade::ScreenSize.x * 4.0f);
  83.  
  84.     float Fc_y1 = (CCValue - NotchHalfWidth) * TimePerSample;
  85.     float Fc_y2 = (CCValue + NotchHalfWidth) * TimePerSample;
  86.     float Fc_y3 = YFreqResponse * TimePerSample;
  87.     float Fc_i = IFreqResponse * TimePerSample;
  88.     float Fc_q = QFreqResponse * TimePerSample;
  89.     float Fc_i_2 = Fc_i * 2.0f;
  90.     float Fc_q_2 = Fc_q * 2.0f;
  91.     float Fc_y1_2 = Fc_y1 * 2.0f;
  92.     float Fc_y2_2 = Fc_y2 * 2.0f;
  93.     float Fc_y3_2 = Fc_y3 * 2.0f;
  94.     float Fc_i_pi2 = Fc_i * PI2;
  95.     float Fc_q_pi2 = Fc_q * PI2;
  96.     float Fc_y1_pi2 = Fc_y1 * PI2;
  97.     float Fc_y2_pi2 = Fc_y2 * PI2;
  98.     float Fc_y3_pi2 = Fc_y3 * PI2;
  99.     float PI2Length = PI2 / SampleCount;
  100.  
  101.     float W = PI2 * CCValue * ScanTime;
  102.     float WoPI = W / PI;
  103.  
  104.     float HOffset = (BValue + SignalOffset) / WoPI;
  105.     float VScale = (AValue * ReShade::ScreenSize.y) / WoPI;
  106.  
  107.     float4 YAccum = 0.0f;
  108.     float4 IAccum = 0.0f;
  109.     float4 QAccum = 0.0f;
  110.  
  111.     float4 Cy = texcoord.y;
  112.     float4 VPosition = Cy;
  113.  
  114.     for (float i = 0; i < SampleCount; i += 4.0f)
  115.     {
  116.         float n = i - HalfSampleCount;
  117.         float4 n4 = n + NotchOffset;
  118.  
  119.         float4 Cx = texcoord.x + (n4 * 0.25f) / ReShade::ScreenSize.x;
  120.         float4 HPosition = Cx;
  121.  
  122.         float4 C = GetCompositeYIQ(float2(Cx.r, Cy.r));
  123.  
  124.         float4 T = HPosition + HOffset + VPosition * VScale;
  125.         float4 WT = W * T + OValue;
  126.  
  127.         float4 SincKernel = 0.54f + 0.46f * cos(PI2Length * n4);
  128.  
  129.         float4 SincYIn1 = Fc_y1_pi2 * n4;
  130.         float4 SincYIn2 = Fc_y2_pi2 * n4;
  131.         float4 SincYIn3 = Fc_y3_pi2 * n4;
  132.         float4 SincIIn = Fc_i_pi2 * n4;
  133.         float4 SincQIn = Fc_q_pi2 * n4;
  134.  
  135.         float4 SincY1 = SincYIn1 != 0.0f ? sin(SincYIn1) / SincYIn1 : 1.0f;
  136.         float4 SincY2 = SincYIn2 != 0.0f ? sin(SincYIn2) / SincYIn2 : 1.0f;
  137.         float4 SincY3 = SincYIn3 != 0.0f ? sin(SincYIn3) / SincYIn3 : 1.0f;
  138.  
  139.         float4 IdealY = (Fc_y1_2 * SincY1 - Fc_y2_2 * SincY2) + Fc_y3_2 * SincY3;
  140.         float4 IdealI = Fc_i_2 * (SincIIn != 0.0f ? sin(SincIIn) / SincIIn : 1.0f);
  141.         float4 IdealQ = Fc_q_2 * (SincQIn != 0.0f ? sin(SincQIn) / SincQIn : 1.0f);
  142.  
  143.         float4 FilterY = SincKernel * IdealY;
  144.         float4 FilterI = SincKernel * IdealI;
  145.         float4 FilterQ = SincKernel * IdealQ;
  146.  
  147.         YAccum = YAccum + C * FilterY;
  148.         IAccum = IAccum + C * cos(WT) * FilterI;
  149.         QAccum = QAccum + C * sin(WT) * FilterQ;
  150.     }
  151.  
  152.     float3 YIQ = float3(
  153.         (YAccum.r + YAccum.g + YAccum.b + YAccum.a),
  154.         (IAccum.r + IAccum.g + IAccum.b + IAccum.a) * 2.0f,
  155.         (QAccum.r + QAccum.g + QAccum.b + QAccum.a) * 2.0f);
  156.  
  157.     float3 RGB = float3(
  158.         dot(YIQ, RDot),
  159.         dot(YIQ, GDot),
  160.         dot(YIQ, BDot));
  161.  
  162.     return float4(RGB, BaseTexel.a);
  163. }
  164.  
  165.  
  166. technique NTSC_MAME
  167. {
  168.     pass NTSCMame
  169.     {
  170.         VertexShader = PostProcessVS;
  171.         PixelShader  = PS_NTSC;
  172.     }
  173. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top