PostProcess.hlsl 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. float4 GaussianBlur(int blurKernelSize, float2 blurDir, float2 blurRadius, float sigma, sampler2D texSampler, float2 texCoord)
  10. {
  11. const int blurKernelHalfSize = blurKernelSize / 2;
  12. // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889)
  13. float3 gaussCoeff;
  14. gaussCoeff.x = 1.0 / (sqrt(2.0 * PI) * sigma);
  15. gaussCoeff.y = exp(-0.5 / (sigma * sigma));
  16. gaussCoeff.z = gaussCoeff.y * gaussCoeff.y;
  17. float2 blurVec = blurRadius * blurDir;
  18. float4 avgValue = float4(0.0, 0.0, 0.0, 0.0);
  19. float gaussCoeffSum = 0.0;
  20. avgValue += tex2D(texSampler, texCoord) * gaussCoeff.x;
  21. gaussCoeffSum += gaussCoeff.x;
  22. gaussCoeff.xy *= gaussCoeff.yz;
  23. for (int i = 1; i <= blurKernelHalfSize; i++)
  24. {
  25. avgValue += tex2D(texSampler, texCoord - i * blurVec) * gaussCoeff.x;
  26. avgValue += tex2D(texSampler, texCoord + i * blurVec) * gaussCoeff.x;
  27. gaussCoeffSum += 2.0 * gaussCoeff.x;
  28. gaussCoeff.xy *= gaussCoeff.yz;
  29. }
  30. return avgValue / gaussCoeffSum;
  31. }
  32. static const float3 LumWeights = float3(0.2126, 0.7152, 0.0722);
  33. float3 ReinhardEq3Tonemap(float3 x)
  34. {
  35. return x / (x + 1.0);
  36. }
  37. float3 ReinhardEq4Tonemap(float3 x, float white)
  38. {
  39. return x * (1.0 + x / white) / (1.0 + x);
  40. }
  41. // Unchared2 tone mapping (See http://filmicgames.com)
  42. static const float A = 0.15;
  43. static const float B = 0.50;
  44. static const float C = 0.10;
  45. static const float D = 0.20;
  46. static const float E = 0.02;
  47. static const float F = 0.30;
  48. float3 Uncharted2Tonemap(float3 x)
  49. {
  50. return ((x*(A*x+C*B)+D*E)/(x*(A*x+B)+D*F))-E/F;
  51. }
  52. float3 ColorCorrection(float3 color, sampler3D lut)
  53. {
  54. float lutSize = 16.0;
  55. float scale = (lutSize - 1.0) / lutSize;
  56. float offset = 1.0 / (2.0 * lutSize);
  57. return tex3D(lut, clamp(color, 0.0, 1.0) * scale + offset).rgb;
  58. }
  59. static const float Gamma = 2.2;
  60. static const float InverseGamma = 1.0 / 2.2;
  61. float3 ToGamma(float3 color)
  62. {
  63. return float3(pow(color, Gamma));
  64. }
  65. float3 ToInverseGamma(float3 color)
  66. {
  67. return float3(pow(color, InverseGamma));
  68. }