default.effect 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. uniform float4x4 ViewProj;
  2. uniform texture2d image;
  3. sampler_state def_sampler {
  4. Filter = Linear;
  5. AddressU = Clamp;
  6. AddressV = Clamp;
  7. };
  8. struct VertInOut {
  9. float4 pos : POSITION;
  10. float2 uv : TEXCOORD0;
  11. };
  12. VertInOut VSDefault(VertInOut vert_in)
  13. {
  14. VertInOut vert_out;
  15. vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
  16. vert_out.uv = vert_in.uv;
  17. return vert_out;
  18. }
  19. float4 PSDrawBare(VertInOut vert_in) : TARGET
  20. {
  21. return image.Sample(def_sampler, vert_in.uv);
  22. }
  23. float4 PSDrawAlphaDivide(VertInOut vert_in) : TARGET
  24. {
  25. float4 rgba = image.Sample(def_sampler, vert_in.uv);
  26. float alpha = rgba.a;
  27. float multiplier = (alpha > 0.0) ? (1.0 / alpha) : 0.0;
  28. return float4(rgba.rgb * multiplier, alpha);
  29. }
  30. float srgb_linear_to_nonlinear_channel(float u)
  31. {
  32. return (u <= 0.0031308) ? (12.92 * u) : ((1.055 * pow(u, 1.0 / 2.4)) - 0.055);
  33. }
  34. float3 srgb_linear_to_nonlinear(float3 v)
  35. {
  36. return float3(srgb_linear_to_nonlinear_channel(v.r), srgb_linear_to_nonlinear_channel(v.g), srgb_linear_to_nonlinear_channel(v.b));
  37. }
  38. float srgb_nonlinear_to_linear_channel(float u)
  39. {
  40. return (u <= 0.04045) ? (u / 12.92) : pow((u + 0.055) / 1.055, 2.4);
  41. }
  42. float3 srgb_nonlinear_to_linear(float3 v)
  43. {
  44. return float3(srgb_nonlinear_to_linear_channel(v.r), srgb_nonlinear_to_linear_channel(v.g), srgb_nonlinear_to_linear_channel(v.b));
  45. }
  46. float4 PSDrawNonlinearAlpha(VertInOut vert_in) : TARGET
  47. {
  48. float4 rgba = image.Sample(def_sampler, vert_in.uv);
  49. rgba.rgb = srgb_linear_to_nonlinear(rgba.rgb);
  50. rgba.rgb *= rgba.a;
  51. rgba.rgb = srgb_nonlinear_to_linear(rgba.rgb);
  52. return rgba;
  53. }
  54. float4 PSDrawSrgbDecompress(VertInOut vert_in) : TARGET
  55. {
  56. float4 rgba = image.Sample(def_sampler, vert_in.uv);
  57. rgba.rgb = srgb_nonlinear_to_linear(rgba.rgb);
  58. return rgba;
  59. }
  60. float4 PSDrawSrgbDecompressPremultiplied(VertInOut vert_in) : TARGET
  61. {
  62. float4 rgba = image.Sample(def_sampler, vert_in.uv);
  63. rgba.rgb = max(float3(0.0, 0.0, 0.0), rgba.rgb / rgba.a);
  64. rgba.rgb = srgb_nonlinear_to_linear(rgba.rgb);
  65. return rgba;
  66. }
  67. technique Draw
  68. {
  69. pass
  70. {
  71. vertex_shader = VSDefault(vert_in);
  72. pixel_shader = PSDrawBare(vert_in);
  73. }
  74. }
  75. technique DrawAlphaDivide
  76. {
  77. pass
  78. {
  79. vertex_shader = VSDefault(vert_in);
  80. pixel_shader = PSDrawAlphaDivide(vert_in);
  81. }
  82. }
  83. technique DrawNonlinearAlpha
  84. {
  85. pass
  86. {
  87. vertex_shader = VSDefault(vert_in);
  88. pixel_shader = PSDrawNonlinearAlpha(vert_in);
  89. }
  90. }
  91. technique DrawSrgbDecompress
  92. {
  93. pass
  94. {
  95. vertex_shader = VSDefault(vert_in);
  96. pixel_shader = PSDrawSrgbDecompress(vert_in);
  97. }
  98. }
  99. technique DrawSrgbDecompressPremultiplied
  100. {
  101. pass
  102. {
  103. vertex_shader = VSDefault(vert_in);
  104. pixel_shader = PSDrawSrgbDecompressPremultiplied(vert_in);
  105. }
  106. }