NearDepthUpscale.frag.glsl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "shaders/Common.glsl"
  6. #include "shaders/Functions.glsl"
  7. layout(location = 0) in vec2 in_uv;
  8. layout(location = 0) out vec4 out_color;
  9. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_depthFullTex;
  10. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_depthHalfTex;
  11. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler2D u_colorTexNearest;
  12. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2D u_colorTexLinear;
  13. #if SSAO_ENABLED
  14. layout(ANKI_TEX_BINDING(0, 4)) uniform sampler2D u_ssaoTex;
  15. #endif
  16. layout(ANKI_UBO_BINDING(0, 0)) uniform _u0
  17. {
  18. vec4 u_linearizePad2;
  19. };
  20. const float DEPTH_THRESHOLD = 1.0 / 1000.0;
  21. const vec2 COLOR_TEX_TEXEL_SIZE = 1.0 / vec2(TEXTURE_WIDTH, TEXTURE_HEIGHT);
  22. const vec2 OFFSETS[8] = vec2[](vec2(-COLOR_TEX_TEXEL_SIZE.x, -COLOR_TEX_TEXEL_SIZE.y),
  23. vec2(0.0, -COLOR_TEX_TEXEL_SIZE.y),
  24. vec2(COLOR_TEX_TEXEL_SIZE.x, -COLOR_TEX_TEXEL_SIZE.y),
  25. vec2(COLOR_TEX_TEXEL_SIZE.x, 0.0),
  26. vec2(COLOR_TEX_TEXEL_SIZE.x, COLOR_TEX_TEXEL_SIZE.y),
  27. vec2(0.0, COLOR_TEX_TEXEL_SIZE.y),
  28. vec2(-COLOR_TEX_TEXEL_SIZE.x, COLOR_TEX_TEXEL_SIZE.y),
  29. vec2(-COLOR_TEX_TEXEL_SIZE.x, 0.0));
  30. void main()
  31. {
  32. // Get the depth of the current fragment
  33. float depth = texture(u_depthFullTex, in_uv).r;
  34. // Gather the depths around the current fragment and:
  35. // - Get the min difference compared to crnt depth
  36. // - Get the new UV that is closer to the crnt depth
  37. float lowDepth = texture(u_depthHalfTex, in_uv).r;
  38. float minDiff = abs(depth - lowDepth);
  39. float maxDiff = minDiff;
  40. vec2 newUv = in_uv;
  41. for(uint i = 0u; i < 8u; ++i)
  42. {
  43. // Read the low depth
  44. vec2 uv = in_uv + OFFSETS[i];
  45. float lowDepth = texture(u_depthHalfTex, uv).r;
  46. // Update the max difference compared to the current fragment
  47. float diff = abs(depth - lowDepth);
  48. maxDiff = max(maxDiff, diff);
  49. // Update the new UV
  50. if(diff < minDiff)
  51. {
  52. minDiff = diff;
  53. newUv = uv;
  54. }
  55. }
  56. // Check for depth discontinuites. Use textureLod because it's undefined if you are sampling mipmaped in a conditional
  57. // branch. See http://teknicool.tumblr.com/post/77263472964/glsl-dynamic-branching-and-texture-samplers
  58. #if 0
  59. float a = u_linearizePad2.x;
  60. float b = u_linearizePad2.y;
  61. float maxDiffLinear = abs(linearizeDepthOptimal(depth + maxDiff, a, b)
  62. - linearizeDepthOptimal(depth, a, b));
  63. #else
  64. float maxDiffLinear = abs(maxDiff);
  65. #endif
  66. vec3 color;
  67. if(maxDiffLinear < DEPTH_THRESHOLD)
  68. {
  69. // No major discontinuites, sample with bilinear
  70. color = textureLod(u_colorTexLinear, in_uv, 0.0).rgb;
  71. }
  72. else
  73. {
  74. // Some discontinuites, need to use the newUv
  75. color = textureLod(u_colorTexNearest, newUv, 0.0).rgb;
  76. }
  77. #if SSAO_ENABLED
  78. float ssao = texture(u_ssaoTex, in_uv).r;
  79. ssao = dither(ssao, 16.0);
  80. out_color = vec4(color, ssao);
  81. #else
  82. out_color = vec4(color, 1.0);
  83. #endif
  84. }