bilinear_lowres_scale.effect 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "color.effect"
  6. uniform float4x4 ViewProj;
  7. uniform texture2d image;
  8. uniform float multiplier;
  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 f_in)
  30. {
  31. float2 uv = f_in.uv;
  32. float2 stepxy = float2(ddx(uv.x), ddy(uv.y));
  33. float2 stepxy1 = stepxy * 0.0625;
  34. float2 stepxy3 = stepxy * 0.1875;
  35. float2 stepxy5 = stepxy * 0.3125;
  36. float2 stepxy7 = stepxy * 0.4375;
  37. // Simulate Direct3D 8-sample pattern
  38. float4 out_color;
  39. out_color = pixel(uv + float2( stepxy1.x, -stepxy3.y));
  40. out_color += pixel(uv + float2(-stepxy1.x, stepxy3.y));
  41. out_color += pixel(uv + float2( stepxy5.x, stepxy1.y));
  42. out_color += pixel(uv + float2(-stepxy3.x, -stepxy5.y));
  43. out_color += pixel(uv + float2(-stepxy5.x, stepxy5.y));
  44. out_color += pixel(uv + float2(-stepxy7.x, -stepxy1.y));
  45. out_color += pixel(uv + float2( stepxy3.x, stepxy7.y));
  46. out_color += pixel(uv + float2( stepxy7.x, -stepxy7.y));
  47. return out_color * 0.125;
  48. }
  49. float4 PSDrawLowresBilinearRGBA(VertData f_in) : TARGET
  50. {
  51. return DrawLowresBilinear(f_in);
  52. }
  53. float4 PSDrawLowresBilinearRGBAMultiply(VertData f_in) : TARGET
  54. {
  55. float4 rgba = DrawLowresBilinear(f_in);
  56. rgba.rgb *= multiplier;
  57. return rgba;
  58. }
  59. float4 PSDrawLowresBilinearRGBATonemap(VertData f_in) : TARGET
  60. {
  61. float4 rgba = DrawLowresBilinear(f_in);
  62. rgba.rgb = rec709_to_rec2020(rgba.rgb);
  63. rgba.rgb = reinhard(rgba.rgb);
  64. rgba.rgb = rec2020_to_rec709(rgba.rgb);
  65. return rgba;
  66. }
  67. float4 PSDrawLowresBilinearRGBAMultiplyTonemap(VertData f_in) : TARGET
  68. {
  69. float4 rgba = DrawLowresBilinear(f_in);
  70. rgba.rgb *= multiplier;
  71. rgba.rgb = rec709_to_rec2020(rgba.rgb);
  72. rgba.rgb = reinhard(rgba.rgb);
  73. rgba.rgb = rec2020_to_rec709(rgba.rgb);
  74. return rgba;
  75. }
  76. float4 PSDrawLowresBilinearRGBADivide(VertData f_in) : TARGET
  77. {
  78. float4 rgba = DrawLowresBilinear(f_in);
  79. rgba.rgb *= max(1. / rgba.a, 0.);
  80. return rgba;
  81. }
  82. technique Draw
  83. {
  84. pass
  85. {
  86. vertex_shader = VSDefault(v_in);
  87. pixel_shader = PSDrawLowresBilinearRGBA(f_in);
  88. }
  89. }
  90. technique DrawMultiply
  91. {
  92. pass
  93. {
  94. vertex_shader = VSDefault(v_in);
  95. pixel_shader = PSDrawLowresBilinearRGBAMultiply(f_in);
  96. }
  97. }
  98. technique DrawTonemap
  99. {
  100. pass
  101. {
  102. vertex_shader = VSDefault(v_in);
  103. pixel_shader = PSDrawLowresBilinearRGBATonemap(f_in);
  104. }
  105. }
  106. technique DrawMultiplyTonemap
  107. {
  108. pass
  109. {
  110. vertex_shader = VSDefault(v_in);
  111. pixel_shader = PSDrawLowresBilinearRGBAMultiplyTonemap(f_in);
  112. }
  113. }
  114. technique DrawAlphaDivide
  115. {
  116. pass
  117. {
  118. vertex_shader = VSDefault(v_in);
  119. pixel_shader = PSDrawLowresBilinearRGBADivide(f_in);
  120. }
  121. }