HBAO_Blur.cginc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //----------------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions
  7. // are met:
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright
  11. // notice, this list of conditions and the following disclaimer in the
  12. // documentation and/or other materials provided with the distribution.
  13. // * Neither the name of NVIDIA CORPORATION nor the names of its
  14. // contributors may be used to endorse or promote products derived
  15. // from this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  18. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  21. // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. //----------------------------------------------------------------------------------
  30. #ifndef HBAO_BLUR_INCLUDED
  31. #define HBAO_BLUR_INCLUDED
  32. #include "HBAO_Common.cginc"
  33. #if COLOR_BLEEDING
  34. inline void FetchAoAndDepth(float2 uv, inout half4 ao, inout float depth) {
  35. ao = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, uv);
  36. depth = LinearizeDepth(FetchRawDepth(uv));
  37. }
  38. inline float CrossBilateralWeight(float r, float d, float d0) {
  39. const float BlurSigma = (float)KERNEL_RADIUS * 0.5;
  40. const float BlurFalloff = 1.0 / (2.0*BlurSigma*BlurSigma);
  41. float dz = (d0 - d) * _BlurSharpness;
  42. return exp2(-r*r*BlurFalloff - dz*dz);
  43. }
  44. inline void ProcessSample(float4 ao, float z, float r, float d0, inout half4 totalAO, inout float totalW) {
  45. float w = CrossBilateralWeight(r, d0, z);
  46. totalW += w;
  47. totalAO += w * ao;
  48. }
  49. inline void ProcessRadius(float2 uv0, float2 deltaUV, float d0, inout half4 totalAO, inout float totalW) {
  50. half4 ao;
  51. float z;
  52. float2 uv;
  53. UNITY_UNROLL
  54. for (int r = 1; r <= KERNEL_RADIUS; r++) {
  55. uv = uv0 + r * deltaUV;
  56. FetchAoAndDepth(uv, ao, z);
  57. ProcessSample(ao, z, r, d0, totalAO, totalW);
  58. }
  59. }
  60. inline half4 ComputeBlur(float2 uv0, float2 deltaUV) {
  61. half4 totalAO;
  62. float depth;
  63. FetchAoAndDepth(uv0, totalAO, depth);
  64. float totalW = 1.0;
  65. ProcessRadius(uv0, -deltaUV, depth, totalAO, totalW);
  66. ProcessRadius(uv0, deltaUV, depth, totalAO, totalW);
  67. totalAO /= totalW;
  68. return totalAO;
  69. }
  70. #else
  71. inline void FetchAoAndDepth(float2 uv, inout half ao, inout float2 depth) {
  72. float3 aod = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, uv).rga;
  73. ao = aod.z;
  74. depth = aod.xy;
  75. }
  76. inline float CrossBilateralWeight(float r, float d, float d0) {
  77. const float BlurSigma = (float)KERNEL_RADIUS * 0.5;
  78. const float BlurFalloff = 1.0 / (2.0*BlurSigma*BlurSigma);
  79. float dz = (d0 - d) * _ProjectionParams.z * _BlurSharpness;
  80. return exp2(-r*r*BlurFalloff - dz*dz);
  81. }
  82. inline void ProcessSample(float2 aoz, float r, float d0, inout half totalAO, inout float totalW) {
  83. float w = CrossBilateralWeight(r, d0, aoz.y);
  84. totalW += w;
  85. totalAO += w * aoz.x;
  86. }
  87. inline void ProcessRadius(float2 uv0, float2 deltaUV, float d0, inout half totalAO, inout float totalW) {
  88. half ao;
  89. float z;
  90. float2 d, uv;
  91. UNITY_UNROLL
  92. for (int r = 1; r <= KERNEL_RADIUS; r++) {
  93. uv = uv0 + r * deltaUV;
  94. FetchAoAndDepth(uv, ao, d);
  95. z = DecodeFloatRG(d);
  96. ProcessSample(float2(ao, z), r, d0, totalAO, totalW);
  97. }
  98. }
  99. inline float4 ComputeBlur(float2 uv0, float2 deltaUV) {
  100. half totalAO;
  101. float2 depth;
  102. FetchAoAndDepth(uv0, totalAO, depth);
  103. float d0 = DecodeFloatRG(depth);
  104. float totalW = 1.0;
  105. ProcessRadius(uv0, -deltaUV, d0, totalAO, totalW);
  106. ProcessRadius(uv0, deltaUV, d0, totalAO, totalW);
  107. totalAO /= totalW;
  108. return float4(depth, 1.0, totalAO);
  109. }
  110. #endif // COLOR_BLEEDING
  111. float4 Blur_Frag(Varyings input) : SV_Target
  112. {
  113. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  114. return ComputeBlur(input.uv, _BlurDeltaUV);
  115. }
  116. #endif // HBAO_BLUR_INCLUDED