lanczos_scale.effect 3.8 KB

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