Pps.frag.glsl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2009-2017, 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/Tonemapping.glsl"
  7. #include "shaders/Functions.glsl"
  8. #define BLUE_NOISE 1
  9. layout(ANKI_TEX_BINDING(0, 0)) uniform sampler2D u_isRt;
  10. layout(ANKI_TEX_BINDING(0, 1)) uniform sampler2D u_ppsBloomLfRt;
  11. layout(ANKI_TEX_BINDING(0, 2)) uniform sampler3D u_lut;
  12. layout(ANKI_TEX_BINDING(0, 3)) uniform sampler2DArray u_blueNoise;
  13. #if DBG_ENABLED
  14. layout(ANKI_TEX_BINDING(0, 5)) uniform sampler2D u_dbgRt;
  15. #endif
  16. layout(std140, ANKI_UBO_BINDING(0, 0)) uniform u0_
  17. {
  18. vec4 u_blueNoiseLayerPad3;
  19. };
  20. layout(std140, ANKI_SS_BINDING(0, 0)) readonly buffer s0_
  21. {
  22. vec4 u_averageLuminancePad3;
  23. };
  24. layout(location = 0) in vec2 in_uv;
  25. layout(location = 0) out vec3 out_color;
  26. const vec2 TEX_OFFSET = vec2(1.0 / float(FBO_WIDTH), 1.0 / float(FBO_HEIGHT));
  27. const vec2 KERNEL[8] = vec2[](vec2(TEX_OFFSET.x, TEX_OFFSET.y),
  28. vec2(0.0, TEX_OFFSET.y),
  29. vec2(-TEX_OFFSET.x, TEX_OFFSET.y),
  30. vec2(-TEX_OFFSET.x, 0.0),
  31. vec2(-TEX_OFFSET.x, -TEX_OFFSET.y),
  32. vec2(0.0, -TEX_OFFSET.y),
  33. vec2(TEX_OFFSET.x, -TEX_OFFSET.y),
  34. vec2(TEX_OFFSET.x, 0.0));
  35. vec3 grayScale(in vec3 col)
  36. {
  37. float grey = (col.r + col.g + col.b) * 0.333333333; // aka: / 3.0
  38. return vec3(grey);
  39. }
  40. vec3 saturation(in vec3 col, in float factor)
  41. {
  42. const vec3 lumCoeff = vec3(0.2125, 0.7154, 0.0721);
  43. vec3 intensity = vec3(dot(col, lumCoeff));
  44. return mix(intensity, col, factor);
  45. }
  46. vec3 gammaCorrection(in float gamma, in vec3 col)
  47. {
  48. return pow(col, vec3(1.0 / gamma));
  49. }
  50. vec3 gammaCorrectionRgb(in vec3 gamma, in vec3 col)
  51. {
  52. return pow(col, 1.0 / gamma);
  53. }
  54. vec3 sharpen(in sampler2D tex, in vec2 texCoords)
  55. {
  56. const float sharpenFactor = 0.15;
  57. vec3 col = textureLod(tex, texCoords, 0.0).rgb;
  58. vec3 col2 = textureLod(tex, texCoords + KERNEL[0], 0.0).rgb;
  59. for(int i = 1; i < 8; i++)
  60. {
  61. col2 += textureLod(tex, texCoords + KERNEL[i], 0.0).rgb;
  62. }
  63. col = col * (8.0 * sharpenFactor + 1.0) - sharpenFactor * col2;
  64. return max(col, vec3(EPSILON));
  65. }
  66. vec3 erosion(in sampler2D tex, in vec2 texCoords)
  67. {
  68. vec3 minValue = texture(tex, texCoords, 0.0).rgb;
  69. for(int i = 0; i < 8; i++)
  70. {
  71. vec3 tmpCol = texture(tex, texCoords + KERNEL[i], 0.0).rgb;
  72. minValue = min(tmpCol, minValue);
  73. }
  74. return minValue;
  75. }
  76. vec3 colorGrading(in vec3 color)
  77. {
  78. const vec3 LUT_SCALE = vec3((LUT_SIZE - 1.0) / LUT_SIZE);
  79. const vec3 LUT_OFFSET = vec3(1.0 / (2.0 * LUT_SIZE));
  80. color = min(color, vec3(1.0));
  81. vec3 lutCoords = color * LUT_SCALE + LUT_OFFSET;
  82. return textureLod(u_lut, lutCoords, 0.0).rgb;
  83. }
  84. void main()
  85. {
  86. vec2 uv = in_uv.xy;
  87. #if SHARPEN_ENABLED
  88. out_color = sharpen(u_isRt, uv);
  89. #else
  90. out_color = textureLod(u_isRt, uv, 0.0).rgb;
  91. #endif
  92. out_color = tonemap(out_color, readFirstInvocationARB(u_averageLuminancePad3.x), 0.0);
  93. #if BLOOM_ENABLED
  94. vec3 bloom = textureLod(u_ppsBloomLfRt, uv, 0.0).rgb;
  95. out_color += bloom;
  96. #endif
  97. out_color = colorGrading(out_color);
  98. #if BLUE_NOISE
  99. vec3 blueNoise = textureLod(u_blueNoise, vec3(FB_SIZE / vec2(64.0) * uv, u_blueNoiseLayerPad3.x), 0.0).rgb;
  100. blueNoise = blueNoise * 2.0 - 1.0;
  101. blueNoise = sign(blueNoise) * (1.0 - sqrt(1.0 - abs(blueNoise)));
  102. out_color += blueNoise / 255.0;
  103. #endif
  104. #if 0
  105. {
  106. out_color = vec3(textureLod(u_isRt, uv, 0.0).rgb);
  107. }
  108. #endif
  109. #if DBG_ENABLED
  110. out_color += textureLod(u_dbgRt, uv, 0.0).rgb;
  111. #endif
  112. }