lanczos_scale.effect 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * lanczos sharper
  3. * note - this shader is adapted from the GPL bsnes shader, very good stuff
  4. * there.
  5. */
  6. uniform float4x4 ViewProj;
  7. uniform texture2d image;
  8. uniform float4x4 color_matrix;
  9. uniform float2 base_dimension_i;
  10. uniform float undistort_factor = 1.0;
  11. sampler_state textureSampler
  12. {
  13. AddressU = Clamp;
  14. AddressV = Clamp;
  15. Filter = Linear;
  16. };
  17. struct VertData {
  18. float4 pos : POSITION;
  19. float2 uv : TEXCOORD0;
  20. };
  21. struct FragData {
  22. float4 pos : POSITION;
  23. float2 uv : TEXCOORD0;
  24. float2 scale : TEXCOORD1;
  25. };
  26. FragData VSDefault(VertData v_in)
  27. {
  28. FragData vert_out;
  29. vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  30. vert_out.uv = v_in.uv;
  31. vert_out.scale = min(0.25 + abs(0.75 / mul(float4(1.0 / base_dimension_i.xy, 1.0, 1.0), ViewProj).xy), 1.0);
  32. return vert_out;
  33. }
  34. float sinc(float x)
  35. {
  36. const float PIval = 3.1415926535897932384626433832795;
  37. return sin(x * PIval) / (x * PIval);
  38. }
  39. float weight(float x, float radius)
  40. {
  41. float ax = abs(x);
  42. if (x == 0.0)
  43. return 1.0;
  44. else if (ax < radius)
  45. return sinc(x) * sinc(x / radius);
  46. else
  47. return 0.0;
  48. }
  49. float3 weight3(float x, float scale)
  50. {
  51. return float3(
  52. weight((x * 2.0 + 0.0 * 2.0 - 3.0) * scale, 3.0),
  53. weight((x * 2.0 + 1.0 * 2.0 - 3.0) * scale, 3.0),
  54. weight((x * 2.0 + 2.0 * 2.0 - 3.0) * scale, 3.0));
  55. }
  56. float AspectUndistortX(float x, float a)
  57. {
  58. // The higher the power, the longer the linear part will be.
  59. return (1.0 - a) * (x * x * x * x * x) + a * x;
  60. }
  61. float AspectUndistortU(float u)
  62. {
  63. // Normalize texture coord to -1.0 to 1.0 range, and back.
  64. return AspectUndistortX((u - 0.5) * 2.0, undistort_factor) * 0.5 + 0.5;
  65. }
  66. float2 pixel_coord(float xpos, float ypos)
  67. {
  68. return float2(AspectUndistortU(xpos), ypos);
  69. }
  70. float4 pixel(float xpos, float ypos, bool undistort)
  71. {
  72. if (undistort)
  73. return image.Sample(textureSampler, pixel_coord(xpos, ypos));
  74. else
  75. return image.Sample(textureSampler, float2(xpos, ypos));
  76. }
  77. float4 get_line(float ypos, float3 xpos1, float3 xpos2, float3 rowtap1,
  78. float3 rowtap2, bool undistort)
  79. {
  80. return
  81. pixel(xpos1.r, ypos, undistort) * rowtap1.r +
  82. pixel(xpos1.g, ypos, undistort) * rowtap2.r +
  83. pixel(xpos1.b, ypos, undistort) * rowtap1.g +
  84. pixel(xpos2.r, ypos, undistort) * rowtap2.g +
  85. pixel(xpos2.g, ypos, undistort) * rowtap1.b +
  86. pixel(xpos2.b, ypos, undistort) * rowtap2.b;
  87. }
  88. float4 DrawLanczos(FragData v_in, bool undistort)
  89. {
  90. float2 stepxy = base_dimension_i;
  91. float2 pos = v_in.uv + stepxy * 0.5;
  92. float2 f = frac(pos / stepxy);
  93. float3 rowtap1 = weight3((1.0 - f.x) / 2.0, v_in.scale.x);
  94. float3 rowtap2 = weight3((1.0 - f.x) / 2.0 + 0.5, v_in.scale.x);
  95. float3 coltap1 = weight3((1.0 - f.y) / 2.0, v_in.scale.y);
  96. float3 coltap2 = weight3((1.0 - f.y) / 2.0 + 0.5, v_in.scale.y);
  97. /* make sure all taps added together is exactly 1.0, otherwise some
  98. * (very small) distortion can occur */
  99. float suml = rowtap1.r + rowtap1.g + rowtap1.b + rowtap2.r + rowtap2.g + rowtap2.b;
  100. float sumc = coltap1.r + coltap1.g + coltap1.b + coltap2.r + coltap2.g + coltap2.b;
  101. rowtap1 /= suml;
  102. rowtap2 /= suml;
  103. coltap1 /= sumc;
  104. coltap2 /= sumc;
  105. float2 xystart = (-2.5 - f) * stepxy + pos;
  106. float3 xpos1 = float3(xystart.x , xystart.x + stepxy.x , xystart.x + stepxy.x * 2.0);
  107. float3 xpos2 = float3(xystart.x + stepxy.x * 3.0, xystart.x + stepxy.x * 4.0, xystart.x + stepxy.x * 5.0);
  108. return
  109. get_line(xystart.y , xpos1, xpos2, rowtap1, rowtap2, undistort) * coltap1.r +
  110. get_line(xystart.y + stepxy.y , xpos1, xpos2, rowtap1, rowtap2, undistort) * coltap2.r +
  111. get_line(xystart.y + stepxy.y * 2.0, xpos1, xpos2, rowtap1, rowtap2, undistort) * coltap1.g +
  112. get_line(xystart.y + stepxy.y * 3.0, xpos1, xpos2, rowtap1, rowtap2, undistort) * coltap2.g +
  113. get_line(xystart.y + stepxy.y * 4.0, xpos1, xpos2, rowtap1, rowtap2, undistort) * coltap1.b +
  114. get_line(xystart.y + stepxy.y * 5.0, xpos1, xpos2, rowtap1, rowtap2, undistort) * coltap2.b;
  115. }
  116. float4 PSDrawLanczosRGBA(FragData v_in, bool undistort) : TARGET
  117. {
  118. return DrawLanczos(v_in, undistort);
  119. }
  120. float4 PSDrawLanczosRGBADivide(FragData v_in) : TARGET
  121. {
  122. float4 rgba = DrawLanczos(v_in, false);
  123. float alpha = rgba.a;
  124. float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
  125. return float4(rgba.rgb * multiplier, alpha);
  126. }
  127. float4 PSDrawLanczosMatrix(FragData v_in) : TARGET
  128. {
  129. float3 rgb = DrawLanczos(v_in, false).rgb;
  130. float3 yuv = mul(float4(saturate(rgb), 1.0), color_matrix).xyz;
  131. return float4(yuv, 1.0);
  132. }
  133. technique Draw
  134. {
  135. pass
  136. {
  137. vertex_shader = VSDefault(v_in);
  138. pixel_shader = PSDrawLanczosRGBA(v_in, false);
  139. }
  140. }
  141. technique DrawAlphaDivide
  142. {
  143. pass
  144. {
  145. vertex_shader = VSDefault(v_in);
  146. pixel_shader = PSDrawLanczosRGBADivide(v_in);
  147. }
  148. }
  149. technique DrawUndistort
  150. {
  151. pass
  152. {
  153. vertex_shader = VSDefault(v_in);
  154. pixel_shader = PSDrawLanczosRGBA(v_in, true);
  155. }
  156. }
  157. technique DrawMatrix
  158. {
  159. pass
  160. {
  161. vertex_shader = VSDefault(v_in);
  162. pixel_shader = PSDrawLanczosMatrix(v_in);
  163. }
  164. }