lanczos_scale.effect 4.7 KB

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