bicubic_scale.effect 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * bicubic sharper (better for downscaling)
  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;
  9. uniform float2 base_dimension_i;
  10. uniform float undistort_factor = 1.0;
  11. sampler_state textureSampler {
  12. Filter = Linear;
  13. AddressU = Clamp;
  14. AddressV = Clamp;
  15. };
  16. struct VertData {
  17. float4 pos : POSITION;
  18. float2 uv : TEXCOORD0;
  19. };
  20. struct VertOut {
  21. float2 uv : TEXCOORD0;
  22. float4 pos : POSITION;
  23. };
  24. struct FragData {
  25. float2 uv : TEXCOORD0;
  26. };
  27. VertOut VSDefault(VertData v_in)
  28. {
  29. VertOut vert_out;
  30. vert_out.uv = v_in.uv * base_dimension;
  31. vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  32. return vert_out;
  33. }
  34. float4 weight4(float x)
  35. {
  36. /* Sharper version. May look better in some cases. B=0, C=0.75 */
  37. return float4(
  38. ((-0.75 * x + 1.5) * x - 0.75) * x,
  39. (1.25 * x - 2.25) * x * x + 1.0,
  40. ((-1.25 * x + 1.5) * x + 0.75) * x,
  41. (0.75 * x - 0.75) * x * x);
  42. }
  43. float AspectUndistortX(float x, float a)
  44. {
  45. // The higher the power, the longer the linear part will be.
  46. return (1.0 - a) * (x * x * x * x * x) + a * x;
  47. }
  48. float AspectUndistortU(float u)
  49. {
  50. // Normalize texture coord to -1.0 to 1.0 range, and back.
  51. return AspectUndistortX((u - 0.5) * 2.0, undistort_factor) * 0.5 + 0.5;
  52. }
  53. float2 undistort_coord(float xpos, float ypos)
  54. {
  55. return float2(AspectUndistortU(xpos), ypos);
  56. }
  57. float4 undistort_pixel(float xpos, float ypos)
  58. {
  59. return image.Sample(textureSampler, undistort_coord(xpos, ypos));
  60. }
  61. float4 undistort_line(float4 xpos, float ypos, float4 rowtaps)
  62. {
  63. return undistort_pixel(xpos.x, ypos) * rowtaps.x +
  64. undistort_pixel(xpos.y, ypos) * rowtaps.y +
  65. undistort_pixel(xpos.z, ypos) * rowtaps.z +
  66. undistort_pixel(xpos.w, ypos) * rowtaps.w;
  67. }
  68. float4 DrawBicubic(FragData f_in, bool undistort)
  69. {
  70. float2 pos = f_in.uv;
  71. float2 pos1 = floor(pos - 0.5) + 0.5;
  72. float2 f = pos - pos1;
  73. float4 rowtaps = weight4(f.x);
  74. float4 coltaps = weight4(f.y);
  75. float2 uv1 = pos1 * base_dimension_i;
  76. float2 uv0 = uv1 - base_dimension_i;
  77. float2 uv2 = uv1 + base_dimension_i;
  78. float2 uv3 = uv2 + base_dimension_i;
  79. if (undistort) {
  80. float4 xpos = float4(uv0.x, uv1.x, uv2.x, uv3.x);
  81. return undistort_line(xpos, uv0.y, rowtaps) * coltaps.x +
  82. undistort_line(xpos, uv1.y, rowtaps) * coltaps.y +
  83. undistort_line(xpos, uv2.y, rowtaps) * coltaps.z +
  84. undistort_line(xpos, uv3.y, rowtaps) * coltaps.w;
  85. }
  86. float u_weight_sum = rowtaps.y + rowtaps.z;
  87. float u_middle_offset = rowtaps.z * base_dimension_i.x / u_weight_sum;
  88. float u_middle = uv1.x + u_middle_offset;
  89. float v_weight_sum = coltaps.y + coltaps.z;
  90. float v_middle_offset = coltaps.z * base_dimension_i.y / v_weight_sum;
  91. float v_middle = uv1.y + v_middle_offset;
  92. int2 coord_top_left = int2(max(uv0 * base_dimension, 0.5));
  93. int2 coord_bottom_right = int2(min(uv3 * base_dimension, base_dimension - 0.5));
  94. float4 top = image.Load(int3(coord_top_left, 0)) * rowtaps.x;
  95. top += image.Sample(textureSampler, float2(u_middle, uv0.y)) * u_weight_sum;
  96. top += image.Load(int3(coord_bottom_right.x, coord_top_left.y, 0)) * rowtaps.w;
  97. float4 total = top * coltaps.x;
  98. float4 middle = image.Sample(textureSampler, float2(uv0.x, v_middle)) * rowtaps.x;
  99. middle += image.Sample(textureSampler, float2(u_middle, v_middle)) * u_weight_sum;
  100. middle += image.Sample(textureSampler, float2(uv3.x, v_middle)) * rowtaps.w;
  101. total += middle * v_weight_sum;
  102. float4 bottom = image.Load(int3(coord_top_left.x, coord_bottom_right.y, 0)) * rowtaps.x;
  103. bottom += image.Sample(textureSampler, float2(u_middle, uv3.y)) * u_weight_sum;
  104. bottom += image.Load(int3(coord_bottom_right, 0)) * rowtaps.w;
  105. total += bottom * coltaps.w;
  106. return total;
  107. }
  108. float4 PSDrawBicubicRGBA(FragData f_in, bool undistort) : TARGET
  109. {
  110. return DrawBicubic(f_in, undistort);
  111. }
  112. float4 PSDrawBicubicRGBADivide(FragData f_in) : TARGET
  113. {
  114. float4 rgba = DrawBicubic(f_in, false);
  115. float alpha = rgba.a;
  116. float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
  117. return float4(rgba.rgb * multiplier, alpha);
  118. }
  119. technique Draw
  120. {
  121. pass
  122. {
  123. vertex_shader = VSDefault(v_in);
  124. pixel_shader = PSDrawBicubicRGBA(f_in, false);
  125. }
  126. }
  127. technique DrawAlphaDivide
  128. {
  129. pass
  130. {
  131. vertex_shader = VSDefault(v_in);
  132. pixel_shader = PSDrawBicubicRGBADivide(f_in);
  133. }
  134. }
  135. technique DrawUndistort
  136. {
  137. pass
  138. {
  139. vertex_shader = VSDefault(v_in);
  140. pixel_shader = PSDrawBicubicRGBA(f_in, true);
  141. }
  142. }