Guest User

ddt-jinc shader

a guest
May 26th, 2014
426
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* COMPATIBILITY
  2. - HLSL compilers
  3. - Cg compilers
  4. */
  5.  
  6. /*
  7. Hyllian's ddt-jinc2-lobe with anti-ringing Shader
  8.  
  9. Copyright (C) 2011-2014 Hyllian/Jararaca - sergiogdb@gmail.com
  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. /*
  28. This is an approximation of Jinc(x)*Jinc(x*r1/r2) for x < 2.5,
  29. where r1 and r2 are the first two zeros of jinc function.
  30. For a jinc 2-lobe best approximation, use A=0.5 and B=0.825.
  31. */
  32.  
  33. // A=0.5, B=0.825 is the best jinc approximation for x<2.5. if B=1.0, it's a lanczos filter.
  34. // Increase A to get more blur. Decrease it to get a sharper picture.
  35. // B = 0.825 to get rid of dithering. Increase B to get a fine sharpness, though dithering returns.
  36. #define A 0.4
  37. #define B 0.9
  38.  
  39. const static float halfpi = 1.5707963267948966192313216916398;
  40. const static float pi = 3.1415926535897932384626433832795;
  41. const static float wa = A*pi;
  42. const static float wb = B*pi;
  43.  
  44.  
  45. const static float3 dtt = float3(65536,255,1);
  46.  
  47. float reduce(float3 color)
  48. {
  49. return dot(color, dtt);
  50. }
  51.  
  52.  
  53. // Calculates the distance between two points
  54. float d(float2 pt1, float2 pt2)
  55. {
  56. float2 v = pt2 - pt1;
  57. return sqrt(dot(v,v));
  58. }
  59.  
  60. float3 min4(float3 a, float3 b, float3 c, float3 d)
  61. {
  62. return min(a, min(b, min(c, d)));
  63. }
  64. float3 max4(float3 a, float3 b, float3 c, float3 d)
  65. {
  66. return max(a, max(b, max(c, d)));
  67. }
  68.  
  69.  
  70. struct input
  71. {
  72. float2 video_size;
  73. float2 texture_size;
  74. float2 output_size;
  75. float frame_count;
  76. float frame_direction;
  77. float frame_rotation;
  78. };
  79.  
  80.  
  81. struct out_vertex {
  82. float4 position : POSITION;
  83. float4 color : COLOR;
  84. float2 texCoord : TEXCOORD0;
  85. };
  86.  
  87. /* VERTEX_SHADER */
  88. out_vertex main_vertex
  89. (
  90. float4 position : POSITION,
  91. float4 color : COLOR,
  92. float2 texCoord1 : TEXCOORD0,
  93.  
  94. uniform float4x4 modelViewProj,
  95. uniform input IN
  96. )
  97. {
  98.  
  99. // This line fix a bug in ATI cards.
  100. float2 tex = texCoord1;
  101.  
  102. out_vertex OUT = {
  103. mul(modelViewProj, position),
  104. color,
  105. tex
  106. };
  107.  
  108. return OUT;
  109. }
  110.  
  111. float4 lanczos(float4 x)
  112. {
  113. float4 res;
  114.  
  115. res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(wa*wb) : sin(x*wa)*sin(x*wb)/(x*x);
  116.  
  117. return res;
  118. }
  119.  
  120. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  121. {
  122. float3 color;
  123. float4x4 weights;
  124.  
  125. float2 dx = float2(1.0, 0.0);
  126. float2 dy = float2(0.0, 1.0);
  127.  
  128. float2 pc = VAR.texCoord*IN.texture_size;
  129.  
  130. float2 tc = (floor(pc-float2(0.5,0.5))+float2(0.5,0.5));
  131.  
  132. float2 pos = frac(pc-float2(0.5,0.5));
  133.  
  134. 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)));
  135. weights[1] = lanczos(float4(d(pc, tc -dx ), d(pc, tc ), d(pc, tc +dx ), d(pc, tc+2.0*dx )));
  136. 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)));
  137. 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)));
  138.  
  139. dx = dx/IN.texture_size;
  140. dy = dy/IN.texture_size;
  141. tc = tc/IN.texture_size;
  142.  
  143. // reading the texels
  144.  
  145. float3 c00 = tex2D(s_p, tc -dx -dy).xyz;
  146. float3 c10 = tex2D(s_p, tc -dy).xyz;
  147. float3 c20 = tex2D(s_p, tc +dx -dy).xyz;
  148. float3 c30 = tex2D(s_p, tc+2.0*dx -dy).xyz;
  149. float3 c01 = tex2D(s_p, tc -dx ).xyz;
  150. float3 c11 = tex2D(s_p, tc ).xyz;
  151. float3 c21 = tex2D(s_p, tc +dx ).xyz;
  152. float3 c31 = tex2D(s_p, tc+2.0*dx ).xyz;
  153. float3 c02 = tex2D(s_p, tc -dx +dy).xyz;
  154. float3 c12 = tex2D(s_p, tc +dy).xyz;
  155. float3 c22 = tex2D(s_p, tc +dx +dy).xyz;
  156. float3 c32 = tex2D(s_p, tc+2.0*dx +dy).xyz;
  157. float3 c03 = tex2D(s_p, tc -dx+2.0*dy).xyz;
  158. float3 c13 = tex2D(s_p, tc +2.0*dy).xyz;
  159. float3 c23 = tex2D(s_p, tc +dx+2.0*dy).xyz;
  160. float3 c33 = tex2D(s_p, tc+2.0*dx+2.0*dy).xyz;
  161.  
  162.  
  163. float a = reduce(c11);
  164. float b = reduce(c21);
  165. float c = reduce(c12);
  166. float d = reduce(c22);
  167.  
  168. float p = abs(pos.x);
  169. float q = abs(pos.y);
  170.  
  171. /*
  172. A B
  173. C D
  174. */
  175. // Get min/max samples
  176. float3 min_sample = min4(c11, c21, c12, c22);
  177. float3 max_sample = max4(c11, c21, c12, c22);
  178.  
  179. if (abs(a-d) < abs(b-c))
  180. {
  181. if (q <= p)
  182. {
  183. c12 = c11 + c22 - c21;
  184. // c01 = c11 + c02 - c12;
  185. // c23 = c13 + c22 - c12;
  186. // c03 = c13 + c02 - c12;
  187. // c01 = c23 = c02 = c13 = c03 = c12;
  188. }
  189. else
  190. {
  191. c21 = c11 + c22 - c12;
  192. // c10 = c11 + c20 - c21;
  193. // c32 = c22 + c31 - c21;
  194. // c30 = c31 + c20 - c21;
  195. // c10 = c32 = c20 = c31 = c30 = c21;
  196. }
  197. }
  198. else if (abs(a-d) > abs(b-c))
  199. {
  200. if ((p+q) < 1.0)
  201. {
  202. c22 = c21 + c12 - c11;
  203. // c31 = c21 + c32 - c22;
  204. // c13 = c23 + c12 - c22;
  205. // c33 = c23 + c32 - c22;
  206. // c13 = c31 = c23 = c32 = c33 = c22;
  207. }
  208. else
  209. {
  210. c11 = c21 + c12 - c22;
  211. // c20 = c21 + c10 - c11;
  212. // c02 = c01 + c12 - c11;
  213. // c00 = c10 + c01 - c11;
  214. // c02 = c20 = c01 = c10 = c00 = c11;
  215. }
  216. }
  217.  
  218.  
  219.  
  220.  
  221. color = mul(weights[0], float4x3(c00, c10, c20, c30));
  222. color+= mul(weights[1], float4x3(c01, c11, c21, c31));
  223. color+= mul(weights[2], float4x3(c02, c12, c22, c32));
  224. color+= mul(weights[3], float4x3(c03, c13, c23, c33));
  225. color = color/(dot(mul(weights, float4(1)), 1));
  226.  
  227. // Anti-ringing
  228. color = clamp(color, min_sample, max_sample);
  229.  
  230. // final sum and weight normalization
  231. return float4(color, 1);
  232.  
  233. }
RAW Paste Data