Guest User

Jinc shader with anti-ringing

a guest
May 10th, 2014
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. /* COMPATIBILITY
  2. - HLSL compilers
  3. - Cg compilers
  4. */
  5.  
  6. /*
  7. Hyllian's lanczos2-sharp Shader
  8.  
  9. Copyright (C) 2011-2014 Hyllian/Jararaca - [email protected]
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24.  
  25. */
  26.  
  27. const static float floatpi = 1.5707963267948966192313216916398;
  28. const static float pi = 3.1415926535897932384626433832795;
  29.  
  30. // Calculates the distance between two points
  31. float d(float2 pt1, float2 pt2)
  32. {
  33. float2 v = pt2 - pt1;
  34. return sqrt(dot(v,v));
  35. }
  36.  
  37. float3 min4(float3 a, float3 b, float3 c, float3 d)
  38. {
  39. return min(a, min(b, min(c, d)));
  40. }
  41. float3 max4(float3 a, float3 b, float3 c, float3 d)
  42. {
  43. return max(a, max(b, max(c, d)));
  44. }
  45.  
  46.  
  47. struct input
  48. {
  49. float2 video_size;
  50. float2 texture_size;
  51. float2 output_size;
  52. float frame_count;
  53. float frame_direction;
  54. float frame_rotation;
  55. };
  56.  
  57.  
  58. struct out_vertex {
  59. float4 position : POSITION;
  60. float4 color : COLOR;
  61. float2 texCoord : TEXCOORD0;
  62. };
  63.  
  64. /* VERTEX_SHADER */
  65. out_vertex main_vertex
  66. (
  67. float4 position : POSITION,
  68. float4 color : COLOR,
  69. float2 texCoord1 : TEXCOORD0,
  70.  
  71. uniform float4x4 modelViewProj,
  72. uniform input IN
  73. )
  74. {
  75.  
  76. // This line fix a bug in ATI cards.
  77. float2 tex = texCoord1;
  78.  
  79. out_vertex OUT = {
  80. mul(modelViewProj, position),
  81. color,
  82. tex
  83. };
  84.  
  85. return OUT;
  86. }
  87.  
  88. float4 lanczos(float4 x)
  89. {
  90. float4 res;
  91. // res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(pi*floatpi) : sin(x*floatpi)*sin(x*pi)/(x*x);
  92.  
  93. // res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(1.0) : (2/(pi*pi))*sin(x*floatpi)*sin(x*pi)/(x*x);
  94.  
  95. // res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(0.5) : pi*cos(x*pi)*pi*cos(x*pi*(1.22/2.233));
  96.  
  97. // res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(0.5) : 2*(0.5-x*x/16.0+x*x*x*x/384.0)*sin(pi*x)/(pi*x);
  98.  
  99. res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(1.0) : sin(pi*x*0.4)*sin(x*pi*0.825)/(0.4*0.825*pi*x*pi*x);
  100.  
  101. //res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(1.0) : cos(x)*sin(x*pi)/(pi*x);
  102.  
  103. //res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(1.0) : cos(x/floatpi)*sin(x*pi)/(pi*x); // Unknown filter kernel
  104.  
  105.  
  106. return res;
  107. }
  108.  
  109. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  110. {
  111. float3 color;
  112. float4x4 weights;
  113.  
  114. float2 dx = float2(1.0, 0.0);
  115. float2 dy = float2(0.0, 1.0);
  116.  
  117. float2 pc = VAR.texCoord*IN.texture_size;
  118.  
  119. float2 tc = (floor(pc-float2(0.5,0.5))+float2(0.5,0.5));
  120.  
  121. weights[0] = lanczos(float4(d(pc, tc -dx -dy), d(pc, tc -dy), d(pc, tc +dx -dy), d(pc, tc+2.0*dx -dy)));
  122. weights[1] = lanczos(float4(d(pc, tc -dx ), d(pc, tc ), d(pc, tc +dx ), d(pc, tc+2.0*dx )));
  123. weights[2] = lanczos(float4(d(pc, tc -dx +dy), d(pc, tc +dy), d(pc, tc +dx +dy), d(pc, tc+2.0*dx +dy)));
  124. weights[3] = lanczos(float4(d(pc, tc -dx+2.0*dy), d(pc, tc +2.0*dy), d(pc, tc +dx+2.0*dy), d(pc, tc+2.0*dx+2.0*dy)));
  125.  
  126. // weights[0].x = weights[0].w = weights[3].x = weights[3].w = 0.0;
  127.  
  128. dx = dx/IN.texture_size;
  129. dy = dy/IN.texture_size;
  130. tc = tc/IN.texture_size;
  131.  
  132. // reading the texels
  133.  
  134. float3 c00 = tex2D(s_p, tc -dx -dy).xyz;
  135. float3 c10 = tex2D(s_p, tc -dy).xyz;
  136. float3 c20 = tex2D(s_p, tc +dx -dy).xyz;
  137. float3 c30 = tex2D(s_p, tc+2.0*dx -dy).xyz;
  138. float3 c01 = tex2D(s_p, tc -dx ).xyz;
  139. float3 c11 = tex2D(s_p, tc ).xyz;
  140. float3 c21 = tex2D(s_p, tc +dx ).xyz;
  141. float3 c31 = tex2D(s_p, tc+2.0*dx ).xyz;
  142. float3 c02 = tex2D(s_p, tc -dx +dy).xyz;
  143. float3 c12 = tex2D(s_p, tc +dy).xyz;
  144. float3 c22 = tex2D(s_p, tc +dx +dy).xyz;
  145. float3 c32 = tex2D(s_p, tc+2.0*dx +dy).xyz;
  146. float3 c03 = tex2D(s_p, tc -dx+2.0*dy).xyz;
  147. float3 c13 = tex2D(s_p, tc +2.0*dy).xyz;
  148. float3 c23 = tex2D(s_p, tc +dx+2.0*dy).xyz;
  149. float3 c33 = tex2D(s_p, tc+2.0*dx+2.0*dy).xyz;
  150.  
  151. // float3 min_sample = min(c11, min(min4(c00, c01, c02, c12), min4(c10, c20, c21, c22)));
  152. // float3 max_sample = max(c11, max(max4(c00, c01, c02, c12), max4(c10, c20, c21, c22)));
  153.  
  154. // float3 min_sample = min(min4(c01, c10, c02, c20), min4(c11, c21, c12, c22));
  155. // float3 max_sample = max(max4(c01, c10, c02, c20), max4(c11, c21, c12, c22));
  156.  
  157. // Get min/max samples
  158. float3 min_sample = min4(c11, c21, c12, c22);
  159. float3 max_sample = max4(c11, c21, c12, c22);
  160.  
  161.  
  162. // float3 min_sample = min4(min4(c00, c01, c02, c03), min4(c10, c11, c12, c13), min4(c20, c21, c22, c23), min4(c30, c31, c32, c33));
  163. // float3 max_sample = max4(max4(c00, c01, c02, c03), max4(c10, c11, c12, c13), max4(c20, c21, c22, c23), max4(c30, c31, c32, c33));
  164.  
  165.  
  166.  
  167. color = mul(weights[0], float4x3(c00, c10, c20, c30));
  168. color+= mul(weights[1], float4x3(c01, c11, c21, c31));
  169. color+= mul(weights[2], float4x3(c02, c12, c22, c32));
  170. color+= mul(weights[3], float4x3(c03, c13, c23, c33));
  171. color = color/(dot(mul(weights, float4(1)), 1));
  172.  
  173. // Anti-ringing
  174. color = clamp(color, min_sample, max_sample);
  175.  
  176. // final sum and weight normalization
  177. return float4(color, 1);
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment