lanczos_scale.effect 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 float3 color_range_min = {0.0, 0.0, 0.0};
  10. uniform float3 color_range_max = {1.0, 1.0, 1.0};
  11. uniform float2 base_dimension_i;
  12. sampler_state textureSampler
  13. {
  14. AddressU = Clamp;
  15. AddressV = Clamp;
  16. Filter = Linear;
  17. };
  18. struct VertData {
  19. float4 pos : POSITION;
  20. float2 uv : TEXCOORD0;
  21. };
  22. VertData VSDefault(VertData v_in)
  23. {
  24. VertData vert_out;
  25. vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  26. vert_out.uv = v_in.uv;
  27. return vert_out;
  28. }
  29. float sinc(float x)
  30. {
  31. const float PIval = 3.1415926535897932384626433832795;
  32. return sin(x * PIval) / (x * PIval);
  33. }
  34. float weight(float x, float radius)
  35. {
  36. float ax = abs(x);
  37. if (x == 0.0)
  38. return 1.0;
  39. else if (ax < radius)
  40. return sinc(x) * sinc(x / radius);
  41. else
  42. return 0.0;
  43. }
  44. float3 weight3(float x)
  45. {
  46. return float3(
  47. weight(x * 2.0 + 0.0 * 2.0 - 3.0, 3.0),
  48. weight(x * 2.0 + 1.0 * 2.0 - 3.0, 3.0),
  49. weight(x * 2.0 + 2.0 * 2.0 - 3.0, 3.0));
  50. }
  51. float4 pixel(float xpos, float ypos)
  52. {
  53. return image.Sample(textureSampler, float2(xpos, ypos));
  54. }
  55. float4 get_line(float ypos, float3 xpos1, float3 xpos2, float3 rowtap1,
  56. float3 rowtap2)
  57. {
  58. return
  59. pixel(xpos1.r, ypos) * rowtap1.r +
  60. pixel(xpos1.g, ypos) * rowtap2.r +
  61. pixel(xpos1.b, ypos) * rowtap1.g +
  62. pixel(xpos2.r, ypos) * rowtap2.g +
  63. pixel(xpos2.g, ypos) * rowtap1.b +
  64. pixel(xpos2.b, ypos) * rowtap2.b;
  65. }
  66. float4 DrawLanczos(VertData v_in)
  67. {
  68. float2 stepxy = base_dimension_i;
  69. float2 pos = v_in.uv + stepxy * 0.5;
  70. float2 f = frac(pos / stepxy);
  71. float3 rowtap1 = weight3((1.0 - f.x) / 2.0);
  72. float3 rowtap2 = weight3((1.0 - f.x) / 2.0 + 0.5);
  73. float3 coltap1 = weight3((1.0 - f.y) / 2.0);
  74. float3 coltap2 = weight3((1.0 - f.y) / 2.0 + 0.5);
  75. /* make sure all taps added together is exactly 1.0, otherwise some
  76. * (very small) distortion can occur */
  77. float suml = rowtap1.r + rowtap1.g + rowtap1.b + rowtap2.r + rowtap2.g + rowtap2.b;
  78. float sumc = coltap1.r + coltap1.g + coltap1.b + coltap2.r + coltap2.g + coltap2.b;
  79. rowtap1 /= suml;
  80. rowtap2 /= suml;
  81. coltap1 /= sumc;
  82. coltap2 /= sumc;
  83. float2 xystart = (-2.5 - f) * stepxy + pos;
  84. float3 xpos1 = float3(xystart.x , xystart.x + stepxy.x , xystart.x + stepxy.x * 2.0);
  85. float3 xpos2 = float3(xystart.x + stepxy.x * 3.0, xystart.x + stepxy.x * 4.0, xystart.x + stepxy.x * 5.0);
  86. return
  87. get_line(xystart.y , xpos1, xpos2, rowtap1, rowtap2) * coltap1.r +
  88. get_line(xystart.y + stepxy.y , xpos1, xpos2, rowtap1, rowtap2) * coltap2.r +
  89. get_line(xystart.y + stepxy.y * 2.0, xpos1, xpos2, rowtap1, rowtap2) * coltap1.g +
  90. get_line(xystart.y + stepxy.y * 3.0, xpos1, xpos2, rowtap1, rowtap2) * coltap2.g +
  91. get_line(xystart.y + stepxy.y * 4.0, xpos1, xpos2, rowtap1, rowtap2) * coltap1.b +
  92. get_line(xystart.y + stepxy.y * 5.0, xpos1, xpos2, rowtap1, rowtap2) * coltap2.b;
  93. }
  94. float4 PSDrawLanczosRGBA(VertData v_in) : TARGET
  95. {
  96. return DrawLanczos(v_in);
  97. }
  98. float4 PSDrawLanczosMatrix(VertData v_in) : TARGET
  99. {
  100. float4 rgba = DrawLanczos(v_in);
  101. float4 yuv;
  102. yuv.xyz = clamp(rgba.xyz, color_range_min, color_range_max);
  103. return saturate(mul(float4(yuv.xyz, 1.0), color_matrix));
  104. }
  105. technique Draw
  106. {
  107. pass
  108. {
  109. vertex_shader = VSDefault(v_in);
  110. pixel_shader = PSDrawLanczosRGBA(v_in);
  111. }
  112. }
  113. technique DrawMatrix
  114. {
  115. pass
  116. {
  117. vertex_shader = VSDefault(v_in);
  118. pixel_shader = PSDrawLanczosMatrix(v_in);
  119. }
  120. }