Advertisement
Guest User

Untitled

a guest
Oct 24th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. /*
  2. rAA post-3x - Pass 0
  3. by Sp00kyFox, 2018-10-20
  4.  
  5. Filter: Nearest
  6. Scale: 1x
  7.  
  8. This is a generalized continuation of the reverse antialiasing filter by
  9. Christoph Feck. Unlike the original filter this is supposed to be used on an
  10. already upscaled image. Which makes it possible to combine rAA with other filters
  11. just as ScaleFX, xBR or others.
  12.  
  13. Pass 0 does the horizontal filtering.
  14.  
  15.  
  16.  
  17. Copyright (c) 2018 Sp00kyFox - ScaleFX@web.de
  18.  
  19. Permission is hereby granted, free of charge, to any person obtaining a copy
  20. of this software and associated documentation files (the "Software"), to deal
  21. in the Software without restriction, including without limitation the rights
  22. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. copies of the Software, and to permit persons to whom the Software is
  24. furnished to do so, subject to the following conditions:
  25.  
  26. The above copyright notice and this permission notice shall be included in
  27. all copies or substantial portions of the Software.
  28.  
  29. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  35. THE SOFTWARE.
  36.  
  37. */
  38.  
  39. #pragma parameter RAA_SHR0 "rAA-3x 0 Sharpness" 2.0 0.00 10.0 0.05
  40. #pragma parameter RAA_SMT0 "rAA-3x 0 Smoothness" 0.5 0.05 10.0 0.05
  41. #pragma parameter RAA_DVT0 "rAA-3x 0 Deviation" 1.0 0.05 10.0 0.05
  42.  
  43.  
  44. #ifdef PARAMETER_UNIFORM
  45. uniform float RAA_SHR0, RAA_SMT0, RAA_DVT0;
  46. #else
  47. #define RAA_SHR0 2.0
  48. #define RAA_SMT0 0.5
  49. #define RAA_DVT0 1.0
  50. #endif
  51.  
  52.  
  53. static const int scl = 3; // scale factor
  54. static const int rad = 7; // search radius
  55.  
  56.  
  57. // core function of rAA - tilt of a pixel
  58. float3 res2x(float3 pre2, float3 pre1, float3 px, float3 pos1, float3 pos2)
  59. {
  60. float d1, d2, w;
  61. float3 a, m, t, t1, t2;
  62. float4x3 pre = float4x3(pre2, pre1, px, pos1);
  63. float4x3 pos = float4x3(pre1, px, pos1, pos2);
  64. float4x3 df = pos - pre;
  65.  
  66. m = (px < 0.5) ? px : (1.0-px);
  67. m = RAA_SHR0 * min(m, min(abs(df[1]), abs(df[2]))); // magnitude
  68. t = (7 * (df[1] + df[2]) - 3 * (df[0] + df[3])) / 16; // tilt
  69.  
  70. a = t == 0.0 ? 1.0 : m/abs(t);
  71. t1 = clamp(t, -m, m); // limit channels
  72. t2 = min(1.0, min(min(a.x, a.y), a.z)) * t; // limit length
  73.  
  74. d1 = length(df[1]); d2 = length(df[2]);
  75. d1 = d1 == 0.0 ? 0.0 : length(cross(df[1], t1))/d1; // distance between line (px, pre1) and point px-t1
  76. d2 = d2 == 0.0 ? 0.0 : length(cross(df[2], t1))/d2; // distance between line (px, pos1) and point px+t1
  77.  
  78. w = min(1.0, max(d1,d2)/0.8125); // color deviation from optimal value
  79.  
  80. return lerp(t1, t2, pow(w, RAA_DVT0));
  81. }
  82.  
  83.  
  84. struct input
  85. {
  86. float2 video_size;
  87. float2 texture_size;
  88. float2 output_size;
  89. float frame_count;
  90. float frame_direction;
  91. float frame_rotation;
  92. };
  93.  
  94.  
  95. struct out_vertex {
  96. float4 position : POSITION;
  97. float4 color : COLOR;
  98. float2 texCoord : TEXCOORD0;
  99. float2 t1 : TEXCOORD1;
  100. };
  101.  
  102. /* VERTEX_SHADER */
  103. out_vertex main_vertex
  104. (
  105. float4 position : POSITION,
  106. float4 color : COLOR,
  107. float2 texCoord1 : TEXCOORD0,
  108.  
  109. uniform float4x4 modelViewProj,
  110. uniform input IN
  111. )
  112. {
  113. out_vertex OUT;
  114.  
  115. OUT.position = mul(modelViewProj, position);
  116. OUT.color = color;
  117.  
  118. float2 ps = float2(1.0/IN.texture_size.x, 1.0/IN.texture_size.y);
  119. float dx = ps.x;
  120. float dy = ps.y;
  121.  
  122. // This line fix a bug in ATI cards.
  123. float2 texCoord = texCoord1 + float2(0.0000001, 0.0000001);
  124.  
  125. OUT.texCoord = texCoord;
  126. OUT.t1 = 1.0/IN.texture_size;
  127.  
  128. return OUT;
  129. }
  130.  
  131.  
  132. /* FRAGMENT SHADER */
  133. float4 main_fragment(in out_vertex VAR, uniform sampler2D decal : TEXUNIT0, uniform input IN) : COLOR
  134. {
  135.  
  136. // read texels
  137.  
  138. float3 tx[2*rad+1];
  139.  
  140. #define TX(n) tx[(n)+rad]
  141.  
  142. TX(0) = tex2D(decal, VAR.texCoord).rgb;
  143.  
  144. for(int i=1; i<=rad; i++){
  145. TX(-i) = tex2D(decal, VAR.texCoord + float2(-i,0)*VAR.t1).rgb;
  146. TX( i) = tex2D(decal, VAR.texCoord + float2( i,0)*VAR.t1).rgb;
  147. }
  148.  
  149.  
  150. // prepare variables for candidate search
  151.  
  152. int2 i1, i2;
  153. float3 df1, df2, A, B, D, E;
  154. float2 d1, d2, d3;
  155. bool2 cn;
  156.  
  157. df1 = TX(1)-TX(0); df2 = TX(0)-TX(-1);
  158.  
  159. d2 = float2(length(df1), length(df2));
  160. d3 = d2.yx;
  161.  
  162.  
  163. // smoothness weight, protects smooth gradients
  164. float sw = d2.x + d2.y;
  165. sw = sw == 0.0 ? 1.0 : pow(length(df1-df2)/sw, RAA_SMT0);
  166.  
  167.  
  168. // look for proper candidates
  169. for(int i=1; i<rad; i++){
  170. d1 = d2;
  171. d2 = d3;
  172. d3 = float2(distance(TX(-i-1), TX(-i)), distance(TX(i), TX(i+1)));
  173. cn = max(d1,d3)<d2;
  174. i2 = cn && i2==0 && i1!=0 ? i : i2;
  175. i1 = cn && i1==0 ? i : i1;
  176.  
  177. B = i1.x == i ? TX(-i) : B;
  178. D = i1.y == i ? TX( i) : D;
  179.  
  180. A = i2.x == 0 && i1.x == i ? TX(-i-1) : i2.x == i ? TX(-i) : A;
  181. E = i2.y == 0 && i1.y == i ? TX( i+1) : i2.y == i ? TX( i) : E;
  182. }
  183.  
  184.  
  185. // rAA core with the candidates found above
  186. float3 t = res2x(A, B, TX(0), D, E);
  187.  
  188. // distance weight
  189. float dw = any(i1 == 0) ? 0.0 : 2.0 * ((i1.x-1.0)/(i1.x+i1.y-2.0)) - 1.0;
  190.  
  191. // result
  192. float3 res = TX(0) + (scl-1.0)/scl * sw*dw * t;
  193.  
  194.  
  195. // prevent ringing
  196. float3 lo = min(min(TX(-1),TX(0)),TX(1));
  197. float3 hi = max(max(TX(-1),TX(0)),TX(1));
  198.  
  199. return float4(clamp(res, lo, hi), 1.0);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement