area.effect 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. uniform float4x4 ViewProj;
  2. uniform float4x4 color_matrix;
  3. uniform float3 color_range_min = {0.0, 0.0, 0.0};
  4. uniform float3 color_range_max = {1.0, 1.0, 1.0};
  5. uniform float2 base_dimension_i;
  6. uniform texture2d image;
  7. sampler_state def_sampler {
  8. Filter = Linear;
  9. AddressU = Clamp;
  10. AddressV = Clamp;
  11. };
  12. struct VertInOut {
  13. float4 pos : POSITION;
  14. float2 uv : TEXCOORD0;
  15. };
  16. VertInOut VSDefault(VertInOut vert_in)
  17. {
  18. VertInOut vert_out;
  19. vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
  20. vert_out.uv = vert_in.uv;
  21. return vert_out;
  22. }
  23. float4 PSDrawAreaRGBA(VertInOut vert_in) : TARGET
  24. {
  25. float4 totalcolor = float4(0.0, 0.0, 0.0, 0.0);
  26. const float2 uv = vert_in.uv;
  27. const float2 uvdelta = float2(ddx(uv.x), ddy(uv.y));
  28. const float2 uvhalfdelta = 0.5 * uvdelta;
  29. const float2 uvmin = uv - uvhalfdelta;
  30. const float2 uvmax = uv + uvhalfdelta;
  31. const int2 loadindexmin = int2(uvmin / base_dimension_i);
  32. const int2 loadindexmax = int2(uvmax / base_dimension_i);
  33. const float2 targetpos = uv / uvdelta;
  34. const float2 targetposleft = targetpos - 0.5;
  35. const float2 targetposright = targetpos + 0.5;
  36. for (int loadindexy = loadindexmin.y; loadindexy <= loadindexmax.y; ++loadindexy)
  37. {
  38. for (int loadindexx = loadindexmin.x; loadindexx <= loadindexmax.x; ++loadindexx)
  39. {
  40. const float2 loadindex = float2(loadindexx, loadindexy);
  41. const float2 potentialtargetmin = loadindex / uvdelta * base_dimension_i;
  42. const float2 potentialtargetmax = (loadindex + 1.0) / uvdelta * base_dimension_i;
  43. const float2 targetmin = max(potentialtargetmin, targetposleft);
  44. const float2 targetmax = min(potentialtargetmax, targetposright);
  45. const float area = (targetmax.x - targetmin.x) * (targetmax.y - targetmin.y);
  46. const float4 sample = image.SampleLevel(def_sampler, (loadindex + 0.5) * base_dimension_i, 0.0);
  47. totalcolor += area * float4(sample.rgb * sample.a, sample.a);
  48. }
  49. }
  50. return float4(totalcolor.rgb / totalcolor.a, totalcolor.a);
  51. }
  52. float3 ConvertFromYuv(float3 yuv)
  53. {
  54. yuv = clamp(yuv, color_range_min, color_range_max);
  55. return saturate(mul(float4(yuv, 1.0), color_matrix)).rgb;
  56. }
  57. float4 PSDrawAreaMatrix(VertInOut vert_in) : TARGET
  58. {
  59. float3 totalcolor = float3(0.0, 0.0, 0.0);
  60. const float2 uv = vert_in.uv;
  61. const float2 uvdelta = float2(ddx(uv.x), ddy(uv.y));
  62. const float2 uvhalfdelta = 0.5 * uvdelta;
  63. const float2 uvmin = uv - uvhalfdelta;
  64. const float2 uvmax = uv + uvhalfdelta;
  65. const int2 loadindexmin = int2(uvmin / base_dimension_i);
  66. const int2 loadindexmax = int2(uvmax / base_dimension_i);
  67. const float2 targetpos = uv / uvdelta;
  68. const float2 targetposleft = targetpos - 0.5;
  69. const float2 targetposright = targetpos + 0.5;
  70. for (int loadindexy = loadindexmin.y; loadindexy <= loadindexmax.y; ++loadindexy)
  71. {
  72. for (int loadindexx = loadindexmin.x; loadindexx <= loadindexmax.x; ++loadindexx)
  73. {
  74. const float2 loadindex = float2(loadindexx, loadindexy);
  75. const float2 potentialtargetmin = loadindex / uvdelta * base_dimension_i;
  76. const float2 potentialtargetmax = (loadindex + 1.0) / uvdelta * base_dimension_i;
  77. const float2 targetmin = max(potentialtargetmin, targetposleft);
  78. const float2 targetmax = min(potentialtargetmax, targetposright);
  79. const float area = (targetmax.x - targetmin.x) * (targetmax.y - targetmin.y);
  80. const float3 yuv = image.SampleLevel(def_sampler, (loadindex + 0.5) * base_dimension_i, 0.0).xyz;
  81. totalcolor += area * ConvertFromYuv(yuv);
  82. }
  83. }
  84. return float4(totalcolor, 1.0);
  85. }
  86. technique Draw
  87. {
  88. pass
  89. {
  90. vertex_shader = VSDefault(vert_in);
  91. pixel_shader = PSDrawAreaRGBA(vert_in);
  92. }
  93. }
  94. technique DrawMatrix
  95. {
  96. pass
  97. {
  98. vertex_shader = VSDefault(vert_in);
  99. pixel_shader = PSDrawAreaMatrix(vert_in);
  100. }
  101. }