bokeh_dof.glsl 6.1 KB

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