Advertisement
Guest User

lanczos2-sharp

a guest
May 6th, 2014
1,354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 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 - 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. 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. struct input
  38. {
  39. float2 video_size;
  40. float2 texture_size;
  41. float2 output_size;
  42. float frame_count;
  43. float frame_direction;
  44. float frame_rotation;
  45. };
  46.  
  47.  
  48. struct out_vertex {
  49. float4 position : POSITION;
  50. float4 color : COLOR;
  51. float2 texCoord : TEXCOORD0;
  52. };
  53.  
  54. /* VERTEX_SHADER */
  55. out_vertex main_vertex
  56. (
  57. float4 position : POSITION,
  58. float4 color : COLOR,
  59. float2 texCoord1 : TEXCOORD0,
  60.  
  61. uniform float4x4 modelViewProj,
  62. uniform input IN
  63. )
  64. {
  65.  
  66. // This line fix a bug in ATI cards.
  67. float2 tex = texCoord1;
  68.  
  69. out_vertex OUT = {
  70. mul(modelViewProj, position),
  71. color,
  72. tex
  73. };
  74.  
  75. return OUT;
  76. }
  77.  
  78.  
  79. float4 lanczos(float4 x)
  80. {
  81. float4 res;
  82.  
  83. res = (x==float4(0.0, 0.0, 0.0, 0.0)) ? float4(pi*floatpi) : sin(x*floatpi)*sin(x*pi)/(x*x);
  84.  
  85. return res;
  86. }
  87.  
  88. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  89. {
  90. float3 color;
  91. float4x4 weights;
  92.  
  93. float2 dx = float2(1.0, 0.0);
  94. float2 dy = float2(0.0, 1.0);
  95.  
  96. float2 pc = VAR.texCoord*IN.texture_size;
  97.  
  98. float2 tc = (floor(pc-float2(0.5,0.5))+float2(0.5,0.5));
  99.  
  100. weights[0] = lanczos(float4( 1.0, d(pc, tc -dy), d(pc, tc +dx -dy), 1.0));
  101. weights[1] = lanczos(float4(d(pc, tc -dx ), d(pc, tc ), d(pc, tc +dx ), d(pc, tc+2.0*dx )));
  102. 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)));
  103. weights[3] = lanczos(float4( 1.0, d(pc, tc +2.0*dy), d(pc, tc +dx+2.0*dy), 1.0));
  104.  
  105. // weights[0].x = weights[0].w = weights[3].x = weights[3].w = 0.0;
  106.  
  107. dx = dx/IN.texture_size;
  108. dy = dy/IN.texture_size;
  109. tc = tc/IN.texture_size;
  110.  
  111. // reading the texels
  112.  
  113. // float3 c00 = tex2D(s_p, tc -dx -dy).xyz;
  114. float3 c10 = tex2D(s_p, tc -dy).xyz;
  115. float3 c20 = tex2D(s_p, tc +dx -dy).xyz;
  116. // float3 c30 = tex2D(s_p, tc+2.0*dx -dy).xyz;
  117. float3 c01 = tex2D(s_p, tc -dx ).xyz;
  118. float3 c11 = tex2D(s_p, tc ).xyz;
  119. float3 c21 = tex2D(s_p, tc +dx ).xyz;
  120. float3 c31 = tex2D(s_p, tc+2.0*dx ).xyz;
  121. float3 c02 = tex2D(s_p, tc -dx +dy).xyz;
  122. float3 c12 = tex2D(s_p, tc +dy).xyz;
  123. float3 c22 = tex2D(s_p, tc +dx +dy).xyz;
  124. float3 c32 = tex2D(s_p, tc+2.0*dx +dy).xyz;
  125. // float3 c03 = tex2D(s_p, tc -dx+2.0*dy).xyz;
  126. float3 c13 = tex2D(s_p, tc +2.0*dy).xyz;
  127. float3 c23 = tex2D(s_p, tc +dx+2.0*dy).xyz;
  128. // float3 c33 = tex2D(s_p, tc+2.0*dx+2.0*dy).xyz;
  129.  
  130. float3 o = float3(0.0);
  131.  
  132. color = mul(weights[0], float4x3( o, c10, c20, o));
  133. color+= mul(weights[1], float4x3(c01, c11, c21, c31));
  134. color+= mul(weights[2], float4x3(c02, c12, c22, c32));
  135. color+= mul(weights[3], float4x3( o, c13, c23, o));
  136.  
  137. // final sum and weight normalization
  138. return float4(color/(dot(mul(weights, float4(1)), 1)), 1);
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement