Advertisement
Ameisen

Untitled

Jan 23rd, 2022
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.33 KB | None | 0 0
  1.     internal void Pass(ReadOnlySpan<Float4> sourceData, ReadOnlySpan<Float4> lastPassData, Span<Float4> target) {
  2.         var source = new Texture(this, sourceData, SourceSize);
  3.         var prev = new Texture(this, lastPassData, SourceSize);
  4.  
  5.         for (int y = 0; y < TargetSize.Height; ++y) {
  6.             int yOffset = GetY(y) * TargetSize.Width;
  7.             for (int x = 0; x < TargetSize.Width; ++x) {
  8.                 int targetOffset = yOffset + GetX(x);
  9.  
  10.                 Float2 sourceOffset = new Float2(
  11.                     x * 0.5f,
  12.                     y * 0.5f
  13.                 );
  14.  
  15.                 Float2 fracP = CgMath.Frac(sourceOffset);
  16.                 Float2 dir = fracP - new Float2(0.5f, 0.5f);
  17.                 if ((dir.X * dir.Y) > 0.0f) {  // I'm unsure how this _ever_ would not be zero? If the inputs are half the size of the output, then xy - 0.5 is always <= zero.
  18.                     target[targetOffset] = (fracP.X > 0.5) ?
  19.                         source.Sample(sourceOffset) :
  20.                         prev.Sample(sourceOffset);
  21.                     continue;
  22.                 }
  23.  
  24.                 // Skip pixels on wrong grid
  25.                 // Cannot really replicate this behavior in this implementation...
  26.                 // float2 fp = frac(VAR.texCoord * IN.texture_size);
  27.                 // float2 dir = fp - float2(0.5, 0.5);
  28.                 // if ((dir.x * dir.y) > 0.0) return (fp.x > 0.5) ? tex2D(s0, VAR.texCoord) : tex2D(PASSPREV2.texture, VAR.texCoord);
  29.  
  30.                 //Float2 g1 = new Float2(0.0f, 0.5f / size.Y); // half texel, source
  31.                 //Float2 g2 = new Float2(0.5f / size.X, 0.0f); // half texel, source
  32.  
  33.                 // float3 P0 = tex2D(PASSPREV2.texture, VAR.texCoord -   3.0*g1        ).xyz;
  34.                 //
  35.  
  36.                 Float2 g1 = (fracP.X > 0.5f) ? new Float2(0.5f, 0.0f) : new Float2(0.0f, 0.5f);
  37.                 Float2 g2 = (fracP.X > 0.5f) ? new Float2(0.0f, 0.5f) : new Float2(0.5f, 0.0f);
  38.  
  39.                 var centroid = prev.Sample(sourceOffset);
  40.  
  41.                 var P0 =   prev.Sample(sourceOffset    -3.0f*g1            ).RGB;
  42.                 var P1 = source.Sample(sourceOffset                -3.0f*g2).RGB;
  43.                 var P2 = source.Sample(sourceOffset                +3.0f*g2).RGB;
  44.                 var P3 =   prev.Sample(sourceOffset    +3.0f*g1            ).RGB;
  45.  
  46.                 // float3 B = tex2D(s0, VAR.texCoord - 2.0 * g1 - g2).xyz;
  47.                 // (2 * 0.5 texels) - 0.5 texels
  48.                 // 1 texel - 0.5 texels
  49.                 // 0.5 texels should become 0 texels.
  50.                 var B =  source.Sample(sourceOffset    -2.0f*g1         -g2).RGB;
  51.                 var C =    prev.Sample(sourceOffset         -g1    -2.0f*g2).RGB;
  52.                 var D =  source.Sample(sourceOffset    -2.0f*g1         +g2).RGB;
  53.                 var E =    prev.Sample(sourceOffset         -g1            ).RGB;
  54.                 var F =  source.Sample(sourceOffset                     -g2).RGB;
  55.                 var G =    prev.Sample(sourceOffset         -g1    +2.0f*g2).RGB;
  56.                 var H =  source.Sample(sourceOffset                     +g2).RGB;
  57.                 var I =    prev.Sample(sourceOffset    +2.0f*g1         +g2).RGB;
  58.  
  59.                 var F4 = source.Sample(sourceOffset         +g1    -2.0f*g2).RGB;
  60.                 var I4 =   prev.Sample(sourceOffset    +2.0f*g1         -g2).RGB;
  61.                 var H5 = source.Sample(sourceOffset         +g1    +2.0f*g2).RGB;
  62.                 var I5 =   prev.Sample(sourceOffset    +2.0f*g1         +g2).RGB;
  63.  
  64.                 float b = RGBToYUV(B);
  65.                 float c = RGBToYUV(C);
  66.                 float d = RGBToYUV(D);
  67.                 float e = RGBToYUV(E);
  68.                 float f = RGBToYUV(F);
  69.                 float g = RGBToYUV(G);
  70.                 float h = RGBToYUV(H);
  71.                 float i = RGBToYUV(I);
  72.  
  73.                 float i4 = RGBToYUV(I4); float p0 = RGBToYUV(P0);
  74.                 float i5 = RGBToYUV(I5); float p1 = RGBToYUV(P1);
  75.                 float h5 = RGBToYUV(H5); float p2 = RGBToYUV(P2);
  76.                 float f4 = RGBToYUV(F4); float p3 = RGBToYUV(P3);
  77.  
  78.                 // Calc edgeness in diagonal directions.
  79.                 float dEdge =
  80.                     WeightedDifferenceDiagonal(d, b, g, e, c, p2, h, f, p1, h5, i, f4, i5, i4) -
  81.                     WeightedDifferenceDiagonal(c, f4, b, f, i4, p0, e, i, p3, d, h, i5, g, h5);
  82.  
  83.                 // Calc edgeness in horizontal/vertical directions.
  84.                 float hvEdge =
  85.                     WeightedDifferenceHorizontalVertical(f, i, e, h, c, i5, b, h5) -
  86.                     WeightedDifferenceHorizontalVertical(e, f, h, i, d, f4, g, i4);
  87.  
  88.                 float limits = Configuration.EdgeStrength + 0.000001f;
  89.                 float edgeStrength = CgMath.SmoothStep(0.0f, limits, Math.Abs(dEdge));
  90.  
  91.                 // Filter weights. Two taps only.
  92.                 Float4 w1 = new(-Weight1, Weight1 + 0.5f, Weight1 + 0.5f, -Weight1);
  93.                 Float4 w2 = new(-Weight2, Weight2 + 0.25f, Weight2 + 0.25f, -Weight2);
  94.  
  95.                 // Filtering and normalization in four direction generating four colors.
  96.                 Float3 c1 = CgMath.MatrixMul(w1, P2, H, F, P1);
  97.                 Float3 c2 = CgMath.MatrixMul(w1, P0, E, I, P3);
  98.                 Float3 c3 = CgMath.MatrixMul(w2, D + G, E + H, F + I, F4 + I4);
  99.                 Float3 c4 = CgMath.MatrixMul(w2, C + B, F + E, I + H, I5 + H5);
  100.  
  101.                 float alpha = centroid.A;
  102.  
  103.                 // Smoothly blends the two strongest directions (one in diagonal and the other in vert/horiz direction).
  104.                 Float3 color = CgMath.Lerp(
  105.                     CgMath.Lerp(c1, c2, CgMath.Step(0.0f, dEdge)),
  106.                     CgMath.Lerp(c3, c4, CgMath.Step(0.0f, hvEdge)),
  107.                     1.0f - edgeStrength
  108.                 );
  109.  
  110.                 // Anti-ringing code.
  111.                 Float3 minSample =
  112.                     CgMath.Min4(E, F, H, I) +
  113.                     (1.0f - Configuration.AntiRinging) *
  114.                     CgMath.Lerp(
  115.                         (P2 - H) * (F - P1),
  116.                         (P0 - E) * (I - P3),
  117.                         CgMath.Step(0.0f, dEdge)
  118.                     );
  119.                 Float3 maxSample =
  120.                     CgMath.Max4(E, F, H, I) -
  121.                     (1.0f - Configuration.AntiRinging) *
  122.                     CgMath.Lerp(
  123.                         (P2 - H) * (F - P1),
  124.                         (P0 - E) * (I - P3),
  125.                         CgMath.Step(0.0f, dEdge)
  126.                     );
  127.                 color = color.Clamp(minSample, maxSample);
  128.  
  129.                 target[targetOffset] = new(color, alpha);
  130.             }
  131.         }
  132.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement