bokeh_dof.glsl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #[compute]
  2. #version 450
  3. #VERSION_DEFINES
  4. #define BLOCK_SIZE 8
  5. layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;
  6. #ifdef MODE_GEN_BLUR_SIZE
  7. layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
  8. layout(set = 1, binding = 0) uniform sampler2D source_depth;
  9. #endif
  10. #if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL) || defined(MODE_BOKEH_CIRCULAR)
  11. layout(set = 1, binding = 0) uniform sampler2D color_texture;
  12. layout(rgba16f, set = 0, binding = 0) uniform restrict writeonly image2D bokeh_image;
  13. #endif
  14. #ifdef MODE_COMPOSITE_BOKEH
  15. layout(rgba16f, set = 0, binding = 0) uniform restrict image2D color_image;
  16. layout(set = 1, binding = 0) uniform sampler2D source_bokeh;
  17. #endif
  18. // based on https://www.shadertoy.com/view/Xd3GDl
  19. #include "bokeh_dof_inc.glsl"
  20. #ifdef MODE_GEN_BLUR_SIZE
  21. float get_depth_at_pos(vec2 uv) {
  22. float depth = textureLod(source_depth, uv, 0.0).x * 2.0 - 1.0;
  23. if (params.orthogonal) {
  24. depth = -(depth * (params.z_far - params.z_near) - (params.z_far + params.z_near)) / 2.0;
  25. } else {
  26. depth = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near + depth * (params.z_far - params.z_near));
  27. }
  28. return depth;
  29. }
  30. float get_blur_size(float depth) {
  31. if (params.blur_near_active && depth < params.blur_near_begin) {
  32. if (params.use_physical_near) {
  33. // Physically-based.
  34. float d = abs(params.blur_near_begin - depth);
  35. return -(d / (params.blur_near_begin - d)) * params.blur_size_near - DEPTH_GAP; // Near blur is negative.
  36. } else {
  37. // Non-physically-based.
  38. return -(1.0 - smoothstep(params.blur_near_end, params.blur_near_begin, depth)) * params.blur_size - DEPTH_GAP; // Near blur is negative.
  39. }
  40. }
  41. if (params.blur_far_active && depth > params.blur_far_begin) {
  42. if (params.use_physical_far) {
  43. // Physically-based.
  44. float d = abs(params.blur_far_begin - depth);
  45. return (d / (params.blur_far_begin + d)) * params.blur_size_far + DEPTH_GAP;
  46. } else {
  47. // Non-physically-based.
  48. return smoothstep(params.blur_far_begin, params.blur_far_end, depth) * params.blur_size + DEPTH_GAP;
  49. }
  50. }
  51. return 0.0;
  52. }
  53. #endif
  54. #if defined(MODE_BOKEH_BOX) || defined(MODE_BOKEH_HEXAGONAL)
  55. vec4 weighted_filter_dir(vec2 dir, vec2 uv, vec2 pixel_size) {
  56. dir *= pixel_size;
  57. vec4 color = texture(color_texture, uv);
  58. vec4 accum = color;
  59. float total = 1.0;
  60. float blur_scale = params.blur_size / float(params.blur_steps);
  61. if (params.use_jitter) {
  62. uv += dir * (hash12n(uv + params.jitter_seed) - 0.5);
  63. }
  64. for (int i = -params.blur_steps; i <= params.blur_steps; i++) {
  65. if (i == 0) {
  66. continue;
  67. }
  68. float radius = float(i) * blur_scale;
  69. vec2 suv = uv + dir * radius;
  70. radius = abs(radius);
  71. vec4 sample_color = texture(color_texture, suv);
  72. float limit;
  73. if (sample_color.a < color.a) {
  74. limit = abs(sample_color.a);
  75. } else {
  76. limit = abs(color.a);
  77. }
  78. limit -= DEPTH_GAP;
  79. float m = smoothstep(radius - 0.5, radius + 0.5, limit);
  80. accum += mix(color, sample_color, m);
  81. total += 1.0;
  82. }
  83. return accum / total;
  84. }
  85. #endif
  86. void main() {
  87. ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
  88. if (any(greaterThan(pos, params.size))) { //too large, do nothing
  89. return;
  90. }
  91. vec2 pixel_size = 1.0 / vec2(params.size);
  92. vec2 uv = vec2(pos) / vec2(params.size);
  93. #ifdef MODE_GEN_BLUR_SIZE
  94. uv += pixel_size * 0.5;
  95. //precompute size in alpha channel
  96. float depth = get_depth_at_pos(uv);
  97. float size = get_blur_size(depth);
  98. vec4 color = imageLoad(color_image, pos);
  99. color.a = size;
  100. imageStore(color_image, pos, color);
  101. #endif
  102. #ifdef MODE_BOKEH_BOX
  103. //pixel_size*=0.5; //resolution is doubled
  104. if (params.second_pass || !params.half_size) {
  105. uv += pixel_size * 0.5; //half pixel to read centers
  106. } else {
  107. uv += pixel_size * 0.25; //half pixel to read centers from full res
  108. }
  109. vec2 dir = (params.second_pass ? vec2(0.0, 1.0) : vec2(1.0, 0.0));
  110. vec4 color = weighted_filter_dir(dir, uv, pixel_size);
  111. imageStore(bokeh_image, pos, color);
  112. #endif
  113. #ifdef MODE_BOKEH_HEXAGONAL
  114. //pixel_size*=0.5; //resolution is doubled
  115. if (params.second_pass || !params.half_size) {
  116. uv += pixel_size * 0.5; //half pixel to read centers
  117. } else {
  118. uv += pixel_size * 0.25; //half pixel to read centers from full res
  119. }
  120. vec2 dir = (params.second_pass ? normalize(vec2(1.0, 0.577350269189626)) : vec2(0.0, 1.0));
  121. vec4 color = weighted_filter_dir(dir, uv, pixel_size);
  122. if (params.second_pass) {
  123. dir = normalize(vec2(-1.0, 0.577350269189626));
  124. vec4 color2 = weighted_filter_dir(dir, uv, pixel_size);
  125. color.rgb = min(color.rgb, color2.rgb);
  126. color.a = (color.a + color2.a) * 0.5;
  127. }
  128. imageStore(bokeh_image, pos, color);
  129. #endif
  130. #ifdef MODE_BOKEH_CIRCULAR
  131. if (params.half_size) {
  132. pixel_size *= 0.5; //resolution is doubled
  133. }
  134. uv += pixel_size * 0.5; //half pixel to read centers
  135. vec4 color = texture(color_texture, uv);
  136. float initial_blur = color.a;
  137. float accum = 1.0;
  138. float radius = params.blur_scale;
  139. for (float ang = 0.0; radius < params.blur_size; ang += GOLDEN_ANGLE) {
  140. vec2 suv = uv + vec2(cos(ang), sin(ang)) * pixel_size * radius;
  141. vec4 sample_color = texture(color_texture, suv);
  142. float sample_size = abs(sample_color.a);
  143. if (sample_color.a > initial_blur) {
  144. sample_size = clamp(sample_size, 0.0, abs(initial_blur) * 2.0);
  145. }
  146. float m = smoothstep(radius - 0.5, radius + 0.5, sample_size);
  147. color += mix(color / accum, sample_color, m);
  148. accum += 1.0;
  149. radius += params.blur_scale / radius;
  150. }
  151. color /= accum;
  152. imageStore(bokeh_image, pos, color);
  153. #endif
  154. #ifdef MODE_COMPOSITE_BOKEH
  155. uv += pixel_size * 0.5;
  156. vec4 color = imageLoad(color_image, pos);
  157. vec4 bokeh = texture(source_bokeh, uv);
  158. float mix_amount;
  159. if (bokeh.a < color.a) {
  160. mix_amount = min(1.0, max(0.0, max(abs(color.a), abs(bokeh.a)) - DEPTH_GAP));
  161. } else {
  162. mix_amount = min(1.0, max(0.0, abs(color.a) - DEPTH_GAP));
  163. }
  164. color.rgb = mix(color.rgb, bokeh.rgb, mix_amount); //blend between hires and lowres
  165. color.a = 0; //reset alpha
  166. imageStore(color_image, pos, color);
  167. #endif
  168. }