bokeh_dof.glsl 6.1 KB

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