bilinear_lowres_scale.effect 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * bilinear low res scaling, samples 9 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. uniform float4x4 color_matrix;
  8. uniform float2 base_dimension_i;
  9. sampler_state textureSampler {
  10. Filter = Linear;
  11. AddressU = Clamp;
  12. AddressV = Clamp;
  13. };
  14. struct VertData {
  15. float4 pos : POSITION;
  16. float2 uv : TEXCOORD0;
  17. };
  18. VertData VSDefault(VertData v_in)
  19. {
  20. VertData vert_out;
  21. vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
  22. vert_out.uv = v_in.uv;
  23. return vert_out;
  24. }
  25. float4 pixel(float2 uv)
  26. {
  27. return image.Sample(textureSampler, uv);
  28. }
  29. float4 DrawLowresBilinear(VertData v_in)
  30. {
  31. float2 stepxy = base_dimension_i;
  32. float4 out_color;
  33. out_color = pixel(v_in.uv);
  34. out_color += pixel(v_in.uv + float2(-stepxy.x, -stepxy.y));
  35. out_color += pixel(v_in.uv + float2(-stepxy.x, 0.0));
  36. out_color += pixel(v_in.uv + float2(-stepxy.x, stepxy.y));
  37. out_color += pixel(v_in.uv + float2( 0.0, -stepxy.y));
  38. out_color += pixel(v_in.uv + float2( 0.0, stepxy.y));
  39. out_color += pixel(v_in.uv + float2( stepxy.x, -stepxy.y));
  40. out_color += pixel(v_in.uv + float2( stepxy.x, 0.0));
  41. out_color += pixel(v_in.uv + float2( stepxy.x, stepxy.y));
  42. return out_color / float4(9.0, 9.0, 9.0, 9.0);
  43. }
  44. float4 PSDrawLowresBilinearRGBA(VertData v_in) : TARGET
  45. {
  46. return DrawLowresBilinear(v_in);
  47. }
  48. float4 PSDrawLowresBilinearRGBADivide(VertData v_in) : TARGET
  49. {
  50. float4 rgba = DrawLowresBilinear(v_in);
  51. float alpha = rgba.a;
  52. float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
  53. return float4(rgba.rgb * multiplier, alpha);
  54. }
  55. float4 PSDrawLowresBilinearMatrix(VertData v_in) : TARGET
  56. {
  57. float3 rgb = DrawLowresBilinear(v_in).rgb;
  58. float3 yuv = mul(float4(saturate(rgb), 1.0), color_matrix).xyz;
  59. return float4(yuv, 1.0);
  60. }
  61. technique Draw
  62. {
  63. pass
  64. {
  65. vertex_shader = VSDefault(v_in);
  66. pixel_shader = PSDrawLowresBilinearRGBA(v_in);
  67. }
  68. }
  69. technique DrawAlphaDivide
  70. {
  71. pass
  72. {
  73. vertex_shader = VSDefault(v_in);
  74. pixel_shader = PSDrawLowresBilinearRGBADivide(v_in);
  75. }
  76. }
  77. technique DrawMatrix
  78. {
  79. pass
  80. {
  81. vertex_shader = VSDefault(v_in);
  82. pixel_shader = PSDrawLowresBilinearMatrix(v_in);
  83. }
  84. }