PostProcess.hlsl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. static const float PI = 3.14159265;
  2. float2 Noise(float2 coord)
  3. {
  4. float noiseX = clamp(frac(sin(dot(coord, float2(12.9898, 78.233))) * 43758.5453), 0.0, 1.0);
  5. float noiseY = clamp(frac(sin(dot(coord, float2(12.9898, 78.233) * 2.0)) * 43758.5453), 0.0, 1.0);
  6. return float2(noiseX, noiseY);
  7. }
  8. // Adapted: http://callumhay.blogspot.com/2010/09/gaussian-blur-shader-glsl.html
  9. #ifndef D3D11
  10. float4 GaussianBlur(int blurKernelSize, float2 blurDir, float2 blurRadius, float sigma, sampler2D texSampler, float2 texCoord)
  11. #else
  12. float4 GaussianBlur(int blurKernelSize, float2 blurDir, float2 blurRadius, float sigma, Texture2D tex, SamplerState texSampler, float2 texCoord)
  13. #endif
  14. {
  15. const int blurKernelHalfSize = blurKernelSize / 2;
  16. // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
  17. float3 gaussCoeff;
  18. gaussCoeff.x = 1.0 / (sqrt(2.0 * PI) * sigma);
  19. gaussCoeff.y = exp(-0.5 / (sigma * sigma));
  20. gaussCoeff.z = gaussCoeff.y * gaussCoeff.y;
  21. float2 blurVec = blurRadius * blurDir;
  22. float4 avgValue = float4(0.0, 0.0, 0.0, 0.0);
  23. float gaussCoeffSum = 0.0;
  24. #ifndef D3D11
  25. avgValue += tex2D(texSampler, texCoord) * gaussCoeff.x;
  26. #else
  27. avgValue += tex.Sample(texSampler, texCoord) * gaussCoeff.x;
  28. #endif
  29. gaussCoeffSum += gaussCoeff.x;
  30. gaussCoeff.xy *= gaussCoeff.yz;
  31. for (int i = 1; i <= blurKernelHalfSize; i++)
  32. {
  33. #ifndef D3D11
  34. avgValue += tex2D(texSampler, texCoord - i * blurVec) * gaussCoeff.x;
  35. avgValue += tex2D(texSampler, texCoord + i * blurVec) * gaussCoeff.x;
  36. #else
  37. avgValue += tex.Sample(texSampler, texCoord - i * blurVec) * gaussCoeff.x;
  38. avgValue += tex.Sample(texSampler, texCoord + i * blurVec) * gaussCoeff.x;
  39. #endif
  40. gaussCoeffSum += 2.0 * gaussCoeff.x;
  41. gaussCoeff.xy *= gaussCoeff.yz;
  42. }
  43. return avgValue / gaussCoeffSum;
  44. }
  45. static const float3 LumWeights = float3(0.2126, 0.7152, 0.0722);
  46. float3 ReinhardEq3Tonemap(float3 x)
  47. {
  48. return x / (x + 1.0);
  49. }
  50. float3 ReinhardEq4Tonemap(float3 x, float white)
  51. {
  52. return x * (1.0 + x / white) / (1.0 + x);
  53. }
  54. // Unchared2 tone mapping (See http://filmicgames.com)
  55. static const float A = 0.15;
  56. static const float B = 0.50;
  57. static const float C = 0.10;
  58. static const float D = 0.20;
  59. static const float E = 0.02;
  60. static const float F = 0.30;
  61. float3 Uncharted2Tonemap(float3 x)
  62. {
  63. return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
  64. }
  65. #ifndef D3D11
  66. float3 ColorCorrection(float3 color, sampler3D lut)
  67. #else
  68. float3 ColorCorrection(float3 color, Texture3D lut, SamplerState lutSampler)
  69. #endif
  70. {
  71. float lutSize = 16.0;
  72. float scale = (lutSize - 1.0) / lutSize;
  73. float offset = 1.0 / (2.0 * lutSize);
  74. #ifndef D3D11
  75. return tex3D(lut, clamp(color, 0.0, 1.0) * scale + offset).rgb;
  76. #else
  77. return lut.Sample(lutSampler, clamp(color, 0.0, 1.0) * scale + offset).rgb;
  78. #endif
  79. }
  80. static const float Gamma = 2.2;
  81. static const float InverseGamma = 1.0 / 2.2;
  82. float3 ToGamma(float3 color)
  83. {
  84. return float3(pow(color, Gamma));
  85. }
  86. float3 ToInverseGamma(float3 color)
  87. {
  88. return float3(pow(color, InverseGamma));
  89. }