bicubic_scale.effect 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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;
  31. vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  32. return vert_out;
  33. }
  34. float weight(float x)
  35. {
  36. float ax = abs(x);
  37. /* Sharper version. May look better in some cases. B=0, C=0.75 */
  38. if (ax < 2.0) {
  39. float six_i = 1.0 / 6.0;
  40. float x_squared = x * x;
  41. if (ax < 1.0) {
  42. return (x_squared * (7.5 * ax + (-13.5))) * six_i + 1.0;
  43. }
  44. return (x_squared * ((-4.5) * ax + 22.5) + (-36.0) * ax) * six_i + 3.0;
  45. }
  46. return 0.0;
  47. }
  48. float4 weight4(float x)
  49. {
  50. return float4(
  51. weight(x - 2.0),
  52. weight(x - 1.0),
  53. weight(x),
  54. weight(x + 1.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 undistort_coord(float xpos, float ypos)
  67. {
  68. return float2(AspectUndistortU(xpos), ypos);
  69. }
  70. float4 undistort_pixel(float xpos, float ypos)
  71. {
  72. return image.Sample(textureSampler, undistort_coord(xpos, ypos));
  73. }
  74. float4 undistort_line(float4 xpos, float ypos, float4 rowtaps)
  75. {
  76. return undistort_pixel(xpos.x, ypos) * rowtaps.x +
  77. undistort_pixel(xpos.y, ypos) * rowtaps.y +
  78. undistort_pixel(xpos.z, ypos) * rowtaps.z +
  79. undistort_pixel(xpos.w, ypos) * rowtaps.w;
  80. }
  81. float4 DrawBicubic(FragData f_in, bool undistort)
  82. {
  83. float2 stepxy = base_dimension_i;
  84. float2 pos = f_in.uv + stepxy * 0.5;
  85. float2 f = frac(pos * base_dimension);
  86. float4 rowtaps = weight4(1.0 - f.x);
  87. float4 coltaps = weight4(1.0 - f.y);
  88. float2 uv0 = (-1.5 - f) * stepxy + pos;
  89. float2 uv1 = uv0 + stepxy;
  90. float2 uv2 = uv1 + stepxy;
  91. float2 uv3 = uv2 + stepxy;
  92. if (undistort) {
  93. float4 xpos = float4(uv0.x, uv1.x, uv2.x, uv3.x);
  94. return undistort_line(xpos, uv0.y, rowtaps) * coltaps.x +
  95. undistort_line(xpos, uv1.y, rowtaps) * coltaps.y +
  96. undistort_line(xpos, uv2.y, rowtaps) * coltaps.z +
  97. undistort_line(xpos, uv3.y, rowtaps) * coltaps.w;
  98. }
  99. float u_weight_sum = rowtaps.y + rowtaps.z;
  100. float u_middle_offset = rowtaps.z * stepxy.x / u_weight_sum;
  101. float u_middle = uv1.x + u_middle_offset;
  102. float v_weight_sum = coltaps.y + coltaps.z;
  103. float v_middle_offset = coltaps.z * stepxy.y / v_weight_sum;
  104. float v_middle = uv1.y + v_middle_offset;
  105. int2 coord_top_left = int2(max(uv0 * base_dimension, 0.5));
  106. int2 coord_bottom_right = int2(min(uv3 * base_dimension, base_dimension - 0.5));
  107. float4 top = image.Load(int3(coord_top_left, 0)) * rowtaps.x;
  108. top += image.Sample(textureSampler, float2(u_middle, uv0.y)) * u_weight_sum;
  109. top += image.Load(int3(coord_bottom_right.x, coord_top_left.y, 0)) * rowtaps.w;
  110. float4 total = top * coltaps.x;
  111. float4 middle = image.Sample(textureSampler, float2(uv0.x, v_middle)) * rowtaps.x;
  112. middle += image.Sample(textureSampler, float2(u_middle, v_middle)) * u_weight_sum;
  113. middle += image.Sample(textureSampler, float2(uv3.x, v_middle)) * rowtaps.w;
  114. total += middle * v_weight_sum;
  115. float4 bottom = image.Load(int3(coord_top_left.x, coord_bottom_right.y, 0)) * rowtaps.x;
  116. bottom += image.Sample(textureSampler, float2(u_middle, uv3.y)) * u_weight_sum;
  117. bottom += image.Load(int3(coord_bottom_right, 0)) * rowtaps.w;
  118. total += bottom * coltaps.w;
  119. return total;
  120. }
  121. float4 PSDrawBicubicRGBA(FragData f_in, bool undistort) : TARGET
  122. {
  123. return DrawBicubic(f_in, undistort);
  124. }
  125. float4 PSDrawBicubicRGBADivide(FragData f_in) : TARGET
  126. {
  127. float4 rgba = DrawBicubic(f_in, false);
  128. float alpha = rgba.a;
  129. float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
  130. return float4(rgba.rgb * multiplier, alpha);
  131. }
  132. technique Draw
  133. {
  134. pass
  135. {
  136. vertex_shader = VSDefault(v_in);
  137. pixel_shader = PSDrawBicubicRGBA(f_in, false);
  138. }
  139. }
  140. technique DrawAlphaDivide
  141. {
  142. pass
  143. {
  144. vertex_shader = VSDefault(v_in);
  145. pixel_shader = PSDrawBicubicRGBADivide(f_in);
  146. }
  147. }
  148. technique DrawUndistort
  149. {
  150. pass
  151. {
  152. vertex_shader = VSDefault(v_in);
  153. pixel_shader = PSDrawBicubicRGBA(f_in, true);
  154. }
  155. }