bilinear_lowres_scale.effect 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * bilinear low res scaling, samples 8 pixels of a larger image to scale to a
  3. * low resolution image below half size
  4. */
  5. uniform float4x4 ViewProj;
  6. uniform texture2d image;
  7. sampler_state textureSampler {
  8. Filter = Linear;
  9. AddressU = Clamp;
  10. AddressV = Clamp;
  11. };
  12. struct VertData {
  13. float4 pos : POSITION;
  14. float2 uv : TEXCOORD0;
  15. };
  16. VertData VSDefault(VertData v_in)
  17. {
  18. VertData vert_out;
  19. vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  20. vert_out.uv = v_in.uv;
  21. return vert_out;
  22. }
  23. float4 pixel(float2 uv)
  24. {
  25. return image.Sample(textureSampler, uv);
  26. }
  27. float4 DrawLowresBilinear(VertData v_in)
  28. {
  29. float2 uv = v_in.uv;
  30. float2 stepxy = float2(ddx(uv.x), ddy(uv.y));
  31. float2 stepxy1 = stepxy * 0.0625;
  32. float2 stepxy3 = stepxy * 0.1875;
  33. float2 stepxy5 = stepxy * 0.3125;
  34. float2 stepxy7 = stepxy * 0.4375;
  35. // Simulate Direct3D 8-sample pattern
  36. float4 out_color;
  37. out_color = pixel(uv + float2( stepxy1.x, -stepxy3.y));
  38. out_color += pixel(uv + float2(-stepxy1.x, stepxy3.y));
  39. out_color += pixel(uv + float2( stepxy5.x, stepxy1.y));
  40. out_color += pixel(uv + float2(-stepxy3.x, -stepxy5.y));
  41. out_color += pixel(uv + float2(-stepxy5.x, stepxy5.y));
  42. out_color += pixel(uv + float2(-stepxy7.x, -stepxy1.y));
  43. out_color += pixel(uv + float2( stepxy3.x, stepxy7.y));
  44. out_color += pixel(uv + float2( stepxy7.x, -stepxy7.y));
  45. return out_color * 0.125;
  46. }
  47. float4 PSDrawLowresBilinearRGBA(VertData v_in) : TARGET
  48. {
  49. return DrawLowresBilinear(v_in);
  50. }
  51. float4 PSDrawLowresBilinearRGBADivide(VertData v_in) : TARGET
  52. {
  53. float4 rgba = DrawLowresBilinear(v_in);
  54. float alpha = rgba.a;
  55. float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
  56. return float4(rgba.rgb * multiplier, alpha);
  57. }
  58. technique Draw
  59. {
  60. pass
  61. {
  62. vertex_shader = VSDefault(v_in);
  63. pixel_shader = PSDrawLowresBilinearRGBA(v_in);
  64. }
  65. }
  66. technique DrawAlphaDivide
  67. {
  68. pass
  69. {
  70. vertex_shader = VSDefault(v_in);
  71. pixel_shader = PSDrawLowresBilinearRGBADivide(v_in);
  72. }
  73. }