Advertisement
Guest User

EWA-cubic shader

a guest
Jun 7th, 2014
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. #pragma parameter B "B" 0.5 0.0 1.0 0.01
  2. #pragma parameter C "C" 0.5 0.0 1.0 0.01
  3. #pragma parameter First_Switch "First Switch" 1.0 0.0 2.0 0.01
  4. #pragma parameter Second_Switch "Second Switch" 2.0 2.0 3.0 0.01
  5. #ifdef PARAMETER_UNIFORM
  6. uniform float B;
  7. uniform float C;
  8. uniform float First_Switch;
  9. uniform float Second_Switch;
  10. #else
  11. #define B 0.5
  12. #define C 0.5
  13. #define First_Switch 1.0
  14. #define Second_Switch 2.0
  15. #endif
  16. // END PARAMETERS //
  17.  
  18. /* COMPATIBILITY
  19. - HLSL compilers
  20. - Cg compilers
  21. */
  22.  
  23. /*
  24. Hyllian's EWA-cubic with anti-ringing Shader
  25.  
  26. Copyright (C) 2011-2014 Hyllian - sergiogdb@gmail.com
  27.  
  28. This program is free software; you can redistribute it and/or
  29. modify it under the terms of the GNU General Public License
  30. as published by the Free Software Foundation; either version 2
  31. of the License, or (at your option) any later version.
  32.  
  33. This program is distributed in the hope that it will be useful,
  34. but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. GNU General Public License for more details.
  37.  
  38. You should have received a copy of the GNU General Public License
  39. along with this program; if not, write to the Free Software
  40. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  41.  
  42. */
  43.  
  44. // Some known filters use these values:
  45.  
  46. // B = 0.0, C = 0.0 => Hermite cubic filter.
  47. // B = 1.0, C = 0.0 => Cubic B-Spline filter.
  48. // B = 0.0, C = 0.5 => Catmull-Rom Spline filter. This is the default used in this shader.
  49. // B = C = 1.0/3.0 => Mitchell-Netravali cubic filter.
  50. // B = 0.3782, C = 0.3109 => Robidoux filter.
  51. // B = 0.2620, C = 0.3690 => Robidoux Sharp filter.
  52. // B = 0.36, C = 0.28 => My best config for ringing elimination in pixel art (Hyllian).
  53.  
  54. // Change these params to configure the horizontal filter.
  55. //#define B 0.5
  56. //#define C 0.5
  57.  
  58.  
  59. // Calculates the distance between two points
  60. float d(float2 pt1, float2 pt2)
  61. {
  62. float2 v = pt2 - pt1;
  63. return sqrt(dot(v,v));
  64. }
  65.  
  66. float3 min4(float3 a, float3 b, float3 c, float3 d)
  67. {
  68. return min(a, min(b, min(c, d)));
  69. }
  70. float3 max4(float3 a, float3 b, float3 c, float3 d)
  71. {
  72. return max(a, max(b, max(c, d)));
  73. }
  74.  
  75.  
  76. struct input
  77. {
  78. float2 video_size;
  79. float2 texture_size;
  80. float2 output_size;
  81. float frame_count;
  82. float frame_direction;
  83. float frame_rotation;
  84. };
  85.  
  86.  
  87. struct out_vertex {
  88. float4 position : POSITION;
  89. float4 color : COLOR;
  90. float2 texCoord : TEXCOORD0;
  91. };
  92.  
  93. /* VERTEX_SHADER */
  94. out_vertex main_vertex
  95. (
  96. float4 position : POSITION,
  97. float4 color : COLOR,
  98. float2 texCoord1 : TEXCOORD0,
  99.  
  100. uniform float4x4 modelViewProj,
  101. uniform input IN
  102. )
  103. {
  104.  
  105. // This line fix a bug in ATI cards.
  106. float2 tex = texCoord1;
  107.  
  108. out_vertex OUT = {
  109. mul(modelViewProj, position),
  110. color,
  111. tex
  112. };
  113.  
  114. return OUT;
  115. }
  116.  
  117. float4 resampler(float4 x)
  118. {
  119. float4 res, x2, x3;
  120.  
  121. x2 = x*x;
  122. x3 = x2*x;
  123.  
  124. float4 w1 = ((12.0-9.0*B-6.0*C)*x3 + (-18.0+12.0*B+6.0*C)*x2 + float4(6.0 - 2.0*B))/6.0;
  125. float4 w2 = ((-B-6.0*C)*x3 + (6.0*B+30.0*C)*x2 + (-12.0*B-48.0*C)*x + float4(8.0*B + 24.0*C))/6.0;
  126.  
  127. res = (x<float4(First_Switch)) ? w1 : (x<float4(Second_Switch)) ? w2 : float4(0.0);
  128.  
  129. return res;
  130. }
  131.  
  132. float4 main_fragment(in out_vertex VAR, uniform sampler2D s_p : TEXUNIT0, uniform input IN) : COLOR
  133. {
  134. float3 color;
  135. float4x4 weights;
  136.  
  137. float2 dx = float2(1.0, 0.0);
  138. float2 dy = float2(0.0, 1.0);
  139.  
  140. float2 pc = VAR.texCoord*IN.texture_size;
  141.  
  142. float2 tc = (floor(pc-float2(0.5,0.5))+float2(0.5,0.5));
  143.  
  144. weights[0] = resampler(float4(d(pc, tc -dx -dy), d(pc, tc -dy), d(pc, tc +dx -dy), d(pc, tc+2.0*dx -dy)));
  145. weights[1] = resampler(float4(d(pc, tc -dx ), d(pc, tc ), d(pc, tc +dx ), d(pc, tc+2.0*dx )));
  146. weights[2] = resampler(float4(d(pc, tc -dx +dy), d(pc, tc +dy), d(pc, tc +dx +dy), d(pc, tc+2.0*dx +dy)));
  147. weights[3] = resampler(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)));
  148.  
  149. dx = dx/IN.texture_size;
  150. dy = dy/IN.texture_size;
  151. tc = tc/IN.texture_size;
  152.  
  153. // reading the texels
  154.  
  155. float3 c00 = tex2D(s_p, tc -dx -dy).xyz;
  156. float3 c10 = tex2D(s_p, tc -dy).xyz;
  157. float3 c20 = tex2D(s_p, tc +dx -dy).xyz;
  158. float3 c30 = tex2D(s_p, tc+2.0*dx -dy).xyz;
  159. float3 c01 = tex2D(s_p, tc -dx ).xyz;
  160. float3 c11 = tex2D(s_p, tc ).xyz;
  161. float3 c21 = tex2D(s_p, tc +dx ).xyz;
  162. float3 c31 = tex2D(s_p, tc+2.0*dx ).xyz;
  163. float3 c02 = tex2D(s_p, tc -dx +dy).xyz;
  164. float3 c12 = tex2D(s_p, tc +dy).xyz;
  165. float3 c22 = tex2D(s_p, tc +dx +dy).xyz;
  166. float3 c32 = tex2D(s_p, tc+2.0*dx +dy).xyz;
  167. float3 c03 = tex2D(s_p, tc -dx+2.0*dy).xyz;
  168. float3 c13 = tex2D(s_p, tc +2.0*dy).xyz;
  169. float3 c23 = tex2D(s_p, tc +dx+2.0*dy).xyz;
  170. float3 c33 = tex2D(s_p, tc+2.0*dx+2.0*dy).xyz;
  171.  
  172. // Get min/max samples
  173. float3 min_sample = min4(c11, c21, c12, c22);
  174. float3 max_sample = max4(c11, c21, c12, c22);
  175.  
  176. color = mul(weights[0], float4x3(c00, c10, c20, c30));
  177. color+= mul(weights[1], float4x3(c01, c11, c21, c31));
  178. color+= mul(weights[2], float4x3(c02, c12, c22, c32));
  179. color+= mul(weights[3], float4x3(c03, c13, c23, c33));
  180. color = color/(dot(mul(weights, float4(1)), 1)); // weight normalization
  181.  
  182. // Anti-ringing
  183. float3 aux = color;
  184. color = clamp(color, min_sample, max_sample);
  185.  
  186. color = lerp(color, aux, 0.5);
  187.  
  188. return float4(color, 1);
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement