lanczos_scale.effect 4.5 KB

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