blur_raster.glsl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* clang-format off */
  2. #[vertex]
  3. #version 450
  4. #VERSION_DEFINES
  5. #include "blur_raster_inc.glsl"
  6. layout(location = 0) out vec2 uv_interp;
  7. /* clang-format on */
  8. void main() {
  9. vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0));
  10. gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0);
  11. uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0
  12. }
  13. /* clang-format off */
  14. #[fragment]
  15. #version 450
  16. #VERSION_DEFINES
  17. #include "blur_raster_inc.glsl"
  18. layout(location = 0) in vec2 uv_interp;
  19. /* clang-format on */
  20. layout(set = 0, binding = 0) uniform sampler2D source_color;
  21. #ifdef GLOW_USE_AUTO_EXPOSURE
  22. layout(set = 1, binding = 0) uniform sampler2D source_auto_exposure;
  23. #endif
  24. layout(location = 0) out vec4 frag_color;
  25. void main() {
  26. // We do not apply our color scale for our mobile renderer here, we'll leave our colors at half brightness and apply scale in the tonemap raster.
  27. #ifdef MODE_MIPMAP
  28. vec2 pix_size = blur.pixel_size;
  29. vec4 color = texture(source_color, uv_interp + vec2(-0.5, -0.5) * pix_size);
  30. color += texture(source_color, uv_interp + vec2(0.5, -0.5) * pix_size);
  31. color += texture(source_color, uv_interp + vec2(0.5, 0.5) * pix_size);
  32. color += texture(source_color, uv_interp + vec2(-0.5, 0.5) * pix_size);
  33. frag_color = color / 4.0;
  34. #endif
  35. #ifdef MODE_GAUSSIAN_BLUR
  36. // For Gaussian Blur we use 13 taps in a single pass instead of 12 taps over 2 passes.
  37. // This minimizes the number of times we change framebuffers which is very important for mobile.
  38. // Source: http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  39. vec4 A = texture(source_color, uv_interp + blur.pixel_size * vec2(-1.0, -1.0));
  40. vec4 B = texture(source_color, uv_interp + blur.pixel_size * vec2(0.0, -1.0));
  41. vec4 C = texture(source_color, uv_interp + blur.pixel_size * vec2(1.0, -1.0));
  42. vec4 D = texture(source_color, uv_interp + blur.pixel_size * vec2(-0.5, -0.5));
  43. vec4 E = texture(source_color, uv_interp + blur.pixel_size * vec2(0.5, -0.5));
  44. vec4 F = texture(source_color, uv_interp + blur.pixel_size * vec2(-1.0, 0.0));
  45. vec4 G = texture(source_color, uv_interp);
  46. vec4 H = texture(source_color, uv_interp + blur.pixel_size * vec2(1.0, 0.0));
  47. vec4 I = texture(source_color, uv_interp + blur.pixel_size * vec2(-0.5, 0.5));
  48. vec4 J = texture(source_color, uv_interp + blur.pixel_size * vec2(0.5, 0.5));
  49. vec4 K = texture(source_color, uv_interp + blur.pixel_size * vec2(-1.0, 1.0));
  50. vec4 L = texture(source_color, uv_interp + blur.pixel_size * vec2(0.0, 1.0));
  51. vec4 M = texture(source_color, uv_interp + blur.pixel_size * vec2(1.0, 1.0));
  52. float base_weight = 0.5 / 4.0;
  53. float lesser_weight = 0.125 / 4.0;
  54. frag_color = (D + E + I + J) * base_weight;
  55. frag_color += (A + B + G + F) * lesser_weight;
  56. frag_color += (B + C + H + G) * lesser_weight;
  57. frag_color += (F + G + L + K) * lesser_weight;
  58. frag_color += (G + H + M + L) * lesser_weight;
  59. #endif
  60. #ifdef MODE_GAUSSIAN_GLOW
  61. //Glow uses larger sigma 1 for a more rounded blur effect
  62. #define GLOW_ADD(m_ofs, m_mult) \
  63. { \
  64. vec2 ofs = uv_interp + m_ofs * pix_size; \
  65. vec4 c = texture(source_color, ofs) * m_mult; \
  66. if (any(lessThan(ofs, vec2(0.0))) || any(greaterThan(ofs, vec2(1.0)))) { \
  67. c *= 0.0; \
  68. } \
  69. color += c; \
  70. }
  71. if (bool(blur.flags & FLAG_HORIZONTAL)) {
  72. vec2 pix_size = blur.pixel_size;
  73. pix_size *= 0.5; //reading from larger buffer, so use more samples
  74. vec4 color = texture(source_color, uv_interp + vec2(0.0, 0.0) * pix_size) * 0.174938;
  75. GLOW_ADD(vec2(1.0, 0.0), 0.165569);
  76. GLOW_ADD(vec2(2.0, 0.0), 0.140367);
  77. GLOW_ADD(vec2(3.0, 0.0), 0.106595);
  78. GLOW_ADD(vec2(-1.0, 0.0), 0.165569);
  79. GLOW_ADD(vec2(-2.0, 0.0), 0.140367);
  80. GLOW_ADD(vec2(-3.0, 0.0), 0.106595);
  81. // only do this in the horizontal pass, if we also do this in the vertical pass we're doubling up.
  82. color *= blur.glow_strength;
  83. frag_color = color;
  84. } else {
  85. vec2 pix_size = blur.pixel_size;
  86. vec4 color = texture(source_color, uv_interp + vec2(0.0, 0.0) * pix_size) * 0.288713;
  87. GLOW_ADD(vec2(0.0, 1.0), 0.233062);
  88. GLOW_ADD(vec2(0.0, 2.0), 0.122581);
  89. GLOW_ADD(vec2(0.0, -1.0), 0.233062);
  90. GLOW_ADD(vec2(0.0, -2.0), 0.122581);
  91. frag_color = color;
  92. }
  93. #undef GLOW_ADD
  94. if (bool(blur.flags & FLAG_GLOW_FIRST_PASS)) {
  95. // In the first pass bring back to correct color range else we're applying the wrong threshold
  96. // in subsequent passes we can use it as is as we'd just be undoing it right after.
  97. frag_color *= blur.luminance_multiplier;
  98. #ifdef GLOW_USE_AUTO_EXPOSURE
  99. frag_color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / blur.glow_auto_exposure_scale;
  100. #endif
  101. frag_color *= blur.glow_exposure;
  102. float luminance = max(frag_color.r, max(frag_color.g, frag_color.b));
  103. float feedback = max(smoothstep(blur.glow_hdr_threshold, blur.glow_hdr_threshold + blur.glow_hdr_scale, luminance), blur.glow_bloom);
  104. frag_color = min(frag_color * feedback, vec4(blur.glow_luminance_cap)) / blur.luminance_multiplier;
  105. }
  106. #endif // MODE_GAUSSIAN_GLOW
  107. #ifdef MODE_COPY
  108. vec4 color = textureLod(source_color, uv_interp, 0.0);
  109. frag_color = color;
  110. #endif
  111. }