Guest User

Untitled

a guest
Oct 24th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. #version 450
  2.  
  3. /*
  4. rAA post-3x - Pass 1
  5. by Sp00kyFox, 2018-10-20
  6.  
  7. Filter: Nearest
  8. Scale: 1x
  9.  
  10. This is a generalized continuation of the reverse antialiasing filter by
  11. Christoph Feck. Unlike the original filter this is supposed to be used on an
  12. already upscaled image. Which makes it possible to combine rAA with other filters
  13. just as ScaleFX, xBR or others.
  14.  
  15. Pass 1 does the vertical filtering.
  16.  
  17.  
  18.  
  19. Copyright (c) 2018 Sp00kyFox - ScaleFX@web.de
  20.  
  21. Permission is hereby granted, free of charge, to any person obtaining a copy
  22. of this software and associated documentation files (the "Software"), to deal
  23. in the Software without restriction, including without limitation the rights
  24. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  25. copies of the Software, and to permit persons to whom the Software is
  26. furnished to do so, subject to the following conditions:
  27.  
  28. The above copyright notice and this permission notice shall be included in
  29. all copies or substantial portions of the Software.
  30.  
  31. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  34. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  35. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  36. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  37. THE SOFTWARE.
  38.  
  39. */
  40.  
  41. layout(push_constant) uniform Push
  42. {
  43. vec4 SourceSize;
  44. vec4 OriginalSize;
  45. vec4 OutputSize;
  46. uint FrameCount;
  47. float RAA_SHR1;
  48. float RAA_SMT1;
  49. float RAA_DVT1;
  50. } params;
  51.  
  52. #pragma parameter RAA_SHR1 "rAA-3x 1 Sharpness" 2.0 0.00 10.0 0.05
  53. #pragma parameter RAA_SMT1 "rAA-3x 1 Smoothness" 0.5 0.05 10.0 0.05
  54. #pragma parameter RAA_DVT1 "rAA-3x 1 Deviation" 1.0 0.05 10.0 0.05
  55.  
  56. layout(std140, set = 0, binding = 0) uniform UBO
  57. {
  58. mat4 MVP;
  59. } global;
  60.  
  61. #pragma stage vertex
  62. layout(location = 0) in vec4 Position;
  63. layout(location = 1) in vec2 TexCoord;
  64. layout(location = 0) out vec2 vTexCoord;
  65.  
  66. void main()
  67. {
  68. gl_Position = global.MVP * Position;
  69. vTexCoord = TexCoord;
  70. }
  71.  
  72. #pragma stage fragment
  73. layout(location = 0) in vec2 vTexCoord;
  74. layout(location = 0) out vec4 FragColor;
  75. layout(set = 0, binding = 2) uniform sampler2D Source;
  76.  
  77. const int scl = 3; // scale factor
  78. const int rad = 7; // search radius
  79.  
  80. // core function of rAA - tilt of a pixel
  81. vec3 res2x(vec3 pre2, vec3 pre1, vec3 px, vec3 pos1, vec3 pos2)
  82. {
  83. float d1, d2, w;
  84. vec3 a, m, t, t1, t2;
  85. mat4x3 pre = mat4x3(pre2, pre1, px, pos1);
  86. mat4x3 pos = mat4x3(pre1, px, pos1, pos2);
  87. mat4x3 df = pos - pre;
  88.  
  89. m.x = (px.x < 0.5) ? px.x : (1.0-px.x);
  90. m.y = (px.y < 0.5) ? px.y : (1.0-px.y);
  91. m.z = (px.z < 0.5) ? px.z : (1.0-px.z);
  92. m = params.RAA_SHR1 * min(m, min(abs(df[1]), abs(df[2]))); // magnitude
  93. t = (7 * (df[1] + df[2]) - 3 * (df[0] + df[3])) / 16; // tilt
  94.  
  95. a.x = t.x == 0.0 ? 1.0 : m.x/abs(t.x);
  96. a.y = t.y == 0.0 ? 1.0 : m.y/abs(t.y);
  97. a.z = t.z == 0.0 ? 1.0 : m.z/abs(t.z);
  98. t1 = clamp(t, -m, m); // limit channels
  99. t2 = min(1.0, min(min(a.x, a.y), a.z)) * t; // limit length
  100.  
  101. d1 = length(df[1]); d2 = length(df[2]);
  102. d1 = d1 == 0.0 ? 0.0 : length(cross(df[1], t1))/d1; // distance between line (px, pre1) and point px-t1
  103. d2 = d2 == 0.0 ? 0.0 : length(cross(df[2], t1))/d2; // distance between line (px, pos1) and point px+t1
  104.  
  105. w = min(1.0, max(d1,d2)/0.8125); // color deviation from optimal value
  106.  
  107. return mix(t1, t2, pow(w, params.RAA_DVT1));
  108. }
  109.  
  110. void main()
  111. {
  112. // read texels
  113.  
  114. vec3 tx[2*rad+1];
  115.  
  116. #define TX(n) tx[(n)+rad]
  117.  
  118. TX(0) = texture(Source, vTexCoord).rgb;
  119.  
  120. for(int i=1; i<=rad; i++){
  121. TX(-i) = texture(Source, vTexCoord + vec2(0,-i)*params.SourceSize.zw).rgb;
  122. TX( i) = texture(Source, vTexCoord + vec2(0, i)*params.SourceSize.zw).rgb;
  123. }
  124.  
  125.  
  126. // prepare variables for candidate search
  127.  
  128. ivec2 i1, i2;
  129. vec3 df1, df2;
  130. vec2 d1, d2, d3;
  131. bvec2 cn;
  132.  
  133. df1 = TX(1)-TX(0); df2 = TX(0)-TX(-1);
  134.  
  135. d2 = vec2(length(df1), length(df2));
  136. d3 = d2.yx;
  137.  
  138.  
  139. // smoothness weight, protects smooth gradients
  140. float sw = d2.x + d2.y;
  141. sw = sw == 0.0 ? 1.0 : pow(length(df1-df2)/sw, params.RAA_SMT1);
  142.  
  143.  
  144. // look for proper candidates
  145. for(int i=1; i<rad; i++){
  146. d1 = d2;
  147. d2 = d3;
  148. d3 = vec2(distance(TX(-i-1), TX(-i)), distance(TX(i), TX(i+1)));
  149. cn.x = max(d1.x,d3.x)<d2.x;
  150. cn.y = max(d1.y,d3.y)<d2.y;
  151. i2.x = cn.x && i2.x==0 && i1.x!=0 ? i : i2.x;
  152. i2.y = cn.y && i2.y==0 && i1.y!=0 ? i : i2.y;
  153. i1.x = cn.x && i1.x==0 ? i : i1.x;
  154. i1.y = cn.y && i1.y==0 ? i : i1.y;
  155. }
  156.  
  157. i2.x = i2.x == 0 ? i1.x+1 : i2.x;
  158. i2.y = i2.y == 0 ? i1.y+1 : i2.y;
  159.  
  160.  
  161. // rAA core with the candidates found above
  162. vec3 t = res2x(TX(-i2.x), TX(-i1.x), TX(0), TX(i1.y), TX(i2.y));
  163.  
  164. // distance weight
  165. float dw = (i1.x == 0 || i1.y == 0) ? 0.0 : 2.0 * ((i1.x-1.0)/(i1.x+i1.y-2.0)) - 1.0;
  166.  
  167. // result
  168. vec3 res = TX(0) + (scl-1.0)/scl * sw*dw * t;
  169.  
  170.  
  171. // prevent ringing
  172. vec3 lo = min(min(TX(-1),TX(0)),TX(1));
  173. vec3 hi = max(max(TX(-1),TX(0)),TX(1));
  174.  
  175. FragColor = vec4(clamp(res, lo, hi), 1.0);
  176. }
Add Comment
Please, Sign In to add comment